2217 bin:decode-string: Input encoding - #2222
Conversation
|
Typo |
|
As noted during the meeting this week, there are problems deciding how to handle a BOM that is not at the start of the input (given that the function allows decoding starting at an offset other than zero). It was also noted (if I heard correctly) that it might be useful to extract the encoding detection logic into a separate function. |
|
We should also look at offsets that…
|
|
It feels like we're rubbing up against some unresolvable problems. Perhaps we should push some of this effort back on the user. After all, an offset of [1] when the string begins with a BOM is either exactly what the user wants to do or it's very, very likely an error, and I don't think we can tell which. I think it was Juri who proposed an inspect function, let's say If you pass in an If It's a fairly expensive function to call, perhaps, but it means you, the user, can work out if you need to skip the BOM, or align a substring at a position where a character starts. The Is that at all sensible? |
Possibly ;) The main reason why we got here was that we were looking for a function that decodes a binary-with-an-optional-BOM to a string. Do you think that |
|
The hard way would be, check if there's a BOM, if there is, decode starting at offset [3]. But I think we could special case the "decode this whole UTF-x string and discard the BOM" behavior if the encoding is UTF-x, begins with a BOM, and no starting offset was given. |
Yes, I agree. We can either raise an error if an offset without encoding is supplied, or assume |
|
I'm inclined to say let's leave If the user wants to drop a BOM appearing at the start of the string (or anywhere else), they can do it by hand. |
|
|
|
I propose the signature I think the user should be able to specify a default to use if inference fails and I think the function should be allowed to return I prefer |
|
I think for most speakers of English (native or nonnative) the two are synonyms. "Infer" was more popular of the two words in the 19th c., but it has been eclipsed by "detect" (see NGram visualization). A case in point: @ndw preferred the word "detection" over "inference" to describe what the function should do. |
|
My resident linguist agrees with me, but I think technically this is a good way forward and I'm not going to lie down in the road over the name. Believing that you know the semantics of a function based entirely on its name is going to lead you astray sometimes. |
An additional function to infer the encoding looks reasonable to me. In addition to that, I think we still need a straightforward solution. Imagine that one would always need to write things like… let $encoding := bin:infer-encoding($bin)
let $offset := if($encoding = 'utf-8') then 3 else
if(starts-with($encoding, 'utf-16')) then 2 else 0
return decode-string($bin, $encoding, $offset)This applies even more if we want to recommend |
|
I think we could say that if the It's going to be convenient for many users and it's easy to work around if you want the BOM or if you want to do something special simply by providing an offset of 0 or an encoding. |
A general question may be what exactly we expect the function to do: Should it focus on the BOM patterns, or should it be allowed to take advantage of implementation-dependent rules or heuristics (in alignment with |
|
I imagined that the semantics of (inferred|detect)-encoding is that it does anything it wants, right down to rolling dice, to work out an encoding. It returns what it decides, or |
|
unparsed-text() has extra information to work with, for example HTTP headers (and I still lament an operating system I used to work with that allowed you to record the encoding in the file descriptor...). So allowing implementation-dependent heuristics for unparsed-text() makes sense. decode-string only has a sequence of octets to work with, so there's much less reason to let different implementations do things differently. |
In that case it could make sense to return a sequence of encoding values, along with confidence values. The ICU |
|
It may be fair to question how relevant heuristics are today. At least on websites, UTF-8 is the clear winner (and Shift-JIS, which can be detected fairly well, is hardly used anymore): https://w3techs.com/technologies/overview/character_encoding |
|
|
Revised; aligned with the updated |
|
"If a Unicode encoding is determined". What is a "Unicode encoding"? An encoding is a way of representing Unicode codepoints as octets - aren't all encodings "Unicode encodings"? [I see that fn:unparsed-text also uses this term]. |
|
It would be useful to have examples where a non-zero offset is supplied, with and without an encoding, with and without a BOM. |
Thanks, I’ll change it to UTF encoding. Note that “Unicode encoding” is also used in the Serialization spec (→ #2195 (comment)). |
|
At meeting 140, the CG agreed to merge this PR. |
Before discussing this PR, we should have a look at #2221.
Closes #2217