drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
util/string.h
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * * Neither the name of Patrick Galbraith nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 Some sections of this code came from the Boost examples.
34*/
35
36#pragma once
37
38#include <utility>
39#include <string>
40#include <vector>
41
42#include <boost/algorithm/string/predicate.hpp>
43#include <boost/functional/hash.hpp>
44#include <memory>
45
46#define drizzle_literal_parameter(X) (X), size_t((sizeof(X) - 1))
47
48namespace drizzled {
49namespace util {
50
52{
53 bool operator()(std::string const& x, std::string const& y) const
54 {
55 return boost::algorithm::iequals(x, y);
56 }
57};
58
60{
61 std::size_t operator()(std::string const& x) const
62 {
63 std::size_t seed = 0;
64 for (std::string::const_reference it : x)
65 boost::hash_combine(seed, std::toupper(it));
66 return seed;
67 }
68};
69
71{
72 std::size_t operator()(std::vector<char> const& x) const
73 {
74 std::size_t seed = 0;
75 for (std::vector<char>::const_reference it : x)
76 boost::hash_combine(seed, it);
77 return seed;
78 }
79};
80
81class String
82{
83public:
84
85 String()
86 {
87 _bytes.resize(1);
88 }
89
90 const char* data() const
91 {
92 return &_bytes[0];
93 }
94
95 char* data()
96 {
97 return &_bytes[0];
98 }
99
100 void assign(const size_t repeat, const char arg)
101 {
102 _bytes.resize(repeat + 1); // inserts \0 at end
103 memset(&_bytes[0], arg, repeat);
104 _bytes[repeat]= 0;
105 }
106
107 void assign(const char *arg, const size_t arg_size)
108 {
109 _bytes.resize(arg_size + 1); // +1 for \0
110 memcpy(&_bytes[0], arg, arg_size);
111 _bytes[arg_size]= 0;
112 }
113
114 void append(const char *arg, const size_t arg_size)
115 {
116 if (not arg or not arg_size)
117 return;
118
119 size_t original_size= size();
120 if (original_size)
121 {
122 _bytes.resize(original_size + arg_size + 1); // inserts NULL since string will already have a NULL
123 memcpy(&_bytes[original_size], arg, arg_size);
124 _bytes[original_size + arg_size]= 0;
125 }
126 else
127 {
128 assign(arg, arg_size);
129 }
130 }
131
132 const char& operator[] (size_t arg) const
133 {
134 return _bytes[arg];
135 }
136
137 char& operator[] (size_t arg)
138 {
139 return _bytes[arg];
140 }
141
142 void clear()
143 {
144 _bytes.resize(1);
145 _bytes[0]= 0;
146 }
147
148 size_t size() const
149 {
150 return _bytes.size() -1;
151 }
152
153private:
154 std::vector<char> _bytes;
155};
156
157} /* namespace util */
158} /* namespace drizzled */
159
TODO: Rename this file - func.h is stupid.