string_view refactor

This commit is contained in:
Jack Andersen 2017-11-12 20:12:37 -10:00
parent b55f265131
commit f5dabee0a7
23 changed files with 137 additions and 193 deletions

View File

@ -1,7 +1,7 @@
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) # because of CMAKE_CXX_STANDARD
project(athena)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

View File

@ -2349,7 +2349,7 @@ public:
fileOutOld(*foOld),
emitVisitor(context, *foOld) {}
void HandleTranslationUnit(clang::ASTContext& context)
void HandleTranslationUnit(clang::ASTContext& context) override
{
/* Write file head */
fileOutOld << "/* Auto generated atdna implementation */\n"
@ -2386,7 +2386,7 @@ class ATDNAAction : public clang::ASTFrontendAction
public:
explicit ATDNAAction() = default;
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler,
llvm::StringRef /*filename*/)
llvm::StringRef /*filename*/) override
{
clang::DependencyOutputOptions DepOpts;
DepOpts.OutputFile = DepFileOut;
@ -2414,7 +2414,7 @@ int main(int argc, const char** argv)
std::vector<std::string> args = {"clang-tool",
"-fsyntax-only",
"-std=c++14",
"-std=c++1z",
"-D__atdna__=1",
"-Wno-expansion-to-defined",
"-Wno-nullability-completeness",

View File

@ -180,7 +180,7 @@ struct String : public DNA<VE>, public std::string
{writer.writeString(*this, sizeVar);}
size_t binarySize(size_t __isz) const
{return __isz + ((sizeVar<0)?(this->size()+1):sizeVar);}
std::string& operator=(const std::string& __str)
std::string& operator=(std::string_view __str)
{return this->assign(__str);}
std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}
@ -205,7 +205,7 @@ struct WString : public DNA<VE>, public std::wstring
}
size_t binarySize(size_t __isz) const
{return __isz + (((sizeVar<0)?(this->size()+1):sizeVar)*2);}
std::wstring& operator=(const std::wstring& __str)
std::wstring& operator=(std::wstring_view __str)
{return this->assign(__str);}
std::wstring& operator=(std::wstring&& __str)
{this->swap(__str); return *this;}
@ -224,7 +224,7 @@ struct WStringAsString : public DNA<VE>, public std::string
{writer.writeStringAsWString(*this, sizeVar);}
size_t binarySize(size_t __isz) const
{return __isz + (((sizeVar<0)?(this->size()+1):sizeVar)*2);}
std::string& operator=(const std::string& __str)
std::string& operator=(std::string_view __str)
{return this->assign(__str);}
std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}

View File

