ucasemap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 2005-2012, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. *******************************************************************************
  8. * file name: ucasemap.h
  9. * encoding: US-ASCII
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2005may06
  14. * created by: Markus W. Scherer
  15. *
  16. * Case mapping service object and functions using it.
  17. */
  18. #ifndef __UCASEMAP_H__
  19. #define __UCASEMAP_H__
  20. #include "unicode/utypes.h"
  21. #include "unicode/ustring.h"
  22. #include "unicode/localpointer.h"
  23. /**
  24. * \file
  25. * \brief C API: Unicode case mapping functions using a UCaseMap service object.
  26. *
  27. * The service object takes care of memory allocations, data loading, and setup
  28. * for the attributes, as usual.
  29. *
  30. * Currently, the functionality provided here does not overlap with uchar.h
  31. * and ustring.h, except for ucasemap_toTitle().
  32. *
  33. * ucasemap_utf8XYZ() functions operate directly on UTF-8 strings.
  34. */
  35. /**
  36. * UCaseMap is an opaque service object for newer ICU case mapping functions.
  37. * Older functions did not use a service object.
  38. * @stable ICU 3.4
  39. */
  40. struct UCaseMap;
  41. typedef struct UCaseMap UCaseMap; /**< C typedef for struct UCaseMap. @stable ICU 3.4 */
  42. /**
  43. * Open a UCaseMap service object for a locale and a set of options.
  44. * The locale ID and options are preprocessed so that functions using the
  45. * service object need not process them in each call.
  46. *
  47. * @param locale ICU locale ID, used for language-dependent
  48. * upper-/lower-/title-casing according to the Unicode standard.
  49. * Usual semantics: ""=root, NULL=default locale, etc.
  50. * @param options Options bit set, used for case folding and string comparisons.
  51. * Same flags as for u_foldCase(), u_strFoldCase(),
  52. * u_strCaseCompare(), etc.
  53. * Use 0 or U_FOLD_CASE_DEFAULT for default behavior.
  54. * @param pErrorCode Must be a valid pointer to an error code value,
  55. * which must not indicate a failure before the function call.
  56. * @return Pointer to a UCaseMap service object, if successful.
  57. *
  58. * @see U_FOLD_CASE_DEFAULT
  59. * @see U_FOLD_CASE_EXCLUDE_SPECIAL_I
  60. * @see U_TITLECASE_NO_LOWERCASE
  61. * @see U_TITLECASE_NO_BREAK_ADJUSTMENT
  62. * @stable ICU 3.4
  63. */
  64. U_STABLE UCaseMap * U_EXPORT2
  65. ucasemap_open(const char *locale, uint32_t options, UErrorCode *pErrorCode);
  66. /**
  67. * Close a UCaseMap service object.
  68. * @param csm Object to be closed.
  69. * @stable ICU 3.4
  70. */
  71. U_STABLE void U_EXPORT2
  72. ucasemap_close(UCaseMap *csm);
  73. #if U_SHOW_CPLUSPLUS_API
  74. U_NAMESPACE_BEGIN
  75. /**
  76. * \class LocalUCaseMapPointer
  77. * "Smart pointer" class, closes a UCaseMap via ucasemap_close().
  78. * For most methods see the LocalPointerBase base class.
  79. *
  80. * @see LocalPointerBase
  81. * @see LocalPointer
  82. * @stable ICU 4.4
  83. */
  84. U_DEFINE_LOCAL_OPEN_POINTER(LocalUCaseMapPointer, UCaseMap, ucasemap_close);
  85. U_NAMESPACE_END
  86. #endif
  87. /**
  88. * Get the locale ID that is used for language-dependent case mappings.
  89. * @param csm UCaseMap service object.
  90. * @return locale ID
  91. * @stable ICU 3.4
  92. */
  93. U_STABLE const char * U_EXPORT2
  94. ucasemap_getLocale(const UCaseMap *csm);
  95. /**
  96. * Get the options bit set that is used for case folding and string comparisons.
  97. * @param csm UCaseMap service object.
  98. * @return options bit set
  99. * @stable ICU 3.4
  100. */
  101. U_STABLE uint32_t U_EXPORT2
  102. ucasemap_getOptions(const UCaseMap *csm);
  103. /**
  104. * Set the locale ID that is used for language-dependent case mappings.
  105. *
  106. * @param csm UCaseMap service object.
  107. * @param locale Locale ID, see ucasemap_open().
  108. * @param pErrorCode Must be a valid pointer to an error code value,
  109. * which must not indicate a failure before the function call.
  110. *
  111. * @see ucasemap_open
  112. * @stable ICU 3.4
  113. */
  114. U_STABLE void U_EXPORT2
  115. ucasemap_setLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode);
  116. /**
  117. * Set the options bit set that is used for case folding and string comparisons.
  118. *
  119. * @param csm UCaseMap service object.
  120. * @param options Options bit set, see ucasemap_open().
  121. * @param pErrorCode Must be a valid pointer to an error code value,
  122. * which must not indicate a failure before the function call.
  123. *
  124. * @see ucasemap_open
  125. * @stable ICU 3.4
  126. */
  127. U_STABLE void U_EXPORT2
  128. ucasemap_setOptions(UCaseMap *csm, uint32_t options, UErrorCode *pErrorCode);
  129. /**
  130. * Do not lowercase non-initial parts of words when titlecasing.
  131. * Option bit for titlecasing APIs that take an options bit set.
  132. *
  133. * By default, titlecasing will titlecase the first cased character
  134. * of a word and lowercase all other characters.
  135. * With this option, the other characters will not be modified.
  136. *
  137. * @see ucasemap_setOptions
  138. * @see ucasemap_toTitle
  139. * @see ucasemap_utf8ToTitle
  140. * @see UnicodeString::toTitle
  141. * @stable ICU 3.8
  142. */
  143. #define U_TITLECASE_NO_LOWERCASE 0x100
  144. /**
  145. * Do not adjust the titlecasing indexes from BreakIterator::next() indexes;
  146. * titlecase exactly the characters at breaks from the iterator.
  147. * Option bit for titlecasing APIs that take an options bit set.
  148. *
  149. * By default, titlecasing will take each break iterator index,
  150. * adjust it by looking for the next cased character, and titlecase that one.
  151. * Other characters are lowercased.
  152. *
  153. * This follows Unicode 4 & 5 section 3.13 Default Case Operations:
  154. *
  155. * R3 toTitlecase(X): Find the word boundaries based on Unicode Standard Annex
  156. * #29, "Text Boundaries." Between each pair of word boundaries, find the first
  157. * cased character F. If F exists, map F to default_title(F); then map each
  158. * subsequent character C to default_lower(C).
  159. *
  160. * @see ucasemap_setOptions
  161. * @see ucasemap_toTitle
  162. * @see ucasemap_utf8ToTitle
  163. * @see UnicodeString::toTitle
  164. * @see U_TITLECASE_NO_LOWERCASE
  165. * @stable ICU 3.8
  166. */
  167. #define U_TITLECASE_NO_BREAK_ADJUSTMENT 0x200
  168. #if !UCONFIG_NO_BREAK_ITERATION
  169. /**
  170. * Get the break iterator that is used for titlecasing.
  171. * Do not modify the returned break iterator.
  172. * @param csm UCaseMap service object.
  173. * @return titlecasing break iterator
  174. * @stable ICU 3.8
  175. */
  176. U_STABLE const UBreakIterator * U_EXPORT2
  177. ucasemap_getBreakIterator(const UCaseMap *csm);
  178. /**
  179. * Set the break iterator that is used for titlecasing.
  180. * The UCaseMap service object releases a previously set break iterator
  181. * and "adopts" this new one, taking ownership of it.
  182. * It will be released in a subsequent call to ucasemap_setBreakIterator()
  183. * or ucasemap_close().
  184. *
  185. * Break iterator operations are not thread-safe. Therefore, titlecasing
  186. * functions use non-const UCaseMap objects. It is not possible to titlecase
  187. * strings concurrently using the same UCaseMap.
  188. *
  189. * @param csm UCaseMap service object.
  190. * @param iterToAdopt Break iterator to be adopted for titlecasing.
  191. * @param pErrorCode Must be a valid pointer to an error code value,
  192. * which must not indicate a failure before the function call.
  193. *
  194. * @see ucasemap_toTitle
  195. * @see ucasemap_utf8ToTitle
  196. * @stable ICU 3.8
  197. */
  198. U_STABLE void U_EXPORT2
  199. ucasemap_setBreakIterator(UCaseMap *csm, UBreakIterator *iterToAdopt, UErrorCode *pErrorCode);
  200. /**
  201. * Titlecase a UTF-16 string. This function is almost a duplicate of u_strToTitle(),
  202. * except that it takes ucasemap_setOptions() into account and has performance
  203. * advantages from being able to use a UCaseMap object for multiple case mapping
  204. * operations, saving setup time.
  205. *
  206. * Casing is locale-dependent and context-sensitive.
  207. * Titlecasing uses a break iterator to find the first characters of words
  208. * that are to be titlecased. It titlecases those characters and lowercases
  209. * all others. (This can be modified with ucasemap_setOptions().)
  210. *
  211. * Note: This function takes a non-const UCaseMap pointer because it will
  212. * open a default break iterator if no break iterator was set yet,
  213. * and effectively call ucasemap_setBreakIterator();
  214. * also because the break iterator is stateful and will be modified during
  215. * the iteration.
  216. *
  217. * The titlecase break iterator can be provided to customize for arbitrary
  218. * styles, using rules and dictionaries beyond the standard iterators.
  219. * The standard titlecase iterator for the root locale implements the
  220. * algorithm of Unicode TR 21.
  221. *
  222. * This function uses only the setUText(), first(), next() and close() methods of the
  223. * provided break iterator.
  224. *
  225. * The result may be longer or shorter than the original.
  226. * The source string and the destination buffer must not overlap.
  227. *
  228. * @param csm UCaseMap service object. This pointer is non-const!
  229. * See the note above for details.
  230. * @param dest A buffer for the result string. The result will be NUL-terminated if
  231. * the buffer is large enough.
  232. * The contents is undefined in case of failure.
  233. * @param destCapacity The size of the buffer (number of bytes). If it is 0, then
  234. * dest may be NULL and the function will only return the length of the result
  235. * without writing any of the result string.
  236. * @param src The original string.
  237. * @param srcLength The length of the original string. If -1, then src must be NUL-terminated.
  238. * @param pErrorCode Must be a valid pointer to an error code value,
  239. * which must not indicate a failure before the function call.
  240. * @return The length of the result string, if successful - or in case of a buffer overflow,
  241. * in which case it will be greater than destCapacity.
  242. *
  243. * @see u_strToTitle
  244. * @stable ICU 3.8
  245. */
  246. U_STABLE int32_t U_EXPORT2
  247. ucasemap_toTitle(UCaseMap *csm,
  248. UChar *dest, int32_t destCapacity,
  249. const UChar *src, int32_t srcLength,
  250. UErrorCode *pErrorCode);
  251. #endif
  252. /**
  253. * Lowercase the characters in a UTF-8 string.
  254. * Casing is locale-dependent and context-sensitive.
  255. * The result may be longer or shorter than the original.
  256. * The source string and the destination buffer must not overlap.
  257. *
  258. * @param csm UCaseMap service object.
  259. * @param dest A buffer for the result string. The result will be NUL-terminated if
  260. * the buffer is large enough.
  261. * The contents is undefined in case of failure.
  262. * @param destCapacity The size of the buffer (number of bytes). If it is 0, then
  263. * dest may be NULL and the function will only return the length of the result
  264. * without writing any of the result string.
  265. * @param src The original string.
  266. * @param srcLength The length of the original string. If -1, then src must be NUL-terminated.
  267. * @param pErrorCode Must be a valid pointer to an error code value,
  268. * which must not indicate a failure before the function call.
  269. * @return The length of the result string, if successful - or in case of a buffer overflow,
  270. * in which case it will be greater than destCapacity.
  271. *
  272. * @see u_strToLower
  273. * @stable ICU 3.4
  274. */
  275. U_STABLE int32_t U_EXPORT2
  276. ucasemap_utf8ToLower(const UCaseMap *csm,
  277. char *dest, int32_t destCapacity,
  278. const char *src, int32_t srcLength,
  279. UErrorCode *pErrorCode);
  280. /**
  281. * Uppercase the characters in a UTF-8 string.
  282. * Casing is locale-dependent and context-sensitive.
  283. * The result may be longer or shorter than the original.
  284. * The source string and the destination buffer must not overlap.
  285. *
  286. * @param csm UCaseMap service object.
  287. * @param dest A buffer for the result string. The result will be NUL-terminated if
  288. * the buffer is large enough.
  289. * The contents is undefined in case of failure.
  290. * @param destCapacity The size of the buffer (number of bytes). If it is 0, then
  291. * dest may be NULL and the function will only return the length of the result
  292. * without writing any of the result string.
  293. * @param src The original string.
  294. * @param srcLength The length of the original string. If -1, then src must be NUL-terminated.
  295. * @param pErrorCode Must be a valid pointer to an error code value,
  296. * which must not indicate a failure before the function call.
  297. * @return The length of the result string, if successful - or in case of a buffer overflow,
  298. * in which case it will be greater than destCapacity.
  299. *
  300. * @see u_strToUpper
  301. * @stable ICU 3.4
  302. */
  303. U_STABLE int32_t U_EXPORT2
  304. ucasemap_utf8ToUpper(const UCaseMap *csm,
  305. char *dest, int32_t destCapacity,
  306. const char *src, int32_t srcLength,
  307. UErrorCode *pErrorCode);
  308. #if !UCONFIG_NO_BREAK_ITERATION
  309. /**
  310. * Titlecase a UTF-8 string.
  311. * Casing is locale-dependent and context-sensitive.
  312. * Titlecasing uses a break iterator to find the first characters of words
  313. * that are to be titlecased. It titlecases those characters and lowercases
  314. * all others. (This can be modified with ucasemap_setOptions().)
  315. *
  316. * Note: This function takes a non-const UCaseMap pointer because it will
  317. * open a default break iterator if no break iterator was set yet,
  318. * and effectively call ucasemap_setBreakIterator();
  319. * also because the break iterator is stateful and will be modified during
  320. * the iteration.
  321. *
  322. * The titlecase break iterator can be provided to customize for arbitrary
  323. * styles, using rules and dictionaries beyond the standard iterators.
  324. * The standard titlecase iterator for the root locale implements the
  325. * algorithm of Unicode TR 21.
  326. *
  327. * This function uses only the setUText(), first(), next() and close() methods of the
  328. * provided break iterator.
  329. *
  330. * The result may be longer or shorter than the original.
  331. * The source string and the destination buffer must not overlap.
  332. *
  333. * @param csm UCaseMap service object. This pointer is non-const!
  334. * See the note above for details.
  335. * @param dest A buffer for the result string. The result will be NUL-terminated if
  336. * the buffer is large enough.
  337. * The contents is undefined in case of failure.
  338. * @param destCapacity The size of the buffer (number of bytes). If it is 0, then
  339. * dest may be NULL and the function will only return the length of the result
  340. * without writing any of the result string.
  341. * @param src The original string.
  342. * @param srcLength The length of the original string. If -1, then src must be NUL-terminated.
  343. * @param pErrorCode Must be a valid pointer to an error code value,
  344. * which must not indicate a failure before the function call.
  345. * @return The length of the result string, if successful - or in case of a buffer overflow,
  346. * in which case it will be greater than destCapacity.
  347. *
  348. * @see u_strToTitle
  349. * @see U_TITLECASE_NO_LOWERCASE
  350. * @see U_TITLECASE_NO_BREAK_ADJUSTMENT
  351. * @stable ICU 3.8
  352. */
  353. U_STABLE int32_t U_EXPORT2
  354. ucasemap_utf8ToTitle(UCaseMap *csm,
  355. char *dest, int32_t destCapacity,
  356. const char *src, int32_t srcLength,
  357. UErrorCode *pErrorCode);
  358. #endif
  359. /**
  360. * Case-folds the characters in a UTF-8 string.
  361. *
  362. * Case-folding is locale-independent and not context-sensitive,
  363. * but there is an option for whether to include or exclude mappings for dotted I
  364. * and dotless i that are marked with 'T' in CaseFolding.txt.
  365. *
  366. * The result may be longer or shorter than the original.
  367. * The source string and the destination buffer must not overlap.
  368. *
  369. * @param csm UCaseMap service object.
  370. * @param dest A buffer for the result string. The result will be NUL-terminated if
  371. * the buffer is large enough.
  372. * The contents is undefined in case of failure.
  373. * @param destCapacity The size of the buffer (number of bytes). If it is 0, then
  374. * dest may be NULL and the function will only return the length of the result
  375. * without writing any of the result string.
  376. * @param src The original string.
  377. * @param srcLength The length of the original string. If -1, then src must be NUL-terminated.
  378. * @param pErrorCode Must be a valid pointer to an error code value,
  379. * which must not indicate a failure before the function call.
  380. * @return The length of the result string, if successful - or in case of a buffer overflow,
  381. * in which case it will be greater than destCapacity.
  382. *
  383. * @see u_strFoldCase
  384. * @see ucasemap_setOptions
  385. * @see U_FOLD_CASE_DEFAULT
  386. * @see U_FOLD_CASE_EXCLUDE_SPECIAL_I
  387. * @stable ICU 3.8
  388. */
  389. U_STABLE int32_t U_EXPORT2
  390. ucasemap_utf8FoldCase(const UCaseMap *csm,
  391. char *dest, int32_t destCapacity,
  392. const char *src, int32_t srcLength,
  393. UErrorCode *pErrorCode);
  394. #endif