diff --git a/components/esm3/esmreader.cpp b/components/esm3/esmreader.cpp index a10eba2378..667132c60f 100644 --- a/components/esm3/esmreader.cpp +++ b/components/esm3/esmreader.cpp @@ -320,7 +320,7 @@ std::string ESMReader::getString(int size) // Convert to UTF8 and return if (mEncoder) - return mEncoder->getUtf8(ptr, size); + return mEncoder->getUtf8(std::string_view(ptr, size)); return std::string (ptr, size); } diff --git a/components/to_utf8/to_utf8.cpp b/components/to_utf8/to_utf8.cpp index f7dc33fcbf..3c4421c605 100644 --- a/components/to_utf8/to_utf8.cpp +++ b/components/to_utf8/to_utf8.cpp @@ -77,12 +77,15 @@ Utf8Encoder::Utf8Encoder(const FromType sourceEncoding): } } -std::string Utf8Encoder::getUtf8(const char* input, size_t size) +std::string Utf8Encoder::getUtf8(std::string_view input) { + if (input.empty()) + return input; + // Double check that the input string stops at some point (it might // contain zero terminators before this, inside its own data, which // is also ok.) - assert(input[size] == 0); + assert(input[input.size()] == 0); // Note: The rest of this function is designed for single-character // input encodings only. It also assumes that the input encoding @@ -93,19 +96,19 @@ std::string Utf8Encoder::getUtf8(const char* input, size_t size) // Compute output length, and check for pure ascii input at the same // time. bool ascii; - size_t outlen = getLength(input, ascii); + size_t outlen = getLength(input.data(), ascii); // If we're pure ascii, then don't bother converting anything. if(ascii) - return std::string(input, outlen); + return std::string(input.data(), outlen); // Make sure the output is large enough resize(outlen); char *out = &mOutput[0]; // Translate - while (*input) - copyFromArray(*(input++), out); + for (const char* ptr = input.data(); *ptr;) + copyFromArray(*(ptr++), out); // Make sure that we wrote the correct number of bytes assert((out-&mOutput[0]) == (int)outlen); @@ -118,12 +121,15 @@ std::string Utf8Encoder::getUtf8(const char* input, size_t size) return std::string(&mOutput[0], outlen); } -std::string Utf8Encoder::getLegacyEnc(const char *input, size_t size) +std::string Utf8Encoder::getLegacyEnc(std::string_view input) { + if (input.empty()) + return input; + // Double check that the input string stops at some point (it might // contain zero terminators before this, inside its own data, which // is also ok.) - assert(input[size] == 0); + assert(input[input.size()] == 0); // TODO: The rest of this function is designed for single-character // input encodings only. It also assumes that the input the input @@ -134,19 +140,19 @@ std::string Utf8Encoder::getLegacyEnc(const char *input, size_t size) // Compute output length, and check for pure ascii input at the same // time. bool ascii; - size_t outlen = getLength2(input, ascii); + size_t outlen = getLength2(input.data(), ascii); // If we're pure ascii, then don't bother converting anything. if(ascii) - return std::string(input, outlen); + return std::string(input.data(), outlen); // Make sure the output is large enough resize(outlen); char *out = &mOutput[0]; // Translate - while(*input) - copyFromArray2(input, out); + for (const char* ptr = input.data(); *ptr;) + copyFromArray2(ptr, out); // Make sure that we wrote the correct number of bytes assert((out-&mOutput[0]) == (int)outlen); diff --git a/components/to_utf8/to_utf8.hpp b/components/to_utf8/to_utf8.hpp index d8c9f09d5d..07960ae3f9 100644 --- a/components/to_utf8/to_utf8.hpp +++ b/components/to_utf8/to_utf8.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace ToUTF8 { @@ -28,17 +29,9 @@ namespace ToUTF8 Utf8Encoder(FromType sourceEncoding); // Convert to UTF8 from the previously given code page. - std::string getUtf8(const char *input, size_t size); - inline std::string getUtf8(const std::string &str) - { - return getUtf8(str.c_str(), str.size()); - } - - std::string getLegacyEnc(const char *input, size_t size); - inline std::string getLegacyEnc(const std::string &str) - { - return getLegacyEnc(str.c_str(), str.size()); - } + std::string getUtf8(std::string_view input); + + std::string getLegacyEnc(std::string_view input); private: void resize(size_t size);