2 **********************************************************************
3 * Copyright (c) 2001-2007, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/19/2001 aliu Creation.
8 **********************************************************************
14 #include "unicode/utypes.h"
15 #include "unicode/uobject.h"
16 #include "unicode/unistr.h"
18 //--------------------------------------------------------------------
20 // i18n utility functions, scoped into the class ICU_Utility.
21 //--------------------------------------------------------------------
28 class U_COMMON_API ICU_Utility
/* not : public UObject because all methods are static */ {
32 * Append a number to the given UnicodeString in the given radix.
33 * Standard digits '0'-'9' are used and letters 'A'-'Z' for
34 * radices 11 through 36.
35 * @param result the digits of the number are appended here
36 * @param n the number to be converted to digits; may be negative.
37 * If negative, a '-' is prepended to the digits.
38 * @param radix a radix from 2 to 36 inclusive.
39 * @param minDigits the minimum number of digits, not including
40 * any '-', to produce. Values less than 2 have no effect. One
41 * digit is always emitted regardless of this parameter.
42 * @return a reference to result
44 static UnicodeString
& appendNumber(UnicodeString
& result
, int32_t n
,
46 int32_t minDigits
= 1);
49 * Return true if the character is NOT printable ASCII.
51 * This method should really be in UnicodeString (or similar). For
52 * now, we implement it here and share it with friend classes.
54 static UBool
isUnprintable(UChar32 c
);
57 * Escape unprintable characters using \uxxxx notation for U+0000 to
58 * U+FFFF and \Uxxxxxxxx for U+10000 and above. If the character is
59 * printable ASCII, then do nothing and return FALSE. Otherwise,
60 * append the escaped notation and return TRUE.
62 static UBool
escapeUnprintable(UnicodeString
& result
, UChar32 c
);
65 * Returns the index of a character, ignoring quoted text.
66 * For example, in the string "abc'hide'h", the 'h' in "hide" will not be
67 * found by a search for 'h'.
68 * @param text text to be searched
69 * @param start the beginning index, inclusive; <code>0 <= start
71 * @param limit the ending index, exclusive; <code>start <= limit
72 * <= text.length()</code>.
73 * @param c character to search for
74 * @return Offset of the first instance of c, or -1 if not found.
76 //?FOR FUTURE USE. DISABLE FOR NOW for coverage reasons.
77 // static int32_t quotedIndexOf(const UnicodeString& text,
78 // int32_t start, int32_t limit,
82 * Skip over a sequence of zero or more white space characters at pos.
83 * @param advance if true, advance pos to the first non-white-space
84 * character at or after pos, or str.length(), if there is none.
85 * Otherwise leave pos unchanged.
86 * @return the index of the first non-white-space character at or
87 * after pos, or str.length(), if there is none.
89 static int32_t skipWhitespace(const UnicodeString
& str
, int32_t& pos
,
90 UBool advance
= FALSE
);
93 * Skip over whitespace in a Replaceable. Whitespace is defined by
94 * uprv_isRuleWhiteSpace(). Skipping may be done in the forward or
95 * reverse direction. In either case, the leftmost index will be
96 * inclusive, and the rightmost index will be exclusive. That is,
97 * given a range defined as [start, limit), the call
98 * skipWhitespace(text, start, limit) will advance start past leading
99 * whitespace, whereas the call skipWhitespace(text, limit, start),
100 * will back up limit past trailing whitespace.
101 * @param text the text to be analyzed
102 * @param pos either the start or limit of a range of 'text', to skip
103 * leading or trailing whitespace, respectively
104 * @param stop either the limit or start of a range of 'text', to skip
105 * leading or trailing whitespace, respectively
106 * @return the new start or limit, depending on what was passed in to
109 //?FOR FUTURE USE. DISABLE FOR NOW for coverage reasons.
110 //? static int32_t skipWhitespace(const Replaceable& text,
111 //? int32_t pos, int32_t stop);
114 * Parse a single non-whitespace character 'ch', optionally
115 * preceded by whitespace.
116 * @param id the string to be parsed
117 * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the
118 * offset of the first character to be parsed. On output, pos[0]
119 * is the index after the last parsed character. If the parse
120 * fails, pos[0] will be unchanged.
121 * @param ch the non-whitespace character to be parsed.
122 * @return true if 'ch' is seen preceded by zero or more
123 * whitespace characters.
125 static UBool
parseChar(const UnicodeString
& id
, int32_t& pos
, UChar ch
);
128 * Parse a pattern string starting at offset pos. Keywords are
129 * matched case-insensitively. Spaces may be skipped and may be
130 * optional or required. Integer values may be parsed, and if
131 * they are, they will be returned in the given array. If
132 * successful, the offset of the next non-space character is
133 * returned. On failure, -1 is returned.
134 * @param pattern must only contain lowercase characters, which
135 * will match their uppercase equivalents as well. A space
136 * character matches one or more required spaces. A '~' character
137 * matches zero or more optional spaces. A '#' character matches
138 * an integer and stores it in parsedInts, which the caller must
139 * ensure has enough capacity.
140 * @param parsedInts array to receive parsed integers. Caller
141 * must ensure that parsedInts.length is >= the number of '#'
142 * signs in 'pattern'.
143 * @return the position after the last character parsed, or -1 if
146 static int32_t parsePattern(const UnicodeString
& rule
, int32_t pos
, int32_t limit
,
147 const UnicodeString
& pattern
, int32_t* parsedInts
);
150 * Parse a pattern string within the given Replaceable and a parsing
151 * pattern. Characters are matched literally and case-sensitively
152 * except for the following special characters:
154 * ~ zero or more uprv_isRuleWhiteSpace chars
156 * If end of pattern is reached with all matches along the way,
157 * pos is advanced to the first unparsed index and returned.
158 * Otherwise -1 is returned.
159 * @param pat pattern that controls parsing
160 * @param text text to be parsed, starting at index
161 * @param index offset to first character to parse
162 * @param limit offset after last character to parse
163 * @return index after last parsed character, or -1 on parse failure.
165 static int32_t parsePattern(const UnicodeString
& pat
,
166 const Replaceable
& text
,
171 * Parse an integer at pos, either of the form \d+ or of the form
172 * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex,
174 * @param pos INPUT-OUTPUT parameter. On input, the first
175 * character to parse. On output, the character after the last
178 static int32_t parseInteger(const UnicodeString
& rule
, int32_t& pos
, int32_t limit
);
181 * Parse a Unicode identifier from the given string at the given
182 * position. Return the identifier, or an empty string if there
184 * @param str the string to parse
185 * @param pos INPUT-OUPUT parameter. On INPUT, pos is the
186 * first character to examine. It must be less than str.length(),
187 * and it must not point to a whitespace character. That is, must
188 * have pos < str.length() and
189 * !UCharacter::isWhitespace(str.char32At(pos)). On
190 * OUTPUT, the position after the last parsed character.
191 * @return the Unicode identifier, or an empty string if there is
192 * no valid identifier at pos.
194 static UnicodeString
parseUnicodeIdentifier(const UnicodeString
& str
, int32_t& pos
);
197 * Parse an unsigned 31-bit integer at the given offset. Use
198 * UCharacter.digit() to parse individual characters into digits.
199 * @param text the text to be parsed
200 * @param pos INPUT-OUTPUT parameter. On entry, pos is the
201 * offset within text at which to start parsing; it should point
202 * to a valid digit. On exit, pos is the offset after the last
203 * parsed character. If the parse failed, it will be unchanged on
204 * exit. Must be >= 0 on entry.
205 * @param radix the radix in which to parse; must be >= 2 and <=
207 * @return a non-negative parsed number, or -1 upon parse failure.
208 * Parse fails if there are no digits, that is, if pos does not
209 * point to a valid digit on entry, or if the number to be parsed
210 * does not fit into a 31-bit unsigned integer.
212 static int32_t parseNumber(const UnicodeString
& text
,
213 int32_t& pos
, int8_t radix
);
215 static void appendToRule(UnicodeString
& rule
,
218 UBool escapeUnprintable
,
219 UnicodeString
& quoteBuf
);
221 static void appendToRule(UnicodeString
& rule
,
222 const UnicodeString
& text
,
224 UBool escapeUnprintable
,
225 UnicodeString
& quoteBuf
);
227 static void appendToRule(UnicodeString
& rule
,
228 const UnicodeMatcher
* matcher
,
229 UBool escapeUnprintable
,
230 UnicodeString
& quoteBuf
);
233 // do not instantiate
240 * Get the set of "white space" characters in the sense of ICU rule
241 * parsers. Caller must close/delete result.
242 * Equivalent to the set of characters with the Pattern_White_Space Unicode property.
243 * Stable set of characters, won't change.
244 * See UAX #31 Identifier and Pattern Syntax: http://www.unicode.org/reports/tr31/
247 U_CAPI U_NAMESPACE_QUALIFIER UnicodeSet
* U_EXPORT2
248 uprv_openRuleWhiteSpaceSet(UErrorCode
* ec
);
251 * Is this character a "white space" in the sense of ICU rule parsers?
252 * Equivalent to test for Pattern_White_Space Unicode property.
253 * Stable set of characters, won't change.
254 * See UAX #31 Identifier and Pattern Syntax: http://www.unicode.org/reports/tr31/
257 U_CAPI UBool U_EXPORT2
258 uprv_isRuleWhiteSpace(UChar32 c
);