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 | 7fa60e4fb188f417e4b968ef37085cc9c1c171e2 (patch) | |
tree | 564b96eb9bd243353e9b3779ad57ce1655fa6f1f /generic | |
parent | 1e29bab5af753724511fb41ab9fcf3148a3f4067 (diff) | |
download | tcl-7fa60e4fb188f417e4b968ef37085cc9c1c171e2.zip tcl-7fa60e4fb188f417e4b968ef37085cc9c1c171e2.tar.gz tcl-7fa60e4fb188f417e4b968ef37085cc9c1c171e2.tar.bz2 |
[3396731] inline string reverse: minor further improvements
Diffstat (limited to 'generic')
-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, |