From fc97151aec4a6ba106b6b302fffb26c7314dc440 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 4 Feb 2021 13:28:48 -0800 Subject: [PATCH] Add a variant of wcs2string which accepts a ptr, length pair This will be useful when refactoring separated buffers. --- src/common.cpp | 8 +++++--- src/common.h | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index 6694a308c..98b00da2a 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -406,10 +406,12 @@ wcstring str2wcstring(const std::string &in, size_t len) { return str2wcs_internal(in.data(), len); } -std::string wcs2string(const wcstring &input) { +std::string wcs2string(const wcstring &input) { return wcs2string(input.data(), input.size()); } + +std::string wcs2string(const wchar_t *in, size_t len) { std::string result; - result.reserve(input.size()); - wcs2string_callback(input.data(), input.size(), [&](const char *buff, size_t bufflen) { + result.reserve(len); + wcs2string_callback(in, len, [&](const char *buff, size_t bufflen) { result.append(buff, bufflen); return true; }); diff --git a/src/common.h b/src/common.h index 328c6f1f3..dbd0b4de6 100644 --- a/src/common.h +++ b/src/common.h @@ -291,6 +291,7 @@ wcstring str2wcstring(const std::string &in, size_t len); /// This function decodes illegal character sequences in a reversible way using the private use /// area. std::string wcs2string(const wcstring &input); +std::string wcs2string(const wchar_t *in, size_t len); // Check if we are running in the test mode, where we should suppress error output #define TESTS_PROGRAM_NAME L"(ignore)"