1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 // Copied from strings/stringpiece.cc with modifications
9 #include "base/string_piece.h"
13 typedef StringPiece::size_type size_type
;
15 bool operator==(const StringPiece
& x
, const StringPiece
& y
) {
16 if (x
.size() != y
.size())
19 return StringPiece::wordmemcmp(x
.data(), y
.data(), x
.size()) == 0;
22 void StringPiece::CopyToString(std::string
* target
) const {
23 target
->assign(!empty() ? data() : "", size());
26 void StringPiece::AppendToString(std::string
* target
) const {
28 target
->append(data(), size());
31 size_type
StringPiece::copy(char* buf
, size_type n
, size_type pos
) const {
32 size_type ret
= std::min(length_
- pos
, n
);
33 memcpy(buf
, ptr_
+ pos
, ret
);
37 size_type
StringPiece::find(const StringPiece
& s
, size_type pos
) const {
41 const char* result
= std::search(ptr_
+ pos
, ptr_
+ length_
,
42 s
.ptr_
, s
.ptr_
+ s
.length_
);
43 const size_type xpos
= result
- ptr_
;
44 return xpos
+ s
.length_
<= length_
? xpos
: npos
;
47 size_type
StringPiece::find(char c
, size_type pos
) const {
51 const char* result
= std::find(ptr_
+ pos
, ptr_
+ length_
, c
);
52 return result
!= ptr_
+ length_
? static_cast<size_t>(result
- ptr_
) : npos
;
55 size_type
StringPiece::rfind(const StringPiece
& s
, size_type pos
) const {
56 if (length_
< s
.length_
)
60 return std::min(length_
, pos
);
62 const char* last
= ptr_
+ std::min(length_
- s
.length_
, pos
) + s
.length_
;
63 const char* result
= std::find_end(ptr_
, last
, s
.ptr_
, s
.ptr_
+ s
.length_
);
64 return result
!= last
? static_cast<size_t>(result
- ptr_
) : npos
;
67 size_type
StringPiece::rfind(char c
, size_type pos
) const {
71 for (size_type i
= std::min(pos
, length_
- 1); ; --i
) {
80 // For each character in characters_wanted, sets the index corresponding
81 // to the ASCII code of that character to 1 in table. This is used by
82 // the find_.*_of methods below to tell whether or not a character is in
83 // the lookup table in constant time.
84 // The argument `table' must be an array that is large enough to hold all
85 // the possible values of an unsigned char. Thus it should be be declared
87 // bool table[UCHAR_MAX + 1]
88 static inline void BuildLookupTable(const StringPiece
& characters_wanted
,
90 const size_type length
= characters_wanted
.length();
91 const char* const data
= characters_wanted
.data();
92 for (size_type i
= 0; i
< length
; ++i
) {
93 table
[static_cast<unsigned char>(data
[i
])] = true;
97 size_type
StringPiece::find_first_of(const StringPiece
& s
,
98 size_type pos
) const {
99 if (length_
== 0 || s
.length_
== 0)
102 // Avoid the cost of BuildLookupTable() for a single-character search.
104 return find_first_of(s
.ptr_
[0], pos
);
106 bool lookup
[UCHAR_MAX
+ 1] = { false };
107 BuildLookupTable(s
, lookup
);
108 for (size_type i
= pos
; i
< length_
; ++i
) {
109 if (lookup
[static_cast<unsigned char>(ptr_
[i
])]) {
116 size_type
StringPiece::find_first_not_of(const StringPiece
& s
,
117 size_type pos
) const {
124 // Avoid the cost of BuildLookupTable() for a single-character search.
126 return find_first_not_of(s
.ptr_
[0], pos
);
128 bool lookup
[UCHAR_MAX
+ 1] = { false };
129 BuildLookupTable(s
, lookup
);
130 for (size_type i
= pos
; i
< length_
; ++i
) {
131 if (!lookup
[static_cast<unsigned char>(ptr_
[i
])]) {
138 size_type
StringPiece::find_first_not_of(char c
, size_type pos
) const {
142 for (; pos
< length_
; ++pos
) {
143 if (ptr_
[pos
] != c
) {
150 size_type
StringPiece::find_last_of(const StringPiece
& s
, size_type pos
) const {
151 if (length_
== 0 || s
.length_
== 0)
154 // Avoid the cost of BuildLookupTable() for a single-character search.
156 return find_last_of(s
.ptr_
[0], pos
);
158 bool lookup
[UCHAR_MAX
+ 1] = { false };
159 BuildLookupTable(s
, lookup
);
160 for (size_type i
= std::min(pos
, length_
- 1); ; --i
) {
161 if (lookup
[static_cast<unsigned char>(ptr_
[i
])])
169 size_type
StringPiece::find_last_not_of(const StringPiece
& s
,
170 size_type pos
) const {
174 size_type i
= std::min(pos
, length_
- 1);
178 // Avoid the cost of BuildLookupTable() for a single-character search.
180 return find_last_not_of(s
.ptr_
[0], pos
);
182 bool lookup
[UCHAR_MAX
+ 1] = { false };
183 BuildLookupTable(s
, lookup
);
185 if (!lookup
[static_cast<unsigned char>(ptr_
[i
])])
193 size_type
StringPiece::find_last_not_of(char c
, size_type pos
) const {
197 for (size_type i
= std::min(pos
, length_
- 1); ; --i
) {
206 StringPiece
StringPiece::substr(size_type pos
, size_type n
) const {
207 if (pos
> length_
) pos
= length_
;
208 if (n
> length_
- pos
) n
= length_
- pos
;
209 return StringPiece(ptr_
+ pos
, n
);
212 const StringPiece::size_type
StringPiece::npos
= size_type(-1);