MongoDB C++ Driver  legacy-1.1.2
string_data.h
1 // string_data.h
2 
3 /* Copyright 2010 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <algorithm> // for min
21 #include <cstring>
22 #include <iosfwd>
23 #include <limits>
24 #include <string>
25 
26 #include "mongo/client/export_macros.h"
27 
28 namespace mongo {
29 
43 class MONGO_CLIENT_API StringData {
44 public:
46  StringData() : _data(NULL), _size(0) {}
47 
52  StringData(const char* str) : _data(str), _size((str == NULL) ? 0 : std::strlen(str)) {}
53 
59  StringData(const char* c, size_t len) : _data(c), _size(len) {}
60 
62  StringData(const std::string& s) : _data(s.c_str()), _size(s.size()) {}
63 
68  struct LiteralTag {};
69  template <size_t N>
70  StringData(const char(&val)[N], LiteralTag)
71  : _data(&val[0]), _size(N - 1) {}
72 
77  int compare(const StringData& other) const;
78 
84  bool equalCaseInsensitive(const StringData& other) const;
85 
86  void copyTo(char* dest, bool includeEndingNull) const;
87 
88  StringData substr(size_t pos, size_t n = std::numeric_limits<size_t>::max()) const;
89 
90  //
91  // finders
92  //
93 
94  size_t find(char c, size_t fromPos = 0) const;
95  size_t find(const StringData& needle) const;
96  size_t rfind(char c, size_t fromPos = std::string::npos) const;
97 
101  bool startsWith(const StringData& prefix) const;
102 
106  bool endsWith(const StringData& suffix) const;
107 
108  //
109  // accessors
110  //
111 
117  const char* rawData() const {
118  return _data;
119  }
120 
121  size_t size() const {
122  return _size;
123  }
124  bool empty() const {
125  return size() == 0;
126  }
127  std::string toString() const {
128  return std::string(_data, size());
129  }
130  char operator[](unsigned pos) const {
131  return _data[pos];
132  }
133 
139  struct Hasher {
140  size_t operator()(const StringData& str) const;
141  };
142 
143  //
144  // iterators
145  //
146 
147  typedef const char* const_iterator;
148 
149  const_iterator begin() const {
150  return rawData();
151  }
152  const_iterator end() const {
153  return rawData() + size();
154  }
155 
156 private:
157  const char* _data; // is not guaranted to be null terminated (see "notes" above)
158  size_t _size; // 'size' does not include the null terminator
159 };
160 
161 inline bool operator==(const StringData& lhs, const StringData& rhs) {
162  return (lhs.size() == rhs.size()) && (lhs.compare(rhs) == 0);
163 }
164 
165 inline bool operator!=(const StringData& lhs, const StringData& rhs) {
166  return !(lhs == rhs);
167 }
168 
169 inline bool operator<(const StringData& lhs, const StringData& rhs) {
170  return lhs.compare(rhs) < 0;
171 }
172 
173 inline bool operator<=(const StringData& lhs, const StringData& rhs) {
174  return lhs.compare(rhs) <= 0;
175 }
176 
177 inline bool operator>(const StringData& lhs, const StringData& rhs) {
178  return lhs.compare(rhs) > 0;
179 }
180 
181 inline bool operator>=(const StringData& lhs, const StringData& rhs) {
182  return lhs.compare(rhs) >= 0;
183 }
184 
185 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC
186 operator<<(std::ostream& stream, const StringData& value);
187 
188 } // namespace mongo
189 
190 #include "mongo/base/string_data-inl.h"
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition: string_data.h:139
StringData()
Constructs an empty string data.
Definition: string_data.h:46
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
StringData(const char *str)
Constructs a StringData, for the case where the length of string is not known.
Definition: string_data.h:52
const char * rawData() const
Get the pointer to the first byte of StringData.
Definition: string_data.h:117
StringData(const std::string &s)
Constructs a StringData, for the case of a string.
Definition: string_data.h:62
StringData(const char *c, size_t len)
Constructs a StringData explicitly, for the case where the length of the string is already known...
Definition: string_data.h:59
Constructs a StringData explicitly, for the case of a literal whose size is known at compile time...
Definition: string_data.h:68