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