Skip to content

Commit cc194e4

Browse files
committed
fix(mobile): match table delimiter rows in linear time
The delimiter-row pattern had two adjacent blank runs, so a long run of spaces followed by a stray character retried every split between them. Each blank run now has one place to go. Signed-off-by: Tom Brow <[email protected]>
1 parent 9e699a5 commit cc194e4

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎mobile/lib/features/channels/message_content/markdown_tables.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ int _cellCount(String row) {
5656
}
5757

5858
final _fenceMarker = RegExp(r'^(`{3,}|~{3,})');
59+
// Each blank run has one place to go, so a long run followed by a stray
60+
// character fails in linear time instead of retrying every split.
5961
final _delimiterRow = RegExp(
60-
r'^ {0,3}\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?\s*$',
62+
r'^ {0,3}\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*(?:\|\s*)?$',
6163
);
6264
final _unescapedPipe = RegExp(r'(?<!\\)\|');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:buzz/features/channels/message_content/markdown_tables.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
test('separates a table from its caption', () {
6+
expect(
7+
separateMarkdownTables('Results:\n| a | b |\n|---|:-:|\n| 1 | 2 |'),
8+
'Results:\n\n| a | b |\n|---|:-:|\n| 1 | 2 |',
9+
);
10+
});
11+
12+
test('a long blank run in a delimiter row takes linear time', () {
13+
// Overlapping blank runs retried every split: about a second at 20,000.
14+
final content = 'caption\n| a |\n|---${' ' * 200000}x|';
15+
final timer = Stopwatch()..start();
16+
expect(separateMarkdownTables(content), content);
17+
expect(timer.elapsedMilliseconds, lessThan(500));
18+
});
19+
}

0 commit comments

Comments
 (0)