diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 0ab398d95..5ab770490 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 0ab398d950b5e46a8b3810a6a483ace393be8b50 +Subproject commit 5ab770490e69e2873fd18eb3d0e40735610dc034 diff --git a/lib/libimhex/include/hex/helpers/file.hpp b/lib/libimhex/include/hex/helpers/file.hpp index e278a589f..dfcd44d69 100644 --- a/lib/libimhex/include/hex/helpers/file.hpp +++ b/lib/libimhex/include/hex/helpers/file.hpp @@ -47,10 +47,12 @@ namespace hex::fs { size_t readBuffer(u8 *buffer, size_t size); std::vector readBytes(size_t numBytes = 0); std::string readString(size_t numBytes = 0); + std::u8string readU8String(size_t numBytes = 0); void write(const u8 *buffer, size_t size); void write(const std::vector &bytes); void write(const std::string &string); + void write(const std::u8string &string); [[nodiscard]] size_t getSize() const; void setSize(u64 size); diff --git a/lib/libimhex/source/helpers/file.cpp b/lib/libimhex/source/helpers/file.cpp index c14d8b530..bd5c6eeef 100644 --- a/lib/libimhex/source/helpers/file.cpp +++ b/lib/libimhex/source/helpers/file.cpp @@ -89,10 +89,24 @@ namespace hex::fs { if (bytes.empty()) return ""; - const char *cString = reinterpret_cast(bytes.data()); + auto cString = reinterpret_cast(bytes.data()); return { cString, std::min(bytes.size(), std::strlen(cString)) }; } + std::u8string File::readU8String(size_t numBytes) { + if (!isValid()) return {}; + + if (getSize() == 0) return {}; + + auto bytes = readBytes(numBytes); + + if (bytes.empty()) + return u8""; + + auto cString = reinterpret_cast(bytes.data()); + return { cString, std::min(bytes.size(), std::strlen(reinterpret_cast(bytes.data()))) }; + } + void File::write(const u8 *buffer, size_t size) { if (!isValid()) return; @@ -111,6 +125,12 @@ namespace hex::fs { std::fwrite(string.data(), string.size(), 1, this->m_file); } + void File::write(const std::u8string &string) { + if (!isValid()) return; + + std::fwrite(string.data(), string.size(), 1, this->m_file); + } + size_t File::getSize() const { if (!isValid()) return 0; diff --git a/tests/helpers/source/file.cpp b/tests/helpers/source/file.cpp index 6b96fcb11..fe5218ec5 100644 --- a/tests/helpers/source/file.cpp +++ b/tests/helpers/source/file.cpp @@ -9,6 +9,8 @@ TEST_SEQUENCE("FileAccess") { const auto FilePath = std::fs::current_path() / "file.txt"; const auto FileContent = "Hello World"; + std::fs::create_directories(FilePath.parent_path()); + { hex::fs::File file(FilePath, hex::fs::File::Mode::Create); TEST_ASSERT(file.isValid()); @@ -38,5 +40,43 @@ TEST_SEQUENCE("FileAccess") { TEST_FAIL(); } + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("UTF-8 Path") { + const auto FilePath = std::fs::current_path() / u8"读写汉字" / u8"привет.txt"; + const auto FileContent = u8"שלום עולם"; + + std::fs::create_directories(FilePath.parent_path()); + + { + hex::fs::File file(FilePath, hex::fs::File::Mode::Create); + TEST_ASSERT(file.isValid()); + + file.write(FileContent); + } + + { + hex::fs::File file(FilePath, hex::fs::File::Mode::Read); + TEST_ASSERT(file.isValid()); + + TEST_ASSERT(file.readU8String() == FileContent); + } + + { + hex::fs::File file(FilePath, hex::fs::File::Mode::Write); + TEST_ASSERT(file.isValid()); + + + file.remove(); + TEST_ASSERT(!file.isValid()); + } + + { + hex::fs::File file(FilePath, hex::fs::File::Mode::Read); + if (file.isValid()) + TEST_FAIL(); + } + TEST_SUCCESS(); }; \ No newline at end of file