unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Shigeru Fukaya <shigeru.fukaya@gmail.com>
To: 17814@debbugs.gnu.org
Subject: bug#17814: 24.3.91; better string manipulation in subr-x
Date: Fri, 20 Jun 2014 04:10:30 +0900	[thread overview]
Message-ID: <38CF8BF2232E16shigeru.fukaya@gmail.com> (raw)

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


Some string manipulation functions in subr-x have room to optimize.


string-trim-left, string-trim-right -- use `substring' and
`match-beginning/end' instead of `replace-match'.  The formers have
bytecodes and the latter not.

string-trim -- call string-trim-left first would be cost effective.
But, to change the code to trim both sides of the string at once might
be better.

string-remove-suffix -- change the last argument of substring will
shorten the code.


I change the string-trim defined using defun from defsubst, as its
string literal is somewhat big (Actually I suspect most of other
functions would also be better if defined by defun).


Regards,
Shigeru

[-- Attachment #2: subr-x.diff --]
[-- Type: application/octet-stream, Size: 1366 bytes --]

--- orig/subr-x.el	2014-03-21 14:34:40.000000000 +0900
+++ ./subr-x.el	2014-06-20 03:43:54.627390700 +0900
@@ -59,18 +59,23 @@
 (defsubst string-trim-left (string)
   "Remove leading whitespace from STRING."
   (if (string-match "\\`[ \t\n\r]+" string)
-      (replace-match "" t t string)
+      (substring string (match-end 0))
     string))
 
 (defsubst string-trim-right (string)
   "Remove trailing whitespace from STRING."
   (if (string-match "[ \t\n\r]+\\'" string)
-      (replace-match "" t t string)
+      (substring string 0 (match-beginning 0))
     string))
 
-(defsubst string-trim (string)
+(defun string-trim (string)
   "Remove leading and trailing whitespace from STRING."
-  (string-trim-left (string-trim-right string)))
+  ;;(string-trim-right (string-trim-left string))
+  (if (string-match (concat "\\`\\(?:[\s\t\n\r]+\\(?1:.*?\\)[\s\t\n\r]*"
+			    "\\|\\(?1:.*?\\)[\s\t\n\r]+\\)\\'")
+		    string)
+      (match-string 1 string)
+    string))
 
 (defsubst string-blank-p (string)
   "Check whether STRING is either empty or only whitespace."
@@ -85,7 +90,7 @@
 (defsubst string-remove-suffix (suffix string)
   "Remove SUFFIX from STRING if present."
   (if (string-suffix-p suffix string)
-      (substring string 0 (- (length string) (length suffix)))
+      (substring string 0 (- (length suffix)))
     string))
 
 (provide 'subr-x)

             reply	other threads:[~2014-06-19 19:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-19 19:10 Shigeru Fukaya [this message]
2014-06-19 20:44 ` bug#17814: 24.3.91; better string manipulation in subr-x Stefan Monnier
2014-06-20 17:14   ` Shigeru Fukaya
2014-06-20 19:14     ` Stefan Monnier
2014-06-21  4:07       ` Shigeru Fukaya
2018-09-19  1:37         ` Noam Postavsky

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/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38CF8BF2232E16shigeru.fukaya@gmail.com \
    --to=shigeru.fukaya@gmail.com \
    --cc=17814@debbugs.gnu.org \
    /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.
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).