[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:53 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);
----------------
labath wrote:
This isn't right. A single wide character can take up to four bytes in the utf8 encoding. This works in `CompleteCharacter` as it's only interested in a single character (and it does the conversion in the other direction). We might be able get away with overapproximating the size, given that the inputs here should be fairly small, but adding a loop with partial conversions should not be that hard either.
https://github.com/llvm/llvm-project/pull/112582
More information about the lldb-commits
mailing list