Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/antonc9018/strlib
A C-style string library for C++
https://github.com/antonc9018/strlib
Last synced: about 2 months ago
JSON representation
A C-style string library for C++
- Host: GitHub
- URL: https://github.com/antonc9018/strlib
- Owner: AntonC9018
- License: mit
- Created: 2021-02-06T22:12:51.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-07T12:22:24.000Z (almost 4 years ago)
- Last Synced: 2024-10-19T07:52:56.818Z (3 months ago)
- Language: C++
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# strlib - A C-style string library for C++
`strlib` is a really simple library for C-style string manipulation in C++. Includes types for a string with a malloc-ed buffer `str_t`, a string view `str_view_t` (either over literal or over a str_t) and a string builder `str_builder_t` and some useful functions over that.
Every string is both null terminated and has a length field, which makes it very comfortable to use with C's string.h functions.
None of the functions expect the string to be null terminated, which means that you can safely construct a string view from C++'s std::string and still be able to leverage all of the libraries functionality:
```cpp
std::string something = "Get this string from e.g. an API";
str_view_t str = { something.data(), something.length() };// Using a function from the library is safe, even though the data is not null-terminated
str_find_char_index(str);
```## Demo
```c++
#include "strlib.h"
#include "strlib.cpp"
#includeint main()
{
{
str_t s = str_copy_lit("Hello");
puts(s.chars); // Hellostr_t s2 = str_copy(s);
str_free(s);
puts(s2.chars); // Hellostr_view_t sv = str_view(s2);
puts(sv.chars); // Hello
str_free(s2);str_view_t sv2 = str_lit("Hello2");
printf("%s | %zu\n", sv2.chars, sv2.length); // Hello2 | 6
}
{
str_builder_t sb = strb_create(5);
strb_cat(sb, "Hello"); // the size if set to 5, but the buffer
// is rescaled when that limit is reached.
strb_chr(sb, ',');
strb_chr(sb, ' ');
strb_cat(sb, "World!");str_t str = strb_build(sb);
puts(str.chars); // Hello, World!
}
return 0;
}
```