refactors stringops.hpp (#3192)

With this PR we refactor `StringUtils::replaceAll` to accept `string_view` as suggested in a code comment. In addition, while we are touching this rebuild happy file, we slim it down a bit by moving a few sparingly used functions elsewhere.
pull/3222/head
Bo Svensson 3 years ago committed by GitHub
parent 5debd6e25a
commit 1960e976e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -313,9 +313,9 @@ struct JournalViewModelImpl : JournalViewModel
for (MWBase::Journal::TTopicIter i = journal->topicBegin (); i != journal->topicEnd (); ++i)
{
Utf8Stream stream (i->first.c_str());
Utf8Stream::UnicodeChar first = Misc::StringUtils::toLowerUtf8(stream.peek());
Utf8Stream::UnicodeChar first = Utf8Stream::toLowerUtf8(stream.peek());
if (first != Misc::StringUtils::toLowerUtf8(character))
if (first != Utf8Stream::toLowerUtf8(character))
continue;
visitor (i->second.getName());

@ -1,6 +1,7 @@
#include "sortfilteritemmodel.hpp"
#include <components/misc/stringops.hpp>
#include <components/misc/utf8stream.hpp>
#include <components/debug/debuglog.hpp>
#include <components/esm/loadalch.hpp>
#include <components/esm/loadappa.hpp>
@ -69,8 +70,8 @@ namespace
return compareType(leftType, rightType);
// compare items by name
std::string leftName = Misc::StringUtils::lowerCaseUtf8(left.mBase.getClass().getName(left.mBase));
std::string rightName = Misc::StringUtils::lowerCaseUtf8(right.mBase.getClass().getName(right.mBase));
std::string leftName = Utf8Stream::lowerCaseUtf8(left.mBase.getClass().getName(left.mBase));
std::string rightName = Utf8Stream::lowerCaseUtf8(right.mBase.getClass().getName(right.mBase));
result = leftName.compare(rightName);
if (result != 0)
@ -213,7 +214,7 @@ namespace MWGui
if (!mNameFilter.empty())
{
const auto itemName = Misc::StringUtils::lowerCaseUtf8(base.getClass().getName(base));
const auto itemName = Utf8Stream::lowerCaseUtf8(base.getClass().getName(base));
return itemName.find(mNameFilter) != std::string::npos;
}
@ -226,7 +227,7 @@ namespace MWGui
for (const auto& effect : effects)
{
const auto ciEffect = Misc::StringUtils::lowerCaseUtf8(effect);
const auto ciEffect = Utf8Stream::lowerCaseUtf8(effect);
if (ciEffect.find(mEffectFilter) != std::string::npos)
return true;
@ -285,7 +286,7 @@ namespace MWGui
return false;
}
std::string compare = Misc::StringUtils::lowerCaseUtf8(item.mBase.getClass().getName(item.mBase));
std::string compare = Utf8Stream::lowerCaseUtf8(item.mBase.getClass().getName(item.mBase));
if(compare.find(mNameFilter) == std::string::npos)
return false;
@ -318,12 +319,12 @@ namespace MWGui
void SortFilterItemModel::setNameFilter (const std::string& filter)
{
mNameFilter = Misc::StringUtils::lowerCaseUtf8(filter);
mNameFilter = Utf8Stream::lowerCaseUtf8(filter);
}
void SortFilterItemModel::setEffectFilter (const std::string& filter)
{
mEffectFilter = Misc::StringUtils::lowerCaseUtf8(filter);
mEffectFilter = Utf8Stream::lowerCaseUtf8(filter);
}
void SortFilterItemModel::update()

@ -1,6 +1,7 @@
#include "spellmodel.hpp"
#include <components/debug/debuglog.hpp>
#include <components/misc/utf8stream.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
@ -69,7 +70,7 @@ namespace MWGui
fullEffectName += " " + wm->getGameSettingString(ESM::Attribute::sGmstAttributeIds[effect.mAttribute], "");
}
std::string convert = Misc::StringUtils::lowerCaseUtf8(fullEffectName);
std::string convert = Utf8Stream::lowerCaseUtf8(fullEffectName);
if (convert.find(filter) != std::string::npos)
{
return true;
@ -90,14 +91,14 @@ namespace MWGui
const MWWorld::ESMStore &esmStore =
MWBase::Environment::get().getWorld()->getStore();
std::string filter = Misc::StringUtils::lowerCaseUtf8(mFilter);
std::string filter = Utf8Stream::lowerCaseUtf8(mFilter);
for (const ESM::Spell* spell : spells)
{
if (spell->mData.mType != ESM::Spell::ST_Power && spell->mData.mType != ESM::Spell::ST_Spell)
continue;
std::string name = Misc::StringUtils::lowerCaseUtf8(spell->mName);
std::string name = Utf8Stream::lowerCaseUtf8(spell->mName);
if (name.find(filter) == std::string::npos
&& !matchingEffectExists(filter, spell->mEffects))
@ -139,7 +140,7 @@ namespace MWGui
if (enchant->mData.mType != ESM::Enchantment::WhenUsed && enchant->mData.mType != ESM::Enchantment::CastOnce)
continue;
std::string name = Misc::StringUtils::lowerCaseUtf8(item.getClass().getName(item));
std::string name = Utf8Stream::lowerCaseUtf8(item.getClass().getName(item));
if (name.find(filter) == std::string::npos
&& !matchingEffectExists(filter, enchant->mEffects))

@ -1,5 +1,7 @@
#include "actions.hpp"
#include <cstring>
#include <components/debug/debuglog.hpp>
#include "../mwworld/cellstore.hpp"

@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include "components/misc/stringops.hpp"
#include "components/misc/algorithm.hpp"
#include <string>
#include <string_view>
@ -18,7 +19,7 @@ struct PartialBinarySearchTest : public ::testing::Test
bool matches(const std::string& keyword)
{
return Misc::StringUtils::partialBinarySearch(mDataVec.begin(), mDataVec.end(), keyword) != mDataVec.end();
return Misc::partialBinarySearch(mDataVec.begin(), mDataVec.end(), keyword) != mDataVec.end();
}
};

@ -29,10 +29,10 @@ namespace Files
std::string temp = str;
static const char hash[] = { escape_hash_filter::sEscape, escape_hash_filter::sHashIdentifier };
Misc::StringUtils::replaceAll(temp, hash, "#", 2, 1);
Misc::StringUtils::replaceAll(temp, std::string_view(hash, 2), "#");
static const char escape[] = { escape_hash_filter::sEscape, escape_hash_filter::sEscapeIdentifier };
Misc::StringUtils::replaceAll(temp, escape, "@", 2, 1);
Misc::StringUtils::replaceAll(temp, std::string_view(escape, 2), "@");
return temp;
}

@ -4,6 +4,8 @@
#include <iterator>
#include <type_traits>
#include "stringops.hpp"
namespace Misc
{
template <typename Iterator, typename BinaryPredicate, typename Function>
@ -31,6 +33,30 @@ namespace Misc
}
return begin;
}
/// Performs a binary search on a sorted container for a string that 'key' starts with
template<typename Iterator, typename T>
static Iterator partialBinarySearch(Iterator begin, Iterator end, const T& key)
{
const Iterator notFound = end;
while(begin < end)
{
const Iterator middle = begin + (std::distance(begin, end) / 2);
int comp = Misc::StringUtils::ciCompareLen((*middle), key, (*middle).size());
if(comp == 0)
return middle;
else if(comp > 0)
end = middle;
else
begin = middle + 1;
}
return notFound;
}
}
#endif

@ -8,8 +8,6 @@
#include <iterator>
#include <functional>
#include "utf8stream.hpp"
namespace Misc
{
class StringUtils
@ -45,70 +43,6 @@ public:
return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
}
static Utf8Stream::UnicodeChar toLowerUtf8(Utf8Stream::UnicodeChar ch)
{
// Russian alphabet
if (ch >= 0x0410 && ch < 0x0430)
return ch + 0x20;
// Cyrillic IO character
if (ch == 0x0401)
return ch + 0x50;
// Latin alphabet
if (ch >= 0x41 && ch < 0x60)
return ch + 0x20;
// Deutch characters
if (ch == 0xc4 || ch == 0xd6 || ch == 0xdc)
return ch + 0x20;
if (ch == 0x1e9e)
return 0xdf;
// TODO: probably we will need to support characters from other languages
return ch;
}
static std::string lowerCaseUtf8(const std::string& str)
{
if (str.empty())
return str;
// Decode string as utf8 characters, convert to lower case and pack them to string
std::string out;
Utf8Stream stream (str.c_str());
while (!stream.eof ())
{
Utf8Stream::UnicodeChar character = toLowerUtf8(stream.peek());
if (character <= 0x7f)
out.append(1, static_cast<char>(character));
else if (character <= 0x7ff)
{
out.append(1, static_cast<char>(0xc0 | ((character >> 6) & 0x1f)));
out.append(1, static_cast<char>(0x80 | (character & 0x3f)));
}
else if (character <= 0xffff)
{
out.append(1, static_cast<char>(0xe0 | ((character >> 12) & 0x0f)));
out.append(1, static_cast<char>(0x80 | ((character >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (character & 0x3f)));
}
else
{
out.append(1, static_cast<char>(0xf0 | ((character >> 18) & 0x07)));
out.append(1, static_cast<char>(0x80 | ((character >> 12) & 0x3f)));
out.append(1, static_cast<char>(0x80 | ((character >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (character & 0x3f)));
}
stream.consume();
}
return out;
}
static bool ciLess(const std::string &x, const std::string &y) {
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), ci());
}
@ -207,55 +141,21 @@ public:
}
};
/// Performs a binary search on a sorted container for a string that 'key' starts with
template<typename Iterator, typename T>
static Iterator partialBinarySearch(Iterator begin, Iterator end, const T& key)
{
const Iterator notFound = end;
while(begin < end)
{
const Iterator middle = begin + (std::distance(begin, end) / 2);
int comp = Misc::StringUtils::ciCompareLen((*middle), key, (*middle).size());
if(comp == 0)
return middle;
else if(comp > 0)
end = middle;
else
begin = middle + 1;
}
return notFound;
}
/** @brief Replaces all occurrences of a string in another string.
*
* @param str The string to operate on.
* @param what The string to replace.
* @param with The replacement string.
* @param whatLen The length of the string to replace.
* @param withLen The length of the replacement string.
*
* @return A reference to the string passed in @p str.
*/
static std::string &replaceAll(std::string &str, const char *what, const char *with,
std::size_t whatLen=std::string::npos, std::size_t withLen=std::string::npos)
static std::string &replaceAll(std::string &str, std::string_view what, std::string_view with)
{
if (whatLen == std::string::npos)
whatLen = strlen(what);
if (withLen == std::string::npos)
withLen = strlen(with);
std::size_t found;
std::size_t offset = 0;
while((found = str.find(what, offset, whatLen)) != std::string::npos)
while((found = str.find(what, offset)) != std::string::npos)
{
str.replace(found, whatLen, with, withLen);
offset = found + withLen;
str.replace(found, what.size(), with);
offset = found + with.size();
}
return str;
}
@ -311,26 +211,11 @@ public:
cont.push_back(str.substr(previous, current - previous));
}
// TODO: use the std::string_view once we will use the C++17.
// It should allow us to avoid data copying while we still will support both string and literal arguments.
static inline void replaceAll(std::string& data, const std::string& toSearch, const std::string& replaceStr)
{
size_t pos = data.find(toSearch);
while( pos != std::string::npos)
{
data.replace(pos, toSearch.size(), replaceStr);
pos = data.find(toSearch, pos + replaceStr.size());
}
}
static inline void replaceLast(std::string& str, const std::string& substr, const std::string& with)
{
size_t pos = str.rfind(substr);
if (pos == std::string::npos)
return;
str.replace(pos, substr.size(), with);
}

@ -2,6 +2,7 @@
#define MISC_UTF8ITER_HPP
#include <cstring>
#include <string>
#include <tuple>
class Utf8Stream
@ -87,6 +88,70 @@ public:
return std::make_pair (chr, cur);
}
static UnicodeChar toLowerUtf8(UnicodeChar ch)
{
// Russian alphabet
if (ch >= 0x0410 && ch < 0x0430)
return ch + 0x20;
// Cyrillic IO character
if (ch == 0x0401)
return ch + 0x50;
// Latin alphabet
if (ch >= 0x41 && ch < 0x60)
return ch + 0x20;
// German characters
if (ch == 0xc4 || ch == 0xd6 || ch == 0xdc)
return ch + 0x20;
if (ch == 0x1e9e)
return 0xdf;
// TODO: probably we will need to support characters from other languages
return ch;
}
static std::string lowerCaseUtf8(const std::string& str)
{
if (str.empty())
return str;
// Decode string as utf8 characters, convert to lower case and pack them to string
std::string out;
Utf8Stream stream (str.c_str());
while (!stream.eof ())
{
UnicodeChar character = toLowerUtf8(stream.peek());
if (character <= 0x7f)
out.append(1, static_cast<char>(character));
else if (character <= 0x7ff)
{
out.append(1, static_cast<char>(0xc0 | ((character >> 6) & 0x1f)));
out.append(1, static_cast<char>(0x80 | (character & 0x3f)));
}
else if (character <= 0xffff)
{
out.append(1, static_cast<char>(0xe0 | ((character >> 12) & 0x0f)));
out.append(1, static_cast<char>(0x80 | ((character >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (character & 0x3f)));
}
else
{
out.append(1, static_cast<char>(0xf0 | ((character >> 18) & 0x07)));
out.append(1, static_cast<char>(0x80 | ((character >> 12) & 0x3f)));
out.append(1, static_cast<char>(0x80 | ((character >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (character & 0x3f)));
}
stream.consume();
}
return out;
}
private:
static std::pair <int, UnicodeChar> octet_count (unsigned char octet)

@ -20,6 +20,7 @@
#include <components/misc/pathhelpers.hpp>
#include <components/misc/stringops.hpp>
#include <components/misc/algorithm.hpp>
#include <components/vfs/manager.hpp>
@ -551,7 +552,7 @@ namespace Resource
std::sort(reservedNames.begin(), reservedNames.end(), Misc::StringUtils::ciLess);
}
std::vector<std::string>::iterator it = Misc::StringUtils::partialBinarySearch(reservedNames.begin(), reservedNames.end(), name);
std::vector<std::string>::iterator it = Misc::partialBinarySearch(reservedNames.begin(), reservedNames.end(), name);
return it != reservedNames.end();
}

@ -1,6 +1,7 @@
#include "lightmanager.hpp"
#include <array>
#include <cstring>
#include <algorithm>
#include <iterator>

Loading…
Cancel
Save