blob: 703d08fa15076ede8b1798e3be9c6923599a716a [file] [log] [blame]
Elliott Hughesd382b862025-07-17 12:13:13 -07001/*
2 * Copyright (C) 2025 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <ctype.h>
30#include <stddef.h>
31#include <string.h>
32#include <wchar.h>
33#include <wctype.h>
34
35// We don't use the llvm-libc routines because they don't just call out to
36// tolower()/towlower(), preferring their own implementations, which don't
37// match ours.
38
39// We can also make all the _l() variants simple aliases for the regular
40// functions, since bionic doesn't implement locales (because if you're
41// serious about i18n, you want icu anyway).
42
43template <typename CharT, typename MapperFn>
44int CaseCmp(const CharT* lhs, const CharT* rhs, MapperFn mapper) {
45 while (*lhs && mapper(*lhs) == mapper(*rhs)) {
46 lhs++;
47 rhs++;
48 }
49 return static_cast<unsigned>(mapper(*lhs)) - static_cast<unsigned>(mapper(*rhs));
50}
51
52template <typename CharT, typename MapperFn>
53int CaseCmp(const CharT* lhs, const CharT* rhs, size_t n, MapperFn mapper) {
54 if (n-- == 0) return 0;
55 while (*lhs && *rhs && n > 0 && mapper(*lhs) == mapper(*rhs)) {
56 if (*lhs == '\0') return 0;
57 lhs++;
58 rhs++;
59 n--;
60 }
61 return static_cast<unsigned>(mapper(*lhs)) - static_cast<unsigned>(mapper(*rhs));
62}
63
64int strcasecmp(const char* lhs, const char* rhs) {
65 return CaseCmp(lhs, rhs, tolower);
66}
67__strong_alias(strcasecmp_l, strcasecmp);
68
69int strncasecmp(const char* lhs, const char* rhs, size_t n) {
70 return CaseCmp(lhs, rhs, n, tolower);
71}
72__strong_alias(strncasecmp_l, strncasecmp);
73
74int wcscasecmp(const wchar_t* lhs, const wchar_t* rhs) {
75 return CaseCmp(lhs, rhs, towlower);
76}
77__strong_alias(wcscasecmp_l, wcscasecmp);
78
79int wcsncasecmp(const wchar_t* lhs, const wchar_t* rhs, size_t n) {
80 return CaseCmp(lhs, rhs, n, towlower);
81}
82__strong_alias(wcsncasecmp_l, wcsncasecmp);