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
11 * This class allows you to match on a partial string. If the allowOverRun flag
\r
12 * is true, then the length() method returns a number 1 larger than is actually
\r
13 * contained by the class.
\r
15 * If one attempts to access the last character as follows:
\r
18 * javajs.util.SB sb = ...;
\r
20 * PartialBuffer pb = new PartialBuffer(sb);
\r
21 * char c = pb.charAt(pb.length()-1);
\r
24 * then two things happen. First, a zero is returned into the variable c.
\r
25 * Second, the overRun flag is set to "true." Accessing data beyond the end of
\r
26 * the buffer is considered an "overRun" of the data.
\r
28 * This can be helpful in determining whether more characters are required for a
\r
29 * match to occur, as the pseudo-code below illustrates.
\r
33 * Regex r = new Regex("some pattern");
\r
34 * pb.allowOverRun = true;
\r
35 * pb.overRun = true;
\r
36 * boolean result = r.matchAt(pb,i);
\r
38 * // The result of the match is not relevant, regardless
\r
39 * // of whether result is true or false. We need to
\r
40 * // append more data to the buffer and try again.
\r
42 * sb.append(more data);
\r
46 class PartialBuffer implements StringLike
\r
50 public boolean allowOverRun = true;
\r
52 public boolean overRun = false;
\r
56 PartialBuffer(javajs.util.SB sb)
\r
61 public char charAt(int n)
\r
64 if (n == sb.length())
\r
69 return sb.charAt(n);
\r
74 return allowOverRun ? sb.length() + 1 : sb.length();
\r
77 public int indexOf(char c)
\r
79 for (int i = 0; i < sb.length(); i++)
\r
81 if (sb.charAt(i) == c)
\r
89 public Object unwrap()
\r
94 public String substring(int i1, int i2)
\r
96 javajs.util.SB sb = new javajs.util.SB();//(i2 - i1);
\r
97 for (int i = i1; i < i2; i++)
\r
99 sb.appendC(charAt(i));
\r
101 return sb.toString();
\r
104 /** Just returns null. */
\r
105 public BasicStringBufferLike newStringBufferLike()
\r