From c744baeb899f82b64c0d03bc4e7aba6f0fa17a92 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 27 Sep 2020 22:15:32 +0200 Subject: [PATCH] Add ctype variants to stdstring and use it in RA --- core_info.c | 15 ++++++--- gfx/drivers_font_renderer/freetype.c | 2 +- gfx/drivers_font_renderer/stb.c | 3 +- gfx/drivers_font_renderer/stb_unicode.c | 3 +- libretro-common/file/config_file.c | 6 ++-- libretro-common/include/string/stdstring.h | 38 ++++++++++++++++------ libretro-common/streams/file_stream.c | 3 +- libretro-common/string/stdstring.c | 28 +++++++++++++--- libretro-db/query.c | 16 ++++----- retroarch.c | 12 +++---- tasks/task_database_cue.c | 4 +-- 11 files changed, 89 insertions(+), 41 deletions(-) diff --git a/core_info.c b/core_info.c index ad02df9a66..207b654a63 100644 --- a/core_info.c +++ b/core_info.c @@ -1351,8 +1351,8 @@ bool core_info_hw_api_supported(core_info_t *info) { case STATE_API_NAME: { - if ( isupper((unsigned char)cur_api[j]) || - islower((unsigned char)cur_api[j])) + if ( ISUPPER((unsigned char)cur_api[j]) || + ISLOWER((unsigned char)cur_api[j])) api_str[api_pos++] = cur_api[j]; else { @@ -1401,14 +1401,21 @@ bool core_info_hw_api_supported(core_info_t *info) } case STATE_API_VERSION: { - if (!found_minor && cur_api[j] >= '0' && cur_api[j] <= '9' && cur_api[j] != '.') + if ( !found_minor + && cur_api[j] >= '0' + && cur_api[j] <= '9' + && cur_api[j] != '.') { found_major = true; if (major_str_pos < sizeof(major_str) - 1) major_str[major_str_pos++] = cur_api[j]; } - else if (found_major && found_minor && cur_api[j] >= '0' && cur_api[j] <= '9') + else if ( + found_major + && found_minor + && cur_api[j] >= '0' + && cur_api[j] <= '9') { if (minor_str_pos < sizeof(minor_str) - 1) minor_str[minor_str_pos++] = cur_api[j]; diff --git a/gfx/drivers_font_renderer/freetype.c b/gfx/drivers_font_renderer/freetype.c index c586226596..facbfa522b 100644 --- a/gfx/drivers_font_renderer/freetype.c +++ b/gfx/drivers_font_renderer/freetype.c @@ -205,7 +205,7 @@ static bool font_renderer_create_atlas(ft_font_renderer_t *handle, float font_si font_renderer_ft_get_glyph(handle, i); for (i = 0; i < 256; i++) - if (isalnum(i)) + if (ISALNUM(i)) font_renderer_ft_get_glyph(handle, i); return true; diff --git a/gfx/drivers_font_renderer/stb.c b/gfx/drivers_font_renderer/stb.c index dd32fce9b1..137b6a6750 100644 --- a/gfx/drivers_font_renderer/stb.c +++ b/gfx/drivers_font_renderer/stb.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "../font_driver.h" #include "../../verbosity.h" @@ -108,7 +109,7 @@ static bool font_renderer_stb_create_atlas(stb_font_renderer_t *self, g->height = c->y1 - c->y0; /* Make sure important characters fit */ - if (isalnum(i) && (!g->width || !g->height)) + if (ISALNUM(i) && (!g->width || !g->height)) { int new_width = width * 1.2; int new_height = height * 1.2; diff --git a/gfx/drivers_font_renderer/stb_unicode.c b/gfx/drivers_font_renderer/stb_unicode.c index 5de291f03c..b5b397cb64 100644 --- a/gfx/drivers_font_renderer/stb_unicode.c +++ b/gfx/drivers_font_renderer/stb_unicode.c @@ -18,6 +18,7 @@ #include #include +#include #include #ifdef WIIU @@ -220,7 +221,7 @@ static bool font_renderer_stb_unicode_create_atlas( for (i = 0; i < 256; i++) { - if (isalnum(i)) + if (ISALNUM(i)) font_renderer_stb_unicode_get_glyph(self, i); } diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index b1d89732c2..38dedaa06d 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -219,7 +219,7 @@ static char *config_file_extract_value(char *line, bool is_value) if (is_value) { - while (isspace((int)*line)) + while (ISSPACE((int)*line)) line++; /* If we don't have an equal sign here, @@ -230,7 +230,7 @@ static char *config_file_extract_value(char *line, bool is_value) line++; } - while (isspace((int)*line)) + while (ISSPACE((int)*line)) line++; /* Note: From this point on, an empty value @@ -517,7 +517,7 @@ static bool config_file_parse_line(config_file_t *conf, } /* Skip to first non-space character */ - while (isspace((int)*line)) + while (ISSPACE((int)*line)) line++; /* Allocate storage for key */ diff --git a/libretro-common/include/string/stdstring.h b/libretro-common/include/string/stdstring.h index b76a8a4e54..5d8c31b322 100644 --- a/libretro-common/include/string/stdstring.h +++ b/libretro-common/include/string/stdstring.h @@ -35,7 +35,32 @@ RETRO_BEGIN_DECLS -#define strcpy_literal(a, b) strcpy(a, b) +#define STRLEN_CONST(x) ((sizeof((x))-1)) + +#define strcpy_literal(a, b) strcpy(a, b) + +#define string_is_not_equal(a, b) !string_is_equal((a), (b)) + +#define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0) +#define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0) + +#define TOLOWER(c) (c | (lr_char_props[c] & 0x20)) +#define TOUPPER(c) (c & ~(lr_char_props[c] & 0x20)) + +/* C standard says \f \v are space, but this one disagrees */ +#define ISSPACE(c) (lr_char_props[c] & 0x80) + +#define ISDIGIT(c) (lr_char_props[c] & 0x40) +#define ISALPHA(c) (lr_char_props[c] & 0x20) +#define ISLOWER(c) (lr_char_props[c] & 0x04) +#define ISUPPER(c) (lr_char_props[c] & 0x02) +#define ISALNUM(c) (lr_char_props[c] & 0x60) +#define ISUALPHA(c) (lr_char_props[c] & 0x28) +#define ISUALNUM(c) (lr_char_props[c] & 0x68) +#define IS_XDIGIT(c) (lr_char_props[c] & 0x01) + +/* Deprecated alias, all callers should use string_is_equal_case_insensitive instead */ +#define string_is_equal_noncase string_is_equal_case_insensitive static INLINE bool string_is_empty(const char *data) { @@ -85,12 +110,6 @@ static INLINE size_t strlen_size(const char *str, size_t size) return i; } -#define STRLEN_CONST(x) ((sizeof((x))-1)) - -#define string_is_not_equal(a, b) !string_is_equal((a), (b)) - -#define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0) -#define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0) static INLINE bool string_is_equal_case_insensitive(const char *a, const char *b) @@ -111,9 +130,6 @@ static INLINE bool string_is_equal_case_insensitive(const char *a, return (result == 0); } -/* Deprecated alias, all callers should use string_is_equal_case_insensitive instead */ -#define string_is_equal_noncase string_is_equal_case_insensitive - char *string_to_upper(char *s); char *string_to_lower(char *s); @@ -175,6 +191,8 @@ char *string_init(const char *src); void string_set(char **string, const char *src); +extern const unsigned char lr_char_props[256]; + RETRO_END_DECLS #endif diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index 280eaf59ad..ae64c4cbc5 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -35,6 +35,7 @@ #include #endif +#include #include #define VFS_FRONTEND #include @@ -255,7 +256,7 @@ int filestream_scanf(RFILE *stream, const char* format, ...) *subfmtiter++ = *format++; } - while (isdigit((unsigned char)*format)) + while (ISDIGIT((unsigned char)*format)) *subfmtiter++ = *format++; /* width */ /* length */ diff --git a/libretro-common/string/stdstring.c b/libretro-common/string/stdstring.c index 2f54daa663..50c35dde8d 100644 --- a/libretro-common/string/stdstring.c +++ b/libretro-common/string/stdstring.c @@ -26,6 +26,26 @@ #include #include +const uint8_t lr_char_props[256] = { + /*x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x80,0x00,0x00, /* 0x */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 1x */ + 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 2x !"#$%&'()*+,-./ */ + 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00, /* 3x 0123456789:;<=>? */ + 0x00,0x23,0x23,0x23,0x23,0x23,0x23,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, /* 4x @ABCDEFGHIJKLMNO */ + 0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x00,0x00,0x00,0x00,0x08, /* 5x PQRSTUVWXYZ[\]^_ */ + 0x00,0x25,0x25,0x25,0x25,0x25,0x25,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24, /* 6x `abcdefghijklmno */ + 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00, /* 7x pqrstuvwxyz{|}~ */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8x */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 9x */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Ax */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Bx */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Cx */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Dx */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Ex */ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Fx */ +}; + char *string_init(const char *src) { return src ? strdup(src) : NULL; @@ -124,7 +144,7 @@ char *string_trim_whitespace_left(char *const s) size_t len = strlen(s); char *current = s; - while (*current && isspace((unsigned char)*current)) + while (*current && ISSPACE((unsigned char)*current)) { ++current; --len; @@ -145,13 +165,13 @@ char *string_trim_whitespace_right(char *const s) size_t len = strlen(s); char *current = s + len - 1; - while (current != s && isspace((unsigned char)*current)) + while (current != s && ISSPACE((unsigned char)*current)) { --current; --len; } - current[isspace((unsigned char)*current) ? 0 : 1] = '\0'; + current[ISSPACE((unsigned char)*current) ? 0 : 1] = '\0'; } return s; @@ -355,7 +375,7 @@ unsigned string_to_unsigned(const char *str) for (ptr = str; *ptr != '\0'; ptr++) { - if (!isdigit((unsigned char)*ptr)) + if (!ISDIGIT((unsigned char)*ptr)) return 0; } diff --git a/libretro-db/query.c b/libretro-db/query.c index 2c590954d0..7839264bf2 100644 --- a/libretro-db/query.c +++ b/libretro-db/query.c @@ -329,7 +329,7 @@ static struct buffer query_parse_integer( } else { - while (isdigit((int)buff.data[buff.offset])) + while (ISDIGIT((int)buff.data[buff.offset])) buff.offset++; } @@ -339,7 +339,7 @@ static struct buffer query_parse_integer( static struct buffer query_chomp(struct buffer buff) { for (; (unsigned)buff.offset < buff.len - && isspace((int)buff.data[buff.offset]); buff.offset++); + && ISSPACE((int)buff.data[buff.offset]); buff.offset++); return buff; } @@ -512,7 +512,7 @@ static struct buffer query_parse_value( query_peek(buff, "'", STRLEN_CONST("'"))) buff = query_parse_string(s, len, buff, value, error); - else if (isdigit((int)buff.data[buff.offset])) + else if (ISDIGIT((int)buff.data[buff.offset])) buff = query_parse_integer(s, len, buff, value, error); return buff; } @@ -556,7 +556,7 @@ static struct buffer query_get_ident( *len = 0; query_peek_char(s, _len, buff, &c, error); - if (*error || !isalpha((int)c)) + if (*error || !ISALPHA((int)c)) return buff; buff.offset++; @@ -565,7 +565,7 @@ static struct buffer query_get_ident( while (!*error) { - if (!(isalnum((int)c) || c == '_')) + if (!(ISALNUM((int)c) || c == '_')) break; buff.offset++; *len = *len + 1; @@ -608,7 +608,7 @@ static struct buffer query_parse_argument( buff = query_chomp(buff); if ( - isalpha((int)buff.data[buff.offset]) + ISALPHA((int)buff.data[buff.offset]) && !( query_peek(buff, "nil", STRLEN_CONST("nil")) || query_peek(buff, "true", STRLEN_CONST("true")) @@ -814,7 +814,7 @@ static struct buffer query_parse_table( goto clean; } - if (isalpha((int)buff.data[buff.offset])) + if (ISALPHA((int)buff.data[buff.offset])) { buff = query_get_ident(s, len, buff, &ident_name, &ident_len, error); @@ -955,7 +955,7 @@ void *libretrodb_query_compile(libretrodb_t *db, if (*error_string) goto error; } - else if (isalpha((int)buff.data[buff.offset])) + else if (ISALPHA((int)buff.data[buff.offset])) buff = query_parse_method_call(tmp_error_buff, error_buff_len, buff, &q->root, error_string); diff --git a/retroarch.c b/retroarch.c index 80568b1f9a..d6deac6157 100644 --- a/retroarch.c +++ b/retroarch.c @@ -5371,7 +5371,7 @@ static int menu_entries_elem_get_first_char( if ((path = list->list[offset].alt ? list->list[offset].alt : list->list[offset].path)) - ret = tolower((int)*path); + ret = TOLOWER((int)*path); /* "Normalize" non-alphabetical entries so they * are lumped together for purposes of jumping. */ @@ -15045,7 +15045,7 @@ static void command_event_set_savestate_auto_index( continue; end = dir_elem + strlen(dir_elem); - while ((end > dir_elem) && isdigit((int)end[-1])) + while ((end > dir_elem) && ISDIGIT((int)end[-1])) end--; idx = (unsigned)strtoul(end, NULL, 0); @@ -17907,7 +17907,7 @@ static const char *core_option_manager_parse_value_label( /* Any label starting with a digit (or +/-) * cannot be a boolean string, and requires * no further processing */ - if (isdigit((unsigned char)*label) || + if (ISDIGIT((unsigned char)*label) || (*label == '+') || (*label == '-')) return label; @@ -28031,8 +28031,8 @@ static const char *input_config_get_prefix(unsigned user, bool meta) enum retro_key input_config_translate_str_to_rk(const char *str) { size_t i; - if (strlen(str) == 1 && isalpha((int)*str)) - return (enum retro_key)(RETROK_a + (tolower((int)*str) - (int)'a')); + if (strlen(str) == 1 && ISALPHA((int)*str)) + return (enum retro_key)(RETROK_a + (TOLOWER((int)*str) - (int)'a')); for (i = 0; input_config_key_map[i].str; i++) { if (string_is_equal_noncase(input_config_key_map[i].str, str)) @@ -28140,7 +28140,7 @@ static void input_config_parse_joy_button( if (*btn == 'h') { const char *str = btn + 1; - if (str && isdigit((int)*str)) + if (str && ISDIGIT((int)*str)) parse_hat(bind, str); } else diff --git a/tasks/task_database_cue.c b/tasks/task_database_cue.c index ba1d7a50e6..ca6227960b 100644 --- a/tasks/task_database_cue.c +++ b/tasks/task_database_cue.c @@ -215,10 +215,10 @@ static int detect_ps1_game_sub(intfstream_t *fp, *game_id++ = toupper(*tmp++); *game_id++ = '-'; - if (!isalnum(*tmp)) + if (!ISALNUM(*tmp)) tmp++; - while (isalnum(*tmp)) + while (ISALNUM(*tmp)) { *game_id++ = *tmp++; if (*tmp == '.')