2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
5 * at Electronni Visti IA, Kiev, Ukraine.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "localeimpl.h"
41 #define ALLOCA_LIMIT 16
44 * In order to properly handle multibyte locales, its easiet to just
45 * convert to wide characters and then use wcscoll. However if an
46 * error occurs, we gracefully fall back to simple strcmp. Caller
50 strcoll_l(const char *s1
, const char *s2
, locale_t loc
)
53 wchar_t *t1
= NULL
, *t2
= NULL
;
54 wchar_t *w1
= NULL
, *w2
= NULL
;
56 const struct lc_collate
*lcc
= loc
->collate
;
59 return (strcmp(s1
, s2
));
65 * Simple assumption: conversion to wide format is strictly
66 * reducing, i.e. a single byte (or multibyte character)
67 * cannot result in multiple wide characters.
69 * We gain a bit of performance by giving preference to alloca
70 * for small string allocations.
72 if (sz1
> ALLOCA_LIMIT
) {
73 if ((t1
= malloc(sz1
* sizeof (wchar_t))) == NULL
)
77 if ((w1
= alloca(sz1
* sizeof (wchar_t))) == NULL
)
80 if (sz2
> ALLOCA_LIMIT
) {
81 if ((t2
= malloc(sz2
* sizeof (wchar_t))) == NULL
)
85 if ((w2
= alloca(sz2
* sizeof (wchar_t))) == NULL
)
89 if ((mbstowcs_l(w1
, s1
, sz1
, loc
)) == (size_t)-1)
92 if ((mbstowcs_l(w2
, s2
, sz2
, loc
)) == (size_t)-1)
95 ret
= wcscoll_l(w1
, w2
, loc
);
108 return (strcmp(s1
, s2
));
112 strcoll(const char *s1
, const char *s2
)
114 return (strcoll_l(s1
, s2
, uselocale(NULL
)));