Carp/core/carp_string.h

289 lines
6.3 KiB
C
Raw Normal View History

2018-02-27 16:14:14 +03:00
#pragma once
#include <string.h>
#include <carp_memory.h>
#include <core.h>
String String_allocate(int len, char byte) {
/* Allocate a string of length 'len + 1'
* setting the first len bytes to byte
* and adding a null terminator
*
* String_alloc(10, "a") == "aaaaaaaaaa"
*/
String ptr = CARP_MALLOC(len+1);
memset(ptr, byte, len);
ptr[len] = '\0';
return ptr;
}
2018-03-18 16:53:03 +03:00
void String_delete(String s) {
2018-02-27 16:14:14 +03:00
CARP_FREE(s);
}
2018-05-19 09:49:48 +03:00
void String_string_MINUS_set_BANG_(String *s, int i, char ch) {
int l = strlen(*s);
(void)l;
assert(i >= 0);
assert(i < l);
2018-05-19 09:49:48 +03:00
(*s)[i] = ch;
}
2018-05-20 06:53:30 +03:00
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, String *src) {
char *dest = (*into) + i;
int lsrc = strlen(*src);
int linto = strlen(*into);
(void)linto;
2018-05-20 06:53:30 +03:00
assert(i >= 0);
/* given a string and indicies
*
* 0 1 2 3 4 5 6 7 8 9
* "a b c d e f g h i j"
* linto = strlen(...) = 10
*
* if we want to insert at '6' a string of length '4'
*
* 0 1 2 3
* "w x y z"
* ldest = strlen(...) = 4
*
* we need to make sure that the new string will not grow the first
*
* 0 1 2 3 4 5 6 7 8 9
* "a b c d e f g h i j"
* ^
* |
* 0 1 2 3
* "w x y z"
*
* we check this by
* (i + ldest - 1) < linto
* (6 + 4 - 1) < 10
* (10 - 1) < 10
* 9 < 10
* true
*
* so this write is safe
*/
assert((i+lsrc-1) < linto);
strncpy(dest, *src, lsrc);
}
2018-03-18 16:53:03 +03:00
String String_copy(String *s) {
2018-02-27 16:14:14 +03:00
size_t len = strlen(*s) + 1;
2018-03-18 16:53:03 +03:00
String ptr = CARP_MALLOC(len);
2018-02-27 16:14:14 +03:00
if (ptr == NULL) {
return NULL;
}
2018-03-18 16:53:03 +03:00
return (String) memcpy(ptr, *s, len);
2018-02-27 16:14:14 +03:00
}
2018-03-18 16:53:03 +03:00
bool String__EQ_(String *a, String *b) {
2018-02-27 16:14:14 +03:00
return strcmp(*a, *b) == 0;
}
2018-06-26 09:00:33 +03:00
bool String__GT_(String *a, String *b) {
return strcmp(*a, *b) > 0;
}
bool String__LT_(String *a, String *b) {
return strcmp(*a, *b) < 0;
}
String String_append(String *a, String *b) {
int la = strlen(*a);
int lb = strlen(*b);
2018-02-27 16:14:14 +03:00
int total = la + lb + 1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(total);
snprintf(buffer, total, "%s%s", *a, *b);
return buffer;
}
int String_length(String *s) {
2018-02-27 16:14:14 +03:00
return strlen(*s);
}
2018-03-18 16:53:03 +03:00
char* String_cstr(String *s) {
2018-02-27 16:14:14 +03:00
return *s;
}
2018-03-18 16:53:03 +03:00
String String_str(String *s) {
2018-02-27 17:30:22 +03:00
int n = strlen(*s) + 1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(n);
2018-02-27 17:30:22 +03:00
snprintf(buffer, n, "%s", *s);
return buffer;
}
2018-03-18 16:53:03 +03:00
String String_prn(String *s) {
2018-02-27 16:14:14 +03:00
int n = strlen(*s) + 4;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(n);
2018-02-27 16:14:14 +03:00
snprintf(buffer, n, "@\"%s\"", *s);
return buffer;
}
2018-03-18 16:53:03 +03:00
char String_char_MINUS_at(String* s, int i) {
2018-02-27 16:14:14 +03:00
return (*s)[i];
}
2018-03-18 16:53:03 +03:00
String String_format(String *str, String *s) {
2018-02-27 16:14:14 +03:00
int size = snprintf(NULL, 0, *str, *s)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
2018-02-27 16:14:14 +03:00
snprintf(buffer, size, *str, *s);
return buffer;
}
2018-03-18 16:53:03 +03:00
Array String_chars(String *s) {
2018-02-27 16:14:14 +03:00
Array chars;
chars.len = strlen(*s);
2018-03-07 14:03:59 +03:00
chars.capacity = chars.len;
2018-02-27 16:14:14 +03:00
chars.data = String_copy(s);
return chars;
}
2018-03-18 16:53:03 +03:00
String String_from_MINUS_chars(Array *a) {
String s = CARP_MALLOC(a->len+1);
2018-03-12 16:56:05 +03:00
memmove(s, a->data, a->len);
s[a->len] = '\0';
2018-02-27 16:14:14 +03:00
return s;
}
2018-03-18 16:53:03 +03:00
String String_tail(String* s) {
2018-02-27 16:14:14 +03:00
int len = strlen(*s);
2018-03-18 16:53:03 +03:00
String news = CARP_MALLOC(len);
2018-02-27 16:14:14 +03:00
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
return news;
}
2018-03-18 16:53:03 +03:00
String String_empty() {
String s = CARP_MALLOC(1);
2018-02-27 16:14:14 +03:00
s[0] = '\0';
return s;
}
2018-03-18 16:53:03 +03:00
String Bool_str(bool b) {
String true_str = "true";
String false_str = "false";
if(b) {
return String_copy(&true_str);
} else {
return String_copy(&false_str);
}
}
2018-03-18 16:53:03 +03:00
String Bool_format(String* str, bool b) {
int size = snprintf(NULL, 0, *str, b)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Char_str(char c) {
String buffer = CARP_MALLOC(2);
snprintf(buffer, 2, "%c", c);
return buffer;
}
String Char_prn(char c) {
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(3);
snprintf(buffer, 3, "\\%c", c);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Char_format(String* str, char b) {
int size = snprintf(NULL, 0, *str, b)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Double_str(double x) {
int size = snprintf(NULL, 0, "%g", x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%g", x);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Double_format(String* s, double x) {
int size = snprintf(NULL, 0, *s, x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *s, x);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Float_str(float x) {
int size = snprintf(NULL, 0, "%gf", x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%gf", x);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Float_format(String* str, float x) {
int size = snprintf(NULL, 0, *str, x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Int_str(int x) {
int size = snprintf(NULL, 0, "%d", x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%d", x);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Int_format(String* str, int x) {
int size = snprintf(NULL, 0, *str, x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
2018-03-18 16:53:03 +03:00
int Int_from_MINUS_string(String *s) {
return atoi(*s);
}
2018-03-18 16:53:03 +03:00
String Long_str(long x) {
int size = snprintf(NULL, 0, "%ldl", x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, "%ldl", x);
return buffer;
}
2018-03-18 16:53:03 +03:00
String Long_format(String* str, long x) {
int size = snprintf(NULL, 0, *str, x)+1;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
2018-03-18 16:53:03 +03:00
long Long_from_MINUS_string(String *s) {
return atol(*s);
}
2018-05-20 11:25:13 +03:00
2018-05-20 11:41:36 +03:00
int String_index_MINUS_of_MINUS_from(String *s, char c, int i) {
/* Return index of first occurrence of `c` in `s` AFTER index i
2018-05-20 11:25:13 +03:00
* Returns -1 if not found
*/
2018-05-20 11:41:36 +03:00
++i; // skip first character as we want AFTER i
2018-05-20 11:25:13 +03:00
int len = strlen(*s);
2018-05-20 11:41:36 +03:00
for (; i<len; ++i) {
2018-05-20 11:25:13 +03:00
if (c == (*s)[i]) {
return i;
}
}
return -1;
}
2018-05-20 11:41:36 +03:00
int String_index_MINUS_of(String *s, char c) {
/* Return index of first occurrence of `c` in `s`
* Returns -1 if not found
*/
return String_index_MINUS_of_MINUS_from(s, c, -1);
}