all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Trimming strings, /emacs/lisp/emacs-lisp/subr-x.el modification
@ 2017-05-02  9:34 Jean-Christophe Helary
  2017-05-02 15:41 ` Clément Pit-Claudel
                   ` (2 more replies)
  0 siblings, 3 replies; 73+ messages in thread
From: Jean-Christophe Helary @ 2017-05-02  9:34 UTC (permalink / raw)
  To: emacs-devel

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

Following the discussion about how trimming in subr-x.el did not allow for non-default regex, I have this small modification to the 3 trimming functions found therein.

Basically:
1) I extracted the default regex and assigned it to string-trim-default-regex
2) I declared optional "trim-left" and "trim-right" arguments respectively for the string-trim-left and string-trim-right functions, and declared both optional for the general string-trim function

The documentation strings are pretty much taken from subr.el so maybe the wording is not the best.

Please advise.

Jean-Christophe


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

diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 440213eb38..188f314871 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -179,21 +179,36 @@ VARLIST can just be a plain tuple.
 
 (define-obsolete-function-alias 'string-reverse 'reverse "25.1")
 
-(defsubst string-trim-left (string)
-  "Remove leading whitespace from STRING."
-  (if (string-match "\\`[ \t\n\r]+" string)
+(defconst string-trim-default-regex "[ \t\n\r]+")
+  "The default value of separators for `string-trim'."
+
+(defsubst string-trim-left (string &optional trim-left)
+  "Trim STRING of leading whitespace matching TRIM-LEFT.
+
+If TRIM-LEFT is non-nil, it should be a regular expression. If nil,
+it defaults to `string-trim-default-regex', normally \"[ \t\n\r]+\"."
+  
+  (if (string-match (concat "\\`" (or  trim-left string-trim-default-regex)) string)
       (replace-match "" t t string)
     string))
 
-(defsubst string-trim-right (string)
-  "Remove trailing whitespace from STRING."
-  (if (string-match "[ \t\n\r]+\\'" string)
+(defsubst string-trim-right (string &optional trim-right)
+  "Trim STRING of trailing whitespace matching TRIM-RIGHT.
+
+If TRIM-RIGHT is non-nil, it should be a regular expression. If nil,
+it defaults to `string-trim-default-regex', normally \"[ \t\n\r]+\"."
+  
+  (if (string-match (concat (or trim-right string-trim-default-regex) "\\'") string)
       (replace-match "" t t string)
     string))
 
-(defsubst string-trim (string)
-  "Remove leading and trailing whitespace from STRING."
-  (string-trim-left (string-trim-right string)))
+(defsubst string-trim (string &optional trim-left trim-right)
+  "Trim STRING of leading and trailing whitespace matching TRIM-LEFT and TRIM-RIGHT.
+
+If TRIM-LEFT and TRIM-RIGHT are non-nil, they should be a regular expression. If nil,
+they default to `string-trim-default-regex', normally \"[ \t\n\r]+\"."
+
+  (string-trim-left (string-trim-right string trim-right) trim-left))
 
 (defsubst string-blank-p (string)
   "Check whether STRING is either empty or only whitespace."

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

end of thread, other threads:[~2017-05-19 11:30 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-02  9:34 Trimming strings, /emacs/lisp/emacs-lisp/subr-x.el modification Jean-Christophe Helary
2017-05-02 15:41 ` Clément Pit-Claudel
2017-05-02 22:35   ` Jean-Christophe Helary
2017-05-02 17:16 ` Eli Zaretskii
2017-05-02 22:48   ` Jean-Christophe Helary
2017-05-02 23:11 ` Mark Oteiza
2017-05-03  1:13   ` Jean-Christophe Helary
2017-05-06  2:41     ` Jean-Christophe Helary
2017-05-06  4:29       ` Tino Calancha
2017-05-06  9:02         ` Jean-Christophe Helary
2017-05-06  9:22           ` Eli Zaretskii
2017-05-06 10:33             ` Jean-Christophe Helary
2017-05-06 10:43               ` Eli Zaretskii
2017-05-06 11:02                 ` Jean-Christophe Helary
2017-05-06 13:05                   ` Jean-Christophe Helary
2017-05-06 13:51                     ` Tino Calancha
2017-05-06 14:24                       ` Eli Zaretskii
2017-05-07  2:25                         ` Tino Calancha
2017-05-07  2:43                           ` Eli Zaretskii
2017-05-07 10:40                             ` Tino Calancha
2017-05-06 17:51                     ` Johan Bockgård
2017-05-06 19:02                       ` Eli Zaretskii
2017-05-06 19:55                         ` Johan Bockgård
2017-05-06 20:03                           ` Eli Zaretskii
2017-05-07 16:23                             ` Johan Bockgård
2017-05-07 16:53                               ` Eli Zaretskii
2017-05-10  9:21                                 ` Yuri Khan
2017-05-10 11:15                                   ` Jean-Christophe Helary
2017-05-10 11:56                                   ` Stefan Monnier
2017-05-10 12:21                                     ` Jean-Christophe Helary
2017-05-08  2:40                             ` Jean-Christophe Helary
2017-05-08 14:28                               ` Eli Zaretskii
2017-05-08 21:33                                 ` Jean-Christophe Helary
2017-05-08 22:45                                   ` Johan Bockgård
2017-05-08 23:15                                     ` Jean-Christophe Helary
2017-05-09 12:05                                       ` Michael Heerdegen
2017-05-09 13:09                                         ` Jean-Christophe Helary
2017-05-09 14:05                                           ` Michael Heerdegen
2017-05-10  9:30                                           ` Michael Heerdegen
2017-05-10 11:11                                             ` Jean-Christophe Helary
2017-05-09 15:23                                         ` Eli Zaretskii
2017-05-09 15:33                                           ` Jean-Christophe Helary
2017-05-09 16:21                                             ` Eli Zaretskii
2017-05-09 23:03                                               ` Jean-Christophe Helary
2017-05-10  0:45                                                 ` Stefan Monnier
2017-05-10  2:44                                                   ` Eli Zaretskii
2017-05-10  3:09                                                     ` Stefan Monnier
2017-05-10  3:49                                                   ` Jean-Christophe Helary
2017-05-10 11:55                                                     ` Stefan Monnier
2017-05-09 16:37                                             ` Andreas Schwab
2017-05-10  2:29                                               ` Eli Zaretskii
2017-05-10  7:45                                                 ` Andreas Schwab
2017-05-10 11:07                                                   ` Jean-Christophe Helary
2017-05-07  4:39                           ` Jean-Christophe Helary
2017-05-07 14:49                             ` Eli Zaretskii
2017-05-07 13:48                       ` Stefan Monnier
2017-05-10 14:26                   ` Jean-Christophe Helary
2017-05-13 13:36                     ` bug#26908: Fwd: " Jean-Christophe Helary
2017-05-16 22:21                       ` Jean-Christophe Helary
2017-05-16 22:21                       ` Jean-Christophe Helary
2017-05-17  2:26                         ` Eli Zaretskii
2017-05-17  2:26                         ` Eli Zaretskii
2017-05-17  3:41                           ` Jean-Christophe Helary
2017-05-17  3:41                           ` Jean-Christophe Helary
2017-05-19 11:30                       ` Eli Zaretskii
2017-05-06 11:06                 ` Jean-Christophe Helary
2017-05-06 16:30           ` Drew Adams
2017-05-06 17:44             ` Stefan Monnier
2017-05-06 18:07               ` subr-x.el code and defsubst [was: Trimming strings, /.../subr-x.el modification] Drew Adams
2017-05-14 22:45                 ` Tianxiang Xiong
2017-05-15  1:11                   ` Drew Adams
2017-05-06  9:12         ` Trimming strings, /emacs/lisp/emacs-lisp/subr-x.el modification Andreas Schwab
2017-05-06 10:31           ` Tino Calancha

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.