Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 11:45:30 GMT
  2. Package com.stevesoft.pat

File Replacer.java

 

Coverage histogram

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

Code metrics

50
111
29
3
372
276
61
0.55
3.83
9.67
2.1

Classes

Class Line # Actions
RegHolder 15 0 0
-1.0 -
CodeVal 27 3 2
0.00%
Replacer 58 108 59
0.378378437.8%
 

Contributing tests

This file is covered by 9 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    import jalview.util.MessageManager;
11   
12    import com.stevesoft.pat.wrap.StringWrap;
13   
14    /** Internally used class. */
 
15    class RegHolder
16    {
17    Regex me = null;
18   
19    RegHolder prev = null;
20    }
21   
22    /**
23    * Internally used class.
24    *
25    * @see CodeRule
26    */
 
27    class CodeVal
28    {
29    int pos;
30   
31    char code;
32   
 
33  0 toggle CodeVal(int p, char c)
34    {
35  0 pos = p;
36  0 code = c;
37    }
38   
 
39  0 toggle public String toString()
40    {
41  0 return "(" + pos + "," + code + ")";
42    }
43    }
44   
45    /**
46    * To use this class, first use either the getReplacer() method from Transformer
47    * or Regex. You can then use replaceAll, replaceFirst, etc. methods on the
48    * Replacer in the same way that you can from either of those two classes.
49    * <p>
50    * The only potential difference between using the methods of Replacer to do the
51    * replacing is that Replacer remembers changes to the replacing object between
52    * calls to replaceAll, replaceFirst etc. For details, see the example file
53    * <a href="http://javaregex.com/code/trans3.java.html">trans3.java</a>.
54    *
55    * @see com.stevesoft.pat.Transformer
56    * @see com.stevesoft.pat.Regex
57    */
 
