aboutsummaryrefslogtreecommitdiff
path: root/src/strings.c
diff options
context:
space:
mode:
authoramy <[email protected]>2023-04-14 13:22:32 +0000
committeramy <[email protected]>2023-04-14 13:22:32 +0000
commitd769c253e8a6ddc67cf8424da2017d309e93f11b (patch)
treef62685a725836337ab29cd8c3448d49d53ed4004 /src/strings.c
init
Diffstat (limited to 'src/strings.c')
-rw-r--r--src/strings.c75
1 files changed, 75 insertions, 0 deletions
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;
+}