Skip to content

Commit

Permalink
Make ColorFromXOrgAppColorName both smaller and more correct (#16824)
Browse files Browse the repository at this point in the history
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 |
  • Loading branch information
DHowett authored Mar 8, 2024
1 parent 0ba680a commit 73fdac6
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/types/colorTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ void Utils::InitializeColorTable(const std::span<COLORREF> table)
std::optional<til::color> 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)
Expand All @@ -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;
}
Expand All @@ -498,11 +498,10 @@ try
return std::nullopt;
}

ss << gsl::narrow_cast<char>(std::towlower(c));
stem += gsl::narrow_cast<char>(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;
Expand All @@ -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
{
Expand All @@ -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;
Expand Down

0 comments on commit 73fdac6

Please sign in to comment.