aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/wcstoimax.c
blob: 59894f60fe00b0699801397371c5976fa3ee760b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <wchar.h>
#include <wctype.h>
#include <inttypes.h>
#include <errno.h>

intmax_t wcstoimax(const wchar_t *s, wchar_t **p, int base)
{
	int sign = 0;
	uintmax_t x;

	/* Initial whitespace */
	for (; iswspace(*s); s++);

	/* Optional sign */
	if (*s == '-') sign = *s++;
	else if (*s == '+') s++;

	x = wcstoumax(s, p, base);
	if (x > INTMAX_MAX) {
		if (!sign || -x != INTMAX_MIN)
			errno = ERANGE;
		return sign ? INTMAX_MIN : INTMAX_MAX;
	}
	return sign ? -x : x;
}