aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchi <chi>2005-03-21 11:59:44 +0000
committerchi <chi>2005-03-21 11:59:44 +0000
commitd9ffadaa5411d62ff47c46532305dcac85c4e0bb (patch)
tree2915dafc08e876333c50cfb79297929f075afcce
parent76e16dfe41d1cad0a7f01d1a45de0cbffe525c94 (diff)
downloadjimtcl-d9ffadaa5411d62ff47c46532305dcac85c4e0bb.zip
jimtcl-d9ffadaa5411d62ff47c46532305dcac85c4e0bb.tar.gz
jimtcl-d9ffadaa5411d62ff47c46532305dcac85c4e0bb.tar.bz2
Add the [scan] command and the Jim_ScanString function + tests.
The scanformat specification will be converted to a new Jim_Obj of type scanFormatStringObjType, that will contain the parsed representation within its internal object representation. This speed up multiple scanning within e.g. a loop, of objects were cached. For internal scanning we use sscanf currently (I am lazy right now). That means also, we will inherit its incapability to handle string with embedded ZERO. It would be not too difficult to implement another scanner just for the string and charset conversion type that could be able to handle those embedded ZEROs, however. Furthermore two small details were fixed: 1. Jim_DoubleToString should also recognize a number if a leading '+' or '-' occured. By recognizing I mean, add a ".0" to such a number. 2. Jim_StrDupLen should also properly handle duplication of substrings. So now it should be possible to do this: const char *str1 = "This is a long string"; char *substr1 = Jim_StrDupLen(str1, 4); Now substr1 should contain a properly ZERO ended "This".
-rw-r--r--jim.c510
-rw-r--r--jim.h9
-rw-r--r--test.tcl603
3 files changed, 1115 insertions, 7 deletions
diff --git a/jim.c b/jim.c
index 9b619e4..57d060f 100644
--- a/jim.c
+++ b/jim.c
@@ -1,7 +1,7 @@
/* Jim - A small embeddable Tcl interpreter
* Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
*
- * $Id: jim.c,v 1.122 2005/03/19 21:39:34 antirez Exp $
+ * $Id: jim.c,v 1.123 2005/03/21 11:59:44 chi Exp $
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -412,7 +412,9 @@ int Jim_DoubleToString(char *buf, double doubleValue)
}
/* Add a final ".0" if it's a number. But not
* for NaN or InF */
- if (isdigit((int)buf[0])) {
+ if (isdigit((int)buf[0])
+ || ((buf[0] == '-' || buf[0] == '+')
+ && isdigit((int)buf[1]))) {
s[0] = '.';
s[1] = '0';
s[2] = '\0';
@@ -498,6 +500,7 @@ char *Jim_StrDupLen(char *s, int l)
char *copy = Jim_Alloc(l+1);
memcpy(copy, s, l+1);
+ copy[l] = 0; /* Just to be sure, original could be substring */
return copy;
}
@@ -6547,6 +6550,442 @@ int Jim_GetBoolFromExpr(Jim_Interp *interp, Jim_Obj *exprObjPtr, int *boolPtr)
}
/* -----------------------------------------------------------------------------
+ * ScanFormat String Object
+ * ---------------------------------------------------------------------------*/
+
+/* This Jim_Obj will held a parsed representation of a format string passed to
+ * the Jim_ScanString command. For error diagnostics, the scanformat string has
+ * to be parsed in its entirely first and then, if correct, can be used for
+ * scanning. To avoid endless re-parsing, the parsed representation will be
+ * stored in an internal representation and re-used for performance reason. */
+
+/* A ScanFmtPartDescr will held the information of /one/ part of the whole
+ * scanformat string. This part will later be used to extract information
+ * out from the string to be parsed by Jim_ScanString */
+
+typedef struct ScanFmtPartDescr {
+ char type; /* Type of conversion (e.g. c, d, f) */
+ char modifier; /* Modify type (e.g. l - long, h - short */
+ int width; /* Maximal width of input to be converted */
+ int pos; /* -1 - no assign, 0 - natural pos, >0 - XPG3 pos */
+ char *arg; /* Specification of a CHARSET conversion */
+ char *prefix; /* Prefix to be scanned literally before conversion */
+} ScanFmtPartDescr;
+
+/* The ScanFmtStringObj will held the internal representation of a scanformat
+ * string parsed and separated in part descriptions. Furthermore it contains
+ * the original string representation of the scanformat string to allow for
+ * fast update of the Jim_Obj's string representation part.
+ *
+ * As add-on the internal object representation add some scratch pad area
+ * for usage by Jim_ScanString to avoid endless allocating and freeing of
+ * memory for purpose of string scanning.
+ *
+ * The error member points to a static allocated string in case of a mal-
+ * formed scanformat string or it contains '0' (NULL) in case of a valid
+ * parse representation.
+ *
+ * The whole memory of the internal representation is allocated as a single
+ * area of memory that will be internally separated. So freeing and duplicating
+ * of such an object is cheap */
+
+typedef struct ScanFmtStringObj {
+ jim_wide size; /* Size of internal repr in bytes */
+ char *stringRep; /* Original string representation */
+ int count; /* Number of ScanFmtPartDescr contained */
+ int convCount; /* Number of conversions that will assign */
+ int maxPos; /* Max position index if XPG3 is used */
+ const char *error; /* Ptr to error text (NULL if no error */
+ char *scratch; /* Some scratch pad used by Jim_ScanString */
+ ScanFmtPartDescr descr[1]; /* The vector of partial descriptions */
+} ScanFmtStringObj;
+
+
+static void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static void UpdateStringOfScanFmt(Jim_Obj *objPtr);
+
+static Jim_ObjType scanFmtStringObjType = {
+ "scanformatstring",
+ FreeScanFmtInternalRep,
+ DupScanFmtInternalRep,
+ UpdateStringOfScanFmt,
+ JIM_TYPE_NONE,
+};
+
+void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ JIM_NOTUSED(interp);
+ Jim_Free((char*)objPtr->internalRep.ptr);
+ objPtr->internalRep.ptr = 0;
+}
+
+void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ jim_wide size = ((ScanFmtStringObj*)srcPtr->internalRep.ptr)->size;
+ ScanFmtStringObj *newVec = (ScanFmtStringObj*)Jim_Alloc(size);
+
+ JIM_NOTUSED(interp);
+ memcpy(newVec, srcPtr->internalRep.ptr, size);
+ dupPtr->internalRep.ptr = newVec;
+ dupPtr->typePtr = &scanFmtStringObjType;
+}
+
+void UpdateStringOfScanFmt(Jim_Obj *objPtr)
+{
+ char *bytes = ((ScanFmtStringObj*)objPtr->internalRep.ptr)->stringRep;
+
+ objPtr->bytes = Jim_StrDup(bytes);
+ objPtr->length = strlen(bytes);
+}
+
+/* SetScanFmtFromAny will parse a given string and create the internal
+ * representation of the format specification. In case of an error
+ * the error data member of the internal representation will be set
+ * to an descriptive error text and the function will be left with
+ * JIM_ERR to indicate unsucessful parsing (aka. malformed scanformat
+ * specification */
+
+static int SetScanFmtFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ ScanFmtStringObj *fmtObj;
+ char *buffer;
+ int maxCount, i, approxSize, lastPos = -1;
+ const char *fmt = objPtr->bytes;
+ int maxFmtLen = objPtr->length;
+ const char *fmtEnd = fmt + maxFmtLen;
+ int curr;
+
+ Jim_FreeIntRep(interp, objPtr);
+ /* Count how many conversions could take place maximally */
+ for (i=0, maxCount=0; i < maxFmtLen; ++i)
+ if (fmt[i] == '%')
+ ++maxCount;
+ /* Calculate an approximation of the memory necessary */
+ approxSize = sizeof(ScanFmtStringObj) /* Size of the container */
+ + (maxCount + 1) * sizeof(ScanFmtPartDescr) /* Size of all partials */
+ + maxFmtLen * sizeof(char) + 3 + 1 /* Scratch + "%n" + '\0' */
+ + maxFmtLen * sizeof(char) + 1 /* Original stringrep */
+ + maxFmtLen * sizeof(char) /* Arg for CHARSETs */
+ + (maxCount +1) * sizeof(char) /* '\0' for every partial */
+ + 1; /* safety byte */
+ fmtObj = (ScanFmtStringObj*)Jim_Alloc(approxSize);
+ memset(fmtObj, 0, approxSize);
+ fmtObj->size = approxSize;
+ fmtObj->maxPos = -1;
+ fmtObj->scratch = (char*)&fmtObj->descr[maxCount+1];
+ fmtObj->stringRep = fmtObj->scratch + maxFmtLen + 3 + 1;
+ memcpy(fmtObj->stringRep, fmt, maxFmtLen);
+ buffer = fmtObj->stringRep + maxFmtLen + 1;
+ objPtr->internalRep.ptr = fmtObj;
+ objPtr->typePtr = &scanFmtStringObjType;
+ for (i=0, curr=0; fmt < fmtEnd; ++fmt) {
+ int width=0, skip;
+ ScanFmtPartDescr *descr = &fmtObj->descr[curr];
+ fmtObj->count++;
+ descr->width = 0; /* Assume width unspecified */
+ /* Overread and store any "literal" prefix */
+ if (*fmt != '%' || fmt[1] == '%') {
+ descr->type = 0;
+ descr->prefix = &buffer[i];
+ for (; fmt < fmtEnd; ++fmt) {
+ if (*fmt == '%') {
+ if (fmt[1] != '%') break;
+ buffer[i++] = *fmt++;
+ }
+ buffer[i++] = *fmt;
+ }
+ buffer[i++] = 0;
+ }
+ /* Skip the conversion introducing '%' sign */
+ ++fmt;
+ /* End reached due to non-conversion literal only? */
+ if (fmt >= fmtEnd)
+ goto done;
+ descr->pos = 0; /* Assume "natural" positioning */
+ if (*fmt == '*') {
+ descr->pos = -1; /* Okay, conversion will not be assigned */
+ ++fmt;
+ } else
+ fmtObj->convCount++; /* Otherwise count as assign-conversion */
+ /* Check if next token is a number (could be width or pos */
+ if (sscanf(fmt, "%d%n", &width, &skip) == 1) {
+ fmt += skip;
+ /* Was the number a XPG3 position specifier? */
+ if (descr->pos != -1 && *fmt == '$') {
+ int prev;
+ ++fmt;
+ descr->pos = width;
+ width = 0;
+ /* Look if "natural" postioning and XPG3 one was mixed */
+ if ((lastPos == 0 && descr->pos > 0)
+ || (lastPos > 0 && descr->pos == 0)) {
+ fmtObj->error = "cannot mix \"%\" and \"%n$\" conversion specifiers";
+ return JIM_ERR;
+ }
+ /* Look if this position was already used */
+ for (prev=0; prev < curr; ++prev) {
+ if (fmtObj->descr[prev].pos == -1) continue;
+ if (fmtObj->descr[prev].pos == descr->pos) {
+ fmtObj->error = "same \"%n$\" conversion specifier "
+ "used more than once";
+ return JIM_ERR;
+ }
+ }
+ /* Try to find a width after the XPG3 specifier */
+ if (sscanf(fmt, "%d%n", &width, &skip) == 1) {
+ descr->width = width;
+ fmt += skip;
+ }
+ if (descr->pos > fmtObj->maxPos)
+ fmtObj->maxPos = descr->pos;
+ } else {
+ /* Number was not a XPG3, so it has to be a width */
+ descr->width = width;
+ }
+ }
+ /* If positioning mode was undetermined yet, fix this */
+ if (lastPos == -1)
+ lastPos = descr->pos;
+ /* Handle CHARSET conversion type ... */
+ if (*fmt == '[') {
+ int swapped = 1, beg = i, end, j;
+ descr->type = '[';
+ descr->arg = &buffer[i];
+ ++fmt;
+ if (*fmt == '^') buffer[i++] = *fmt++;
+ if (*fmt == ']') buffer[i++] = *fmt++;
+ while (*fmt && *fmt != ']') buffer[i++] = *fmt++;
+ if (*fmt != ']') {
+ fmtObj->error = "unmatched [ in format string";
+ return JIM_ERR;
+ }
+ end = i;
+ buffer[i++] = 0;
+ /* In case a range fence was given "backwards", swap it */
+ while (swapped) {
+ swapped = 0;
+ for (j=beg+1; j < end-1; ++j) {
+ if (buffer[j] == '-' && buffer[j-1] > buffer[j+1]) {
+ char tmp = buffer[j-1];
+ buffer[j-1] = buffer[j+1];
+ buffer[j+1] = tmp;
+ swapped = 1;
+ }
+ }
+ }
+ } else {
+ /* Remember any valid modifier if given */
+ if (strchr("hlL", *fmt) != 0)
+ descr->modifier = tolower(*fmt++);
+
+ descr->type = *fmt;
+ if (strchr("efgcsndoxui", *fmt) == 0) {
+ fmtObj->error = "bad scan conversion character";
+ return JIM_ERR;
+ } else if (*fmt == 'c' && descr->width != 0) {
+ fmtObj->error = "field width may not be specified in %c "
+ "conversion";
+ return JIM_ERR;
+ }
+ }
+ curr++;
+ }
+done:
+ if (fmtObj->convCount == 0) {
+ fmtObj->error = "no any conversion specifier given";
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+/* Some accessor macros to allow lowlevel access to fields of internal repr */
+
+#define FormatGetCnvCount(_fo_) \
+ ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->convCount
+#define FormatGetMaxPos(_fo_) \
+ ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->maxPos
+#define FormatGetError(_fo_) \
+ ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->error
+
+/* BuildScanFormat will build a format string suitable for usage in functions
+ * of the scanf family. The information to build such a format specification
+ * will be taken from the part description of the parsed format information
+ * contained in the internal object representation.
+ *
+ * The format string will be build within the scratch pad area of the
+ * object representation and therefore overwritten every time another one
+ * is build. A '%n' will be appended to the format string to get the number
+ * of characters scanned so far. */
+
+static const char *BuildScanFormat(ScanFmtStringObj *fmtObj, long index)
+{
+ ScanFmtPartDescr *descr = &fmtObj->descr[index];
+ char *buffer = fmtObj->scratch;
+ if (descr->prefix) {
+ strcpy(buffer, descr->prefix);
+ buffer += strlen(descr->prefix);
+ }
+ *buffer++ = '%';
+ if (descr->width != 0) {
+ sprintf(buffer, "%d", descr->width);
+ buffer += strlen(buffer);
+ }
+ if (descr->type == '[') {
+ sprintf(buffer, "[%s]%%n", descr->arg);
+ } else {
+ if (descr->modifier == 'l' && strchr("ndioux", descr->type) != 0)
+ *buffer++ = 'q';
+ else if (strchr("ndiouxefg", descr->type) != 0)
+ *buffer++ = 'l';
+ sprintf(buffer, "%c%%n", descr->type);
+ }
+ return fmtObj->scratch;
+}
+
+/* ScanOneEntry will scan one entry out of the string passed as argument.
+ * It use the sscanf() function for this task. After extracting and
+ * converting of the value, the count of scanned characters will be
+ * returned of -1 in case of no conversion tool place and string was
+ * already scanned thru */
+
+static int ScanOneEntry(Jim_Interp *interp, const char *str, long pos,
+ ScanFmtStringObj *fmtObj, long index, Jim_Obj **valObjPtr)
+{
+ char buffer[256];
+ char *value = buffer;
+ const char *fmt;
+ const ScanFmtPartDescr *descr = &fmtObj->descr[index];
+ long sLen = 0, scanned = 0;
+ int rc;
+
+ /* In case of a string value to be parsed we look if a larger
+ * buffer is necessary to hold the result */
+ if (descr->type == 's' || descr->type == '[') {
+ sLen = strlen(&str[pos]);
+ if ((unsigned)sLen >= sizeof(buffer))
+ value = Jim_Alloc(sLen * sizeof(char) + 1);
+ }
+ fmt = BuildScanFormat(fmtObj, index);
+ rc = sscanf(&str[pos], fmt, value, &scanned);
+ if (descr->type == 'n') {
+ if (descr->modifier == 'l')
+ *valObjPtr = Jim_NewIntObj(interp, pos + *(jim_wide*)value);
+ else
+ *valObjPtr = Jim_NewIntObj(interp, pos + *(long*)value);
+ /* Probably a prefix was before the '%n' spec? Add that length */
+ scanned = *(long*)value;
+ } else if (rc == EOF) {
+ scanned = -1;
+ } else if (rc == 1) {
+ /* Convert the scanned value and add assign to be returned */
+ switch (descr->type) {
+ case 'c':
+ *valObjPtr = Jim_NewIntObj(interp, *value);
+ break;
+ case 'd': case 'o': case 'x': case 'u': case 'i':
+ if (descr->modifier == 'l')
+ *valObjPtr = Jim_NewIntObj(interp, *(jim_wide*)value);
+ else
+ *valObjPtr = Jim_NewIntObj(interp, *(long*)value);
+ break;
+ case 's': case '[':
+ *valObjPtr = Jim_NewStringObj(interp, value, -1);
+ break;
+ case 'e': case 'f': case 'g':
+ *valObjPtr = Jim_NewDoubleObj(interp, *(double*)value);
+ break;
+ }
+ }
+ /* If string was scanned and additional buffer allocated, free it! */
+ if ((descr->type == 's' || descr->type == '[')
+ && value != buffer)
+ Jim_Free((char*)value);
+ return scanned;
+}
+
+/* Jim_ScanString is the workhorse of string scanning. It will scan a given
+ * string and returns all converted (and not ignored) values in a list back
+ * to the caller. If an error occured, a NULL pointer will be returned */
+
+Jim_Obj *Jim_ScanString(Jim_Interp *interp, Jim_Obj *strObjPtr,
+ Jim_Obj *fmtObjPtr, int flags)
+{
+ int i, strLen, pos, scanned = 1;
+ const char *str = Jim_GetString(strObjPtr, &strLen);
+ Jim_Obj *resultList = 0;
+ Jim_Obj **resultVec;
+ int resultc;
+ Jim_Obj *emptyStr = 0;
+ ScanFmtStringObj *fmtObj;
+
+ /* If format specification is not an object, convert it! */
+ if (fmtObjPtr->typePtr != &scanFmtStringObjType)
+ SetScanFmtFromAny(interp, fmtObjPtr);
+ fmtObj = (ScanFmtStringObj*)fmtObjPtr->internalRep.ptr;
+ /* Check if format specification was valid */
+ if (fmtObj->error != 0) {
+ if (flags & JIM_ERRMSG)
+ Jim_SetResultString(interp, fmtObj->error, -1);
+ return 0;
+ }
+ /* Allocate a new "shared" empty string for all unassigned conversions */
+ emptyStr = Jim_NewEmptyStringObj(interp);
+ Jim_IncrRefCount(emptyStr);
+ /* Create a list and fill it with empty strings up to max specified XPG3 */
+ resultList = Jim_NewListObj(interp, 0, 0);
+ if (fmtObj->maxPos > 0) {
+ for (i=0; i < fmtObj->maxPos; ++i)
+ Jim_ListAppendElement(interp, resultList, emptyStr);
+ JimListGetElements(interp, resultList, &resultc, &resultVec);
+ }
+ /* Now handle every partial format description */
+ for (i=0, pos=0; i < fmtObj->count; ++i) {
+ ScanFmtPartDescr *descr = &(fmtObj->descr[i]);
+ Jim_Obj *value = 0;
+ /* Only last type may be "literal" w/o conversion - skip it! */
+ if (descr->type == 0) continue;
+ /* As long as any conversion could be done, we will proceed */
+ if (scanned > 0)
+ scanned = ScanOneEntry(interp, str, pos, fmtObj, i, &value);
+ /* In case our first try results in EOF, we will leave */
+ if (scanned == -1 && i == 0)
+ goto eof;
+ /* Advance next pos-to-be-scanned for the amount scanned already */
+ pos += scanned;
+ /* value == 0 means no conversion took place so take empty string */
+ if (value == 0)
+ value = Jim_NewEmptyStringObj(interp);
+ /* If value is a non-assignable one, skip it */
+ if (descr->pos == -1) {
+ Jim_FreeNewObj(interp, value);
+ } else if (descr->pos == 0)
+ /* Otherwise append it to the result list if no XPG3 was given */
+ Jim_ListAppendElement(interp, resultList, value);
+ else if (resultVec[descr->pos-1] == emptyStr) {
+ /* But due to given XPG3, put the value into the corr. slot */
+ Jim_DecrRefCount(interp, resultVec[descr->pos-1]);
+ Jim_IncrRefCount(value);
+ resultVec[descr->pos-1] = value;
+ } else {
+ /* Otherwise, the slot was already used - free obj and ERROR */
+ Jim_FreeNewObj(interp, value);
+ goto err;
+ }
+ }
+ Jim_DecrRefCount(interp, emptyStr);
+ return resultList;
+eof:
+ Jim_DecrRefCount(interp, emptyStr);
+ Jim_FreeNewObj(interp, resultList);
+ return (Jim_Obj*)EOF;
+err:
+ Jim_DecrRefCount(interp, emptyStr);
+ Jim_FreeNewObj(interp, resultList);
+ return 0;
+}
+
+/* -----------------------------------------------------------------------------
* Dynamic libraries support (WIN32 not supported)
* ---------------------------------------------------------------------------*/
@@ -9941,6 +10380,70 @@ static int Jim_FormatCoreCommand(Jim_Interp *interp, int argc,
return JIM_OK;
}
+/* [scan] */
+static int Jim_ScanCoreCommand(Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv)
+{
+ Jim_Obj *listPtr, **outVec;
+ int outc, i, count = 0;
+
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "string formatString ?varName ...?");
+ return JIM_ERR;
+ }
+ if (argv[2]->typePtr != &scanFmtStringObjType)
+ SetScanFmtFromAny(interp, argv[2]);
+ if (FormatGetError(argv[2]) != 0) {
+ Jim_SetResultString(interp, FormatGetError(argv[2]), -1);
+ return JIM_ERR;
+ }
+ if (argc > 3) {
+ int maxPos = FormatGetMaxPos(argv[2]);
+ int count = FormatGetCnvCount(argv[2]);
+ if (maxPos > argc-3) {
+ Jim_SetResultString(interp, "\"%n$\" argument index out of range", -1);
+ return JIM_ERR;
+ } else if (count != 0 && count < argc-3) {
+ Jim_SetResultString(interp, "variable is not assigned by any "
+ "conversion specifiers", -1);
+ return JIM_ERR;
+ } else if (count > argc-3) {
+ Jim_SetResultString(interp, "different numbers of variable names and "
+ "field specifiers", -1);
+ return JIM_ERR;
+ }
+ }
+ listPtr = Jim_ScanString(interp, argv[1], argv[2], JIM_ERRMSG);
+ if (listPtr == 0)
+ return JIM_ERR;
+ if (argc > 3) {
+ if (listPtr == (Jim_Obj*)EOF) {
+ Jim_SetResult(interp, Jim_NewIntObj(interp, -1));
+ return JIM_OK;
+ }
+ JimListGetElements(interp, listPtr, &outc, &outVec);
+ for (i = 0; i < outc; ++i) {
+ if (Jim_Length(outVec[i]) > 0) {
+ ++count;
+ if (Jim_SetVariable(interp, argv[3+i], outVec[i]) != JIM_OK)
+ goto err;
+ }
+ }
+ Jim_FreeNewObj(interp, listPtr);
+ Jim_SetResult(interp, Jim_NewIntObj(interp, count));
+ } else {
+ if (listPtr == (Jim_Obj*)EOF) {
+ Jim_SetResult(interp, Jim_NewListObj(interp, 0, 0));
+ return JIM_OK;
+ }
+ Jim_SetResult(interp, listPtr);
+ }
+ return JIM_OK;
+err:
+ Jim_FreeNewObj(interp, listPtr);
+ return JIM_ERR;
+}
+
/* [error] */
static int Jim_ErrorCoreCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
@@ -10083,6 +10586,7 @@ static struct {
{"split", Jim_SplitCoreCommand},
{"join", Jim_JoinCoreCommand},
{"format", Jim_FormatCoreCommand},
+ {"scan", Jim_ScanCoreCommand},
{"error", Jim_ErrorCoreCommand},
{"lrange", Jim_LrangeCoreCommand},
{"env", Jim_EnvCoreCommand},
@@ -10206,7 +10710,7 @@ int Jim_InteractivePrompt(Jim_Interp *interp)
printf("Welcome to Jim version %d.%d, "
"Copyright (c) 2005 Salvatore Sanfilippo\n",
JIM_VERSION / 100, JIM_VERSION % 100);
- printf("CVS ID: $Id: jim.c,v 1.122 2005/03/19 21:39:34 antirez Exp $\n");
+ printf("CVS ID: $Id: jim.c,v 1.123 2005/03/21 11:59:44 chi Exp $\n");
Jim_SetVariableStrWithStr(interp, "jim_interactive", "1");
while (1) {
char buf[1024];
diff --git a/jim.h b/jim.h
index f74e140..aeb914e 100644
--- a/jim.h
+++ b/jim.h
@@ -1,7 +1,7 @@
/* Jim - A small embeddable Tcl interpreter
* Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
*
- * $Id: jim.h,v 1.61 2005/03/19 19:12:30 antirez Exp $
+ * $Id: jim.h,v 1.62 2005/03/21 11:59:44 chi Exp $
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -581,6 +581,8 @@ JIM_STATIC Jim_Obj * JIM_API(Jim_StringRangeObj) (Jim_Interp *interp,
Jim_Obj *lastObjPtr);
JIM_STATIC Jim_Obj * JIM_API(Jim_FormatString) (Jim_Interp *interp,
Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv);
+JIM_STATIC Jim_Obj * JIM_API(Jim_ScanString) (Jim_Interp *interp, Jim_Obj *strObjPtr,
+ Jim_Obj *fmtObjPtr, int flags);
JIM_STATIC int JIM_API(Jim_CompareStringImmediate) (Jim_Interp *interp,
Jim_Obj *objPtr, const char *str);
@@ -696,9 +698,9 @@ JIM_STATIC Jim_Obj * JIM_API(Jim_NewIntObj) (Jim_Interp *interp,
/* double object */
JIM_STATIC int JIM_API(Jim_GetDouble)(Jim_Interp *interp, Jim_Obj *objPtr,
- double *doublePtr);
+ double *doublePtr);
JIM_STATIC void JIM_API(Jim_SetDouble)(Jim_Interp *interp, Jim_Obj *objPtr,
- double doubleValue);
+ double doubleValue);
JIM_STATIC Jim_Obj * JIM_API(Jim_NewDoubleObj)(Jim_Interp *interp, double doubleValue);
/* shared strings */
@@ -781,6 +783,7 @@ static void Jim_InitExtension(Jim_Interp *interp, const char *version)
JIM_GET_API(StringMatchObj);
JIM_GET_API(StringRangeObj);
JIM_GET_API(FormatString);
+ JIM_GET_API(ScanString);
JIM_GET_API(CompareStringImmediate);
JIM_GET_API(NewReference);
JIM_GET_API(GetReference);
diff --git a/test.tcl b/test.tcl
index a8f4dd4..470a532 100644
--- a/test.tcl
+++ b/test.tcl
@@ -1,4 +1,4 @@
-# $Id: test.tcl,v 1.23 2005/03/19 21:39:34 antirez Exp $
+# $Id: test.tcl,v 1.24 2005/03/21 11:59:44 chi Exp $
#
# This are Tcl tests imported into Jim. Tests that will probably not be passed
# in the long term are usually removed (for example all the tests about
@@ -3444,6 +3444,607 @@ test lrange-2.4 {error conditions} {
#} {1 {unmatched open brace in list}}
################################################################################
+# SCAN
+################################################################################
+
+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-1.9 {BuildCharSet, CharInSet no match} {
+ catch {unset x}
+ list [scan {= f} {= %[TF]} x] [info exists x]
+} {0 0}
+
+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 - mixing "%" and "%n$" conversion specifiers} {
+ list [catch {scan {12 14} {%d%1$d}} msg] $msg
+} {1 {cannot mix "%" and "%n$" conversion specifiers}}
+test scan-3.2 {ValidateFormat - mixing "%" and "%n$" conversion specifiers} {
+ list [catch {scan {} {%d%1$d}} msg] $msg
+} {1 {cannot mix "%" and "%n$" conversion specifiers}}
+test scan-3.3 {ValidateFormat - "%n$" argument index out of range} { #FIXME
+ list [catch {scan {a} {%2$d%1$d} x}] [info exists x]
+} {1 1}
+test scan-3.4 {ValidateFormat} {
+ # degenerate case, before changed from 8.2 to 8.3
+ list [catch {scan {a} %d} msg] $msg
+} {0 {{}}}
+test scan-3.5 {ValidateFormat} {
+ list [catch {scan {aaaaaaaaaa} {%10c} a} msg] $msg
+} {1 {field width may not be specified in %c conversion}}
+test scan-3.6 {ValidateFormat} {
+ list [catch {scan {} {%*1$d} a} msg] $msg
+} {1 {bad scan conversion character}}
+test scan-3.7 {ValidateFormat} {
+ list [catch {scan {} {%1$d%1$d} a} msg] $msg
+} {1 {same "%n$" conversion specifier used more than once}}
+test scan-3.8 {ValidateFormat} {
+ list [catch {scan {} a x} msg] $msg
+} {1 {no any conversion specifier given}}
+test scan-3.9 {ValidateFormat} {
+ list [catch {scan {} {%2$s} x} msg] $msg
+} {1 {"%n$" argument index out of range}}
+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 formatString ?varName ...?"}}
+test scan-4.2 {Tcl_ScanObjCmd, argument checks} {
+ list [catch {scan string} msg] $msg
+} {1 {wrong # args: should be "scan string formatString ?varName ...?"}}
+test scan-4.3 {Tcl_ScanObjCmd, argument checks} {
+ # degenerate case, before changed from 8.2 to 8.3
+ list [catch {scan string format} msg] $msg
+} {1 {no any conversion specifier given}}
+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} {
+ # degenerate case, before changed from 8.2 to 8.3
+ list [catch {scan { abc def } { abc def }} msg] $msg
+} {1 {no any conversion specifier given}}
+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 1}
+test scan-4.40.1 {Tcl_ScanObjCmd, base-16 integer scanning} {
+ set x {}
+ list [scan {0xF 0x00A0B 0X0XF} {%x %x %x} x y z] $x $y $z
+} {3 15 2571 0}
+test scan-4.40.2 {Tcl_ScanObjCmd, base-16 integer scanning} {
+ catch {unset x}
+ list [scan {xF} {%x} x] [info exists x]
+} {0 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 {}}
+# Following test, Tcl will return {-1 {}}, but I do not understand why!
+# For me, its the same as 4.43
+test scan-4.44 {Tcl_ScanObjCmd, integer scanning, odd cases} {
+ set x {}
+ list [scan {+} {%i} x] $x
+} {0 {}}
+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.10000000000000001 0.20000000000000001 3.0}
+test scan-4.50 {Tcl_ScanObjCmd, float scanning} {
+ list [scan {12345678a} %f x] $x
+} {1 12345678.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.10000000000000001}
+# This test is as strange as 4.44 so I changed the outcome
+test scan-4.55 {Tcl_ScanObjCmd, odd cases} {
+ set x {}
+ list [scan {+} %f x] $x
+} {0 {}}
+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}
+# TODO: Enable following tests, if [format] works properly
+# procedure that returns the range of integers
+#proc int_range {} {
+# for { set MIN_INT 1 } { $MIN_INT > 0 } {} {
+# set MIN_INT [expr { $MIN_INT << 1 }]
+# }
+# set MAX_INT [expr { ~ $MIN_INT }]
+# return [list $MIN_INT $MAX_INT]
+#}
+#test scan-4.62 {scanning of large and negative octal integers} {
+# foreach { MIN_INT MAX_INT } [int_range] {}
+# set scanstring [format {%o %o %o} -1 $MIN_INT $MAX_INT]
+# list [scan $scanstring {%o %o %o} a b c] \
+# [expr { $a == -1 }] [expr { $b == $MIN_INT }] [expr { $c == $MAX_INT }]
+#} {3 1 1 1}
+#test scan-4.63 {scanning of large and negative hex integers} {
+# foreach { MIN_INT MAX_INT } [int_range] {}
+# set scanstring [format {%x %x %x} -1 $MIN_INT $MAX_INT]
+# list [scan $scanstring {%x %x %x} a b c] \
+# [expr { $a == -1 }] [expr { $b == $MIN_INT }] [expr { $c == $MAX_INT }]
+#} {3 1 1 1}
+
+# clean up from last two tests
+
+#catch {
+# rename int_range {}
+#}
+
+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-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-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-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-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-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-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-5.8 {integer scanning} {
+ set a {}; set b {}
+ list [scan "a 1234" "%d %d" a b] $a $b
+} {0 {} {}}
+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-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 {} {}}
+test scan-5.12 {integer scanning} {
+ set a {}; set b {}; set c {}
+ list [scan "7810179016327718216,6c63546f6c6c6548,661432506755433062510" \
+ %ld,%lx,%lo a b c] $a $b $c
+} {3 7810179016327718216 7810179016327718216 7810179016327718216}
+
+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.1000000000000001 -300000000.0 0.99961999999999995 {}}
+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.1999999999999993}
+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}
+#~ #
+#~ # Some libc implementations consider 3.e- bad input. The ANSI
+#~ # spec states that digits must follow the - sign.
+#~ #
+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-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.5999999999999996 99999.699999999997 87.643000000000001 118.0}
+test scan-6.6 {floating-point scanning} {
+ 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.2344999999999999 0.69699999999999995 124.0 5.0000000000000002e-05}
+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.5999999999999996 {} {} {}}
+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.5999999999999996 5.2000000000000002 {} {}}
+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-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-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-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-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}
+#
+# FOLLOWING TESTS DISABLED DUE TO LACK OF UNICODE HANDLING
+#
+#~ 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-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-7.8 {string and character scanning, unicode} {
+ #~ set a {}; set b {}
+ #~ list [scan "ab\ufeffdef" "%\[ab\ufeff\]" a] $a
+#~ } "1 ab\ufeff"
+
+test scan-8.1 {error conditions} {
+ catch {scan a}
+} 1
+test scan-8.2 {error conditions} {
+ catch {scan a} msg
+ set msg
+} {wrong # args: should be "scan string formatString ?varName ...?"}
+test scan-8.3 {error conditions} {
+ list [catch {scan a %D x} msg] $msg
+} {1 {bad scan conversion character}}
+test scan-8.4 {error conditions} {
+ list [catch {scan a %O x} msg] $msg
+} {1 {bad scan conversion character}}
+test scan-8.5 {error conditions} {
+ list [catch {scan a %X x} msg] $msg
+} {1 {bad scan conversion character}}
+test scan-8.6 {error conditions} {
+ list [catch {scan a %F x} msg] $msg
+} {1 {bad scan conversion character}}
+test scan-8.7 {error conditions} {
+ list [catch {scan a %E x} msg] $msg
+} {1 {bad scan conversion character}}
+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-8.9 {error conditions} {
+ list [catch {scan a "%d %d" a b c} msg] $msg
+} {1 {variable is not assigned by any conversion specifiers}}
+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-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-8.12 {error conditions} {
+ list [catch {scan 44 %2c a} msg] $msg
+} {1 {field width may not be specified in %c conversion}}
+test scan-8.13 {error conditions} {
+ list [catch {scan abc {%[} x} msg] $msg
+} {1 {unmatched [ in format string}}
+test scan-8.14 {error conditions} {
+ list [catch {scan abc {%[^a} x} msg] $msg
+} {1 {unmatched [ in format string}}
+test scan-8.15 {error conditions} {
+ list [catch {scan abc {%[^]a} x} msg] $msg
+} {1 {unmatched [ in format string}}
+test scan-8.16 {error conditions} {
+ list [catch {scan abc {%[]a} x} msg] $msg
+} {1 {unmatched [ in format string}}
+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-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-10.1 {miscellaneous tests} {
+ set a {}
+ list [scan ab16c ab%dc a] $a
+} {1 16}
+test scan-10.2 {miscellaneous tests} {
+ set a {}
+ list [scan ax16c ab%dc a] $a
+} {0 {}}
+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-10.4 {miscellaneous tests} {
+ set a {}
+ list [catch {scan ab%c14 ab%%c%d a} msg] $msg $a
+} {0 1 14}
+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-11.1 {alignment in results array (TCL_ALIGN)} {
+ scan "123 13.6" "%s %f" a b
+ set b
+} 13.6
+test scan-11.2 {alignment in results array (TCL_ALIGN)} {
+ scan "1234567 13.6" "%s %f" a b
+ set b
+} 13.6
+test scan-11.3 {alignment in results array (TCL_ALIGN)} {
+ scan "12345678901 13.6" "%s %f" a b
+ set b
+} 13.6
+test scan-11.4 {alignment in results array (TCL_ALIGN)} {
+ scan "123456789012345 13.6" "%s %f" a b
+ set b
+} 13.6
+test scan-11.5 {alignment in results array (TCL_ALIGN)} {
+ scan "1234567890123456789 13.6" "%s %f" a b
+ set b
+} 13.6
+test scan-12.1 {Tcl_ScanObjCmd, inline case} {
+ scan a %c
+} 97
+test scan-12.2 {Tcl_ScanObjCmd, inline case} {
+ scan abc %c%c%c%c
+} {97 98 99 {}}
+test scan-12.3 {Tcl_ScanObjCmd, inline case} {
+ scan abc %s%c
+} {abc {}}
+test scan-12.4 {Tcl_ScanObjCmd, inline case, underflow} {
+ scan abc abc%c
+} {}
+test scan-12.5 {Tcl_ScanObjCmd, inline case} {
+ scan abc bogus%c%c%c
+} {{} {} {}}
+#
+# Expected result of following test was changed. Tcl expects {0 {}}, but
+# I feel a complain is correct, as no conversion ever can take place!
+#
+test scan-12.6 {Tcl_ScanObjCmd, inline case} {
+ # degenerate case, behavior changed from 8.2 to 8.3
+ list [catch {scan foo foobar} msg] $msg
+} {1 {no any conversion specifier given}}
+test scan-12.7 {Tcl_ScanObjCmd, inline case 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 %d"
+} {10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 {}}
+test scan-13.1 {Tcl_ScanObjCmd, inline XPG case} {
+ scan a {%1$c}
+} 97
+test scan-13.2 {Tcl_ScanObjCmd, inline XPG case} {
+ scan abc {%1$c%2$c%3$c%4$c}
+} {97 98 99 {}}
+test scan-13.3 {Tcl_ScanObjCmd, inline XPG case} {
+ list [catch {scan abc {%1$c%1$c}} msg] $msg
+} {1 {same "%n$" conversion specifier used more than once}}
+test scan-13.4 {Tcl_ScanObjCmd, inline XPG case} {
+ scan abc {%2$s%1$c}
+} {{} abc}
+test scan-13.5 {Tcl_ScanObjCmd, inline XPG case, underflow} {
+ list [catch {scan abc {abc%5$c}} msg] $msg
+} {0 {}}
+test scan-13.6 {Tcl_ScanObjCmd, inline XPG case} {
+ catch {scan abc {bogus%1$c%5$c%10$c}} msg
+ list [llength $msg] $msg
+} {10 {{} {} {} {} {} {} {} {} {} {}}}
+test scan-13.7 {Tcl_ScanObjCmd, inline XPG case lots of arguments} {
+ scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" {%20$d %18$d %17$d %16$d %15$d %14$d %13$d %12$d %11$d %10$d %9$d %8$d %7$d %6$d %5$d %4$d %3$d %2$d %1$d}
+} {190 180 170 160 150 140 130 120 110 100 90 80 70 60 50 40 30 20 {} 10}
+test scan-13.8 {Tcl_ScanObjCmd, inline XPG case lots of arguments} {
+ set msg [scan "10 20 30" {%100$d %5$d %200$d}]
+ list [llength $msg] [lindex $msg 99] [lindex $msg 4] [lindex $msg 199]
+} {200 10 20 30}
+
+
+################################################################################
# JIM REGRESSION TESTS
################################################################################
test regression-1.0 {Rename against procedures with static vars} {