Allow `string-limit' to work on encoded strings

* doc/lispref/strings.texi (Creating Strings): Document it.

* lisp/emacs-lisp/subr-x.el (string-limit): Allow limiting on
encoded strings.
This commit is contained in:
Lars Ingebrigtsen 2020-12-25 05:58:09 +01:00
parent 269cec13a2
commit af359de917
4 changed files with 59 additions and 7 deletions

View file

@ -600,6 +600,26 @@
(should (equal (string-limit "foo" 0) ""))
(should-error (string-limit "foo" -1)))
(ert-deftest subr-string-limit-coding ()
(should (not (multibyte-string-p (string-limit "foó" 10 nil 'utf-8))))
(should (equal (string-limit "foó" 10 nil 'utf-8) "fo\303\263"))
(should (equal (string-limit "foó" 3 nil 'utf-8) "fo"))
(should (equal (string-limit "foó" 4 nil 'utf-8) "fo\303\263"))
(should (equal (string-limit "foóa" 4 nil 'utf-8) "fo\303\263"))
(should (equal (string-limit "foóá" 4 nil 'utf-8) "fo\303\263"))
(should (equal (string-limit "foóa" 4 nil 'iso-8859-1) "fo\363a"))
(should (equal (string-limit "foóá" 4 nil 'iso-8859-1) "fo\363\341"))
(should (equal (string-limit "foóá" 4 nil 'utf-16) "\376\377\000f"))
(should (equal (string-limit "foó" 10 t 'utf-8) "fo\303\263"))
(should (equal (string-limit "foó" 3 t 'utf-8) "o\303\263"))
(should (equal (string-limit "foó" 4 t 'utf-8) "fo\303\263"))
(should (equal (string-limit "foóa" 4 t 'utf-8) "o\303\263a"))
(should (equal (string-limit "foóá" 4 t 'utf-8) "\303\263\303\241"))
(should (equal (string-limit "foóa" 4 t 'iso-8859-1) "fo\363a"))
(should (equal (string-limit "foóá" 4 t 'iso-8859-1) "fo\363\341"))
(should (equal (string-limit "foóá" 4 t 'utf-16) "\376\377\000\341")))
(ert-deftest subr-string-lines ()
(should (equal (string-lines "foo") '("foo")))
(should (equal (string-lines "foo \nbar") '("foo " "bar"))))