@@ -31,34 +31,26 @@ namespace LIBC_NAMESPACE_DECL {
3131namespace internal {
3232
3333// Returns the idx to the first character in src that is not a whitespace
34- // character (as determined by isspace() / iswspace() )
34+ // character (as determined by isspace())
3535template <typename CharType>
3636LIBC_INLINE size_t
3737first_non_whitespace (const CharType *__restrict src,
3838 size_t src_len = cpp::numeric_limits<size_t >::max()) {
3939 size_t src_cur = 0 ;
40- while (src_cur < src_len) {
41- if constexpr (cpp::is_same_v<CharType, char >) {
42- if (!internal::isspace (src[src_cur]))
43- break ;
44- } else {
45- if (!internal::iswspace (src[src_cur]))
46- break ;
47- }
48- ++src_cur;
49- }
40+ for (; src_cur < src_len && internal::isspace (src[src_cur]); ++src_cur)
41+ ;
5042 return src_cur;
5143}
5244
5345// Returns +1, -1, or 0 if 'src' starts with (respectively)
5446// plus sign, minus sign, or neither.
5547template <typename CharType>
5648LIBC_INLINE static int get_sign (const CharType *__restrict src) {
57- if constexpr (cpp::is_same_v<CharType, char >) {
58- return (src[ 0 ] == ' + ' ) ? 1 : (src[ 0 ] == ' - ' ? - 1 : 0 ) ;
59- } else {
60- return (src[ 0 ] == L ' + ' ) ? 1 : (src[ 0 ] == L ' - ' ? - 1 : 0 ) ;
61- }
49+ if ( is_char_or_wchar (src[ 0 ], ' + ' , L ' + ' ))
50+ return 1 ;
51+ if ( is_char_or_wchar (src[ 0 ], ' - ' , L ' - ' ))
52+ return - 1 ;
53+ return 0 ;
6254}
6355
6456// checks if the next 3 characters of the string pointer are the start of a
@@ -68,13 +60,9 @@ LIBC_INLINE static bool is_hex_start(const CharType *__restrict src,
6860 size_t src_len) {
6961 if (src_len < 3 )
7062 return false ;
71- if constexpr (cpp::is_same_v<CharType, char >) {
72- return src[0 ] == ' 0' && tolower (src[1 ]) == ' x' && isalnum (src[2 ]) &&
73- b36_char_to_int (src[2 ]) < 16 ;
74- } else {
75- return src[0 ] == L' 0' && towlower (src[1 ]) == L' x' && iswalnum (src[2 ]) &&
76- b36_wchar_to_int (src[2 ]) < 16 ;
77- }
63+ return is_char_or_wchar (src[0 ], ' 0' , L' 0' ) &&
64+ is_char_or_wchar (tolower (src[1 ]), ' x' , L' x' ) && isalnum (src[2 ]) &&
65+ b36_char_to_int (src[2 ]) < 16 ;
7866}
7967
8068// Takes the address of the string pointer and parses the base from the start of
@@ -90,14 +78,8 @@ LIBC_INLINE static int infer_base(const CharType *__restrict src,
9078 // An octal number is defined as "the prefix 0 optionally followed by a
9179 // sequence of the digits 0 through 7 only" (C standard 6.4.4.1) and so any
9280 // number that starts with 0, including just 0, is an octal number.
93- if (src_len > 0 ) {
94- if constexpr (cpp::is_same_v<CharType, char >) {
95- if (src[0 ] == ' 0' )
96- return 8 ;
97- } else {
98- if (src[0 ] == L' 0' )
99- return 8 ;
100- }
81+ if (src_len > 0 && is_char_or_wchar (src[0 ], ' 0' , L' 0' )) {
82+ return 8 ;
10183 }
10284 // A decimal number is defined as beginning "with a nonzero digit and
10385 // consist[ing] of a sequence of decimal digits." (C standard 6.4.4.1)
@@ -150,18 +132,8 @@ strtointeger(const CharType *__restrict src, int base,
150132 bool is_number = false ;
151133 int error_val = 0 ;
152134 ResultType result = 0 ;
153- while (src_cur < src_len) {
154- int cur_digit;
155- if constexpr (cpp::is_same_v<CharType, char >) {
156- if (!isalnum (src[src_cur]))
157- break ;
158- cur_digit = b36_char_to_int (src[src_cur]);
159- } else {
160- if (!iswalnum (src[src_cur]))
161- break ;
162- cur_digit = b36_wchar_to_int (src[src_cur]);
163- }
164-
135+ while (src_cur < src_len && isalnum (src[src_cur])) {
136+ int cur_digit = b36_char_to_int (src[src_cur]);
165137 if (cur_digit >= base)
166138 break ;
167139
0 commit comments