make the loopOverCodePointss inline Unit loopers

This commit is contained in:
devycarol 2025-03-06 22:14:30 -07:00
parent b6a13a7473
commit 2787b586ae
2 changed files with 18 additions and 29 deletions

View file

@ -117,14 +117,14 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
if (text == null) actualSteps = steps if (text == null) actualSteps = steps
else loopOverCodePoints(text) { cp, charCount -> else loopOverCodePoints(text) { cp, charCount ->
actualSteps += charCount actualSteps += charCount
actualSteps >= steps if (actualSteps >= steps) return actualSteps
} }
} else { } else {
val text = inputLogic.mConnection.getTextBeforeCursor(-steps * 4, 0) val text = inputLogic.mConnection.getTextBeforeCursor(-steps * 4, 0)
if (text == null) actualSteps = steps if (text == null) actualSteps = steps
else loopOverCodePointsBackwards(text) { cp, charCount -> else loopOverCodePointsBackwards(text) { cp, charCount ->
actualSteps -= charCount actualSteps -= charCount
actualSteps <= steps if (actualSteps <= steps) return actualSteps
} }
} }
return actualSteps return actualSteps
@ -220,12 +220,9 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
var actualSteps = 0 var actualSteps = 0
// corrected steps to avoid splitting chars belonging to the same codepoint // corrected steps to avoid splitting chars belonging to the same codepoint
loopOverCodePoints(text) { cp, charCount -> loopOverCodePoints(text) { cp, charCount ->
if (StringUtils.mightBeEmoji(cp)) { if (StringUtils.mightBeEmoji(cp)) return 0
actualSteps = 0
true
}
actualSteps += charCount actualSteps += charCount
actualSteps >= steps if (actualSteps >= steps) return min(actualSteps, text.length)
} }
return min(actualSteps, text.length) return min(actualSteps, text.length)
} }
@ -234,12 +231,9 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
var actualSteps = 0 var actualSteps = 0
// corrected steps to avoid splitting chars belonging to the same codepoint // corrected steps to avoid splitting chars belonging to the same codepoint
loopOverCodePointsBackwards(text) { cp, charCount -> loopOverCodePointsBackwards(text) { cp, charCount ->
if (StringUtils.mightBeEmoji(cp)) { if (StringUtils.mightBeEmoji(cp)) return 0
actualSteps = 0
true
}
actualSteps -= charCount actualSteps -= charCount
actualSteps <= steps if (actualSteps <= steps) return -min(-actualSteps, text.length)
} }
return -min(-actualSteps, text.length) return -min(-actualSteps, text.length)
} }

View file

@ -9,24 +9,24 @@ import helium314.keyboard.latin.settings.SpacingAndPunctuations
import java.math.BigInteger import java.math.BigInteger
import java.util.Locale import java.util.Locale
fun loopOverCodePoints(text: CharSequence, loop: (cp: Int, charCount: Int) -> Boolean) { inline fun loopOverCodePoints(text: CharSequence, loop: (cp: Int, charCount: Int) -> Unit) {
val s = text.toString() val s = text.toString()
var offset = 0 var offset = 0
while (offset < s.length) { while (offset < s.length) {
val cp = s.codePointAt(offset) val cp = s.codePointAt(offset)
val charCount = Character.charCount(cp) val charCount = Character.charCount(cp)
if (loop(cp, charCount)) return loop(cp, charCount)
offset += charCount offset += charCount
} }
} }
fun loopOverCodePointsBackwards(text: CharSequence, loop: (cp: Int, charCount: Int) -> Boolean) { inline fun loopOverCodePointsBackwards(text: CharSequence, loop: (cp: Int, charCount: Int) -> Unit) {
val s = text.toString() val s = text.toString()
var offset = s.length var offset = s.length
while (offset > 0) { while (offset > 0) {
val cp = s.codePointBefore(offset) val cp = s.codePointBefore(offset)
val charCount = Character.charCount(cp) val charCount = Character.charCount(cp)
if (loop(cp, charCount)) return loop(cp, charCount)
offset -= charCount offset -= charCount
} }
} }
@ -34,28 +34,23 @@ fun loopOverCodePointsBackwards(text: CharSequence, loop: (cp: Int, charCount: I
fun nonWordCodePointAndNoSpaceBeforeCursor(text: CharSequence, spacingAndPunctuations: SpacingAndPunctuations): Boolean { fun nonWordCodePointAndNoSpaceBeforeCursor(text: CharSequence, spacingAndPunctuations: SpacingAndPunctuations): Boolean {
var space = false var space = false
var nonWordCodePoint = false var nonWordCodePoint = false
loopOverCodePointsBackwards(text) { cp, charCount -> loopOverCodePointsBackwards(text) { cp, _ ->
if (!space && Character.isWhitespace(cp)) space = true if (!space && Character.isWhitespace(cp)) space = true
// treat double quote like a word codepoint for the purpose of this function (not great, maybe clarify name, or extend list of chars?) // treat double quote like a word codepoint for this function (not great, maybe clarify name or extend list of chars?)
if (!nonWordCodePoint && !spacingAndPunctuations.isWordCodePoint(cp) && cp != '"'.code) { if (!nonWordCodePoint && !spacingAndPunctuations.isWordCodePoint(cp) && cp != '"'.code) {
nonWordCodePoint = true nonWordCodePoint = true
} }
space && nonWordCodePoint // stop if both are found if (space && nonWordCodePoint) return false // stop if both are found
} }
return nonWordCodePoint && !space // return true if an non-word codepoint and no space was found return nonWordCodePoint // return true if a non-word codepoint and no space was found
} }
fun hasLetterBeforeLastSpaceBeforeCursor(text: CharSequence): Boolean { fun hasLetterBeforeLastSpaceBeforeCursor(text: CharSequence): Boolean {
var letter = false loopOverCodePointsBackwards(text) { cp, _ ->
loopOverCodePointsBackwards(text) { cp, charCount -> if (Character.isWhitespace(cp)) return false
if (Character.isWhitespace(cp)) true else if (Character.isLetter(cp)) return true
else if (Character.isLetter(cp)) {
letter = true
true
}
else false
} }
return letter return false
} }
/** get the complete emoji at end of [text], considering that emojis can be joined with ZWJ resulting in different emojis */ /** get the complete emoji at end of [text], considering that emojis can be joined with ZWJ resulting in different emojis */