Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package com.stevesoft.pat

File RegRes.java

 

Coverage histogram

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

Code metrics

46
66
26
1
297
191
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 44 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(" sub(" + n + ")=" + matchedFrom(n) + ":" + charsMatched(n));
51    }
52  0 return sb.toString();
53    }
54   
 
55  3468 toggle public RegRes()
56    {
57    }
58   
 
59  161 toggle public RegRes(RegRes r)
60    {
61  161 copyOutOf(r);
62    }
63   
 
64  161 toggle public void copyOutOf(RegRes r)
65    {
66  161 if (r.marks == null)
67    {
68  161 marks = null;
69    }
70    else
71    {
72  0 try
73    {
74    // marks = (Hashtable)r.marks.clone();
75  0 marks = new int[r.marks.length];
76  0 for (int i = 0; i < marks.length; i++)
77    {
78  0 marks[i] = r.marks[i];
79    }
80    // marks = (int[])r.marks.clone();
81    } catch (Throwable t)
82    {
83    }
84    }
85  161 didMatch_ = r.didMatch_;
86  161 src = r.src;
87  161 charsMatched_ = r.charsMatched_;
88  161 matchFrom_ = r.matchFrom_;
89  161 numSubs_ = r.numSubs_;
90    }
91   
 
92  0 toggle public Object clone()
93    {
94  0 return new RegRes(this);
95    }
96   
 
97  0 toggle public boolean equals(RegRes r)
98    {
99  0 if (charsMatched_ != r.charsMatched_ || matchFrom_ != r.matchFrom_
100    || didMatch_ != r.didMatch_ || numSubs_ != r.numSubs_
101    || !src.unwrap().equals(r.src.unwrap()))
102    {
103  0 return false;
104    }
105  0 if (marks == null && r.marks != null)
106    {
107  0 return false;
108    }
109  0 if (marks != null && r.marks == null)
110    {
111  0 return false;
112    }
113  0 for (int i = 1; i <= numSubs_; i++)
114    {
115  0 if (matchedFrom(i) != r.matchedFrom(i))
116    {
117  0 return false;
118    }
119  0 else if (charsMatched(i) != r.charsMatched(i))
120    {
121  0 return false;
122    }
123    }
124  0 return true;
125    }
126   
127    /** Obtains the match if successful, null otherwise. */
 
128  22 toggle public String stringMatched()
129    {
130  22 int mf = matchedFrom(), cm = charsMatched();
131  22 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  39385 toggle public int matchedFrom(int i)
140    {
141  39385 if (marks == null || i > numSubs_)
142    {
143  0 return -1;
144    }
145    // Integer in=(Integer)marks.get("left"+i);
146    // return in == null ? -1 : in.intValue();
147  39385 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  13010 toggle public int charsMatched(int i)
155    {
156  13010 if (marks == null || i > numSubs_ || !didMatch_)
157    {
158  0 return -1;
159    }
160    // Integer in = (Integer)marks.get("right"+i);
161    // int i2 = in==null ? -1 : in.intValue();
162  13010 int mf = matchedFrom(i);
163  13010 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  36 toggle public int matchedTo(int i)
171    {
172  36 if (marks == null || i > numSubs_ || !didMatch_)
173    {
174  0 return -1;
175    }
176  36 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  13010 toggle public String stringMatched(int i)
184    {
185  13010 int mf = matchedFrom(i), cm = charsMatched(i);
186  13010 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  0 toggle public String left()
195    {
196  0 int mf = matchedFrom();
197  0 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  0 toggle public String left(int i)
205    {
206  0 int mf = matchedFrom(i);
207  0 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  0 toggle public String right()
215    {
216  0 int mf = matchedFrom(), cm = charsMatched();
217  0 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  0 toggle public String right(int i)
226    {
227  0 int mf = matchedFrom(i), cm = charsMatched(i);
228  0 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  454 toggle public int matchedFrom()
237    {
238  454 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  77 toggle public int charsMatched()
246    {
247  77 return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
248    }
249   
250    /**
251    * This is matchedFrom()+charsMatched() after a successful match, or -1
252    * otherwise.
253    */
 
254  10388 toggle public int matchedTo()
255    {
256  10388 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  76 toggle public int numSubs()
264    {
265  76 return numSubs_;
266    }
267   
268    /** Contains true if the last match was successful. */
 
269  3 toggle public boolean didMatch()
270    {
271  3 return didMatch_;
272    }
273   
274    /** An older name for matchedFrom. */
 
275  0 toggle public int matchFrom()
276    {
277  0 return matchedFrom();
278    }
279   
280    /** An older name for stringMatched(). */
 
281  0 toggle public String substring()
282    {
283  0 return stringMatched();
284    }
285   
286    /** An older name for matchedFrom. */
 
287  0 toggle public int matchFrom(int i)
288    {
289  0 return matchedFrom(i);
290    }
291   
292    /** An older name for stringMatched. */
 
293  0 toggle public String substring(int i)
294    {
295  0 return stringMatched(i);
296    }
297    }