Skip to content

Commit 496eaa6

Browse files
committed
ScratchConfiguration: Store graphic effect names as lowercase
1 parent 1b92c19 commit 496eaa6

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/scratchconfiguration.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ void ScratchConfiguration::registerGraphicsEffect(std::shared_ptr<IGraphicsEffec
3232
if (!effect)
3333
return;
3434

35-
getImpl()->graphicsEffects[effect->name()] = effect;
35+
std::string lower = effect->name();
36+
std::transform(lower.begin(), lower.end(), lower.begin(), [](char c) { return std::tolower(c); });
37+
getImpl()->graphicsEffects[lower] = effect;
3638
}
3739

3840
/*! Removes the given graphics effect. */
3941
void ScratchConfiguration::removeGraphicsEffect(const std::string &name)
4042
{
41-
getImpl()->graphicsEffects.erase(name);
43+
std::string lower = name;
44+
std::transform(lower.begin(), lower.end(), lower.begin(), [](char c) { return std::tolower(c); });
45+
getImpl()->graphicsEffects.erase(lower);
4246
}
4347

4448
/*! Returns the graphics effect with the given name, or nullptr if it isn't registered. */
@@ -48,15 +52,12 @@ IGraphicsEffect *ScratchConfiguration::getGraphicsEffect(const std::string &name
4852
std::string lower = name;
4953
std::transform(lower.begin(), lower.end(), lower.begin(), [](char c) { return std::tolower(c); });
5054

51-
const auto &effects = getImpl()->graphicsEffects;
52-
53-
for (const auto &[currentName, effect] : effects) {
54-
std::string current = currentName;
55-
std::transform(current.begin(), current.end(), current.begin(), [](char c) { return std::tolower(c); });
55+
auto it = getImpl()->graphicsEffects.find(lower);
5656

57-
if (current == lower)
58-
return effect.get();
59-
}
57+
if (it == getImpl()->graphicsEffects.cend())
58+
return nullptr;
59+
else
60+
return it->second.get();
6061

6162
return nullptr;
6263
}

test/scratchconfiguration/scratchconfiguration_test.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ TEST_F(ScratchConfigurationTest, GraphicsEffects_CaseInsensitive)
8383
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effEct2"), effect2.get());
8484
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect3"), nullptr);
8585

86-
ScratchConfiguration::removeGraphicsEffect("effect1");
8786
ScratchConfiguration::removeGraphicsEffect("effect2");
87+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect1"), effect1.get());
88+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect2"), nullptr);
89+
90+
ScratchConfiguration::removeGraphicsEffect("effEct1");
91+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect1"), nullptr);
8892
}

0 commit comments

Comments
 (0)