XXXX redacted: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added w/p/o and s/i meanings)
m (syntax highlighting fixup automation)
Line 68: Line 68:
This uses ASObjC to access macOS's Foundation framework's regex and text-replacement methods. The methods support [https://unicode-org.github.io/icu/userguide/strings/regexp.html ICU]-compatible regular expressions.
This uses ASObjC to access macOS's Foundation framework's regex and text-replacement methods. The methods support [https://unicode-org.github.io/icu/userguide/strings/regexp.html ICU]-compatible regular expressions.


<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "Foundation"
use scripting additions
use scripting additions
Line 124: Line 124:
set output to output as text
set output to output as text
set AppleScript's text item delimiters to astid
set AppleScript's text item delimiters to astid
return output</lang>
return output</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"Redact Tom:
<syntaxhighlight lang="applescript">"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|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.
[w|i|n]: XXX? Toms bottom tomato is in his stomach while playing the \"Tom-tom\" brand tom-toms. That's so XXX.
Line 142: Line 142:
[p|s|o]: Tom? Toms XXXXXX XXXXXX is in his XXXXXXX while playing the \"XXXXXXX\" brand XXXXXXXX. 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.
[p|i|o]: XXX? XXXX XXXXXX XXXXXX is in his XXXXXXX while playing the \"XXXXXXX\" brand XXXXXXXX. That's so XXX.
"</lang>
"</syntaxhighlight>
Or with the grapheme text:
Or with the grapheme text:
<lang applescript>set graphemeText to "🧑 👨 🧔 👨‍👩‍👦"
<syntaxhighlight lang="applescript">set graphemeText to "🧑 👨 🧔 👨‍👩‍👦"
set output to {}
set output to {}
repeat with redactionTarget in {"👨", "👨‍👩‍👦"}
repeat with redactionTarget in {"👨", "👨‍👩‍👦"}
Line 155: Line 155:
set output to output as text
set output to output as text
set AppleScript's text item delimiters to astid
set AppleScript's text item delimiters to astid
return output</lang>
return output</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"Redact 👨:
<syntaxhighlight lang="applescript">"Redact 👨:
[w]: 🧑 X 🧔 👨‍👩‍👦
[w]: 🧑 X 🧔 👨‍👩‍👦


Redact 👨‍👩‍👦:
Redact 👨‍👩‍👦:
[w]: 🧑 👨 🧔 X
[w]: 🧑 👨 🧔 X
"</lang>
"</syntaxhighlight>


===Vanilla (core language only)===
===Vanilla (core language only)===
Line 170: Line 170:
Test code and output as above.
Test code and output as above.


<lang applescript>on redact(theText, redactionTargets, options)
<syntaxhighlight lang="applescript">on redact(theText, redactionTargets, options)
(* Script object containing the basic process. *)
(* Script object containing the basic process. *)
script default
script default
Line 283: Line 283:
return redactor's outputText
return redactor's outputText
end redact</lang>
end redact</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
No Complex Unicode!
No Complex Unicode!
<lang AutoHotkey>str = Tom? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so tom.
<syntaxhighlight lang="autohotkey">str = Tom? Toms bottom tomato is in his stomach while playing the "Tom-tom" brand tom-toms. That's so tom.
words := ["Tom", "tom"]
words := ["Tom", "tom"]
opts := ["wsn", "win", "psn", "pin", "pso", "pio"]
opts := ["wsn", "win", "psn", "pin", "pso", "pio"]
Line 317: Line 317:
}
}
return str
return str
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Redact 'Tom'
<pre>Redact 'Tom'
Line 337: Line 337:
=={{header|C}}==
=={{header|C}}==
This is a very basic ASCII-only implementation, no Unicode or regular expressions.
This is a very basic ASCII-only implementation, no Unicode or regular expressions.
<lang c>#include <ctype.h>
<syntaxhighlight lang="c">#include <ctype.h>
#include <stdbool.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
Line 405: Line 405:
do_basic_tests("tom");
do_basic_tests("tom");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 428: Line 428:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


using namespace std;
using namespace std;
Line 501: Line 501:
example(text, "tom");
example(text, "tom");
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Redact Tom
<pre>Redact Tom
Line 520: Line 520:


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


Line 591: Line 591:
example(text, "Tom");
example(text, "Tom");
example(text, "tom");
example(text, "tom");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Redact Tom
<pre>Redact Tom
Line 616: Line 616:


