From 398cc2b6399f55de2f58d39b830ddcae2fd94aa1 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 28 May 2024 19:13:57 +0200 Subject: [PATCH] cleanup: remove localized snprintf() functions The last use of these functions was removed in ae299d5e663c. And that's a good thing, because snprintf-style interfaces make zero sense in times of variable-length character encodings. Signed-off-by: Berthold Stoeger --- core/format.cpp | 54 ------------------------------------------------- core/format.h | 15 -------------- 2 files changed, 69 deletions(-) diff --git a/core/format.cpp b/core/format.cpp index 345b564f9..2215b4191 100644 --- a/core/format.cpp +++ b/core/format.cpp @@ -343,60 +343,6 @@ QString vqasprintf_loc(const char *fmt, va_list ap_in) return ret; } -// Put a formated string respecting the default locale into a C-style array in UTF-8 encoding. -// The only complication arises from the fact that we don't want to cut through multi-byte UTF-8 code points. -extern "C" int snprintf_loc(char *dst, size_t size, const char *cformat, ...) -{ - va_list ap; - va_start(ap, cformat); - int res = vsnprintf_loc(dst, size, cformat, ap); - va_end(ap); - return res; -} - -extern "C" int vsnprintf_loc(char *dst, size_t size, const char *cformat, va_list ap) -{ - QByteArray utf8 = vqasprintf_loc(cformat, ap).toUtf8(); - const char *data = utf8.constData(); - size_t utf8_size = utf8.size(); - if (size == 0) - return utf8_size; - if (size < utf8_size + 1) { - memcpy(dst, data, size - 1); - if ((data[size - 1] & 0xC0) == 0x80) { - // We truncated a multi-byte UTF-8 encoding. - --size; - // Jump to last copied byte. - if (size > 0) - --size; - while(size > 0 && (dst[size] & 0xC0) == 0x80) - --size; - dst[size] = 0; - } else { - dst[size - 1] = 0; - } - } else { - memcpy(dst, data, utf8_size + 1); // QByteArray guarantees a trailing 0 - } - return utf8_size; -} - -int asprintf_loc(char **dst, const char *cformat, ...) -{ - va_list ap; - va_start(ap, cformat); - int res = vasprintf_loc(dst, cformat, ap); - va_end(ap); - return res; -} - -int vasprintf_loc(char **dst, const char *cformat, va_list ap) -{ - QByteArray utf8 = vqasprintf_loc(cformat, ap).toUtf8(); - *dst = strdup(utf8.constData()); - return utf8.size(); -} - extern "C" void put_vformat_loc(struct membuffer *b, const char *fmt, va_list args) { QByteArray utf8 = vqasprintf_loc(fmt, args).toUtf8(); diff --git a/core/format.h b/core/format.h index 71f8a7875..599911082 100644 --- a/core/format.h +++ b/core/format.h @@ -12,22 +12,7 @@ __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 -extern "C" { -#endif - -__printf(3, 4) int snprintf_loc(char *dst, size_t size, const char *cformat, ...); -__printf(3, 0) int vsnprintf_loc(char *dst, size_t size, const char *cformat, va_list ap); -__printf(2, 3) int asprintf_loc(char **dst, const char *cformat, ...); -__printf(2, 0) int vasprintf_loc(char **dst, const char *cformat, va_list ap); - -#ifdef __cplusplus -} - __printf(1, 2) std::string format_string_std(const char *fmt, ...); - #endif #endif