Multisplit: Difference between revisions
Content deleted Content added
m Protected "Multisplit": Counter-productive edit warring ([edit=autoconfirmed] (expires 14:21, 3 May 2011 (UTC)) [move=autoconfirmed] (expires 14:21, 3 May 2011 (UTC))) |
Fix D implementation |
||
Line 11: | Line 11: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.array, std.algorithm; |
<lang d>import std.stdio, std.array, std.algorithm; |
||
string[] multiSplit(in string s, string[] divisors) { |
string[] multiSplit(in string s, string[] divisors) { |
||
⚫ | |||
⚫ | |||
string[] result; |
string[] result; |
||
auto rest = s.idup; |
auto rest = s.idup; |
||
while (true) { |
|||
auto best= ""; |
|||
auto delim= ""; |
|||
bool done= true; |
|||
foreach (div; divisors) { |
|||
⚫ | |||
auto maybe= find(rest, div); |
|||
// divisor is not found OR it was last in string |
|||
if (maybe.length > best.length) { |
|||
best= maybe; |
|||
delim= div; |
|||
done= false; |
|||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} else { |
|||
auto t= findSplit(rest, delim); |
|||
result[$ - 1]= t[0].idup; |
|||
⚫ | |||
} |
|||
} |
} |
||
if (!rest.empty) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return result; |
|||
} |
} |
||
void main() { |
void main() { |
||
auto s = "a!===b=!=c==!=d"; |
auto s = "a!===b=!=c==!=d"; |
||
auto divs = ["==", "!=", "=", "!"]; |
auto divs = ["==", "!=", "=", "!"]; |
||
auto lst = multiSplit(s, divs); |
auto lst = multiSplit(s, divs); |
||
foreach (i, p; lst) { |
foreach (i, p; lst) { |
||
write(p); |
write(p); |
||
if (i < lst.length-1) { |
|||
write(" {} "); |
|||
} |
|||
} |
} |
||
writeln(); |
writeln(); |
||
}</lang> |
}</lang> |
||
Output (separators are in brackets): |
Output (separators are in brackets): |
||
<pre>a |
<pre>a {} {} b {} {} c {} {} d</pre> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |