# Copyright 2007-2008 Kevin Reid, under the terms of the MIT X license # found at http://www.opensource.org/licenses/mit-license.html ................ pragma.syntax("0.9") def utf_8 :DeepFrozen := ["UTF-8"] def unsignedIntegerCoding :DeepFrozen := def shortCoding :DeepFrozen := unsignedIntegerCoding[16] def utf8StringWithLength16Coding implements DeepFrozen, ExitViaHere { # implements Binary to asType() { return String } to writingTo(out) { # , schedule # XXX verify stream's byte size def writeShort := shortCoding.writingTo(out) return def writer(value :String) { def data := EIO.takeAll(utf_8.encode(value.asStream(), [].asMap())) writeShort(data.size()) # may fail out.reserve().resolve(data) } } to takingFrom(ins, schedule) { # XXX verify stream's byte size def readShort := shortCoding.takingFrom(ins, schedule) return def taker() :(schedule.result(String)) { return schedule.whenResolved(readShort(), fn size { # XXX unnecessarily non-streaming; fixable with a "substream of N of the characters from this stream" schedule.whenResolved(EIO.takeRange(size, size, ins), fn data { def ta := EIO.takeAll(utf_8.decode(data.asStream(), [].asMap())) # workaround for bug in takeRange if (ta == []) { "" } else { ta } }) }) } } }