Tag: parser

(PB) String Between Characters

Posted by on April 26, 2011

This is a small purebasic function used to retrieve a string between two known characters, it also allows you to provide a starting position.

Procedure.s str_between_char( *Source.CHARACTER, First.C, Last.C, StartAt.i = 0 )
	Define.i dwLength
	
	*Source + (StartAt * SizeOf(CHARACTER))
	Repeat
		If *Source\c = First ; If this character matches the First character
			*Source + SizeOf(CHARACTER)
			dwLength = *Source
			Repeat ; Repeat until we find an occurrance with the Last character
				*Source + SizeOf(CHARACTER)
			Until *Source\c = Last
			dwLength = (*Source - dwLength)
			ProcedureReturn PeekS( *Source - dwLength, dwLength ) ; Peek the output string
			Break
		EndIf
		*Source + SizeOf(CHARACTER)
	Until *Source\c = #Null
	
EndProcedure

Example use:

Define.s str 		= "dd(hallo)xyz(a)"
Define.s result 	= str_between_char(@str, '(', ')', 2)
Debug result

If you’re working on a parser or similar project this will sure come in handy.
Enjoy!

Faster ways of finding a character inside a string, in PureBasic.

Posted by on March 3, 2008

For those of you who either prototype or work with PureBasic on a daily basis, if you ever found yourself looking for faster ways of performing string manipulation, while still using the string system this language provides, be glad for I’ll be posting a few of my solutions to speed up the process.

My first entry is the FindChar() routine. Unlike the official FindString(), this one only searches for a single character. In situations where you’ll be dealing with single character string searches rather than multi-character, this routine will give you up to 2x speed increase in both Ascii and Unicode modes. Worst case scenario, you’ll get equal results speed-wise.

The FindChar() code can be found at > here <

More…