Local-String Library Sources

Download Version 2.0.0

This code comes from a collection of external and internal sources. They are a bundle of very useful string routines that I use quite often. Functions such as snprintf and strsep are very important routines to have in one's back pocket. Others are just useful.

Snprintf

This code is derived from software contributed to University of California, Berkeley by Chris Torek. It is an implementation of the snprintf and vsnprintf routines which provide sprintf functionality while providing bounds protection by allowing the programmer to specify the size of the destination buffer.

It was then hacked up a bit by Gray Watson to bundle the function into one file and have them return the number of characters they added to the buffer.

There is no reason why anyone would ever use sprintf. When I am debugging memory overruns in other folk's software, I always go looking for sprintf calls. With this said, modern operating systems such as Digits Unix/OSF, Solaris, etc. do not provide this function in their libc repertoire. Shame.

Strsep

The initial code for strtok was taken off the Net a ways back. It was then morphed into strsep and strsep_len. Strsep is a much better function than strtok in that it is thread safe as does not depend on previous calls to the function to get future tokens.

Strsep_len is even better than strsep (IMO) because it is non-destructive and allows \0 to be a separating character.

There is no reason why anyone would ever use strtok. These days, programmers should consider every function to have the requirement of being thread-safe. The leasons learned in thread programming are good module level consistancy, resource "frugal-ness", and low function co-dependance.

Since most of the code that we write is copied from place to place and then morphed, the implications of writing poor functions, even if it solves an immediate problem, are larger than they seem.

Addbuf

I hate to see in any mature C routine a number of strcat calls together. For each one, the string pointer has to start at the beginning of the buffer and walk across all of the characters. Bleah.

The addbuf routines are designed to allow the programmer to efficiently build a large string buffer on the fly. I use this quite often when creating dynamic HTML pages for instance.

I've also included some interesting 7-bit unsigned decimal number routines. I use these from time to time in binary data streams. They can be used to compression unsigned integers and long-integer values where space is a concern.

Repository

The newest versions of the library are available via the web:

http://256stuff.com/sources/locstring/

More 256 Sources
Gray Watson Land