+package org.xwt.js;
+
+import gnu.regexp.*;
+
+public class JSRegexp extends JSCallable {
+ private boolean global;
+ private RE re;
+ private int lastIndex;
+
+ public JSRegexp(Object arg0, Object arg1) throws JS.Exn {
+ if(arg0 instanceof JSRegexp) {
+ JSRegexp r = (JSRegexp) arg0;
+ this.global = r.global;
+ this.re = r.re;
+ this.lastIndex = r.lastIndex;
+ } else {
+ String pattern = arg0.toString();
+ String sFlags = null;
+ int flags = 0;
+ if(arg1 != null) sFlags = (String)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 JS.Exn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
+ }
+ }
+ re = newRE(pattern,flags);
+ _put("source",pattern);
+ _put("global",wrapBool(global));
+ _put("ignoreCase",wrapBool(flags & RE.REG_ICASE));
+ _put("multiline",wrapBool(flags & RE.REG_MULTILINE));
+ }
+ }
+
+ public Object call(Object method, JSArray args) throws JS.Exn {
+ if (method.equals("exec")) {
+ return exec(args);
+ } else if (method.equals("test")) {
+ return test(args);
+ } else if (method.equals("toString")) {
+ return toString();
+ }
+ return null;
+ }
+
+ // gcj bug...
+ public Object get(Object key) { return _get(key); }
+ public void put(Object key,Object value) { _put(key,value); }
+
+ public Object _get(Object key) {
+ if(key.equals("lastIndex")) return new Integer(lastIndex);
+ return super.get(key);
+ }
+
+ public void _put(Object key, Object value) {
+ if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
+ super.put(key,value);
+ }
+
+ private Object exec(String s) throws JS.Exn {
+ 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();
+ if(match == null)
+ return null;
+ else
+ return matchToExecResult(match,re,s);
+ }
+
+ private static Object matchToExecResult(REMatch match, RE re, String s) {
+ JSObj ret = new JSObj();
+ ret.put("index",new Integer(match.getStartIndex()));
+ ret.put("input",s);
+ int n = re.getNumSubs();
+ ret.put("length",new Integer(n+1));
+ ret.put("0",match.toString());
+ for(int i=1;i<=n;i++)
+ ret.put(Integer.toString(i),match.toString(i));
+ return ret;
+ }
+
+
+ private Object exec(JSArray args) throws JS.Exn {
+ if(args.length() < 1) throw new JS.Exn("Not enough args to exec");
+ String s = args.elementAt(0).toString();
+ return exec(s);
+ }
+
+ private Object test(JSArray args) throws JS.Exn {
+ if(args.length() < 1) throw new JS.Exn("Not enough args to match");
+ String s = args.elementAt(0).toString();
+
+ if(global) {
+ 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 wrapBool(match != null);
+ } else {
+ return wrapBool(re.getMatch(s) != null);
+ }
+ }
+
+ public String toString() {
+ 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();
+ }
+
+ public static Object stringMatch(Object o, JSArray args) throws JS.Exn {
+ if(args.length() < 1) throw new JS.Exn("not enough args to match");
+ Object arg0 = args.elementAt(0);
+ String s = o.toString();
+ RE re;
+ JSRegexp regexp = null;
+ if(arg0 instanceof JSRegexp) {
+ regexp = (JSRegexp) arg0;
+ re = regexp.re;
+ } else {
+ re = newRE(arg0.toString(),0);
+ }
+
+ if(regexp == null) {
+ REMatch match = re.getMatch(s);
+ return matchToExecResult(match,re,s);
+ }
+ if(!regexp.global)
+ return regexp.exec(s);
+
+ JSArray ret = new JSArray();
+ REMatch[] matches = re.getAllMatches(s);
+ for(int i=0;i<matches.length;i++)
+ ret.addElement(matches[i].toString());
+ if(matches.length > 0)
+ regexp.lastIndex = matches[matches.length-1].getEndIndex();
+ else
+ regexp.lastIndex = s.length();
+ return ret;
+ }
+
+ public static Object stringSearch(Object o, JSArray args) throws JS.Exn {
+ if(args.length() < 1) throw new JS.Exn("not enough args to match");
+ Object arg0 = args.elementAt(0);
+ String s = o.toString();
+ RE re;
+ if(arg0 instanceof JSRegexp)
+ re = ((JSRegexp)arg0).re;
+ else
+ re = newRE(arg0.toString(),0);
+ REMatch match = re.getMatch(s);
+ if(match == null) return new Integer(-1);
+ return new Integer(match.getStartIndex());
+ }
+
+ public static Object stringReplace(Object o, JSArray args) throws JS.Exn {
+ if(args.length() < 2) throw new JS.Exn("not enough args to replace");
+ Object arg0 = args.elementAt(0);
+ Object arg1 = args.elementAt(1);
+ String s = o.toString();
+ RE re;
+ JSCallable 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 JSCallable)
+ replaceFunc = (JSCallable) arg1;
+ else
+ replaceString = arg1.toString();
+ 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) {
+ // FEATURE: reintroduce
+ throw new JS.Exn("stringReplace() with a replacement function is temporarily disabled");
+ /*
+ JSArray a = new JSArray();
+ a.addElement(match.toString());
+ if(regexp != null) {
+ int n = re.getNumSubs();
+ for(int j=1;j<=n;j++)
+ a.addElement(match.toString(j));
+ }
+ a.addElement(new Integer(match.getStartIndex()));
+ a.addElement(s);
+ Object ret = replaceFunc.call(a, null);
+ sb.append(ret.toString());
+ */
+ } 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 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();
+ }
+
+
+ public static Object stringSplit(Object o,JSArray args) {
+ String s = o.toString();
+ if(args.length() < 1 || args.elementAt(0) == null || s.length() == 0) {
+ JSArray ret = new JSArray();
+ ret.addElement(s);
+ return ret;
+ }
+ Object arg0 = args.elementAt(0);
+
+ int limit = args.length() < 2 ? Integer.MAX_VALUE : JS.toInt(args.elementAt(1));
+ 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 = arg0.toString();
+ }
+
+ // 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(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(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(m.toString(i));
+ if(ret.length() == limit) break OUTER;
+ }
+ }
+ } else {
+ int x = s.indexOf(sep,p);
+ if(x == -1) break OUTER;
+ ret.addElement(s.substring(p,x));
+ p = x + sep.length();
+ }
+ if(ret.length() == limit) break;
+ }
+ if(p < s.length() && ret.length() != limit)
+ ret.addElement(s.substring(p));
+ return ret;
+ }
+
+ public static RE newRE(String pattern, int flags) throws JS.Exn {
+ try {
+ return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
+ } catch(REException e) {
+ throw new JS.Exn(e.toString());
+ }
+ }
+
+ private static Boolean wrapBool(boolean b) {
+ return b ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ private static Boolean wrapBool(int n) {
+ return wrapBool(n != 0);
+ }
+
+ public String typeName() { return "regexp"; }
+}