MString now string builds with << operator.
This commit is contained in:
parent
d2f13c192c
commit
db801727c6
31
MString.cpp
31
MString.cpp
@ -47,6 +47,11 @@ namespace coreutils {
|
||||
free(data);
|
||||
}
|
||||
|
||||
int overflow(int c) {
|
||||
std::cout << '{' << c << "}";
|
||||
return 0;
|
||||
}
|
||||
|
||||
MString& MString::operator=(coreutils::ZString& value) {
|
||||
if(*this == value)
|
||||
return *this;
|
||||
@ -71,9 +76,33 @@ namespace coreutils {
|
||||
}
|
||||
|
||||
MString& MString::operator<<(const char *value) {
|
||||
int temp = length;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,13 @@ namespace coreutils {
|
||||
///
|
||||
|
||||
MString& operator<<(const char *value);
|
||||
|
||||
|
||||
MString& operator<<(const int value);
|
||||
|
||||
MString& operator<<(coreutils::ZString &zstring);
|
||||
|
||||
MString& operator<<(std::string value);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
Binary file not shown.
@ -20,11 +20,15 @@ int main(int argc, char **argv) {
|
||||
coreutils::MString test4;
|
||||
test4 << "this is a test.";
|
||||
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.");
|
||||
// coreutils::MString test = temp;
|
||||
// std::cout << "operator=: [" << test << "]." << std::endl;
|
||||
|
||||
// Stream assignment capabilities.
|
||||
coreutils::MString test6;
|
||||
int number = 75;
|
||||
test6 << "Will this work? " << number << test4 << " See how this works.";
|
||||
std::cout << "streaming: [" << test6 << "]." << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user