From ad8f394835ef5c3f22a0f10cf3b35e3e6e9c2af7 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 8 Feb 2025 10:51:47 +0100 Subject: [PATCH] Codechange: introduce string::iterator aware Utf8Decode, Utf8Encode and Utf8PrevChar --- src/string_func.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/string_func.h b/src/string_func.h index eda01a566d..c7c9478dcb 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -76,9 +76,11 @@ inline size_t ttd_strnlen(const char *str, size_t maxlen) bool IsValidChar(char32_t key, CharSetFilter afilter); size_t Utf8Decode(char32_t *c, const char *s); +inline size_t Utf8Decode(char32_t *c, std::string::iterator &s) { return Utf8Decode(c, &*s); } size_t Utf8Encode(char *buf, char32_t c); size_t Utf8Encode(std::ostreambuf_iterator &buf, char32_t c); size_t Utf8Encode(std::back_insert_iterator &buf, char32_t c); +inline size_t Utf8Encode(std::string::iterator &s, char32_t c) { return Utf8Encode(&*s, c); } size_t Utf8TrimString(char *s, size_t maxlen); @@ -160,6 +162,13 @@ inline const char *Utf8PrevChar(const char *s) return ret; } +inline std::string::iterator Utf8PrevChar(std::string::iterator &s) +{ + auto *cur = &*s; + auto *prev = Utf8PrevChar(cur); + return s - (cur - prev); +} + size_t Utf8StringLength(const char *s); size_t Utf8StringLength(const std::string &str);