diff options
Diffstat (limited to 'src/types/str.c')
| -rw-r--r-- | src/types/str.c | 132 |
1 files changed, 66 insertions, 66 deletions
diff --git a/src/types/str.c b/src/types/str.c index 929057e..364a02f 100644 --- a/src/types/str.c +++ b/src/types/str.c @@ -1,66 +1,66 @@ -#include "str.h" -#include "../lua.h" -#include "../util.h" - -#define alloc_buffer 64 - -str* str_initl(char* init, size_t len){ - if(init == NULL){ - char cc = '\0'; - init = &cc; - } - - str* s = malloc(sizeof * s); - s->_bytes = len + 1 + alloc_buffer; - s->c = malloc(s->_bytes); - if(s->c == NULL) p_fatal("failed to allocate string\n"); - s->len = len ; - - memcpy(s->c, init, len + 1); - return s; -} - -str* str_init(char* init){ - return str_initl(init, strlen(init)); -} - -void str_free(str* s){ - free(s->c); - return free(s); -} - -void str_push(str* s, char* insert){ - s->len += strlen(insert); - if(s->len + 1 >= s->_bytes) - s->c = realloc(s->c, s->_bytes = s->len + 1 + alloc_buffer); - strcat(s->c, insert); -} - -void str_pushl(str* s, char* insert, size_t l){ - if(s->len + l >= s->_bytes) - s->c = realloc(s->c, s->_bytes = s->len + l + alloc_buffer); - //strcat(s->c, insert); - for(int i = 0; i != l; i++){ - s->c[i + s->len] = insert[i]; - } - s->len += l; - s->c[s->len] = '\0'; -} - -void str_clear(str* s){ - memset(s->c, 0, s->len); - - s->len = 0; -} - -void str_popf(str* s, int len){ - memmove(s->c, s->c + len, s->len -= len); - s->c[s->len] = 0; -} - -void str_popb(str* s, int len){ - s->len -= len; - s->c[s->len] = 0; -} - - +#include "str.h"
+#include "../lua.h"
+#include "../util.h"
+
+#define alloc_buffer 64
+
+str* str_initl(char* init, size_t len){
+ if(init == NULL){
+ char cc = '\0';
+ init = &cc;
+ }
+
+ str* s = malloc(sizeof * s);
+ s->_bytes = len + 1 + alloc_buffer;
+ s->c = malloc(s->_bytes);
+ if(s->c == NULL) p_fatal("failed to allocate string\n");
+ s->len = len ;
+
+ memcpy(s->c, init, len + 1);
+ return s;
+}
+
+str* str_init(char* init){
+ return str_initl(init, strlen(init));
+}
+
+void str_free(str* s){
+ free(s->c);
+ return free(s);
+}
+
+void str_push(str* s, char* insert){
+ s->len += strlen(insert);
+ if(s->len + 1 >= s->_bytes)
+ s->c = realloc(s->c, s->_bytes = s->len + 1 + alloc_buffer);
+ strcat(s->c, insert);
+}
+
+void str_pushl(str* s, char* insert, size_t l){
+ if(s->len + l >= s->_bytes)
+ s->c = realloc(s->c, s->_bytes = s->len + l + alloc_buffer);
+ //strcat(s->c, insert);
+ for(int i = 0; i != l; i++){
+ s->c[i + s->len] = insert[i];
+ }
+ s->len += l;
+ s->c[s->len] = '\0';
+}
+
+void str_clear(str* s){
+ memset(s->c, 0, s->len);
+
+ s->len = 0;
+}
+
+void str_popf(str* s, int len){
+ memmove(s->c, s->c + len, s->len -= len);
+ s->c[s->len] = 0;
+}
+
+void str_popb(str* s, int len){
+ s->len -= len;
+ s->c[s->len] = 0;
+}
+
+
|
