From 73fdac6308300a03ac4fb3b40635e7e376527cd9 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Fri, 8 Mar 2024 14:14:40 -0600 Subject: [PATCH] Make ColorFromXOrgAppColorName both smaller and more correct (#16824) We don't need to use `stringstream` to generate a ten-character string, and we for _sure_ don't need to use the locale-aware ctype functions after we just wrote a comment saying "XOrg colors are always Latin-1" | Size Diff | Object | Library | | --------- | -------------- | -------- | | -11.8 KB | colorTable.obj | ConTypes | --- src/types/colorTable.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/types/colorTable.cpp b/src/types/colorTable.cpp index f21b8586217..52f043e34e0 100644 --- a/src/types/colorTable.cpp +++ b/src/types/colorTable.cpp @@ -464,7 +464,7 @@ void Utils::InitializeColorTable(const std::span table) std::optional Utils::ColorFromXOrgAppColorName(const std::wstring_view wstr) noexcept try { - std::stringstream ss; + std::string stem; size_t variantIndex = 0; auto foundVariant = false; for (const auto c : wstr) @@ -485,7 +485,7 @@ try } // Ignore spaces. - if (std::iswspace(c)) + if (c == L' ' || c == L'\f' || c == L'\n' || c == L'\r' || c == L'\t' || c == L'\v') { continue; } @@ -498,11 +498,10 @@ try return std::nullopt; } - ss << gsl::narrow_cast(std::towlower(c)); + stem += gsl::narrow_cast(til::tolower_ascii(c)); } - auto name = ss.str(); - const auto variantColorIter = xorgAppVariantColorTable.find(name); + const auto variantColorIter = xorgAppVariantColorTable.find(stem); if (variantColorIter != xorgAppVariantColorTable.end()) { const auto colors = variantColorIter->second; @@ -513,7 +512,7 @@ try } // Calculate the color value for gray0 - gray99. - if ((name == "gray" || name == "grey") && foundVariant) + if ((stem == "gray" || stem == "grey") && foundVariant) { if (variantIndex > 100) // size_t is unsigned, so >=0 is implicit { @@ -523,7 +522,7 @@ try return til::color{ component, component, component }; } - const auto colorIter = xorgAppColorTable.find(name); + const auto colorIter = xorgAppColorTable.find(stem); if (colorIter != xorgAppColorTable.end()) { return colorIter->second;