diff options
-rw-r--r-- | include/cru/common/Base.hpp | 3 | ||||
-rw-r--r-- | include/cru/common/StringUtil.hpp | 6 | ||||
-rw-r--r-- | include/cru/common/io/Stream.hpp | 8 | ||||
-rw-r--r-- | src/common/Base.cpp | 7 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/StringUtil.cpp | 10 | ||||
-rw-r--r-- | src/common/io/Stream.cpp | 13 |
7 files changed, 45 insertions, 3 deletions
diff --git a/include/cru/common/Base.hpp b/include/cru/common/Base.hpp index 85f07132..bab4f733 100644 --- a/include/cru/common/Base.hpp +++ b/include/cru/common/Base.hpp @@ -12,7 +12,6 @@ #endif #include <gsl/gsl> -#include <stdexcept> #define CRU_UNUSED(entity) static_cast<void>(entity); @@ -82,7 +81,7 @@ struct CRU_BASE_API Interface { virtual ~Interface() = default; }; -[[noreturn]] inline void UnreachableCode() { std::terminate(); } +[[noreturn]] void CRU_BASE_API UnreachableCode(); using Index = gsl::index; diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index 27ad4b43..3c673db1 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -223,4 +223,10 @@ char16_t CRU_BASE_API ToLower(char16_t c); char16_t CRU_BASE_API ToUpper(char16_t c); bool CRU_BASE_API IsWhitespace(char16_t c); + +Utf8CodePointIterator CRU_BASE_API CreateUtf8Iterator(const std::byte* buffer, + Index size); +Utf8CodePointIterator CRU_BASE_API +CreateUtf8Iterator(const std::vector<std::byte>& buffer); + } // namespace cru diff --git a/include/cru/common/io/Stream.hpp b/include/cru/common/io/Stream.hpp index fedd4a0f..78beba21 100644 --- a/include/cru/common/io/Stream.hpp +++ b/include/cru/common/io/Stream.hpp @@ -2,7 +2,10 @@ #include "../Base.hpp" +#include "../String.hpp" + #include <cstddef> +#include <vector> namespace cru::io { class CRU_BASE_API Stream : public Object { @@ -33,6 +36,11 @@ class CRU_BASE_API Stream : public Object { Index Write(const char* buffer, Index offset, Index size); Index Write(const char* buffer, Index size); + virtual std::vector<std::byte> ReadAll(); + + // Utf8 encoding. + String ReadAllAsString(); + virtual void Flush(); virtual void Close(); diff --git a/src/common/Base.cpp b/src/common/Base.cpp new file mode 100644 index 00000000..dbbc0d61 --- /dev/null +++ b/src/common/Base.cpp @@ -0,0 +1,7 @@ +#include "cru/common/Base.hpp" + +#include <stdexcept> + +namespace cru { +void UnreachableCode() { std::terminate(); } +} // namespace cru diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index b5b20d46..d1e3e35a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,5 +1,6 @@ set(CRU_BASE_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/common) add_library(cru_base SHARED + Base.cpp Exception.cpp Logger.cpp PropertyTree.cpp diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index 6e478033..04435a0a 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -1,5 +1,4 @@ #include "cru/common/StringUtil.hpp" -#include <functional> #include "cru/common/Base.hpp" #include "cru/common/Exception.hpp" @@ -258,4 +257,13 @@ char16_t ToUpper(char16_t c) { bool IsWhitespace(char16_t c) { return c == u' ' || c == u'\t' || c == u'\n' || c == u'\r'; } + +Utf8CodePointIterator CreateUtf8Iterator(const std::byte* buffer, Index size) { + return Utf8CodePointIterator(reinterpret_cast<const char*>(buffer), size); +} + +Utf8CodePointIterator CreateUtf8Iterator(const std::vector<std::byte>& buffer) { + return CreateUtf8Iterator(buffer.data(), buffer.size()); +} + } // namespace cru diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp index 5062aa72..bf102441 100644 --- a/src/common/io/Stream.cpp +++ b/src/common/io/Stream.cpp @@ -27,6 +27,19 @@ Index Stream::Write(const char* buffer, Index size) { return Write(reinterpret_cast<const std::byte*>(buffer), size); } +std::vector<std::byte> Stream::ReadAll() { + std::vector<std::byte> buffer; + buffer.resize(GetSize()); + Read(buffer.data(), 0, buffer.size()); + return buffer; +} + +String Stream::ReadAllAsString() { + auto buffer = ReadAll(); + return String::FromUtf8(reinterpret_cast<const char*>(buffer.data()), + buffer.size()); +} + void Stream::Flush() {} void Stream::Close() {} |