2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
10 Shareware: package pat
\r
11 <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
\r
13 StrPos is used internally by regex to parse the regular expression. */
\r
14 public class StrPos {
\r
17 /** Return the position in the string pointed to */
\r
18 public int pos() { return pos; }
\r
20 /** This contains the escape character, which is \ by default. */
\r
21 public char esc=Pattern.ESC;
\r
23 /** Returns the current, possibly escaped, character. */
\r
24 public char thisChar() { return c; }
\r
26 boolean dontMatch,eos;
\r
28 /** tell whether we are at end of string */
\r
29 public boolean eos() { return eos; }
\r
30 /** initialize a StrPos from another StrPos. */
\r
31 public StrPos(StrPos sp) {
\r
34 /** copy a StrPos from sp to this. */
\r
35 public void dup(StrPos sp) {
\r
39 dontMatch = sp.dontMatch;
\r
42 /** Initialize a StrPos by giving it a String, and a
\r
43 position within the String. */
\r
44 public StrPos(String s,int pos) {
\r
49 /** Advance the place where StrPos points within the String.
\r
50 Counts a backslash as part of the next character. */
\r
51 public StrPos inc() {
\r
53 if(pos >= s.length()) {
\r
59 if(c == esc && pos+1<s.length()) {
\r
70 /** Compare the (possibly escaped) character
\r
71 pointed to by StrPos. Return true if they are the
\r
72 same, but lways return if character pointed to is escaped. */
\r
73 public boolean match(char ch) {
\r
74 if(dontMatch || eos) return false;
\r
77 /** As match, but only matches if the character is escaped. */
\r
78 public boolean escMatch(char ch) {
\r
79 if(!dontMatch || eos) return false;
\r
83 /** Returns true if the current
\r
84 character is escaped (preceeded by "\"). */
\r
85 public boolean escaped() { return dontMatch; }
\r
86 /** Increment the string pointer by each character in
\r
87 <pre>st</pre> that matches a non-escaped
\r
89 public boolean incMatch(String st) {
\r
90 StrPos sp = new StrPos(this);
\r
92 for(i=0;i<st.length();i++) {
\r
93 if(!sp.match(st.charAt(i)) )
\r
100 /** Read in an integer. */
\r
101 public patInt getPatInt() {
\r
102 if(incMatch("inf"))
\r
103 return new patInf();
\r
105 StrPos sp = new StrPos(this);
\r
106 for(i=0;!sp.eos && sp.c >= '0' && sp.c <= '9';i++) {
\r
107 cnt = 10*cnt+sp.c-'0';
\r
110 if(i==0) return null;
\r
112 return new patInt(cnt);
\r
114 /** get the string that we are processing. */
\r
115 public String getString() { return s; }
\r