OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // Copied from strings/stringpiece.cc with modifications | 4 // Copied from strings/stringpiece.cc with modifications |
5 | 5 |
6 #include <algorithm> | 6 #include <algorithm> |
7 #include <ostream> | 7 #include <ostream> |
8 | 8 |
9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 s.ptr_, s.ptr_ + s.length_); | 47 s.ptr_, s.ptr_ + s.length_); |
48 const size_type xpos = result - ptr_; | 48 const size_type xpos = result - ptr_; |
49 return xpos + s.length_ <= length_ ? xpos : npos; | 49 return xpos + s.length_ <= length_ ? xpos : npos; |
50 } | 50 } |
51 | 51 |
52 size_type StringPiece::find(char c, size_type pos) const { | 52 size_type StringPiece::find(char c, size_type pos) const { |
53 if (pos >= length_) | 53 if (pos >= length_) |
54 return npos; | 54 return npos; |
55 | 55 |
56 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); | 56 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); |
57 return result != ptr_ + length_ ? result - ptr_ : npos; | 57 return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos; |
58 } | 58 } |
59 | 59 |
60 size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { | 60 size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { |
61 if (length_ < s.length_) | 61 if (length_ < s.length_) |
62 return npos; | 62 return npos; |
63 | 63 |
64 if (s.empty()) | 64 if (s.empty()) |
65 return std::min(length_, pos); | 65 return std::min(length_, pos); |
66 | 66 |
67 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; | 67 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; |
68 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); | 68 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); |
69 return result != last ? result - ptr_ : npos; | 69 return result != last ? static_cast<size_t>(result - ptr_) : npos; |
70 } | 70 } |
71 | 71 |
72 size_type StringPiece::rfind(char c, size_type pos) const { | 72 size_type StringPiece::rfind(char c, size_type pos) const { |
73 if (length_ == 0) | 73 if (length_ == 0) |
74 return npos; | 74 return npos; |
75 | 75 |
76 for (size_type i = std::min(pos, length_ - 1); ; --i) { | 76 for (size_type i = std::min(pos, length_ - 1); ; --i) { |
77 if (ptr_[i] == c) | 77 if (ptr_[i] == c) |
78 return i; | 78 return i; |
79 if (i == 0) | 79 if (i == 0) |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 210 |
211 StringPiece StringPiece::substr(size_type pos, size_type n) const { | 211 StringPiece StringPiece::substr(size_type pos, size_type n) const { |
212 if (pos > length_) pos = length_; | 212 if (pos > length_) pos = length_; |
213 if (n > length_ - pos) n = length_ - pos; | 213 if (n > length_ - pos) n = length_ - pos; |
214 return StringPiece(ptr_ + pos, n); | 214 return StringPiece(ptr_ + pos, n); |
215 } | 215 } |
216 | 216 |
217 const StringPiece::size_type StringPiece::npos = size_type(-1); | 217 const StringPiece::size_type StringPiece::npos = size_type(-1); |
218 | 218 |
219 } // namespace base | 219 } // namespace base |
OLD | NEW |