Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[m6w6/libmemcached] / libhashkit / string.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libhashkit library
4 *
5 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37
38 #include <libhashkit/common.h>
39
40 #include <cassert>
41 #include <cstring>
42
43 #define HASHKIT_BLOCK_SIZE 1024
44
45 struct hashkit_string_st {
46 char *end;
47 size_t current_size;
48 char *string;
49 };
50
51 inline static bool _string_check(hashkit_string_st *string, size_t need)
52 {
53 if (need and need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
54 {
55 size_t current_offset= (size_t) (string->end - string->string);
56
57 /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
58 size_t adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / HASHKIT_BLOCK_SIZE;
59 adjust++;
60
61 size_t new_size= sizeof(char) * (size_t)((adjust * HASHKIT_BLOCK_SIZE) + string->current_size);
62 /* Test for overflow */
63 if (new_size < need)
64 {
65 return false;
66 }
67
68 char *new_value= (char*)realloc(string->string, new_size);
69
70 if (new_value == NULL)
71 {
72 return false;
73 }
74
75 string->string= new_value;
76 string->end= string->string + current_offset;
77
78 string->current_size+= (HASHKIT_BLOCK_SIZE * adjust);
79 }
80
81 return true;
82 }
83
84 static inline void _init_string(hashkit_string_st *self)
85 {
86 self->current_size= 0;
87 self->end= self->string= NULL;
88 }
89
90 hashkit_string_st *hashkit_string_create(size_t initial_size)
91 {
92 hashkit_string_st* self= (hashkit_string_st*)calloc(1, sizeof(hashkit_string_st));
93
94 if (self)
95 {
96 if (_string_check(self, initial_size) == false)
97 {
98 free(self);
99
100 return NULL;
101 }
102 }
103
104 return self;
105 }
106
107 #if 0
108 static bool hashkit_string_append_null(hashkit_string_st *string)
109 {
110 if (_string_check(string, 1) == false)
111 {
112 return false;
113 }
114
115 *string->end= 0;
116
117 return true;
118 }
119 #endif
120
121 bool hashkit_string_append_character(hashkit_string_st *string,
122 char character)
123 {
124 if (_string_check(string, 1) == false)
125 {
126 return false;
127 }
128
129 *string->end= character;
130 string->end++;
131
132 return true;
133 }
134
135 bool hashkit_string_append(hashkit_string_st *string,
136 const char *value, size_t length)
137 {
138 if (_string_check(string, length) == false)
139 {
140 return false;
141 }
142
143 assert(length <= string->current_size);
144 assert(string->string);
145 assert(string->end >= string->string);
146
147 memcpy(string->end, value, length);
148 string->end+= length;
149
150 return true;
151 }
152
153 char *hashkit_string_c_copy(hashkit_string_st *string)
154 {
155 if (hashkit_string_length(string) == 0)
156 {
157 return NULL;
158 }
159
160 char *c_ptr= static_cast<char *>(malloc((hashkit_string_length(string)+1) * sizeof(char)));
161 if (c_ptr == NULL)
162 {
163 return NULL;
164 }
165
166 memcpy(c_ptr, hashkit_string_c_str(string), hashkit_string_length(string));
167 c_ptr[hashkit_string_length(string)]= 0;
168
169 return c_ptr;
170 }
171
172 void hashkit_string_reset(hashkit_string_st *string)
173 {
174 string->end= string->string;
175 }
176
177 void hashkit_string_free(hashkit_string_st *ptr)
178 {
179 if (ptr == NULL)
180 {
181 return;
182 }
183
184 if (ptr->string)
185 {
186 free(ptr->string);
187 }
188 free(ptr);
189 }
190
191 bool hashkit_string_resize(hashkit_string_st& string, const size_t need)
192 {
193 return _string_check(&string, need);
194 }
195
196 size_t hashkit_string_length(const hashkit_string_st *self)
197 {
198 return size_t(self->end -self->string);
199 }
200
201 size_t hashkit_string_max_size(const hashkit_string_st *self)
202 {
203 return self->current_size;
204 }
205
206 char *hashkit_string_take(hashkit_string_st *self)
207 {
208 assert(self);
209 if (self == NULL)
210 {
211 return NULL;
212 }
213 char *value= self->string;
214
215 _init_string(self);
216
217 return value;
218 }
219
220 char *hashkit_string_c_str_mutable(hashkit_string_st *self)
221 {
222 assert(self);
223 if (self == NULL)
224 {
225 return NULL;
226 }
227 return self->string;
228 }
229
230 const char *hashkit_string_c_str(const hashkit_string_st* self)
231 {
232 assert(self);
233 if (self == NULL)
234 {
235 return NULL;
236 }
237 return self->string;
238 }
239
240 void hashkit_string_set_length(hashkit_string_st *self, size_t length)
241 {
242 assert(self);
243 if (self and _string_check(self, length))
244 {
245 self->end= self->string +length;
246 }
247 }