Clean up UTF-8 error handling in Dart

This commit is contained in:
Natalie Weizenbaum 2017-12-02 15:02:37 -08:00
parent 2cfb0daf4d
commit 8fd211d789
2 changed files with 9 additions and 14 deletions

View File

@ -26,21 +26,16 @@ String readFile(String path) {
try {
return UTF8.decode(bytes);
} on FormatException {
} on FormatException catch (error) {
var decodedUntilError =
UTF8.decode(bytes.sublist(0, error.offset), allowMalformed: true);
var stringOffset = decodedUntilError.length;
if (decodedUntilError.endsWith("<EFBFBD>")) stringOffset--;
var decoded = UTF8.decode(bytes, allowMalformed: true);
var sourceFile = new SourceFile.fromString(decoded, url: p.toUri(path));
// TODO(nweiz): Use [FormatException.offset] instead when
// dart-lang/sdk#28293 is fixed.
for (var i = 0; i < bytes.length; i++) {
if (decoded.codeUnitAt(i) != 0xFFFD) continue;
throw new SassException(
"Invalid UTF-8.", sourceFile.location(i).pointSpan());
}
// This should be unreachable, but we'll rethrow the original exception just
// in case.
rethrow;
throw new SassException(
"Invalid UTF-8.", sourceFile.location(stringOffset).pointSpan());
}
}

View File

@ -8,7 +8,7 @@ executables:
dart-sass: sass
environment:
sdk: '>=1.21.0 <2.0.0'
sdk: '>=1.22.0 <2.0.0'
dependencies:
args: "^0.13.0"