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;
12 import com.stevesoft.pat.wrap.*;
15 * This class allows you to replace the text in strings as you read them in. Be
16 * careful what you do with this freedom... using Regex.perlCode("s{.*}{x}s") as
17 * your pattern will result in loading the entire contents of the Reader into
20 public class RegexReader extends Reader
22 RBuffer rb = new RBuffer(new StringBuffer());
24 PartialBuffer wrap = new PartialBuffer(rb.sb);
26 boolean moreToRead = true;
35 public RegexReader(Regex rex, Reader r)
38 rp = rex.getReplacer();
41 public RegexReader(Transformer tex, Reader r)
44 rp = tex.getReplacer();
47 public void reset() throws IOException
50 rb = new RBuffer(new StringBuffer());
51 wrap = new PartialBuffer(rb.sb);
55 void readData() throws IOException
59 while ((c = r.read()) != -1)
61 rb.sb.append((char) c);
67 if (c == -1 && n == 0)
70 wrap.allowOverRun = false;
74 void getMoreData() throws IOException
76 while (rb.pos >= rb.epos)
87 else if (rb.epos >= rb.sb.length() && rb.epos > nmax)
94 else if (rb.epos >= rb.sb.length() && moreToRead)
98 else if (rp.getRegex().matchAt(wrap, rb.epos))
106 StringBufferWrap sbw = new StringBufferWrap();
107 StringBufferLike sbl = new StringBufferLike(sbw);
109 * ReplaceRule rr = rex.getReplaceRule(); while(rr != null) {
110 * rr.apply(sbl,rex); rr = rr.next; }
112 Regex rex = rp.getRegex();
113 int npos = rex.matchedTo();
117 rp.apply(rex, rex.getReplaceRule());
119 RBuffer rb2 = new RBuffer((StringBuffer) sbw.unwrap());
120 rb2.epos = rb2.sb.length();
121 RBuffer rb3 = new RBuffer(rb.sb);
129 if (rb3.epos > rb3.sb.length())
131 if (rb.pos >= rb.epos)
135 rb3.pos = rb3.epos = 0;
143 rb3.pos = rb3.epos = npos;
154 else if (rb.epos < rb.sb.length())
166 public int read() throws IOException
168 if (rb.pos >= rb.epos)
171 if (rb.pos >= rb.epos)
176 // System.out.println(rb);
177 return rb.sb.charAt(rb.pos++);
180 public int read(char[] buf, int off, int len) throws IOException
184 for (int i = off; i < end; i++)
200 public void close() throws IOException
205 public boolean markSupported()
211 * Get the size of the working buffer. The current buffer may be larger if the
212 * pattern demands it.
214 public int getBufferSize()
220 * Set the size of the working buffer. The current buffer may be larger if the
221 * pattern demands it.
223 public void setBufferSize(int n)
231 * This function no longer serves any purpose.
235 public int getMaxLines()
241 * This function no longer serves any purpose.
245 public void setMaxLines(int ml)
253 * This function no longer serves any purpose.
257 public char getEOLchar()
263 * This function no longer serves any purpose.
267 public void setEOLchar(char c)
272 public long skip(long d) throws IOException
274 // This is probably inefficient, I just did it
275 // this way to avoid possible bugs.
277 while (n < d && read() != -1)
285 * static void test(String re,String inp,int n) throws Exception { Reader r =
286 * new StringReader(inp); r = new BufferedReader(r); Regex rex =
287 * Regex.perlCode(re); String res1 = rex.replaceAll(inp); int c = -1;
288 * StringBuffer sb = new StringBuffer(); RegexReader rr = new
289 * RegexReader(rex,r); rr.setBufferSize(n); while( (c = rr.read()) != -1)
290 * sb.append((char)c); String res2 = sb.toString(); if(!res1.equals(res2)) {
291 * System.out.println("nmax="+n); System.out.println("re="+re);
292 * System.out.println("inp="+inp); System.out.println("res1="+res1);
293 * System.out.println("res2="+res2); System.exit(255); } } public static void
294 * main(String[] args) throws Exception { for(int n=6;n<15;n++) {
295 * test("s/x/y/","-----x123456789",n); test("s/x/y/","x123456789",n);
296 * test("s/x/y/","-----x",n);
297 * test("s/x.*?x/y/",".xx..x..x...x...x....x....x",n);
298 * test("s/x.*x/[$&]/","--x........x--xx",n);
299 * test("s/x.*x/[$&]/","--x........x------",n);
300 * test("s/.$/a/m","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb",n);
301 * test("s/.$/a/","123",n);
302 * test("s/.$/a/","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb",n);
303 * test("s/^./a/","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb",n);
304 * test("s/$/a/","bbb",n); test("s/^/a/","bbb",n); test("s/^/a/","",n);
305 * test("s{.*}{N}","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",n);
306 * test("s/.{0,7}/y/","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",n);
307 * test("s/x/$&/","xxx",n); } System.out.println("Success!!!"); }