patterncMinor
Bare-bones string library
Viewed 0 times
librarybonesstringbare
Problem
After years of criticizing others, I've finally found the time and worked up the courage to polish up one of my bits of code and solicit criticisms of my own.
This is a simple dynamic-string library that I wrote for one of my old C projects (don't judge too harshly if you look into it -- it's a few years old, and I've just begun revising it).
Some notes on the design:
Please note that this is not intended to be a fully functioning, Swiss army knife of a string library. The functionality is very limited, and that's the way I intended it. All it is used for is corralling an HTTP request response into something a bit safer and more flexible than a bare manually managed c-string. There is a possibility that it may grow more complex though, so anything in the interface that could prohibit that would be of great interest to me.
To my knowledge, it is fully compliant to the C99 standard.
string_buffer.h
```
#ifndef UPMON_STRING_BUFFER_H
#define UPMON_STRING_BUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include
#include
typedef struct StringBuffer {
char* str;
size_t len;
size_t cap;
char small_str[64];
} StringBuffer;
void string_buffer_init(StringBuffer* s);
void string_buffer_cleanup(StringBuffer* s);
const char string_buffer_cstr(const StringBuffer s);
size_t string_buffer_length(const StringBuffer* s);
bool string_buffer_set_bytes(StringBuffer s, const char str, size_t len);
bool string_buffer_set_cstr(StringBuffer s, const char str);
bool string_buffer_set_string_buffer(StringBuffer dst, const StringBuffer src);
bool string_buffer_append_bytes(StringBuffer s, const char str, size_t len);
bool string_buffer_append_cstr(StringBuffer* s, const
This is a simple dynamic-string library that I wrote for one of my old C projects (don't judge too harshly if you look into it -- it's a few years old, and I've just begun revising it).
Some notes on the design:
- Its interface is fairly heavily influenced by C++'s
std::string
- Internally, it manages a dynamically allocated character buffer
- It is encoding agnostic -- it thinks in bytes, not true characters
- For academic interest, I went ahead and did a naive small string optimization
Please note that this is not intended to be a fully functioning, Swiss army knife of a string library. The functionality is very limited, and that's the way I intended it. All it is used for is corralling an HTTP request response into something a bit safer and more flexible than a bare manually managed c-string. There is a possibility that it may grow more complex though, so anything in the interface that could prohibit that would be of great interest to me.
To my knowledge, it is fully compliant to the C99 standard.
string_buffer.h
```
#ifndef UPMON_STRING_BUFFER_H
#define UPMON_STRING_BUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include
#include
typedef struct StringBuffer {
char* str;
size_t len;
size_t cap;
char small_str[64];
} StringBuffer;
void string_buffer_init(StringBuffer* s);
void string_buffer_cleanup(StringBuffer* s);
const char string_buffer_cstr(const StringBuffer s);
size_t string_buffer_length(const StringBuffer* s);
bool string_buffer_set_bytes(StringBuffer s, const char str, size_t len);
bool string_buffer_set_cstr(StringBuffer s, const char str);
bool string_buffer_set_string_buffer(StringBuffer dst, const StringBuffer src);
bool string_buffer_append_bytes(StringBuffer s, const char str, size_t len);
bool string_buffer_append_cstr(StringBuffer* s, const
Solution
Overall, I think this is very well done. There is one major thing I find wrong with it though:
Where is the documentation?!?!?!
Sure, as a developer using this library I could read through your short source file and pick it apart to figure out how everything should work. But my time is better spent elsewhere, reading the documentation of other larger projects and programming my own code. That could actually even be a breaking point between using this library, and using a similar (even inferior) library that had good documentation for me to read.
Where is the documentation?!?!?!
Sure, as a developer using this library I could read through your short source file and pick it apart to figure out how everything should work. But my time is better spent elsewhere, reading the documentation of other larger projects and programming my own code. That could actually even be a breaking point between using this library, and using a similar (even inferior) library that had good documentation for me to read.
Context
StackExchange Code Review Q#52737, answer score: 8
Revisions (0)
No revisions yet.