static bool g_utf8_validate(const char * str, int max_len, const char ** end) { const char * p; if(max_len < 0) p = fast_validate(str); else p = fast_validate_len(str, max_len); if(end) *end = p; if((max_len >= 0 && p != str + max_len) || (max_len < 0 && *p != '\0')) return false; else return true; }
/** * g_utf8_validate: * @str: a pointer to character data * @max_len: max bytes to validate, or -1 to go until NUL * @end: return location for end of valid data * * Validates UTF-8 encoded text. @str is the text to validate; * if @str is nul-terminated, then @max_len can be -1, otherwise * @max_len should be the number of bytes to validate. * If @end is non-%NULL, then the end of the valid range * will be stored there (i.e. the start of the first invalid * character if some bytes were invalid, or the end of the text * being validated otherwise). * * Note that g_utf8_validate() returns %FALSE if @max_len is * positive and NUL is met before @max_len bytes have been read. * * Returns %TRUE if all of @str was valid. Many GLib and GTK+ * routines <emphasis>require</emphasis> valid UTF-8 as input; * so data read from a file or the network should be checked * with g_utf8_validate() before doing anything else with it. * * Return value: %TRUE if the text was valid UTF-8 **/ gboolean g_utf8_validate (const char *str, gssize max_len, const gchar **end) { const gchar *p; if (max_len < 0) p = fast_validate (str); else p = fast_validate_len (str, max_len); if (end) *end = p; if ((max_len >= 0 && p != str + max_len) || (max_len < 0 && *p != '\0')) return FALSE; else return TRUE; }