about summary refs log tree commit diff stats
path: root/src/loader/response.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-22 20:14:08 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-22 20:14:19 +0100
commit78ffc938fa7e4baad0a55625026b765d215be1aa (patch)
tree857db2e963efb13933d1e7954e9d4dc657b6f11d /src/loader/response.nim
parentdef15ede4fbd686b0ee9b193f41b2a47190aa43a (diff)
downloadchawan-78ffc938fa7e4baad0a55625026b765d215be1aa.tar.gz
Replace Chakasu with Chagashi
The API is horrid :( but at least it copies less.

TODO: think of a better API.
Diffstat (limited to 'src/loader/response.nim')
-rw-r--r--src/loader/response.nim18
1 files changed, 8 insertions, 10 deletions
diff --git a/src/loader/response.nim b/src/loader/response.nim
index 0869a73e..6168aff6 100644
--- a/src/loader/response.nim
+++ b/src/loader/response.nim
@@ -1,5 +1,4 @@
 import std/streams
-import std/unicode
 
 import bindings/quickjs
 import io/promise
@@ -11,9 +10,9 @@ import loader/request
 import types/blob
 import types/url
 
-import chakasu/charset
-import chakasu/decoderstream
-import chakasu/encoderstream
+import chagashi/charset
+import chagashi/decoder
+import chagashi/validator
 
 type
   ResponseType* = enum
@@ -106,13 +105,12 @@ proc text*(response: Response): Promise[JSResult[string]] {.jsfunc.} =
       CHARSET_UTF_8
     else:
       response.charset
-    if cs == CHARSET_UTF_8 and s.validateUtf8() == -1:
-      ok(s)
+    #TODO this is inefficient
+    # maybe add a JS type that turns a seq[char] into JS strings
+    if cs in {CHARSET_UTF_8, CHARSET_UNKNOWN}:
+      ok(s.toValidUTF8())
     else:
-      let ss = newStringStream(s)
-      let ds = newDecoderStream(ss, cs)
-      let es = newEncoderStream(ds, CHARSET_UTF_8)
-      return ok(es.readAll())
+      ok(newTextDecoder(cs).decodeAll(s))
   )
 
 proc blob*(response: Response): Promise[JSResult[Blob]] {.jsfunc.} =