"puts" does not work with non UTF-8 strings
Closed / Fixed218434ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:57:33
OriginalBugID: 6096 Bug
Version: 8.2.1
SubmitDate: '2000-08-09'
LastModified: '2000-08-10'
Severity: SER
Status: Closed
Submitter: techsupp
ChangedBy: ericm
RelatedBugIDs: 3345
OS: Digital Unix
OSVersion: Tru64 UNIX Version 5.1
Machine: Digital Personal WorkStation 500au (Alpha)
FixedDate: '2000-10-25'
ClosedDate: '2000-08-10'
Name:
Rigoberto Corujo
Extensions:
TclX 8.1
ReproducibleScript:
When running Extended Tcl on Tru64 UNIX with a dechanzi Chinese locale, the output of the following command is not properly displayed. That is, some of the date output contains "?" characters.
$ tcl
tcl>puts "[fmtclock [getclock]]"
This is because the output of "fmtclock" is not UTF-8 encoded, but "puts", or rather the "WriteChars()" routine that implements the "puts" command, expects the string it is processing to be UTF-8 encoded. Running the following command produces the correct output, however, which means that "puts" needs to be fixed.
$ tcl
tcl>fmtclock [getclock]
The "WriteChars()" routine ("./tcl8.2/generic/tclIO.c") should accept strings that are UTF-8 encoded and strings that are locally encoded. If the string is already locally encoded, then it should not try to do the UTF-8 to local encoding.
ObservedBehavior:
Some question marks ("?") are displayed in the date string that is output.
DesiredBehavior:
The output of
tcl>puts "[fmtclock [getclock]]"
should be the same as
tcl>fmtclock [getclock]
Since the output is in Chinese I will not show it here as it will likely not display properly on a browser.
Patch:
One of our I18N engineers provided the following patch as a workaround, but cautioned that a more comprehensive solution is needed which requires knoUTF-8 and non-UTF-8 strings get passed among the different modules.
$ diff -c tclIO.c.old tclIO.c.new > tclIO.c.patch
$ cat tclIO.c.patch
*** tclIO.c.old Wed Aug 2 08:43:52 2000
--- tclIO.c.new Wed Aug 2 08:43:57 2000
***************
*** 2984,2990 ****
--- 2984,3001 ----
int dstWrote, dstLen, stageLen, stageMax, stageRead;
Tcl_Encoding encoding;
char safe[BUFFER_PADDING];
+ #ifndef NO_DEC_I18N
+ extern int Tcl_IsWellFormUtf8(uchar_t *, int) ;
+ /*
+ * QAR 81435
+ * Assume string is in native encoding if it is not a well-form
+ * UTF-8 sequence.
+ */
+ if (!Tcl_IsWellFormUtf8((uchar_t *)src, srcLen))
+ return(WriteBytes(chanPtr, src, srcLen)) ;
+ #endif
+
total = 0;
sawLF = 0;
savedLF = 0;
$ diff -c tclUtf.c.old tclUtf.c.new > tclIO.c.patch
$ cat tclIO.c.patch
*** tclUtf.c.old Wed Aug 2 08:45:31 2000
--- tclUtf.c.new Wed Aug 2 08:45:38 2000
***************
*** 1581,1583 ****
--- 1581,1621 ----
return (((ALPHA_BITS | DIGIT_BITS | CONNECTOR_BITS) >> category) & 1);
}
+
+
+ #ifndef NO_DEC_I18N
+ /*
+ *----------------------------------------------------------------------
+ *
+ * Tcl_IsWellFormUtf8
+ *
+ * Test if a string is a well-form UTF-8 sequence.
+ *
+ * Results:
+ * Returns 1 if string is a well-form UTF-8 sequence.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+ #define TCL_VALID_UTF8_NONFIRSTBYTE(b) (((b) & 0xc0) == 0x80)
+ int
+ Tcl_IsWellFormUtf8(uchar_t *str, int length)
+ {
+ int ch, len ;
+
+ while (length-- > 0)
+ {
+ ch = *str++ ;
+ if (isascii(ch))
+ continue ;
+ if ((len = totalBytes[ch] - 1) <= 0)
+ return(0) ; /* Invalid UTF-8 sequence */
+ while (len-- > 0)
+ if (!TCL_VALID_UTF8_NONFIRSTBYTE(*str++))
+ return(0) ;
+ }
+ return(1) ; /* Well-form UTF-8 sequence */
+ }
+ #endif
PatchFiles:
./tcl8.2/generic/tclIO.c
./tcl8.2/generic/tclUtf.c
As was discussed in the newsgroup, [puts] should not have to handle non-UTF-8 strings, as it is part of the "API contract" that WriteChars is given UTF-8 strings.
So it is fair to say that the problem should be solved in [fmtclock]; however, that doesn't really give the whole story. Since TclX 7.5, [fmtclock] has simply been an alias to [clock format]. There was a bug in [clock format] that caused it to return non-UTF-8 strings, which only showed up when used in locales like the one you used. This bug was corrected for 8.4a1, and the fix was backported for 8.3.2. I suggest that you upgrade your Tcl installation to that version.
Eric Melski The Other Tcl Guy
ericm at ajubasolutions.com Ajuba Solutions
-- 08/10/2000 ericm
welch Closed ticket [218434ffff].
[History diff]2000-10-26 08:22:15
dgp Ticket [218434ffff]: 4 changes
[History diff]2001-04-19 04:15:11
Log in (anonymous is fine) to add a comment.