Started reworking strings
This commit is contained in:
parent
40007c27a6
commit
f3af20aa36
|
|
@ -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
|
||||
|
|
@ -7,10 +7,22 @@
|
|||
#define SLS_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue