]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/util.cpp
2 **********************************************************************
3 * Copyright (c) 2001-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/19/2001 aliu Creation.
8 **********************************************************************
12 #include "unicode/uchar.h"
13 #include "unicode/unimatch.h"
16 // Define UChar constants using hex for EBCDIC compatibility
18 static const UChar BACKSLASH
= 0x005C; /*\*/
19 static const UChar UPPER_U
= 0x0055; /*U*/
20 static const UChar LOWER_U
= 0x0075; /*u*/
21 static const UChar APOSTROPHE
= 0x0027; // '\''
22 static const UChar SPACE
= 0x0020; // ' '
24 // "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
25 static const UChar DIGITS
[] = {
26 48,49,50,51,52,53,54,55,56,57,
27 65,66,67,68,69,70,71,72,73,74,
28 75,76,77,78,79,80,81,82,83,84,
32 UnicodeString
& ICU_Utility::appendNumber(UnicodeString
& result
, int32_t n
,
33 int32_t radix
, int32_t minDigits
) {
34 if (radix
< 2 || radix
> 36) {
36 return result
.append((UChar
)63/*?*/);
41 result
.append((UChar
)45/*-*/);
43 // First determine the number of digits
51 // Now generate the digits
52 while (--minDigits
> 0) {
53 result
.append(DIGITS
[0]);
56 int32_t digit
= n
/ r
;
57 result
.append(DIGITS
[digit
]);
64 static const UChar HEX
[16] = {48,49,50,51,52,53,54,55, // 0-7
65 56,57,65,66,67,68,69,70}; // 8-9 A-F
68 * Return true if the character is NOT printable ASCII.
70 UBool
ICU_Utility::isUnprintable(UChar32 c
) {
71 return !(c
>= 0x20 && c
<= 0x7E);
75 * Escape unprintable characters using \uxxxx notation for U+0000 to
76 * U+FFFF and \Uxxxxxxxx for U+10000 and above. If the character is
77 * printable ASCII, then do nothing and return FALSE. Otherwise,
78 * append the escaped notation and return TRUE.
80 UBool
ICU_Utility::escapeUnprintable(UnicodeString
& result
, UChar32 c
) {
81 if (isUnprintable(c
)) {
82 result
.append(BACKSLASH
);
84 result
.append(UPPER_U
);
85 result
.append(HEX
[0xF&(c
>>28)]);
86 result
.append(HEX
[0xF&(c
>>24)]);
87 result
.append(HEX
[0xF&(c
>>20)]);
88 result
.append(HEX
[0xF&(c
>>16)]);
90 result
.append(LOWER_U
);
92 result
.append(HEX
[0xF&(c
>>12)]);
93 result
.append(HEX
[0xF&(c
>>8)]);
94 result
.append(HEX
[0xF&(c
>>4)]);
95 result
.append(HEX
[0xF&c
]);
102 * Returns the index of a character, ignoring quoted text.
103 * For example, in the string "abc'hide'h", the 'h' in "hide" will not be
104 * found by a search for 'h'.
106 // FOR FUTURE USE. DISABLE FOR NOW for coverage reasons.
108 int32_t ICU_Utility::quotedIndexOf(const UnicodeString& text,
109 int32_t start, int32_t limit,
111 for (int32_t i=start; i<limit; ++i) {
112 UChar c = text.charAt(i);
113 if (c == BACKSLASH) {
115 } else if (c == APOSTROPHE) {
117 && text.charAt(i) != APOSTROPHE) {}
118 } else if (c == charToFind) {
127 * Skip over a sequence of zero or more white space characters at pos.
128 * @param advance if true, advance pos to the first non-white-space
129 * character at or after pos, or str.length(), if there is none.
130 * Otherwise leave pos unchanged.
131 * @return the index of the first non-white-space character at or
132 * after pos, or str.length(), if there is none.
134 int32_t ICU_Utility::skipWhitespace(const UnicodeString
& str
, int32_t& pos
,
137 while (p
< str
.length()) {
138 UChar32 c
= str
.char32At(p
);
139 if (!uprv_isRuleWhiteSpace(c
)) {
142 p
+= UTF_CHAR_LENGTH(c
);
151 * Skip over whitespace in a Replaceable. Whitespace is defined by
152 * uprv_isRuleWhiteSpace(). Skipping may be done in the forward or
153 * reverse direction. In either case, the leftmost index will be
154 * inclusive, and the rightmost index will be exclusive. That is,
155 * given a range defined as [start, limit), the call
156 * skipWhitespace(text, start, limit) will advance start past leading
157 * whitespace, whereas the call skipWhitespace(text, limit, start),
158 * will back up limit past trailing whitespace.
159 * @param text the text to be analyzed
160 * @param pos either the start or limit of a range of 'text', to skip
161 * leading or trailing whitespace, respectively
162 * @param stop either the limit or start of a range of 'text', to skip
163 * leading or trailing whitespace, respectively
164 * @return the new start or limit, depending on what was passed in to
167 //?FOR FUTURE USE. DISABLE FOR NOW for coverage reasons.
168 //?int32_t ICU_Utility::skipWhitespace(const Replaceable& text,
169 //? int32_t pos, int32_t stop) {
171 //? UBool isForward = (stop >= pos);
173 //? if (!isForward) {
174 //? --pos; // pos is a limit, so back up by one
177 //? while (pos != stop &&
178 //? uprv_isRuleWhiteSpace(c = text.char32At(pos))) {
180 //? pos += UTF_CHAR_LENGTH(c);
182 //? pos -= UTF_CHAR_LENGTH(c);
186 //? if (!isForward) {
187 //? ++pos; // make pos back into a limit
194 * Parse a single non-whitespace character 'ch', optionally
195 * preceded by whitespace.
196 * @param id the string to be parsed
197 * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the
198 * offset of the first character to be parsed. On output, pos[0]
199 * is the index after the last parsed character. If the parse
200 * fails, pos[0] will be unchanged.
201 * @param ch the non-whitespace character to be parsed.
202 * @return true if 'ch' is seen preceded by zero or more
203 * whitespace characters.
205 UBool
ICU_Utility::parseChar(const UnicodeString
& id
, int32_t& pos
, UChar ch
) {
207 skipWhitespace(id
, pos
, TRUE
);
208 if (pos
== id
.length() ||
209 id
.charAt(pos
) != ch
) {
218 * Parse a pattern string starting at offset pos. Keywords are
219 * matched case-insensitively. Spaces may be skipped and may be
220 * optional or required. Integer values may be parsed, and if
221 * they are, they will be returned in the given array. If
222 * successful, the offset of the next non-space character is
223 * returned. On failure, -1 is returned.
224 * @param pattern must only contain lowercase characters, which
225 * will match their uppercase equivalents as well. A space
226 * character matches one or more required spaces. A '~' character
227 * matches zero or more optional spaces. A '#' character matches
228 * an integer and stores it in parsedInts, which the caller must
229 * ensure has enough capacity.
230 * @param parsedInts array to receive parsed integers. Caller
231 * must ensure that parsedInts.length is >= the number of '#'
232 * signs in 'pattern'.
233 * @return the position after the last character parsed, or -1 if
236 int32_t ICU_Utility::parsePattern(const UnicodeString
& rule
, int32_t pos
, int32_t limit
,
237 const UnicodeString
& pattern
, int32_t* parsedInts
) {
238 // TODO Update this to handle surrogates
240 int32_t intCount
= 0; // number of integers parsed
241 for (int32_t i
=0; i
<pattern
.length(); ++i
) {
242 UChar cpat
= pattern
.charAt(i
);
249 c
= rule
.charAt(pos
++);
250 if (!uprv_isRuleWhiteSpace(c
)) {
253 // FALL THROUGH to skipWhitespace
255 pos
= skipWhitespace(rule
, pos
);
259 parsedInts
[intCount
++] = parseInteger(rule
, p
, limit
);
261 // Syntax error; failed to parse integer
270 c
= (UChar
) u_tolower(rule
.charAt(pos
++));
281 * Parse a pattern string within the given Replaceable and a parsing
282 * pattern. Characters are matched literally and case-sensitively
283 * except for the following special characters:
285 * ~ zero or more uprv_isRuleWhiteSpace chars
287 * If end of pattern is reached with all matches along the way,
288 * pos is advanced to the first unparsed index and returned.
289 * Otherwise -1 is returned.
290 * @param pat pattern that controls parsing
291 * @param text text to be parsed, starting at index
292 * @param index offset to first character to parse
293 * @param limit offset after last character to parse
294 * @return index after last parsed character, or -1 on parse failure.
296 int32_t ICU_Utility::parsePattern(const UnicodeString
& pat
,
297 const Replaceable
& text
,
302 // empty pattern matches immediately
303 if (ipat
== pat
.length()) {
307 UChar32 cpat
= pat
.char32At(ipat
);
309 while (index
< limit
) {
310 UChar32 c
= text
.char32At(index
);
313 if (cpat
== 126 /*~*/) {
314 if (uprv_isRuleWhiteSpace(c
)) {
315 index
+= UTF_CHAR_LENGTH(c
);
318 if (++ipat
== pat
.length()) {
319 return index
; // success; c unparsed
321 // fall thru; process c again with next cpat
326 else if (c
== cpat
) {
327 index
+= UTF_CHAR_LENGTH(c
);
328 ipat
+= UTF_CHAR_LENGTH(cpat
);
329 if (ipat
== pat
.length()) {
330 return index
; // success; c parsed
332 // fall thru; get next cpat
335 // match failure of literal
340 cpat
= pat
.char32At(ipat
);
343 return -1; // text ended before end of pat
347 * Parse an integer at pos, either of the form \d+ or of the form
348 * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex,
350 * @param pos INPUT-OUTPUT parameter. On input, the first
351 * character to parse. On output, the character after the last
354 int32_t ICU_Utility::parseInteger(const UnicodeString
& rule
, int32_t& pos
, int32_t limit
) {
360 if (p
< limit
&& rule
.charAt(p
) == 48 /*0*/) {
361 if (p
+1 < limit
&& (rule
.charAt(p
+1) == 0x78 /*x*/ || rule
.charAt(p
+1) == 0x58 /*X*/)) {
373 int32_t d
= u_digit(rule
.charAt(p
++), radix
);
379 int32_t v
= (value
* radix
) + d
;
381 // If there are too many input digits, at some point
382 // the value will go negative, e.g., if we have seen
383 // "0x8000000" already and there is another '0', when
384 // we parse the next 0 the value will go negative.
396 * Parse a Unicode identifier from the given string at the given
397 * position. Return the identifier, or an empty string if there
399 * @param str the string to parse
400 * @param pos INPUT-OUPUT parameter. On INPUT, pos is the
401 * first character to examine. It must be less than str.length(),
402 * and it must not point to a whitespace character. That is, must
403 * have pos < str.length() and
404 * !uprv_isRuleWhiteSpace(str.char32At(pos)). On
405 * OUTPUT, the position after the last parsed character.
406 * @return the Unicode identifier, or an empty string if there is
407 * no valid identifier at pos.
409 UnicodeString
ICU_Utility::parseUnicodeIdentifier(const UnicodeString
& str
, int32_t& pos
) {
410 // assert(pos < str.length());
411 // assert(!uprv_isRuleWhiteSpace(str.char32At(pos)));
414 while (p
< str
.length()) {
415 UChar32 ch
= str
.char32At(p
);
416 if (buf
.length() == 0) {
417 if (u_isIDStart(ch
)) {
424 if (u_isIDPart(ch
)) {
430 p
+= UTF_CHAR_LENGTH(ch
);
437 * Parse an unsigned 31-bit integer at the given offset. Use
438 * UCharacter.digit() to parse individual characters into digits.
439 * @param text the text to be parsed
440 * @param pos INPUT-OUTPUT parameter. On entry, pos[0] is the
441 * offset within text at which to start parsing; it should point
442 * to a valid digit. On exit, pos[0] is the offset after the last
443 * parsed character. If the parse failed, it will be unchanged on
444 * exit. Must be >= 0 on entry.
445 * @param radix the radix in which to parse; must be >= 2 and <=
447 * @return a non-negative parsed number, or -1 upon parse failure.
448 * Parse fails if there are no digits, that is, if pos[0] does not
449 * point to a valid digit on entry, or if the number to be parsed
450 * does not fit into a 31-bit unsigned integer.
452 int32_t ICU_Utility::parseNumber(const UnicodeString
& text
,
453 int32_t& pos
, int8_t radix
) {
454 // assert(pos[0] >= 0);
455 // assert(radix >= 2);
456 // assert(radix <= 36);
459 while (p
< text
.length()) {
460 UChar32 ch
= text
.char32At(p
);
461 int32_t d
= u_digit(ch
, radix
);
466 // ASSUME that when a 32-bit integer overflows it becomes
467 // negative. E.g., 214748364 * 10 + 8 => negative value.
481 * Append a character to a rule that is being built up. To flush
482 * the quoteBuf to rule, make one final call with isLiteral == TRUE.
483 * If there is no final character, pass in (UChar32)-1 as c.
484 * @param rule the string to append the character to
485 * @param c the character to append, or (UChar32)-1 if none.
486 * @param isLiteral if true, then the given character should not be
487 * quoted or escaped. Usually this means it is a syntactic element
489 * @param escapeUnprintable if true, then unprintable characters
490 * should be escaped using \uxxxx or \Uxxxxxxxx. These escapes will
491 * appear outside of quotes.
492 * @param quoteBuf a buffer which is used to build up quoted
493 * substrings. The caller should initially supply an empty buffer,
494 * and thereafter should not modify the buffer. The buffer should be
495 * cleared out by, at the end, calling this method with a literal
498 void ICU_Utility::appendToRule(UnicodeString
& rule
,
501 UBool escapeUnprintable
,
502 UnicodeString
& quoteBuf
) {
503 // If we are escaping unprintables, then escape them outside
504 // quotes. \u and \U are not recognized within quotes. The same
505 // logic applies to literals, but literals are never escaped.
507 (escapeUnprintable
&& ICU_Utility::isUnprintable(c
))) {
508 if (quoteBuf
.length() > 0) {
509 // We prefer backslash APOSTROPHE to double APOSTROPHE
510 // (more readable, less similar to ") so if there are
511 // double APOSTROPHEs at the ends, we pull them outside
514 // If the first thing in the quoteBuf is APOSTROPHE
515 // (doubled) then pull it out.
516 while (quoteBuf
.length() >= 2 &&
517 quoteBuf
.charAt(0) == APOSTROPHE
&&
518 quoteBuf
.charAt(1) == APOSTROPHE
) {
519 rule
.append(BACKSLASH
).append(APOSTROPHE
);
520 quoteBuf
.remove(0, 2);
522 // If the last thing in the quoteBuf is APOSTROPHE
523 // (doubled) then remove and count it and add it after.
524 int32_t trailingCount
= 0;
525 while (quoteBuf
.length() >= 2 &&
526 quoteBuf
.charAt(quoteBuf
.length()-2) == APOSTROPHE
&&
527 quoteBuf
.charAt(quoteBuf
.length()-1) == APOSTROPHE
) {
528 quoteBuf
.truncate(quoteBuf
.length()-2);
531 if (quoteBuf
.length() > 0) {
532 rule
.append(APOSTROPHE
);
533 rule
.append(quoteBuf
);
534 rule
.append(APOSTROPHE
);
535 quoteBuf
.truncate(0);
537 while (trailingCount
-- > 0) {
538 rule
.append(BACKSLASH
).append(APOSTROPHE
);
541 if (c
!= (UChar32
)-1) {
542 /* Since spaces are ignored during parsing, they are
543 * emitted only for readability. We emit one here
544 * only if there isn't already one at the end of the
548 int32_t len
= rule
.length();
549 if (len
> 0 && rule
.charAt(len
-1) != c
) {
552 } else if (!escapeUnprintable
|| !ICU_Utility::escapeUnprintable(rule
, c
)) {
558 // Escape ' and '\' and don't begin a quote just for them
559 else if (quoteBuf
.length() == 0 &&
560 (c
== APOSTROPHE
|| c
== BACKSLASH
)) {
561 rule
.append(BACKSLASH
);
565 // Specials (printable ascii that isn't [0-9a-zA-Z]) and
566 // whitespace need quoting. Also append stuff to quotes if we are
567 // building up a quoted substring already.
568 else if (quoteBuf
.length() > 0 ||
569 (c
>= 0x0021 && c
<= 0x007E &&
570 !((c
>= 0x0030/*'0'*/ && c
<= 0x0039/*'9'*/) ||
571 (c
>= 0x0041/*'A'*/ && c
<= 0x005A/*'Z'*/) ||
572 (c
>= 0x0061/*'a'*/ && c
<= 0x007A/*'z'*/))) ||
573 uprv_isRuleWhiteSpace(c
)) {
575 // Double ' within a quote
576 if (c
== APOSTROPHE
) {
581 // Otherwise just append
587 void ICU_Utility::appendToRule(UnicodeString
& rule
,
588 const UnicodeString
& text
,
590 UBool escapeUnprintable
,
591 UnicodeString
& quoteBuf
) {
592 for (int32_t i
=0; i
<text
.length(); ++i
) {
593 appendToRule(rule
, text
[i
], isLiteral
, escapeUnprintable
, quoteBuf
);
598 * Given a matcher reference, which may be null, append its
599 * pattern as a literal to the given rule.
601 void ICU_Utility::appendToRule(UnicodeString
& rule
,
602 const UnicodeMatcher
* matcher
,
603 UBool escapeUnprintable
,
604 UnicodeString
& quoteBuf
) {
605 if (matcher
!= NULL
) {
607 appendToRule(rule
, matcher
->toPattern(pat
, escapeUnprintable
),
608 TRUE
, escapeUnprintable
, quoteBuf
);
612 U_CAPI UBool U_EXPORT2
613 uprv_isRuleWhiteSpace(UChar32 c
) {
614 /* "white space" in the sense of ICU rule parsers
615 This is a FIXED LIST that is NOT DEPENDENT ON UNICODE PROPERTIES.
616 See UTR #31: http://www.unicode.org/reports/tr31/.
617 U+0009..U+000D, U+0020, U+0085, U+200E..U+200F, and U+2028..U+2029
619 return (c
>= 0x0009 && c
<= 0x2029 &&
620 (c
<= 0x000D || c
== 0x0020 || c
== 0x0085 ||
621 c
== 0x200E || c
== 0x200F || c
>= 0x2028));