Skip to main content
deleted 14 characters in body
Source Link
Jamal
  • 35.4k
  • 13
  • 135
  • 240

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

  1. __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::string is likely to outperform your library.

  2. StringBuffer declaration: You can omit the struct name when using typedef. I changed len and cap to length and    capacity, 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

  1. I do like the small_str which cuts down on small allocations.
    When When you realloc the pointer, though, you just ask for min_cap. After After the memcpy, 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

  1. There is no check for NULLNULL pointers. That's gonna lead to lots of    SIGSEGVs if you accidentally forget to call string_buffer_init, for for example.
  2. 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:

  • stringbuffer

    stringbuffer

  • stringBuffer

    stringBuffer

  • sb

    sb rather than string_builder.

    rather than string_builder. It should be a compact name; consider e.g. glPushMatrix() or alcOpenDevice.*/

    It should be a compact name; consider e.g. glPushMatrix() or alcOpenDevice.

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.

Therefore I will be considering safety concerns, if you don't mind.

string_buffer.h

  1. __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::string is likely to outperform your library.

  2. StringBuffer declaration: You can omit the struct name when using typedef. I changed len and cap to length and  capacity, as they look clearer. typedef struct { char* str; size_t length; size_t capacity; char small_str[64]; } StringBuffer;


string_buffer.cpp

  1. I do like the small_str which cuts down on small allocations.
    When you realloc the pointer, though, you just ask for min_cap. After the memcpy, 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.

*I would turn sizeof(small_str) into a constant, kind of default_size, since it appears frequently.


Overall design

  1. There is no check for NULL pointers. That's gonna lead to lots of  SIGSEGVs if you accidentally forget to call string_buffer_init, for example.
  2. It's important to point out that you're actually using a function which belongs to an external library. Nevertheless, I'd go for:
  • stringbuffer
  • stringBuffer
  • sb
    rather than string_builder. It should be a compact name; consider e.g. glPushMatrix() or alcOpenDevice.*/

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.

Therefore I will be considering safety concerns, if you don't mind.

string_buffer.h

  1. __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::string is likely to outperform your library.

  2. StringBuffer declaration: You can omit the struct name when using typedef. I changed len and cap to length and  capacity, as they look clearer.

    typedef struct {
        char* str;
        size_t length;
        size_t capacity;
        char small_str[64];
    } StringBuffer;
    

string_buffer.cpp

  1. I do like the small_str which cuts down on small allocations. When you realloc the pointer, though, you just ask for min_cap. After the memcpy, 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.

* I would turn sizeof(small_str) into a constant, kind of like default_size, since it appears frequently.


Overall design

  1. There is no check for NULL pointers. That's gonna lead to lots of  SIGSEGVs if you accidentally forget to call string_buffer_init, for example.
  2. It's important to point out that you're actually using a function which belongs to an external library.

Nevertheless, I'd go for:

  • stringbuffer

  • stringBuffer

  • sb rather than string_builder.

    It should be a compact name; consider e.g. glPushMatrix() or alcOpenDevice.

Source Link
edmz
  • 1k
  • 7
  • 16

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.

Therefore I will be considering safety concerns, if you don't mind.

string_buffer.h

  1. __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::string is likely to outperform your library.

  2. StringBuffer declaration: You can omit the struct name when using typedef. I changed len and cap to length and capacity, as they look clearer. typedef struct { char* str; size_t length; size_t capacity; char small_str[64]; } StringBuffer;


string_buffer.cpp

  1. I do like the small_str which cuts down on small allocations.
    When you realloc the pointer, though, you just ask for min_cap. After the memcpy, 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.

*I would turn sizeof(small_str) into a constant, kind of default_size, since it appears frequently.


Overall design

  1. There is no check for NULL pointers. That's gonna lead to lots of SIGSEGVs if you accidentally forget to call string_buffer_init, for example.
  2. It's important to point out that you're actually using a function which belongs to an external library. Nevertheless, I'd go for:
  • stringbuffer
  • stringBuffer
  • sb
    rather than string_builder. It should be a compact name; consider e.g. glPushMatrix() or alcOpenDevice.*/