Path: blob/master/waterbox/libc/functions/_PDCLIB/_PDCLIB_strtox_main.c
2 views
/* _PDCLIB_strtox_main( const char * *, int, _PDCLIB_uintmax_t, _PDCLIB_uintmax_t, int )12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#include <ctype.h>7#include <errno.h>8#include <string.h>9#include <stdint.h>1011#ifndef REGTEST12#include "_PDCLIB_int.h"13_PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, uintmax_t error, uintmax_t limval, int limdigit, char * sign )14{15_PDCLIB_uintmax_t rc = 0;16int digit = -1;17const char * x;18while ( ( x = memchr( _PDCLIB_digits, tolower(**p), base ) ) != NULL )19{20digit = x - _PDCLIB_digits;21if ( ( rc < limval ) || ( ( rc == limval ) && ( digit <= limdigit ) ) )22{23rc = rc * base + (unsigned)digit;24++(*p);25}26else27{28errno = ERANGE;29/* TODO: Only if endptr != NULL - but do we really want *another* parameter? */30/* TODO: Earlier version was missing tolower() here but was not caught by tests */31while ( memchr( _PDCLIB_digits, tolower(**p), base ) != NULL ) ++(*p);32/* TODO: This is ugly, but keeps caller from negating the error value */33*sign = '+';34return error;35}36}37if ( digit == -1 )38{39*p = NULL;40return 0;41}42return rc;43}44#endif4546#ifdef TEST47#include "_PDCLIB_test.h"48#include <errno.h>4950int main( void )51{52#ifndef REGTEST53const char * p;54char test[] = "123_";55char fail[] = "xxx";56char sign = '-';57/* basic functionality */58p = test;59errno = 0;60TESTCASE( _PDCLIB_strtox_main( &p, 10u, (uintmax_t)999, (uintmax_t)12, 3, &sign ) == 123 );61TESTCASE( errno == 0 );62TESTCASE( p == &test[3] );63/* proper functioning to smaller base */64p = test;65TESTCASE( _PDCLIB_strtox_main( &p, 8u, (uintmax_t)999, (uintmax_t)12, 3, &sign ) == 0123 );66TESTCASE( errno == 0 );67TESTCASE( p == &test[3] );68/* overflowing subject sequence must still return proper endptr */69p = test;70TESTCASE( _PDCLIB_strtox_main( &p, 4u, (uintmax_t)999, (uintmax_t)1, 2, &sign ) == 999 );71TESTCASE( errno == ERANGE );72TESTCASE( p == &test[3] );73TESTCASE( sign == '+' );74/* testing conversion failure */75errno = 0;76p = fail;77sign = '-';78TESTCASE( _PDCLIB_strtox_main( &p, 10u, (uintmax_t)999, (uintmax_t)99, 8, &sign ) == 0 );79TESTCASE( p == NULL );80#endif81return TEST_RESULTS;82}8384#endif858687