diff --git a/ZString.cpp b/ZString.cpp index 9529dd2..99074b9 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -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)) { diff --git a/ZString.h b/ZString.h index e253c77..330e5a2 100644 --- a/ZString.h +++ b/ZString.h @@ -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; diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index f916e61..16257e7 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -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; }