From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Arne Babenhauserheide Newsgroups: gmane.lisp.guile.devel Subject: Re: efficient implementation of string-replace-substring / string-replace-all Date: Wed, 26 Mar 2014 21:14:51 +0100 Message-ID: <3463836.TjAz1UR6yo@fluss> References: <87y570pzbm.wl%arne_bab@web.de> <87a9cgsksy.fsf@pobox.com> <87mwggkwbz.fsf@yeeloong.lan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1395864907 11381 80.91.229.3 (26 Mar 2014 20:15:07 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 26 Mar 2014 20:15:07 +0000 (UTC) Cc: Andy Wingo , guile-devel@gnu.org To: Mark H Weaver Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Mar 26 21:15:14 2014 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WSuEE-00008K-48 for guile-devel@m.gmane.org; Wed, 26 Mar 2014 21:15:14 +0100 Original-Received: from localhost ([::1]:50080 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSuED-00005N-Ld for guile-devel@m.gmane.org; Wed, 26 Mar 2014 16:15:13 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56442) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSuE5-0008CV-P5 for guile-devel@gnu.org; Wed, 26 Mar 2014 16:15:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WSuDz-0004U2-Ct for guile-devel@gnu.org; Wed, 26 Mar 2014 16:15:05 -0400 Original-Received: from mout.web.de ([212.227.15.3]:49344) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSuDz-0004T4-3d for guile-devel@gnu.org; Wed, 26 Mar 2014 16:14:59 -0400 Original-Received: from fluss.localnet ([85.212.66.186]) by smtp.web.de (mrweb002) with ESMTPSA (Nemesis) id 0MX0q4-1WYQ0G48EG-00VxAJ; Wed, 26 Mar 2014 21:14:53 +0100 User-Agent: KMail/4.12.3 (Linux/3.10.7-gentoo-r1; KDE/4.12.3; x86_64; ; ) In-Reply-To: <87mwggkwbz.fsf@yeeloong.lan> X-Provags-ID: V03:K0:wms0K2aXdWBcSgvdYY+VaW9KI4vXKqACeNOdnR92r+u5SRubmp6 APw7yG7U+XJxhidbjqp6U9wB20+nJpBZP5P5dgP/4HqE+MEAON5Z1sOapZxpq6TivUlA7dB 6cDnjj/nfuKGJsG9i7JLIsZxf8t7Fb0sAIzv3DgFOz0EWdtWQJsCYhwgjKrZJcTwofxoOWI nmz4rDuYj+IYCOhjA6HZw== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 212.227.15.3 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:17029 Archived-At: Am Montag, 24. M=C3=A4rz 2014, 01:19:12 schrieb Mark H Weaver: > Andy Wingo writes: >=20 > > On Fri 13 Sep 2013 21:41, Mark H Weaver 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) > > =3D> (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 character= s > representable in the %default-port-encoding Then I=E2=80=99ll keep the original one for the time being. How are the chances of getting this function into regular Guile? If not= hing else, then the speedup of more than factor 80 over my naive implem= entation would merit providing the efficient one to all guile users, I = think. (Also I think that replacing a substring is a very common operat= ion when working with text - like serving websites) Best wishes, Arne --=20 Unpolitisch sein hei=C3=9Ft politisch sein,=20 ohne es zu merken.=20 - Arne (http://draketo.de)