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