1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $(date) The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 *******************************************************************************/
22 // This software is now distributed according to
23 // the Lesser Gnu Public License. Please see
24 // http://www.gnu.org/copyleft/lesser.txt for
26 // -- Happy Computing!
28 package com.stevesoft.pat;
31 * This class allows you to match on a partial string. If the allowOverRun flag
32 * is true, then the length() method returns a number 1 larger than is actually
33 * contained by the class.
35 * If one attempts to access the last character as follows:
38 * StringBuffer sb = ...;
40 * PartialBuffer pb = new PartialBuffer(sb);
41 * char c = pb.charAt(pb.length()-1);
44 * then two things happen. First, a zero is returned into the variable c.
45 * Second, the overRun flag is set to "true." Accessing data beyond the end of
46 * the buffer is considered an "overRun" of the data.
48 * This can be helpful in determining whether more characters are required for a
49 * match to occur, as the pseudo-code below illustrates.
53 * Regex r = new Regex("some pattern");
54 * pb.allowOverRun = true;
56 * boolean result = r.matchAt(pb,i);
58 * // The result of the match is not relevant, regardless
59 * // of whether result is true or false. We need to
60 * // append more data to the buffer and try again.
62 * sb.append(more data);
66 class PartialBuffer implements StringLike
70 public boolean allowOverRun = true;
72 public boolean overRun = false;
76 PartialBuffer(StringBuffer sb)
81 public char charAt(int n)
94 return allowOverRun ? sb.length() + 1 : sb.length();
97 public int indexOf(char c)
99 for (int i = 0; i < sb.length(); i++)
101 if (sb.charAt(i) == c)
109 public Object unwrap()
114 public String substring(int i1, int i2)
116 StringBuffer sb = new StringBuffer(i2 - i1);
117 for (int i = i1; i < i2; i++)
119 sb.append(charAt(i));
121 return sb.toString();
124 /** Just returns null. */
125 public BasicStringBufferLike newStringBufferLike()