mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-06-20 16:10:52 +00:00
make the loopOverCodePointss inline Unit loopers
This commit is contained in:
parent
b6a13a7473
commit
2787b586ae
2 changed files with 18 additions and 29 deletions
|
@ -117,14 +117,14 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
|
|||
if (text == null) actualSteps = steps
|
||||
else loopOverCodePoints(text) { cp, charCount ->
|
||||
actualSteps += charCount
|
||||
actualSteps >= steps
|
||||
if (actualSteps >= steps) return actualSteps
|
||||
}
|
||||
} else {
|
||||
val text = inputLogic.mConnection.getTextBeforeCursor(-steps * 4, 0)
|
||||
if (text == null) actualSteps = steps
|
||||
else loopOverCodePointsBackwards(text) { cp, charCount ->
|
||||
actualSteps -= charCount
|
||||
actualSteps <= steps
|
||||
if (actualSteps <= steps) return actualSteps
|
||||
}
|
||||
}
|
||||
return actualSteps
|
||||
|
@ -220,12 +220,9 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
|
|||
var actualSteps = 0
|
||||
// corrected steps to avoid splitting chars belonging to the same codepoint
|
||||
loopOverCodePoints(text) { cp, charCount ->
|
||||
if (StringUtils.mightBeEmoji(cp)) {
|
||||
actualSteps = 0
|
||||
true
|
||||
}
|
||||
if (StringUtils.mightBeEmoji(cp)) return 0
|
||||
actualSteps += charCount
|
||||
actualSteps >= steps
|
||||
if (actualSteps >= steps) 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
|
||||
// corrected steps to avoid splitting chars belonging to the same codepoint
|
||||
loopOverCodePointsBackwards(text) { cp, charCount ->
|
||||
if (StringUtils.mightBeEmoji(cp)) {
|
||||
actualSteps = 0
|
||||
true
|
||||
}
|
||||
if (StringUtils.mightBeEmoji(cp)) return 0
|
||||
actualSteps -= charCount
|
||||
actualSteps <= steps
|
||||
if (actualSteps <= steps) return -min(-actualSteps, text.length)
|
||||
}
|
||||
return -min(-actualSteps, text.length)
|
||||
}
|
||||
|
|
|
@ -9,24 +9,24 @@ import helium314.keyboard.latin.settings.SpacingAndPunctuations
|
|||
import java.math.BigInteger
|
||||
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()
|
||||
var offset = 0
|
||||
while (offset < s.length) {
|
||||
val cp = s.codePointAt(offset)
|
||||
val charCount = Character.charCount(cp)
|
||||
if (loop(cp, charCount)) return
|
||||
loop(cp, 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()
|
||||
var offset = s.length
|
||||
while (offset > 0) {
|
||||
val cp = s.codePointBefore(offset)
|
||||
val charCount = Character.charCount(cp)
|
||||
if (loop(cp, charCount)) return
|
||||
loop(cp, charCount)
|
||||
offset -= charCount
|
||||
}
|
||||
}
|
||||
|
@ -34,28 +34,23 @@ fun loopOverCodePointsBackwards(text: CharSequence, loop: (cp: Int, charCount: I
|
|||
fun nonWordCodePointAndNoSpaceBeforeCursor(text: CharSequence, spacingAndPunctuations: SpacingAndPunctuations): Boolean {
|
||||
var space = false
|
||||
var nonWordCodePoint = false
|
||||
loopOverCodePointsBackwards(text) { cp, charCount ->
|
||||
loopOverCodePointsBackwards(text) { cp, _ ->
|
||||
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) {
|
||||
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 {
|
||||
var letter = false
|
||||
loopOverCodePointsBackwards(text) { cp, charCount ->
|
||||
if (Character.isWhitespace(cp)) true
|
||||
else if (Character.isLetter(cp)) {
|
||||
letter = true
|
||||
true
|
||||
}
|
||||
else false
|
||||
loopOverCodePointsBackwards(text) { cp, _ ->
|
||||
if (Character.isWhitespace(cp)) return false
|
||||
else if (Character.isLetter(cp)) return true
|
||||
}
|
||||
return letter
|
||||
return false
|
||||
}
|
||||
|
||||
/** get the complete emoji at end of [text], considering that emojis can be joined with ZWJ resulting in different emojis */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue