Skip to content

Commit a28d2f2

Browse files
committed
small code improvements in src/utils/charsets.cpp
This changes some small pieces of code to have slightly better performance in some cases.
1 parent b9cf3ad commit a28d2f2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/utils/charsets.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ static std::string ucs2_to_utf8(void const* buffer, size_t num_bytes)
8686
while (*ptr)
8787
{
8888
num_bytes += 2;
89-
ptr++;
89+
++ptr;
9090
}
9191
}
9292

9393
std::unique_ptr<char[]> utf8(new char[(num_bytes / 2) * 3 + 1]);
94-
uint8_t const* buf = reinterpret_cast<uint8_t const*>(buffer);
94+
const uint8_t const* buf = reinterpret_cast<uint8_t const*>(buffer);
9595

9696
if (num_bytes >= 2)
9797
{
@@ -150,8 +150,8 @@ static std::string ebulatin_to_utf8(void const* buffer, size_t num_bytes)
150150
{
151151
while (*ptr)
152152
{
153-
num_bytes++;
154-
ptr++;
153+
++num_bytes;
154+
++ptr;
155155
}
156156
}
157157

@@ -180,12 +180,16 @@ std::string toUtf8(const void* buffer, CharacterSet charset, size_t num_bytes)
180180
if (buffer == nullptr)
181181
throw std::logic_error("Cannot convert charset of empty buffer");
182182

183-
if (charset == CharacterSet::UnicodeUcs2)
184-
return ucs2_to_utf8(buffer, num_bytes);
185-
else if (charset == CharacterSet::UnicodeUtf8)
186-
return utf8_to_utf8(buffer, num_bytes);
187-
else
188-
return ebulatin_to_utf8(buffer, num_bytes);
183+
switch (charset)
184+
{
185+
case CharacterSet::UnicodeUcs2:
186+
return ucs2_to_utf8(buffer, num_bytes);
187+
case CharacterSet::UnicodeUtf8:
188+
return utf8_to_utf8(buffer, num_bytes);
189+
case CharacterSet::EbuLatin:
190+
default:
191+
return ebulatin_to_utf8(buffer, num_bytes);
192+
}
189193
}
190194

191195
} // namespace charsets

0 commit comments

Comments
 (0)