1 /* 2 ********************************************************************** 3 * Copyright (c) 2001-2011, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 * Date Name Description 7 * 11/19/2001 aliu Creation. 8 ********************************************************************** 9 */ 10 11 #include "unicode/uchar.h" 12 #include "patternprops.h" 13 #include "util.h" 14 15 U_NAMESPACE_BEGIN 16 17 /** 18 * Parse an integer at pos, either of the form \d+ or of the form 19 * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex, 20 * or octal format. 21 * @param pos INPUT-OUTPUT parameter. On input, the first 22 * character to parse. On output, the character after the last 23 * parsed character. 24 */ 25 int32_t ICU_Utility::parseInteger(const UnicodeString& rule, int32_t& pos, int32_t limit) { 26 int32_t count = 0; 27 int32_t value = 0; 28 int32_t p = pos; 29 int8_t radix = 10; 30 31 if (p < limit && rule.charAt(p) == 48 /*0*/) { 32 if (p+1 < limit && (rule.charAt(p+1) == 0x78 /*x*/ || rule.charAt(p+1) == 0x58 /*X*/)) { 33 p += 2; 34 radix = 16; 35 } 36 else { 37 p++; 38 count = 1; 39 radix = 8; 40 } 41 } 42 43 while (p < limit) { 44 int32_t d = u_digit(rule.charAt(p++), radix); 45 if (d < 0) { 46 --p; 47 break; 48 } 49 ++count; 50 int32_t v = (value * radix) + d; 51 if (v <= value) { 52 // If there are too many input digits, at some point 53 // the value will go negative, e.g., if we have seen 54 // "0x8000000" already and there is another '0', when 55 // we parse the next 0 the value will go negative. 56 return 0; 57 } 58 value = v; 59 } 60 if (count > 0) { 61 pos = p; 62 } 63 return value; 64 } 65 66 /** 67 * Parse a pattern string starting at offset pos. Keywords are 68 * matched case-insensitively. Spaces may be skipped and may be 69 * optional or required. Integer values may be parsed, and if 70 * they are, they will be returned in the given array. If 71 * successful, the offset of the next non-space character is 72 * returned. On failure, -1 is returned. 73 * @param pattern must only contain lowercase characters, which 74 * will match their uppercase equivalents as well. A space 75 * character matches one or more required spaces. A '~' character 76 * matches zero or more optional spaces. A '#' character matches 77 * an integer and stores it in parsedInts, which the caller must 78 * ensure has enough capacity. 79 * @param parsedInts array to receive parsed integers. Caller 80 * must ensure that parsedInts.length is >= the number of '#' 81 * signs in 'pattern'. 82 * @return the position after the last character parsed, or -1 if 83 * the parse failed 84 */ 85 int32_t ICU_Utility::parsePattern(const UnicodeString& rule, int32_t pos, int32_t limit, 86 const UnicodeString& pattern, int32_t* parsedInts) { 87 // TODO Update this to handle surrogates 88 int32_t p; 89 int32_t intCount = 0; // number of integers parsed 90 for (int32_t i=0; i<pattern.length(); ++i) { 91 UChar cpat = pattern.charAt(i); 92 UChar c; 93 switch (cpat) { 94 case 32 /*' '*/: 95 if (pos >= limit) { 96 return -1; 97 } 98 c = rule.charAt(pos++); 99 if (!PatternProps::isWhiteSpace(c)) { 100 return -1; 101 } 102 // FALL THROUGH to skipWhitespace 103 case 126 /*'~'*/: 104 pos = skipWhitespace(rule, pos); 105 break; 106 case 35 /*'#'*/: 107 p = pos; 108 parsedInts[intCount++] = parseInteger(rule, p, limit); 109 if (p == pos) { 110 // Syntax error; failed to parse integer 111 return -1; 112 } 113 pos = p; 114 break; 115 default: 116 if (pos >= limit) { 117 return -1; 118 } 119 c = (UChar) u_tolower(rule.charAt(pos++)); 120 if (c != cpat) { 121 return -1; 122 } 123 break; 124 } 125 } 126 return pos; 127 } 128 129 /** 130 * Parse a Unicode identifier from the given string at the given 131 * position. Return the identifier, or an empty string if there 132 * is no identifier. 133 * @param str the string to parse 134 * @param pos INPUT-OUPUT parameter. On INPUT, pos is the 135 * first character to examine. It must be less than str.length(), 136 * and it must not point to a whitespace character. That is, must 137 * have pos < str.length(). On 138 * OUTPUT, the position after the last parsed character. 139 * @return the Unicode identifier, or an empty string if there is 140 * no valid identifier at pos. 141 */ 142 UnicodeString ICU_Utility::parseUnicodeIdentifier(const UnicodeString& str, int32_t& pos) { 143 // assert(pos < str.length()); 144 UnicodeString buf; 145 int p = pos; 146 while (p < str.length()) { 147 UChar32 ch = str.char32At(p); 148 if (buf.length() == 0) { 149 if (u_isIDStart(ch)) { 150 buf.append(ch); 151 } else { 152 buf.truncate(0); 153 return buf; 154 } 155 } else { 156 if (u_isIDPart(ch)) { 157 buf.append(ch); 158 } else { 159 break; 160 } 161 } 162 p += UTF_CHAR_LENGTH(ch); 163 } 164 pos = p; 165 return buf; 166 } 167 168 /** 169 * Parse an unsigned 31-bit integer at the given offset. Use 170 * UCharacter.digit() to parse individual characters into digits. 171 * @param text the text to be parsed 172 * @param pos INPUT-OUTPUT parameter. On entry, pos[0] is the 173 * offset within text at which to start parsing; it should point 174 * to a valid digit. On exit, pos[0] is the offset after the last 175 * parsed character. If the parse failed, it will be unchanged on 176 * exit. Must be >= 0 on entry. 177 * @param radix the radix in which to parse; must be >= 2 and <= 178 * 36. 179 * @return a non-negative parsed number, or -1 upon parse failure. 180 * Parse fails if there are no digits, that is, if pos[0] does not 181 * point to a valid digit on entry, or if the number to be parsed 182 * does not fit into a 31-bit unsigned integer. 183 */ 184 int32_t ICU_Utility::parseNumber(const UnicodeString& text, 185 int32_t& pos, int8_t radix) { 186 // assert(pos[0] >= 0); 187 // assert(radix >= 2); 188 // assert(radix <= 36); 189 int32_t n = 0; 190 int32_t p = pos; 191 while (p < text.length()) { 192 UChar32 ch = text.char32At(p); 193 int32_t d = u_digit(ch, radix); 194 if (d < 0) { 195 break; 196 } 197 n = radix*n + d; 198 // ASSUME that when a 32-bit integer overflows it becomes 199 // negative. E.g., 214748364 * 10 + 8 => negative value. 200 if (n < 0) { 201 return -1; 202 } 203 ++p; 204 } 205 if (p == pos) { 206 return -1; 207 } 208 pos = p; 209 return n; 210 } 211 212 U_NAMESPACE_END 213 214