From: Denys Vlasenko Date: Wed, 6 Mar 2013 22:44:23 +0000 (+0100) Subject: Open-code isprint(c) and isspace(c) X-Git-Tag: v4.8~97 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5198ed4bb36e3105a1f12bb3250bee78b6e0dd72;p=strace Open-code isprint(c) and isspace(c) We don't call setlocale, thus we always use C locale. But libc supports various other locales, and therefore its ctype interface is general and at times inefficient. For example, in glibc these macros result in function call, whereas for e.g. isprint(c) just c >= ' ' && c <= 0x7e suffices. By open-coding ctype checks (we have only 4 of them) we avoid function calls, we get smaller code: text data bss dec hex filename 245127 680 5708 251515 3d67b strace_old 245019 676 5708 251403 3d60b strace and we don't link in ctype tables (beneficial for static builds). Signed-off-by: Denys Vlasenko --- diff --git a/defs.h b/defs.h index c20fcc61..bb4003f5 100644 --- a/defs.h +++ b/defs.h @@ -52,7 +52,10 @@ #include #include #include -#include +/* Open-coding isprint(ch) et al proved more efficient than calling + * generalized libc interface. We don't *want* to do non-ASCII anyway. + */ +/* #include */ #include #include #include diff --git a/file.c b/file.c index cfdd0bb9..2af14bff 100644 --- a/file.c +++ b/file.c @@ -2456,7 +2456,7 @@ print_xattr_val(struct tcb *tcp, int failed, unsigned char *in = &buf[3 * size]; size_t i; for (i = 0; i < size; ++i) { - if (isprint(in[i])) + if (in[i] >= ' ' && in[i] <= 0x7e) *out++ = in[i]; else { #define tohex(n) "0123456789abcdef"[n] diff --git a/util.c b/util.c index 0cc43feb..84ab00eb 100644 --- a/util.c +++ b/util.c @@ -400,7 +400,16 @@ string_quote(const char *instr, char *outstr, long len, int size) /* Check for NUL-terminated string. */ if (c == eol) break; - if (!isprint(c) && !isspace(c)) { + + /* Force hex unless c is printable or whitespace */ + if (c > 0x7e) { + usehex = 1; + break; + } + /* In ASCII isspace is only these chars: "\t\n\v\f\r". + * They happen to have ASCII codes 9,10,11,12,13. + */ + if (c < ' ' && (unsigned)(c - 9) >= 5) { usehex = 1; break; } @@ -453,7 +462,7 @@ string_quote(const char *instr, char *outstr, long len, int size) *s++ = 'v'; break; default: - if (isprint(c)) + if (c >= ' ' && c <= 0x7e) *s++ = c; else { /* Print \octal */