From a231d5c7d0005ab35af9cfa29e859bc9a3a9cac9 Mon Sep 17 00:00:00 2001 From: John Marino Date: Mon, 21 Nov 2011 23:13:43 +0100 Subject: [PATCH] libc: Add wcscasecmp function This function performs a case-insensitive string comparison test on wide characters. It is a GNU extension, not POSIX. Some packages in pkgsrc require it. --- include/wchar.h | 1 + lib/libc/string/Makefile.inc | 2 ++ lib/libc/string/wcscasecmp.c | 42 ++++++++++++++++++++++++++++++++++++ lib/libc/string/wmemchr.3 | 6 +++++- 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/libc/string/wcscasecmp.c diff --git a/include/wchar.h b/include/wchar.h index 770332b750..9a7e0277d0 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -133,6 +133,7 @@ wchar_t *wcscpy(wchar_t * __restrict, const wchar_t * __restrict); size_t wcscspn(const wchar_t *, const wchar_t *); size_t wcsftime(wchar_t * __restrict, size_t, const wchar_t * __restrict, const struct tm * __restrict); +int wcscasecmp(const wchar_t *, const wchar_t *); size_t wcslen(const wchar_t *); wchar_t *wcsncat(wchar_t * __restrict, const wchar_t * __restrict, size_t); int wcsncmp(const wchar_t *, const wchar_t *, size_t); diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index cb52808237..c6b60b042c 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -18,6 +18,7 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \ wcscspn.c wcsdup.c \ wcslcat.c wcslcpy.c wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcsnlen.c \ wcspbrk.c \ + wcscasecmp.c \ wcsrchr.c wcsspn.c wcsstr.c wcstok.c wcswidth.c wcsxfrm.c wmemchr.c \ wmemcmp.c \ wmemcpy.c wmemmove.c wmemset.c @@ -75,6 +76,7 @@ MLINKS+=wmemchr.3 wcscat.3 \ wmemchr.3 wcsrchr.3 \ wmemchr.3 wcsspn.3 \ wmemchr.3 wcsstr.3 \ + wmemchr.3 wcscasecmp.3 \ wmemchr.3 wmemcmp.3 \ wmemchr.3 wmemcpy.3 \ wmemchr.3 wmemmove.3 \ diff --git a/lib/libc/string/wcscasecmp.c b/lib/libc/string/wcscasecmp.c new file mode 100644 index 0000000000..1f5630d96e --- /dev/null +++ b/lib/libc/string/wcscasecmp.c @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2009 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +int +wcscasecmp(const wchar_t *s1, const wchar_t *s2) +{ + wchar_t c1, c2; + + for (; *s1; s1++, s2++) { + c1 = towlower(*s1); + c2 = towlower(*s2); + if (c1 != c2) + return ((int)c1 - c2); + } + return (-*s2); +} diff --git a/lib/libc/string/wmemchr.3 b/lib/libc/string/wmemchr.3 index 5528943d2c..f26abc7714 100644 --- a/lib/libc/string/wmemchr.3 +++ b/lib/libc/string/wmemchr.3 @@ -36,7 +36,7 @@ .\" $FreeBSD: src/lib/libc/string/wmemchr.3,v 1.8 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/wmemchr.3,v 1.2 2003/06/17 04:26:47 dillon Exp $ .\" -.Dd April 23, 2009 +.Dd November 21, 2011 .Dt WMEMCHR 3 .Os .Sh NAME @@ -45,6 +45,7 @@ .Nm wmemcpy , .Nm wmemmove , .Nm wmemset , +.Nm wcscasecmp , .Nm wcscat , .Nm wcschr , .Nm wcscmp , @@ -77,6 +78,8 @@ .Fn wmemmove "wchar_t *s1" "const wchar_t *s2" "size_t n" .Ft wchar_t * .Fn wmemset "wchar_t *s" "wchar_t c" "size_t n" +.Ft int +.Fn wcscasecmp "const wchar_t *s1" "const wchar_t *s2" .Ft wchar_t * .Fn wcscat "wchar_t * restrict s1" "const wchar_t * restrict s2" .Ft wchar_t * @@ -146,6 +149,7 @@ counterpart, such as These functions conform to .St -isoC-99 , with the exception of +.Fn wcscasecmp , .Fn wcsdup , and .Fn wcsnlen , -- 2.41.0