-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBleep.cs
124 lines (107 loc) · 3.37 KB
/
Bleep.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using System.Text.RegularExpressions;
namespace Bleep
{
/// <summary>
/// Summary description for Bleep
/// </summary>
[TestFixture]
public class BleepTest
{
Bleep b = new Bleep();
[Test]
public void FrankBecomesF___k()
{
Assert.AreEqual("F***k", b.Filter("Frank"));
}
[Test]
public void frankBecomesf___k()
{
Assert.AreEqual("f***k", b.Filter("frank"));
}
[Test]
public void frAnkBecomesf___k()
{
Assert.AreEqual("f***k", b.Filter("frAnk"));
}
[Test]
public void BobBecomesB_b()
{
Assert.AreEqual("B*b", b.Filter("Bob"));
}
[Test]
public void FranklyShouldNotBeFiltered()
{
Assert.AreEqual("Frankly", b.Filter("Frankly"));
}
[Test]
public void FrankFordShouldBeFilteredAsF___kFord()
{
Assert.AreEqual("F***k Ford", b.Filter("Frank Ford"));
}
[Test]
public void FrankFordWithQuoteShouldBeFilteredAsF___kFord()
{
Assert.AreEqual("\"F***k Ford\"", b.Filter("\"Frank Ford\""));
}
[Test]
public void FrankWithFullStopShouldBeFilteredAsF___kFullStop()
{
Assert.AreEqual("F***k.", b.Filter("Frank."));
}
[Test]
public void BobBobbyShouldBeFilteredAsB_bBobby()
{
Assert.AreEqual("B*b Bobby", b.Filter("Bob Bobby"));
}
[Test]
public void FrankWhiteSpacesShouldBeFilteredOnlyF___k()
{
Assert.AreEqual("~!@#$%^&*(()_+{}|\":?>< F***k~!@#$%^&*(()_+{}|\":?>< F***k", b.Filter("~!@#$%^&*(()_+{}|\":?>< Frank~!@#$%^&*(()_+{}|\":?>< Frank"));
}
}
public class Bleep
{
public string Filter(string input)
{
string[] filteredWords = new string[] { "frank", "bob" };
string[] replacementWords = new string[] { "ran", "o" };
string[] starsWords = new string[] { "***", "*" };
string[] words = input.Split(new string[] { "~","!","@","#","$","%","^","&","*","(",")","_","+","{","}","|","\\"," ",":","?",">","<",",",".","?","<",">","\"" }, StringSplitOptions.None);
string output = "";
foreach (string word in words)
{
string result = word;
for (int i = 0; i < filteredWords.Length; i++)
{
if (result.ToLower() == filteredWords[i])
{
result = Regex.Replace(result, replacementWords[i], starsWords[i], RegexOptions.IgnoreCase);
}
}
output += result + '\u200c';
}
return Restore(input, output);
}
public string Restore(string input, string output)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (output[i] == '\u200c')
{
sb.Append(input[i]);
}
else
{
sb.Append(output[i]);
}
}
return sb.ToString();
}
}
}