Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-29
05:22
Merge [3d5e024e64e79fcf]: Fix [9c258a841a]: document %t/%z/%j/%q size field specifiers. Fix %j/%z/%... check-in: 0780a92568 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-05-31
09:39 • Ticket [9c258a841a] format - undocumented type specifiers + possible bugs st... artifact: 7a6db21a10 user: jan.nijtmans
2024-05-30
22:02
Fix [9c258a841a]: document %t/%z/%j/%q size field specifiers. Fix %j/%z/%t matching the C printf check-in: 3d5e024e64 user: jan.nijtmans tags: trunk, main
21:52
Fix [9c258a841a]: document %t/%z/%j/%q size field specifiers. Fix %j/%z/%t matching the C printf check-in: 9c0c9d1a4a user: jan.nijtmans tags: core-8-branch
21:26
Let's [9c258a841a|fix] the j%j/%q type specifiers, so they behave like C in scripts, and document th... closed check-in: 0c66ee7d75 user: jan.nijtmans tags: bug-9c258a841a
03:23 • Ticket [9c258a841a] format - undocumented type specifiers + possible bugs st... artifact: 68a9603a63 user: apnadkarni
2024-05-29
15:47 • Ticket [9c258a841a]: 3 changes artifact: 3734b841b1 user: apnadkarni
15:20
Let's [9c258a841a|fix] the %p/%z/%t type specifiers, so they behave like C in scripts, and document ... check-in: 2d6520b382 user: jan.nijtmans tags: bug-9c258a841a
12:00 • Ticket [9c258a841a] format - undocumented type specifiers + possible bugs st... artifact: 4068aaff7e user: jan.nijtmans
08:41 • Ticket [9c258a841a]: 3 changes artifact: 917e68dc0c user: jan.nijtmans
07:58 • Ticket [9c258a841a]: 3 changes artifact: 71735e3169 user: jan.nijtmans
03:52 • Ticket [9c258a841a]: 3 changes artifact: 379892be57 user: apnadkarni
02:40 • Ticket [9c258a841a]: 4 changes artifact: 0e659ee8d9 user: apnadkarni
2024-05-28
15:56 • Ticket [9c258a841a]: 3 changes artifact: aa53f237bb user: apnadkarni
12:06 • New ticket [9c258a841a]. artifact: 5115c07f37 user: apnadkarni

format - undocumented type specifiers + possible bugs

Open9c258a841a03860f24e52283b650c7823da2e8e6Created 3 years agoUpdated 3 years ago
Descriptionmarkdown2024-05-28 12:06:56

Subsequent to TIP 476, the format command accepts new size field specifiers t,z,j,q,I,I32,I64. These are not documented in the manpage.

Additionally, I think the "j" "z" and "t" specifiers do not match the C printf (assuming that is the intent). They are supposed to be sizeof intmax_t, size_t and ptrdiff_t respectively but in format they do not seem to truncate to fit those sizes (unlike "l" for example). Is that intentional?

Referenced by5
  • 0780a92568Merge [3d5e024e64e79fcf]: Fix [9c258a841a]: document %t/%z/%j/%q size field specifiers. Fix %j/%z/%t matching the C printf. check-in2024-08-29 05:22:45
  • 3d5e024e64Fix [9c258a841a]: document %t/%z/%j/%q size field specifiers. Fix %j/%z/%t matching the C printf check-in2024-05-30 22:02:14
  • 9c0c9d1a4aFix [9c258a841a]: document %t/%z/%j/%q size field specifiers. Fix %j/%z/%t matching the C printf check-in2024-05-30 21:52:02
  • 0c66ee7d75Let's [9c258a841a|fix] the j%j/%q type specifiers, so they behave like C in scripts, and document them check-in2024-05-30 21:26:39
  • 2d6520b382Let's [9c258a841a|fix] the %p/%z/%t type specifiers, so they behave like C in scripts, and document them check-in2024-05-29 15:20:31
apnadkarnimarkdown2024-05-28 15:56:39

Based on the output below, I think j, t, z are not what I would expect as per their C printf definition (assuming that is the goal). They should truncate values that do not fit in intmax_t, ptrdiff_t, size_t to the appropriate size (32 or 64-bit depending on architecture). I would certainly find that useful in FFI type extensions.

