blob: 64326e1652af44f226f3dfb8ce9a16ceff18de59 [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
initial.commitd7cae122008-07-26 21:49:3822#include <string>
23
24#include "base/basictypes.h"
25
[email protected]8a16266e2009-09-10 21:08:3926namespace base {
27
initial.commitd7cae122008-07-26 21:49:3828class StringPiece {
29 public:
[email protected]a502bbe72011-01-07 18:06:4530 // standard STL container boilerplate
initial.commitd7cae122008-07-26 21:49:3831 typedef size_t size_type;
[email protected]a502bbe72011-01-07 18:06:4532 typedef char value_type;
33 typedef const char* pointer;
34 typedef const char& reference;
35 typedef const char& const_reference;
36 typedef ptrdiff_t difference_type;
37 typedef const char* const_iterator;
38 typedef const char* iterator;
39 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
40 typedef std::reverse_iterator<iterator> reverse_iterator;
initial.commitd7cae122008-07-26 21:49:3841
[email protected]a502bbe72011-01-07 18:06:4542 static const size_type npos;
initial.commitd7cae122008-07-26 21:49:3843
44 public:
45 // We provide non-explicit singleton constructors so users can pass
46 // in a "const char*" or a "string" wherever a "StringPiece" is
47 // expected.
48 StringPiece() : ptr_(NULL), length_(0) { }
49 StringPiece(const char* str)
50 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { }
51 StringPiece(const std::string& str)
52 : ptr_(str.data()), length_(str.size()) { }
53 StringPiece(const char* offset, size_type len)
54 : ptr_(offset), length_(len) { }
55
56 // data() may return a pointer to a buffer with embedded NULs, and the
57 // returned buffer may or may not be null terminated. Therefore it is
58 // typically a mistake to pass data() to a routine that expects a NUL
59 // terminated string.
60 const char* data() const { return ptr_; }
61 size_type size() const { return length_; }
62 size_type length() const { return length_; }
63 bool empty() const { return length_ == 0; }
64
[email protected]2fdc86a2010-01-26 23:08:0265 void clear() {
66 ptr_ = NULL;
67 length_ = 0;
68 }
69 void set(const char* data, size_type len) {
70 ptr_ = data;
71 length_ = len;
72 }
initial.commitd7cae122008-07-26 21:49:3873 void set(const char* str) {
74 ptr_ = str;
75 length_ = str ? strlen(str) : 0;
76 }
77 void set(const void* data, size_type len) {
78 ptr_ = reinterpret_cast<const char*>(data);
79 length_ = len;
80 }
81
82 char operator[](size_type i) const { return ptr_[i]; }
83
84 void remove_prefix(size_type n) {
85 ptr_ += n;
86 length_ -= n;
87 }
88
89 void remove_suffix(size_type n) {
90 length_ -= n;
91 }
92
93 int compare(const StringPiece& x) const {
[email protected]39a749c2011-01-28 02:40:4694 int r = wordmemcmp(
95 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
initial.commitd7cae122008-07-26 21:49:3896 if (r == 0) {
97 if (length_ < x.length_) r = -1;
98 else if (length_ > x.length_) r = +1;
99 }
100 return r;
101 }
102
103 std::string as_string() const {
104 // std::string doesn't like to take a NULL pointer even with a 0 size.
105 return std::string(!empty() ? data() : "", size());
106 }
107
108 void CopyToString(std::string* target) const;
109 void AppendToString(std::string* target) const;
110
111 // Does "this" start with "x"
112 bool starts_with(const StringPiece& x) const {
113 return ((length_ >= x.length_) &&
114 (wordmemcmp(ptr_, x.ptr_, x.length_) == 0));
115 }
116
117 // Does "this" end with "x"
118 bool ends_with(const StringPiece& x) const {
119 return ((length_ >= x.length_) &&
120 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
121 }
122
initial.commitd7cae122008-07-26 21:49:38123 iterator begin() const { return ptr_; }
124 iterator end() const { return ptr_ + length_; }
125 const_reverse_iterator rbegin() const {
126 return const_reverse_iterator(ptr_ + length_);
127 }
128 const_reverse_iterator rend() const {
129 return const_reverse_iterator(ptr_);
130 }
131
132 size_type max_size() const { return length_; }
133 size_type capacity() const { return length_; }
134
135 size_type copy(char* buf, size_type n, size_type pos = 0) const;
136
137 size_type find(const StringPiece& s, size_type pos = 0) const;
138 size_type find(char c, size_type pos = 0) const;
139 size_type rfind(const StringPiece& s, size_type pos = npos) const;
140 size_type rfind(char c, size_type pos = npos) const;
141
142 size_type find_first_of(const StringPiece& s, size_type pos = 0) const;
143 size_type find_first_of(char c, size_type pos = 0) const {
144 return find(c, pos);
145 }
146 size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const;
147 size_type find_first_not_of(char c, size_type pos = 0) const;
148 size_type find_last_of(const StringPiece& s, size_type pos = npos) const;
149 size_type find_last_of(char c, size_type pos = npos) const {
150 return rfind(c, pos);
151 }
152 size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const;
153 size_type find_last_not_of(char c, size_type pos = npos) const;
154
155 StringPiece substr(size_type pos, size_type n = npos) const;
156
157 static int wordmemcmp(const char* p, const char* p2, size_type N) {
158 return memcmp(p, p2, N);
159 }
[email protected]a502bbe72011-01-07 18:06:45160
161 private:
162 const char* ptr_;
163 size_type length_;
initial.commitd7cae122008-07-26 21:49:38164};
165
166bool operator==(const StringPiece& x, const StringPiece& y);
167
168inline bool operator!=(const StringPiece& x, const StringPiece& y) {
169 return !(x == y);
170}
171
172inline bool operator<(const StringPiece& x, const StringPiece& y) {
[email protected]39a749c2011-01-28 02:40:46173 const int r = StringPiece::wordmemcmp(
174 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
initial.commitd7cae122008-07-26 21:49:38175 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
176}
177
178inline bool operator>(const StringPiece& x, const StringPiece& y) {
179 return y < x;
180}
181
182inline bool operator<=(const StringPiece& x, const StringPiece& y) {
183 return !(x > y);
184}
185
186inline bool operator>=(const StringPiece& x, const StringPiece& y) {
187 return !(x < y);
188}
189
[email protected]8a16266e2009-09-10 21:08:39190} // namespace base
191
[email protected]baa010b2008-11-08 23:16:31192#endif // BASE_STRING_PIECE_H_