4fc4ea4d6979410c41201eaa2c91640b0d71f250
[awesomized/libmemcached] / src / libhashkit / string.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libhashkit/common.h"
17
18 #include <cassert>
19 #include <cstring>
20
21 #define HASHKIT_BLOCK_SIZE 1024
22
23 struct hashkit_string_st {
24 char *end;
25 size_t current_size;
26 char *string;
27 };
28
29 inline static bool _string_check(hashkit_string_st *string, size_t need) {
30 if (need and need > (size_t)(string->current_size - (size_t)(string->end - string->string))) {
31 size_t current_offset = (size_t)(string->end - string->string);
32
33 /* This is the block multiplier. To keep it larger and surive division errors we must round it
34 * up */
35 size_t adjust = (need - (size_t)(string->current_size - (size_t)(string->end - string->string)))
36 / HASHKIT_BLOCK_SIZE;
37 adjust++;
38
39 size_t new_size = sizeof(char) * (size_t)((adjust * HASHKIT_BLOCK_SIZE) + string->current_size);
40 /* Test for overflow */
41 if (new_size < need) {
42 return false;
43 }
44
45 char *new_value = (char *) realloc(string->string, new_size);
46
47 if (new_value == NULL) {
48 return false;
49 }
50
51 string->string = new_value;
52 string->end = string->string + current_offset;
53
54 string->current_size += (HASHKIT_BLOCK_SIZE * adjust);
55 }
56
57 return true;
58 }
59
60 static inline void _init_string(hashkit_string_st *self) {
61 self->current_size = 0;
62 self->end = self->string = NULL;
63 }
64
65 hashkit_string_st *hashkit_string_create(size_t initial_size) {
66 hashkit_string_st *self = (hashkit_string_st *) calloc(1, sizeof(hashkit_string_st));
67
68 if (self) {
69 if (_string_check(self, initial_size) == false) {
70 free(self);
71
72 return NULL;
73 }
74 }
75
76 return self;
77 }
78
79 #if 0
80 static bool hashkit_string_append_null(hashkit_string_st *string)
81 {
82 if (_string_check(string, 1) == false)
83 {
84 return false;
85 }
86
87 *string->end= 0;
88
89 return true;
90 }
91 #endif
92
93 bool hashkit_string_append_character(hashkit_string_st *string, char character) {
94 if (_string_check(string, 1) == false) {
95 return false;
96 }
97
98 *string->end = character;
99 string->end++;
100
101 return true;
102 }
103
104 bool hashkit_string_append(hashkit_string_st *string, const char *value, size_t length) {
105 if (_string_check(string, length) == false) {
106 return false;
107 }
108
109 assert(length <= string->current_size);
110 assert(string->string);
111 assert(string->end >= string->string);
112
113 memcpy(string->end, value, length);
114 string->end += length;
115
116 return true;
117 }
118
119 char *hashkit_string_c_copy(hashkit_string_st *string) {
120 if (hashkit_string_length(string) == 0) {
121 return NULL;
122 }
123
124 char *c_ptr = static_cast<char *>(malloc((hashkit_string_length(string) + 1) * sizeof(char)));
125 if (c_ptr == NULL) {
126 return NULL;
127 }
128
129 memcpy(c_ptr, hashkit_string_c_str(string), hashkit_string_length(string));
130 c_ptr[hashkit_string_length(string)] = 0;
131
132 return c_ptr;
133 }
134
135 void hashkit_string_reset(hashkit_string_st *string) {
136 string->end = string->string;
137 }
138
139 void hashkit_string_free(hashkit_string_st *ptr) {
140 if (ptr == NULL) {
141 return;
142 }
143
144 if (ptr->string) {
145 free(ptr->string);
146 }
147 free(ptr);
148 }
149
150 bool hashkit_string_resize(hashkit_string_st &string, const size_t need) {
151 return _string_check(&string, need);
152 }
153
154 size_t hashkit_string_length(const hashkit_string_st *self) {
155 return size_t(self->end - self->string);
156 }
157
158 size_t hashkit_string_max_size(const hashkit_string_st *self) {
159 return self->current_size;
160 }
161
162 char *hashkit_string_take(hashkit_string_st *self) {
163 assert(self);
164 if (self == NULL) {
165 return NULL;
166 }
167 char *value = self->string;
168
169 _init_string(self);
170
171 return value;
172 }
173
174 char *hashkit_string_c_str_mutable(hashkit_string_st *self) {
175 assert(self);
176 if (self == NULL) {
177 return NULL;
178 }
179 return self->string;
180 }
181
182 const char *hashkit_string_c_str(const hashkit_string_st *self) {
183 assert(self);
184 if (self == NULL) {
185 return NULL;
186 }
187 return self->string;
188 }
189
190 void hashkit_string_set_length(hashkit_string_st *self, size_t length) {
191 assert(self);
192 if (self and _string_check(self, length)) {
193 self->end = self->string + length;
194 }
195 }