util.h 11 KB

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