Class | Line # | Actions | |||
---|---|---|---|---|---|
PartialBuffer | 46 | 17 | 12 |
1 | // | |
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 | |
5 | // the details. | |
6 | // -- Happy Computing! | |
7 | // | |
8 | package com.stevesoft.pat; | |
9 | ||
10 | /** | |
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. | |
14 | * <p> | |
15 | * If one attempts to access the last character as follows: | |
16 | * | |
17 | * <pre> | |
18 | * StringBuffer sb = ...; | |
19 | * ... | |
20 | * PartialBuffer pb = new PartialBuffer(sb); | |
21 | * char c = pb.charAt(pb.length()-1); | |
22 | * </pre> | |
23 | * | |
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. | |
27 | * <p> | |
28 | * This can be helpful in determining whether more characters are required for a | |
29 | * match to occur, as the pseudo-code below illustrates. | |
30 | * | |
31 | * <pre> | |
32 | * int i = ...; | |
33 | * Regex r = new Regex("some pattern"); | |
34 | * pb.allowOverRun = true; | |
35 | * pb.overRun = true; | |
36 | * boolean result = r.matchAt(pb,i); | |
37 | * if(pb.overRun) { | |
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. | |
41 | * .... | |
42 | * sb.append(more data); | |
43 | * } | |
44 | * </pre> | |
45 | */ | |
46 | class PartialBuffer implements StringLike | |
47 | { | |
48 | int off; | |
49 | ||
50 | public boolean allowOverRun = true; | |
51 | ||
52 | public boolean overRun = false; | |
53 | ||
54 | StringBuffer sb; | |
55 | ||
56 | 0 | PartialBuffer(StringBuffer sb) |
57 | { | |
58 | 0 | this.sb = sb; |
59 | } | |
60 | ||
61 | 0 | public char charAt(int n) |
62 | { | |
63 | 0 | n += off; |
64 | 0 | if (n == sb.length()) |
65 | { | |
66 | 0 | overRun = true; |
67 | 0 | return 0; |
68 | } | |
69 | 0 | return sb.charAt(n); |
70 | } | |
71 | ||
72 | 0 | public int length() |
73 | { | |
74 | 0 | return allowOverRun ? sb.length() + 1 : sb.length(); |
75 | } | |
76 | ||
77 | 0 | public int indexOf(char c) |
78 | { | |
79 | 0 | for (int i = 0; i < sb.length(); i++) |
80 | { | |
81 | 0 | if (sb.charAt(i) == c) |
82 | { | |
83 | 0 | return i; |
84 | } | |
85 | } | |
86 | 0 | return -1; |
87 | } | |
88 | ||
89 | 0 | public Object unwrap() |
90 | { | |
91 | 0 | return sb; |
92 | } | |
93 | ||
94 | 0 | public String substring(int i1, int i2) |
95 | { | |
96 | 0 | StringBuffer sb = new StringBuffer(i2 - i1); |
97 | 0 | for (int i = i1; i < i2; i++) |
98 | { | |
99 | 0 | sb.append(charAt(i)); |
100 | } | |
101 | 0 | return sb.toString(); |
102 | } | |
103 | ||
104 | /** Just returns null. */ | |
105 | 0 | public BasicStringBufferLike newStringBufferLike() |
106 | { | |
107 | 0 | return null; |
108 | } | |
109 | } |