guile-devel
[Top][All Lists]
Advanced

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

string-delete consing


From: Kevin Ryde
Subject: string-delete consing
Date: Sun, 05 Jun 2005 11:37:11 +1000
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.3 (gnu/linux)

string-delete looks like it throws off a fair bit of garbage, a cons
cell for every character in the output string.

I'm thinking of the following for the "char" and "charset" deleting
cases.  Count what will be kept then copy to a destination string.

I'm not quite sure about the scm_i_string_chars though.  The idea is
to call that again after anything that might gc, is it?


    if (SCM_CHARP (char_pred))
      {
        char chr, *dst;
        size_t count;

        chr = SCM_CHAR (char_pred);

        /* count how many chars to keep */
        count = 0;
        for (idx = cstart; idx < cend; idx++)
          if (cstr[idx] != chr)
            count++;

        /* new string for them (and cstr again in case gc moves it) */
        newstr = scm_i_make_string (count, &dst);
        cstr = scm_i_string_chars (s);

        /* decrement "count" in this loop as well as using idx, so that if
           another thread is simultaneously changing the input str we
           won't go past the end of newstr */
        for (idx = cstart; idx < cend && count != 0; idx++, count--)
          {
            if (cstr[idx] != chr)
              *dst++ = cstr[idx];
          }
        result = newstr;
      }

For the "procedure" case, I'm thinking of mallocing a block to keep
the characters the procedure says to keep.  That block would start out
as the size of the input, then be realloced down when the true size is
known.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]