File tree Expand file tree Collapse file tree 5 files changed +48
-2
lines changed Expand file tree Collapse file tree 5 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 1
- ## 1.2.1-wip
1
+ ## 1.3.0
2
2
3
3
* Require Dart 3.1.0
4
4
5
+ * Add a ` SpanScanner.spanFromPosition() ` method which takes raw code units
6
+ rather than ` SpanScanner.spanFrom() ` 's ` LineScannerState ` s.
7
+
5
8
## 1.2.0
6
9
7
10
* Require Dart 2.18.0
Original file line number Diff line number Diff line change @@ -78,6 +78,18 @@ class RelativeSpanScanner extends StringScanner implements SpanScanner {
78
78
_startLocation.offset + endPosition);
79
79
}
80
80
81
+ @override
82
+ FileSpan spanFromPosition (int startPosition, [int ? endPosition]) {
83
+ RangeError .checkValidRange (
84
+ startPosition,
85
+ endPosition,
86
+ _sourceFile.length - _startLocation.offset,
87
+ 'startPosition' ,
88
+ 'endPosition' );
89
+ return _sourceFile.span (_startLocation.offset + startPosition,
90
+ _startLocation.offset + (endPosition ?? position));
91
+ }
92
+
81
93
@override
82
94
bool matches (Pattern pattern) {
83
95
if (! super .matches (pattern)) {
Original file line number Diff line number Diff line change @@ -91,6 +91,17 @@ class SpanScanner extends StringScanner implements LineScanner {
91
91
return _sourceFile.span (startState.position, endPosition);
92
92
}
93
93
94
+ /// Creates a [FileSpan] representing the source range between [startPosition]
95
+ /// and [endPosition] , or the current position if [endPosition] is null.
96
+ ///
97
+ /// Each position should be a code unit offset into the string being scanned,
98
+ /// with the same conventions as [StringScanner.position] .
99
+ ///
100
+ /// Throws a [RangeError] if [startPosition] or [endPosition] aren't within
101
+ /// this source file.
102
+ FileSpan spanFromPosition (int startPosition, [int ? endPosition]) =>
103
+ _sourceFile.span (startPosition, endPosition ?? position);
104
+
94
105
@override
95
106
bool matches (Pattern pattern) {
96
107
if (! super .matches (pattern)) {
Original file line number Diff line number Diff line change 1
1
name : string_scanner
2
- version : 1.2.1-wip
2
+ version : 1.3.0
3
3
description : A class for parsing strings using a sequence of patterns.
4
4
repository : https://github.com/dart-lang/string_scanner
5
5
Original file line number Diff line number Diff line change @@ -75,6 +75,16 @@ void main() {
75
75
expect (span.text, equals ('o\n bar\n ba' ));
76
76
});
77
77
78
+ test ('.spanFromPosition() returns a span from a previous state' , () {
79
+ scanner.scan ('fo' );
80
+ final start = scanner.position;
81
+ scanner.scan ('o\n ba' );
82
+ scanner.scan ('r\n ba' );
83
+
84
+ final span = scanner.spanFromPosition (start + 2 , start + 5 );
85
+ expect (span.text, equals ('bar' ));
86
+ });
87
+
78
88
test ('.emptySpan returns an empty span at the current location' , () {
79
89
scanner.scan ('foo\n ba' );
80
90
@@ -139,6 +149,16 @@ void testForImplementation(
139
149
expect (span.text, equals ('o\n bar\n ba' ));
140
150
});
141
151
152
+ test ('.spanFromPosition() returns a span from a previous state' , () {
153
+ scanner.scan ('fo' );
154
+ final start = scanner.position;
155
+ scanner.scan ('o\n ba' );
156
+ scanner.scan ('r\n ba' );
157
+
158
+ final span = scanner.spanFromPosition (start + 2 , start + 5 );
159
+ expect (span.text, equals ('bar' ));
160
+ });
161
+
142
162
test ('.emptySpan returns an empty span at the current location' , () {
143
163
scanner.scan ('foo\n ba' );
144
164
You can’t perform that action at this time.
0 commit comments