Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2021-03-18
13:44 • Closed ticket [3466099fff]: BOM in Unicode plus 8 other changes artifact: c2079890aa user: jan.nijtmans
2014-02-21
19:07 • Ticket [3466099fff]: 1 change artifact: 1819af9b08 user: jan.nijtmans
2012-03-08
16:48 • Ticket [3466099fff]: 4 changes artifact: 5ffe1498ed user: sebres
16:15 • Ticket [3466099fff]: 4 changes artifact: f92c31647f user: sebres
05:03 • Ticket [3466099fff]: 4 changes artifact: bfac3ca3e6 user: nijtmans
03:37 • Ticket [3466099fff]: 4 changes artifact: a1ba7a91d7 user: nijtmans
2012-03-07
21:34 • Ticket [3466099fff]: 4 changes artifact: 50151d8fe5 user: dkf
21:09 • Ticket [3466099fff]: 4 changes artifact: 3e041c971b user: sebres
20:54 • Ticket [3466099fff]: 4 changes artifact: 85eeec6395 user: dkf
2012-03-06
21:31 • Ticket [3466099fff]: 4 changes artifact: b3adff03df user: sebres
21:23 • Ticket [3466099fff]: 4 changes artifact: cd74ba5c7a user: sebres
2012-03-02
17:05 • Ticket [3466099fff]: 4 changes artifact: d83e73431e user: sebres
05:49 • Open ticket [3466099fff]. artifact: 98df5c8df3 user: nijtmans
2012-03-01
05:48 • Closed ticket [3466099fff]. artifact: 96acf7efef user: nijtmans
2012-02-20
22:14 • Ticket [3466099fff]: 4 changes artifact: 4b7317dcfe user: nijtmans
20:16 • Ticket [3466099fff]: 4 changes artifact: cdea52d52e user: dkf
2012-02-19
22:27 • Ticket [3466099fff]: 5 changes artifact: 0b56039e32 user: nijtmans
2012-01-09
22:07 • Ticket [3466099fff]: 4 changes artifact: 85bf9adc57 user: nijtmans
21:12 • Ticket [3466099fff]: 4 changes artifact: ccd09fbeb0 user: dgp
2011-12-29
22:42 • Ticket [3466099fff]: 1 change artifact: 61a6bf077d user: nijtmans
06:32 • Ticket [3466099fff]: 4 changes artifact: da6fa50545 user: nijtmans
01:13 • Ticket [3466099fff]: 4 changes artifact: 8fc287d2d8 user: dkf
2011-12-28
16:26 • Ticket [3466099fff]: 4 changes artifact: 0e934a6d17 user: nijtmans
2011-12-27
17:31 • New ticket [3466099fff]. artifact: 438b54e2ec user: dkf

BOM in Unicode

Closed / Fixed3466099fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 6 years ago
Descriptionplain2011-12-27 17:31:19
I was reading about the problems that some people are having with Tcl scripts on Windows due to that platform's insistence on putting a byte-order mark at the start of a UTF-8 file. (Arguably wrong, but we're stuck with it.) For reference:
https://groups.google.com/group/comp.lang.tcl/browse_frm/thread/cb6fbae11b95fac6/c4211cabc90a8b30?hl=en#c4211cabc90a8b30

I was wondering if the most effective way of dealing with this would be to make Tcl treat a stray BOM as whitespace for the purpose of script parsing? I don't know exactly how practical this is, but it would make the most pressing part of the problem Go Away.
nijtmansplain2011-12-28 16:26:48
I think I would modify Tcl_FSEvalFileEx such that when it
encounters a BOM as first character (in any of the forms
allowed by Unicode), it would switch the encoding
accordingly. Then it would work with UTF-16 as well, in
both little- and big-endian formst. It will be about the
same amount of work.
dkfplain2011-12-29 01:13:28
Makes sense. I think we can get away just fine with making Tcl_FSEvalFileEx assume that the file's contents are supposed to be a script and so do a bit more magic than normal. (Theoretically, we also ought to think about doing progressive evaluation of "large" files, say over 1MB. That's for another time.)
nijtmansplain2011-12-29 06:32:31
First attempt implemented in branch bug-3466099

