[Lldb-commits] [lldb] [lldb] Use Locale to convert between std::wstring and std::string (NFC) (PR #112582)
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Thu Oct 17 06:56:52 PDT 2024
================
@@ -76,6 +76,46 @@ using namespace lldb_private::line_editor;
#endif // #if LLDB_EDITLINE_USE_WCHAR
+#if LLDB_EDITLINE_USE_WCHAR
+std::string ToBytes(const std::wstring &in) {
+ static std::locale locale("C.UTF-8");
+ static const auto &cvt =
+ std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(locale);
+
+ const size_t length = in.length();
+ std::string output(length + 1, 0x0);
+
+ std::mbstate_t mbs{};
+ const wchar_t *in_next;
+ char *out_next;
+
+ if (cvt.out(mbs, in.data(), in.data() + length + 1, in_next, output.data(),
+ output.data() + output.length() + 1,
+ out_next) == std::codecvt_base::ok)
+ return output;
+ return {};
+}
+
+std::wstring FromBytes(const std::string &in) {
+ static std::locale locale("C.UTF-8");
+ static const auto &cvt =
+ std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(locale);
+
+ const size_t length = in.length();
+ std::wstring output(length + 1, 0x0);
----------------
labath wrote:
I believe this direction should be ok (even without the spurious +1).
https://github.com/llvm/llvm-project/pull/112582
More information about the lldb-commits
mailing list