get element from String$

Blitz3D Forums/Blitz3D Programming/get element from String$

Bobysait(Posted 2006) [#1]
This function allow to get element of a string "chaine$", Just specify the Id of the element "Offset%", and the operator "Sep$"
Graphics 800,600,0,2

Text 10,10,SSP_GET_Line$("a,b,c,d",1)
Text 10,30,SSP_GET_Line$("a,b,c,d",2)
Text 10,50,SSP_GET_Line$("a,b,c,d",3)
Text 10,70,SSP_GET_Line$("a/b/c/d",4,"/")
Text 10,90,SSP_GET_Line$("a,b,c,d",5)
Text 10,110,SSP_GET_Line$("a,b,c,d,.25",5)
Text 10,130,SSP_GET_Line$("a,b,.25,d",5)
Text 10,150,SSP_GET_Line$("a,b,.25,d",3)
Text 10,170,SSP_GET_Line$("0.02|b|c|d",2,"|")
Text 10,190,SSP_GET_Line$("0.02,b,c,d",1)
Flip
WaitKey
End


Function SSP_GET_Line$(chaine$,Offset%=1,Sep$=",")
	If Offset<1 DBG_Nfo "/X\ GetLine Erreur => Offset nul " Return False
	ligne$=chaine
	LnSize%=Len (ligne)
	Local l_var$
	Local loc1%
	If offset=1
		If LnSize>0
			loc1=Instr(ligne,Sep)
			If loc1=0
				l_var$=ligne
			Else
				l_var$=Left(ligne,loc1-1)
			EndIf
			DBG_Nfo "/!\ GetLine :"+chaine+"=> "+l_var+" Offset="+offset
			Return l_var$
		Else
			DBG_Nfo "/X\ GetLine Erreur => Chaine vierge "
		EndIf
	Else
		If LnSize%>2
			; supprime le premier argument
			Local loc2%		=	1
			loc2%=Instr(ligne$,Sep$)
			If loc2%=0
				DBG_Nfo "/X\ GetLine Erreur => la chaine '"+chaine$+"' ne contien pas assez '"+offset%+"' arguments!"
				Return False
			EndIf
			Local	l_part$	=	Right(ligne$,LnSize%-loc2%)
			Local	lnPart%	=	LnSize%
			; cherche l'offset et retourne la chaine entre 'Sep'
			For i = 2 To Offset%
				Loc2%	=	Instr (l_part$,Sep$)
				If i=offset%
					If loc2% = 0
						l_var$=l_Part$
					Else
						l_var$=Left(l_Part$,loc2%-1)
					EndIf
					If Len(l_var$)=0
						DBG_Nfo "/X\ GetLine Erreur => Retourne une chaine vide"
						Return False
					Else
						DBG_Nfo "/!\ GetLine :"+chaine$+"=> "+l_var$+" Offset="+offset%
						Return l_var$
					EndIf
				EndIf
				If loc2%=0	DBG_Nfo "/X\ GetLine Erreur => Offset trop grand " Return False
				lnPart%	=	Len(l_Part$)
				l_part$	=	Right (l_part$,lnPart%-loc2%)
				lnPart%	=	Len(l_Part$)
			Next
		Else
			DBG_Nfo "/X\ GetLine erreur => nombre de caractere insuffisant ( 3 mini ! ) !"
			Return False
		EndIf
	EndIf
End Function


Function DBG_Nfo(texte$)
	DebugLog texte
End Function




Rroff(Posted 2006) [#2]
similiar function I created, but slightly different useage - mine doesn't catch errors like a null index or null token, infact looking at it again now I'm not sure what would happen if the user put in a huge offset/index value that didn't exist in the input string.




Bobysait(Posted 2006) [#3]
I tryed your function, and it seems to be faster.
Maybe anyone should use yours instead.

I use mine in order to be sure program won't MAV, cause i'm working on a "never crash" lib of single surface particule system, and i have to be sure nothing could bug it.
But we do not all need this type of function.

Thanks Rroff !


Rroff(Posted 2006) [#4]
I did some testing and mines on average 7% faster, not a huge amount but if your trying to squeeze the last drops of performance out it could make a difference... however I can't guarantee mine won't MAV if used incorrectly.