move decapitalize into a separate function

This commit is contained in:
Helium314 2024-05-02 16:17:46 +02:00
parent 955359e33b
commit da3a7dd854
2 changed files with 10 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import android.content.Context;
import android.provider.UserDictionary; import android.provider.UserDictionary;
import android.text.TextUtils; import android.text.TextUtils;
import helium314.keyboard.latin.common.StringUtilsKt;
import helium314.keyboard.latin.settings.SettingsValues; import helium314.keyboard.latin.settings.SettingsValues;
import helium314.keyboard.latin.utils.DeviceProtectedUtils; import helium314.keyboard.latin.utils.DeviceProtectedUtils;
import helium314.keyboard.latin.utils.Log; import helium314.keyboard.latin.utils.Log;
@ -588,7 +589,7 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
// if suggestion was auto-capitalized, check against both the suggestion and the de-capitalized suggestion // if suggestion was auto-capitalized, check against both the suggestion and the de-capitalized suggestion
final String decapitalizedSuggestion; final String decapitalizedSuggestion;
if (wasAutoCapitalized) if (wasAutoCapitalized)
decapitalizedSuggestion = word.substring(0, 1).toLowerCase() + word.substring(1); decapitalizedSuggestion = StringUtilsKt.decapitalize(word, getCurrentLocale());
else else
decapitalizedSuggestion = word; decapitalizedSuggestion = word;
for (int i = 0; i < mDictionaryGroups.size(); i ++) { for (int i = 0; i < mDictionaryGroups.size(); i ++) {
@ -667,7 +668,7 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
if (wasAutoCapitalized) { if (wasAutoCapitalized) {
// used word with lower-case first letter instead of all lower-case, as auto-capitalize // used word with lower-case first letter instead of all lower-case, as auto-capitalize
// does not affect the other letters // does not affect the other letters
final String decapitalizedWord = word.substring(0, 1).toLowerCase(dictionaryGroup.mLocale) + word.substring(1); final String decapitalizedWord = StringUtilsKt.decapitalize(word, dictionaryGroup.mLocale);
if (isValidWord(word, ALL_DICTIONARY_TYPES, dictionaryGroup) && !isValidWord(decapitalizedWord, ALL_DICTIONARY_TYPES, dictionaryGroup)) { if (isValidWord(word, ALL_DICTIONARY_TYPES, dictionaryGroup) && !isValidWord(decapitalizedWord, ALL_DICTIONARY_TYPES, dictionaryGroup)) {
// If the word was auto-capitalized and exists only as a capitalized word in the // If the word was auto-capitalized and exists only as a capitalized word in the
// dictionary, then we must not downcase it before registering it. For example, // dictionary, then we must not downcase it before registering it. For example,

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.mightBeEmoji
import helium314.keyboard.latin.common.StringUtils.newSingleCodePointString import helium314.keyboard.latin.common.StringUtils.newSingleCodePointString
import helium314.keyboard.latin.settings.SpacingAndPunctuations import helium314.keyboard.latin.settings.SpacingAndPunctuations
import java.util.Locale
fun loopOverCodePoints(s: CharSequence, run: (Int) -> Boolean) { fun loopOverCodePoints(s: CharSequence, run: (Int) -> Boolean) {
val text = if (s is String) s else s.toString() val text = if (s is String) s else s.toString()
@ -100,6 +101,12 @@ fun String.splitOnFirstSpacesOnly(): List<String> {
return out return out
} }
fun String.decapitalize(locale: Locale): String {
if (isEmpty() || !this[0].isUpperCase())
return this
return replaceFirstChar { it.lowercase(locale) }
}
fun isEmoji(c: Int): Boolean = mightBeEmoji(c) && isEmoji(newSingleCodePointString(c)) fun isEmoji(c: Int): Boolean = mightBeEmoji(c) && isEmoji(newSingleCodePointString(c))
fun isEmoji(s: String): Boolean = mightBeEmoji(s) && s.matches(emoRegex) fun isEmoji(s: String): Boolean = mightBeEmoji(s) && s.matches(emoRegex)