gnucash/libgnucash/scm
Christopher Lam 7d15e6e4e7 [utilities] create general string-replace-substring
copied function created by Mark Weaver, core guile dev and augmented
to selectively replace substring indices

This is a much more efficient function than the previous
gnc:substring-replace which will constantly split lists using
substring, and create new strings using string-append.

It also does tail call optimization properly, unlike the previous
functions.

https://lists.gnu.org/archive/html/guile-devel/2013-09/msg00029.html -
original

"Here's an implementation that does this benchmark about 80 times
faster on my machine: (20 milliseconds vs 1.69 seconds)

--8<---------------cut here---------------start------------->8---
(define* (string-replace-substring s substr replacement
                                   #:optional
                                   (start 0)
                                   (end (string-length s)))
  (let ((substr-length (string-length substr)))
    (if (zero? substr-length)
        (error "string-replace-substring: empty substr")
        (let loop ((start start)
                   (pieces (list (substring s 0 start))))
          (let ((idx (string-contains s substr start end)))
            (if idx
                (loop (+ idx substr-length)
                      (cons* replacement
                             (substring s start idx)
                             pieces))
                (string-concatenate-reverse (cons (substring s start)
                                                  pieces))))))))
--8<---------------cut here---------------end--------------->8---

The reason this is so much faster is because it avoids needless
generation of intermediate strings."
2019-04-25 10:52:45 +08:00
..
test Create CMakeLists.txt in libgnucash/scm/test. 2019-04-25 10:51:20 +08:00
CMakeLists.txt Create CMakeLists.txt in libgnucash/scm/test. 2019-04-25 10:51:20 +08:00
price-quotes.scm I18N: Review of price-quotes.scm 2019-04-08 17:37:33 +02:00
utilities.scm [utilities] create general string-replace-substring 2019-04-25 10:52:45 +08:00