Skip to content

Commit

Permalink
add wcsnlen()
Browse files Browse the repository at this point in the history
wcsnlen() is strnlen()'s counterpart for wide-character strings. both
were added to ieee std 1003.1 in issue 7.

from freebsd

XXX documentation missing!
  • Loading branch information
martelletto authored and drahn committed Apr 20, 2014
1 parent 33aeeb8 commit 0d91f60
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/wchar.h
Expand Up @@ -154,6 +154,7 @@ int wcsncasecmp(const wchar_t *, const wchar_t *, size_t);

size_t mbsnrtowcs(wchar_t * __restrict, const char ** __restrict, size_t,
size_t, mbstate_t * __restrict);
size_t wcsnlen(const wchar_t *, size_t);
size_t wcsnrtombs(char * __restrict, const wchar_t ** __restrict, size_t,
size_t, mbstate_t * __restrict);
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/libc/string/Makefile.inc
Expand Up @@ -11,7 +11,7 @@ SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \
wcslcpy.c wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c \
wcsspn.c wcsstr.c wcstok.c wcswcs.c wcswidth.c wcsxfrm.c wmemchr.c \
wmemcmp.c wmemcpy.c wmemmove.c wmemset.c wcsdup.c \
timingsafe_bcmp.c wcscasecmp.c wcsncasecmp.c
timingsafe_bcmp.c wcscasecmp.c wcsncasecmp.c wcsnlen.c

# machine-dependent net sources
# m-d Makefile.inc must include sources for:
Expand Down
39 changes: 39 additions & 0 deletions lib/libc/string/wcsnlen.c
@@ -0,0 +1,39 @@
/*-
* Copyright (c) 2009 David Schultz <das@FreeBSD.org>
* 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 <wchar.h>

size_t
wcsnlen(const wchar_t *s, size_t maxlen)
{
size_t len;

for (len = 0; len < maxlen; len++, s++) {
if (!*s)
break;
}
return (len);
}

0 comments on commit 0d91f60

Please sign in to comment.