Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Problem with command "string map" in Tcl 8.5

Closed / Invalid3483277fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 14 years ago
Descriptionplain2012-02-02 16:53:41
 Hi
 We are using TCL as an integrated scripting language in our application. But after upgrading TCL version from 8.3 to 8.5 we have started facing problem with the command “string map” below is code where the output of  “string map” is incorrect.
 -------------------------------------------------------

 set a "TEST1°TEST2°TEST3°TEST4°TEST5°A-B-C-D-E-F"
 puts $a
 #content of the variable a : TEST1°TEST2°TEST3°TEST4°TEST5°A-B-C-D-E-F
 set a [string map { "-" ";" } $a]
 puts $a
 #content of the variable a : TEST1ï¾°TEST2ï¾°TEST3ï¾°TEST4ï¾°TEST5ï¾°A;B;C;D;E;F
 -----------------------------------------------------------
 In the output of "string map" degree sign (°) is getting converted into ï¾°. Similar code was working correctly with Tcl version 8.3.

 Thanks
dgpplain2012-02-03 00:50:45
I suspect your script is not in the proper encoding.

How do the commands above reach the interpreter?
Are they stored in a file, and brought in with the [source]
command?  If so, is that file stored in the encoding returned
by the [encoding system] command?  If not, it's your job
to tell [source] your script file is not in the default encoding
by passing the proper -encoding value.

The alternative, which is the more common approach, is to
write your script files using only ASCII characters, and use
\uHHHH substitution to encode any literal characters in your
script outside that character range.
dgpplain2012-02-03 00:56:47
Reading again, you say you use Tcl as an "integrated
scripting language" which suggests the commands above
may be reaching the interpreter via a call into the Tcl library
like

    Tcl_Eval(interp, script);

or any of the other routines in the Tcl*Eval* family.

In that case it is the responsibility of the Tcl_*Eval*
caller to pass in the (char *) argument "script" a pointer
to a character buffer holding your script in Tcl's internal
encoding, which is a variant of utf-8.  The Tcl routine
Tcl_ExternalToUtfDString() is usually a useful tool in getting
that done properly.
dalkunteplain2012-02-03 13:20:58
Hi Don,
Thanks for your update.

We are using TCL_EvalFile to evaluate the contents of the TCl script as below.

Tcl_EvalFile( interp, full_path_tcl_script)

 File which is passed as a parameter to this Tcl_EvalFile is a ascii file.  When you say “it is the responsibility of the Tcl_*Eval* caller to pass in the (char *) argument "script" a pointer to a character buffer holding your script in Tcl's internal encoding, which is a variant of utf-8.”
Does that mean the file which is passed to TCL_EvalFile function should be in utf8 format? Please correct me if I am wrong.

Also I am not able to understand why this was working correctly with Tcl version 8.3 and not working with Tcl version 8.5? Are there any encoding related changes done in Tcl version 8.5.  

Thanks
sebresplain2012-02-03 16:52:22
the Tcl_EvalFile reads the file in current system encoding of given interpreter:
also if you "encoding system" returns here as example "cp1252" - your source file should be in "cp1252". 

The multi byte <-> single byte (auto) translation can be a little bit different between 8.5 and 8.3. But i could not reproduce it, without more information, such platform, system encoding, file encoding, "init.tcl" loaded (or not), etc. 

Otherwise, read the file in the "file encoding" with "Tcl_FSEvalFileEx": 

Tcl_Obj *pathPtr = Tcl_NewStringObj("filename.tcl", -1);
Tcl_IncrRefCount(pathPtr);
ret = Tcl_FSEvalFileEx(interp, pathPtr, encodingName);
Tcl_DecrRefCount(pathPtr);

or by using of pure tcl:

source -encoding "utf-8" "filename.tcl"
dgpplain2012-02-08 04:36:32
What does [encoding system] return in your
integrated scripting environment?

My guess is that it returns "identity".

This would indicate your program is
failing to call Tcl_FindExecutable().
True?

Somewhere in the startup of your program
add the call 

    Tcl_FindExecutable(NULL);

(or, if you have the command line arguments
 of your program in an argv[] array, you can
 call

    Tcl_FindExecutable(argv[0]);

)

Then see if that makes your symptoms go away.
dgp Pending ticket [3483277fff]. [History diff]2012-04-19 23:08:29
dgpplain2012-06-05 21:00:44
allow_comments - 1
Log in (anonymous is fine) to add a comment.