mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-28 21:07:59 +03:00
Return string_view from Utf8Encoder functions
To avoid redundant std::string constructions.
This commit is contained in:
parent
c9c7fb7e49
commit
c75e938c46
5 changed files with 30 additions and 21 deletions
|
@ -77,7 +77,7 @@ Utf8Encoder::Utf8Encoder(const FromType sourceEncoding):
|
|||
}
|
||||
}
|
||||
|
||||
std::string Utf8Encoder::getUtf8(std::string_view input)
|
||||
std::string_view Utf8Encoder::getUtf8(std::string_view input)
|
||||
{
|
||||
if (input.empty())
|
||||
return input;
|
||||
|
@ -100,7 +100,7 @@ std::string Utf8Encoder::getUtf8(std::string_view input)
|
|||
|
||||
// If we're pure ascii, then don't bother converting anything.
|
||||
if(ascii)
|
||||
return std::string(input.data(), outlen);
|
||||
return std::string_view(input.data(), outlen);
|
||||
|
||||
// Make sure the output is large enough
|
||||
resize(outlen);
|
||||
|
@ -117,11 +117,10 @@ std::string Utf8Encoder::getUtf8(std::string_view input)
|
|||
assert(mOutput.size() > outlen);
|
||||
assert(mOutput[outlen] == 0);
|
||||
|
||||
// Return a string
|
||||
return std::string(&mOutput[0], outlen);
|
||||
return std::string_view(mOutput.data(), outlen);
|
||||
}
|
||||
|
||||
std::string Utf8Encoder::getLegacyEnc(std::string_view input)
|
||||
std::string_view Utf8Encoder::getLegacyEnc(std::string_view input)
|
||||
{
|
||||
if (input.empty())
|
||||
return input;
|
||||
|
@ -144,7 +143,7 @@ std::string Utf8Encoder::getLegacyEnc(std::string_view input)
|
|||
|
||||
// If we're pure ascii, then don't bother converting anything.
|
||||
if(ascii)
|
||||
return std::string(input.data(), outlen);
|
||||
return std::string_view(input.data(), outlen);
|
||||
|
||||
// Make sure the output is large enough
|
||||
resize(outlen);
|
||||
|
@ -161,8 +160,7 @@ std::string Utf8Encoder::getLegacyEnc(std::string_view input)
|
|||
assert(mOutput.size() > outlen);
|
||||
assert(mOutput[outlen] == 0);
|
||||
|
||||
// Return a string
|
||||
return std::string(&mOutput[0], outlen);
|
||||
return std::string_view(mOutput.data(), outlen);
|
||||
}
|
||||
|
||||
// Make sure the output vector is large enough for 'size' bytes,
|
||||
|
|
|
@ -28,10 +28,14 @@ namespace ToUTF8
|
|||
public:
|
||||
Utf8Encoder(FromType sourceEncoding);
|
||||
|
||||
// Convert to UTF8 from the previously given code page.
|
||||
std::string getUtf8(std::string_view input);
|
||||
/// Convert to UTF8 from the previously given code page.
|
||||
/// Returns a view to internal buffer invalidate by next getUtf8 or getLegacyEnc call if input is not
|
||||
/// ASCII-only string. Otherwise returns a view to the input.
|
||||
std::string_view getUtf8(std::string_view input);
|
||||
|
||||
std::string getLegacyEnc(std::string_view input);
|
||||
/// Returns a view to internal buffer invalidate by next getUtf8 or getLegacyEnc call if input is not
|
||||
/// ASCII-only string. Otherwise returns a view to the input.
|
||||
std::string_view getLegacyEnc(std::string_view input);
|
||||
|
||||
private:
|
||||
void resize(size_t size);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue