added GnuRegexp.java to eliminated dependency on gnu.regexp.jar
[org.ibex.js.git] / src / org / ibex / js / JSRegexp.java
index da22d2e..fb9e67d 100644 (file)
@@ -1,12 +1,10 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.ibex.js;
 
-import gnu.regexp.*;
-
 /** A JavaScript regular expression object */
 public class JSRegexp extends JS {
     private boolean global;
-    private RE re;
+    private GnuRegexp.RE re;
     private int lastIndex;
     
     public JSRegexp(Object arg0, Object arg1) throws JSExn {
@@ -23,8 +21,8 @@ public class JSRegexp extends JS {
             if(sFlags == null) sFlags = "";
             for(int i=0;i<sFlags.length();i++) {
                 switch(sFlags.charAt(i)) {
-                    case 'i': flags |= RE.REG_ICASE; break;
-                    case 'm': flags |= RE.REG_MULTILINE; break;
+                    case 'i': flags |= GnuRegexp.RE.REG_ICASE; break;
+                    case 'm': flags |= GnuRegexp.RE.REG_MULTILINE; break;
                     case 'g': global = true; break;
                     default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
                 }
@@ -32,8 +30,8 @@ public class JSRegexp extends JS {
             re = newRE(pattern,flags);
             put("source", pattern);
             put("global", B(global));
-            put("ignoreCase", B(flags & RE.REG_ICASE));
-            put("multiline", B(flags & RE.REG_MULTILINE));
+            put("ignoreCase", B(flags & GnuRegexp.RE.REG_ICASE));
+            put("multiline", B(flags & GnuRegexp.RE.REG_MULTILINE));
         }
     }
 
@@ -45,7 +43,7 @@ public class JSRegexp extends JS {
                     String s = (String)a0;
                     int start = global ? lastIndex : 0;
                     if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
-                    REMatch match = re.getMatch(s,start);
+                    GnuRegexp.REMatch match = re.getMatch(s,start);
                     if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
                     return match == null ? null : matchToExecResult(match,re,s);
                 }
@@ -54,7 +52,7 @@ public class JSRegexp extends JS {
                     if (!global) return B(re.getMatch(s) != null);
                     int start = global ? lastIndex : 0;
                     if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
-                    REMatch match = re.getMatch(s,start);
+                    GnuRegexp.REMatch match = re.getMatch(s,start);
                     lastIndex = match != null ? s.length() : match.getEndIndex();
                     return B(match != null);
                 }
@@ -89,7 +87,7 @@ public class JSRegexp extends JS {
         super.put(key,value);
     }
   
-    private static Object matchToExecResult(REMatch match, RE re, String s) {
+    private static Object matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) {
         try {
             JS ret = new JS();
             ret.put("index", N(match.getStartIndex()));
@@ -121,7 +119,7 @@ public class JSRegexp extends JS {
     
     public static Object stringMatch(Object o, Object arg0) throws JSExn {
         String s = o.toString();
-        RE re;
+        GnuRegexp.RE re;
         JSRegexp regexp = null;
         if(arg0 instanceof JSRegexp) {
             regexp = (JSRegexp) arg0;
@@ -131,13 +129,13 @@ public class JSRegexp extends JS {
         }
         
         if(regexp == null) {
-            REMatch match = re.getMatch(s);
+            GnuRegexp.REMatch match = re.getMatch(s);
             return matchToExecResult(match,re,s);
         }
         if(!regexp.global) return regexp.callMethod("exec", s, null, null, null, 1);
         
         JSArray ret = new JSArray();
-        REMatch[] matches = re.getAllMatches(s);
+        GnuRegexp.REMatch[] matches = re.getAllMatches(s);
         for(int i=0;i<matches.length;i++) ret.addElement(matches[i].toString());
         regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
         return ret;
@@ -145,14 +143,14 @@ public class JSRegexp extends JS {
     
     public static Object stringSearch(Object o, Object arg0) throws JSExn  {
         String s = o.toString();
-        RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(arg0.toString(),0);
-        REMatch match = re.getMatch(s);
+        GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(arg0.toString(),0);
+        GnuRegexp.REMatch match = re.getMatch(s);
         return match == null ? N(-1) : N(match.getStartIndex());
     }
     
     public static Object stringReplace(Object o, Object arg0, Object arg1) throws JSExn {
         String s = o.toString();
-        RE re;
+        GnuRegexp.RE re;
         JSFunction replaceFunc = null;
         String replaceString = null;
         JSRegexp regexp = null;
@@ -166,7 +164,7 @@ public class JSRegexp extends JS {
             replaceFunc = (JSFunction) arg1;
         else
             replaceString = JS.toString(arg1.toString());
-        REMatch[] matches;
+        GnuRegexp.REMatch[] matches;
         if(regexp != null && regexp.global) {
             matches = re.getAllMatches(s);
             if(regexp != null) {
@@ -176,18 +174,18 @@ public class JSRegexp extends JS {
                     regexp.lastIndex = s.length();
             }
         } else {
-            REMatch match = re.getMatch(s);
+            GnuRegexp.REMatch match = re.getMatch(s);
             if(match != null)
-                matches = new REMatch[]{ match };
+                matches = new GnuRegexp.REMatch[]{ match };
             else
-                matches = new REMatch[0];
+                matches = new GnuRegexp.REMatch[0];
         }
         
         StringBuffer sb = new StringBuffer(s.length());
         int pos = 0;
         char[] sa = s.toCharArray();
         for(int i=0;i<matches.length;i++) {
-            REMatch match = matches[i];
+            GnuRegexp.REMatch match = matches[i];
             sb.append(sa,pos,match.getStartIndex()-pos);
             pos = match.getEndIndex();
             if(replaceFunc != null) {
@@ -229,7 +227,7 @@ public class JSRegexp extends JS {
         return sb.toString();
     }
     
-    private static String mySubstitute(REMatch match, String s, String source) {
+    private static String mySubstitute(GnuRegexp.REMatch match, String s, String source) {
         StringBuffer sb = new StringBuffer();
         int i,n;
         char c,c2;
@@ -276,7 +274,7 @@ public class JSRegexp extends JS {
         if(limit < 0) limit = Integer.MAX_VALUE;
         if(limit == 0) return new JSArray();
         
-        RE re = null;
+        GnuRegexp.RE re = null;
         JSRegexp regexp = null;
         String sep = null;
         JSArray ret = new JSArray();
@@ -300,7 +298,7 @@ public class JSRegexp extends JS {
         
         OUTER: while(p < s.length()) {
             if(re != null) {
-                REMatch m = re.getMatch(s,p);
+                GnuRegexp.REMatch m = re.getMatch(s,p);
                 if(m == null) break OUTER;
                 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
                 ret.addElement(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex()));
@@ -324,10 +322,10 @@ public class JSRegexp extends JS {
         return ret;
     }
     
-    public static RE newRE(String pattern, int flags) throws JSExn {
+    public static GnuRegexp.RE newRE(String pattern, int flags) throws JSExn {
         try {
-            return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
-        } catch(REException e) {
+            return new GnuRegexp.RE(pattern,flags,GnuRegexp.RESyntax.RE_SYNTAX_PERL5);
+        } catch(GnuRegexp.REException e) {
             throw new JSExn(e.toString());
         }
     }