glib/utf8: Use SIMD for g_str_is_ascii()

This moves g_str_is_ascii() from gstrfuncs.c to gutf8.c so that we can
reuse the same SIMD code for ASCII validation.

On Apple Silicon:

 Before:  3297 MB/s
  After: 26146 MB/s
This commit is contained in:
Christian Hergert 2024-10-01 15:30:05 -07:00
parent 1d3d7336ed
commit b72650542c
2 changed files with 19 additions and 23 deletions

View File

@ -1604,29 +1604,6 @@ g_ascii_strup (const gchar *str,
return result;
}
/**
* g_str_is_ascii:
* @str: a string
*
* Determines if a string is pure ASCII. A string is pure ASCII if it
* contains no bytes with the high bit set.
*
* Returns: true if @str is ASCII
*
* Since: 2.40
*/
gboolean
g_str_is_ascii (const gchar *str)
{
gsize i;
for (i = 0; str[i]; i++)
if (str[i] & 0x80)
return FALSE;
return TRUE;
}
/**
* g_strdown:
* @string: the string to convert

View File

@ -1886,6 +1886,25 @@ g_utf8_validate_len (const char *str,
return max_len == 0;
}
/**
* g_str_is_ascii:
* @str: a string
*
* Determines if a string is pure ASCII. A string is pure ASCII if it
* contains no bytes with the high bit set.
*
* Returns: true if @str is ASCII
*
* Since: 2.40
*/
gboolean
g_str_is_ascii (const gchar *str)
{
utf8_verify_ascii (&str, NULL);
return *str == 0;
}
/**
* g_unichar_validate:
* @ch: a Unicode character