diff --git a/csharp/Lib/Utils/StringParsing.cs b/csharp/Lib/Utils/StringParsing.cs new file mode 100644 index 000000000..37a3fd13b --- /dev/null +++ b/csharp/Lib/Utils/StringParsing.cs @@ -0,0 +1,87 @@ +using System.Reactive.Linq; +using System.Text; + +namespace InnovEnergy.Lib.Utils; + +public static class StringParsing +{ + public static IObservable ParseLines(this IObservable> source, String eol = "\n") + { + return ParseLines(source, Encoding.UTF8, eol); + } + + public static IObservable ParseLines(this IObservable> source, Encoding encoding, String eol = "\n") + { + return source + .Select(d => encoding.GetString(d.Span)) + .ParseLines(eol); + } + + public static IObservable ParseLines(this IObservable source, String eol = "\n") + { + var accumulator = new StringBuilder(); + + return source + .Append(eol) + .SelectMany(SplitLines); + + IEnumerable SplitLines(String s) + { + if (!s.Contains(eol)) + { + accumulator.Append(s); + yield break; + } + + var split = s.Split(eol); // scatter + + yield return accumulator.Append(split[0]).ToString(); // gather + + foreach (var line in split[1..^1]) + yield return line; + + accumulator.Clear(); + accumulator.Append(split[^1]); + } + } + + public static IEnumerable ParseLines(this IEnumerable> source, String eol = "\n") + { + return ParseLines(source, Encoding.UTF8, eol); + } + + public static IEnumerable ParseLines(this IEnumerable> source, Encoding encoding, String eol = "\n") + { + return source + .Select(d => encoding.GetString(d.Span)) + .ParseLines(eol); + } + + public static IEnumerable ParseLines(this IEnumerable source, String eol = "\n") + { + var accumulator = new StringBuilder(); + + return source + .Append(eol) + .SelectMany(SplitLines); + + IEnumerable SplitLines(String s) + { + if (!s.Contains(eol)) + { + accumulator.Append(s); + yield break; + } + + var split = s.Split(eol); // scatter + + yield return accumulator.Append(split[0]).ToString(); // gather + + foreach (var line in split[1..^1]) + yield return line; + + accumulator.Clear(); + accumulator.Append(split[^1]); + } + } +} \ No newline at end of file