Permalink
Browse files

Remove IRegexpAdapter and use Java 1.4 Regexp by default.

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@1412 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
1 parent 05818f6 commit 2363f341747cdb1f08d95d5a783bcf90e74f65ac Jan Arne Petersen committed Nov 20, 2004
View
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry excluding="**/CVS/*" kind="src" path="test"/>
- <classpathentry excluding="**/Constants.java" kind="src" path="build/gen-src"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="lib" path="lib/bsf.jar"/>
- <classpathentry kind="lib" path="lib/jakarta-oro-2.0.6.jar"/>
- <classpathentry kind="lib" path="lib/junit.jar"/>
- <classpathentry kind="lib" path="lib/gnu-regexp-1.1.4.jar"/>
- <classpathentry kind="lib" path="lib/ant.jar"/>
- <classpathentry kind="output" path="build/classes"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry excluding="**/CVS/*" kind="src" path="test"/>
+ <classpathentry excluding="**/Constants.java" kind="src" path="build/gen-src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="lib" path="lib/bsf.jar"/>
+ <classpathentry kind="lib" path="lib/jakarta-oro-2.0.6.jar"/>
+ <classpathentry kind="lib" path="lib/junit.jar"/>
+ <classpathentry kind="lib" path="lib/ant.jar"/>
+ <classpathentry kind="output" path="build/classes"/>
</classpath>
View
@@ -19,11 +19,8 @@
<patternset id="java.src.pattern">
<include name="**/*.java"/>
<exclude unless="bsf.present" name="org/jruby/javasupport/bsf/**/*.java"/>
- <exclude unless="oro.present" name="**/ORORegexpAdapter.java"/>
- <exclude unless="jdk1.4+" name="**/JDKRegexpAdapter.java"/>
<exclude unless="jdk1.4+" name="**/XmlAstMarshal.java"/>
<exclude unless="jdk1.4+" name="**/AstPersistenceDelegates.java"/>
- <exclude unless="gnuregexp.present" name="**/GNURegexpAdapter.java"/>
<!-- Removed cglib for now
<exclude unless="cglib.present" name="**/Cglib*.java"/>
-->
@@ -69,12 +66,6 @@
<available property="junit.present"
classname="junit.framework.TestCase"
classpathref="build.classpath"/>
- <available property="oro.present"
- classname="org.apache.oro.text.regex.Perl5Pattern"
- classpathref="build.classpath"/>
- <available property="gnuregexp.present"
- classname="gnu.regexp.RE"
- classpathref="build.classpath"/>
<available property="cglib.present"
classname="net.sf.cglib.reflect.FastClass"
classpathref="build.classpath"/>
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
Binary file not shown.
View
@@ -66,7 +66,6 @@
import org.jruby.runtime.callback.Callback;
import org.jruby.runtime.load.ILoadService;
import org.jruby.runtime.load.LoadServiceFactory;
-import org.jruby.runtime.regexp.IRegexpAdapter;
import org.jruby.util.Asserts;
import java.io.BufferedReader;
@@ -151,9 +150,7 @@
/**
* Create and initialize a new jruby Runtime.
*/
- private Ruby(Class regexpAdapterClass) {
- this.regexpAdapterClass = regexpAdapterClass;
-
+ private Ruby() {
nilObject = RubyObject.nilObject(this);
trueObject = new RubyBoolean(this, true);
falseObject = new RubyBoolean(this, false);
@@ -168,8 +165,7 @@ private Ruby(Class regexpAdapterClass) {
* @return the JRuby runtime
*/
public static Ruby getDefaultInstance(String regexpEngineName) {
- Class regexpAdapterClass = IRegexpAdapter.getAdapter(regexpEngineName);
- Ruby runtime = new Ruby(regexpAdapterClass);
+ Ruby runtime = new Ruby();
runtime.init();
return runtime;
}
@@ -29,14 +29,15 @@
*/
package org.jruby;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import org.jruby.exceptions.ArgumentError;
import org.jruby.exceptions.TypeError;
import org.jruby.parser.ReOptions;
import org.jruby.runtime.CallbackFactory;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.MarshalStream;
-import org.jruby.runtime.regexp.IRegexpAdapter;
-import org.jruby.util.Asserts;
import org.jruby.util.PrintfFormat;
/**
@@ -45,19 +46,11 @@
* @version $Revision$
*/
public class RubyRegexp extends RubyObject implements ReOptions {
- private IRegexpAdapter matcher;
- private String pattern;
- private int options;
+ private Pattern pattern;
private String lang = null;
public RubyRegexp(Ruby runtime) {
super(runtime, runtime.getClass("Regexp"));
- try {
- matcher = (IRegexpAdapter) runtime.getRegexpAdapterClass().newInstance();
- } catch (Exception ex) {
- // can't happen if JRuby is invoked via Main class
- Asserts.notReached("Couldn't create regexp adapter");
- }
}
public static RubyClass createRegexpClass(Ruby runtime) {
@@ -88,19 +81,18 @@ public static RubyClass createRegexpClass(Ruby runtime) {
return regexpClass;
}
- public void initialize(String pat, int opts) {
- pattern = pat;
- options = opts;
+ public void initialize(String regex, int options) {
+ int flags = 0;
if ((options & RE_OPTION_IGNORECASE) > 0) {
- matcher.setCasefold(true);
+ flags |= Pattern.CASE_INSENSITIVE;
}
if ((options & RE_OPTION_EXTENDED) > 0) {
- matcher.setExtended(true);
+ flags |= Pattern.COMMENTS;
}
if ((options & RE_OPTION_MULTILINE) > 0) {
- matcher.setMultiline(true);
+ flags |= Pattern.DOTALL;
}
- matcher.compile(getRuntime(), pattern);
+ pattern = Pattern.compile(regex, flags);
}
public static String quote(String orig) {
@@ -117,13 +109,10 @@ public static String quote(String orig) {
private void recompileIfNeeded() {
checkInitialized();
- if (matcher.getCasefold() && (options & RE_OPTION_IGNORECASE) == 0) {
- initialize(pattern, options);
- }
}
private void checkInitialized() {
- if (matcher == null) {
+ if (pattern == null) {
throw new TypeError(getRuntime(), "uninitialized Regexp");
}
}
@@ -215,8 +204,7 @@ public IRubyObject equal(IRubyObject other) {
return getRuntime().getFalse();
}
- return RubyBoolean.newBoolean(getRuntime(),
- !(matcher.getCasefold() ^ re.matcher.getCasefold()));
+ return RubyBoolean.newBoolean(getRuntime(), pattern.flags() == re.pattern.flags());
}
/** rb_reg_match2
@@ -257,15 +245,15 @@ public IRubyObject match_m(IRubyObject target) {
*/
public RubyString source() {
checkInitialized();
- return RubyString.newString(getRuntime(), pattern);
+ return RubyString.newString(getRuntime(), pattern.pattern());
}
/** rb_reg_casefold_p
*
*/
public RubyBoolean casefold() {
checkInitialized();
- return RubyBoolean.newBoolean(getRuntime(), matcher.getCasefold());
+ return RubyBoolean.newBoolean(getRuntime(), (pattern.flags() & Pattern.CASE_INSENSITIVE) != 0);
}
/** rb_reg_nth_match
@@ -323,16 +311,32 @@ public int search(IRubyObject target, int pos) {
recompileIfNeeded();
// If nothing match then nil will be returned
- IRubyObject result = matcher.search(getRuntime(), str, pos);
+ IRubyObject result = match(str, pos);
getRuntime().getScope().setBackref(result);
// If nothing match then -1 will be returned
return result instanceof RubyMatchData ? ((RubyMatchData) result).matchStartPosition() : -1;
}
public IRubyObject search2(String str) {
- return matcher.search(getRuntime(), str, 0);
+ return match(str, 0);
}
+
+ private IRubyObject match(String target, int startPos) {
+ Matcher matcher = pattern.matcher(target);
+ if (matcher.find(startPos)) {
+ int count = matcher.groupCount() + 1;
+ int[] begin = new int[count];
+ int[] end = new int[count];
+ for (int i = 0; i < count; i++) {
+ begin[i] = matcher.start(i);
+ end[i] = matcher.end(i);
+ }
+ return new RubyMatchData(runtime, target, begin, end);
+ }
+ return runtime.getNil();
+ }
+
/** rb_reg_regsub
*
@@ -392,7 +396,9 @@ public IRubyObject regsub(IRubyObject str, RubyMatchData match) {
// TODO: Could this be better hooked up to RubyObject#clone?
public IRubyObject rbClone() {
- RubyRegexp newObj = newRegexp(source(), options, lang);
+ RubyRegexp newObj = new RubyRegexp(runtime);
+ newObj.pattern = pattern;
+ newObj.lang = lang;
setupClone(newObj);
return newObj;
}
@@ -401,12 +407,13 @@ public IRubyObject rbClone() {
*
*/
public RubyString inspect() {
- final int length = pattern.length();
+ final String regex = pattern.pattern();
+ final int length = regex.length();
StringBuffer sb = new StringBuffer(length + 2);
sb.append('/');
for (int i = 0; i < length; i++) {
- char c = pattern.charAt(i);
+ char c = regex.charAt(i);
if (RubyString.isAlnum(c)) {
sb.append(c);
@@ -434,13 +441,13 @@ public RubyString inspect() {
}
sb.append('/');
- if ((options & RE_OPTION_IGNORECASE) > 0) {
+ if ((pattern.flags() & Pattern.CASE_INSENSITIVE) > 0) {
sb.append('i');
}
- if ((options & RE_OPTION_MULTILINE) > 0) {
+ if ((pattern.flags() & Pattern.DOTALL) > 0) {
sb.append('m');
}
- if ((options & RE_OPTION_EXTENDED) > 0) {
+ if ((pattern.flags() & Pattern.COMMENTS) > 0) {
sb.append('x');
}
@@ -449,7 +456,7 @@ public RubyString inspect() {
public void marshalTo(MarshalStream output) throws java.io.IOException {
output.write('/');
- output.dumpString(pattern);
- output.dumpInt(options);
+ output.dumpString(pattern.pattern());
+ output.dumpInt(pattern.flags());
}
-}
+}
@@ -1,119 +0,0 @@
-/*
- * Copyright (C) 2001 Benoit Cerrina <b.cerrina@wanadoo.fr>
- * Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
- * Copyright (C) 2001 Chad Fowler
- * Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
- *
- * JRuby - http://jruby.sourceforge.net
- *
- * This file is part of JRuby
- *
- * JRuby is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License or
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * JRuby is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License and GNU Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU General Public
- * License and GNU Lesser General Public License along with JRuby;
- * if not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- */
-package org.jruby.runtime.regexp;
-
-import gnu.regexp.RE;
-import gnu.regexp.REException;
-import gnu.regexp.REMatch;
-import org.jruby.Ruby;
-import org.jruby.RubyMatchData;
-import org.jruby.exceptions.RegexpError;
-import org.jruby.runtime.builtin.IRubyObject;
-
-/**
- * Regexp adapter for gnu.regexp.
- */
-public class GNURegexpAdapter extends IRegexpAdapter {
-
- private RE re;
- private int cflags = 0;
- //private int eflags = RE.REG_NOTBOL | RE.REG_NOTEOL;
- private int eflags = 0;
- private boolean extended;
-
- /**
- * Compile the regex.
- */
- public void compile(Ruby runtime, String pattern) throws RegexpError {
- if (extended) {
- pattern = unextend(pattern);
- }
-
- try {
- this.re = new RE(pattern, cflags);
- } catch (REException e) {
- throw new RegexpError(runtime, e.getMessage());
- }
- }
-
- /**
- * Set whether matches should be case-insensitive or not
- */
- public void setCasefold(boolean set) {
- if (set) {
- cflags |= RE.REG_ICASE;
- } else {
- cflags &= ~RE.REG_ICASE;
- }
- }
-
- /**
- * Get whether matches are case-insensitive or not
- */
- public boolean getCasefold() {
- return (cflags & RE.REG_ICASE) > 0;
- }
-
- /**
- * Set whether patterns can contain comments and extra whitespace
- */
- public void setExtended(boolean set) {
- extended = set;
- }
-
- /**
- * Set whether the dot metacharacter should match newlines
- */
- public void setMultiline(boolean set) {
- if (set) {
- cflags |= RE.REG_DOT_NEWLINE;
- } else {
- cflags &= ~RE.REG_DOT_NEWLINE;
- }
- }
-
- /**
- * Does the given argument match the pattern?
- */
- public IRubyObject search(Ruby runtime, String target, int startPos) {
- REMatch match = re.getMatch(target, startPos, eflags);
- if (match != null) {
- int count = re.getNumSubs() + 1;
- int[] begin = new int[count];
- int[] end = new int[count];
- for (int i = 0; i < count; i++) {
- begin[i] = match.getStartIndex(i);
- end[i] = match.getEndIndex(i);
- }
- return new RubyMatchData(runtime, target, begin, end);
- }
- return runtime.getNil();
- }
-}
Oops, something went wrong.

0 comments on commit 2363f34

Please sign in to comment.