next up previous contents
Next: About this Documentation Up: Str Class Documentation Previous: Contents   Contents

Motivation: What Is Str And Why Should I Use It?

Str is a string class for C/C++ programmers that is designed to meet the following goals:

Efficient

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.

Easy to use

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.

Minimal dependencies

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.

Support for your coding style and coding 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.

Simple, compact data structure

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*.

Tight integration with 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.

Avoid major performance problems of char*

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. $O(n)$ time). Str adds a length field that accelerates these operations.

Automated memory management in the default case

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.

Flexible allocation options (i.e. Str memory can initially reside on the stack)

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!

Ability to optimize for time or space

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.



Subsections
next up previous contents
Next: About this Documentation Up: Str Class Documentation Previous: Contents   Contents
2007-05-05