diff options
| author | Shinichiro Hamaji <shinichiro.hamaji@gmail.com> | 2016-02-17 15:43:38 +0900 |
|---|---|---|
| committer | Shinichiro Hamaji <shinichiro.hamaji@gmail.com> | 2016-02-17 15:46:14 +0900 |
| commit | 121165ed878d844662a25d5ee3b95ab1ceaddca2 (patch) | |
| tree | e56742c0b59827c1c8f089fd6da91f793cf33960 | |
| parent | 0a6e2a4e09f3863a150b060a68640b5efc99beab (diff) | |
| download | platform_build_kati-121165ed878d844662a25d5ee3b95ab1ceaddca2.tar.gz platform_build_kati-121165ed878d844662a25d5ee3b95ab1ceaddca2.tar.bz2 platform_build_kati-121165ed878d844662a25d5ee3b95ab1ceaddca2.zip | |
[C++] Compare last 8 bytes first in StringPiece::operator==
| -rw-r--r-- | string_piece.cc | 12 | ||||
| -rw-r--r-- | string_piece_test.cc | 4 |
2 files changed, 14 insertions, 2 deletions
diff --git a/string_piece.cc b/string_piece.cc index 78de4ed..d287616 100644 --- a/string_piece.cc +++ b/string_piece.cc @@ -21,6 +21,7 @@ #include <ctype.h> #include <limits.h> +#include <stdint.h> #include <algorithm> #include <ostream> @@ -32,8 +33,15 @@ typedef StringPiece::size_type size_type; bool operator==(const StringPiece& x, const StringPiece& y) { if (x.size() != y.size()) return false; - - return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; + size_t len = x.size(); + if (len >= sizeof(uint64_t)) { + len -= sizeof(uint64_t); + uint64_t xt = *reinterpret_cast<const uint64_t*>(x.data() + len); + uint64_t yt = *reinterpret_cast<const uint64_t*>(y.data() + len); + if (xt != yt) + return false; + } + return StringPiece::wordmemcmp(x.data(), y.data(), len) == 0; } void StringPiece::CopyToString(std::string* target) const { diff --git a/string_piece_test.cc b/string_piece_test.cc index 7b253a0..0434cbe 100644 --- a/string_piece_test.cc +++ b/string_piece_test.cc @@ -30,4 +30,8 @@ int main() { assert(sps.size() == 2); assert(sps.count(StringPiece("foo")) == 1); assert(sps.count(StringPiece("bar")) == 1); + + assert(StringPiece("hogefugahige") == StringPiece("hogefugahige")); + assert(StringPiece("hogefugahoge") != StringPiece("hogefugahige")); + assert(StringPiece("hogefugahige") != StringPiece("higefugahige")); } |
