diff --git a/icu4c/source/common/stringpiece.cpp b/icu4c/source/common/stringpiece.cpp index a7cfc090da0..851a49a53b6 100644 --- a/icu4c/source/common/stringpiece.cpp +++ b/icu4c/source/common/stringpiece.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2009, International Business Machines +// Copyright (C) 2009-2010, International Business Machines // Corporation and others. All Rights Reserved. // // Copyright 2004 and onwards Google Inc. @@ -9,6 +9,7 @@ #include "unicode/utypes.h" #include "unicode/stringpiece.h" #include "cstring.h" +#include "cmemory.h" U_NAMESPACE_BEGIN @@ -40,6 +41,29 @@ StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) { length_ = len; } +void StringPiece::set(const char* str) { + ptr_ = str; + if (str != NULL) + length_ = static_cast(uprv_strlen(str)); + else + length_ = 0; +} + +U_EXPORT UBool U_EXPORT2 +operator==(const StringPiece& x, const StringPiece& y) { + int32_t len = x.size(); + if (len != y.size()) { + return false; + } + const char* p = x.data(); + const char* p2 = y.data(); + // Test last byte in case strings share large common prefix + if ((len > 0) && (p[len-1] != p2[len-1])) return false; + // At this point we can, but don't have to, ignore the last byte. + return uprv_memcmp(p, p2, len-1) == 0; +} + + /* Microsft Visual Studios <= 8.0 complains about redefinition of this * static const class variable. However, the C++ standard states that this * definition is correct. Perhaps there is a bug in the Microsoft compiler. diff --git a/icu4c/source/common/unicode/stringpiece.h b/icu4c/source/common/unicode/stringpiece.h index 5b8d9906bb5..79d5a050ec7 100644 --- a/icu4c/source/common/unicode/stringpiece.h +++ b/icu4c/source/common/unicode/stringpiece.h @@ -134,6 +134,21 @@ class U_COMMON_API StringPiece : public UMemory { */ void clear() { ptr_ = NULL; length_ = 0; } + /** + * Reset the stringpiece to refer to new data. + * @param data pointer the new string data. Need not be nul terminated. + * @param len the length of the new data + * @internal + */ + void set(const char* data, int32_t len) { ptr_ = data; length_ = len; } + + /** + * Reset the stringpiece to refer to new data. + * @param str a pointer to a NUL-terminated string. + * @internal + */ + void set(const char* str); + /** * Removes the first n string units. * @param n prefix length, must be non-negative and <=length() @@ -183,6 +198,27 @@ class U_COMMON_API StringPiece : public UMemory { } }; +/** + * Global operator == for StringPiece + * @param x The first StringPiece to compare. + * @param y The second StringPiece to compare. + * @return TRUE if the string data is equal + * @internal + */ +U_EXPORT UBool U_EXPORT2 +operator==(const StringPiece& x, const StringPiece& y); + +/** + * Global operator != for StringPiece + * @param x The first StringPiece to compare. + * @param y The second StringPiece to compare. + * @return TRUE if the string data is not equal + * @internal + */ +inline UBool operator!=(const StringPiece& x, const StringPiece& y) { + return !(x == y); +} + U_NAMESPACE_END #endif // __STRINGPIECE_H__