1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.metricshub.wbem.sblim.cimclient.internal.uri;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
45
46
47
48
49
50 public class URIString implements CharSequence {
51
52
53
54
55
56
57
58
59 public URIString(char[] pCharArray, int pStart, int pEnd) {
60 if (pEnd < pStart) throw new IndexOutOfBoundsException("end:" + pEnd + " < start:" + pStart);
61 if (pEnd > pCharArray.length) throw new IndexOutOfBoundsException(
62 "charArray.length:" +
63 pCharArray.length +
64 ", start:" +
65 pStart +
66 ", end:" +
67 pEnd +
68 "\n" +
69 "end:" +
70 pEnd +
71 " > charArray.length:" +
72 pCharArray.length
73 );
74 this.iCA = pCharArray;
75 this.iInitStart = this.iStart = pStart;
76 this.iEnd = pEnd;
77 }
78
79
80
81
82
83
84 public URIString(char[] pCharArray) {
85 this(pCharArray, 0, pCharArray.length);
86 }
87
88
89
90
91
92
93 public URIString(String pStr) {
94 this(pStr.toCharArray());
95 }
96
97
98
99
100
101
102 public URIString(URIString pUriStr) {
103 set(pUriStr);
104 }
105
106
107
108
109
110
111 public void set(URIString pUriStr) {
112 this.iCA = pUriStr.iCA;
113 this.iStart = pUriStr.iStart;
114 this.iEnd = pUriStr.iEnd;
115 }
116
117
118
119
120
121
122 public URIString deepCopy() {
123 return new URIString(this);
124 }
125
126
127
128
129
130
131
132 public int find(char pChar) {
133 for (int i = 0; i < length(); i++) if (charAt(i) == pChar) return i;
134 return -1;
135 }
136
137
138
139
140 public char charAt(int pIdx) {
141 return this.iCA[this.iStart + pIdx];
142 }
143
144
145
146
147 public int length() {
148 return this.iEnd - this.iStart;
149 }
150
151
152
153
154 public CharSequence subSequence(int pStart, int pEnd) {
155 return new URIString(this.iCA, this.iStart + pStart, this.iStart + pEnd);
156 }
157
158
159
160
161 @Override
162 public String toString() {
163 return new String(this.iCA, this.iStart, length());
164 }
165
166
167
168
169
170
171 public String toInitString() {
172 return new String(this.iCA, this.iInitStart, this.iEnd - this.iInitStart);
173 }
174
175
176
177
178
179
180 public int getPos() {
181 return this.iStart - this.iInitStart;
182 }
183
184
185
186
187
188
189 public String markPosition() {
190 return markPosition(getPos());
191 }
192
193
194
195
196
197
198
199 public String markPosition(int pPos) {
200 StringBuffer buf = new StringBuffer();
201 buf.append(toInitString() + '\n');
202 for (int i = 0; i < pPos; i++) buf.append(' ');
203 buf.append("^\n");
204 return buf.toString();
205 }
206
207
208
209
210
211
212
213
214
215
216 public String substring(int pBeginIdx, int pEndIdx) {
217 return new String(this.iCA, this.iStart + pBeginIdx, pEndIdx - pBeginIdx);
218 }
219
220
221
222
223
224
225
226 public boolean startsWith(char pC) {
227 if (length() == 0) return false;
228 return charAt(0) == pC;
229 }
230
231
232
233
234 public void cutStarting() {
235 cutStarting(1);
236 }
237
238
239
240
241
242
243
244 public boolean cutStarting(char pC) {
245 if (startsWith(pC)) {
246 ++this.iStart;
247 return true;
248 }
249 return false;
250 }
251
252
253
254
255
256
257
258 public boolean cutStarting(CharSequence pSeq) {
259 if (pSeq.length() > length()) return false;
260 for (int i = 0; i < pSeq.length(); i++) if (charAt(i) != pSeq.charAt(i)) return false;
261 this.iStart += pSeq.length();
262 return true;
263 }
264
265
266
267
268
269
270
271
272 public boolean cutStarting(String pStr, boolean pIgnoreCase) {
273 if (pStr.length() > length()) return false;
274 String subStr = substring(0, pStr.length());
275 if (pIgnoreCase ? pStr.equalsIgnoreCase(subStr) : pStr.equals(subStr)) {
276 this.iStart += pStr.length();
277 return true;
278 }
279 return false;
280 }
281
282
283
284
285
286
287 public void cutStarting(int pChars) {
288 if (pChars > length()) throw new IndexOutOfBoundsException("chars:" + pChars + " > length()" + length());
289 this.iStart += pChars;
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304 public String removeTill(char pChar, boolean pRemoveChar, boolean pMustFound) {
305 int pos = find(pChar);
306 if (pos < 0) {
307 if (pMustFound) return null;
308 pos = length();
309 pRemoveChar = false;
310 }
311 String strVal = substring(0, pos);
312 cutStarting(pRemoveChar ? pos + 1 : pos);
313 return strVal;
314 }
315
316
317
318
319
320
321
322
323
324 public String removeTill(char pChar, boolean pRemoveChar) {
325 return removeTill(pChar, pRemoveChar, false);
326 }
327
328
329
330
331
332
333
334
335 public String removeTill(char pChar) {
336 return removeTill(pChar, false);
337 }
338
339
340
341
342
343
344
345
346
347 public boolean matchAndCut(Pattern pPat, int pGroup) {
348 this.iM = pPat.matcher(new URIString(this));
349 if (this.iM.matches()) {
350 this.iStart += this.iM.end(pGroup);
351 return true;
352 }
353 return false;
354 }
355
356
357
358
359
360 public String group(int pI) {
361 return this.iM.group(pI);
362 }
363
364 private char[] iCA;
365
366 private int iInitStart, iStart, iEnd;
367
368 private Matcher iM;
369 }