Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | // Copyright (C) 2009-2013, International Business Machines |
michael@0 | 2 | // Corporation and others. All Rights Reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Copyright 2004 and onwards Google Inc. |
michael@0 | 5 | // |
michael@0 | 6 | // Author: wilsonh@google.com (Wilson Hsieh) |
michael@0 | 7 | // |
michael@0 | 8 | |
michael@0 | 9 | #include "unicode/utypes.h" |
michael@0 | 10 | #include "unicode/stringpiece.h" |
michael@0 | 11 | #include "cstring.h" |
michael@0 | 12 | #include "cmemory.h" |
michael@0 | 13 | |
michael@0 | 14 | U_NAMESPACE_BEGIN |
michael@0 | 15 | |
michael@0 | 16 | StringPiece::StringPiece(const char* str) |
michael@0 | 17 | : ptr_(str), length_((str == NULL) ? 0 : static_cast<int32_t>(uprv_strlen(str))) { } |
michael@0 | 18 | |
michael@0 | 19 | StringPiece::StringPiece(const StringPiece& x, int32_t pos) { |
michael@0 | 20 | if (pos < 0) { |
michael@0 | 21 | pos = 0; |
michael@0 | 22 | } else if (pos > x.length_) { |
michael@0 | 23 | pos = x.length_; |
michael@0 | 24 | } |
michael@0 | 25 | ptr_ = x.ptr_ + pos; |
michael@0 | 26 | length_ = x.length_ - pos; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) { |
michael@0 | 30 | if (pos < 0) { |
michael@0 | 31 | pos = 0; |
michael@0 | 32 | } else if (pos > x.length_) { |
michael@0 | 33 | pos = x.length_; |
michael@0 | 34 | } |
michael@0 | 35 | if (len < 0) { |
michael@0 | 36 | len = 0; |
michael@0 | 37 | } else if (len > x.length_ - pos) { |
michael@0 | 38 | len = x.length_ - pos; |
michael@0 | 39 | } |
michael@0 | 40 | ptr_ = x.ptr_ + pos; |
michael@0 | 41 | length_ = len; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | void StringPiece::set(const char* str) { |
michael@0 | 45 | ptr_ = str; |
michael@0 | 46 | if (str != NULL) |
michael@0 | 47 | length_ = static_cast<int32_t>(uprv_strlen(str)); |
michael@0 | 48 | else |
michael@0 | 49 | length_ = 0; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | U_EXPORT UBool U_EXPORT2 |
michael@0 | 53 | operator==(const StringPiece& x, const StringPiece& y) { |
michael@0 | 54 | int32_t len = x.size(); |
michael@0 | 55 | if (len != y.size()) { |
michael@0 | 56 | return false; |
michael@0 | 57 | } |
michael@0 | 58 | if (len == 0) { |
michael@0 | 59 | return true; |
michael@0 | 60 | } |
michael@0 | 61 | const char* p = x.data(); |
michael@0 | 62 | const char* p2 = y.data(); |
michael@0 | 63 | // Test last byte in case strings share large common prefix |
michael@0 | 64 | --len; |
michael@0 | 65 | if (p[len] != p2[len]) return false; |
michael@0 | 66 | // At this point we can, but don't have to, ignore the last byte. |
michael@0 | 67 | return uprv_memcmp(p, p2, len) == 0; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | |
michael@0 | 71 | const int32_t StringPiece::npos = 0x7fffffff; |
michael@0 | 72 | |
michael@0 | 73 | U_NAMESPACE_END |