Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at optimizing SplitOnWordBoundaries #3140

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ fun String.doubleQuote(): String =
fun String.dq(): String = this.doubleQuote()

private val completeWords: List<String> = listOf("ipv4", "ipv6", "sigv4", "mib", "gib", "kib", "ttl")

private fun String.splitOnWordBoundaries(): List<String> {
val out = mutableListOf<String>()
// These are whole words but cased differently, e.g. `IPv4`, `MiB`, `GiB`, `TtL`
var currentWord = ""

var completeWordInProgress = true
// emit the current word and update from the next character
val emit = { next: Char ->
completeWordInProgress = true
if (currentWord.isNotEmpty()) {
out += currentWord.lowercase()
}
Expand All @@ -37,13 +40,17 @@ private fun String.splitOnWordBoundaries(): List<String> {
}
val allLowerCase = this.lowercase() == this
this.forEachIndexed { index, nextCharacter ->
val peek = this.getOrNull(index + 1)
val doublePeek = this.getOrNull(index + 2)
val completeWordInProgress = completeWords.any {
(currentWord + this.substring(index)).lowercase().startsWith(
it,
)
} && !completeWords.contains(currentWord.lowercase())
val computeWordInProgress = {
val result = completeWordInProgress && currentWord.isNotEmpty() && completeWords.any {
it.startsWith(currentWord, ignoreCase = true) && (currentWord + this.substring(index)).startsWith(
it,
ignoreCase = true,
) && !it.equals(currentWord, ignoreCase = true)
}

completeWordInProgress = result
result
}
when {
// [C] in these docs indicates the value of nextCharacter
// A[_]B
Expand All @@ -53,15 +60,15 @@ private fun String.splitOnWordBoundaries(): List<String> {
currentWord.isEmpty() -> currentWord += nextCharacter.toString()

// Abc[D]ef or Ab2[D]ef
!completeWordInProgress && loweredFollowedByUpper(currentWord, nextCharacter) -> emit(nextCharacter)
!computeWordInProgress() && loweredFollowedByUpper(currentWord, nextCharacter) -> emit(nextCharacter)

// s3[k]ey
!completeWordInProgress && allLowerCase && digitFollowedByLower(currentWord, nextCharacter) -> emit(
!computeWordInProgress() && allLowerCase && digitFollowedByLower(currentWord, nextCharacter) -> emit(
nextCharacter,
)

// DB[P]roxy, or `IAM[U]ser` but not AC[L]s
endOfAcronym(currentWord, nextCharacter, peek, doublePeek) -> emit(nextCharacter)
endOfAcronym(currentWord, nextCharacter, this.getOrNull(index + 1), this.getOrNull(index + 2)) -> emit(nextCharacter)

// If we haven't found a word boundary, push it and keep going
else -> currentWord += nextCharacter.toString()
Expand Down