Skip to content

Commit 679c4c0

Browse files
committed
properly obtain argument positional index from regex
1 parent 492ca27 commit 679c4c0

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

as2f/src/main/kotlin/dev/vishna/as2f/AndroidStrings.kt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import java.io.InputStream
88
import java.lang.IllegalArgumentException
99
import javax.xml.parsers.DocumentBuilderFactory
1010

11+
private val regexStringArgs = Regex("%([1-9]+)\\\$s")
12+
private val regexNumArgs = Regex("%([1-9]+)\\\$d")
1113
data class AndroidStrings(
1214
val locale: String,
1315
val strings : List<StringNode>,
@@ -127,24 +129,23 @@ fun Map<String, String>.asArbText() : String {
127129
/**
128130
* very unoptimized converter
129131
*/
130-
private fun String.toArb() : String {
132+
private fun String.toArb(): String {
131133
var input: String = this
132-
val regexArgs = Regex("%([1-9]+)s")
133-
val regexNums = Regex("%([1-9]+)d")
134-
replacementMap.forEach { key, value ->
135-
val findArgs: Boolean = !regexArgs.findAll(key).toList().isNullOrEmpty()
136-
val findNums: Boolean = !regexNums.findAll(key).toList().isNullOrEmpty()
137-
when {
138-
findArgs -> {
139-
input = input.replace(regexArgs, value)
140-
}
141-
findNums -> {
142-
input = input.replace(regexNums, value)
143-
}
144-
else -> {
145-
input = input.replace(key, value)
146-
}
147-
}
134+
135+
val args = regexStringArgs.findAll(input).toList()
136+
args.forEach {
137+
val argPosition = it.groupValues.last()
138+
input = input.replace(it.value, "\${arg$argPosition}")
139+
}
140+
141+
val nums = regexNumArgs.findAll(input).toList()
142+
nums.forEach {
143+
val argPosition = it.groupValues.last()
144+
input = input.replace(it.value, "\${num$argPosition}")
145+
}
146+
147+
replacementMap.forEach { (key, value) ->
148+
input = input.replace(key, value)
148149
}
149150
return input
150151
}

0 commit comments

Comments
 (0)