X-Git-Url: http://git.megacz.com/?p=org.ibex.js.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSRegexp.java;h=e7e35f7f96655767e4b43eb6361578bc8498c88d;hp=c7ed118e14e2dcb5e53765b3266bc78f8f77d2aa;hb=f63da7aeac2f942be41aefde91002d14117a8573;hpb=19d66e161db458135518efd3539048f44e1e5622 diff --git a/src/org/ibex/js/JSRegexp.java b/src/org/ibex/js/JSRegexp.java index c7ed118..e7e35f7 100644 --- a/src/org/ibex/js/JSRegexp.java +++ b/src/org/ibex/js/JSRegexp.java @@ -1,8 +1,13 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.js; /** A JavaScript regular expression object */ -public class JSRegexp extends JS { +public class JSRegexp extends JS.Immutable { + private static final JS.Method METHOD = new JS.Method(); + private boolean global; private GnuRegexp.RE re; private int lastIndex; @@ -16,13 +21,13 @@ public class JSRegexp extends JS { this.global = r.global; this.re = r.re; this.lastIndex = r.lastIndex; - this.pattern = pattern; - this.flags = flags; + this.pattern = r.pattern; + this.flags = r.flags; } else { - String pattern = JS.toString(arg0); + String pattern = JSU.toString(arg0); String sFlags = null; int flags = 0; - if(arg1 != null) sFlags = JS.toString(arg1); + if(arg1 != null) sFlags = JSU.toString(arg1); if(sFlags == null) sFlags = ""; for(int i=0;i= s.length()) { lastIndex = 0; return null; } GnuRegexp.REMatch match = re.getMatch(s,start); @@ -51,48 +56,53 @@ public class JSRegexp extends JS { return match == null ? null : matchToExecResult(match,re,s); } case "test": { - String s = JS.toString(a0); - if (!global) return B(re.getMatch(s) != null); + String s = JSU.toString(args[0]); + if (!global) return JSU.B(re.getMatch(s) != null); int start = global ? lastIndex : 0; if(start < 0 || start >= s.length()) { lastIndex = 0; return null; } GnuRegexp.REMatch match = re.getMatch(s,start); lastIndex = match != null ? s.length() : match.getEndIndex(); - return B(match != null); + return JSU.B(match != null); } - case "toString": return JS.S(a0.coerceToString()); - case "stringMatch": return stringMatch(a0,a1); - case "stringSearch": return stringSearch(a0,a1); + case "toString": return JSU.S(args[0].coerceToString()); //#end break; } case 2: { - //#switch(JS.toString(method)) - case "stringReplace": return stringReplace(a0, a1,a2); + //#switch(JSU.str(method)) + case "stringMatch": return stringMatch(args[0], args[1]); + case "stringSearch": return stringSearch(args[0], args[1]); + //#end + break; + } + case 3: { + //#switch(JSU.str(method)) + case "stringReplace": return stringReplace(args[0], args[1], args[2]); //#end break; } } - return super.callMethod(method, a0, a1, a2, rest, nargs); + return super.call(method, args); } public JS get(JS key) throws JSExn { - //#switch(JS.toString(key)) + //#switch(JSU.str(key)) case "exec": return METHOD; case "test": return METHOD; case "toString": return METHOD; - case "lastIndex": return N(lastIndex); + case "lastIndex": return JSU.N(lastIndex); case "source": return pattern; - case "global": return JS.B(global); - case "ignoreCase": return B(flags & GnuRegexp.RE.REG_ICASE); - case "multiline": return B(flags & GnuRegexp.RE.REG_MULTILINE); + case "global": return JSU.B(global); + case "ignoreCase": return JSU.B(flags & GnuRegexp.RE.REG_ICASE); + case "multiline": return JSU.B(flags & GnuRegexp.RE.REG_MULTILINE); //#end return super.get(key); } public void put(JS key, JS value) throws JSExn { - if(JS.isString(key)) { - if(JS.toString(key).equals("lastIndex")) { - lastIndex = JS.toInt(value); + if(JSU.isString(key)) { + if(JSU.toString(key).equals("lastIndex")) { + lastIndex = JSU.toInt(value); return; } } @@ -100,21 +110,22 @@ public class JSRegexp extends JS { } private static JS matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) { + if (match == null) return null; try { - JS ret = new JS.O(); - ret.put(JS.S("index"), N(match.getStartIndex())); - ret.put(JS.S("input"),JS.S(s)); + JS ret = new JS.Obj(); + ret.put(JSU.S("index"), JSU.N(match.getStartIndex())); + ret.put(JSU.S("input"), JSU.S(s)); int n = re.getNumSubs(); - 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))); + ret.put(JSU.S("length"), JSU.N(n+1)); + ret.put(JSU.ZERO, JSU.S(match.toString())); + for(int i=1;i<=n;i++) ret.put(JSU.N(i),JSU.S(match.toString(i))); return ret; } catch (JSExn e) { throw new Error("this should never happen"); } } - String coerceToString() { + public String coerceToString() { StringBuffer sb = new StringBuffer(); sb.append('/'); sb.append(pattern); @@ -125,39 +136,43 @@ public class JSRegexp extends JS { return sb.toString(); } + private static final JS[] execarg = new JS[1]; static JS stringMatch(JS o, JS arg0) throws JSExn { - String s = JS.toString(o); + String s = JSU.toString(o); GnuRegexp.RE re; JSRegexp regexp = null; if(arg0 instanceof JSRegexp) { regexp = (JSRegexp) arg0; re = regexp.re; } else { - re = newRE(JS.toString(arg0),0); + re = newRE(JSU.toString(arg0),0); } if(regexp == null) { GnuRegexp.REMatch match = re.getMatch(s); return matchToExecResult(match,re,s); } - if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1); - - JSArray ret = new JSArray(); + try { + execarg[0] = o; + if(!regexp.global) return regexp.call(JSU.S("exec"), execarg); + } finally { execarg[0] = null; } + GnuRegexp.REMatch[] matches = re.getAllMatches(s); - for(int i=0;i 0 ? matches[matches.length-1].getEndIndex() : s.length(); return ret; } static JS stringSearch(JS o, JS arg0) throws JSExn { - String s = JS.toString(o); - GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0); + String s = JSU.toString(o); + GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JSU.toString(arg0),0); GnuRegexp.REMatch match = re.getMatch(s); - return match == null ? N(-1) : N(match.getStartIndex()); + return match == null ? JSU.N(-1) : JSU.N(match.getStartIndex()); } static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn { - String s = JS.toString(o); + String s = JSU.toString(o); GnuRegexp.RE re; JSFunction replaceFunc = null; String replaceString = null; @@ -171,7 +186,7 @@ public class JSRegexp extends JS { if(arg1 instanceof JSFunction) replaceFunc = (JSFunction) arg1; else - replaceString = JS.toString(arg1); + replaceString = JSU.toString(arg1); GnuRegexp.REMatch[] matches; if(regexp != null && regexp.global) { matches = re.getAllMatches(s); @@ -199,32 +214,16 @@ public class JSRegexp extends JS { if(replaceFunc != null) { int n = (regexp == null ? 0 : re.getNumSubs()); int numArgs = 3 + n; - 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 = 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 = JS.S(s); - break; - case 4: - a2 = N(match.getStartIndex()); - rest[0] = JS.S(s); - break; - default: - rest[rest.length - 2] = N(match.getStartIndex()); - rest[rest.length - 1] = JS.S(s); - } + JS[] args = new JS[3 + n]; + args[0] = JSU.S(match.toString()); + args[1] = null; + args[2] = null; + for(int j=1;j<=n;j++) args[j] = JSU.S(match.toString(j)); + args[args.length - 2] = JSU.N(match.getStartIndex()); + args[args.length - 1] = JSU.S(s); // note: can't perform pausing operations in here - sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs))); + sb.append(JSU.toString(replaceFunc.call(null, args))); } else { sb.append(mySubstitute(match,replaceString,s)); @@ -232,7 +231,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 JS.S(sb.toString()); + return JSU.S(sb.toString()); } private static String mySubstitute(GnuRegexp.REMatch match, String s, String source) { @@ -278,10 +277,10 @@ public class JSRegexp extends JS { 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); + String s = JSU.toString(s_); + int limit = nargs < 2 ? Integer.MAX_VALUE : JSU.toInt(arg1); if(limit < 0) limit = Integer.MAX_VALUE; - if(limit == 0) return new JSArray(); + if(limit == 0) return new JSArray(0); GnuRegexp.RE re = null; JSRegexp regexp = null; @@ -293,7 +292,7 @@ public class JSRegexp extends JS { regexp = (JSRegexp) arg0; re = regexp.re; } else { - sep = JS.toString(arg0); + sep = JSU.toString(arg0); } // special case this for speed. additionally, the code below doesn't properly handle @@ -301,7 +300,7 @@ public class JSRegexp extends JS { if(sep != null && sep.length()==0) { int len = s.length(); for(int i=0;i