xref: /openbsd-src/lib/libc/string/wcscasecmp_l.c (revision 3a628b46e7aaa520a6215eccabf31d313c2e7de0)
1*3a628b46Sschwarze /*	$OpenBSD: wcscasecmp_l.c,v 1.1 2017/09/05 03:16:14 schwarze Exp $ */
2*3a628b46Sschwarze 
3*3a628b46Sschwarze /*
4*3a628b46Sschwarze  * Copyright (c) 2011 Marc Espie
5*3a628b46Sschwarze  *
6*3a628b46Sschwarze  * Redistribution and use in source and binary forms, with or without
7*3a628b46Sschwarze  * modification, are permitted provided that the following conditions
8*3a628b46Sschwarze  * are met:
9*3a628b46Sschwarze  * 1. Redistributions of source code must retain the above copyright
10*3a628b46Sschwarze  *    notice, this list of conditions and the following disclaimer.
11*3a628b46Sschwarze  * 2. Redistributions in binary form must reproduce the above copyright
12*3a628b46Sschwarze  *    notice, this list of conditions and the following disclaimer in the
13*3a628b46Sschwarze  *    documentation and/or other materials provided with the distribution.
14*3a628b46Sschwarze  *
15*3a628b46Sschwarze  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16*3a628b46Sschwarze  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*3a628b46Sschwarze  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18*3a628b46Sschwarze  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
19*3a628b46Sschwarze  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20*3a628b46Sschwarze  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21*3a628b46Sschwarze  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*3a628b46Sschwarze  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*3a628b46Sschwarze  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*3a628b46Sschwarze  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25*3a628b46Sschwarze  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*3a628b46Sschwarze  */
27*3a628b46Sschwarze 
28*3a628b46Sschwarze #include <wchar.h>
29*3a628b46Sschwarze #include <wctype.h>
30*3a628b46Sschwarze #include "locale/runetype.h"
31*3a628b46Sschwarze 
32*3a628b46Sschwarze int
wcscasecmp_l(const wchar_t * s1,const wchar_t * s2,locale_t locale)33*3a628b46Sschwarze wcscasecmp_l(const wchar_t *s1, const wchar_t *s2, locale_t locale)
34*3a628b46Sschwarze {
35*3a628b46Sschwarze 	wchar_t l1, l2;
36*3a628b46Sschwarze 
37*3a628b46Sschwarze 	while ((l1 = towlower_l(*s1++, locale)) ==
38*3a628b46Sschwarze 	    (l2 = towlower_l(*s2++, locale))) {
39*3a628b46Sschwarze 		if (l1 == 0)
40*3a628b46Sschwarze 			return (0);
41*3a628b46Sschwarze 	}
42*3a628b46Sschwarze 	/* XXX assumes wchar_t = int */
43*3a628b46Sschwarze 	return ((rune_t)l1 - (rune_t)l2);
44*3a628b46Sschwarze }
45*3a628b46Sschwarze 
46*3a628b46Sschwarze int
wcsncasecmp_l(const wchar_t * s1,const wchar_t * s2,size_t n,locale_t locale)47*3a628b46Sschwarze wcsncasecmp_l(const wchar_t *s1, const wchar_t *s2, size_t n, locale_t locale)
48*3a628b46Sschwarze {
49*3a628b46Sschwarze 	wchar_t l1, l2;
50*3a628b46Sschwarze 
51*3a628b46Sschwarze 	if (n == 0)
52*3a628b46Sschwarze 		return (0);
53*3a628b46Sschwarze 	do {
54*3a628b46Sschwarze 		if (((l1 = towlower_l(*s1++, locale))) !=
55*3a628b46Sschwarze 		    (l2 = towlower_l(*s2++, locale))) {
56*3a628b46Sschwarze 			/* XXX assumes wchar_t = int */
57*3a628b46Sschwarze 			return ((rune_t)l1 - (rune_t)l2);
58*3a628b46Sschwarze 		}
59*3a628b46Sschwarze 		if (l1 == 0)
60*3a628b46Sschwarze 			break;
61*3a628b46Sschwarze 	} while (--n != 0);
62*3a628b46Sschwarze 	return (0);
63*3a628b46Sschwarze }
64