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;
10 import java.io.IOException;
11 import java.io.Reader;
13 import com.stevesoft.pat.wrap.StringBufferWrap;
16 * This class allows you to replace the text in strings as you read them in. Be
17 * careful what you do with this freedom... using Regex.perlCode("s{.*}{x}s") as
18 * your pattern will result in loading the entire contents of the Reader into
21 public class RegexReader extends Reader
23 RBuffer rb = new RBuffer(new StringBuffer());
25 PartialBuffer wrap = new PartialBuffer(rb.sb);
27 boolean moreToRead = true;
36 public RegexReader(Regex rex, Reader r)
39 rp = rex.getReplacer();
42 public RegexReader(Transformer tex, Reader r)
45 rp = tex.getReplacer();
48 public void reset() throws IOException
51 rb = new RBuffer(new StringBuffer());
52 wrap = new PartialBuffer(rb.sb);
56 void readData() throws IOException
60 while ((c = r.read()) != -1)
62 rb.sb.append((char) c);
68 if (c == -1 && n == 0)
71 wrap.allowOverRun = false;
75 void getMoreData() throws IOException
77 while (rb.pos >= rb.epos)
88 else if (rb.epos >= rb.sb.length() && rb.epos > nmax)
95 else if (rb.epos >= rb.sb.length() && moreToRead)
99 else if (rp.getRegex().matchAt(wrap, rb.epos))
107 StringBufferWrap sbw = new StringBufferWrap();
108 StringBufferLike sbl = new StringBufferLike(sbw);
110 * ReplaceRule rr = rex.getReplaceRule(); while(rr != null) {
111 * rr.apply(sbl,rex); rr = rr.next; }
113 Regex rex = rp.getRegex();
114 int npos = rex.matchedTo();
118 rp.apply(rex, rex.getReplaceRule());
120 RBuffer rb2 = new RBuffer((StringBuffer) sbw.unwrap());
121 rb2.epos = rb2.sb.length();
122 RBuffer rb3 = new RBuffer(rb.sb);
130 if (rb3.epos > rb3.sb.length())
132 if (rb.pos >= rb.epos)
136 rb3.pos = rb3.epos = 0;
144 rb3.pos = rb3.epos = npos;
155 else if (rb.epos < rb.sb.length())
167 public int read() throws IOException
169 if (rb.pos >= rb.epos)
172 if (rb.pos >= rb.epos)
177 // System.out.println(rb);
178 return rb.sb.charAt(rb.pos++);
181 public int read(char[] buf, int off, int len) throws IOException
185 for (int i = off; i < end; i++)
201 public void close() throws IOException
206 public boolean markSupported()
212 * Get the size of the working buffer. The current buffer may be larger if the
213 * pattern demands it.
215 public int getBufferSize()
221 * Set the size of the working buffer. The current buffer may be larger if the
222 * pattern demands it.
224 public void setBufferSize(int n)
232 * This function no longer serves any purpose.
237 public int getMaxLines()
243 * This function no longer serves any purpose.
248 public void setMaxLines(int ml)
256 * This function no longer serves any purpose.
261 public char getEOLchar()
267 * This function no longer serves any purpose.
272 public void setEOLchar(char c)
277 public long skip(long d) throws IOException
279 // This is probably inefficient, I just did it
280 // this way to avoid possible bugs.
282 while (n < d && read() != -1)
290 * static void test(String re,String inp,int n) throws Exception { Reader r =
291 * new StringReader(inp); r = new BufferedReader(r); Regex rex =
292 * Regex.perlCode(re); String res1 = rex.replaceAll(inp); int c = -1;
293 * StringBuffer sb = new StringBuffer(); RegexReader rr = new
294 * RegexReader(rex,r); rr.setBufferSize(n); while( (c = rr.read()) != -1)
295 * sb.append((char)c); String res2 = sb.toString(); if(!res1.equals(res2)) {
296 * System.out.println("nmax="+n); System.out.println("re="+re);
297 * System.out.println("inp="+inp); System.out.println("res1="+res1);
298 * System.out.println("res2="+res2); System.exit(255); } } public static void
299 * main(String[] args) throws Exception { for(int n=6;n<15;n++) {
300 * test("s/x/y/","-----x123456789",n); test("s/x/y/","x123456789",n);
301 * test("s/x/y/","-----x",n);
302 * test("s/x.*?x/y/",".xx..x..x...x...x....x....x",n);
303 * test("s/x.*x/[$&]/","--x........x--xx",n);
304 * test("s/x.*x/[$&]/","--x........x------",n);
305 * test("s/.$/a/m","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb",n);
306 * test("s/.$/a/","123",n);
307 * test("s/.$/a/","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb",n);
308 * test("s/^./a/","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb",n);
309 * test("s/$/a/","bbb",n); test("s/^/a/","bbb",n); test("s/^/a/","",n);
310 * test("s{.*}{N}","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",n);
311 * test("s/.{0,7}/y/","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",n);
312 * test("s/x/$&/","xxx",n); } System.out.println("Success!!!"); }