diff --git a/SLS_C/SlsStr.md b/SLS_C/SlsStr.md new file mode 100644 index 0000000..9353158 --- /dev/null +++ b/SLS_C/SlsStr.md @@ -0,0 +1,17 @@ +# Sls String +*string.h* + +## Formats + +- `c` char\* +- `d` int32_t +- `l` int64_t +- `ul` uint64_t +- `zu` size_t +- `f` double +- `s` string.h SlsStr +- `t` lexer.h TokenType +- `a` lexer.h ArrayType +- `i` lexer.h IntegerBuiltInType +- `e` error.h SlsError +- `b` bool.h Boolean diff --git a/SLS_C/include/sls/string.h b/SLS_C/include/sls/string.h index e068a0c..62d5a93 100644 --- a/SLS_C/include/sls/string.h +++ b/SLS_C/include/sls/string.h @@ -7,10 +7,22 @@ #define SLS_STRING_H #include +#include #include "bool.h" +typedef struct { + const size_t len; + const char *str; + const Boolean allocated; +} SlsStr; + +#define SLS_STR(s) (SlsStr){ sizeof(s) - 1, (s), FALSE } + int isascii(unsigned char c); size_t strnlen(const char *s, size_t maxlen); +SlsStr malloc_str(const char *s, size_t maxlen); +SlsStr format(const SlsStr s, ...); + #endif // SLS_STRING_H diff --git a/SLS_C/src/string.c b/SLS_C/src/string.c index b7a86e3..cabfb91 100644 --- a/SLS_C/src/string.c +++ b/SLS_C/src/string.c @@ -5,6 +5,7 @@ #include #include +#include int isascii(unsigned char c) { return c < 128; @@ -16,3 +17,10 @@ size_t strnlen(const char *s, size_t maxlen) { if (s[i] == '\0') break; return i; } + +char *malloc_str(const char *s, size_t maxlen) { + size_t length = strnlen(s, maxlen); + char *new_str = (char *)malloc(sizeof(char) * length); + strncpy(new_str, s, length); + return new_str; +}