File tree Expand file tree Collapse file tree 2 files changed +48
-4
lines changed
components/resources/library/src
commonMain/kotlin/org/jetbrains/compose/resources
commonTest/kotlin/org/jetbrains/compose/resources Expand file tree Collapse file tree 2 files changed +48
-4
lines changed Original file line number Diff line number Diff line change @@ -4,9 +4,23 @@ import org.jetbrains.compose.resources.plural.PluralCategory
44import kotlin.io.encoding.Base64
55import kotlin.io.encoding.ExperimentalEncodingApi
66
7- private val SimpleStringFormatRegex = Regex (""" %(\d+)\$[ds]""" )
8- internal fun String.replaceWithArgs (args : List <String >) = SimpleStringFormatRegex .replace(this ) { matchResult ->
9- args[matchResult.groupValues[1 ].toInt() - 1 ]
7+ private val SimpleStringFormatRegex = Regex (""" %(?:([1-9]\d*)\$)?[ds]""" )
8+ internal fun String.replaceWithArgs (args : List <String >): String {
9+ if (! SimpleStringFormatRegex .containsMatchIn(this )) return this
10+
11+ return SimpleStringFormatRegex .replace(this ) { match ->
12+ val placeholderNumber = match.groups[1 ]?.value?.toIntOrNull()
13+ val index = when {
14+ placeholderNumber != null -> placeholderNumber - 1
15+ args.size == 1 -> 0
16+ else -> {
17+ throw IllegalArgumentException (
18+ " Formatting failed: Non-positional placeholder '${match.value} ' is ambiguous when multiple arguments are provided in \" $this \" "
19+ )
20+ }
21+ }
22+ args[index]
23+ }
1024}
1125
1226internal sealed interface StringItem {
Original file line number Diff line number Diff line change @@ -180,4 +180,34 @@ class StringFormatTest {
180180 // Only the first argument should be used, ignoring the rest
181181 assertEquals(" Hello Alice!" , result)
182182 }
183- }
183+
184+ @Test
185+ fun `replaceWithArgs handle single argument format` () {
186+ val template = " Hello %s!"
187+ val args = listOf (" Alice" )
188+
189+ val result = template.replaceWithArgs(args)
190+
191+ assertEquals(" Hello Alice!" , result)
192+ }
193+
194+ @Test
195+ fun `replaceWithArgs handle multiple placeholders for single argument` () {
196+ val template = " %s and %s are best friends!"
197+ val args = listOf (" Alice" )
198+
199+ val result = template.replaceWithArgs(args)
200+
201+ assertEquals(" Alice and Alice are best friends!" , result)
202+ }
203+
204+ @Test
205+ fun `replaceWithArgs throw exception when multiple arguments with single placeholder format` () {
206+ val template = " Hello %s, you have %d new messages!"
207+ val args = listOf (" Alice" , " 15" )
208+
209+ assertFailsWith<IllegalArgumentException > {
210+ template.replaceWithArgs(args)
211+ }
212+ }
213+ }
You can’t perform that action at this time.
0 commit comments