Re: glib strlen ?



On Tue, Mar 25, 2003 at 10:17:53AM -0500, Tristan Van Berkom wrote:
Mark Mielke wrote:
Actually, if he is speaking about a GString object, the following snippet
does what he wants:
    if (string != 0 && string->len != 0)
        /* String is defined and has >0 length... */
    else
        /* String is not defined, or has 0 length... */
strlen(mystr) should be part of string.h
strlen() is defficient in a number of ways. First, it cannot store '\0'.

KISS (Keep It Simple Stupid), 
      whats the point of initializing all kinds of
fancy structures and objects just to manipulate strings ?
string.h is something you can trust; it's _always_ worked.

Merely quoting "KISS" is not an argument by itself that a chosen
implementation is more or less simple. I find accusation that a person
doesn't follow "KISS" to be an insulting statement that really means
"If you don't do it the way I do, a way that I label simple, you must
be stupid."

You are assuming that strlen() is more simple. In fact, you are
incorrect, because you have not considered that strlen() is but one of
a large family of defficient functions including strcat(), strdup(),
etc.  which can all be easily and *simply* replaced with calls to
g_string_new(), g_string_append(), etc.

Oh, btw, how do you "KISS" g_string_prepend() with string.h?

Compare: (s1 prepending to s2)

    char *t  = malloc(strlen(s1) + strlen(s2) + 1);
    *t = '\0';
    strcat(t, s1);
    strcat(t, s2);
    free(s2);
    s2 = t;

To:

    g_string_prepend(s2, s1);

The reason GLIB exists is because a few people found libc.a to be lacking.
Now that it *does* exist, it would be foolish for a person to choose to
only use a small piece of GLIB for the only reason that they found the
implementation to be a little bit more complex. Users of GLIB don't have to
know how it is implemented, so for the most part, implementation complexity
doesn't matter. Are you aware that GCC+GLIBC (not GLIB) implements functions
like strlen() in very complex manners including conditional use of inlined
assembly?

Also, for strings that are more than a few Kb long, functions like strlen()
are *extremely* defficient not only in terms of run time, but because they
may trash numerous memory cache lines for no reason at all.

If the original poster is using GString, I challenge you to prove to me that:

    strlen(s->str)

Is better or 'simpler' than:

    s->len

"KISS" indeed.

mark

-- 
mark mielke cc/markm ncf ca/markm nortelnetworks com __________________________
.  .  _  ._  . .   .__    .  . ._. .__ .   . . .__  | Neighbourhood Coder
|\/| |_| |_| |/    |_     |\/|  |  |_  |   |/  |_   | 
|  | | | | \ | \   |__ .  |  | .|. |__ |__ | \ |__  | Ottawa, Ontario, Canada

  One ring to rule them all, one ring to find them, one ring to bring them all
                       and in the darkness bind them...

                           http://mark.mielke.cc/




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