Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/waterbox/libc/functions/_PDCLIB/_PDCLIB_strtox_prelim.c
2 views
1
/* _PDCLIB_strtox_prelim( const char *, char *, int * )
2
3
This file is part of the Public Domain C Library (PDCLib).
4
Permission is granted to use, modify, and / or redistribute at will.
5
*/
6
7
#include <ctype.h>
8
#include <stddef.h>
9
#include <string.h>
10
#ifndef REGTEST
11
const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base )
12
{
13
/* skipping leading whitespace */
14
while ( isspace( *p ) ) ++p;
15
/* determining / skipping sign */
16
if ( *p != '+' && *p != '-' ) *sign = '+';
17
else *sign = *(p++);
18
/* determining base */
19
if ( *p == '0' )
20
{
21
++p;
22
if ( ( *base == 0 || *base == 16 ) && ( *p == 'x' || *p == 'X' ) )
23
{
24
*base = 16;
25
++p;
26
/* catching a border case here: "0x" followed by a non-digit should
27
be parsed as the unprefixed zero.
28
We have to "rewind" the parsing; having the base set to 16 if it
29
was zero previously does not hurt, as the result is zero anyway.
30
*/
31
if ( memchr( _PDCLIB_digits, tolower(*p), *base ) == NULL )
32
{
33
p -= 2;
34
}
35
}
36
else if ( *base == 0 )
37
{
38
*base = 8;
39
}
40
else
41
{
42
--p;
43
}
44
}
45
else if ( ! *base )
46
{
47
*base = 10;
48
}
49
return ( ( *base >= 2 ) && ( *base <= 36 ) ) ? p : NULL;
50
}
51
#endif
52
53
#ifdef TEST
54
#include "_PDCLIB_test.h"
55
56
int main( void )
57
{
58
#ifndef REGTEST
59
int base = 0;
60
char sign = '\0';
61
char test1[] = " 123";
62
char test2[] = "\t+0123";
63
char test3[] = "\v-0x123";
64
TESTCASE( _PDCLIB_strtox_prelim( test1, &sign, &base ) == &test1[2] );
65
TESTCASE( sign == '+' );
66
TESTCASE( base == 10 );
67
base = 0;
68
sign = '\0';
69
TESTCASE( _PDCLIB_strtox_prelim( test2, &sign, &base ) == &test2[3] );
70
TESTCASE( sign == '+' );
71
TESTCASE( base == 8 );
72
base = 0;
73
sign = '\0';
74
TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[4] );
75
TESTCASE( sign == '-' );
76
TESTCASE( base == 16 );
77
base = 10;
78
sign = '\0';
79
TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[2] );
80
TESTCASE( sign == '-' );
81
TESTCASE( base == 10 );
82
base = 1;
83
TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );
84
base = 37;
85
TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );
86
#endif
87
return TEST_RESULTS;
88
}
89
90
#endif
91
92