unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: Mark H Weaver <mhw@netris.org>
Cc: arne_bab@web.de, guile-devel@gnu.org
Subject: Re: efficient implementation of string-replace-substring / string-replace-all
Date: Sun, 23 Mar 2014 21:48:45 +0100	[thread overview]
Message-ID: <87a9cgsksy.fsf@pobox.com> (raw)
In-Reply-To: <87wqmkjyqr.fsf@tines.lan> (Mark H. Weaver's message of "Fri, 13 Sep 2013 15:41:16 -0400")

On Fri 13 Sep 2013 21:41, Mark H Weaver <mhw@netris.org> writes:

> Here's an implementation that does this benchmark about 80 times faster
> on my machine: (20 milliseconds vs 1.69 seconds)
>
> (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))))))))

Inspired to code-golf a bit, here's one that's even faster :)

(define (string-replace-substring s substring replacement)
  "Replace every instance of substring in s by replacement."
  (let ((sublen (string-length substring)))
    (with-output-to-string
      (lambda ()
        (let lp ((start 0))
          (cond
           ((string-contains s substring start)
            => (lambda (end)
                 (display (substring/shared s start end))
                 (display replacement)
                 (lp (+ end sublen))))
           (else
            (display (substring/shared s start)))))))))

Just marginally so, though.

Andy
-- 
http://wingolog.org/



  reply	other threads:[~2014-03-23 20:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-13 14:32 efficient implementation of string-replace-substring / string-replace-all Arne Babenhauserheide
2013-09-13 19:41 ` Mark H Weaver
2014-03-23 20:48   ` Andy Wingo [this message]
2014-03-24  5:19     ` Mark H Weaver
2014-03-26 20:14       ` Arne Babenhauserheide

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a9cgsksy.fsf@pobox.com \
    --to=wingo@pobox.com \
    --cc=arne_bab@web.de \
    --cc=guile-devel@gnu.org \
    --cc=mhw@netris.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).