5 public class Regexp extends JS.Obj {
6 private boolean global;
10 public Regexp(JS.Array args) throws JS.Exn {
11 if(args.length() < 1) throw new JS.Exn("Not enough args to regexp");
12 Object arg0 = args.elementAt(0);
13 if(arg0 instanceof Regexp) {
14 Regexp r = (Regexp) arg0;
15 this.global = r.global;
17 this.lastIndex = r.lastIndex;
19 String pattern = arg0.toString();
22 if(args.length() == 2) sFlags = args.elementAt(1).toString();
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 JS.Exn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
32 re = newRE(pattern,flags);
33 _put("source",pattern);
34 _put("global",wrapBool(global));
35 _put("ignoreCase",wrapBool(flags & RE.REG_ICASE));
36 _put("multiline",wrapBool(flags & RE.REG_MULTILINE));
40 public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
41 if (method.equals("exec")) {
42 if (checkOnly) return Boolean.TRUE;
44 } else if (method.equals("test")) {
45 if (checkOnly) return Boolean.TRUE;
47 } else if (method.equals("toString")) {
48 if (checkOnly) return Boolean.TRUE;
51 if (checkOnly) return Boolean.FALSE;
56 public Object get(Object key) { return _get(key); }
57 public void put(Object key,Object value) { _put(key,value); }
59 public Object _get(Object key) {
60 if(key.equals("lastIndex")) return new Integer(lastIndex);
61 return super.get(key);
64 public void _put(Object key, Object value) {
65 if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
69 private Object exec(String s) throws JS.Exn {
70 int start = global ? lastIndex : 0;
71 if(start < 0 || start >= s.length()) {
76 REMatch match = re.getMatch(s,start);
78 lastIndex = match == null ? s.length() : match.getEndIndex();
82 return matchToExecResult(match,re,s);
85 private static Object matchToExecResult(REMatch match, RE re, String s) {
86 JS.Obj ret = new JS.Obj();
87 ret.put("index",new Integer(match.getStartIndex()));
89 int n = re.getNumSubs();
90 ret.put("length",new Integer(n+1));
91 ret.put("0",match.toString());
93 ret.put(Integer.toString(i),match.toString(i));
98 private Object exec(JS.Array args) throws JS.Exn {
99 if(args.length() < 1) throw new JS.Exn("Not enough args to exec");
100 String s = args.elementAt(0).toString();
104 private Object test(JS.Array args) throws JS.Exn {
105 if(args.length() < 1) throw new JS.Exn("Not enough args to match");
106 String s = args.elementAt(0).toString();
109 int start = global ? lastIndex : 0;
110 if(start < 0 || start >= s.length()) {
115 REMatch match = re.getMatch(s,start);
116 lastIndex = match != null ? s.length() : match.getEndIndex();
117 return wrapBool(match != null);
119 return wrapBool(re.getMatch(s) != null);
123 public String toString() {
124 StringBuffer sb = new StringBuffer();
126 sb.append(_get("source"));
128 if(global) sb.append('g');
129 if(Boolean.TRUE.equals(_get("ignoreCase"))) sb.append('i');
130 if(Boolean.TRUE.equals(_get("multiline"))) sb.append('m');
131 return sb.toString();
134 public static Object stringMatch(Object o, JS.Array args) throws JS.Exn {
135 if(args.length() < 1) throw new JS.Exn("not enough args to match");
136 Object arg0 = args.elementAt(0);
137 String s = o.toString();
139 Regexp regexp = null;
140 if(arg0 instanceof Regexp) {
141 regexp = (Regexp) arg0;
144 re = newRE(arg0.toString(),0);
148 REMatch match = re.getMatch(s);
149 return matchToExecResult(match,re,s);
152 return regexp.exec(s);
154 JS.Array ret = new JS.Array();
155 REMatch[] matches = re.getAllMatches(s);
156 for(int i=0;i<matches.length;i++)
157 ret.addElement(matches[i].toString());
158 if(matches.length > 0)
159 regexp.lastIndex = matches[matches.length-1].getEndIndex();
161 regexp.lastIndex = s.length();
165 public static Object stringSearch(Object o, JS.Array args) throws JS.Exn {
166 if(args.length() < 1) throw new JS.Exn("not enough args to match");
167 Object arg0 = args.elementAt(0);
168 String s = o.toString();
170 if(arg0 instanceof Regexp)
171 re = ((Regexp)arg0).re;
173 re = newRE(arg0.toString(),0);
174 REMatch match = re.getMatch(s);
175 if(match == null) return new Integer(-1);
176 return new Integer(match.getStartIndex());
179 public static Object stringReplace(Object o, JS.Array args) throws JS.Exn {
180 if(args.length() < 2) throw new JS.Exn("not enough args to replace");
181 Object arg0 = args.elementAt(0);
182 Object arg1 = args.elementAt(1);
183 String s = o.toString();
185 JS.Callable replaceFunc = null;
186 String replaceString = null;
187 Regexp regexp = null;
188 if(arg0 instanceof Regexp) {
189 regexp = (Regexp) arg0;
192 re = newRE(arg0.toString(),0);
194 if(arg1 instanceof JS.Callable)
195 replaceFunc = (JS.Callable) arg1;
197 replaceString = arg1.toString();
199 if(regexp != null && regexp.global) {
200 matches = re.getAllMatches(s);
202 if(matches.length > 0)
203 regexp.lastIndex = matches[matches.length-1].getEndIndex();
205 regexp.lastIndex = s.length();
208 REMatch match = re.getMatch(s);
210 matches = new REMatch[]{ match };
212 matches = new REMatch[0];
215 StringBuffer sb = new StringBuffer(s.length());
217 char[] sa = s.toCharArray();
218 for(int i=0;i<matches.length;i++) {
219 REMatch match = matches[i];
220 sb.append(sa,pos,match.getStartIndex()-pos);
221 pos = match.getEndIndex();
222 if(replaceFunc != null) {
223 JS.Array a = new JS.Array();
224 a.addElement(match.toString());
226 int n = re.getNumSubs();
227 for(int j=1;j<=n;j++)
228 a.addElement(match.toString(j));
230 a.addElement(new Integer(match.getStartIndex()));
232 Object ret = replaceFunc.call(a);
233 sb.append(ret.toString());
235 sb.append(mySubstitute(match,replaceString,s));
238 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
239 sb.append(sa,end,sa.length-end);
240 return sb.toString();
243 private static String mySubstitute(REMatch match, String s, String source) {
244 StringBuffer sb = new StringBuffer();
247 for(i=0;i<s.length()-1;i++) {
256 case '0': case '1': case '2': case '3': case '4':
257 case '5': case '6': case '7': case '8': case '9':
258 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
259 n = (c - '0') * 10 + (c2 - '0');
265 sb.append(match.toString(n));
268 sb.append('$'); break;
270 sb.append(match.toString()); break;
272 sb.append(source.substring(0,match.getStartIndex())); break;
274 sb.append(source.substring(match.getEndIndex())); break;
280 if(i < s.length()) sb.append(s.charAt(i));
281 return sb.toString();
285 public static Object stringSplit(Object o,JS.Array args) {
286 String s = o.toString();
287 if(args.length() < 1 || args.elementAt(0) == null || s.length() == 0) {
288 JS.Array ret = new JS.Array();
292 Object arg0 = args.elementAt(0);
294 int limit = args.length() < 2 ? Integer.MAX_VALUE : JS.toInt(args.elementAt(1));
295 if(limit < 0) limit = Integer.MAX_VALUE;
296 if(limit == 0) return new JS.Array();
299 Regexp regexp = null;
301 JS.Array ret = new JS.Array();
304 if(arg0 instanceof Regexp) {
305 regexp = (Regexp) arg0;
308 sep = arg0.toString();
311 // special case this for speed. additionally, the code below doesn't properly handle
312 // zero length strings
313 if(sep != null && sep.length()==0) {
314 int len = s.length();
315 for(int i=0;i<len;i++)
316 ret.addElement(s.substring(i,i+1));
320 OUTER: while(p < s.length()) {
322 REMatch m = re.getMatch(s,p);
323 if(m == null) break OUTER;
324 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
325 ret.addElement(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex()));
326 p = zeroLength ? p + 1 : m.getEndIndex();
328 for(int i=1;i<=re.getNumSubs();i++) {
329 ret.addElement(m.toString(i));
330 if(ret.length() == limit) break OUTER;
334 int x = s.indexOf(sep,p);
335 if(x == -1) break OUTER;
336 ret.addElement(s.substring(p,x));
337 p = x + sep.length();
339 if(ret.length() == limit) break;
341 if(p < s.length() && ret.length() != limit)
342 ret.addElement(s.substring(p));
346 public static RE newRE(String pattern, int flags) throws JS.Exn {
348 return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
349 } catch(REException e) {
350 throw new JS.Exn(e.toString());
354 private static Boolean wrapBool(boolean b) {
355 return b ? Boolean.TRUE : Boolean.FALSE;
358 private static Boolean wrapBool(int n) {
359 return wrapBool(n != 0);
362 public String typeName() { return "regexp"; }