From 18008acc141a55449b28c1af487a080c4bbcb355 Mon Sep 17 00:00:00 2001 From: bptato Date: Sat, 21 Oct 2023 23:34:56 +0200 Subject: base64: reduce pointless re-coding using JSString We now expose some functions from QuickJS to interact with JavaScript strings without re-encoding them into UTF-8. --- lib/quickjs/quickjs.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'lib/quickjs/quickjs.c') diff --git a/lib/quickjs/quickjs.c b/lib/quickjs/quickjs.c index 61bcce93..697ced39 100644 --- a/lib/quickjs/quickjs.c +++ b/lib/quickjs/quickjs.c @@ -208,7 +208,6 @@ typedef enum JSErrorEnum { #define __exception __attribute__((warn_unused_result)) typedef struct JSShape JSShape; -typedef struct JSString JSString; typedef struct JSString JSAtomStruct; typedef enum { @@ -4101,6 +4100,34 @@ void JS_FreeCString(JSContext *ctx, const char *ptr) JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p)); } +/* create a string from a narrow Unicode (latin-1) buffer */ +JSValue JS_NewNarrowStringLen(JSContext *ctx, const char *buf, size_t buf_len) +{ + if (buf_len > JS_STRING_LEN_MAX) + return JS_ThrowInternalError(ctx, "string too long"); + return js_new_string8(ctx, (const uint8_t *)buf, buf_len); +} + +JS_BOOL JS_IsStringWideChar(JSString *str) +{ + return str->is_wide_char; +} + +uint8_t *JS_GetNarrowStringBuffer(JSString *str) +{ + return str->u.str8; +} + +uint16_t *JS_GetWideStringBuffer(JSString *str) +{ + return str->u.str16; +} + +uint32_t JS_GetStringLength(JSString *str) +{ + return str->len; +} + static int memcmp16_8(const uint16_t *src1, const uint8_t *src2, int len) { int c, i; -- cgit 1.4.1-2-gfad0