Skip to content
Tcl Source Code View Ticket
Login

View Ticket

scan %i

Closed / Rejectedcd29ba23cc373c087f14305c3a3135d797ac4ac5Created 10 years agoUpdated 42 days ago
Descriptionwiki2016-10-05 07:32:18
According to the documentation, %i scans an integer with the base (i.e. decimal, binary, octal, or hexadecimal) determined in the same fashion as described in expr.

So one can expect:

  scan 0b10 %i v ;# v = 2 expected, but is 0
  scan 0o77 %i v ;# v = 63 expected, but is 0

Also note that:

  scan 0xf %i v ;# v = 15
  scan 077 %i v ;# v = 63

The test scan-4.41 in scan.test, line 337 is also buggy:

test scan-4.41 {Tcl_ScanObjCmd, base-unknown integer scanning} -setup {
    set x {}
} -body {
    list [scan {10 010 0x10 0b10} {%i%i%i%i} x y z t] $x $y $z $t
} -result {4 10 8 16 0}

The flag TCL_PARSE_SCAN_PREFIXES set in tclScan.c for %i is documented as ignore prefixes in tclStrToD.c.

dkf Ticket [cd29ba23cc]: 4 changes [History diff]2016-10-08 16:54:03
ferrieuxplain2016-10-09 08:41:36
Base-two is a rather late addition on Tcl (TIP 343, 2008).
As such, extreme care is due wrt backward compatibility.
In the course of the discussion around TIP 343, the risk was deemed too high of breaking scripts expecting "b" to be seen as an integer-breaker (as it had been for over 15 years before the TIP). Add to this the extreme rarity of base-two as a natural choice for input when %i is used (when it is the most natural choice, the code may be expected to explicitly [scan "0b%b"]).

Now if you're proposing this for Tcl9, where incompatibility is expected, please say so.
bericplain2016-10-09 09:24:54
So we have to live with this. Sad.
Then I think the documentation should be changed to say the truth: %i only scans decimal, octal with "0" prefix, and hexadecimal ("0x" prefix) but not octal with "0o" nor binary with "0b" prefix.
Could we add another % scan char?
ferrieuxplain2016-10-09 10:37:36
You're right about the documentation; fixed in commit b6c6a73a68dc1526.

As for the scan chars, there's already %b; what do you have in mind ?
bericplain2016-10-09 11:27:37
I mean, a new char, say %I, which does what was documented, scanning any integer format, including 0o and 0b.
What I do instead of scan now is:

if {[string is integer -strict $v]} {
    set v [expr {$v}]
} else {  ...
ferrieuxplain2016-10-09 14:49:09
If you feel your need is so ubiquitous it deserves the code bloat, submit a TIP.
serhiy.storchakawiki2026-10-15 11:29:05

Closing: decided above.

serhiy.storchaka Ticket [cd29ba23cc]: 3 changes [History diff]2026-10-22 17:19:26
Log in (anonymous is fine) to add a comment.