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