Last active
April 30, 2020 12:15
-
-
Save bboyle1234/62274d824f74ae0d81a2d843e9be32a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class CharPipe { | |
protected readonly CharPipe Continuation; | |
public CharPipe(CharPipe continuation) { | |
Continuation = continuation; | |
} | |
public abstract void Write(char c); | |
public abstract void Complete(); | |
} | |
class ToLowerCharPipe : CharPipe { | |
public ToLowerCharPipe(CharPipe continuation) : base(continuation) { } | |
public override void Write(char c) => Continuation.Write(char.ToLower(c)); | |
public override void Complete() => Continuation.Complete(); | |
} | |
class ReplaceCharPipe : CharPipe { | |
readonly char[] Search; | |
readonly char[] Replace; | |
int matchCount = 0; | |
public ReplaceCharPipe(CharPipe continuation, string search, string replace) : base(continuation) { | |
Search = search.ToCharArray(); | |
Replace = replace.ToCharArray(); | |
} | |
public override void Write(char c) { | |
if (c == Search[matchCount]) { | |
matchCount++; | |
if (matchCount == Search.Length) { | |
for (var i = 0; i < Replace.Length; i++) | |
Continuation.Write(Replace[i]); | |
matchCount = 0; | |
} | |
} else { | |
for (var i = 0; i < matchCount; i++) | |
Continuation.Write(Search[i]); | |
Continuation.Write(c); | |
matchCount = 0; | |
} | |
} | |
public override void Complete() { | |
for (var i = 0; i < matchCount; i++) { | |
Continuation.Write(Search[i]); | |
} | |
Continuation.Complete(); | |
} | |
} | |
class TrimCharPipe : CharPipe { | |
readonly List<char> WhiteSpaceBuffer = new List<char>(); | |
bool hasStartedChars = false; | |
public TrimCharPipe(CharPipe continuation) : base(continuation) { } | |
public override void Write(char c) { | |
if (hasStartedChars) { | |
if (char.IsWhiteSpace(c)) { | |
WhiteSpaceBuffer.Add(c); | |
} else { | |
foreach (var c2 in WhiteSpaceBuffer) | |
Continuation.Write(c2); | |
WhiteSpaceBuffer.Clear(); | |
Continuation.Write(c); | |
} | |
} else { | |
if (!char.IsWhiteSpace(c)) { | |
Continuation.Write(c); | |
hasStartedChars = true; | |
} | |
} | |
} | |
public override void Complete() => Continuation.Complete(); | |
} | |
class ResultCharPipe : CharPipe { | |
readonly StringBuilder sb = new StringBuilder(); | |
public ResultCharPipe(CharPipe continuation) : base(continuation) { } | |
public override void Write(char c) => sb.Append(c); | |
public override void Complete() => Continuation.Complete(); | |
public string GetResult() => sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment