Skip to content

Commit 043bd58

Browse files
committed
Don't use regex to identify lang files, improving performance
1 parent 5a6e07f commit 043bd58

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/main/kotlin/translations/TranslationFiles.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ import java.util.Locale
6262
object TranslationFiles {
6363
private val MC_1_12_2 = SemanticVersion.release(1, 12, 2)
6464

65-
fun isTranslationFile(file: VirtualFile?) =
66-
file?.mcDomain != null && file.mcPath?.startsWith("lang/") == true &&
67-
file.fileType in listOf(LangFileType, JsonFileType.INSTANCE)
65+
fun isTranslationFile(file: VirtualFile?): Boolean {
66+
val mcPath = file?.mcPath ?: return false
67+
return mcPath.startsWith("lang/") && file.fileType in listOf(LangFileType, JsonFileType.INSTANCE)
68+
}
6869

6970
fun getLocale(file: VirtualFile?) =
7071
file?.nameWithoutExtension?.lowercase(Locale.ENGLISH)

src/main/kotlin/util/files.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,31 @@ val VirtualFile.manifest: Manifest?
4848
null
4949
}
5050

51-
// Technically resource domains are much more restricted ([a-z0-9_-]+) in modern versions, but we want to support as much as possible
52-
private val RESOURCE_LOCATION_PATTERN = Regex("^.*?/(assets|data)/([^/]+)/(.*?)$")
53-
5451
val VirtualFile.mcDomain: String?
55-
get() = RESOURCE_LOCATION_PATTERN.matchEntire(this.path)?.groupValues?.get(2)
52+
get() = mcDomainAndPath?.first
5653
val VirtualFile.mcPath: String?
57-
get() = RESOURCE_LOCATION_PATTERN.matchEntire(this.path)?.groupValues?.get(3)
54+
get() = mcDomainAndPath?.second
55+
val VirtualFile.mcDomainAndPath: Pair<String, String>?
56+
get() {
57+
var domain: String? = null
58+
val path = mutableListOf<String>()
59+
var vf: VirtualFile? = this
60+
while (vf != null) {
61+
val name = vf.name
62+
if (name == "assets" || name == "data") {
63+
break
64+
}
65+
domain?.let(path::add)
66+
domain = name
67+
vf = vf.parent
68+
}
69+
if (vf == null || domain == null) {
70+
// if vf is null, we never found "assets" or "data", if domain is null our file path was too short.
71+
return null
72+
}
73+
path.reverse()
74+
return domain to path.joinToString("/")
75+
}
5876

5977
operator fun Manifest.get(attribute: String): String? = mainAttributes.getValue(attribute)
6078
operator fun Manifest.get(attribute: Attributes.Name): String? = mainAttributes.getValue(attribute)

0 commit comments

Comments
 (0)