Commit ffbe6150 authored by Jack Andersen's avatar Jack Andersen

string_view refactor

parent 09e05822
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) # because of CMAKE_CXX_STANDARD
project(kabufuda)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(include)
......
......@@ -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. <br />
......@@ -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
......
......@@ -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
......
......@@ -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__
......@@ -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 };
}
......
......@@ -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<const utf8proc_uint8_t*>(src.c_str());
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(src.data());
while (*buf)
{
utf8proc_int32_t wc;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment