Clover icon

Coverage Report

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

File StrPos.java

 

Coverage histogram

../../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

20
53
13
1
192
127
28
0.53
4.08
13
2.15

Classes

Class Line # Actions
StrPos 17 53 28
0.837209383.7%
 

Contributing tests

This file is covered by 41 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    * StrPos is used internally by regex to parse the regular expression.
16    */
 
17    public class StrPos
18    {
19    String s;
20   
21    int pos;
22   
23    /** Return the position in the string pointed to */
 
24  0 toggle public int pos()
25    {
26  0 return pos;
27    }
28   
29    /** This contains the escape character, which is \ by default. */
30    public char esc = Pattern.ESC;
31   
32    char c;
33   
34    /** Returns the current, possibly escaped, character. */
 
35  0 toggle public char thisChar()
36    {
37  0 return c;
38    }
39   
40    boolean dontMatch, eos;
41   
42    /** tell whether we are at end of string */
 
43  0 toggle public boolean eos()
44    {
45  0 return eos;
46    }
47   
48    /** initialize a StrPos from another StrPos. */
 
49  60028 toggle public StrPos(StrPos sp)
50    {
51  60028 dup(sp);
52    }
53   
54    /** copy a StrPos from sp to this. */
 
55  60190 toggle public void dup(StrPos sp)
56    {
57  60190 s = sp.s;
58  60190 pos = sp.pos;
59  60190 c = sp.c;
60  60190 dontMatch = sp.dontMatch;
61  60190 eos = sp.eos;
62    }
63   
64    /**
65    * Initialize a StrPos by giving it a String, and a position within the
66    * String.
67    */
 
68  3016 toggle public StrPos(String s, int pos)
69    {
70  3016 this.s = s;
71  3016 this.pos = pos - 1;
72  3016 inc();
73    }
74   
75    /**
76    * Advance the place where StrPos points within the String. Counts a backslash
77    * as part of the next character.
78    */
 
79  43662 toggle public StrPos inc()
80    {
81  43662 pos++;
82  43662 if (pos >= s.length())
83    {
84  1543 eos = true;
85  1543 return this;
86    }
87  42119 eos = false;
88  42119 c = s.charAt(pos);
89  42119 if (c == esc && pos + 1 < s.length())
90    {
91  6033 pos++;
92  6033 c = s.charAt(pos);
93  6033 if (c != esc)
94    {
95  6017 dontMatch = true;
96    }
97    else
98    {
99  16 dontMatch = false;
100    }
101    }
102    else
103    {
104  36086 dontMatch = false;
105    }
106  42119 return this;
107    }
108   
109    /**
110    * Compare the (possibly escaped) character pointed to by StrPos. Return true
111    * if they are the same, but lways return if character pointed to is escaped.
112    */
 
113  151792 toggle public boolean match(char ch)
114    {
115  151792 if (dontMatch || eos)
116    {
117  22860 return false;
118    }
119  128932 return c == ch;
120    }
121   
122    /** As match, but only matches if the character is escaped. */
 
123  78477 toggle public boolean escMatch(char ch)
124    {
125  78477 if (!dontMatch || eos)
126    {
127  70773 return false;
128    }
129  7704 return c == ch;
130    }
131   
132    /**
133    * Returns true if the current character is escaped (preceeded by "\").
134    */
 
135  0 toggle public boolean escaped()
136    {
137  0 return dontMatch;
138    }
139   
140    /**
141    * Increment the string pointer by each character in
142    *
143    * <pre>
144    * st
145    * </pre>
146    *
147    * that matches a non-escaped character.
148    */
 
149  49320 toggle public boolean incMatch(String st)
150    {
151  49320 StrPos sp = new StrPos(this);
152  49320 int i;
153  59490 for (i = 0; i < st.length(); i++)
154    {
155  59451 if (!sp.match(st.charAt(i)))
156    {
157  49281 return false;
158    }
159  10170 sp.inc();
160    }
161  39 dup(sp);
162  39 return true;
163    }
164   
165    /** Read in an integer. */
 
166  123 toggle public patInt getPatInt()
167    {
168  123 if (incMatch("inf"))
169    {
170  0 return new patInf();
171    }
172  123 int i, cnt = 0;
173  123 StrPos sp = new StrPos(this);
174  249 for (i = 0; !sp.eos && sp.c >= '0' && sp.c <= '9'; i++)
175    {
176  126 cnt = 10 * cnt + sp.c - '0';
177  126 sp.inc();
178    }
179  123 if (i == 0)
180    {
181  0 return null;
182    }
183  123 dup(sp);
184  123 return new patInt(cnt);
185    }
186   
187    /** get the string that we are processing. */
 
188  0 toggle public String getString()
189    {
190  0 return s;
191    }
192    };