From f5dabee0a785d30476937296e999db442f1005f9 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 12 Nov 2017 20:12:37 -1000 Subject: [PATCH] string_view refactor --- CMakeLists.txt | 2 +- atdna/main.cpp | 6 +-- include/athena/DNA.hpp | 6 +-- include/athena/DNAYaml.hpp | 58 +++++++++-------------- include/athena/Dir.hpp | 14 +++--- include/athena/FileInfo.hpp | 24 +++++----- include/athena/FileReader.hpp | 4 +- include/athena/FileWriter.hpp | 8 ++-- include/athena/IStreamWriter.hpp | 36 +++++++------- include/athena/MemoryWriter.hpp | 4 +- include/athena/SpriteFileWriter.hpp | 2 +- include/athena/Utility.hpp | 16 +++---- include/utf8proc.h | 8 ++-- src/athena/DNAYaml.cpp | 73 ++++++----------------------- src/athena/Dir.cpp | 14 +++--- src/athena/FileInfo.cpp | 2 +- src/athena/FileReader.cpp | 4 +- src/athena/FileReaderWin32.cpp | 4 +- src/athena/FileWriterNix.cpp | 4 +- src/athena/FileWriterWin32.cpp | 4 +- src/athena/MemoryWriter.cpp | 4 +- src/athena/SpriteFileWriter.cpp | 2 +- src/athena/Utility.cpp | 31 ++++++------ 23 files changed, 137 insertions(+), 193 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eead7f8..5df5355 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/atdna/main.cpp b/atdna/main.cpp index 41b66e9..4ba9bed 100644 --- a/atdna/main.cpp +++ b/atdna/main.cpp @@ -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 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 args = {"clang-tool", "-fsyntax-only", - "-std=c++14", + "-std=c++1z", "-D__atdna__=1", "-Wno-expansion-to-defined", "-Wno-nullability-completeness", diff --git a/include/athena/DNA.hpp b/include/athena/DNA.hpp index fc1688e..3a137be 100644 --- a/include/athena/DNA.hpp +++ b/include/athena/DNA.hpp @@ -180,7 +180,7 @@ struct String : public DNA, 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, 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, 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;} diff --git a/include/athena/DNAYaml.hpp b/include/athena/DNAYaml.hpp index 05b6e6c..33fecab 100644 --- a/include/athena/DNAYaml.hpp +++ b/include/athena/DNAYaml.hpp @@ -25,7 +25,7 @@ struct YAMLNode std::vector> m_seqChildren; std::vector>> 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 ValToNode(const std::unique_ptr& val, size_ template <> std::string NodeToVal(const YAMLNode* node); -std::unique_ptr ValToNode(const std::string& val); - -std::unique_ptr ValToNode(const char* val); +std::unique_ptr ValToNode(std::string_view val); template <> std::wstring NodeToVal(const YAMLNode* node); -std::unique_ptr ValToNode(const std::wstring& val); +std::unique_ptr ValToNode(std::wstring_view val); -std::unique_ptr ValToNode(const wchar_t* val); +std::unique_ptr ValToNode(std::u16string_view val); -std::unique_ptr ValToNode(const std::u16string& val); - -std::unique_ptr ValToNode(const char16_t* val); - -std::unique_ptr ValToNode(const std::u32string& val); - -std::unique_ptr ValToNode(const char32_t* val); +std::unique_ptr ValToNode(std::u32string_view val); std::string base64_encode(const atUint8* bytes_to_encode, size_t in_len); -std::unique_ptr base64_decode(const std::string& encoded_string); +std::unique_ptr 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& 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 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 } template - 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, 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(*this) = std::move(__str); return *this;} @@ -650,7 +638,7 @@ struct WStringYaml : public DNAYaml, 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, 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;} diff --git a/include/athena/Dir.hpp b/include/athena/Dir.hpp index 154b6b5..bc884cb 100644 --- a/include/athena/Dir.hpp +++ b/include/athena/Dir.hpp @@ -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 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; }; diff --git a/include/athena/FileInfo.hpp b/include/athena/FileInfo.hpp index 11f46d0..c324694 100644 --- a/include/athena/FileInfo.hpp +++ b/include/athena/FileInfo.hpp @@ -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: diff --git a/include/athena/FileReader.hpp b/include/athena/FileReader.hpp index eb1a129..44281f9 100644 --- a/include/athena/FileReader.hpp +++ b/include/athena/FileReader.hpp @@ -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 diff --git a/include/athena/FileWriter.hpp b/include/athena/FileWriter.hpp index 5a6cd02..f26ad4f 100644 --- a/include/athena/FileWriter.hpp +++ b/include/athena/FileWriter.hpp @@ -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 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 diff --git a/include/athena/IStreamWriter.hpp b/include/athena/IStreamWriter.hpp index c3940a0..ad04eba 100644 --- a/include/athena/IStreamWriter.hpp +++ b/include/athena/IStreamWriter.hpp @@ -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(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(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(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) { diff --git a/include/athena/MemoryWriter.hpp b/include/athena/MemoryWriter.hpp index 669bc18..158f1e1 100644 --- a/include/athena/MemoryWriter.hpp +++ b/include/athena/MemoryWriter.hpp @@ -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.
* It seeks relative to the current position by default. diff --git a/include/athena/SpriteFileWriter.hpp b/include/athena/SpriteFileWriter.hpp index db8c3cd..ad95480 100644 --- a/include/athena/SpriteFileWriter.hpp +++ b/include/athena/SpriteFileWriter.hpp @@ -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); }; diff --git a/include/athena/Utility.hpp b/include/athena/Utility.hpp index 6c3e0f5..bd4717c 100644 --- a/include/athena/Utility.hpp +++ b/include/athena/Utility.hpp @@ -188,16 +188,16 @@ inline double BigDouble(double& val) } void fillRandom(atUint8 * rndArea, atUint64 count); -std::vector split(const std::string &s, char delim); +std::vector split(std::string_view s, char delim); atUint64 rand64(); -std::string join(const std::vector& elems, const std::string& delims); +std::string join(const std::vector& 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 diff --git a/include/utf8proc.h b/include/utf8proc.h index 5372e7d..80c2e8d 100644 --- a/include/utf8proc.h +++ b/include/utf8proc.h @@ -228,9 +228,9 @@ static inline utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8pro class UTF8Iterator : public std::iterator { - 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(&*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; diff --git a/src/athena/DNAYaml.cpp b/src/athena/DNAYaml.cpp index 0a393e3..d1e4354 100644 --- a/src/athena/DNAYaml.cpp +++ b/src/athena/DNAYaml.cpp @@ -353,14 +353,7 @@ std::string NodeToVal(const YAMLNode* node) return node->m_scalarString; } -std::unique_ptr ValToNode(const std::string& val) -{ - YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = val; - return std::unique_ptr(ret); -} - -std::unique_ptr ValToNode(const char* val) +std::unique_ptr 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 ValToNode(const std::wstring& val) +std::unique_ptr 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 ValToNode(const std::wstring& val) return std::unique_ptr(ret); } -std::unique_ptr ValToNode(const wchar_t* val) -{ - std::wstring wstr(val); - return ValToNode(wstr); -} - -std::unique_ptr ValToNode(const std::u16string& val) +std::unique_ptr 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 ValToNode(const std::u16string& val) return std::unique_ptr(ret); } -std::unique_ptr ValToNode(const char16_t* val) -{ - std::u16string wstr(val); - return ValToNode(wstr); -} - -std::unique_ptr ValToNode(const std::u32string& val) +std::unique_ptr 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 ValToNode(const std::u32string& val) return std::unique_ptr(ret); } -std::unique_ptr 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&>(name, val, byteCount); } -void YAMLDocWriter::writeString(const char* name, const std::string& val) +void YAMLDocWriter::writeString(const char* name, std::string_view val) { - writeVal(name, val); + writeVal(name, val); } -void YAMLDocWriter::writeString(const char* name, const char* val) +void YAMLDocWriter::writeWString(const char* name, std::wstring_view val) { - writeVal(name, val); + writeVal(name, val); } -void YAMLDocWriter::writeWString(const char* name, const std::wstring& val) +void YAMLDocWriter::writeU16String(const char* name, std::u16string_view val) { - writeVal(name, val); + writeVal(name, val); } -void YAMLDocWriter::writeWString(const char* name, const wchar_t* val) +void YAMLDocWriter::writeU32String(const char* name, std::u32string_view val) { - writeVal(name, val); -} - -void YAMLDocWriter::writeU16String(const char* name, const std::u16string& val) -{ - writeVal(name, val); -} - -void YAMLDocWriter::writeU16String(const char* name, const char16_t* val) -{ - writeVal(name, val); -} - -void YAMLDocWriter::writeU32String(const char* name, const std::u32string& val) -{ - writeVal(name, val); -} - -void YAMLDocWriter::writeU32String(const char* name, const char32_t* val) -{ - writeVal(name, val); + writeVal(name, val); } static inline void InsertNode(std::vector& nodeStack, @@ -1407,7 +1362,7 @@ std::string base64_encode(const atUint8* bytes_to_encode, size_t in_len) return ret; } -std::unique_ptr base64_decode(const std::string& encoded_string) +std::unique_ptr base64_decode(std::string_view encoded_string) { int in_len = encoded_string.size(); int i = 0; diff --git a/src/athena/Dir.cpp b/src/athena/Dir.cpp index 0aefe1b..b49ebc0 100644 --- a/src/athena/Dir.cpp +++ b/src/athena/Dir.cpp @@ -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 dirs = utility::split(path, '/'); if (dirs.empty()) diff --git a/src/athena/FileInfo.cpp b/src/athena/FileInfo.cpp index b55fb3c..e212139 100644 --- a/src/athena/FileInfo.cpp +++ b/src/athena/FileInfo.cpp @@ -32,7 +32,7 @@ namespace athena { -FileInfo::FileInfo(const std::string& path) +FileInfo::FileInfo(std::string_view path) : m_path(path) { } diff --git a/src/athena/FileReader.cpp b/src/athena/FileReader.cpp index c167a72..90f0968 100644 --- a/src/athena/FileReader.cpp +++ b/src/athena/FileReader.cpp @@ -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), diff --git a/src/athena/FileReaderWin32.cpp b/src/athena/FileReaderWin32.cpp index b47a62d..9ad38fd 100644 --- a/src/athena/FileReaderWin32.cpp +++ b/src/athena/FileReaderWin32.cpp @@ -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), diff --git a/src/athena/FileWriterNix.cpp b/src/athena/FileWriterNix.cpp index 8844669..0ba5dbf 100644 --- a/src/athena/FileWriterNix.cpp +++ b/src/athena/FileWriterNix.cpp @@ -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) diff --git a/src/athena/FileWriterWin32.cpp b/src/athena/FileWriterWin32.cpp index 198d796..6ff98fe 100644 --- a/src/athena/FileWriterWin32.cpp +++ b/src/athena/FileWriterWin32.cpp @@ -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) diff --git a/src/athena/MemoryWriter.cpp b/src/athena/MemoryWriter.cpp index 98ab76b..eee0734 100644 --- a/src/athena/MemoryWriter.cpp +++ b/src/athena/MemoryWriter.cpp @@ -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()) { diff --git a/src/athena/SpriteFileWriter.cpp b/src/athena/SpriteFileWriter.cpp index fdf034e..fb28c32 100644 --- a/src/athena/SpriteFileWriter.cpp +++ b/src/athena/SpriteFileWriter.cpp @@ -13,7 +13,7 @@ SpriteFileWriter::SpriteFileWriter(atUint8* data, atUint64 length) { } -SpriteFileWriter::SpriteFileWriter(const std::string& filepath) +SpriteFileWriter::SpriteFileWriter(std::string_view filepath) : MemoryCopyWriter(filepath) { } diff --git a/src/athena/Utility.cpp b/src/athena/Utility.cpp index ed93832..3f75a8f 100644 --- a/src/athena/Utility.cpp +++ b/src/athena/Utility.cpp @@ -42,9 +42,10 @@ void fillRandom(atUint8* rndArea, atUint64 count) } } -std::vector& split(const std::string& s, char delim, std::vector& elems) +static std::vector& split(std::string_view s, char delim, std::vector& 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& split(const std::string& s, char delim, std::vector split(const std::string& s, char delim) +std::vector split(std::string_view s, char delim) { std::vector elems; split(s, delim, elems); return elems; } -std::string join(const std::vector& elems, const std::string& delims) +std::string join(const std::vector& elems, std::string_view delims) { std::ostringstream ret; - std::copy(elems.begin(), elems.end(), std::ostream_iterator(ret, delims.c_str())); + std::copy(elems.begin(), elems.end(), std::ostream_iterator(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(src.c_str()); + const utf8proc_uint8_t* buf = reinterpret_cast(src.data()); while (*buf) { utf8proc_int32_t wc;