1 /* Copyright (C) 1995-1997,2002,2004-2006,2010,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
27 #include <sys/param.h>
30 # define STRING_TYPE char
31 # define USTRING_TYPE unsigned char
32 # define STRXFRM __strxfrm_l
33 # define STRCMP strcmp
34 # define STRLEN strlen
35 # define STPNCPY __stpncpy
36 # define WEIGHT_H "../locale/weight.h"
41 #define CONCAT(a,b) CONCAT1(a,b)
42 #define CONCAT1(a,b) a##b
44 #include "../locale/localeinfo.h"
47 #ifndef WIDE_CHAR_VERSION
49 /* We need UTF-8 encoding of numbers. */
51 utf8_encode (char *buf
, int val
)
64 for (step
= 2; step
< 6; ++step
)
65 if ((val
& (~(uint32_t)0 << (5 * step
+ 1))) == 0)
69 *buf
= (unsigned char) (~0xff >> step
);
73 buf
[step
] = 0x80 | (val
& 0x3f);
86 STRXFRM (STRING_TYPE
*dest
, const STRING_TYPE
*src
, size_t n
, __locale_t l
)
88 struct __locale_data
*current
= l
->__locales
[LC_COLLATE
];
89 uint_fast32_t nrules
= current
->values
[_NL_ITEM_INDEX (_NL_COLLATE_NRULES
)].word
;
90 /* We don't assign the following values right away since it might be
91 unnecessary in case there are no rules. */
92 const unsigned char *rulesets
;
94 const USTRING_TYPE
*weights
;
95 const USTRING_TYPE
*extra
;
96 const int32_t *indirect
;
100 const USTRING_TYPE
*usrc
;
101 size_t srclen
= STRLEN (src
);
103 unsigned char *rulearr
;
113 STPNCPY (dest
, src
, MIN (srclen
+ 1, n
));
118 rulesets
= (const unsigned char *)
119 current
->values
[_NL_ITEM_INDEX (_NL_COLLATE_RULESETS
)].string
;
120 table
= (const int32_t *)
121 current
->values
[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_TABLE
,SUFFIX
))].string
;
122 weights
= (const USTRING_TYPE
*)
123 current
->values
[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_WEIGHT
,SUFFIX
))].string
;
124 extra
= (const USTRING_TYPE
*)
125 current
->values
[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_EXTRA
,SUFFIX
))].string
;
126 indirect
= (const int32_t *)
127 current
->values
[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_INDIRECT
,SUFFIX
))].string
;
130 assert (((uintptr_t) table
) % __alignof__ (table
[0]) == 0);
131 assert (((uintptr_t) weights
) % __alignof__ (weights
[0]) == 0);
132 assert (((uintptr_t) extra
) % __alignof__ (extra
[0]) == 0);
133 assert (((uintptr_t) indirect
) % __alignof__ (indirect
[0]) == 0);
135 /* Handle an empty string as a special case. */
143 /* We need the elements of the string as unsigned values since they
144 are used as indeces. */
145 usrc
= (const USTRING_TYPE
*) src
;
147 /* Perform the first pass over the string and while doing this find
148 and store the weights for each character. Since we want this to
149 be as fast as possible we are using `alloca' to store the temporary
150 values. But since there is no limit on the length of the string
151 we have to use `malloc' if the string is too long. We should be
152 very conservative here. */
153 if (! __libc_use_alloca ((srclen
+ 1) * (sizeof (int32_t) + 1)))
155 idxarr
= (int32_t *) malloc ((srclen
+ 1) * (sizeof (int32_t) + 1));
156 rulearr
= (unsigned char *) &idxarr
[srclen
];
159 /* No memory. Well, go with the stack then.
161 XXX Once this implementation is stable we will handle this
162 differently. Instead of precomputing the indeces we will
163 do this in time. This means, though, that this happens for
171 idxarr
= (int32_t *) alloca (srclen
* sizeof (int32_t));
172 rulearr
= (unsigned char *) alloca (srclen
+ 1);
178 int32_t tmp
= findidx (&usrc
, -1);
179 rulearr
[idxmax
] = tmp
>> 24;
180 idxarr
[idxmax
] = tmp
& 0xffffff;
184 while (*usrc
!= L('\0'));
186 /* This element is only read, the value never used but to determine
187 another value which then is ignored. */
188 rulearr
[idxmax
] = '\0';
190 /* Now the passes over the weights. We now use the indeces we found
193 for (pass
= 0; pass
< nrules
; ++pass
)
195 size_t backw_stop
= ~0ul;
196 int rule
= rulesets
[rulearr
[0] * nrules
+ pass
];
197 /* We assume that if a rule has defined `position' in one section
198 this is true for all of them. */
199 int position
= rule
& sort_position
;
201 last_needed
= needed
;
204 for (idxcnt
= 0; idxcnt
< idxmax
; ++idxcnt
)
206 if ((rule
& sort_forward
) != 0)
210 if (backw_stop
!= ~0ul)
212 /* Handle the pushed elements now. */
215 for (backw
= idxcnt
; backw
> backw_stop
; )
218 len
= weights
[idxarr
[backw
]++];
220 if (needed
+ len
< n
)
222 dest
[needed
++] = weights
[idxarr
[backw
]++];
225 /* No more characters fit into the buffer. */
227 idxarr
[backw
] += len
;
234 /* Now handle the forward element. */
235 len
= weights
[idxarr
[idxcnt
]++];
236 if (needed
+ len
< n
)
238 dest
[needed
++] = weights
[idxarr
[idxcnt
]++];
241 /* No more characters fit into the buffer. */
243 idxarr
[idxcnt
] += len
;
248 /* Remember where the backwards series started. */
249 if (backw_stop
== ~0ul)
253 rule
= rulesets
[rulearr
[idxcnt
+ 1] * nrules
+ pass
];
257 if (backw_stop
!= ~0ul)
259 /* Handle the pushed elements now. */
263 while (backw
> backw_stop
)
265 size_t len
= weights
[idxarr
[--backw
]++];
267 if (needed
+ len
< n
)
269 dest
[needed
++] = weights
[idxarr
[backw
]++];
272 /* No more characters fit into the buffer. */
274 idxarr
[backw
] += len
;
282 #ifndef WIDE_CHAR_VERSION
288 for (idxcnt
= 0; idxcnt
< idxmax
; ++idxcnt
)
290 if ((rule
& sort_forward
) != 0)
294 if (backw_stop
!= ~0ul)
296 /* Handle the pushed elements now. */
299 for (backw
= idxcnt
; backw
> backw_stop
; )
302 len
= weights
[idxarr
[backw
]++];
305 #ifdef WIDE_CHAR_VERSION
306 if (needed
+ 1 + len
< n
)
309 for (i
= 0; i
< len
; ++i
)
310 dest
[needed
+ 1 + i
] =
311 weights
[idxarr
[backw
] + i
];
315 buflen
= utf8_encode (buf
, val
);
316 if (needed
+ buflen
+ len
< n
)
318 for (i
= 0; i
< buflen
; ++i
)
319 dest
[needed
+ i
] = buf
[i
];
320 for (i
= 0; i
< len
; ++i
)
321 dest
[needed
+ buflen
+ i
] =
322 weights
[idxarr
[backw
] + i
];
324 needed
+= buflen
+ len
;
326 idxarr
[backw
] += len
;
336 /* Now handle the forward element. */
337 len
= weights
[idxarr
[idxcnt
]++];
340 #ifdef WIDE_CHAR_VERSION
341 if (needed
+ 1+ len
< n
)
344 for (i
= 0; i
< len
; ++i
)
345 dest
[needed
+ 1 + i
] =
346 weights
[idxarr
[idxcnt
] + i
];
350 buflen
= utf8_encode (buf
, val
);
351 if (needed
+ buflen
+ len
< n
)
353 for (i
= 0; i
< buflen
; ++i
)
354 dest
[needed
+ i
] = buf
[i
];
355 for (i
= 0; i
< len
; ++i
)
356 dest
[needed
+ buflen
+ i
] =
357 weights
[idxarr
[idxcnt
] + i
];
359 needed
+= buflen
+ len
;
361 idxarr
[idxcnt
] += len
;
365 /* Note that we don't have to increment `idxarr[idxcnt]'
366 since the length is zero. */
371 /* Remember where the backwards series started. */
372 if (backw_stop
== ~0ul)
376 rule
= rulesets
[rulearr
[idxcnt
+ 1] * nrules
+ pass
];
379 if (backw_stop
!= ~0ul)
381 /* Handle the pushed elements now. */
385 while (backw
> backw_stop
)
387 size_t len
= weights
[idxarr
[--backw
]++];
390 #ifdef WIDE_CHAR_VERSION
391 if (needed
+ 1 + len
< n
)
394 for (i
= 0; i
< len
; ++i
)
395 dest
[needed
+ 1 + i
] =
396 weights
[idxarr
[backw
] + i
];
400 buflen
= utf8_encode (buf
, val
);
401 if (needed
+ buflen
+ len
< n
)
403 for (i
= 0; i
< buflen
; ++i
)
404 dest
[needed
+ i
] = buf
[i
];
405 for (i
= 0; i
< len
; ++i
)
406 dest
[needed
+ buflen
+ i
] =
407 weights
[idxarr
[backw
] + i
];
409 needed
+= buflen
+ len
;
411 idxarr
[backw
] += len
;
420 /* Finally store the byte to separate the passes or terminate
423 dest
[needed
] = pass
+ 1 < nrules
? L('\1') : L('\0');
427 /* This is a little optimization: many collation specifications have
428 a `position' rule at the end and if no non-ignored character
429 is found the last \1 byte is immediately followed by a \0 byte
430 signalling this. We can avoid the \1 byte(s). */
431 if (needed
> 2 && needed
== last_needed
+ 1)
433 /* Remove the \1 byte. */
435 dest
[needed
- 1] = L('\0');
438 /* Free the memory if needed. */
442 /* Return the number of bytes/words we need, but don't count the NUL
443 byte/word at the end. */
446 libc_hidden_def (STRXFRM
)
448 #ifndef WIDE_CHAR_VERSION
449 weak_alias (__strxfrm_l
, strxfrm_l
)