diff --git a/components/esm/reader.cpp b/components/esm/reader.cpp index 1f22b95b47..7fcc2cce3a 100644 --- a/components/esm/reader.cpp +++ b/components/esm/reader.cpp @@ -38,45 +38,17 @@ namespace ESM } bool Reader::getStringImpl(std::string& str, std::size_t size, - Files::IStreamPtr filestream, ToUTF8::Utf8Encoder* encoder, bool hasNull) + Files::IStreamPtr filestream, ToUTF8::StatelessUtf8Encoder* encoder, bool hasNull) { std::size_t newSize = size; if (encoder) { - if (!hasNull) - newSize += 1; // Utf8Encoder::getLength() expects a null terminator - - std::string tmp; - tmp.resize(newSize); - filestream->read(&tmp[0], size); - if ((std::size_t)filestream->gcount() == size) + std::string input(size, '\0'); + filestream->read(input.data(), size); + if (filestream->gcount() == static_cast(size)) { - if (hasNull) - { - assert (tmp[newSize - 1] == '\0' - && "ESM4::Reader::getString string is not terminated with a null"); - } - else - { - // NOTE: script text of some mods have terminating null; - // unfortunately we just have to deal with it - //if (tmp[newSize - 2] != '\0') - tmp[newSize - 1] = '\0'; // for Utf8Encoder::getLength() - } - - encoder->toUtf8(tmp, str, newSize - 1); - // NOTE: the encoder converts “keep” as “keep†which increases the length, - // which results in below resize() truncating the string - if (str.size() == newSize) // ascii - str.resize(newSize - 1); // don't want the null terminator - else - { - // this is a horrible hack but fortunately there's only a few like this - std::string tmp2(str.data()); - str.swap(tmp2); - } - + encoder->getUtf8(input, ToUTF8::BufferAllocationPolicy::FitToRequiredSize, str); return true; } } diff --git a/components/esm/reader.hpp b/components/esm/reader.hpp index a5e3dd516b..5356d9bd01 100644 --- a/components/esm/reader.hpp +++ b/components/esm/reader.hpp @@ -31,7 +31,7 @@ namespace ESM virtual inline bool hasMoreRecs() const = 0; - virtual inline void setEncoder(ToUTF8::Utf8Encoder* encoder) = 0; + virtual inline void setEncoder(ToUTF8::StatelessUtf8Encoder* encoder) = 0; // used to check for dependencies e.g. CS::Editor::run() virtual inline const std::vector& getGameFiles() const = 0; @@ -53,7 +53,7 @@ namespace ESM protected: bool getStringImpl(std::string& str, std::size_t size, - Files::IStreamPtr filestream, ToUTF8::Utf8Encoder* encoder, bool hasNull = false); + Files::IStreamPtr filestream, ToUTF8::StatelessUtf8Encoder* encoder, bool hasNull = false); }; } diff --git a/components/esm4/reader.hpp b/components/esm4/reader.hpp index b3f1070495..c1bf808dd5 100644 --- a/components/esm4/reader.hpp +++ b/components/esm4/reader.hpp @@ -77,7 +77,7 @@ namespace ESM4 { ReaderContext mCtx; - ToUTF8::Utf8Encoder* mEncoder; + ToUTF8::StatelessUtf8Encoder* mEncoder; std::size_t mFileSize; @@ -140,7 +140,7 @@ namespace ESM4 { inline bool isEsm4() const final { return true; } - inline void setEncoder(ToUTF8::Utf8Encoder* encoder) final { mEncoder = encoder; }; + inline void setEncoder(ToUTF8::StatelessUtf8Encoder* encoder) final { mEncoder = encoder; }; const std::vector& getGameFiles() const final { return mHeader.mMaster; }