sl@0: /* sl@0: ********************************************************************** sl@0: * Copyright (c) 2001-2005, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: ********************************************************************** sl@0: * Date Name Description sl@0: * 11/19/2001 aliu Creation. sl@0: ********************************************************************** sl@0: */ sl@0: #ifndef ICU_UTIL_H sl@0: #define ICU_UTIL_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: #include "unicode/uobject.h" sl@0: #include "unicode/unistr.h" sl@0: sl@0: //-------------------------------------------------------------------- sl@0: // class ICU_Utility sl@0: // i18n utility functions, scoped into the class ICU_Utility. sl@0: //-------------------------------------------------------------------- sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: class UnicodeMatcher; sl@0: sl@0: class U_COMMON_API ICU_Utility /* not : public UObject because all methods are static */ { sl@0: public: sl@0: sl@0: /** sl@0: * Append a number to the given UnicodeString in the given radix. sl@0: * Standard digits '0'-'9' are used and letters 'A'-'Z' for sl@0: * radices 11 through 36. sl@0: * @param result the digits of the number are appended here sl@0: * @param n the number to be converted to digits; may be negative. sl@0: * If negative, a '-' is prepended to the digits. sl@0: * @param radix a radix from 2 to 36 inclusive. sl@0: * @param minDigits the minimum number of digits, not including sl@0: * any '-', to produce. Values less than 2 have no effect. One sl@0: * digit is always emitted regardless of this parameter. sl@0: * @return a reference to result sl@0: */ sl@0: static UnicodeString& appendNumber(UnicodeString& result, int32_t n, sl@0: int32_t radix = 10, sl@0: int32_t minDigits = 1); sl@0: sl@0: /** sl@0: * Return true if the character is NOT printable ASCII. sl@0: * sl@0: * This method should really be in UnicodeString (or similar). For sl@0: * now, we implement it here and share it with friend classes. sl@0: */ sl@0: static UBool isUnprintable(UChar32 c); sl@0: sl@0: /** sl@0: * Escape unprintable characters using \uxxxx notation for U+0000 to sl@0: * U+FFFF and \Uxxxxxxxx for U+10000 and above. If the character is sl@0: * printable ASCII, then do nothing and return FALSE. Otherwise, sl@0: * append the escaped notation and return TRUE. sl@0: */ sl@0: static UBool escapeUnprintable(UnicodeString& result, UChar32 c); sl@0: sl@0: /** sl@0: * Returns the index of a character, ignoring quoted text. sl@0: * For example, in the string "abc'hide'h", the 'h' in "hide" will not be sl@0: * found by a search for 'h'. sl@0: * @param text text to be searched sl@0: * @param start the beginning index, inclusive; 0 <= start sl@0: * <= limit. sl@0: * @param limit the ending index, exclusive; start <= limit sl@0: * <= text.length(). sl@0: * @param c character to search for sl@0: * @return Offset of the first instance of c, or -1 if not found. sl@0: */ sl@0: //?FOR FUTURE USE. DISABLE FOR NOW for coverage reasons. sl@0: // static int32_t quotedIndexOf(const UnicodeString& text, sl@0: // int32_t start, int32_t limit, sl@0: // UChar c); sl@0: sl@0: /** sl@0: * Skip over a sequence of zero or more white space characters at pos. sl@0: * @param advance if true, advance pos to the first non-white-space sl@0: * character at or after pos, or str.length(), if there is none. sl@0: * Otherwise leave pos unchanged. sl@0: * @return the index of the first non-white-space character at or sl@0: * after pos, or str.length(), if there is none. sl@0: */ sl@0: static int32_t skipWhitespace(const UnicodeString& str, int32_t& pos, sl@0: UBool advance = FALSE); sl@0: sl@0: /** sl@0: * Skip over whitespace in a Replaceable. Whitespace is defined by sl@0: * uprv_isRuleWhiteSpace(). Skipping may be done in the forward or sl@0: * reverse direction. In either case, the leftmost index will be sl@0: * inclusive, and the rightmost index will be exclusive. That is, sl@0: * given a range defined as [start, limit), the call sl@0: * skipWhitespace(text, start, limit) will advance start past leading sl@0: * whitespace, whereas the call skipWhitespace(text, limit, start), sl@0: * will back up limit past trailing whitespace. sl@0: * @param text the text to be analyzed sl@0: * @param pos either the start or limit of a range of 'text', to skip sl@0: * leading or trailing whitespace, respectively sl@0: * @param stop either the limit or start of a range of 'text', to skip sl@0: * leading or trailing whitespace, respectively sl@0: * @return the new start or limit, depending on what was passed in to sl@0: * 'pos' sl@0: */ sl@0: //?FOR FUTURE USE. DISABLE FOR NOW for coverage reasons. sl@0: //? static int32_t skipWhitespace(const Replaceable& text, sl@0: //? int32_t pos, int32_t stop); sl@0: sl@0: /** sl@0: * Parse a single non-whitespace character 'ch', optionally sl@0: * preceded by whitespace. sl@0: * @param id the string to be parsed sl@0: * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the sl@0: * offset of the first character to be parsed. On output, pos[0] sl@0: * is the index after the last parsed character. If the parse sl@0: * fails, pos[0] will be unchanged. sl@0: * @param ch the non-whitespace character to be parsed. sl@0: * @return true if 'ch' is seen preceded by zero or more sl@0: * whitespace characters. sl@0: */ sl@0: static UBool parseChar(const UnicodeString& id, int32_t& pos, UChar ch); sl@0: sl@0: /** sl@0: * Parse a pattern string starting at offset pos. Keywords are sl@0: * matched case-insensitively. Spaces may be skipped and may be sl@0: * optional or required. Integer values may be parsed, and if sl@0: * they are, they will be returned in the given array. If sl@0: * successful, the offset of the next non-space character is sl@0: * returned. On failure, -1 is returned. sl@0: * @param pattern must only contain lowercase characters, which sl@0: * will match their uppercase equivalents as well. A space sl@0: * character matches one or more required spaces. A '~' character sl@0: * matches zero or more optional spaces. A '#' character matches sl@0: * an integer and stores it in parsedInts, which the caller must sl@0: * ensure has enough capacity. sl@0: * @param parsedInts array to receive parsed integers. Caller sl@0: * must ensure that parsedInts.length is >= the number of '#' sl@0: * signs in 'pattern'. sl@0: * @return the position after the last character parsed, or -1 if sl@0: * the parse failed sl@0: */ sl@0: static int32_t parsePattern(const UnicodeString& rule, int32_t pos, int32_t limit, sl@0: const UnicodeString& pattern, int32_t* parsedInts); sl@0: sl@0: /** sl@0: * Parse a pattern string within the given Replaceable and a parsing sl@0: * pattern. Characters are matched literally and case-sensitively sl@0: * except for the following special characters: sl@0: * sl@0: * ~ zero or more uprv_isRuleWhiteSpace chars sl@0: * sl@0: * If end of pattern is reached with all matches along the way, sl@0: * pos is advanced to the first unparsed index and returned. sl@0: * Otherwise -1 is returned. sl@0: * @param pat pattern that controls parsing sl@0: * @param text text to be parsed, starting at index sl@0: * @param index offset to first character to parse sl@0: * @param limit offset after last character to parse sl@0: * @return index after last parsed character, or -1 on parse failure. sl@0: */ sl@0: static int32_t parsePattern(const UnicodeString& pat, sl@0: const Replaceable& text, sl@0: int32_t index, sl@0: int32_t limit); sl@0: sl@0: /** sl@0: * Parse an integer at pos, either of the form \d+ or of the form sl@0: * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex, sl@0: * or octal format. sl@0: * @param pos INPUT-OUTPUT parameter. On input, the first sl@0: * character to parse. On output, the character after the last sl@0: * parsed character. sl@0: */ sl@0: static int32_t parseInteger(const UnicodeString& rule, int32_t& pos, int32_t limit); sl@0: sl@0: /** sl@0: * Parse a Unicode identifier from the given string at the given sl@0: * position. Return the identifier, or an empty string if there sl@0: * is no identifier. sl@0: * @param str the string to parse sl@0: * @param pos INPUT-OUPUT parameter. On INPUT, pos is the sl@0: * first character to examine. It must be less than str.length(), sl@0: * and it must not point to a whitespace character. That is, must sl@0: * have pos < str.length() and sl@0: * !UCharacter::isWhitespace(str.char32At(pos)). On sl@0: * OUTPUT, the position after the last parsed character. sl@0: * @return the Unicode identifier, or an empty string if there is sl@0: * no valid identifier at pos. sl@0: */ sl@0: static UnicodeString parseUnicodeIdentifier(const UnicodeString& str, int32_t& pos); sl@0: sl@0: /** sl@0: * Parse an unsigned 31-bit integer at the given offset. Use sl@0: * UCharacter.digit() to parse individual characters into digits. sl@0: * @param text the text to be parsed sl@0: * @param pos INPUT-OUTPUT parameter. On entry, pos is the sl@0: * offset within text at which to start parsing; it should point sl@0: * to a valid digit. On exit, pos is the offset after the last sl@0: * parsed character. If the parse failed, it will be unchanged on sl@0: * exit. Must be >= 0 on entry. sl@0: * @param radix the radix in which to parse; must be >= 2 and <= sl@0: * 36. sl@0: * @return a non-negative parsed number, or -1 upon parse failure. sl@0: * Parse fails if there are no digits, that is, if pos does not sl@0: * point to a valid digit on entry, or if the number to be parsed sl@0: * does not fit into a 31-bit unsigned integer. sl@0: */ sl@0: static int32_t parseNumber(const UnicodeString& text, sl@0: int32_t& pos, int8_t radix); sl@0: sl@0: static void appendToRule(UnicodeString& rule, sl@0: UChar32 c, sl@0: UBool isLiteral, sl@0: UBool escapeUnprintable, sl@0: UnicodeString& quoteBuf); sl@0: sl@0: static void appendToRule(UnicodeString& rule, sl@0: const UnicodeString& text, sl@0: UBool isLiteral, sl@0: UBool escapeUnprintable, sl@0: UnicodeString& quoteBuf); sl@0: sl@0: static void appendToRule(UnicodeString& rule, sl@0: const UnicodeMatcher* matcher, sl@0: UBool escapeUnprintable, sl@0: UnicodeString& quoteBuf); sl@0: sl@0: private: sl@0: // do not instantiate sl@0: ICU_Utility(); sl@0: }; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: /** sl@0: * Is this character a "white space" in the sense of ICU rule parsers? sl@0: * Equivalent to test for Pattern_White_Space Unicode property. sl@0: * Stable set of characters, won't change. sl@0: * See UAX #31 Identifier and Pattern Syntax: http://www.unicode.org/reports/tr31/ sl@0: * @internal sl@0: */ sl@0: U_CAPI UBool U_EXPORT2 sl@0: uprv_isRuleWhiteSpace(UChar32 c); sl@0: sl@0: #endif sl@0: //eof