OLD | NEW |
1 // Copyright 2004 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2004 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #include "re2/stringpiece.h" | 5 #include "re2/stringpiece.h" |
6 #include "util/util.h" | 6 #include "util/util.h" |
7 | 7 |
8 using re2::StringPiece; | 8 using re2::StringPiece; |
9 | 9 |
10 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { | 10 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { |
(...skipping 15 matching lines...) Expand all Loading... |
26 if (*p != *p2) | 26 if (*p != *p2) |
27 return false; | 27 return false; |
28 } | 28 } |
29 return true; | 29 return true; |
30 } | 30 } |
31 | 31 |
32 void StringPiece::CopyToString(string* target) const { | 32 void StringPiece::CopyToString(string* target) const { |
33 target->assign(ptr_, length_); | 33 target->assign(ptr_, length_); |
34 } | 34 } |
35 | 35 |
36 int StringPiece::copy(char* buf, size_type n, size_type pos) const { | 36 void StringPiece::AppendToString(string* target) const { |
37 int ret = min(length_ - pos, n); | 37 target->append(ptr_, length_); |
| 38 } |
| 39 |
| 40 StringPiece::size_type StringPiece::copy(char* buf, size_type n, |
| 41 size_type pos) const { |
| 42 size_type ret = min(length_ - pos, n); |
38 memcpy(buf, ptr_ + pos, ret); | 43 memcpy(buf, ptr_ + pos, ret); |
39 return ret; | 44 return ret; |
40 } | 45 } |
41 | 46 |
42 int StringPiece::find(const StringPiece& s, size_type pos) const { | 47 bool StringPiece::contains(StringPiece s) const { |
| 48 return find(s, 0) != npos; |
| 49 } |
| 50 |
| 51 StringPiece::size_type StringPiece::find(const StringPiece& s, |
| 52 size_type pos) const { |
43 if (length_ < 0 || pos > static_cast<size_type>(length_)) | 53 if (length_ < 0 || pos > static_cast<size_type>(length_)) |
44 return npos; | 54 return npos; |
45 | 55 |
46 const char* result = std::search(ptr_ + pos, ptr_ + length_, | 56 const char* result = std::search(ptr_ + pos, ptr_ + length_, |
47 s.ptr_, s.ptr_ + s.length_); | 57 s.ptr_, s.ptr_ + s.length_); |
48 const size_type xpos = result - ptr_; | 58 const size_type xpos = result - ptr_; |
49 return xpos + s.length_ <= length_ ? xpos : npos; | 59 return xpos + s.length_ <= static_cast<size_type>(length_) ? xpos : npos; |
50 } | 60 } |
51 | 61 |
52 int StringPiece::find(char c, size_type pos) const { | 62 StringPiece::size_type StringPiece::find(char c, size_type pos) const { |
53 if (length_ <= 0 || pos >= static_cast<size_type>(length_)) { | 63 if (length_ <= 0 || pos >= static_cast<size_type>(length_)) { |
54 return npos; | 64 return npos; |
55 } | 65 } |
56 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); | 66 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); |
57 return result != ptr_ + length_ ? result - ptr_ : npos; | 67 return result != ptr_ + length_ ? result - ptr_ : npos; |
58 } | 68 } |
59 | 69 |
60 int StringPiece::rfind(const StringPiece& s, size_type pos) const { | 70 StringPiece::size_type StringPiece::rfind(const StringPiece& s, |
| 71 size_type pos) const { |
61 if (length_ < s.length_) return npos; | 72 if (length_ < s.length_) return npos; |
62 const size_t ulen = length_; | 73 const size_type ulen = length_; |
63 if (s.length_ == 0) return min(ulen, pos); | 74 if (s.length_ == 0) return min(ulen, pos); |
64 | 75 |
65 const char* last = ptr_ + min(ulen - s.length_, pos) + s.length_; | 76 const char* last = ptr_ + min(ulen - s.length_, pos) + s.length_; |
66 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); | 77 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); |
67 return result != last ? result - ptr_ : npos; | 78 return result != last ? result - ptr_ : npos; |
68 } | 79 } |
69 | 80 |
70 int StringPiece::rfind(char c, size_type pos) const { | 81 StringPiece::size_type StringPiece::rfind(char c, size_type pos) const { |
71 if (length_ <= 0) return npos; | 82 if (length_ <= 0) return npos; |
72 for (int i = min(pos, static_cast<size_type>(length_ - 1)); | 83 for (int i = static_cast<int>(min(pos, static_cast<size_type>(length_ - 1))); |
73 i >= 0; --i) { | 84 i >= 0; --i) { |
74 if (ptr_[i] == c) { | 85 if (ptr_[i] == c) { |
75 return i; | 86 return i; |
76 } | 87 } |
77 } | 88 } |
78 return npos; | 89 return npos; |
79 } | 90 } |
80 | 91 |
81 StringPiece StringPiece::substr(size_type pos, size_type n) const { | 92 StringPiece StringPiece::substr(size_type pos, size_type n) const { |
82 if (pos > length_) pos = length_; | 93 if (pos > static_cast<size_type>(length_)) pos = static_cast<size_type>(length
_); |
83 if (n > length_ - pos) n = length_ - pos; | 94 if (n > length_ - pos) n = length_ - pos; |
84 return StringPiece(ptr_ + pos, n); | 95 return StringPiece(ptr_ + pos, static_cast<int>(n)); |
85 } | 96 } |
86 | 97 |
87 const StringPiece::size_type StringPiece::npos = size_type(-1); | 98 const StringPiece::size_type StringPiece::npos = size_type(-1); |
OLD | NEW |