Fix a corner-case in g_utf8_find_next_char

In the case that *p is '\0', we should return p + 1, not p.
This change allows to simplify g_utf8_find_next_char a bit.

https://bugzilla.gnome.org/show_bug.cgi?id=547200
This commit is contained in:
Matthias Clasen 2015-10-04 15:28:02 -04:00
parent 4215c0ce91
commit e0e652e403
2 changed files with 17 additions and 9 deletions

View File

@ -168,16 +168,18 @@ gchar *
g_utf8_find_next_char (const gchar *p,
const gchar *end)
{
if (*p)
if (end)
{
if (end)
for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
;
else
for (++p; (*p & 0xc0) == 0x80; ++p)
;
for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
;
return (p >= end) ? NULL : (gchar *)p;
}
else
{
for (++p; (*p & 0xc0) == 0x80; ++p)
;
return (gchar *)p;
}
return (p == end) ? NULL : (gchar *)p;
}
/**

View File

@ -97,7 +97,7 @@ test_find (void)
* U+0041 Latin Capital Letter A (\101)
* U+1EB6 Latin Capital Letter A With Breve And Dot Below (\341\272\266)
*/
const gchar *str = "\340\254\213\360\220\244\200\101\341\272\266";
const gchar *str = "\340\254\213\360\220\244\200\101\341\272\266\0\101";
const gchar *p = str + strlen (str);
const gchar *q;
@ -122,6 +122,12 @@ test_find (void)
g_assert (q == str + 3);
q = g_utf8_find_next_char (q, str + 6);
g_assert (q == NULL);
q = g_utf8_find_next_char (str, str);
g_assert (q == NULL);
q = g_utf8_find_next_char (str + strlen (str), NULL);
g_assert (q == str + strlen (str) + 1);
}
int main (int argc, char *argv[])