strcoll_l.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Copyright (C) 1995-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Written by Ulrich Drepper <drepper@gnu.org>, 1995.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <langinfo.h>
  17. #include <locale.h>
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <sys/param.h>
  22. #include <libc-diag.h>
  23. #ifndef STRING_TYPE
  24. # define STRING_TYPE char
  25. # define USTRING_TYPE unsigned char
  26. # define STRCOLL __strcoll_l
  27. # define STRCMP strcmp
  28. # define WEIGHT_H "../locale/weight.h"
  29. # define SUFFIX MB
  30. # define L(arg) arg
  31. #endif
  32. #define CONCAT(a,b) CONCAT1(a,b)
  33. #define CONCAT1(a,b) a##b
  34. #include "../locale/localeinfo.h"
  35. #include WEIGHT_H
  36. /* Track status while looking for sequences in a string. */
  37. typedef struct
  38. {
  39. int len; /* Length of the current sequence. */
  40. size_t val; /* Position of the sequence relative to the
  41. previous non-ignored sequence. */
  42. size_t idxmax; /* Maximum index in sequences. */
  43. size_t idxcnt; /* Current count of indices. */
  44. size_t backw; /* Current Backward sequence index. */
  45. size_t backw_stop; /* Index where the backward sequences stop. */
  46. const USTRING_TYPE *us; /* The string. */
  47. unsigned char rule; /* Saved rule for the first sequence. */
  48. int32_t idx; /* Index to weight of the current sequence. */
  49. int32_t save_idx; /* Save looked up index of a forward
  50. sequence after the last backward
  51. sequence. */
  52. const USTRING_TYPE *back_us; /* Beginning of the backward sequence. */
  53. } coll_seq;
  54. /* Get next sequence. Traverse the string as required. */
  55. static __always_inline void
  56. get_next_seq (coll_seq *seq, int nrules, const unsigned char *rulesets,
  57. const USTRING_TYPE *weights, const int32_t *table,
  58. const USTRING_TYPE *extra, const int32_t *indirect,
  59. int pass)
  60. {
  61. size_t val = seq->val = 0;
  62. int len = seq->len;
  63. size_t backw_stop = seq->backw_stop;
  64. size_t backw = seq->backw;
  65. size_t idxcnt = seq->idxcnt;
  66. size_t idxmax = seq->idxmax;
  67. int32_t idx = seq->idx;
  68. const USTRING_TYPE *us = seq->us;
  69. while (len == 0)
  70. {
  71. ++val;
  72. if (backw_stop != ~0ul)
  73. {
  74. /* There is something pushed. */
  75. if (backw == backw_stop)
  76. {
  77. /* The last pushed character was handled. Continue
  78. with forward characters. */
  79. if (idxcnt < idxmax)
  80. {
  81. idx = seq->save_idx;
  82. backw_stop = ~0ul;
  83. }
  84. else
  85. {
  86. /* Nothing anymore. The backward sequence ended with
  87. the last sequence in the string. Note that len is
  88. still zero. */
  89. idx = 0;
  90. break;
  91. }
  92. }
  93. else
  94. {
  95. /* XXX Traverse BACKW sequences from the beginning of
  96. BACKW_STOP to get the next sequence. Is ther a quicker way
  97. to do this? */
  98. size_t i = backw_stop;
  99. us = seq->back_us;
  100. while (i < backw)
  101. {
  102. int32_t tmp = findidx (table, indirect, extra, &us, -1);
  103. idx = tmp & 0xffffff;
  104. i++;
  105. }
  106. --backw;
  107. us = seq->us;
  108. }
  109. }
  110. else
  111. {
  112. backw_stop = idxmax;
  113. int32_t prev_idx = idx;
  114. while (*us != L('\0'))
  115. {
  116. int32_t tmp = findidx (table, indirect, extra, &us, -1);
  117. unsigned char rule = tmp >> 24;
  118. prev_idx = idx;
  119. idx = tmp & 0xffffff;
  120. idxcnt = idxmax++;
  121. /* Save the rule for the first sequence. */
  122. if (__glibc_unlikely (idxcnt == 0))
  123. seq->rule = rule;
  124. if ((rulesets[rule * nrules + pass]
  125. & sort_backward) == 0)
  126. /* No more backward characters to push. */
  127. break;
  128. ++idxcnt;
  129. }
  130. if (backw_stop >= idxcnt)
  131. {
  132. /* No sequence at all or just one. */
  133. if (idxcnt == idxmax || backw_stop > idxcnt)
  134. /* Note that len is still zero. */
  135. break;
  136. backw_stop = ~0ul;
  137. }
  138. else
  139. {
  140. /* We pushed backward sequences. If the stream ended with the
  141. backward sequence, then we process the last sequence we
  142. found. Otherwise we process the sequence before the last
  143. one since the last one was a forward sequence. */
  144. seq->back_us = seq->us;
  145. seq->us = us;
  146. backw = idxcnt;
  147. if (idxmax > idxcnt)
  148. {
  149. backw--;
  150. seq->save_idx = idx;
  151. idx = prev_idx;
  152. }
  153. if (backw > backw_stop)
  154. backw--;
  155. }
  156. }
  157. /* With GCC 5.3 when compiling with -Os the compiler complains
  158. that idx, taken from seq->idx (seq1 or seq2 from STRCOLL) may
  159. be used uninitialized. In general this can't possibly be true
  160. since seq1.idx and seq2.idx are initialized to zero in the
  161. outer function. Only one case where seq->idx is restored from
  162. seq->save_idx might result in an uninitialized idx value, but
  163. it is guarded by a sequence of checks against backw_stop which
  164. ensures that seq->save_idx was saved to first and contains a
  165. valid value. */
  166. DIAG_PUSH_NEEDS_COMMENT;
  167. DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
  168. len = weights[idx++];
  169. DIAG_POP_NEEDS_COMMENT;
  170. /* Skip over indices of previous levels. */
  171. for (int i = 0; i < pass; i++)
  172. {
  173. idx += len;
  174. len = weights[idx];
  175. idx++;
  176. }
  177. }
  178. /* Update the structure. */
  179. seq->val = val;
  180. seq->len = len;
  181. seq->backw_stop = backw_stop;
  182. seq->backw = backw;
  183. seq->idxcnt = idxcnt;
  184. seq->idxmax = idxmax;
  185. seq->us = us;
  186. seq->idx = idx;
  187. }
  188. /* Compare two sequences. */
  189. static __always_inline int
  190. do_compare (coll_seq *seq1, coll_seq *seq2, int position,
  191. const USTRING_TYPE *weights)
  192. {
  193. int seq1len = seq1->len;
  194. int seq2len = seq2->len;
  195. size_t val1 = seq1->val;
  196. size_t val2 = seq2->val;
  197. int idx1 = seq1->idx;
  198. int idx2 = seq2->idx;
  199. int result = 0;
  200. /* Test for position if necessary. */
  201. if (position && val1 != val2)
  202. {
  203. result = val1 > val2 ? 1 : -1;
  204. goto out;
  205. }
  206. /* Compare the two sequences. */
  207. do
  208. {
  209. if (weights[idx1] != weights[idx2])
  210. {
  211. /* The sequences differ. */
  212. result = weights[idx1] - weights[idx2];
  213. goto out;
  214. }
  215. /* Increment the offsets. */
  216. ++idx1;
  217. ++idx2;
  218. --seq1len;
  219. --seq2len;
  220. }
  221. while (seq1len > 0 && seq2len > 0);
  222. if (position && seq1len != seq2len)
  223. result = seq1len - seq2len;
  224. out:
  225. seq1->len = seq1len;
  226. seq2->len = seq2len;
  227. seq1->idx = idx1;
  228. seq2->idx = idx2;
  229. return result;
  230. }
  231. int
  232. STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, locale_t l)
  233. {
  234. struct __locale_data *current = l->__locales[LC_COLLATE];
  235. uint_fast32_t nrules = current->values[_NL_ITEM_INDEX (_NL_COLLATE_NRULES)].word;
  236. /* We don't assign the following values right away since it might be
  237. unnecessary in case there are no rules. */
  238. const unsigned char *rulesets;
  239. const int32_t *table;
  240. const USTRING_TYPE *weights;
  241. const USTRING_TYPE *extra;
  242. const int32_t *indirect;
  243. if (nrules == 0)
  244. return STRCMP (s1, s2);
  245. /* Catch empty strings. */
  246. if (__glibc_unlikely (*s1 == '\0') || __glibc_unlikely (*s2 == '\0'))
  247. return (*s1 != '\0') - (*s2 != '\0');
  248. rulesets = (const unsigned char *)
  249. current->values[_NL_ITEM_INDEX (_NL_COLLATE_RULESETS)].string;
  250. table = (const int32_t *)
  251. current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_TABLE,SUFFIX))].string;
  252. weights = (const USTRING_TYPE *)
  253. current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_WEIGHT,SUFFIX))].string;
  254. extra = (const USTRING_TYPE *)
  255. current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_EXTRA,SUFFIX))].string;
  256. indirect = (const int32_t *)
  257. current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_INDIRECT,SUFFIX))].string;
  258. assert (((uintptr_t) table) % __alignof__ (table[0]) == 0);
  259. assert (((uintptr_t) weights) % __alignof__ (weights[0]) == 0);
  260. assert (((uintptr_t) extra) % __alignof__ (extra[0]) == 0);
  261. assert (((uintptr_t) indirect) % __alignof__ (indirect[0]) == 0);
  262. int result = 0, rule = 0;
  263. /* With GCC 7 when compiling with -Os the compiler warns that
  264. seq1.back_us and seq2.back_us might be used uninitialized.
  265. Sometimes this warning appears at locations in locale/weightwc.h
  266. where the actual use is, but on architectures other than x86_64,
  267. x86 and s390x, a warning appears at the definitions of seq1 and
  268. seq2. This uninitialized use is impossible for the same reason
  269. as described in comments in locale/weightwc.h. */
  270. DIAG_PUSH_NEEDS_COMMENT;
  271. DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
  272. coll_seq seq1, seq2;
  273. DIAG_POP_NEEDS_COMMENT;
  274. seq1.len = 0;
  275. seq1.idxmax = 0;
  276. seq1.rule = 0;
  277. seq2.len = 0;
  278. seq2.idxmax = 0;
  279. for (int pass = 0; pass < nrules; ++pass)
  280. {
  281. seq1.idxcnt = 0;
  282. seq1.idx = 0;
  283. seq2.idx = 0;
  284. seq1.backw_stop = ~0ul;
  285. seq1.backw = ~0ul;
  286. seq2.idxcnt = 0;
  287. seq2.backw_stop = ~0ul;
  288. seq2.backw = ~0ul;
  289. /* We need the elements of the strings as unsigned values since they
  290. are used as indices. */
  291. seq1.us = (const USTRING_TYPE *) s1;
  292. seq2.us = (const USTRING_TYPE *) s2;
  293. /* We assume that if a rule has defined `position' in one section
  294. this is true for all of them. Please note that the localedef programs
  295. makes sure that `position' is not used at the first level. */
  296. int position = rulesets[rule * nrules + pass] & sort_position;
  297. while (1)
  298. {
  299. get_next_seq (&seq1, nrules, rulesets, weights, table,
  300. extra, indirect, pass);
  301. get_next_seq (&seq2, nrules, rulesets, weights, table,
  302. extra, indirect, pass);
  303. /* See whether any or both strings are empty. */
  304. if (seq1.len == 0 || seq2.len == 0)
  305. {
  306. if (seq1.len == seq2.len)
  307. {
  308. /* Both strings ended and are equal at this level. Do a
  309. byte-level comparison to ensure that we don't waste time
  310. going through multiple passes for totally equal strings
  311. before proceeding to subsequent passes. */
  312. if (pass == 0 && STRCMP (s1, s2) == 0)
  313. return result;
  314. else
  315. break;
  316. }
  317. /* This means one string is shorter than the other. Find out
  318. which one and return an appropriate value. */
  319. return seq1.len == 0 ? -1 : 1;
  320. }
  321. result = do_compare (&seq1, &seq2, position, weights);
  322. if (result != 0)
  323. return result;
  324. }
  325. rule = seq1.rule;
  326. }
  327. return result;
  328. }
  329. libc_hidden_def (STRCOLL)
  330. #ifndef WIDE_CHAR_VERSION
  331. weak_alias (__strcoll_l, strcoll_l)
  332. #endif