+++ /dev/null
-// 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 int lastIndex;
-
- private JS pattern;
- private int flags;
-
- 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 = JS.toString(arg0);
- String sFlags = null;
- int flags = 0;
- if(arg1 != null) sFlags = JS.toString(arg1);
- 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 'g': global = true; break;
- default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
- }
- }
- re = newRE(pattern,flags);
- this.pattern = JS.S(pattern);
- this.flags = flags;
- }
- }
-
- public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
- switch(nargs) {
- case 1: {
- //#jswitch(method)
- case "exec": {
- String s = JS.toString(a0);
- int start = global ? lastIndex : 0;
- if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
- REMatch match = re.getMatch(s,start);
- if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
- return match == null ? null : matchToExecResult(match,re,s);
- }
- case "test": {
- 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; }
- REMatch match = re.getMatch(s,start);
- lastIndex = match != null ? s.length() : match.getEndIndex();
- return B(match != null);
- }
- case "toString": return JS.S(a0.coerceToString());
- case "stringMatch": return stringMatch(a0,a1);
- case "stringSearch": return stringSearch(a0,a1);
- //#end
- break;
- }
- case 2: {
- //#jswitch(method)
- case "stringReplace": return stringReplace(a0, a1,a2);
- //#end
- break;
- }
- }
- return super.callMethod(method, a0, a1, a2, rest, nargs);
- }
-
- 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(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 JS matchToExecResult(REMatch match, RE re, String s) {
- try {
- JS ret = new JS.O();
- ret.put(JS.S("index"), N(match.getStartIndex()));
- ret.put(JS.S("input"),JS.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)));
- return ret;
- } 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();
- }
-
- 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(JS.toString(arg0),0);
- }
-
- if(regexp == null) {
- 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();
- REMatch[] matches = re.getAllMatches(s);
- for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
- regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
- return ret;
- }
-
- 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());
- }
-
- static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
- String s = JS.toString(o);
- RE re;
- JSFunction replaceFunc = null;
- String replaceString = null;
- JSRegexp regexp = null;
- if(arg0 instanceof JSRegexp) {
- regexp = (JSRegexp) arg0;
- re = regexp.re;
- } else {
- re = newRE(arg0.toString(),0);
- }
- if(arg1 instanceof JSFunction)
- replaceFunc = (JSFunction) arg1;
- else
- replaceString = JS.toString(arg1);
- REMatch[] matches;
- if(regexp != null && regexp.global) {
- matches = re.getAllMatches(s);
- if(regexp != null) {
- if(matches.length > 0)
- regexp.lastIndex = matches[matches.length-1].getEndIndex();
- else
- regexp.lastIndex = s.length();
- }
- } else {
- REMatch match = re.getMatch(s);
- if(match != null)
- matches = new REMatch[]{ match };
- else
- matches = new 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];
- sb.append(sa,pos,match.getStartIndex()-pos);
- pos = match.getEndIndex();
- 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);
- }
-
- // note: can't perform pausing operations in here
- sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
-
- } else {
- sb.append(mySubstitute(match,replaceString,s));
- }
- }
- int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
- sb.append(sa,end,sa.length-end);
- return JS.S(sb.toString());
- }
-
- private static String mySubstitute(REMatch match, String s, String source) {
- StringBuffer sb = new StringBuffer();
- int i,n;
- char c,c2;
- for(i=0;i<s.length()-1;i++) {
- c = s.charAt(i);
- if(c != '$') {
- sb.append(c);
- continue;
- }
- i++;
- c = s.charAt(i);
- switch(c) {
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
- n = (c - '0') * 10 + (c2 - '0');
- i++;
- } else {
- n = c - '0';
- }
- if(n > 0)
- sb.append(match.toString(n));
- break;
- case '$':
- sb.append('$'); break;
- case '&':
- sb.append(match.toString()); break;
- case '`':
- sb.append(source.substring(0,match.getStartIndex())); break;
- case '\'':
- sb.append(source.substring(match.getEndIndex())); break;
- default:
- sb.append('$');
- sb.append(c);
- }
- }
- if(i < s.length()) sb.append(s.charAt(i));
- return sb.toString();
- }
-
-
- 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();
-
- RE re = null;
- JSRegexp regexp = null;
- String sep = null;
- JSArray ret = new JSArray();
- int p = 0;
-
- if(arg0 instanceof JSRegexp) {
- regexp = (JSRegexp) arg0;
- re = regexp.re;
- } else {
- sep = JS.toString(arg0);
- }
-
- // special case this for speed. additionally, the code below doesn't properly handle
- // zero length strings
- if(sep != null && sep.length()==0) {
- int len = s.length();
- for(int i=0;i<len;i++)
- ret.addElement(JS.S(s.substring(i,i+1)));
- return ret;
- }
-
- OUTER: while(p < s.length()) {
- if(re != null) {
- REMatch m = re.getMatch(s,p);
- if(m == null) break OUTER;
- boolean zeroLength = m.getStartIndex() == m.getEndIndex();
- ret.addElement(JS.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
- p = zeroLength ? p + 1 : m.getEndIndex();
- if(!zeroLength) {
- for(int i=1;i<=re.getNumSubs();i++) {
- ret.addElement(JS.S(m.toString(i)));
- if(ret.length() == limit) break OUTER;
- }
- }
- } else {
- int x = s.indexOf(sep,p);
- if(x == -1) break OUTER;
- ret.addElement(JS.S(s.substring(p,x)));
- p = x + sep.length();
- }
- if(ret.length() == limit) break;
- }
- if(p < s.length() && ret.length() != limit)
- ret.addElement(JS.S(s.substring(p)));
- return ret;
- }
-
- private static RE newRE(String pattern, int flags) throws JSExn {
- try {
- return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
- } catch(REException e) {
- throw new JSExn(e.toString());
- }
- }
-}