From 5456ef1d50aeff97e0c9027003892bfba3f3ff97 Mon Sep 17 00:00:00 2001 From: Project579 Date: Sun, 11 Sep 2022 03:26:06 +0200 Subject: [PATCH] Add new functions and overloads to support std::u8string and std::filesystem::path. --- apps/bsatool/bsatool.cpp | 1 + apps/openmw/mwinput/controllermanager.cpp | 1 - .../files/conversion_tests.cpp | 4 +- apps/openmw_test_suite/testing_util.hpp | 2 +- .../crashcatcher/windows_crashcatcher.cpp | 2 +- components/debug/debuglog.cpp | 2 +- components/files/configurationmanager.cpp | 1 + components/files/conversion.cpp | 2 +- components/misc/strings/algorithm.hpp | 23 +++++++++ components/misc/strings/conversion.hpp | 49 +++++++++++++++++++ components/misc/strings/lower.hpp | 17 ++++++- 11 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 components/misc/strings/conversion.hpp diff --git a/apps/bsatool/bsatool.cpp b/apps/bsatool/bsatool.cpp index 3af5e08ecb..0f6a7d35e7 100644 --- a/apps/bsatool/bsatool.cpp +++ b/apps/bsatool/bsatool.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #define BSATOOL_VERSION 1.1 diff --git a/apps/openmw/mwinput/controllermanager.cpp b/apps/openmw/mwinput/controllermanager.cpp index 96d14a3720..d6ed81cd15 100644 --- a/apps/openmw/mwinput/controllermanager.cpp +++ b/apps/openmw/mwinput/controllermanager.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include "../mwbase/environment.hpp" diff --git a/apps/openmw_test_suite/files/conversion_tests.cpp b/apps/openmw_test_suite/files/conversion_tests.cpp index ec0ec9fb23..6c9d801b9c 100644 --- a/apps/openmw_test_suite/files/conversion_tests.cpp +++ b/apps/openmw_test_suite/files/conversion_tests.cpp @@ -1,10 +1,8 @@ #include -#include +#include #include -#include - namespace { using namespace testing; diff --git a/apps/openmw_test_suite/testing_util.hpp b/apps/openmw_test_suite/testing_util.hpp index ce364d6db6..767f775c6b 100644 --- a/apps/openmw_test_suite/testing_util.hpp +++ b/apps/openmw_test_suite/testing_util.hpp @@ -6,7 +6,7 @@ #include #include -#include +#include namespace TestingOpenMW { diff --git a/components/crashcatcher/windows_crashcatcher.cpp b/components/crashcatcher/windows_crashcatcher.cpp index 47f1ba6c2c..46a09f43f6 100644 --- a/components/crashcatcher/windows_crashcatcher.cpp +++ b/components/crashcatcher/windows_crashcatcher.cpp @@ -9,7 +9,7 @@ #include "windows_crashshm.hpp" #include -#include +#include namespace Crash { diff --git a/components/debug/debuglog.cpp b/components/debug/debuglog.cpp index bb0c4b57e8..bb67847f6e 100644 --- a/components/debug/debuglog.cpp +++ b/components/debug/debuglog.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include namespace Debug { diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 8f4bc12c6b..fdd955207f 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include diff --git a/components/files/conversion.cpp b/components/files/conversion.cpp index ab32158ff9..fa9f2a5fb3 100644 --- a/components/files/conversion.cpp +++ b/components/files/conversion.cpp @@ -1,7 +1,7 @@ #include "conversion.hpp" -#include +#include std::string Files::pathToUnicodeString(const std::filesystem::path& path) { diff --git a/components/misc/strings/algorithm.hpp b/components/misc/strings/algorithm.hpp index 3fcaf12709..04fe81b195 100644 --- a/components/misc/strings/algorithm.hpp +++ b/components/misc/strings/algorithm.hpp @@ -27,6 +27,13 @@ namespace Misc::StringUtils return std::equal(std::begin(x), std::end(x), std::begin(y), [] (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) { @@ -114,12 +121,28 @@ namespace Misc::StringUtils } 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) { return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin(), [](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) { diff --git a/components/misc/strings/conversion.hpp b/components/misc/strings/conversion.hpp new file mode 100644 index 0000000000..b1d88e69d9 --- /dev/null +++ b/components/misc/strings/conversion.hpp @@ -0,0 +1,49 @@ +#ifndef COMPONENTS_MISC_STRINGS_CONVERSION_H +#define COMPONENTS_MISC_STRINGS_CONVERSION_H + +#include + +namespace Misc::StringUtils +{ + inline const char* u8StringToString(const char8_t* str) + { + return reinterpret_cast(str); + } + + inline char* u8StringToString(char8_t* str) + { + return reinterpret_cast(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(str); // Undefined behavior if the contents of "char" aren't UTF8 or ASCII. + } + + inline char8_t* stringToU8String(char* str) + { + return reinterpret_cast(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 \ No newline at end of file diff --git a/components/misc/strings/lower.hpp b/components/misc/strings/lower.hpp index 8486af7c6d..d17ca6aa00 100644 --- a/components/misc/strings/lower.hpp +++ b/components/misc/strings/lower.hpp @@ -32,11 +32,20 @@ namespace Misc::StringUtils { return tolowermap[static_cast(c)]; } + inline constexpr char8_t toLower(char8_t c) + { + return tolowermap[static_cast(c)]; + } /// Transforms input string to lower case w/o copy 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); } @@ -47,6 +56,12 @@ namespace Misc::StringUtils lowerCaseInPlace(out); return out; } + inline std::u8string lowerCase(std::u8string_view in) + { + std::u8string out(in); + lowerCaseInPlace(out); + return out; + } } #endif