Skip to content
Merged
Show file tree
Hide file tree
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 @@ -67,7 +67,6 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) {
if (stream == null) return
stream.reader().use { reader ->
var mode = READER_MODE_NONE
val colonSpaceRegex = ":\\s+".toRegex()
reader.forEachLine { l ->
val line = l.trim()
if (line.isEmpty()) return@forEachLine
Expand Down Expand Up @@ -257,9 +256,9 @@ private fun getCurrencyKey(locale: Locale): Pair<String, List<String>> {
return euro
if (locale.toString().matches(euroLocales))
return euro
if (locale.language.matches("ca|eu|lb|mt|pms".toRegex()))
if (locale.language.matches(euroLanguagesRegex))
return euro
if (locale.language.matches("ak|dag|ee|fa|gaa|ha|he|ig|iw|lo|ko|km|mn|ne|si|th|uk|vi|yo".toRegex()))
if (locale.language.matches(otherLanguagesRegex))
return genericCurrencyKey(getCurrency(locale))
if (locale.language == "hy")
return dram
Expand All @@ -271,7 +270,7 @@ private fun getCurrencyKey(locale: Locale): Pair<String, List<String>> {
return genericCurrencyKey(getCurrency(locale))
if (locale.country != "IN" && locale.language == "ta")
return genericCurrencyKey("௹")
if (locale.country == "IN" || locale.language.matches("hi|kn|ml|mr|ta|te|gu".toRegex()))
if (locale.country == "IN" || locale.language.matches(inLanguagesRegex))
return rupee
if (locale.country == "GB")
return pound
Expand Down Expand Up @@ -325,6 +324,11 @@ private val dollar = "$" to listOf("£", "¢", "€", "¥", "₱")
private val euroCountries = "AD|AT|BE|BG|HR|CY|CZ|DA|EE|FI|FR|DE|GR|HU|IE|IT|XK|LV|LT|LU|MT|MO|ME|NL|PL|PT|RO|SM|SK|SI|ES|VA".toRegex()
private val euroLocales = "bg|ca|cs|da|de|el|en|es|et|eu|fi|fr|ga|gl|hr|hu|it|lb|lt|lv|mt|nl|pl|pt|ro|sk|sl|sq|sr|sv".toRegex()

private val colonSpaceRegex = ":\\s+".toRegex()
private val euroLanguagesRegex = "ca|eu|lb|mt|pms".toRegex()
private val otherLanguagesRegex = "ak|dag|ee|fa|gaa|ha|he|ig|iw|lo|ko|km|mn|ne|si|th|uk|vi|yo".toRegex()
private val inLanguagesRegex = "hi|kn|ml|mr|ta|te|gu".toRegex()

const val POPUP_KEYS_ALL = "all"
const val POPUP_KEYS_MORE = "more"
const val POPUP_KEYS_MAIN = "main"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class EmojiAltPhysicalKeyDetector(resources: Resources) {
val values = resources.getStringArray(resourceId)
if (values != null) {
for (i in values.indices) {
val valuePair = values[i].split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val valuePair = values[i].split(commaRegex).dropLastWhile { it.isEmpty() }.toTypedArray()
if (valuePair.size != 2) {
Log.w(TAG, "Expected 2 integers in $name[$i] : ${values[i]}")
}
Expand All @@ -148,3 +148,5 @@ class EmojiAltPhysicalKeyDetector(resources: Resources) {
}
}
}

private val commaRegex = ",".toRegex()
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object SoundPackImporter {
}

fun getPackDir(context: Context, packId: String): File {
val cleanId = packId.replace("[^a-zA-Z0-9._-]".toRegex(), "_")
val cleanId = packId.replace(filenameAllowedCharsRegex, "_")
return File(getSoundPacksDir(context), cleanId)
}

Expand Down Expand Up @@ -225,7 +225,7 @@ object SoundPackImporter {
): String? {
val ext = originalName.substringAfterLast(".", "ogg").lowercase()
val rawName = originalName.substringBeforeLast(".")
val cleanId = "custom_${rawName.replace("[^a-zA-Z0-9_-]".toRegex(), "_").lowercase()}_${System.currentTimeMillis() % 10000}"
val cleanId = "custom_${rawName.replace(packIdAllowedCharsRegex, "_").lowercase()}_${System.currentTimeMillis() % 10000}"
val displayName = customName?.takeIf { it.isNotBlank() } ?: rawName
val packDir = getPackDir(context, cleanId)
packDir.mkdirs()
Expand Down Expand Up @@ -300,7 +300,7 @@ object SoundPackImporter {
val finalId = if (manifest != null) {
manifest.id
} else {
val baseName = zipFile.nameWithoutExtension.replace("[^a-zA-Z0-9_-]".toRegex(), "_").lowercase()
val baseName = zipFile.nameWithoutExtension.replace(packIdAllowedCharsRegex, "_").lowercase()
"custom_${baseName}_${System.currentTimeMillis() % 10000}"
}

Expand Down Expand Up @@ -505,3 +505,6 @@ object SoundPackImporter {
return uri.lastPathSegment
}
}

private val filenameAllowedCharsRegex = "[^a-zA-Z0-9._-]".toRegex()
private val packIdAllowedCharsRegex = "[^a-zA-Z0-9_-]".toRegex()
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ object LayoutUtils {
}

fun getSimpleRowStrings(layoutContent: String): List<String> =
layoutContent.replace("\r\n", "\n").split("\\n\\s*\\n".toRegex()).filter { it.isNotBlank() }
layoutContent.replace("\r\n", "\n").split(doubleNewlineRegex).filter { it.isNotBlank() }
}

private val doubleNewlineRegex = "\\n\\s*\\n".toRegex()
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,22 @@ private const val PREFS_FILE_NAME = "preferences.json"
private const val PROTECTED_PREFS_FILE_NAME = "protected_preferences.json"
private const val FLOATING_KEYBOARD_PREFS_FILE_NAME = "floating_keyboard_preferences.json"

private val blacklistsRegex = "blacklists${File.separator}.*\\.txt".toRegex()
private val layoutsRegex = "layouts${File.separator}.*${LayoutUtilsCustom.CUSTOM_LAYOUT_PREFIX}+\\..{0,4}".toRegex()
private val dictsRegex = "dicts${File.separator}.*${File.separator}.*user\\.dict".toRegex()
private val historyDictRegex = "UserHistoryDictionary.*${File.separator}UserHistoryDictionary.*\\.(body|header)".toRegex()
private val bgImgRegex = "custom_background_image.*".toRegex()
private val fontRegex = "custom_font".toRegex()
private val emojiFontRegex = "custom_emoji_font".toRegex()

private val backupFilePatterns by lazy { listOf(
"blacklists${File.separator}.*\\.txt".toRegex(),
"layouts${File.separator}.*${LayoutUtilsCustom.CUSTOM_LAYOUT_PREFIX}+\\..{0,4}".toRegex(), // can't expect a period at the end, as this would break restoring older backups
"dicts${File.separator}.*${File.separator}.*user\\.dict".toRegex(),
"UserHistoryDictionary.*${File.separator}UserHistoryDictionary.*\\.(body|header)".toRegex(),
"custom_background_image.*".toRegex(),
"custom_font".toRegex(),
"custom_emoji_font".toRegex(),
blacklistsRegex,
layoutsRegex, // can't expect a period at the end, as this would break restoring older backups
dictsRegex,
historyDictRegex,
bgImgRegex,
fontRegex,
emojiFontRegex,
) }

enum class BackupCategory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ fun LoadHandwritingPluginPreference(
conn.connect()
if (conn.responseCode == 200) {
val response = conn.inputStream.bufferedReader().use { it.readText() }
val regex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
val match = regex.find(response)
val match = tagNameRegex.find(response)
if (match != null) {
val tag = match.groupValues[1]
remoteVersion = tag
Expand Down Expand Up @@ -320,6 +319,8 @@ private fun isUpdateAvailable(local: String, remote: String): Boolean {
return false
}

private val tagNameRegex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()

private fun buildLanguageEntries(context: Context): List<Pair<String, String>> {
val sysLocale = context.resources.configuration.locales[0]
val locales = java.util.Locale.getAvailableLocales()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ fun LoadOcrPluginPreference(
conn.connect()
if (conn.responseCode == 200) {
val response = conn.inputStream.bufferedReader().use { it.readText() }
val regex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
val match = regex.find(response)
val match = tagNameRegex.find(response)
if (match != null) {
remoteVersion = match.groupValues[1]
}
Expand Down Expand Up @@ -321,3 +320,5 @@ private fun isUpdateAvailable(local: String, remote: String): Boolean {
}
return false
}

private val tagNameRegex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ fun LoadOfflineAiPluginPreference(
conn.connect()
if (conn.responseCode == 200) {
val response = conn.inputStream.bufferedReader().use { it.readText() }
val regex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
val match = regex.find(response)
val match = tagNameRegex.find(response)
if (match != null) {
val tag = match.groupValues[1]
remoteVersion = tag
Expand Down Expand Up @@ -318,3 +317,5 @@ private fun isUpdateAvailable(local: String, remote: String): Boolean {
}
return false
}

private val tagNameRegex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ fun LoadTranslationPluginPreference(
conn.connect()
if (conn.responseCode == 200) {
val response = conn.inputStream.bufferedReader().use { it.readText() }
val regex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
val match = regex.find(response)
val match = tagNameRegex.find(response)
if (match != null) {
val tag = match.groupValues[1]
remoteVersion = tag
Expand Down Expand Up @@ -323,6 +322,8 @@ private fun isUpdateAvailable(local: String, remote: String): Boolean {
return false
}

private val tagNameRegex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()

@Composable
fun TranslationModePreference() {
val ctx = LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ fun VoiceSettingsScreen(
conn.connect()
if (conn.responseCode == 200) {
val response = conn.inputStream.bufferedReader().use { it.readText() }
val regex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()
val match = regex.find(response)
val match = tagNameRegex.find(response)
if (match != null) {
val tag = match.groupValues[1]
remoteVersion = tag
Expand Down Expand Up @@ -842,6 +841,8 @@ private fun isUpdateAvailable(local: String, remote: String): Boolean {
return false
}

private val tagNameRegex = "\"tag_name\"\\s*:\\s*\"([^\"]+)\"".toRegex()

fun createVoiceSettings(context: Context): List<Setting> = listOf(
Setting(
key = VoiceConstants.PREF_VOICE_PROVIDER,
Expand Down
Loading