From 53cd75525b061550a466b67bd0a8069d0e6b8f5b Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 12 Nov 2017 20:18:30 -1000 Subject: [PATCH] string_view refactor --- include/kabufuda/Card.hpp | 6 +-- include/kabufuda/Util.hpp | 62 +++++++++++--------------- include/kabufuda/WideStringConvert.hpp | 4 +- lib/kabufuda/Card.cpp | 6 +-- lib/kabufuda/WideStringConvert.cpp | 6 +-- 5 files changed, 37 insertions(+), 47 deletions(-) diff --git a/include/kabufuda/Card.hpp b/include/kabufuda/Card.hpp index aac6ae9..6961508 100644 --- a/include/kabufuda/Card.hpp +++ b/include/kabufuda/Card.hpp @@ -157,7 +157,7 @@ public: * @param game * @param maker */ - Card(const SystemString& filepath, const char* game = nullptr, const char* maker = nullptr); + Card(SystemStringView filepath, const char* game = nullptr, const char* maker = nullptr); ~Card(); /** @@ -409,7 +409,7 @@ public: * @brief Returns basic stats about a card image without opening a handle * @return ProbeResults structure */ - static ProbeResults probeCardFile(const SystemString& filename); + static ProbeResults probeCardFile(SystemStringView filename); /** * @brief Writes any changes to the Card instance immediately to disk.
@@ -425,7 +425,7 @@ public: /** * @brief Access host filename of card */ - const SystemString& cardFilename() const { return m_filename; } + SystemStringView cardFilename() const { return m_filename; } /** * @brief Gets card-scope error state diff --git a/include/kabufuda/Util.hpp b/include/kabufuda/Util.hpp index 8cfeb1b..92d30f6 100644 --- a/include/kabufuda/Util.hpp +++ b/include/kabufuda/Util.hpp @@ -184,36 +184,31 @@ static inline double SBig(double val) { return val; } typedef wchar_t SystemChar; static inline size_t StrLen(const SystemChar* str) { return wcslen(str); } typedef std::wstring SystemString; +typedef std::wstring_view SystemStringView; static inline void ToLower(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towlower); } static inline void ToUpper(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towupper); } -class SystemUTF8View +class SystemUTF8Conv { std::string m_utf8; public: - explicit SystemUTF8View(const SystemString& str) : m_utf8(WideToUTF8(str)) {} - operator const std::string&() const { return m_utf8; } - const std::string& str() const { return m_utf8; } + explicit SystemUTF8Conv(SystemStringView str) : m_utf8(WideToUTF8(str)) {} + std::string_view str() const { return m_utf8; } const char* c_str() const { return m_utf8.c_str(); } - std::string operator+(const std::string& other) const { return m_utf8 + other; } - std::string operator+(const char* other) const { return m_utf8 + other; } + std::string operator+(std::string_view other) const { return m_utf8 + other; } }; -inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } -inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } -class SystemStringView +inline std::string operator+(std::string_view lhs, const SystemUTF8Conv& rhs) { return lhs + std::string(rhs); } +class SystemStringConv { std::wstring m_sys; public: - explicit SystemStringView(const std::string& str) : m_sys(UTF8ToWide(str)) {} - operator const std::wstring&() const { return m_sys; } - const std::wstring& sys_str() const { return m_sys; } + explicit SystemStringConv(std::string_view str) : m_sys(UTF8ToWide(str)) {} + SystemStringView sys_str() const { return m_sys; } const SystemChar* c_str() const { return m_sys.c_str(); } - std::wstring operator+(const std::wstring& other) const { return m_sys + other; } - std::wstring operator+(const wchar_t* other) const { return m_sys + other; } + std::wstring operator+(const std::wstring_view other) const { return m_sys + other; } }; -inline std::wstring operator+(const std::wstring& lhs, const SystemStringView& rhs) { return lhs + std::wstring(rhs); } -inline std::wstring operator+(const wchar_t* lhs, const SystemStringView& rhs) { return lhs + std::wstring(rhs); } +inline std::wstring operator+(const std::wstring_view lhs, const SystemStringConv& rhs) { return lhs + std::wstring(rhs); } #ifndef _S #define _S(val) L##val #endif @@ -222,36 +217,31 @@ typedef struct _stat Sstat; typedef char SystemChar; static inline size_t StrLen(const SystemChar* str) { return strlen(str); } typedef std::string SystemString; +typedef std::string_view SystemStringView; static inline void ToLower(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), tolower); } static inline void ToUpper(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), toupper); } -class SystemUTF8View +class SystemUTF8Conv { - const std::string& m_utf8; + std::string_view m_utf8; public: - explicit SystemUTF8View(const SystemString& str) : m_utf8(str) {} - operator const std::string&() const { return m_utf8; } - const std::string& str() const { return m_utf8; } - const char* c_str() const { return m_utf8.c_str(); } - std::string operator+(const std::string& other) const { return std::string(m_utf8) + other; } - std::string operator+(const char* other) const { return std::string(m_utf8) + other; } + explicit SystemUTF8Conv(SystemStringView str) : m_utf8(str) {} + std::string_view str() const { return m_utf8; } + const char* c_str() const { return m_utf8.data(); } + std::string operator+(std::string_view other) const { return std::string(m_utf8) + other.data(); } }; -inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } -inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } -class SystemStringView +inline std::string operator+(std::string_view lhs, const SystemUTF8Conv& rhs) { return std::string(lhs) + rhs.c_str(); } +class SystemStringConv { - const std::string& m_sys; + SystemStringView m_sys; public: - explicit SystemStringView(const std::string& str) : m_sys(str) {} - operator const std::string&() const { return m_sys; } - const std::string& sys_str() const { return m_sys; } - const SystemChar* c_str() const { return m_sys.c_str(); } - std::string operator+(const std::string& other) const { return m_sys + other; } - std::string operator+(const char* other) const { return m_sys + other; } + explicit SystemStringConv(std::string_view str) : m_sys(str) {} + SystemStringView sys_str() const { return m_sys; } + const SystemChar* c_str() const { return m_sys.data(); } + std::string operator+(std::string_view other) const { return std::string(m_sys) + other.data(); } }; -inline std::string operator+(const std::string& lhs, const SystemStringView& rhs) { return lhs + std::string(rhs); } -inline std::string operator+(const char* lhs, const SystemStringView& rhs) { return lhs + std::string(rhs); } +inline std::string operator+(std::string_view lhs, const SystemStringConv& rhs) { return std::string(lhs) + rhs.c_str(); } #ifndef _S #define _S(val) val #endif diff --git a/include/kabufuda/WideStringConvert.hpp b/include/kabufuda/WideStringConvert.hpp index 44757a9..795fcc2 100644 --- a/include/kabufuda/WideStringConvert.hpp +++ b/include/kabufuda/WideStringConvert.hpp @@ -5,8 +5,8 @@ namespace kabufuda { -std::string WideToUTF8(const std::wstring& src); -std::wstring UTF8ToWide(const std::string& src); +std::string WideToUTF8(std::wstring_view src); +std::wstring UTF8ToWide(std::string_view src); } #endif // __KABU_WIDESTRINGCONVERT_HPP__ diff --git a/lib/kabufuda/Card.cpp b/lib/kabufuda/Card.cpp index abe0e94..13f7348 100644 --- a/lib/kabufuda/Card.cpp +++ b/lib/kabufuda/Card.cpp @@ -71,7 +71,7 @@ Card& Card::operator=(Card&& other) return *this; } -Card::Card(const SystemString& filename, const char* game, const char* maker) : m_filename(filename) +Card::Card(SystemStringView filename, const char* game, const char* maker) : m_filename(filename) { memset(__raw, 0xFF, BlockSize); if (game && strlen(game) == 4) @@ -848,10 +848,10 @@ void Card::format(ECardSlot id, ECardSize size, EEncoding encoding) } } -ProbeResults Card::probeCardFile(const SystemString& filename) +ProbeResults Card::probeCardFile(SystemStringView filename) { Sstat stat; - if (Stat(filename.c_str(), &stat) || !S_ISREG(stat.st_mode)) + if (Stat(filename.data(), &stat) || !S_ISREG(stat.st_mode)) return { ECardResult::NOCARD, 0, 0 }; return { ECardResult::READY, uint32_t(stat.st_size / BlockSize) / MbitToBlocks, 0x2000 }; } diff --git a/lib/kabufuda/WideStringConvert.cpp b/lib/kabufuda/WideStringConvert.cpp index 50c8d86..7aca689 100644 --- a/lib/kabufuda/WideStringConvert.cpp +++ b/lib/kabufuda/WideStringConvert.cpp @@ -3,7 +3,7 @@ namespace kabufuda { -std::string WideToUTF8(const std::wstring& src) +std::string WideToUTF8(std::wstring_view src) { std::string retval; retval.reserve(src.length()); @@ -21,11 +21,11 @@ std::string WideToUTF8(const std::wstring& src) return retval; } -std::wstring UTF8ToWide(const std::string& src) +std::wstring UTF8ToWide(std::string_view src) { std::wstring retval; retval.reserve(src.length()); - const utf8proc_uint8_t* buf = reinterpret_cast(src.c_str()); + const utf8proc_uint8_t* buf = reinterpret_cast(src.data()); while (*buf) { utf8proc_int32_t wc;