1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
38 #include <libhashkit/common.h>
43 #define HASHKIT_BLOCK_SIZE 1024
45 struct hashkit_string_st
{
51 inline static bool _string_check(hashkit_string_st
*string
, size_t need
)
53 if (need
and need
> (size_t)(string
->current_size
- (size_t)(string
->end
- string
->string
)))
55 size_t current_offset
= (size_t) (string
->end
- string
->string
);
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
;
61 size_t new_size
= sizeof(char) * (size_t)((adjust
* HASHKIT_BLOCK_SIZE
) + string
->current_size
);
62 /* Test for overflow */
68 char *new_value
= (char*)realloc(string
->string
, new_size
);
70 if (new_value
== NULL
)
75 string
->string
= new_value
;
76 string
->end
= string
->string
+ current_offset
;
78 string
->current_size
+= (HASHKIT_BLOCK_SIZE
* adjust
);
84 static inline void _init_string(hashkit_string_st
*self
)
86 self
->current_size
= 0;
87 self
->end
= self
->string
= NULL
;
90 hashkit_string_st
*hashkit_string_create(size_t initial_size
)
92 hashkit_string_st
* self
= (hashkit_string_st
*)calloc(1, sizeof(hashkit_string_st
));
96 if (_string_check(self
, initial_size
) == false)
108 static bool hashkit_string_append_null(hashkit_string_st
*string
)
110 if (_string_check(string
, 1) == false)
121 bool hashkit_string_append_character(hashkit_string_st
*string
,
124 if (_string_check(string
, 1) == false)
129 *string
->end
= character
;
135 bool hashkit_string_append(hashkit_string_st
*string
,
136 const char *value
, size_t length
)
138 if (_string_check(string
, length
) == false)
143 assert(length
<= string
->current_size
);
144 assert(string
->string
);
145 assert(string
->end
>= string
->string
);
147 memcpy(string
->end
, value
, length
);
148 string
->end
+= length
;
153 char *hashkit_string_c_copy(hashkit_string_st
*string
)
155 if (hashkit_string_length(string
) == 0)
160 char *c_ptr
= static_cast<char *>(malloc((hashkit_string_length(string
)+1) * sizeof(char)));
166 memcpy(c_ptr
, hashkit_string_c_str(string
), hashkit_string_length(string
));
167 c_ptr
[hashkit_string_length(string
)]= 0;
172 void hashkit_string_reset(hashkit_string_st
*string
)
174 string
->end
= string
->string
;
177 void hashkit_string_free(hashkit_string_st
*ptr
)
191 bool hashkit_string_resize(hashkit_string_st
& string
, const size_t need
)
193 return _string_check(&string
, need
);
196 size_t hashkit_string_length(const hashkit_string_st
*self
)
198 return size_t(self
->end
-self
->string
);
201 size_t hashkit_string_max_size(const hashkit_string_st
*self
)
203 return self
->current_size
;
206 char *hashkit_string_take(hashkit_string_st
*self
)
213 char *value
= self
->string
;
220 char *hashkit_string_c_str_mutable(hashkit_string_st
*self
)
230 const char *hashkit_string_c_str(const hashkit_string_st
* self
)
240 void hashkit_string_set_length(hashkit_string_st
*self
, size_t length
)
243 if (self
and _string_check(self
, length
))
245 self
->end
= self
->string
+length
;