intl/icu/source/common/util_props.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial