summaryrefslogtreecommitdiff
path: root/src/runtime/c/gu/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/runtime/c/gu/string.c')
-rw-r--r--src/runtime/c/gu/string.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/runtime/c/gu/string.c b/src/runtime/c/gu/string.c
index e2708aae1..c8b443daf 100644
--- a/src/runtime/c/gu/string.c
+++ b/src/runtime/c/gu/string.c
@@ -5,6 +5,7 @@
#include <gu/string.h>
#include <gu/utf8.h>
#include <gu/assert.h>
+#include <stdlib.h>
const GuString gu_empty_string = { 1 };
@@ -166,6 +167,43 @@ gu_string_write(GuString s, GuOut* out, GuExn* err)
}
GuString
+gu_string_read(size_t len, GuPool* pool, GuIn* in, GuExn* err)
+{
+ uint8_t* buf = alloca(len*4);
+ uint8_t* p = buf;
+ for (size_t i = 0; i < len; i++) {
+ gu_in_utf8_buf(&p, in, err);
+ }
+ return gu_utf8_string(buf, p-buf, pool);
+}
+
+GuString
+gu_string_read_latin1(size_t len, GuPool* pool, GuIn* in, GuExn* err)
+{
+ if (len < GU_MIN(sizeof(GuWord), 128)) {
+ GuWord w = 0;
+ for (size_t n = 0; n < len; n++) {
+ w = w << 8 | gu_in_u8(in, err);
+ }
+ w = w << 8 | (len << 1) | 1;
+ return (GuString) { w };
+ }
+ uint8_t* p = NULL;
+ if (len < 256) {
+ p = gu_malloc_aligned(pool, 1 + len, 2);
+ p[0] = (uint8_t) len;
+ } else {
+ p = gu_malloc_prefixed(pool, gu_alignof(size_t),
+ sizeof(size_t), 1, 1 + len);
+ ((size_t*) p)[-1] = len;
+ p[0] = 0;
+ }
+
+ gu_in_bytes(in, &p[1], len, err);
+ return (GuString) { (GuWord) (void*) p };
+}
+
+GuString
gu_format_string_v(const char* fmt, va_list args, GuPool* pool)
{
GuPool* tmp_pool = gu_local_pool();