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