Remove redundancy in Eclipse
[jalview.git] / src / com / stevesoft / pat / StrPos.java
1 //\r
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
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 /** \r
10         Shareware: package pat\r
11    <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>\r
12 */ /**\r
13 StrPos is used internally by regex to parse the regular expression. */\r
14 public class StrPos {\r
15     String s;\r
16     int pos;\r
17     /** Return the position in the string pointed to */\r
18     public int pos() { return pos; }\r
19 \r
20     /** This contains the escape character, which is \ by default. */\r
21     public char esc=Pattern.ESC;\r
22     char c;\r
23     /** Returns the current, possibly escaped, character. */\r
24     public char thisChar() { return c; }\r
25 \r
26     boolean dontMatch,eos;\r
27 \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
32         dup(sp);\r
33     }\r
34     /** copy a StrPos from sp to this. */\r
35     public void dup(StrPos sp) {\r
36         s = sp.s;\r
37         pos = sp.pos;\r
38         c = sp.c;\r
39         dontMatch = sp.dontMatch;\r
40         eos = sp.eos;\r
41     }\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
45         this.s=s;\r
46         this.pos=pos-1;\r
47         inc();\r
48     }\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
52         pos++;\r
53         if(pos >= s.length()) {\r
54             eos = true;\r
55             return this;\r
56         }\r
57         eos = false;\r
58         c = s.charAt(pos);\r
59         if(c == esc && pos+1<s.length()) {\r
60             pos++;\r
61             c = s.charAt(pos);\r
62             if(c != esc)\r
63                 dontMatch = true;\r
64             else\r
65                 dontMatch = false;\r
66         } else\r
67             dontMatch = false;\r
68         return this;\r
69     }\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
75         return c == ch;\r
76     }\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
80         return c == ch;\r
81     }\r
82 \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
88          character. */\r
89     public boolean incMatch(String st) {\r
90         StrPos sp = new StrPos(this);\r
91         int i;\r
92         for(i=0;i<st.length();i++) {\r
93             if(!sp.match(st.charAt(i)) )\r
94                 return false;\r
95             sp.inc();\r
96         }\r
97         dup(sp);\r
98         return true;\r
99     }\r
100     /** Read in an integer. */\r
101     public patInt getPatInt() {\r
102         if(incMatch("inf"))\r
103             return new patInf();\r
104         int i,cnt=0;\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
108             sp.inc();\r
109         }\r
110         if(i==0) return null;\r
111         dup(sp);\r
112         return new patInt(cnt);\r
113     }\r
114     /** get the string that we are processing. */\r
115     public String getString() { return s; }\r
116 };\r