~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/string.h

  • Committer: Mark Atwood
  • Date: 2011-08-12 04:08:33 UTC
  • mfrom: (2385.2.17 refactor5)
  • Revision ID: me@mark.atwood.name-20110812040833-u6j85nc6ahuc0dtz
mergeĀ lp:~olafvdspek/drizzle/refactor5

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
  Some sections of this code came from the Boost examples.
34
34
*/
35
35
 
36
 
#ifndef DRIZZLED_UTIL_STRING_H
37
 
#define DRIZZLED_UTIL_STRING_H
 
36
#pragma once
38
37
 
39
38
#include <utility>
40
39
#include <string>
41
40
#include <vector>
42
41
 
43
42
#include <boost/algorithm/string/predicate.hpp>
 
43
#include <boost/foreach.hpp>
44
44
#include <boost/functional/hash.hpp>
45
45
#include <boost/shared_ptr.hpp>
46
46
 
47
 
namespace drizzled
48
 
{
49
 
 
50
 
namespace util
51
 
{
52
 
 
53
 
 
54
 
namespace string {
55
 
typedef boost::shared_ptr<std::string> shared_ptr;
56
 
typedef boost::shared_ptr<const std::string> const_shared_ptr;
57
 
typedef std::vector< std::string > vector;
58
 
}
 
47
#define drizzle_literal_parameter(X) (X), size_t((sizeof(X) - 1))
 
48
 
 
49
namespace drizzled {
 
50
namespace util {
59
51
 
60
52
struct insensitive_equal_to : std::binary_function<std::string, std::string, bool>
61
53
{
62
54
  bool operator()(std::string const& x, std::string const& y) const
63
55
  {
64
 
    return boost::algorithm::iequals(x, y, std::locale());
 
56
    return boost::algorithm::iequals(x, y);
65
57
  }
66
58
};
67
59
 
70
62
  std::size_t operator()(std::string const& x) const
71
63
  {
72
64
    std::size_t seed = 0;
73
 
    std::locale locale;
74
 
 
75
 
    for(std::string::const_iterator it = x.begin(); it != x.end(); ++it)
76
 
    {
77
 
      boost::hash_combine(seed, std::toupper(*it, locale));
78
 
    }
79
 
 
 
65
    BOOST_FOREACH(std::string::const_reference it, x)
 
66
      boost::hash_combine(seed, std::toupper(it));
80
67
    return seed;
81
68
  }
82
69
};
86
73
  std::size_t operator()(std::vector<char> const& x) const
87
74
  {
88
75
    std::size_t seed = 0;
89
 
 
90
 
    for(std::vector<char>::const_iterator it = x.begin(); it != x.end(); ++it)
91
 
    {
92
 
      boost::hash_combine(seed, *it);
93
 
    }
94
 
 
 
76
    BOOST_FOREACH(std::vector<char>::const_reference it, x)
 
77
      boost::hash_combine(seed, it);
95
78
    return seed;
96
79
  }
97
80
};
98
81
 
 
82
class String
 
83
{
 
84
public:
 
85
 
 
86
  String()
 
87
  {
 
88
    _bytes.resize(1);
 
89
  }
 
90
 
 
91
  const char* data() const
 
92
  {
 
93
    return &_bytes[0];
 
94
  }
 
95
 
 
96
  char* data()
 
97
  {
 
98
    return &_bytes[0];
 
99
  }
 
100
 
 
101
  void assign(const size_t repeat, const char arg)
 
102
  {
 
103
    _bytes.resize(repeat + 1); // inserts \0 at end
 
104
    memset(&_bytes[0], arg, repeat);
 
105
    _bytes[repeat]= 0;
 
106
  }
 
107
 
 
108
  void assign(const char *arg, const size_t arg_size)
 
109
  {
 
110
    _bytes.resize(arg_size + 1); // +1 for \0
 
111
    memcpy(&_bytes[0], arg, arg_size);
 
112
    _bytes[arg_size]= 0;
 
113
  }
 
114
 
 
115
  void append(const char *arg, const size_t arg_size)
 
116
  {
 
117
    if (not arg or not arg_size)
 
118
      return;
 
119
 
 
120
    size_t original_size= size();
 
121
    if (original_size)
 
122
    {
 
123
      _bytes.resize(original_size + arg_size + 1); // inserts NULL since string will already have a NULL
 
124
      memcpy(&_bytes[original_size], arg, arg_size);
 
125
      _bytes[original_size + arg_size]= 0;
 
126
    }
 
127
    else
 
128
    {
 
129
      assign(arg, arg_size);
 
130
    }
 
131
  }
 
132
 
 
133
  const char& operator[] (size_t arg) const
 
134
  {
 
135
    return _bytes[arg];
 
136
  }
 
137
 
 
138
  char& operator[] (size_t arg)
 
139
  {
 
140
    return _bytes[arg];
 
141
  }
 
142
 
 
143
  void clear()
 
144
  {
 
145
    _bytes.resize(1);
 
146
    _bytes[0]= 0;
 
147
  }
 
148
 
 
149
  size_t size() const
 
150
  {
 
151
    return _bytes.size() -1;
 
152
  }
 
153
 
 
154
private:
 
155
  std::vector<char> _bytes;
 
156
};
 
157
 
99
158
} /* namespace util */
100
159
} /* namespace drizzled */
101
160
 
102
 
#endif /* DRIZZLED_UTIL_STRING_H */