summaryrefslogtreecommitdiff
path: root/src/string.cpp
diff options
context:
space:
mode:
authorDarkvater <darkvater@openttd.org>2007-03-05 00:45:56 +0000
committerDarkvater <darkvater@openttd.org>2007-03-05 00:45:56 +0000
commit915ae8ffc28aee2e0bb9d9c623a711b4fc1f7faa (patch)
tree347173f94845f2ed93206df1d8a324c41c93e2bd /src/string.cpp
parentaea64adbb9ff755be303515bebb428d92e7bcc92 (diff)
downloadopenttd-915ae8ffc28aee2e0bb9d9c623a711b4fc1f7faa.tar.xz
(svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
-Codechange: Add a function Utf8TrimString() that properly trims a string to an UTF8 encoding seperation instead of somewhere in the wild (and use it in the chat area)
Diffstat (limited to 'src/string.cpp')
-rw-r--r--src/string.cpp26
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