https://github.com/sagiegurari/c_string_buffer
A simple string buffer for C
https://github.com/sagiegurari/c_string_buffer
c c-lib c-library string string-buffer
Last synced: 9 months ago
JSON representation
A simple string buffer for C
- Host: GitHub
- URL: https://github.com/sagiegurari/c_string_buffer
- Owner: sagiegurari
- License: apache-2.0
- Created: 2020-04-17T13:07:10.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-27T07:12:16.000Z (over 2 years ago)
- Last Synced: 2025-03-26T01:41:30.913Z (9 months ago)
- Topics: c, c-lib, c-library, string, string-buffer
- Language: C
- Size: 80.1 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# stringbuffer
[](https://github.com/sagiegurari/c_string_buffer/actions)
[](https://github.com/sagiegurari/c_string_buffer/releases)
[](https://github.com/sagiegurari/c_string_buffer/blob/master/LICENSE)
> A simple string buffer for C.
* [Overview](#overview)
* [Usage](#usage)
* [Contributing](.github/CONTRIBUTING.md)
* [Release History](CHANGELOG.md)
* [License](#license)
## Overview
This library provides a simple string buffer which allows adding different basic data types.
It can be used to generate large strings without the need to worry about memory allocation or type conversions.
```c
#include "stringbuffer.h"
#include
int main()
{
// init the buffer with predefined size or with a provided size
// use stringbuffer_new for predefined size
struct StringBuffer *buffer = stringbuffer_new_with_options(
1024, // initial size
true // allow resizing
);
// different append functions
stringbuffer_append(buffer, 'A');
stringbuffer_append_string(buffer, "12345");
stringbuffer_append_bool(buffer, true);
stringbuffer_append_short(buffer, 150);
stringbuffer_append_unsigned_short(buffer, 150);
stringbuffer_append_int(buffer, 150);
stringbuffer_append_unsigned_int(buffer, 150);
stringbuffer_append_long(buffer, 150);
stringbuffer_append_unsigned_long(buffer, 150);
stringbuffer_append_long_long(buffer, 150);
stringbuffer_append_unsigned_long_long(buffer, 150);
stringbuffer_append_string_with_options(
buffer,
"12345",
1, // offset
2 // length
);
// increase buffer size if needed to ensure capacity
// and prevent multiple reallocations in case of many
// append operations
stringbuffer_ensure_capacity(buffer, 10240);
// shrink the internal buffer to fit the exact value
// of the current content and prevent using more memory
// than needed.
stringbuffer_shrink(buffer);
// get a clone text value of the current buffer content
char *text = stringbuffer_to_string(buffer);
printf("The Text: %s\n", text);
// we can clear the content we have so far and reuse the
// string buffer instance
stringbuffer_clear(buffer);
bool is_empty = stringbuffer_is_empty(buffer);
printf("Is Empty: %d\n", is_empty);
// once done, free the buffer
stringbuffer_release(buffer);
} /* main */
```
## Contributing
See [contributing guide](.github/CONTRIBUTING.md)
See [Changelog](CHANGELOG.md)
## License
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.