aboutsummaryrefslogtreecommitdiff
path: root/src/str.c
diff options
context:
space:
mode:
authordacctal <donotcontactmevia@email.invalid>2026-07-21 21:32:27 +0000
committerdacctal <donotcontactmevia@email.invalid>2026-07-21 21:32:27 +0000
commit184bd50e2ea12b4bf40f4ae1b0d685f0bfa1c995 (patch)
treebfe4f0a8fb39def302bdc807308c30b60dd18a92 /src/str.c
parentc251d37c98efba534766e914f7392cc7a0470351 (diff)
started 2.0 refactor. Co-authored-by: ezntek <eason@ezntek.com>
Diffstat (limited to 'src/str.c')
-rw-r--r--src/str.c34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/str.c b/src/str.c
index e12719c..1a9e677 100644
--- a/src/str.c
+++ b/src/str.c
@@ -27,7 +27,7 @@ along with this program.If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h>
#include <string.h>
-#include "globs.h"
+#include "common.h"
#include "str.h"
// NOTE: only use this assertion in functions where you don't depend
@@ -76,6 +76,15 @@ str mstr(const char *s) {
return str_from_str_slc(_cstrslc(s));
}
+str str_adopt(char *s) {
+ str adopted = {
+ .data = s,
+ .len = strlen(s),
+ };
+ adopted.cap = adopted.len;
+ return adopted;
+}
+
str str_from_str_slc(const str_slc s) {
str res = str_new_with_capacity(s.len + 1);
str_copy_str_slc_into(&res, s);
@@ -207,13 +216,32 @@ void str_append_str_slc(str *src, const str_slc new) {
size_t len_after_append = src->len + new.len;
if (len_after_append + 1 > src->cap) {
src->cap = len_after_append + 1;
- str_reserve(src, src->cap);
+ str_reserve_exact(src, src->cap);
}
memcpy(src->data + src->len, new.data, new.len + 1);
src->len = len_after_append;
src->data[len_after_append] = 0;
}
+void str_append_format(str *src, const char *format, ...) {
+ assert_str_is_valid(dest);
+
+ va_list args, args_copy;
+ va_start(args, format);
+ va_copy(args_copy, args);
+
+ size_t len = vsnprintf(NULL, 0, format, args_copy);
+ if (src->len + len + 1 > src->cap) {
+ src->cap = 1 + src->len + len;
+ str_reserve_exact(src, src->cap);
+ }
+
+ vsnprintf(src->data + src->len, len + 1, format, args);
+ src->len += len;
+
+ va_end(args);
+}
+
char str_pop_last(str *src) {
char c = str_last(src);
src->len--;
@@ -502,7 +530,7 @@ str_slc str_slc_from_cstr(const char *s) {
return _cstrslc(s);
}
-str_slc mstrslc(const char *s) {
+str_slc mslc(const char *s) {
return (str_slc){s, strlen(s)};
}