From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Jean-Christophe Helary Newsgroups: gmane.emacs.devel Subject: Trimming strings, /emacs/lisp/emacs-lisp/subr-x.el modification Date: Tue, 2 May 2017 18:34:17 +0900 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_87E0FF7E-E2D1-4707-BE72-8546A201FEA4" X-Trace: blaine.gmane.org 1493717769 31426 195.159.176.226 (2 May 2017 09:36:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 2 May 2017 09:36:09 +0000 (UTC) To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue May 02 11:36:05 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5UE0-000856-OC for ged-emacs-devel@m.gmane.org; Tue, 02 May 2017 11:36:04 +0200 Original-Received: from localhost ([::1]:57704 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5UE6-0000gd-6j for ged-emacs-devel@m.gmane.org; Tue, 02 May 2017 05:36:10 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46076) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5UDX-0000gU-Gj for emacs-devel@gnu.org; Tue, 02 May 2017 05:35:36 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5UDU-0005Dq-Ch for emacs-devel@gnu.org; Tue, 02 May 2017 05:35:35 -0400 Original-Received: from mail-pg0-x22d.google.com ([2607:f8b0:400e:c05::22d]:32837) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d5UDU-0005DG-3r for emacs-devel@gnu.org; Tue, 02 May 2017 05:35:32 -0400 Original-Received: by mail-pg0-x22d.google.com with SMTP id y4so53084759pge.0 for ; Tue, 02 May 2017 02:35:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:mime-version:subject:message-id:date:to; bh=dQTCdDCJ9ElFt3r65nrnZMKUJHiGjuXoNsg1Fge2mvY=; b=QjYBzWjXqXlFXcbIdE9WP9mf5Hh/kdv3wqU27dRnGgPsIR9lwUCERP7k29I4UyP5JB 89KC/dWSwYM5ZxrawTgPbaKJ0jDtDwvN7FOkcxZ8UhxywhuaykBdg6jGg1+xsga3Wxwf K2fooo4rEOAOBY07ZYfABKV4HsaIVffEF2jBMjzuFfLVxKYVqpbV1EKPF6pYZJjIfYLk QmouNOMFFqjuFXkKIblzGfuRzUSa3WgV68DRx9DochHcHm8bkyD8Oh7X5r73dApv3CiV iKpKdvJwByhb0Um2mX3m9kDky9/Y5FlIR5Xy9d4ZFJu5HNkrcR1Q8lTazYQOHDNXbrCX 0MaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=dQTCdDCJ9ElFt3r65nrnZMKUJHiGjuXoNsg1Fge2mvY=; b=THHSyi9YHDceSA4vz9CcuZd8HkzoOqCuDbCFNRe9i7MZVNi00YomBXokXZVrCrSpRX zc1N7R5IOZFXCocsbIiRwBbbUwIVgvSmG3j0OCWrVUaaxQuu3XEe4lsWQYrE1qdnm/9S pF6gSavWAdaEgZT8tMdQ8Z9GQz2pVr1pQvWfksEYeswqmMB89rzUwgd/h4auWy3Rpikb MDb3mVzT4q5IwzwA6nV7OAZhI/oIfSUo5wjxuo1dz8tIWzx6+JHBUaUUEeLtZ6lyp4aF K54DzLR+E1kbfr7vrQ8bA/vEGKfOfLa+IV3cWTgUxLeMO6I+mmQrExtBE/UijlyAY4ZJ xXsw== X-Gm-Message-State: AN3rC/4DAE7IZIfxOvTmgLhWpeV5t/L30spB+QQSNczKkb7Nvswou4qe CfwTkYJEepffuJVJbjY= X-Received: by 10.84.135.3 with SMTP id 3mr39790425pli.65.1493717731066; Tue, 02 May 2017 02:35:31 -0700 (PDT) Original-Received: from [10.254.2.140] ([210.160.37.43]) by smtp.gmail.com with ESMTPSA id s3sm25507419pgn.29.2017.05.02.02.35.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 May 2017 02:35:29 -0700 (PDT) X-Mailer: Apple Mail (2.3273) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::22d X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:214512 Archived-At: --Apple-Mail=_87E0FF7E-E2D1-4707-BE72-8546A201FEA4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 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 --Apple-Mail=_87E0FF7E-E2D1-4707-BE72-8546A201FEA4 Content-Disposition: attachment; filename=subr-x.el.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="subr-x.el.diff" Content-Transfer-Encoding: 7bit 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." --Apple-Mail=_87E0FF7E-E2D1-4707-BE72-8546A201FEA4--