diff --git a/include/BwString.h b/include/BwString.h index 61e3ee6..d3bc018 100644 --- a/include/BwString.h +++ b/include/BwString.h @@ -6,9 +6,9 @@ extern "C" { #endif -BwString* BwString_new(const char* data, size_t len); -char* BwString_data(BwString* self); -size_t BwString_len(BwString* self); +BwString* BwString_new(const char* data, int len); +const char* BwString_data(const BwString* self); +int BwString_len(const BwString* self); void BwString_release(BwString* self); #ifdef __cplusplus diff --git a/include/Types.h b/include/Types.h index 6e6ab11..4faffce 100644 --- a/include/Types.h +++ b/include/Types.h @@ -1,7 +1,6 @@ #pragma once #include -#include #ifdef __cplusplus extern "C" { diff --git a/src/BwString.cpp b/src/BwString.cpp index 02a7e03..fae1176 100644 --- a/src/BwString.cpp +++ b/src/BwString.cpp @@ -1,33 +1,31 @@ #include -#include -#include +#include +#include struct BwString_ { - char* data; - size_t length; + std::string data; }; -BwString* BwString_new(const char* data, size_t len) { - BwString* self = new BwString(); - self->length = len; - - self->data = new char[len + 1 /* terminating \0 */]; - memmove(self->data, data, len); - self->data[len] = '\0'; +BwString* BwString_new(const char* data, int len) { + assert(data); + BwString* const self = new BwString(); + self->data.assign(data, len); return self; } -char* BwString_data(BwString* self) { - return self->data; +const char* BwString_data(const BwString* self) { + assert(self); + return self->data.c_str(); } -size_t BwString_len(BwString* self) { - return self->length; +int BwString_len(const BwString* self) { + assert(self); + return self->data.length(); } void BwString_release(BwString* self) { - delete[] self->data; + assert(self); delete self; }