Brace expansion: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(21 intermediate revisions by 12 users not shown)
Line 4:
 
 
;Task
{{task heading}}
 
Write a function that can perform brace expansion on any input string, according to the following specification.<br>
Line 273:
:* &nbsp; [[Brace_expansion_using_ranges]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang="11l">F getitem(=s, depth = 0)
V out = [‘’]
L s != ‘’
Line 313 ⟶ 312:
R ([‘’] * 0, ‘’)
 
L(s) |‘~/{Downloads,Pictures}/*.{jpg,gif,png}
It{{em,alic}iz,erat}e{d,}, please.
{,{,gotta have{ ,\, again\, }}more }cowbell!
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}’.split("\n")
print(([s] [+] getitem(s)[0]).join("\n\t")"\n")</langsyntaxhighlight>
{{out}}
<pre>
Line 332 ⟶ 331:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|AppleScript}}==
 
This may seem like a cheat, but it ''is'' a legitimate and obvious way to tackle the problem with AppleScript in the unlikely event that the need should arise. It adjusts the escape levels in the input text, feeds it as a shell script to macOS's sh shell, and lets ''it'' handle the expansion.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
Line 372 ⟶ 370:
set AppleScript's text item delimiters to astid
 
log output -- To see the result without the backslash-escaping.</langsyntaxhighlight>
 
{{output}}
Line 393 ⟶ 391:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}*)</pre>
 
=={{header|AutoHotkey}}==
{{incorrect|AutoHotkey|Output for edge cases is wrong.}}
 
<syntaxhighlight lang="autohotkey">;This one is a lot more simpler than the rest
<lang autohotkey>; AutoHotkey runs Perl-compatible regular expressions no problem
BraceExp(string, del:="`n") {
t=~/{Downloads,Pictures}/ *.{jpg,gif,png} ; Note I added a space so the RosettaCode syntax highlighting will work. The AutoHotkey interpreter handles it no problem.
Loop, Parse, string
msgbox % expand(t)
if (A_LoopField = "{")
t=It{{em,alic}iz,erat}e{d,}, please.
break
msgbox % expand(t)
else
t={,{,gotta have{ ,\, again\, }}more }cowbell!
substring .= A_LoopField
msgbox % expand(t)
substr := SubStr(string, InStr(string, "{")+1, InStr(string, "}")-InStr(string, "{")-1)
t={}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}
Loop, Parse, substr, `,
msgbox % expand(t)
toreturn .= substring . A_LoopField . del
return toreturn
}
 
Msgbox, % BraceExp("enable_{video,audio}")
expand(t) {
Msgbox, % BraceExp("apple {bush,tree}")
t:=RegExReplace(t, "\\\\", "\!") ; if you want to use these character combinations literally in your string, just switch these susbstitutions to something unicode
Msgbox, % BraceExp("h{i,ello}")
,t:=RegExReplace(t, "\\,", "\#")
Msgbox, % BraceExp("rosetta{code,stone}")</syntaxhighlight>
,t:=RegExReplace(t, "\\\{", "\@")
,t:=RegExReplace(t, "\\\}", "\&")
a=1
While a or b
; expand (braces with multiple commas) which are (apparently inside something else)
t:=RegExReplace(t, "Sxm)^(.*) ([{,]) ([^{},]*) \{ ([^{},]*) , ([^{},]* , [^{}]*) \} ([^{},]*?) ([},]) (.*)$", "$1$2$3$4$6,$3{$5}$6$7$8", a)
; expand (braces with single commas) which are (apparently inside something else)
,t:=RegExReplace(t, "Sxm)^(.*) ([{,]) ([^{},]*) \{ ([^{},]*) , ([^{},]*) \} ([^{},]*?) ([},]) (.*)$", "$1$2$3$4$6,$3$5$6$7$8", b)
a=1
While a or b
; expand braces with single commas
t:=RegExReplace(t, "Sxm)^(.*) \{ ([^{},]*) , ([^{},]*) \} (.*)$", "$1$2$4`r`n$1$3$4", a)
; expand braces with multiple commas
,t:=RegExReplace(t, "Sxm)^(.*) \{ ([^{},]*) , ([^{},]* , [^{}]*) \} (.*)$", "$1$2$4`r`n$1{$3}$4", b)
t:=RegExReplace(t, "\\!", "\\")
,t:=RegExReplace(t, "\\#", "\,")
,t:=RegExReplace(t, "\\@", "\{")
,t:=RegExReplace(t, "\\&", "\}")
Return t
}</lang>
{{out}}
<pre>~/Downloads/*.jpgenable_video
enable_audio
~/Downloads/*.gif
~/Downloads/*.png
~/Pictures/*.jpg
~/Pictures/*.gif
~/Pictures/*.png
 
apple bush
Itemized, please.
apple tree
Itemize, please.
Italicized, please.
Italicize, please.
Iterated, please.
Iterate, please.
 
hi
cowbell!
hello
more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
 
{}} some {\\edge }{ cases, here\\\}
{}} some {\\edgy }{ cases, here\\\}</pre>
 
rosettacode
rosettastone</pre>
=={{header|C}}==
Handles only properly formed input.
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 765 ⟶ 734:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Pattern: ~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 788 ⟶ 757:
gotta have more cowbell!
gotta have\, again\, more cowbell!</pre>
=={{header|C sharp|C#}}==
 
=={{header|C++}}==
C++11 solution:
 
<lang cpp>#include <iostream>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
 
namespace detail {
 
template <typename ForwardIterator>
class tokenizer
{
ForwardIterator _tbegin, _tend, _end;
public:
tokenizer(ForwardIterator begin, ForwardIterator end)
: _tbegin(begin), _tend(begin), _end(end)
{ }
template <typename Lambda>
bool next(Lambda istoken)
{
if (_tbegin == _end) {
return false;
}
_tbegin = _tend;
for (; _tend != _end && !istoken(*_tend); ++_tend) {
if (*_tend == '\\' && std::next(_tend) != _end) {
++_tend;
}
}
if (_tend == _tbegin) {
_tend++;
}
return _tbegin != _end;
}
ForwardIterator begin() const { return _tbegin; }
ForwardIterator end() const { return _tend; }
bool operator==(char c) { return *_tbegin == c; }
};
 
template <typename List>
void append_all(List & lista, const List & listb)
{
if (listb.size() == 1) {
for (auto & a : lista) {
a += listb.back();
}
} else {
List tmp;
for (auto & a : lista) {
for (auto & b : listb) {
tmp.push_back(a + b);
}
}
lista = std::move(tmp);
}
}
 
template <typename String, typename List, typename Tokenizer>
List expand(Tokenizer & token)
{
std::vector<List> alts{ { String() } };
while (token.next([](char c) { return c == '{' || c == ',' || c == '}'; })) {
if (token == '{') {
append_all(alts.back(), expand<String, List>(token));
} else if (token == ',') {
alts.push_back({ String() });
} else if (token == '}') {
if (alts.size() == 1) {
for (auto & a : alts.back()) {
a = '{' + a + '}';
}
return alts.back();
} else {
for (std::size_t i = 1; i < alts.size(); i++) {
alts.front().insert(alts.front().end(),
std::make_move_iterator(std::begin(alts[i])),
std::make_move_iterator(std::end(alts[i])));
}
return std::move(alts.front());
}
} else {
for (auto & a : alts.back()) {
a.append(token.begin(), token.end());
}
}
}
List result{ String{ '{' } };
append_all(result, alts.front());
for (std::size_t i = 1; i < alts.size(); i++) {
for (auto & a : result) {
a += ',';
}
append_all(result, alts[i]);
}
return result;
}
 
} // namespace detail
 
template <
typename ForwardIterator,
typename String = std::basic_string<
typename std::iterator_traits<ForwardIterator>::value_type
>,
typename List = std::vector<String>
>
List expand(ForwardIterator begin, ForwardIterator end)
{
detail::tokenizer<ForwardIterator> token(begin, end);
List list{ String() };
while (token.next([](char c) { return c == '{'; })) {
if (token == '{') {
detail::append_all(list, detail::expand<String, List>(token));
} else {
for (auto & a : list) {
a.append(token.begin(), token.end());
}
}
}
return list;
}
 
template <
typename Range,
typename String = std::basic_string<typename Range::value_type>,
typename List = std::vector<String>
>
List expand(const Range & range)
{
using Iterator = typename Range::const_iterator;
return expand<Iterator, String, List>(std::begin(range), std::end(range));
}
 
int main()
{
for (std::string string : {
R"(~/{Downloads,Pictures}/*.{jpg,gif,png})",
R"(It{{em,alic}iz,erat}e{d,}, please.)",
R"({,{,gotta have{ ,\, again\, }}more }cowbell!)",
R"({}} some {\\{edge,edgy} }{ cases, here\\\})",
R"(a{b{1,2}c)",
R"(a{1,2}b}c)",
R"(a{1,{2},3}b)",
R"(a{b{1,2}c{}})",
R"(more{ darn{ cowbell,},})",
R"(ab{c,d\,e{f,g\h},i\,j{k,l\,m}n,o\,p}qr)",
R"({a,{\,b}c)",
R"(a{b,{{c}})",
R"({a{\}b,c}d)",
R"({a,b{{1,2}e}f)",
R"({}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\})",
R"({{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{)",
}) {
std::cout << string << '\n';
for (auto expansion : expand(string)) {
std::cout << " " << expansion << '\n';
}
std::cout << '\n';
}
return 0;
}</lang>
 
=={{header|C sharp}}==
This approach turns the string into a tree structure, with Tokens that are either text, a concatenation or an alteration.
{{works with|C sharp|8}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections.Generic;
Line 1,125 ⟶ 916:
return result;
}
}</langsyntaxhighlight>
 
{{trans|Python}}
<langsyntaxhighlight lang="csharp">public static class BraceExpansion
{
const char L = '{', R = '}', S = ',';
Line 1,176 ⟶ 967:
return (null, "");
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,200 ⟶ 991:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}</pre>
=={{header|C++}}==
C++11 solution:
 
<syntaxhighlight lang="cpp">#include <iostream>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
 
namespace detail {
 
template <typename ForwardIterator>
class tokenizer
{
ForwardIterator _tbegin, _tend, _end;
public:
tokenizer(ForwardIterator begin, ForwardIterator end)
: _tbegin(begin), _tend(begin), _end(end)
{ }
template <typename Lambda>
bool next(Lambda istoken)
{
if (_tbegin == _end) {
return false;
}
_tbegin = _tend;
for (; _tend != _end && !istoken(*_tend); ++_tend) {
if (*_tend == '\\' && std::next(_tend) != _end) {
++_tend;
}
}
if (_tend == _tbegin) {
_tend++;
}
return _tbegin != _end;
}
ForwardIterator begin() const { return _tbegin; }
ForwardIterator end() const { return _tend; }
bool operator==(char c) { return *_tbegin == c; }
};
 
template <typename List>
void append_all(List & lista, const List & listb)
{
if (listb.size() == 1) {
for (auto & a : lista) {
a += listb.back();
}
} else {
List tmp;
for (auto & a : lista) {
for (auto & b : listb) {
tmp.push_back(a + b);
}
}
lista = std::move(tmp);
}
}
 
template <typename String, typename List, typename Tokenizer>
List expand(Tokenizer & token)
{
std::vector<List> alts{ { String() } };
while (token.next([](char c) { return c == '{' || c == ',' || c == '}'; })) {
if (token == '{') {
append_all(alts.back(), expand<String, List>(token));
} else if (token == ',') {
alts.push_back({ String() });
} else if (token == '}') {
if (alts.size() == 1) {
for (auto & a : alts.back()) {
a = '{' + a + '}';
}
return alts.back();
} else {
for (std::size_t i = 1; i < alts.size(); i++) {
alts.front().insert(alts.front().end(),
std::make_move_iterator(std::begin(alts[i])),
std::make_move_iterator(std::end(alts[i])));
}
return std::move(alts.front());
}
} else {
for (auto & a : alts.back()) {
a.append(token.begin(), token.end());
}
}
}
List result{ String{ '{' } };
append_all(result, alts.front());
for (std::size_t i = 1; i < alts.size(); i++) {
for (auto & a : result) {
a += ',';
}
append_all(result, alts[i]);
}
return result;
}
 
} // namespace detail
 
template <
typename ForwardIterator,
typename String = std::basic_string<
typename std::iterator_traits<ForwardIterator>::value_type
>,
typename List = std::vector<String>
>
List expand(ForwardIterator begin, ForwardIterator end)
{
detail::tokenizer<ForwardIterator> token(begin, end);
List list{ String() };
while (token.next([](char c) { return c == '{'; })) {
if (token == '{') {
detail::append_all(list, detail::expand<String, List>(token));
} else {
for (auto & a : list) {
a.append(token.begin(), token.end());
}
}
}
return list;
}
 
template <
typename Range,
typename String = std::basic_string<typename Range::value_type>,
typename List = std::vector<String>
>
List expand(const Range & range)
{
using Iterator = typename Range::const_iterator;
return expand<Iterator, String, List>(std::begin(range), std::end(range));
}
 
int main()
{
for (std::string string : {
R"(~/{Downloads,Pictures}/*.{jpg,gif,png})",
R"(It{{em,alic}iz,erat}e{d,}, please.)",
R"({,{,gotta have{ ,\, again\, }}more }cowbell!)",
R"({}} some {\\{edge,edgy} }{ cases, here\\\})",
R"(a{b{1,2}c)",
R"(a{1,2}b}c)",
R"(a{1,{2},3}b)",
R"(a{b{1,2}c{}})",
R"(more{ darn{ cowbell,},})",
R"(ab{c,d\,e{f,g\h},i\,j{k,l\,m}n,o\,p}qr)",
R"({a,{\,b}c)",
R"(a{b,{{c}})",
R"({a{\}b,c}d)",
R"({a,b{{1,2}e}f)",
R"({}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\})",
R"({{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{)",
}) {
std::cout << string << '\n';
for (auto expansion : expand(string)) {
std::cout << " " << expansion << '\n';
}
std::cout << '\n';
}
return 0;
}</syntaxhighlight>
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defstruct alternation
(alternatives nil :type list))
 
Line 1,287 ⟶ 1,253:
(dolist (output (expand input))
(format t " ~A~%" output))
(terpri)))</langsyntaxhighlight>
{{out}}
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 1,315 ⟶ 1,281:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|D}}==
{{trans|Python}}
This code is not UTF-corrected, because it uses slicing instead of front, popFront, etc.
<langsyntaxhighlight lang="d">import std.stdio, std.typecons, std.array, std.range, std.algorithm, std.string;
 
Nullable!(Tuple!(string[], string)) getGroup(string s, in uint depth)
Line 1,399 ⟶ 1,364:
foreach (const s; testCases.splitLines)
writefln("%s\n%-( %s\n%)\n", s, s.getItems[0]);
}</langsyntaxhighlight>
{{out}}
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 1,471 ⟶ 1,436:
{a,b{2e}f
</pre>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Brace_expansion do
def getitem(s), do: getitem(String.codepoints(s), 0, [""])
Line 1,525 ⟶ 1,489:
|> Enum.each(fn str -> IO.puts "\t#{str}" end)
IO.puts ""
end)</langsyntaxhighlight>
 
{{out}}
Line 1,555 ⟶ 1,519:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Go}}==
<code>expand.go</code>:
<langsyntaxhighlight lang="go">package expand
 
// Expander is anything that can be expanded into a slice of strings.
Line 1,759 ⟶ 1,722:
alt = append(alt, sub)
return alt
}</langsyntaxhighlight>
<code>expand_test.go</code>
<langsyntaxhighlight lang="go">package expand
 
import (
Line 1,844 ⟶ 1,807:
Expand(input)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,899 ⟶ 1,862:
ok rosetta_code/Brace_expansion 3.347s
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class BraceExpansion {
static void main(String[] args) {
for (String s : [
Line 1,951 ⟶ 1,913:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Itemized, please.
Line 1,974 ⟶ 1,936:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}</pre>
 
=={{header|Haskell}}==
[http://www.reddit.com/r/readablecode/comments/1w6exe/p6_crosswalk_braceexpansionparses/cf229at "Here is a direct translation to Haskell using parsec"] (of [http://rosettacode.org/mw/index.php?title=Brace_expansion&oldid=175567#Raku an earlier version of the Raku solution]):
 
<langsyntaxhighlight lang="haskell">import qualified Text.Parsec as P
 
showExpansion :: String -> String
Line 2,021 ⟶ 1,982:
, "{,{,gotta have{ ,\\, again\\, }}more }cowbell!"
, "{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}"
]</langsyntaxhighlight>
{{out}}
<pre>It{{em,alic}iz,erat}e{d,}, please.
Line 2,044 ⟶ 2,005:
{}} some }{\\ edge \,{ cases, {here} \\\\\}
{}} some }{\\ edge \,{ cases, {here} \\\\\}</pre>
 
=={{header|J}}==
 
Implementation:
 
<syntaxhighlight lang="j">
<lang J>
NB. legit { , and } do not follow a legit backslash:
legit=: 1,_1}.4>(3;(_2[\"1".;._2]0 :0);('\';a.);0 _1 0 1)&;:&.(' '&,)
Line 2,091 ⟶ 2,051:
options=. }:mask <;._1 y
prefix,each options,each suffix
)</langsyntaxhighlight>
 
Examples:
 
<langsyntaxhighlight Jlang="j"> >expand t1
~/Downloads/*.jpg
~/Downloads/*.gif
Line 2,116 ⟶ 2,076:
>expand t4
{}} some {\\edge }{ cases, here\\\}
{}} some {\\edgy }{ cases, here\\\}</langsyntaxhighlight>
 
Explanation:
Line 2,125 ⟶ 2,085:
 
Finally, for each integer that we've used to mark delimiter locations, split out each of the marked options (each with a copy of that group's prefix and suffix). (Then when all that is done, take the absolute values convert back to unicode for the final result.)
 
=={{header|Java}}==
Should be able to handle all printable Unicode.
<langsyntaxhighlight lang="java">public class BraceExpansion {
 
public static void main(String[] args) {
Line 2,173 ⟶ 2,132:
}
}
}</langsyntaxhighlight>
 
<pre>Itemized, please.
Line 2,196 ⟶ 2,155:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}</pre>
 
=={{header|JavaScript}}==
 
Line 2,207 ⟶ 2,165:
Each node of the parse tree consists of one of two simple functions (AND: syntagmatic concatenation, OR: flattening of paradigms) with a set of arguments, each of which may be a plain string or an AND or OR subtree. The expansions are derived by evaluating the parse tree as an expression.
 
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
'use strict'
 
Line 2,397 ⟶ 2,355:
}).join('\n\n');
 
})();</langsyntaxhighlight>
 
Value returned by function:
Line 2,433 ⟶ 2,391:
Sample of parse trees logged to the console:
 
<syntaxhighlight lang="javascript">{
<lang JavaScript>{
"fn": "[function and]",
"args": [
Line 2,467 ⟶ 2,425:
" please."
]
}</langsyntaxhighlight>
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
 
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
<syntaxhighlight lang=jq>
# Input: a string
# Emit an array of the expansions
def expand:
 
# Emit [ array, string ]
def getItem($depth):
 
def getGroup($depth):
{ out: [], comma: false, s: . }
| until (.s == "" or .return;
(.s | getItem($depth)) as $t
| $t[0] as $g
| .s = $t[1]
| if .s == "" then .return = [[], ""]
else .out += $g
| if .s[0:1] == "}"
then if .comma then .return = [.out, .s[1:]]
else .return = [ (.out | map( "{" + . + "}" )), .s[1:]]
end
else if .s[0:1] == ","
then .comma = true
| .s |= .[1:]
else .
end
end
end)
| if .return then .return else [[], ""] end ;
 
{ out: [""], s: .}
| until( (.s == "") or .return;
.c = .s[0:1]
| if ($depth > 0) and (.c == "," or .c == "}")
then .return = [.out, .s]
else .cont = false
| if .c == "{"
then (.s[1:] | getGroup($depth+1)) as $x
| if $x[0] | length > 0
# conform to the "lexicographic" ordering requirement
then .out |= [ .[] as $o | $o + $x[0][] ]
| .s = $x[1]
| .cont = true
else .
end
else .
end
end
| if (.cont | not)
then if (.c == "\\") and ((.s|length) > 1)
then .c += .s[1:2]
| .s |= .[1:]
else .
end
| .out = [.out[] + .c]
| .s |= .[1:]
else .
end )
| if .return then .return else [.out, .s] end ;
 
getItem(0)[0];
def inputs: [
"~/{Downloads,Pictures}/*.{jpg,gif,png}",
"It{{em,alic}iz,erat}e{d,}, please.",
"{,{,gotta have{ ,\\, again\\, }}more }cowbell!",
"{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}"
];
 
inputs[]
| "\n",
.,
" " + expand[]
</syntaxhighlight>
{{output}}
<pre>
~/{Downloads,Pictures}/*.{jpg,gif,png}
~/Downloads/*.jpg
~/Downloads/*.gif
~/Downloads/*.png
~/Pictures/*.jpg
~/Pictures/*.gif
~/Pictures/*.png
 
 
It{{em,alic}iz,erat}e{d,}, please.
Itemized, please.
Itemize, please.
Italicized, please.
Italicize, please.
Iterated, please.
Iterate, please.
 
 
{,{,gotta have{ ,\, again\, }}more }cowbell!
cowbell!
more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
 
 
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Julia}}==
{{trans|Python}} <langsyntaxhighlight lang="julia"> function getitem(s, depth=0)
out = [""]
while s != ""
Line 2,521 ⟶ 2,589:
println(ans)
end
end </langsyntaxhighlight> {{output}} <pre>
~/{Downloads,Pictures}/*.{jpg,gif,png}
--------------------------------------------
Line 2,555 ⟶ 2,623:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
object BraceExpansion {
Line 2,606 ⟶ 2,674:
BraceExpansion.expand(s)
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,632 ⟶ 2,700:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
=={{header|Lua}}==
 
{{trans|Python}}
 
Note that this code isn't very memory efficient.
 
<syntaxhighlight lang="lua">local function wrapEachItem(items, prefix, suffix)
local itemsWrapped = {}
 
for i, item in ipairs(items) do
itemsWrapped[i] = prefix .. item .. suffix
end
 
return itemsWrapped
end
 
local function getAllItemCombinationsConcatenated(aItems, bItems)
local combinations = {}
 
for _, a in ipairs(aItems) do
for _, b in ipairs(bItems) do
table.insert(combinations, a..b)
end
end
 
return combinations
end
 
local getItems -- Forward declaration.
 
local function getGroup(s, pos, depth)
local groupItems = {}
local foundComma = false
 
while pos <= #s do
local items
items, pos = getItems(s, pos, depth)
if pos > #s then break end
 
for _, item in ipairs(items) do
table.insert(groupItems, item)
end
 
local c = s:sub(pos, pos)
 
if c == "}" then -- Possibly end of group.
if foundComma then return groupItems, pos+1 end
return wrapEachItem(groupItems, "{", "}"), pos+1 -- No group.
 
elseif c == "," then
foundComma, pos = true, pos+1
end
end
 
return nil -- No group.
end
 
function getItems(s, pos, depth)
local items = {""}
 
while pos <= #s do
local c = s:sub(pos, pos)
 
if depth > 0 and (c == "," or c == "}") then -- End of item in surrounding group.
return items, pos
end
 
local groupItems, nextPos = nil
if c == "{" then -- Possibly start of a group.
groupItems, nextPos = getGroup(s, pos+1, depth+1)
end
 
if groupItems then
items, pos = getAllItemCombinationsConcatenated(items, groupItems), nextPos
else
if c == "\\" and pos < #s then -- Escaped character.
pos = pos + 1
c = c .. s:sub(pos, pos)
end
items, pos = wrapEachItem(items, "", c), pos+1
end
end
 
return items, pos
end
 
local tests = [[
~/{Downloads,Pictures}/*.{jpg,gif,png}
It{{em,alic}iz,erat}e{d,}, please.
{,{,gotta have{ ,\, again\, }}more }cowbell!
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}
]]
 
for test in tests:gmatch"[^\n]+" do
print(test)
for _, item in ipairs(getItems(test, 1, 0)) do
print("\t"..item)
end
print()
end</syntaxhighlight>
 
{{out}}
<pre>
~/{Downloads,Pictures}/*.{jpg,gif,png}
~/Downloads/*.jpg
~/Downloads/*.gif
~/Downloads/*.png
~/Pictures/*.jpg
~/Pictures/*.gif
~/Pictures/*.png
...
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">(*The strategy is to first capture all special sub-expressions and reformat them so they are semantically clear. The built in function Distribute could then do the work of creating the alternatives, but the order wouldn't match that given in the instructions (although as a set the alternatives would be correct). I'll take a more complicated route so as to follow the instructions exactly.*)
 
(*A few named constants for readability.*)
EscapeToken="\\";(*In Mathematica, backslash is an escape character when inputing a string, so we need to escape it.*)
LeftBraceToken="{";
RightBraceToken="}";
 
(*This basically sequesters escaped substrings so that they don't get matched during later processing.*)
CaptureEscapes[exp:{___String}]:=SequenceReplace[exp,{EscapeToken,x_}:>EscapeToken<>x];
 
(*Any remaining braces are un-escaped. I'm "unstringifying" them to more easily pick them out during later processing.*)
CaptureBraces[exp:{___String}]:=ReplaceAll[exp,{LeftBraceToken->LeftBrace,RightBraceToken->RightBrace}];
 
(*Building up trees for the braced expressions. Extra braces are just raw data, so transform them back to strings.*)
CaptureBraceTrees[exp:{(_String|LeftBrace|RightBrace)...}]:=ReplaceAll[FixedPoint[SequenceReplace[{LeftBrace,seq:(_String|_BraceTree)...,RightBrace}:>BraceTree[seq]],exp],{LeftBrace->LeftBraceToken,RightBrace->RightBraceToken}];
 
(*At thie point, we should have an expression with well-braced substructures representing potential alternatives. We must expand brace trees to alternatives in the correct order.*)
ExpandBraceTrees[exp:Expr[head___String,bt_BraceTree,tail___]]:=ReplaceAll[Thread[Expr[head,ToAlternatives[bt],tail]],alt_Alt:>Sequence@@alt];
ExpandBraceTrees[exp:Expr[___String]]:={exp};
ExpandBraceTrees[exps:{__Expr}]:=Catenate[ExpandBraceTrees/@exps];
 
(*If there are no commas, then it's a literal sub-expression. Otherwise, it's a set of alternatives.*)
ToAlternatives[bt_BraceTree]:={LeftBraceToken<>StringJoin@@bt<>RightBraceToken}/;FreeQ[bt,","];
ToAlternatives[BraceTree[","]]=ToAlternatives[BraceTree["",",",""]];
ToAlternatives[bt:BraceTree[",",__]]:=ToAlternatives[Prepend[bt,""]];
ToAlternatives[bt:BraceTree[__,","]]:=ToAlternatives[Append[bt,""]];
ToAlternatives[bt_BraceTree]:=Alt@@@SequenceSplit[List@@bt,{","}];
 
NormalizeExpression=Apply[Expr]@*CaptureBraceTrees@*CaptureBraces@*CaptureEscapes@*Characters;
 
BraceExpand[str_String]:=ReplaceAll[FixedPoint[ExpandBraceTrees,NormalizeExpression[str]],Expr->StringJoin];
 
(*Data was stored in a local file.*)
BraceTestData=ReadList[FileNameJoin[{NotebookDirectory[],"BraceTestData.txt"}],String];BraceTestData//TableForm</syntaxhighlight>
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
It{{em,alic}iz,erat}e{d,}, please.
{,{,gotta have{ ,\, again\, }}more }cowbell!
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}</pre>
 
{{out}}
<pre>Column[Column /@ BraceExpand /@ BraceTestData, Left, 2]</pre>
<pre>~/Downloads/*.jpg
~/Downloads/*.gif
~/Downloads/*.png
~/Pictures/*.jpg
~/Pictures/*.gif
~/Pictures/*.png
 
 
Itemized, please.
Itemize, please.
Italicized, please.
Italicize, please.
Iterated, please.
Iterate, please.
 
 
cowbell!
more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
 
 
{}} some }{,\\ edge \,{ cases, {here} \\\\\}
{}} some }{,\\ edge \,{ cases, {here} \\\\\}</pre>
=={{header|Nim}}==
{{trans|Seed7}}
<langsyntaxhighlight Nimlang="nim">proc expandBraces(str: string) =
 
var
Line 2,682 ⟶ 2,930:
"{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}"]:
echo "\nExpansions of \"", str, "\":"
expandBraces(str)</langsyntaxhighlight>
 
{{out}}
Line 2,711 ⟶ 2,959:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}</pre>
 
=={{header|Perl}}==
 
Line 2,719 ⟶ 2,966:
So here is a manual solution that implements the specification precisely:
 
<langsyntaxhighlight lang="perl">sub brace_expand {
my $input = shift;
my @stack = ([my $current = ['']]);
Line 2,759 ⟶ 3,006:
return @$current;
}</langsyntaxhighlight>
 
Usage demonstration:
<langsyntaxhighlight lang="perl">while (my $input = <DATA>) {
chomp($input);
print "$input\n";
Line 2,773 ⟶ 3,020:
It{{em,alic}iz,erat}e{d,}, please.
{,{,gotta have{ ,\, again\, }}more }cowbell!
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,803 ⟶ 3,050:
 
</pre>
 
=={{header|Phix}}==
Fairly straightforward recursive solution
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>--
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Brace_expansion.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
-- ================================
<span style="color: #008080;">function</span> <span style="color: #000000;">pair</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">stems</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">brest</span><span style="color: #0000FF;">)</span>
--
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function pair(sequence stems, sequence brest)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stems</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence res = {}
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brest</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=1 to length(stems) do
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">stems</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">brest</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
for j=1 to length(brest) do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
res = append(res,stems[i]&brest[j])
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return res
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
function brarse(string s)
<span style="color: #008080;">while</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer idx = 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
while idx<=length(s) do
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'{'</span> <span style="color: #008080;">then</span>
integer ch = s[idx]
<span style="color: #004080;">sequence</span> <span style="color: #000000;">alts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">}</span>
if ch='{' then
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
sequence alts = {idx}
<span style="color: #004080;">integer</span> <span style="color: #000000;">l0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">idx</span>
idx += 1
<span style="color: #004080;">bool</span> <span style="color: #000000;">nest</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bl0</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
integer l0 = idx
<span style="color: #004080;">integer</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
bool nest = false
<span style="color: #008080;">while</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer level = 1
<span style="color: #008080;">switch</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
while idx<=length(s) do
<span style="color: #008080;">case</span> <span style="color: #008000;">'{'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
switch s[idx] do
<span style="color: #000000;">nest</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
case '{': level += 1
<span style="color: #008080;">case</span> <span style="color: #008000;">'}'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
nest = true
<span style="color: #000000;">bl0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">level</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
case '}': level -= 1
<span style="color: #008080;">case</span> <span style="color: #008000;">','</span><span style="color: #0000FF;">:</span> <span style="color: #008080;">if</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if level=0 then exit end if
<span style="color: #000000;">alts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">)</span>
case ',': if level=1 then
<span altsstyle="color: #008080;">end</span> <span style="color: append(alts,idx)#008080;">if</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'\\'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span case '\\'style="color: idx#008080;">end</span> +<span style="color: 1#008080;">switch</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bl0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end switch
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
idx += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alts</span><span style="color: #0000FF;">)></span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if length(alts)>1 and level=0 then
<span style="color: #000000;">alts</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">idx</span>
alts &= idx
<span style="color: #004080;">sequence</span> <span style="color: #000000;">stems</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
sequence stems = {}
<span style="color: #004080;">string</span> <span style="color: #000000;">stem</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">alts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
string stem = s[1..alts[1]-1]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alts</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=2 to length(alts) do
<span style="color: #004080;">string</span> <span style="color: #000000;">rest</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">alts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">alts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
string rest = s[alts[i-1]+1..alts[i]-1]
<span style="color: #008080;">if</span> <span style="color: #000000;">nest</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">inners</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rest</span><span style="color: #0000FF;">)</span>
sequence inners = brarse(rest)
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">inners</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for j=1 to length(inners) do
<span style="color: #000000;">stems</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stems</span><span style="color: #0000FF;">,</span><span style="color: #000000;">stem</span><span style="color: #0000FF;">&</span><span style="color: #000000;">inners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
stems = append(stems,stem&inners[j])
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">stems</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stems</span><span style="color: #0000FF;">,</span><span style="color: #000000;">stem</span><span style="color: #0000FF;">&</span><span style="color: #000000;">rest</span><span style="color: #0000FF;">)</span>
stems = append(stems,stem&rest)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">pair</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stems</span><span style="color: #0000FF;">,</span><span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]))</span>
return pair(stems,brarse(s[idx+1..$]))
<span style="color: #008080;">elsif</span> <span style="color: #000000;">nest</span> <span style="color: #008080;">then</span>
elsif nest then
<span style="color: #008080;">return</span> <span style="color: #000000;">pair</span><span style="color: #0000FF;">({</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l0</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]},</span><span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l0</span><span style="color: #0000FF;">..$]))</span>
return pair({s[1..l0-1]},brarse(s[l0..$]))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
idx += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end while
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">}</span>
return {s}
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
 
<span style="color: #000080;font-style:italic;">-- (since ? and pp() add their own backslash escapes:)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">edump</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
procedure edump(sequence x)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=1 to length(x) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
printf(1,"%s\n",{x[i]})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
end procedure
 
<span style="color: #000000;">edump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"~/{Downloads,Pictures}/*.{jpg,gif,png}"</span><span style="color: #0000FF;">))</span>
edump(brarse("~/{Downloads,Pictures}/*.{jpg,gif,png}"))
<span style="color: #000000;">edump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"It{{em,alic}iz,erat}e{d,}, please."</span><span style="color: #0000FF;">))</span>
edump(brarse("It{{em,alic}iz,erat}e{d,}, please."))
<span style="color: #000000;">edump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`{,{,gotta have{ ,\, again\, }}more }cowbell!`</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">edump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brarse</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}`</span><span style="color: #0000FF;">))</langspan>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
Line 2,900 ⟶ 3,147:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|PHP}}==
{{trans|Python}}
<langsyntaxhighlight lang="php">function getitem($s,$depth=0) {
$out = [''];
while ($s) {
Line 2,976 ⟶ 3,222:
printf(" %s\n", $expansion);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,005 ⟶ 3,251:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de braceExpand (Str)
(let Lst
(make
Line 3,038 ⟶ 3,283:
(if (pair (car Lst))
(mapcan recurse (car Lst))
(list (car Lst)) ) ) ) ) ) ) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">(test
(quote
"~/Downloads/*.jpg"
Line 3,072 ⟶ 3,317:
"{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}"
"{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}" )
(braceExpand "{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}") )</langsyntaxhighlight>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Expand-Braces ( [string]$String )
{
Line 3,132 ⟶ 3,376:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$TestStrings = @(
'It{{em,alic}iz,erat}e{d,}, please.'
Line 3,148 ⟶ 3,392:
Expand-Braces $String
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,181 ⟶ 3,425:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
sym(',', commalist) --> ['\\',','], !.
sym(H, Context) --> [H], { not(H = '{'; H = '}'), (Context = commalist -> not(H = ','); true) }.
syms([H|T], Context) --> sym(H, Context), !, syms(T, Context).
syms([], _) --> [].
symbol(Symbol, Context) --> syms(Syms,Context), {atom_chars(Symbol, Syms)}.
 
braces(Member) --> ['{'], commalist(List), ['}'], {length(List, Len), Len > 1, member(Member, List)}.
 
commalist([H|T]) --> sym_braces(H, commalist), [','], commalist(T).
commalist([H]) --> sym_braces(H, commalist).
 
sym_braces(String, Context) --> symbol(S1, Context), braces(S2), sym_braces(S3, Context), {atomics_to_string([S1,S2,S3],String)}.
sym_braces(String, Context) --> braces(S1), symbol(S2, Context), sym_braces(S3, Context), {atomics_to_string([S1,S2,S3],String)}.
sym_braces(String, Context) --> symbol(String, Context).
sym_braces(String, _) --> braces(String).
sym_braces(String, Context) --> ['{'], sym_braces(S2, Context), {atomics_to_string(['{',S2],String)}.
sym_braces(String, Context) --> ['}'], sym_braces(S2, Context), {atomics_to_string(['}',S2],String)}.
 
grammar(String) --> sym_braces(String, braces).
 
brace_expansion(In, Out) :- atom_chars(In, Chars), findall(Out,grammar(Out, Chars, []), List), list_to_set(List, Out).
</syntaxhighlight>
 
Testing:
<syntaxhighlight lang="prolog">
?- brace_expansion("~/{Downloads,Pictures}/*.{jpg,gif,png}", Out).
Out = ["~/Downloads/*.jpg","~/Downloads/*.gif","~/Downloads/*.png","~/Pictures/*.jpg","~/Pictures/*.gif","~/Pictures/*.png"].
 
?- brace_expansion("It{{em,alic}iz,erat}e{d,}, please.", Out).
Out = ["Itemized, please.", "Itemize, please.", "Iterated, please.", "Iterate, please.", "Italicized, please.", "Italicize, please."].
 
?- brace_expansion("{,{,gotta have{ ,\\, again\\, }}more }cowbell!", Out).
Out = ["cowbell!", "more cowbell!", "gotta have more cowbell!", "gotta have, again, more cowbell!"].
 
</syntaxhighlight>
=={{header|Python}}==
<langsyntaxhighlight lang="python">def getitem(s, depth=0):
out = [""]
while s:
Line 3,222 ⟶ 3,502:
{,{,gotta have{ ,\, again\, }}more }cowbell!
{}} some }{,{\\\\{ edge, edge} \,}{ cases, {here} \\\\\\\\\}'''.split('\n'):
print "\n\t".join([s] + getitem(s)[0]) + "\n"</langsyntaxhighlight>
 
{{out}}
Line 3,253 ⟶ 3,533:
 
</pre>
 
=={{header|Racket}}==
{{trans|Python}}
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/match)
(define (merge-lists as . bss)
Line 3,305 ⟶ 3,584:
))
(for ((s (in-list patterns)) #:when (printf "expand: ~a~%" s) (x (in-list (brace-expand s))))
(printf "\t~a~%" x)))</langsyntaxhighlight>
{{out}}
<pre>expand: ~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 3,329 ⟶ 3,608:
{}} some }{,{\\ edge ,}{ cases, {here} \\\\}
{}} some }{,{\\ edge ,}{ cases, {here} \\\\}</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 3,337 ⟶ 3,615:
 
On the other end, we recursively walk the parse tree returning expanded sublists, and we do the cartesian concatenation of sublists at each level by use of the <tt>X~</tt> operator, which is a "cross" metaoperator used on a simple <tt>~</tt> concatenation. As a list infix operator, <tt>X~</tt> does not care how many items are on either side, which is just what you want in this case, since some of the arguments are strings and some are lists. Here we use a fold or reduction form in square brackets to interpose the cross-concat between each value generated by the map, which returns a mixture of lists and literal strings. One other thing that might not be obvious: if we bind to the match variable, <tt>$/</tt>, we automatically get all the syntactic sugar for its submatches. In this case, <tt>$0</tt> is short for <tt>$/[0]</tt>, and represents all the submatches captured by 0th set of parens in either <tt>TOP</tt> or <tt>alt</tt>. <tt>$&lt;meta&gt;</tt> is likewise short for <tt>$/&lt;meta&gt;</tt>, and retrieves what was captured by that named submatch.
<syntaxhighlight lang="raku" perl6line>grammar BraceExpansion {
token TOP { ( <meta> | . )* }
token meta { '{' <alts> '}' | \\ . }
Line 3,379 ⟶ 3,657:
a{b,{{c}}
{a{\}b,c}d
END</langsyntaxhighlight>
{{out}}
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 3,442 ⟶ 3,720:
{a\}bd
{acd</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*- REXX --------------------------------------------------------------
* Brace expansion
* 26.07.2016
Line 3,651 ⟶ 3,928:
Say arg(1) /* show on screen */
Call lineout oid,arg(1) /* write to file */
Return</langsyntaxhighlight>
{{out}}
<pre>J:\>rexx braces
Line 3,698 ⟶ 3,975:
{}
1 {}</pre>
 
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def getitem(s, depth=0)
out = [""]
until s.empty?
Line 3,741 ⟶ 4,017:
puts getitem(s)[0].map{|str| "\t"+str}
puts
end</langsyntaxhighlight>
 
{{out}}
Line 3,771 ⟶ 4,047:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">const OPEN_CHAR: char = '{';
const CLOSE_CHAR: char = '}';
const SEPARATOR: char = ',';
Line 3,956 ⟶ 4,231:
println!("{}", line);
}
}</langsyntaxhighlight>
 
 
Line 3,991 ⟶ 4,266:
 
{{trans|Python}}
<langsyntaxhighlight lang="rust">
fn main() {
let input = "~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 4,072 ⟶ 4,347:
None
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,099 ⟶ 4,374:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Scala}}==
{{works with|Scala|2.11}}
<langsyntaxhighlight lang="scala">
import collection.mutable.ListBuffer
case class State(isChild: Boolean, alts: ListBuffer[String], rem: List[Char])
Line 4,133 ⟶ 4,407:
parseElem(State(false, ListBuffer(""), s.toList)).alts
}
</syntaxhighlight>
</lang>
Demonstrating:
<langsyntaxhighlight lang="scala">
println(expand("""~/{Downloads,Pictures}/*.{jpg,gif,png}""") mkString "\n")
println(expand("It{{em,alic}iz,erat}e{d,}, please.") mkString "\n")
println(expand("""{,{,gotta have{ ,\, again\, }}more }cowbell!""") mkString "\n")
println(expand("""{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}""") mkString "\n")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,162 ⟶ 4,436:
{}} some }{,\\ edge \,{ cases, {here} \\\\\}
</pre>
 
=={{header|Scheme}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
(define (parse-brackets str)
;; We parse the bracketed strings using an accumulator and a stack
Line 4,255 ⟶ 4,528:
(bracket-expand "It{{em,alic}iz,erat}e{d,}")
;; '("Ited" "Ite" "Itemed" "Iteme" "Italiced" "Italice" "Itized" "Itize" "Iterated" "Iterate")
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: expandBraces (in string: stri) is func
Line 4,317 ⟶ 4,589:
expandBraces(stri);
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,344 ⟶ 4,616:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func brace_expand (input) {
var current = ['']
var stack = [[current]]
Line 4,408 ⟶ 4,679:
It{{em,alic}iz,erat}e{d,}, please.
{,{,gotta have{ ,\, again\, }}more }cowbell!
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,437 ⟶ 4,708:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Simula}}==
{{trans|Python}}
<langsyntaxhighlight lang="simula">CLASS ARRAYLISTS;
BEGIN
 
Line 4,526 ⟶ 4,796:
END;
 
END;</langsyntaxhighlight><syntaxhighlight lang ="simula">EXTERNAL CLASS ARRAYLISTS;
ARRAYLISTS
BEGIN
Line 4,676 ⟶ 4,946:
 
END
</syntaxhighlight>
</lang>
{{out}}
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
Line 4,705 ⟶ 4,975:
 
</pre>
 
=={{header|Tailspin}}==
<langsyntaxhighlight lang="tailspin">
templates braceExpansion
composer braceParse
Line 4,720 ⟶ 4,989:
 
templates collateSequence
data part <[]|''> local
@: [''];
$... -> #
$@!
when <´part´ '.*'> do
def part: $;
@: [$@... -> '$;$part;'];
Line 4,741 ⟶ 5,011:
 
'{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}' -> '"$;" expands to $ -> braceExpansion ... -> '$#10;$;';$#10;$#10;' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,773 ⟶ 5,043:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc combine {cases1 cases2 {insert ""}} {
Line 4,842 ⟶ 5,112:
}
return $current
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">foreach testcase {
"~/{Downloads,Pictures}/*.{jpg,gif,png}"
"It{{em,alic}iz,erat}e{d,}, please."
Line 4,851 ⟶ 5,121:
} {
puts $testcase\n\t[join [commatize $testcase] \n\t]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,877 ⟶ 5,147:
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
=={{header|TXR}}==
 
The approach here is to parse the notation into a nested tree of strings. In the following diagram the <code>-></code> arrow indicates that the tree on the left denotes the list of output strings on the right.
 
A list with no operator symbol denotes catenation:
 
<pre>("foo" "bar") -> ("foobar")</pre>
 
The <code>/</code> symbol (slash, usually denoting arithmetic division) denotes alternation:
 
<pre>(/ "foo" "bar") -> ("foo" "bar")
("inter" (/ "pol" "pret") "ation") -> ("interpolation" "interpretation")</pre>
 
This notation is processed by the <code>bexp-expand</code> function to produce the list of strings which it denotes. The <code>bexp-parse</code> function parses a string containing brace expansion into the above notation.
 
The backslashes and edge cases are handled between the tokenizing and parsing. Backslashed characters are represented as tokens which include the backslash. Thus the <code>\{</code> token compares unequal to <code>{</code> and isn't mistaken for it. These backslashed tokens just look like any other text that has no special meaning.
 
The empty <code>{}</code> is handled as a token, but other cases of braces containing no commas are handled in the parser.
 
When the parser has scanned a complete, valid brace that contains no comma, instead of generating a <code>(/ ...)</code> tree node from the content, it generates <code>("{" ... "}")</code>, rendering the braces as literal strings. The <code>...</code> content may contain <code>/</code> operators, as required.
 
When the parser has scanned an incomplete brace, it puts out <code>("{" ...)</code>: the dangling brace is represented literally, followed by the items that have been parsed out. The comma elements are preserved in this case; the lack of a closing brace turns off their meaning.
 
In the main case of a balanced brace with commas, the parsed out elements are split on the commas, which are removed, and that forms the arguments of <code>/</code> node.
 
<syntaxhighlight lang="txrlisp">;; API
(defun brace-expand (str)
(bexp-expand (bexp-parse str)))
 
;; parser
(defstruct bexp-parse-ctx ()
str
toks)
 
(defun bexp-parse (str)
(let ((ctx (new bexp-parse-ctx
str str
;; tokenizer
toks (remqual "" (tok #/([{},]|{}|\\\\|\\.)/ t str)))))
(build
(whilet ((next (pop ctx.toks)))
(add
(if (equal next "{")
(bexp-parse-brace ctx)
next))))))
 
(defun bexp-parse-brace (ctx)
(buildn
(let ((orig-toks ctx.toks))
(caseq (whilet ((next (pop ctx.toks)))
(casequal next
("{" (add (bexp-parse-brace ctx)))
("}" (return :ok))
(t (add next))))
(:ok
(cond
((memqual "," (get))
(flow (get)
(split* @1 (op where (op equal ",")))
(cons '/)))
(t
(add* "{")
(add "}")
(get))))
(nil
(add* "{")
(get))))))
 
;; expander
(defun bexp-expand (tree : (path (new list-builder)))
(build
(match-case tree
(() (add (cat-str path.(get))))
(((/ . @alt) . @rest)
(let ((saved-path path.(get)))
(each ((elem alt))
path.(oust saved-path)
(pend (bexp-expand (cons elem rest) path)))))
((@(consp @succ) . @rest)
(pend (bexp-expand (append succ rest) path)))
((@head . @rest)
path.(add head)
(pend (bexp-expand rest path))))))
 
;; Tests
(tprint (brace-expand "~/{Downloads,Pictures}/*.{jpg,gif,png}"))
(tprint (brace-expand "It{{em,alic}iz,erat}e{d,}, please."))
(tprint (brace-expand "{,{,gotta have{ ,\\, again\\, }}more }cowbell!"))
(tprint (brace-expand "{}} some }{,{\\\\{ edge, edge} \\,}{ cases, {here} \\\\\\\\\\}"))</syntaxhighlight>
 
{{out}}
 
<pre>~/Downloads/*.jpg
~/Downloads/*.gif
~/Downloads/*.png
~/Pictures/*.jpg
~/Pictures/*.gif
~/Pictures/*.png
Itemized, please.
Itemize, please.
Italicized, please.
Italicize, please.
Iterated, please.
Iterate, please.
cowbell!
more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
{}} some }{,{\\ edge \,}{ cases, {here} \\\\\}
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|Python}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function GetGroup(s As String, depth As Integer) As Tuple(Of List(Of String), String)
Dim out As New List(Of String)
Dim comma = False
While Not String.IsNullOrEmpty(s)
Dim gs = GetItem(s, depth)
Dim g = gs.Item1
s = gs.Item2
If String.IsNullOrEmpty(s) Then
Exit While
End If
out.AddRange(g)
 
If s(0) = "}" Then
If comma Then
Return Tuple.Create(out, s.Substring(1))
End If
Return Tuple.Create(out.Select(Function(a) "{" + a + "}").ToList(), s.Substring(1))
End If
 
If s(0) = "," Then
comma = True
s = s.Substring(1)
End If
End While
Return Nothing
End Function
 
Function GetItem(s As String, Optional depth As Integer = 0) As Tuple(Of List(Of String), String)
Dim out As New List(Of String) From {""}
While Not String.IsNullOrEmpty(s)
Dim c = s(0)
If depth > 0 AndAlso (c = "," OrElse c = "}") Then
Return Tuple.Create(out, s)
End If
If c = "{" Then
Dim x = GetGroup(s.Substring(1), depth + 1)
If Not IsNothing(x) Then
Dim tout As New List(Of String)
For Each a In out
For Each b In x.Item1
tout.Add(a + b)
Next
Next
out = tout
s = x.Item2
Continue While
End If
End If
If c = "\" AndAlso s.Length > 1 Then
c += s(1)
s = s.Substring(1)
End If
out = out.Select(Function(a) a + c).ToList()
s = s.Substring(1)
End While
Return Tuple.Create(out, s)
End Function
 
Sub Main()
For Each s In {
"It{{em,alic}iz,erat}e{d,}, please.",
"~/{Downloads,Pictures}/*.{jpg,gif,png}",
"{,{,gotta have{ ,\, again\, }}more }cowbell!",
"{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}"
}
Dim fmt = "{0}" + vbNewLine + vbTab + "{1}"
Dim parts = GetItem(s)
Dim res = String.Join(vbNewLine + vbTab, parts.Item1)
Console.WriteLine(fmt, s, res)
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>It{{em,alic}iz,erat}e{d,}, please.
Itemized, please.
Itemize, please.
Italicized, please.
Italicize, please.
Iterated, please.
Iterate, please.
~/{Downloads,Pictures}/*.{jpg,gif,png}
~/Downloads/*.jpg
~/Downloads/*.gif
~/Downloads/*.png
~/Pictures/*.jpg
~/Pictures/*.gif
~/Pictures/*.png
{,{,gotta have{ ,\, again\, }}more }cowbell!
cowbell!
more cowbell!
gotta have more cowbell!
gotta have\ again\ more cowbell!
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}
{}} some }{,{\ edge \}{ cases, {here} \\\
{}} some }{,{\ edge \}{ cases, {here} \\\</pre>
 
=={{header|Wren}}==
{{trans|Python}}
<langsyntaxhighlight ecmascriptlang="wren">var getGroup // forward declaration
 
var getItem = Fn.new { |s, depth|
Line 4,945 ⟶ 5,427:
for (s in getItem.call(input, 0)[0]) System.print(" " + s)
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 4,978 ⟶ 5,460:
=={{header|zkl}}==
This is a two pass algorithm (2*length(string)), one pass to find valid {} pairs, the next pass to expand them.
<langsyntaxhighlight lang="zkl">fcn eyeball(code,ps=L(),brace=False){ //-->indexes of valid braces & commas
cs:=L();
foreach c in (code){ // start fresh or continue (if recursing)
Line 5,009 ⟶ 5,491:
}
strings
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach bs in (T("It{{em,alic}iz,erat}e{d,}", "~/{Downloads,Pictures}/*.{jpg,gif,png}",
"It{{em,alic}iz,erat}e{d,}, please.", "a{2,1}b{X,Y,X}c", 0'|a\\{\\\{b,c\,d}|,
"{a,b{c{,{d}}e}f", 0'|{,{,gotta have{ ,\, again\, }}more }cowbell!|,
Line 5,016 ⟶ 5,498:
{
"%s expands to\n %s".fmt(bs,expando(bs)).println();
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits