Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #ifndef DSTRINGS_H
  2. #define DSTRINGS_H
  3.  
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <limits.h>
  9. #include <assert.h>
  10.  
  11. #define DYNAMIC_STRING_GROW_FACTOR 1.5
  12.  
  13. typedef struct DynamicString {
  14. char *s;
  15. size_t capacity;
  16. size_t length;
  17. } DynamicString;
  18.  
  19. void createDynamicString(DynamicString *, size_t);
  20. void destroyDynamicString(DynamicString *);
  21. void increaseDynamicStringCapacity(DynamicString *);
  22. void appendCharToDynamicString(DynamicString *, char);
  23.  
  24. /* The CREATEDYNAMICSTRING function
  25. *
  26. * The function initializes a DynamicString passing by the first argument.
  27. * The initial capacity of the string is passing by the second argument.
  28. * Capacity is the max length of the string. At the same time length is
  29. * current length of the string. Thus the function allocates capacity + 1
  30. * bytes for the string (considering the null-character).
  31. *
  32. * The input pointer to DynamicString struture should be a valid pointer and
  33. * capacity should be greater than 0.
  34. */
  35. void createDynamicString(DynamicString *ret, size_t capacity)
  36. {
  37. assert(ret);
  38. assert(capacity > 0);
  39.  
  40. ret->s = malloc(capacity + 1);
  41. assert(ret->s);
  42.  
  43. ret->length = 0;
  44. ret->capacity = capacity;
  45. }
  46.  
  47. /* The APPENDCHARTODYNAMICSTRING function
  48. *
  49. * The function appends a character to the input DynamicString. If capacity of
  50. * the string is not enough the function increases it.
  51. *
  52. * The input pointer to a DynamicString should be a valid pointer as well as
  53. * its string buffer.
  54. */
  55. void appendCharToDynamicString(DynamicString *s, char c)
  56. {
  57. assert(s);
  58. assert(s->s);
  59.  
  60. if (s->length == s->capacity)
  61. increaseDynamicStringCapacity(s);
  62.  
  63. s->s[s->length++] = c;
  64. s->s[s->length] = '\0';
  65. }
  66.  
  67. /* The INCREASEDYNAMICSTRINGCAPACITY function
  68. *
  69. * The function increases capacity of the input DynamicString. Grow factor
  70. * is sets by a macro constant DYNAMIC_STRING_GROW_FACTOR.
  71. *
  72. * The input pointer to a DynamicString struture should be a valid pointer
  73. * as well as its string buffer.
  74. */
  75. void increaseDynamicStringCapacity(DynamicString *s)
  76. {
  77. assert(s);
  78. assert(s->s);
  79.  
  80. const size_t newCapacity = ceil(s->capacity * DYNAMIC_STRING_GROW_FACTOR);
  81.  
  82. s->s = realloc(s->s, newCapacity + 1);
  83. assert(s->s);
  84.  
  85. s->capacity = newCapacity;
  86. }
  87.  
  88. /* The DESTROYDYNAMICSTRING function
  89. *
  90. * The function destroys the input DynamicString. It frees the string buffer
  91. * of the input DynamicString.
  92. *
  93. * The input pointer to a DynamicString should be a valid pointer as well as
  94. * its string buffer.
  95. */
  96. void destroyDynamicString(DynamicString *s)
  97. {
  98. assert(s);
  99. assert(s->s);
  100.  
  101. free(s->s);
  102. }
  103.  
  104. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement