[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Obsolete string-to-multibyte hard to replace
From: |
Stefan Monnier |
Subject: |
Obsolete string-to-multibyte hard to replace |
Date: |
Mon, 29 May 2017 09:01:24 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) |
I was looking at ruler-mode.el's bytecomp warning about string-to-multibyte
and it seems like there is no good way to write cleaner code here:
(let (...
(ruler
(propertize
(string-to-multibyte
(make-string w ruler-mode-basic-graduation-char))
...))
...)
[...]
(aset ruler k (aref c (setq m (1- m))))
Here, the problem is as follows: make-string returns a unibyte string if
the initial char is ASCII and a multibyte string otherwise.
Seems innucuous, but it means that if the initial char
(ruler-mode-basic-graduation-char above) is ASCII, you can't later
insert a multibyte char into that string with `aset`.
Replacing string-to-multibyte with decode-coding-string is clearly wrong
in the above case (I mean it'll work, but it's even more of a hack than
using string-to-multibyte).
While investigating it, I noticed that my local Emacs hacks include
a change of make-string so it always returns a multibyte string
(I think I made this change, because an all-ASCII multibyte string is
a more precise information than an all-ASCII unibyte string: with
a multibyte string, the fact that `x->size == x->size_byte` immediately
tells us this is an ASCII-only string whereas with a unibyte string we
can't know if that string is ASCII-only without checking each and every
byte).
I think it'd make sense to change make-string so it always returns
a multibyte string, and maybe to also introduce a new make-unibyte-string.
Stefan
- Obsolete string-to-multibyte hard to replace,
Stefan Monnier <=