@ -25,7 +25,7 @@ struct YAMLNode
std::vector<std::unique_ptr<YAMLNode>> m_seqChildren;
std::vector<std::pair<std::string, std::unique_ptr<YAMLNode>>> m_mapChildren;
YAMLNode(yaml_node_type_t type) : m_type(type) {}
inline const YAMLNode* findMapChild(const char* key) const
inline const YAMLNode* findMapChild(std::string_view key) const
{
for (const auto& item : m_mapChildren)
if (!item.first.compare(key))
@ -133,42 +133,34 @@ std::unique_ptr<YAMLNode> ValToNode(const std::unique_ptr<atUint8[]>& val, size_
template <>
std::string NodeToVal(const YAMLNode* node);
std::unique_ptr<YAMLNode> ValToNode(const std::string& val);
std::unique_ptr<YAMLNode> ValToNode(const char* val);
std::unique_ptr<YAMLNode> ValToNode(std::string_view val);
template <>
std::wstring NodeToVal(const YAMLNode* node);
std::unique_ptr<YAMLNode> ValToNode(const std::wstring& val);
std::unique_ptr<YAMLNode> ValToNode(std::wstring_view val);
std::unique_ptr<YAMLNode> ValToNode(const wchar_t* val);
std::unique_ptr<YAMLNode> ValToNode(std::u16string_view val);
std::unique_ptr<YAMLNode> ValToNode(const std::u16string& val);
std::unique_ptr<YAMLNode> ValToNode(const char16_t* val);
std::unique_ptr<YAMLNode> ValToNode(const std::u32string& val);
std::unique_ptr<YAMLNode> ValToNode(const char32_t* val);
std::unique_ptr<YAMLNode> ValToNode(std::u32string_view val);
std::string base64_encode(const atUint8* bytes_to_encode, size_t in_len);
std::unique_ptr<atUint8[]> base64_decode(const std::string& encoded_string);
std::unique_ptr<atUint8[]> base64_decode(std::string_view encoded_string);
void HandleYAMLParserError(yaml_parser_t* parser);
void HandleYAMLEmitterError(yaml_emitter_t* emitter);
struct YAMLStdStringReaderState
struct YAMLStdStringViewReaderState
{
std::string::const_iterator begin;
std::string::const_iterator end;
YAMLStdStringReaderState(const std::string& str)
std::string_view::const_iterator begin;
std::string_view::const_iterator end;
YAMLStdStringViewReaderState(std::string_view str)
{
begin = str.begin();
end = str.end();
}
};
int YAMLStdStringReader(YAMLStdStringReaderState* str,
int YAMLStdStringReader(YAMLStdStringViewReaderState* str,
unsigned char* buffer, size_t size, size_t* size_read);
int YAMLStdStringWriter(std::string* str, unsigned char* buffer, size_t size);
@ -433,14 +425,10 @@ public:
void writeVec3d(const char* name, const atVec3d& val);
void writeVec4d(const char* name, const atVec4d& val);
void writeUBytes(const char* name, const std::unique_ptr<atUint8[]>& val, size_t byteCount);
void writeString(const char* name, const std::string& val);
void writeString(const char* name, const char* val);
void writeWString(const char* name, const std::wstring& val);
void writeWString(const char* name, const wchar_t* val);
void writeU16String(const char* name, const std::u16string& val);
void writeU16String(const char* name, const char16_t* val);
void writeU32String(const char* name, const std::u32string& val);
void writeU32String(const char* name, const char32_t* val);
void writeString(const char* name, std::string_view val);
void writeWString(const char* name, std::wstring_view val);
void writeU16String(const char* name, std::u16string_view val);
void writeU32String(const char* name, std::u32string_view val);
};
int YAMLAthenaReader(athena::io::IStreamReader* reader,
@ -503,9 +491,9 @@ struct DNAYaml : DNA<DNAE>
return res;
}
bool fromYAMLString(const std::string& str)
bool fromYAMLString(std::string_view str)
{
YAMLStdStringReaderState reader(str);
YAMLStdStringViewReaderState reader(str);
YAMLDocReader docReader;
yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader);
if (!docReader.parse(nullptr))
@ -515,9 +503,9 @@ struct DNAYaml : DNA<DNAE>
}
template<class DNASubtype>
static bool ValidateFromYAMLString(const std::string& str)
static bool ValidateFromYAMLString(std::string_view str)
{
YAMLStdStringReaderState reader(str);
YAMLStdStringViewReaderState reader(str);
YAMLDocReader docReader;
yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader);
bool retval = docReader.ValidateClassType(DNASubtype::DNAType());
@ -622,9 +610,9 @@ struct StringYaml : public DNAYaml<VE>, public std::string
void write(athena::io::YAMLDocWriter& writer) const
{writer.writeString(nullptr, *this);}
StringYaml() = default;
StringYaml(const std::string& __str) : std::string(__str) {}
StringYaml(std::string_view __str) : std::string(__str) {}
StringYaml(std::string&& __str) : std::string(std::move(__str)) {}
std::string& operator=(const std::string& __str)
std::string& operator=(std::string_view __str)
{return this->assign(__str);}
std::string& operator=(std::string&& __str)
{static_cast<std::string&>(*this) = std::move(__str); return *this;}
@ -650,7 +638,7 @@ struct WStringYaml : public DNAYaml<VE>, public std::wstring
{this->assign(std::move(reader.readWString(nullptr)));}
void write(athena::io::YAMLDocWriter& writer) const
{writer.writeWString(nullptr, *this);}
std::wstring& operator=(const std::wstring& __str)
std::wstring& operator=(std::wstring_view __str)
{return this->assign(__str);}
std::wstring& operator=(std::wstring&& __str)
{this->swap(__str); return *this;}
@ -670,7 +658,7 @@ struct WStringAsStringYaml : public DNAYaml<VE>, public std::string
{this->assign(std::move(reader.readString(nullptr)));}
void write(athena::io::YAMLDocWriter& writer) const
{writer.writeString(nullptr, *this);}
std::string& operator=(const std::string& __str)
std::string& operator=(std::string_view __str)
{return this->assign(__str);}
std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}

