Completed ZString::compare method.

This commit is contained in:
Brad Arant 2024-07-24 17:01:25 +00:00
parent 6409bd2444
commit ad74a9de85
2 changed files with 26 additions and 2 deletions

View File

@ -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) {

View File

@ -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;
}