Donal, do you see any negative effects of this? The disadvantage is that
any stream which does not contain a BOM will need to seek to the
start, and be read again in (possibly) another encoding...
Still, I think this is the way I would go.

Any feedback is highly appreciated!
nijtmans Ticket [3466099fff]: 1 change [History diff]2011-12-29 22:42:40
dgpplain2012-01-09 21:12:14
I think that when Tcl_FSEvalFileEx() 
receives a non-NULL value of encodingName,
that request ought to be honored.

When the caller hasn't made an explicit request, then
I can see some value in using BOM contents as a way
to make a better guess than blindly using the system encoding.
nijtmansplain2012-01-09 22:07:32
>I think that when Tcl_FSEvalFileEx()
>receives a non-NULL value of encodingName,
>that request ought to be honored.

Agreed, but it's a little bit trickier. If the user
explicitly speciefies "utf-8" or "unicode" that
should be honored too.

Actually, I am thinking about splitting
the functionality between Tcl_FSEvalFileEx
and the encoding machinery.

Currently the encoding "" is synonymous with the
system encoding. We could also create an
additional encoding with the name "", which
reads the first 2/3/4 bytes to see if it is some kind
of BOM. If it is, switch to the corresponding
encoding (utf-8, utf-16, ....) otherwise go on
using default system encoding. Then the
only thing left to be done in Tcl_FSEvalFileEx
is to strip the BOM.
nijtmansplain2012-02-19 22:27:37
New attempt in bug-3466099 branch (threw away the old one)

Tcl_FSEvalFileEx now throws away the BOM when it is the
first character in the stream. If the encoding is set correctly
(e.g. to UTF-8) this will work on Unix and Windows.
Added test case source-2.7 to prove that. Advantage:
no seek is needed, as in the previous implementation.
So it is harmless for Tcl 8.4/8.5 as well.

This could be improved by adding a new encoding
named "", which is almost the same as the system
encoding. The only difference is that, if the first
characters of the stream is a BOM in any of the
UTF-8 or UTF-16 forms it will swith to this
encoding, otherwise it will behave exactly
like the system encoding. I'll leave that
for some other day (and for Tcl 8.6 only)
dkfplain2012-02-20 20:16:41
Your test passes and I think it is correctly testing this feature. (I made the test clearer so that it has the file contents setup in the test body; that's clearer if the test fails.)
nijtmansplain2012-02-20 22:14:30
Thanks! Yes I agree with your changes.
nijtmansplain2012-03-01 05:48:49
Committed to core-8-4-branch, core-8-5-branch and trunk.

The situation as described in the above reference, where
the Tcl script file started with BOM, now works as expected.

I don't thing that putting a BOM as start in an UTF-8 file
is wrong, see http://unicode.org/faq/utf_bom.html

Q: When a BOM is used, is it only in 16-bit Unicode text?
A: No, a BOM can be used as a signature no matter how the Unicode text is transformed: UTF-16, UTF-8, or UTF-32. The exact bytes comprising the BOM will be whatever the Unicode character U+FEFF is converted into by that transformation format. In that form, the BOM serves to indicate both that it is a Unicode file, and which of the formats it is in.

Now Tcl conforms to that, which it never did.
nijtmansplain2012-03-02 05:49:02
Re-opening, because one situation is not handled yet,
which can cause problems. On Windows, normally the
system encoding is cp1252 (actually cp1250-1258). The
previous part only handles the situation that the encoding
is set to utf-8 explicitely.

However, the BOM indicates that the remaining should be
handled as utf-8, regardless of the system encoding.

Therefore, I created a new branch bug-3466099,
meant as an experiment (again). What it does:
If the system encoding is cp125[0-8] or identity
and the file starts with a BOM, the BOM is
skipped and the encoding is automatically
set to utf-8 while reading the remaining of the file

This experiment is committed in branch
bug-3466099. Remarks more than welcome.

Test cases are source-2.8 up to source-2.17 for
the different system encodings.

Specific question. Is there any other encoding
commonly encountered on Windows, which should
be handled the same way?

Regards,
           Jan Nijtmans
sebresplain2012-03-02 17:05:15
The solution with cpBomTable is although good, but theoretically the parameter 'encodingName' could be another single byte encoding such iso8859-X, etc.
So the prepared array 'cpBomTable' should be greater and it will be no more practical.
I see 2 solution here:
  1) read 4 first characters binary, if not BOM convert its to given 'encodingName', set channel encoding to 'encodingName', read further.
  2) read 1 first characters in utf-8, if not BOM seek to start, set channel encoding to 'encodingName', read further.

