X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSRegexp.java;h=00a6f40a12cecbe06d606a20aeb430351c40c8db;hp=f2034265b84382d77d4497eefa2086c5283b6e97;hb=85c8f402be33df8440511492fb597fba9c2eb45f;hpb=fffcafc33aa4066bdf85da7a32e1a1cdb9db2d6f diff --git a/src/org/ibex/js/JSRegexp.java b/src/org/ibex/js/JSRegexp.java index f203426..00a6f40 100644 --- a/src/org/ibex/js/JSRegexp.java +++ b/src/org/ibex/js/JSRegexp.java @@ -8,18 +8,23 @@ public class JSRegexp extends JS { private boolean global; private RE re; private int lastIndex; + + private JS pattern; + private int flags; - public JSRegexp(Object arg0, Object arg1) throws JSExn { + public JSRegexp(JS arg0, JS arg1) throws JSExn { if(arg0 instanceof JSRegexp) { JSRegexp r = (JSRegexp) arg0; this.global = r.global; this.re = r.re; this.lastIndex = r.lastIndex; + this.pattern = pattern; + this.flags = flags; } else { - String pattern = (String)arg0; + String pattern = JS.toString(arg0); String sFlags = null; int flags = 0; - if(arg1 != null) sFlags = (String)arg1; + if(arg1 != null) sFlags = JS.toString(arg1); if(sFlags == null) sFlags = ""; for(int i=0;i= s.length()) { lastIndex = 0; return null; } REMatch match = re.getMatch(s,start); @@ -50,7 +53,7 @@ public class JSRegexp extends JS { return match == null ? null : matchToExecResult(match,re,s); } case "test": { - String s = (String)a0; + String s = JS.toString(a0); if (!global) return B(re.getMatch(s) != null); int start = global ? lastIndex : 0; if(start < 0 || start >= s.length()) { lastIndex = 0; return null; } @@ -58,14 +61,14 @@ public class JSRegexp extends JS { lastIndex = match != null ? s.length() : match.getEndIndex(); return B(match != null); } - case "toString": return toString(a0); + case "toString": return JS.S(a0.coerceToString()); case "stringMatch": return stringMatch(a0,a1); case "stringSearch": return stringSearch(a0,a1); //#end break; } case 2: { - //#switch(method) + //#jswitch(method) case "stringReplace": return stringReplace(a0, a1,a2); //#end break; @@ -74,84 +77,89 @@ public class JSRegexp extends JS { return super.callMethod(method, a0, a1, a2, rest, nargs); } - public Object get(Object key) throws JSExn { - //#switch(key) + public JS get(JS key) throws JSExn { + //#jswitch(key) case "exec": return METHOD; case "test": return METHOD; case "toString": return METHOD; case "lastIndex": return N(lastIndex); + case "source": return pattern; + case "global": return JS.B(global); + case "ignoreCase": return B(flags & RE.REG_ICASE); + case "multiline": return B(flags & RE.REG_MULTILINE); //#end return super.get(key); } - public void put(Object key, Object value) throws JSExn { - if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue(); + public void put(JS key, JS value) throws JSExn { + if(JS.isString(key)) { + if(JS.toString(key).equals("lastIndex")) { + lastIndex = JS.toInt(value); + return; + } + } super.put(key,value); } - private static Object matchToExecResult(REMatch match, RE re, String s) { + private static JS matchToExecResult(REMatch match, RE re, String s) { try { JS ret = new JS.O(); - ret.put("index", N(match.getStartIndex())); - ret.put("input",s); + ret.put(JS.S("index"), N(match.getStartIndex())); + ret.put(JS.S("input"),JS.S(s)); int n = re.getNumSubs(); - ret.put("length", N(n+1)); - ret.put("0",match.toString()); - for(int i=1;i<=n;i++) ret.put(Integer.toString(i),match.toString(i)); + ret.put(JS.S("length"), N(n+1)); + ret.put(ZERO,JS.S(match.toString())); + for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i))); return ret; } catch (JSExn e) { throw new Error("this should never happen"); } } - public String toString() { - try { - StringBuffer sb = new StringBuffer(); - sb.append('/'); - sb.append(get("source")); - sb.append('/'); - if(global) sb.append('g'); - if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i'); - if(Boolean.TRUE.equals(get("multiline"))) sb.append('m'); - return sb.toString(); - } catch (JSExn e) { - throw new Error("this should never happen"); - } + String coerceToString() { + StringBuffer sb = new StringBuffer(); + sb.append('/'); + sb.append(pattern); + sb.append('/'); + if(global) sb.append('g'); + if((flags & RE.REG_ICASE) != 0) sb.append('i'); + if((flags & RE.REG_MULTILINE) != 0) sb.append('m'); + return sb.toString(); } - public static Object stringMatch(Object o, Object arg0) throws JSExn { - String s = o.toString(); + public static JS stringMatch(JS o, JS arg0) throws JSExn { + String s = JS.toString(o); RE re; JSRegexp regexp = null; if(arg0 instanceof JSRegexp) { regexp = (JSRegexp) arg0; re = regexp.re; } else { - re = newRE(arg0.toString(),0); + re = newRE(JS.toString(arg0),0); } if(regexp == null) { REMatch match = re.getMatch(s); return matchToExecResult(match,re,s); } - if(!regexp.global) return regexp.callMethod("exec", s, null, null, null, 1); + if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1); JSArray ret = new JSArray(); REMatch[] matches = re.getAllMatches(s); - for(int i=0;i 0 ? matches[matches.length-1].getEndIndex() : s.length(); return ret; } - 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); + public static JS stringSearch(JS o, JS arg0) throws JSExn { + String s = JS.toString(o); + RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0); 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(); + public static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn { + String s = JS.toString(o); RE re; JSFunction replaceFunc = null; String replaceString = null; @@ -165,7 +173,7 @@ public class JSRegexp extends JS { if(arg1 instanceof JSFunction) replaceFunc = (JSFunction) arg1; else - replaceString = JS.toString(arg1.toString()); + replaceString = JS.toString(arg1); REMatch[] matches; if(regexp != null && regexp.global) { matches = re.getAllMatches(s); @@ -193,32 +201,32 @@ public class JSRegexp extends JS { if(replaceFunc != null) { int n = (regexp == null ? 0 : re.getNumSubs()); int numArgs = 3 + n; - Object[] rest = new Object[numArgs - 3]; - Object a0 = match.toString(); - Object a1 = null; - Object a2 = null; + JS[] rest = new JS[numArgs - 3]; + JS a0 = JS.S(match.toString()); + JS a1 = null; + JS a2 = null; for(int j=1;j<=n;j++) switch(j) { - case 1: a1 = match.toString(j); break; - case 2: a2 = match.toString(j); break; - default: rest[j - 3] = match.toString(j); break; + case 1: a1 = JS.S(match.toString(j)); break; + case 2: a2 = JS.S(match.toString(j)); break; + default: rest[j - 3] = JS.S(match.toString(j)); break; } switch(numArgs) { case 3: a1 = N(match.getStartIndex()); - a2 = s; + a2 = JS.S(s); break; case 4: a2 = N(match.getStartIndex()); - rest[0] = s; + rest[0] = JS.S(s); break; default: rest[rest.length - 2] = N(match.getStartIndex()); - rest[rest.length - 1] = s; + rest[rest.length - 1] = JS.S(s); } // note: can't perform pausing operations in here - sb.append((String)replaceFunc.call(a0, a1, a2, rest, numArgs)); + sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs))); } else { sb.append(mySubstitute(match,replaceString,s)); @@ -226,7 +234,7 @@ public class JSRegexp extends JS { } int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex(); sb.append(sa,end,sa.length-end); - return sb.toString(); + return JS.S(sb.toString()); } private static String mySubstitute(REMatch match, String s, String source) { @@ -271,7 +279,8 @@ public class JSRegexp extends JS { } - public static Object stringSplit(String s, Object arg0, Object arg1, int nargs) { + public static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn { + String s = JS.toString(s_); int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1); if(limit < 0) limit = Integer.MAX_VALUE; if(limit == 0) return new JSArray(); @@ -286,7 +295,7 @@ public class JSRegexp extends JS { regexp = (JSRegexp) arg0; re = regexp.re; } else { - sep = arg0.toString(); + sep = JS.toString(arg0); } // special case this for speed. additionally, the code below doesn't properly handle @@ -294,7 +303,7 @@ public class JSRegexp extends JS { if(sep != null && sep.length()==0) { int len = s.length(); for(int i=0;i