~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/string.h

  • Committer: Brian Aker
  • Date: 2011-05-20 21:45:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2314.
  • Revision ID: brian@tangent.org-20110520214526-gzh4wsppevztsk7g
Introduce a simple string like class (we have byte strings that we use where it is not safe to use std::string because of binary data and silent issues with c_str()).

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include <boost/functional/hash.hpp>
45
45
#include <boost/shared_ptr.hpp>
46
46
 
 
47
#define drizzle_literal_parameter(X) (X), size_t((sizeof(X) - 1))
 
48
 
47
49
namespace drizzled {
48
50
namespace util {
49
51
 
77
79
  }
78
80
};
79
81
 
 
82
class String
 
83
{
 
84
public:
 
85
 
 
86
  String()
 
87
  {
 
88
    _bytes.resize(1);
 
89
  }
 
90
 
 
91
  const char *c_str() const
 
92
  {
 
93
    return &_bytes[0];
 
94
  }
 
95
 
 
96
  char *c_str()
 
97
  {
 
98
    return &_bytes[0];
 
99
  }
 
100
 
 
101
  void assign(const size_t repeat, const char arg)
 
102
  {
 
103
    _bytes.resize(repeat +1); // inserts NULL at end
 
104
    memset(&_bytes[0], int(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 NULL
 
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
 
80
158
} /* namespace util */
81
159
} /* namespace drizzled */
82
160