Add new functions and overloads to support std::u8string and std::filesystem::path.

crashfix_debugdraw
Project579 2 years ago
parent 796911e67d
commit 5456ef1d50

@ -10,6 +10,7 @@
#include <components/misc/strings/algorithm.hpp> #include <components/misc/strings/algorithm.hpp>
#include <components/files/configurationmanager.hpp> #include <components/files/configurationmanager.hpp>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/misc/strings/conversion.hpp>
#define BSATOOL_VERSION 1.1 #define BSATOOL_VERSION 1.1

@ -7,7 +7,6 @@
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/sdlutil/sdlmappings.hpp> #include <components/sdlutil/sdlmappings.hpp>
#include <components/misc/stringops.hpp>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"

@ -1,10 +1,8 @@
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/strings/conversion.hpp>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <array>
namespace namespace
{ {
using namespace testing; using namespace testing;

@ -6,7 +6,7 @@
#include <components/vfs/archive.hpp> #include <components/vfs/archive.hpp>
#include <components/vfs/manager.hpp> #include <components/vfs/manager.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/strings/conversion.hpp>
namespace TestingOpenMW namespace TestingOpenMW
{ {

@ -9,7 +9,7 @@
#include "windows_crashshm.hpp" #include "windows_crashshm.hpp"
#include <SDL_messagebox.h> #include <SDL_messagebox.h>
#include <components/misc/stringops.hpp> #include <components/misc/strings/conversion.hpp>
namespace Crash namespace Crash
{ {

@ -3,7 +3,7 @@
#include <mutex> #include <mutex>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/strings/conversion.hpp>
namespace Debug namespace Debug
{ {

@ -5,6 +5,7 @@
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/files/configfileparser.hpp> #include <components/files/configfileparser.hpp>
#include <components/fallback/validate.hpp> #include <components/fallback/validate.hpp>
#include <components/misc/strings/conversion.hpp>
#include <boost/program_options/variables_map.hpp> #include <boost/program_options/variables_map.hpp>
#include <boost/program_options/options_description.hpp> #include <boost/program_options/options_description.hpp>

@ -1,7 +1,7 @@
#include "conversion.hpp" #include "conversion.hpp"
#include <components/misc/stringops.hpp> #include <components/misc/strings/conversion.hpp>
std::string Files::pathToUnicodeString(const std::filesystem::path& path) std::string Files::pathToUnicodeString(const std::filesystem::path& path)
{ {

@ -27,6 +27,13 @@ namespace Misc::StringUtils
return std::equal(std::begin(x), std::end(x), std::begin(y), return std::equal(std::begin(x), std::end(x), std::begin(y),
[] (char l, char r) { return toLower(l) == toLower(r); }); [] (char l, char r) { return toLower(l) == toLower(r); });
} }
inline bool ciEqual(std::u8string_view x, std::u8string_view y)
{
if (std::size(x) != std::size(y))
return false;
return std::equal(std::begin(x), std::end(x), std::begin(y),
[](char l, char r) { return toLower(l) == toLower(r); });
}
inline bool ciStartsWith(std::string_view value, std::string_view prefix) inline bool ciStartsWith(std::string_view value, std::string_view prefix)
{ {
@ -114,12 +121,28 @@ namespace Misc::StringUtils
} }
return str; return str;
} }
inline std::u8string& replaceAll(std::u8string& str, std::u8string_view what, std::u8string_view with)
{
std::size_t found;
std::size_t offset = 0;
while ((found = str.find(what, offset)) != std::u8string::npos)
{
str.replace(found, what.size(), with);
offset = found + with.size();
}
return str;
}
inline bool ciEndsWith(std::string_view s, std::string_view suffix) inline bool ciEndsWith(std::string_view s, std::string_view suffix)
{ {
return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin(), return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin(),
[](char l, char r) { return toLower(l) == toLower(r); }); [](char l, char r) { return toLower(l) == toLower(r); });
} }
inline bool ciEndsWith(std::u8string_view s, std::u8string_view suffix)
{
return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin(),
[](char l, char r) { return toLower(l) == toLower(r); });
}
inline void trim(std::string& s) inline void trim(std::string& s)
{ {

@ -0,0 +1,49 @@
#ifndef COMPONENTS_MISC_STRINGS_CONVERSION_H
#define COMPONENTS_MISC_STRINGS_CONVERSION_H
#include <string>
namespace Misc::StringUtils
{
inline const char* u8StringToString(const char8_t* str)
{
return reinterpret_cast<const char*>(str);
}
inline char* u8StringToString(char8_t* str)
{
return reinterpret_cast<char*>(str);
}
inline std::string u8StringToString(std::u8string_view str)
{
return {str.begin(), str.end()};
}
inline std::string u8StringToString(std::u8string&& str)
{
return {str.begin(), str.end()};
}
inline const char8_t* stringToU8String(const char* str)
{
return reinterpret_cast<const char8_t*>(str); // Undefined behavior if the contents of "char" aren't UTF8 or ASCII.
}
inline char8_t* stringToU8String(char* str)
{
return reinterpret_cast<char8_t*>(str); // Undefined behavior if the contents of "char" aren't UTF8 or ASCII.
}
inline std::u8string stringToU8String(std::string_view str)
{
return { str.begin(), str.end() }; // Undefined behavior if the contents of "char" aren't UTF8 or ASCII.
}
inline std::u8string stringToU8String(std::string&& str)
{
return { str.begin(), str.end() }; // Undefined behavior if the contents of "char" aren't UTF8 or ASCII.
}
}
#endif //COMPONENTS_MISC_STRINGS_CONVERSION_H

@ -32,11 +32,20 @@ namespace Misc::StringUtils
{ {
return tolowermap[static_cast<unsigned char>(c)]; return tolowermap[static_cast<unsigned char>(c)];
} }
inline constexpr char8_t toLower(char8_t c)
{
return tolowermap[static_cast<unsigned char>(c)];
}
/// Transforms input string to lower case w/o copy /// Transforms input string to lower case w/o copy
inline void lowerCaseInPlace(std::string& str) inline void lowerCaseInPlace(std::string& str)
{ {
for (char& ch : str) for (auto& ch : str)
ch = toLower(ch);
}
inline void lowerCaseInPlace(std::u8string& str)
{
for (auto& ch : str)
ch = toLower(ch); ch = toLower(ch);
} }
@ -47,6 +56,12 @@ namespace Misc::StringUtils
lowerCaseInPlace(out); lowerCaseInPlace(out);
return out; return out;
} }
inline std::u8string lowerCase(std::u8string_view in)
{
std::u8string out(in);
lowerCaseInPlace(out);
return out;
}
} }
#endif #endif

Loading…
Cancel
Save