use SpacedTokens instead of regex for splitting on whitespace

and where possible directly used SpacedTokens to avoid unnecessary list
This commit is contained in:
Helium314 2025-05-14 17:04:31 +02:00
parent c96eec601d
commit f48438f30a
2 changed files with 6 additions and 6 deletions

View file

@ -11,6 +11,7 @@ import helium314.keyboard.latin.R
import helium314.keyboard.latin.common.splitOnFirstSpacesOnly
import helium314.keyboard.latin.common.splitOnWhitespace
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.SpacedTokens
import helium314.keyboard.latin.utils.SubtypeLocaleUtils
import java.io.InputStream
import java.util.Locale
@ -83,7 +84,7 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) {
READER_MODE_EXTRA_KEYS -> if (!onlyPopupKeys) addExtraKey(line.split(colonSpaceRegex, 2))
READER_MODE_LABELS -> if (!onlyPopupKeys) addLabel(line.split(colonSpaceRegex, 2))
READER_MODE_NUMBER_ROW -> localizedNumberKeys = line.splitOnWhitespace()
READER_MODE_TLD -> line.splitOnWhitespace().forEach { tlds.add(".$it") }
READER_MODE_TLD -> SpacedTokens(line).forEach { tlds.add(".$it") }
}
}
}
@ -226,7 +227,7 @@ private fun getLocaleTlds(locale: Locale): LinkedHashSet<String> {
return tlds
specialCountryTlds.forEach {
if (ccLower != it.first) return@forEach
tlds.addAll(it.second.splitOnWhitespace())
tlds.addAll(SpacedTokens(it.second))
return@getLocaleTlds tlds
}
tlds.add(".$ccLower")
@ -235,7 +236,7 @@ private fun getLocaleTlds(locale: Locale): LinkedHashSet<String> {
private fun getDefaultTlds(locale: Locale): LinkedHashSet<String> {
val tlds = linkedSetOf<String>()
tlds.addAll(defaultTlds.splitOnWhitespace())
tlds.addAll(SpacedTokens(defaultTlds))
if ((locale.language != "en" && euroLocales.matches(locale.language)) || euroCountries.matches(locale.country))
tlds.add(".eu")
return tlds

View file

@ -6,6 +6,7 @@ import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
import helium314.keyboard.latin.common.StringUtils.mightBeEmoji
import helium314.keyboard.latin.common.StringUtils.newSingleCodePointString
import helium314.keyboard.latin.settings.SpacingAndPunctuations
import helium314.keyboard.latin.utils.SpacedTokens
import java.math.BigInteger
import java.util.Locale
@ -132,9 +133,7 @@ fun isEmoji(c: Int): Boolean = mightBeEmoji(c) && isEmoji(newSingleCodePointStri
fun isEmoji(text: CharSequence): Boolean = mightBeEmoji(text) && text.matches(emoRegex)
fun String.splitOnWhitespace() = split(whitespaceSplitRegex)
private val whitespaceSplitRegex = "\\s+".toRegex()
fun String.splitOnWhitespace() = SpacedTokens(this).toList()
// from https://github.com/mathiasbynens/emoji-test-regex-pattern, MIT license
// matches single emojis only