From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.devel Subject: Re: Trimming strings, /emacs/lisp/emacs-lisp/subr-x.el modification Date: Sat, 6 May 2017 13:29:16 +0900 (JST) Message-ID: References: <87vapij1l7.fsf@holos> <6870A2B6-F685-4955-9C0A-256601DB47BC@gmail.com> <51D5E92C-F125-4ADE-8C55-E3513C00ECDC@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; BOUNDARY="8323329-678767571-1494044958=:15158" X-Trace: blaine.gmane.org 1494044974 31712 195.159.176.226 (6 May 2017 04:29:34 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 6 May 2017 04:29:34 +0000 (UTC) User-Agent: Alpine 2.20 (DEB 67 2015-01-07) Cc: Mark Oteiza , Tino Calancha , emacs-devel To: Jean-Christophe Helary Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat May 06 06:29:28 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 1d6rLU-00087Z-GW for ged-emacs-devel@m.gmane.org; Sat, 06 May 2017 06:29:28 +0200 Original-Received: from localhost ([::1]:49949 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6rLY-0004HE-Mz for ged-emacs-devel@m.gmane.org; Sat, 06 May 2017 00:29:32 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47825) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6rLS-0004H9-JJ for emacs-devel@gnu.org; Sat, 06 May 2017 00:29:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6rLO-0005MA-Kv for emacs-devel@gnu.org; Sat, 06 May 2017 00:29:26 -0400 Original-Received: from mail-pf0-x22a.google.com ([2607:f8b0:400e:c00::22a]:35797) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d6rLO-0005L0-Ef for emacs-devel@gnu.org; Sat, 06 May 2017 00:29:22 -0400 Original-Received: by mail-pf0-x22a.google.com with SMTP id v14so10834877pfd.2 for ; Fri, 05 May 2017 21:29:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=M3JQUMQ/SgaOtLguY+FRalLhIQyXhWNYM9OYVyTEbJE=; b=U98OOmxtUz/wo12rybKOoL90ed1aDOfHV1yHxKtG3pyprXK9d7xq87v955oyrP9Plw drrd8h9qu4BepdVZctSux0mASxVfcMSoWgSl+aix7NN8QvA0n+IXjgGipntCbQLf7+Tm IerzcTj9uxRU/60QuIIAeHP7WVLcCPQOIoSTLZuuf9oI31g7V2kcpefSBzAA9slg6p5e lttIHxyR+ubCeFLGyV5MX4jQxUssJMZCb8ioES4uM4bOJplPSxe9pSO9+HeWQdoC6I8S 23n/vyNCy1SDSqmhzWdKN4vRvDclQBEc2lrTBXQxyYh36t5f55MKALPOcLggEDxP+ALK jF+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=M3JQUMQ/SgaOtLguY+FRalLhIQyXhWNYM9OYVyTEbJE=; b=fztYX1hqVrvpOebLQ2Wxc1yMmOPq5/zj7UUjUjm5SpH+LG6NHH0z3FT0giVnXqPrAt EmqigXJOb50G32U7q8hstUQZDr7628Vnyq0bHaI7Nc+ZeFdZ4aGbr57RQsulR04IDSFw yGFTGbkEGiBLxulmA5EkYmP0Q7DB0Aih+6MKKN0fZRk07989maIt1ffGJg4nnvn/1DmT P0n4R1gdXljNm1U4zKzlabju10OsXlKHjUYs5aHxoTpGVvvhVWVVD/yxDJh2zI7YG3OE w1j39h03EltSKmGpsSJ+SgGBVLd53vK+GYIHbtsjVgb8AELxtrMiKHZMXWfgvvQXwsib mPTQ== X-Gm-Message-State: AN3rC/7TsfFEgHnxDph2skObQNZe3gH8twh2YZdWISpX/m7SD/e47jZu MkavZLKqiCjdLg== X-Received: by 10.98.7.1 with SMTP id b1mr21076662pfd.20.1494044959866; Fri, 05 May 2017 21:29:19 -0700 (PDT) Original-Received: from calancha-pc (222.139.137.133.dy.bbexcite.jp. [133.137.139.222]) by smtp.gmail.com with ESMTPSA id a24sm8611865pfl.70.2017.05.05.21.29.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 May 2017 21:29:19 -0700 (PDT) X-Google-Original-From: Tino Calancha X-X-Sender: calancha@calancha-pc In-Reply-To: <51D5E92C-F125-4ADE-8C55-E3513C00ECDC@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::22a 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:214599 Archived-At: --8323329-678767571-1494044958=:15158 Content-Type: text/plain; charset=ISO-8859-7; format=flowed Content-Transfer-Encoding: 8BIT On Sat, 6 May 2017, Jean-Christophe Helary wrote: >>> Basically everything in subr-x is either a macro or a defsubst, >>> >>> As such, most uses of subr-x are done with (eval-when-compile (require >>> 'subr-x)). Won't the use of a global variable break these? >> >> (defconst string-trim-default-regex "[ \t\n\r]+") >> "The default value of the trimmed string for `string-trim'." >> >> I have no idea what the effects would be. What would you suggest? Hi Jean-Christophe, Mark is right. Your patch won't work in the following example: Write a file '/tmp/test.el' --8<-----------------------------cut here---------------start------------->8--- (eval-when-compile (require 'subr-x)) (defun test () (message (string-trim " Hi! "))) --8<-----------------------------cut here---------------end--------------->8--- M-! emacs -batch -eval '(byte-compile-file "/tmp/test.el")' && \ emacs -batch -l /tmp/test.elc -eval "(test)" RET ;; Signals error: Symbol¢s value as variable is void: string-trim-default-regex Instead, something as follows would work: --8<-----------------------------cut here---------------start------------->8--- commit dc4d7fa64c5cf4f3c3fa04182da974f2d0bc6499 Author: Jean-Christophe Helary Date: Sat May 6 13:24:03 2017 +0900 Allow trim strings of whitespaces with a general regexp * lisp/emacs-lisp/subr-x.el (string-trim-left, string-trim-right): Add optional arg REGEXP. (string-trim): Add optional args TRIM-LEFT and TRIM-RIGHT. diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 440213eb38..5148e8b724 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -179,21 +179,29 @@ string-join (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 whitespace 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 whitespace 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 whitespace matching TRIM-LEFT and TRIM-RIGHT. + +Both, TRIM-RIGHT and TRIM-LEFT 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." --8<-----------------------------cut here---------------end--------------->8--- In GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-05-05 Repository revision: 927dcbd2e6e0e53fcfb09296716e11c002ab1518 Cheers, Tino --8323329-678767571-1494044958=:15158--