Jump to content

XXXX redacted: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added w/p/o and s/i meanings)
m (syntax highlighting fixup automation)
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.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
Line 124:
set output to output as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight 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|i|n]: XXX? Toms bottom tomato is in his stomach while playing the \"Tom-tom\" brand tom-toms. That's so XXX.
Line 142:
[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.
"</langsyntaxhighlight>
Or with the grapheme text:
<langsyntaxhighlight lang="applescript">set graphemeText to "🧑 👨 🧔 👨‍👩‍👦"
set output to {}
repeat with redactionTarget in {"👨", "👨‍👩‍👦"}
Line 155:
set output to output as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Redact 👨:
[w]: 🧑 X 🧔 👨‍👩‍👦
 
Redact 👨‍👩‍👦:
[w]: 🧑 👨 🧔 X
"</langsyntaxhighlight>
 
===Vanilla (core language only)===
Line 170:
Test code and output as above.
 
<langsyntaxhighlight lang="applescript">on redact(theText, redactionTargets, options)
(* Script object containing the basic process. *)
script default
Line 283:
return redactor's outputText
end redact</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
No Complex Unicode!
<langsyntaxhighlight AutoHotkeylang="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"]
opts := ["wsn", "win", "psn", "pin", "pso", "pio"]
Line 317:
}
return str
}</langsyntaxhighlight>
{{out}}
<pre>Redact 'Tom'
Line 337:
=={{header|C}}==
This is a very basic ASCII-only implementation, no Unicode or regular expressions.
<langsyntaxhighlight lang="c">#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
Line 405:
do_basic_tests("tom");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 428:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
using namespace std;
Line 501:
example(text, "tom");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Redact Tom
Line 520:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
import std.uni;
 
Line 591:
example(text, "Tom");
example(text, "tom");
}</langsyntaxhighlight>
{{out}}
<pre>Redact Tom
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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 705:
allOpts = []string{"[p]", "[p|o]"}
printResults(text, allOpts, allWords)
}</langsyntaxhighlight>
 
{{out}}
Line 795:
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.
<langsyntaxhighlight lang="julia">function doif_equals(word, pattern, insens=false)
regex = insens ? Regex("^$pattern\$", "i") : Regex("^$pattern\$")
return replace(word, regex => pattern in multichars ? "X" : "X"^length(pattern))
Line 850:
println()
end
</langsyntaxhighlight>{{out}}
<pre>
 
Line 879:
=={{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.
<langsyntaxhighlight lang="lua">function redact(text, targ, opts)
local part, case, ovrk = opts:find("p")~=nil, opts:find("s")~=nil, opts:find("o")~=nil
local oknp = ovrk or not part
Line 896:
print(opts .. " " .. redact(text, targ, opts))
end
end</langsyntaxhighlight>
{{out}}
<pre>Redact 'Tom':
Line 915:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 957:
printf "%s %s\n", $option, redact($test, $redact, %$opts)
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:40ex;overflow:scroll;">Redact 'Tom':
Line 1,023:
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.
<!--<langsyntaxhighlight Phixlang="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;">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:
"""</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>
<!--</langsyntaxhighlight>-->
{{out}}
w/p/o means whole/partial/overkill word, s/i means case sensitive/insensitive.
Line 1,132:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>sub redact ( Str $str, Str $redact, :i(:$insensitive) = False, :p(:$partial) = False, :o(:$overkill) = False ) {
my $rx =
$insensitive ??
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);
}</langsyntaxhighlight>
{{out}}
<pre style="height:40ex;overflow:scroll;">Redact 'Tom':
Line 1,268:
=={{header|REXX}}==
REXX doesn't have &nbsp; ''regular expressions'', &nbsp; so I had to roll-my-own parsing.
<langsyntaxhighlight REXXlang="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"' ,
"brand tom-toms. That's so tom."
Line 1,315:
nz= nz !.head.r || na || !.tail.r
end /*r*/
return strip( translate(nz, 'X', ?) )</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,347:
=={{header|Swift}}==
{{trans|AppleScript}}
<langsyntaxhighlight lang="swift">import Foundation
 
struct RedactionOptions: OptionSet {
Line 1,432:
doExtraTest(target: "👨")
print()
doExtraTest(target: "👨‍👩‍👦")</langsyntaxhighlight>
 
{{out}}
Line 1,461:
=={{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.
<langsyntaxhighlight lang="tailspin">
composer redact&{secret:}
@: { fill: '', leftBound: '\b{g}', rightBound: '\b{g}', case: '' };
Line 1,498:
$;
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,527:
{{libheader|Wren-str}}
{{libheader|Wren-upc}}
<langsyntaxhighlight lang="ecmascript">import "/pattern" for Pattern
import "/str" for Str
import "/upc" for Graphemes
Line 1,597:
text = "Argentina🧑🇦🇹 France👨🇫🇷 Germany🧔🇩🇪 Netherlands👨‍👩‍👦🇳🇱"
allOpts = ["[p]", "[p|o]"]
printResults.call(text, allOpts, allWords)</langsyntaxhighlight>
 
{{out}}
10,351

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.