diff options
author | dgp <dgp@users.sourceforge.net> | 2011-04-28 14:39:42 (GMT) |
---|---|---|
committer | dgp <dgp@users.sourceforge.net> | 2011-04-28 14:39:42 (GMT) |
commit | 5702e695062ec36c3addd403d748af48164db8b1 (patch) | |
tree | 0d25400a3f10a579b1905153391d01db6cb2a9d4 /generic/tclUtil.c | |
parent | 24517f610fc4cd90b353209a77a26e7818c7a209 (diff) | |
download | tcl-5702e695062ec36c3addd403d748af48164db8b1.zip tcl-5702e695062ec36c3addd403d748af48164db8b1.tar.gz tcl-5702e695062ec36c3addd403d748af48164db8b1.tar.bz2 |
New utility routines: TclIsSpaceProc() and TclCountSpaceRuns()
Use to replace calls to isspace() and their /* INTL */ risk.
Diffstat (limited to 'generic/tclUtil.c')
-rw-r--r-- | generic/tclUtil.c | 95 |
1 files changed, 61 insertions, 34 deletions
diff --git a/generic/tclUtil.c b/generic/tclUtil.c index b3e1e08..4a7f79f 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -89,6 +89,58 @@ Tcl_ObjType tclEndOffsetType = { /* *---------------------------------------------------------------------- * + * TclCountSpaceRuns -- + * + * Given 'bytes' pointing to 'numBytes' bytes, scan through them and + * count the number of whitespace runs that could be list element + * separators. If 'numBytes' is -1, scan to the terminating '\0'. + * + * Results: + * Returns the count. If 'endPtr' is not NULL, writes a pointer to + * the end of the string scanned there. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +int +TclCountSpaceRuns( + CONST char *bytes, + int numBytes, + CONST char **endPtr) +{ + int count = 0; + + while (numBytes) { + if ((numBytes == -1) && (*bytes == '\0')) { + break; + } + if (TclIsSpaceProc(*bytes)) { + /* Space run started; bump count */ + count++; + do { + bytes++; + numBytes -= (numBytes != -1); + } while (numBytes && TclIsSpaceProc(*bytes)); + if (numBytes == 0) { + break; + } + /* (*bytes) is non-space; return to counting state */ + } + bytes++; + numBytes -= (numBytes != -1); + } + if (endPtr) { + *endPtr = bytes; + } + return count; +} + +/* + *---------------------------------------------------------------------- + * * TclFindElement -- * * Given a pointer into a Tcl list, locate the first (or next) element in @@ -155,7 +207,7 @@ TclFindElement( */ limit = (list + listLength); - while ((p < limit) && (isspace(UCHAR(*p)))) { /* INTL: ISO space. */ + while ((p < limit) && (TclIsSpaceProc(*p))) { p++; } if (p == limit) { /* no element found */ @@ -203,8 +255,7 @@ TclFindElement( } else if (openBraces == 1) { size = (p - elemStart); p++; - if ((p >= limit) - || isspace(UCHAR(*p))) { /* INTL: ISO space. */ + if ((p >= limit) || TclIsSpaceProc(*p)) { goto done; } @@ -214,8 +265,7 @@ TclFindElement( if (interp != NULL) { p2 = p; - while ((p2 < limit) - && (!isspace(UCHAR(*p2))) /* INTL: ISO space. */ + while ((p2 < limit) && (!TclIsSpaceProc(*p2)) && (p2 < p+20)) { p2++; } @@ -262,8 +312,7 @@ TclFindElement( if (inQuotes) { size = (p - elemStart); p++; - if ((p >= limit) - || isspace(UCHAR(*p))) { /* INTL: ISO space */ + if ((p >= limit) || TclIsSpaceProc(*p)) { goto done; } @@ -273,8 +322,7 @@ TclFindElement( if (interp != NULL) { p2 = p; - while ((p2 < limit) - && (!isspace(UCHAR(*p2))) /* INTL: ISO space */ + while ((p2 < limit) && (!TclIsSpaceProc(*p2)) && (p2 < p+20)) { p2++; } @@ -311,7 +359,7 @@ TclFindElement( } done: - while ((p < limit) && (isspace(UCHAR(*p)))) { /* INTL: ISO space. */ + while ((p < limit) && (TclIsSpaceProc(*p))) { p++; } *elementPtr = elemStart; @@ -409,7 +457,7 @@ Tcl_SplitList( CONST char ***argvPtr) /* Pointer to place to store pointer to array * of pointers to list elements. */ { - CONST char **argv, *l, *element; + CONST char **argv, *end, *element; char *p; int length, size, i, result, elSize, brace; @@ -420,29 +468,8 @@ Tcl_SplitList( * the list. */ - for (size = 2, l = list; *l != 0; l++) { - if (isspace(UCHAR(*l))) { /* INTL: ISO space. */ - size++; - - /* - * Consecutive space can only count as a single list delimiter. - */ - - while (1) { - char next = *(l + 1); - - if (next == '\0') { - break; - } - ++l; - if (isspace(UCHAR(next))) { /* INTL: ISO space. */ - continue; - } - break; - } - } - } - length = l - list; + size = TclCountSpaceRuns(list, -1, &end) + 2; + length = end - list; argv = (CONST char **) ckalloc((unsigned) ((size * sizeof(char *)) + length + 1)); for (i = 0, p = ((char *) argv) + size*sizeof(char *); |