]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/util_props.cpp
2 **********************************************************************
3 * Copyright (c) 2001-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/19/2001 aliu Creation.
8 **********************************************************************
11 #include "unicode/uchar.h"
12 #include "unicode/utf16.h"
13 #include "patternprops.h"
19 * Parse an integer at pos, either of the form \d+ or of the form
20 * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex,
22 * @param pos INPUT-OUTPUT parameter. On input, the first
23 * character to parse. On output, the character after the last
26 int32_t ICU_Utility::parseInteger(const UnicodeString
& rule
, int32_t& pos
, int32_t limit
) {
32 if (p
< limit
&& rule
.charAt(p
) == 48 /*0*/) {
33 if (p
+1 < limit
&& (rule
.charAt(p
+1) == 0x78 /*x*/ || rule
.charAt(p
+1) == 0x58 /*X*/)) {
45 int32_t d
= u_digit(rule
.charAt(p
++), radix
);
51 int32_t v
= (value
* radix
) + d
;
53 // If there are too many input digits, at some point
54 // the value will go negative, e.g., if we have seen
55 // "0x8000000" already and there is another '0', when
56 // we parse the next 0 the value will go negative.
68 * Parse a pattern string starting at offset pos. Keywords are
69 * matched case-insensitively. Spaces may be skipped and may be
70 * optional or required. Integer values may be parsed, and if
71 * they are, they will be returned in the given array. If
72 * successful, the offset of the next non-space character is
73 * returned. On failure, -1 is returned.
74 * @param pattern must only contain lowercase characters, which
75 * will match their uppercase equivalents as well. A space
76 * character matches one or more required spaces. A '~' character
77 * matches zero or more optional spaces. A '#' character matches
78 * an integer and stores it in parsedInts, which the caller must
79 * ensure has enough capacity.
80 * @param parsedInts array to receive parsed integers. Caller
81 * must ensure that parsedInts.length is >= the number of '#'
83 * @return the position after the last character parsed, or -1 if
86 int32_t ICU_Utility::parsePattern(const UnicodeString
& rule
, int32_t pos
, int32_t limit
,
87 const UnicodeString
& pattern
, int32_t* parsedInts
) {
88 // TODO Update this to handle surrogates
90 int32_t intCount
= 0; // number of integers parsed
91 for (int32_t i
=0; i
<pattern
.length(); ++i
) {
92 UChar cpat
= pattern
.charAt(i
);
99 c
= rule
.charAt(pos
++);
100 if (!PatternProps::isWhiteSpace(c
)) {
103 // FALL THROUGH to skipWhitespace
106 pos
= skipWhitespace(rule
, pos
);
110 parsedInts
[intCount
++] = parseInteger(rule
, p
, limit
);
112 // Syntax error; failed to parse integer
121 c
= (UChar
) u_tolower(rule
.charAt(pos
++));
132 * Parse a Unicode identifier from the given string at the given
133 * position. Return the identifier, or an empty string if there
135 * @param str the string to parse
136 * @param pos INPUT-OUPUT parameter. On INPUT, pos is the
137 * first character to examine. It must be less than str.length(),
138 * and it must not point to a whitespace character. That is, must
139 * have pos < str.length(). On
140 * OUTPUT, the position after the last parsed character.
141 * @return the Unicode identifier, or an empty string if there is
142 * no valid identifier at pos.
144 UnicodeString
ICU_Utility::parseUnicodeIdentifier(const UnicodeString
& str
, int32_t& pos
) {
145 // assert(pos < str.length());
148 while (p
< str
.length()) {
149 UChar32 ch
= str
.char32At(p
);
150 if (buf
.length() == 0) {
151 if (u_isIDStart(ch
)) {
158 if (u_isIDPart(ch
)) {
171 * Parse an unsigned 31-bit integer at the given offset. Use
172 * UCharacter.digit() to parse individual characters into digits.
173 * @param text the text to be parsed
174 * @param pos INPUT-OUTPUT parameter. On entry, pos[0] is the
175 * offset within text at which to start parsing; it should point
176 * to a valid digit. On exit, pos[0] is the offset after the last
177 * parsed character. If the parse failed, it will be unchanged on
178 * exit. Must be >= 0 on entry.
179 * @param radix the radix in which to parse; must be >= 2 and <=
181 * @return a non-negative parsed number, or -1 upon parse failure.
182 * Parse fails if there are no digits, that is, if pos[0] does not
183 * point to a valid digit on entry, or if the number to be parsed
184 * does not fit into a 31-bit unsigned integer.
186 int32_t ICU_Utility::parseNumber(const UnicodeString
& text
,
187 int32_t& pos
, int8_t radix
) {
188 // assert(pos[0] >= 0);
189 // assert(radix >= 2);
190 // assert(radix <= 36);
193 while (p
< text
.length()) {
194 UChar32 ch
= text
.char32At(p
);
195 int32_t d
= u_digit(ch
, radix
);
200 // ASSUME that when a 32-bit integer overflows it becomes
201 // negative. E.g., 214748364 * 10 + 8 => negative value.