diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-20 22:18:23 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-20 22:18:23 +0100 |
commit | 151571c4540e02b130c808d58d3cc169dd87627a (patch) | |
tree | 57627ffdcd8f57d72d5f4baef56847087a7460f9 /strbuf.h | |
parent | Further C++ improvements, maybe doesn't segfault now. (diff) | |
download | calp-c++.tar.gz calp-c++.tar.xz |
new segfaults, I give up.c++
Diffstat (limited to 'strbuf.h')
-rw-r--r-- | strbuf.h | 28 |
1 files changed, 15 insertions, 13 deletions
@@ -3,9 +3,11 @@ #include <unistd.h> #include <cstdlib> -#include <cstring> +// #include <cstring> #include <string> +#include <iostream> + /* * A high level string type which holds it's own length, how much * memmory it has allocated for itself, and a seek pointer into the @@ -15,12 +17,13 @@ * access to the memmory. */ struct strbuf { - char* mem; /* TODO add support for negative ptr */ - int ptr = 0; - size_t alloc; + int ptr = 0; + size_t alloc = 0; size_t len = 0; + char* mem; + strbuf () : strbuf (1) { }; strbuf (const strbuf& other); @@ -36,11 +39,9 @@ struct strbuf { */ void realloc (size_t len); - bool operator==(strbuf& other) - { return strncmp(this->mem, other.mem, this->len) == 0; } + bool operator==(strbuf& other); - bool operator==(const char* other) - { return strncmp(this->mem, other, this->len) == 0 ; } + bool operator==(const char* other); strbuf& operator=(strbuf* other); @@ -72,15 +73,16 @@ struct strbuf { this->ptr = 0; this->len = 0; }; - std::string to_string() { - return std::string (this->mem); - } - char* c_str() { - return this->mem; + char* c_str(); + + std::string to_string() { + return std::string (this->c_str()); } }; +std::ostream& operator << (std::ostream& out, strbuf& str); + /* * Reallocs dest to be the same size as src, and copies the contents * of src into dest. |