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