string_view refactor

This commit is contained in:
Jack Andersen 2017-11-12 20:18:30 -10:00
parent 09e0582282
commit ffbe61508f
6 changed files with 38 additions and 48 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) # because of CMAKE_CXX_STANDARD cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) # because of CMAKE_CXX_STANDARD
project(kabufuda) project(kabufuda)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(include) include_directories(include)

View File

@ -157,7 +157,7 @@ public:
* @param game * @param game
* @param maker * @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(); ~Card();
/** /**
@ -409,7 +409,7 @@ public:
* @brief Returns basic stats about a card image without opening a handle * @brief Returns basic stats about a card image without opening a handle
* @return ProbeResults structure * @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. <br /> * @brief Writes any changes to the Card instance immediately to disk. <br />
@ -425,7 +425,7 @@ public:
/** /**
* @brief Access host filename of card * @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 * @brief Gets card-scope error state

View File

@ -184,36 +184,31 @@ static inline double SBig(double val) { return val; }
typedef wchar_t SystemChar; typedef wchar_t SystemChar;
static inline size_t StrLen(const SystemChar* str) { return wcslen(str); } static inline size_t StrLen(const SystemChar* str) { return wcslen(str); }
typedef std::wstring SystemString; 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 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); } static inline void ToUpper(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towupper); }
class SystemUTF8View class SystemUTF8Conv
{ {
std::string m_utf8; std::string m_utf8;
public: public:
explicit SystemUTF8View(const SystemString& str) : m_utf8(WideToUTF8(str)) {} explicit SystemUTF8Conv(SystemStringView str) : m_utf8(WideToUTF8(str)) {}
operator const std::string&() const { return m_utf8; } std::string_view str() const { return m_utf8; }
const std::string& str() const { return m_utf8; }
const char* c_str() const { return m_utf8.c_str(); } 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+(std::string_view other) const { return m_utf8 + other; }
std::string operator+(const char* 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+(std::string_view lhs, const SystemUTF8Conv& rhs) { return lhs + std::string(rhs); }
inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } class SystemStringConv
class SystemStringView
{ {
std::wstring m_sys; std::wstring m_sys;
public: public:
explicit SystemStringView(const std::string& str) : m_sys(UTF8ToWide(str)) {} explicit SystemStringConv(std::string_view str) : m_sys(UTF8ToWide(str)) {}
operator const std::wstring&() const { return m_sys; } SystemStringView sys_str() const { return m_sys; }
const std::wstring& sys_str() const { return m_sys; }
const SystemChar* c_str() const { return m_sys.c_str(); } 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 std::wstring_view other) const { return m_sys + other; }
std::wstring operator+(const wchar_t* 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 std::wstring_view lhs, const SystemStringConv& rhs) { return lhs + std::wstring(rhs); }
inline std::wstring operator+(const wchar_t* lhs, const SystemStringView& rhs) { return lhs + std::wstring(rhs); }
#ifndef _S #ifndef _S
#define _S(val) L##val #define _S(val) L##val
#endif #endif
@ -222,36 +217,31 @@ typedef struct _stat Sstat;
typedef char SystemChar; typedef char SystemChar;
static inline size_t StrLen(const SystemChar* str) { return strlen(str); } static inline size_t StrLen(const SystemChar* str) { return strlen(str); }
typedef std::string SystemString; 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 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); } 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: public:
explicit SystemUTF8View(const SystemString& str) : m_utf8(str) {} explicit SystemUTF8Conv(SystemStringView str) : m_utf8(str) {}
operator const std::string&() const { return m_utf8; } std::string_view str() const { return m_utf8; }
const std::string& str() const { return m_utf8; } const char* c_str() const { return m_utf8.data(); }
const char* c_str() const { return m_utf8.c_str(); } std::string operator+(std::string_view other) const { return std::string(m_utf8) + other.data(); }
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; }
}; };
inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } inline std::string operator+(std::string_view lhs, const SystemUTF8Conv& rhs) { return std::string(lhs) + rhs.c_str(); }
inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) { return lhs + std::string(rhs); } class SystemStringConv
class SystemStringView
{ {
const std::string& m_sys; SystemStringView m_sys;
public: public:
explicit SystemStringView(const std::string& str) : m_sys(str) {} explicit SystemStringConv(std::string_view str) : m_sys(str) {}
operator const std::string&() const { return m_sys; } SystemStringView sys_str() const { return m_sys; }
const std::string& sys_str() const { return m_sys; } const SystemChar* c_str() const { return m_sys.data(); }
const SystemChar* c_str() const { return m_sys.c_str(); } std::string operator+(std::string_view other) const { return std::string(m_sys) + other.data(); }
std::string operator+(const std::string& other) const { return m_sys + other; }
std::string operator+(const char* other) const { return m_sys + other; }
}; };
inline std::string operator+(const std::string& 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(); }
inline std::string operator+(const char* lhs, const SystemStringView& rhs) { return lhs + std::string(rhs); }
#ifndef _S #ifndef _S
#define _S(val) val #define _S(val) val
#endif #endif

View File

@ -5,8 +5,8 @@
namespace kabufuda namespace kabufuda
{ {
std::string WideToUTF8(const std::wstring& src); std::string WideToUTF8(std::wstring_view src);
std::wstring UTF8ToWide(const std::string& src); std::wstring UTF8ToWide(std::string_view src);
} }
#endif // __KABU_WIDESTRINGCONVERT_HPP__ #endif // __KABU_WIDESTRINGCONVERT_HPP__

View File

@ -71,7 +71,7 @@ Card& Card::operator=(Card&& other)
return *this; 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); memset(__raw, 0xFF, BlockSize);
if (game && strlen(game) == 4) 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; 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::NOCARD, 0, 0 };
return { ECardResult::READY, uint32_t(stat.st_size / BlockSize) / MbitToBlocks, 0x2000 }; return { ECardResult::READY, uint32_t(stat.st_size / BlockSize) / MbitToBlocks, 0x2000 };
} }

View File

@ -3,7 +3,7 @@
namespace kabufuda namespace kabufuda
{ {
std::string WideToUTF8(const std::wstring& src) std::string WideToUTF8(std::wstring_view src)
{ {
std::string retval; std::string retval;
retval.reserve(src.length()); retval.reserve(src.length());
@ -21,11 +21,11 @@ std::string WideToUTF8(const std::wstring& src)
return retval; return retval;
} }
std::wstring UTF8ToWide(const std::string& src) std::wstring UTF8ToWide(std::string_view src)
{ {
std::wstring retval; std::wstring retval;
retval.reserve(src.length()); retval.reserve(src.length());
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(src.c_str()); const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(src.data());
while (*buf) while (*buf)
{ {
utf8proc_int32_t wc; utf8proc_int32_t wc;