mirror of
https://github.com/OpenMW/openmw.git
synced 2025-03-30 14:06:41 +00:00
Fix buffer resizing by StatelessUtf8Encoder
This commit is contained in:
parent
8a0fb4d633
commit
875d9dcead
2 changed files with 27 additions and 1 deletions
|
@ -136,4 +136,24 @@ namespace
|
|||
Params {ToUTF8::WINDOWS_1251, "russian-win1251.txt", "russian-utf8.txt"},
|
||||
Params {ToUTF8::WINDOWS_1252, "french-win1252.txt", "french-utf8.txt"}
|
||||
));
|
||||
|
||||
TEST(StatelessUtf8EncoderTest, shouldCleanupBuffer)
|
||||
{
|
||||
std::string buffer;
|
||||
StatelessUtf8Encoder encoder(FromType::WINDOWS_1252);
|
||||
encoder.getUtf8(std::string_view("long string\x92"), BufferAllocationPolicy::UseGrowFactor, buffer);
|
||||
const std::string shortString("short\x92");
|
||||
ASSERT_GT(buffer.size(), shortString.size());
|
||||
const std::string_view shortUtf8 = encoder.getUtf8(shortString, BufferAllocationPolicy::UseGrowFactor, buffer);
|
||||
ASSERT_GE(buffer.size(), shortUtf8.size());
|
||||
EXPECT_EQ(buffer[shortUtf8.size()], '\0') << buffer;
|
||||
}
|
||||
|
||||
TEST(StatelessUtf8EncoderTest, withFitToRequiredSizeShouldResizeBuffer)
|
||||
{
|
||||
std::string buffer;
|
||||
StatelessUtf8Encoder encoder(FromType::WINDOWS_1252);
|
||||
const std::string_view utf8 = encoder.getUtf8(std::string_view("long string\x92"), BufferAllocationPolicy::FitToRequiredSize, buffer);
|
||||
EXPECT_EQ(buffer.size(), utf8.size());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,7 +72,13 @@ namespace
|
|||
// including a terminating zero after it.
|
||||
void resize(std::size_t size, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer)
|
||||
{
|
||||
if (buffer.size() >= size)
|
||||
if (buffer.size() > size)
|
||||
{
|
||||
buffer[size] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (buffer.size() == size)
|
||||
return;
|
||||
|
||||
switch (bufferAllocationPolicy)
|
||||
|
|
Loading…
Reference in a new issue