diff --git a/ZString.cpp b/ZString.cpp index 56e69c8..9529dd2 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -1,3 +1,4 @@ + #include "ZString.h" #include "Log.h" #include "Exception.h" @@ -251,7 +252,23 @@ namespace coreutils { } int ZString::compare(ZString zstring) { - + char *end1 = data + length; + char *end2 = zstring.getData() + getLength(); + char *cursor1 = cursor; + char *cursor2 = zstring.getCursor(); + while((cursor1 < end1) && (cursor2 < end2)) { + if(*cursor1 < *cursor2) + return -1; + if(*cursor1 > *cursor2) + return 1; + ++cursor1; + ++cursor2; + } + if((cursor1 == end1) && (cursor2 != end2)) + return -1; + if((cursor2 == end2) && (cursor1 != end1)) + return 1; + return 0; } bool ZString::equals(const char *value) { diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index 6d329a5..f916e61 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -69,6 +69,13 @@ int main(int argc, char **argv) { std::cout << test7.lineIsWhitespace() << test7.goeol() << std::endl; std::cout << test7.lineIsWhitespace() << test7.goeol() << std::endl; std::cout << test7.lineIsWhitespace() << test7.goeol() << std::endl; - + + coreutils::ZString test8("value1"); + coreutils::ZString test9("value2"); + std::cout << test8.compare(test9) << ":" << test9.compare(test8) << ":" << test8.compare(test8) << std::endl; + + coreutils::ZString test10("value1"); + coreutils::ZString test11("value1"); + std::cout << test10.compare(test11) << ":" << test11.compare(test10) << std::endl; }