// -- Happy Computing!\r
//\r
package com.stevesoft.pat;\r
-/** \r
+\r
+/**\r
Shareware: package pat\r
<a href="copyright.html">Copyright 2001, Steven R. Brandt</a>\r
-*/ /**\r
-StrPos is used internally by regex to parse the regular expression. */\r
-public class StrPos {\r
- String s;\r
- int pos;\r
- /** Return the position in the string pointed to */\r
- public int pos() { return pos; }\r
+ */\r
+/**\r
+ StrPos is used internally by regex to parse the regular expression. */\r
+public class StrPos\r
+{\r
+ String s;\r
+ int pos;\r
+ /** Return the position in the string pointed to */\r
+ public int pos()\r
+ {\r
+ return pos;\r
+ }\r
+\r
+ /** This contains the escape character, which is \ by default. */\r
+ public char esc = Pattern.ESC;\r
+ char c;\r
+ /** Returns the current, possibly escaped, character. */\r
+ public char thisChar()\r
+ {\r
+ return c;\r
+ }\r
+\r
+ boolean dontMatch, eos;\r
\r
- /** This contains the escape character, which is \ by default. */\r
- public char esc=Pattern.ESC;\r
- char c;\r
- /** Returns the current, possibly escaped, character. */\r
- public char thisChar() { return c; }\r
+ /** tell whether we are at end of string */\r
+ public boolean eos()\r
+ {\r
+ return eos;\r
+ }\r
\r
- boolean dontMatch,eos;\r
+ /** initialize a StrPos from another StrPos. */\r
+ public StrPos(StrPos sp)\r
+ {\r
+ dup(sp);\r
+ }\r
\r
- /** tell whether we are at end of string */\r
- public boolean eos() { return eos; }\r
- /** initialize a StrPos from another StrPos. */\r
- public StrPos(StrPos sp) {\r
- dup(sp);\r
+ /** copy a StrPos from sp to this. */\r
+ public void dup(StrPos sp)\r
+ {\r
+ s = sp.s;\r
+ pos = sp.pos;\r
+ c = sp.c;\r
+ dontMatch = sp.dontMatch;\r
+ eos = sp.eos;\r
+ }\r
+\r
+ /** Initialize a StrPos by giving it a String, and a\r
+ position within the String. */\r
+ public StrPos(String s, int pos)\r
+ {\r
+ this.s = s;\r
+ this.pos = pos - 1;\r
+ inc();\r
+ }\r
+\r
+ /** Advance the place where StrPos points within the String.\r
+ Counts a backslash as part of the next character. */\r
+ public StrPos inc()\r
+ {\r
+ pos++;\r
+ if (pos >= s.length())\r
+ {\r
+ eos = true;\r
+ return this;\r
}\r
- /** copy a StrPos from sp to this. */\r
- public void dup(StrPos sp) {\r
- s = sp.s;\r
- pos = sp.pos;\r
- c = sp.c;\r
- dontMatch = sp.dontMatch;\r
- eos = sp.eos;\r
+ eos = false;\r
+ c = s.charAt(pos);\r
+ if (c == esc && pos + 1 < s.length())\r
+ {\r
+ pos++;\r
+ c = s.charAt(pos);\r
+ if (c != esc)\r
+ {\r
+ dontMatch = true;\r
+ }\r
+ else\r
+ {\r
+ dontMatch = false;\r
+ }\r
}\r
- /** Initialize a StrPos by giving it a String, and a\r
- position within the String. */\r
- public StrPos(String s,int pos) {\r
- this.s=s;\r
- this.pos=pos-1;\r
- inc();\r
+ else\r
+ {\r
+ dontMatch = false;\r
}\r
- /** Advance the place where StrPos points within the String.\r
- Counts a backslash as part of the next character. */\r
- public StrPos inc() {\r
- pos++;\r
- if(pos >= s.length()) {\r
- eos = true;\r
- return this;\r
- }\r
- eos = false;\r
- c = s.charAt(pos);\r
- if(c == esc && pos+1<s.length()) {\r
- pos++;\r
- c = s.charAt(pos);\r
- if(c != esc)\r
- dontMatch = true;\r
- else\r
- dontMatch = false;\r
- } else\r
- dontMatch = false;\r
- return this;\r
+ return this;\r
+ }\r
+\r
+ /** Compare the (possibly escaped) character\r
+ pointed to by StrPos. Return true if they are the\r
+ same, but lways return if character pointed to is escaped. */\r
+ public boolean match(char ch)\r
+ {\r
+ if (dontMatch || eos)\r
+ {\r
+ return false;\r
}\r
- /** Compare the (possibly escaped) character\r
- pointed to by StrPos. Return true if they are the\r
- same, but lways return if character pointed to is escaped. */\r
- public boolean match(char ch) {\r
- if(dontMatch || eos) return false;\r
- return c == ch;\r
+ return c == ch;\r
+ }\r
+\r
+ /** As match, but only matches if the character is escaped. */\r
+ public boolean escMatch(char ch)\r
+ {\r
+ if (!dontMatch || eos)\r
+ {\r
+ return false;\r
}\r
- /** As match, but only matches if the character is escaped. */\r
- public boolean escMatch(char ch) {\r
- if(!dontMatch || eos) return false;\r
- return c == ch;\r
+ return c == ch;\r
+ }\r
+\r
+ /** Returns true if the current\r
+ character is escaped (preceeded by "\"). */\r
+ public boolean escaped()\r
+ {\r
+ return dontMatch;\r
+ }\r
+\r
+ /** Increment the string pointer by each character in\r
+ <pre>st</pre> that matches a non-escaped\r
+ character. */\r
+ public boolean incMatch(String st)\r
+ {\r
+ StrPos sp = new StrPos(this);\r
+ int i;\r
+ for (i = 0; i < st.length(); i++)\r
+ {\r
+ if (!sp.match(st.charAt(i)))\r
+ {\r
+ return false;\r
+ }\r
+ sp.inc();\r
}\r
+ dup(sp);\r
+ return true;\r
+ }\r
\r
- /** Returns true if the current\r
- character is escaped (preceeded by "\"). */\r
- public boolean escaped() { return dontMatch; }\r
- /** Increment the string pointer by each character in\r
- <pre>st</pre> that matches a non-escaped\r
- character. */\r
- public boolean incMatch(String st) {\r
- StrPos sp = new StrPos(this);\r
- int i;\r
- for(i=0;i<st.length();i++) {\r
- if(!sp.match(st.charAt(i)) )\r
- return false;\r
- sp.inc();\r
- }\r
- dup(sp);\r
- return true;\r
+ /** Read in an integer. */\r
+ public patInt getPatInt()\r
+ {\r
+ if (incMatch("inf"))\r
+ {\r
+ return new patInf();\r
}\r
- /** Read in an integer. */\r
- public patInt getPatInt() {\r
- if(incMatch("inf"))\r
- return new patInf();\r
- int i,cnt=0;\r
- StrPos sp = new StrPos(this);\r
- for(i=0;!sp.eos && sp.c >= '0' && sp.c <= '9';i++) {\r
- cnt = 10*cnt+sp.c-'0';\r
- sp.inc();\r
- }\r
- if(i==0) return null;\r
- dup(sp);\r
- return new patInt(cnt);\r
+ int i, cnt = 0;\r
+ StrPos sp = new StrPos(this);\r
+ for (i = 0; !sp.eos && sp.c >= '0' && sp.c <= '9'; i++)\r
+ {\r
+ cnt = 10 * cnt + sp.c - '0';\r
+ sp.inc();\r
}\r
- /** get the string that we are processing. */\r
- public String getString() { return s; }\r
+ if (i == 0)\r
+ {\r
+ return null;\r
+ }\r
+ dup(sp);\r
+ return new patInt(cnt);\r
+ }\r
+\r
+ /** get the string that we are processing. */\r
+ public String getString()\r
+ {\r
+ return s;\r
+ }\r
};\r