about summary refs log tree commit diff stats
path: root/src/io/runestream.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-12 00:05:14 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-12 00:13:59 +0200
commitaf3c8348a096b80a22d9463c516a932689a4836c (patch)
treef340768cd666ed56e9fbf87e3a03b01e4e6d6d04 /src/io/runestream.nim
parentf150a706cfbe07ba0ebbfb6fdd904ff454ad7c60 (diff)
downloadchawan-af3c8348a096b80a22d9463c516a932689a4836c.tar.gz
Improve encoding support
* Use the output charset in lineedit (as w3m does)
* encoder: fix broken UTF-8 encoding, use openArray instead of var
  seq for input queue
* Add RuneStream as an in-memory interface to EncoderStream
* Document display-charset config option
Diffstat (limited to 'src/io/runestream.nim')
-rw-r--r--src/io/runestream.nim31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/io/runestream.nim b/src/io/runestream.nim
new file mode 100644
index 00000000..6facda91
--- /dev/null
+++ b/src/io/runestream.nim
@@ -0,0 +1,31 @@
+import streams
+
+type RuneStream* = ref object of Stream
+  at: int # index in u32 (i.e. position * 4)
+  source: seq[uint32]
+
+proc runeClose(s: Stream) =
+  let s = cast[RuneStream](s)
+  s.source.setLen(0)
+
+proc runeReadData(s: Stream, buffer: pointer, bufLen: int): int =
+  let s = cast[RuneStream](s)
+  let L = min(bufLen, s.source.len - s.at)
+  if s.source.len == s.at:
+    return
+  copyMem(buffer, addr s.source[s.at], L * sizeof(uint32))
+  s.at += L
+  assert s.at <= s.source.len
+  return L * sizeof(uint32)
+
+proc runeAtEnd(s: Stream): bool =
+  let s = cast[RuneStream](s)
+  return s.at == s.source.len
+
+proc newRuneStream*(source: openarray[uint32]): RuneStream =
+  return RuneStream(
+    source: @source,
+    closeImpl: runeClose,
+    readDataImpl: runeReadData,
+    atEndImpl: runeAtEnd
+  )