[Top][All Lists]

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

Re: [PATCH] Add string-replace-substring to (ice-9 string-fun)

From: Arne Babenhauserheide
Subject: Re: [PATCH] Add string-replace-substring to (ice-9 string-fun)
Date: Sun, 05 Jan 2020 16:49:26 +0100
User-agent: mu4e 1.2.0; emacs 26.1

Christopher Lam <address@hidden> writes:

> This function has an interesting history.
> Someone or something in #guile had clued me there was a magic
> string-replace-substring somewhere in guile-user archives. With some luck I
> found it, and had bookmarked it, and kept it at the back of my mind for a
> good number of months.

To take the history further: I had initially started a very slow version
of string-replace-substring for wisp (because in the original wisp
parser which re-implemented something like read I needed
string-replacing all the time):

The original version looked like this (written on the Friday 13th in
September 2013):
define : string-replace-substring s substring replacement
       . "Replace every instance of substring in s by replacement."
       let : : sublen : string-length substring
           let replacer
               : newstring s
                 index : string-contains s substring
               if : not : equal? index #f
                  let : : replaced : string-replace s replacement index : + 
index sublen
                    replacer replaced : string-contains replaced substring
                  . newstring

;; ^
;; |
;; +---- horribly slow code! Do not copy!

It was afterwards improved with help by ijp to be 2x faster, and then by
Mark Weaver to be 80x faster (that’s not a typo: 80x faster!).

Half a year later Andy Wingo improved it further, replacing define* by
define, but I did not use it back then, because I needed full utf8 at
all times, regardless of the port encoding.

So the current version is at least 160x faster than my initial naive

Thank you very much for picking it up and getting it into Guile!

Best wishes,
Unpolitisch sein
heißt politisch sein
ohne es zu merken

reply via email to

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