Remove redundant functions from Utf8Encoder interface

C++20
elsid 3 years ago
parent 2a7d28712f
commit c9c7fb7e49
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -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);
}

@ -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);

@ -4,6 +4,7 @@
#include <string>
#include <cstring>
#include <vector>
#include <string_view>
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);

Loading…
Cancel
Save