Fixed == operator for empty string comparison.

This commit is contained in:
Brad Arant 2024-06-14 10:21:28 -07:00
parent f6e5e60e5b
commit beb3673241
8 changed files with 25 additions and 14 deletions

View File

@ -38,7 +38,13 @@ namespace coreutils {
cursor = this->data;
}
MString::MString(ZString zstring) {
MString::MString(const ZString &zstring) {
setSize(zstring.getLength());
memcpy(data, zstring.getData(), zstring.getLength());
cursor = data;
}
MString::MString(ZString &zstring) {
setSize(zstring.getLength());
memcpy(data, zstring.getData(), zstring.getLength());
cursor = data;

View File

@ -6,8 +6,7 @@
#include <sstream>
#include <ostream>
namespace coreutils
{
namespace coreutils {
///
/// Use the MString object when you need a permanent backing store on the heap
@ -42,12 +41,15 @@ namespace coreutils
MString(const unsigned char *data, size_t length);
MString (ZString zstring);
// MString (ZString zstring);
///
/// Consructor providing a copy of a ZString.
///
MString (ZString &zstring);
MString (const ZString &zstring);
///
/// Consructor providing a copy of a ZString.
///

View File

@ -68,6 +68,8 @@ namespace coreutils {
}
bool ZString::operator==(const ZString &valuex) {
if((valuex.getLength() == 0) && (length != 0))
return false;
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0);
}
@ -348,11 +350,11 @@ namespace coreutils {
return ZString(temp, size);
}
char *ZString::getData() {
char *ZString::getData() const {
return data;
}
char *ZString::getCursor() {
char *ZString::getCursor() const {
return cursor;
}
@ -362,7 +364,7 @@ namespace coreutils {
this->cursor = cursor;
}
size_t ZString::getLength() {
size_t ZString::getLength() const {
return length - (cursor - data);
}

View File

@ -311,13 +311,13 @@ namespace coreutils {
/// the number of bytes specified by size.
///
char *getData();
char *getData() const;
///
///
///
char *getCursor();
char *getCursor() const;
///
///
@ -329,7 +329,7 @@ namespace coreutils {
/// Return the length of the ZString.
///
size_t getLength();
size_t getLength() const;
///
/// Set this ZString to have the same data space as the ZString pointer

View File

@ -1,4 +1,4 @@
#!/bin/bash
g++ -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
#g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils

Binary file not shown.

Binary file not shown.

View File

@ -6,8 +6,9 @@ int main(int argc, char **argv) {
// Test comparison operations.
coreutils::ZString test1("");
coreutils::ZString test1("else");
std::cout << (test1 != ".") << std::endl;
std::cout << (test1 == "") << std::endl;
// Test constructors.