1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>StringUtils.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">everge</a> > <a href="index.source.html" class="el_package">bjc.everge</a> > <span class="el_source">StringUtils.java</span></div><h1>StringUtils.java</h1><pre class="source lang-java linenums">package bjc.everge;
import java.util.*;
import java.util.regex.Pattern;
/**
* Utility methods for strings.
*
* @author Ben Culkin.
*/
<span class="nc" id="L12">public class StringUtils {</span>
/**
* Is the class in debug mode or not?
*/
<span class="fc" id="L16"> public static boolean isDebug = false;</span>
/**
* Split a string on every occurrence of a string not preceded by an escape.
*
* @param escape
* The escape that stops splitting.
* @param splat
* The string to split on. If this starts with the escape
* sequence, things will work poorly.
* @param inp
* The string to split.
* @return The string split as specified above.
*/
public static String[] escapeSplit(String escape, String splat, String inp) {
/*
* Special case some stuffs.
*/
// No input
<span class="pc bpc" id="L36" title="2 of 4 branches missed."> if (inp == null || inp.equals("")) {</span>
<span class="nc" id="L37"> return new String[] {</span>
inp
};
}
// Input does not contain any delimiters
<span class="fc bfc" id="L43" title="All 2 branches covered."> if (!inp.contains(splat)) {</span>
<span class="fc" id="L44"> return new String[] {</span>
inp
};
}
// No escape, so we can just split normally
<span class="pc bpc" id="L50" title="1 of 4 branches missed."> if (escape == null || escape.equals("")) {</span>
<span class="fc" id="L51"> return inp.split(Pattern.quote(splat));</span>
}
<span class="fc" id="L54"> List<String> ret = new ArrayList<>();</span>
/*
* Set up working variables
*/
// Copy of parameters
<span class="fc" id="L61"> String wrk = inp;</span>
// Index of first occurrence of split string
<span class="fc" id="L64"> int sidx = wrk.indexOf(splat);</span>
// Index of first occurrence of escaped string
<span class="fc" id="L66"> int eidx = wrk.indexOf(escape);</span>
// Was the last thing we saw an escape?
// This is used to enable the handling of escaping escapes
<span class="fc" id="L70"> boolean hadEscape = false;</span>
// As long as there an occurrence of either the split/escape
<span class="fc bfc" id="L73" title="All 4 branches covered."> while (sidx != -1 || eidx != -1) {</span>
// If there is an escape before a split
<span class="fc bfc" id="L75" title="All 4 branches covered."> if (eidx > 0 && eidx < sidx) {</span>
<span class="pc bpc" id="L76" title="1 of 2 branches missed."> if (isDebug)</span>
<span class="nc" id="L77"> System.err.printf("[TRACE] Considering escape\n");</span>
/*
* We potentially have an escaped sequence: - either an escaped split - or
* an escaped escape
*/
// Check for an escaped split
<span class="fc" id="L85"> boolean hasEscapedSplit = wrk.startsWith(splat, eidx + escape.length());</span>
<span class="fc bfc" id="L86" title="All 2 branches covered."> if (hasEscapedSplit) {</span>
// Skip over it
<span class="fc" id="L88"> int ofst = eidx + splat.length();</span>
<span class="fc" id="L90"> wrk = sliceStringL(wrk, eidx, escape.length());</span>
// Recalculate indexes
<span class="fc" id="L93"> sidx = wrk.indexOf(splat, ofst);</span>
<span class="fc" id="L94"> eidx = wrk.indexOf(escape, ofst);</span>
<span class="pc bpc" id="L96" title="1 of 2 branches missed."> if (isDebug) {</span>
<span class="nc" id="L97"> System.err.printf("[TRACE] After esc. split (%s) %d/%d\n", wrk,</span>
<span class="nc" id="L98"> sidx, eidx);</span>
}
// No pending escape
<span class="fc" id="L102"> hadEscape = false;</span>
<span class="fc" id="L103"> continue;</span>
}
// Check for an escaped escape
<span class="fc" id="L107"> boolean hasEscapedEscape = wrk.startsWith(escape, eidx + escape.length());</span>
<span class="pc bpc" id="L108" title="1 of 2 branches missed."> if (hasEscapedEscape) {</span>
// Skip over it
<span class="fc" id="L110"> int ofst = eidx + escape.length();</span>
<span class="fc" id="L112"> wrk = sliceStringL(wrk, eidx, escape.length());</span>
// Recalculate indexes
<span class="fc" id="L115"> sidx = wrk.indexOf(splat, ofst);</span>
<span class="fc" id="L116"> eidx = wrk.indexOf(escape, ofst);</span>
<span class="pc bpc" id="L118" title="1 of 2 branches missed."> if (isDebug) {</span>
<span class="nc" id="L119"> System.err.printf("[TRACE] After esc. escape (%s)/(%s) %d/%d\n",</span>
<span class="nc" id="L120"> wrk, wrk.substring(ofst), sidx, eidx);</span>
}
// There's a pending escape
<span class="fc" id="L124"> hadEscape = true;</span>
<span class="fc" id="L125"> continue;</span>
}
}
// Calculate whether there is currently an escape
<span class="fc" id="L130"> boolean hasEscape = false;</span>
{
<span class="fc" id="L132"> boolean tmp = wrk.startsWith(escape, sidx - escape.length());</span>
// boolean tmp = wrk.regionMatches(lo, escape, 0, escape.length());
<span class="fc bfc" id="L135" title="All 2 branches covered."> hasEscape = hadEscape ? false : tmp;</span>
}
// Handle anything that the pending escape may be applied to
<span class="pc bpc" id="L139" title="1 of 4 branches missed."> while (sidx != -1 && hasEscape) {</span>
<span class="nc" id="L140"> int oidx = wrk.indexOf(splat, sidx + escape.length());</span>
<span class="nc bnc" id="L142" title="All 2 branches missed."> if (oidx == -1)</span>
<span class="nc" id="L143"> break;</span>
<span class="nc" id="L145"> wrk = sliceStringL(wrk, oidx, escape.length());</span>
<span class="nc" id="L147"> sidx = oidx;</span>
<span class="nc" id="L149"> hasEscape = wrk.startsWith(escape, sidx - escape.length());</span>
<span class="nc" id="L150"> }</span>
<span class="fc bfc" id="L152" title="All 2 branches covered."> if (sidx == -1) {</span>
<span class="fc" id="L153"> break;</span>
}
<span class="fc" id="L156"> String tmp = wrk.substring(0, sidx);</span>
<span class="pc bpc" id="L158" title="1 of 2 branches missed."> if (isDebug) {</span>
<span class="nc" id="L159"> System.err.printf("[TRACE] Adding (%s) to returned splits; (%s)\n", tmp,</span>
<span class="nc" id="L160"> wrk.substring(sidx));</span>
}
<span class="fc" id="L163"> ret.add(tmp);</span>
<span class="pc bpc" id="L164" title="1 of 4 branches missed."> if (!tmp.equals("") && wrk.endsWith(tmp)) {</span>
<span class="nc" id="L165"> wrk = "";</span>
} else {
<span class="pc bpc" id="L167" title="1 of 2 branches missed."> if (wrk.indexOf(splat, sidx) != -1) {</span>
<span class="fc" id="L168"> wrk = wrk.substring(sidx + splat.length());</span>
} else {
<span class="nc" id="L170"> wrk = wrk.substring(sidx);</span>
}
}
<span class="fc" id="L174"> sidx = wrk.indexOf(splat);</span>
<span class="fc" id="L175"> eidx = wrk.indexOf(escape);</span>
<span class="fc" id="L177"> hadEscape = false;</span>
<span class="fc" id="L178"> }</span>
<span class="fc bfc" id="L180" title="All 2 branches covered."> if (!wrk.equals(""))</span>
<span class="fc" id="L181"> ret.add(wrk);</span>
<span class="fc" id="L183"> return ret.toArray(new String[0]);</span>
}
/**
* Slice a substring out of another string.
*
* @param strang
* The string to remove a substring from.
* @param lft
* The left-side of the substring to remove.
* @param rft
* The right-side of the substring to remove.
*
* @return The string, with the substring removed.
*/
public static String sliceString(String strang, int lft, int rft) {
<span class="fc" id="L199"> String leftSide = strang.substring(0, lft);</span>
<span class="fc" id="L200"> String rightSide = strang.substring(rft);</span>
<span class="fc" id="L202"> return leftSide + rightSide;</span>
}
/**
* Slice a substring out of another string.
*
* @param strang
* The string to remove a substring from.
* @param lft
* The left-side of the substring to remove.
* @param len
* The length of the substring to remove.
*
* @return The string, with the substring removed.
*/
public static String sliceStringL(String strang, int lft, int len) {
<span class="fc" id="L218"> String leftSide = strang.substring(0, lft);</span>
<span class="fc" id="L219"> String rightSide = strang.substring(lft + len);</span>
<span class="fc" id="L221"> return leftSide + rightSide;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.2.201808211720</span></div></body></html>
|