MString now string builds with << operator.

This commit is contained in:
Brad Arant 2022-07-28 18:36:44 -07:00
parent d2f13c192c
commit db801727c6
4 changed files with 45 additions and 6 deletions

View File

@ -47,6 +47,11 @@ namespace coreutils {
free(data); free(data);
} }
int overflow(int c) {
std::cout << '{' << c << "}";
return 0;
}
MString& MString::operator=(coreutils::ZString& value) { MString& MString::operator=(coreutils::ZString& value) {
if(*this == value) if(*this == value)
return *this; return *this;
@ -71,9 +76,33 @@ namespace coreutils {
} }
MString& MString::operator<<(const char *value) { MString& MString::operator<<(const char *value) {
int temp = length;
int len = strlen(value); int len = strlen(value);
setSize(len + length);
memcpy(data + temp, value, len);
return *this;
}
MString& MString::operator<<(const int value) {
std::stringstream temp;
temp << value;
*this << temp.str();
return *this;
}
MString& MString::operator<<(coreutils::ZString &zstring) {
int temp = length;
int len = length + zstring.getLength();
setSize(len); setSize(len);
memcpy(data, value, len); memcpy(data + temp, zstring.getData(), zstring.getLength());
return *this;
}
MString& MString::operator<<(std::string value) {
int temp = length;
int len = length + value.length();
setSize(len);
memcpy(data + temp, value.c_str(), value.length());
return *this; return *this;
} }

View File

@ -76,6 +76,12 @@ namespace coreutils {
MString& operator<<(const char *value); MString& operator<<(const char *value);
MString& operator<<(const int value);
MString& operator<<(coreutils::ZString &zstring);
MString& operator<<(std::string value);
/// ///
/// ///
/// ///

Binary file not shown.

View File

@ -20,10 +20,14 @@ int main(int argc, char **argv) {
coreutils::MString test4; coreutils::MString test4;
test4 << "this is a test."; test4 << "this is a test.";
std::cout << "char* operator<<: [" << test4 << "]." << std::endl; std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
test4 << "another test " << 75;
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
// coreutils::ZString temp("this is literal zstring data."); // Stream assignment capabilities.
// coreutils::MString test = temp; coreutils::MString test6;
// std::cout << "operator=: [" << test << "]." << std::endl; int number = 75;
test6 << "Will this work? " << number << test4 << " See how this works.";
std::cout << "streaming: [" << test6 << "]." << std::endl;
return 0; return 0;
} }