From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.bugs Subject: bug#56685: OBOE in string-truncate-left? Date: Fri, 22 Jul 2022 12:26:09 +0200 Message-ID: <87sfmtfnpa.fsf@gmx.net> References: 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="6293"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: 56685@debbugs.gnu.org To: Stefan Kangas Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 22 12:29:24 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1oEpuO-0001R5-Qc for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 22 Jul 2022 12:29:24 +0200 Original-Received: from localhost ([::1]:41302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEpuM-0007CH-6b for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 22 Jul 2022 06:29:22 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59768) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEps6-0005gW-B1 for bug-gnu-emacs@gnu.org; Fri, 22 Jul 2022 06:27:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:50443) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oEps6-0001rd-1p for bug-gnu-emacs@gnu.org; Fri, 22 Jul 2022 06:27:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oEps5-0004YS-Td for bug-gnu-emacs@gnu.org; Fri, 22 Jul 2022 06:27:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stephen Berman Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 22 Jul 2022 10:27:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56685 X-GNU-PR-Package: emacs Original-Received: via spool by 56685-submit@debbugs.gnu.org id=B56685.165848558117443 (code B ref 56685); Fri, 22 Jul 2022 10:27:01 +0000 Original-Received: (at 56685) by debbugs.gnu.org; 22 Jul 2022 10:26:21 +0000 Original-Received: from localhost ([127.0.0.1]:40192 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oEprQ-0004XH-S8 for submit@debbugs.gnu.org; Fri, 22 Jul 2022 06:26:21 -0400 Original-Received: from mout.gmx.net ([212.227.15.15]:60101) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oEprO-0004X1-9z for 56685@debbugs.gnu.org; Fri, 22 Jul 2022 06:26:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1658485572; bh=fCPs+auESTQfZzKm/1tghc7fSj1PhkgZSOU0bLoJakg=; h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References:Date; b=YuojvFUTL65CQOEDZNbrEuuWw3bSbv88pE1TF1XsfJ/3AbeFrtVhUg4Hy+eieK8bO Q0XLJWeAgfu3BvJAHvHiMmF5tzFnesudIn6oxqtiILXJWob8u8ukHhTFDNMrYUc9Ip hDwxiwVVSmWLT/jgk18KTX1kb4AgJwuVMm7quxoo= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from strobelfs2 ([89.246.38.216]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MJVDW-1nuxWm0eqG-00Jqpb; Fri, 22 Jul 2022 12:26:12 +0200 In-Reply-To: (Stefan Kangas's message of "Thu, 21 Jul 2022 17:10:36 -0500") X-Provags-ID: V03:K1:Ej7KGQ7GC6lAt2zKqLpG45E8+ENNHi39hoGrYgBusPIgcPwfegw yvMSsl2/zkLAoM4Wr+M3XOAvEaOA4eSFG2oOMvFc634NQYnAwdD2ibVMf5ZqjhP5lqkCWsm YIdRjfP3ACRL0I9DvpaSwrnRXF/ykuDNd0ZrKRAAnM9NZkZrL/WoWjTbqW4HlpDC+sPXGzr ctvRfw1kmHr8Pap55SxjA== X-UI-Out-Filterresults: notjunk:1;V03:K0:YCz9UyXtKsA=:fBwxwHRS63Yqf5eWBEfZH/ oKtKFUUcckbuw+rOdws1YzFOEvdGKtAHx62+cTKFa9zBIIBH4YfDq1gFGehbguO099zojc5He sYvamkeDu7nxvU1wysKryqAR3UkAA1U26kKgQ+1AEr/gnI2mDOVfE4gQFy5iMzbHvqqyUEROy u/t6uuWjjoVMTjymENeRi7gAiy5oWBtwG3/1uygZ1LwyPMuRSdiC6DMVc79J4W8/rfvDxlXl1 uL8Dp7yX8JD5y8nhQysB0KkzkRwh1sDf4Vhccr1stcx8SpYZ5yYQmAwhkeewW3tbCLSquC0M3 R/vy48E/icd94B1MjO2kYW1UaQoJQI32/muxQn4HpKU5uf6xZ2hE/7Hu6lzYDEEzcw94ffhv1 mFc/0M2pA4Z2K2sgxuCaTpXVI+RX5eYoJazGrBC0CNhJOQgPKKPSqoMBnKXu1EnV4vjp4b+bc biduWCs6pW8YRU2LY5VhHTr/04UE4Zh64o1m53cLIWtmhSt4Dnuh2gCWxggrE3PBriOI7YQMT Mq4vpX6gI372KpovUtJunvn6gj1vvJ9uuf4ncvRNY6V47Jaddkyj1GHZ4DwCj0sJtOUCbvBdX T2+TxyEp/m3aUuoZPOmBpgwZDqfZKg0IuE87uVyPZFHDQmmbD3+oKPGNpU1+tQzA6lT5BRq2F qD8eXyKv9amELgTQ3rDCo9q8Ql3HswnP7Tt4QvOla5dumPxVoMQt9zYIPRVE2SFMsQHaZfC74 FFmCPOL99K7jsdvrRJ/n3DLIOeZjSZoKo49Co5lsPtKdS6tTLTKJKLWIYnFdSE/8KVKol7QN X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:237608 Archived-At: --=-=-= Content-Type: text/plain On Thu, 21 Jul 2022 17:10:36 -0500 Stefan Kangas wrote: > Try evaluating: > > (length (string-truncate-left "longstring" 8)) > => 9 > > But the docstring says "Truncate STRING to LENGTH". > This seems like a bug. Yes, and I also think it's counterintuitive that LENGTH includes the length of "...". Worse, if STRING is short enough, the resulting string (with "...") can be longer than LENGTH: (string-truncate-left "band" 3) "...d" (string-truncate-left "band" 2) "...d" (string-truncate-left "band" 1) "...d" (string-truncate-left "and" 2) "...d" (string-truncate-left "and" 1) "...d" Note that with the last two examples, the result is longer than the original string, contradicting the meaning of truncation. I think LENGTH should mean just the numbers of characters in STRING after truncation (excluding "..."), and if the result with the prefix "..." is not shorter than the original string, there should be no truncation. The following patch does this. --=-=-= Content-Type: text/x-patch Content-Disposition: inline Content-Description: string-truncate-left patch Content-Transfer-Encoding: quoted-printable diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 5037ae47e8..6eefd5d141 100644 =2D-- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -107,12 +107,13 @@ 'string-reverse ;;;###autoload (defun string-truncate-left (string length) - "Truncate STRING to LENGTH, replacing initial surplus with \"...\"." + "Return STRING's last LENGTH characters prefixed with \"...\". +If the resulting string with the prefix is not shorter than the +original length of STRING, return STRING unchanged." (let ((strlen (length string))) - (if (<=3D strlen length) + (if (<=3D strlen (+ length 3)) string - (setq length (max 0 (- length 3))) - (concat "..." (substring string (max 0 (- strlen 1 length))))))) + (concat "..." (substring string (max 0 (- strlen length))))))) (defsubst string-blank-p (string) "Check whether STRING is either empty or only whitespace. --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable =2D- Steve Berman --=-=-=--