Quick challenge (who's is the fastest)

Miscellaneous Forums/General Discussion/Quick challenge (who's is the fastest)

Need a function to seperate text with a given seperator.. Thought of a few ways to do it, and had a way I did it in the past (sloppy), but I want the fastest possible.. Figured I would head over here for some input.

Given a string of text like "Hello jim, How, have you , been?"

Devise a function to take the string of text, a seperator character, and an index, and return the text between the seperator.

Any thoughts (B3D or B+ compadible)?

I hate sifting through the archives for something... We need a search feature :-)

Look in the code archives for Tokenizers.

They work in a similar way to the C++ command "strtok"

Here is one : http://www.blitzbasic.com/codearcs/codearcs.php?code=290

smaller :P
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=1365

oooo, that second one is just a bit faster than mine :-)

Thanks for the replies guys!

Off the top of my head:

[edit]
Heres my function after converting frmo Visual Basic, without BB.

Function Gettextsection(inp$,sep$,index)
local last = 1,cnt,ins,out$,olast
repeat
	ins = InStr(inp$, sep$,last)
	If ins = 0 Then ins = Len(inp$)
	If ins > 0 Then olast=last: Cnt = Cnt + 1: last = ins + 1
Until ins = 0 Or Cnt = index or index=0
If olast>0 then out$ = Mid$(inp$, olast, ins - olast)
Return Out$
End Function




Heres the function as I originally wrote it, Still untested
Function index(inp$,tok$,getindex)
local Lastpos=1
local curcount=0
local ins
local out$

repeat
 ins=instr(inp$,tok$,lastpos)
 if ins>0 then out$=mid$(inp$,lastpos,ins):lastpos=ins+1:curcount=curcount+1 ;Thanks Grey Alien!!!! :P :P :P
until curcount=index or ins=0

if curcount=getindex then return Out$
end function



Not sure if that will do what you need, but I knocked it up without documents or anything, so it will likely have a couple of bugs.

Type textlist

Field value$
Field index
End Type 

Function ParseTextIncludingSeparator(stringvalue$,separator$)
startchar=1
For i=1 To Len(stringvalue$)
If Mid(stringvalue,i,1)=separator Or i=Len(stringvalue) Then 
bitoftext.textlist=New textlist
bitoftext\index=startchar
bitoftext\value=Mid(stringvalue,startchar,1+i-startchar)
startchar=i+1
EndIf 

Next
End Function


Function ParseTextNotIncludingSeparator(stringvalue$,separator$)
startchar=1
For i=1 To Len(stringvalue$)
If Mid(stringvalue,i,1)=separator Or i=Len(stringvalue) Then 
bitoftext.textlist=New textlist
bitoftext\index=startchar
If Mid(stringvalue,i,1)=separator Then bitoftext\value=Mid(stringvalue,startchar,i-startchar) Else bitoftext\value=Mid(Stringvalue,startchar,1+i-startchar)
startchar=i+1
EndIf 

Next
End Function

Graphics 800,600,32,2
;example
ParseTextIncludingSeparator("Hello jim, How, have you , been?",",")
i=0
For bitoftext.textlist=Each textlist
Text 0,i,"Index:"+bitoftext\index+", Text:"+bitoftext\value
i=i+15
Next
Delete Each textlist
ParseTextNotIncludingSeparator("Hello jim, How, have you , been?",",")
For bitoftext.textlist=Each textlist
Text 0,i,"Index:"+bitoftext\index+", Text:"+bitoftext\value
i=i+15
Next

Flip
WaitKey
End


EDIT - (5 minutes later) - I think I misunderstood what you were after...

Mine will do what he wants, with some minor tweaking.

I dont have the docs and can't be arsed to check, but the code is well on its way to being the simplest. I use it when sending delimited network packets.

cyg: curcount doesn't do anything in yours!

Dugh.

I did that on purpose, I promise ;)