Re: g_strsep implementation




Manish Singh <yosh@gimp.org> writes:

> [1  <text/plain; us-ascii (7bit)>]
> glib has a bunch of nice string functions, but one glaring omission is
> strsep. So here's a patch to implement g_strsep.
> 
> I think it's small enough to go in for 1.2, and it rounds out glib's
> string portability functions nicely.

Hmmmm,

Assumming that we want a reentrant strtok (strtok itself
is pretty portable), wouldn't it be better to implement
g_strtok_r(), rather than g_strsep, since strtok_r
is the POSIX and Unix98 standard?

Actually, if strsplit split on a set of delimeters 
rather than a substring, I'd tend to tell people to
just use that. Yes, it is less memory efficient, 
but that might be better than API bloat and conceptual
bloat.

Only a few comments about implementation:
 
> +  if (!(p =* string))
> +    return NULL;

p = *string;
if (!p)
  return NULL;

> +  
> +  if (delim[0] == '\0' || delim[1] == '\0')
> +    {
> +      gchar ch = delim[0];
> +      
> +      if (ch != '\0')
> +        q = (*p == ch) ? p : strchr (p + 1, ch);
> +      else
> +	q = NULL;
> +    }
> +  else
> +    q = strpbrk (p, delim);

I'd tend to let strpbrk do this kind of optimization.
The only common time we save the function call is if
*p = ch.

Regards,
                                        Owen



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