diff options
author | Nicolas Catania <niko@google.com> | 2010-02-08 14:13:47 -0800 |
---|---|---|
committer | Nicolas Catania <niko@google.com> | 2010-02-08 20:41:39 -0800 |
commit | 6309a85f3be27b49451e37d0b31446e0cf727f23 (patch) | |
tree | dd47e89f93544ea4aed1bc1ace8197b187209025 | |
parent | cb8eb8e1390d1343563a55c117b5c39cfa87fe1d (diff) | |
download | astl-6309a85f3be27b49451e37d0b31446e0cf727f23.tar.gz |
Added <,>,<= and >= operators functions for strings.
-rw-r--r-- | include/string | 9 | ||||
-rw-r--r-- | src/string.cpp | 20 | ||||
-rw-r--r-- | tests/test_string.cpp | 16 |
3 files changed, 45 insertions, 0 deletions
diff --git a/include/string b/include/string index e7fe904..dccd5ac 100644 --- a/include/string +++ b/include/string @@ -306,6 +306,15 @@ class string size_type mLength; // len of the string excl. null-terminator. }; +// Comparaison: +bool operator<(const string& lhs, const string& rhs); +bool operator<=(const string& lhs, const string& rhs); +bool operator>(const string& lhs, const string& rhs); +bool operator>=(const string& lhs, const string& rhs); + +// Swap +void swap(string& lhs, string& rhs); + // I/O ostream& operator<<(ostream& os, const string& str); diff --git a/src/string.cpp b/src/string.cpp index 4e763e6..d2457fc 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -613,6 +613,26 @@ string::size_type string::find_last_not_of(value_type c, size_type pos) const { } } +bool operator<(const string& lhs, const string& rhs) { + return lhs.compare(rhs) < 0; +} + +bool operator<=(const string& lhs, const string& rhs) { + return lhs.compare(rhs) <= 0; +} + +bool operator>(const string& lhs, const string& rhs) { + return lhs.compare(rhs) > 0; +} + +bool operator>=(const string& lhs, const string& rhs) { + return lhs.compare(rhs) >= 0; +} + +void swap(string& lhs, string& rhs) { + lhs.swap(rhs); +} + ostream& operator<<(ostream& os, const string& str) { return os.write_formatted(str.data(), str.size()); } diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 412f065..c2405ae 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -488,6 +488,22 @@ bool testCompare() string str06; EXPECT_TRUE(str06 != NULL); + { + string str_long("this is"); + string str_short("it"); + EXPECT_TRUE(str_long > str_short); + EXPECT_TRUE(str_long >= str_short); + EXPECT_FALSE(str_long < str_short); + EXPECT_FALSE(str_long <= str_short); + } + { + string str_lhs("this is"); + string str_rhs("this is"); + EXPECT_FALSE(str_lhs > str_rhs); + EXPECT_TRUE(str_lhs >= str_rhs); + EXPECT_FALSE(str_lhs < str_rhs); + EXPECT_TRUE(str_lhs <= str_rhs); + } return true; } |