summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2018-12-11 20:48:20 -0500
committerNat Goodspeed <nat@lindenlab.com>2018-12-11 20:48:20 -0500
commit4e894eb2a7ed6651c54890cd20106bfacd61ef0a (patch)
treea1464528bfb90c5b35c5545e135217d947be21e2
parentbb4a649b9c324e7989de6e59398c85e80fe2acc7 (diff)
SL-10153: Improve ll_convert_string_to_wide() and its converse.
Instead of returning a wchar_t* and requiring the caller to delete it later, return a std::basic_string<wchar_t> that's self-cleaning. If the caller wants a wchar_t*, s/he can call c_str() on the returned string. Default the code_page parameter to CP_UTF8, since we try to be really consistent about using UTF-8 encoding for all our internal std::strings.
-rw-r--r--indra/llcommon/llstring.cpp32
-rw-r--r--indra/llcommon/llstring.h10
2 files changed, 28 insertions, 14 deletions
diff --git a/indra/llcommon/llstring.cpp b/indra/llcommon/llstring.cpp
index 9a02fecd72..42390c8a7b 100644
--- a/indra/llcommon/llstring.cpp
+++ b/indra/llcommon/llstring.cpp
@@ -30,6 +30,7 @@
#include "llerror.h"
#include "llfasttimer.h"
#include "llsd.h"
+#include <vector>
#if LL_WINDOWS
#include "llwin32headerslean.h"
@@ -672,6 +673,11 @@ namespace snprintf_hack
}
}
+std::string ll_convert_wide_to_string(const wchar_t* in)
+{
+ return ll_convert_wide_to_string(in, CP_UTF8);
+}
+
std::string ll_convert_wide_to_string(const wchar_t* in, unsigned int code_page)
{
std::string out;
@@ -709,7 +715,12 @@ std::string ll_convert_wide_to_string(const wchar_t* in, unsigned int code_page)
return out;
}
-wchar_t* ll_convert_string_to_wide(const std::string& in, unsigned int code_page)
+std::basic_string<wchar_t> ll_convert_string_to_wide(const std::string& in)
+{
+ return ll_convert_string_to_wide(in, CP_UTF8);
+}
+
+std::basic_string<wchar_t> ll_convert_string_to_wide(const std::string& in, unsigned int code_page)
{
// From review:
// We can preallocate a wide char buffer that is the same length (in wchar_t elements) as the utf8 input,
@@ -719,24 +730,25 @@ wchar_t* ll_convert_string_to_wide(const std::string& in, unsigned int code_page
// but we *are* seeing string operations taking a bunch of time, especially when constructing widgets.
// int output_str_len = MultiByteToWideChar(code_page, 0, in.c_str(), in.length(), NULL, 0);
- // reserve place to NULL terminator
- int output_str_len = in.length();
- wchar_t* w_out = new wchar_t[output_str_len + 1];
+ // reserve an output buffer that will be destroyed on exit, with a place
+ // to put NULL terminator
+ std::vector<wchar_t> w_out(in.length() + 1);
- memset(w_out, 0, output_str_len + 1);
- int real_output_str_len = MultiByteToWideChar (code_page, 0, in.c_str(), in.length(), w_out, output_str_len);
+ memset(&w_out[0], 0, w_out.size());
+ int real_output_str_len = MultiByteToWideChar(code_page, 0, in.c_str(), in.length(),
+ &w_out[0], w_out.size() - 1);
//looks like MultiByteToWideChar didn't add null terminator to converted string, see EXT-4858.
w_out[real_output_str_len] = 0;
- return w_out;
+ // construct string<wchar_t> from our temporary output buffer
+ return {&w_out[0]};
}
std::string ll_convert_string_to_utf8_string(const std::string& in)
{
- wchar_t* w_mesg = ll_convert_string_to_wide(in, CP_ACP);
- std::string out_utf8(ll_convert_wide_to_string(w_mesg, CP_UTF8));
- delete[] w_mesg;
+ auto w_mesg = ll_convert_string_to_wide(in, CP_ACP);
+ std::string out_utf8(ll_convert_wide_to_string(w_mesg.c_str(), CP_UTF8));
return out_utf8;
}
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h
index 68ee9db46b..7c3e9f952d 100644
--- a/indra/llcommon/llstring.h
+++ b/indra/llcommon/llstring.h
@@ -635,16 +635,18 @@ using snprintf_hack::snprintf;
* This replaces the unsafe W2A macro from ATL.
*/
LL_COMMON_API std::string ll_convert_wide_to_string(const wchar_t* in, unsigned int code_page);
+LL_COMMON_API std::string ll_convert_wide_to_string(const wchar_t* in); // default CP_UTF8
/**
* Converts a string to wide string.
- *
- * It will allocate memory for result string with "new []". Don't forget to release it with "delete []".
*/
-LL_COMMON_API wchar_t* ll_convert_string_to_wide(const std::string& in, unsigned int code_page);
+LL_COMMON_API std::basic_string<wchar_t> ll_convert_string_to_wide(const std::string& in,
+ unsigned int code_page);
+LL_COMMON_API std::basic_string<wchar_t> ll_convert_string_to_wide(const std::string& in);
+ // default CP_UTF8
/**
- * Converts incoming string into urf8 string
+ * Converts incoming string into utf8 string
*
*/
LL_COMMON_API std::string ll_convert_string_to_utf8_string(const std::string& in);