unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Returning argument string (regexp-quote)
@ 2019-09-24 11:28 Mattias Engdegård
  2019-09-24 11:49 ` Andreas Schwab
  2019-09-24 16:20 ` Eli Zaretskii
  0 siblings, 2 replies; 27+ messages in thread
From: Mattias Engdegård @ 2019-09-24 11:28 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 354 bytes --]

In most calls to 'regexp-quote', the argument doesn't actually need converting. It would be nice to avoid the allocation in that case. Patch.

More generally, do we need to preserve allocation for functions not specifically documented to return new strings? I can imagine there being code that depend on it by accident, but string mutation is rare.


[-- Attachment #2: 0001-Allow-regexp-quote-to-return-its-argument.patch --]
[-- Type: application/octet-stream, Size: 1127 bytes --]

From 30b34719db89f6094346e9dd4004509674e7021c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Mon, 23 Sep 2019 18:56:30 +0200
Subject: [PATCH] Allow regexp-quote to return its argument

* src/search.c (Fregexp_quote): Only allocate a new string if needed.
---
 src/search.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/search.c b/src/search.c
index 1e57d2ecbe..fc9b85f413 100644
--- a/src/search.c
+++ b/src/search.c
@@ -3138,10 +3138,12 @@ DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0,
     }
 
   Lisp_Object result
-    = make_specified_string (temp,
-			     SCHARS (string) + backslashes_added,
-			     out - temp,
-			     STRING_MULTIBYTE (string));
+    = (out - temp > SBYTES (string)
+       ? make_specified_string (temp,
+                                SCHARS (string) + backslashes_added,
+                                out - temp,
+                                STRING_MULTIBYTE (string))
+       : string);
   SAFE_FREE ();
   return result;
 }
-- 
2.21.0 (Apple Git-122)


^ permalink raw reply related	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2019-10-02 15:15 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-24 11:28 Returning argument string (regexp-quote) Mattias Engdegård
2019-09-24 11:49 ` Andreas Schwab
2019-09-24 16:20 ` Eli Zaretskii
2019-09-24 17:44   ` Mattias Engdegård
2019-09-24 18:24     ` Paul Eggert
2019-09-24 19:47     ` Eli Zaretskii
2019-09-25 13:41       ` Mattias Engdegård
2019-09-25 15:08         ` Eli Zaretskii
2019-09-25 19:55           ` Compilation failure on macOS 10.15 Catalina Zach Pearson
2019-09-25 20:29             ` mituharu
2019-09-25 21:55               ` Zach Pearson
2019-09-25 22:47                 ` mituharu
2019-09-25 23:07                   ` Zach Pearson
2019-09-25 23:39                     ` mituharu
2019-09-27 23:22                     ` chad
2019-10-02  3:59                       ` Zach Pearson
2019-10-02  4:04                         ` YAMAMOTO Mitsuharu
2019-10-02  4:34                           ` Zach Pearson
2019-10-02  4:42                             ` YAMAMOTO Mitsuharu
2019-10-02  4:43                               ` Zach Pearson
2019-10-02  5:00                                 ` YAMAMOTO Mitsuharu
2019-10-02  5:07                                   ` Zach Pearson
2019-10-02  7:43                                   ` Zach Pearson
2019-10-02 15:15                                     ` Xu Xin
2019-09-26  8:34             ` Robert Pluim
2019-09-26 16:05               ` Zach Pearson
2019-09-26 21:00                 ` mituharu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).