[gtk-server] performance and safety of Trim_String

  • From: Jim Bailey <sleepydgym@xxxxxxxxx>
  • To: gtk-server@xxxxxxxxxxxxx
  • Date: Wed, 1 Dec 2004 08:13:08 -0800 (PST)

Hello, I know gtk-server.c is really just concept code
(according to the author), but I would like to
contribute a new version of Trim_String, which is a
bit safer and a lot faster.

char *Trim_String(char *data)
    int last_idx;

    /* Get rid of whitespaces at the beginning of the
string */
    while (*data == ' '|| *data == '\t' || *data ==
'\n') data++;

    /* Check there is anything left to chop off at the
end */
    last_idx = strlen( data ) - 1;
    if ( last_idx < 0 ) return data;

    /* Find the last non-whitespace at the end of the
string */
    while ( data[last_idx] == ' ' || data[last_idx] ==
'\t' || data[last_idx] == '\n' ) last_idx--;

    /* Actually chop off the whitespaces at the end of
the string */
    data[last_idx+1] = '\0';

    /* Return stripped string */
    return data;

I was originally looking through the source to see how
escaping is handled, e.g. for setting the contents of
a textarea to something with brackets, commas, and
newlines. The newline doesn't seem to be possible, and
the scheme for brackets and commas just pushes the
problem onto other characters of your choice rather
than solving the issue.

Maybe it would be worth while to come up with another
syntax like substituting commas with ?, brackets with
?) newlines with ?n and ? with ?? (i am deliberately
avoiding \ as it is used too much by every other
language). Would any of the client languages have
trouble escaping/unescaping that? Of course the exact
escape character could be definable...

Not that I am too bothered, for the moment I find it
is easier to use gtk2 directly from clisp so don't
have a need for gtk-server, but there have been (and
will be) plenty of times when it would have extremely
useful to me. Great idea, please keep up the good



Do you Yahoo!? 
All your favorites on one personal page ? Try My Yahoo!

Other related posts: