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