summaryrefslogtreecommitdiff
path: root/src/common/string_util.cpp
diff options
context:
space:
mode:
authorGravatar Lioncash2018-07-22 15:47:37 -0400
committerGravatar Lioncash2018-07-22 16:39:21 -0400
commit9d331221970a26a2d2f68fe51c0249b62fafd0a2 (patch)
treeac87eddd414ff4cbd1983070ebcf92e32c4973d4 /src/common/string_util.cpp
parentstring_util: Use emplace_back() in SplitString() instead of push_back() (diff)
downloadyuzu-9d331221970a26a2d2f68fe51c0249b62fafd0a2.tar.gz
yuzu-9d331221970a26a2d2f68fe51c0249b62fafd0a2.tar.xz
yuzu-9d331221970a26a2d2f68fe51c0249b62fafd0a2.zip
string_util: Get rid of separate resize() in CPToUTF16(), UTF16ToUTF8(), CodeToUTF8() and UTF8ToUTF16()
There's no need to perform the resize separately here, since the constructor allows presizing the buffer. Also move the empty string check before the construction of the string to make the early out more straightforward.
Diffstat (limited to 'src/common/string_util.cpp')
-rw-r--r--src/common/string_util.cpp42
1 files changed, 22 insertions, 20 deletions
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index 6737655a5..1f0456aee 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -220,31 +220,37 @@ std::u16string UTF8ToUTF16(const std::string& input) {
220} 220}
221 221
222static std::wstring CPToUTF16(u32 code_page, const std::string& input) { 222static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
223 auto const size = 223 const auto size =
224 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0); 224 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
225 225
226 std::wstring output; 226 if (size == 0) {
227 output.resize(size); 227 return L"";
228 }
229
230 std::wstring output(size, L'\0');
228 231
229 if (size == 0 || 232 if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
230 size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), 233 &output[0], static_cast<int>(output.size()))) {
231 &output[0], static_cast<int>(output.size())))
232 output.clear(); 234 output.clear();
235 }
233 236
234 return output; 237 return output;
235} 238}
236 239
237std::string UTF16ToUTF8(const std::wstring& input) { 240std::string UTF16ToUTF8(const std::wstring& input) {
238 auto const size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), 241 const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
239 nullptr, 0, nullptr, nullptr); 242 nullptr, 0, nullptr, nullptr);
243 if (size == 0) {
244 return "";
245 }
240 246
241 std::string output; 247 std::string output(size, '\0');
242 output.resize(size);
243 248
244 if (size == 0 || 249 if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
245 size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), 250 &output[0], static_cast<int>(output.size()), nullptr,
246 &output[0], static_cast<int>(output.size()), nullptr, nullptr)) 251 nullptr)) {
247 output.clear(); 252 output.clear();
253 }
248 254
249 return output; 255 return output;
250} 256}
@@ -265,8 +271,6 @@ std::string CP1252ToUTF8(const std::string& input) {
265 271
266template <typename T> 272template <typename T>
267static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) { 273static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
268 std::string result;
269
270 iconv_t const conv_desc = iconv_open("UTF-8", fromcode); 274 iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
271 if ((iconv_t)(-1) == conv_desc) { 275 if ((iconv_t)(-1) == conv_desc) {
272 LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno)); 276 LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno));
@@ -278,8 +282,7 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
278 // Multiply by 4, which is the max number of bytes to encode a codepoint 282 // Multiply by 4, which is the max number of bytes to encode a codepoint
279 const size_t out_buffer_size = 4 * in_bytes; 283 const size_t out_buffer_size = 4 * in_bytes;
280 284
281 std::string out_buffer; 285 std::string out_buffer(out_buffer_size, '\0');
282 out_buffer.resize(out_buffer_size);
283 286
284 auto src_buffer = &input[0]; 287 auto src_buffer = &input[0];
285 size_t src_bytes = in_bytes; 288 size_t src_bytes = in_bytes;
@@ -304,6 +307,7 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
304 } 307 }
305 } 308 }
306 309
310 std::string result;
307 out_buffer.resize(out_buffer_size - dst_bytes); 311 out_buffer.resize(out_buffer_size - dst_bytes);
308 out_buffer.swap(result); 312 out_buffer.swap(result);
309 313
@@ -313,8 +317,6 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
313} 317}
314 318
315std::u16string UTF8ToUTF16(const std::string& input) { 319std::u16string UTF8ToUTF16(const std::string& input) {
316 std::u16string result;
317
318 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8"); 320 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
319 if ((iconv_t)(-1) == conv_desc) { 321 if ((iconv_t)(-1) == conv_desc) {
320 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno)); 322 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno));
@@ -326,8 +328,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
326 // Multiply by 4, which is the max number of bytes to encode a codepoint 328 // Multiply by 4, which is the max number of bytes to encode a codepoint
327 const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes; 329 const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes;
328 330
329 std::u16string out_buffer; 331 std::u16string out_buffer(out_buffer_size, char16_t{});
330 out_buffer.resize(out_buffer_size);
331 332
332 char* src_buffer = const_cast<char*>(&input[0]); 333 char* src_buffer = const_cast<char*>(&input[0]);
333 size_t src_bytes = in_bytes; 334 size_t src_bytes = in_bytes;
@@ -352,6 +353,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
352 } 353 }
353 } 354 }
354 355
356 std::u16string result;
355 out_buffer.resize(out_buffer_size - dst_bytes); 357 out_buffer.resize(out_buffer_size - dst_bytes);
356 out_buffer.swap(result); 358 out_buffer.swap(result);
357 359