aboutsummaryrefslogtreecommitdiff
path: root/src/strings.c
blob: 83b7fb824cc819f96bfee4b22ce4ee8bee70c4df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;
}