Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct SmartStr_st
- {
- char* buff; //the actual string,
- int size; //the number of allocated bytes in our buffer,
- int len; //the length of the data inside the buffer.
- } SmartStr;
- /*
- Just some simple constructors, the returned object has 'size'
- bytes allocated for the buffer and length = 0.
- (or strlen(text)+1 bytes allocated)
- */
- SmartStr* smart_str_empty_new(int size);
- SmartStr* smart_str_new(char* text);
- /*
- our realloc function
- ps.: this is declared and defined in the implementation ('.c') file since
- it's a private function.
- This function will expand the buffer. The final size will be 'str->size' + 'space'.
- */
- static void smart_str_expand(SmartStr* str, int space)
- {
- char* aux = (char*)malloc(sizeof(char)*(str->size + space));
- strcpy(aux, str->buffer);
- free(str->buffer);
- str->buffer = aux;
- str->size += space;
- }
- /*
- And finally, the append functions:
- */
- SmartStr* smart_str_raw_append(SmartStr* a, char* b, int blength)
- {
- if (a->size < (a->len + blength))
- smart_str_expand(a, a->length + blength);
- strcat(a->buffer, b);
- return a; //The return is just for compatibility with other string libraries.
- }
- SmartStr* smart_str_append(SmartStr* a, SmartStr* b)
- {
- return smart_str_raw_append(a, b->buffer, b->len);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement