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 ref-counter (4-bytes) and the string length (4-bytes).
Features: :
- It is a
const char *
havingsizeof(cstring) = 8
- Behaves like a
std::string
- Reference counting integrated reduce 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)
Main drawbacks:
- Immutable content (we cannot change the string content)
- 8 additional bytes are allocated (ref-counter + length + eventual padding)
- No Small String Optimization (SSO)
- String length and ref-count limited to
4'294'967'295
- Stateful allocators (
sizeof(allocator) > 0
) not supported
Drop off cstring.hpp
in your project and start using the cstring.
Read cstring-example.cpp
file to see how to use it.
#include "cstring.hpp"
...
// just use it like a const std::string
gto::cstring str1 = "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.