Skip to content

Commit

Permalink
FormatTokensRewrite: reuse index claimed earlier
Browse files Browse the repository at this point in the history
We just need to check that this index is not occupied by another format
token, which could happen if a rule staked claim for this format token
previously.
  • Loading branch information
kitbellew committed Mar 27, 2024
1 parent bcbc17e commit 6ca9565
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,8 @@ object FormatTokensRewrite {
private[rewrite] def claimedRule(ftIdx: Int): Option[Replacement] =
claimed.get(ftIdx).map(tokens.apply).filter(_ ne null)

@inline
private[rewrite] def claim(ftIdx: Int, repl: Replacement): Int = {
val idx = tokens.length
claimed.update(ftIdx, idx)
tokens.append(
justClaim(ftIdx) {
if (repl eq null) null
else
(repl.how match {
Expand All @@ -336,8 +333,27 @@ object FormatTokensRewrite {
}
case _ => None
}).getOrElse(repl)
)
idx
}
}

private def justClaim(ftIdx: Int)(repl: Replacement): Int = {
val idx = tokens.length
val claimedIdx = claimed.getOrElseUpdate(ftIdx, idx)
val preClaimed = claimedIdx < idx
if (
preClaimed && {
val oldrepl = tokens(claimedIdx)
oldrepl != null && oldrepl.idx == ftIdx
}
) {
tokens(claimedIdx) = repl
claimedIdx
} else {
if (preClaimed)
claimed.update(ftIdx, idx)
tokens.append(repl)
idx
}
}

private[FormatTokensRewrite] def applyRule(
Expand Down

0 comments on commit 6ca9565

Please sign in to comment.