FindOneOf() - String utility

Share your advanced PureBasic knowledge/code with the community.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

FindOneOf() - String utility

Post by superadnim »

Code: Select all

Procedure.l FindOneOf( *set.character, *string.character, start=0)
	Define.Character *cTemp
	#INDXBASE = 1
	If *set And *string
		
		If start 
			*string + (SizeOf(Character) * start)
		EndIf
		
		Repeat
			
			*cTemp = *set
			Repeat
				If *string\c = *cTemp\c
					ProcedureReturn (*cTemp - *set)+#INDXBASE
				EndIf
				*cTemp + SizeOf(Character)
			Until *cTemp\c 	= 0
			
			*string + SizeOf(Character)
		Until *string\c 	= 0
		
	EndIf
	
EndProcedure

;-

set.s 		= "abcdef"
string.s 	= "Hello"
Debug FindOneOf(@set, @string) ; should be 5
Debug FindOneOf(@set, @string, 3) ; should be 0

stringToSearch.s = "The quick brown fox jumped over the lazy dog."
Debug FindOneOf(@"aeiou", @stringToSearch) ; gives 2, which is the index at which the "e" is found at.
Debug FindOneOf(@"aeiou", @stringToSearch, 4) ; gives 5 (position of 'u' in set)
MFC, ColdFusion, etc. All have a lot of string manipulation routines in common, this one is an example.

I couldn't find a fast way of doing this without having to do at least 4 PB calls and a few more inside the loops (mid, etc).

If it's not behaving properly, please let me know and I'll see what I can do.

In case you wonder, in simple words: It's a FindString() but, with a list of characters, the search is done on a per character basis and not on a per group basis.

To my understanding, this routine originally returned the index from the SET and not the STRING. It also began to count from 0 instead of 1, if you want 1-based results, do set #INDXBASE to 1 instead of 0. I hope PB's compiler is smart enough not to do a +0 at the end, let's hope so.
- In my posted code I use 1 though, because it's similar to what PB does.


If it was useful or you know how to optimize it, let me know
:)

Part of my project requires mid-advanced routines such as this one, that's the reason I seem to be reinventing the wheel, as some people say.

Feedback appreciated.