diff --git a/MString.cpp b/MString.cpp index 32a18fe..05a83ca 100644 --- a/MString.cpp +++ b/MString.cpp @@ -1,6 +1,5 @@ #include "MString.h" #include "Log.h" -#include #include #include @@ -56,6 +55,27 @@ namespace coreutils { memcpy(data + len, value.getData(), value.getLength()); return *this; } + + MString& MString::operator=(const char *value) { + int len = strlen(value); + setSize(len); + memcpy(data, value, len); + return *this; + } + + MString& MString::operator=(char value) { + int len = 1; + setSize(1); + *data = value; + return *this; + } + + MString& MString::operator<<(const char *value) { + int len = strlen(value); + setSize(len); + memcpy(data, value, len); + return *this; + } int MString::write(char ch) { setSize(length + 1); diff --git a/MString.h b/MString.h index 1773714..9252399 100644 --- a/MString.h +++ b/MString.h @@ -2,6 +2,8 @@ #define __MString_h__ #include "ZString.h" +#include +#include namespace coreutils { @@ -56,6 +58,24 @@ namespace coreutils { MString& operator=(coreutils::ZString& data); + /// + /// Assignment operator will copy data to backing store. + /// + + MString& operator=(const char *data); + + /// + /// Assignment operator will copy data to backing store. + /// + + MString& operator=(char data); + + /// + /// + /// + + MString& operator<<(const char *value); + /// /// /// diff --git a/testing/compile b/testing/compile new file mode 100755 index 0000000..d4d2dd7 --- /dev/null +++ b/testing/compile @@ -0,0 +1,2 @@ +#!/bin/bash +g++ -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils \ No newline at end of file diff --git a/testing/mstring_test b/testing/mstring_test new file mode 100755 index 0000000..ecf0e8f Binary files /dev/null and b/testing/mstring_test differ diff --git a/testing/mstring_test.cpp b/testing/mstring_test.cpp new file mode 100644 index 0000000..e0aae0d --- /dev/null +++ b/testing/mstring_test.cpp @@ -0,0 +1,30 @@ +#include +#include "../MString.h" + +int main(int argc, char **argv) { + + // Literal string assignment tests. + coreutils::MString test1 = "this is an assignment test."; + std::cout << "char * operator=: [" << test1 << "]." << std::endl; + test1 = "new value."; + std::cout << "char * operator=: [" << test1 << "]." << std::endl; + + coreutils::MString test2("this is another test."); + std::cout << "assign on constructor: [" << test2 << "]." << std::endl; + + // Character assignment tests. + coreutils::MString test3; + test3 = 'a'; + std::cout << "char operator=: [" << test3 << "]." << std::endl; + + coreutils::MString test4; + test4 << "this is a test."; + std::cout << "char* operator<<: [" << test4 << "]." << std::endl; + +// coreutils::ZString temp("this is literal zstring data."); +// coreutils::MString test = temp; +// std::cout << "operator=: [" << test << "]." << std::endl; + + return 0; +} +