Skip to content

Commit 005fab6

Browse files
authored
Don't crash on surrogate pairs. (#5)
Closes flutter#4
1 parent 50d0424 commit 005fab6

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.2
2+
3+
* `SpanScanner` no longer crashes when creating a span that contains a UTF-16
4+
surrogate pair.
5+
16
## 1.0.1
27

38
* Fix the error text emitted by `StringScanner.expectChar()`.

lib/src/span_scanner.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SpanScanner extends StringScanner implements LineScanner {
5656
/// [FileSpan]s as well as for error reporting. It can be a [String], a
5757
/// [Uri], or `null`.
5858
SpanScanner(String string, {sourceUrl, int position})
59-
: _sourceFile = new SourceFile(string, url: sourceUrl),
59+
: _sourceFile = new SourceFile.fromString(string, url: sourceUrl),
6060
super(string, sourceUrl: sourceUrl, position: position);
6161

6262
/// Creates a new [SpanScanner] that eagerly computes line and column numbers.

lib/src/string_scanner.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class StringScanner {
204204
}
205205
if (length == null) length = match == null ? 0 : match.end - match.start;
206206

207-
var sourceFile = new SourceFile(string, url: sourceUrl);
207+
var sourceFile = new SourceFile.fromString(string, url: sourceUrl);
208208
var span = sourceFile.span(position, position + length);
209209
throw new StringScannerException(message, span, string);
210210
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
name: string_scanner
2-
version: 1.0.1
2+
version: 1.0.2
33
author: "Dart Team <[email protected]>"
44
homepage: https://github.com/dart-lang/string_scanner
55
description: >
66
A class for parsing strings using a sequence of patterns.
77
dependencies:
88
charcode: "^1.1.0"
9-
source_span: "^1.0.0"
9+
source_span: "^1.4.0"
1010
dev_dependencies:
1111
test: ">=0.12.0 <0.13.0"
1212
environment:

test/span_scanner_test.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import 'package:test/test.dart';
99
import 'utils.dart';
1010

1111
void main() {
12-
testForImplementation("lazy", () {
13-
return new SpanScanner('foo\nbar\nbaz', sourceUrl: 'source');
12+
testForImplementation("lazy", ([string]) {
13+
return new SpanScanner(string ?? 'foo\nbar\nbaz', sourceUrl: 'source');
1414
});
1515

16-
testForImplementation("eager", () {
17-
return new SpanScanner.eager('foo\nbar\nbaz', sourceUrl: 'source');
16+
testForImplementation("eager", ([string]) {
17+
return new SpanScanner.eager(string ?? 'foo\nbar\nbaz',
18+
sourceUrl: 'source');
1819
});
1920

2021
group("within", () {
@@ -23,7 +24,7 @@ void main() {
2324

2425
var scanner;
2526
setUp(() {
26-
var file = new SourceFile(text, url: 'source');
27+
var file = new SourceFile.fromString(text, url: 'source');
2728
scanner = new SpanScanner.within(
2829
file.span(startOffset, text.indexOf(' :after')));
2930
});
@@ -136,6 +137,15 @@ void testForImplementation(String name, SpanScanner create()) {
136137
expect(span.text, equals('o\nbar\nba'));
137138
});
138139

140+
test(".spanFrom() handles surrogate pairs correctly", () {
141+
scanner = create('fo\u{12345}o');
142+
scanner.scan('fo');
143+
var state = scanner.state;
144+
scanner.scan('\u{12345}o');
145+
var span = scanner.spanFrom(state);
146+
expect(span.text, equals('\u{12345}o'));
147+
});
148+
139149
test(".emptySpan returns an empty span at the current location", () {
140150
scanner.scan('foo\nba');
141151

0 commit comments

Comments
 (0)