Introduce wcs2string_appending

wcs2string_appending is like wcs2string, but appends to a std::string
instead of creating a new one. This will be more efficient when a string
can be reused, or if we want to accumulate multiple wcstrings into a
single std::string.
This commit is contained in:
ridiculousfish 2021-02-17 15:36:22 -08:00
parent fce485e6a8
commit f1b6a5939d
2 changed files with 11 additions and 3 deletions

View file

@ -413,12 +413,17 @@ std::string wcs2string(const wcstring &input) { return wcs2string(input.data(),
std::string wcs2string(const wchar_t *in, size_t len) {
if (len == 0) return std::string{};
std::string result;
result.reserve(len);
wcs2string_appending(in, len, &result);
return result;
}
void wcs2string_appending(const wchar_t *in, size_t len, std::string *receiver) {
assert(receiver && "Null receiver");
receiver->reserve(receiver->size() + len);
wcs2string_callback(in, len, [&](const char *buff, size_t bufflen) {
result.append(buff, bufflen);
receiver->append(buff, bufflen);
return true;
});
return result;
}
/// Test if the character can be encoded using the current locale.

View file

@ -307,6 +307,9 @@ wcstring str2wcstring(const std::string &in, size_t len);
std::string wcs2string(const wcstring &input);
std::string wcs2string(const wchar_t *in, size_t len);
/// Like wcs2string, but appends to \p receiver instead of returning a new string.
void wcs2string_appending(const wchar_t *in, size_t len, std::string *receiver);
// Check if we are running in the test mode, where we should suppress error output
#define TESTS_PROGRAM_NAME L"(ignore)"
bool should_suppress_stderr_for_tests();