Replace ciCompareLen with ciStartsWith where possible

`ciCompareLen(a, b, b.size()) == 0` expression is an equivalent of checking for
equality of `a` prefix with size `b.size()` with `b`.

`ciCompareLen(a, b, a.size()) == 0` is also the same thing but `a` is a prefix
`b` should start with.
crashfix_debugdraw
elsid 2 years ago
parent bf0865d03d
commit 5dc612aa54
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -205,7 +205,7 @@ namespace MWClass
{
const std::string_view soulgemPrefix = "misc_soulgem";
if (::Misc::StringUtils::ciCompareLen(ptr.getCellRef().getRefId(), soulgemPrefix, soulgemPrefix.length()) == 0)
if (Misc::StringUtils::ciStartsWith(ptr.getCellRef().getRefId(), soulgemPrefix))
return std::make_unique<MWWorld::ActionSoulgem>(ptr);
return std::make_unique<MWWorld::NullAction>();

@ -309,7 +309,7 @@ namespace MWScript
const std::string soulgemPrefix = "misc_soulgem";
if (::Misc::StringUtils::ciCompareLen(item, soulgemPrefix, soulgemPrefix.length()) == 0)
if (Misc::StringUtils::ciStartsWith(item, soulgemPrefix))
{
it = invStore.end();

@ -147,10 +147,7 @@ namespace MWWorld
{
std::vector<const T*> results;
std::copy_if(mShared.begin(), mShared.end(), std::back_inserter(results),
[&id](const T* item)
{
return Misc::StringUtils::ciCompareLen(id, item->mId, id.size()) == 0;
});
[&id](const T* item) { return Misc::StringUtils::ciStartsWith(item->mId, id); });
if(!results.empty())
return results[Misc::Rng::rollDice(results.size(), prng)];
return nullptr;

@ -151,4 +151,35 @@ namespace
{
EXPECT_FALSE(ciEqual(std::string("a"), std::string("aa")));
}
TEST(MiscStringsCiStartsWith, empty_string_should_start_with_empty_prefix)
{
EXPECT_TRUE(ciStartsWith(std::string_view(), std::string_view()));
}
TEST(MiscStringsCiStartsWith, string_should_start_with_empty_prefix)
{
EXPECT_TRUE(ciStartsWith("foo", std::string_view()));
}
TEST(MiscStringsCiStartsWith, string_should_start_with_own_prefix)
{
std::string string = "some string";
EXPECT_TRUE(ciStartsWith(string, string.substr(0, 4)));
}
TEST(MiscStringsCiStartsWith, string_should_start_with_the_same_value)
{
EXPECT_TRUE(ciStartsWith("foo", "foo"));
}
TEST(MiscStringsCiStartsWith, string_should_not_start_with_not_its_prefix)
{
EXPECT_FALSE(ciStartsWith("some string", "foo"));
}
TEST(MiscStringsCiStartsWith, string_should_not_start_with_longer_string_having_matching_prefix)
{
EXPECT_FALSE(ciStartsWith("foo", "foo bar"));
}
}

@ -28,6 +28,11 @@ namespace Misc::StringUtils
[] (char l, char r) { return toLower(l) == toLower(r); });
}
inline bool ciStartsWith(std::string_view value, std::string_view prefix)
{
return ciEqual(value.substr(0, prefix.size()), prefix);
}
inline int ciCompareLen(std::string_view x, std::string_view y, std::size_t len)
{
std::string_view::const_iterator xit = x.begin();

@ -373,7 +373,7 @@ void BulletNifLoader::handleNode(const std::string& fileName, const Nif::Node& n
// affecting the entire subtree of this node
Nif::NiStringExtraData *sd = (Nif::NiStringExtraData*)e.getPtr();
if (Misc::StringUtils::ciCompareLen(sd->string, "NC", 2) == 0)
if (Misc::StringUtils::ciStartsWith(sd->string, "NC"))
{
// NCC flag in vanilla is partly case sensitive: prefix NC is case insensitive but second C needs be uppercase
if (sd->string.length() > 2 && sd->string[2] == 'C')

@ -72,10 +72,11 @@ namespace SceneUtil
bool filterMatches(std::string_view name) const
{
if (Misc::StringUtils::ciCompareLen(name, mFilter, mFilter.size()) == 0)
if (Misc::StringUtils::ciStartsWith(name, mFilter))
return true;
if (Misc::StringUtils::ciCompareLen(name, "tri ", 4) == 0)
return Misc::StringUtils::ciCompareLen(name.substr(4), mFilter, mFilter.size()) == 0;
constexpr std::string_view prefix = "tri ";
if (Misc::StringUtils::ciStartsWith(name, prefix))
return Misc::StringUtils::ciStartsWith(name.substr(prefix.size()), mFilter);
return false;
}

@ -144,8 +144,7 @@ namespace SceneUtil
void RemoveTriBipVisitor::applyImpl(osg::Node& node)
{
const std::string_view toFind = "tri bip";
if (Misc::StringUtils::ciCompareLen(node.getName(), toFind, toFind.size()) == 0)
if (Misc::StringUtils::ciStartsWith(node.getName(), "tri bip"))
{
osg::Group* parent = static_cast<osg::Group*>(*(getNodePath().end()-2));
// Not safe to remove in apply(), since the visitor is still iterating the child list

Loading…
Cancel
Save