Skip to content

Some cleanup, fixing, and enhancements to TextFormatter #3275

@dodexahedron

Description

@dodexahedron

While doing a cleanup and performance pass on TextFormatter, the loops in the FindHotKey method made go hmmm.

They're basically Rune-ified IndexOf operations, looking for the hotkey specifier Rune and then the following Rune as the hotkey, so long as it isn't FFFD.

But the state machine doesn't account for the character following the hotkey specifier being FFFD.

When that happens, the state machine keeps running until it finds the next non-specifier and non-FFFD Rune and calls that the hotkey. That's of course not expected, but what makes it even more dangerous is that the index provided in the hotPos out parameter will be the index of the specifier, as with normal cases, but the following index is not the hotkey that it chose.

The naive fix is simple: Just have it reset curHotPos to -1 each time it finds Rune.ReplacementChar.

I've added several test cases that cover various sequences containing the hotkey specifier in at least one position and FFFD in at least one position.

I also see some simple ways to squeeze almost free performance out of that method, so I'm doing that as well (this was in the middle of performance work, after all). Mostly some span stuff and avoiding as many loop iterations as I can figure out without spending too much time on just that method.

Metadata

Metadata

Assignees

Labels

testingIssues related to testing

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions