summaryrefslogtreecommitdiff
path: root/src/cairo-unicode.c
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@behdad.org>2008-08-07 20:45:54 -0400
committerBehdad Esfahbod <behdad@behdad.org>2008-08-08 03:01:16 -0400
commit6b3f6dc77abbc48d741b92cd62f93da68f00b9a2 (patch)
treebc78cff8eec137358f5fedb7b8310899bfd12025 /src/cairo-unicode.c
parent8c514a40b80e11cc904db3ce9ee353839321044e (diff)
downloadcairo-6b3f6dc77abbc48d741b92cd62f93da68f00b9a2.tar.gz
[unicode] Add _cairo_utf8_get_char_validated()
Diffstat (limited to 'src/cairo-unicode.c')
-rw-r--r--src/cairo-unicode.c42
1 files changed, 40 insertions, 2 deletions
diff --git a/src/cairo-unicode.c b/src/cairo-unicode.c
index 973ef5767..39d37b8e8 100644
--- a/src/cairo-unicode.c
+++ b/src/cairo-unicode.c
@@ -196,6 +196,40 @@ _utf8_get_char_extended (const unsigned char *p,
}
/**
+ * _cairo_utf8_get_char_validated:
+ * @p: a UTF-8 string
+ * @unicode: location to store one Unicode character
+ *
+ * Decodes the first character of a valid UTF-8 string, and returns
+ * the number of bytes consumed.
+ *
+ * Note that the string should be valid. Do not use this without
+ * validating the string first.
+ *
+ * Returns: the number of bytes forming the character returned.
+ **/
+int
+_cairo_utf8_get_char_validated (const char *p,
+ uint32_t *unicode)
+{
+ int i, mask = 0, len;
+ uint32_t result;
+ unsigned char c = (unsigned char) *p;
+
+ UTF8_COMPUTE (c, mask, len);
+ if (len == -1) {
+ if (unicode)
+ *unicode = (uint32_t)-1;
+ return 1;
+ }
+ UTF8_GET (result, p, i, mask, len);
+
+ if (unicode)
+ *unicode = result;
+ return len;
+}
+
+/**
* _cairo_utf8_to_utf32:
* @str: an UTF-8 string
* @len: length of @str in bytes, or -1 if it is nul-terminated.
@@ -266,7 +300,7 @@ _cairo_utf8_to_ucs4 (const char *str,
* _cairo_ucs4_to_utf8:
* @unicode: a UCS-4 character
* @utf8: buffer to write utf8 string into. Must have at least 4 bytes
- * space available.
+ * space available. Or %NULL.
*
* Return value: Number of bytes in the utf8 string or 0 if an invalid
* unicode character
@@ -279,7 +313,8 @@ _cairo_ucs4_to_utf8 (uint32_t unicode,
char *p;
if (unicode < 0x80) {
- *utf8 = unicode;
+ if (utf8)
+ *utf8 = unicode;
return 1;
} else if (unicode < 0x800) {
bytes = 2;
@@ -291,6 +326,9 @@ _cairo_ucs4_to_utf8 (uint32_t unicode,
return 0;
}
+ if (!utf8)
+ return bytes;
+
p = utf8 + bytes;
while (p > utf8) {
*--p = 0x80 | (unicode & 0x3f);