From 9695ea005d4af93dcd60f74f10fd3c54499a182f Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 11 Nov 2021 16:31:58 -0800 Subject: chore: split up base library into individual files for smaller binaries --- sys/base/string/append.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 sys/base/string/append.c (limited to 'sys/base/string/append.c') diff --git a/sys/base/string/append.c b/sys/base/string/append.c new file mode 100644 index 0000000..d4d0396 --- /dev/null +++ b/sys/base/string/append.c @@ -0,0 +1,53 @@ +#include "internal.h" + +// append will append the given null terminated c string to the string data +// structure. this variant can append a substring of length len of the given +// string to our buffer. the result is reallocated if not enough room is present +// in the buffer. +int +str·appendlen(string *s, vlong n, const byte* b) +{ + /* + bl = strlen(b); + if (n > bl) panicf("attempted to make a substring longer than string"); + */ + + str·grow(s, n); + if(*s == nil) + return 0; + + Hdr* h = (Hdr*)(*s - sizeof(Hdr)); + + memcpy(*s + str·len(*s), b, n); + h->len += n; + (*s)[h->len] = '\0'; + + return n; +} + +// append will append the given null terminated c string to the string data +// structure. this variant will append the entire string. +int +str·append(string *s, const byte* b) +{ + return str·appendlen(s, strlen(b), b); +} + +// appendbyte will append the given byte to our string. +// NOTE: as the byte is on the stack, it is not null-terminated. +// can not pass to the above functions. +int +str·appendbyte(string *s, const byte b) +{ + str·grow(s, 1); + if(*s == nil) + return 0; + + Hdr* h = (Hdr*)(*s - sizeof(Hdr)); + + *(*s + str·len(*s)) = b; + h->len++; + (*s)[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..? + + return 1; +} -- cgit v1.2.1