1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/stringpiece.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 1.4 +// Copyright (C) 2009-2013, International Business Machines 1.5 +// Corporation and others. All Rights Reserved. 1.6 +// 1.7 +// Copyright 2004 and onwards Google Inc. 1.8 +// 1.9 +// Author: wilsonh@google.com (Wilson Hsieh) 1.10 +// 1.11 + 1.12 +#include "unicode/utypes.h" 1.13 +#include "unicode/stringpiece.h" 1.14 +#include "cstring.h" 1.15 +#include "cmemory.h" 1.16 + 1.17 +U_NAMESPACE_BEGIN 1.18 + 1.19 +StringPiece::StringPiece(const char* str) 1.20 + : ptr_(str), length_((str == NULL) ? 0 : static_cast<int32_t>(uprv_strlen(str))) { } 1.21 + 1.22 +StringPiece::StringPiece(const StringPiece& x, int32_t pos) { 1.23 + if (pos < 0) { 1.24 + pos = 0; 1.25 + } else if (pos > x.length_) { 1.26 + pos = x.length_; 1.27 + } 1.28 + ptr_ = x.ptr_ + pos; 1.29 + length_ = x.length_ - pos; 1.30 +} 1.31 + 1.32 +StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) { 1.33 + if (pos < 0) { 1.34 + pos = 0; 1.35 + } else if (pos > x.length_) { 1.36 + pos = x.length_; 1.37 + } 1.38 + if (len < 0) { 1.39 + len = 0; 1.40 + } else if (len > x.length_ - pos) { 1.41 + len = x.length_ - pos; 1.42 + } 1.43 + ptr_ = x.ptr_ + pos; 1.44 + length_ = len; 1.45 +} 1.46 + 1.47 +void StringPiece::set(const char* str) { 1.48 + ptr_ = str; 1.49 + if (str != NULL) 1.50 + length_ = static_cast<int32_t>(uprv_strlen(str)); 1.51 + else 1.52 + length_ = 0; 1.53 +} 1.54 + 1.55 +U_EXPORT UBool U_EXPORT2 1.56 +operator==(const StringPiece& x, const StringPiece& y) { 1.57 + int32_t len = x.size(); 1.58 + if (len != y.size()) { 1.59 + return false; 1.60 + } 1.61 + if (len == 0) { 1.62 + return true; 1.63 + } 1.64 + const char* p = x.data(); 1.65 + const char* p2 = y.data(); 1.66 + // Test last byte in case strings share large common prefix 1.67 + --len; 1.68 + if (p[len] != p2[len]) return false; 1.69 + // At this point we can, but don't have to, ignore the last byte. 1.70 + return uprv_memcmp(p, p2, len) == 0; 1.71 +} 1.72 + 1.73 + 1.74 +const int32_t StringPiece::npos = 0x7fffffff; 1.75 + 1.76 +U_NAMESPACE_END