JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / unused / com / stevesoft / pat / PartialBuffer.java
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  *  javajs.util.SB 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(&quot;some pattern&quot;);
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   javajs.util.SB sb;
55
56   PartialBuffer(javajs.util.SB sb)
57   {
58     this.sb = sb;
59   }
60
61   public char charAt(int n)
62   {
63     n += off;
64     if (n == sb.length())
65     {
66       overRun = true;
67       return 0;
68     }
69     return sb.charAt(n);
70   }
71
72   public int length()
73   {
74     return allowOverRun ? sb.length() + 1 : sb.length();
75   }
76
77   public int indexOf(char c)
78   {
79     for (int i = 0; i < sb.length(); i++)
80     {
81       if (sb.charAt(i) == c)
82       {
83         return i;
84       }
85     }
86     return -1;
87   }
88
89   public Object unwrap()
90   {
91     return sb;
92   }
93
94   public String substring(int i1, int i2)
95   {
96     javajs.util.SB sb = new javajs.util.SB();//(i2 - i1);
97     for (int i = i1; i < i2; i++)
98     {
99       sb.appendC(charAt(i));
100     }
101     return sb.toString();
102   }
103
104   /** Just returns null. */
105   public BasicStringBufferLike newStringBufferLike()
106   {
107     return null;
108   }
109 }