From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#43598: replace-in-string: finishing touches Date: Fri, 25 Sep 2020 12:42:06 +0200 Message-ID: <2CFAAACA-2FD3-44C2-B12E-E49DAA968115@acm.org> References: <77CD28C7-77C0-4DED-ACD0-21418489BADE@acm.org> <87blhuu3w9.fsf@gnus.org> Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.15\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_EB777C97-CDF8-48EF-A5E7-78C47B853B0E" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19123"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 43598@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Sep 25 12:43:11 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLlC2-0004t7-Jp for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 25 Sep 2020 12:43:10 +0200 Original-Received: from localhost ([::1]:38544 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLlC1-0007S6-LQ for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 25 Sep 2020 06:43:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48390) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLlBu-0007Qj-7C for bug-gnu-emacs@gnu.org; Fri, 25 Sep 2020 06:43:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59476) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kLlBt-0002NB-UM for bug-gnu-emacs@gnu.org; Fri, 25 Sep 2020 06:43:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kLlBt-0006fy-RD for bug-gnu-emacs@gnu.org; Fri, 25 Sep 2020 06:43:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 25 Sep 2020 10:43:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 43598 X-GNU-PR-Package: emacs Original-Received: via spool by 43598-submit@debbugs.gnu.org id=B43598.160103054925601 (code B ref 43598); Fri, 25 Sep 2020 10:43:01 +0000 Original-Received: (at 43598) by debbugs.gnu.org; 25 Sep 2020 10:42:29 +0000 Original-Received: from localhost ([127.0.0.1]:42789 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kLlBN-0006eq-1M for submit@debbugs.gnu.org; Fri, 25 Sep 2020 06:42:29 -0400 Original-Received: from mail1458c50.megamailservers.eu ([91.136.14.58]:59296 helo=mail267c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kLlBH-0006eW-KG for 43598@debbugs.gnu.org; Fri, 25 Sep 2020 06:42:27 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1601030536; bh=OoIWLt5kJkXQ6llnn3obcxfpnWqJMQpn+PsRW0qS2JA=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=W5DcUa5Pc0R/kdpuDS6MSkSSXr4v4zaXbP1RHmio27tt6WLAoHDe5FbRR5+EsTFFx GIGnHQtxHuMPohIyIwP6OaAnzzTwh83rEXQnw3FS/BYqvW/DtBZwWWA7yWahsmagig vtSSaCZ2XECqnmr4CamMAgzF+Ckz4+27RTPJyeAc= Feedback-ID: mattiase@acm.or Original-Received: from stanniol.lan (c-304ee655.032-75-73746f71.bbcust.telenor.se [85.230.78.48]) (authenticated bits=0) by mail267c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 08PAg7Ap023190; Fri, 25 Sep 2020 10:42:08 +0000 In-Reply-To: <87blhuu3w9.fsf@gnus.org> X-Mailer: Apple Mail (2.3445.104.15) X-CTCH-RefID: str=0001.0A782F26.5F6DC988.0044, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=Cf92G4jl c=1 sm=1 tr=0 a=63Z2wlQ1NB3xHpgKFKE71g==:117 a=63Z2wlQ1NB3xHpgKFKE71g==:17 a=M51BFTxLslgA:10 a=OocQHUDgAAAA:8 a=58POXaC9i2PCeO8P82MA:9 a=QEXdDO2ut3YA:10 a=AVAWrLMCqMzcJ98pz_gA:9 a=De_Ol2h6w80A:10 a=xUZTl98r3Qw_uB5NK3jt:22 X-Origin-Country: SE X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:188945 Archived-At: --Apple-Mail=_EB777C97-CDF8-48EF-A5E7-78C47B853B0E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 25 sep. 2020 kl. 01.54 skrev Lars Ingebrigtsen : > I went ahead and checked in a new C-level function string-search, = which > should be an efficient way to search for strings in strings (using > memmem, which Emacs has via Gnulib?), and this fixed these corner = cases. Thank you! Here are some proposed tweaks (diff attached): 1. Check the range of the START-POS argument so that we don't crash. The permitted range is [0..N] where N is (length HAYSTACK), thus we = permit a start right after the last character but no further. We could also return nil in these cases but I think an error is more = useful. 2. Make the docs more precise about various things. 3. Slight simplification of the implementation logic to avoid testing = the same conditions multiple times. 4. More tests, especially for edge cases. Can't have too many! One test still fails: (string-search "=C3=B8" "\303\270") which should return nil but currently matches. I think it's wrong to convert the needle to unibyte (using = Fstring_as_unibyte) in this case, but I haven't decided what the best = solution would be. We should also consider the optimisations: - If SCHARS(needle)>SCHARS(haystack) then no match is possible. - If either needle or haystack is all-ASCII (all bytes in 0..127), then = we can use memmem without conversion. --Apple-Mail=_EB777C97-CDF8-48EF-A5E7-78C47B853B0E Content-Disposition: attachment; filename=string-search.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="string-search.diff" Content-Transfer-Encoding: quoted-printable diff=20--git=20a/doc/lispref/strings.texi=20b/doc/lispref/strings.texi=0A= index=206eb3d6f310..0f157c39d6=20100644=0A---=20= a/doc/lispref/strings.texi=0A+++=20b/doc/lispref/strings.texi=0A@@=20= -660,8=20+660,10=20@@=20Text=20Comparison=0A=20Return=20the=20position=20= of=20the=20first=20instance=20of=20@var{needle}=20in=0A=20= @var{haystack},=20both=20of=20which=20are=20strings.=20=20If=20= @var{start-pos}=20is=0A=20non-@code{nil},=20start=20searching=20from=20= that=20position=20in=20@var{needle}.=0A+Return=20@code{nil}=20if=20no=20= match=20was=20found.=0A=20This=20function=20only=20considers=20the=20= characters=20in=20the=20strings=20when=20doing=0A-the=20comparison;=20= text=20properties=20are=20ignored.=0A+the=20comparison;=20text=20= properties=20are=20ignored.=20=20Matching=20is=20always=0A= +case-sensitive.=0A=20@end=20defun=0A=20=0A=20@defun=20compare-strings=20= string1=20start1=20end1=20string2=20start2=20end2=20&optional=20= ignore-case=0Adiff=20--git=20a/src/fns.c=20b/src/fns.c=0Aindex=20= 3927e4306e..2f64d95576=20100644=0A---=20a/src/fns.c=0A+++=20b/src/fns.c=0A= @@=20-5456,15=20+5456,18=20@@=20DEFUN=20("buffer-hash",=20Fbuffer_hash,=20= Sbuffer_hash,=200,=201,=200,=0A=20=0A=20DEFUN=20("string-search",=20= Fstring_search,=20Sstring_search,=202,=203,=200,=0A=20=20=20=20=20=20=20=20= doc:=20/*=20Search=20for=20the=20string=20NEEDLE=20in=20the=20string=20= HAYSTACK.=0A-The=20return=20value=20is=20the=20position=20of=20the=20= first=20instance=20of=20NEEDLE=20in=0A-HAYSTACK.=0A+The=20return=20value=20= is=20the=20position=20of=20the=20first=20occurrence=20of=20NEEDLE=20in=0A= +HAYSTACK,=20or=20nil=20if=20no=20match=20was=20found.=0A=20=0A=20The=20= optional=20START-POS=20argument=20says=20where=20to=20start=20searching=20= in=0A-HAYSTACK.=20=20If=20not=20given,=20start=20at=20the=20beginning.=20= */)=0A+HAYSTACK=20and=20defaults=20to=20zero=20(start=20at=20the=20= beginning).=0A+It=20must=20be=20between=20zero=20and=20the=20length=20of=20= HAYSTACK,=20inclusive.=0A+=0A+Case=20is=20always=20significant=20and=20= text=20properties=20are=20ignored.=20*/)=0A=20=20=20(register=20= Lisp_Object=20needle,=20Lisp_Object=20haystack,=20Lisp_Object=20= start_pos)=0A=20{=0A=20=20=20ptrdiff_t=20start_byte=20=3D=200,=20= haybytes;=0A-=20=20char=20*res=20=3D=20NULL,=20*haystart;=0A+=20=20char=20= *res,=20*haystart;=0A=20=0A=20=20=20CHECK_STRING=20(needle);=0A=20=20=20= CHECK_STRING=20(haystack);=0A@@=20-5472,7=20+5475,10=20@@=20DEFUN=20= ("string-search",=20Fstring_search,=20Sstring_search,=202,=203,=200,=0A=20= =20=20if=20(!NILP=20(start_pos))=0A=20=20=20=20=20{=0A=20=20=20=20=20=20=20= CHECK_FIXNUM=20(start_pos);=0A-=20=20=20=20=20=20start_byte=20=3D=20= string_char_to_byte=20(haystack,=20XFIXNUM=20(start_pos));=0A+=20=20=20=20= =20=20EMACS_INT=20start=20=3D=20XFIXNUM=20(start_pos);=0A+=20=20=20=20=20= =20if=20(start=20<=200=20||=20start=20>=20SCHARS=20(haystack))=0A+=20=20=20= =20=20=20=20=20xsignal1=20(Qargs_out_of_range,=20start_pos);=0A+=20=20=20= =20=20=20start_byte=20=3D=20string_char_to_byte=20(haystack,=20start);=0A= =20=20=20=20=20}=0A=20=0A=20=20=20haystart=20=3D=20SSDATA=20(haystack)=20= +=20start_byte;=0A@@=20-5481,13=20+5487,13=20@@=20DEFUN=20= ("string-search",=20Fstring_search,=20Sstring_search,=202,=203,=200,=0A=20= =20=20if=20(STRING_MULTIBYTE=20(haystack)=20=3D=3D=20STRING_MULTIBYTE=20= (needle))=0A=20=20=20=20=20res=20=3D=20memmem=20(haystart,=20haybytes,=0A= =20=09=09=20=20SSDATA=20(needle),=20SBYTES=20(needle));=0A-=20=20else=20= if=20(STRING_MULTIBYTE=20(haystack)=20&&=20!STRING_MULTIBYTE=20(needle))=0A= +=20=20else=20if=20(STRING_MULTIBYTE=20(haystack))=20=20/*=20unibyte=20= needle=20*/=0A=20=20=20=20=20{=0A=20=20=20=20=20=20=20Lisp_Object=20= multi_needle=20=3D=20string_to_multibyte=20(needle);=0A=20=20=20=20=20=20= =20res=20=3D=20memmem=20(haystart,=20haybytes,=0A=20=09=09=20=20=20=20= SSDATA=20(multi_needle),=20SBYTES=20(multi_needle));=0A=20=20=20=20=20}=0A= -=20=20else=20if=20(!STRING_MULTIBYTE=20(haystack)=20&&=20= STRING_MULTIBYTE=20(needle))=0A+=20=20else=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20/*=20unibyte=20haystack,=20= multibyte=20needle=20*/=0A=20=20=20=20=20{=0A=20=20=20=20=20=20=20= Lisp_Object=20uni_needle=20=3D=20Fstring_as_unibyte=20(needle);=0A=20=20=20= =20=20=20=20res=20=3D=20memmem=20(haystart,=20haybytes,=0Adiff=20--git=20= a/test/src/fns-tests.el=20b/test/src/fns-tests.el=0Aindex=20= 8c2b1300dc..323743d842=20100644=0A---=20a/test/src/fns-tests.el=0A+++=20= b/test/src/fns-tests.el=0A@@=20-907,6=20+907,12=20@@=20string-search=0A=20= =20=20(should=20(equal=20(string-search=20"foo"=20"foobarzot")=200))=0A=20= =20=20(should=20(not=20(string-search=20"fooz"=20"foobarzot")))=0A=20=20=20= (should=20(not=20(string-search=20"zot"=20"foobarzo")))=0A+=20=20(should=20= (equal=20(string-search=20"ab"=20"ab")=200))=0A+=20=20(should=20(equal=20= (string-search=20"ab\0"=20"ab")=20nil))=0A+=20=20(should=20(equal=20= (string-search=20"ab"=20"abababab"=203)=204))=0A+=20=20(should=20(equal=20= (string-search=20"ab"=20"ababac"=203)=20nil))=0A+=20=20(let=20= ((case-fold-search=20t))=0A+=20=20=20=20(should=20(equal=20= (string-search=20"ab"=20"AB")=20nil)))=0A=20=0A=20=20=20(should=20(equal=0A= =20=20=20=20=20=20=20=20=20=20=20=20(string-search=20(make-string=202=20= 130)=0A@@=20-923,4=20+929,26=20@@=20string-search=0A=20=20=20(should=20= (not=20(string-search=20(make-string=201=20255)=20"a\377=C3=B8")))=0A=20=20= =20(should=20(not=20(string-search=20(make-string=201=20255)=20= "a\377a")))=0A=20=0A-=20=20(should=20(equal=20(string-search=20"f=C3=B3o"=20= "zotf=C3=B3o")=203)))=0A+=20=20(should=20(equal=20(string-search=20= "f=C3=B3o"=20"zotf=C3=B3o")=203))=0A+=0A+=20=20(should=20(equal=20= (string-search=20(string-to-multibyte=20"\377")=20"ab\377c")=202))=0A+=20= =20(should=20(equal=20(string-search=20"\303"=20"a=C3=B8b")=20nil))=0A+=20= =20(should=20(equal=20(string-search=20"\270"=20"a=C3=B8b")=20nil))=0A+=20= =20;;=20This=20test=20currently=20fails,=20but=20it=20shouldn't!=0A+=20=20= ;;(should=20(equal=20(string-search=20"=C3=B8"=20"\303\270")=20nil))=0A+=0A= +=20=20(should-error=20(string-search=20"a"=20"abc"=20-1))=0A+=20=20= (should-error=20(string-search=20"a"=20"abc"=204))=0A+=20=20= (should-error=20(string-search=20"a"=20"abc"=20100000000000))=0A+=0A+=20=20= (should=20(equal=20(string-search=20"a"=20"aaa"=203)=20nil))=0A+=20=20= (should=20(equal=20(string-search=20"\0"=20"")=20nil))=0A+=0A+=20=20= (should=20(equal=20(string-search=20""=20"")=200))=0A+=20=20= (should-error=20(string-search=20""=20""=201))=0A+=20=20(should=20(equal=20= (string-search=20""=20"abc")=200))=0A+=20=20(should=20(equal=20= (string-search=20""=20"abc"=202)=202))=0A+=20=20(should=20(equal=20= (string-search=20""=20"abc"=203)=203))=0A+=20=20(should-error=20= (string-search=20""=20"abc"=204))=0A+=20=20(should-error=20= (string-search=20""=20"abc"=20-1))=0A+=20=20)=0A= --Apple-Mail=_EB777C97-CDF8-48EF-A5E7-78C47B853B0E--