diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2011-08-29 07:25:27 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2011-08-29 07:25:27 (GMT) |
commit | 4bf140f4c07b28a2391994ffe9de23029fba40cf (patch) | |
tree | 564b96eb9bd243353e9b3779ad57ce1655fa6f1f /generic/tclStringObj.c | |
parent | 435b4283f54c233f5e59210eaf786bf3c0e0bb39 (diff) | |
download | tcl-4bf140f4c07b28a2391994ffe9de23029fba40cf.zip tcl-4bf140f4c07b28a2391994ffe9de23029fba40cf.tar.gz tcl-4bf140f4c07b28a2391994ffe9de23029fba40cf.tar.bz2 |
[3396731] inline string reverse: minor further improvements
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r-- | generic/tclStringObj.c | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c index bccd28a..d721c47 100644 --- a/generic/tclStringObj.c +++ b/generic/tclStringObj.c @@ -2653,47 +2653,24 @@ Tcl_ObjPrintf( *--------------------------------------------------------------------------- */ -void +static void ReverseBytes( unsigned char *to, /* Copy bytes into here... */ unsigned char *from, /* ...from here... */ int count) /* Until this many are copied, */ /* reversing as you go. */ { - unsigned char *src = from + count - 1; + unsigned char *src = from + count; if (to == from) { /* Reversing in place */ - while (to < src) { + while (--src > to) { unsigned char c = *src; - *src-- = *to; - *to++ = c; - } - } else { - while (src >= from) { - *to++ = *src--; - } - } -} - -void -ReverseUniChars( - Tcl_UniChar *to, /* Copy Tcl_UniChars into here... */ - Tcl_UniChar *from, /* ...from here... */ - unsigned int count) /* Until this many are copied, */ - /* reversing as you go. */ -{ - Tcl_UniChar *src = from + count - 1; - if (to == from) { - /* Reversing in place */ - from += count - 1; - while (to < src) { - Tcl_UniChar c = *src; - *src-- = *to; + *src = *to; *to++ = c; } } else { - while (src >= from) { - *to++ = *src--; + while (--src >= from) { + *to++ = *src; } } } @@ -2703,6 +2680,7 @@ TclStringObjReverse( Tcl_Obj *objPtr) { String *stringPtr; + Tcl_UniChar ch; if (TclIsPureByteArray(objPtr)) { int numBytes; @@ -2720,18 +2698,31 @@ TclStringObjReverse( if (stringPtr->hasUnicode) { Tcl_UniChar *from = Tcl_GetUnicode(objPtr); + Tcl_UniChar *src = from + stringPtr->numChars; if (Tcl_IsShared(objPtr)) { + Tcl_UniChar *to; + /* * Create a non-empty, pure unicode value, so we can coax * Tcl_SetObjLength into growing the unicode rep buffer. */ - Tcl_UniChar ch = 0; + ch = 0; objPtr = Tcl_NewUnicodeObj(&ch, 1); Tcl_SetObjLength(objPtr, stringPtr->numChars); + to = Tcl_GetUnicode(objPtr); + while (--src >= from) { + *to++ = *src; + } + } else { + /* Reversing in place */ + while (--src > from) { + ch = *src; + *src = *from; + *from++ = ch; + } } - ReverseUniChars(Tcl_GetUnicode(objPtr), from, stringPtr->numChars); } if (objPtr->bytes) { @@ -2763,7 +2754,6 @@ TclStringObjReverse( * It's part of the contract for objPtr->bytes values. * Thus, we can skip calling Tcl_UtfCharComplete() here. */ - Tcl_UniChar ch = 0; int bytesInChar = Tcl_UtfToUniChar(from, &ch); ReverseBytes((unsigned char *)to, (unsigned char *)from, |