1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
6 /** A JavaScript regular expression object */
7 public class JSRegexp extends JS {
8 private boolean global;
10 private int lastIndex;
12 public JSRegexp(Object arg0, Object arg1) throws JSExn {
13 if(arg0 instanceof JSRegexp) {
14 JSRegexp r = (JSRegexp) arg0;
15 this.global = r.global;
17 this.lastIndex = r.lastIndex;
19 String pattern = (String)arg0;
22 if(arg1 != null) sFlags = (String)arg1;
23 if(sFlags == null) sFlags = "";
24 for(int i=0;i<sFlags.length();i++) {
25 switch(sFlags.charAt(i)) {
26 case 'i': flags |= RE.REG_ICASE; break;
27 case 'm': flags |= RE.REG_MULTILINE; break;
28 case 'g': global = true; break;
29 default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
32 re = newRE(pattern,flags);
33 put("source", pattern);
34 put("global", B(global));
35 put("ignoreCase", B(flags & RE.REG_ICASE));
36 put("multiline", B(flags & RE.REG_MULTILINE));
40 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
45 String s = (String)a0;
46 int start = global ? lastIndex : 0;
47 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
48 REMatch match = re.getMatch(s,start);
49 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
50 return match == null ? null : matchToExecResult(match,re,s);
53 String s = (String)a0;
54 if (!global) return B(re.getMatch(s) != null);
55 int start = global ? lastIndex : 0;
56 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
57 REMatch match = re.getMatch(s,start);
58 lastIndex = match != null ? s.length() : match.getEndIndex();
59 return B(match != null);
61 case "toString": return toString(a0);
62 case "stringMatch": return stringMatch(a0,a1);
63 case "stringSearch": return stringSearch(a0,a1);
69 case "stringReplace": return stringReplace(a0, a1,a2);
74 return super.callMethod(method, a0, a1, a2, rest, nargs);
77 public Object get(Object key) {
79 case "exec": return METHOD;
80 case "test": return METHOD;
81 case "toString": return METHOD;
82 case "lastIndex": return N(lastIndex);
84 return super.get(key);
87 public void put(Object key, Object value) {
88 if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
92 private static Object matchToExecResult(REMatch match, RE re, String s) {
94 ret.put("index", N(match.getStartIndex()));
96 int n = re.getNumSubs();
97 ret.put("length", N(n+1));
98 ret.put("0",match.toString());
99 for(int i=1;i<=n;i++) ret.put(Integer.toString(i),match.toString(i));
103 public String toString() {
104 StringBuffer sb = new StringBuffer();
106 sb.append(get("source"));
108 if(global) sb.append('g');
109 if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i');
110 if(Boolean.TRUE.equals(get("multiline"))) sb.append('m');
111 return sb.toString();
114 public static Object stringMatch(Object o, Object arg0) throws JSExn {
115 String s = o.toString();
117 JSRegexp regexp = null;
118 if(arg0 instanceof JSRegexp) {
119 regexp = (JSRegexp) arg0;
122 re = newRE(arg0.toString(),0);
126 REMatch match = re.getMatch(s);
127 return matchToExecResult(match,re,s);
129 if(!regexp.global) return regexp.callMethod("exec", s, null, null, null, 1);
131 JSArray ret = new JSArray();
132 REMatch[] matches = re.getAllMatches(s);
133 for(int i=0;i<matches.length;i++) ret.addElement(matches[i].toString());
134 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
138 public static Object stringSearch(Object o, Object arg0) throws JSExn {
139 String s = o.toString();
140 RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(arg0.toString(),0);
141 REMatch match = re.getMatch(s);
142 return match == null ? N(-1) : N(match.getStartIndex());
145 public static Object stringReplace(Object o, Object arg0, Object arg1) throws JSExn {
146 String s = o.toString();
148 JS replaceFunc = null;
149 String replaceString = null;
150 JSRegexp regexp = null;
151 if(arg0 instanceof JSRegexp) {
152 regexp = (JSRegexp) arg0;
155 re = newRE(arg0.toString(),0);
157 if(arg1 instanceof JS)
158 replaceFunc = (JS) arg1;
160 replaceString = arg1.toString();
162 if(regexp != null && regexp.global) {
163 matches = re.getAllMatches(s);
165 if(matches.length > 0)
166 regexp.lastIndex = matches[matches.length-1].getEndIndex();
168 regexp.lastIndex = s.length();
171 REMatch match = re.getMatch(s);
173 matches = new REMatch[]{ match };
175 matches = new REMatch[0];
178 StringBuffer sb = new StringBuffer(s.length());
180 char[] sa = s.toCharArray();
181 for(int i=0;i<matches.length;i++) {
182 REMatch match = matches[i];
183 sb.append(sa,pos,match.getStartIndex()-pos);
184 pos = match.getEndIndex();
185 if(replaceFunc != null) {
186 int n = (regexp == null ? 0 : re.getNumSubs());
188 Object[] rest = new Object[numArgs - 3];
189 Object a0 = match.toString();
192 for(int j=1;j<=n;j++)
194 case 1: a1 = match.toString(j); break;
195 case 2: a2 = match.toString(j); break;
196 default: rest[j - 3] = match.toString(j); break;
200 a1 = N(match.getStartIndex());
204 a2 = N(match.getStartIndex());
208 rest[rest.length - 2] = N(match.getStartIndex());
209 rest[rest.length - 1] = s;
212 // note: can't perform pausing operations in here
213 sb.append((String)replaceFunc.call(a0, a1, a2, rest, numArgs));
216 sb.append(mySubstitute(match,replaceString,s));
219 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
220 sb.append(sa,end,sa.length-end);
221 return sb.toString();
224 private static String mySubstitute(REMatch match, String s, String source) {
225 StringBuffer sb = new StringBuffer();
228 for(i=0;i<s.length()-1;i++) {
237 case '0': case '1': case '2': case '3': case '4':
238 case '5': case '6': case '7': case '8': case '9':
239 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
240 n = (c - '0') * 10 + (c2 - '0');
246 sb.append(match.toString(n));
249 sb.append('$'); break;
251 sb.append(match.toString()); break;
253 sb.append(source.substring(0,match.getStartIndex())); break;
255 sb.append(source.substring(match.getEndIndex())); break;
261 if(i < s.length()) sb.append(s.charAt(i));
262 return sb.toString();
266 public static Object stringSplit(String s, Object arg0, Object arg1, int nargs) {
267 int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
268 if(limit < 0) limit = Integer.MAX_VALUE;
269 if(limit == 0) return new JSArray();
272 JSRegexp regexp = null;
274 JSArray ret = new JSArray();
277 if(arg0 instanceof JSRegexp) {
278 regexp = (JSRegexp) arg0;
281 sep = arg0.toString();
284 // special case this for speed. additionally, the code below doesn't properly handle
285 // zero length strings
286 if(sep != null && sep.length()==0) {
287 int len = s.length();
288 for(int i=0;i<len;i++)
289 ret.addElement(s.substring(i,i+1));
293 OUTER: while(p < s.length()) {
295 REMatch m = re.getMatch(s,p);
296 if(m == null) break OUTER;
297 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
298 ret.addElement(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex()));
299 p = zeroLength ? p + 1 : m.getEndIndex();
301 for(int i=1;i<=re.getNumSubs();i++) {
302 ret.addElement(m.toString(i));
303 if(ret.length() == limit) break OUTER;
307 int x = s.indexOf(sep,p);
308 if(x == -1) break OUTER;
309 ret.addElement(s.substring(p,x));
310 p = x + sep.length();
312 if(ret.length() == limit) break;
314 if(p < s.length() && ret.length() != limit)
315 ret.addElement(s.substring(p));
319 public static RE newRE(String pattern, int flags) throws JSExn {
321 return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
322 } catch(REException e) {
323 throw new JSExn(e.toString());
327 public String typeName() { return "regexp"; }