needed for applet search
[jalview.git] / src / com / stevesoft / pat / PartialBuffer.java
diff --git a/src/com/stevesoft/pat/PartialBuffer.java b/src/com/stevesoft/pat/PartialBuffer.java
new file mode 100755 (executable)
index 0000000..2239545
--- /dev/null
@@ -0,0 +1,84 @@
+//\r
+// This software is now distributed according to\r
+// the Lesser Gnu Public License.  Please see\r
+// http://www.gnu.org/copyleft/lesser.txt for\r
+// the details.\r
+//    -- Happy Computing!\r
+//\r
+package com.stevesoft.pat;\r
+\r
+import java.io.*;\r
+\r
+/** This class allows you to match on a partial string.\r
+    If the allowOverRun flag is true, then the\r
+    length() method returns a number 1 larger than\r
+    is actually contained by the class.\r
+    <p>\r
+    If one attempts to access the last character as\r
+    follows:\r
+    <pre>\r
+    StringBuffer sb = ...;\r
+    ...\r
+    PartialBuffer pb = new PartialBuffer(sb);\r
+    char c = pb.charAt(pb.length()-1);\r
+    </pre>\r
+    then two things happen.  First, a zero is returned\r
+    into the variable c.  Second, the overRun flag is\r
+    set to "true."  Accessing data beyond the end of\r
+    the buffer is considered an "overRun" of the data.\r
+    <p>\r
+    This can be helpful in determining whether more\r
+    characters are required for a match to occur, as\r
+    the pseudo-code below illustrates.\r
+    <pre>\r
+    int i = ...;\r
+    Regex r = new Regex("some pattern");\r
+    pb.allowOverRun = true;\r
+    pb.overRun = true;\r
+    boolean result = r.matchAt(pb,i);\r
+    if(pb.overRun) {\r
+      // The result of the match is not relevant, regardless\r
+      // of whether result is true or false.  We need to\r
+      // append more data to the buffer and try again.\r
+      ....\r
+      sb.append(more data);\r
+    }\r
+    </pre>\r
+    */\r
+class PartialBuffer implements StringLike {\r
+  int off;\r
+  public boolean allowOverRun = true;\r
+  public boolean overRun = false;\r
+  StringBuffer sb;\r
+  PartialBuffer(StringBuffer sb) {\r
+    this.sb = sb;\r
+  }\r
+  public char charAt(int n) {\r
+    n += off;\r
+    if(n == sb.length()) {\r
+      overRun = true;\r
+      return 0;\r
+    }\r
+    return sb.charAt(n);\r
+  }\r
+  public int length() {\r
+    return allowOverRun ? sb.length()+1 : sb.length();\r
+  }\r
+  public int indexOf(char c) {\r
+    for(int i=0;i<sb.length();i++)\r
+      if(sb.charAt(i)==c)\r
+        return i;\r
+    return -1;\r
+  }\r
+  public Object unwrap() { return sb; }\r
+  public String substring(int i1,int i2) {\r
+    StringBuffer sb = new StringBuffer(i2-i1);\r
+    for(int i=i1;i<i2;i++)\r
+      sb.append(charAt(i));\r
+    return sb.toString();\r
+  }\r
+  /** Just returns null. */\r
+  public BasicStringBufferLike newStringBufferLike() {\r
+    return null;\r
+  }\r
+}\r