From 0ac7773ac08a0f8cc40f26a6f1ce37eab01103eb Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 4 May 2023 09:46:50 +0200 Subject: [PATCH] add/group extensions to parse strings --- csharp/Lib/Utils/StringParsing.cs | 87 +++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 csharp/Lib/Utils/StringParsing.cs 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