summary refs log tree commit diff
path: root/strio.h
diff options
context:
space:
mode:
authorWormHeamer2025-06-23 03:38:58 -0400
committerWormHeamer2025-06-23 03:38:58 -0400
commit83425585f75d7763e1126207f4481e68e006802c (patch)
tree13ecfdf9d01a184ac48dcd92855e0f376d031fe2 /strio.h
parent77dbfeab0b777f7e063c7d10d91572f423c5dd18 (diff)
add str_to_i64
Diffstat (limited to 'strio.h')
-rw-r--r--strio.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/strio.h b/strio.h
index c8bb21f..029f026 100644
--- a/strio.h
+++ b/strio.h
@@ -10,10 +10,13 @@ void str_putf(Str s, FILE *f);
 void str_put(Str s);
 
 int str_to_u64(Str s, uint64_t *out);
+int str_to_i64(Str s, int64_t *out);
+
 void str_cat_i64(Str *out, int64_t c, char pad_char, int min_width, Arena *a);
 void str_cat_u64(Str *out, uint64_t c, char pad_char, int min_width, Arena *a);
 void str_cat_fmtv(Str *out, Arena *arena, const char *fmt, va_list ap);
 void str_cat_fmt(Str *out, Arena *arena, const char *fmt, ...);
+
 Str str_fmtv(Arena *arena, const char *fmt, va_list ap);
 Str str_fmt(Arena *arena, const char *fmt, ...);
 const char *cstr_fmt(Arena *arena, const char *fmt, ...);
@@ -89,6 +92,18 @@ int str_to_u64(Str s, uint64_t *out) {
 	return 0;
 }
 
+int str_to_i64(Str s, int64_t *out) {
+	int64_t sign = 1;
+	if (s.n > 0 && s.s[0] == '-') {
+		s = str_skip(s, 1);
+		sign = -1;
+	}
+	uint64_t u64;
+	if (str_to_u64(s, &u64)) return -1;
+	*out = u64 * sign;
+	return 0;
+}
+
 static void str_cat_u64_(char buf[32], int *n, uint64_t c) {
 	int i = 0;
 	buf[31] = '\0';