Many fixes for MString, ZString.

This commit is contained in:
Brad Arant 2022-08-23 09:30:56 -07:00
parent d0d5172afa
commit 2a349c6ceb
9 changed files with 65 additions and 34 deletions

View File

@ -63,4 +63,9 @@ namespace coreutils {
return std::string(buffer, size);
}
coreutils::ZString & File::asZString() {
zstring = ZString(buffer, size);
return zstring;
}
}

3
File.h
View File

@ -24,7 +24,7 @@ namespace coreutils {
void write(std::string data);
void write(coreutils::ZString &data);
std::string asString();
coreutils::ZString& asZString();
char *buffer;
size_t size;
@ -33,6 +33,7 @@ namespace coreutils {
private:
void open(std::string fileName, int mode, int authority);
int fd;
coreutils::ZString zstring;
};

View File

@ -105,30 +105,30 @@ namespace coreutils {
int len = strlen(value);
setSize(len + length);
memcpy(data + temp, value, len);
return *this;
return *this;
}
MString& MString::operator<<(const int value) {
std::stringstream temp;
temp << value;
*this << temp.str();
return *this;
return *this;
}
// MString& MString::operator<<(coreutils::ZString &zstring) {
// int temp = length;
// int len = length + zstring.getLength();
// setSize(len);
// memcpy(data + temp, zstring.getData(), zstring.getLength());
// return *this;
// }
// MString& MString::operator<<(coreutils::ZString &zstring) {
// int temp = length;
// int len = length + zstring.getLength();
// setSize(len);
// memcpy(data + temp, zstring.getData(), zstring.getLength());
// return *this;
// }
MString& MString::operator<<(coreutils::ZString zstring) {
int temp = length;
int len = length + zstring.getLength();
setSize(len);
memcpy(data + temp, zstring.getData(), zstring.getLength());
return *this;
return *this;
}
MString& MString::operator<<(std::string value) {
@ -136,7 +136,7 @@ namespace coreutils {
int len = length + value.length();
setSize(len);
memcpy(data + temp, value.c_str(), value.length());
return *this;
return *this;
}
MString& MString::write(char ch) {

View File

@ -45,14 +45,20 @@ namespace coreutils {
// Log(LOG_DEBUG_2) << "ZString Copy Constructor: ";
}
ZString::ZString(std::string data) {
ZString::ZString(std::string &data) {
this->data = (char *)data.c_str();
length = data.length();
cursor = (char *)data.c_str();
}
// ZString::ZString(std::string data) {
// this->data = (char *)data.c_str();
// length = data.length();
// cursor = (char *)data.c_str();
// }
ZString::~ZString() {
if(cdata)
if(cdata != NULL)
free(cdata);
}
@ -64,10 +70,16 @@ namespace coreutils {
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) > 0);
}
bool ZString::operator==(const ZString &valuex) const {
bool ZString::operator==(const ZString &valuex) {
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0);
}
bool ZString::operator==(std::string value) {
if(value.length() != length)
return false;
return (strncmp(data, value.c_str(), length) == 0);
}
bool ZString::operator==(const char *valuex) const {
return (strncmp(data, valuex, length) == 0);
}
@ -105,14 +117,14 @@ namespace coreutils {
}
void ZString::push() {
stack.push(cursor);
stack.push(cursor);
}
void ZString::pop() {
cursor = stack.top();
stack.pop();
stack.pop();
}
std::string ZString::str() {
return std::string(data, length);
}
@ -126,7 +138,7 @@ namespace coreutils {
strncpy(cdata, data, length);
cdata[length] = '\0';
return cdata;
}
}
ZString ZString::substring(int start) {
char *end = data + length;
@ -167,7 +179,7 @@ namespace coreutils {
}
}
list.push_back(ZString(cursor, pos - cursor));
cursor = pos;
cursor = data;
return list;
}

View File

@ -56,7 +56,7 @@ namespace coreutils {
///
///
///
ZString(const char *data, size_t length);
///
@ -69,7 +69,13 @@ namespace coreutils {
/// Consructor from a string.
///
ZString(std::string string);
ZString(std::string &string);
///
/// Consructor from a string.
///
// ZString(std::string string);
///
/// Destructor for the ZString.
@ -90,15 +96,16 @@ namespace coreutils {
///
///
///
bool operator<(const ZString &valuex) const;
///
///
///
bool operator>(const ZString &valuex) const;
bool operator==(const ZString &valuex) const;
bool operator==(const ZString &valuex);
bool operator==(std::string value);
bool operator==(const char *valuex) const;
bool operator!=(const ZString &valuex) const;
bool operator!=(const char *valuex) const;
@ -165,9 +172,9 @@ namespace coreutils {
///
///
///
ZString substring(int start, int len);
///
///
///
@ -197,7 +204,7 @@ namespace coreutils {
///
///
///
ZString getTokenExclude(std::string exclude);
///
@ -252,13 +259,13 @@ namespace coreutils {
///
///
///
bool ifNext(ZString &value);
///
///
///
int ifEqualsCount(ZString &comparator);
///
@ -296,7 +303,7 @@ namespace coreutils {
///
///
///
void setCursor(char *cursor);
///
@ -323,7 +330,7 @@ namespace coreutils {
///
///
///
ZString unparsed();
///

Binary file not shown.

View File

@ -17,8 +17,9 @@ int main(int argc, char **argv) {
test3 = 'a';
std::cout << "char operator=: [" << test3 << "]." << std::endl;
coreutils::ZString test7("zstring data");
coreutils::MString test4;
test4 << "this is a test.";
test4 << "this is a test." << test7;
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
test4 << "another test " << 75;
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;

Binary file not shown.

View File

@ -6,7 +6,12 @@ int main(int argc, char **argv) {
// Test comparison operations.
coreutils::ZString test1("");
std::cout << (test1 != ".");
std::cout << (test1 != ".") << std::endl;
// Test constructors.
coreutils::ZString test3("1659979856.747021214|barant@barant");
std::cout << test3 << std::endl;
// Test split operations.