From fdf4cecddba5f644a160dfffae4915d971978363 Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Fri, 1 Aug 2014 02:47:52 +0200 Subject: [PATCH] Backported a recent std.utf script. --- source/dyaml/nogcutil.d | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/dyaml/nogcutil.d b/source/dyaml/nogcutil.d index 771a393..0806567 100644 --- a/source/dyaml/nogcutil.d +++ b/source/dyaml/nogcutil.d @@ -306,6 +306,8 @@ ValidateResult validateUTF8NoGC(const(char[]) str) @trusted pure nothrow @nogc } assert(fst & 0x80); + // starter must have at least 2 first bits set + if((fst & 0b1100_0000) != 0b1100_0000) { return invalidUTF(pstr[0 .. length]); } ubyte tmp = void; dchar d = fst; // upper control bits are masked out later fst <<= 1; @@ -373,11 +375,13 @@ dchar decodeValidUTF8NoGC(const(char[]) str, ref size_t index) ubyte fst = pstr[0]; assert(fst & 0x80); + enum invalidUTFMsg = "Invalid UTF-8 sequence in supposedly validated string"; + // starter must have at least 2 first bits set + assert((fst & 0b1100_0000) == 0b1100_0000, invalidUTFMsg); ubyte tmp = void; dchar d = fst; // upper control bits are masked out later fst <<= 1; - enum invalidUTFMsg = "Invalid UTF-8 sequence in supposedly validated string"; foreach (i; TypeTuple!(1, 2, 3)) { assert(i != length, "Decoding out of bounds in supposedly validated UTF-8");