ucasemap.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2005-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: ucasemap.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2005may06
  16. * created by: Markus W. Scherer
  17. *
  18. * Case mapping service object and functions using it.
  19. */
  20. #include "unicode/utypes.h"
  21. #include "unicode/brkiter.h"
  22. #include "unicode/bytestream.h"
  23. #include "unicode/casemap.h"
  24. #include "unicode/edits.h"
  25. #include "unicode/stringoptions.h"
  26. #include "unicode/stringpiece.h"
  27. #include "unicode/ubrk.h"
  28. #include "unicode/uloc.h"
  29. #include "unicode/ustring.h"
  30. #include "unicode/ucasemap.h"
  31. #if !UCONFIG_NO_BREAK_ITERATION
  32. #include "unicode/utext.h"
  33. #endif
  34. #include "unicode/utf.h"
  35. #include "unicode/utf8.h"
  36. #include "unicode/utf16.h"
  37. #include "bytesinkutil.h"
  38. #include "cmemory.h"
  39. #include "cstring.h"
  40. #include "uassert.h"
  41. #include "ucase.h"
  42. #include "ucasemap_imp.h"
  43. U_NAMESPACE_USE
  44. /* UCaseMap service object -------------------------------------------------- */
  45. UCaseMap::UCaseMap(const char *localeID, uint32_t opts, UErrorCode *pErrorCode) :
  46. #if !UCONFIG_NO_BREAK_ITERATION
  47. iter(nullptr),
  48. #endif
  49. caseLocale(UCASE_LOC_UNKNOWN), options(opts) {
  50. ucasemap_setLocale(this, localeID, pErrorCode);
  51. }
  52. UCaseMap::~UCaseMap() {
  53. #if !UCONFIG_NO_BREAK_ITERATION
  54. delete iter;
  55. #endif
  56. }
  57. U_CAPI UCaseMap * U_EXPORT2
  58. ucasemap_open(const char *locale, uint32_t options, UErrorCode *pErrorCode) {
  59. if(U_FAILURE(*pErrorCode)) {
  60. return nullptr;
  61. }
  62. UCaseMap *csm = new UCaseMap(locale, options, pErrorCode);
  63. if(csm==nullptr) {
  64. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  65. return nullptr;
  66. } else if (U_FAILURE(*pErrorCode)) {
  67. delete csm;
  68. return nullptr;
  69. }
  70. return csm;
  71. }
  72. U_CAPI void U_EXPORT2
  73. ucasemap_close(UCaseMap *csm) {
  74. delete csm;
  75. }
  76. U_CAPI const char * U_EXPORT2
  77. ucasemap_getLocale(const UCaseMap *csm) {
  78. return csm->locale;
  79. }
  80. U_CAPI uint32_t U_EXPORT2
  81. ucasemap_getOptions(const UCaseMap *csm) {
  82. return csm->options;
  83. }
  84. U_CAPI void U_EXPORT2
  85. ucasemap_setLocale(UCaseMap *csm, const char *locale, UErrorCode *pErrorCode) {
  86. if(U_FAILURE(*pErrorCode)) {
  87. return;
  88. }
  89. if (locale != nullptr && *locale == 0) {
  90. csm->locale[0] = 0;
  91. csm->caseLocale = UCASE_LOC_ROOT;
  92. return;
  93. }
  94. int32_t length=uloc_getName(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode);
  95. if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || length==sizeof(csm->locale)) {
  96. *pErrorCode=U_ZERO_ERROR;
  97. /* we only really need the language code for case mappings */
  98. length=uloc_getLanguage(locale, csm->locale, (int32_t)sizeof(csm->locale), pErrorCode);
  99. }
  100. if(length==sizeof(csm->locale)) {
  101. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  102. }
  103. if(U_SUCCESS(*pErrorCode)) {
  104. csm->caseLocale = ucase_getCaseLocale(csm->locale);
  105. } else {
  106. csm->locale[0]=0;
  107. csm->caseLocale = UCASE_LOC_ROOT;
  108. }
  109. }
  110. U_CAPI void U_EXPORT2
  111. ucasemap_setOptions(UCaseMap *csm, uint32_t options, UErrorCode *pErrorCode) {
  112. if(U_FAILURE(*pErrorCode)) {
  113. return;
  114. }
  115. csm->options=options;
  116. }
  117. /* UTF-8 string case mappings ----------------------------------------------- */
  118. /* TODO(markus): Move to a new, separate utf8case.cpp file. */
  119. namespace {
  120. /* append a full case mapping result, see UCASE_MAX_STRING_LENGTH */
  121. inline UBool
  122. appendResult(int32_t cpLength, int32_t result, const char16_t *s,
  123. ByteSink &sink, uint32_t options, icu::Edits *edits, UErrorCode &errorCode) {
  124. U_ASSERT(U_SUCCESS(errorCode));
  125. /* decode the result */
  126. if(result<0) {
  127. /* (not) original code point */
  128. if(edits!=nullptr) {
  129. edits->addUnchanged(cpLength);
  130. }
  131. if((options & U_OMIT_UNCHANGED_TEXT) == 0) {
  132. ByteSinkUtil::appendCodePoint(cpLength, ~result, sink);
  133. }
  134. } else {
  135. if(result<=UCASE_MAX_STRING_LENGTH) {
  136. // string: "result" is the UTF-16 length
  137. return ByteSinkUtil::appendChange(cpLength, s, result, sink, edits, errorCode);
  138. } else {
  139. ByteSinkUtil::appendCodePoint(cpLength, result, sink, edits);
  140. }
  141. }
  142. return true;
  143. }
  144. // See unicode/utf8.h U8_APPEND_UNSAFE().
  145. inline uint8_t getTwoByteLead(UChar32 c) { return static_cast<uint8_t>((c >> 6) | 0xc0); }
  146. inline uint8_t getTwoByteTrail(UChar32 c) { return static_cast<uint8_t>((c & 0x3f) | 0x80); }
  147. UChar32 U_CALLCONV
  148. utf8_caseContextIterator(void *context, int8_t dir) {
  149. UCaseContext* csc = static_cast<UCaseContext*>(context);
  150. UChar32 c;
  151. if(dir<0) {
  152. /* reset for backward iteration */
  153. csc->index=csc->cpStart;
  154. csc->dir=dir;
  155. } else if(dir>0) {
  156. /* reset for forward iteration */
  157. csc->index=csc->cpLimit;
  158. csc->dir=dir;
  159. } else {
  160. /* continue current iteration direction */
  161. dir=csc->dir;
  162. }
  163. if(dir<0) {
  164. if(csc->start<csc->index) {
  165. U8_PREV((const uint8_t *)csc->p, csc->start, csc->index, c);
  166. return c;
  167. }
  168. } else {
  169. if(csc->index<csc->limit) {
  170. U8_NEXT((const uint8_t *)csc->p, csc->index, csc->limit, c);
  171. return c;
  172. }
  173. }
  174. return U_SENTINEL;
  175. }
  176. /**
  177. * caseLocale >= 0: Lowercases [srcStart..srcLimit[ but takes context [0..srcLength[ into account.
  178. * caseLocale < 0: Case-folds [srcStart..srcLimit[.
  179. */
  180. void toLower(int32_t caseLocale, uint32_t options,
  181. const uint8_t *src, UCaseContext *csc, int32_t srcStart, int32_t srcLimit,
  182. icu::ByteSink &sink, icu::Edits *edits, UErrorCode &errorCode) {
  183. const int8_t *latinToLower;
  184. if (caseLocale == UCASE_LOC_ROOT ||
  185. (caseLocale >= 0 ?
  186. !(caseLocale == UCASE_LOC_TURKISH || caseLocale == UCASE_LOC_LITHUANIAN) :
  187. (options & _FOLD_CASE_OPTIONS_MASK) == U_FOLD_CASE_DEFAULT)) {
  188. latinToLower = LatinCase::TO_LOWER_NORMAL;
  189. } else {
  190. latinToLower = LatinCase::TO_LOWER_TR_LT;
  191. }
  192. const UTrie2 *trie = ucase_getTrie();
  193. int32_t prev = srcStart;
  194. int32_t srcIndex = srcStart;
  195. for (;;) {
  196. // fast path for simple cases
  197. int32_t cpStart;
  198. UChar32 c;
  199. for (;;) {
  200. if (U_FAILURE(errorCode) || srcIndex >= srcLimit) {
  201. c = U_SENTINEL;
  202. break;
  203. }
  204. uint8_t lead = src[srcIndex++];
  205. if (lead <= 0x7f) {
  206. int8_t d = latinToLower[lead];
  207. if (d == LatinCase::EXC) {
  208. cpStart = srcIndex - 1;
  209. c = lead;
  210. break;
  211. }
  212. if (d == 0) { continue; }
  213. ByteSinkUtil::appendUnchanged(src + prev, srcIndex - 1 - prev,
  214. sink, options, edits, errorCode);
  215. char ascii = static_cast<char>(lead + d);
  216. sink.Append(&ascii, 1);
  217. if (edits != nullptr) {
  218. edits->addReplace(1, 1);
  219. }
  220. prev = srcIndex;
  221. continue;
  222. } else if (lead < 0xe3) {
  223. uint8_t t;
  224. if (0xc2 <= lead && lead <= 0xc5 && srcIndex < srcLimit &&
  225. (t = src[srcIndex] - 0x80) <= 0x3f) {
  226. // U+0080..U+017F
  227. ++srcIndex;
  228. c = ((lead - 0xc0) << 6) | t;
  229. int8_t d = latinToLower[c];
  230. if (d == LatinCase::EXC) {
  231. cpStart = srcIndex - 2;
  232. break;
  233. }
  234. if (d == 0) { continue; }
  235. ByteSinkUtil::appendUnchanged(src + prev, srcIndex - 2 - prev,
  236. sink, options, edits, errorCode);
  237. ByteSinkUtil::appendTwoBytes(c + d, sink);
  238. if (edits != nullptr) {
  239. edits->addReplace(2, 2);
  240. }
  241. prev = srcIndex;
  242. continue;
  243. }
  244. } else if ((lead <= 0xe9 || lead == 0xeb || lead == 0xec) &&
  245. (srcIndex + 2) <= srcLimit &&
  246. U8_IS_TRAIL(src[srcIndex]) && U8_IS_TRAIL(src[srcIndex + 1])) {
  247. // most of CJK: no case mappings
  248. srcIndex += 2;
  249. continue;
  250. }
  251. cpStart = --srcIndex;
  252. U8_NEXT(src, srcIndex, srcLimit, c);
  253. if (c < 0) {
  254. // ill-formed UTF-8
  255. continue;
  256. }
  257. uint16_t props = UTRIE2_GET16(trie, c);
  258. if (UCASE_HAS_EXCEPTION(props)) { break; }
  259. int32_t delta;
  260. if (!UCASE_IS_UPPER_OR_TITLE(props) || (delta = UCASE_GET_DELTA(props)) == 0) {
  261. continue;
  262. }
  263. ByteSinkUtil::appendUnchanged(src + prev, cpStart - prev,
  264. sink, options, edits, errorCode);
  265. ByteSinkUtil::appendCodePoint(srcIndex - cpStart, c + delta, sink, edits);
  266. prev = srcIndex;
  267. }
  268. if (c < 0) {
  269. break;
  270. }
  271. // slow path
  272. const char16_t *s;
  273. if (caseLocale >= 0) {
  274. csc->cpStart = cpStart;
  275. csc->cpLimit = srcIndex;
  276. c = ucase_toFullLower(c, utf8_caseContextIterator, csc, &s, caseLocale);
  277. } else {
  278. c = ucase_toFullFolding(c, &s, options);
  279. }
  280. if (c >= 0) {
  281. ByteSinkUtil::appendUnchanged(src + prev, cpStart - prev,
  282. sink, options, edits, errorCode);
  283. appendResult(srcIndex - cpStart, c, s, sink, options, edits, errorCode);
  284. prev = srcIndex;
  285. }
  286. }
  287. ByteSinkUtil::appendUnchanged(src + prev, srcIndex - prev,
  288. sink, options, edits, errorCode);
  289. }
  290. void toUpper(int32_t caseLocale, uint32_t options,
  291. const uint8_t *src, UCaseContext *csc, int32_t srcLength,
  292. icu::ByteSink &sink, icu::Edits *edits, UErrorCode &errorCode) {
  293. const int8_t *latinToUpper;
  294. if (caseLocale == UCASE_LOC_TURKISH) {
  295. latinToUpper = LatinCase::TO_UPPER_TR;
  296. } else {
  297. latinToUpper = LatinCase::TO_UPPER_NORMAL;
  298. }
  299. const UTrie2 *trie = ucase_getTrie();
  300. int32_t prev = 0;
  301. int32_t srcIndex = 0;
  302. for (;;) {
  303. // fast path for simple cases
  304. int32_t cpStart;
  305. UChar32 c;
  306. for (;;) {
  307. if (U_FAILURE(errorCode) || srcIndex >= srcLength) {
  308. c = U_SENTINEL;
  309. break;
  310. }
  311. uint8_t lead = src[srcIndex++];
  312. if (lead <= 0x7f) {
  313. int8_t d = latinToUpper[lead];
  314. if (d == LatinCase::EXC) {
  315. cpStart = srcIndex - 1;
  316. c = lead;
  317. break;
  318. }
  319. if (d == 0) { continue; }
  320. ByteSinkUtil::appendUnchanged(src + prev, srcIndex - 1 - prev,
  321. sink, options, edits, errorCode);
  322. char ascii = static_cast<char>(lead + d);
  323. sink.Append(&ascii, 1);
  324. if (edits != nullptr) {
  325. edits->addReplace(1, 1);
  326. }
  327. prev = srcIndex;
  328. continue;
  329. } else if (lead < 0xe3) {
  330. uint8_t t;
  331. if (0xc2 <= lead && lead <= 0xc5 && srcIndex < srcLength &&
  332. (t = src[srcIndex] - 0x80) <= 0x3f) {
  333. // U+0080..U+017F
  334. ++srcIndex;
  335. c = ((lead - 0xc0) << 6) | t;
  336. int8_t d = latinToUpper[c];
  337. if (d == LatinCase::EXC) {
  338. cpStart = srcIndex - 2;
  339. break;
  340. }
  341. if (d == 0) { continue; }
  342. ByteSinkUtil::appendUnchanged(src + prev, srcIndex - 2 - prev,
  343. sink, options, edits, errorCode);
  344. ByteSinkUtil::appendTwoBytes(c + d, sink);
  345. if (edits != nullptr) {
  346. edits->addReplace(2, 2);
  347. }
  348. prev = srcIndex;
  349. continue;
  350. }
  351. } else if ((lead <= 0xe9 || lead == 0xeb || lead == 0xec) &&
  352. (srcIndex + 2) <= srcLength &&
  353. U8_IS_TRAIL(src[srcIndex]) && U8_IS_TRAIL(src[srcIndex + 1])) {
  354. // most of CJK: no case mappings
  355. srcIndex += 2;
  356. continue;
  357. }
  358. cpStart = --srcIndex;
  359. U8_NEXT(src, srcIndex, srcLength, c);
  360. if (c < 0) {
  361. // ill-formed UTF-8
  362. continue;
  363. }
  364. uint16_t props = UTRIE2_GET16(trie, c);
  365. if (UCASE_HAS_EXCEPTION(props)) { break; }
  366. int32_t delta;
  367. if (UCASE_GET_TYPE(props) != UCASE_LOWER || (delta = UCASE_GET_DELTA(props)) == 0) {
  368. continue;
  369. }
  370. ByteSinkUtil::appendUnchanged(src + prev, cpStart - prev,
  371. sink, options, edits, errorCode);
  372. ByteSinkUtil::appendCodePoint(srcIndex - cpStart, c + delta, sink, edits);
  373. prev = srcIndex;
  374. }
  375. if (c < 0) {
  376. break;
  377. }
  378. // slow path
  379. csc->cpStart = cpStart;
  380. csc->cpLimit = srcIndex;
  381. const char16_t *s;
  382. c = ucase_toFullUpper(c, utf8_caseContextIterator, csc, &s, caseLocale);
  383. if (c >= 0) {
  384. ByteSinkUtil::appendUnchanged(src + prev, cpStart - prev,
  385. sink, options, edits, errorCode);
  386. appendResult(srcIndex - cpStart, c, s, sink, options, edits, errorCode);
  387. prev = srcIndex;
  388. }
  389. }
  390. ByteSinkUtil::appendUnchanged(src + prev, srcIndex - prev,
  391. sink, options, edits, errorCode);
  392. }
  393. } // namespace
  394. #if !UCONFIG_NO_BREAK_ITERATION
  395. namespace {
  396. constexpr uint8_t ACUTE_BYTE0 = u8"\u0301"[0];
  397. constexpr uint8_t ACUTE_BYTE1 = u8"\u0301"[1];
  398. /**
  399. * Input: c is a letter I with or without acute accent.
  400. * start is the index in src after c, and is less than segmentLimit.
  401. * If a plain i/I is followed by a plain j/J,
  402. * or an i/I with acute (precomposed or decomposed) is followed by a j/J with acute,
  403. * then we output accordingly.
  404. *
  405. * @return the src index after the titlecased sequence, or the start index if no Dutch IJ
  406. */
  407. int32_t maybeTitleDutchIJ(const uint8_t *src, UChar32 c, int32_t start, int32_t segmentLimit,
  408. ByteSink &sink, uint32_t options, icu::Edits *edits, UErrorCode &errorCode) {
  409. U_ASSERT(start < segmentLimit);
  410. int32_t index = start;
  411. bool withAcute = false;
  412. // If the conditions are met, then the following variables tell us what to output.
  413. int32_t unchanged1 = 0; // code units before the j, or the whole sequence (0..3)
  414. bool doTitleJ = false; // true if the j needs to be titlecased
  415. int32_t unchanged2 = 0; // after the j (0 or 1)
  416. // next character after the first letter
  417. UChar32 c2;
  418. c2 = src[index++];
  419. // Is the first letter an i/I with accent?
  420. if (c == u'I') {
  421. if (c2 == ACUTE_BYTE0 && index < segmentLimit && src[index++] == ACUTE_BYTE1) {
  422. withAcute = true;
  423. unchanged1 = 2; // ACUTE is 2 code units in UTF-8
  424. if (index == segmentLimit) { return start; }
  425. c2 = src[index++];
  426. }
  427. } else { // Í
  428. withAcute = true;
  429. }
  430. // Is the next character a j/J?
  431. if (c2 == u'j') {
  432. doTitleJ = true;
  433. } else if (c2 == u'J') {
  434. ++unchanged1;
  435. } else {
  436. return start;
  437. }
  438. // A plain i/I must be followed by a plain j/J.
  439. // An i/I with acute must be followed by a j/J with acute.
  440. if (withAcute) {
  441. if ((index + 1) >= segmentLimit || src[index++] != ACUTE_BYTE0 || src[index++] != ACUTE_BYTE1) {
  442. return start;
  443. }
  444. if (doTitleJ) {
  445. unchanged2 = 2; // ACUTE is 2 code units in UTF-8
  446. } else {
  447. unchanged1 = unchanged1 + 2; // ACUTE is 2 code units in UTF-8
  448. }
  449. }
  450. // There must not be another combining mark.
  451. if (index < segmentLimit) {
  452. int32_t cp;
  453. int32_t i = index;
  454. U8_NEXT(src, i, segmentLimit, cp);
  455. uint32_t typeMask = U_GET_GC_MASK(cp);
  456. if ((typeMask & U_GC_M_MASK) != 0) {
  457. return start;
  458. }
  459. }
  460. // Output the rest of the Dutch IJ.
  461. ByteSinkUtil::appendUnchanged(src + start, unchanged1, sink, options, edits, errorCode);
  462. start += unchanged1;
  463. if (doTitleJ) {
  464. ByteSinkUtil::appendCodePoint(1, u'J', sink, edits);
  465. ++start;
  466. }
  467. ByteSinkUtil::appendUnchanged(src + start, unchanged2, sink, options, edits, errorCode);
  468. U_ASSERT(start + unchanged2 == index);
  469. return index;
  470. }
  471. } // namespace
  472. U_CFUNC void U_CALLCONV
  473. ucasemap_internalUTF8ToTitle(
  474. int32_t caseLocale, uint32_t options, BreakIterator *iter,
  475. const uint8_t *src, int32_t srcLength,
  476. ByteSink &sink, icu::Edits *edits,
  477. UErrorCode &errorCode) {
  478. if (!ustrcase_checkTitleAdjustmentOptions(options, errorCode)) {
  479. return;
  480. }
  481. /* set up local variables */
  482. UCaseContext csc=UCASECONTEXT_INITIALIZER;
  483. csc.p=(void *)src;
  484. csc.limit=srcLength;
  485. int32_t prev=0;
  486. UBool isFirstIndex=true;
  487. /* titlecasing loop */
  488. while(prev<srcLength) {
  489. /* find next index where to titlecase */
  490. int32_t index;
  491. if(isFirstIndex) {
  492. isFirstIndex=false;
  493. index=iter->first();
  494. } else {
  495. index=iter->next();
  496. }
  497. if(index==UBRK_DONE || index>srcLength) {
  498. index=srcLength;
  499. }
  500. /*
  501. * Segment [prev..index[ into 3 parts:
  502. * a) skipped characters (copy as-is) [prev..titleStart[
  503. * b) first letter (titlecase) [titleStart..titleLimit[
  504. * c) subsequent characters (lowercase) [titleLimit..index[
  505. */
  506. if(prev<index) {
  507. /* find and copy skipped characters [prev..titleStart[ */
  508. int32_t titleStart=prev;
  509. int32_t titleLimit=prev;
  510. UChar32 c;
  511. U8_NEXT(src, titleLimit, index, c);
  512. if ((options&U_TITLECASE_NO_BREAK_ADJUSTMENT)==0) {
  513. // Adjust the titlecasing index to the next cased character,
  514. // or to the next letter/number/symbol/private use.
  515. // Stop with titleStart<titleLimit<=index
  516. // if there is a character to be titlecased,
  517. // or else stop with titleStart==titleLimit==index.
  518. UBool toCased = (options&U_TITLECASE_ADJUST_TO_CASED) != 0;
  519. while (toCased ? UCASE_NONE==ucase_getType(c) : !ustrcase_isLNS(c)) {
  520. titleStart=titleLimit;
  521. if(titleLimit==index) {
  522. break;
  523. }
  524. U8_NEXT(src, titleLimit, index, c);
  525. }
  526. if (prev < titleStart) {
  527. if (!ByteSinkUtil::appendUnchanged(src+prev, titleStart-prev,
  528. sink, options, edits, errorCode)) {
  529. return;
  530. }
  531. }
  532. }
  533. if(titleStart<titleLimit) {
  534. /* titlecase c which is from [titleStart..titleLimit[ */
  535. if(c>=0) {
  536. csc.cpStart=titleStart;
  537. csc.cpLimit=titleLimit;
  538. const char16_t *s;
  539. c=ucase_toFullTitle(c, utf8_caseContextIterator, &csc, &s, caseLocale);
  540. if (!appendResult(titleLimit-titleStart, c, s, sink, options, edits, errorCode)) {
  541. return;
  542. }
  543. } else {
  544. // Malformed UTF-8.
  545. if (!ByteSinkUtil::appendUnchanged(src+titleStart, titleLimit-titleStart,
  546. sink, options, edits, errorCode)) {
  547. return;
  548. }
  549. }
  550. /* Special case Dutch IJ titlecasing */
  551. if (titleLimit < index &&
  552. caseLocale == UCASE_LOC_DUTCH) {
  553. if (c < 0) {
  554. c = ~c;
  555. }
  556. if (c == u'I' || c == u'Í') {
  557. titleLimit = maybeTitleDutchIJ(src, c, titleLimit, index, sink, options, edits, errorCode);
  558. }
  559. }
  560. /* lowercase [titleLimit..index[ */
  561. if(titleLimit<index) {
  562. if((options&U_TITLECASE_NO_LOWERCASE)==0) {
  563. /* Normal operation: Lowercase the rest of the word. */
  564. toLower(caseLocale, options,
  565. src, &csc, titleLimit, index,
  566. sink, edits, errorCode);
  567. if(U_FAILURE(errorCode)) {
  568. return;
  569. }
  570. } else {
  571. /* Optionally just copy the rest of the word unchanged. */
  572. if (!ByteSinkUtil::appendUnchanged(src+titleLimit, index-titleLimit,
  573. sink, options, edits, errorCode)) {
  574. return;
  575. }
  576. }
  577. }
  578. }
  579. }
  580. prev=index;
  581. }
  582. }
  583. #endif
  584. U_NAMESPACE_BEGIN
  585. namespace GreekUpper {
  586. UBool isFollowedByCasedLetter(const uint8_t *s, int32_t i, int32_t length) {
  587. while (i < length) {
  588. UChar32 c;
  589. U8_NEXT(s, i, length, c);
  590. int32_t type = ucase_getTypeOrIgnorable(c);
  591. if ((type & UCASE_IGNORABLE) != 0) {
  592. // Case-ignorable, continue with the loop.
  593. } else if (type != UCASE_NONE) {
  594. return true; // Followed by cased letter.
  595. } else {
  596. return false; // Uncased and not case-ignorable.
  597. }
  598. }
  599. return false; // Not followed by cased letter.
  600. }
  601. // Keep this consistent with the UTF-16 version in ustrcase.cpp and the Java version in CaseMap.java.
  602. void toUpper(uint32_t options,
  603. const uint8_t *src, int32_t srcLength,
  604. ByteSink &sink, Edits *edits,
  605. UErrorCode &errorCode) {
  606. uint32_t state = 0;
  607. for (int32_t i = 0; i < srcLength;) {
  608. int32_t nextIndex = i;
  609. UChar32 c;
  610. U8_NEXT(src, nextIndex, srcLength, c);
  611. uint32_t nextState = 0;
  612. int32_t type = ucase_getTypeOrIgnorable(c);
  613. if ((type & UCASE_IGNORABLE) != 0) {
  614. // c is case-ignorable
  615. nextState |= (state & AFTER_CASED);
  616. } else if (type != UCASE_NONE) {
  617. // c is cased
  618. nextState |= AFTER_CASED;
  619. }
  620. uint32_t data = getLetterData(c);
  621. if (data > 0) {
  622. uint32_t upper = data & UPPER_MASK;
  623. // Add a dialytika to this iota or ypsilon vowel
  624. // if we removed a tonos from the previous vowel,
  625. // and that previous vowel did not also have (or gain) a dialytika.
  626. // Adding one only to the final vowel in a longer sequence
  627. // (which does not occur in normal writing) would require lookahead.
  628. // Set the same flag as for preserving an existing dialytika.
  629. if ((data & HAS_VOWEL) != 0 &&
  630. (state & (AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT | AFTER_VOWEL_WITH_COMBINING_ACCENT)) !=
  631. 0 &&
  632. (upper == 0x399 || upper == 0x3A5)) {
  633. data |= (state & AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT) != 0 ? HAS_DIALYTIKA
  634. : HAS_COMBINING_DIALYTIKA;
  635. }
  636. int32_t numYpogegrammeni = 0; // Map each one to a trailing, spacing, capital iota.
  637. if ((data & HAS_YPOGEGRAMMENI) != 0) {
  638. numYpogegrammeni = 1;
  639. }
  640. const UBool hasPrecomposedAccent = (data & HAS_ACCENT) != 0;
  641. // Skip combining diacritics after this Greek letter.
  642. int32_t nextNextIndex = nextIndex;
  643. while (nextIndex < srcLength) {
  644. UChar32 c2;
  645. U8_NEXT(src, nextNextIndex, srcLength, c2);
  646. uint32_t diacriticData = getDiacriticData(c2);
  647. if (diacriticData != 0) {
  648. data |= diacriticData;
  649. if ((diacriticData & HAS_YPOGEGRAMMENI) != 0) {
  650. ++numYpogegrammeni;
  651. }
  652. nextIndex = nextNextIndex;
  653. } else {
  654. break; // not a Greek diacritic
  655. }
  656. }
  657. if ((data & HAS_VOWEL_AND_ACCENT_AND_DIALYTIKA) == HAS_VOWEL_AND_ACCENT) {
  658. nextState |= hasPrecomposedAccent ? AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT
  659. : AFTER_VOWEL_WITH_COMBINING_ACCENT;
  660. }
  661. // Map according to Greek rules.
  662. UBool addTonos = false;
  663. if (upper == 0x397 &&
  664. (data & HAS_ACCENT) != 0 &&
  665. numYpogegrammeni == 0 &&
  666. (state & AFTER_CASED) == 0 &&
  667. !isFollowedByCasedLetter(src, nextIndex, srcLength)) {
  668. // Keep disjunctive "or" with (only) a tonos.
  669. // We use the same "word boundary" conditions as for the Final_Sigma test.
  670. if (hasPrecomposedAccent) {
  671. upper = 0x389; // Preserve the precomposed form.
  672. } else {
  673. addTonos = true;
  674. }
  675. } else if ((data & HAS_DIALYTIKA) != 0) {
  676. // Preserve a vowel with dialytika in precomposed form if it exists.
  677. if (upper == 0x399) {
  678. upper = 0x3AA;
  679. data &= ~HAS_EITHER_DIALYTIKA;
  680. } else if (upper == 0x3A5) {
  681. upper = 0x3AB;
  682. data &= ~HAS_EITHER_DIALYTIKA;
  683. }
  684. }
  685. UBool change;
  686. if (edits == nullptr && (options & U_OMIT_UNCHANGED_TEXT) == 0) {
  687. change = true; // common, simple usage
  688. } else {
  689. // Find out first whether we are changing the text.
  690. U_ASSERT(0x370 <= upper && upper <= 0x3ff); // 2-byte UTF-8, main Greek block
  691. change = (i + 2) > nextIndex ||
  692. src[i] != getTwoByteLead(upper) || src[i + 1] != getTwoByteTrail(upper) ||
  693. numYpogegrammeni > 0;
  694. int32_t i2 = i + 2;
  695. if ((data & HAS_EITHER_DIALYTIKA) != 0) {
  696. change |= (i2 + 2) > nextIndex ||
  697. src[i2] != static_cast<uint8_t>(u8"\u0308"[0]) ||
  698. src[i2 + 1] != static_cast<uint8_t>(u8"\u0308"[1]);
  699. i2 += 2;
  700. }
  701. if (addTonos) {
  702. change |= (i2 + 2) > nextIndex ||
  703. src[i2] != static_cast<uint8_t>(u8"\u0301"[0]) ||
  704. src[i2 + 1] != static_cast<uint8_t>(u8"\u0301"[1]);
  705. i2 += 2;
  706. }
  707. int32_t oldLength = nextIndex - i;
  708. int32_t newLength = (i2 - i) + numYpogegrammeni * 2; // 2 bytes per U+0399
  709. change |= oldLength != newLength;
  710. if (change) {
  711. if (edits != nullptr) {
  712. edits->addReplace(oldLength, newLength);
  713. }
  714. } else {
  715. if (edits != nullptr) {
  716. edits->addUnchanged(oldLength);
  717. }
  718. // Write unchanged text?
  719. change = (options & U_OMIT_UNCHANGED_TEXT) == 0;
  720. }
  721. }
  722. if (change) {
  723. ByteSinkUtil::appendTwoBytes(upper, sink);
  724. if ((data & HAS_EITHER_DIALYTIKA) != 0) {
  725. sink.AppendU8(u8"\u0308", 2); // restore or add a dialytika
  726. }
  727. if (addTonos) {
  728. sink.AppendU8(u8"\u0301", 2);
  729. }
  730. while (numYpogegrammeni > 0) {
  731. sink.AppendU8(u8"\u0399", 2);
  732. --numYpogegrammeni;
  733. }
  734. }
  735. } else if(c>=0) {
  736. const char16_t *s;
  737. c=ucase_toFullUpper(c, nullptr, nullptr, &s, UCASE_LOC_GREEK);
  738. if (!appendResult(nextIndex - i, c, s, sink, options, edits, errorCode)) {
  739. return;
  740. }
  741. } else {
  742. // Malformed UTF-8.
  743. if (!ByteSinkUtil::appendUnchanged(src+i, nextIndex-i,
  744. sink, options, edits, errorCode)) {
  745. return;
  746. }
  747. }
  748. i = nextIndex;
  749. state = nextState;
  750. }
  751. }
  752. } // namespace GreekUpper
  753. U_NAMESPACE_END
  754. static void U_CALLCONV
  755. ucasemap_internalUTF8ToLower(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_UNUSED
  756. const uint8_t *src, int32_t srcLength,
  757. icu::ByteSink &sink, icu::Edits *edits,
  758. UErrorCode &errorCode) {
  759. UCaseContext csc=UCASECONTEXT_INITIALIZER;
  760. csc.p=(void *)src;
  761. csc.limit=srcLength;
  762. toLower(
  763. caseLocale, options,
  764. src, &csc, 0, srcLength,
  765. sink, edits, errorCode);
  766. }
  767. static void U_CALLCONV
  768. ucasemap_internalUTF8ToUpper(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_UNUSED
  769. const uint8_t *src, int32_t srcLength,
  770. icu::ByteSink &sink, icu::Edits *edits,
  771. UErrorCode &errorCode) {
  772. if (caseLocale == UCASE_LOC_GREEK) {
  773. GreekUpper::toUpper(options, src, srcLength, sink, edits, errorCode);
  774. } else {
  775. UCaseContext csc=UCASECONTEXT_INITIALIZER;
  776. csc.p=(void *)src;
  777. csc.limit=srcLength;
  778. toUpper(
  779. caseLocale, options,
  780. src, &csc, srcLength,
  781. sink, edits, errorCode);
  782. }
  783. }
  784. static void U_CALLCONV
  785. ucasemap_internalUTF8Fold(int32_t /* caseLocale */, uint32_t options, UCASEMAP_BREAK_ITERATOR_UNUSED
  786. const uint8_t *src, int32_t srcLength,
  787. icu::ByteSink &sink, icu::Edits *edits,
  788. UErrorCode &errorCode) {
  789. toLower(
  790. -1, options,
  791. src, nullptr, 0, srcLength,
  792. sink, edits, errorCode);
  793. }
  794. void
  795. ucasemap_mapUTF8(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  796. const char *src, int32_t srcLength,
  797. UTF8CaseMapper *stringCaseMapper,
  798. icu::ByteSink &sink, icu::Edits *edits,
  799. UErrorCode &errorCode) {
  800. /* check argument values */
  801. if (U_FAILURE(errorCode)) {
  802. return;
  803. }
  804. if ((src == nullptr && srcLength != 0) || srcLength < -1) {
  805. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  806. return;
  807. }
  808. // Get the string length.
  809. if (srcLength == -1) {
  810. srcLength = static_cast<int32_t>(uprv_strlen(src));
  811. }
  812. if (edits != nullptr && (options & U_EDITS_NO_RESET) == 0) {
  813. edits->reset();
  814. }
  815. stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR
  816. reinterpret_cast<const uint8_t*>(src), srcLength, sink, edits, errorCode);
  817. sink.Flush();
  818. if (U_SUCCESS(errorCode)) {
  819. if (edits != nullptr) {
  820. edits->copyErrorTo(errorCode);
  821. }
  822. }
  823. }
  824. int32_t
  825. ucasemap_mapUTF8(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
  826. char *dest, int32_t destCapacity,
  827. const char *src, int32_t srcLength,
  828. UTF8CaseMapper *stringCaseMapper,
  829. icu::Edits *edits,
  830. UErrorCode &errorCode) {
  831. /* check argument values */
  832. if(U_FAILURE(errorCode)) {
  833. return 0;
  834. }
  835. if( destCapacity<0 ||
  836. (dest==nullptr && destCapacity>0) ||
  837. (src==nullptr && srcLength!=0) || srcLength<-1
  838. ) {
  839. errorCode=U_ILLEGAL_ARGUMENT_ERROR;
  840. return 0;
  841. }
  842. /* get the string length */
  843. if(srcLength==-1) {
  844. srcLength = static_cast<int32_t>(uprv_strlen(src));
  845. }
  846. /* check for overlapping source and destination */
  847. if( dest!=nullptr &&
  848. ((src>=dest && src<(dest+destCapacity)) ||
  849. (dest>=src && dest<(src+srcLength)))
  850. ) {
  851. errorCode=U_ILLEGAL_ARGUMENT_ERROR;
  852. return 0;
  853. }
  854. if (edits != nullptr && (options & U_EDITS_NO_RESET) == 0) {
  855. edits->reset();
  856. }
  857. int32_t reslen = ByteSinkUtil::viaByteSinkToTerminatedChars(
  858. dest, destCapacity,
  859. [&](ByteSink& sink, UErrorCode& status) {
  860. stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR
  861. reinterpret_cast<const uint8_t*>(src), srcLength, sink, edits, status);
  862. },
  863. errorCode);
  864. if (U_SUCCESS(errorCode) && edits != nullptr) {
  865. edits->copyErrorTo(errorCode);
  866. }
  867. return reslen;
  868. }
  869. /* public API functions */
  870. U_CAPI int32_t U_EXPORT2
  871. ucasemap_utf8ToLower(const UCaseMap *csm,
  872. char *dest, int32_t destCapacity,
  873. const char *src, int32_t srcLength,
  874. UErrorCode *pErrorCode) {
  875. return ucasemap_mapUTF8(
  876. csm->caseLocale, csm->options, UCASEMAP_BREAK_ITERATOR_NULL
  877. dest, destCapacity,
  878. src, srcLength,
  879. ucasemap_internalUTF8ToLower, nullptr, *pErrorCode);
  880. }
  881. U_CAPI int32_t U_EXPORT2
  882. ucasemap_utf8ToUpper(const UCaseMap *csm,
  883. char *dest, int32_t destCapacity,
  884. const char *src, int32_t srcLength,
  885. UErrorCode *pErrorCode) {
  886. return ucasemap_mapUTF8(
  887. csm->caseLocale, csm->options, UCASEMAP_BREAK_ITERATOR_NULL
  888. dest, destCapacity,
  889. src, srcLength,
  890. ucasemap_internalUTF8ToUpper, nullptr, *pErrorCode);
  891. }
  892. U_CAPI int32_t U_EXPORT2
  893. ucasemap_utf8FoldCase(const UCaseMap *csm,
  894. char *dest, int32_t destCapacity,
  895. const char *src, int32_t srcLength,
  896. UErrorCode *pErrorCode) {
  897. return ucasemap_mapUTF8(
  898. UCASE_LOC_ROOT, csm->options, UCASEMAP_BREAK_ITERATOR_NULL
  899. dest, destCapacity,
  900. src, srcLength,
  901. ucasemap_internalUTF8Fold, nullptr, *pErrorCode);
  902. }
  903. U_NAMESPACE_BEGIN
  904. void CaseMap::utf8ToLower(
  905. const char *locale, uint32_t options,
  906. StringPiece src, ByteSink &sink, Edits *edits,
  907. UErrorCode &errorCode) {
  908. ucasemap_mapUTF8(
  909. ustrcase_getCaseLocale(locale), options, UCASEMAP_BREAK_ITERATOR_NULL
  910. src.data(), src.length(),
  911. ucasemap_internalUTF8ToLower, sink, edits, errorCode);
  912. }
  913. void CaseMap::utf8ToUpper(
  914. const char *locale, uint32_t options,
  915. StringPiece src, ByteSink &sink, Edits *edits,
  916. UErrorCode &errorCode) {
  917. ucasemap_mapUTF8(
  918. ustrcase_getCaseLocale(locale), options, UCASEMAP_BREAK_ITERATOR_NULL
  919. src.data(), src.length(),
  920. ucasemap_internalUTF8ToUpper, sink, edits, errorCode);
  921. }
  922. void CaseMap::utf8Fold(
  923. uint32_t options,
  924. StringPiece src, ByteSink &sink, Edits *edits,
  925. UErrorCode &errorCode) {
  926. ucasemap_mapUTF8(
  927. UCASE_LOC_ROOT, options, UCASEMAP_BREAK_ITERATOR_NULL
  928. src.data(), src.length(),
  929. ucasemap_internalUTF8Fold, sink, edits, errorCode);
  930. }
  931. int32_t CaseMap::utf8ToLower(
  932. const char *locale, uint32_t options,
  933. const char *src, int32_t srcLength,
  934. char *dest, int32_t destCapacity, Edits *edits,
  935. UErrorCode &errorCode) {
  936. return ucasemap_mapUTF8(
  937. ustrcase_getCaseLocale(locale), options, UCASEMAP_BREAK_ITERATOR_NULL
  938. dest, destCapacity,
  939. src, srcLength,
  940. ucasemap_internalUTF8ToLower, edits, errorCode);
  941. }
  942. int32_t CaseMap::utf8ToUpper(
  943. const char *locale, uint32_t options,
  944. const char *src, int32_t srcLength,
  945. char *dest, int32_t destCapacity, Edits *edits,
  946. UErrorCode &errorCode) {
  947. return ucasemap_mapUTF8(
  948. ustrcase_getCaseLocale(locale), options, UCASEMAP_BREAK_ITERATOR_NULL
  949. dest, destCapacity,
  950. src, srcLength,
  951. ucasemap_internalUTF8ToUpper, edits, errorCode);
  952. }
  953. int32_t CaseMap::utf8Fold(
  954. uint32_t options,
  955. const char *src, int32_t srcLength,
  956. char *dest, int32_t destCapacity, Edits *edits,
  957. UErrorCode &errorCode) {
  958. return ucasemap_mapUTF8(
  959. UCASE_LOC_ROOT, options, UCASEMAP_BREAK_ITERATOR_NULL
  960. dest, destCapacity,
  961. src, srcLength,
  962. ucasemap_internalUTF8Fold, edits, errorCode);
  963. }
  964. U_NAMESPACE_END