GNU bug report logs -
#56685
OBOE in string-truncate-left?
Previous Next
Reported by: Stefan Kangas <stefan <at> marxist.se>
Date: Thu, 21 Jul 2022 22:11:02 UTC
Severity: normal
Fixed in version 29.1
Done: Lars Ingebrigtsen <larsi <at> gnus.org>
Bug is archived. No further changes may be made.
Full log
Message #8 received at 56685 <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
On Thu, 21 Jul 2022 17:10:36 -0500 Stefan Kangas <stefan <at> marxist.se> 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.
[Message part 2 (text/x-patch, inline)]
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 5037ae47e8..6eefd5d141 100644
--- 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 (<= strlen length)
+ (if (<= 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.
[Message part 3 (text/plain, inline)]
--
Steve Berman
This bug report was last modified 2 years and 300 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.