Add the remaining C99 wide character string to integer conversion functions.

Restrict qualifiers were added to the existing prototypes in <inttypes.h>
and the typedef for wchar_t was removed.
This commit is contained in:
Tim J. Robbins 2002-09-22 08:06:45 +00:00
parent 04a42d5ee0
commit 1302dabd28
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103793
8 changed files with 558 additions and 26 deletions

View File

@ -32,13 +32,6 @@
#include <machine/_inttypes.h>
#include <sys/stdint.h>
#ifndef __cplusplus
#ifndef _WCHAR_T_DECLARED
typedef __wchar_t wchar_t;
#define _WCHAR_T_DECLARED
#endif
#endif
typedef struct {
intmax_t quot; /* Quotient. */
intmax_t rem; /* Remainder. */
@ -50,9 +43,10 @@ imaxdiv_t imaxdiv(intmax_t, intmax_t) __pure2;
intmax_t strtoimax(const char * __restrict, char ** __restrict, int);
uintmax_t strtoumax(const char * __restrict, char ** __restrict, int);
/* XXX: The following functions are missing the restrict type qualifier. */
intmax_t wcstoimax(const wchar_t *, wchar_t **, int);
uintmax_t wcstoumax(const wchar_t *, wchar_t **, int);
intmax_t wcstoimax(const __wchar_t * __restrict,
__wchar_t ** __restrict, int);
uintmax_t wcstoumax(const __wchar_t * __restrict,
__wchar_t ** __restrict, int);
__END_DECLS
#endif /* !_INTTYPES_H_ */

View File

@ -154,8 +154,12 @@ double wcstod(const wchar_t * __restrict, wchar_t ** __restrict);
wchar_t *wcstok(wchar_t * __restrict, const wchar_t * __restrict,
wchar_t ** __restrict);
long wcstol(const wchar_t * __restrict, wchar_t ** __restrict, int);
long long
wcstoll(const wchar_t * __restrict, wchar_t ** __restrict, int);
unsigned long
wcstoul(const wchar_t * __restrict, wchar_t ** __restrict, int);
unsigned long long
wcstoull(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

@ -12,9 +12,10 @@ SRCS+= big5.c btowc.c collate.c collcmp.c euc.c fix_grouping.c frune.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 wcsftime.c wcstod.c \
wcstol.c \
wcstoimax.c wcstol.c wcstoll.c \
wcstombs.c \
wcstoul.c wctob.c wctomb.c wctrans.c wctype.c wcwidth.c
wcstoul.c wcstoull.c wcstoumax.c wctob.c wctomb.c wctrans.c wctype.c \
wcwidth.c
.if ${LIB} == "c"
MAN+= btowc.3 \
@ -48,7 +49,8 @@ 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+=wcstol.3 wcstoul.3 wcstol.3 wcstoll.3 wcstol.3 wcstoull.3 \
wcstol.3 wcstoimax.3 wcstol.3 wcstoumax.3
MLINKS+=wctrans.3 towctrans.3
MLINKS+=wctype.3 iswctype.3
.endif

128
lib/libc/locale/wcstoimax.c Normal file
View File

@ -0,0 +1,128 @@
/*-
* Copyright (c) 1992, 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>
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "from @(#)strtol.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
__FBSDID("FreeBSD: src/lib/libc/stdlib/strtoimax.c,v 1.8 2002/09/06 11:23:59 tjr Exp ");
#endif
__FBSDID("$FreeBSD$");
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
/*
* Convert a wide character string to a intmax_t integer.
*/
intmax_t
wcstoimax(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
int base)
{
const wchar_t *s;
uintmax_t acc;
wchar_t c;
uintmax_t cutoff;
int neg, any, cutlim;
/*
* See strtoimax 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 = neg ? (uintmax_t)-(INTMAX_MIN + INTMAX_MAX) + INTMAX_MAX
: INTMAX_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 >= 'a' && c <= '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 ? INTMAX_MIN : INTMAX_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);
}

View File

@ -28,11 +28,17 @@
.Dt WCSTOL 3
.Os
.Sh NAME
.Nm wcstol , wcstoul
.Nm wcstol , wcstoul ,
.Nm wcstoll , wcstoull ,
.Nm wcstoimax , wcstoumax
.Nd "convert a wide character string value to a"
.Vt long
.Vt long ,
.Vt "unsigned long" ,
.Vt "long long" ,
.Vt "unsigned long long" ,
.Vt intmax_t
or
.Vt "unsigned long"
.Vt uintmax_t
integer
.Sh LIBRARY
.Lb libc
@ -42,28 +48,47 @@ integer
.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"
.Ft "long long"
.Fn wcstoll "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base"
.Ft "unsigned long long"
.Fn wcstoull "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base"
.In inttypes.h
.Ft intmax_t
.Fn wcstoimax "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base"
.Ft uintmax_t
.Fn wcstoumax "const wchar_t * restrict nptr" "wchar_t ** restrict endptr" "int base"
.Sh DESCRIPTION
The
.Fn wcstol
.Fn wcstol ,
.Fn wcstoul ,
.Fn wcstoll ,
.Fn wcstoull ,
.Fn wcstoimax
and
.Fn wcstoul
.Fn wcstoumax
functions are wide-character versions of the
.Fn strtol
.Fn strtol ,
.Fn strtoul ,
.Fn strtoll ,
.Fn strtoull ,
.Fn strtoimax
and
.Fn strtoul
.Fn strtoumax
functions.
Refer to
.Xr strtol 3
and
.Xr strtoul 3
Refer to their manual pages (for example
.Xr strtol 3 )
for details.
.Sh SEE ALSO
.Xr strtol 3 ,
.Xr strtoul 3
.Sh STANDARDS
The
.Fn wcstol
.Fn wcstol ,
.Fn wcstoul ,
.Fn wcstoll ,
.Fn wcstoull ,
.Fn wcstoimax
and
.Fn wcstoul
.Fn wcstoumax
functions conform to
.St -isoC-99 .

127
lib/libc/locale/wcstoll.c Normal file
View File

@ -0,0 +1,127 @@
/*-
* Copyright (c) 1992, 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>
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)strtoq.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
__FBSDID("FreeBSD: src/lib/libc/stdlib/strtoll.c,v 1.19 2002/09/06 11:23:59 tjr Exp ");
#endif
__FBSDID("$FreeBSD$");
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
/*
* Convert a wide character string to a long long integer.
*/
long long
wcstoll(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, int base)
{
const wchar_t *s;
unsigned long long acc;
wchar_t c;
unsigned long long cutoff;
int neg, any, cutlim;
/*
* See strtoll 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 = neg ? (unsigned long long)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX
: LLONG_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 ? LLONG_MIN : LLONG_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);
}

126
lib/libc/locale/wcstoull.c Normal file
View File

@ -0,0 +1,126 @@
/*-
* Copyright (c) 1992, 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>
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
__FBSDID("FreeBSD: src/lib/libc/stdlib/strtoull.c,v 1.18 2002/09/06 11:23:59 tjr Exp ");
#endif
__FBSDID("$FreeBSD$");
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
/*
* Convert a wide character string to an unsigned long long integer.
*/
unsigned long long
wcstoull(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
int base)
{
const wchar_t *s;
unsigned long long acc;
wchar_t c;
unsigned long long cutoff;
int neg, any, cutlim;
/*
* See strtoull 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 = ULLONG_MAX / base;
cutlim = ULLONG_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 = ULLONG_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);
}

126
lib/libc/locale/wcstoumax.c Normal file
View File

@ -0,0 +1,126 @@
/*-
* Copyright (c) 1992, 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>
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "from @(#)strtoul.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
__FBSDID("FreeBSD: src/lib/libc/stdlib/strtoumax.c,v 1.8 2002/09/06 11:23:59 tjr Exp ");
#endif
__FBSDID("$FreeBSD$");
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
/*
* Convert a wide character string to a uintmax_t integer.
*/
uintmax_t
wcstoumax(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
int base)
{
const wchar_t *s;
uintmax_t acc;
wchar_t c;
uintmax_t cutoff;
int neg, any, cutlim;
/*
* See strtoimax 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 = UINTMAX_MAX / base;
cutlim = UINTMAX_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 = UINTMAX_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);
}