Skip to content

Commit

Permalink
Remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-abblix committed May 25, 2024
1 parent 3456b8b commit 5bbe1a2
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions Abblix.Utils/Sanitized.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,37 @@ public Sanitized(object? source)
{
var c = source[i];

switch (c)
var replacement = c switch
{
case '\n':
ReplaceTo("\\n", ref builder, source, i);
break;
case '\r':
ReplaceTo("\\r", ref builder, source, i);
break;
case '\t':
ReplaceTo("\\t", ref builder, source, i);
break;
case '\"':
ReplaceTo("\\\"", ref builder, source, i);
break;
case '\'':
ReplaceTo("\\'", ref builder, source, i);
break;
case '\\':
ReplaceTo(@"\\", ref builder, source, i);
break;
case ',':
ReplaceTo("\\,", ref builder, source, i);
break;
case ';':
ReplaceTo("\\;", ref builder, source, i);
break;
default:
if (0x00 <= c && c <= 0x1f || c == 0x7f)
ReplaceTo(null, ref builder, source, i);
else
builder?.Append(c);
break;
'\n' => "\\n",
'\r' => "\\r",
'\t' => "\\t",
'\"' => "\\\"",
'\'' => "\\'",
'\\' => @"\\",
',' => "\\,",
';' => "\\;",
_ => null
};

if (replacement != null)
{
ReplaceTo(ref builder, source, i, replacement);
}
else if (0x00 <= c && c <= 0x1f || c == 0x7f)
{
ReplaceTo(ref builder, source, i, null);
}
else
{
builder?.Append(c);
}
}

return builder != null ? builder.ToString() : source;
}

private void ReplaceTo(string? replacement, ref StringBuilder? builder, string source, int i)
private void ReplaceTo(ref StringBuilder? builder, string source, int i, string? replacement)
{
builder ??= new StringBuilder(source, 0, i, source.Length + (replacement?.Length ?? 0) - 1);
builder.Append(replacement);
Expand Down

0 comments on commit 5bbe1a2

Please sign in to comment.