ucase.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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) 2004-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: ucase.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2004aug30
  16. * created by: Markus W. Scherer
  17. *
  18. * Low-level Unicode character/string case mapping code.
  19. * Much code moved here (and modified) from uchar.c.
  20. */
  21. #include "unicode/utypes.h"
  22. #include "unicode/unistr.h"
  23. #include "unicode/uset.h"
  24. #include "unicode/utf16.h"
  25. #include "cmemory.h"
  26. #include "uassert.h"
  27. #include "ucase.h"
  28. #include "umutex.h"
  29. #include "utrie2.h"
  30. /* ucase_props_data.h is machine-generated by genprops/casepropsbuilder.cpp */
  31. #define INCLUDED_FROM_UCASE_CPP
  32. #include "ucase_props_data.h"
  33. /* set of property starts for UnicodeSet ------------------------------------ */
  34. static UBool U_CALLCONV
  35. _enumPropertyStartsRange(const void *context, UChar32 start, UChar32 /*end*/, uint32_t /*value*/) {
  36. /* add the start code point to the USet */
  37. const USetAdder* sa = static_cast<const USetAdder*>(context);
  38. sa->add(sa->set, start);
  39. return true;
  40. }
  41. U_CFUNC void U_EXPORT2
  42. ucase_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
  43. if(U_FAILURE(*pErrorCode)) {
  44. return;
  45. }
  46. /* add the start code point of each same-value range of the trie */
  47. utrie2_enum(&ucase_props_singleton.trie, nullptr, _enumPropertyStartsRange, sa);
  48. /* add code points with hardcoded properties, plus the ones following them */
  49. /* (none right now, see comment below) */
  50. /*
  51. * Omit code points with hardcoded specialcasing properties
  52. * because we do not build property UnicodeSets for them right now.
  53. */
  54. }
  55. /* data access primitives --------------------------------------------------- */
  56. U_CAPI const struct UCaseProps * U_EXPORT2
  57. ucase_getSingleton(int32_t *pExceptionsLength, int32_t *pUnfoldLength) {
  58. *pExceptionsLength = UPRV_LENGTHOF(ucase_props_exceptions);
  59. *pUnfoldLength = UPRV_LENGTHOF(ucase_props_unfold);
  60. return &ucase_props_singleton;
  61. }
  62. U_CFUNC const UTrie2 * U_EXPORT2
  63. ucase_getTrie() {
  64. return &ucase_props_singleton.trie;
  65. }
  66. #define GET_EXCEPTIONS(csp, props) ((csp)->exceptions+((props)>>UCASE_EXC_SHIFT))
  67. /* number of bits in an 8-bit integer value */
  68. static const uint8_t flagsOffset[256]={
  69. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  70. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  71. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  72. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  73. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  74. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  75. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  76. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  77. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  78. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  79. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  80. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  81. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  82. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  83. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  84. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  85. };
  86. #define HAS_SLOT(flags, idx) ((flags)&(1<<(idx)))
  87. #define SLOT_OFFSET(flags, idx) flagsOffset[(flags)&((1<<(idx))-1)]
  88. /*
  89. * Get the value of an optional-value slot where HAS_SLOT(excWord, idx).
  90. *
  91. * @param excWord (in) initial exceptions word
  92. * @param idx (in) desired slot index
  93. * @param pExc16 (in/out) const uint16_t * after excWord=*pExc16++;
  94. * moved to the last uint16_t of the value, use +1 for beginning of next slot
  95. * @param value (out) int32_t or uint32_t output if hasSlot, otherwise not modified
  96. */
  97. #define GET_SLOT_VALUE(excWord, idx, pExc16, value) UPRV_BLOCK_MACRO_BEGIN { \
  98. if(((excWord)&UCASE_EXC_DOUBLE_SLOTS)==0) { \
  99. (pExc16)+=SLOT_OFFSET(excWord, idx); \
  100. (value)=*pExc16; \
  101. } else { \
  102. (pExc16)+=2*SLOT_OFFSET(excWord, idx); \
  103. (value)=*pExc16++; \
  104. (value)=((value)<<16)|*pExc16; \
  105. } \
  106. } UPRV_BLOCK_MACRO_END
  107. /* simple case mappings ----------------------------------------------------- */
  108. U_CAPI UChar32 U_EXPORT2
  109. ucase_tolower(UChar32 c) {
  110. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  111. if(!UCASE_HAS_EXCEPTION(props)) {
  112. if(UCASE_IS_UPPER_OR_TITLE(props)) {
  113. c+=UCASE_GET_DELTA(props);
  114. }
  115. } else {
  116. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  117. uint16_t excWord=*pe++;
  118. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
  119. int32_t delta;
  120. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
  121. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  122. }
  123. if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
  124. GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe, c);
  125. }
  126. }
  127. return c;
  128. }
  129. U_CAPI UChar32 U_EXPORT2
  130. ucase_toupper(UChar32 c) {
  131. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  132. if(!UCASE_HAS_EXCEPTION(props)) {
  133. if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
  134. c+=UCASE_GET_DELTA(props);
  135. }
  136. } else {
  137. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  138. uint16_t excWord=*pe++;
  139. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_GET_TYPE(props)==UCASE_LOWER) {
  140. int32_t delta;
  141. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
  142. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  143. }
  144. if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
  145. GET_SLOT_VALUE(excWord, UCASE_EXC_UPPER, pe, c);
  146. }
  147. }
  148. return c;
  149. }
  150. U_CAPI UChar32 U_EXPORT2
  151. ucase_totitle(UChar32 c) {
  152. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  153. if(!UCASE_HAS_EXCEPTION(props)) {
  154. if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
  155. c+=UCASE_GET_DELTA(props);
  156. }
  157. } else {
  158. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  159. uint16_t excWord=*pe++;
  160. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_GET_TYPE(props)==UCASE_LOWER) {
  161. int32_t delta;
  162. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
  163. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  164. }
  165. int32_t idx;
  166. if(HAS_SLOT(excWord, UCASE_EXC_TITLE)) {
  167. idx=UCASE_EXC_TITLE;
  168. } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
  169. idx=UCASE_EXC_UPPER;
  170. } else {
  171. return c;
  172. }
  173. GET_SLOT_VALUE(excWord, idx, pe, c);
  174. }
  175. return c;
  176. }
  177. static const char16_t iDot[2] = { 0x69, 0x307 };
  178. static const char16_t jDot[2] = { 0x6a, 0x307 };
  179. static const char16_t iOgonekDot[3] = { 0x12f, 0x307 };
  180. static const char16_t iDotGrave[3] = { 0x69, 0x307, 0x300 };
  181. static const char16_t iDotAcute[3] = { 0x69, 0x307, 0x301 };
  182. static const char16_t iDotTilde[3] = { 0x69, 0x307, 0x303 };
  183. U_CFUNC void U_EXPORT2
  184. ucase_addCaseClosure(UChar32 c, const USetAdder *sa) {
  185. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  186. if(!UCASE_HAS_EXCEPTION(props)) {
  187. if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
  188. /* add the one simple case mapping, no matter what type it is */
  189. int32_t delta=UCASE_GET_DELTA(props);
  190. if(delta!=0) {
  191. sa->add(sa->set, c+delta);
  192. }
  193. }
  194. } else {
  195. /*
  196. * c has exceptions, so there may be multiple simple and/or
  197. * full case mappings. Add them all.
  198. */
  199. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  200. uint16_t excWord=*pe++;
  201. const uint16_t *pe0=pe;
  202. // Hardcode the case closure of i and its relatives and ignore the
  203. // data file data for these characters.
  204. // The Turkic dotless i and dotted I with their case mapping conditions
  205. // and case folding option make the related characters behave specially.
  206. // This code matches their closure behavior to their case folding behavior.
  207. if (excWord&UCASE_EXC_CONDITIONAL_FOLD) {
  208. // These characters have Turkic case foldings. Hardcode their closure.
  209. if (c == 0x49) {
  210. // Regular i and I are in one equivalence class.
  211. sa->add(sa->set, 0x69);
  212. return;
  213. } else if (c == 0x130) {
  214. // Dotted I is in a class with <0069 0307>
  215. // (for canonical equivalence with <0049 0307>).
  216. sa->addString(sa->set, iDot, 2);
  217. return;
  218. }
  219. } else if (c == 0x69) {
  220. sa->add(sa->set, 0x49);
  221. return;
  222. } else if (c == 0x131) {
  223. // Dotless i is in a class by itself.
  224. return;
  225. }
  226. /* add all simple case mappings */
  227. for(int32_t idx=UCASE_EXC_LOWER; idx<=UCASE_EXC_TITLE; ++idx) {
  228. if(HAS_SLOT(excWord, idx)) {
  229. pe=pe0;
  230. UChar32 mapping;
  231. GET_SLOT_VALUE(excWord, idx, pe, mapping);
  232. sa->add(sa->set, mapping);
  233. }
  234. }
  235. if(HAS_SLOT(excWord, UCASE_EXC_DELTA)) {
  236. pe=pe0;
  237. int32_t delta;
  238. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
  239. sa->add(sa->set, (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta);
  240. }
  241. /* get the closure string pointer & length */
  242. const char16_t *closure;
  243. int32_t closureLength;
  244. if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) {
  245. pe=pe0;
  246. GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength);
  247. closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */
  248. closure=(const char16_t *)pe+1; /* behind this slot, unless there are full case mappings */
  249. } else {
  250. closureLength=0;
  251. closure=nullptr;
  252. }
  253. /* add the full case folding */
  254. if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
  255. pe=pe0;
  256. int32_t fullLength;
  257. GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength);
  258. /* start of full case mapping strings */
  259. ++pe;
  260. fullLength&=0xffff; /* bits 16 and higher are reserved */
  261. /* skip the lowercase result string */
  262. pe+=fullLength&UCASE_FULL_LOWER;
  263. fullLength>>=4;
  264. /* add the full case folding string */
  265. int32_t length=fullLength&0xf;
  266. if(length!=0) {
  267. sa->addString(sa->set, (const char16_t *)pe, length);
  268. pe+=length;
  269. }
  270. /* skip the uppercase and titlecase strings */
  271. fullLength>>=4;
  272. pe+=fullLength&0xf;
  273. fullLength>>=4;
  274. pe+=fullLength;
  275. closure=(const char16_t *)pe; /* behind full case mappings */
  276. }
  277. /* add each code point in the closure string */
  278. for(int32_t idx=0; idx<closureLength;) {
  279. UChar32 mapping;
  280. U16_NEXT_UNSAFE(closure, idx, mapping);
  281. sa->add(sa->set, mapping);
  282. }
  283. }
  284. }
  285. U_CFUNC void U_EXPORT2
  286. ucase_addSimpleCaseClosure(UChar32 c, const USetAdder *sa) {
  287. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  288. if(!UCASE_HAS_EXCEPTION(props)) {
  289. if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
  290. /* add the one simple case mapping, no matter what type it is */
  291. int32_t delta=UCASE_GET_DELTA(props);
  292. if(delta!=0) {
  293. sa->add(sa->set, c+delta);
  294. }
  295. }
  296. } else {
  297. // c has exceptions. Add the mappings relevant for scf=Simple_Case_Folding.
  298. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  299. uint16_t excWord=*pe++;
  300. const uint16_t *pe0=pe;
  301. // Hardcode the case closure of i and its relatives and ignore the
  302. // data file data for these characters, like in ucase_addCaseClosure().
  303. if (excWord&UCASE_EXC_CONDITIONAL_FOLD) {
  304. // These characters have Turkic case foldings. Hardcode their closure.
  305. if (c == 0x49) {
  306. // Regular i and I are in one equivalence class.
  307. sa->add(sa->set, 0x69);
  308. return;
  309. } else if (c == 0x130) {
  310. // For scf=Simple_Case_Folding, dotted I is in a class by itself.
  311. return;
  312. }
  313. } else if (c == 0x69) {
  314. sa->add(sa->set, 0x49);
  315. return;
  316. } else if (c == 0x131) {
  317. // Dotless i is in a class by itself.
  318. return;
  319. }
  320. // Add all simple case mappings.
  321. for(int32_t idx=UCASE_EXC_LOWER; idx<=UCASE_EXC_TITLE; ++idx) {
  322. if(HAS_SLOT(excWord, idx)) {
  323. pe=pe0;
  324. UChar32 mapping;
  325. GET_SLOT_VALUE(excWord, idx, pe, mapping);
  326. sa->add(sa->set, mapping);
  327. }
  328. }
  329. if(HAS_SLOT(excWord, UCASE_EXC_DELTA)) {
  330. pe=pe0;
  331. int32_t delta;
  332. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
  333. UChar32 mapping = (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  334. sa->add(sa->set, mapping);
  335. }
  336. /* get the closure string pointer & length */
  337. const char16_t *closure;
  338. int32_t closureLength;
  339. if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) {
  340. pe=pe0;
  341. GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength);
  342. closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */
  343. closure=(const char16_t *)pe+1; /* behind this slot, unless there are full case mappings */
  344. } else {
  345. closureLength=0;
  346. closure=nullptr;
  347. }
  348. // Skip the full case mappings.
  349. if(closureLength > 0 && HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
  350. pe=pe0;
  351. int32_t fullLength;
  352. GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength);
  353. /* start of full case mapping strings */
  354. ++pe;
  355. fullLength&=0xffff; /* bits 16 and higher are reserved */
  356. // Skip all 4 full case mappings.
  357. pe+=fullLength&UCASE_FULL_LOWER;
  358. fullLength>>=4;
  359. pe+=fullLength&0xf;
  360. fullLength>>=4;
  361. pe+=fullLength&0xf;
  362. fullLength>>=4;
  363. pe+=fullLength;
  364. closure=(const char16_t *)pe; /* behind full case mappings */
  365. }
  366. // Add each code point in the closure string whose scf maps back to c.
  367. for(int32_t idx=0; idx<closureLength;) {
  368. UChar32 mapping;
  369. U16_NEXT_UNSAFE(closure, idx, mapping);
  370. sa->add(sa->set, mapping);
  371. }
  372. }
  373. }
  374. /*
  375. * compare s, which has a length, with t, which has a maximum length or is NUL-terminated
  376. * must be length>0 and max>0 and length<=max
  377. */
  378. static inline int32_t
  379. strcmpMax(const char16_t *s, int32_t length, const char16_t *t, int32_t max) {
  380. int32_t c1, c2;
  381. max-=length; /* we require length<=max, so no need to decrement max in the loop */
  382. do {
  383. c1=*s++;
  384. c2=*t++;
  385. if(c2==0) {
  386. return 1; /* reached the end of t but not of s */
  387. }
  388. c1-=c2;
  389. if(c1!=0) {
  390. return c1; /* return difference result */
  391. }
  392. } while(--length>0);
  393. /* ends with length==0 */
  394. if(max==0 || *t==0) {
  395. return 0; /* equal to length of both strings */
  396. } else {
  397. return -max; /* return length difference */
  398. }
  399. }
  400. U_CFUNC UBool U_EXPORT2
  401. ucase_addStringCaseClosure(const char16_t *s, int32_t length, const USetAdder *sa) {
  402. int32_t i, start, limit, result, unfoldRows, unfoldRowWidth, unfoldStringWidth;
  403. if(ucase_props_singleton.unfold==nullptr || s==nullptr) {
  404. return false; /* no reverse case folding data, or no string */
  405. }
  406. if(length<=1) {
  407. /* the string is too short to find any match */
  408. /*
  409. * more precise would be:
  410. * if(!u_strHasMoreChar32Than(s, length, 1))
  411. * but this does not make much practical difference because
  412. * a single supplementary code point would just not be found
  413. */
  414. return false;
  415. }
  416. const uint16_t *unfold=ucase_props_singleton.unfold;
  417. unfoldRows=unfold[UCASE_UNFOLD_ROWS];
  418. unfoldRowWidth=unfold[UCASE_UNFOLD_ROW_WIDTH];
  419. unfoldStringWidth=unfold[UCASE_UNFOLD_STRING_WIDTH];
  420. unfold+=unfoldRowWidth;
  421. if(length>unfoldStringWidth) {
  422. /* the string is too long to find any match */
  423. return false;
  424. }
  425. /* do a binary search for the string */
  426. start=0;
  427. limit=unfoldRows;
  428. while(start<limit) {
  429. i=(start+limit)/2;
  430. const char16_t *p=reinterpret_cast<const char16_t *>(unfold+(i*unfoldRowWidth));
  431. result=strcmpMax(s, length, p, unfoldStringWidth);
  432. if(result==0) {
  433. /* found the string: add each code point, and its case closure */
  434. UChar32 c;
  435. for(i=unfoldStringWidth; i<unfoldRowWidth && p[i]!=0;) {
  436. U16_NEXT_UNSAFE(p, i, c);
  437. sa->add(sa->set, c);
  438. ucase_addCaseClosure(c, sa);
  439. }
  440. return true;
  441. } else if(result<0) {
  442. limit=i;
  443. } else /* result>0 */ {
  444. start=i+1;
  445. }
  446. }
  447. return false; /* string not found */
  448. }
  449. U_NAMESPACE_BEGIN
  450. FullCaseFoldingIterator::FullCaseFoldingIterator()
  451. : unfold(reinterpret_cast<const char16_t *>(ucase_props_singleton.unfold)),
  452. unfoldRows(unfold[UCASE_UNFOLD_ROWS]),
  453. unfoldRowWidth(unfold[UCASE_UNFOLD_ROW_WIDTH]),
  454. unfoldStringWidth(unfold[UCASE_UNFOLD_STRING_WIDTH]),
  455. currentRow(0),
  456. rowCpIndex(unfoldStringWidth) {
  457. unfold+=unfoldRowWidth;
  458. }
  459. UChar32
  460. FullCaseFoldingIterator::next(UnicodeString &full) {
  461. // Advance past the last-delivered code point.
  462. const char16_t *p=unfold+(currentRow*unfoldRowWidth);
  463. if(rowCpIndex>=unfoldRowWidth || p[rowCpIndex]==0) {
  464. ++currentRow;
  465. p+=unfoldRowWidth;
  466. rowCpIndex=unfoldStringWidth;
  467. }
  468. if(currentRow>=unfoldRows) { return U_SENTINEL; }
  469. // Set "full" to the NUL-terminated string in the first unfold column.
  470. int32_t length=unfoldStringWidth;
  471. while(length>0 && p[length-1]==0) { --length; }
  472. full.setTo(false, p, length);
  473. // Return the code point.
  474. UChar32 c;
  475. U16_NEXT_UNSAFE(p, rowCpIndex, c);
  476. return c;
  477. }
  478. namespace LatinCase {
  479. const int8_t TO_LOWER_NORMAL[LIMIT] = {
  480. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  481. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  482. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  483. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  484. 0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  485. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 0, 0, 0, 0,
  486. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  487. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  488. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  489. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  490. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  491. 0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  492. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  493. 32, 32, 32, 32, 32, 32, 32, 0, 32, 32, 32, 32, 32, 32, 32, EXC,
  494. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  495. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  496. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  497. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  498. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  499. EXC, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1,
  500. 0, 1, 0, 1, 0, 1, 0, 1, 0, EXC, 1, 0, 1, 0, 1, 0,
  501. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  502. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  503. 1, 0, 1, 0, 1, 0, 1, 0, -121, 1, 0, 1, 0, 1, 0, EXC
  504. };
  505. const int8_t TO_LOWER_TR_LT[LIMIT] = {
  506. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  507. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  508. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  509. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  510. 0, 32, 32, 32, 32, 32, 32, 32, 32, EXC, EXC, 32, 32, 32, 32, 32,
  511. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 0, 0, 0, 0,
  512. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  513. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  514. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  515. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  516. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  517. 0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  518. 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, EXC, EXC, 32, 32,
  519. 32, 32, 32, 32, 32, 32, 32, 0, 32, 32, 32, 32, 32, 32, 32, EXC,
  520. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  521. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  522. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  523. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  524. 1, 0, 1, 0, 1, 0, 1, 0, EXC, 0, 1, 0, 1, 0, EXC, 0,
  525. EXC, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1,
  526. 0, 1, 0, 1, 0, 1, 0, 1, 0, EXC, 1, 0, 1, 0, 1, 0,
  527. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  528. 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  529. 1, 0, 1, 0, 1, 0, 1, 0, -121, 1, 0, 1, 0, 1, 0, EXC
  530. };
  531. const int8_t TO_UPPER_NORMAL[LIMIT] = {
  532. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  533. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  534. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  535. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  536. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  537. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  538. 0, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32,
  539. -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, 0, 0, 0, 0, 0,
  540. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  541. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  542. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  543. 0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  544. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  545. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, EXC,
  546. -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32,
  547. -32, -32, -32, -32, -32, -32, -32, 0, -32, -32, -32, -32, -32, -32, -32, 121,
  548. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  549. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  550. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  551. 0, EXC, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0,
  552. -1, 0, -1, 0, -1, 0, -1, 0, -1, EXC, 0, -1, 0, -1, 0, -1,
  553. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  554. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  555. 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, EXC
  556. };
  557. const int8_t TO_UPPER_TR[LIMIT] = {
  558. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  559. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  560. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  561. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  562. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  563. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  564. 0, -32, -32, -32, -32, -32, -32, -32, -32, EXC, -32, -32, -32, -32, -32, -32,
  565. -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, 0, 0, 0, 0, 0,
  566. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  567. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  568. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  569. 0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  570. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  571. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, EXC,
  572. -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32,
  573. -32, -32, -32, -32, -32, -32, -32, 0, -32, -32, -32, -32, -32, -32, -32, 121,
  574. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  575. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  576. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  577. 0, EXC, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0,
  578. -1, 0, -1, 0, -1, 0, -1, 0, -1, EXC, 0, -1, 0, -1, 0, -1,
  579. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  580. 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
  581. 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, EXC
  582. };
  583. } // namespace LatinCase
  584. U_NAMESPACE_END
  585. /** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */
  586. U_CAPI int32_t U_EXPORT2
  587. ucase_getType(UChar32 c) {
  588. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  589. return UCASE_GET_TYPE(props);
  590. }
  591. /** @return same as ucase_getType() and set bit 2 if c is case-ignorable */
  592. U_CAPI int32_t U_EXPORT2
  593. ucase_getTypeOrIgnorable(UChar32 c) {
  594. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  595. return UCASE_GET_TYPE_AND_IGNORABLE(props);
  596. }
  597. /** @return UCASE_NO_DOT, UCASE_SOFT_DOTTED, UCASE_ABOVE, UCASE_OTHER_ACCENT */
  598. static inline int32_t
  599. getDotType(UChar32 c) {
  600. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  601. if(!UCASE_HAS_EXCEPTION(props)) {
  602. return props&UCASE_DOT_MASK;
  603. } else {
  604. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  605. return (*pe>>UCASE_EXC_DOT_SHIFT)&UCASE_DOT_MASK;
  606. }
  607. }
  608. U_CAPI UBool U_EXPORT2
  609. ucase_isSoftDotted(UChar32 c) {
  610. return getDotType(c)==UCASE_SOFT_DOTTED;
  611. }
  612. U_CAPI UBool U_EXPORT2
  613. ucase_isCaseSensitive(UChar32 c) {
  614. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  615. if(!UCASE_HAS_EXCEPTION(props)) {
  616. return (props&UCASE_SENSITIVE)!=0;
  617. } else {
  618. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  619. return (*pe&UCASE_EXC_SENSITIVE)!=0;
  620. }
  621. }
  622. /* string casing ------------------------------------------------------------ */
  623. /*
  624. * These internal functions form the core of string case mappings.
  625. * They map single code points to result code points or strings and take
  626. * all necessary conditions (context, locale ID, options) into account.
  627. *
  628. * They do not iterate over the source or write to the destination
  629. * so that the same functions are useful for non-standard string storage,
  630. * such as in a Replaceable (for Transliterator) or UTF-8/32 strings etc.
  631. * For the same reason, the "surrounding text" context is passed in as a
  632. * UCaseContextIterator which does not make any assumptions about
  633. * the underlying storage.
  634. *
  635. * This section contains helper functions that check for conditions
  636. * in the input text surrounding the current code point
  637. * according to SpecialCasing.txt.
  638. *
  639. * Each helper function gets the index
  640. * - after the current code point if it looks at following text
  641. * - before the current code point if it looks at preceding text
  642. *
  643. * Unicode 3.2 UAX 21 "Case Mappings" defines the conditions as follows:
  644. *
  645. * Final_Sigma
  646. * C is preceded by a sequence consisting of
  647. * a cased letter and a case-ignorable sequence,
  648. * and C is not followed by a sequence consisting of
  649. * an ignorable sequence and then a cased letter.
  650. *
  651. * More_Above
  652. * C is followed by one or more characters of combining class 230 (ABOVE)
  653. * in the combining character sequence.
  654. *
  655. * After_Soft_Dotted
  656. * The last preceding character with combining class of zero before C
  657. * was Soft_Dotted,
  658. * and there is no intervening combining character class 230 (ABOVE).
  659. *
  660. * Before_Dot
  661. * C is followed by combining dot above (U+0307).
  662. * Any sequence of characters with a combining class that is neither 0 nor 230
  663. * may intervene between the current character and the combining dot above.
  664. *
  665. * The erratum from 2002-10-31 adds the condition
  666. *
  667. * After_I
  668. * The last preceding base character was an uppercase I, and there is no
  669. * intervening combining character class 230 (ABOVE).
  670. *
  671. * (See Jitterbug 2344 and the comments on After_I below.)
  672. *
  673. * Helper definitions in Unicode 3.2 UAX 21:
  674. *
  675. * D1. A character C is defined to be cased
  676. * if it meets any of the following criteria:
  677. *
  678. * - The general category of C is Titlecase Letter (Lt)
  679. * - In [CoreProps], C has one of the properties Uppercase, or Lowercase
  680. * - Given D = NFD(C), then it is not the case that:
  681. * D = UCD_lower(D) = UCD_upper(D) = UCD_title(D)
  682. * (This third criterion does not add any characters to the list
  683. * for Unicode 3.2. Ignored.)
  684. *
  685. * D2. A character C is defined to be case-ignorable
  686. * if it meets either of the following criteria:
  687. *
  688. * - The general category of C is
  689. * Nonspacing Mark (Mn), or Enclosing Mark (Me), or Format Control (Cf), or
  690. * Letter Modifier (Lm), or Symbol Modifier (Sk)
  691. * - C is one of the following characters
  692. * U+0027 APOSTROPHE
  693. * U+00AD SOFT HYPHEN (SHY)
  694. * U+2019 RIGHT SINGLE QUOTATION MARK
  695. * (the preferred character for apostrophe)
  696. *
  697. * D3. A case-ignorable sequence is a sequence of
  698. * zero or more case-ignorable characters.
  699. */
  700. #define is_d(c) ((c)=='d' || (c)=='D')
  701. #define is_e(c) ((c)=='e' || (c)=='E')
  702. #define is_i(c) ((c)=='i' || (c)=='I')
  703. #define is_l(c) ((c)=='l' || (c)=='L')
  704. #define is_r(c) ((c)=='r' || (c)=='R')
  705. #define is_t(c) ((c)=='t' || (c)=='T')
  706. #define is_u(c) ((c)=='u' || (c)=='U')
  707. #define is_y(c) ((c)=='y' || (c)=='Y')
  708. #define is_z(c) ((c)=='z' || (c)=='Z')
  709. /* separator? */
  710. #define is_sep(c) ((c)=='_' || (c)=='-' || (c)==0)
  711. /**
  712. * Requires non-nullptr locale ID but otherwise does the equivalent of
  713. * checking for language codes as if uloc_getLanguage() were called:
  714. * Accepts both 2- and 3-letter codes and accepts case variants.
  715. */
  716. U_CFUNC int32_t
  717. ucase_getCaseLocale(const char *locale) {
  718. /*
  719. * This function used to use uloc_getLanguage(), but the current code
  720. * removes the dependency of this low-level code on uloc implementation code
  721. * and is faster because not the whole locale ID has to be
  722. * examined and copied/transformed.
  723. *
  724. * Because this code does not want to depend on uloc, the caller must
  725. * pass in a non-nullptr locale, i.e., may need to call uloc_getDefault().
  726. */
  727. char c=*locale++;
  728. // Fastpath for English "en" which is often used for default (=root locale) case mappings,
  729. // and for Chinese "zh": Very common but no special case mapping behavior.
  730. // Then check lowercase vs. uppercase to reduce the number of comparisons
  731. // for other locales without special behavior.
  732. if(c=='e') {
  733. /* el or ell? */
  734. c=*locale++;
  735. if(is_l(c)) {
  736. c=*locale++;
  737. if(is_l(c)) {
  738. c=*locale;
  739. }
  740. if(is_sep(c)) {
  741. return UCASE_LOC_GREEK;
  742. }
  743. }
  744. // en, es, ... -> root
  745. } else if(c=='z') {
  746. return UCASE_LOC_ROOT;
  747. #if U_CHARSET_FAMILY==U_ASCII_FAMILY
  748. } else if(c>='a') { // ASCII a-z = 0x61..0x7a, after A-Z
  749. #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
  750. } else if(c<='z') { // EBCDIC a-z = 0x81..0xa9 with two gaps, before A-Z
  751. #else
  752. # error Unknown charset family!
  753. #endif
  754. // lowercase c
  755. if(c=='t') {
  756. /* tr or tur? */
  757. c=*locale++;
  758. if(is_u(c)) {
  759. c=*locale++;
  760. }
  761. if(is_r(c)) {
  762. c=*locale;
  763. if(is_sep(c)) {
  764. return UCASE_LOC_TURKISH;
  765. }
  766. }
  767. } else if(c=='a') {
  768. /* az or aze? */
  769. c=*locale++;
  770. if(is_z(c)) {
  771. c=*locale++;
  772. if(is_e(c)) {
  773. c=*locale;
  774. }
  775. if(is_sep(c)) {
  776. return UCASE_LOC_TURKISH;
  777. }
  778. }
  779. } else if(c=='l') {
  780. /* lt or lit? */
  781. c=*locale++;
  782. if(is_i(c)) {
  783. c=*locale++;
  784. }
  785. if(is_t(c)) {
  786. c=*locale;
  787. if(is_sep(c)) {
  788. return UCASE_LOC_LITHUANIAN;
  789. }
  790. }
  791. } else if(c=='n') {
  792. /* nl or nld? */
  793. c=*locale++;
  794. if(is_l(c)) {
  795. c=*locale++;
  796. if(is_d(c)) {
  797. c=*locale;
  798. }
  799. if(is_sep(c)) {
  800. return UCASE_LOC_DUTCH;
  801. }
  802. }
  803. } else if(c=='h') {
  804. /* hy or hye? *not* hyw */
  805. c=*locale++;
  806. if(is_y(c)) {
  807. c=*locale++;
  808. if(is_e(c)) {
  809. c=*locale;
  810. }
  811. if(is_sep(c)) {
  812. return UCASE_LOC_ARMENIAN;
  813. }
  814. }
  815. }
  816. } else {
  817. // uppercase c
  818. // Same code as for lowercase c but also check for 'E'.
  819. if(c=='T') {
  820. /* tr or tur? */
  821. c=*locale++;
  822. if(is_u(c)) {
  823. c=*locale++;
  824. }
  825. if(is_r(c)) {
  826. c=*locale;
  827. if(is_sep(c)) {
  828. return UCASE_LOC_TURKISH;
  829. }
  830. }
  831. } else if(c=='A') {
  832. /* az or aze? */
  833. c=*locale++;
  834. if(is_z(c)) {
  835. c=*locale++;
  836. if(is_e(c)) {
  837. c=*locale;
  838. }
  839. if(is_sep(c)) {
  840. return UCASE_LOC_TURKISH;
  841. }
  842. }
  843. } else if(c=='L') {
  844. /* lt or lit? */
  845. c=*locale++;
  846. if(is_i(c)) {
  847. c=*locale++;
  848. }
  849. if(is_t(c)) {
  850. c=*locale;
  851. if(is_sep(c)) {
  852. return UCASE_LOC_LITHUANIAN;
  853. }
  854. }
  855. } else if(c=='E') {
  856. /* el or ell? */
  857. c=*locale++;
  858. if(is_l(c)) {
  859. c=*locale++;
  860. if(is_l(c)) {
  861. c=*locale;
  862. }
  863. if(is_sep(c)) {
  864. return UCASE_LOC_GREEK;
  865. }
  866. }
  867. } else if(c=='N') {
  868. /* nl or nld? */
  869. c=*locale++;
  870. if(is_l(c)) {
  871. c=*locale++;
  872. if(is_d(c)) {
  873. c=*locale;
  874. }
  875. if(is_sep(c)) {
  876. return UCASE_LOC_DUTCH;
  877. }
  878. }
  879. } else if(c=='H') {
  880. /* hy or hye? *not* hyw */
  881. c=*locale++;
  882. if(is_y(c)) {
  883. c=*locale++;
  884. if(is_e(c)) {
  885. c=*locale;
  886. }
  887. if(is_sep(c)) {
  888. return UCASE_LOC_ARMENIAN;
  889. }
  890. }
  891. }
  892. }
  893. return UCASE_LOC_ROOT;
  894. }
  895. /*
  896. * Is followed by
  897. * {case-ignorable}* cased
  898. * ?
  899. * (dir determines looking forward/backward)
  900. * If a character is case-ignorable, it is skipped regardless of whether
  901. * it is also cased or not.
  902. */
  903. static UBool
  904. isFollowedByCasedLetter(UCaseContextIterator *iter, void *context, int8_t dir) {
  905. UChar32 c;
  906. if(iter==nullptr) {
  907. return false;
  908. }
  909. for(/* dir!=0 sets direction */; (c=iter(context, dir))>=0; dir=0) {
  910. int32_t type=ucase_getTypeOrIgnorable(c);
  911. if(type&4) {
  912. /* case-ignorable, continue with the loop */
  913. } else if(type!=UCASE_NONE) {
  914. return true; /* followed by cased letter */
  915. } else {
  916. return false; /* uncased and not case-ignorable */
  917. }
  918. }
  919. return false; /* not followed by cased letter */
  920. }
  921. /* Is preceded by Soft_Dotted character with no intervening cc=230 ? */
  922. static UBool
  923. isPrecededBySoftDotted(UCaseContextIterator *iter, void *context) {
  924. UChar32 c;
  925. int32_t dotType;
  926. int8_t dir;
  927. if(iter==nullptr) {
  928. return false;
  929. }
  930. for(dir=-1; (c=iter(context, dir))>=0; dir=0) {
  931. dotType=getDotType(c);
  932. if(dotType==UCASE_SOFT_DOTTED) {
  933. return true; /* preceded by TYPE_i */
  934. } else if(dotType!=UCASE_OTHER_ACCENT) {
  935. return false; /* preceded by different base character (not TYPE_i), or intervening cc==230 */
  936. }
  937. }
  938. return false; /* not preceded by TYPE_i */
  939. }
  940. /*
  941. * See Jitterbug 2344:
  942. * The condition After_I for Turkic-lowercasing of U+0307 combining dot above
  943. * is checked in ICU 2.0, 2.1, 2.6 but was not in 2.2 & 2.4 because
  944. * we made those releases compatible with Unicode 3.2 which had not fixed
  945. * a related bug in SpecialCasing.txt.
  946. *
  947. * From the Jitterbug 2344 text:
  948. * ... this bug is listed as a Unicode erratum
  949. * from 2002-10-31 at http://www.unicode.org/uni2errata/UnicodeErrata.html
  950. * <quote>
  951. * There are two errors in SpecialCasing.txt.
  952. * 1. Missing semicolons on two lines. ... [irrelevant for ICU]
  953. * 2. An incorrect context definition. Correct as follows:
  954. * < 0307; ; 0307; 0307; tr After_Soft_Dotted; # COMBINING DOT ABOVE
  955. * < 0307; ; 0307; 0307; az After_Soft_Dotted; # COMBINING DOT ABOVE
  956. * ---
  957. * > 0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
  958. * > 0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
  959. * where the context After_I is defined as:
  960. * The last preceding base character was an uppercase I, and there is no
  961. * intervening combining character class 230 (ABOVE).
  962. * </quote>
  963. *
  964. * Note that SpecialCasing.txt even in Unicode 3.2 described the condition as:
  965. *
  966. * # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
  967. * # This matches the behavior of the canonically equivalent I-dot_above
  968. *
  969. * See also the description in this place in older versions of uchar.c (revision 1.100).
  970. *
  971. * Markus W. Scherer 2003-feb-15
  972. */
  973. /* Is preceded by base character 'I' with no intervening cc=230 ? */
  974. static UBool
  975. isPrecededBy_I(UCaseContextIterator *iter, void *context) {
  976. UChar32 c;
  977. int32_t dotType;
  978. int8_t dir;
  979. if(iter==nullptr) {
  980. return false;
  981. }
  982. for(dir=-1; (c=iter(context, dir))>=0; dir=0) {
  983. if(c==0x49) {
  984. return true; /* preceded by I */
  985. }
  986. dotType=getDotType(c);
  987. if(dotType!=UCASE_OTHER_ACCENT) {
  988. return false; /* preceded by different base character (not I), or intervening cc==230 */
  989. }
  990. }
  991. return false; /* not preceded by I */
  992. }
  993. /* Is followed by one or more cc==230 ? */
  994. static UBool
  995. isFollowedByMoreAbove(UCaseContextIterator *iter, void *context) {
  996. UChar32 c;
  997. int32_t dotType;
  998. int8_t dir;
  999. if(iter==nullptr) {
  1000. return false;
  1001. }
  1002. for(dir=1; (c=iter(context, dir))>=0; dir=0) {
  1003. dotType=getDotType(c);
  1004. if(dotType==UCASE_ABOVE) {
  1005. return true; /* at least one cc==230 following */
  1006. } else if(dotType!=UCASE_OTHER_ACCENT) {
  1007. return false; /* next base character, no more cc==230 following */
  1008. }
  1009. }
  1010. return false; /* no more cc==230 following */
  1011. }
  1012. /* Is followed by a dot above (without cc==230 in between) ? */
  1013. static UBool
  1014. isFollowedByDotAbove(UCaseContextIterator *iter, void *context) {
  1015. UChar32 c;
  1016. int32_t dotType;
  1017. int8_t dir;
  1018. if(iter==nullptr) {
  1019. return false;
  1020. }
  1021. for(dir=1; (c=iter(context, dir))>=0; dir=0) {
  1022. if(c==0x307) {
  1023. return true;
  1024. }
  1025. dotType=getDotType(c);
  1026. if(dotType!=UCASE_OTHER_ACCENT) {
  1027. return false; /* next base character or cc==230 in between */
  1028. }
  1029. }
  1030. return false; /* no dot above following */
  1031. }
  1032. U_CAPI int32_t U_EXPORT2
  1033. ucase_toFullLower(UChar32 c,
  1034. UCaseContextIterator *iter, void *context,
  1035. const char16_t **pString,
  1036. int32_t loc) {
  1037. // The sign of the result has meaning, input must be non-negative so that it can be returned as is.
  1038. U_ASSERT(c >= 0);
  1039. UChar32 result=c;
  1040. // Reset the output pointer in case it was uninitialized.
  1041. *pString=nullptr;
  1042. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  1043. if(!UCASE_HAS_EXCEPTION(props)) {
  1044. if(UCASE_IS_UPPER_OR_TITLE(props)) {
  1045. result=c+UCASE_GET_DELTA(props);
  1046. }
  1047. } else {
  1048. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props), *pe2;
  1049. uint16_t excWord=*pe++;
  1050. int32_t full;
  1051. pe2=pe;
  1052. if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
  1053. /* use hardcoded conditions and mappings */
  1054. /*
  1055. * Test for conditional mappings first
  1056. * (otherwise the unconditional default mappings are always taken),
  1057. * then test for characters that have unconditional mappings in SpecialCasing.txt,
  1058. * then get the UnicodeData.txt mappings.
  1059. */
  1060. if( loc==UCASE_LOC_LITHUANIAN &&
  1061. /* base characters, find accents above */
  1062. (((c==0x49 || c==0x4a || c==0x12e) &&
  1063. isFollowedByMoreAbove(iter, context)) ||
  1064. /* precomposed with accent above, no need to find one */
  1065. (c==0xcc || c==0xcd || c==0x128))
  1066. ) {
  1067. /*
  1068. # Lithuanian
  1069. # Lithuanian retains the dot in a lowercase i when followed by accents.
  1070. # Introduce an explicit dot above when lowercasing capital I's and J's
  1071. # whenever there are more accents above.
  1072. # (of the accents used in Lithuanian: grave, acute, tilde above, and ogonek)
  1073. 0049; 0069 0307; 0049; 0049; lt More_Above; # LATIN CAPITAL LETTER I
  1074. 004A; 006A 0307; 004A; 004A; lt More_Above; # LATIN CAPITAL LETTER J
  1075. 012E; 012F 0307; 012E; 012E; lt More_Above; # LATIN CAPITAL LETTER I WITH OGONEK
  1076. 00CC; 0069 0307 0300; 00CC; 00CC; lt; # LATIN CAPITAL LETTER I WITH GRAVE
  1077. 00CD; 0069 0307 0301; 00CD; 00CD; lt; # LATIN CAPITAL LETTER I WITH ACUTE
  1078. 0128; 0069 0307 0303; 0128; 0128; lt; # LATIN CAPITAL LETTER I WITH TILDE
  1079. */
  1080. switch(c) {
  1081. case 0x49: /* LATIN CAPITAL LETTER I */
  1082. *pString=iDot;
  1083. return 2;
  1084. case 0x4a: /* LATIN CAPITAL LETTER J */
  1085. *pString=jDot;
  1086. return 2;
  1087. case 0x12e: /* LATIN CAPITAL LETTER I WITH OGONEK */
  1088. *pString=iOgonekDot;
  1089. return 2;
  1090. case 0xcc: /* LATIN CAPITAL LETTER I WITH GRAVE */
  1091. *pString=iDotGrave;
  1092. return 3;
  1093. case 0xcd: /* LATIN CAPITAL LETTER I WITH ACUTE */
  1094. *pString=iDotAcute;
  1095. return 3;
  1096. case 0x128: /* LATIN CAPITAL LETTER I WITH TILDE */
  1097. *pString=iDotTilde;
  1098. return 3;
  1099. default:
  1100. return 0; /* will not occur */
  1101. }
  1102. /* # Turkish and Azeri */
  1103. } else if(loc==UCASE_LOC_TURKISH && c==0x130) {
  1104. /*
  1105. # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
  1106. # The following rules handle those cases.
  1107. 0130; 0069; 0130; 0130; tr # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1108. 0130; 0069; 0130; 0130; az # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1109. */
  1110. return 0x69;
  1111. } else if(loc==UCASE_LOC_TURKISH && c==0x307 && isPrecededBy_I(iter, context)) {
  1112. /*
  1113. # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
  1114. # This matches the behavior of the canonically equivalent I-dot_above
  1115. 0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
  1116. 0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
  1117. */
  1118. return 0; /* remove the dot (continue without output) */
  1119. } else if(loc==UCASE_LOC_TURKISH && c==0x49 && !isFollowedByDotAbove(iter, context)) {
  1120. /*
  1121. # When lowercasing, unless an I is before a dot_above, it turns into a dotless i.
  1122. 0049; 0131; 0049; 0049; tr Not_Before_Dot; # LATIN CAPITAL LETTER I
  1123. 0049; 0131; 0049; 0049; az Not_Before_Dot; # LATIN CAPITAL LETTER I
  1124. */
  1125. return 0x131;
  1126. } else if(c==0x130) {
  1127. /*
  1128. # Preserve canonical equivalence for I with dot. Turkic is handled below.
  1129. 0130; 0069 0307; 0130; 0130; # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1130. */
  1131. *pString=iDot;
  1132. return 2;
  1133. } else if( c==0x3a3 &&
  1134. !isFollowedByCasedLetter(iter, context, 1) &&
  1135. isFollowedByCasedLetter(iter, context, -1) /* -1=preceded */
  1136. ) {
  1137. /* greek capital sigma maps depending on surrounding cased letters (see SpecialCasing.txt) */
  1138. /*
  1139. # Special case for final form of sigma
  1140. 03A3; 03C2; 03A3; 03A3; Final_Sigma; # GREEK CAPITAL LETTER SIGMA
  1141. */
  1142. return 0x3c2; /* greek small final sigma */
  1143. } else {
  1144. /* no known conditional special case mapping, use a normal mapping */
  1145. }
  1146. } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
  1147. GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
  1148. full&=UCASE_FULL_LOWER;
  1149. if(full!=0) {
  1150. /* set the output pointer to the lowercase mapping */
  1151. *pString=reinterpret_cast<const char16_t *>(pe+1);
  1152. /* return the string length */
  1153. return full;
  1154. }
  1155. }
  1156. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
  1157. int32_t delta;
  1158. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe2, delta);
  1159. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  1160. }
  1161. if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
  1162. GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe2, result);
  1163. }
  1164. }
  1165. return (result==c) ? ~result : result;
  1166. }
  1167. /* internal */
  1168. static int32_t
  1169. toUpperOrTitle(UChar32 c,
  1170. UCaseContextIterator *iter, void *context,
  1171. const char16_t **pString,
  1172. int32_t loc,
  1173. UBool upperNotTitle) {
  1174. // The sign of the result has meaning, input must be non-negative so that it can be returned as is.
  1175. U_ASSERT(c >= 0);
  1176. UChar32 result=c;
  1177. // Reset the output pointer in case it was uninitialized.
  1178. *pString=nullptr;
  1179. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  1180. if(!UCASE_HAS_EXCEPTION(props)) {
  1181. if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
  1182. result=c+UCASE_GET_DELTA(props);
  1183. }
  1184. } else {
  1185. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props), *pe2;
  1186. uint16_t excWord=*pe++;
  1187. int32_t full, idx;
  1188. pe2=pe;
  1189. if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
  1190. /* use hardcoded conditions and mappings */
  1191. if(loc==UCASE_LOC_TURKISH && c==0x69) {
  1192. /*
  1193. # Turkish and Azeri
  1194. # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
  1195. # The following rules handle those cases.
  1196. # When uppercasing, i turns into a dotted capital I
  1197. 0069; 0069; 0130; 0130; tr; # LATIN SMALL LETTER I
  1198. 0069; 0069; 0130; 0130; az; # LATIN SMALL LETTER I
  1199. */
  1200. return 0x130;
  1201. } else if(loc==UCASE_LOC_LITHUANIAN && c==0x307 && isPrecededBySoftDotted(iter, context)) {
  1202. /*
  1203. # Lithuanian
  1204. # Lithuanian retains the dot in a lowercase i when followed by accents.
  1205. # Remove DOT ABOVE after "i" with upper or titlecase
  1206. 0307; 0307; ; ; lt After_Soft_Dotted; # COMBINING DOT ABOVE
  1207. */
  1208. return 0; /* remove the dot (continue without output) */
  1209. } else if(c==0x0587) {
  1210. // See ICU-13416:
  1211. // և ligature ech-yiwn
  1212. // uppercases to ԵՒ=ech+yiwn by default and in Western Armenian,
  1213. // but to ԵՎ=ech+vew in Eastern Armenian.
  1214. if(loc==UCASE_LOC_ARMENIAN) {
  1215. *pString=upperNotTitle ? u"ԵՎ" : u"Եվ";
  1216. } else {
  1217. *pString=upperNotTitle ? u"ԵՒ" : u"Եւ";
  1218. }
  1219. return 2;
  1220. } else {
  1221. /* no known conditional special case mapping, use a normal mapping */
  1222. }
  1223. } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
  1224. GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
  1225. /* start of full case mapping strings */
  1226. ++pe;
  1227. /* skip the lowercase and case-folding result strings */
  1228. pe+=full&UCASE_FULL_LOWER;
  1229. full>>=4;
  1230. pe+=full&0xf;
  1231. full>>=4;
  1232. if(upperNotTitle) {
  1233. full&=0xf;
  1234. } else {
  1235. /* skip the uppercase result string */
  1236. pe+=full&0xf;
  1237. full=(full>>4)&0xf;
  1238. }
  1239. if(full!=0) {
  1240. /* set the output pointer to the result string */
  1241. *pString=reinterpret_cast<const char16_t *>(pe);
  1242. /* return the string length */
  1243. return full;
  1244. }
  1245. }
  1246. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_GET_TYPE(props)==UCASE_LOWER) {
  1247. int32_t delta;
  1248. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe2, delta);
  1249. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  1250. }
  1251. if(!upperNotTitle && HAS_SLOT(excWord, UCASE_EXC_TITLE)) {
  1252. idx=UCASE_EXC_TITLE;
  1253. } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
  1254. /* here, titlecase is same as uppercase */
  1255. idx=UCASE_EXC_UPPER;
  1256. } else {
  1257. return ~c;
  1258. }
  1259. GET_SLOT_VALUE(excWord, idx, pe2, result);
  1260. }
  1261. return (result==c) ? ~result : result;
  1262. }
  1263. U_CAPI int32_t U_EXPORT2
  1264. ucase_toFullUpper(UChar32 c,
  1265. UCaseContextIterator *iter, void *context,
  1266. const char16_t **pString,
  1267. int32_t caseLocale) {
  1268. return toUpperOrTitle(c, iter, context, pString, caseLocale, true);
  1269. }
  1270. U_CAPI int32_t U_EXPORT2
  1271. ucase_toFullTitle(UChar32 c,
  1272. UCaseContextIterator *iter, void *context,
  1273. const char16_t **pString,
  1274. int32_t caseLocale) {
  1275. return toUpperOrTitle(c, iter, context, pString, caseLocale, false);
  1276. }
  1277. /* case folding ------------------------------------------------------------- */
  1278. /*
  1279. * Case folding is similar to lowercasing.
  1280. * The result may be a simple mapping, i.e., a single code point, or
  1281. * a full mapping, i.e., a string.
  1282. * If the case folding for a code point is the same as its simple (1:1) lowercase mapping,
  1283. * then only the lowercase mapping is stored.
  1284. *
  1285. * Some special cases are hardcoded because their conditions cannot be
  1286. * parsed and processed from CaseFolding.txt.
  1287. *
  1288. * Unicode 3.2 CaseFolding.txt specifies for its status field:
  1289. # C: common case folding, common mappings shared by both simple and full mappings.
  1290. # F: full case folding, mappings that cause strings to grow in length. Multiple characters are separated by spaces.
  1291. # S: simple case folding, mappings to single characters where different from F.
  1292. # T: special case for uppercase I and dotted uppercase I
  1293. # - For non-Turkic languages, this mapping is normally not used.
  1294. # - For Turkic languages (tr, az), this mapping can be used instead of the normal mapping for these characters.
  1295. #
  1296. # Usage:
  1297. # A. To do a simple case folding, use the mappings with status C + S.
  1298. # B. To do a full case folding, use the mappings with status C + F.
  1299. #
  1300. # The mappings with status T can be used or omitted depending on the desired case-folding
  1301. # behavior. (The default option is to exclude them.)
  1302. * Unicode 3.2 has 'T' mappings as follows:
  1303. 0049; T; 0131; # LATIN CAPITAL LETTER I
  1304. 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1305. * while the default mappings for these code points are:
  1306. 0049; C; 0069; # LATIN CAPITAL LETTER I
  1307. 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1308. * U+0130 has no simple case folding (simple-case-folds to itself).
  1309. */
  1310. /* return the simple case folding mapping for c */
  1311. U_CAPI UChar32 U_EXPORT2
  1312. ucase_fold(UChar32 c, uint32_t options) {
  1313. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  1314. if(!UCASE_HAS_EXCEPTION(props)) {
  1315. if(UCASE_IS_UPPER_OR_TITLE(props)) {
  1316. c+=UCASE_GET_DELTA(props);
  1317. }
  1318. } else {
  1319. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
  1320. uint16_t excWord=*pe++;
  1321. int32_t idx;
  1322. if(excWord&UCASE_EXC_CONDITIONAL_FOLD) {
  1323. /* special case folding mappings, hardcoded */
  1324. if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) {
  1325. /* default mappings */
  1326. if(c==0x49) {
  1327. /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
  1328. return 0x69;
  1329. } else if(c==0x130) {
  1330. /* no simple case folding for U+0130 */
  1331. return c;
  1332. }
  1333. } else {
  1334. /* Turkic mappings */
  1335. if(c==0x49) {
  1336. /* 0049; T; 0131; # LATIN CAPITAL LETTER I */
  1337. return 0x131;
  1338. } else if(c==0x130) {
  1339. /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
  1340. return 0x69;
  1341. }
  1342. }
  1343. }
  1344. if((excWord&UCASE_EXC_NO_SIMPLE_CASE_FOLDING)!=0) {
  1345. return c;
  1346. }
  1347. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
  1348. int32_t delta;
  1349. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
  1350. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  1351. }
  1352. if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) {
  1353. idx=UCASE_EXC_FOLD;
  1354. } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
  1355. idx=UCASE_EXC_LOWER;
  1356. } else {
  1357. return c;
  1358. }
  1359. GET_SLOT_VALUE(excWord, idx, pe, c);
  1360. }
  1361. return c;
  1362. }
  1363. /*
  1364. * Issue for canonical caseless match (UAX #21):
  1365. * Turkic casefolding (using "T" mappings in CaseFolding.txt) does not preserve
  1366. * canonical equivalence, unlike default-option casefolding.
  1367. * For example, I-grave and I + grave fold to strings that are not canonically
  1368. * equivalent.
  1369. * For more details, see the comment in unorm_compare() in unorm.cpp
  1370. * and the intermediate prototype changes for Jitterbug 2021.
  1371. * (For example, revision 1.104 of uchar.c and 1.4 of CaseFolding.txt.)
  1372. *
  1373. * This did not get fixed because it appears that it is not possible to fix
  1374. * it for uppercase and lowercase characters (I-grave vs. i-grave)
  1375. * together in a way that they still fold to common result strings.
  1376. */
  1377. U_CAPI int32_t U_EXPORT2
  1378. ucase_toFullFolding(UChar32 c,
  1379. const char16_t **pString,
  1380. uint32_t options) {
  1381. // The sign of the result has meaning, input must be non-negative so that it can be returned as is.
  1382. U_ASSERT(c >= 0);
  1383. UChar32 result=c;
  1384. // Reset the output pointer in case it was uninitialized.
  1385. *pString=nullptr;
  1386. uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
  1387. if(!UCASE_HAS_EXCEPTION(props)) {
  1388. if(UCASE_IS_UPPER_OR_TITLE(props)) {
  1389. result=c+UCASE_GET_DELTA(props);
  1390. }
  1391. } else {
  1392. const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props), *pe2;
  1393. uint16_t excWord=*pe++;
  1394. int32_t full, idx;
  1395. pe2=pe;
  1396. if(excWord&UCASE_EXC_CONDITIONAL_FOLD) {
  1397. /* use hardcoded conditions and mappings */
  1398. if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) {
  1399. /* default mappings */
  1400. if(c==0x49) {
  1401. /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
  1402. return 0x69;
  1403. } else if(c==0x130) {
  1404. /* 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
  1405. *pString=iDot;
  1406. return 2;
  1407. }
  1408. } else {
  1409. /* Turkic mappings */
  1410. if(c==0x49) {
  1411. /* 0049; T; 0131; # LATIN CAPITAL LETTER I */
  1412. return 0x131;
  1413. } else if(c==0x130) {
  1414. /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
  1415. return 0x69;
  1416. }
  1417. }
  1418. } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
  1419. GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
  1420. /* start of full case mapping strings */
  1421. ++pe;
  1422. /* skip the lowercase result string */
  1423. pe+=full&UCASE_FULL_LOWER;
  1424. full=(full>>4)&0xf;
  1425. if(full!=0) {
  1426. /* set the output pointer to the result string */
  1427. *pString=reinterpret_cast<const char16_t *>(pe);
  1428. /* return the string length */
  1429. return full;
  1430. }
  1431. }
  1432. if((excWord&UCASE_EXC_NO_SIMPLE_CASE_FOLDING)!=0) {
  1433. return ~c;
  1434. }
  1435. if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
  1436. int32_t delta;
  1437. GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe2, delta);
  1438. return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
  1439. }
  1440. if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) {
  1441. idx=UCASE_EXC_FOLD;
  1442. } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
  1443. idx=UCASE_EXC_LOWER;
  1444. } else {
  1445. return ~c;
  1446. }
  1447. GET_SLOT_VALUE(excWord, idx, pe2, result);
  1448. }
  1449. return (result==c) ? ~result : result;
  1450. }
  1451. /* case mapping properties API ---------------------------------------------- */
  1452. /* public API (see uchar.h) */
  1453. U_CAPI UBool U_EXPORT2
  1454. u_isULowercase(UChar32 c) {
  1455. return UCASE_LOWER==ucase_getType(c);
  1456. }
  1457. U_CAPI UBool U_EXPORT2
  1458. u_isUUppercase(UChar32 c) {
  1459. return UCASE_UPPER==ucase_getType(c);
  1460. }
  1461. /* Transforms the Unicode character to its lower case equivalent.*/
  1462. U_CAPI UChar32 U_EXPORT2
  1463. u_tolower(UChar32 c) {
  1464. return ucase_tolower(c);
  1465. }
  1466. /* Transforms the Unicode character to its upper case equivalent.*/
  1467. U_CAPI UChar32 U_EXPORT2
  1468. u_toupper(UChar32 c) {
  1469. return ucase_toupper(c);
  1470. }
  1471. /* Transforms the Unicode character to its title case equivalent.*/
  1472. U_CAPI UChar32 U_EXPORT2
  1473. u_totitle(UChar32 c) {
  1474. return ucase_totitle(c);
  1475. }
  1476. /* return the simple case folding mapping for c */
  1477. U_CAPI UChar32 U_EXPORT2
  1478. u_foldCase(UChar32 c, uint32_t options) {
  1479. return ucase_fold(c, options);
  1480. }
  1481. U_CFUNC int32_t U_EXPORT2
  1482. ucase_hasBinaryProperty(UChar32 c, UProperty which) {
  1483. /* case mapping properties */
  1484. const char16_t *resultString;
  1485. switch(which) {
  1486. case UCHAR_LOWERCASE:
  1487. return (UBool)(UCASE_LOWER==ucase_getType(c));
  1488. case UCHAR_UPPERCASE:
  1489. return (UBool)(UCASE_UPPER==ucase_getType(c));
  1490. case UCHAR_SOFT_DOTTED:
  1491. return ucase_isSoftDotted(c);
  1492. case UCHAR_CASE_SENSITIVE:
  1493. return ucase_isCaseSensitive(c);
  1494. case UCHAR_CASED:
  1495. return (UBool)(UCASE_NONE!=ucase_getType(c));
  1496. case UCHAR_CASE_IGNORABLE:
  1497. return (UBool)(ucase_getTypeOrIgnorable(c)>>2);
  1498. /*
  1499. * Note: The following Changes_When_Xyz are defined as testing whether
  1500. * the NFD form of the input changes when Xyz-case-mapped.
  1501. * However, this simpler implementation of these properties,
  1502. * ignoring NFD, passes the tests.
  1503. * The implementation needs to be changed if the tests start failing.
  1504. * When that happens, optimizations should be used to work with the
  1505. * per-single-code point ucase_toFullXyz() functions unless
  1506. * the NFD form has more than one code point,
  1507. * and the property starts set needs to be the union of the
  1508. * start sets for normalization and case mappings.
  1509. */
  1510. case UCHAR_CHANGES_WHEN_LOWERCASED:
  1511. return (UBool)(ucase_toFullLower(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
  1512. case UCHAR_CHANGES_WHEN_UPPERCASED:
  1513. return (UBool)(ucase_toFullUpper(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
  1514. case UCHAR_CHANGES_WHEN_TITLECASED:
  1515. return (UBool)(ucase_toFullTitle(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
  1516. /* case UCHAR_CHANGES_WHEN_CASEFOLDED: -- in uprops.c */
  1517. case UCHAR_CHANGES_WHEN_CASEMAPPED:
  1518. return (UBool)(
  1519. ucase_toFullLower(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0 ||
  1520. ucase_toFullUpper(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0 ||
  1521. ucase_toFullTitle(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
  1522. default:
  1523. return false;
  1524. }
  1525. }