]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/util.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2001-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 11/19/2001 aliu Creation.
10 **********************************************************************
13 #include "unicode/unimatch.h"
14 #include "unicode/utf16.h"
15 #include "patternprops.h"
18 // Define UChar constants using hex for EBCDIC compatibility
20 static const UChar BACKSLASH
= 0x005C; /*\*/
21 static const UChar UPPER_U
= 0x0055; /*U*/
22 static const UChar LOWER_U
= 0x0075; /*u*/
23 static const UChar APOSTROPHE
= 0x0027; // '\''
24 static const UChar SPACE
= 0x0020; // ' '
26 // "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27 static const UChar DIGITS
[] = {
28 48,49,50,51,52,53,54,55,56,57,
29 65,66,67,68,69,70,71,72,73,74,
30 75,76,77,78,79,80,81,82,83,84,
36 UnicodeString
& ICU_Utility::appendNumber(UnicodeString
& result
, int32_t n
,
37 int32_t radix
, int32_t minDigits
) {
38 if (radix
< 2 || radix
> 36) {
40 return result
.append((UChar
)63/*?*/);
45 result
.append((UChar
)45/*-*/);
47 // First determine the number of digits
55 // Now generate the digits
56 while (--minDigits
> 0) {
57 result
.append(DIGITS
[0]);
60 int32_t digit
= n
/ r
;
61 result
.append(DIGITS
[digit
]);
69 * Return true if the character is NOT printable ASCII.
71 UBool
ICU_Utility::isUnprintable(UChar32 c
) {
72 return !(c
>= 0x20 && c
<= 0x7E);
76 * Escape unprintable characters using \uxxxx notation for U+0000 to
77 * U+FFFF and \Uxxxxxxxx for U+10000 and above. If the character is
78 * printable ASCII, then do nothing and return FALSE. Otherwise,
79 * append the escaped notation and return TRUE.
81 UBool
ICU_Utility::escapeUnprintable(UnicodeString
& result
, UChar32 c
) {
82 if (isUnprintable(c
)) {
83 result
.append(BACKSLASH
);
85 result
.append(UPPER_U
);
86 result
.append(DIGITS
[0xF&(c
>>28)]);
87 result
.append(DIGITS
[0xF&(c
>>24)]);
88 result
.append(DIGITS
[0xF&(c
>>20)]);
89 result
.append(DIGITS
[0xF&(c
>>16)]);
91 result
.append(LOWER_U
);
93 result
.append(DIGITS
[0xF&(c
>>12)]);
94 result
.append(DIGITS
[0xF&(c
>>8)]);
95 result
.append(DIGITS
[0xF&(c
>>4)]);
96 result
.append(DIGITS
[0xF&c
]);
103 * Returns the index of a character, ignoring quoted text.
104 * For example, in the string "abc'hide'h", the 'h' in "hide" will not be
105 * found by a search for 'h'.
107 // FOR FUTURE USE. DISABLE FOR NOW for coverage reasons.
109 int32_t ICU_Utility::quotedIndexOf(const UnicodeString& text,
110 int32_t start, int32_t limit,
112 for (int32_t i=start; i<limit; ++i) {
113 UChar c = text.charAt(i);
114 if (c == BACKSLASH) {
116 } else if (c == APOSTROPHE) {
118 && text.charAt(i) != APOSTROPHE) {}
119 } else if (c == charToFind) {
128 * Skip over a sequence of zero or more white space characters at pos.
129 * @param advance if true, advance pos to the first non-white-space
130 * character at or after pos, or str.length(), if there is none.
131 * Otherwise leave pos unchanged.
132 * @return the index of the first non-white-space character at or
133 * after pos, or str.length(), if there is none.
135 int32_t ICU_Utility::skipWhitespace(const UnicodeString
& str
, int32_t& pos
,
138 const UChar
* s
= str
.getBuffer();
139 p
= (int32_t)(PatternProps::skipWhiteSpace(s
+ p
, str
.length() - p
) - s
);
147 * Skip over Pattern_White_Space in a Replaceable.
148 * Skipping may be done in the forward or
149 * reverse direction. In either case, the leftmost index will be
150 * inclusive, and the rightmost index will be exclusive. That is,
151 * given a range defined as [start, limit), the call
152 * skipWhitespace(text, start, limit) will advance start past leading
153 * whitespace, whereas the call skipWhitespace(text, limit, start),
154 * will back up limit past trailing whitespace.
155 * @param text the text to be analyzed
156 * @param pos either the start or limit of a range of 'text', to skip
157 * leading or trailing whitespace, respectively
158 * @param stop either the limit or start of a range of 'text', to skip
159 * leading or trailing whitespace, respectively
160 * @return the new start or limit, depending on what was passed in to
163 //?FOR FUTURE USE. DISABLE FOR NOW for coverage reasons.
164 //?int32_t ICU_Utility::skipWhitespace(const Replaceable& text,
165 //? int32_t pos, int32_t stop) {
167 //? UBool isForward = (stop >= pos);
169 //? if (!isForward) {
170 //? --pos; // pos is a limit, so back up by one
173 //? while (pos != stop &&
174 //? PatternProps::isWhiteSpace(c = text.char32At(pos))) {
176 //? pos += U16_LENGTH(c);
178 //? pos -= U16_LENGTH(c);
182 //? if (!isForward) {
183 //? ++pos; // make pos back into a limit
190 * Parse a single non-whitespace character 'ch', optionally
191 * preceded by whitespace.
192 * @param id the string to be parsed
193 * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the
194 * offset of the first character to be parsed. On output, pos[0]
195 * is the index after the last parsed character. If the parse
196 * fails, pos[0] will be unchanged.
197 * @param ch the non-whitespace character to be parsed.
198 * @return true if 'ch' is seen preceded by zero or more
199 * whitespace characters.
201 UBool
ICU_Utility::parseChar(const UnicodeString
& id
, int32_t& pos
, UChar ch
) {
203 skipWhitespace(id
, pos
, TRUE
);
204 if (pos
== id
.length() ||
205 id
.charAt(pos
) != ch
) {
214 * Parse a pattern string within the given Replaceable and a parsing
215 * pattern. Characters are matched literally and case-sensitively
216 * except for the following special characters:
218 * ~ zero or more Pattern_White_Space chars
220 * If end of pattern is reached with all matches along the way,
221 * pos is advanced to the first unparsed index and returned.
222 * Otherwise -1 is returned.
223 * @param pat pattern that controls parsing
224 * @param text text to be parsed, starting at index
225 * @param index offset to first character to parse
226 * @param limit offset after last character to parse
227 * @return index after last parsed character, or -1 on parse failure.
229 int32_t ICU_Utility::parsePattern(const UnicodeString
& pat
,
230 const Replaceable
& text
,
235 // empty pattern matches immediately
236 if (ipat
== pat
.length()) {
240 UChar32 cpat
= pat
.char32At(ipat
);
242 while (index
< limit
) {
243 UChar32 c
= text
.char32At(index
);
246 if (cpat
== 126 /*~*/) {
247 if (PatternProps::isWhiteSpace(c
)) {
248 index
+= U16_LENGTH(c
);
251 if (++ipat
== pat
.length()) {
252 return index
; // success; c unparsed
254 // fall thru; process c again with next cpat
259 else if (c
== cpat
) {
260 index
+= U16_LENGTH(c
);
261 ipat
+= U16_LENGTH(cpat
);
262 if (ipat
== pat
.length()) {
263 return index
; // success; c parsed
265 // fall thru; get next cpat
268 // match failure of literal
273 cpat
= pat
.char32At(ipat
);
276 return -1; // text ended before end of pat
279 int32_t ICU_Utility::parseAsciiInteger(const UnicodeString
& str
, int32_t& pos
) {
282 while (pos
< str
.length() && (c
= str
.charAt(pos
)) >= u
'0' && c
<= u
'9') {
283 result
= result
* 10 + (c
- u
'0');
290 * Append a character to a rule that is being built up. To flush
291 * the quoteBuf to rule, make one final call with isLiteral == TRUE.
292 * If there is no final character, pass in (UChar32)-1 as c.
293 * @param rule the string to append the character to
294 * @param c the character to append, or (UChar32)-1 if none.
295 * @param isLiteral if true, then the given character should not be
296 * quoted or escaped. Usually this means it is a syntactic element
298 * @param escapeUnprintable if true, then unprintable characters
299 * should be escaped using \uxxxx or \Uxxxxxxxx. These escapes will
300 * appear outside of quotes.
301 * @param quoteBuf a buffer which is used to build up quoted
302 * substrings. The caller should initially supply an empty buffer,
303 * and thereafter should not modify the buffer. The buffer should be
304 * cleared out by, at the end, calling this method with a literal
307 void ICU_Utility::appendToRule(UnicodeString
& rule
,
310 UBool escapeUnprintable
,
311 UnicodeString
& quoteBuf
) {
312 // If we are escaping unprintables, then escape them outside
313 // quotes. \u and \U are not recognized within quotes. The same
314 // logic applies to literals, but literals are never escaped.
316 (escapeUnprintable
&& ICU_Utility::isUnprintable(c
))) {
317 if (quoteBuf
.length() > 0) {
318 // We prefer backslash APOSTROPHE to double APOSTROPHE
319 // (more readable, less similar to ") so if there are
320 // double APOSTROPHEs at the ends, we pull them outside
323 // If the first thing in the quoteBuf is APOSTROPHE
324 // (doubled) then pull it out.
325 while (quoteBuf
.length() >= 2 &&
326 quoteBuf
.charAt(0) == APOSTROPHE
&&
327 quoteBuf
.charAt(1) == APOSTROPHE
) {
328 rule
.append(BACKSLASH
).append(APOSTROPHE
);
329 quoteBuf
.remove(0, 2);
331 // If the last thing in the quoteBuf is APOSTROPHE
332 // (doubled) then remove and count it and add it after.
333 int32_t trailingCount
= 0;
334 while (quoteBuf
.length() >= 2 &&
335 quoteBuf
.charAt(quoteBuf
.length()-2) == APOSTROPHE
&&
336 quoteBuf
.charAt(quoteBuf
.length()-1) == APOSTROPHE
) {
337 quoteBuf
.truncate(quoteBuf
.length()-2);
340 if (quoteBuf
.length() > 0) {
341 rule
.append(APOSTROPHE
);
342 rule
.append(quoteBuf
);
343 rule
.append(APOSTROPHE
);
344 quoteBuf
.truncate(0);
346 while (trailingCount
-- > 0) {
347 rule
.append(BACKSLASH
).append(APOSTROPHE
);
350 if (c
!= (UChar32
)-1) {
351 /* Since spaces are ignored during parsing, they are
352 * emitted only for readability. We emit one here
353 * only if there isn't already one at the end of the
357 int32_t len
= rule
.length();
358 if (len
> 0 && rule
.charAt(len
-1) != c
) {
361 } else if (!escapeUnprintable
|| !ICU_Utility::escapeUnprintable(rule
, c
)) {
367 // Escape ' and '\' and don't begin a quote just for them
368 else if (quoteBuf
.length() == 0 &&
369 (c
== APOSTROPHE
|| c
== BACKSLASH
)) {
370 rule
.append(BACKSLASH
);
374 // Specials (printable ascii that isn't [0-9a-zA-Z]) and
375 // whitespace need quoting. Also append stuff to quotes if we are
376 // building up a quoted substring already.
377 else if (quoteBuf
.length() > 0 ||
378 (c
>= 0x0021 && c
<= 0x007E &&
379 !((c
>= 0x0030/*'0'*/ && c
<= 0x0039/*'9'*/) ||
380 (c
>= 0x0041/*'A'*/ && c
<= 0x005A/*'Z'*/) ||
381 (c
>= 0x0061/*'a'*/ && c
<= 0x007A/*'z'*/))) ||
382 PatternProps::isWhiteSpace(c
)) {
384 // Double ' within a quote
385 if (c
== APOSTROPHE
) {
390 // Otherwise just append
396 void ICU_Utility::appendToRule(UnicodeString
& rule
,
397 const UnicodeString
& text
,
399 UBool escapeUnprintable
,
400 UnicodeString
& quoteBuf
) {
401 for (int32_t i
=0; i
<text
.length(); ++i
) {
402 appendToRule(rule
, text
[i
], isLiteral
, escapeUnprintable
, quoteBuf
);
407 * Given a matcher reference, which may be null, append its
408 * pattern as a literal to the given rule.
410 void ICU_Utility::appendToRule(UnicodeString
& rule
,
411 const UnicodeMatcher
* matcher
,
412 UBool escapeUnprintable
,
413 UnicodeString
& quoteBuf
) {
414 if (matcher
!= NULL
) {
416 appendToRule(rule
, matcher
->toPattern(pat
, escapeUnprintable
),
417 TRUE
, escapeUnprintable
, quoteBuf
);