ucase.cpp 61 KB

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