XXXX redacted: Difference between revisions

Content added Content deleted
m (Minor edit to C code)
Line 375: Line 375:
[p|i|o]: XXX? XXXX XXXXXX XXXXXX is in his XXXXXXX while playing the "XXXXXXX" brand XXXXXXXX. That's so XXX.
[p|i|o]: XXX? XXXX XXXXXX XXXXXX is in his XXXXXXX while playing the "XXXXXXX" brand XXXXXXXX. That's so XXX.
</pre>
</pre>

=={{header|D}}==
<lang d>import std.stdio;
import std.uni;

string redact(string source, string word, bool partial = false, bool insensitive = false, bool overkill = false) {
bool different(char s, char w) {
if (insensitive) {
return s.toUpper != w.toUpper;
} else {
return s != w;
}
}

bool isWordChar(char c) {
return c == '-' || c.isAlpha;
}

auto temp = source.dup;

foreach (i; 0 .. temp.length - word.length + 1) {
bool match = true;
foreach (j; 0 .. word.length) {
if (different(temp[i + j], word[j])) {
match = false;
break;
}
}
if (match) {
auto beg = i;
auto end = i + word.length;

if (!partial) {
if (beg > 0 && isWordChar(temp[beg - 1])) {
// writeln("b boundary ", temp[beg - 1]);
continue;
}
if (end < temp.length && isWordChar(temp[end])) {
// writeln("e boundary ", temp[end]);
continue;
}
}
if (overkill) {
while (beg > 0 && isWordChar(temp[beg - 1])) {
beg--;
}
while (end < temp.length - 1 && isWordChar(temp[end])) {
end++;
}
}

temp[beg .. end] = 'X';
}
}

return temp.idup;
}

void example(string source, string word) {
writeln("Redact ", word);
writeln("[w|s|n] ", redact(source, word, false, false, false));
writeln("[w|i|n] ", redact(source, word, false, true, false));
writeln("[p|s|n] ", redact(source, word, true, false, false));
writeln("[p|i|n] ", redact(source, word, true, true, false));
writeln("[p|s|o] ", redact(source, word, true, false, true));
writeln("[p|i|o] ", redact(source, word, true, true, true));
writeln;
}

void main(string[] args) {
string text = `Tom? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so tom`;
example(text, "Tom");
example(text, "tom");
}</lang>
{{out}}
<pre>Redact Tom
[w|s|n] XXX? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so tom
[w|i|n] XXX? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so XXX
[p|s|n] XXX? XXXs bottom tomato is in his stomach while playing the "XXX-tom" brand tom-toms. That's so tom
[p|i|n] XXX? XXXs botXXX XXXato is in his sXXXach while playing the "XXX-XXX" brand XXX-XXXs. That's so XXX
[p|s|o] XXX? XXXX bottom tomato is in his stomach while playing the "XXXXXXX" brand tom-toms. That's so tom
[p|i|o] XXX? XXXX XXXXXX XXXXXX is in his XXXXXXX while playing the "XXXXXXX" brand XXXXXXXX. That's so XXX

Redact tom
[w|s|n] Tom? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so XXX
[w|i|n] XXX? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so XXX
[p|s|n] Tom? Toms botXXX XXXato is in his sXXXach while playing the "Tom-XXX" brand XXX-XXXs. That's so XXX
[p|i|n] XXX? XXXs botXXX XXXato is in his sXXXach while playing the "XXX-XXX" brand XXX-XXXs. That's so XXX
[p|s|o] Tom? Toms XXXXXX XXXXXX is in his XXXXXXX while playing the "XXXXXXX" brand XXXXXXXX. That's so XXX
[p|i|o] XXX? XXXX XXXXXX XXXXXX is in his XXXXXXX while playing the "XXXXXXX" brand XXXXXXXX. That's so XXX</pre>


=={{header|Go}}==
=={{header|Go}}==