cstring is a C++ immutable C-string (aka const char array) with reference counting.
It is a pointer to chars where the pointed memory is prefixed by the reference counter (4 bytes) and the string length (4 bytes).
Features:
- It is a
const char *havingsizeof(cstring) = 8. - Behaves like a
std::string. - Integrated reference counting reduces the number of indirections.
- Includes string length, harnessing the full power of
string_view. - Easy debug (object points to string content).
- Additional utility functions (
contains(),trim(), etc). - A comparator (
cstring_compare) is provided for the most common types.
Main drawbacks:
- Immutable content (the string content cannot be changed).
- 8 additional bytes are allocated (reference counter + length + eventual padding).
- No Small String Optimization (SSO).
- String length and reference counter are limited to
4'294'967'295. - Stateful allocators (
sizeof(allocator) > 0) are not supported.
Drop cstring.hpp into your project and start using cstring.
Read the cstring-example.cpp file to see how to use it.
#include "cstring.hpp"
...
// just use it like a const std::string
gto::cstring str = "hello world!"
std::cout << "Length of '" << str << "' is " << str.length() << std::endl;# example
make example
# unit tests
make tests
# code coverage
make coverage
firefox coverage/index.html &| Name | Contribution |
|---|---|
| Gerard Torrent | Initial work Code maintainer |
| Matthieu M. | Code review |
This project is licensed under the GNU LGPL v3 License - see the LICENSE file for details.