MongoDB C++ Driver  legacy-1.1.2
string_data-inl.h
1 // string_data_inline.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 // this should never be included directly
19 
20 #include <stdexcept>
21 
22 namespace mongo {
23 
24 inline int StringData::compare(const StringData& other) const {
25  int res = memcmp(_data, other._data, std::min(_size, other._size));
26  if (res != 0) {
27  return res > 0 ? 1 : -1;
28  } else if (_size == other._size) {
29  return 0;
30  } else {
31  return _size > other._size ? 1 : -1;
32  }
33 }
34 
35 inline bool StringData::equalCaseInsensitive(const StringData& other) const {
36  if (other.size() != size())
37  return false;
38 
39  for (size_t x = 0; x < size(); x++) {
40  char a = _data[x];
41  char b = other._data[x];
42  if (a == b)
43  continue;
44  if (tolower(a) == tolower(b))
45  continue;
46  return false;
47  }
48 
49  return true;
50 }
51 
52 inline void StringData::copyTo(char* dest, bool includeEndingNull) const {
53  memcpy(dest, _data, size());
54  if (includeEndingNull)
55  dest[size()] = 0;
56 }
57 
58 inline size_t StringData::find(char c, size_t fromPos) const {
59  if (fromPos >= size())
60  return std::string::npos;
61 
62  const void* x = memchr(_data + fromPos, c, _size - fromPos);
63  if (x == 0)
64  return std::string::npos;
65  return static_cast<size_t>(static_cast<const char*>(x) - _data);
66 }
67 
68 inline size_t StringData::find(const StringData& needle) const {
69  size_t mx = size();
70  size_t needleSize = needle.size();
71 
72  if (needleSize == 0)
73  return 0;
74  else if (needleSize > mx)
75  return std::string::npos;
76 
77  mx -= needleSize;
78 
79  for (size_t i = 0; i <= mx; i++) {
80  if (memcmp(_data + i, needle._data, needleSize) == 0)
81  return i;
82  }
83  return std::string::npos;
84 }
85 
86 inline size_t StringData::rfind(char c, size_t fromPos) const {
87  const size_t sz = size();
88  if (fromPos > sz)
89  fromPos = sz;
90 
91  for (const char* cur = _data + fromPos; cur > _data; --cur) {
92  if (*(cur - 1) == c)
93  return (cur - _data) - 1;
94  }
95  return std::string::npos;
96 }
97 
98 inline StringData StringData::substr(size_t pos, size_t n) const {
99  if (pos > size())
100  throw std::out_of_range("out of range");
101 
102  // truncate to end of string
103  if (n > size() - pos)
104  n = size() - pos;
105 
106  return StringData(_data + pos, n);
107 }
108 
109 inline bool StringData::startsWith(const StringData& prefix) const {
110  // TODO: Investigate an optimized implementation.
111  return substr(0, prefix.size()) == prefix;
112 }
113 
114 inline bool StringData::endsWith(const StringData& suffix) const {
115  // TODO: Investigate an optimized implementation.
116  const size_t thisSize = size();
117  const size_t suffixSize = suffix.size();
118  if (suffixSize > thisSize)
119  return false;
120  return substr(thisSize - suffixSize) == suffix;
121 }
122 
123 } // namespace mongo
bool equalCaseInsensitive(const StringData &other) const
note: this uses tolower, and therefore does not handle come languages correctly.
Definition: string_data-inl.h:35
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
bool endsWith(const StringData &suffix) const
Returns true if 'suffix' is a substring of this instance, anchored at the end.
Definition: string_data-inl.h:114
int compare(const StringData &other) const
Returns -1, 0, or 1 if 'this' is less, equal, or greater than 'other' in lexicographical order...
Definition: string_data-inl.h:24
bool startsWith(const StringData &prefix) const
Returns true if 'prefix' is a substring of this instance, anchored at position 0. ...
Definition: string_data-inl.h:109