blob: 80c6cabf5edcbff5eb2941cd602c02317f3336a1 [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384// Copied from strings/stringpiece.h with modifications
5//
6// A string-like object that points to a sized piece of memory.
7//
8// Functions or methods may use const StringPiece& parameters to accept either
9// a "const char*" or a "string" value that will be implicitly converted to
10// a StringPiece. The implicit conversion means that it is often appropriate
11// to include this .h file in other files rather than forward-declaring
12// StringPiece as would be appropriate for most other Google classes.
13//
14// Systematic usage of StringPiece is encouraged as it will reduce unnecessary
15// conversions from "const char*" to "string" and back again.
16//
17
[email protected]baa010b2008-11-08 23:16:3118#ifndef BASE_STRING_PIECE_H_
19#define BASE_STRING_PIECE_H_
[email protected]32b76ef2010-07-26 23:08:2420#pragma once
initial.commitd7cae122008-07-26 21:49:3821
22#include <algorithm>
23#include <iosfwd>
24#include <string>
25
26#include "base/basictypes.h"
27
[email protected]8a16266e2009-09-10 21:08:3928namespace base {
29
initial.commitd7cae122008-07-26 21:49:3830class StringPiece {
31 public:
[email protected]a502bbe72011-01-07 18:06:4532 // standard STL container boilerplate
initial.commitd7cae122008-07-26 21:49:3833 typedef size_t size_type;
[email protected]a502bbe72011-01-07 18:06:4534 typedef char value_type;
35 typedef const char* pointer;
36 typedef const char& reference;
37 typedef const char& const_reference;
38 typedef ptrdiff_t difference_type;
39 typedef const char* const_iterator;
40 typedef const char* iterator;
41 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
42 typedef std::reverse_iterator<iterator> reverse_iterator;
initial.commitd7cae122008-07-26 21:49:3843
[email protected]a502bbe72011-01-07 18:06:4544 static const size_type npos;
initial.commitd7cae122008-07-26 21:49:3845
46 public:
47 // We provide non-explicit singleton constructors so users can pass
48 // in a "const char*" or a "string" wherever a "StringPiece" is
49 // expected.
50 StringPiece() : ptr_(NULL), length_(0) { }
51 StringPiece(const char* str)
52 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { }
53 StringPiece(const std::string& str)
54 : ptr_(str.data()), length_(str.size()) { }
55 StringPiece(const char* offset, size_type len)
56 : ptr_(offset), length_(len) { }
57
58 // data() may return a pointer to a buffer with embedded NULs, and the
59 // returned buffer may or may not be null terminated. Therefore it is
60 // typically a mistake to pass data() to a routine that expects a NUL
61 // terminated string.
62 const char* data() const { return ptr_; }
63 size_type size() const { return length_; }
64 size_type length() const { return length_; }
65 bool empty() const { return length_ == 0; }
66
[email protected]2fdc86a2010-01-26 23:08:0267 void clear() {
68 ptr_ = NULL;
69 length_ = 0;
70 }
71 void set(const char* data, size_type len) {
72 ptr_ = data;
73 length_ = len;
74 }
initial.commitd7cae122008-07-26 21:49:3875 void set(const char* str) {
76 ptr_ = str;
77 length_ = str ? strlen(str) : 0;
78 }
79 void set(const void* data, size_type len) {
80 ptr_ = reinterpret_cast<const char*>(data);
81 length_ = len;
82 }
83
84 char operator[](size_type i) const { return ptr_[i]; }
85
86 void remove_prefix(size_type n) {
87 ptr_ += n;
88 length_ -= n;
89 }
90
91 void remove_suffix(size_type n) {
92 length_ -= n;
93 }
94
95 int compare(const StringPiece& x) const {
96 int r = wordmemcmp(ptr_, x.ptr_, std::min(length_, x.length_));
97 if (r == 0) {
98 if (length_ < x.length_) r = -1;
99 else if (length_ > x.length_) r = +1;
100 }
101 return r;
102 }
103
104 std::string as_string() const {
105 // std::string doesn't like to take a NULL pointer even with a 0 size.
106 return std::string(!empty() ? data() : "", size());
107 }
108
109 void CopyToString(std::string* target) const;
110 void AppendToString(std::string* target) const;
111
112 // Does "this" start with "x"
113 bool starts_with(const StringPiece& x) const {
114 return ((length_ >= x.length_) &&
115 (wordmemcmp(ptr_, x.ptr_, x.length_) == 0));
116 }
117
118 // Does "this" end with "x"
119 bool ends_with(const StringPiece& x) const {
120 return ((length_ >= x.length_) &&
121 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
122 }
123
initial.commitd7cae122008-07-26 21:49:38124 iterator begin() const { return ptr_; }
125 iterator end() const { return ptr_ + length_; }
126 const_reverse_iterator rbegin() const {
127 return const_reverse_iterator(ptr_ + length_);
128 }
129 const_reverse_iterator rend() const {
130 return const_reverse_iterator(ptr_);
131 }
132
133 size_type max_size() const { return length_; }
134 size_type capacity() const { return length_; }
135
136 size_type copy(char* buf, size_type n, size_type pos = 0) const;
137
138 size_type find(const StringPiece& s, size_type pos = 0) const;
139 size_type find(char c, size_type pos = 0) const;
140 size_type rfind(const StringPiece& s, size_type pos = npos) const;
141 size_type rfind(char c, size_type pos = npos) const;
142
143 size_type find_first_of(const StringPiece& s, size_type pos = 0) const;
144 size_type find_first_of(char c, size_type pos = 0) const {
145 return find(c, pos);
146 }
147 size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const;
148 size_type find_first_not_of(char c, size_type pos = 0) const;
149 size_type find_last_of(const StringPiece& s, size_type pos = npos) const;
150 size_type find_last_of(char c, size_type pos = npos) const {
151 return rfind(c, pos);
152 }
153 size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const;
154 size_type find_last_not_of(char c, size_type pos = npos) const;
155
156 StringPiece substr(size_type pos, size_type n = npos) const;
157
158 static int wordmemcmp(const char* p, const char* p2, size_type N) {
159 return memcmp(p, p2, N);
160 }
[email protected]a502bbe72011-01-07 18:06:45161
162 private:
163 const char* ptr_;
164 size_type length_;
initial.commitd7cae122008-07-26 21:49:38165};
166
167bool operator==(const StringPiece& x, const StringPiece& y);
168
169inline bool operator!=(const StringPiece& x, const StringPiece& y) {
170 return !(x == y);
171}
172
173inline bool operator<(const StringPiece& x, const StringPiece& y) {
174 const int r = StringPiece::wordmemcmp(x.data(), y.data(),
175 std::min(x.size(), y.size()));
176 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
177}
178
179inline bool operator>(const StringPiece& x, const StringPiece& y) {
180 return y < x;
181}
182
183inline bool operator<=(const StringPiece& x, const StringPiece& y) {
184 return !(x > y);
185}
186
187inline bool operator>=(const StringPiece& x, const StringPiece& y) {
188 return !(x < y);
189}
190
191// allow StringPiece to be logged (needed for unit testing).
192extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
193
[email protected]8a16266e2009-09-10 21:08:39194} // namespace base
195
[email protected]baa010b2008-11-08 23:16:31196#endif // BASE_STRING_PIECE_H_