unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Arne Babenhauserheide <arne_bab@web.de>
To: Mark H Weaver <mhw@netris.org>
Cc: Andy Wingo <wingo@pobox.com>, guile-devel@gnu.org
Subject: Re: efficient implementation of string-replace-substring / string-replace-all
Date: Wed, 26 Mar 2014 21:14:51 +0100	[thread overview]
Message-ID: <3463836.TjAz1UR6yo@fluss> (raw)
In-Reply-To: <87mwggkwbz.fsf@yeeloong.lan>

Am Montag, 24. März 2014, 01:19:12 schrieb Mark H Weaver:
> Andy Wingo <wingo@pobox.com> writes:
> 
> > On Fri 13 Sep 2013 21:41, Mark H Weaver <mhw@netris.org> writes:
> >
> > 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.

Nice!

> One warning though: in Guile 2.0, string ports only support characters
> representable in the %default-port-encoding

Then I’ll keep the original one for the time being.

How are the chances of getting this function into regular Guile? If nothing else, then the speedup of more than factor 80 over my naive implementation would merit providing the efficient one to all guile users, I think. (Also I think that replacing a substring is a very common operation when working with text - like serving websites)

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein, 
ohne es zu merken. 
- Arne (http://draketo.de)






      reply	other threads:[~2014-03-26 20:14 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
2014-03-24  5:19     ` Mark H Weaver
2014-03-26 20:14       ` Arne Babenhauserheide [this message]

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=3463836.TjAz1UR6yo@fluss \
    --to=arne_bab@web.de \
    --cc=guile-devel@gnu.org \
    --cc=mhw@netris.org \
    --cc=wingo@pobox.com \
    /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).