From 8b51ff7ded9c34c8174aaae8c3439229ad22c8f6 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 8 Mar 2024 08:12:32 +0100 Subject: [PATCH] core: add locale aware formatting function to std::string We had locale aware formatting functions that generated QStrings. Create an alternative that creates std::string, since we want that in the core. This commit is unfortunate for two reasons: - The function is called "casprintf()" for analogy with the QString version. However, the non locale aware function is called "format_string_std()" for analogy with "format_string()". Ultimately these names should be unified. Probably, once there are no membuffer users left. - This does UTF-16->UTF-8->UTF-16 roundtrips. The core formatting functions should render UTF-8 and only convert to UTF-16, in the UI layer. Signed-off-by: Berthold Stoeger --- core/format.cpp | 10 ++++++++++ core/format.h | 1 + 2 files changed, 11 insertions(+) diff --git a/core/format.cpp b/core/format.cpp index e9a7c9525..b037361bb 100644 --- a/core/format.cpp +++ b/core/format.cpp @@ -407,3 +407,13 @@ extern "C" void put_vformat_loc(struct membuffer *b, const char *fmt, va_list ar memcpy(b->buffer + b->len, data, utf8_size); b->len += utf8_size; } + +// TODO: Avoid back-and-forth conversion between UTF16 and UTF8. +std::string casprintf_loc(const char *cformat, ...) +{ + va_list ap; + va_start(ap, cformat); + QByteArray utf8 = vqasprintf_loc(cformat, ap).toUtf8(); + va_end(ap); + return std::string(utf8.constData(), utf8.size()); +} diff --git a/core/format.h b/core/format.h index 0498fe2be..511b319f8 100644 --- a/core/format.h +++ b/core/format.h @@ -11,6 +11,7 @@ #include __printf(1, 2) QString qasprintf_loc(const char *cformat, ...); __printf(1, 0) QString vqasprintf_loc(const char *cformat, va_list ap); +__printf(1, 2) std::string casprintf_loc(const char *cformat, ...); #endif #ifdef __cplusplus