From 861d8f512fd6d21d600999faa12dde6bd363bef0 Mon Sep 17 00:00:00 2001 From: oehhar Date: Sun, 25 Oct 2020 20:03:10 +0000 Subject: TIP586: C String Parsing Support for binary scan --- generic/tclBinary.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/generic/tclBinary.c b/generic/tclBinary.c index f53c707..8a3541b 100644 --- a/generic/tclBinary.c +++ b/generic/tclBinary.c @@ -1518,7 +1518,8 @@ BinaryScanCmd( } switch (cmd) { case 'a': - case 'A': { + case 'A': + case 'C': { unsigned char *src; if (arg >= objc) { @@ -1540,10 +1541,18 @@ BinaryScanCmd( 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; -- cgit v0.12 From 31145f178e965c316eb97dcb24fe34779c6bd50e Mon Sep 17 00:00:00 2001 From: oehhar Date: Mon, 26 Oct 2020 10:53:29 +0000 Subject: Copied man page and test from Androwish https://www.androwish.org/home/ci/bc8b7e8094b66169 --- doc/binary.n | 9 +++++++++ tests/binary.test | 11 ++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/binary.n b/doc/binary.n index 0e8b28e..3ba823b 100644 --- a/doc/binary.n +++ b/doc/binary.n @@ -762,6 +762,15 @@ high-to-low order within each byte. For example, 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" A* 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 diff --git a/tests/binary.test b/tests/binary.test index cf3195f..501ec0d 100644 --- a/tests/binary.test +++ b/tests/binary.test @@ -759,7 +759,16 @@ test binary-21.12 {Tcl_BinaryObjCmd: scan} -setup { } -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} -- cgit v0.12 From 577981fee9f598026571fd4d5c81821ef0d42e9f Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Mon, 26 Oct 2020 14:45:47 +0000 Subject: re-trigger Travis build (and fix some eol-spacing) --- doc/binary.n | 2 +- tests/binary.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/binary.n b/doc/binary.n index 3ba823b..6b2c0eb 100644 --- a/doc/binary.n +++ b/doc/binary.n @@ -770,7 +770,7 @@ and terminates at the first null (C string semantics). For example, \fBbinary scan\fR "abc\e000efghi" A* var1 .CE will return \fB1\fR with \fBabc\fR stored in \fIvar1\fR. -.RE +.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 diff --git a/tests/binary.test b/tests/binary.test index 501ec0d..7433fe8 100644 --- a/tests/binary.test +++ b/tests/binary.test @@ -768,7 +768,7 @@ 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 }} +} -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} -- cgit v0.12 From f145d5f091d380c580c0d4d6e662731d2b8bf1a2 Mon Sep 17 00:00:00 2001 From: oehhar Date: Wed, 28 Oct 2020 06:59:12 +0000 Subject: Corrected doc: modifier C instead A --- doc/binary.n | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/binary.n b/doc/binary.n index 6b2c0eb..9b8b106 100644 --- a/doc/binary.n +++ b/doc/binary.n @@ -767,7 +767,7 @@ 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" A* var1 +\fBbinary scan\fR "abc\e000efghi" C* var1 .CE will return \fB1\fR with \fBabc\fR stored in \fIvar1\fR. .RE -- cgit v0.12