1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package com.stevesoft.pat

File RegRes.java

 

Coverage histogram

../../../img/srcFileCovDistChart4.png
49% of files have more coverage

Code metrics

46
66
26
1
298
192
72
1.09
2.54
26
2.77

Classes

Class
Line #
Actions
RegRes 17 66 72
0.376811637.7%
 

Contributing tests

This file is covered by 101 tests. .

Source view

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  0 toggle public String getString()
27    {
28  0 return src.toString();
29    }
30   
31    /** Obtain the source StringLike object. */
 
32  3 toggle public StringLike getStringLike()
33    {
34  3 return src;
35    }
36   
37    protected int charsMatched_ = 0, matchFrom_ = 0, numSubs_ = 0;
38   
 
39  0 toggle public String toString()
40    {
41  0 StringBuffer sb = new StringBuffer();
42  0 sb.append("match=" + matchedFrom() + ":" + charsMatched());
43  0 if (!didMatch())
44    {
45  0 return sb.toString();
46    }
47  0 for (int i = 0; i < numSubs(); i++)
48    {
49  0 int n = i + 1;
50  0 sb.append(
51    " sub(" + n + ")=" + matchedFrom(n) + ":" + charsMatched(n));
52    }
53  0 return sb.toString();
54    }
55   
 
56  4921 toggle public RegRes()
57    {
58    }
59   
 
60  196 toggle public RegRes(RegRes r)
61    {
62  196 copyOutOf(r);
63    }
64   
 
65  196 toggle public void copyOutOf(RegRes r)
66    {
67  196 if (r.marks == null)
68    {
69  196 marks = null;
70    }
71    else
72    {
73  0 try
74    {
75    // marks = (Hashtable)r.marks.clone();
76  0 marks = new int[r.marks.length];
77  0 for (int i = 0; i < marks.length; i++)
78    {
79  0 marks[i] = r.marks[i];
80    }
81    // marks = (int[])r.marks.clone();
82    } catch (Throwable t)
83    {
84    }
85    }
86  196 didMatch_ = r.didMatch_;
87  196 src = r.src;
88  196 charsMatched_ = r.charsMatched_;
89  196 matchFrom_ = r.matchFrom_;
90  196 numSubs_ = r.numSubs_;
91    }
92   
 
93  0 toggle public Object clone()
94    {
95  0 return new RegRes(this);
96    }
97   
 
98  0 toggle public boolean equals(RegRes r)
99    {
100  0 if (charsMatched_ != r.charsMatched_ || matchFrom_ != r.matchFrom_
101    || didMatch_ != r.didMatch_ || numSubs_ != r.numSubs_
102    || !src.unwrap().equals(r.src.unwrap()))
103    {
104  0 return false;
105    }
106  0 if (marks == null && r.marks != null)
107    {
108  0 return false;
109    }
110  0 if (marks != null && r.marks == null)
111    {
112  0 return false;
113    }
114  0 for (int i = 1; i <= numSubs_; i++)
115    {
116  0 if (matchedFrom(i) != r.matchedFrom(i))
117    {
118  0 return false;
119    }
120  0 else if (charsMatched(i) != r.charsMatched(i))
121    {
122  0 return false;
123    }
124    }
125  0 return true;
126    }
127   
128    /** Obtains the match if successful, null otherwise. */
 
129  25 toggle public String stringMatched()
130    {
131  25 int mf = matchedFrom(), cm = charsMatched();
132  25 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  40727 toggle public int matchedFrom(int i)
141    {
142  40727 if (marks == null || i > numSubs_)
143    {
144  0 return -1;
145    }
146    // Integer in=(Integer)marks.get("left"+i);
147    // return in == null ? -1 : in.intValue();
148  40727 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  13389 toggle public int charsMatched(int i)
156    {
157  13389 if (marks == null || i > numSubs_ || !didMatch_)
158    {
159  0 return -1;
160    }
161    // Integer in = (Integer)marks.get("right"+i);
162    // int i2 = in==null ? -1 : in.intValue();
163  13389 int mf = matchedFrom(i);
164  13389 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  36 toggle public int matchedTo(int i)
172    {
173  36 if (marks == null || i > numSubs_ || !didMatch_)
174    {
175  0 return -1;
176    }
177  36 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  13389 toggle public String stringMatched(int i)
185    {
186  13389 int mf = matchedFrom(i), cm = charsMatched(i);
187  13389 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  0 toggle public String left()
196    {
197  0 int mf = matchedFrom();
198  0 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  0 toggle public String left(int i)
206    {
207  0 int mf = matchedFrom(i);
208  0 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  0 toggle public String right()
216    {
217  0 int mf = matchedFrom(), cm = charsMatched();
218  0 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  0 toggle public String right(int i)
227    {
228  0 int mf = matchedFrom(i), cm = charsMatched(i);
229  0 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  764 toggle public int matchedFrom()
238    {
239  764 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  100 toggle public int charsMatched()
247    {
248  100 return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
249    }
250   
251    /**
252    * This is matchedFrom()+charsMatched() after a successful match, or -1
253    * otherwise.
254    */
 
255  11650 toggle public int matchedTo()
256    {
257  11650 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  76 toggle public int numSubs()
265    {
266  76 return numSubs_;
267    }
268   
269    /** Contains true if the last match was successful. */
 
270  3 toggle public boolean didMatch()
271    {
272  3 return didMatch_;
273    }
274   
275    /** An older name for matchedFrom. */
 
276  0 toggle public int matchFrom()
277    {
278  0 return matchedFrom();
279    }
280   
281    /** An older name for stringMatched(). */
 
282  0 toggle public String substring()
283    {
284  0 return stringMatched();
285    }
286   
287    /** An older name for matchedFrom. */
 
288  0 toggle public int matchFrom(int i)
289    {
290  0 return matchedFrom(i);
291    }
292   
293    /** An older name for stringMatched. */
 
294  0 toggle public String substring(int i)
295    {
296  0 return stringMatched(i);
297    }
298    }