summaryrefslogtreecommitdiff
path: root/pango/pango-layout.c
diff options
context:
space:
mode:
authorOwen Taylor <otaylor@redhat.com>2001-08-22 23:53:13 +0000
committerOwen Taylor <otaylor@src.gnome.org>2001-08-22 23:53:13 +0000
commit9a2e1914e525e56b7fcb25699e19c9667f2c5491 (patch)
tree0f851982f6f9642944181a8317ee5d9a203d8c2d /pango/pango-layout.c
parent057c4d839c8f979700df14eaa73e1c4d2a55f0d0 (diff)
downloadpango-9a2e1914e525e56b7fcb25699e19c9667f2c5491.tar.gz
Simply use g_utf8_validate() to validate the text, avoiding problem where
Wed Aug 22 19:52:18 2001 Owen Taylor <otaylor@redhat.com> * pango/pango-layout.c (pango_layout_set_text): Simply use g_utf8_validate() to validate the text, avoiding problem where layout was left in hosed state for invalid UTF-8, and also a bug with reading one-past length.
Diffstat (limited to 'pango/pango-layout.c')
-rw-r--r--pango/pango-layout.c51
1 files changed, 13 insertions, 38 deletions
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index 093da577..46436e11 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -720,51 +720,26 @@ pango_layout_set_text (PangoLayout *layout,
const char *text,
int length)
{
+ const gchar *end;
+
g_return_if_fail (layout != NULL);
g_return_if_fail (length == 0 || text != NULL);
+
+ if (!g_utf8_validate (text, length, &end))
+ g_warning ("Invalid UTF8 string passed to pango_layout_set_text()");
+ length = end - text;
+
if (layout->text)
g_free (layout->text);
- if (length == 0)
- {
- layout->text = g_strdup ("");
- layout->n_chars = 0;
- }
- else
- {
- const char *p = text;
- int n_chars = 0;
-
- while (*p && (length < 0 || p < text + length))
- {
- if (g_utf8_get_char (p) == (gunichar)-1)
- {
- g_warning ("Invalid UTF8 string passed to pango_layout_set_text()");
- return;
- }
- p = g_utf8_next_char (p);
- n_chars++;
- }
-
- if (length < 0)
- length = p - text;
-
- if (length >= 0 && p != text + length)
- g_warning ("string passed to pango_layout_set_text() contains embedded NULL");
-
- length = p - text;
-
- /* NULL-terminate the text for convenience.
- */
-
- layout->text = g_malloc (length + 1);
- memcpy (layout->text, text, length);
- layout->text[length] = '\0';
+ /* NULL-terminate the text for convenience.
+ */
+ layout->text = g_malloc (length + 1);
+ memcpy (layout->text, text, length);
+ layout->text[length] = '\0';
- layout->n_chars = n_chars;
- }
-
+ layout->n_chars = g_utf8_strlen (layout->text, -1);
layout->length = length;
pango_layout_clear_lines (layout);