diff options
-rw-r--r-- | doc/string.n | 12 | ||||
-rw-r--r-- | generic/tclCmdMZ.c | 27 | ||||
-rw-r--r-- | tests/cmdMZ.test | 34 | ||||
-rw-r--r-- | tests/string.test | 8 |
4 files changed, 57 insertions, 24 deletions
diff --git a/doc/string.n b/doc/string.n index 0071b32..81df8b0 100644 --- a/doc/string.n +++ b/doc/string.n @@ -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: string.n,v 1.4 1999/05/04 01:33:10 stanton Exp $ +'\" RCS: @(#) $Id: string.n,v 1.5 1999/05/04 02:57:54 stanton Exp $ '\" .so man.macros .TH string n 8.1 Tcl "Tcl Built-In Commands" @@ -23,6 +23,10 @@ Performs one of several string operations, depending on \fIoption\fR. The legal \fIoption\fRs (which may be abbreviated) are: .VS 8.1 .TP +\fBstring bytelength \fIstring\fR +Returns a decimal string giving the number of bytes in the +\fIstring\fR when represented as UTF-8. +.TP \fBstring compare \fIstring1 string2\fR ?\fIlength\fR? .VE 8.1 Perform a character-by-character comparison of strings \fIstring1\fR and @@ -70,11 +74,11 @@ follows: The char specified at this numerical index .IP \fBend\fR 10 The last char of the string. -.IP \fBexpression\fR 10 +.IP \fIexpression\fR 10 A Tcl expression that returns a number. -.IP \fBend[+-]expression\fR 10 +.IP \fBend[+-]\fIexpression\fR 10 The last char of the string plus or minus the number specified -in the expression (ie: end-1). +in the expression (e.g. \fBend-1\fR). .RE .VE 8.1 If \fIcharIndex\fR is less than 0 or greater than diff --git a/generic/tclCmdMZ.c b/generic/tclCmdMZ.c index 52cdf10..90b9687 100644 --- a/generic/tclCmdMZ.c +++ b/generic/tclCmdMZ.c @@ -13,7 +13,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclCmdMZ.c,v 1.4 1999/05/04 01:33:10 stanton Exp $ + * RCS: @(#) $Id: tclCmdMZ.c,v 1.5 1999/05/04 02:57:55 stanton Exp $ */ #include "tclInt.h" @@ -806,7 +806,7 @@ Tcl_StringObjCmd(dummy, interp, objc, objv) char *string1, *string2; int length1, length2; static char *options[] = { - "bytes", "compare", "equal", "first", + "bytelength", "compare", "equal", "first", "icompare", "iequal", "index", "last", "length", "map", "match", "range", "repeat", "replace", @@ -815,7 +815,7 @@ Tcl_StringObjCmd(dummy, interp, objc, objv) "wordend", "wordstart", (char *) NULL }; enum options { - STR_BYTES, STR_COMPARE, STR_EQUAL, STR_FIRST, + STR_BYTELENGTH, STR_COMPARE, STR_EQUAL, STR_FIRST, STR_ICOMPARE, STR_IEQUAL, STR_INDEX, STR_LAST, STR_LENGTH, STR_MAP, STR_MATCH, STR_RANGE, STR_REPEAT, STR_REPLACE, @@ -1044,18 +1044,31 @@ Tcl_StringObjCmd(dummy, interp, objc, objv) Tcl_SetIntObj(resultPtr, match); break; } - case STR_BYTES: + case STR_BYTELENGTH: case STR_LENGTH: { if (objc != 3) { Tcl_WrongNumArgs(interp, 2, objv, "string"); return TCL_ERROR; } - string1 = Tcl_GetStringFromObj(objv[2], &length1); - if ((enum options) index == STR_BYTES) { + if ((enum options) index == STR_BYTELENGTH) { + string1 = Tcl_GetStringFromObj(objv[2], &length1); Tcl_SetIntObj(resultPtr, length1); } else { - Tcl_SetIntObj(resultPtr, Tcl_NumUtfChars(string1, length1)); + /* + * If we have a ByteArray object, avoid recomputing the + * string since the byte array contains one byte per + * character. + */ + + if (objv[2]->typePtr == &tclByteArrayType) { + string1 = Tcl_GetByteArrayFromObj(objv[2], &length1); + Tcl_SetIntObj(resultPtr, length1); + } else { + string1 = Tcl_GetStringFromObj(objv[2], &length1); + Tcl_SetIntObj(resultPtr, Tcl_NumUtfChars(string1, + length1)); + } } break; } diff --git a/tests/cmdMZ.test b/tests/cmdMZ.test index c9ead57..285df0d 100644 --- a/tests/cmdMZ.test +++ b/tests/cmdMZ.test @@ -11,7 +11,7 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: cmdMZ.test,v 1.3 1999/05/04 01:33:11 stanton Exp $ +# RCS: @(#) $Id: cmdMZ.test,v 1.4 1999/05/04 02:57:55 stanton Exp $ if {[lsearch [namespace children] ::tcltest] == -1} { source [file join [pwd] [file dirname [info script]] defs.tcl] @@ -159,7 +159,7 @@ test cmdMZ-5.1 {Tcl_StringObjCmd: error conditions} { } {1 {wrong # args: should be "string option arg ?arg ...?"}} test cmdMZ-5.2 {Tcl_StringObjCmd: error conditions} { list [catch {string gorp a b} msg] $msg -} {1 {bad option "gorp": must be bytes, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} +} {1 {bad option "gorp": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} test cmdMZ-6.1 {Tcl_StringObjCmd: string compare} { list [catch {string compare a} msg] $msg @@ -279,21 +279,36 @@ test cmdMZ-9.7 {Tcl_StringObjCmd: string last, unicode} { string las \u7266 xxxx12\u7266xx345x678 } 6 -test cmdMZ-10.1 {Tcl_StringObjCmd: string length} { +test cmdMZ-10.1 {Tcl_StringObjCmd: string bytelength} { + list [catch {string bytelength} msg] $msg +} {1 {wrong # args: should be "string bytelength string"}} +test cmdMZ-10.2 {Tcl_StringObjCmd: string bytelength} { + list [catch {string bytelength a b} msg] $msg +} {1 {wrong # args: should be "string bytelength string"}} +test cmdMZ-10.3 {Tcl_StringObjCmd: string bytelength} { + string bytelength "\u00c7" +} 2 +test cmdMZ-10.4 {Tcl_StringObjCmd: string bytelength} { + string b "" +} 0 +test cmdMZ-10.5 {Tcl_StringObjCmd: string length} { list [catch {string length} msg] $msg } {1 {wrong # args: should be "string length string"}} -test cmdMZ-10.2 {Tcl_StringObjCmd: string length} { +test cmdMZ-10.6 {Tcl_StringObjCmd: string length} { list [catch {string length a b} msg] $msg } {1 {wrong # args: should be "string length string"}} -test cmdMZ-10.3 {Tcl_StringObjCmd: string length} { +test cmdMZ-10.7 {Tcl_StringObjCmd: string length} { string length "a little string" } 15 -test cmdMZ-10.4 {Tcl_StringObjCmd: string length} { +test cmdMZ-10.8 {Tcl_StringObjCmd: string length} { string le "" } 0 -test cmdMZ-10.5 {Tcl_StringObjCmd: string length, unicode} { +test cmdMZ-10.9 {Tcl_StringObjCmd: string length, unicode} { string le "abcd\u7266" } 5 +test cmdMZ-10.10 {Tcl_StringObjCmd: string length, byte arrays} { + string le [binary format B 1] +} 1 test cmdMZ-11.1 {Tcl_StringObjCmd: string match} { list [catch {string match a} msg] $msg @@ -478,7 +493,7 @@ test cmdMZ-18.4 {Tcl_StringObjCmd: string trimright errors} { } {1 {wrong # args: should be "string trimright string ?chars?"}} test cmdMZ-18.5 {Tcl_StringObjCmd: string trimright errors} { list [catch {string trimg a} msg] $msg -} {1 {bad option "trimg": must be bytes, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} +} {1 {bad option "trimg": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} test cmdMZ-19.1 {Tcl_StringObjCmd: string wordend} { list [catch {string wordend a} msg] $msg @@ -522,7 +537,7 @@ test cmdMZ-19.13 {Tcl_StringObjCmd: string wordend, unicode} { test cmdMZ-20.1 {Tcl_StringObjCmd: string wordstart} { list [catch {string word a} msg] $msg -} {1 {ambiguous option "word": must be bytes, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} +} {1 {ambiguous option "word": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} test cmdMZ-20.2 {Tcl_StringObjCmd: string wordstart} { list [catch {string wordstart a} msg] $msg } {1 {wrong # args: should be "string wordstart string index"}} @@ -579,3 +594,4 @@ return + diff --git a/tests/string.test b/tests/string.test index 013cde4..3809ba9 100644 --- a/tests/string.test +++ b/tests/string.test @@ -11,7 +11,7 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: string.test,v 1.6 1999/05/04 01:33:12 stanton Exp $ +# RCS: @(#) $Id: string.test,v 1.7 1999/05/04 02:57:55 stanton Exp $ if {[lsearch [namespace children] ::tcltest] == -1} { source [file join [pwd] [file dirname [info script]] defs.tcl] @@ -310,7 +310,7 @@ test string-10.4 {string trimright errors} { } {1 {wrong # args: should be "string trimright string ?chars?"}} test string-10.5 {string trimright errors} { list [catch {string trimg a} msg] $msg -} {1 {bad option "trimg": must be bytes, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} +} {1 {bad option "trimg": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} test string-11.1 {string tolower} { string tolower ABCDeF @@ -435,7 +435,7 @@ test string-14.9 {string wordend} { test string-15.1 {string wordstart} { list [catch {string word a} msg] $msg -} {1 {ambiguous option "word": must be bytes, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} +} {1 {ambiguous option "word": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} test string-15.2 {string wordstart} { list [catch {string wordstart a} msg] $msg } {1 {wrong # args: should be "string wordstart string index"}} @@ -466,7 +466,7 @@ test string-15.10 {string wordstart} { test string-16.1 {error conditions} { list [catch {string gorp a b} msg] $msg -} {1 {bad option "gorp": must be bytes, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} +} {1 {bad option "gorp": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}} test string-16.2 {error conditions} { list [catch {string} msg] $msg } {1 {wrong # args: should be "string option arg ?arg ...?"}} |