From ea2f129577f49f48f441f0a4c494041ab0c0fd75 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 27 May 2024 14:37:25 +0200 Subject: [PATCH] string: stop abusing logical | to load operands and test for zero Looks short and compact, but requires static_cast to cover T1 and T2 of different signess. Which doesn't look all that nice anymore. However, typecasting would only hide the warning. We want the warning to occur, to encourage us to sync types on template invocation instead. Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 52 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index 1a65585fe..66ad0b2bf 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -926,8 +926,11 @@ namespace stdex { _Assume_(str1 || !count1); _Assume_(str2 || !count2); - size_t i; T1 a; T2 b; - for (i = 0; i < count1 && i < count2 && ((a = str1[i]) | (b = str2[i])); ++i) { + size_t i; + for (i = 0; i < count1 && i < count2; ++i) { + auto a = str1[i]; + auto b = str2[i]; + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; } @@ -1122,14 +1125,14 @@ namespace stdex { _Assume_(str1); _Assume_(str2); - size_t i; T1 a; T2 b; - for (i = 0; (a = tolower(str1[i])) | (b = tolower(str2[i])); ++i) { + size_t i; + for (i = 0; ; ++i) { + auto a = tolower(str1[i]); + auto b = tolower(str2[i]); + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; } - if (str1[i]) return +1; - if (str2[i]) return -1; - return 0; } /// @@ -1146,10 +1149,13 @@ namespace stdex { _Assume_(str1); _Assume_(str2); - size_t i; T1 a; T2 b; + size_t i; const auto& ctype1 = std::use_facet>(locale); const auto& ctype2 = std::use_facet>(locale); - for (i = 0; (a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i])); ++i) { + for (i = 0;; ++i) { + auto a = ctype1.tolower(str1[i]); + auto b = ctype2.tolower(str2[i]); + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; } @@ -1172,8 +1178,11 @@ namespace stdex { _Assume_(str1 || !count); _Assume_(str2 || !count); - size_t i; T1 a; T2 b; - for (i = 0; i < count && ((a = tolower(str1[i])) | (b = tolower(str2[i]))); ++i) { + size_t i; + for (i = 0; i < count; ++i) { + auto a = tolower(str1[i]); + auto b = tolower(str2[i]); + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; } @@ -1197,10 +1206,13 @@ namespace stdex { _Assume_(str1 || !count); _Assume_(str2 || !count); - size_t i; T1 a; T2 b; + size_t i; const auto& ctype1 = std::use_facet>(locale); const auto& ctype2 = std::use_facet>(locale); - for (i = 0; i < count && ((a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i]))); ++i) { + for (i = 0; i < count; ++i) { + auto a = ctype1.tolower(str1[i]); + auto b = ctype2.tolower(str2[i]); + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; } @@ -1226,8 +1238,11 @@ namespace stdex { _Assume_(str1 || !count1); _Assume_(str2 || !count2); - size_t i; T1 a; T2 b; - for (i = 0; i < count1 && i < count2 && ((a = tolower(str1[i])) | (b = tolower(str2[i]))); ++i) { + size_t i; + for (i = 0; i < count1 && i < count2; ++i) { + auto a = tolower(str1[i]); + auto b = tolower(str2[i]); + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; } @@ -1255,10 +1270,13 @@ namespace stdex { _Assume_(str1 || !count1); _Assume_(str2 || !count2); - size_t i; T1 a; T2 b; + size_t i; const auto& ctype1 = std::use_facet>(locale); const auto& ctype2 = std::use_facet>(locale); - for (i = 0; i < count1 && i < count2 && ((a = ctype1.tolower(str1[i])) | (b = ctype2.tolower(str2[i]))); ++i) { + for (i = 0; i < count1 && i < count2; ++i) { + auto a = ctype1.tolower(str1[i]); + auto b = ctype2.tolower(str2[i]); + if (!a && !b) return 0; if (a > b) return +1; if (a < b) return -1; }