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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// **********************************************************************
//
// Copyright (c) 2002
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
package IceInternal;
public final class StringUtil
{
//
// Return the index of the first character in str to
// appear in match, starting from 0. Returns -1 if none is
// found.
//
public static int
findFirstOf(String str, String match)
{
return findFirstOf(str, match, 0);
}
//
// Return the index of the first character in str to
// appear in match, starting from start. Returns -1 if none is
// found.
//
public static int
findFirstOf(String str, String match, int start)
{
final int len = str.length();
for(int i = start; i < len; i++)
{
char ch = str.charAt(i);
if(match.indexOf(ch) != -1)
{
return i;
}
}
return -1;
}
//
// Return the index of the first character in str which does
// not appear in match, starting from 0. Returns -1 if none is
// found.
//
public static int
findFirstNotOf(String str, String match)
{
return findFirstNotOf(str, match, 0);
}
//
// Return the index of the first character in str which does
// not appear in match, starting from start. Returns -1 if none is
// found.
//
public static int
findFirstNotOf(String str, String match, int start)
{
final int len = str.length();
for(int i = start; i < len; i++)
{
char ch = str.charAt(i);
if(match.indexOf(ch) == -1)
{
return i;
}
}
return -1;
}
private static void
encodeChar(byte b, StringBuffer s, String special)
{
switch(b)
{
case (byte)'\\':
{
s.append("\\\\");
break;
}
case (byte)'\'':
{
s.append("\\'");
break;
}
case (byte)'"':
{
s.append("\\\"");
break;
}
case (byte)'\b':
{
s.append("\\b");
break;
}
case (byte)'\f':
{
s.append("\\f");
break;
}
case (byte)'\n':
{
s.append("\\n");
break;
}
case (byte)'\r':
{
s.append("\\r");
break;
}
case (byte)'\t':
{
s.append("\\t");
break;
}
default:
{
if(b <= (byte)31 || b == (byte)127) // Bytes are signed in Java (-128 to 127)
{
s.append('\\');
String octal = Integer.toOctalString(b);
//
// Add leading zeroes so that we avoid problems during
// decoding. For example, consider the encoded string
// \0013 (i.e., a character with value 1 followed by
// the character '3'). If the leading zeroes were omitted,
// the result would be incorrectly interpreted by the
// decoder as a single character with value 11.
//
for(int j = octal.length(); j < 3; j++)
{
s.append('0');
}
s.append(octal);
}
else if(special != null && special.indexOf((char)b) != -1)
{
s.append('\\');
encodeChar(b, s, null);
}
else
{
s.append((char)b);
}
}
}
}
//
// Encodes a string into UTF8, escaping all characters outside the range [32-126]
// as well as any special characters determined by the caller.
//
public static String
encodeString(String s, String special)
{
byte[] bytes = null;
try
{
bytes = s.getBytes("UTF8");
}
catch(java.io.UnsupportedEncodingException ex)
{
assert(false);
return null;
}
StringBuffer result = new StringBuffer(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
encodeChar(bytes[i], result, special);
}
return result.toString();
}
//
// Decodes a UTF8 string. Decoding starts at the given start position
// (inclusive) and stops at the given end position (exclusive). Upon success,
// the result parameter holds the decoded string and true is returned.
// A return value of false indicates an error was detected in the encoding.
//
public static boolean
decodeString(String s, int start, int end, Ice.StringHolder result)
{
final int len = s.length();
assert(start >= 0);
assert(end <= len);
assert(start <= end);
byte[] bytes = new byte[len];
int bc = 0;
while(start < end)
{
char ch = s.charAt(start);
if(ch == '\\')
{
start++;
if(start == end)
{
return false; // Missing character
}
ch = s.charAt(start);
switch(ch)
{
case '\\':
{
bytes[bc++] = (byte)'\\';
break;
}
case '\'':
case '"':
{
bytes[bc++] = (byte)ch;
break;
}
case 'b':
{
bytes[bc++] = (byte)'\b';
break;
}
case 'f':
{
bytes[bc++] = (byte)'\f';
break;
}
case 'n':
{
bytes[bc++] = (byte)'\n';
break;
}
case 'r':
{
bytes[bc++] = (byte)'\r';
break;
}
case 't':
{
bytes[bc++] = (byte)'\t';
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
int count = 0;
int val = 0;
while(count < 3 && start < end && s.charAt(start) >= '0' && s.charAt(start) <= '9')
{
val <<= 3;
val |= s.charAt(start) - '0';
start++;
count++;
}
if(val > 255)
{
return false; // Octal value out of range
}
bytes[bc++] = (byte)val;
continue; // don't increment start
}
default:
{
byte b = (byte)ch;
if(b <= (byte)31 || b == (byte)127) // Bytes are signed in Java (-128 to 127)
{
return false; // Malformed encoding
}
else
{
bytes[bc++] = b;
}
}
}
}
else
{
bytes[bc++] = (byte)ch;
}
start++;
}
try
{
result.value = new String(bytes, 0, bc, "UTF8");
}
catch(java.io.UnsupportedEncodingException ex)
{
assert(false);
}
return true;
}
public static int
checkQuote(String s)
{
return checkQuote(s, 0);
}
//
// If a single or double quotation mark is found at the start position,
// then the position of the matching closing quote is returned. If no
// quotation mark is found at the start position, then 0 is returned.
// If no matching closing quote is found, then -1 is returned.
//
public static int
checkQuote(String s, int start)
{
char quoteChar = s.charAt(start);
if(quoteChar == '"' || quoteChar == '\'')
{
start++;
final int len = s.length();
int pos;
while(start < len && (pos = s.indexOf(quoteChar, start)) != -1)
{
if(s.charAt(pos - 1) != '\\')
{
return pos;
}
start = pos + 1;
}
return -1; // Unmatched quote
}
return 0; // Not quoted
}
}
|