Skip to content

Commit 88525b9

Browse files
committed
Check res id using String.isJavaIdentifier{Start,Part}
1 parent 1c3f52e commit 88525b9

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

app/src/main/java/org/fcitx/fcitx5/android/core/data/DataManager.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.fcitx.fcitx5.android.BuildConfig
1515
import org.fcitx.fcitx5.android.core.data.DataManager.dataDir
1616
import org.fcitx.fcitx5.android.utils.FileUtil
1717
import org.fcitx.fcitx5.android.utils.appContext
18-
import org.fcitx.fcitx5.android.utils.javaIdRegex
18+
import org.fcitx.fcitx5.android.utils.isJavaIdentifier
1919
import org.xmlpull.v1.XmlPullParser
2020
import timber.log.Timber
2121
import java.io.File
@@ -149,14 +149,13 @@ object DataManager {
149149
}
150150
parser.close()
151151

152-
// Replace @string/ with string resource
153-
description = description?.let { d ->
154-
d.removePrefix("@string/").let { s ->
155-
if (s.matches(javaIdRegex)) {
156-
@SuppressLint("DiscouragedApi")
157-
val id = res.getIdentifier(s, "string", packageName)
158-
if (id != 0) res.getString(id) else d
159-
} else d
152+
if (description?.startsWith("@string/") == true) {
153+
// Replace "@string/" with string resource
154+
val s = description.substring(8)
155+
if (s.isJavaIdentifier()) {
156+
@SuppressLint("DiscouragedApi")
157+
val id = res.getIdentifier(s, "string", packageName)
158+
if (id != 0) description = res.getString(id)
160159
}
161160
}
162161

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* SPDX-FileCopyrightText: Copyright 2024 Fcitx5 for Android Contributors
4+
*/
5+
6+
package org.fcitx.fcitx5.android.utils
7+
8+
fun String.isJavaIdentifier(): Boolean {
9+
if (this.isEmpty()) return false
10+
if (!this[0].isJavaIdentifierStart()) return false
11+
for (i in 1..<this.length) {
12+
if (!this[i].isJavaIdentifierPart()) return false
13+
}
14+
return true
15+
}

app/src/main/java/org/fcitx/fcitx5/android/utils/Utils.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ inline fun <T> withTempDir(block: (File) -> T): T {
242242
@Suppress("FunctionName")
243243
fun <T> WeakHashSet(): MutableSet<T> = Collections.newSetFromMap(WeakHashMap<T, Boolean>())
244244

245-
val javaIdRegex = Regex("(?:\\b[_a-zA-Z]|\\B\\$)\\w*+")
246-
247245
fun InputConnection.withBatchEdit(block: InputConnection.() -> Unit) {
248246
beginBatchEdit()
249247
block.invoke(this)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy