Browse Source

Add StripStart string extension method

pull/165/head
Ionite 1 year ago
parent
commit
e76ef3508e
No known key found for this signature in database
  1. 47
      StabilityMatrix.Core/Extensions/StringExtensions.cs

47
StabilityMatrix.Core/Extensions/StringExtensions.cs

@ -4,24 +4,26 @@ namespace StabilityMatrix.Core.Extensions;
public static class StringExtensions
{
private static string EncodeNonAsciiCharacters(string value) {
private static string EncodeNonAsciiCharacters(string value)
{
var sb = new StringBuilder();
foreach (var c in value)
foreach (var c in value)
{
// If not ascii or not printable
if (c > 127 || c < 32)
{
// This character is too big for ASCII
var encodedValue = "\\u" + ((int) c).ToString("x4");
var encodedValue = "\\u" + ((int)c).ToString("x4");
sb.Append(encodedValue);
}
else {
else
{
sb.Append(c);
}
}
return sb.ToString();
}
/// <summary>
/// Converts string to repr
/// </summary>
@ -35,22 +37,24 @@ public static class StringExtensions
writer.Write("'");
foreach (var ch in str)
{
writer.Write(ch switch
{
'\0' => "\\0",
'\n' => "\\n",
'\r' => "\\r",
'\t' => "\\t",
// Non ascii
_ when ch > 127 || ch < 32 => $"\\u{(int) ch:x4}",
_ => ch.ToString()
});
writer.Write(
ch switch
{
'\0' => "\\0",
'\n' => "\\n",
'\r' => "\\r",
'\t' => "\\t",
// Non ascii
_ when ch > 127 || ch < 32 => $"\\u{(int)ch:x4}",
_ => ch.ToString()
}
);
}
writer.Write("'");
return writer.ToString();
}
/// <summary>
/// Counts continuous sequence of a character
/// from the start of the string
@ -71,4 +75,13 @@ public static class StringExtensions
}
return count;
}
/// <summary>
/// Strips the substring from the start of the string
/// </summary>
public static string StripStart(this string str, string subString)
{
var index = str.IndexOf(subString, StringComparison.Ordinal);
return index < 0 ? str : str.Remove(index, subString.Length);
}
}

Loading…
Cancel
Save