Created
September 23, 2016 08:31
-
-
Save water-air-flash/fb74ab18c53c485769c2a6cd1b81f082 to your computer and use it in GitHub Desktop.
Dynamic string implementation for C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include "string.h" | |
| string *readline() { | |
| string *buf = str_create(""); | |
| int c; | |
| for(c = getchar(); c != EOF && c != '\n'; c = getchar()) { | |
| str_cappend(buf, c); | |
| } | |
| return buf; | |
| } | |
| int main() { | |
| string *mystring = readline(); | |
| // example std input: "this is an example" | |
| printf("%s\n", str_get(mystring)); // prints: this is an example | |
| printf("%d\n", str_length(mystring)); // prints: 18 | |
| printf("%c\n", str_getc(mystring, 5)); // prints: 'i' | |
| strncpy(mystring->str, "here", strlen("here")); | |
| printf("%s\n", str_get(mystring)); // prints: here is an example | |
| str_free(mystring); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "string.h" | |
| #define MAX(a, b) ((a) > (b) ? (a) : (b)) | |
| string *str_create(char *init) { | |
| string *s = (string *)malloc(sizeof(string)); | |
| if(!s) return NULL; | |
| s->size = MAX(strlen(init) + 1, STRSIZE); | |
| s->str = (char *)malloc(s->size); | |
| strcpy(s->str, init); | |
| return s; | |
| } | |
| void str_free(string *s) { | |
| free(s->str); | |
| free(s); | |
| } | |
| void str_growto(string *s, unsigned int newsize) { | |
| if(newsize <= s->size) return; | |
| unsigned int oldsize = s->size; | |
| while(s->size < newsize) | |
| s->size *= 2; | |
| s->str = (char *)realloc((void *)s->str, s->size); | |
| memset((void *)(s->str + oldsize), '\0', s->size - oldsize); | |
| } | |
| void str_grow(string *s) { | |
| str_growto(s, s->size * 2); | |
| } | |
| void str_shrink(string *s) { | |
| s->size /= 2; | |
| s->size = MAX(s->size, STRSIZE); | |
| s->str = (char *)realloc((void *)s->str, s->size); | |
| s->str[s->size-1] = '\0'; | |
| } | |
| unsigned int str_sizeof(string *s) { | |
| return s->size; | |
| } | |
| unsigned int str_length(string *s) { | |
| return strlen(s->str); | |
| } | |
| char str_getc(string *s, unsigned int index) { | |
| if(index >= s->size) return '\0'; | |
| return s->str[index]; | |
| } | |
| char *str_get(string *s) { | |
| return s->str; | |
| } | |
| void str_append(string *s, char *app) { | |
| int len = strlen(s->str); | |
| str_growto(s, s->size + len); | |
| int i; | |
| for(i = 0; i < strlen(app); i++) { | |
| s->str[len+i] = app[i]; | |
| } | |
| } | |
| void str_cappend(string *s, char c) { | |
| unsigned int len = strlen(s->str); | |
| if(len >= s->size-1) | |
| str_grow(s); | |
| s->str[len] = c; | |
| } | |
| void str_set(string *s, char *newstr) { | |
| str_growto(s, s->size + strlen(newstr)); | |
| strcpy(s->str, newstr); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef STRING_H | |
| #define STRING_H | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #define STRSIZE 10 | |
| typedef struct { | |
| char *str; | |
| unsigned int size; | |
| } string; | |
| string *str_create(char *init); | |
| void str_free(string *s); | |
| void str_grow(string *s); | |
| void str_growto(string *s, unsigned int newsize); | |
| void str_shrink(string *s); | |
| unsigned int str_sizeof(string *s); | |
| unsigned int str_length(string *s); | |
| void str_append(string *s, char *app); | |
| void str_cappend(string *s, char c); | |
| char str_getc(string *s, unsigned int index); | |
| char *str_get(string *s); | |
| void str_set(string *s, char *newstr); | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment