Chapter 4. AFC String

Table of Contents
Introduction
Methods
 

Last night I stayed up late playing poker with Tarot cards. I got a full house and four people died.

 
--Stephen Wright 

Introduction

This is a suite of functions to create, manipulate and dispose strings.

As you should already know, strings are a bad animal for C programmers, sice they are just a bunch of bytes in memory where no bound control is made during manipulation and that can lead to the worse (and most hidden) segmentation fault problems.

If you use inside your programs these functions and what we have called AFC Strings, you will not suffer for these problems. Every function for manipulating strings does bound checking and integrity checking, so if you copy "supercalifragilisticespiralidous" into a string 10 bytes long, you'll not damage external memory, but just have back the first 10 chars of the word.

But there's more. AFC Strings offer a bunch of optimized calls, like afc_string_len() that is light year faster than the standard C strlen function, and afc_string_max() that let's you know how many chars can be handled by the provided string.

The most important thing is that AFC Strings are backward compatible with standard C strins (array of chars), so you can pass a standard AFC String to functions like printf, sprintf and so on without problems.

C strings and AFC Strings functions: Please, note that where an AFC String is needed, you must provide an AFC String and not just a C string, or you'll get into troubles. As we mentioned before, AFC Strings can (and should) be used as standard C strings in all function calls, like printf or sprintf, but the opposite is never true.

So keep in mind: where you need an AFC String, you need an AFC String. Where you need a C string, you can provide both a C string or an AFC String.

AFC Strings also offer advanced manipulation functions, like afc_string_radix() used to convert a number to a given base (up to base64 is supported); afc_string_hash() that is able to generate an hash value for the provided string or afc_string_pattern_match() that is able to match the given string against a standard pattern match string to see if the pattern is ok.

To create a new AFC String you use afc_string_new() then you can manipulate it with standard AFC functions like afc_string_copy() afc_string_left() afc_string_right() or afc_string_make() When you have finished with a string, remember to call afc_string_delete() to free the memory associated with it.