]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/util.cpp
ICU-400.38.tar.gz
[apple/icu.git] / icuSources / common / util.cpp
index 203fde518e8769158abe7e024b76401880275ba4..f084a291343ec9d560320166ca73dee9c45cdd92 100644 (file)
@@ -1,6 +1,6 @@
 /*
 **********************************************************************
-*   Copyright (c) 2001-2004, International Business Machines
+*   Copyright (c) 2001-2008, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
 *   Date        Name        Description
@@ -9,9 +9,8 @@
 */
 
 #include "util.h"
-#include "unicode/uchar.h"
 #include "unicode/unimatch.h"
-#include "uprops.h"
+#include "unicode/uniset.h"
 
 // Define UChar constants using hex for EBCDIC compatibility
 
@@ -29,6 +28,8 @@ static const UChar DIGITS[] = {
     85,86,87,88,89,90
 };
 
+U_NAMESPACE_BEGIN
+
 UnicodeString& ICU_Utility::appendNumber(UnicodeString& result, int32_t n,
                                      int32_t radix, int32_t minDigits) {
     if (radix < 2 || radix > 36) {
@@ -61,9 +62,6 @@ UnicodeString& ICU_Utility::appendNumber(UnicodeString& result, int32_t n,
     return result;
 }
 
-static const UChar HEX[16] = {48,49,50,51,52,53,54,55,  // 0-7
-                              56,57,65,66,67,68,69,70}; // 8-9 A-F
-
 /**
  * Return true if the character is NOT printable ASCII.
  */
@@ -82,17 +80,17 @@ UBool ICU_Utility::escapeUnprintable(UnicodeString& result, UChar32 c) {
         result.append(BACKSLASH);
         if (c & ~0xFFFF) {
             result.append(UPPER_U);
-            result.append(HEX[0xF&(c>>28)]);
-            result.append(HEX[0xF&(c>>24)]);
-            result.append(HEX[0xF&(c>>20)]);
-            result.append(HEX[0xF&(c>>16)]);
+            result.append(DIGITS[0xF&(c>>28)]);
+            result.append(DIGITS[0xF&(c>>24)]);
+            result.append(DIGITS[0xF&(c>>20)]);
+            result.append(DIGITS[0xF&(c>>16)]);
         } else {
             result.append(LOWER_U);
         }
-        result.append(HEX[0xF&(c>>12)]);
-        result.append(HEX[0xF&(c>>8)]);
-        result.append(HEX[0xF&(c>>4)]);
-        result.append(HEX[0xF&c]);
+        result.append(DIGITS[0xF&(c>>12)]);
+        result.append(DIGITS[0xF&(c>>8)]);
+        result.append(DIGITS[0xF&(c>>4)]);
+        result.append(DIGITS[0xF&c]);
         return TRUE;
     }
     return FALSE;
@@ -214,69 +212,6 @@ UBool ICU_Utility::parseChar(const UnicodeString& id, int32_t& pos, UChar ch) {
     return TRUE;
 }
 
-/**
- * Parse a pattern string starting at offset pos.  Keywords are
- * matched case-insensitively.  Spaces may be skipped and may be
- * optional or required.  Integer values may be parsed, and if
- * they are, they will be returned in the given array.  If
- * successful, the offset of the next non-space character is
- * returned.  On failure, -1 is returned.
- * @param pattern must only contain lowercase characters, which
- * will match their uppercase equivalents as well.  A space
- * character matches one or more required spaces.  A '~' character
- * matches zero or more optional spaces.  A '#' character matches
- * an integer and stores it in parsedInts, which the caller must
- * ensure has enough capacity.
- * @param parsedInts array to receive parsed integers.  Caller
- * must ensure that parsedInts.length is >= the number of '#'
- * signs in 'pattern'.
- * @return the position after the last character parsed, or -1 if
- * the parse failed
- */
-int32_t ICU_Utility::parsePattern(const UnicodeString& rule, int32_t pos, int32_t limit,
-                              const UnicodeString& pattern, int32_t* parsedInts) {
-    // TODO Update this to handle surrogates
-    int32_t p;
-    int32_t intCount = 0; // number of integers parsed
-    for (int32_t i=0; i<pattern.length(); ++i) {
-        UChar cpat = pattern.charAt(i);
-        UChar c;
-        switch (cpat) {
-        case 32 /*' '*/:
-            if (pos >= limit) {
-                return -1;
-            }
-            c = rule.charAt(pos++);
-            if (!uprv_isRuleWhiteSpace(c)) {
-                return -1;
-            }
-            // FALL THROUGH to skipWhitespace
-        case 126 /*'~'*/:
-            pos = skipWhitespace(rule, pos);
-            break;
-        case 35 /*'#'*/:
-            p = pos;
-            parsedInts[intCount++] = parseInteger(rule, p, limit);
-            if (p == pos) {
-                // Syntax error; failed to parse integer
-                return -1;
-            }
-            pos = p;
-            break;
-        default:
-            if (pos >= limit) {
-                return -1;
-            }
-            c = (UChar) u_tolower(rule.charAt(pos++));
-            if (c != cpat) {
-                return -1;
-            }
-            break;
-        }
-    }
-    return pos;
-}
-
 /**
  * Parse a pattern string within the given Replaceable and a parsing
  * pattern.  Characters are matched literally and case-sensitively
@@ -343,140 +278,6 @@ int32_t ICU_Utility::parsePattern(const UnicodeString& pat,
     return -1; // text ended before end of pat
 }
 
-/**
- * Parse an integer at pos, either of the form \d+ or of the form
- * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex,
- * or octal format.
- * @param pos INPUT-OUTPUT parameter.  On input, the first
- * character to parse.  On output, the character after the last
- * parsed character.
- */
-int32_t ICU_Utility::parseInteger(const UnicodeString& rule, int32_t& pos, int32_t limit) {
-    int32_t count = 0;
-    int32_t value = 0;
-    int32_t p = pos;
-    int8_t radix = 10;
-
-    if (p < limit && rule.charAt(p) == 48 /*0*/) {
-        if (p+1 < limit && (rule.charAt(p+1) == 0x78 /*x*/ || rule.charAt(p+1) == 0x58 /*X*/)) {
-            p += 2;
-            radix = 16;
-        }
-        else {
-            p++;
-            count = 1;
-            radix = 8;
-        }
-    }
-
-    while (p < limit) {
-        int32_t d = u_digit(rule.charAt(p++), radix);
-        if (d < 0) {
-            --p;
-            break;
-        }
-        ++count;
-        int32_t v = (value * radix) + d;
-        if (v <= value) {
-            // If there are too many input digits, at some point
-            // the value will go negative, e.g., if we have seen
-            // "0x8000000" already and there is another '0', when
-            // we parse the next 0 the value will go negative.
-            return 0;
-        }
-        value = v;
-    }
-    if (count > 0) {
-        pos = p;
-    }
-    return value;
-}
-
-/**
- * Parse a Unicode identifier from the given string at the given
- * position.  Return the identifier, or an empty string if there
- * is no identifier.
- * @param str the string to parse
- * @param pos INPUT-OUPUT parameter.  On INPUT, pos is the
- * first character to examine.  It must be less than str.length(),
- * and it must not point to a whitespace character.  That is, must
- * have pos < str.length() and
- * !uprv_isRuleWhiteSpace(str.char32At(pos)).  On
- * OUTPUT, the position after the last parsed character.
- * @return the Unicode identifier, or an empty string if there is
- * no valid identifier at pos.
- */
-UnicodeString ICU_Utility::parseUnicodeIdentifier(const UnicodeString& str, int32_t& pos) {
-    // assert(pos < str.length());
-    // assert(!uprv_isRuleWhiteSpace(str.char32At(pos)));
-    UnicodeString buf;
-    int p = pos;
-    while (p < str.length()) {
-        UChar32 ch = str.char32At(p);
-        if (buf.length() == 0) {
-            if (u_isIDStart(ch)) {
-                buf.append(ch);
-            } else {
-                buf.truncate(0);
-                return buf;
-            }
-        } else {
-            if (u_isIDPart(ch)) {
-                buf.append(ch);
-            } else {
-                break;
-            }
-        }
-        p += UTF_CHAR_LENGTH(ch);
-    }
-    pos = p;
-    return buf;
-}
-
-/**
- * Parse an unsigned 31-bit integer at the given offset.  Use
- * UCharacter.digit() to parse individual characters into digits.
- * @param text the text to be parsed
- * @param pos INPUT-OUTPUT parameter.  On entry, pos[0] is the
- * offset within text at which to start parsing; it should point
- * to a valid digit.  On exit, pos[0] is the offset after the last
- * parsed character.  If the parse failed, it will be unchanged on
- * exit.  Must be >= 0 on entry.
- * @param radix the radix in which to parse; must be >= 2 and <=
- * 36.
- * @return a non-negative parsed number, or -1 upon parse failure.
- * Parse fails if there are no digits, that is, if pos[0] does not
- * point to a valid digit on entry, or if the number to be parsed
- * does not fit into a 31-bit unsigned integer.
- */
-int32_t ICU_Utility::parseNumber(const UnicodeString& text,
-                                 int32_t& pos, int8_t radix) {
-    // assert(pos[0] >= 0);
-    // assert(radix >= 2);
-    // assert(radix <= 36);
-    int32_t n = 0;
-    int32_t p = pos;
-    while (p < text.length()) {
-        UChar32 ch = text.char32At(p);
-        int32_t d = u_digit(ch, radix);
-        if (d < 0) {
-            break;
-        }
-        n = radix*n + d;
-        // ASSUME that when a 32-bit integer overflows it becomes
-        // negative.  E.g., 214748364 * 10 + 8 => negative value.
-        if (n < 0) {
-            return -1;
-        }
-        ++p;
-    }
-    if (p == pos) {
-        return -1;
-    }
-    pos = p;
-    return n;
-}
-
 /**
  * Append a character to a rule that is being built up.  To flush
  * the quoteBuf to rule, make one final call with isLiteral == TRUE.
@@ -609,16 +410,36 @@ void ICU_Utility::appendToRule(UnicodeString& rule,
     }
 }
 
+U_NAMESPACE_END
+
 U_CAPI UBool U_EXPORT2
 uprv_isRuleWhiteSpace(UChar32 c) {
     /* "white space" in the sense of ICU rule parsers
        This is a FIXED LIST that is NOT DEPENDENT ON UNICODE PROPERTIES.
-       See UTR #31: http://www.unicode.org/reports/tr31/.
+       See UAX #31 Identifier and Pattern Syntax: http://www.unicode.org/reports/tr31/
        U+0009..U+000D, U+0020, U+0085, U+200E..U+200F, and U+2028..U+2029
+       Equivalent to test for Pattern_White_Space Unicode property.
     */
     return (c >= 0x0009 && c <= 0x2029 &&
             (c <= 0x000D || c == 0x0020 || c == 0x0085 ||
              c == 0x200E || c == 0x200F || c >= 0x2028));
 }
 
+U_CAPI U_NAMESPACE_QUALIFIER UnicodeSet* U_EXPORT2
+uprv_openRuleWhiteSpaceSet(UErrorCode* ec) {
+    if(U_FAILURE(*ec)) {
+        return NULL;
+    }
+    // create a set with the Pattern_White_Space characters,
+    // without a pattern for fewer code dependencies
+    U_NAMESPACE_QUALIFIER UnicodeSet *set=new U_NAMESPACE_QUALIFIER UnicodeSet(9, 0xd);
+    // Check for new failure.
+    if (set == NULL) {
+        *ec = U_MEMORY_ALLOCATION_ERROR;
+        return NULL;
+    }
+    set->UnicodeSet::add(0x20).add(0x85).add(0x200e, 0x200f).add(0x2028, 0x2029);
+    return set;
+}
+
 //eof