Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-10-14
11:25 • Ticket [f2f70aa60c] regsub -all misses matches in Tcl 8.6 status still Close... artifact: 44aece65b2 user: gustafn2
09:00 • Closed ticket [f2f70aa60c]. artifact: 247284fd10 user: dkf
07:37 • New ticket [f2f70aa60c]. artifact: ef29f025bb user: gustafn2

regsub -all misses matches in Tcl 8.6

Closed / Rejectedf2f70aa60c0844cd9ebcaabb630e5ea64325db90Created 10 years agoUpdated 10 years ago
Descriptionplain2016-10-14 07:37:36
"regsub -all" returns different results in tcl 8.5 and tcl 8.6 The example below was condensed out of a complex example in openacs, where one function uses special characters as "markers" in regsub. When \x001 and \x002 are used as markers, not all of these markers can be changed back with tcl 8.6, where it succeeds with tcl 8.5. When \u0001 and \u0002 are used instead, the behavior is correct in all cases. It seems as if the change was introduced in early Tcl 8.6, since the problem is contained already in tcl 8.6.0.

===================================================
set txt {
    <img src="http://openacs.org/place1.png">
    <img src="http://openacs.org/place2.png">
}

# broken in 8.6.0 .. 8.6.6, ok in 8.5.19
set changed [regsub -all {(http://[^\(\)\"<>\s]+)} $txt "\x001\\1\x002" txt1]
set changed_back [regsub -all {\x001([^\x002]*)\x002} $txt1 {\1} txt2]
puts stderr "$tcl_patchLevel: changed $changed changed_back $changed_back"

# ok in 8.6.0 .. 8.6.6, 8.5.19
set changed [regsub -all {(http://[^\(\)\"<>\s]+)} $txt "\u0001\\1\u0002" txt1]
set changed_back [regsub -all {\u0001([^\u0002]*)\u0002} $txt1 {\1} txt2]
puts stderr "$tcl_patchLevel: changed $changed changed_back $changed_back"

# 1st case broken:
#     8.6.6: changed 2 changed_back 1
#     8.6.6: changed 2 changed_back 2
#
# OK: 
#     8.5.19: changed 2 changed_back 2
#     8.5.19: changed 2 changed_back 2
dkfwiki2016-10-14 09:00:23

This was the change to the \xXX escape in Tcl itself. It was always documented to be followed by exactly two hex digits, but was in practice followed incorrectly by an arbitrary number of hex digits of which all but the last two were thrown away. This meant (for example) that \x44AZ expanded to JZ and not to DAZ. This was horribly wrong, and was fixed in 8.6 to conform to the specification. See TIP #388 for the details.

The workaround is to use the \uXXXX form as that was always (correctly) consuming exactly 4 hex digits.

gustafn2plain2016-10-14 11:25:59
many thanks for the quick reply. i can confirm that the 2-hex-digit variant is correct:

  set changed [regsub -all {(http://[^\(\)\"<>\s]+)} $txt "\x01\\1\x02" txt1]
  set changed_back [regsub -all {\x01([^\x02]*)\x02} $txt1 {\1} txt2]
  puts stderr "$tcl_patchLevel: changed $changed changed_back $changed_back"

its my fault, i have not spotted the 3-hex-digit at the first place, the change is documented in http://wiki.tcl.tk/21276.

i am still not sure, why the regsub works sometimes and sometimes no (in the original case 7x ok, 1x not ok)... but i am not sure if i want to know this.
Log in (anonymous is fine) to add a comment.