library: improve performance for one 'escape' function

While this patch has some cosmetic whitespace changes,
more importantly it makes that 'esc_all' function more
efficient. By abandoning the indexed loop approach for
a direct pointer manipulation, we will save 9 iterated
machine instructions, for a total of 33 bytes of code.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2021-01-12 00:00:00 -06:00 committed by Craig Small
parent 0cf6942be7
commit 031a08f2a7

View File

@ -26,14 +26,13 @@
#include "escape.h" #include "escape.h"
#include "readproc.h" #include "readproc.h"
#define SECURE_ESCAPE_ARGS(dst, bytes) do { \ #define SECURE_ESCAPE_ARGS(dst, bytes) do { \
if ((bytes) <= 0) return 0; \ if ((bytes) <= 0) return 0; \
*(dst) = '\0'; \ *(dst) = '\0'; \
if ((bytes) >= INT_MAX) return 0; \ if ((bytes) >= INT_MAX) return 0; \
} while (0) } while (0)
static char UTF_tab[] = { static const char UTF_tab[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00 - 0x0F 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00 - 0x0F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10 - 0x1F 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10 - 0x1F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20 - 0x2F 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20 - 0x2F
@ -46,34 +45,34 @@ static char UTF_tab[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x90 - 0x9F -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x90 - 0x9F
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xA0 - 0xAF -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xA0 - 0xAF
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xB0 - 0xBF -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xB0 - 0xBF
-1,-1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xC0 - 0xCF, 0xC2 = begins 2 -1,-1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xC0 - 0xCF
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xD0 - 0xDF 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xD0 - 0xDF
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE0 - 0xEF, 0xE0 = begins 3 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE0 - 0xEF
4, 4, 4, 4, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xF0 - 0xFF, 0xF0 = begins 4 4, 4, 4, 4, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xF0 - 0xFF
}; // ( 0xF5 & beyond invalid ) };
static const unsigned char ESC_tab[] = { static const unsigned char ESC_tab[] = {
"@..............................." // 0x00 - 0x1F "@..............................." // 0x00 - 0x1F
"||||||||||||||||||||||||||||||||" // 0x20 - 0x3F "||||||||||||||||||||||||||||||||" // 0x20 - 0x3F
"||||||||||||||||||||||||||||||||" // 0x40 - 0x5f "||||||||||||||||||||||||||||||||" // 0x40 - 0x5f
"|||||||||||||||||||||||||||||||." // 0x60 - 0x7F "|||||||||||||||||||||||||||||||." // 0x60 - 0x7F
"????????????????????????????????" // 0x80 - 0x9F "????????????????????????????????" // 0x80 - 0x9F
"????????????????????????????????" // 0xA0 - 0xBF "????????????????????????????????" // 0xA0 - 0xBF
"????????????????????????????????" // 0xC0 - 0xDF "????????????????????????????????" // 0xC0 - 0xDF
"????????????????????????????????" // 0xE0 - 0xFF "????????????????????????????????" // 0xE0 - 0xFF
}; };
static inline void esc_all (unsigned char *str) { static inline void esc_all (unsigned char *str) {
unsigned char c; unsigned char c;
int i;
// if bad locale/corrupt str, replace non-printing stuff // if bad locale/corrupt str, replace non-printing stuff
for (i = 0; str[i] != '\0'; i++) while (*str) {
if ((c = ESC_tab[str[i]]) != '|') if ((c = ESC_tab[*str]) != '|')
str[i] = c; *str = c;
++str;
}
} }
static inline void esc_ctl (unsigned char *str, int len) { static inline void esc_ctl (unsigned char *str, int len) {
int i, n; int i, n;
@ -90,7 +89,6 @@ static inline void esc_ctl (unsigned char *str, int len) {
} }
} }
int escape_str (unsigned char *dst, const unsigned char *src, int bufsize) { int escape_str (unsigned char *dst, const unsigned char *src, int bufsize) {
static int utf_sw = 0; static int utf_sw = 0;
int n; int n;
@ -113,7 +111,6 @@ int escape_str (unsigned char *dst, const unsigned char *src, int bufsize) {
return n; return n;
} }
int escape_command (unsigned char *outbuf, const proc_t *pp, int bytes, unsigned flags) { int escape_command (unsigned char *outbuf, const proc_t *pp, int bytes, unsigned flags) {
int overhead = 0; int overhead = 0;
int end = 0; int end = 0;