A modernized C++ string library extracted from baseclasses (Vestris Inc., 1994–1999).
src/string.hpp—CStringTemplate<C, StackSize>class template +CStringA,CStringW,CStringtypedefssrc/string_impl.hpp— all template implementations (include to instantiate custom StackSizes)src/string.cpp— explicit instantiations forcharandwchar_tat default StackSizeplatform/— lightweight platform headerstests/— CppUnit test suite (120 tests)
Removed from the original: Object, Vector, Internet (URL/HTML), EStrings, GStrings, StringTable, tracing, and property macros.
API uses modern camelCase conventions (toUpper, trim, indexOf, etc.).
| Type | Character | Default stack buffer |
|---|---|---|
CString / CStringA |
char |
18 chars |
CStringW |
wchar_t |
18 wide chars |
CStringTemplate<C, StackSize> |
any | custom |
Strings up to StackSize - 1 characters are stored on the stack with no heap allocation. Longer strings spill to the heap transparently.
- C++ compiler:
g++orclang++(C++11 or later) - autoconf 2.69+: to regenerate
configurefromconfigure.ac - GNU make
- CppUnit 1.15+: for running tests
Install on macOS with Homebrew:
brew install autoconf cppunitautoconf
./configure
makeThis produces lib/libbasecl.a.
make checkOr directly:
g++ -std=c++11 -I. -Isrc $(pkg-config --cflags --libs cppunit) \
-o tests/run_tests \
tests/main.cpp tests/string_test.cpp tests/wstring_test.cpp \
tests/stack_size_test.cpp tests/stack_size_inst.cpp \
src/string.cpp
./tests/run_testsg++ -std=c++11 -I. -Isrc -o myapp myapp.cpp src/string.cpp#include <string.hpp>
// char strings
CString a("Hello, World!");
a.toUpper(); // "HELLO, WORLD!"
a.trim(); // strip whitespace
bool b = a.equalsIgnoreCase("hello"); // case-insensitive compare
int i = a.indexOf('o'); // find character
CString n = CString::fromInt(42); // int → string
int v = CString::toInt(n); // string → int
// wide strings
CStringW w(L"héllo");
w.toLower();
// custom stack size (no heap allocation for strings ≤ 63 chars)
CStringTemplate<char, 64> s("short");Strings with different StackSize values freely interoperate:
CStringTemplate<char, 4> small("hi");
CStringTemplate<char, 32> large("hi");
large = small; // assign across sizes
bool eq = (small == large); // compare across sizes
large.append(small); // concatenate across sizes
CStringTemplate<char, 4> copy(large); // construct across sizesTo use a non-default StackSize in your own code, include the implementation
header and add an explicit instantiation in one .cpp file:
// mystring_inst.cpp
#include <string_impl.hpp>
template class CStringTemplate<char, 64>;| Expression | Description |
|---|---|
CString s |
empty string |
CString s("hello") |
from C string |
CString s('x') |
from character |
CString s(other) |
copy (same or different StackSize) |
s = other |
assign (same or different StackSize) |
| Method | Description |
|---|---|
toUpper() |
convert to uppercase in place |
toLower() |
convert to lowercase in place |
hasUpper() |
true if any uppercase character |
hasLower() |
true if any lowercase character |
toUpperChar(c) |
uppercase a single char (static) |
toLowerChar(c) |
lowercase a single char (static) |
| Method | Description |
|---|---|
indexOf(ch) |
first index of character |
indexOf(str) |
first index of substring |
indexOfIgnoreCase(str) |
case-insensitive substring search |
lastIndexOf(ch) |
last index of character |
| Method | Description |
|---|---|
equals(s) |
case-sensitive equality |
equalsIgnoreCase(s) |
case-insensitive equality |
notEquals(s) |
inequality |
startsWith(s) |
prefix check |
startsWithIgnoreCase(s) |
case-insensitive prefix |
endsWith(s) |
suffix check |
endsWithIgnoreCase(s) |
case-insensitive suffix |
compare(s) |
lexicographic compare |
compareIgnoreCase(s) |
case-insensitive compare |
Operators ==, !=, <, >, <=, >= |
standard comparisons (works across StackSizes) |
| Method | Description |
|---|---|
append(s) / += |
concatenate (works across StackSizes) |
insert(pos, s) |
insert at position |
erase(start, count) |
remove characters |
replace(src, tgt) |
replace substring |
trim(ch) |
strip character from both ends |
trimLeft() / trimRight() |
strip from one end |
trimSpace() |
strip all whitespace (0–space range) |
reverse() |
reverse in place |
clear() |
empty the string |
| Method | Description |
|---|---|
mid(first, count) |
extract substring |
left(count) |
left N characters |
right(count) |
right N characters |
getLine(pos) |
extract next line (advances pos) |
extractLine() |
remove and return first line |
| Method | Description |
|---|---|
CString::fromInt(n) |
int → CString |
CString::fromLong(n) |
long → CString |
CString::fromFloat(f) |
float → CString |
CString::fromDouble(d) |
double → CString |
CString::toInt(s) |
CString → int |
CString::toLong(s) |
CString → long |
CString::toFloat(s) |
CString → float |
CString::toDouble(s) |
CString → double |
CString::toHex(s) |
hex CString → int |
isInt() / isFloat() / isLong() / isHex() |
type checks |