Tag: code

(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!

URL Encoding in PureBasic, the proper way.

Posted by on March 30, 2008

The other day I found myself in the need of a compliant url-encoding routine for the PB language, not surprisingly theres none available by default (there are actually no specialized libraries whatsoever, and the little you see is implemented from third-party code…).

So I had to code my own, as always. When no proper framework is given, you have to either depend on others to implement it or just do it yourself, since the former requires time and patience… I always choose the latter 🙂

Heres a standards compliant routine to URL-Encode URIs in PureBasic:

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

More…