2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
10 /** This class provides a method for parsing the "s/.../.../" constructs
\r
15 final static char close(char c) {
\r
16 // This switch statement does not behave
\r
17 // properly when compiled with jdk1.1.5
\r
40 final public static String codify(String s,boolean keepbs) {
\r
41 return codify(s,0,s.length(),keepbs);
\r
43 final public static String codify(String s,int i0,int iN,boolean keepbs) {
\r
44 StringBuffer sb = new StringBuffer();
\r
45 boolean ucmode = false, lcmode = false, litmode = false;
\r
46 boolean uc1 = false, lc1 = false;
\r
47 boolean modified = false;
\r
48 for(int i=i0;i<iN;i++) {
\r
49 char c = s.charAt(i);
\r
50 boolean mf = true, app=true;
\r
55 char c2 = s.charAt(i);
\r
73 uc1=lc1=ucmode=lcmode=litmode=false;
\r
79 if(keepbs) mf = false;
\r
98 if(litmode && needbs(c))
\r
103 return modified ? sb.toString() : s;
\r
105 final static char uc(char c) {
\r
106 return CaseMgr.toUpperCase(c);
\r
108 final static char lc(char c) {
\r
109 return CaseMgr.toLowerCase(c);
\r
111 final static boolean needbs(char c) {
\r
112 if(c >= 'a' && c <= 'z')
\r
114 if(c >= 'A' && c <= 'Z')
\r
116 if(c >= '0' && c <= '9')
\r
122 final static Regex parse(String s) {
\r
123 boolean igncase = false, optim = false, gFlag = false;
\r
124 boolean sFlag = false, mFlag = false, xFlag = false;
\r
126 StringBuffer s1 = new StringBuffer();
\r
127 StringBuffer s2 = new StringBuffer();
\r
129 char mode,delim='/',cdelim='/';
\r
130 if(s.length() >= 3 && s.charAt(0)=='s') {
\r
132 delim = s.charAt(1);
\r
133 cdelim = close(delim);
\r
135 } else if(s.length() >= 2 && s.charAt(0)=='m') {
\r
137 delim = s.charAt(1);
\r
138 cdelim = close(delim);
\r
140 } else if(s.length() >= 1 && s.charAt(0)=='/') {
\r
145 RegSyntaxError.endItAll(
\r
146 "Regex.perlCode should be of the "+
\r
147 "form s/// or m// or //");
\r
148 } catch(RegSyntax rs) {}
\r
151 for(;i<s.length();i++) {
\r
152 if(s.charAt(i)=='\\') {
\r
155 } else if(s.charAt(i)==cdelim && count==0) {
\r
158 } else if(s.charAt(i)==delim && cdelim != delim) {
\r
160 } else if(s.charAt(i)==cdelim && cdelim != delim) {
\r
163 s1.append(s.charAt(i));
\r
165 if(mode=='s' && cdelim != delim) {
\r
166 while(i<s.length() && Prop.isWhite(s.charAt(i)) )
\r
168 if(i>=s.length()) {
\r
170 RegSyntaxError.endItAll(""+mode+delim+" needs "+cdelim);
\r
171 } catch(RegSyntax rs) {}
\r
174 cdelim = close(delim = s.charAt(i));
\r
179 for(;i<s.length();i++) {
\r
180 if(s.charAt(i)=='\\') {
\r
183 } else if(s.charAt(i)==cdelim && count==0) {
\r
186 } else if(s.charAt(i)==delim && cdelim != delim) {
\r
188 } else if(s.charAt(i)==cdelim && cdelim != delim) {
\r
191 s2.append(s.charAt(i));
\r
194 for(;i<s.length();i++) {
\r
195 char c = s.charAt(i);
\r
218 RegSyntaxError.endItAll("Illegal flag to pattern: "+c);
\r
219 } catch(RegSyntax rs) {}
\r
223 Regex r = new Regex();
\r
225 String pat=s1.toString(),reprul=s2.toString();
\r
228 reprul = strip(reprul);
\r
231 r.ignoreCase |= igncase;
\r
235 if(optim) r.optimize();
\r
237 r.setReplaceRule(new StringRule(reprul));
\r
239 r.setReplaceRule(ReplaceRule.perlCode(reprul));
\r
240 } catch(RegSyntax rs) {
\r
245 static String strip(String s) {
\r
246 StringBuffer sb = new StringBuffer();
\r
247 for(int i=0;i<s.length();i++) {
\r
248 char c = s.charAt(i);
\r
249 if(Prop.isWhite(c)) {
\r
251 } else if(c == '#') {
\r
253 while(i<s.length()) {
\r
254 if(s.charAt(i) == '\n')
\r
258 } else if(c == '\\') {
\r
260 sb.append(s.charAt(++i));
\r
264 return sb.toString();
\r