hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 5c5cfb36e47cdaf80a3c292a32f6b9e9d9e48ab9
parent 8d8c605613c577a876b7889592eff6eb3fac2f77
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon,  1 Feb 2021 16:38:40 -0500

encoding::utf8::decode: polymorphize

Diffstat:
Mencoding/utf8/decode.ha | 24++++++++++++++----------
Mencoding/utf8/rune.ha | 3---
2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/encoding/utf8/decode.ha b/encoding/utf8/decode.ha @@ -7,21 +7,25 @@ export type decoder = struct { src: []u8, }; -export fn decode(src: str) decoder = decoder { - offs = 0z, - src = strings::to_utf8(src), +// Initializes a new UTF-8 decoder. +export fn decode(src: (str | []u8)) decoder = match (src) { + s: str => decoder { + offs = 0z, + src = strings::to_utf8(s), + }, + b: []u8 => decoder { + offs = 0z, + src = b, + }, }; -// Initializes a new UTF-8 decoder for a byte slice. -export fn decode_bytes(src: []u8) decoder = decoder { - offs = 0z, - src = src, -}; - -// Indicates that more data is needed, or that a partial UTF-8 sequence was +// Indicates that more data is needed, i.e. that a partial UTF-8 sequence was // encountered. export type more = void; +// An error indicating that an invalid UTF-8 sequence was found. +export type invalid = void; + // Returns the next rune from a decoder. If the slice ends with a complete UTF-8 // sequence, void is returned. If an incomplete sequence is encountered, more is // returned. And if an invalid sequence is encountered, invalid returned. diff --git a/encoding/utf8/rune.ha b/encoding/utf8/rune.ha @@ -1,8 +1,5 @@ use types; -// An error indicating that an invalid UTF-8 sequence was found. -export type invalid = void; - const masks: [_]u8 = [0x7Fu8, 0x1Fu8, 0x0Fu8, 0x07u8, 0x03u8, 0x01u8]; type rsize = struct {