Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::string inside BwString #8

Merged
merged 1 commit into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/BwString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion include/Types.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
Expand Down
30 changes: 14 additions & 16 deletions src/BwString.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
#include <BwString.h>

#include <stdlib.h>
#include <string.h>
#include <string>
#include <assert.h>

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;
}