mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 11:26:16 +01:00
gutf8: Add a g_utf8_validate_len() function
This is a variant of g_utf8_validate() which requires the length to be specified, thereby allowing string lengths up to G_MAXSIZE rather than just G_MAXSSIZE. Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
parent
8e60b3dde0
commit
7a4025cac1
@ -2964,6 +2964,7 @@ g_utf8_strrchr
|
|||||||
g_utf8_strreverse
|
g_utf8_strreverse
|
||||||
g_utf8_substring
|
g_utf8_substring
|
||||||
g_utf8_validate
|
g_utf8_validate
|
||||||
|
g_utf8_validate_len
|
||||||
g_utf8_make_valid
|
g_utf8_make_valid
|
||||||
|
|
||||||
<SUBSECTION>
|
<SUBSECTION>
|
||||||
|
@ -847,6 +847,10 @@ GLIB_AVAILABLE_IN_ALL
|
|||||||
gboolean g_utf8_validate (const gchar *str,
|
gboolean g_utf8_validate (const gchar *str,
|
||||||
gssize max_len,
|
gssize max_len,
|
||||||
const gchar **end);
|
const gchar **end);
|
||||||
|
GLIB_AVAILABLE_IN_2_60
|
||||||
|
gboolean g_utf8_validate_len (const gchar *str,
|
||||||
|
gsize max_len,
|
||||||
|
const gchar **end);
|
||||||
|
|
||||||
GLIB_AVAILABLE_IN_ALL
|
GLIB_AVAILABLE_IN_ALL
|
||||||
gchar *g_utf8_strup (const gchar *str,
|
gchar *g_utf8_strup (const gchar *str,
|
||||||
|
44
glib/gutf8.c
44
glib/gutf8.c
@ -1669,16 +1669,48 @@ g_utf8_validate (const char *str,
|
|||||||
{
|
{
|
||||||
const gchar *p;
|
const gchar *p;
|
||||||
|
|
||||||
if (max_len < 0)
|
if (max_len >= 0)
|
||||||
p = fast_validate (str);
|
return g_utf8_validate_len (str, max_len, end);
|
||||||
else
|
|
||||||
p = fast_validate_len (str, max_len);
|
p = fast_validate (str);
|
||||||
|
|
||||||
if (end)
|
if (end)
|
||||||
*end = p;
|
*end = p;
|
||||||
|
|
||||||
if ((max_len >= 0 && p != str + max_len) ||
|
if (*p != '\0')
|
||||||
(max_len < 0 && *p != '\0'))
|
return FALSE;
|
||||||
|
else
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_utf8_validate_len:
|
||||||
|
* @str: (array length=max_len) (element-type guint8): a pointer to character data
|
||||||
|
* @max_len: max bytes to validate
|
||||||
|
* @end: (out) (optional) (transfer none): return location for end of valid data
|
||||||
|
*
|
||||||
|
* Validates UTF-8 encoded text.
|
||||||
|
*
|
||||||
|
* As with g_utf8_validate(), but @max_len must be set, and hence this function
|
||||||
|
* will always return %FALSE if any of the bytes of @str are nul.
|
||||||
|
*
|
||||||
|
* Returns: %TRUE if the text was valid UTF-8
|
||||||
|
* Since: 2.60
|
||||||
|
*/
|
||||||
|
gboolean
|
||||||
|
g_utf8_validate_len (const char *str,
|
||||||
|
gsize max_len,
|
||||||
|
const gchar **end)
|
||||||
|
|
||||||
|
{
|
||||||
|
const gchar *p;
|
||||||
|
|
||||||
|
p = fast_validate_len (str, max_len);
|
||||||
|
|
||||||
|
if (end)
|
||||||
|
*end = p;
|
||||||
|
|
||||||
|
if (p != str + max_len)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
else
|
else
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -280,15 +280,22 @@ do_test (gconstpointer d)
|
|||||||
|
|
||||||
result = g_utf8_validate (test->text, test->max_len, &end);
|
result = g_utf8_validate (test->text, test->max_len, &end);
|
||||||
|
|
||||||
g_assert (result == test->valid);
|
g_assert_true (result == test->valid);
|
||||||
g_assert (end - test->text == test->offset);
|
g_assert_cmpint (end - test->text, ==, test->offset);
|
||||||
|
|
||||||
if (test->max_len < 0)
|
if (test->max_len < 0)
|
||||||
{
|
{
|
||||||
result = g_utf8_validate (test->text, strlen (test->text), &end);
|
result = g_utf8_validate (test->text, strlen (test->text), &end);
|
||||||
|
|
||||||
g_assert (result == test->valid);
|
g_assert_true (result == test->valid);
|
||||||
g_assert (end - test->text == test->offset);
|
g_assert_cmpint (end - test->text, ==, test->offset);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = g_utf8_validate_len (test->text, test->max_len, &end);
|
||||||
|
|
||||||
|
g_assert_true (result == test->valid);
|
||||||
|
g_assert_cmpint (end - test->text, ==, test->offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user