View File

@ -13,23 +13,23 @@ namespace athena
class Dir
{
public:
explicit Dir(const std::string& path);
explicit Dir(std::string_view path);
std::string absolutePath() const;
static inline std::string absolutePath(const std::string& path)
static inline std::string absolutePath(std::string_view path)
{ return Dir(path).absolutePath(); }
bool isDir() const;
static bool isDir(const std::string dir)
static bool isDir(std::string_view dir)
{ return Dir(dir).isDir(); }
std::vector<FileInfo> files() const;
bool cd(const std::string& path);
bool rm(const std::string& path);
bool cd(std::string_view path);
bool rm(std::string_view path);
bool touch();
static bool mkdir(const std::string& dir, mode_t mode = 0755);
static bool mkpath(const std::string& path, mode_t mode = 0755);
static bool mkdir(std::string_view dir, mode_t mode = 0755);
static bool mkpath(std::string_view path, mode_t mode = 0755);
private:
std::string m_path;
};

View File

@ -10,45 +10,45 @@ namespace athena
class FileInfo
{
public:
explicit FileInfo(const std::string& path = std::string());
explicit FileInfo(std::string_view path = {});
std::string absolutePath() const;
static inline std::string absolutePath(const std::string& lnk)
static inline std::string absolutePath(std::string_view lnk)
{ return FileInfo(lnk).absolutePath(); }
std::string absoluteFilePath() const;
static inline std::string absoluteFilePath(const std::string& path)
static inline std::string absoluteFilePath(std::string_view path)
{ return FileInfo(path).absoluteFilePath(); }
std::string filename() const;
static inline std::string filename(const std::string path)
static inline std::string filename(std::string_view path)
{ return FileInfo(path).filename(); }
std::string path() const;
static inline std::string path(const std::string path)
std::string path() const { return m_path; }
static inline std::string path(std::string_view path)
{ return FileInfo(path).path(); }
std::string extension() const;
static inline std::string extension(const std::string path)
static inline std::string extension(std::string_view path)
{ return FileInfo(path).extension(); }
atUint64 size() const;
static inline atUint64 size(const std::string path)
static inline atUint64 size(std::string_view path)
{ return FileInfo(path).size(); }
bool exists() const;
static inline bool exists(const std::string& path)
static inline bool exists(std::string_view path)
{ return FileInfo(path).exists(); }
bool isLink() const;
static inline bool isLink(const std::string& lnk)
static inline bool isLink(std::string_view lnk)
{ return FileInfo(lnk).isLink(); }
bool isFile() const;
static inline bool isFile(const std::string& path)
static inline bool isFile(std::string_view path)
{ return FileInfo(path).isFile(); }
bool touch() const;
static inline bool touch(const std::string& path)
static inline bool touch(std::string_view path)
{ return FileInfo(path).touch(); }
private:

View File

@ -21,8 +21,8 @@ namespace io
class FileReader : public IStreamReader
{
public:
FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
FileReader(const std::wstring& filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
FileReader(std::string_view filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
FileReader(std::wstring_view filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
virtual ~FileReader();
inline std::string filename() const

View File

@ -18,8 +18,8 @@ namespace io
class FileWriter : public IStreamWriter
{
public:
FileWriter(const std::string& filename, bool overwrite = true, bool globalErr=true);
FileWriter(const std::wstring& filename, bool overwrite = true, bool globalErr=true);
FileWriter(std::string_view filename, bool overwrite = true, bool globalErr=true);
FileWriter(std::wstring_view filename, bool overwrite = true, bool globalErr=true);
virtual ~FileWriter();
inline std::string filename() const
@ -78,7 +78,7 @@ class TransactionalFileWriter : public IStreamWriter
std::vector<uint8_t> m_deferredBuffer;
atUint64 m_position = 0;
public:
TransactionalFileWriter(const std::string& filename, bool overwrite = true, bool globalErr=true)
TransactionalFileWriter(std::string_view filename, bool overwrite = true, bool globalErr=true)
: m_overwrite(overwrite), m_globalErr(globalErr)
{
#if _WIN32
@ -87,7 +87,7 @@ public:
m_filename = filename;
#endif
}
TransactionalFileWriter(const std::wstring& filename, bool overwrite = true, bool globalErr=true)
TransactionalFileWriter(std::wstring_view filename, bool overwrite = true, bool globalErr=true)
: m_overwrite(overwrite), m_globalErr(globalErr)
{
#if _WIN32

View File

@ -694,11 +694,11 @@ public:
*
* Endianness is set with setEndian
*/
inline void writeStringAsWString(const std::string& str, atInt32 fixedLen = -1)
inline void writeStringAsWString(std::string_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
std::string tmpStr = "\xEF\xBB\xBF" + str;
std::string tmpStr = std::string("\xEF\xBB\xBF") + str.data();
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(tmpStr.c_str());
if (fixedLen < 0)
{
@ -752,11 +752,11 @@ public:
*
* Endianness is little
*/
inline void writeStringAsWStringLittle(const std::string& str, atInt32 fixedLen = -1)
inline void writeStringAsWStringLittle(std::string_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
std::string tmpStr = "\xEF\xBB\xBF" + str;
std::string tmpStr = std::string("\xEF\xBB\xBF") + str.data();
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(tmpStr.c_str());
if (fixedLen < 0)
{
@ -810,12 +810,12 @@ public:
*
* Endianness is big
*/
inline void writeStringAsWStringBig(const std::string& str, atInt32 fixedLen = -1)
inline void writeStringAsWStringBig(std::string_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
std::string tmpStr = "\xEF\xBB\xBF" + str;
std::string tmpStr = std::string("\xEF\xBB\xBF") + str.data();
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(tmpStr.c_str());
if (fixedLen < 0)
{
@ -866,7 +866,7 @@ public:
* @param str The string to write to the buffer
* @param fixedLen If not -1, the number of characters to zero-fill string to
*/
inline void writeString(const std::string& str, atInt32 fixedLen = -1)
inline void writeString(std::string_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
@ -896,7 +896,7 @@ public:
}
}
}
inline void writeVal(const std::string& val) {writeString(val);}
inline void writeVal(std::string_view val) {writeString(val);}
/** @brief Writes an wstring to the buffer and advances the buffer.
*
@ -905,7 +905,7 @@ public:
*
* Endianness is set with setEndian
*/
inline void writeWString(const std::wstring& str, atInt32 fixedLen = -1)
inline void writeWString(std::wstring_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
@ -935,7 +935,7 @@ public:
}
}
}
inline void writeVal(const std::wstring& val) {writeWString(val);}
inline void writeVal(std::wstring_view val) {writeWString(val);}
/** @brief Writes an wstring to the buffer and advances the buffer.
*
@ -944,7 +944,7 @@ public:
*
* Endianness is little
*/
inline void writeWStringLittle(const std::wstring& str, atInt32 fixedLen = -1)
inline void writeWStringLittle(std::wstring_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
@ -974,7 +974,7 @@ public:
}
}
}
inline void writeValLittle(const std::wstring& val) {writeWStringLittle(val);}
inline void writeValLittle(std::wstring_view val) {writeWStringLittle(val);}
/** @brief Writes an wstring to the buffer and advances the buffer.
*
@ -983,7 +983,7 @@ public:
*
* Endianness is big
*/
inline void writeWStringBig(const std::wstring& str, atInt32 fixedLen = -1)
inline void writeWStringBig(std::wstring_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
@ -1013,7 +1013,7 @@ public:
}
}
}
inline void writeValBig(const std::wstring& val) {writeWStringBig(val);}
inline void writeValBig(std::wstring_view val) {writeWStringBig(val);}
/** @brief Writes a u16string to the buffer and advances the buffer.
*
@ -1022,7 +1022,7 @@ public:
*
* Endianness is big
*/
inline void writeU16StringBig(const std::u16string& str, atInt32 fixedLen = -1)
inline void writeU16StringBig(std::u16string_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
@ -1052,7 +1052,7 @@ public:
}
}
}
inline void writeValBig(const std::u16string& val) {writeU16StringBig(val);}
inline void writeValBig(std::u16string_view val) {writeU16StringBig(val);}
/** @brief Writes a u16string to the buffer and advances the buffer.
*
@ -1061,7 +1061,7 @@ public:
*
* Endianness is big
*/
inline void writeU32StringBig(const std::u32string& str, atInt32 fixedLen = -1)
inline void writeU32StringBig(std::u32string_view str, atInt32 fixedLen = -1)
{
if (fixedLen == 0)
return;
@ -1091,7 +1091,7 @@ public:
}
}
}
inline void writeValBig(const std::u32string& val) {writeU32StringBig(val);}
inline void writeValBig(std::u32string_view val) {writeU32StringBig(val);}
inline void fill(atUint8 val, atUint64 length)
{

View File

@ -89,7 +89,7 @@ public:
*
* @param filename If not empty, the filename to save to
*/
void save(const std::string& filename = "");
void save(std::string_view filename = {});
/*! @brief Writes the given buffer with the specified length, buffers can be bigger than the length
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
@ -124,7 +124,7 @@ public:
*
* @param filename The file to create the stream from
*/
MemoryCopyWriter(const std::string& filename);
MemoryCopyWriter(std::string_view filename);
/*! @brief Sets the buffers position relative to the specified position.<br />
* It seeks relative to the current position by default.

View File

@ -18,7 +18,7 @@ class SpriteFileWriter : public MemoryCopyWriter
public:
SpriteFileWriter(atUint8* data, atUint64 length);
SpriteFileWriter(const std::string& filepath);
SpriteFileWriter(std::string_view filepath);
void writeFile(Sakura::SpriteFile* file);
};

View File

@ -188,16 +188,16 @@ inline double BigDouble(double& val)
}
void fillRandom(atUint8 * rndArea, atUint64 count);
std::vector<std::string> split(const std::string &s, char delim);
std::vector<std::string> split(std::string_view s, char delim);
atUint64 rand64();
std::string join(const std::vector<std::string>& elems, const std::string& delims);
std::string join(const std::vector<std::string>& elems, std::string_view delims);
void tolower(std::string& str);
void toupper(std::string& str);
std::string vsprintf(const char* fmt, va_list list);
std::string sprintf(const char* fmt, ...);
bool parseBool(const std::string& boolean, bool* valid = NULL);
bool parseBool(std::string_view boolean, bool* valid = NULL);
int countChar(const std::string& str, const char chr, int* lastOccur = NULL);
int countChar(std::string_view str, const char chr, int* lastOccur = NULL);
// trim from start
std::string& ltrim(std::string& s);
@ -207,14 +207,14 @@ std::string& rtrim(std::string& s);
// trim from both ends
std::string& trim(std::string& s);
atUint64 fileSize(const std::string& filename);
atUint64 fileSize(std::string_view filename);
#ifdef _MSC_VER
atUint64 fileSize(const std::wstring& filename);
atUint64 fileSize(std::wstring_view filename);
#endif
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);
} // utility
} // Athena

View File

@ -228,9 +228,9 @@ static inline utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8pro
class UTF8Iterator : public std::iterator<std::forward_iterator_tag, uint32_t>
{
std::string::const_iterator m_it;
std::string_view::const_iterator m_it;
public:
UTF8Iterator(const std::string::const_iterator& it) : m_it(it) {}
UTF8Iterator(const std::string_view::const_iterator& it) : m_it(it) {}
UTF8Iterator& operator+=(size_t v)
{
for (size_t i=0 ; i<v ; ++i)
@ -273,8 +273,8 @@ public:
utf8proc_iterate(reinterpret_cast<const utf8proc_uint8_t*>(&*m_it), -1, &ret);
return ret;
}
std::string::const_iterator iter() const {return m_it;}
size_t countTo(std::string::const_iterator end) const
std::string_view::const_iterator iter() const {return m_it;}
size_t countTo(std::string_view::const_iterator end) const
{
UTF8Iterator it(m_it);
size_t ret = 0;

View File

@ -353,14 +353,7 @@ std::string NodeToVal(const YAMLNode* node)
return node->m_scalarString;
}
std::unique_ptr<YAMLNode> ValToNode(const std::string& val)
{
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = val;
return std::unique_ptr<YAMLNode>(ret);
}
std::unique_ptr<YAMLNode> ValToNode(const char* val)
std::unique_ptr<YAMLNode> ValToNode(std::string_view val)
{
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = val;
@ -388,7 +381,7 @@ std::wstring NodeToVal(const YAMLNode* node)
return retval;
}
std::unique_ptr<YAMLNode> ValToNode(const std::wstring& val)
std::unique_ptr<YAMLNode> ValToNode(std::wstring_view val)
{
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString.reserve(val.length());
@ -406,13 +399,7 @@ std::unique_ptr<YAMLNode> ValToNode(const std::wstring& val)
return std::unique_ptr<YAMLNode>(ret);
}
std::unique_ptr<YAMLNode> ValToNode(const wchar_t* val)
{
std::wstring wstr(val);
return ValToNode(wstr);
}
std::unique_ptr<YAMLNode> ValToNode(const std::u16string& val)
std::unique_ptr<YAMLNode> ValToNode(std::u16string_view val)
{
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString.reserve(val.length());
@ -430,13 +417,7 @@ std::unique_ptr<YAMLNode> ValToNode(const std::u16string& val)
return std::unique_ptr<YAMLNode>(ret);
}
std::unique_ptr<YAMLNode> ValToNode(const char16_t* val)
{
std::u16string wstr(val);
return ValToNode(wstr);
}
std::unique_ptr<YAMLNode> ValToNode(const std::u32string& val)
std::unique_ptr<YAMLNode> ValToNode(std::u32string_view val)
{
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString.reserve(val.length());
@ -454,12 +435,6 @@ std::unique_ptr<YAMLNode> ValToNode(const std::u32string& val)
return std::unique_ptr<YAMLNode>(ret);
}
std::unique_ptr<YAMLNode> ValToNode(const char32_t* val)
{
std::u32string wstr(val);
return ValToNode(wstr);
}
static const char* ErrorString(yaml_error_type_t errt)
{
switch (errt)
@ -494,7 +469,7 @@ void HandleYAMLEmitterError(yaml_emitter_t* emitter)
atError("YAML error: %s: %s", ErrorString(emitter->error), emitter->problem?emitter->problem:"");
}
int YAMLStdStringReader(YAMLStdStringReaderState* reader,
int YAMLStdStringReader(YAMLStdStringViewReaderState* reader,
unsigned char* buffer, size_t size, size_t* size_read)
{
size_t diff = reader->end - reader->begin;
@ -779,44 +754,24 @@ void YAMLDocWriter::writeUBytes(const char* name,
writeVal<const std::unique_ptr<atUint8[]>&>(name, val, byteCount);
}
void YAMLDocWriter::writeString(const char* name, const std::string& val)
void YAMLDocWriter::writeString(const char* name, std::string_view val)
{
writeVal<std::string>(name, val);
writeVal<std::string_view>(name, val);
}
void YAMLDocWriter::writeString(const char* name, const char* val)
void YAMLDocWriter::writeWString(const char* name, std::wstring_view val)
{
writeVal<const char*>(name, val);
writeVal<std::wstring_view>(name, val);
}
void YAMLDocWriter::writeWString(const char* name, const std::wstring& val)
void YAMLDocWriter::writeU16String(const char* name, std::u16string_view val)
{
writeVal<std::wstring>(name, val);
writeVal<std::u16string_view>(name, val);
}
void YAMLDocWriter::writeWString(const char* name, const wchar_t* val)
void YAMLDocWriter::writeU32String(const char* name, std::u32string_view val)
{
writeVal<const wchar_t*>(name, val);
}
void YAMLDocWriter::writeU16String(const char* name, const std::u16string& val)
{
writeVal<std::u16string>(name, val);
}
void YAMLDocWriter::writeU16String(const char* name, const char16_t* val)
{
writeVal<const char16_t*>(name, val);
}
void YAMLDocWriter::writeU32String(const char* name, const std::u32string& val)
{
writeVal<std::u32string>(name, val);
}
void YAMLDocWriter::writeU32String(const char* name, const char32_t* val)
{
writeVal<const char32_t*>(name, val);
writeVal<std::u32string_view>(name, val);
}
static inline void InsertNode(std::vector<YAMLNode*>& nodeStack,
@ -1407,7 +1362,7 @@ std::string base64_encode(const atUint8* bytes_to_encode, size_t in_len)
return ret;
}
std::unique_ptr<atUint8[]> base64_decode(const std::string& encoded_string)
std::unique_ptr<atUint8[]> base64_decode(std::string_view encoded_string)
{
int in_len = encoded_string.size();
int i = 0;

View File

@ -19,7 +19,7 @@
namespace athena
{
Dir::Dir(const std::string &path)
Dir::Dir(std::string_view path)
: m_path(path)
{
}
@ -39,7 +39,7 @@ bool Dir::isDir() const
return (S_ISDIR(st.st_mode));
}
bool Dir::cd(const std::string& path)
bool Dir::cd(std::string_view path)
{
Dir tmp(path);
if (tmp.isDir())
@ -51,9 +51,9 @@ bool Dir::cd(const std::string& path)
return false;
}
bool Dir::rm(const std::string& path)
bool Dir::rm(std::string_view path)
{
return !(remove((m_path + "/" + path).c_str()) < 0);
return !(remove((m_path + "/" + path.data()).c_str()) < 0);
}
bool Dir::touch()
@ -67,16 +67,16 @@ bool Dir::touch()
return false;
}
bool Dir::mkdir(const std::string& dir, mode_t mode)
bool Dir::mkdir(std::string_view dir, mode_t mode)
{
#if _WIN32
return !(::_mkdir(dir.c_str()) < 0);
#else
return !(::mkdir(dir.c_str(), mode) < 0);
return !(::mkdir(dir.data(), mode) < 0);
#endif
}
bool Dir::mkpath(const std::string& path, mode_t mode)
bool Dir::mkpath(std::string_view path, mode_t mode)
{
std::vector<std::string> dirs = utility::split(path, '/');
if (dirs.empty())

View File

@ -32,7 +32,7 @@
namespace athena
{
FileInfo::FileInfo(const std::string& path)
FileInfo::FileInfo(std::string_view path)
: m_path(path)
{
}

View File

@ -11,7 +11,7 @@ namespace athena
{
namespace io
{
FileReader::FileReader(const std::string& filename, atInt32 cacheSize, bool globalErr)
FileReader::FileReader(std::string_view filename, atInt32 cacheSize, bool globalErr)
: m_fileHandle(nullptr),
m_cacheData(nullptr),
m_offset(0),
@ -22,7 +22,7 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize, bool glob
setCacheSize(cacheSize);
}
FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize, bool globalErr)
FileReader::FileReader(std::wstring_view filename, atInt32 cacheSize, bool globalErr)
: m_fileHandle(nullptr),
m_cacheData(nullptr),
m_offset(0),

View File

@ -5,7 +5,7 @@ namespace athena
{
namespace io
{
FileReader::FileReader(const std::string& filename, atInt32 cacheSize, bool globalErr)
FileReader::FileReader(std::string_view filename, atInt32 cacheSize, bool globalErr)
: m_fileHandle(nullptr),
m_cacheData(nullptr),
m_offset(0),
@ -16,7 +16,7 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize, bool glob
setCacheSize(cacheSize);
}
FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize, bool globalErr)
FileReader::FileReader(std::wstring_view filename, atInt32 cacheSize, bool globalErr)
: m_fileHandle(nullptr),
m_cacheData(nullptr),
m_offset(0),

View File

@ -11,7 +11,7 @@ namespace athena
{
namespace io
{
FileWriter::FileWriter(const std::string& filename, bool overwrite, bool globalErr)
FileWriter::FileWriter(std::string_view filename, bool overwrite, bool globalErr)
: m_fileHandle(NULL),
m_bytePosition(0),
m_globalErr(globalErr)
@ -24,7 +24,7 @@ FileWriter::FileWriter(const std::string& filename, bool overwrite, bool globalE
open(overwrite);
}
FileWriter::FileWriter(const std::wstring& filename, bool overwrite, bool globalErr)
FileWriter::FileWriter(std::wstring_view filename, bool overwrite, bool globalErr)
: m_fileHandle(NULL),
m_bytePosition(0),
m_globalErr(globalErr)

View File

@ -5,7 +5,7 @@ namespace athena
{
namespace io
{
FileWriter::FileWriter(const std::string& filename, bool overwrite, bool globalErr)
FileWriter::FileWriter(std::string_view filename, bool overwrite, bool globalErr)
: m_fileHandle(0),
m_bytePosition(0),
m_globalErr(globalErr)
@ -14,7 +14,7 @@ FileWriter::FileWriter(const std::string& filename, bool overwrite, bool globalE
open(overwrite);
}
FileWriter::FileWriter(const std::wstring& filename, bool overwrite, bool globalErr)
FileWriter::FileWriter(std::wstring_view filename, bool overwrite, bool globalErr)
: m_fileHandle(0),
m_bytePosition(0),
m_globalErr(globalErr)

View File

@ -55,7 +55,7 @@ MemoryCopyWriter::MemoryCopyWriter(atUint8* data, atUint64 length)
memmove(m_data, data, length);
}
MemoryCopyWriter::MemoryCopyWriter(const std::string& filename)
MemoryCopyWriter::MemoryCopyWriter(std::string_view filename)
{
m_filepath = filename;
m_length = 0x10;
@ -210,7 +210,7 @@ atUint8* MemoryWriter::data() const
}
void MemoryWriter::save(const std::string& filename)
void MemoryWriter::save(std::string_view filename)
{
if (filename.empty() && m_filepath.empty())
{

View File

@ -13,7 +13,7 @@ SpriteFileWriter::SpriteFileWriter(atUint8* data, atUint64 length)
{
}
SpriteFileWriter::SpriteFileWriter(const std::string& filepath)
SpriteFileWriter::SpriteFileWriter(std::string_view filepath)
: MemoryCopyWriter(filepath)
{
}

View File

@ -42,9 +42,10 @@ void fillRandom(atUint8* rndArea, atUint64 count)
}
}
std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems)
static std::vector<std::string>& split(std::string_view s, char delim, std::vector<std::string>& elems)
{
std::stringstream ss(s);
std::string tmps(s);
std::stringstream ss(tmps);
std::string item;
while (std::getline(ss, item, delim))
@ -54,17 +55,17 @@ std::vector<std::string>& split(const std::string& s, char delim, std::vector<st
}
std::vector<std::string> split(const std::string& s, char delim)
std::vector<std::string> split(std::string_view s, char delim)
{
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
std::string join(const std::vector<std::string>& elems, const std::string& delims)
std::string join(const std::vector<std::string>& elems, std::string_view delims)
{
std::ostringstream ret;
std::copy(elems.begin(), elems.end(), std::ostream_iterator<std::string>(ret, delims.c_str()));
std::copy(elems.begin(), elems.end(), std::ostream_iterator<std::string>(ret, delims.data()));
return ret.str();
}
@ -108,9 +109,9 @@ std::string sprintf(const char* fmt, ...)
return ret;
}
bool parseBool(const std::string& boolean, bool* valid)
bool parseBool(std::string_view boolean, bool* valid)
{
std::string val = boolean;
std::string val(boolean);
// compare must be case insensitive
// This is the cleanest solution since I only need to do it once
tolower(val);
@ -141,7 +142,7 @@ bool parseBool(const std::string& boolean, bool* valid)
return false;
}
int countChar(const std::string& str, const char chr, int* lastOccur)
int countChar(std::string_view str, const char chr, int* lastOccur)
{
int ret = 0;
@ -163,18 +164,18 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
return ret;
}
atUint64 fileSize(const std::string& filename)
atUint64 fileSize(std::string_view filename)
{
atStat64_t st;
atStat64(filename.c_str(), &st);
atStat64(filename.data(), &st);
return st.st_size;
}
#ifdef _MSC_VER
atUint64 fileSize(const std::wstring& filename)
atUint64 fileSize(std::wstring_view filename)
{
atStat64_t st;
_wstati64(filename.c_str(), &st);
_wstati64(filename.data(), &st);
return st.st_size;
}
#endif
@ -216,7 +217,7 @@ atUint64 rand64()
return r0 | r1 | r2 | r3;
}
std::string wideToUtf8(const std::wstring& src)
std::string wideToUtf8(std::wstring_view src)
{
std::string retval;
retval.reserve(src.length());
@ -234,11 +235,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;