A string library in C with C++ standard features for string manipulation, designed to mimic the behavior of C++'s std::string (specifically std::basic_string<char>).
I don't have a documentation for now but for reference check: cppreference
#include "cstring.h"
int main(void)
{
// Initialization
cstring s = cstr_init("Hello");
// Modifiers
cstr_append(&s, " World");
cstr_insert(&s, 5, " amazing");
// Access & Info
printf("String: %s\n", s);
printf("Length: %zu\n", cstr_length(s));
// Operations
if (cstr_contains(s, "amazing")) {
printf("It's amazing!\n");
}
// Cleanup
cstr_free(&s);
return 0;
}cstr_init(): Default constructor.cstr_init("literal"): Copy constructor.cstr_init(count, 'c'): Create a string withcountcharacters of 'c'.cstr_literal(name, "content"): Create a stack-allocated, literal asconst_cstring.
cstr_at(s, index): Bounds-checked access(supports[]but not recommended).cstr_front(s),cstr_back(s): First and last characters.
cstr_append(&s, ...): Append strings, chars, or substrings.cstr_insert(&s, pos, ...): Insert at a specific position.cstr_erase(&s, pos, count): Remove a range of characters.cstr_replace(&s, pos, count, ...): Replace a range with new content.cstr_push_back(&s, 'c'),cstr_pop_back(&s): Stack-like operations.
cstr_find(s, ...): Find first occurrence of a char or string.cstr_rfind(s, ...): Find last occurrence of a char or string.cstr_find_first_of(s, ...): Find first occurrence of any char in a set.cstr_find_last_of(s, ...): Find last occurrence of any char in a set.cstr_find_first_not_of(s, ...): Find first char not in a set.cstr_find_last_not_of(s, ...): Find last char not in a set.cstr_contains(s, ...): Check for existence.cstr_starts_with(s, ...)/cstr_ends_with(s, ...): Prefix/Suffix checks.cstr_compare(s, other): Lexicographical comparison.
- CMake 3.16.0+
- A C compiler (GCC, Clang, or MSVC)
- Git
git clone --recursive https://github.com/lucky017/cstring.git
cd cstringLinux / macOS
cmake -B build
cmake --build buildWindows (MSVC)
cmake -B build
cmake --build build --config Release| Option | Default | Description |
|---|---|---|
-DBUILD_SHARED=ON |
OFF | Build as a shared library instead of static |
-DBUILD_TESTS=ON |
OFF | Build and run the test suite |
Add to your CMakeLists.txt:
add_subdirectory(cstring)
target_link_libraries(your_target PRIVATE cstring)- Input & Output
- Numeric conversions
- Hash support for cstring
- String View
- UTF-8 support
- Support for wchar_t, char16_t and char32_t
- Issues of performance and designs
Note: This library is byte-based and does not natively support multi-byte encodings like UTF-8.