libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/strtou.c (138 lines, 3.7 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
// util/strtou.c
// String conversion source file from libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

#include "util.h"

#include <ctype.h>
#include <errno.h>

/* Convert a string to an unsigned 8-bit integer */
u8 strtou8(const char *nptr, char **endptr, register int base) {
	register const char *s = nptr; register u8 i = 0, c;
	
	for (; isspace(*s); ++s);
	
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
	
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
		s += 2; base = 16;
	}
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
	
	for (;; ++s) {
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
		else { break; }
		
		if (c >= base) { break; }
		if (i > (U8_MAX - c) / base) {
			errno = ERANGE; i = U8_MAX; goto end;
		}
		
		i = i * base + c;
	}
	
end:;
	if (endptr) { *endptr = (char *)s; } return i;
}

/* Convert a string to an unsigned 16-bit integer */
u16 strtou16(const char *nptr, char **endptr, register int base) {
	register const char *s = nptr; register u16 i = 0, c;
	
	for (; isspace(*s); ++s);
	
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
	
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
		s += 2; base = 16;
	}
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
	
	for (;; ++s) {
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
		else { break; }
		
		if (c >= base) { break; }
		if (i > (U16_MAX - c) / base) {
			errno = ERANGE; i = U16_MAX; goto end;
		}
		
		i = i * base + c;
	}
	
end:;
	if (endptr) { *endptr = (char *)s; } return i;
}

/* Convert a string to an unsigned 32-bit integer */
u32 strtou32(const char *nptr, char **endptr, register int base) {
	register const char *s = nptr; register u32 i = 0, c;
	
	for (; isspace(*s); ++s);
	
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
	
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
		s += 2; base = 16;
	}
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
	
	for (;; ++s) {
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
		else { break; }
		
		if (c >= base) { break; }
		if (i > (U32_MAX - c) / base) {
			errno = ERANGE; i = U32_MAX; goto end;
		}
		
		i = i * base + c;
	}
	
end:;
	if (endptr) { *endptr = (char *)s; } return i;
}

/* Convert a string to an unsigned 64-bit integer */
u64 strtou64(const char *nptr, char **endptr, register int base) {
	register const char *s = nptr; register u64 i = 0, c;
	
	for (; isspace(*s); ++s);
	
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
	
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
		s += 2; base = 16;
	}
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
	
	for (;; ++s) {
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
		else { break; }
		
		if (c >= base) { break; }
		if (i > (U64_MAX - c) / base) {
			errno = ERANGE; i = U64_MAX; goto end;
		}
		
		i = i * base + c;
	}
	
end:;
	if (endptr) { *endptr = (char *)s; } return i;
}