unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Jean-Christophe Helary <jean.christophe.helary@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: mvoteiza@udel.edu, emacs-devel@gnu.org, tino.calancha@gmail.com
Subject: Re: Trimming strings, /emacs/lisp/emacs-lisp/subr-x.el modification
Date: Sat, 6 May 2017 22:05:06 +0900	[thread overview]
Message-ID: <3D0BA0E6-02E2-4E90-8A59-121065547E1C@gmail.com> (raw)
In-Reply-To: <0E129E02-FA64-4664-825B-8854A66CA9C6@gmail.com>

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

Ok, I think I have something clean.
(btw, I'm done with the copyleft paperwork)

Here is the diff, and a potential log message.

Jean-Christophe 


===========================
Add optional regexp for subr-x.el trimming functions

* lisp/emacs-lisp/subr-x.el (string-trim-left) (string-trim-right) (string-trim): add optional regexp that defaults on the original behavior.
===========================



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

diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 440213eb38..40fe78c104 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -179,21 +179,30 @@ 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)
+(defsubst string-trim-left (string &optional regexp)
+  "Trim STRING of leading string matching REGEXP.
+
+REGEXP defaults to \"[ \\t\\n\\r]+\"."
+  
+  (if (string-match (concat "\\`" (or  regexp "[ \t\n\r]+")) 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 regexp)
+  "Trim STRING of trailing string matching REGEXP.
+
+REGEXP defaults to  \"[ \\t\\n\\r]+\"."
+  
+  (if (string-match (concat (or regexp "[ \t\n\r]+") "\\'") 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 strings matching TRIM-LEFT and TRIM-RIGHT.
+
+TRIM-LEFT and TRIM-RIGHT default to \"[ \\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."

[-- Attachment #3: Type: text/plain, Size: 1120 bytes --]





> On May 6, 2017, at 20:02, Jean-Christophe Helary <jean.christophe.helary@gmail.com> wrote:
> 
> Ok, I'm fine with that. Apologies for my lack of understanding of the issues.
> 
> Jean-Christophe
> 
>> On May 6, 2017, at 19:43, Eli Zaretskii <eliz@gnu.org> wrote:
>> 
>>> From: Jean-Christophe Helary <jean.christophe.helary@gmail.com>
>>> Date: Sat, 6 May 2017 19:33:10 +0900
>>> Cc: tino.calancha@gmail.com,
>>> mvoteiza@udel.edu,
>>> emacs-devel@gnu.org
>>> 
>>> My original idea was to put the regexp into a variable that *can* eventually be overridden by a user preference.
>> 
>> Does this really have legitimate use cases?  We are talking about a
>> very low-level function; overriding its default definition of
>> whitespace by a user option would affect every single use of this
>> function, and will most probably disrupt some code which uses it.
>> 
>> I think having a way for overriding the default programmatically
>> should be good enough, at least for now.  Let's not provide a
>> defcustom unless and until we have use cases for that which we want to
>> support.
> 


  reply	other threads:[~2017-05-06 13:05 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
     [not found]                     ` <95032941-C381-47C3-9554-3E13DE6A5013@gmail.com>
2017-05-16 22:21                       ` bug#26908: Fwd: " Jean-Christophe Helary
2017-05-17  2:26                         ` Eli Zaretskii
2017-05-17  3:41                           ` Jean-Christophe Helary
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

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=3D0BA0E6-02E2-4E90-8A59-121065547E1C@gmail.com \
    --to=jean.christophe.helary@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=mvoteiza@udel.edu \
    --cc=tino.calancha@gmail.com \
    /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).