208 lines
5.5 KiB
C#
208 lines
5.5 KiB
C#
namespace InnovEnergy.SysTools.Utils;
|
|
|
|
internal static class StringUtils
|
|
{
|
|
|
|
public static String NewLineBefore(this String s)
|
|
{
|
|
return Environment.NewLine + s;
|
|
}
|
|
|
|
public static String AddTitle(this String s, String title)
|
|
{
|
|
return title.Underline() + s;
|
|
}
|
|
|
|
public static String Underline(this String s)
|
|
{
|
|
return s.NewLine() + "".PadRight(s.Length, '-').NewLine(2);
|
|
}
|
|
|
|
public static Boolean IsNullOrEmpty(this String s)
|
|
{
|
|
return String.IsNullOrEmpty(s);
|
|
}
|
|
|
|
public static Boolean IsNullOrWhiteSpace(this String s)
|
|
{
|
|
return String.IsNullOrWhiteSpace(s);
|
|
}
|
|
|
|
public static String Format(this Object value, String fmt)
|
|
{
|
|
return String.Format(fmt, value);
|
|
}
|
|
|
|
public static String NewLine(this String s)
|
|
{
|
|
return s + Environment.NewLine;
|
|
}
|
|
|
|
public static String NewLine(this String s, Int32 number)
|
|
{
|
|
return Enumerable
|
|
.Repeat(Environment.NewLine, number)
|
|
.Aggregate(s, (a, b) => a + b);
|
|
}
|
|
|
|
public static String AppendSpaces(this String s, Int32 count = 4)
|
|
{
|
|
return s.PadRight(s.Length + count);
|
|
}
|
|
|
|
public static String Append(this String s, String other)
|
|
{
|
|
return s + other;
|
|
}
|
|
|
|
public static String Concat(this IEnumerable<String> ss)
|
|
{
|
|
return String.Join("", ss);
|
|
}
|
|
|
|
public static String SurroundWith(this String s, String before, String after = null)
|
|
{
|
|
return before + s + (after ?? before);
|
|
}
|
|
|
|
|
|
public static String ShellSingleQuote(this String s)
|
|
{
|
|
return $"'{s.Replace("'", @"'\''")}'";
|
|
}
|
|
|
|
public static String Quote(this String s)
|
|
{
|
|
return s.Replace(@"\", @"\\").Replace("\"", "\\\"").SurroundWith("\"");
|
|
}
|
|
|
|
public static String Join(this IEnumerable<String> strings)
|
|
{
|
|
return String.Join("", strings);
|
|
}
|
|
|
|
public static String JoinWith(this IEnumerable<String> strings, String separator)
|
|
{
|
|
return String.Join(separator, strings);
|
|
}
|
|
|
|
public static String JoinToLines(this IEnumerable<String> lines)
|
|
{
|
|
return String.Join(Environment.NewLine, lines);
|
|
}
|
|
|
|
public static String JoinToLines(params String[] lines)
|
|
{
|
|
return String.Join(Environment.NewLine, lines);
|
|
}
|
|
|
|
public static String Indent(this String text)
|
|
{
|
|
return " ".SideBySideWith(text, "");
|
|
}
|
|
|
|
public static String Indent(this String text, String indentor)
|
|
{
|
|
return indentor.SideBySideWith(text);
|
|
}
|
|
|
|
public static IReadOnlyList<String> SplitLines(this String s)
|
|
{
|
|
return s.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
|
|
}
|
|
|
|
public static String SideBySideWith(this String left, String right, String separator = " ")
|
|
{
|
|
var leftLines = left .Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
|
|
var rightLines = right.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
|
|
|
|
var leftWidth = leftLines.Count != 0
|
|
? leftLines.Max(l => l.Length)
|
|
: 0;
|
|
|
|
var nLines = Math.Max(leftLines.Count, rightLines.Count);
|
|
|
|
var leftPadded = leftLines .Pad(nLines, "").Select(l => l.PadRight(leftWidth));
|
|
var rightPadded = rightLines.Pad(nLines, "");
|
|
|
|
return Enumerable
|
|
.Zip(leftPadded, rightPadded, (l, r) => String.Join(separator, l, r))
|
|
.JoinToLines();
|
|
}
|
|
|
|
|
|
public static String JoinWith<T>(this IEnumerable<T> args, String separator)
|
|
{
|
|
return String.Join(separator, args);
|
|
}
|
|
|
|
public static IEnumerable<String> GetLinesStartingWith(this IEnumerable<String> lines, String prefix)
|
|
{
|
|
return lines.Where(l => l.StartsWith(prefix));
|
|
}
|
|
|
|
public static String GetLineStartingWith(this IEnumerable<String> lines, String prefix)
|
|
{
|
|
return lines
|
|
.GetLinesStartingWith(prefix)
|
|
.Single();
|
|
}
|
|
|
|
|
|
|
|
public static ILookup<String, String> ParseKeyValuesTextFile(this SysPath file)
|
|
{
|
|
var lines = file
|
|
.ReadLines()
|
|
.Select(l => l.Trim())
|
|
.Where(l => l.Length > 0 && !l.StartsWith("#") && !l.StartsWith(";"));
|
|
|
|
var keyValuePairs = from line in lines
|
|
let fields = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
|
|
let key = fields[0]
|
|
from value in fields.Skip(1)
|
|
select (key, value);
|
|
|
|
return keyValuePairs.ToLookup(e => e.key, e => e.value);
|
|
}
|
|
|
|
public static Boolean EndsWith(this String str, Char c)
|
|
{
|
|
if (str.Length <= 0)
|
|
return false;
|
|
|
|
return str[str.Length - 1] == c;
|
|
}
|
|
|
|
public static String CommonPrefix(String str, params String[] more)
|
|
{
|
|
var prefixLength = str
|
|
.TakeWhile((c, i) => more.All(s => i < s.Length && s[i] == c))
|
|
.Count();
|
|
|
|
return str.Substring(0, prefixLength);
|
|
}
|
|
|
|
|
|
public static String AfterFirst(this String str, Char separator)
|
|
{
|
|
return str.Substring(str.IndexOf(separator) + 1);
|
|
}
|
|
|
|
public static String AfterLast(this String str, Char separator)
|
|
{
|
|
return str.Substring(str.LastIndexOf(separator) + 1);
|
|
}
|
|
|
|
public static String UntilLast(this String str, Char separator)
|
|
{
|
|
return str.Substring(0, str.LastIndexOf(separator));
|
|
}
|
|
|
|
public static String UntilFirst(this String str, Char separator)
|
|
{
|
|
return str.Substring(0, str.IndexOf(separator));
|
|
}
|
|
|
|
|
|
} |