strcoll_l.c 11 KB

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