From d769c253e8a6ddc67cf8424da2017d309e93f11b Mon Sep 17 00:00:00 2001 From: amy Date: Fri, 14 Apr 2023 13:22:32 +0000 Subject: init --- src/strings.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/strings.c (limited to 'src/strings.c') diff --git a/src/strings.c b/src/strings.c new file mode 100644 index 0000000..83b7fb8 --- /dev/null +++ b/src/strings.c @@ -0,0 +1,75 @@ +#include "strings.h" +#include "util.h" +int str_size(str*str){ + for(int i = 0;;i++) + if(str->str[i]=='\0') + return i; + return 0; +} +int ca_size(char*str){ + for(int i = 0;;i++) + if(str[i]=='\0') + return i; + return 0; +} +void str_pushc(str*str,char ch){ + str->str = realloc(str->str,sizeof(str->str)*(str->len+1)); + if(str->str==NULL) + err("failed to realloc string",pexit); + str->str[str->len]=ch; + str->str[str->len+1]='\0'; + str->len++; +} +void str_pushca(str*str,char*ins){ + int size = ca_size(ins); + str->str = realloc(str->str,sizeof(str->str)*(str->len+1+size)); + if(str->str==NULL) + err("failed to realloc string",pexit); + for(int i = 0; i!=size; i++) + str_pushc(str,ins[i]); +} +str* str_init(){ + str* str = malloc(sizeof(*str)); + str->str = malloc(sizeof(str->str)); + if(str->str==NULL||str==NULL) + err("failed to alloc string",pexit); + str->str[0] = '\0'; + str->len = 0; + return str; +} +int str_cmp_str(str*str1,str*str2){ + if(str1->len!=str2->len) + return 0; + for(int i = 0; i!=str1->len;i++) + if(str1->str[i]!=str2->str[i]) + return 0; + return 1; +} +int str_cmp_ca(str*str,char*ca){ + int len = ca_size(ca); + if(len!=str->len) + return 0; + for(int i = 0; i!=str->len;i++) + if(str->str[i]!=ca[i]) + return 0; + return 1; +} +void str_free(str*str){ + free(str->str); + free(str); +} +str* str_init_only_str(str*str){ + //str* str = malloc(sizeof(*str)); + str->str = malloc(sizeof(str->str)); + if(str->str==NULL||str==NULL) + err("failed to alloc string",pexit); + str->str[0] = '\0'; + str->len = 0; + return str; +} +void str_clear(str*str){ + for(int i = 0; i!=str->len; i++){ + str->str[i]='\0'; + } + str->len=0; +} -- cgit v1.2.3