diff options
Diffstat (limited to 'src/string.cpp')
-rw-r--r-- | src/string.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/string.cpp b/src/string.cpp index d55d9a70e..d5c499d0a 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -268,3 +268,29 @@ size_t Utf8Encode(char *buf, WChar c) *buf = '?'; return 1; } + +/** + * Properly terminate an UTF8 string to some maximum length + * @param s string to check if it needs additional trimming + * @param maxlen the maximum length the buffer can have. + * @return the new length in bytes of the string (eg. strlen(new_string)) + * @NOTE maxlen is the string length _INCLUDING_ the terminating '\0' + */ +size_t Utf8TrimString(char *s, size_t maxlen) +{ + size_t length = 0; + + for (const char *ptr = strchr(s, '\0'); *s != '\0';) { + size_t len = Utf8EncodedCharLen(*s); + if (len == 0) break; // invalid encoding + + /* Take care when a hard cutoff was made for the string and + * the last UTF8 sequence is invalid */ + if (length + len >= maxlen || (s + len > ptr)) break; + s += len; + length += len; + } + + *s = '\0'; + return length; +}
\ No newline at end of file |