Overview
| Artifact ID: | e2edab7807fad9ecad536ab9cf4c8494d3aedb6615716a66f2174ed2b1f3e8e4 |
|---|---|
| Ticket: | 535705ffffffffffffffffffffffffffffffffff
Tcl interface to stop on encoding errors |
| User & Date: | oehhar on 2021-03-31 19:28:53 |
Changes
- icomment:
Dear Jan, first of all, thank you for the implementation. That is great. Here is an example with two two-byte utf-8 sequences, where the last byte is not transmitted: <verbatim> % catch {encoding convertfrom utf-8 [string range [encoding convertto utf-8 ÄÖ] 0 end-1] -stoponerror} e d 1 % set d -code 1 -level 0 -errorstack {INNER {invokeStk1 ::tcl::encoding::convertfrom utf-8 ÃÃ -stoponerror}} -errorcode NONE -errorinfo {unexpected byte at index 2: 'Ã' (\xC3) while executing "encoding convertfrom utf-8 [string range [encoding convertto utf-8 ÄÖ] 0 end-1] -stoponerror"} -errorline 1 </verbatim> This is great. I would prefer to return the position index (2) as part of the error code to make it machine parsable without parsing an error message. <B>Example error code proposal</B>: <I>[list ENCODING STOPONERROR $errorPosition]</I> So, the following code would be possible without string parsing on the error message: <verbatim> proc receive {data} { # This is an eventual remainder of the last run global unprocessedData # Append current data chunk to last remainder append unprocessedData $data try { set dataUTF8 [encoding convertfrom utf-8 $unprocessedData -stoponerror] set unprocessedData "" } trap {ENCODING STOPONERROR} {errorMessage errorDict} { # Partial receive set errorPosition [lindex [dict get $errorDict -errorcode] 2] set dataUTF8 [encoding convertfrom utf-8 [string range $unprocessedData 0 $errorPosition-1] set unprocessedData [string range $unprocessedData $errorPosition end] } on error { # handle other errors } # process dataUTF8 } </verbatim> The idea is a receiving routine receiving binary data which is an utf-8 stream, but the received limits may not match. The receive procedure is called for each received data package and manages the assembly to a valid utf-8 string without loosing or changing data (both possible without flag -stoponerror). Thank you again, I appreciate, Harald <B>Note 1</B>: Other interfaces are possible, specially that the already encoded substring until the error position may be available somehow, so it is not necessary to do a 2nd call to "encoding convertfrom". But that is a detail. <B>Note 2</B>: I did not check for a possible possix error code which might be more appropriate. The error code list is just an example. <B>Note 3</B>: I don't see the need to give the error byte value in the error message. For me, the error position in the source string in the error code would just do the job. IMHO this is for automatic processing, not for radable text. <B>Note 4</B>: As readable error message, I would prefer to mention the used encoding to have something like: "data not in 'utf-8' format". <B>Note 5</B>: For the records: Test suite results with MS-VC2015 32 bit on MS-Win10 64bit GER, compared to tcl8.7a3: Still failing tests: socket-14.11.0 - login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"