ucharstrie.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2010-2011, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * file name: ucharstrie.h
  9. * encoding: UTF-8
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2010nov14
  14. * created by: Markus W. Scherer
  15. */
  16. #include "unicode/utypes.h"
  17. #include "unicode/appendable.h"
  18. #include "unicode/ucharstrie.h"
  19. #include "unicode/uobject.h"
  20. #include "unicode/utf16.h"
  21. #include "cmemory.h"
  22. #include "uassert.h"
  23. U_NAMESPACE_BEGIN
  24. UCharsTrie::~UCharsTrie() {
  25. uprv_free(ownedArray_);
  26. }
  27. UStringTrieResult
  28. UCharsTrie::current() const {
  29. const char16_t *pos=pos_;
  30. if(pos==nullptr) {
  31. return USTRINGTRIE_NO_MATCH;
  32. } else {
  33. int32_t node;
  34. return (remainingMatchLength_<0 && (node=*pos)>=kMinValueLead) ?
  35. valueResult(node) : USTRINGTRIE_NO_VALUE;
  36. }
  37. }
  38. UStringTrieResult
  39. UCharsTrie::firstForCodePoint(UChar32 cp) {
  40. return cp<=0xffff ?
  41. first(cp) :
  42. (USTRINGTRIE_HAS_NEXT(first(U16_LEAD(cp))) ?
  43. next(U16_TRAIL(cp)) :
  44. USTRINGTRIE_NO_MATCH);
  45. }
  46. UStringTrieResult
  47. UCharsTrie::nextForCodePoint(UChar32 cp) {
  48. return cp<=0xffff ?
  49. next(cp) :
  50. (USTRINGTRIE_HAS_NEXT(next(U16_LEAD(cp))) ?
  51. next(U16_TRAIL(cp)) :
  52. USTRINGTRIE_NO_MATCH);
  53. }
  54. UStringTrieResult
  55. UCharsTrie::branchNext(const char16_t *pos, int32_t length, int32_t uchar) {
  56. // Branch according to the current unit.
  57. if(length==0) {
  58. length=*pos++;
  59. }
  60. ++length;
  61. // The length of the branch is the number of units to select from.
  62. // The data structure encodes a binary search.
  63. while(length>kMaxBranchLinearSubNodeLength) {
  64. if(uchar<*pos++) {
  65. length>>=1;
  66. pos=jumpByDelta(pos);
  67. } else {
  68. length=length-(length>>1);
  69. pos=skipDelta(pos);
  70. }
  71. }
  72. // Drop down to linear search for the last few units.
  73. // length>=2 because the loop body above sees length>kMaxBranchLinearSubNodeLength>=3
  74. // and divides length by 2.
  75. do {
  76. if(uchar==*pos++) {
  77. UStringTrieResult result;
  78. int32_t node=*pos;
  79. if(node&kValueIsFinal) {
  80. // Leave the final value for getValue() to read.
  81. result=USTRINGTRIE_FINAL_VALUE;
  82. } else {
  83. // Use the non-final value as the jump delta.
  84. ++pos;
  85. // int32_t delta=readValue(pos, node);
  86. int32_t delta;
  87. if(node<kMinTwoUnitValueLead) {
  88. delta=node;
  89. } else if(node<kThreeUnitValueLead) {
  90. delta=((node-kMinTwoUnitValueLead)<<16)|*pos++;
  91. } else {
  92. delta=(pos[0]<<16)|pos[1];
  93. pos+=2;
  94. }
  95. // end readValue()
  96. pos+=delta;
  97. node=*pos;
  98. result= node>=kMinValueLead ? valueResult(node) : USTRINGTRIE_NO_VALUE;
  99. }
  100. pos_=pos;
  101. return result;
  102. }
  103. --length;
  104. pos=skipValue(pos);
  105. } while(length>1);
  106. if(uchar==*pos++) {
  107. pos_=pos;
  108. int32_t node=*pos;
  109. return node>=kMinValueLead ? valueResult(node) : USTRINGTRIE_NO_VALUE;
  110. } else {
  111. stop();
  112. return USTRINGTRIE_NO_MATCH;
  113. }
  114. }
  115. UStringTrieResult
  116. UCharsTrie::nextImpl(const char16_t *pos, int32_t uchar) {
  117. int32_t node=*pos++;
  118. for(;;) {
  119. if(node<kMinLinearMatch) {
  120. return branchNext(pos, node, uchar);
  121. } else if(node<kMinValueLead) {
  122. // Match the first of length+1 units.
  123. int32_t length=node-kMinLinearMatch; // Actual match length minus 1.
  124. if(uchar==*pos++) {
  125. remainingMatchLength_=--length;
  126. pos_=pos;
  127. return (length<0 && (node=*pos)>=kMinValueLead) ?
  128. valueResult(node) : USTRINGTRIE_NO_VALUE;
  129. } else {
  130. // No match.
  131. break;
  132. }
  133. } else if(node&kValueIsFinal) {
  134. // No further matching units.
  135. break;
  136. } else {
  137. // Skip intermediate value.
  138. pos=skipNodeValue(pos, node);
  139. node&=kNodeTypeMask;
  140. }
  141. }
  142. stop();
  143. return USTRINGTRIE_NO_MATCH;
  144. }
  145. UStringTrieResult
  146. UCharsTrie::next(int32_t uchar) {
  147. const char16_t *pos=pos_;
  148. if(pos==nullptr) {
  149. return USTRINGTRIE_NO_MATCH;
  150. }
  151. int32_t length=remainingMatchLength_; // Actual remaining match length minus 1.
  152. if(length>=0) {
  153. // Remaining part of a linear-match node.
  154. if(uchar==*pos++) {
  155. remainingMatchLength_=--length;
  156. pos_=pos;
  157. int32_t node;
  158. return (length<0 && (node=*pos)>=kMinValueLead) ?
  159. valueResult(node) : USTRINGTRIE_NO_VALUE;
  160. } else {
  161. stop();
  162. return USTRINGTRIE_NO_MATCH;
  163. }
  164. }
  165. return nextImpl(pos, uchar);
  166. }
  167. UStringTrieResult
  168. UCharsTrie::next(ConstChar16Ptr ptr, int32_t sLength) {
  169. const char16_t *s=ptr;
  170. if(sLength<0 ? *s==0 : sLength==0) {
  171. // Empty input.
  172. return current();
  173. }
  174. const char16_t *pos=pos_;
  175. if(pos==nullptr) {
  176. return USTRINGTRIE_NO_MATCH;
  177. }
  178. int32_t length=remainingMatchLength_; // Actual remaining match length minus 1.
  179. for(;;) {
  180. // Fetch the next input unit, if there is one.
  181. // Continue a linear-match node without rechecking sLength<0.
  182. int32_t uchar;
  183. if(sLength<0) {
  184. for(;;) {
  185. if((uchar=*s++)==0) {
  186. remainingMatchLength_=length;
  187. pos_=pos;
  188. int32_t node;
  189. return (length<0 && (node=*pos)>=kMinValueLead) ?
  190. valueResult(node) : USTRINGTRIE_NO_VALUE;
  191. }
  192. if(length<0) {
  193. remainingMatchLength_=length;
  194. break;
  195. }
  196. if(uchar!=*pos) {
  197. stop();
  198. return USTRINGTRIE_NO_MATCH;
  199. }
  200. ++pos;
  201. --length;
  202. }
  203. } else {
  204. for(;;) {
  205. if(sLength==0) {
  206. remainingMatchLength_=length;
  207. pos_=pos;
  208. int32_t node;
  209. return (length<0 && (node=*pos)>=kMinValueLead) ?
  210. valueResult(node) : USTRINGTRIE_NO_VALUE;
  211. }
  212. uchar=*s++;
  213. --sLength;
  214. if(length<0) {
  215. remainingMatchLength_=length;
  216. break;
  217. }
  218. if(uchar!=*pos) {
  219. stop();
  220. return USTRINGTRIE_NO_MATCH;
  221. }
  222. ++pos;
  223. --length;
  224. }
  225. }
  226. int32_t node=*pos++;
  227. for(;;) {
  228. if(node<kMinLinearMatch) {
  229. UStringTrieResult result=branchNext(pos, node, uchar);
  230. if(result==USTRINGTRIE_NO_MATCH) {
  231. return USTRINGTRIE_NO_MATCH;
  232. }
  233. // Fetch the next input unit, if there is one.
  234. if(sLength<0) {
  235. if((uchar=*s++)==0) {
  236. return result;
  237. }
  238. } else {
  239. if(sLength==0) {
  240. return result;
  241. }
  242. uchar=*s++;
  243. --sLength;
  244. }
  245. if(result==USTRINGTRIE_FINAL_VALUE) {
  246. // No further matching units.
  247. stop();
  248. return USTRINGTRIE_NO_MATCH;
  249. }
  250. pos=pos_; // branchNext() advanced pos and wrote it to pos_ .
  251. node=*pos++;
  252. } else if(node<kMinValueLead) {
  253. // Match length+1 units.
  254. length=node-kMinLinearMatch; // Actual match length minus 1.
  255. if(uchar!=*pos) {
  256. stop();
  257. return USTRINGTRIE_NO_MATCH;
  258. }
  259. ++pos;
  260. --length;
  261. break;
  262. } else if(node&kValueIsFinal) {
  263. // No further matching units.
  264. stop();
  265. return USTRINGTRIE_NO_MATCH;
  266. } else {
  267. // Skip intermediate value.
  268. pos=skipNodeValue(pos, node);
  269. node&=kNodeTypeMask;
  270. }
  271. }
  272. }
  273. }
  274. const char16_t *
  275. UCharsTrie::findUniqueValueFromBranch(const char16_t *pos, int32_t length,
  276. UBool haveUniqueValue, int32_t &uniqueValue) {
  277. while(length>kMaxBranchLinearSubNodeLength) {
  278. ++pos; // ignore the comparison unit
  279. if(nullptr==findUniqueValueFromBranch(jumpByDelta(pos), length>>1, haveUniqueValue, uniqueValue)) {
  280. return nullptr;
  281. }
  282. length=length-(length>>1);
  283. pos=skipDelta(pos);
  284. }
  285. do {
  286. ++pos; // ignore a comparison unit
  287. // handle its value
  288. int32_t node=*pos++;
  289. UBool isFinal=(UBool)(node>>15);
  290. node&=0x7fff;
  291. int32_t value=readValue(pos, node);
  292. pos=skipValue(pos, node);
  293. if(isFinal) {
  294. if(haveUniqueValue) {
  295. if(value!=uniqueValue) {
  296. return nullptr;
  297. }
  298. } else {
  299. uniqueValue=value;
  300. haveUniqueValue=true;
  301. }
  302. } else {
  303. if(!findUniqueValue(pos+value, haveUniqueValue, uniqueValue)) {
  304. return nullptr;
  305. }
  306. haveUniqueValue=true;
  307. }
  308. } while(--length>1);
  309. return pos+1; // ignore the last comparison unit
  310. }
  311. UBool
  312. UCharsTrie::findUniqueValue(const char16_t *pos, UBool haveUniqueValue, int32_t &uniqueValue) {
  313. int32_t node=*pos++;
  314. for(;;) {
  315. if(node<kMinLinearMatch) {
  316. if(node==0) {
  317. node=*pos++;
  318. }
  319. pos=findUniqueValueFromBranch(pos, node+1, haveUniqueValue, uniqueValue);
  320. if(pos==nullptr) {
  321. return false;
  322. }
  323. haveUniqueValue=true;
  324. node=*pos++;
  325. } else if(node<kMinValueLead) {
  326. // linear-match node
  327. pos+=node-kMinLinearMatch+1; // Ignore the match units.
  328. node=*pos++;
  329. } else {
  330. UBool isFinal=(UBool)(node>>15);
  331. int32_t value;
  332. if(isFinal) {
  333. value=readValue(pos, node&0x7fff);
  334. } else {
  335. value=readNodeValue(pos, node);
  336. }
  337. if(haveUniqueValue) {
  338. if(value!=uniqueValue) {
  339. return false;
  340. }
  341. } else {
  342. uniqueValue=value;
  343. haveUniqueValue=true;
  344. }
  345. if(isFinal) {
  346. return true;
  347. }
  348. pos=skipNodeValue(pos, node);
  349. node&=kNodeTypeMask;
  350. }
  351. }
  352. }
  353. int32_t
  354. UCharsTrie::getNextUChars(Appendable &out) const {
  355. const char16_t *pos=pos_;
  356. if(pos==nullptr) {
  357. return 0;
  358. }
  359. if(remainingMatchLength_>=0) {
  360. out.appendCodeUnit(*pos); // Next unit of a pending linear-match node.
  361. return 1;
  362. }
  363. int32_t node=*pos++;
  364. if(node>=kMinValueLead) {
  365. if(node&kValueIsFinal) {
  366. return 0;
  367. } else {
  368. pos=skipNodeValue(pos, node);
  369. node&=kNodeTypeMask;
  370. }
  371. }
  372. if(node<kMinLinearMatch) {
  373. if(node==0) {
  374. node=*pos++;
  375. }
  376. out.reserveAppendCapacity(++node);
  377. getNextBranchUChars(pos, node, out);
  378. return node;
  379. } else {
  380. // First unit of the linear-match node.
  381. out.appendCodeUnit(*pos);
  382. return 1;
  383. }
  384. }
  385. void
  386. UCharsTrie::getNextBranchUChars(const char16_t *pos, int32_t length, Appendable &out) {
  387. while(length>kMaxBranchLinearSubNodeLength) {
  388. ++pos; // ignore the comparison unit
  389. getNextBranchUChars(jumpByDelta(pos), length>>1, out);
  390. length=length-(length>>1);
  391. pos=skipDelta(pos);
  392. }
  393. do {
  394. out.appendCodeUnit(*pos++);
  395. pos=skipValue(pos);
  396. } while(--length>1);
  397. out.appendCodeUnit(*pos);
  398. }
  399. U_NAMESPACE_END