58    public class Replacer
59    {
60    boolean first;
61   
62    /** Instantiate a new Replacer. */
 
63  18 toggle public Replacer()
64    {
65    }
66   
 
67  0 toggle public StringLike replaceFirstRegion(String s, Regex r, int start,
68    int end)
69    {
70  0 return replaceFirstRegion(new StringWrap(s), r, start, end);
71    }
72   
73    /**
74    * This method replaces the first occurence of the Regex in the String
75    * starting with position pos according to the Replacer rule of this object.
76    */
 
77  0 toggle public StringLike replaceFirstRegion(StringLike s, Regex r, int start,
78    int end)
79    {
80  0 first = true;
81  0 rh.me = r;
82  0 rh.prev = null;
83  0 return dorep(s, start, end);
84    }
85   
 
86  0 toggle public StringLike replaceFirst(StringLike s)
87    {
88  0 return replaceFirstRegion(s, 0, s.length());
89    }
90   
 
91  0 toggle public StringLike replaceFirstFrom(StringLike s, int start)
92    {
93  0 return replaceFirstRegion(s, start, s.length());
94    }
95   
 
96  0 toggle public StringLike replaceFirstRegion(StringLike s, int start, int end)
97    {
98  0 first = true;
99  0 return dorep(s, start, end);
100    }
101   
102    RegHolder rh = new RegHolder();
103   
 
104  152 toggle public StringLike replaceAllRegion(String s, Regex r, int start, int end)
105    {
106  152 return replaceAllRegion(new StringWrap(s), r, start, end);
107    }
108   
109    /**
110    * This method replaces all occurences of the Regex in the String starting
111    * with postition pos according to the Replacer rule of this object.
112    */
 
113  152 toggle public StringLike replaceAllRegion(StringLike s, Regex r, int start,
114    int end)
115    {
116  152 first = false;
117    // reset
118  152 rh.me = r;
119  152 rh.prev = null;
120  152 return dorep(s, start, end);
121    }
122   
 
123  0 toggle public StringLike replaceAll(StringLike s)
124    {
125  0 return replaceAllRegion(s, 0, s.length());
126    }
127   
 
128  0 toggle public StringLike replaceAllFrom(StringLike s, int start)
129    {
130  0 return replaceAllRegion(s, start, s.length());
131    }
132   
 
133  0 toggle public StringLike replaceAllRegion(StringLike s, int start, int end)
134    {
135  0 first = false;
136  0 return dorep(s, start, end);
137    }
138   
 
139  0 toggle public String replaceAll(String s)
140    {
141  0 return replaceAllRegion(new StringWrap(s), 0, s.length()).toString();
142    }
143   
 
144  0 toggle public String replaceAllFrom(String s, int start)
145    {
146  0 return replaceAllRegion(new StringWrap(s), start, s.length())
147    .toString();
148    }
149   
 
150  0 toggle public String replaceAllRegion(String s, int start, int end)
151    {
152  0 first = false;
153  0 return dorep(new StringWrap(s), start, end).toString();
154    }
155   
 
156  0 toggle final public boolean isSpecial(ReplaceRule x)
157    {
158  0 while (x != null)
159    {
160  0 if (x instanceof SpecialRule || (x instanceof RuleHolder
161    && ((RuleHolder) x).held instanceof SpecialRule))
162    {
163  0 return true;
164    }
165  0 x = x.next;
166    }
167  0 return false;
168    }
169   
 
170  0 toggle final public void apply1(RegRes rr)
171    {
172  0 rr.charsMatched_++;
173  0 apply(rr, null);
174  0 rr.charsMatched_--;
175    }
176   
 
177  152 toggle final StringLike dorep(StringLike s, int start, int end)
178    {
179  152 StringLike ret = s;
180  152 want_more_text = false;
181  152 lastMatchedTo = 0;
182  152 if (rh.me == null)
183    {
184  0 throw new NullPointerException(MessageManager
185    .getString("exception.replace_null_regex_pointer"));
186    }
187  152 if (rh.me._search(s, start, end))
188    {
189  3 int rmn = rh.me.matchedTo();
190  3 if (rh.me.charsMatched() == 0 && !isSpecial(rh.me.getReplaceRule()))
191    {
192  0 apply1(rh.me);
193  0 rmn++;
194    }
195  3 apply(rh.me);
196  3 if (!first)
197    {
198  3 for (int i = rmn; !want_more_text
199    && rh.me._search(s, i, end); i = rmn)
200    {
201  0 rmn = rh.me.matchedTo();
202  0 if (rh.me.charsMatched() == 0)
203    {
204  0 if (!isSpecial(rh.me.getReplaceRule()))
205    {
206  0 apply1(rh.me);
207    }
208  0 rmn++;
209    }
210  0 apply(rh.me);
211    }
212    }
213  3 ret = finish();
214  3 ret = ret == null ? s : ret;
215    }
216  152 return ret;
217    }
218   
219    StringBufferLike sb = null;
220   
221    StringLike src = null;
222   
223    int pos = 0;
224   
225    /**
226    * This method allows you to apply the results of several matches in a
227    * sequence to modify a String of text. Each call in the sequence must operate
228    * on the same piece of text and the matchedFrom() of each RegRes given to
229    * this method must be greater in value than the preceeding RegRes's
230    * matchedTo() value.
231    */
 
232  3 toggle public void apply(RegRes r, ReplaceRule rp)
233    {
234  3 if (rp == null || (rp.next == null && rp instanceof AmpersandRule))
235    {
236  0 return;
237    }
238  3 if (r.didMatch())
239    {
240  3 if (src == null)
241    {
242  3 src = r.getStringLike();
243    }
244  3 if (sb == null)
245    {
246  3 sb = new StringBufferLike(src.newStringBufferLike());
247    }
248  3 int rmf = r.matchedFrom();
249  72 for (int ii = pos; ii < rmf; ii++)
250    {
251  69 sb.append(src.charAt(ii));
252    }
253   
254  6 for (ReplaceRule x = rp; x != null; x = x.next)
255    {
256  3 x.apply(sb, r);
257  3 if (x instanceof SpecialRule)
258    {
259  0 if (x instanceof WantMoreTextReplaceRule && want_more_text_enable)
260    {
261  0 want_more_text = true;
262    }
263  0 else if (x instanceof PushRule)
264    {
265  0 RegHolder rh2 = new RegHolder();
266  0 rh2.me = ((PushRule) x).NewRule;
267  0 rh2.prev = rh;
268  0 rh = rh2;
269    }
270  0 else if (x instanceof PopRule)
271    {
272  0 if (rh.prev != null)
273    {
274  0 rh = rh.prev;
275    }
276    }
277  0 else if (x instanceof ChangeRule)
278    {
279  0 rh.me = ((ChangeRule) x).NewRule;
280    }
281    }
282    }
283  3 if (!want_more_text)
284    {
285  3 pos = r.matchedTo();
286    }
287    }
288    }
289   
290    boolean want_more_text = false, want_more_text_enable = false;
291   
 
292  0 toggle public boolean WantMoreText()
293    {
294  0 return want_more_text;
295    }
296   
297    /**
298    * Another form of apply, it is the same as apply(r,r.getReplaceRule()).
299    */
 
300  3 toggle public void apply(Regex r)
301    {
302  3 apply(r, r.getReplaceRule());
303    }
304   
305    /**
306    * This finishes the replacement, appending the right() part of the last
307    * RegRes given to substitute(RegRes). After this method is called, the
308    * Replace object is reset to perform another substitution. If no RegRes
309    * objects with a true didMatch are applied, this returns null.
310    */
 
311  3 toggle public StringLike finish()
312    {
313  3 if (src == null)
314    {
315  0 return null;
316    }
317    // sb.append(src.substring(pos,src.length()));
318  3 int s_end = src.length();
319  60 for (int ii = pos; ii < s_end; ii++)
320    {
321  57 sb.append(src.charAt(ii));
322    }
323  3 src = null;
324  3 lastMatchedTo = pos;
325  3 pos = 0;
326  3 StringLike retstr = sb.toStringLike();
327  3 sb = null;
328  3 return retstr;
329    }
330   
331    int lastMatchedTo = 0;
332   
 
333  0 toggle public Object clone()
334    {
335  0 Replacer r = new Replacer();
336  0 r.first = first;
337  0 r.src = src;
338  0 r.sb = sb;
339  0 r.pos = pos;
340  0 r.lastMatchedTo = lastMatchedTo;
341  0 r.want_more_text = want_more_text;
342  0 r.want_more_text_enable = want_more_text_enable;
343  0 r.rh.me = rh.me;
344  0 r.rh.prev = rh.prev;
345  0 return r;
346    }
347   
 
348  0 toggle public int lastMatchedTo()
349    {
350  0 return lastMatchedTo;
351    }
352   
 
353  0 toggle public Regex getRegex()
354    {
355  0 return rh.me;
356    }
357   
 
358  0 toggle public void setSource(StringLike sl)
359    {
360  0 src = sl;
361    }
362   
 
363  0 toggle public void setBuffer(StringBufferLike sbl)
364    {
365  0 sb = sbl;
366    }
367   
 
368  0 toggle public void setPos(int pos)
369    {
370  0 this.pos = pos;
371    }
372    }