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));
38 // FIXME: Do whatever we need to do to take advantage of the GETCALL bytecode when its available
39 final JS.Callable execFN = new JS.Callable() { public Object call(JS.Array args) { return exec(args); } };
40 final JS.Callable testFN = new JS.Callable() { public Object call(JS.Array args) { return test(args); } };
41 final JS.Callable toStringFN = new JS.Callable() { public Object call(JS.Array args) { return Regexp.this.toString(); } };
44 put("toString",toStringFN);
47 public Object get(Object key) {
48 if(key.equals("lastIndex")) return new Integer(lastIndex);
49 return super.get(key);
52 public void put(Object key, Object value) {
53 if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
57 private Object exec(String s) throws JS.Exn {
58 int start = global ? lastIndex : 0;
59 if(start < 0 || start >= s.length()) {
64 REMatch match = re.getMatch(s,start);
66 lastIndex = match == null ? s.length() : match.getEndIndex();
70 return matchToExecResult(match,re,s);
73 private static Object matchToExecResult(REMatch match, RE re, String s) {
74 JS.Obj ret = new JS.Obj();
75 ret.put("index",new Integer(match.getStartIndex()));
77 int n = re.getNumSubs();
78 ret.put("length",new Integer(n+1));
79 ret.put("0",match.toString());
81 ret.put(Integer.toString(i),match.toString(i));
86 private Object exec(JS.Array args) throws JS.Exn {
87 if(args.length() < 1) throw new JS.Exn("Not enough args to exec");
88 String s = args.elementAt(0).toString();
92 private Object test(JS.Array args) throws JS.Exn {
93 if(args.length() < 1) throw new JS.Exn("Not enough args to match");
94 String s = args.elementAt(0).toString();
97 int start = global ? lastIndex : 0;
98 if(start < 0 || start >= s.length()) {
103 REMatch match = re.getMatch(s,start);
104 lastIndex = match != null ? s.length() : match.getEndIndex();
105 return wrapBool(match != null);
107 return wrapBool(re.getMatch(s) != null);
111 public String toString() {
112 StringBuffer sb = new StringBuffer();
114 sb.append(get("source"));
116 if(global) sb.append('g');
117 if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i');
118 if(Boolean.TRUE.equals(get("multiline"))) sb.append('m');
119 return sb.toString();
122 public static Object stringMatch(Object o, JS.Array args) throws JS.Exn {
123 if(args.length() < 1) throw new JS.Exn("not enough args to match");
124 Object arg0 = args.elementAt(0);
125 String s = o.toString();
127 Regexp regexp = null;
128 if(arg0 instanceof Regexp) {
129 regexp = (Regexp) arg0;
132 re = newRE(arg0.toString(),0);
136 REMatch match = re.getMatch(s);
137 return matchToExecResult(match,re,s);
140 return regexp.exec(s);
142 JS.Array ret = new JS.Array();
143 REMatch[] matches = re.getAllMatches(s);
144 for(int i=0;i<matches.length;i++)
145 ret.addElement(matches[i].toString());
146 if(matches.length > 0)
147 regexp.lastIndex = matches[matches.length-1].getEndIndex();
149 regexp.lastIndex = s.length();
153 public static Object stringSearch(Object o, JS.Array args) throws JS.Exn {
154 if(args.length() < 1) throw new JS.Exn("not enough args to match");
155 Object arg0 = args.elementAt(0);
156 String s = o.toString();
158 if(arg0 instanceof Regexp)
159 re = ((Regexp)arg0).re;
161 re = newRE(arg0.toString(),0);
162 REMatch match = re.getMatch(s);
163 if(match == null) return new Integer(-1);
164 return new Integer(match.getStartIndex());
167 public static Object stringReplace(Object o, JS.Array args) throws JS.Exn {
168 if(args.length() < 2) throw new JS.Exn("not enough args to replace");
169 Object arg0 = args.elementAt(0);
170 Object arg1 = args.elementAt(1);
171 String s = o.toString();
173 JS.Callable replaceFunc = null;
174 String replaceString = null;
175 Regexp regexp = null;
176 if(arg0 instanceof Regexp) {
177 regexp = (Regexp) arg0;
180 re = newRE(arg0.toString(),0);
182 if(arg1 instanceof JS.Callable)
183 replaceFunc = (JS.Callable) arg1;
185 replaceString = arg1.toString();
187 if(regexp != null && regexp.global) {
188 matches = re.getAllMatches(s);
190 if(matches.length > 0)
191 regexp.lastIndex = matches[matches.length-1].getEndIndex();
193 regexp.lastIndex = s.length();
196 REMatch match = re.getMatch(s);
198 matches = new REMatch[]{ match };
200 matches = new REMatch[0];
203 StringBuffer sb = new StringBuffer(s.length());
205 char[] sa = s.toCharArray();
206 for(int i=0;i<matches.length;i++) {
207 REMatch match = matches[i];
208 sb.append(sa,pos,match.getStartIndex()-pos);
209 pos = match.getEndIndex();
210 if(replaceFunc != null) {
211 JS.Array a = new JS.Array();
212 a.addElement(match.toString());
214 int n = re.getNumSubs();
215 for(int j=1;j<=n;j++)
216 a.addElement(match.toString(j));
218 a.addElement(new Integer(match.getStartIndex()));
220 Object ret = replaceFunc.call(a);
221 sb.append(ret.toString());
223 sb.append(mySubstitute(match,replaceString,s));
226 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
227 sb.append(sa,end,sa.length-end);
228 return sb.toString();
231 private static String mySubstitute(REMatch match, String s, String source) {
232 StringBuffer sb = new StringBuffer();
235 for(i=0;i<s.length()-1;i++) {
244 case '0': case '1': case '2': case '3': case '4':
245 case '5': case '6': case '7': case '8': case '9':
246 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
247 n = (c - '0') * 10 + (c2 - '0');
253 sb.append(match.toString(n));
256 sb.append('$'); break;
258 sb.append(match.toString()); break;
260 sb.append(source.substring(0,match.getStartIndex())); break;
262 sb.append(source.substring(match.getEndIndex())); break;
268 if(i < s.length()) sb.append(s.charAt(i));
269 return sb.toString();
273 public static Object stringSplit(Object o,JS.Array args) {
274 String s = o.toString();
275 if(args.length() < 1 || args.elementAt(0) == null || s.length() == 0) {
276 JS.Array ret = new JS.Array();
280 Object arg0 = args.elementAt(0);
282 int limit = args.length() < 2 ? Integer.MAX_VALUE : JS.toInt(args.elementAt(1));
283 if(limit < 0) limit = Integer.MAX_VALUE;
284 if(limit == 0) return new JS.Array();
287 Regexp regexp = null;
289 JS.Array ret = new JS.Array();
292 if(arg0 instanceof Regexp) {
293 regexp = (Regexp) arg0;
296 sep = arg0.toString();
299 // special case this for speed. additionally, the code below doesn't properly handle
300 // zero length strings
301 if(sep != null && sep.length()==0) {
302 int len = s.length();
303 for(int i=0;i<len;i++)
304 ret.addElement(s.substring(i,i+1));
308 OUTER: while(p < s.length()) {
310 REMatch m = re.getMatch(s,p);
311 if(m == null) break OUTER;
312 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
313 ret.addElement(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex()));
314 p = zeroLength ? p + 1 : m.getEndIndex();
316 for(int i=1;i<=re.getNumSubs();i++) {
317 ret.addElement(m.toString(i));
318 if(ret.length() == limit) break OUTER;
322 int x = s.indexOf(sep,p);
323 if(x == -1) break OUTER;
324 ret.addElement(s.substring(p,x));
325 p = x + sep.length();
327 if(ret.length() == limit) break;
329 if(p < s.length() && ret.length() != limit)
330 ret.addElement(s.substring(p));
334 public static RE newRE(String pattern, int flags) throws JS.Exn {
336 return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
337 } catch(REException e) {
338 throw new JS.Exn(e.toString());
342 private static Boolean wrapBool(boolean b) {
343 return b ? Boolean.TRUE : Boolean.FALSE;
346 private static Boolean wrapBool(int n) {
347 return wrapBool(n != 0);