StringReverse

From Second Life Wiki
Revision as of 01:47, 29 May 2010 by Void Singer (talk | contribs) (Moved out of user page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Function: string uStringRev( string vStrSrc );

Returns a string that is vStrSrc with the characters in reverse order

  • vStrSrc: source string to reverse

Code:

Smaller version

  • LSO: 169 bytes
  • MONO: 512 bytes

<lsl>string uStringRev( string vStrSrc ){

   integer vIntCnt = llStringLength( vStrSrc );
   while (vIntCnt){
       vStrSrc += llGetSubString( vStrSrc, (vIntCnt = ~-vIntCnt), vIntCnt );
       vStrSrc = llDeleteSubString( vStrSrc, vIntCnt, vIntCnt );
   }
   return vStrSrc;

} /*//-- Anti-License Text --//*/ /*// Contributed Freely to the Public Domain without limitation. //*/ /*// 2009 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ] //*/ /*// Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ] //*/ /*//-- --//*/</lsl>

Faster version

  • LSO: 184 bytes
  • MONO: 512 bytes

<lsl>string uStringRev( string vStrSrc ){

   integer vIntCnt = llStringLength( vStrSrc );
   while (vIntCnt){
       vStrSrc += llGetSubString( vStrSrc, (vIntCnt = ~-vIntCnt), vIntCnt );
   }
   return llGetSubString( vStrSrc, llStringLength( vStrSrc ) >> 1, 0xFFFFFFFF );

} /*//-- Anti-License Text --//*/ /*// Contributed Freely to the Public Domain without limitation. //*/ /*// 2009 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ] //*/ /*// Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ] //*/

/*//-- --//*/</lsl>

Cveats:

  • The "Faster" version doubles the string size internally, and is more likely to cause a stack/heap collision(script crash) with large strings and or limited free memory