[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: efficient implementation of string-replace-substring / string-replac
From: |
Arne Babenhauserheide |
Subject: |
Re: efficient implementation of string-replace-substring / string-replace-all |
Date: |
Wed, 26 Mar 2014 21:14:51 +0100 |
User-agent: |
KMail/4.12.3 (Linux/3.10.7-gentoo-r1; KDE/4.12.3; x86_64; ; ) |
Am Montag, 24. März 2014, 01:19:12 schrieb Mark H Weaver:
> Andy Wingo <address@hidden> writes:
>
> > On Fri 13 Sep 2013 21:41, Mark H Weaver <address@hidden> 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)