Add wcstol() and wcstoul(), based on strtol() and strtoul().

This commit is contained in:
Tim J. Robbins 2002-09-08 13:27:26 +00:00
parent 524cb6766e
commit 5fd1762445
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103097
5 changed files with 317 additions and 4 deletions

View File

@ -148,6 +148,9 @@ wchar_t *wcsstr(const wchar_t * __restrict, const wchar_t * __restrict);
int wctob(wint_t);
wchar_t *wcstok(wchar_t * __restrict, const wchar_t * __restrict,
wchar_t ** __restrict);
long wcstol(const wchar_t * __restrict, wchar_t ** __restrict, int);
unsigned long
wcstoul(const wchar_t * __restrict, wchar_t ** __restrict, int);
wchar_t *wmemchr(const wchar_t *, wchar_t, size_t);
int wmemcmp(const wchar_t *, const wchar_t *, size_t);
wchar_t *wmemcpy(wchar_t * __restrict, const wchar_t * __restrict, size_t);

View File

@ -11,8 +11,8 @@ SRCS+= big5.c btowc.c collate.c collcmp.c euc.c fix_grouping.c frune.c \
mbrtowc.c mbrune.c mbsinit.c mbsrtowcs.c mbtowc.c mbstowcs.c \
mskanji.c nl_langinfo.c nomacros.c none.c rune.c \
runetype.c setinvalidrune.c setlocale.c setrunelocale.c table.c \
tolower.c toupper.c utf2.c wcrtomb.c wcsrtombs.c wcstombs.c \
wctob.c wctomb.c wctrans.c wctype.c wcwidth.c
tolower.c toupper.c utf2.c wcrtomb.c wcsrtombs.c wcstol.c wcstombs.c \
wcstoul.c wctob.c wctomb.c wctrans.c wctype.c wcwidth.c
.if ${LIB} == "c"
MAN+= btowc.3 \
@ -22,8 +22,8 @@ MAN+= btowc.3 \
mbsrtowcs.3 multibyte.3 \
nl_langinfo.3 \
rune.3 \
setlocale.3 toascii.3 tolower.3 toupper.3 towlower.3 wcsrtombs.3 \
wcrtomb.3 \
setlocale.3 toascii.3 tolower.3 toupper.3 towlower.3 wcrtomb.3 \
wcsrtombs.3 wcstol.3 \
wctrans.3 wctype.3 wcwidth.3
MAN+= euc.4 utf2.4
@ -45,6 +45,7 @@ MLINKS+=rune.3 fgetrune.3 rune.3 fputrune.3 rune.3 fungetrune.3 \
rune.3 sputrune.3
MLINKS+=setlocale.3 localeconv.3
MLINKS+=towlower.3 towupper.3
MLINKS+=wcstol.3 wcstoul.3
MLINKS+=wctrans.3 towctrans.3
MLINKS+=wctype.3 iswctype.3
.endif

69
lib/libc/locale/wcstol.3 Normal file
View File

@ -0,0 +1,69 @@
.\" Copyright (c) 2002 Tim J. Robbins
.\" 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.
.\"
.\" $FreeBSD$
.\"
.Dd September 7, 2002
.Dt WCSTOL 3
.Os
.Sh NAME
.Nm wcstol , wcstoul
.Nd "convert a wide character string value to a"
.Vt long
or
.Vt "unsigned long"
integer
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In wchar.h
.Ft long
.Fn wcstol "const wchar_t * restrict nptr" "wchar_t * restrict endptr" "int base"
.Ft "unsigned long"
.Fn wcstoul "const wchar_t * restrict nptr" "wchar_t * restrict endptr" "int base"
.Sh DESCRIPTION
The
.Fn wcstol
and
.Fn wcstoul
functions are wide-character versions of the
.Fn strtol
and
.Fn strtoul
functions.
Refer to
.Xr strtol 3
and
.Xr strtoul 3
for details.
.Sh SEE ALSO
.Xr strtol 3 ,
.Xr strtoul 3
.Sh STANDARDS
The
.Fn wcstol
and
.Fn wcstoul
functions conform to
.St -isoC-99 .

121
lib/libc/locale/wcstol.c Normal file
View File

@ -0,0 +1,121 @@
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <wchar.h>
#include <wctype.h>
/*
* Convert a string to a long integer.
*/
long
wcstol(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, int base)
{
const wchar_t *s;
unsigned long acc;
wchar_t c;
unsigned long cutoff;
int neg, any, cutlim;
/*
* See strtol for comments as to the logic used.
*/
s = nptr;
do {
c = *s++;
} while (iswspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == L'+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == L'0' && (*s == L'x' || *s == L'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == L'0' ? 8 : 10;
acc = any = 0;
if (base < 2 || base > 36)
goto noconv;
cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX
: LONG_MAX;
cutlim = cutoff % base;
cutoff /= base;
for ( ; ; c = *s++) {
#ifdef notyet
if (iswdigit(c))
c = digittoint(c);
else
#endif
if (c >= L'0' && c <= L'9')
c -= L'0';
else if (c >= L'A' && c <= L'Z')
c -= L'A' - 10;
else if (c >= L'a' && c <= L'z')
c -= L'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = neg ? LONG_MIN : LONG_MAX;
errno = ERANGE;
} else if (!any) {
noconv:
errno = EINVAL;
} else if (neg)
acc = -acc;
if (endptr != NULL)
*endptr = (wchar_t *)(any ? s - 1 : nptr);
return (acc);
}

119
lib/libc/locale/wcstoul.c Normal file
View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 1990, 1993
* The Regents of the University of California. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <wchar.h>
#include <wctype.h>
/*
* Convert a wide character string to an unsigned long integer.
*/
unsigned long
wcstoul(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, int base)
{
const wchar_t *s;
unsigned long acc;
wchar_t c;
unsigned long cutoff;
int neg, any, cutlim;
/*
* See strtol for comments as to the logic used.
*/
s = nptr;
do {
c = *s++;
} while (iswspace(c));
if (c == L'-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == L'+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == L'0' && (*s == L'x' || *s == L'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == L'0' ? 8 : 10;
acc = any = 0;
if (base < 2 || base > 36)
goto noconv;
cutoff = ULONG_MAX / base;
cutlim = ULONG_MAX % base;
for ( ; ; c = *s++) {
#ifdef notyet
if (iswdigit(c))
c = digittoint(c);
else
#endif
if (c >= L'0' && c <= L'9')
c -= L'0';
else if (c >= L'A' && c <= L'Z')
c -= L'A' - 10;
else if (c >= L'a' && c <= L'z')
c -= L'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = ULONG_MAX;
errno = ERANGE;
} else if (!any) {
noconv:
errno = EINVAL;
} else if (neg)
acc = -acc;
if (endptr != NULL)
*endptr = (wchar_t *)(any ? s - 1 : nptr);
return (acc);
}