X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fcom%2Fstevesoft%2Fpat%2FPartialBuffer.java;h=c53d6df532f872382261c5897dfc2295df637999;hb=4b2be7cd8c14e67cc347609199e25c7f09fda988;hp=2239545f63a5926e46f6e407fe636be4fff6e664;hpb=c40cf903f740a72ab63dd1abc10fa33450ce660d;p=jalview.git diff --git a/src/com/stevesoft/pat/PartialBuffer.java b/src/com/stevesoft/pat/PartialBuffer.java index 2239545..c53d6df 100755 --- a/src/com/stevesoft/pat/PartialBuffer.java +++ b/src/com/stevesoft/pat/PartialBuffer.java @@ -1,84 +1,109 @@ -// -// This software is now distributed according to -// the Lesser Gnu Public License. Please see -// http://www.gnu.org/copyleft/lesser.txt for -// the details. -// -- Happy Computing! -// -package com.stevesoft.pat; - -import java.io.*; - -/** This class allows you to match on a partial string. - If the allowOverRun flag is true, then the - length() method returns a number 1 larger than - is actually contained by the class. -

- If one attempts to access the last character as - follows: -

-    StringBuffer sb = ...;
-    ...
-    PartialBuffer pb = new PartialBuffer(sb);
-    char c = pb.charAt(pb.length()-1);
-    
- then two things happen. First, a zero is returned - into the variable c. Second, the overRun flag is - set to "true." Accessing data beyond the end of - the buffer is considered an "overRun" of the data. -

- This can be helpful in determining whether more - characters are required for a match to occur, as - the pseudo-code below illustrates. -

-    int i = ...;
-    Regex r = new Regex("some pattern");
-    pb.allowOverRun = true;
-    pb.overRun = true;
-    boolean result = r.matchAt(pb,i);
-    if(pb.overRun) {
-      // The result of the match is not relevant, regardless
-      // of whether result is true or false.  We need to
-      // append more data to the buffer and try again.
-      ....
-      sb.append(more data);
-    }
-    
- */ -class PartialBuffer implements StringLike { - int off; - public boolean allowOverRun = true; - public boolean overRun = false; - StringBuffer sb; - PartialBuffer(StringBuffer sb) { - this.sb = sb; - } - public char charAt(int n) { - n += off; - if(n == sb.length()) { - overRun = true; - return 0; - } - return sb.charAt(n); - } - public int length() { - return allowOverRun ? sb.length()+1 : sb.length(); - } - public int indexOf(char c) { - for(int i=0;i + * If one attempts to access the last character as follows: + * + *
+ *  StringBuffer sb = ...;
+ *  ...
+ *  PartialBuffer pb = new PartialBuffer(sb);
+ *  char c = pb.charAt(pb.length()-1);
+ * 
+ * + * then two things happen. First, a zero is returned into the variable c. + * Second, the overRun flag is set to "true." Accessing data beyond the end of + * the buffer is considered an "overRun" of the data. + *

+ * This can be helpful in determining whether more characters are required for a + * match to occur, as the pseudo-code below illustrates. + * + *

+ *  int i = ...;
+ *  Regex r = new Regex("some pattern");
+ *  pb.allowOverRun = true;
+ *  pb.overRun = true;
+ *  boolean result = r.matchAt(pb,i);
+ *  if(pb.overRun) {
+ *  // The result of the match is not relevant, regardless
+ *  // of whether result is true or false.  We need to
+ *  // append more data to the buffer and try again.
+ *  ....
+ *  sb.append(more data);
+ *  }
+ * 
+ */ +class PartialBuffer implements StringLike +{ + int off; + + public boolean allowOverRun = true; + + public boolean overRun = false; + + StringBuffer sb; + + PartialBuffer(StringBuffer sb) + { + this.sb = sb; + } + + public char charAt(int n) + { + n += off; + if (n == sb.length()) + { + overRun = true; + return 0; + } + return sb.charAt(n); + } + + public int length() + { + return allowOverRun ? sb.length() + 1 : sb.length(); + } + + public int indexOf(char c) + { + for (int i = 0; i < sb.length(); i++) + { + if (sb.charAt(i) == c) + { + return i; + } + } + return -1; + } + + public Object unwrap() + { + return sb; + } + + public String substring(int i1, int i2) + { + StringBuffer sb = new StringBuffer(i2 - i1); + for (int i = i1; i < i2; i++) + { + sb.append(charAt(i)); + } + return sb.toString(); + } + + /** Just returns null. */ + public BasicStringBufferLike newStringBufferLike() + { + return null; + } +}