From 8fd211d789aeb80824890a315f57dd6d4ccfb7cb Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Sat, 2 Dec 2017 15:02:37 -0800 Subject: [PATCH] Clean up UTF-8 error handling in Dart --- lib/src/io/vm.dart | 21 ++++++++------------- pubspec.yaml | 2 +- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/src/io/vm.dart b/lib/src/io/vm.dart index 9471170f..565b15aa 100644 --- a/lib/src/io/vm.dart +++ b/lib/src/io/vm.dart @@ -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("�")) 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()); } } diff --git a/pubspec.yaml b/pubspec.yaml index 84518446..7c3bb5e9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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"