diff options
author | stanton <stanton> | 1998-11-18 04:15:44 (GMT) |
---|---|---|
committer | stanton <stanton> | 1998-11-18 04:15:44 (GMT) |
commit | 07651b136a461e4b6e1a77afa6c5e239ba53bb73 (patch) | |
tree | 708225bada5ddd54a27552822192f9f05e1b8bd7 | |
parent | 3a2bd5677129efab7771cdabea4575eb4f7e4f64 (diff) | |
download | tcl-07651b136a461e4b6e1a77afa6c5e239ba53bb73.zip tcl-07651b136a461e4b6e1a77afa6c5e239ba53bb73.tar.gz tcl-07651b136a461e4b6e1a77afa6c5e239ba53bb73.tar.bz2 |
* tclScan.c: moved "scan" implementation out of tclCmdMZ.c and
added Unicode support. This required a complete reimplementation
of the command to avoid using scanf(), which isn't Unicode aware.
Two new features were added in the process: %n to return the
current number of characters consumed, and XPG3-style %n$ argument
order specifiers similar to those provided by the "format"
command. [Bug: 833]
-rw-r--r-- | changes | 9 | ||||
-rw-r--r-- | doc/scan.n | 68 | ||||
-rw-r--r-- | generic/ChangeLog | 56 | ||||
-rw-r--r-- | generic/tclScan.c | 55 | ||||
-rw-r--r-- | tests/scan.test | 416 |
5 files changed, 507 insertions, 97 deletions
@@ -1,6 +1,6 @@ Recent user-visible changes to Tcl: -RCS: @(#) $Id: changes,v 1.1.2.10 1998/11/11 04:08:12 stanton Exp $ +RCS: @(#) $Id: changes,v 1.1.2.11 1998/11/18 04:15:44 stanton Exp $ 1. No more [command1] [command2] construct for grouping multiple commands on a single command line. @@ -3988,3 +3988,10 @@ in %s format strings. (stanton) ("-line", "-lineanchor", and "-linestop") that control how regular expressions treat line breaks. See the regexp manual entry for more details. (stanton) + +11/17/98 (bug fix) "scan" now correctly handles Unicode +characters. (stanton) + +11/17/98 (new feature) "scan" now supports XPG3 position specifiers +and the "%n" conversion character. See the "scan" manual entry for +more details. (stanton) @@ -5,7 +5,7 @@ '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" -'\" RCS: @(#) $Id: scan.n,v 1.1.2.1 1998/09/24 23:58:36 stanton Exp $ +'\" RCS: @(#) $Id: scan.n,v 1.1.2.2 1998/11/18 04:15:46 stanton Exp $ '\" .so man.macros .TH scan n "" Tcl "Tcl Built-In Commands" @@ -31,25 +31,41 @@ and assigned to the corresponding variable. .SH "DETAILS ON SCANNING" .PP -\fBScan\fR operates by scanning \fIstring\fR and \fIformatString\fR together. -If the next character in \fIformatString\fR is a blank or tab then it +\fBScan\fR operates by scanning \fIstring\fR and \fIformat\fR together. +If the next character in \fIformat\fR is a blank or tab then it matches any number of white space characters in \fIstring\fR (including zero). Otherwise, if it isn't a \fB%\fR character then it must match the next character of \fIstring\fR. -When a \fB%\fR is encountered in \fIformatString\fR, it indicates +When a \fB%\fR is encountered in \fIformat\fR, it indicates the start of a conversion specifier. -A conversion specifier contains three fields after the \fB%\fR: +A conversion specifier contains up to four fields after the \fB%\fR: a \fB*\fR, which indicates that the converted value is to be discarded -instead of assigned to a variable; a number indicating a maximum field -width; and a conversion character. +.VS 8.1 +instead of assigned to a variable; a XPG3 position specifier; a number +.VE 8.1 +indicating a maximum field width; and a conversion character. All of these fields are optional except for the conversion character. +The fields that are present must appear in the order given above. .PP -When \fBscan\fR finds a conversion specifier in \fIformatString\fR, it -first skips any white-space characters in \fIstring\fR. +When \fBscan\fR finds a conversion specifier in \fIformat\fR, it +first skips any white-space characters in \fIstring\fR (unless the +specifier is \fB[\fR or \fBc\fR). Then it converts the next input characters according to the conversion specifier and stores the result in the variable given by the next argument to \fBscan\fR. +.VS 8.1 +.PP +If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in +``\fB%2$d\fR'', then the variable to use is not taken from the next +sequential argument. Instead, it is taken from the argument indicated +by the number, where 1 corresponds to the first \fIvarName\fR. If +there are any positional specifiers in \fIformat\fR then all of the +specifiers must be positional. Every \fIvarName\fR on the argument +list must correspond to exactly one conversion specifier or an error +is generated. +.VE 8.1 +.PP The following conversion characters are supported: .TP 10 \fBd\fR @@ -63,6 +79,17 @@ value is stored in the variable as a decimal string. \fBx\fR The input field must be a hexadecimal integer. It is read in and the value is stored in the variable as a decimal string. +.VS 8.1 +.TP 10 +\fBu\fR +The input field must be a decimal integer. The value is stored in the +variable as an unsigned decimal integer string. +.TP 10 +\fBi\fR +The input field must be an integer. The base (i.e. decimal, octal, or +hexadecimal) is determined in the same fashion as described in +\fBexpr\fR. The value is stored in the variable as a decimal string. +.VE 8.1 .TP 10 \fBc\fR A single character is read in and its binary value is stored in @@ -92,6 +119,13 @@ The matching string is stored in the variable. If the first character between the brackets is a \fB]\fR then it is treated as part of \fIchars\fR rather than the closing bracket for the set. +.VS 8.1 +If \fIchars\fR +contains a sequence of the form \fIa\fB\-\fIb\fR then any +character between \fIa\fR and \fIb\fR (inclusive) will match. +If the first or last character between the brackets is a \fB\-\fR, then +it is treated as part of \fIchars\fR rather than indicating a range. +.VE 8.1 .TP 10 \fB[^\fIchars\fB]\fR The input field consists of any number of characters not in @@ -100,6 +134,18 @@ The matching string is stored in the variable. If the character immediately following the \fB^\fR is a \fB]\fR then it is treated as part of the set rather than the closing bracket for the set. +.VS 8.1 +If \fIchars\fR +contains a sequence of the form \fIa\fB\-\fIb\fR then any +character between \fIa\fR and \fIb\fR (inclusive) will be excluded +from the set. +If the first or last character between the brackets is a \fB\-\fR, then +it is treated as part of \fIchars\fR rather than indicating a range. +.TP 10 +\fBn\fR +No input is consumed from the input string. Instead, the total number +of chacters scanned from the input string so far is stored in the variable. +.VE 8.1 .LP The number of characters read from the input for a conversion is the largest number that makes sense for that particular conversion (e.g. @@ -115,9 +161,11 @@ then no variable is assigned and the next scan argument is not consumed. .PP The behavior of the \fBscan\fR command is the same as the behavior of the ANSI C \fBsscanf\fR procedure except for the following differences: +.VS 8.1 .IP [1] -\fB%p\fR and \fB%n\fR conversion specifiers are not currently +\fB%p\fR conversion specifier is not currently supported. +.VE 8.1 .IP [2] For \fB%c\fR conversions a single character value is converted to a decimal string, which is then assigned to the diff --git a/generic/ChangeLog b/generic/ChangeLog new file mode 100644 index 0000000..f0ce5fd --- /dev/null +++ b/generic/ChangeLog @@ -0,0 +1,56 @@ +1998-11-17 <stanton@GASPODE> + + * tclScan.c: moved "scan" implementation out of tclCmdMZ.c and + added Unicode support. This required a complete reimplementation + of the command to avoid using scanf(), which isn't Unicode aware. + Two new features were added in the process: %n to return the + current number of characters consumed, and XPG3-style %n$ argument + order specifiers similar to those provided by the "format" + command. [Bug: 833] + + * tclAlloc.c: changed so allocated memory is always 8-byte aligned + to improve memory performance and to ensure that it will work on + systems that don't like accessing 4-byte aligned values + (e.g. Solaris and HP-UX). [Bug: 834] + +1998-11-06 <stanton@GASPODE> + + * tclVar.c (TclGetIndexedScalar): Fixed bug 796, var name was + getting lost before being passed to CallTraces. + +1998-10-21 <stanton@GASPODE> + + * added "encoding" command + + * Moved internal regexp declarations from tclInt.h to tclRegexp.h + + * integrated regexp updates from Henry Spencer + +1998-10-15 <stanton@GASPODE> + + * tclUtf.c: added Unicode character table support + + * tclInt.h: added TclUniCharIsWordChar + + * tclCmdMZ.c (Tcl_StringObjCmd): added "totitle" subcommand, + changed "wordend" and "wordstart" to properly handle Unicode word + characters and connector punctuation + +1998-10-05 <stanton@GASPODE> + + * tclCompile.c (TclCompileScript): changed to avoid modifying the + input string in place because name lookup operations could have + arbitrary side effects + + * tclInterp.c: added guard against deleting current interpreter + + * tclFileName.c: added warnings around code that modifies strings + in place + + * tclExecute.c: fixed off-by-one copying error, fixed merge bugs + + * tclEvent.c: changed so USE_TCLALLOC is tested for value instead + of definition + + * tclCompCmds.c: replaced SCCS strings, added warnings around code + that modifies strings in place diff --git a/generic/tclScan.c b/generic/tclScan.c index 1b2fb20..4b00b06 100644 --- a/generic/tclScan.c +++ b/generic/tclScan.c @@ -8,7 +8,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclScan.c,v 1.1.2.1 1998/11/16 20:45:22 stanton Exp $ + * RCS: @(#) $Id: tclScan.c,v 1.1.2.2 1998/11/18 04:15:46 stanton Exp $ */ #include "tclInt.h" @@ -50,7 +50,8 @@ typedef struct CharSet { * Declarations for functions used only in this file. */ -static int CharInSet _ANSI_ARGS_((CharSet *cset, Tcl_UniChar ch)); +static char * BuildCharSet _ANSI_ARGS_((CharSet *cset, char *format)); +static int CharInSet _ANSI_ARGS_((CharSet *cset, int ch)); static void ReleaseCharSet _ANSI_ARGS_((CharSet *cset)); static int ValidateFormat _ANSI_ARGS_((Tcl_Interp *interp, char *format, int numVars)); @@ -107,9 +108,10 @@ BuildCharSet(cset, format) end += Tcl_UtfToUniChar(end, &ch); } - cset->chars = ckalloc(sizeof(Tcl_UniChar) * (end - format - 1)); + cset->chars = (Tcl_UniChar *) ckalloc(sizeof(Tcl_UniChar) + * (end - format - 1)); if (nranges > 0) { - cset->ranges = ckalloc(sizeof(struct Range)*nranges); + cset->ranges = (struct Range *) ckalloc(sizeof(struct Range)*nranges); } else { cset->ranges = NULL; } @@ -145,8 +147,18 @@ BuildCharSet(cset, format) cset->chars[cset->nchars++] = ch; } else { format += Tcl_UtfToUniChar(format, &ch); - cset->ranges[cset->nranges].start = start; - cset->ranges[cset->nranges].end = ch; + + /* + * Check to see if the range is in reverse order. + */ + + if (start < ch) { + cset->ranges[cset->nranges].start = start; + cset->ranges[cset->nranges].end = ch; + } else { + cset->ranges[cset->nranges].start = ch; + cset->ranges[cset->nranges].end = start; + } cset->nranges++; } } else { @@ -174,10 +186,12 @@ BuildCharSet(cset, format) */ static int -CharInSet(cset, ch) +CharInSet(cset, c) CharSet *cset; - Tcl_UniChar ch; + int c; /* Character to test, passed as int because + * of non-ANSI prototypes. */ { + Tcl_UniChar ch = (Tcl_UniChar) c; int i, match = 0; for (i = 0; i < cset->nchars; i++) { if (cset->chars[i] == ch) { @@ -188,7 +202,7 @@ CharInSet(cset, ch) if (!match) { for (i = 0; i < cset->nranges; i++) { if ((cset->ranges[i].start <= ch) - || (ch <= cset->ranges[i].end)) { + && (ch <= cset->ranges[i].end)) { match = 1; break; } @@ -251,7 +265,6 @@ ValidateFormat(interp, format, numVars) char *end; Tcl_UniChar ch; int *nassign = (int*)ckalloc(sizeof(int) * numVars); - int code = TCL_OK; int objIndex; /* @@ -465,14 +478,14 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) Tcl_Obj *CONST objv[]; /* Argument objects. */ { char *format; - int numVars; + int numVars, nconversions; int objIndex, offset, i, value, result, code; char *string, *end, *baseString; - char op; - int base; + char op = 0; + int base = 0; int underflow = 0; size_t width; - long (*fn)(); + long (*fn)() = NULL; Tcl_UniChar ch, sch; Tcl_Obj **objs, *objPtr; int flags; @@ -516,6 +529,7 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) */ objIndex = 0; + nconversions = 0; while (*format != '\0') { format += Tcl_UtfToUniChar(format, &ch); @@ -602,6 +616,7 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) Tcl_IncrRefCount(objPtr); objs[objIndex++] = objPtr; } + nconversions++; continue; case 'd': @@ -723,7 +738,7 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) format = BuildCharSet(&cset, format); while (*end != '\0') { offset = Tcl_UtfToUniChar(end, &sch); - if (!CharInSet(&cset, sch)) { + if (!CharInSet(&cset, (int)sch)) { break; } end += offset; @@ -853,6 +868,9 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) */ if (flags & SCAN_NODIGITS) { + if (*string == '\0') { + underflow = 1; + } goto done; } else if (end[-1] == 'x' || end[-1] == 'X') { end--; @@ -952,6 +970,9 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) * There were no digits at all so scanning has * failed and we are done. */ + if (*string == '\0') { + underflow = 1; + } goto done; } @@ -981,6 +1002,7 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) } break; } + nconversions++; } done: @@ -1002,6 +1024,9 @@ Tcl_ScanObjCmd(dummy, interp, objc, objv) } ckfree((char*) objs); if (code == TCL_OK) { + if (underflow && (nconversions == 0)) { + result = -1; + } Tcl_SetObjResult(interp, Tcl_NewIntObj(result)); } return code; diff --git a/tests/scan.test b/tests/scan.test index bbae718..ec7a264 100644 --- a/tests/scan.test +++ b/tests/scan.test @@ -10,76 +10,347 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: scan.test,v 1.1.2.3 1998/11/16 20:45:23 stanton Exp $ +# RCS: @(#) $Id: scan.test,v 1.1.2.4 1998/11/18 04:15:46 stanton Exp $ if {[string compare test [info procs test]] == 1} then {source defs} -test scan-1.1 {ValidateFormat} { +test scan-1.1 {BuildCharSet, CharInSet} { + list [scan foo {%[^o]} x] $x +} {1 f} +test scan-1.2 {BuildCharSet, CharInSet} { + list [scan \]foo {%[]f]} x] $x +} {1 \]f} +test scan-1.3 {BuildCharSet, CharInSet} { + list [scan abc-def {%[a-c]} x] $x +} {1 abc} +test scan-1.4 {BuildCharSet, CharInSet} { + list [scan abc-def {%[a-c]} x] $x +} {1 abc} +test scan-1.5 {BuildCharSet, CharInSet} { + list [scan -abc-def {%[-ac]} x] $x +} {1 -a} +test scan-1.6 {BuildCharSet, CharInSet} { + list [scan -abc-def {%[ac-]} x] $x +} {1 -a} +test scan-1.7 {BuildCharSet, CharInSet} { + list [scan abc-def {%[c-a]} x] $x +} {1 abc} +test scan-1.8 {BuildCharSet, CharInSet} { + list [scan def-abc {%[^c-a]} x] $x +} {1 def-} + +test scan-2.1 {ReleaseCharSet} { + list [scan abcde {%[abc]} x] $x +} {1 abc} +test scan-2.2 {ReleaseCharSet} { + list [scan abcde {%[a-c]} x] $x +} {1 abc} + +test scan-3.1 {ValidateFormat} { list [catch {scan {} {%d%1$d} x} msg] $msg } {1 {cannot mix "%" and "%n$" conversion specifiers}} -test scan-1.1 {ValidateFormat} { +test scan-3.2 {ValidateFormat} { list [catch {scan {} {%d%1$d} x} msg] $msg } {1 {cannot mix "%" and "%n$" conversion specifiers}} -test scan-1.1 {ValidateFormat} { +test scan-3.3 {ValidateFormat} { list [catch {scan {} {%2$d%d} x} msg] $msg } {1 {"%n$" argument index out of range}} -test scan-1.1 {ValidateFormat} { +test scan-3.4 {ValidateFormat} { list [catch {scan {} %d} msg] $msg } {1 {different numbers of variable names and field specifiers}} -test scan-1.1 {ValidateFormat} { +test scan-3.5 {ValidateFormat} { list [catch {scan {} {%10c} a} msg] $msg } {1 {field width may not be specified in %c conversion}} -test scan-1.1 {ValidateFormat} { +test scan-3.6 {ValidateFormat} { list [catch {scan {} {%*1$d} a} msg] $msg } {1 {bad scan conversion character "$"}} -test scan-1.1 {ValidateFormat} { +test scan-3.7 {ValidateFormat} { list [catch {scan {} {%1$d%1$d} a} msg] $msg } {1 {variable is assigned by multiple "%n$" conversion specifiers}} -test scan-1.1 {ValidateFormat} { +test scan-3.8 {ValidateFormat} { list [catch {scan {} a x} msg] $msg } {1 {variable is not assigend by any conversion specifiers}} +test scan-3.9 {ValidateFormat} { + list [catch {scan {} {%2$s} x y} msg] $msg +} {1 {variable is not assigend by any conversion specifiers}} +test scan-3.10 {ValidateFormat} { + list [catch {scan {} {%[a} x} msg] $msg +} {1 {unmatched [ in format string}} +test scan-3.11 {ValidateFormat} { + list [catch {scan {} {%[^a} x} msg] $msg +} {1 {unmatched [ in format string}} +test scan-3.12 {ValidateFormat} { + list [catch {scan {} {%[]a} x} msg] $msg +} {1 {unmatched [ in format string}} +test scan-3.13 {ValidateFormat} { + list [catch {scan {} {%[^]a} x} msg] $msg +} {1 {unmatched [ in format string}} +test scan-4.1 {Tcl_ScanObjCmd, argument checks} { + list [catch {scan} msg] $msg +} {1 {wrong # args: should be "scan string format ?varName varName ...?"}} +test scan-4.2 {Tcl_ScanObjCmd, argument checks} { + list [catch {scan string} msg] $msg +} {1 {wrong # args: should be "scan string format ?varName varName ...?"}} +test scan-4.3 {Tcl_ScanObjCmd, argument checks} { + list [catch {scan string format} msg] $msg +} {0 0} +test scan-4.4 {Tcl_ScanObjCmd, whitespace} { + list [scan { abc def } {%s%s} x y] $x $y +} {2 abc def} +test scan-4.5 {Tcl_ScanObjCmd, whitespace} { + list [scan { abc def } { %s %s } x y] $x $y +} {2 abc def} +test scan-4.6 {Tcl_ScanObjCmd, whitespace} { + list [scan { abc def } { %s %s } x y] $x $y +} {2 abc def} +test scan-4.7 {Tcl_ScanObjCmd, literals} { + scan { abc def } { abc def } +} 0 +test scan-4.8 {Tcl_ScanObjCmd, literals} { + set x {} + list [scan { abcg} { abc def %1s} x] $x +} {0 {}} +test scan-4.9 {Tcl_ScanObjCmd, literals} { + list [scan { abc%defghi} { abc %% def%n } x] $x +} {1 10} +test scan-4.10 {Tcl_ScanObjCmd, assignment suppression} { + list [scan { abc def } { %*c%s def } x] $x +} {1 bc} +test scan-4.11 {Tcl_ScanObjCmd, XPG3-style} { + list [scan { abc def } {%2$s %1$s} x y] $x $y +} {2 def abc} +test scan-4.12 {Tcl_ScanObjCmd, width specifiers} { + list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e +} {5 abc 123 456.0 789 012} +test scan-4.13 {Tcl_ScanObjCmd, width specifiers} { + list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e +} {5 abc 123 456.0 789 012} +test scan-4.14 {Tcl_ScanObjCmd, underflow} { + set x {} + list [scan {a} {a%d} x] $x +} {-1 {}} +test scan-4.15 {Tcl_ScanObjCmd, underflow} { + set x {} + list [scan {} {a%d} x] $x +} {-1 {}} +test scan-4.16 {Tcl_ScanObjCmd, underflow} { + set x {} + list [scan {ab} {a%d} x] $x +} {0 {}} +test scan-4.17 {Tcl_ScanObjCmd, underflow} { + set x {} + list [scan {a } {a%d} x] $x +} {-1 {}} +test scan-4.18 {Tcl_ScanObjCmd, skipping whitespace} { + list [scan { b} {%c%s} x y] $x $y +} {2 32 b} +test scan-4.19 {Tcl_ScanObjCmd, skipping whitespace} { + list [scan { b} {%[^b]%s} x y] $x $y +} {2 { } b} +test scan-4.20 {Tcl_ScanObjCmd, string scanning} { + list [scan {abc def} {%s} x] $x +} {1 abc} +test scan-4.21 {Tcl_ScanObjCmd, string scanning} { + list [scan {abc def} {%0s} x] $x +} {1 abc} +test scan-4.22 {Tcl_ScanObjCmd, string scanning} { + list [scan {abc def} {%2s} x] $x +} {1 ab} +test scan-4.23 {Tcl_ScanObjCmd, string scanning} { + list [scan {abc def} {%*s%n} x] $x +} {1 3} +test scan-4.24 {Tcl_ScanObjCmd, charset scanning} { + list [scan {abcdef} {%[a-c]} x] $x +} {1 abc} +test scan-4.25 {Tcl_ScanObjCmd, charset scanning} { + list [scan {abcdef} {%0[a-c]} x] $x +} {1 abc} +test scan-4.26 {Tcl_ScanObjCmd, charset scanning} { + list [scan {abcdef} {%2[a-c]} x] $x +} {1 ab} +test scan-4.27 {Tcl_ScanObjCmd, charset scanning} { + list [scan {abcdef} {%*[a-c]%n} x] $x +} {1 3} +test scan-4.28 {Tcl_ScanObjCmd, character scanning} { + list [scan {abcdef} {%c} x] $x +} {1 97} +test scan-4.29 {Tcl_ScanObjCmd, character scanning} { + list [scan {abcdef} {%*c%n} x] $x +} {1 1} +test scan-4.30 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {1234567890a} {%3d} x] $x +} {1 123} +test scan-4.31 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {1234567890a} {%d} x] $x +} {1 1234567890} +test scan-4.32 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {01234567890a} {%d} x] $x +} {1 1234567890} +test scan-4.33 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {+01234} {%d} x] $x +} {1 1234} +test scan-4.34 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {-01234} {%d} x] $x +} {1 -1234} +test scan-4.35 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {a01234} {%d} x] $x +} {0 {}} +test scan-4.36 {Tcl_ScanObjCmd, base-10 integer scanning} { + set x {} + list [scan {0x10} {%d} x] $x +} {1 0} +test scan-4.37 {Tcl_ScanObjCmd, base-8 integer scanning} { + set x {} + list [scan {012345678} {%o} x] $x +} {1 342391} +test scan-4.38 {Tcl_ScanObjCmd, base-8 integer scanning} { + set x {} + list [scan {+1238 -1239 123a} {%o%*s%o%*s%o} x y z] $x $y $z +} {3 83 -83 83} +test scan-4.39 {Tcl_ScanObjCmd, base-16 integer scanning} { + set x {} + list [scan {+1238 -123a 0123} {%x%x%x} x y z] $x $y $z +} {3 4664 -4666 291} +test scan-4.40 {Tcl_ScanObjCmd, base-16 integer scanning} { + set x {} + list [scan {aBcDeF AbCdEf 0x1} {%x%x%x} x y z] $x $y $z +} {3 11259375 11259375 0} +test scan-4.41 {Tcl_ScanObjCmd, base-unknown integer scanning} { + set x {} + list [scan {10 010 0x10} {%i%i%i} x y z] $x $y $z +} {3 10 8 16} +test scan-4.42 {Tcl_ScanObjCmd, base-unknown integer scanning} { + set x {} + list [scan {10 010 0X10} {%i%i%i} x y z] $x $y $z +} {3 10 8 16} +test scan-4.43 {Tcl_ScanObjCmd, integer scanning, odd cases} { + set x {} + list [scan {+ } {%i} x] $x +} {0 {}} +test scan-4.44 {Tcl_ScanObjCmd, integer scanning, odd cases} { + set x {} + list [scan {+} {%i} x] $x +} {-1 {}} +test scan-4.45 {Tcl_ScanObjCmd, integer scanning, odd cases} { + set x {} + list [scan {0x} {%i%s} x y] $x $y +} {2 0 x} +test scan-4.46 {Tcl_ScanObjCmd, integer scanning, odd cases} { + set x {} + list [scan {0X} {%i%s} x y] $x $y +} {2 0 X} +test scan-4.47 {Tcl_ScanObjCmd, integer scanning, suppressed} { + set x {} + list [scan {123def} {%*i%s} x] $x +} {1 def} +test scan-4.48 {Tcl_ScanObjCmd, float scanning} { + list [scan {1 2 3} {%e %f %g} x y z] $x $y $z +} {3 1.0 2.0 3.0} +test scan-4.49 {Tcl_ScanObjCmd, float scanning} { + list [scan {.1 0.2 3.} {%e %f %g} x y z] $x $y $z +} {3 0.1 0.2 3.0} +test scan-4.50 {Tcl_ScanObjCmd, float scanning} { + list [scan {1234567890a} %f x] $x +} {1 1234567890.0} +test scan-4.51 {Tcl_ScanObjCmd, float scanning} { + list [scan {+123+45} %f x] $x +} {1 123.0} +test scan-4.52 {Tcl_ScanObjCmd, float scanning} { + list [scan {-123+45} %f x] $x +} {1 -123.0} +test scan-4.53 {Tcl_ScanObjCmd, float scanning} { + list [scan {1.0e1} %f x] $x +} {1 10.0} +test scan-4.54 {Tcl_ScanObjCmd, float scanning} { + list [scan {1.0e-1} %f x] $x +} {1 0.1} +test scan-4.55 {Tcl_ScanObjCmd, odd cases} { + set x {} + list [scan {+} %f x] $x +} {-1 {}} +test scan-4.56 {Tcl_ScanObjCmd, odd cases} { + set x {} + list [scan {1.0e} %f%s x y] $x $y +} {2 1.0 e} +test scan-4.57 {Tcl_ScanObjCmd, odd cases} { + set x {} + list [scan {1.0e+} %f%s x y] $x $y +} {2 1.0 e+} +test scan-4.58 {Tcl_ScanObjCmd, odd cases} { + set x {} + set y {} + list [scan {e1} %f%s x y] $x $y +} {0 {} {}} +test scan-4.59 {Tcl_ScanObjCmd, float scanning} { + list [scan {1.0e-1x} %*f%n x] $x +} {1 6} +test scan-4.60 {Tcl_ScanObjCmd, set errors} { + set x {} + set y {} + catch {unset z}; array set z {} + set result [list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] \ + $msg $x $y] + unset z + set result +} {1 {couldn't set variable "z"} abc ghi} +test scan-4.61 {Tcl_ScanObjCmd, set errors} { + set x {} + catch {unset y}; array set y {} + catch {unset z}; array set z {} + set result [list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] \ + $msg $x] + unset y + unset z + set result +} {1 {couldn't set variable "z"couldn't set variable "y"} abc} -test scan-1.1 {integer scanning} { +test scan-5.1 {integer scanning} { set a {}; set b {}; set c {}; set d {} list [scan "-20 1476 \n33 0" "%d %d %d %d" a b c d] $a $b $c $d } {4 -20 1476 33 0} -test scan-1.2 {integer scanning} { +test scan-5.2 {integer scanning} { set a {}; set b {}; set c {} list [scan "-45 16 7890 +10" "%2d %*d %10d %d" a b c] $a $b $c } {3 -4 16 7890} -test scan-1.3 {integer scanning} { +test scan-5.3 {integer scanning} { set a {}; set b {}; set c {}; set d {} list [scan "-45 16 +10 987" "%ld %d %ld %d" a b c d] $a $b $c $d } {4 -45 16 10 987} -test scan-1.4 {integer scanning} { +test scan-5.4 {integer scanning} { set a {}; set b {}; set c {}; set d {} list [scan "14 1ab 62 10" "%d %x %lo %x" a b c d] $a $b $c $d } {4 14 427 50 16} -test scan-1.5 {integer scanning} { +test scan-5.5 {integer scanning} { set a {}; set b {}; set c {}; set d {} list [scan "12345670 1234567890ab cdefg" "%o %o %x %lx" a b c d] \ $a $b $c $d } {4 2739128 342391 561323 52719} -test scan-1.6 {integer scanning} { +test scan-5.6 {integer scanning} { set a {}; set b {}; set c {}; set d {} list [scan "ab123-24642" "%2x %3x %3o %2o" a b c d] $a $b $c $d } {4 171 291 -20 52} -test scan-1.7 {integer scanning} { +test scan-5.7 {integer scanning} { set a {}; set b {} list [scan "1234567 234 567 " "%*3x %x %*o %4o" a b] $a $b } {2 17767 375} -test scan-1.8 {integer scanning} { +test scan-5.8 {integer scanning} { set a {}; set b {} list [scan "a 1234" "%d %d" a b] $a $b } {0 {} {}} -test scan-1.9 {integer scanning} { +test scan-5.9 {integer scanning} { set a {}; set b {}; set c {}; set d {}; list [scan "12345678" "%2d %2d %2ld %2d" a b c d] $a $b $c $d } {4 12 34 56 78} -test scan-1.10 {integer scanning} { +test scan-5.10 {integer scanning} { set a {}; set b {}; set c {}; set d {} list [scan "1 2 " "%hd %d %d %d" a b c d] $a $b $c $d } {2 1 2 {} {}} @@ -88,20 +359,21 @@ test scan-1.10 {integer scanning} { # not defined by the ANSI spec. Some implementations wrap the # input (-16) some return MAX_INT. # -test scan-1.11 {integer scanning} {nonPortable} { +test scan-5.11 {integer scanning} {nonportable} { set a {}; set b {}; - list [scan "4294967280 4294967280" "%u %d" a b] $a $b -} {2 4294967280 -16} + list [scan "4294967280 4294967280" "%u %d" a b] $a \ + [expr {$b == -16 || $b == 0x7fffffff}] +} {2 4294967280 1} -test scan-2.1 {floating-point scanning} {eformat} { +test scan-6.1 {floating-point scanning} { set a {}; set b {}; set c {}; set d {} list [scan "2.1 -3.0e8 .99962 a" "%f%g%e%f" a b c d] $a $b $c $d } {3 2.1 -300000000.0 0.99962 {}} -test scan-2.2 {floating-point scanning} { +test scan-6.2 {floating-point scanning} { set a {}; set b {}; set c {}; set d {} list [scan "-1.2345 +8.2 9" "%3e %3lf %f %f" a b c d] $a $b $c $d } {4 -1.0 234.0 5.0 8.2} -test scan-2.3 {floating-point scanning} { +test scan-6.3 {floating-point scanning} { set a {}; set b {}; set c {} list [scan "1e00004 332E-4 3e+4" "%Lf %*2e %f %f" a b c] $a $c } {3 10000.0 30000.0} @@ -109,187 +381,189 @@ test scan-2.3 {floating-point scanning} { # Some libc implementations consider 3.e- bad input. The ANSI # spec states that digits must follow the - sign. # -test scan-2.4 {floating-point scanning} {nonPortable} { +test scan-6.4 {floating-point scanning} { set a {}; set b {}; set c {} list [scan "1. 47.6 2.e2 3.e-" "%f %*f %f %f" a b c] $a $b $c } {3 1.0 200.0 3.0} -test scan-2.5 {floating-point scanning} { +test scan-6.5 {floating-point scanning} { set a {}; set b {}; set c {}; set d {} list [scan "4.6 99999.7 876.43e-1 118" "%f %f %f %e" a b c d] $a $b $c $d } {4 4.6 99999.7 87.643 118.0} -test scan-2.6 {floating-point scanning} {eformat} { +test scan-6.6 {floating-point scanning} {eformat} { set a {}; set b {}; set c {}; set d {} list [scan "1.2345 697.0e-3 124 .00005" "%f %e %f %e" a b c d] $a $b $c $d } {4 1.2345 0.697 124.0 5e-05} -test scan-2.7 {floating-point scanning} { +test scan-6.7 {floating-point scanning} { set a {}; set b {}; set c {}; set d {} list [scan "4.6abc" "%f %f %f %f" a b c d] $a $b $c $d } {1 4.6 {} {} {}} -test scan-2.8 {floating-point scanning} { +test scan-6.8 {floating-point scanning} { set a {}; set b {}; set c {}; set d {} list [scan "4.6 5.2" "%f %f %f %f" a b c d] $a $b $c $d } {2 4.6 5.2 {} {}} -test scan-3.1 {string and character scanning} { +test scan-7.1 {string and character scanning} { set a {}; set b {}; set c {}; set d {} list [scan "abc defghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d } {4 abc def ghijk dum} -test scan-3.2 {string and character scanning} { +test scan-7.2 {string and character scanning} { set a {}; set b {}; set c {}; set d {} list [scan "a bcdef" "%c%c%1s %s" a b c d] $a $b $c $d } {4 97 32 b cdef} -test scan-3.3 {string and character scanning} { +test scan-7.3 {string and character scanning} { set a {}; set b {}; set c {} list [scan "123456 test " "%*c%*s %s %s %s" a b c] $a $b $c } {1 test {} {}} -test scan-3.4 {string and character scanning} { +test scan-7.4 {string and character scanning} { set a {}; set b {}; set c {}; set d list [scan "ababcd01234 f 123450" {%4[abcd] %4[abcd] %[^abcdef] %[^0]} a b c d] $a $b $c $d } {4 abab cd {01234 } {f 12345}} -test scan-3.5 {string and character scanning} { +test scan-7.5 {string and character scanning} { set a {}; set b {}; set c {} list [scan "aaaaaabc aaabcdefg + + XYZQR" {%*4[a] %s %*4[a]%s%*4[ +]%c} a b c] $a $b $c } {3 aabc bcdefg 43} -test scan-3.6 {string and character scanning, unicode} { +test scan-7.6 {string and character scanning, unicode} { set a {}; set b {}; set c {}; set d {} list [scan "abc d\u00c7fghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d } "4 abc d\u00c7f ghijk dum" -test scan-3.7 {string and character scanning, unicode} { +test scan-7.7 {string and character scanning, unicode} { set a {}; set b {} list [scan "ab\u00c7cdef" "ab%c%c" a b] $a $b } "2 199 99" -test scan-3.7 {string and character scanning, unicode} { +test scan-7.8 {string and character scanning, unicode} { set a {}; set b {} list [scan "ab\ufeffdef" "%\[ab\ufeff\]" a] $a } "1 ab\ufeff" -test scan-4.1 {error conditions} { +test scan-8.1 {error conditions} { catch {scan a} } 1 -test scan-4.2 {error conditions} { +test scan-8.2 {error conditions} { catch {scan a} msg set msg } {wrong # args: should be "scan string format ?varName varName ...?"} -test scan-4.5 {error conditions} { +test scan-8.3 {error conditions} { list [catch {scan a %D x} msg] $msg } {1 {bad scan conversion character "D"}} -test scan-4.6 {error conditions} { +test scan-8.4 {error conditions} { list [catch {scan a %O x} msg] $msg } {1 {bad scan conversion character "O"}} -test scan-4.7 {error conditions} { +test scan-8.5 {error conditions} { list [catch {scan a %X x} msg] $msg } {1 {bad scan conversion character "X"}} -test scan-4.8 {error conditions} { +test scan-8.6 {error conditions} { list [catch {scan a %F x} msg] $msg } {1 {bad scan conversion character "F"}} -test scan-4.9 {error conditions} { +test scan-8.7 {error conditions} { list [catch {scan a %E x} msg] $msg } {1 {bad scan conversion character "E"}} -test scan-4.10 {error conditions} { +test scan-8.8 {error conditions} { list [catch {scan a "%d %d" a} msg] $msg } {1 {different numbers of variable names and field specifiers}} -test scan-4.11 {error conditions} { +test scan-8.9 {error conditions} { list [catch {scan a "%d %d" a b c} msg] $msg } {1 {variable is not assigend by any conversion specifiers}} -test scan-4.12 {error conditions} { +test scan-8.10 {error conditions} { set a {}; set b {}; set c {}; set d {} list [expr {[scan " a" " a %d %d %d %d" a b c d] <= 0}] $a $b $c $d } {1 {} {} {} {}} -test scan-4.13 {error conditions} { +test scan-8.11 {error conditions} { set a {}; set b {}; set c {}; set d {} list [scan "1 2" "%d %d %d %d" a b c d] $a $b $c $d } {2 1 2 {} {}} -test scan-4.14 {error conditions} { +test scan-8.12 {error conditions} { catch {unset a} set a(0) 44 list [catch {scan 44 %d a} msg] $msg } {1 {couldn't set variable "a"}} -test scan-4.15 {error conditions} { +test scan-8.13 {error conditions} { catch {unset a} set a(0) 44 list [catch {scan 44 %c a} msg] $msg } {1 {couldn't set variable "a"}} -test scan-4.16 {error conditions} { +test scan-8.14 {error conditions} { catch {unset a} set a(0) 44 list [catch {scan 44 %s a} msg] $msg } {1 {couldn't set variable "a"}} -test scan-4.17 {error conditions} { +test scan-8.15 {error conditions} { catch {unset a} set a(0) 44 list [catch {scan 44 %f a} msg] $msg } {1 {couldn't set variable "a"}} -test scan-4.18 {error conditions} { +test scan-8.16 {error conditions} { catch {unset a} set a(0) 44 list [catch {scan 44 %f a} msg] $msg } {1 {couldn't set variable "a"}} catch {unset a} -test scan-4.19 {error conditions} { +test scan-8.17 {error conditions} { list [catch {scan 44 %2c a} msg] $msg } {1 {field width may not be specified in %c conversion}} -test scan-4.20 {error conditions} { +test scan-8.18 {error conditions} { list [catch {scan abc {%[} x} msg] $msg } {1 {unmatched [ in format string}} -test scan-4.20 {error conditions} { +test scan-8.19 {error conditions} { list [catch {scan abc {%[^a} x} msg] $msg } {1 {unmatched [ in format string}} -test scan-4.20 {error conditions} { +test scan-8.20 {error conditions} { list [catch {scan abc {%[^]a} x} msg] $msg } {1 {unmatched [ in format string}} -test scan-4.20 {error conditions} { +test scan-8.21 {error conditions} { list [catch {scan abc {%[]a} x} msg] $msg } {1 {unmatched [ in format string}} -test scan-5.1 {lots of arguments} { +test scan-9.1 {lots of arguments} { scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 } 20 -test scan-5.2 {lots of arguments} { +test scan-9.2 {lots of arguments} { scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 set a20 } 200 -test scan-6.1 {miscellaneous tests} { +test scan-10.1 {miscellaneous tests} { set a {} list [scan ab16c ab%dc a] $a } {1 16} -test scan-6.2 {miscellaneous tests} { +test scan-10.2 {miscellaneous tests} { set a {} list [scan ax16c ab%dc a] $a } {0 {}} -test scan-6.3 {miscellaneous tests} { +test scan-10.3 {miscellaneous tests} { set a {} list [catch {scan ab%c114 ab%%c%d a} msg] $msg $a } {0 1 114} -test scan-6.4 {miscellaneous tests} { +test scan-10.4 {miscellaneous tests} { set a {} list [catch {scan ab%c14 ab%%c%d a} msg] $msg $a } {0 1 14} -test scan-6.5 {miscellaneous tests} { +test scan-10.5 {miscellaneous tests} { catch {unset arr} set arr(2) {} list [catch {scan ab%c14 ab%%c%d arr(2)} msg] $msg $arr(2) } {0 1 14} -test scan-7.1 {alignment in results array (TCL_ALIGN)} { +test scan-11.1 {alignment in results array (TCL_ALIGN)} { scan "123 13.6" "%s %f" a b set b } 13.6 -test scan-7.2 {alignment in results array (TCL_ALIGN)} { +test scan-11.2 {alignment in results array (TCL_ALIGN)} { scan "1234567 13.6" "%s %f" a b set b } 13.6 -test scan-7.3 {alignment in results array (TCL_ALIGN)} { +test scan-11.3 {alignment in results array (TCL_ALIGN)} { scan "12345678901 13.6" "%s %f" a b set b } 13.6 -test scan-7.4 {alignment in results array (TCL_ALIGN)} { +test scan-11.4 {alignment in results array (TCL_ALIGN)} { scan "123456789012345 13.6" "%s %f" a b set b } 13.6 -test scan-7.5 {alignment in results array (TCL_ALIGN)} { +test scan-11.5 {alignment in results array (TCL_ALIGN)} { scan "1234567890123456789 13.6" "%s %f" a b set b } 13.6 return + + |