Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash on surrogate pairs. #5

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.2

* `SpanScanner` no longer crashes when creating a span that contains a UTF-16
surrogate pair.

## 1.0.1

* Fix the error text emitted by `StringScanner.expectChar()`.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/span_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SpanScanner extends StringScanner implements LineScanner {
/// [FileSpan]s as well as for error reporting. It can be a [String], a
/// [Uri], or `null`.
SpanScanner(String string, {sourceUrl, int position})
: _sourceFile = new SourceFile(string, url: sourceUrl),
: _sourceFile = new SourceFile.fromString(string, url: sourceUrl),
super(string, sourceUrl: sourceUrl, position: position);

/// Creates a new [SpanScanner] that eagerly computes line and column numbers.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/string_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class StringScanner {
}
if (length == null) length = match == null ? 0 : match.end - match.start;

var sourceFile = new SourceFile(string, url: sourceUrl);
var sourceFile = new SourceFile.fromString(string, url: sourceUrl);
var span = sourceFile.span(position, position + length);
throw new StringScannerException(message, span, string);
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: string_scanner
version: 1.0.1
version: 1.0.2
author: "Dart Team <[email protected]>"
homepage: https://github.com/dart-lang/string_scanner
description: >
A class for parsing strings using a sequence of patterns.
dependencies:
charcode: "^1.1.0"
source_span: "^1.0.0"
source_span: "^1.4.0"
dev_dependencies:
test: ">=0.12.0 <0.13.0"
environment:
Expand Down
20 changes: 15 additions & 5 deletions test/span_scanner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import 'package:test/test.dart';
import 'utils.dart';

void main() {
testForImplementation("lazy", () {
return new SpanScanner('foo\nbar\nbaz', sourceUrl: 'source');
testForImplementation("lazy", ([string]) {
return new SpanScanner(string ?? 'foo\nbar\nbaz', sourceUrl: 'source');
});

testForImplementation("eager", () {
return new SpanScanner.eager('foo\nbar\nbaz', sourceUrl: 'source');
testForImplementation("eager", ([string]) {
return new SpanScanner.eager(string ?? 'foo\nbar\nbaz',
sourceUrl: 'source');
});

group("within", () {
Expand All @@ -23,7 +24,7 @@ void main() {

var scanner;
setUp(() {
var file = new SourceFile(text, url: 'source');
var file = new SourceFile.fromString(text, url: 'source');
scanner = new SpanScanner.within(
file.span(startOffset, text.indexOf(' :after')));
});
Expand Down Expand Up @@ -136,6 +137,15 @@ void testForImplementation(String name, SpanScanner create()) {
expect(span.text, equals('o\nbar\nba'));
});

test(".spanFrom() handles surrogate pairs correctly", () {
scanner = create('fo\u{12345}o');
scanner.scan('fo');
var state = scanner.state;
scanner.scan('\u{12345}o');
var span = scanner.spanFrom(state);
expect(span.text, equals('\u{12345}o'));
});

test(".emptySpan returns an empty span at the current location", () {
scanner.scan('foo\nba');

Expand Down