Use inline ctype functions for ato*/strto* and scanf.
This also let us remove the `unsigned char` casts, since we define our
inlines to work for all values.
Before:
Benchmark Time CPU Iterations
-------------------------------------------------------------
BM_inttypes_strtoimax 112 ns 112 ns 6222193
BM_inttypes_strtoumax 104 ns 104 ns 6725010
BM_stdlib_strtol 113 ns 113 ns 6195861
BM_stdlib_strtoll 113 ns 113 ns 6195633
BM_stdlib_strtoul 105 ns 105 ns 6691394
BM_stdlib_strtoull 105 ns 105 ns 6690695
BM_stdio_scanf_d 504 ns 503 ns 1385224
BM_stdio_scanf_maps 1900 ns 1898 ns 369260
BM_stdio_scanf_maps_baseline 1030 ns 1030 ns 678832
BM_stdio_scanf_s 433 ns 432 ns 1619086
After:
BM_inttypes_strtoimax 91 ns 91 ns 7718194
BM_inttypes_strtoumax 82 ns 82 ns 8508052
BM_stdlib_strtol 92 ns 92 ns 7674694
BM_stdlib_strtoll 91 ns 91 ns 7639228
BM_stdlib_strtoul 83 ns 82 ns 8500304
BM_stdlib_strtoull 82 ns 82 ns 8504929
BM_stdio_scanf_d 465 ns 465 ns 1507891
BM_stdio_scanf_maps 1836 ns 1836 ns 381082
BM_stdio_scanf_maps_baseline 846 ns 845 ns 830881
BM_stdio_scanf_s 419 ns 419 ns 1671979
Bug: N/A
Test: ran tests, benchmarks
Change-Id: I44681daf16c4328b060770cf11fc0633157c427f
diff --git a/libc/stdio/vfscanf.c b/libc/stdio/vfscanf.c
index c9e4385..f0ed4ae 100644
--- a/libc/stdio/vfscanf.c
+++ b/libc/stdio/vfscanf.c
@@ -31,7 +31,6 @@
* SUCH DAMAGE.
*/
-#include <ctype.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h>
@@ -41,6 +40,8 @@
#include <wctype.h>
#include "local.h"
+#include <private/bionic_ctype.h>
+
#define BUF 513 /* Maximum length of numeric string. */
/*
@@ -116,8 +117,8 @@
for (;;) {
c = *fmt++;
if (c == 0) return (nassigned);
- if (isspace(c)) {
- while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p)) nread++, fp->_r--, fp->_p++;
+ if (IsSpace(c)) {
+ while ((fp->_r > 0 || __srefill(fp) == 0) && IsSpace(*fp->_p)) nread++, fp->_r--, fp->_p++;
continue;
}
if (c != '%') goto literal;
@@ -127,11 +128,11 @@
* switch on the format. continue if done;
* break once format type is derived.
*/
- again:
+again:
c = *fmt++;
switch (c) {
case '%':
- literal:
+literal:
if (fp->_r <= 0 && __srefill(fp)) goto input_failure;
if (*fp->_p != c) goto match_failure;
fp->_r--, fp->_p++;
@@ -286,7 +287,7 @@
return (EOF);
default: /* compat */
- if (isupper(c)) flags |= LONG;
+ if (IsUpper(c)) flags |= LONG;
c = CT_INT;
base = 10;
break;
@@ -302,12 +303,13 @@
* that suppress this.
*/
if ((flags & NOSKIP) == 0) {
- while (isspace(*fp->_p)) {
+ while (IsSpace(*fp->_p)) {
nread++;
- if (--fp->_r > 0)
+ if (--fp->_r > 0) {
fp->_p++;
- else if (__srefill(fp))
+ } else if (__srefill(fp)) {
goto input_failure;
+ }
}
/*
* Note that there is at least one character in
@@ -395,7 +397,7 @@
wcp = (flags & SUPPRESS) == 0 ? va_arg(ap, wchar_t*) : &twc;
n = 0;
- while ((c == CT_CCL || !isspace(*fp->_p)) && width != 0) {
+ while ((c == CT_CCL || !IsSpace(*fp->_p)) && width != 0) {
if (n == (int)MB_CUR_MAX) {
fp->_flags |= __SERR;
goto input_failure;
@@ -439,7 +441,7 @@
n = nchars;
} else if (flags & SUPPRESS) {
n = 0;
- while ((c == CT_CCL && ccltab[*fp->_p]) || (c == CT_STRING && !isspace(*fp->_p))) {
+ while ((c == CT_CCL && ccltab[*fp->_p]) || (c == CT_STRING && !IsSpace(*fp->_p))) {
n++, fp->_r--, fp->_p++;
if (--width == 0) break;
if (fp->_r <= 0 && __srefill(fp)) {
@@ -449,7 +451,7 @@
}
} else {
p0 = p = va_arg(ap, char*);
- while ((c == CT_CCL && ccltab[*fp->_p]) || (c == CT_STRING && !isspace(*fp->_p))) {
+ while ((c == CT_CCL && ccltab[*fp->_p]) || (c == CT_STRING && !IsSpace(*fp->_p))) {
fp->_r--;
*p++ = *fp->_p++;
if (--width == 0) break;