XXXX redacted: Difference between revisions

Add ed example
m (→‎{{header|Wren}}: Minor tidy)
(Add ed example)
 
(2 intermediate revisions by one other user not shown)
Line 778:
[p|i|o] XXX? XXXX XXXXXX XXXXXX is in his XXXXXXX while playing the "XXXXXXX" brand XXXXXXXX. That's so XXX</pre>
 
=={{header|ed}}==
 
Uses non-portable [[GNU Ed]] extension for case-insensitive matching. The number of replacement lines has to be copy-pasted as many times as necessary for full replacement. Because, unlike [[sed]], ed has no loops. Also note that the replacement is always three X-es, because ed has no way to customize the replacement length.
 
<syntaxhighlight lang="sed">
H
2,j
1s/.*/&|/
,j
t
t0
t0
t0
t0
t0
# Repeat as many times as possible. Because ed has no loops
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)\b\1\b/\1|\2XXX/g
1s/^(.*)\|(.*)$/\2/
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/^(.*)\|(.*)\b\1\b/\1|\2XXX/gi
2s/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.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/g
5s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/g
5s/^(.*)\|(.*)$/\2/
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)\b(\w*)\1(\w*)\b(.*)$/\1|\2XXX\5/gi
6s/^(.*)\|(.*)$/\2/
# Pretty descriptions
1s/.*/Whole word: &/
2s/.*/Whole word, case insensitive: &/
3s/.*/Partial word: &/
4s/.*/Partial word, case insensitive: &/
5s/.*/Partial word, overkill: &/
6s/.*/Partial word, case insensitive, overkill: &/
,p
Q</syntaxhighlight>
 
{{out}}
 
<pre>$ cat xxx-redacted.ed | ed -lEGs xxx-redacted.input
Whole word: XXX? Toms bottom tomato is in his stomach while playing the "XXX-tom" brand tom-toms. That's so tom.
Whole word, case insensitive: XXX? Toms bottom tomato is in his stomach while playing the "XXX-XXX" brand XXX-toms. That's so XXX.
Partial word: XXX? XXXs bottom tomato is in his stomach while playing the "XXX-tom" brand tom-toms. That's so tom.
Partial word, case insensitive: XXX? XXXs botXXX XXXato is in his sXXXach while playing the "XXX-XXX" brand XXX-XXXs. That's so XXX.
Partial word, overkill: XXX? XXX bottom tomato is in his stomach while playing the "XXX-tom" brand tom-toms. That's so tom.
Partial word, case insensitive, overkill: XXX? XXX XXX XXX is in his XXX while playing the "XXX-XXX" brand XXX-XXX. That's so XXX.</pre>
 
=={{header|FreeBASIC}}==
{{trans|D}}
No Complex Unicode!
<syntaxhighlight lang="vbnet">Function isWordChar(c As String) As Integer
Return (c = "-" Or c >= "A" And c <= "Z" Or c >= "a" And c <= "z")
End Function
 
Function Redact(source As String, word As String, partial As Boolean, insensitive As Boolean, overkill As Boolean) As String
Dim As Integer i, j, beg, fin, match
Dim As String temp, s, w
temp = source
i = 1
While i <= Len(temp) - Len(word) + 1
match = 1
j = 1
While j <= Len(word)
s = Mid(temp, i + j - 1, 1)
w = Mid(word, j, 1)
If insensitive Then
If Lcase(s) <> Lcase(w) Then
match = 0
Exit While
End If
Else
If s <> w Then
match = 0
Exit While
End If
End If
j += 1
Wend
If match Then
beg = i
fin = i + Len(word) - 1
If Not partial Then
If beg > 1 And isWordChar(Mid(temp, beg - 1, 1)) Then
i += 1
Continue While
End If
If fin < Len(temp) And isWordChar(Mid(temp, fin + 1, 1)) Then
i += 1
Continue While
End If
End If
If overkill Then
While beg > 1 And isWordChar(Mid(temp, beg - 1, 1))
beg -= 1
Wend
While fin < Len(temp) And isWordChar(Mid(temp, fin + 1, 1))
fin += 1
Wend
End If
Mid(temp, beg, fin - beg + 1) = String(fin - beg + 1, "X")
End If
i += 1
Wend
Return temp
End Function
 
Sub Example(source As String, word As String)
Print "Redact '"; word; "':"
Print "[w|s|n] "; Redact(source, word, False, False, False)
Print "[w|i|n] "; Redact(source, word, False, True, False)
Print "[p|s|n] "; Redact(source, word, True, False, False)
Print "[p|i|n] "; Redact(source, word, True, True, False)
Print "[p|s|o] "; Redact(source, word, True, False, True)
Print "[p|i|o] "; Redact(source, word, True, True, True)
Print
End Sub
 
Dim As String text
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")
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as D entry.</pre>
 
=={{header|FutureBasic}}==
Line 1,383 ⟶ 1,563:
</pre>
 
=={{header|RakuPython}}==
{{works with|Python|3.x}}
{{trans|D}}
<syntaxhighlight lang="python">#!/usr/bin/env python3
 
def redact(source, word, partial=False, insensitive=False, overkill=False):
def different(s, w):
if insensitive:
return s.upper() != w.upper()
else:
return s != w
 
def is_word_char(c):
return c == '-' or c.isalpha()
 
temp = list(source)
i = 0
while i <= len(temp) - len(word):
match = True
j = 0
while j < len(word):
if different(temp[i + j], word[j]):
match = False
break
j += 1
if match:
beg = i
end = i + len(word)
if not partial:
if beg > 0 and is_word_char(temp[beg - 1]):
i += 1
continue
if end < len(temp) and is_word_char(temp[end]):
i += 1
continue
if overkill:
while beg > 0 and is_word_char(temp[beg - 1]):
beg -= 1
while end < len(temp) - 1 and is_word_char(temp[end]):
end += 1
for k in range(beg, end):
temp[k] = 'X'
i += 1
return ''.join(temp)
 
def example(source, word):
print("Redact '", word, "':")
print("[w|s|n]", redact(source, word, False, False, False))
print("[w|i|n]", redact(source, word, False, True, False))
print("[p|s|n]", redact(source, word, True, False, False))
print("[p|i|n]", redact(source, word, True, True, False))
print("[p|s|o]", redact(source, word, True, False, True))
print("[p|i|o]", redact(source, word, True, True, True))
print()
 
if __name__ == "__main__":
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")
</syntaxhighlight>
 
=={{header|Raku}}==
{{works with|Rakudo|2020.02}}
 
110

edits