I'm trying now the solution number 1.
sebresplain2012-03-06 21:23:20
Commit of another solution (1) with auto recognition (without fixed cpBomTable).
See http://core.tcl.tk/tcl/info/8da0451f94

Tests:
source.test:    Total   31      Passed  31      Skipped 0       Failed  0
sebresplain2012-03-06 21:31:15
A pity that parameter "encodingName" of the function "Tcl_FSEvalFileEx" is not a Tcl_Obj.
dkfplain2012-03-07 20:54:02
What's the problem with “encodingName” being a “const char *”? That's the type of the argument to Tcl_SetChannelOption…
sebresplain2012-03-07 21:09:57
Because of pair Tcl_GetEncoding/Tcl_FreeEncoding
and corresponding part of Tcl_SetChannelOption can be extracted/extended with  such as "Tcl_SetChannelEncoding"/"Tcl_GetChannelEncoding" or something as "Tcl_SetChannelObjOption".

Idea here would be to use the function "Tcl_GetEncodingFromObj"...
I'm optimizer ad infinitum :)
dkfplain2012-03-07 21:34:48
But then we'd need to deal with the problem of how to send a Tcl_Obj through the channel API, and that's an API that may cross thread boundaries (which Tcl_Obj values _must not_ due to the way their memory is managed) and it's going to be hard to make it all work with source potentially getting data out of a VFS.

Find something else to optimize. Something easy.
nijtmansplain2012-03-08 03:37:46
So, now we have 3 attempts in this branch. Thanks!

However, I'm not satisfied with any of them, although
up to now sebres' one was the most clever.

Currently I am thinking about building it in
the channel code: something like a channel
option "-checkbom true", which has the effect
that the channel first waits for the next 3
bytes, and checks if it is the BOM. If so,
it outputs utf-8 BOM and switches
encoding to utf-8, otherwise continue
as before. Advantage: the channel
code has access to the binary
buffer, so doesn't need to do
so much trickery.

Disadvantage: A new channel
option needs a TIP.
nijtmansplain2012-03-08 05:03:48
So, started a 4th attempt, building it in the channel code.

Implemented is the addition of a -checkbom
channel flag (name is open for discussion).
Actual handling of this flag is not implemented
yet, so the source-2.x testcases still fail. A
complete implementation should make those
testcases pass.

Still didn't figure out where the flag handling
should be done, but knowing that the
implementation should not be difficult
any more......
sebresplain2012-03-08 16:15:57
Hi Jan,
you have placed Tcl_SetChannelOption "-checkbom" in else scope of 
if (encodingName != NULL) ...

that means the following code does not ignore BOM from utf-8 file "test.tcl":
source -encoding utf-8 test.tcl
only for this one:
source test.tcl
sebresplain2012-03-08 16:48:42
Have commited revised "source.test"
jan.nijtmans Ticket [3466099fff]: 1 change [History diff]2014-02-21 19:07:10
jan.nijtmansplain2021-03-18 13:44:28
This is fixed now in 8.7, thanks to [https://core.tcl-lang.org/tips/doc/trunk/tip/587.md| TIP #587]. Closing.
Log in (anonymous is fine) to add a comment.