Index: doc/binary.n ================================================================== --- doc/binary.n +++ doc/binary.n @@ -760,10 +760,19 @@ .CE .PP will return \fB2\fR with \fB01110\fR stored in \fIvar1\fR and \fB1000011100000101\fR stored in \fIvar2\fR. .RE +.IP \fBC\fR 5 +This form is similar to \fBA\fR, except that it scans the data from start +and terminates at the first null (C string semantics). For example, +.RS +.CS +\fBbinary scan\fR "abc\e000efghi" C* var1 +.CE +will return \fB1\fR with \fBabc\fR stored in \fIvar1\fR. +.RE .IP \fBH\fR 5 The data is turned into a string of \fIcount\fR hexadecimal digits in high-to-low order represented as a sequence of characters in the set .QW 0123456789abcdef . The data bytes are scanned in first to last Index: generic/tclBinary.c ================================================================== --- generic/tclBinary.c +++ generic/tclBinary.c @@ -1516,11 +1516,12 @@ if (!GetFormatSpec(&format, &cmd, &count, &flags)) { goto done; } switch (cmd) { case 'a': - case 'A': { + case 'A': + case 'C': { unsigned char *src; if (arg >= objc) { DeleteScanNumberCache(numberCachePtr); goto badIndex; @@ -1538,14 +1539,22 @@ src = buffer + offset; size = count; /* - * Trim trailing nulls and spaces, if necessary. + * Apply C string semantics or trim trailing + * nulls and spaces, if necessary. */ - if (cmd == 'A') { + if (cmd == 'C') { + for (i = 0; i < size; i++) { + if (src[i] == '\0') { + size = i; + break; + } + } + } else if (cmd == 'A') { while (size > 0) { if (src[size - 1] != '\0' && src[size - 1] != ' ') { break; } size--; Index: tests/binary.test ================================================================== --- tests/binary.test +++ tests/binary.test @@ -757,11 +757,20 @@ test binary-21.12 {Tcl_BinaryObjCmd: scan} -setup { unset -nocomplain arg1 } -body { list [binary scan "abc def \x00ghi " A* arg1] $arg1 } -result [list 1 "abc def \x00ghi"] - +test binary-21.13 {Tcl_BinaryObjCmd: scan} -setup { + unset -nocomplain arg1 +} -body { + list [binary scan "abc def \x00 " C* arg1] $arg1 +} -result {1 {abc def }} +test binary-21.12 {Tcl_BinaryObjCmd: scan} -setup { + unset -nocomplain arg1 +} -body { + list [binary scan "abc def \x00ghi" C* arg1] $arg1 +} -result {1 {abc def }} test binary-22.1 {Tcl_BinaryObjCmd: scan} -returnCodes error -body { binary scan abc b } -result {not enough arguments for all format specifiers} test binary-22.2 {Tcl_BinaryObjCmd: scan} { unset -nocomplain arg1