michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2001-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * Date Name Description michael@0: * 11/19/2001 aliu Creation. michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/uchar.h" michael@0: #include "unicode/utf16.h" michael@0: #include "patternprops.h" michael@0: #include "util.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * Parse an integer at pos, either of the form \d+ or of the form michael@0: * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex, michael@0: * or octal format. michael@0: * @param pos INPUT-OUTPUT parameter. On input, the first michael@0: * character to parse. On output, the character after the last michael@0: * parsed character. michael@0: */ michael@0: int32_t ICU_Utility::parseInteger(const UnicodeString& rule, int32_t& pos, int32_t limit) { michael@0: int32_t count = 0; michael@0: int32_t value = 0; michael@0: int32_t p = pos; michael@0: int8_t radix = 10; michael@0: michael@0: if (p < limit && rule.charAt(p) == 48 /*0*/) { michael@0: if (p+1 < limit && (rule.charAt(p+1) == 0x78 /*x*/ || rule.charAt(p+1) == 0x58 /*X*/)) { michael@0: p += 2; michael@0: radix = 16; michael@0: } michael@0: else { michael@0: p++; michael@0: count = 1; michael@0: radix = 8; michael@0: } michael@0: } michael@0: michael@0: while (p < limit) { michael@0: int32_t d = u_digit(rule.charAt(p++), radix); michael@0: if (d < 0) { michael@0: --p; michael@0: break; michael@0: } michael@0: ++count; michael@0: int32_t v = (value * radix) + d; michael@0: if (v <= value) { michael@0: // If there are too many input digits, at some point michael@0: // the value will go negative, e.g., if we have seen michael@0: // "0x8000000" already and there is another '0', when michael@0: // we parse the next 0 the value will go negative. michael@0: return 0; michael@0: } michael@0: value = v; michael@0: } michael@0: if (count > 0) { michael@0: pos = p; michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: /** michael@0: * Parse a pattern string starting at offset pos. Keywords are michael@0: * matched case-insensitively. Spaces may be skipped and may be michael@0: * optional or required. Integer values may be parsed, and if michael@0: * they are, they will be returned in the given array. If michael@0: * successful, the offset of the next non-space character is michael@0: * returned. On failure, -1 is returned. michael@0: * @param pattern must only contain lowercase characters, which michael@0: * will match their uppercase equivalents as well. A space michael@0: * character matches one or more required spaces. A '~' character michael@0: * matches zero or more optional spaces. A '#' character matches michael@0: * an integer and stores it in parsedInts, which the caller must michael@0: * ensure has enough capacity. michael@0: * @param parsedInts array to receive parsed integers. Caller michael@0: * must ensure that parsedInts.length is >= the number of '#' michael@0: * signs in 'pattern'. michael@0: * @return the position after the last character parsed, or -1 if michael@0: * the parse failed michael@0: */ michael@0: int32_t ICU_Utility::parsePattern(const UnicodeString& rule, int32_t pos, int32_t limit, michael@0: const UnicodeString& pattern, int32_t* parsedInts) { michael@0: // TODO Update this to handle surrogates michael@0: int32_t p; michael@0: int32_t intCount = 0; // number of integers parsed michael@0: for (int32_t i=0; i= limit) { michael@0: return -1; michael@0: } michael@0: c = rule.charAt(pos++); michael@0: if (!PatternProps::isWhiteSpace(c)) { michael@0: return -1; michael@0: } michael@0: // FALL THROUGH to skipWhitespace michael@0: case 126 /*'~'*/: michael@0: pos = skipWhitespace(rule, pos); michael@0: break; michael@0: case 35 /*'#'*/: michael@0: p = pos; michael@0: parsedInts[intCount++] = parseInteger(rule, p, limit); michael@0: if (p == pos) { michael@0: // Syntax error; failed to parse integer michael@0: return -1; michael@0: } michael@0: pos = p; michael@0: break; michael@0: default: michael@0: if (pos >= limit) { michael@0: return -1; michael@0: } michael@0: c = (UChar) u_tolower(rule.charAt(pos++)); michael@0: if (c != cpat) { michael@0: return -1; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: return pos; michael@0: } michael@0: michael@0: /** michael@0: * Parse a Unicode identifier from the given string at the given michael@0: * position. Return the identifier, or an empty string if there michael@0: * is no identifier. michael@0: * @param str the string to parse michael@0: * @param pos INPUT-OUPUT parameter. On INPUT, pos is the michael@0: * first character to examine. It must be less than str.length(), michael@0: * and it must not point to a whitespace character. That is, must michael@0: * have pos < str.length(). On michael@0: * OUTPUT, the position after the last parsed character. michael@0: * @return the Unicode identifier, or an empty string if there is michael@0: * no valid identifier at pos. michael@0: */ michael@0: UnicodeString ICU_Utility::parseUnicodeIdentifier(const UnicodeString& str, int32_t& pos) { michael@0: // assert(pos < str.length()); michael@0: UnicodeString buf; michael@0: int p = pos; michael@0: while (p < str.length()) { michael@0: UChar32 ch = str.char32At(p); michael@0: if (buf.length() == 0) { michael@0: if (u_isIDStart(ch)) { michael@0: buf.append(ch); michael@0: } else { michael@0: buf.truncate(0); michael@0: return buf; michael@0: } michael@0: } else { michael@0: if (u_isIDPart(ch)) { michael@0: buf.append(ch); michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: p += U16_LENGTH(ch); michael@0: } michael@0: pos = p; michael@0: return buf; michael@0: } michael@0: michael@0: /** michael@0: * Parse an unsigned 31-bit integer at the given offset. Use michael@0: * UCharacter.digit() to parse individual characters into digits. michael@0: * @param text the text to be parsed michael@0: * @param pos INPUT-OUTPUT parameter. On entry, pos[0] is the michael@0: * offset within text at which to start parsing; it should point michael@0: * to a valid digit. On exit, pos[0] is the offset after the last michael@0: * parsed character. If the parse failed, it will be unchanged on michael@0: * exit. Must be >= 0 on entry. michael@0: * @param radix the radix in which to parse; must be >= 2 and <= michael@0: * 36. michael@0: * @return a non-negative parsed number, or -1 upon parse failure. michael@0: * Parse fails if there are no digits, that is, if pos[0] does not michael@0: * point to a valid digit on entry, or if the number to be parsed michael@0: * does not fit into a 31-bit unsigned integer. michael@0: */ michael@0: int32_t ICU_Utility::parseNumber(const UnicodeString& text, michael@0: int32_t& pos, int8_t radix) { michael@0: // assert(pos[0] >= 0); michael@0: // assert(radix >= 2); michael@0: // assert(radix <= 36); michael@0: int32_t n = 0; michael@0: int32_t p = pos; michael@0: while (p < text.length()) { michael@0: UChar32 ch = text.char32At(p); michael@0: int32_t d = u_digit(ch, radix); michael@0: if (d < 0) { michael@0: break; michael@0: } michael@0: n = radix*n + d; michael@0: // ASSUME that when a 32-bit integer overflows it becomes michael@0: // negative. E.g., 214748364 * 10 + 8 => negative value. michael@0: if (n < 0) { michael@0: return -1; michael@0: } michael@0: ++p; michael@0: } michael@0: if (p == pos) { michael@0: return -1; michael@0: } michael@0: pos = p; michael@0: return n; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: