MongoDB C++ Driver  legacy-1.1.2
stringutils.h
1 // stringutils.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 <ctype.h>
21 
22 #include <string>
23 #include <vector>
24 
25 #include <boost/scoped_array.hpp>
26 
27 #include "mongo/base/string_data.h"
28 
29 namespace mongo {
30 
31 // see also mongoutils/str.h - perhaps move these there?
32 // see also text.h
33 
34 void splitStringDelim(const std::string& str, std::vector<std::string>* res, char delim);
35 
36 void joinStringDelim(const std::vector<std::string>& strs, std::string* res, char delim);
37 
38 inline std::string tolowerString(StringData input) {
39  std::string::size_type sz = input.size();
40 
41  boost::scoped_array<char> line(new char[sz + 1]);
42  char* copy = line.get();
43 
44  for (std::string::size_type i = 0; i < sz; i++) {
45  char c = input[i];
46  copy[i] = (char)tolower((int)c);
47  }
48  copy[sz] = 0;
49  return copy;
50 }
51 
53 class LexNumCmp {
54 public:
56  LexNumCmp(bool lexOnly);
63  static int cmp(const StringData& s1, const StringData& s2, bool lexOnly);
64  int cmp(const StringData& s1, const StringData& s2) const;
65  bool operator()(const StringData& s1, const StringData& s2) const;
66 
67 private:
68  bool _lexOnly;
69 };
70 
71 // TODO: Sane-ify core string functionality
72 // For now, this needs to be near the LexNumCmp or else
73 int versionCmp(const StringData rhs, const StringData lhs);
74 
75 } // namespace mongo
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
Functor for combining lexical and numeric comparisons.
Definition: stringutils.h:53
LexNumCmp(bool lexOnly)
static int cmp(const StringData &s1, const StringData &s2, bool lexOnly)
Non numeric characters are compared lexicographically; numeric substrings are compared numerically; d...