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 // Convert to UTF8 and return
if (mEncoder) if (mEncoder)
return mEncoder->getUtf8(ptr, size); return mEncoder->getUtf8(std::string_view(ptr, size));
return std::string (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 // Double check that the input string stops at some point (it might
// contain zero terminators before this, inside its own data, which // contain zero terminators before this, inside its own data, which
// is also ok.) // is also ok.)
assert(input[size] == 0); assert(input[input.size()] == 0);
// Note: The rest of this function is designed for single-character // Note: The rest of this function is designed for single-character
// input encodings only. It also assumes that the input encoding // 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 // Compute output length, and check for pure ascii input at the same
// time. // time.
bool ascii; 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 we're pure ascii, then don't bother converting anything.
if(ascii) if(ascii)
return std::string(input, outlen); return std::string(input.data(), outlen);
// Make sure the output is large enough // Make sure the output is large enough
resize(outlen); resize(outlen);
char *out = &mOutput[0]; char *out = &mOutput[0];
// Translate // Translate
while (*input) for (const char* ptr = input.data(); *ptr;)
copyFromArray(*(input++), out); copyFromArray(*(ptr++), out);
// Make sure that we wrote the correct number of bytes // Make sure that we wrote the correct number of bytes
assert((out-&mOutput[0]) == (int)outlen); 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); 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 // Double check that the input string stops at some point (it might
// contain zero terminators before this, inside its own data, which // contain zero terminators before this, inside its own data, which
// is also ok.) // is also ok.)
assert(input[size] == 0); assert(input[input.size()] == 0);
// TODO: The rest of this function is designed for single-character // TODO: The rest of this function is designed for single-character
// input encodings only. It also assumes that the input the input // 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 // Compute output length, and check for pure ascii input at the same
// time. // time.
bool ascii; 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 we're pure ascii, then don't bother converting anything.
if(ascii) if(ascii)
return std::string(input, outlen); return std::string(input.data(), outlen);
// Make sure the output is large enough // Make sure the output is large enough
resize(outlen); resize(outlen);
char *out = &mOutput[0]; char *out = &mOutput[0];
// Translate // Translate
while(*input) for (const char* ptr = input.data(); *ptr;)
copyFromArray2(input, out); copyFromArray2(ptr, out);
// Make sure that we wrote the correct number of bytes // Make sure that we wrote the correct number of bytes
assert((out-&mOutput[0]) == (int)outlen); assert((out-&mOutput[0]) == (int)outlen);

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <string_view>
namespace ToUTF8 namespace ToUTF8
{ {
@ -28,17 +29,9 @@ namespace ToUTF8
Utf8Encoder(FromType sourceEncoding); Utf8Encoder(FromType sourceEncoding);
// Convert to UTF8 from the previously given code page. // Convert to UTF8 from the previously given code page.
std::string getUtf8(const char *input, size_t size); std::string getUtf8(std::string_view input);
inline std::string getUtf8(const std::string &str)
{
return getUtf8(str.c_str(), str.size());
}
std::string getLegacyEnc(const char *input, size_t size); std::string getLegacyEnc(std::string_view input);
inline std::string getLegacyEnc(const std::string &str)
{
return getLegacyEnc(str.c_str(), str.size());
}
private: private:
void resize(size_t size); void resize(size_t size);

Loading…
Cancel
Save