ucasemap_imp.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // ucasemap_imp.h
  4. // created: 2017feb08 Markus W. Scherer
  5. #ifndef __UCASEMAP_IMP_H__
  6. #define __UCASEMAP_IMP_H__
  7. #include "unicode/utypes.h"
  8. #include "unicode/ucasemap.h"
  9. #include "unicode/uchar.h"
  10. #include "ucase.h"
  11. /**
  12. * Bit mask for the titlecasing iterator options bit field.
  13. * Currently only 3 out of 8 values are used:
  14. * 0 (words), U_TITLECASE_WHOLE_STRING, U_TITLECASE_SENTENCES.
  15. * See stringoptions.h.
  16. * @internal
  17. */
  18. #define U_TITLECASE_ITERATOR_MASK 0xe0
  19. /**
  20. * Bit mask for the titlecasing index adjustment options bit set.
  21. * Currently two bits are defined:
  22. * U_TITLECASE_NO_BREAK_ADJUSTMENT, U_TITLECASE_ADJUST_TO_CASED.
  23. * See stringoptions.h.
  24. * @internal
  25. */
  26. #define U_TITLECASE_ADJUSTMENT_MASK 0x600
  27. /**
  28. * Internal API, used by u_strcasecmp() etc.
  29. * Compare strings case-insensitively,
  30. * in code point order or code unit order.
  31. */
  32. U_CFUNC int32_t
  33. u_strcmpFold(const UChar *s1, int32_t length1,
  34. const UChar *s2, int32_t length2,
  35. uint32_t options,
  36. UErrorCode *pErrorCode);
  37. /**
  38. * Internal API, used for detecting length of
  39. * shared prefix case-insensitively.
  40. * @param s1 input string 1
  41. * @param length1 length of string 1, or -1 (NULL terminated)
  42. * @param s2 input string 2
  43. * @param length2 length of string 2, or -1 (NULL terminated)
  44. * @param options compare options
  45. * @param matchLen1 (output) length of partial prefix match in s1
  46. * @param matchLen2 (output) length of partial prefix match in s2
  47. * @param pErrorCode receives error status
  48. */
  49. U_CAPI void
  50. u_caseInsensitivePrefixMatch(const UChar *s1, int32_t length1,
  51. const UChar *s2, int32_t length2,
  52. uint32_t options,
  53. int32_t *matchLen1, int32_t *matchLen2,
  54. UErrorCode *pErrorCode);
  55. #ifdef __cplusplus
  56. U_NAMESPACE_BEGIN
  57. class BreakIterator; // unicode/brkiter.h
  58. class ByteSink;
  59. class Locale; // unicode/locid.h
  60. /** Returns true if the options are valid. Otherwise false, and sets an error. */
  61. inline UBool ustrcase_checkTitleAdjustmentOptions(uint32_t options, UErrorCode &errorCode) {
  62. if (U_FAILURE(errorCode)) { return false; }
  63. if ((options & U_TITLECASE_ADJUSTMENT_MASK) == U_TITLECASE_ADJUSTMENT_MASK) {
  64. // Both options together.
  65. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  66. return false;
  67. }
  68. return true;
  69. }
  70. inline UBool ustrcase_isLNS(UChar32 c) {
  71. // Letter, number, symbol,
  72. // or a private use code point because those are typically used as letters or numbers.
  73. // Consider modifier letters only if they are cased.
  74. const uint32_t LNS = (U_GC_L_MASK|U_GC_N_MASK|U_GC_S_MASK|U_GC_CO_MASK) & ~U_GC_LM_MASK;
  75. int gc = u_charType(c);
  76. return (U_MASK(gc) & LNS) != 0 || (gc == U_MODIFIER_LETTER && ucase_getType(c) != UCASE_NONE);
  77. }
  78. #if !UCONFIG_NO_BREAK_ITERATION
  79. /** Returns nullptr if error. Pass in either locale or locID, not both. */
  80. U_CFUNC
  81. BreakIterator *ustrcase_getTitleBreakIterator(
  82. const Locale *locale, const char *locID, uint32_t options, BreakIterator *iter,
  83. LocalPointer<BreakIterator> &ownedIter, UErrorCode &errorCode);
  84. #endif
  85. U_NAMESPACE_END
  86. #include "unicode/unistr.h" // for UStringCaseMapper
  87. /*
  88. * Internal string casing functions implementing
  89. * ustring.h/ustrcase.cpp and UnicodeString case mapping functions.
  90. */
  91. struct UCaseMap : public icu::UMemory {
  92. /** Implements most of ucasemap_open(). */
  93. UCaseMap(const char *localeID, uint32_t opts, UErrorCode *pErrorCode);
  94. ~UCaseMap();
  95. #if !UCONFIG_NO_BREAK_ITERATION
  96. icu::BreakIterator *iter; /* We adopt the iterator, so we own it. */
  97. #endif
  98. char locale[32];
  99. int32_t caseLocale;
  100. uint32_t options;
  101. };
  102. #if UCONFIG_NO_BREAK_ITERATION
  103. # define UCASEMAP_BREAK_ITERATOR_PARAM
  104. # define UCASEMAP_BREAK_ITERATOR_UNUSED
  105. # define UCASEMAP_BREAK_ITERATOR
  106. # define UCASEMAP_BREAK_ITERATOR_NULL
  107. #else
  108. # define UCASEMAP_BREAK_ITERATOR_PARAM icu::BreakIterator *iter,
  109. # define UCASEMAP_BREAK_ITERATOR_UNUSED icu::BreakIterator *,
  110. # define UCASEMAP_BREAK_ITERATOR iter,
  111. # define UCASEMAP_BREAK_ITERATOR_NULL NULL,
  112. #endif
  113. U_CFUNC int32_t
  114. ustrcase_getCaseLocale(const char *locale);
  115. // TODO: swap src / dest if approved for new public api
  116. /** Implements UStringCaseMapper. */
  117. U_CFUNC int32_t U_CALLCONV
  118. ustrcase_internalToLower(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  119. char16_t *dest, int32_t destCapacity,
  120. const char16_t *src, int32_t srcLength,
  121. icu::Edits *edits,
  122. UErrorCode &errorCode);
  123. /** Implements UStringCaseMapper. */
  124. U_CFUNC int32_t U_CALLCONV
  125. ustrcase_internalToUpper(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  126. char16_t *dest, int32_t destCapacity,
  127. const char16_t *src, int32_t srcLength,
  128. icu::Edits *edits,
  129. UErrorCode &errorCode);
  130. #if !UCONFIG_NO_BREAK_ITERATION
  131. /** Implements UStringCaseMapper. */
  132. U_CFUNC int32_t U_CALLCONV
  133. ustrcase_internalToTitle(int32_t caseLocale, uint32_t options,
  134. icu::BreakIterator *iter,
  135. char16_t *dest, int32_t destCapacity,
  136. const char16_t *src, int32_t srcLength,
  137. icu::Edits *edits,
  138. UErrorCode &errorCode);
  139. #endif
  140. /** Implements UStringCaseMapper. */
  141. U_CFUNC int32_t U_CALLCONV
  142. ustrcase_internalFold(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  143. char16_t *dest, int32_t destCapacity,
  144. const char16_t *src, int32_t srcLength,
  145. icu::Edits *edits,
  146. UErrorCode &errorCode);
  147. /**
  148. * Common string case mapping implementation for ucasemap_toXyz() and UnicodeString::toXyz().
  149. * Implements argument checking.
  150. */
  151. U_CFUNC int32_t
  152. ustrcase_map(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  153. char16_t *dest, int32_t destCapacity,
  154. const char16_t *src, int32_t srcLength,
  155. UStringCaseMapper *stringCaseMapper,
  156. icu::Edits *edits,
  157. UErrorCode &errorCode);
  158. /**
  159. * Common string case mapping implementation for old-fashioned u_strToXyz() functions
  160. * that allow the source string to overlap the destination buffer.
  161. * Implements argument checking and internally works with an intermediate buffer if necessary.
  162. */
  163. U_CFUNC int32_t
  164. ustrcase_mapWithOverlap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  165. char16_t *dest, int32_t destCapacity,
  166. const char16_t *src, int32_t srcLength,
  167. UStringCaseMapper *stringCaseMapper,
  168. UErrorCode &errorCode);
  169. /**
  170. * UTF-8 string case mapping function type, used by ucasemap_mapUTF8().
  171. * UTF-8 version of UStringCaseMapper.
  172. * All error checking must be done.
  173. * The UCaseMap must be fully initialized, with locale and/or iter set as needed.
  174. */
  175. typedef void U_CALLCONV
  176. UTF8CaseMapper(int32_t caseLocale, uint32_t options,
  177. #if !UCONFIG_NO_BREAK_ITERATION
  178. icu::BreakIterator *iter,
  179. #endif
  180. const uint8_t *src, int32_t srcLength,
  181. icu::ByteSink &sink, icu::Edits *edits,
  182. UErrorCode &errorCode);
  183. #if !UCONFIG_NO_BREAK_ITERATION
  184. /** Implements UTF8CaseMapper. */
  185. U_CFUNC void U_CALLCONV
  186. ucasemap_internalUTF8ToTitle(int32_t caseLocale, uint32_t options,
  187. icu::BreakIterator *iter,
  188. const uint8_t *src, int32_t srcLength,
  189. icu::ByteSink &sink, icu::Edits *edits,
  190. UErrorCode &errorCode);
  191. #endif
  192. void
  193. ucasemap_mapUTF8(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  194. const char *src, int32_t srcLength,
  195. UTF8CaseMapper *stringCaseMapper,
  196. icu::ByteSink &sink, icu::Edits *edits,
  197. UErrorCode &errorCode);
  198. /**
  199. * Implements argument checking and buffer handling
  200. * for UTF-8 string case mapping as a common function.
  201. */
  202. int32_t
  203. ucasemap_mapUTF8(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  204. char *dest, int32_t destCapacity,
  205. const char *src, int32_t srcLength,
  206. UTF8CaseMapper *stringCaseMapper,
  207. icu::Edits *edits,
  208. UErrorCode &errorCode);
  209. U_NAMESPACE_BEGIN
  210. namespace GreekUpper {
  211. // Data bits.
  212. static const uint32_t UPPER_MASK = 0x3ff;
  213. static const uint32_t HAS_VOWEL = 0x1000;
  214. static const uint32_t HAS_YPOGEGRAMMENI = 0x2000;
  215. static const uint32_t HAS_ACCENT = 0x4000;
  216. static const uint32_t HAS_DIALYTIKA = 0x8000;
  217. // Further bits during data building and processing, not stored in the data map.
  218. static const uint32_t HAS_COMBINING_DIALYTIKA = 0x10000;
  219. static const uint32_t HAS_OTHER_GREEK_DIACRITIC = 0x20000;
  220. static const uint32_t HAS_VOWEL_AND_ACCENT = HAS_VOWEL | HAS_ACCENT;
  221. static const uint32_t HAS_VOWEL_AND_ACCENT_AND_DIALYTIKA =
  222. HAS_VOWEL_AND_ACCENT | HAS_DIALYTIKA;
  223. static const uint32_t HAS_EITHER_DIALYTIKA = HAS_DIALYTIKA | HAS_COMBINING_DIALYTIKA;
  224. // State bits.
  225. static const uint32_t AFTER_CASED = 1;
  226. static const uint32_t AFTER_VOWEL_WITH_COMBINING_ACCENT = 2;
  227. static const uint32_t AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT = 4;
  228. uint32_t getLetterData(UChar32 c);
  229. /**
  230. * Returns a non-zero value for each of the Greek combining diacritics
  231. * listed in The Unicode Standard, version 8, chapter 7.2 Greek,
  232. * plus some perispomeni look-alikes.
  233. */
  234. uint32_t getDiacriticData(UChar32 c);
  235. } // namespace GreekUpper
  236. U_NAMESPACE_END
  237. #endif // __cplusplus
  238. #endif // __UCASEMAP_IMP_H__