To get the number of 'X's right where a ZWJ emoji or other character combination is being replaced, a third party library function is used which counts the number of graphemes in a string, as required by the task.
To get the number of 'X's right where a ZWJ emoji or other character combination is being replaced, a third party library function is used which counts the number of graphemes in a string, as required by the task.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 705: Line 705:
allOpts = []string{"[p]", "[p|o]"}
allOpts = []string{"[p]", "[p|o]"}
printResults(text, allOpts, allWords)
printResults(text, allOpts, allWords)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 795: Line 795:
The solution must kludge a check with the variable "multichar" to properly substitute "X" instead of "XXXX" with the last example.
The solution must kludge a check with the variable "multichar" to properly substitute "X" instead of "XXXX" with the last example.
Otherwise Julia (v 1.4) interprets one 184-bit Unicode extended emoji character as four Unicode characters.
Otherwise Julia (v 1.4) interprets one 184-bit Unicode extended emoji character as four Unicode characters.
<lang julia>function doif_equals(word, pattern, insens=false)
<syntaxhighlight lang="julia">function doif_equals(word, pattern, insens=false)
regex = insens ? Regex("^$pattern\$", "i") : Regex("^$pattern\$")
regex = insens ? Regex("^$pattern\$", "i") : Regex("^$pattern\$")
return replace(word, regex => pattern in multichars ? "X" : "X"^length(pattern))
return replace(word, regex => pattern in multichars ? "X" : "X"^length(pattern))
Line 850: Line 850:
println()
println()
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>


