// 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 {
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) + "\"");
}
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));
}
}
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);
}
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);
}
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()));
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;
}
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;
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;
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) {
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) {
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;
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();
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()));
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());
}
}