mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-20 23:25:43 +01:00
Read and write unclosed backtick runs and HTML openers in time linear in the text
This commit is contained in:
@@ -1,9 +1,39 @@
|
||||
import { runLength } from './emphasis-matching.ts'
|
||||
|
||||
// Each run's start by its length, read once, so asking for a closer at every opener stays linear in the text.
|
||||
export interface BacktickRuns {
|
||||
readonly asked: Map<number, number>
|
||||
readonly starts: ReadonlyMap<number, readonly number[]>
|
||||
}
|
||||
|
||||
export function backtickRun(text: string, index: number): number {
|
||||
return text.charAt(index) === '`' ? runLength(text, index) : 0
|
||||
}
|
||||
|
||||
export function backtickRuns(text: string): BacktickRuns {
|
||||
const starts = new Map<number, number[]>()
|
||||
for (let start = text.indexOf('`'); start !== -1; ) {
|
||||
const length = runLength(text, start)
|
||||
const known = starts.get(length)
|
||||
if (known === undefined) starts.set(length, [start])
|
||||
else known.push(start)
|
||||
start = text.indexOf('`', start + length)
|
||||
}
|
||||
return { asked: new Map(), starts }
|
||||
}
|
||||
|
||||
// As `closingBacktickRun`, for `from` past the end of an opener's run. Readers ask with `from` rising, which keeps
|
||||
// each length's cursor moving forward; an ask behind the cursor restarts that length from its first run.
|
||||
export function closingRun(runs: BacktickRuns, from: number, opener: number): number | undefined {
|
||||
const starts = runs.starts.get(opener)
|
||||
if (starts === undefined) return undefined
|
||||
let at = runs.asked.get(opener) ?? 0
|
||||
if ((starts[at - 1] ?? -1) >= from) at = 0
|
||||
while ((starts[at] ?? Infinity) < from) at += 1
|
||||
runs.asked.set(opener, at)
|
||||
return starts[at]
|
||||
}
|
||||
|
||||
// Where the run of exactly `opener` backticks closing a code span begins, `undefined` where none does.
|
||||
export function closingBacktickRun(text: string, from: number, opener: number): number | undefined {
|
||||
let cursor = from
|
||||
|
||||
Reference in New Issue
Block a user