X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fcom%2Fstevesoft%2Fpat%2FPartialBuffer.java;h=c53d6df532f872382261c5897dfc2295df637999;hb=57e49a53c5022f7c3539a807493e5431e24a07c5;hp=fd57b276d23d89a23b54593cbf38bca4fa848a68;hpb=f24dacb1da56fccf05d684e2f4899facec2aecf7;p=jalview.git diff --git a/src/com/stevesoft/pat/PartialBuffer.java b/src/com/stevesoft/pat/PartialBuffer.java index fd57b27..c53d6df 100755 --- a/src/com/stevesoft/pat/PartialBuffer.java +++ b/src/com/stevesoft/pat/PartialBuffer.java @@ -1,83 +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; - - -/** 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
+ * 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; + } +}