This is a Light-weight string wrapper around the C string interface.
This class is heavily influenced by Tom Forsyth's article "A sprintf
that isn't as ugly" https://home.comcast.net/~tom_forsyth/blog.wiki.html#[[A%20sprintf%20that%20isn%27t%20as%20ugly]]
The goals are: No dynamic allocation. Easier to read and control than sprintf() Type-safe (in as much as C can ever be type-safe - bloody auto-converts). Overflow-safe (i.e. it will refuse to scribble, and will assert in debug mode).
LwString needs to be fed a pointer and a maximum length size. The pointer's preexisting contents will be preserved. For example: char buf[1024]; buf[0] = '\0'; LwString myStr( buf, 1024 );
{ char *buf = new char[1024]; LwString myStr( buf, 1024 ); delete buf; //This class will never deallocate the pointer. }
char buf[512]; buf[0] = 'H'; buf[1] = 'i'; buf[2] = '!'; buf[3] = '\0'; LwString myStr( buf, 512 ); printf( myStr.c_str() ) //Hi!
In order to concatenate strings, you need to use the a() function. Concatenations are appended to the current string. You can concatenate strings, integers, raw char strings, and floats. Examples: Example 1: myStr.a( "Hello", " ", "World!" ); //Hello World!
Example 2: myStr.a( "Hello", " " ); myStr.a( "World!" ); //Hello World!
Example 3: char myZero = 0; myStr.a( "One", " + ", 1, " equals ", myZero ); //One + 1 equals 0
Example 4: myStr.a( 1.5f, " + ", 1.5f, " = ", 1.5f + 1.5f ); //1.5 + 1.5 = 3.0
The += operator only works with strings.
Additionally, there are a few STL-like interfaces: begin (iterator), end (iterator), find, find_first_of The input arguments, behavior and return values are the same as std::string.
Users can specify the precision of floats using LwString::Float myStr.a( LwString::Float( 3.5f, precision, minWidth ) );