% set int64 0x8000000000000001
0x8000000000000001
% set int65 0x10000000000000000
0x10000000000000000
% foreach t {d u} {foreach s {{} j l ll t z I I32 I64} {puts "$s$t\t[format %$s$t $::int64] [format %$s$t $::int65]"}}
d       1 0
jd      9223372036854775809 18446744073709551616
ld      -9223372036854775807 0
lld     9223372036854775809 18446744073709551616
td      9223372036854775809 18446744073709551616
zd      9223372036854775809 18446744073709551616
Id      1 0
I32d    1 0
I64d    -9223372036854775807 0
u       1 0
ju      9223372036854775809 18446744073709551616
lu      9223372036854775809 0
llu     9223372036854775809 18446744073709551616
tu      9223372036854775809 18446744073709551616
zu      9223372036854775809 18446744073709551616
Iu      1 0
I32u    1 0
I64u    9223372036854775809 0
apnadkarnimarkdown2024-05-29 02:40:01

A further nit, %a and %A differ between Windows and Linux (or probably VC vs gcc)

Windows:

% format %A 16
0x1.0000000000000p+4

Linux:

% format %a 16
0x1p+4

Format manpage says default precision is 13 so the Windows version appears correct.

apnadkarnimarkdown2024-05-29 03:52:09

Another inconsistency - manpage says

If neither h nor l nor L are present, the integer value is truncated to the same range as that produced by the int() function of the expr command (at least a 32-bit range, but determined by the value of the wordSize element of the tcl_platform array)

However, on Windows above does not hold

% set x 0x7ffffffff
0x7ffffffff
% format %x $x
ffffffff
% format %llx [expr int($x)]
7ffffffff

The mathfunc manpage is also inconsistent with this behavior

The argument may be any numeric value. The integer part of arg is determined, and then the low order bits of that integer value up to the machine word size are returned as an integer value. For reference, the number of bytes in the machine word are stored in the wordSize element of the tcl_platform array.

I can start on fixing up these issues but I am not sure what the behavior is supposed to be. Are j, z, t supposed to match C? Is format %d supposed to match int() ? Is int() supposed to match tcl_platform(wordSize)? Which part of mismatches is to be fixed?...

jan.nijtmanswiki2024-05-29 07:58:52

> Are j, z, t supposed to match C?

Those specifiers are not documented for "format" on purpose: They are meant to be used from C (Tcl_ObjPrintf), not at script level. So, if someone uses it from a Tcl script, any behavior is fine, as long as Tcl_ObjPrintf() continues working as it does now.

> Is format %d supposed to match int() ?

No, see TIP #514

> Is int() supposed to match tcl_platform(wordSize)?

No, see TIP #514

jan.nijtmansplain2024-05-29 08:41:35
> If neither h nor l nor L are present, the integer value is truncated to the same range .....

I think this sentence is wrong: If we want to have C behavior, %d should be 32-bit always. Doesn't Tcl 8.6 do that too?

Also the documentation for %p is wrong: It refers to the "z" specifier, which is not documented .....
jan.nijtmanswiki2024-05-29 12:00:34

Thinking more about it, %z and %t should be documented and match C. Further on, %d without any specifier should truncate for 32-bit always, also matching C.

I see a TIP coming ....

apnadkarniplain2024-05-29 15:47:33
I agree all accepted specifiers, modifiers etc. should be documented. I am not sure a TIP is needed though I suppose no harm. TIP 476 already covers them I thought except it's missing some detail about semantics. The presumption would be that unless stated otherwise, they should produce the same result as C99 printf.
apnadkarniplain2024-05-30 03:23:43
I added experimental tests in a bug-9c258a841a-tests branch which show some differences w.r.t C99 (not necessarily bugs since some are definitely documented). More to flush out any differences and resolve as bugs or works as designed. Run make test with "-f format.test -match *c99*"
jan.nijtmanswiki2024-05-31 09:39:27

I think the documentation problems are corrected now, and the behavior of "j"/"z"/"t" as well. Thanks for the testcases!

Regarding 'hp', it looks like the c99 printf simply ignores the 'h' while we handle it as expected (however useless that is ....). I don't think we should do anything about it.

On %a/%A, I think both Windows and UNIX output look OK, it's not clear from the documentation whether ending '0' digits may be removed or not, that's the difference. I don't think it's important enough to do anything about it.

Regarding 'I'/'I32'/'I64', can you agree on leaving it undocumented but only make it available on Windows? See: [01e6a242c4fc275c]. It's actually only intended for Tcl_ObjPrintf() on Windows, it doesn't make sense for 'format'.

Log in (anonymous is fine) to add a comment.