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 <
It complies with the RFC 3986 (the current standard) and it’s the fastest method available for the language. In my last benchmarks it proved to be 0.20 times faster than the fastest known/shared url-encode routine for this language (however, that one wasn’t compliant at all!). It’s also 2.50 times faster than the average suggested “solutions” for URL-Encoding in this language.
Notice the character ‘%’ being checked in one of the case statements, this is simply to prevent duplicate percent-encodings on already encoded URIs. Speed wise, you could still further optimize this routine here and there, but I personally think it’s not worth the hassle (readability is still important).
The reason I always optimize my routines once I’m done developing them is simply due to the fact that every little uop you save (or should I say ‘shave’?) counts, whether it’s a trivial operation or not, it doesn’t matter! – that’s simply a clue: It’ll give you a reference on how many time you should spend optimizing the routine. Yet in the case of a trivial operation, it doesn’t mean you should leave it “as is”. Again, just because you’ll call the routine once or twice during runtime doesn’t mean you can’t improve it!.
Thus if you can up-scale your code, then do it. Not only you’ll gain execution speed, but also knowledge; that’s a precious thing to earn.
i knew you where alive 😉