From b81b15c0f284194a10d17be0a8daa622ec578d94 Mon Sep 17 00:00:00 2001 From: hershey Date: Fri, 30 Apr 1999 16:22:24 +0000 Subject: Tcl_UtfToUpper and Tcl_UtfToTitle now works on badly formed Utf strings. --- generic/tclUtf.c | 29 +++++++++++++++++++++++------ tests/string.test | 21 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/generic/tclUtf.c b/generic/tclUtf.c index a04dca8..0c46d26 100644 --- a/generic/tclUtf.c +++ b/generic/tclUtf.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: tclUtf.c,v 1.3 1999/04/29 00:04:41 hershey Exp $ + * RCS: @(#) $Id: tclUtf.c,v 1.4 1999/04/30 16:22:24 hershey Exp $ */ #include "tclInt.h" @@ -1006,8 +1006,9 @@ int Tcl_UtfToTitle(str) char *str; /* String to convert in place. */ { - Tcl_UniChar ch; + Tcl_UniChar ch, titleChar, lowChar; char *src, *dst; + int bytes; /* * Capitalize the first character and then lowercase the rest of the @@ -1017,12 +1018,28 @@ Tcl_UtfToTitle(str) src = dst = str; if (*src) { - src += Tcl_UtfToUniChar(src, &ch); - dst += Tcl_UniCharToUtf(Tcl_UniCharToTitle(ch), dst); + bytes = Tcl_UtfToUniChar(src, &ch); + titleChar = Tcl_UniCharToTitle(ch); + + if (bytes < UtfCount(titleChar)) { + memcpy(dst, src, (size_t) bytes); + dst += bytes; + } else { + dst += Tcl_UniCharToUtf(titleChar, dst); + } + src += bytes; } while (*src) { - src += Tcl_UtfToUniChar(src, &ch); - dst += Tcl_UniCharToUtf(Tcl_UniCharToLower(ch), dst); + bytes = Tcl_UtfToUniChar(src, &ch); + lowChar = Tcl_UniCharToLower(ch); + + if (bytes < UtfCount(lowChar)) { + memcpy(dst, src, (size_t) bytes); + dst += bytes; + } else { + dst += Tcl_UniCharToUtf(lowChar, dst); + } + src += bytes; } *dst = '\0'; return (dst - str); diff --git a/tests/string.test b/tests/string.test index 5efcdbc..1648dc4 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.4 1999/04/29 18:14:29 hershey Exp $ +# RCS: @(#) $Id: string.test,v 1.5 1999/04/30 16:22:25 hershey Exp $ if {[lsearch [namespace children] ::tcltest] == -1} { source [file join [pwd] [file dirname [info script]] defs.tcl] @@ -313,6 +313,25 @@ test string-11.6 {string tolower called with badly formed Utf string} { string tolower [bytestring "\u00fcBER"] } [bytestring "\u00fcber"] +test string-11.7 {string totitle} { + string totitle ABCDeF +} {Abcdef} +test string-11.8 {string totitle} { + string totitle "aBC d Hij xyZ" +} {Abc d hij xyz} +test string-11.9 {string totitle} { + string totitle {123#$&*()} +} {123#$&*()} +test string-11.10 {string totitle} { + list [catch {string totitle} msg] $msg +} {1 {wrong # args: should be "string totitle string"}} +test string-11.11 {string totitle} { + list [catch {string totitle a b} msg] $msg +} {1 {wrong # args: should be "string totitle string"}} +test string-11.12 {string totitle called with badly formed Utf string} { + string totitle [bytestring "\u00fcBER"] +} [bytestring "\u00fcber"] + test string-12.1 {string toupper} { string toupper abCDEf } {ABCDEF} -- cgit v0.12