Difference between revisions of "SubStringLastIndex"

From Second Life Wiki
Jump to navigation Jump to search
(normally i disapprove of such shenanigans, but then I also disapprove of using recursion, so it's moot. This should keep the heap from ballooning as quickly (which will help in LSO but not in Mono))
m (More obvious this way (avoids the negative index issue), since we evaluate from right to left, the recursion should happen before the parameters for the add are pushed onto the stack.)
Line 42: Line 42:
         return 0;
         return 0;
     integer index = llSubStringIndex(vStrSrc, vStrTst);
     integer index = llSubStringIndex(vStrSrc, vStrTst);
    integer index2;
     if (index == -1) //found, look again
     if (index != -1) //found, look again
         return -1;
         index2 = uSubStringLastIndex(llGetSubString(vStrSrc, index + 1, -1), (vStrSrc = vStrTst = "") + vStrTst) + 1;
    return index + 1 + uSubStringLastIndex(llDeleteSubString(vStrSrc, 0, index), (vStrSrc = vStrTst = "") + vStrTst);
    return index + index2;
}
}
/*// Contributed Freely to the Public Domain without limitation by Sasun Steinbeck. //*/</lsl>
/*// Contributed Freely to the Public Domain without limitation by Sasun Steinbeck. //*/</lsl>

Revision as of 23:22, 23 March 2012

User-Defined Function: integer uSubStringLastIndex( string vStrSrc, string vStrTst );

Returns a integer that is the positive index of the last vStrTst within vStrSrc

  • vStrSrc: source string to check
  • vStrTst: string to look for

if vStrTst is not found in vStrSrc -1 is returned.
the index of the first character is 0


Code:

  • LSO: 182 bytes
  • MONO: 1024 bytes

<lsl>integer uSubStringLastIndex( string vStrSrc, string vStrTst ){

   integer vIdxFnd =
     llStringLength( vStrSrc ) -
     llStringLength( vStrTst ) -
     llStringLength(
       llList2String(
         llParseStringKeepNulls( vStrSrc, (list)vStrTst, [] ),
         0xFFFFFFFF ) //-- (-1)
       );
return (vIdxFnd

Caveats

  • Performs a literal match (case sensitive).
    • Wildcards and RegEx are not supported.
  • Attempting to match an empty string ("") will return 0 instead of -1.

Notes

  • This function is operates exactly like llSubStringIndex (including caveats), from the opposite end of the string.