Scanner now uses @nogc UTF decoding.

This commit is contained in:
Ferdinand Majerech 2014-08-02 01:16:29 +02:00
parent e1209711af
commit e6fdade4a6

View file

@ -20,7 +20,6 @@ import std.exception;
import std.string; import std.string;
import std.typecons; import std.typecons;
import std.traits : Unqual; import std.traits : Unqual;
import std.utf;
import dyaml.fastcharsearch; import dyaml.fastcharsearch;
import dyaml.escapes; import dyaml.escapes;
@ -1942,11 +1941,24 @@ final class Scanner
// //
// This is probably slow, but simple and URI escapes are extremely uncommon // This is probably slow, but simple and URI escapes are extremely uncommon
// in YAML. // in YAML.
//
// Returns the number of bytes used by the dchar in bytes on success,
// size_t.max on failure.
static size_t getDchar(char[] bytes, Reader reader_) static size_t getDchar(char[] bytes, Reader reader_)
{ {
import std.utf;
size_t nextChar; size_t nextChar;
const c = std.utf.decode(bytes[], nextChar); dchar c;
if(bytes[0] < 0x80)
{
c = bytes[0];
++nextChar;
}
else
{
const decoded = decodeUTF8NoGC!(No.validated)(bytes[], nextChar);
if(decoded.errorMessage !is null) { return size_t.max; }
c = decoded.decoded;
}
reader_.sliceBuilder.write(c); reader_.sliceBuilder.write(c);
if(bytes.length - nextChar > 0) if(bytes.length - nextChar > 0)
{ {
@ -1957,14 +1969,19 @@ final class Scanner
} }
enum contextMsg = "While scanning a " ~ name; enum contextMsg = "While scanning a " ~ name;
try
{
while(reader_.peek() == '%') while(reader_.peek() == '%')
{ {
reader_.forward(); reader_.forward();
if(bytesUsed == bytes.length) if(bytesUsed == bytes.length)
{ {
bytesUsed = getDchar(bytes[], reader_); bytesUsed = getDchar(bytes[], reader_);
if(bytesUsed == size_t.max)
{
error(contextMsg, startMark,
"Invalid UTF-8 data encoded in URI escape sequence",
reader_.mark);
return;
}
} }
char b = 0; char b = 0;
@ -1996,16 +2013,6 @@ final class Scanner
bytesUsed = getDchar(bytes[0 .. bytesUsed], reader_); bytesUsed = getDchar(bytes[0 .. bytesUsed], reader_);
} }
catch(UTFException e)
{
error(contextMsg, startMark, e.msg, mark);
return;
}
catch(Exception e)
{
assert(false, "Unexpected exception in scanURIEscapesToSlice");
}
}
/// Scan a line break, if any. /// Scan a line break, if any.