Category: PureBasic

Marching Box Algorithm Implementation

Posted by on April 13, 2015

This implementation of the Marching Box Algorithm makes use of some clever programming for code readability, however it may not be inherently easy to read for beginners, but it is a good example of how to implement an algorithm such as this one, in a clean way.

It may not be the fastest implementation but this code has served me well in the past and it turns out to be quite easy to maintain as well.

The primary application is to traverse for example bitmaps, you could implement a bucket fill or follow a contour easily, however more patterns may be required depending on your requirements.

I’ve used this, among with a peucker implementation, to create rough vector representations of bitmaps in the past. You may have to add exclusions or rules if you don’t want to check the extra 4 directions in diagonal, this depends on the type of images or data you’ll be working with.


Probable optimizations include denesting and less error checking on the match procedure for the release version, however that’s also implementation dependent.

This original implementation was aimed toward image manipulation, hence you’ll notice some variable names aren’t generic, but relevant to the subject. You may change this as you please.

At any rate, enjoy!


(PB) FileToStringEx – FileToString – Helper functions

Posted by on December 22, 2014


Here’s a small but useful io function, The basic routine is also included for less demanding uses.

This function will read a text file into a string, the Ex (extended) functionality allows for defining size constraints to the return stringĀ and positioning of the file pointer for location/seeking purposes.

On big text files and in cases where you want to limit memory usage, this is a very useful routine. Otherwise the basic function works just fine.

The extended routine can also be used with a callback to set a progress bar, based on data size you can also calculate the remaining time, however that’s all beyond the scope of this post.


Use example:

Keep in mind that with the basic routine the whole file will be read into memory, so you have to beware of this detail.

If you never expect file sizes to exceed a certain range, this won’t be an issue. Otherwise use the Ex function to prevent memory and other performance related issues.

If the file is not found the return string is empty, this is the expected behavior, no error codes are used but you can easily implement this.

Have fun!

PureBasic – strip_tags() just like in PHP!

Posted by on February 11, 2014

Let the strippin’ begin!

This is a basic implementation of PHP’s strip_tags() for PureBasic, it’s not fully optimized but it’s fast enough for most applications. It doesn’t use native string manipulation functions, instead it parses through each character of the string.

Naturally it’s going to be faster than using REGEXP, but not as flexible. Lots of room for improvement, but for now it is what it is!

Simple use example:

Got any ideas on how to improve this?, let me know!


(PB) Capitalizing strings using native code

Posted by on November 10, 2013

Sometimes plain text needs to be formatted in non common ways. In this case we’ve got filenames where spaces have been converted to either “-” or “_” and each word may or may not be capitalized. The idea is to have a certain degree of uniformity on the user interface so the labels are formatted to meet these requirements.

One of the steps involved is to simply capitalize every word in the string. In this case we are also recovering the space characters prior to formatting.

We don’t know if the first character is part of a word or if it’s just a space, due to the actual requirements of this application multiple spaces are ignored and trimmed to a single instance.

To avoid using multiple string manipulation function calls, we handle all of the manipulation through low level code (without going into ASM). This way we avoid the use of Trim(), LCase(), Mid(), Left(), Right(), etc. To speed things up.

The routine is quite simple and allows for all kinds of modifications, since we are performing all of the string manipulation ourselves. We assume ASCII to be the input and output.

That’s all for now, just had a need for this and couldn’t find a proper function out there (only a “let’s see who can make it faster and forget about readability” war at the official forums, not very handy for real world projects).

Have fun,

(PB) Loading CSV directly to a Hash Map

Posted by on February 21, 2013

When dealing with string maps, being able to easily load them from a file is a must. For example take language files for different local translations. The simplest, cleanest format would be CSV. The ideal data structure? — A hash map.

While trivial, this code will come in handy for many other things!

Example of data:

Example of use:

Couldn’t be easier!