summaryrefslogtreecommitdiff
path: root/src/string_func.h
diff options
context:
space:
mode:
authormichi_cc <michi_cc@openttd.org>2013-08-05 20:35:31 +0000
committermichi_cc <michi_cc@openttd.org>2013-08-05 20:35:31 +0000
commit76367f6bf1b5b459c9a15faa0cc0ea1dab191c6f (patch)
treeda45ccb0547732c9ff21f670971d5e7979c38f8c /src/string_func.h
parente7dc14b25af4b2802a956dd1cd99c187fb4acb56 (diff)
downloadopenttd-76367f6bf1b5b459c9a15faa0cc0ea1dab191c6f.tar.xz
(svn r25653) -Add: Caret movement by words for CJK languages.
Diffstat (limited to 'src/string_func.h')
-rw-r--r--src/string_func.h46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/string_func.h b/src/string_func.h
index b0a42b808..d7056f1be 100644
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -90,7 +90,6 @@ static inline WChar Utf8Consume(const char **s)
return c;
}
-
/**
* Return the length of a UTF-8 encoded character.
* @param c Unicode character.
@@ -157,6 +156,51 @@ static inline const char *Utf8PrevChar(const char *s)
size_t Utf8StringLength(const char *s);
/**
+ * Is the given character a lead surrogate code point?
+ * @param c The character to test.
+ * @return True if the character is a lead surrogate code point.
+ */
+static inline bool Utf16IsLeadSurrogate(uint c)
+{
+ return c >= 0xD800 && c <= 0xDBFF;
+}
+
+/**
+ * Is the given character a lead surrogate code point?
+ * @param c The character to test.
+ * @return True if the character is a lead surrogate code point.
+ */
+static inline bool Utf16IsTrailSurrogate(uint c)
+{
+ return c >= 0xDC00 && c <= 0xDFFF;
+}
+
+/**
+ * Convert an UTF-16 surrogate pair to the corresponding Unicode character.
+ * @param lead Lead surrogate code point.
+ * @param trail Trail surrogate code point.
+ * @return Decoded Unicode character.
+ */
+static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
+{
+ return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
+}
+
+/**
+ * Decode an UTF-16 character.
+ * @param c Pointer to one or two UTF-16 code points.
+ * @return Decoded Unicode character.
+ */
+static inline WChar Utf16DecodeChar(const uint16 *c)
+{
+ if (Utf16IsLeadSurrogate(c[0])) {
+ return Utf16DecodeSurrogate(c[0], c[1]);
+ } else {
+ return *c;
+ }
+}
+
+/**
* Is the given character a text direction character.
* @param c The character to test.
* @return true iff the character is used to influence