Commit 093624e2 authored by Will Hilton's avatar Will Hilton

Add example unicode stdin pull-stream queue.

parent 1998645f
......@@ -172,8 +172,10 @@ if (o.OpenMP.id === 0) {
}
```
...OK you're right, that is totally too hard. Idea: make a through stream that
consumes UTF-8 bytes and emits JavaScript's UTF-16 string characters.
...OK you're right, that is totally too hard. You can get a preview of an improved experience
in examples/stdin/unicode-queue.js. I'm working on a through stream that accepts
a stream of UTF-8 bytes and returns a stream of UTF-16 (JavaScript) single-character strings.
Which leads us too...
### stdin : pull-stream Source
......
function (o) {
var queue = []
function schedule (task) {
queue.push(task)
}
function read (end, cb) {
if (end) return cb(end)
var c = o.modules.getchar()
if (c === undefined) return schedule([read, end, cb])
if (c instanceof Error) return cb(c)
return schedule([cb, null, c])
}
function tick () {
if (queue.length === 0) throw Error("Queue complete.");
// Dequeue
var task = queue.shift();
// Run it
task[0].apply(null, task.slice(1))
return true
}
if (o.OpenMP.id === 0) {
var unicodeStream = o.modules.compile('unicode-stream.js')
var readUnicode = unicodeStream(read)
var c
var chars = []
readUnicode(null, function next(end, char) {
if (end) throw end
o.modules.print(char)
schedule([readUnicode, null, next])
// readUnicode(null, next)
})
var i = 0;
try {
while (true) {
o.modules.print("Tick count: " + i)
tick()
i++
}
} catch (e) {
o.modules.print(e)
}
}
}
function unicodeStream (read) {
var UTF8decoder = new TextDecoder('utf-8')
function howManyBytes (firstByte) {
if (firstByte > 254) throw Error("Number too large to be a byte: " + firstByte)
if (firstByte >> 7 === 0) return 1
if (firstByte >> 5 === 0b110) return 2
if (firstByte >> 4 === 0b1110) return 3
if (firstByte >> 3 === 0b11110) return 4
}
return function unicodeThrough (end, cb) {
if (end) return cb(end)
var length = null;
var chars = [];
read(null, function next(end, byte) {
if (end) return cb(end)
length = length || howManyBytes(byte)
chars.push(byte)
var char = UTF8decoder.decode(new Uint8Array(chars))
if (chars.length === length) return cb(null, char)
read(null, next)
})
}
}
// function unicodeStream (emit) {
// function howManyBytes (firstByte) {
// if (firstByte > 254) throw Error("Number too large to be a byte: " + firstByte)
// if (firstByte >> 7 === 0) return 1
// if (firstByte >> 5 === 0b110) return 2
// if (firstByte >> 4 === 0b1110) return 3
// if (firstByte >> 3 === 0b11110) return 4
// }
// var length = null;
// var collection = [];
// return function (byte) {
// if (length === null) length = howManyBytes(byte)
// collection.push(byte)
// if (collection.length === length) {
// emit(collection)
// collection = []
// length = null
// }
// }
// }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment