All it is used for is corralling an HTTP request response into something a bit safer and more flexible than a bare manually managed cC-string.
Therefore I will be considering safety concerns, if you don't mind.
string_buffer.h
__cplusplus: I don't know whether this string library is used in C++ too or not. Either way, I would personally avoid that: C++ has
std::string. And if, for some reason, you are using this library to handle strings...just don't again.std::stringis likely to outperform your library.StringBuffer declaration: You can omit the struct name when using
typedef. I changedlenandcaptolengthandcapacity, as they look clearer.typedef struct { char* str; size_t length; size_t capacity; char small_str[64]; } StringBuffer;typedef struct { char* str; size_t length; size_t capacity; char small_str[64]; } StringBuffer;
string_buffer.cpp
- I do like the
small_strwhich cuts down on small allocations.
When When youreallocthe pointer, though, you just ask formin_cap. After After thememcpy,length==== capacity. Thus, I would recommend you to allocate more bytes e.g.min_cap+sizeof(small_str)*. You can find this useful if the library often handles big strings.
*IThus, I would recommend you to allocate more bytes e.g. min_cap + sizeof(small_str) *. You can find this useful if the library often handles big strings.
* I would turn sizeof(small_str) into a constant, kind of like default_size, since it appears frequently.
Overall design
- There is no check for NULL
NULLpointers. That's gonna lead to lots ofSIGSEGVs if you accidentally forget to callstring_buffer_init, for for example. - It's important to point out that you're actually using a function which belongs to an external library. Nevertheless, I'd go for:
Nevertheless, I'd go for:
stringbufferstringbufferstringBufferstringBuffersb
rather thansbrather thanstring_builder.string_builder. It should be a compact name; consider e.g.glPushMatrix()oralcOpenDevice.*/It should be a compact name; consider e.g.
glPushMatrix()oralcOpenDevice.