String FindLast bug
Monkey Forums/Monkey Bug Reports/String FindLast bug| 
 | ||
| (V81b) String's FindLast behavior is incorrect when specifying a starting offset value. Example: Strict Function Main:Int() Local str1:String = "John loves Denny. Denny loves John." Local str2:String = "Denny" Local start:Int = 23 Local result:Int = str1.FindLast(str2, start) If result >= 0 Print "Found ~q" + str2 + "~q at offset " + result Else Print "Did not find ~q" + str2 + "~q after offset " + start Endif Return 0 End Resultant output: Found "Denny" at offset 18 Expected output: Did not find "Denny" after offset 23 Tested on HTML5, C++ and Desktop targets. Same behavior on all three. FindLast should return -1 when a string does not exist after a given starting offset. Currently, if the string is not found above the starting offset, it continues checking below the starting offset. To fix (for CPP Tool / Desktop targets) change the FindLast(String, int) method of the String class (in lang.cpp) to something like: 
	int FindLast( String find,int start )const{
		
		int exit=start;
		
		if (exit < 0) 
			exit=0;
		else
			if (exit > rep->length)
				exit = rep->length;
		
		start=rep->length-find.rep->length;
		while( start>=exit){
			if( !t_memcmp( rep->data+start,find.rep->data,find.rep->length ) ) return start;
			--start;
		}
		return -1;
	}
The other target platforms' native code will require equivalent modifications. | 
| 
 | ||
| The idea here is that the string is searched backwards, therefore only substrings appearing at positions <= startIndex will be compared. This is sort of like C# string's LastIndexOf : http://msdn.microsoft.com/en-us/library/bc3z4t9d So it's not a bug, just an 'interesting' design choice. I do think I prefer what you'd like to happen, but I wont be changing it in the near future. | 
| 
 | ||
| Ah, I see. Fair enough... |