From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] csv-mode.el: Add function for reading a CSV line Date: Sun, 26 May 2024 10:08:11 +0200 Message-ID: <86r0dp2dxw.fsf@fastmail.fm> References: <86wmnmixmf.fsf@fastmail.fm> <87le42v093.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39061"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Stefan Monnier To: Stefan Monnier via "Emacs development discussions." Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun May 26 10:09:11 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sB8wH-0009s3-Gr for ged-emacs-devel@m.gmane-mx.org; Sun, 26 May 2024 10:09:10 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sB8vX-0006dq-Pb; Sun, 26 May 2024 04:08:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sB8vU-0006dF-2x for emacs-devel@gnu.org; Sun, 26 May 2024 04:08:20 -0400 Original-Received: from wfout4-smtp.messagingengine.com ([64.147.123.147]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sB8vS-0007qB-3n for emacs-devel@gnu.org; Sun, 26 May 2024 04:08:19 -0400 Original-Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.west.internal (Postfix) with ESMTP id 74EAB1C000A9; Sun, 26 May 2024 04:08:16 -0400 (EDT) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sun, 26 May 2024 04:08:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.fm; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1716710896; x=1716797296; bh=g6N9mpEHA9 BIiga8eOVc02rB+PkPJGnv9rTtv+wXvTM=; b=Gbbd4Xp8ia4v3GTYeXcTCPWlAZ 6tR73NDxVQjaeOtJH1S1nRaw+q6AamDWAbHrIkSH1p12nRAEfFB4TLnz+bf0tFSl QZic13swZj3tE38u7PuaAVArSQ5al3faoudkHiGiNvEq6lg1PsbiSfKKXFS1Hjde j95YqQN2vFeGbDL7FpH0dz13j21FD5I5LiR0573TpX6MQrENid5qDi+rajvO0jD+ rfLe9mBi+nK/tiZIt7eK0Yv+6kXT5V7GtDJruFeV9Nc8VdrApz59AxfVp8YRYIn7 WeCenJbPrco22Rsf3mV0Rh/ibGGag9M0sfb2MoRwxY/F+m7titWja3XqRPBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1716710896; x=1716797296; bh=g6N9mpEHA9BIiga8eOVc02rB+PkP JGnv9rTtv+wXvTM=; b=Iys/1pZKvS84ZdPc2cfkKqwuQbGR/SqVtEjRadJFIJnK l6RV1vZsgZB4vfKtN3+PmbgqEzgF4au0E7W0jjAJ1PlDVFF6X41Qzt0Aq4QP8+3d /9NE4C840tCChw6IWwPfi7e6gYWSiinBL+7XC5mRGoM6JFl8q726eo94yR2PjOg+ w3togOb0AIPh0Tjw+f2EnoJIFgt/RHSSPKHrLhSATyWlMc7OaIx+Z8yzn7zSWS7O A1sQIprWyiBcACpP67KLIBIlSJQ+hGKkYsKs8eCpeKxYtnJSjM+nkc45RE1BQQ2f qWKlhNeAF9w6lS/d9yOgjrpaPdc4Aoy0ikPUU0LHqQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdejvdcutefuodetggdotefrodftvfcurf hrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufgjfhffkfggtgesmhdtreertddttdenucfhrhhomheplfhoohhsthcumfhr vghmvghrshcuoehjohhoshhtkhhrvghmvghrshesfhgrshhtmhgrihhlrdhfmheqnecugg ftrfgrthhtvghrnhepvdeuudfghfdtvedttdelvdeludelheehfeevfffgvdehjedvieei veekjeeikefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepjhhoohhsthhkrhgvmhgvrhhssehfrghsthhmrghilhdrfhhm X-ME-Proxy: Feedback-ID: ie15541ac:Fastmail Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 26 May 2024 04:08:14 -0400 (EDT) In-Reply-To: (Stefan Monnier via's message of "Sun, 26 May 2024 00:07:32 -0400") Received-SPF: pass client-ip=64.147.123.147; envelope-from=joostkremers@fastmail.fm; helo=wfout4-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:319579 Archived-At: --=-=-= Content-Type: text/plain On Sun, May 26 2024, Stefan Monnier via "Emacs development discussions." wrote: >>> +(defun csv--unquote-value (value) >>> + "Remove quotes around VALUE. >>> +If VALUE contains escaped quote characters, un-escape them. If >>> +VALUE is not quoted, return it unchanged." >>> + (save-match-data >>> + (let ((quote-regexp (apply #'concat `("[" ,@csv-field-quotes "]")))) >>> + (string-match (concat "^\\(" quote-regexp "\\)\\(.*\\)\\(" quote-regexp "\\)$") value) > > `save-match-data` around a function's body is an anti-pattern. Thanks for pointing that out. So IIUC it should just be removed? Patch: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Remove-save-match-data-from-csv-unquote-value.patch >From 21016520838b7398559f004872f0964478f17b06 Mon Sep 17 00:00:00 2001 From: Joost Kremers Date: Sun, 26 May 2024 10:05:19 +0200 Subject: [PATCH] Remove 'save-match-data' from 'csv--unquote-value'. As per its doc string, 'save-match-data' should only be used to save one's own match data, not the caller's. --- csv-mode.el | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/csv-mode.el b/csv-mode.el index ebcd9da..37bff30 100644 --- a/csv-mode.el +++ b/csv-mode.el @@ -1408,14 +1408,13 @@ point is assumed to be at the beginning of the line." "Remove quotes around VALUE. If VALUE contains escaped quote characters, un-escape them. If VALUE is not quoted, return it unchanged." - (save-match-data - (let ((quote-regexp (apply #'concat `("[" ,@csv-field-quotes "]")))) - (if-let (((string-match (concat "^\\(" quote-regexp "\\)\\(.*\\)\\(" quote-regexp "\\)$") value)) - (quote-char (match-string 1 value)) - ((equal quote-char (match-string 3 value))) - (unquoted (match-string 2 value))) - (replace-regexp-in-string (concat quote-char quote-char) quote-char unquoted) - value)))) + (let ((quote-regexp (apply #'concat `("[" ,@csv-field-quotes "]")))) + (if-let (((string-match (concat "^\\(" quote-regexp "\\)\\(.*\\)\\(" quote-regexp "\\)$") value)) + (quote-char (match-string 1 value)) + ((equal quote-char (match-string 3 value))) + (unquoted (match-string 2 value))) + (replace-regexp-in-string (concat quote-char quote-char) quote-char unquoted) + value))) (defun csv-parse-current-row () "Parse the current CSV line. -- 2.45.1 --=-=-= Content-Type: text/plain -- Joost Kremers Life has its moments --=-=-=--