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