Fixed operator< on ZString.

This commit is contained in:
Brad Arant 2024-07-31 13:03:45 -07:00
parent ad74a9de85
commit 0db4be8a99
3 changed files with 26 additions and 2 deletions

View File

@ -62,7 +62,23 @@ namespace coreutils {
}
bool ZString::operator<(const ZString &valuex) const {
return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length)) < 0);
char *end1 = data + length;
char *end2 = valuex.getData() + valuex.getLength();
char *cursor1 = cursor;
char *cursor2 = valuex.getCursor();
while((cursor1 < end1) && (cursor2 < end2)) {
if(*cursor1 < *cursor2)
return 1;
if(*cursor1 > *cursor2)
return 0;
++cursor1;
++cursor2;
}
if((cursor1 == end1) && (cursor2 != end2))
return 1;
if((cursor2 == end2) && (cursor1 != end1))
return 0;
return 0;
}
bool ZString::operator>(const ZString &valuex) const {
@ -253,7 +269,7 @@ namespace coreutils {
int ZString::compare(ZString zstring) {
char *end1 = data + length;
char *end2 = zstring.getData() + getLength();
char *end2 = zstring.getData() + zstring.getLength();
char *cursor1 = cursor;
char *cursor2 = zstring.getCursor();
while((cursor1 < end1) && (cursor2 < end2)) {

View File

@ -100,6 +100,10 @@ namespace coreutils {
/// A friend method used to write the value of the ZString
/// to an ostream using the << syntax.
///
///
/// The < operator is used to compare values if ZString is used in std::map.
///
bool operator<(const ZString &valuex) const;

View File

@ -77,5 +77,9 @@ int main(int argc, char **argv) {
coreutils::ZString test10("value1");
coreutils::ZString test11("value1");
std::cout << test10.compare(test11) << ":" << test11.compare(test10) << std::endl;
coreutils::ZString test12("1");
coreutils::ZString test13("12");
std::cout << (test12 < test13) << ":" << (test13 < test12) << std::endl;
}