From 767800fcb36be28c1191ff24ca3ba99c4df4a07f Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Tue, 20 Mar 2001 21:30:40 +0000 Subject: [PATCH] rewrite, based on bug #52328 from Anders 2001-03-20 Havoc Pennington * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from Anders --- ChangeLog | 5 +++++ ChangeLog.pre-2-0 | 5 +++++ ChangeLog.pre-2-10 | 5 +++++ ChangeLog.pre-2-12 | 5 +++++ ChangeLog.pre-2-2 | 5 +++++ ChangeLog.pre-2-4 | 5 +++++ ChangeLog.pre-2-6 | 5 +++++ ChangeLog.pre-2-8 | 5 +++++ glib/gutf8.c | 39 +++++++++++++++++++++++++++------------ gutf8.c | 39 +++++++++++++++++++++++++++------------ 10 files changed, 94 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5c40cd1..995f57a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 5c40cd1..995f57a 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +2001-03-20 Havoc Pennington + + * gutf8.c (g_utf8_strlen): rewrite, based on bug #52328 from + Anders + 2001-03-19 Havoc Pennington * gutf8.c (g_unichar_validate): added this function diff --git a/glib/gutf8.c b/glib/gutf8.c index 464ccab..74d053f 100644 --- a/glib/gutf8.c +++ b/glib/gutf8.c @@ -206,20 +206,35 @@ g_utf8_strlen (const gchar *p, gint max) { int len = 0; const gchar *start = p; - /* special case for the empty string */ - if (!*p) - return 0; - /* Note that the test here and the test in the loop differ subtly. - In the loop we want to see if we've passed the maximum limit -- - for instance if the buffer ends mid-character. Here at the top - of the loop we want to see if we've just reached the last byte. */ - while (max < 0 || p - start < max) + + if (max < 0) { - p = g_utf8_next_char (p); - ++len; - if (! *p || (max > 0 && p - start > max)) - break; + while (*p) + { + p = g_utf8_next_char (p); + ++len; + } + } + else + { + if (max == 0 || !*p) + return 0; + + p = g_utf8_next_char (p); + + while (p - start < max && *p) + { + ++len; + p = g_utf8_next_char (p); + } + + /* only do the last len increment if we got a complete + * char (don't count partial chars) + */ + if (p - start == max) + ++len; } + return len; } diff --git a/gutf8.c b/gutf8.c index 464ccab..74d053f 100644 --- a/gutf8.c +++ b/gutf8.c @@ -206,20 +206,35 @@ g_utf8_strlen (const gchar *p, gint max) { int len = 0; const gchar *start = p; - /* special case for the empty string */ - if (!*p) - return 0; - /* Note that the test here and the test in the loop differ subtly. - In the loop we want to see if we've passed the maximum limit -- - for instance if the buffer ends mid-character. Here at the top - of the loop we want to see if we've just reached the last byte. */ - while (max < 0 || p - start < max) + + if (max < 0) { - p = g_utf8_next_char (p); - ++len; - if (! *p || (max > 0 && p - start > max)) - break; + while (*p) + { + p = g_utf8_next_char (p); + ++len; + } + } + else + { + if (max == 0 || !*p) + return 0; + + p = g_utf8_next_char (p); + + while (p - start < max && *p) + { + ++len; + p = g_utf8_next_char (p); + } + + /* only do the last len increment if we got a complete + * char (don't count partial chars) + */ + if (p - start == max) + ++len; } + return len; } -- 2.7.4