From 7a2a491fdf72a1648e6b138a8eefd05ab81f65a0 Mon Sep 17 00:00:00 2001 From: John Marino Date: Mon, 21 Nov 2011 23:30:09 +0100 Subject: [PATCH] libc: Add wcsncasecmp function This function performs a case-insensitive string comparison test of not more than a specified number of wide characters. It is a GNU extension, not POSIX. Some packages in pkgsrc may require it. --- include/wchar.h | 1 + lib/libc/string/Makefile.inc | 2 ++ lib/libc/string/wcsncasecmp.c | 46 +++++++++++++++++++++++++++++++++++ lib/libc/string/wmemchr.3 | 4 +++ 4 files changed, 53 insertions(+) create mode 100644 lib/libc/string/wcsncasecmp.c diff --git a/include/wchar.h b/include/wchar.h index 9a7e0277d0..3f602d2aed 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -134,6 +134,7 @@ 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 *); +int wcsncasecmp(const wchar_t *, const wchar_t *, size_t n); 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 c6b60b042c..7b3168da36 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -19,6 +19,7 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \ wcslcat.c wcslcpy.c wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcsnlen.c \ wcspbrk.c \ wcscasecmp.c \ + wcsncasecmp.c \ wcsrchr.c wcsspn.c wcsstr.c wcstok.c wcswidth.c wcsxfrm.c wmemchr.c \ wmemcmp.c \ wmemcpy.c wmemmove.c wmemset.c @@ -77,6 +78,7 @@ MLINKS+=wmemchr.3 wcscat.3 \ wmemchr.3 wcsspn.3 \ wmemchr.3 wcsstr.3 \ wmemchr.3 wcscasecmp.3 \ + wmemchr.3 wcsncasecmp.3 \ wmemchr.3 wmemcmp.3 \ wmemchr.3 wmemcpy.3 \ wmemchr.3 wmemmove.3 \ diff --git a/lib/libc/string/wcsncasecmp.c b/lib/libc/string/wcsncasecmp.c new file mode 100644 index 0000000000..820068dddb --- /dev/null +++ b/lib/libc/string/wcsncasecmp.c @@ -0,0 +1,46 @@ +/*- + * 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 +wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n) +{ + wchar_t c1, c2; + + if (n == 0) + return (0); + for (; *s1; s1++, s2++) { + c1 = towlower(*s1); + c2 = towlower(*s2); + if (c1 != c2) + return ((int)c1 - c2); + if (--n == 0) + return (0); + } + return (-*s2); +} diff --git a/lib/libc/string/wmemchr.3 b/lib/libc/string/wmemchr.3 index f26abc7714..d3d43d4a91 100644 --- a/lib/libc/string/wmemchr.3 +++ b/lib/libc/string/wmemchr.3 @@ -55,6 +55,7 @@ .Nm wcslcat , .Nm wcslcpy , .Nm wcslen , +.Nm wcsncasecmp , .Nm wcsncat , .Nm wcsncmp , .Nm wcsncpy , @@ -98,6 +99,8 @@ .Fn wcslcpy "wchar_t *s1" "const wchar_t *s2" "size_t n" .Ft size_t .Fn wcslen "const wchar_t *s" +.Ft int +.Fn wcsncasecmp "const wchar_t *s1" "const wchar_t *s2" "size_t n" .Ft wchar_t * .Fn wcsncat "wchar_t * restrict s1" "const wchar_t * restrict s2" "size_t n" .Ft int @@ -151,6 +154,7 @@ These functions conform to with the exception of .Fn wcscasecmp , .Fn wcsdup , +.Fn wcsncasecmp , and .Fn wcsnlen , which conform to -- 2.41.0