1 // Copyright (C) 2009, International Business Machines 2 // Corporation and others. All Rights Reserved. 3 // 4 // Copyright 2004 and onwards Google Inc. 5 // 6 // Author: wilsonh (at) google.com (Wilson Hsieh) 7 // 8 9 #include "unicode/utypes.h" 10 #include "unicode/stringpiece.h" 11 #include "cstring.h" 12 13 U_NAMESPACE_BEGIN 14 15 StringPiece::StringPiece(const char* str) 16 : ptr_(str), length_((str == NULL) ? 0 : static_cast<int32_t>(uprv_strlen(str))) { } 17 18 StringPiece::StringPiece(const StringPiece& x, int32_t pos) { 19 if (pos < 0) { 20 pos = 0; 21 } else if (pos > x.length_) { 22 pos = x.length_; 23 } 24 ptr_ = x.ptr_ + pos; 25 length_ = x.length_ - pos; 26 } 27 28 StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) { 29 if (pos < 0) { 30 pos = 0; 31 } else if (pos > x.length_) { 32 pos = x.length_; 33 } 34 if (len < 0) { 35 len = 0; 36 } else if (len > x.length_ - pos) { 37 len = x.length_ - pos; 38 } 39 ptr_ = x.ptr_ + pos; 40 length_ = len; 41 } 42 43 /* Microsft Visual Studios <= 8.0 complains about redefinition of this 44 * static const class variable. However, the C++ standard states that this 45 * definition is correct. Perhaps there is a bug in the Microsoft compiler. 46 * This is not an issue on any other compilers (that we know of) including 47 * Visual Studios 9.0. 48 * Cygwin with MSVC 9.0 also complains here about redefinition. 49 */ 50 #if (!defined(_MSC_VER) || (_MSC_VER >= 1500)) && !defined(CYGWINMSVC) 51 const int32_t StringPiece::npos; 52 #endif 53 54 U_NAMESPACE_END 55