susumu.yata
null+****@clear*****
Fri Mar 22 10:41:25 JST 2013
susumu.yata 2013-03-22 10:41:25 +0900 (Fri, 22 Mar 2013) New Revision: a8611a9a47893e2ebaea9a3360a14043ad10af30 https://github.com/groonga/grnxx/commit/a8611a9a47893e2ebaea9a3360a14043ad10af30 Message: Add grnxx::Slice::compare() which ignores the starting "n" bytes. Modified files: lib/slice.hpp test/test_slice.cpp Modified: lib/slice.hpp (+12 -0) =================================================================== --- lib/slice.hpp 2013-03-21 11:51:38 +0900 (64ece56) +++ lib/slice.hpp 2013-03-22 10:41:25 +0900 (0c4498c) @@ -79,6 +79,18 @@ class Slice { } return (size_ < s.size_) ? -1 : (size_ > s.size_); } + // Compare "*this" and "s" and return a negative value if "*this" < "s", + // zero if "*this" == "s", or a positive value otherwise (if "*this" > "s"). + // The starting "offset" bytes of "*this" and "s" are ignored. Note that too + // large "offset" results in undefined behavior. + int compare(const Slice &s, size_t offset) const { + const size_t min_size = (size_ < s.size_) ? size_ : s.size_; + int result = std::memcmp(ptr_ + offset, s.ptr_ + offset, min_size - offset); + if (result != 0) { + return result; + } + return (size_ < s.size_) ? -1 : (size_ > s.size_); + } // Return true iff "s" is a prefix of "*this". bool starts_with(const Slice &s) const { Modified: test/test_slice.cpp (+9 -0) =================================================================== --- test/test_slice.cpp 2013-03-21 11:51:38 +0900 (1c31c21) +++ test/test_slice.cpp 2013-03-22 10:41:25 +0900 (d8f9489) @@ -142,6 +142,15 @@ void test_compare() { assert(cde.compare(abc) > 0); assert(cde.compare(abcde) > 0); assert(cde.compare(cde) == 0); + + grnxx::Slice xycde("xycde"); + + assert(abcde.compare(xycde, 0) < 0); + assert(abcde.compare(xycde, 1) < 0); + assert(abcde.compare(xycde, 2) == 0); + assert(abcde.compare(xycde, 3) == 0); + assert(abcde.compare(xycde, 4) == 0); + assert(abcde.compare(xycde, 5) == 0); } void test_starts_with() { -------------- next part -------------- HTML����������������������������...Download