From fc4a07c8222e63bfdf432ffc70b25de95f6f4cc6 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Mon, 6 Jan 2020 13:04:57 +0100 Subject: [PATCH] Roll custom versions of isalpha/isalnum. Avoids locale issues and avoids unexpected assertions for non-ASCII values. --- spirv_cross_parsed_ir.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/spirv_cross_parsed_ir.cpp b/spirv_cross_parsed_ir.cpp index 23872671..4b65b4e0 100644 --- a/spirv_cross_parsed_ir.cpp +++ b/spirv_cross_parsed_ir.cpp @@ -128,6 +128,17 @@ void ParsedIR::set_id_bounds(uint32_t bounds) block_meta.resize(bounds); } +// Roll our own versions of these functions to avoid potential locale shenanigans. +static bool is_alpha(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +static bool is_alphanumeric(char c) +{ + return is_alpha(c) || (c >= '0' && c <= '9'); +} + static string ensure_valid_identifier(const string &name, bool member) { // Functions in glslangValidator are mangled with name( stuff. @@ -143,20 +154,20 @@ static string ensure_valid_identifier(const string &name, bool member) // _m variables are reserved by the internal implementation, // otherwise, make sure the name is a valid identifier. if (i == 0) - c = isalpha(c) ? c : '_'; + c = is_alpha(c) ? c : '_'; else if (i == 2 && str[0] == '_' && str[1] == 'm') - c = isalpha(c) ? c : '_'; + c = is_alpha(c) ? c : '_'; else - c = isalnum(c) ? c : '_'; + c = is_alphanumeric(c) ? c : '_'; } else { // _ variables are reserved by the internal implementation, // otherwise, make sure the name is a valid identifier. if (i == 0 || (str[0] == '_' && i == 1)) - c = isalpha(c) ? c : '_'; + c = is_alpha(c) ? c : '_'; else - c = isalnum(c) ? c : '_'; + c = is_alphanumeric(c) ? c : '_'; } } return str;