diff --git a/MString.cpp b/MString.cpp index 05b700c..88104bd 100644 --- a/MString.cpp +++ b/MString.cpp @@ -2,6 +2,7 @@ #include "Log.h" #include #include +#include namespace coreutils { @@ -12,32 +13,26 @@ namespace coreutils { } MString::MString(const char *data) { - this->length = strlen(data); - setSize(this->length); - this->data = (char *)malloc(bufferSize); + setSize(strlen(data)); memcpy(this->data, data, length); cursor = this->data; } MString::MString(char *data, size_t length) { - this->length = length; setSize(length); memcpy(this->data, data, length); cursor = this->data; } MString::MString(const char *data, size_t length) { - this->length = length; setSize(length); memcpy(this->data, data, length); cursor = this->data; } MString::MString(const MString &mstring) { - bufferSize = (mstring.length % 256) + 256; - data = (char *)malloc(bufferSize); + setSize(mstring.length); memcpy(data, mstring.data, mstring.length); - length = mstring.length; cursor = data; // Log(LOG_DEBUG_2) << "MString Copy Constructor: "; } @@ -45,7 +40,6 @@ namespace coreutils { MString::MString(std::string data) { setSize(data.length()); memcpy(this->data, (char *)data.c_str(), data.length()); - length = data.length(); cursor = this->data; } @@ -55,21 +49,26 @@ namespace coreutils { } int MString::write(char ch) { - setSize(bufferSize + 1); - data[length++] = ch; + setSize(length + 1); + *(data + length - 1) = ch; return 1; } int MString::write(ZString &value) { - setSize(bufferSize + value.getLength()); - memcpy(data, value.getData(), value.getLength()); - length += value.getLength(); + int len = length; + setSize(length + value.getLength()); + memcpy(data + len, value.getData(), value.getLength()); return value.getLength(); } void MString::setSize(int size) { - bufferSize = (size % 256) + 256; - data = (char *)realloc(data, bufferSize); + int cursorOffset = cursor - data; + int newBufferSize = ((size / 256) + 1) * 256; + if(bufferSize != newBufferSize) { + bufferSize = newBufferSize; + data = (char *)realloc(data, bufferSize); + cursor = data + cursorOffset; + } length = size; } diff --git a/ZString.cpp b/ZString.cpp index f990551..eb6b262 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -81,6 +81,15 @@ namespace coreutils { cursor += length; return cursor - temp; } + + void ZString::push() { + stack.push(cursor); + } + + void ZString::pop() { + cursor = stack.top(); + stack.pop(); + } std::string ZString::str() { diff --git a/ZString.h b/ZString.h index 25c217c..175f42f 100644 --- a/ZString.h +++ b/ZString.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace coreutils { @@ -27,7 +28,7 @@ namespace coreutils { /// During the parsing process you can retrieve the parsed and unparsed sections of /// the ZString as ZStrings. /// - /// You can stream the ZString to an output stream usinf the << operator and the entire + /// You can stream the ZString to an output stream using the << operator and the entire /// contents of the ZString will be output regardless of parsing activity. /// @@ -99,6 +100,20 @@ namespace coreutils { /// int incrementCursor(int length); + + /// + /// Use the push method to push the current cursor pointer to a stack. + /// + + void push(); + + /// + /// Use the pop method to pop a previously pushed cursor pointer from the + /// cursor stack. The cursor will point to the same location in the + /// ZString where the cursor was pushed. + /// + + void pop(); /// /// Return the value of the ZString from the cursor to the end. @@ -282,6 +297,7 @@ namespace coreutils { private: std::vector list; + std::stack stack; bool isCharacter(char ch, const char *string); };