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