Str
is a string class for C/C++ programmers that is designed to
meet the following goals:
The included benchmarks in chapter 2 show that the Str
class is as fast as raw char*
strings in many cases (faster in
some situations, slower in others). When compared to the
standard C++ string
class, Str
performs significantly
better in nearly all cases.
The Str
class attempts to make strings almost as easy to work with as they are in
languages like TCL or Python. The "convention over configuration"
design philosophy is used. This means that functions are designed to be
intuitive and have default arguments that fit common use cases.
The Str
class requires only a few bits of the standard C library to function. The
library itself is a single .cpp
file and a single header file designed to
be compiled directly into a project. The .cpp
and header files are
designed to be tailorable for specific project needs.
The Str
class supports using both C and C++-style stream I/O. This means that you
can code using stdio
or iostream
without extra effort due to your
choice. Str
also supports optional bounds checking on string indexing
operations. You can choose speed or safety depending on the
requirements of your project.
Many string classes provide a nice API but do little to explain how
things actually work. In contrast, Str
is designed to be simple enough
so that its users can understand exactly how it works without a lot of
learning effort. Chapter 3 of this document explains the data
structure so that you can precisely understand the trade-offs between
The Str
class and char*
.
The Str
class supports on-the-fly translation to and from
char*
. You can use Str
anywhere a char*
is used. You can also wrap a Str
around any existing
char*
. The conversion to char*
only involves dereferencing a
pointer and is therefore very fast.
A null-terminated char*
is a simple and memory-efficient way to
represent a string. Unfortunately, many common operations, such as
determining string length and appending to a char*
string, require the
entire string to be walked (i.e. time). Str
adds a length field
that accelerates these operations.
By default, Str
automatically manages buffer sizes and automatically
prevents string operations from overflowing allocated buffers. As
described below, this process can be tuned and tailored by the programmer.
For many, the reason for choosing C/C++ over other languages is performance.
Unfortunately, over-abundant allocation/deallocation of heap memory can be a
major performance hit. On the other hand, using fast stack or persistent
memory is subject to buffer overruns. Buffer overruns are the cause of
crashes, unexpected behavior and security problems. The Str
class gives
a programmer the option to initially create string memory on the stack, and
automatically transist to the heap in the event that the stack buffer would be
overrun. Furthermore, Str
makes this task simple and intuitive;
requiring only a single extra line of code!
The Str
class provides the ability to optimize a given string for time or space. A
string that is optimized for space will not over allocate its buffer
beyond the string length. A string that is optimized for time can
efficiently handle a series of operations that grow the string. The Str
class
also gives the programmer the option to control buffer allocation sizes exactly.
This can be useful when performance tuning and in special cases.