Jump to content

Brace expansion: Difference between revisions

→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(Added Kotlin)
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 1,663:
=={{header|Kotlin}}==
{{trans|Java}}
<lang scala>// version 1.1.12
 
object BraceExpansion {
Line 1,669:
 
private val r = Regex("""([\\]{2}|[\\][,}{])""")
 
private fun expandR(pre: String, s: String, suf: String) {
val noEscape = s.replace(r, " ")
var sb = StringBuilder("")
var i1 = noEscape.indexOf('{')
var i2 = 0
 
outer@ while (i1 != -1) {
Line 1,682:
while (i2 < s.length && depth > 0) {
val c = noEscape[i2]
if (c == '{')
depth++
else if (c == '}')
depth--
if (c == ',' && depth == 1)
sb[i2] = '\u0000'
else if (c == '}' && depth == 0 && sb.indexOf("\u0000") != -1)
Line 1,693:
}
i1 = noEscape.indexOf('{', i1 + 1)
}
if (i1 == -1) {
if (suf.length > 0)
Line 1,699:
else
println("$pre$s$suf")
}
else {
for (m in sb.substring(i1 + 1, i2).split('\u0000'))
Line 1,708:
 
fun main(args: Array<String>) {
val strings = arrayOf(
"""~/{Downloads,Pictures}/*.{jpg,gif,png}""",
"""It{{em,alic}iz,erat}e{d,}, please.""",
Cookies help us deliver our services. By using our services, you agree to our use of cookies.