Line 879: Line 879:
=={{header|Lua}}==
=={{header|Lua}}==
Note: The syntax-highlighter used here for Lua appears to be confused by the nested quote styles, but the syntax is valid as written.
Note: The syntax-highlighter used here for Lua appears to be confused by the nested quote styles, but the syntax is valid as written.
<lang lua>function redact(text, targ, opts)
<syntaxhighlight lang="lua">function redact(text, targ, opts)
local part, case, ovrk = opts:find("p")~=nil, opts:find("s")~=nil, opts:find("o")~=nil
local part, case, ovrk = opts:find("p")~=nil, opts:find("s")~=nil, opts:find("o")~=nil
local oknp = ovrk or not part
local oknp = ovrk or not part
Line 896: Line 896:
print(opts .. " " .. redact(text, targ, opts))
print(opts .. " " .. redact(text, targ, opts))
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Redact 'Tom':
<pre>Redact 'Tom':
Line 915: Line 915:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 957: Line 957:
printf "%s %s\n", $option, redact($test, $redact, %$opts)
printf "%s %s\n", $option, redact($test, $redact, %$opts)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:40ex;overflow:scroll;">Redact 'Tom':
<pre style="height:40ex;overflow:scroll;">Redact 'Tom':
Line 1,023: Line 1,023:
Written on the assumption that overkill implies partial (see talk page).<br>
Written on the assumption that overkill implies partial (see talk page).<br>
utf32_length() fashioned after [[Reverse_a_string#Phix]] with added ZWJ - I do not expect it to be entirely complete.
utf32_length() fashioned after [[Reverse_a_string#Phix]] with added ZWJ - I do not expect it to be entirely complete.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">enum</span> <span style="color: #000000;">WHOLE</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PARTIAL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">OVERKILL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">INSENSITIVE</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">WHOLE</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PARTIAL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">OVERKILL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">INSENSITIVE</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">spunc</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" \r\n.?\""</span> <span style="color: #000080;font-style:italic;">-- spaces and punctuation</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">spunc</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" \r\n.?\""</span> <span style="color: #000080;font-style:italic;">-- spaces and punctuation</span>
Line 1,097: Line 1,097:
"""</span>
"""</span>
<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: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ut</span><span style="color: #0000FF;">}&</span><span style="color: #000000;">redact</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ut</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"👨"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">WHOLE</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">redact</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ut</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"👨‍👩‍👦"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">WHOLE</span><span style="color: #0000FF;">))</span>
<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: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ut</span><span style="color: #0000FF;">}&</span><span style="color: #000000;">redact</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ut</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"👨"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">WHOLE</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">redact</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ut</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"👨‍👩‍👦"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">WHOLE</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
w/p/o means whole/partial/overkill word, s/i means case sensitive/insensitive.
w/p/o means whole/partial/overkill word, s/i means case sensitive/insensitive.
Line 1,132: Line 1,132:
{{works with|Rakudo|2020.02}}
{{works with|Rakudo|2020.02}}


<lang perl6>sub redact ( Str $str, Str $redact, :i(:$insensitive) = False, :p(:$partial) = False, :o(:$overkill) = False ) {
<syntaxhighlight lang="raku" line>sub redact ( Str $str, Str $redact, :i(:$insensitive) = False, :p(:$partial) = False, :o(:$overkill) = False ) {
my $rx =
my $rx =
$insensitive ??
$insensitive ??
Line 1,194: Line 1,194:
printf "%20s %s\n", "Redact '👨' [p|o]", $emoji.&redact('👨', :p, :o);
printf "%20s %s\n", "Redact '👨' [p|o]", $emoji.&redact('👨', :p, :o);
printf "%20s %s\n", "Redact '👨‍👩‍👦' [p|o]", $emoji.&redact('👨‍👩‍👦', :p, :o);
printf "%20s %s\n", "Redact '👨‍👩‍👦' [p|o]", $emoji.&redact('👨‍👩‍👦', :p, :o);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:40ex;overflow:scroll;">Redact 'Tom':
<pre style="height:40ex;overflow:scroll;">Redact 'Tom':
Line 1,268: Line 1,268:
=={{header|REXX}}==
=={{header|REXX}}==
REXX doesn't have &nbsp; ''regular expressions'', &nbsp; so I had to roll-my-own parsing.
REXX doesn't have &nbsp; ''regular expressions'', &nbsp; so I had to roll-my-own parsing.
<lang REXX>/*REXX program redacts a string (using Xs) from a text, depending upon specified options*/
<syntaxhighlight lang="rexx">/*REXX program redacts a string (using Xs) from a text, depending upon specified options*/
zDefault= 'Tom? Toms bottom tomato is in his stomach while playing the "Tom-tom"' ,
zDefault= 'Tom? Toms bottom tomato is in his stomach while playing the "Tom-tom"' ,
"brand tom-toms. That's so tom."
"brand tom-toms. That's so tom."
Line 1,315: Line 1,315:
nz= nz !.head.r || na || !.tail.r
nz= nz !.head.r || na || !.tail.r
end /*r*/
end /*r*/
return strip( translate(nz, 'X', ?) )</lang>
return strip( translate(nz, 'X', ?) )</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,347: Line 1,347:
=={{header|Swift}}==
=={{header|Swift}}==
{{trans|AppleScript}}
{{trans|AppleScript}}
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


struct RedactionOptions: OptionSet {
struct RedactionOptions: OptionSet {
Line 1,432: Line 1,432:
doExtraTest(target: "👨")
doExtraTest(target: "👨")
print()
print()
doExtraTest(target: "👨‍👩‍👦")</lang>
doExtraTest(target: "👨‍👩‍👦")</syntaxhighlight>


{{out}}
{{out}}
Line 1,461: Line 1,461:
=={{header|Tailspin}}==
=={{header|Tailspin}}==
This is using the normal definition of words, i.e. emoji do not form words or word boundaries, so the stretch assignment must be matched as a partial match. This solution parses the flags inline and takes the secret to be redacted as a parameter to illustrate both options, although in a production solution I would imagine one might pass both the same way.
This is using the normal definition of words, i.e. emoji do not form words or word boundaries, so the stretch assignment must be matched as a partial match. This solution parses the flags inline and takes the secret to be redacted as a parameter to illustrate both options, although in a production solution I would imagine one might pass both the same way.
<lang tailspin>
<syntaxhighlight lang="tailspin">
composer redact&{secret:}
composer redact&{secret:}
@: { fill: '', leftBound: '\b{g}', rightBound: '\b{g}', case: '' };
@: { fill: '', leftBound: '\b{g}', rightBound: '\b{g}', case: '' };
Line 1,498: Line 1,498:
$;
$;
' -> !OUT::write
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,527: Line 1,527:
{{libheader|Wren-str}}
{{libheader|Wren-str}}
{{libheader|Wren-upc}}
{{libheader|Wren-upc}}
<lang ecmascript>import "/pattern" for Pattern
<syntaxhighlight lang="ecmascript">import "/pattern" for Pattern
import "/str" for Str
import "/str" for Str
import "/upc" for Graphemes
import "/upc" for Graphemes
Line 1,597: Line 1,597:
text = "Argentina🧑🇦🇹 France👨🇫🇷 Germany🧔🇩🇪 Netherlands👨‍👩‍👦🇳🇱"
text = "Argentina🧑🇦🇹 France👨🇫🇷 Germany🧔🇩🇪 Netherlands👨‍👩‍👦🇳🇱"
allOpts = ["[p]", "[p|o]"]
allOpts = ["[p]", "[p|o]"]
printResults.call(text, allOpts, allWords)</lang>
printResults.call(text, allOpts, allWords)</syntaxhighlight>


{{out}}
{{out}}