1 /* Copyright (c) 2009 Corinna Vinschen <corinna@vinschen.de> */
2 /*
3 FUNCTION
4 	<<wcsncasecmp>>---case-insensitive wide character string compare
5 
6 INDEX
7 	wcsncasecmp
8 
9 SYNOPSIS
10 	#include <wchar.h>
11 	int wcsncasecmp(const wchar_t *<[a]>, const wchar_t * <[b]>, size_t <[length]>);
12 
13 DESCRIPTION
14 	<<wcsncasecmp>> compares up to <[length]> wide characters
15 	from the string at <[a]> to the string at <[b]> in a
16 	case-insensitive manner.
17 
18 RETURNS
19 
20 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>> (after
21 	both are converted to uppercase), <<wcsncasecmp>> returns a
22 	number greater than zero.  If the two strings are equivalent,
23 	<<wcsncasecmp>> returns zero.  If <<*<[a]>>> sorts
24 	lexicographically before <<*<[b]>>>, <<wcsncasecmp>> returns a
25 	number less than zero.
26 
27 PORTABILITY
28 POSIX-1.2008
29 
30 <<wcsncasecmp>> requires no supporting OS subroutines. It uses
31 tolower() from elsewhere in this library.
32 
33 QUICKREF
34 	wcsncasecmp
35 */
36 
37 #define _DEFAULT_SOURCE
38 #include <wchar.h>
39 #include <wctype.h>
40 
41 int
wcsncasecmp(const wchar_t * s1,const wchar_t * s2,size_t n)42 wcsncasecmp (const wchar_t *s1,
43 	const wchar_t *s2,
44 	size_t n)
45 {
46   int d = 0;
47   for ( ; n != 0; n--)
48     {
49       const int c1 = towlower (*s1++);
50       const int c2 = towlower (*s2++);
51       if (((d = c1 - c2) != 0) || (c2 == '\0'))
52         break;
53     }
54   return d;
55 }
56