Formatting
[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 /**\r
11         Shareware: package pat\r
12    <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>\r
13  */\r
14 /**\r
15  StrPos is used internally by regex to parse the regular expression. */\r
16 public class StrPos\r
17 {\r
18   String s;\r
19   int pos;\r
20   /** Return the position in the string pointed to */\r
21   public int pos()\r
22   {\r
23     return pos;\r
24   }\r
25 \r
26   /** This contains the escape character, which is \ by default. */\r
27   public char esc = Pattern.ESC;\r
28   char c;\r
29   /** Returns the current, possibly escaped, character. */\r
30   public char thisChar()\r
31   {\r
32     return c;\r
33   }\r
34 \r
35   boolean dontMatch, eos;\r
36 \r
37   /** tell whether we are at end of string */\r
38   public boolean eos()\r
39   {\r
40     return eos;\r
41   }\r
42 \r
43   /** initialize a StrPos from another StrPos. */\r
44   public StrPos(StrPos sp)\r
45   {\r
46     dup(sp);\r
47   }\r
48 \r
49   /** copy a StrPos from sp to this. */\r
50   public void dup(StrPos sp)\r
51   {\r
52     s = sp.s;\r
53     pos = sp.pos;\r
54     c = sp.c;\r
55     dontMatch = sp.dontMatch;\r
56     eos = sp.eos;\r
57   }\r
58 \r
59   /** Initialize a StrPos by giving it a String, and a\r
60        position within the String. */\r
61   public StrPos(String s, int pos)\r
62   {\r
63     this.s = s;\r
64     this.pos = pos - 1;\r
65     inc();\r
66   }\r
67 \r
68   /** Advance the place where StrPos points within the String.\r
69        Counts a backslash as part of the next character. */\r
70   public StrPos inc()\r
71   {\r
72     pos++;\r
73     if (pos >= s.length())\r
74     {\r
75       eos = true;\r
76       return this;\r
77     }\r
78     eos = false;\r
79     c = s.charAt(pos);\r
80     if (c == esc && pos + 1 < s.length())\r
81     {\r
82       pos++;\r
83       c = s.charAt(pos);\r
84       if (c != esc)\r
85       {\r
86         dontMatch = true;\r
87       }\r
88       else\r
89       {\r
90         dontMatch = false;\r
91       }\r
92     }\r
93     else\r
94     {\r
95       dontMatch = false;\r
96     }\r
97     return this;\r
98   }\r
99 \r
100   /** Compare the (possibly escaped) character\r
101        pointed to by StrPos.  Return true if they are the\r
102        same, but lways return if character pointed to is escaped. */\r
103   public boolean match(char ch)\r
104   {\r
105     if (dontMatch || eos)\r
106     {\r
107       return false;\r
108     }\r
109     return c == ch;\r
110   }\r
111 \r
112   /** As match, but only matches if the character is escaped. */\r
113   public boolean escMatch(char ch)\r
114   {\r
115     if (!dontMatch || eos)\r
116     {\r
117       return false;\r
118     }\r
119     return c == ch;\r
120   }\r
121 \r
122   /** Returns true if the current\r
123       character is escaped (preceeded by "\"). */\r
124   public boolean escaped()\r
125   {\r
126     return dontMatch;\r
127   }\r
128 \r
129   /** Increment the string pointer by each character in\r
130        <pre>st</pre> that matches a non-escaped\r
131        character. */\r
132   public boolean incMatch(String st)\r
133   {\r
134     StrPos sp = new StrPos(this);\r
135     int i;\r
136     for (i = 0; i < st.length(); i++)\r
137     {\r
138       if (!sp.match(st.charAt(i)))\r
139       {\r
140         return false;\r
141       }\r
142       sp.inc();\r
143     }\r
144     dup(sp);\r
145     return true;\r
146   }\r
147 \r
148   /** Read in an integer. */\r
149   public patInt getPatInt()\r
150   {\r
151     if (incMatch("inf"))\r
152     {\r
153       return new patInf();\r
154     }\r
155     int i, cnt = 0;\r
156     StrPos sp = new StrPos(this);\r
157     for (i = 0; !sp.eos && sp.c >= '0' && sp.c <= '9'; i++)\r
158     {\r
159       cnt = 10 * cnt + sp.c - '0';\r
160       sp.inc();\r
161     }\r
162     if (i == 0)\r
163     {\r
164       return null;\r
165     }\r
166     dup(sp);\r
167     return new patInt(cnt);\r
168   }\r
169 \r
170   /** get the string that we are processing. */\r
171   public String getString()\r
172   {\r
173     return s;\r
174   }\r
175 };\r