string.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include <libhashkit/common.h>
  37. #include <cassert>
  38. #include <cstring>
  39. #define HASHKIT_BLOCK_SIZE 1024
  40. struct hashkit_string_st {
  41. char *end;
  42. size_t current_size;
  43. char *string;
  44. };
  45. inline static bool _string_check(hashkit_string_st *string, size_t need)
  46. {
  47. if (need and need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
  48. {
  49. size_t current_offset= (size_t) (string->end - string->string);
  50. /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
  51. size_t adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / HASHKIT_BLOCK_SIZE;
  52. adjust++;
  53. size_t new_size= sizeof(char) * (size_t)((adjust * HASHKIT_BLOCK_SIZE) + string->current_size);
  54. /* Test for overflow */
  55. if (new_size < need)
  56. {
  57. return false;
  58. }
  59. char *new_value= (char*)realloc(string->string, new_size);
  60. if (new_value == NULL)
  61. {
  62. return false;
  63. }
  64. string->string= new_value;
  65. string->end= string->string + current_offset;
  66. string->current_size+= (HASHKIT_BLOCK_SIZE * adjust);
  67. }
  68. return true;
  69. }
  70. static inline void _init_string(hashkit_string_st *self)
  71. {
  72. self->current_size= 0;
  73. self->end= self->string= NULL;
  74. }
  75. hashkit_string_st *hashkit_string_create(size_t initial_size)
  76. {
  77. hashkit_string_st* self= (hashkit_string_st*)calloc(1, sizeof(hashkit_string_st));
  78. if (self)
  79. {
  80. if (_string_check(self, initial_size) == false)
  81. {
  82. free(self);
  83. return NULL;
  84. }
  85. }
  86. return self;
  87. }
  88. #if 0
  89. static bool hashkit_string_append_null(hashkit_string_st *string)
  90. {
  91. if (_string_check(string, 1) == false)
  92. {
  93. return false;
  94. }
  95. *string->end= 0;
  96. return true;
  97. }
  98. #endif
  99. bool hashkit_string_append_character(hashkit_string_st *string,
  100. char character)
  101. {
  102. if (_string_check(string, 1) == false)
  103. {
  104. return false;
  105. }
  106. *string->end= character;
  107. string->end++;
  108. return true;
  109. }
  110. bool hashkit_string_append(hashkit_string_st *string,
  111. const char *value, size_t length)
  112. {
  113. if (_string_check(string, length) == false)
  114. {
  115. return false;
  116. }
  117. assert(length <= string->current_size);
  118. assert(string->string);
  119. assert(string->end >= string->string);
  120. memcpy(string->end, value, length);
  121. string->end+= length;
  122. return true;
  123. }
  124. char *hashkit_string_c_copy(hashkit_string_st *string)
  125. {
  126. if (hashkit_string_length(string) == 0)
  127. {
  128. return NULL;
  129. }
  130. char *c_ptr= static_cast<char *>(malloc((hashkit_string_length(string)+1) * sizeof(char)));
  131. if (c_ptr == NULL)
  132. {
  133. return NULL;
  134. }
  135. memcpy(c_ptr, hashkit_string_c_str(string), hashkit_string_length(string));
  136. c_ptr[hashkit_string_length(string)]= 0;
  137. return c_ptr;
  138. }
  139. void hashkit_string_reset(hashkit_string_st *string)
  140. {
  141. string->end= string->string;
  142. }
  143. void hashkit_string_free(hashkit_string_st *ptr)
  144. {
  145. if (ptr == NULL)
  146. {
  147. return;
  148. }
  149. if (ptr->string)
  150. {
  151. free(ptr->string);
  152. }
  153. free(ptr);
  154. }
  155. bool hashkit_string_resize(hashkit_string_st& string, const size_t need)
  156. {
  157. return _string_check(&string, need);
  158. }
  159. size_t hashkit_string_length(const hashkit_string_st *self)
  160. {
  161. return size_t(self->end -self->string);
  162. }
  163. size_t hashkit_string_max_size(const hashkit_string_st *self)
  164. {
  165. return self->current_size;
  166. }
  167. char *hashkit_string_take(hashkit_string_st *self)
  168. {
  169. assert(self);
  170. if (self == NULL)
  171. {
  172. return NULL;
  173. }
  174. char *value= self->string;
  175. _init_string(self);
  176. return value;
  177. }
  178. char *hashkit_string_c_str_mutable(hashkit_string_st *self)
  179. {
  180. assert(self);
  181. if (self == NULL)
  182. {
  183. return NULL;
  184. }
  185. return self->string;
  186. }
  187. const char *hashkit_string_c_str(const hashkit_string_st* self)
  188. {
  189. assert(self);
  190. if (self == NULL)
  191. {
  192. return NULL;
  193. }
  194. return self->string;
  195. }
  196. void hashkit_string_set_length(hashkit_string_st *self, size_t length)
  197. {
  198. assert(self);
  199. if (self and _string_check(self, length))
  200. {
  201. self->end= self->string +length;
  202. }
  203. }