bytestrie.cpp 14 KB

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