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