]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/stringpiece.cpp
1 // Copyright (C) 2009-2013, International Business Machines
2 // Corporation and others. All Rights Reserved.
4 // Copyright 2004 and onwards Google Inc.
6 // Author: wilsonh@google.com (Wilson Hsieh)
9 #include "unicode/utypes.h"
10 #include "unicode/stringpiece.h"
16 StringPiece::StringPiece(const char* str
)
17 : ptr_(str
), length_((str
== NULL
) ? 0 : static_cast<int32_t>(uprv_strlen(str
))) { }
19 StringPiece::StringPiece(const StringPiece
& x
, int32_t pos
) {
22 } else if (pos
> x
.length_
) {
26 length_
= x
.length_
- pos
;
29 StringPiece::StringPiece(const StringPiece
& x
, int32_t pos
, int32_t len
) {
32 } else if (pos
> x
.length_
) {
37 } else if (len
> x
.length_
- pos
) {
38 len
= x
.length_
- pos
;
44 void StringPiece::set(const char* str
) {
47 length_
= static_cast<int32_t>(uprv_strlen(str
));
52 U_EXPORT UBool U_EXPORT2
53 operator==(const StringPiece
& x
, const StringPiece
& y
) {
54 int32_t len
= x
.size();
55 if (len
!= y
.size()) {
61 const char* p
= x
.data();
62 const char* p2
= y
.data();
63 // Test last byte in case strings share large common prefix
65 if (p
[len
] != p2
[len
]) return false;
66 // At this point we can, but don't have to, ignore the last byte.
67 return uprv_memcmp(p
, p2
, len
) == 0;
71 const int32_t StringPiece::npos
= 0x7fffffff;