From: Benjamin Riefenstahl <b.riefenstahl@turtle-trading.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 31679@debbugs.gnu.org
Subject: bug#31679: 26.1; detect-coding-string does not detect UTF-16
Date: Sat, 02 Jun 2018 15:55:49 +0200 [thread overview]
Message-ID: <874lilgumy.fsf@blei.turtle-trading.net> (raw)
In-Reply-To: <83zi0deish.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 02 Jun 2018 10:42:22 +0300")
Hi Eli,
>> From: Benjamin Riefenstahl <b.riefenstahl@turtle-trading.net>
>> (detect-coding-string "h\0t\0m\0l\0")
>>
>> And I was surprised that this does not detect UTF-16 but instead gives
>> (no-conversion).
Eli Zaretskii writes:
> First, you should lose the trailing null (or add one more), since
> UTF-16 strings must, by definition, have an even number of bytes.
Actually this string *has* 8 bytes, the last '\0' completes the 'l' to
form the two-byte character.
> Next, you should disable null byte detection by binding
> inhibit-null-byte-detection to a non-nil value, because otherwise
> Emacs's guesswork will prefer no-conversion, assuming this is binary
> data.
O.k. that is a good tip.
> Why? because it is perfectly valid for a plain-ASCII string to include
> null bytes, so Emacs prefers to guess ASCII.
While NUL is a valid ASCII character according to the standard,
practically nobody uses it as a character. So for a heuristic in this
context, it would be a bad decision to treat it just as another
character.
And indeed NUL bytes are treated as a strong indication of binary data,
it seems. I tried to debug this. The C routine detect_coding_utf_16
tries to distinguish between binary and UTF-16, but it is not called for
the string above. That routine is called OTOH, when I add a non-ASCII
character as in "h\0t\0m\0l\0ü\0", but even than it decides that the
string is not UTF-16 (?).
> Morale: detecting an encoding in Emacs is based on heuristic
> _guesswork_, which is heavily biased to what is deemed to be the most
> frequent use cases. And UTF-16 is quite infrequent, at least on Posix
> hosts.
>
> IOW, detecting encoding in Emacs is not as reliable as you seem to
> expect. If you _know_ the text is in UTF-16, just tell Emacs to use
> that, don't let it guess.
My use-case is that I am trying to paste types other than UTF8_STRING
from the X11 clipboard, and have them handled as automatically as
possible. While official clipboard types probably have a documented
encoding (and I have code for those), applications like Firefox also put
private formats there. And Firefox seems to like UTF-16, even the
text/html format it puts there is UTF-16.
I have tried to debug the C routines that implement this (s.a.), but the
code is somewhat hairy. I guess I'll have another look to see if I can
understand it better.
Thanks so far,
benny
next prev parent reply other threads:[~2018-06-02 13:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-01 19:40 bug#31679: 26.1; detect-coding-string does not detect UTF-16 Benjamin Riefenstahl
2018-06-02 7:42 ` Eli Zaretskii
2018-06-02 13:55 ` Benjamin Riefenstahl [this message]
2018-06-02 14:24 ` Eli Zaretskii
2021-08-12 13:51 ` Lars Ingebrigtsen
2021-09-09 15:23 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874lilgumy.fsf@blei.turtle-trading.net \
--to=b.riefenstahl@turtle-trading.net \
--cc=31679@debbugs.gnu.org \
--cc=eliz@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.