Fixed operator< on ZString.
This commit is contained in:
parent
ad74a9de85
commit
0db4be8a99
20
ZString.cpp
20
ZString.cpp
@ -62,7 +62,23 @@ namespace coreutils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ZString::operator<(const ZString &valuex) const {
|
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 {
|
bool ZString::operator>(const ZString &valuex) const {
|
||||||
@ -253,7 +269,7 @@ namespace coreutils {
|
|||||||
|
|
||||||
int ZString::compare(ZString zstring) {
|
int ZString::compare(ZString zstring) {
|
||||||
char *end1 = data + length;
|
char *end1 = data + length;
|
||||||
char *end2 = zstring.getData() + getLength();
|
char *end2 = zstring.getData() + zstring.getLength();
|
||||||
char *cursor1 = cursor;
|
char *cursor1 = cursor;
|
||||||
char *cursor2 = zstring.getCursor();
|
char *cursor2 = zstring.getCursor();
|
||||||
while((cursor1 < end1) && (cursor2 < end2)) {
|
while((cursor1 < end1) && (cursor2 < end2)) {
|
||||||
|
@ -101,6 +101,10 @@ namespace coreutils {
|
|||||||
/// to an ostream using the << syntax.
|
/// 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;
|
bool operator<(const ZString &valuex) const;
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -78,4 +78,8 @@ int main(int argc, char **argv) {
|
|||||||
coreutils::ZString test11("value1");
|
coreutils::ZString test11("value1");
|
||||||
std::cout << test10.compare(test11) << ":" << test11.compare(test10) << std::endl;
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user