From a9eecd69989630528387af3e62849e7a35209c1f Mon Sep 17 00:00:00 2001 From: 20kdc Date: Fri, 28 Jan 2022 15:22:37 +0000 Subject: [PATCH] ApplyAbbreviations: Work around GCC optimizations being weird. --- im_mip.cc | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/im_mip.cc b/im_mip.cc index e14a0a0..4dd0031 100644 --- a/im_mip.cc +++ b/im_mip.cc @@ -81,15 +81,16 @@ rgb_image_c *MIP_LoadImage(const char *filename) return img; } - -static bool ReplacePrefix(char *name, const char *prefix, char ch) +// Last character of prefix is the character it represents. +static bool ReplacePrefix(char *name, const char *prefix) { - if (strncmp(name, prefix, strlen(prefix)) != 0) + int prefix_len = strlen(prefix) - 1; + if (strncmp(name, prefix, prefix_len) != 0) return false; - *name++ = ch; + *name++ = prefix[prefix_len]; - int move_chars = strlen(prefix) - 1; + int move_chars = prefix_len - 1; if (move_chars > 1) { @@ -102,6 +103,14 @@ static bool ReplacePrefix(char *name, const char *prefix, char ch) return true; } +static const char * abbr_prefixes[] = { + "star_*", + "plus_+", + "minu_-", + "divd_/", + NULL +}; + static void ApplyAbbreviations(char *name, bool *fullbright) { // make it lower case @@ -112,14 +121,19 @@ static void ApplyAbbreviations(char *name, bool *fullbright) if (len >= 6) { - ReplacePrefix(name, "star_", '*') - || ReplacePrefix(name, "plus_", '+') - || ReplacePrefix(name, "minu_", '-') - || ReplacePrefix(name, "divd_", '/'); + // This used to be a series of logical ORs. + // The problem is, at least as of GCC 9.3.0, the side-effects are "lost". + // So the second strlen never happened. + for (const char ** prefix = abbr_prefixes; *prefix; prefix++) + { + if (ReplacePrefix(name, *prefix)) + { + len = strlen(name); + break; + } + } } - len = strlen(name); - if (len >= 5 && memcmp(name+len-4, "_fbr", 4) == 0) { *fullbright = true;