Brace expansion: Difference between revisions
Content added Content deleted
Line 1,897: | Line 1,897: | ||
ok rosetta_code/Brace_expansion 3.347s |
ok rosetta_code/Brace_expansion 3.347s |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
|||
{{trans|Java}} |
|||
<lang groovy>class BraceExpansion { |
|||
static void main(String[] args) { |
|||
for (String s : [ |
|||
"It{{em,alic}iz,erat}e{d,}, please.", |
|||
"~/{Downloads,Pictures}/*.{jpg,gif,png}", |
|||
"{,{,gotta have{ ,\\, again\\, }}more }cowbell!", |
|||
"{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}" |
|||
]) { |
|||
println() |
|||
expand(s) |
|||
} |
|||
} |
|||
static void expand(String s) { |
|||
expandR("", s, "") |
|||
} |
|||
private static void expandR(String pre, String s, String suf) { |
|||
int i1 = -1, i2 = 0 |
|||
String noEscape = s.replaceAll("([\\\\]{2}|[\\\\][,}{])", " ") |
|||
StringBuilder sb = null |
|||
outer: |
|||
while ((i1 = noEscape.indexOf('{', i1 + 1)) != -1) { |
|||
i2 = i1 + 1 |
|||
sb = new StringBuilder(s) |
|||
for (int depth = 1; i2 < s.length() && depth > 0; i2++) { |
|||
char c = noEscape.charAt(i2) |
|||
depth = (c == ('{' as char)) ? ++depth : depth |
|||
depth = (c == ('}' as char)) ? --depth : depth |
|||
if (c == (',' as char) && depth == 1) { |
|||
sb.setCharAt(i2, '\u0000' as char) |
|||
} else if (c == ('}' as char) && depth == 0 && sb.indexOf("\u0000") != -1) { |
|||
break outer |
|||
} |
|||
} |
|||
} |
|||
if (i1 == -1) { |
|||
if (suf.length() > 0) { |
|||
expandR(pre + s, suf, "") |
|||
} else { |
|||
printf("%s%s%s%n", pre, s, suf) |
|||
} |
|||
} else { |
|||
for (String m : sb.substring(i1 + 1, i2).split("\u0000", -1)) { |
|||
expandR(pre + s.substring(0, i1), m, s.substring(i2 + 1) + suf) |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Itemized, please. |
|||
Itemize, please. |
|||
Italicized, please. |
|||
Italicize, please. |
|||
Iterated, please. |
|||
Iterate, please. |
|||
~/Downloads/*.jpg |
|||
~/Downloads/*.gif |
|||
~/Downloads/*.png |
|||
~/Pictures/*.jpg |
|||
~/Pictures/*.gif |
|||
~/Pictures/*.png |
|||
cowbell! |
|||
more cowbell! |
|||
gotta have more cowbell! |
|||
gotta have\, again\, more cowbell! |
|||
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\} |
|||
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |