Re: g_str_is_ascii()



David Necas (Yeti) wrote:

On Thu, May 19, 2005 at 06:18:44PM -0400, Claudio Saavedra wrote:
 

I need to write a function to reject strings that are utf-8, i.e. accept
only convencional ascii strings.

I was wondering if there is any other way more nice than making a 

gboolean
str_is_ascii (const gchar *str)
{     
     return (str (mystring) == g_utf8_strlen (str, -1));
   

           I suppose this should read strlen(str)
 

}
   


Beside being incorrect -- result of g_utf8_strlen() on
a string that is not valid utf8 is not defined -- this is
quite a waste of resources.

   while (*str) {
      if ((guchar)*str >= 128)
          return FALSE;
   }
   return TRUE;

does the latter: accept only ASCII strings.  It does not do
the former (reject valid UTF-8), except when you know the
string is valid UTF-8 and only need to check whether it is
valid ASCII too, in this case the two checks conincide.

Aside from the fact that this gives you an infinite loop if the first
character is ASCII... might want to stick a 'str++' somewhere in there ^_~.

    -brian




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]