util.h 11 KB

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