codeSpanAt function
The code span whose opening backtick run starts at i, if it closes.
CommonMark: a span opens with a run of N backticks and closes at the next run of exactly N. Returns the index of the closing run and N, or null when no run of the same length follows — the opening run is then literal text.
end bounds the search, for callers scanning more than one paragraph: a
code span never crosses a blank line.
Implementation
({int close, int run})? codeSpanAt(String text, int i, {int? end}) {
final n = end ?? text.length;
var run = 0;
while (i + run < n && text.codeUnitAt(i + run) == _backtickUnit) {
run += 1;
}
if (run == 0) {
return null;
}
var j = i + run;
while (j < n) {
if (text.codeUnitAt(j) != _backtickUnit) {
j += 1;
continue;
}
var close = 0;
while (j + close < n && text.codeUnitAt(j + close) == _backtickUnit) {
close += 1;
}
if (close == run) {
return (close: j, run: run);
}
j += close;
}
return null;
}