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