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