2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat;
11 * This class provides a method for parsing the "s/.../.../" constructs of
18 final static char close(char c)
20 // This switch statement does not behave
21 // properly when compiled with jdk1.1.5
24 * switch(c) { case '[': return ']'; case '(': return ')'; case '{': return
46 final public static String codify(String s, boolean keepbs)
48 return codify(s, 0, s.length(), keepbs);
51 final public static String codify(String s, int i0, int iN, boolean keepbs)
53 StringBuffer sb = new StringBuffer();
54 boolean ucmode = false, lcmode = false, litmode = false;
55 boolean uc1 = false, lc1 = false;
56 boolean modified = false;
57 for (int i = i0; i < iN; i++)
60 boolean mf = true, app = true;
67 char c2 = s.charAt(i);
86 uc1 = lc1 = ucmode = lcmode = litmode = false;
124 if (litmode && needbs(c))
131 return modified ? sb.toString() : s;
134 final static char uc(char c)
136 return CaseMgr.toUpperCase(c);
139 final static char lc(char c)
141 return CaseMgr.toLowerCase(c);
144 final static boolean needbs(char c)
146 if (c >= 'a' && c <= 'z')
150 if (c >= 'A' && c <= 'Z')
154 if (c >= '0' && c <= '9')
165 final static Regex parse(String s)
167 boolean igncase = false, optim = false, gFlag = false;
168 boolean sFlag = false, mFlag = false, xFlag = false;
170 StringBuffer s1 = new StringBuffer();
171 StringBuffer s2 = new StringBuffer();
172 int i = 0, count = 0;
173 char mode, delim = '/', cdelim = '/';
174 if (s.length() >= 3 && s.charAt(0) == 's')
178 cdelim = close(delim);
181 else if (s.length() >= 2 && s.charAt(0) == 'm')
185 cdelim = close(delim);
188 else if (s.length() >= 1 && s.charAt(0) == '/')
197 RegSyntaxError.endItAll("Regex.perlCode should be of the "
198 + "form s/// or m// or //");
199 } catch (RegSyntax rs)
204 for (; i < s.length(); i++)
206 if (s.charAt(i) == '\\')
211 else if (s.charAt(i) == cdelim && count == 0)
216 else if (s.charAt(i) == delim && cdelim != delim)
220 else if (s.charAt(i) == cdelim && cdelim != delim)
224 s1.append(s.charAt(i));
226 if (mode == 's' && cdelim != delim)
228 while (i < s.length() && Prop.isWhite(s.charAt(i)))
236 RegSyntaxError.endItAll("" + mode + delim + " needs " + cdelim);
237 } catch (RegSyntax rs)
242 cdelim = close(delim = s.charAt(i));
248 for (; i < s.length(); i++)
250 if (s.charAt(i) == '\\')
255 else if (s.charAt(i) == cdelim && count == 0)
260 else if (s.charAt(i) == delim && cdelim != delim)
264 else if (s.charAt(i) == cdelim && cdelim != delim)
268 s2.append(s.charAt(i));
271 for (; i < s.length(); i++)
273 char c = s.charAt(i);
299 RegSyntaxError.endItAll("Illegal flag to pattern: " + c);
300 } catch (RegSyntax rs)
306 Regex r = new Regex();
309 String pat = s1.toString(), reprul = s2.toString();
313 reprul = strip(reprul);
316 r.ignoreCase |= igncase;
326 r.setReplaceRule(new StringRule(reprul));
330 r.setReplaceRule(ReplaceRule.perlCode(reprul));
332 } catch (RegSyntax rs)
339 static String strip(String s)
341 StringBuffer sb = new StringBuffer();
342 for (int i = 0; i < s.length(); i++)
344 char c = s.charAt(i);
352 while (i < s.length())
354 if (s.charAt(i) == '\n')
364 sb.append(s.charAt(++i));
371 return sb.toString();