|
2014-04-04
| ||
| 18:14 | • Closed ticket [ded7528bff]: subst/regsub/format/scan gets confused by braces... artifact: 2796553d17 user: dgp | |
|
2014-02-16
| ||
| 04:02 | • Ticket [ded7528bff]: 3 changes artifact: c5e33e0963 user: dgp | |
| 03:55 | • Ticket [ded7528bff]: 3 changes artifact: 9b78079142 user: dgp | |
| 01:38 | • New ticket [ded7528bff]. artifact: 731ae5f5ce user: ericp | |
subst/regsub/format/scan gets confused by braces
Closed / Invalidded7528bffe8bdf4cf5ab9f327b50b207777378eCreated 13 years agoUpdated 13 years ago
Descriptionplain2014-02-16 01:38:06
I was trying to write a function that would urlencode a string, with \uxxxx sequences. http://rosettacode.org/wiki/JSON#Tcl led me to trying this code:
set escaped1 [subst \
[regsub -all {[\[\]\$\u0000-\u001f\u0080-\uffff]} \
$value2 {[format "\\\\u%04x" [scan {&} %c]]}]]
which I thought worked. However, it seems to drop characters when
braces are involved:
% set value {w1 on the <blah abc {} def > form}
w1 on the <blah abc {} def > form
% set value1 [string map [list "@" "<@@>"] $value]
w1 on the <blah abc {} def > form
% set value2 [string map [list "\\" "<@>"] $value1]
w1 on the <blah abc {} def > form
% set escaped1 [subst [regsub -all {[\[\]\\\{\\\}\$\u0000-\u001f\u0080-\uffff]}
$value2 {[format "\\\\u%04x" [scan {&} %c]]}]]
w1 on the <blah abc \u007b def > form
The close-brace should appear as \u007d, but it's missing completely.
Bug, or is there better usage?
dgpplain2014-02-16 03:55:36
There's no bug in Tcl demonstrated here.
It's quite likely you are demonstrating buggy attempts
to accomplish whatever it is you are trying to do.
It helps not to obscure the intermediate results, or to
include pointless preliminaries. Examine this:
% set a {w1 on the <blah abc {} def > form}
w1 on the <blah abc {} def > form
% set b [regsub -all {[\[\]\\\{\\\}\$\u0000-\u001f\u0080-\uffff]} $a {[format "\\\\u%04x" [scan {&} %c]]}]
w1 on the <blah abc [format "\\u%04x" [scan {{} %c]][format "\\u%04x" [scan {}} %c]] def > form
% set c [subst $b]
w1 on the <blah abc \u007b def > form
Take a good look at the value of $b and think through
how [subst] is going to parse the brace quoted words in
the command substitutions found within it. You likely
were not expecting the command
scan {{} %c]][format "\\u%04x" [scan {}} %c
to be part of it, but that's what happened. (That's
the [scan] command with only 2 arguments)
Just as a aside, whenever you see a Tcl programmer trying
to perfect string manipulation techniques to omit or disguise
braces, it's a very good bet they are mis-using lists somewhere.
Frequently it involves mistaking the appearance of the list
itself with the appearance of the elements it comprises.
What is the real problem to be solved when you get into trouble?
dgpplain2014-02-16 04:02:44
All that said, if you just want to replace the open brace
with the string \u007b and the close brace with the string \u007d
then the tool to use is [string map]
% set s {foo {} bar}
foo {} bar
% set result [string map [list "{" {\u007b} "}" {\u007d}] $s]
foo \u007b\u007d bar
dgp Closed ticket [ded7528bff].
[History diff]2014-04-04 18:14:42
Log in (anonymous is fine) to add a comment.