Formatting
[jalview.git] / src / com / stevesoft / pat / RegRes.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  This class is used to store a result from Regex */\r
16 public class RegRes\r
17     implements Cloneable\r
18 {\r
19   protected int[] marks = null;\r
20   protected boolean didMatch_ = false;\r
21   protected StringLike src = null;\r
22 \r
23   /** Obtain the text String that was matched against. */\r
24   public String getString()\r
25   {\r
26     return src.toString();\r
27   }\r
28 \r
29   /** Obtain the source StringLike object. */\r
30   public StringLike getStringLike()\r
31   {\r
32     return src;\r
33   }\r
34 \r
35   protected int charsMatched_ = 0, matchFrom_ = 0, numSubs_ = 0;\r
36   public String toString()\r
37   {\r
38     StringBuffer sb = new StringBuffer();\r
39     sb.append("match=" + matchedFrom() + ":" + charsMatched());\r
40     if (!didMatch())\r
41     {\r
42       return sb.toString();\r
43     }\r
44     for (int i = 0; i < numSubs(); i++)\r
45     {\r
46       int n = i + 1;\r
47       sb.append(" sub(" + n + ")=" + matchedFrom(n) +\r
48                 ":" + charsMatched(n));\r
49     }\r
50     return sb.toString();\r
51   }\r
52 \r
53   public RegRes()\r
54   {}\r
55 \r
56   public RegRes(RegRes r)\r
57   {\r
58     copyOutOf(r);\r
59   }\r
60 \r
61   public void copyOutOf(RegRes r)\r
62   {\r
63     if (r.marks == null)\r
64     {\r
65       marks = null;\r
66     }\r
67     else\r
68     {\r
69       try\r
70       {\r
71         //marks = (Hashtable)r.marks.clone();\r
72         marks = new int[r.marks.length];\r
73         for (int i = 0; i < marks.length; i++)\r
74         {\r
75           marks[i] = r.marks[i];\r
76         }\r
77         //marks = (int[])r.marks.clone();\r
78       }\r
79       catch (Throwable t)\r
80       {}\r
81     }\r
82     didMatch_ = r.didMatch_;\r
83     src = r.src;\r
84     charsMatched_ = r.charsMatched_;\r
85     matchFrom_ = r.matchFrom_;\r
86     numSubs_ = r.numSubs_;\r
87   }\r
88 \r
89   public Object clone()\r
90   {\r
91     return new RegRes(this);\r
92   }\r
93 \r
94   public boolean equals(RegRes r)\r
95   {\r
96     if (charsMatched_ != r.charsMatched_\r
97         || matchFrom_ != r.matchFrom_\r
98         || didMatch_ != r.didMatch_\r
99         || numSubs_ != r.numSubs_\r
100         || !src.unwrap().equals(r.src.unwrap()))\r
101     {\r
102       return false;\r
103     }\r
104     if (marks == null && r.marks != null)\r
105     {\r
106       return false;\r
107     }\r
108     if (marks != null && r.marks == null)\r
109     {\r
110       return false;\r
111     }\r
112     for (int i = 1; i <= numSubs_; i++)\r
113     {\r
114       if (matchedFrom(i) != r.matchedFrom(i))\r
115       {\r
116         return false;\r
117       }\r
118       else if (charsMatched(i) != r.charsMatched(i))\r
119       {\r
120         return false;\r
121       }\r
122     }\r
123     return true;\r
124   }\r
125 \r
126   /** Obtains the match if successful, null otherwise.*/\r
127   public String stringMatched()\r
128   {\r
129     int mf = matchedFrom(), cm = charsMatched();\r
130     return!didMatch_ || mf < 0 || cm < 0 ? null :\r
131         src.substring(mf, mf + cm);\r
132   }\r
133 \r
134   /** Obtains the position backreference number i begins to match, or\r
135        -1 if backreference i was not matched. */\r
136   public int matchedFrom(int i)\r
137   {\r
138     if (marks == null || i > numSubs_)\r
139     {\r
140       return -1;\r
141     }\r
142     //Integer in=(Integer)marks.get("left"+i);\r
143     //return in == null ? -1 : in.intValue();\r
144     return marks[i];\r
145   }\r
146 \r
147   /** Obtains the number of characters matched by backreference i, or\r
148        -1 if backreference i was not matched. */\r
149   public int charsMatched(int i)\r
150   {\r
151     if (marks == null || i > numSubs_ || !didMatch_)\r
152     {\r
153       return -1;\r
154     }\r
155     //Integer in = (Integer)marks.get("right"+i);\r
156     //int i2 = in==null ? -1 : in.intValue();\r
157     int mf = matchedFrom(i);\r
158     return mf < 0 ? -1 : marks[i + numSubs_] - matchedFrom(i);\r
159   }\r
160 \r
161   /** This is either equal to matchedFrom(i)+charsMatched(i) if the match\r
162       was successful, or -1 if it was not. */\r
163   public int matchedTo(int i)\r
164   {\r
165     if (marks == null || i > numSubs_ || !didMatch_)\r
166     {\r
167       return -1;\r
168     }\r
169     return marks[i + numSubs_];\r
170   }\r
171 \r
172   /** Obtains a substring matching the nth set\r
173               of parenthesis from the pattern. See\r
174               numSubs(void), or null if the nth backrefence did\r
175               not match. */\r
176   public String stringMatched(int i)\r
177   {\r
178     int mf = matchedFrom(i), cm = charsMatched(i);\r
179     return!didMatch_ || mf < 0 || cm < 0 ? null :\r
180         src.substring(mf, mf + cm);\r
181   }\r
182 \r
183   /** This returns the part of the string that preceeds the match,\r
184        or null if the match failed.*/\r
185   public String left()\r
186   {\r
187     int mf = matchedFrom();\r
188     return!didMatch_ || (mf < 0) ? null : src.substring(0, mf);\r
189   }\r
190 \r
191   /** This returns the part of the string that follows the ith\r
192               backreference, or null if the backreference did not match. */\r
193   public String left(int i)\r
194   {\r
195     int mf = matchedFrom(i);\r
196     return!didMatch_ || (mf < 0) ? null : src.substring(0, mf);\r
197   }\r
198 \r
199   /** This returns the part of the string that follows the match,\r
200        or null if the backreference did not match.*/\r
201   public String right()\r
202   {\r
203     int mf = matchedFrom(), cm = charsMatched();\r
204     return!didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf +\r
205         cm, src.length());\r
206   }\r
207 \r
208   /** This returns the string to the right of the ith backreference,\r
209        or null if the backreference did not match. */\r
210   public String right(int i)\r
211   {\r
212     int mf = matchedFrom(i), cm = charsMatched(i);\r
213     return!didMatch_ || mf < 0 || cm < 0 ? null :\r
214         src.substring(mf + cm, src.length());\r
215   }\r
216 \r
217   /** After a successful match, this returns the location of\r
218               the first matching character, or -1 if the match failed.*/\r
219   public int matchedFrom()\r
220   {\r
221     return!didMatch_ ? -1 : matchFrom_;\r
222   }\r
223 \r
224   /** After a successful match, this returns the number of\r
225               characters in the match, or -1 if the match failed. */\r
226   public int charsMatched()\r
227   {\r
228     return!didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;\r
229   }\r
230 \r
231   /** This is matchedFrom()+charsMatched() after a successful match,\r
232       or -1 otherwise. */\r
233   public int matchedTo()\r
234   {\r
235     return!didMatch_ ? -1 : matchFrom_ + charsMatched_;\r
236   }\r
237 \r
238   /** This returns the number of\r
239               backreferences (parenthesis) in the pattern,\r
240               i.e. the pattern "(ab)" has\r
241               one, the pattern "(a)(b)" has two, etc. */\r
242   public int numSubs()\r
243   {\r
244     return numSubs_;\r
245   }\r
246 \r
247   /** Contains true if the last match was successful. */\r
248   public boolean didMatch()\r
249   {\r
250     return didMatch_;\r
251   }\r
252 \r
253   /** An older name for matchedFrom. */\r
254   public int matchFrom()\r
255   {\r
256     return matchedFrom();\r
257   }\r
258 \r
259   /** An older name for stringMatched(). */\r
260   public String substring()\r
261   {\r
262     return stringMatched();\r
263   }\r
264 \r
265   /** An older name for matchedFrom. */\r
266   public int matchFrom(int i)\r
267   {\r
268     return matchedFrom(i);\r
269   }\r
270 \r
271   /** An older name for stringMatched. */\r
272   public String substring(int i)\r
273   {\r
274     return stringMatched(i);\r
275   }\r
276 }\r