Artifacts Associated With Ticket ded7528bffe8bdf4
Ticket change [731ae5f5ce] (rid 136896) by ericp on 2014-02-16 01:38:06:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
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? - foundin initialized to: "8.6"
- is_private initialized to: "0"
- login: "ericp"
- priority initialized to: "5 Medium"
- private_contact initialized to: "6d1053e9208f1074fd485c15cc0d7e49e190bac5"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "ericp"
- subsystem initialized to: "18. Commands M-Z"
- title initialized to: "subst/regsub/format/scan gets confused by braces"
- type initialized to: "Bug"
Ticket change [9b78079142] (rid 136897) by dgp on 2014-02-16 03:55:36:
- icomment:
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? - login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [c5e33e0963] (rid 136898) by dgp on 2014-02-16 04:02:44:
- icomment:
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 - login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [2796553d17] (rid 137513) by dgp on 2014-04-04 18:14:42:
- assignee changed to: "dgp"
- closedate changed to: "2456752.26021431"
- closer changed to: "dgp"
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "Invalid"
- status changed to: "Closed"