From fccb1407717a3655e7a95115ff9f8cd084a68f72 Mon Sep 17 00:00:00 2001 From: cypherpotato Date: Sat, 17 Aug 2024 03:18:30 -0300 Subject: [PATCH] parsing improvements --- .../Sisk.IniConfiguration/IniDocument.cs | 2 +- .../Sisk.IniConfiguration/IniSection.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/extensions/Sisk.IniConfiguration/IniDocument.cs b/extensions/Sisk.IniConfiguration/IniDocument.cs index ce6d72c..35eca07 100644 --- a/extensions/Sisk.IniConfiguration/IniDocument.cs +++ b/extensions/Sisk.IniConfiguration/IniDocument.cs @@ -69,7 +69,7 @@ public static IniDocument FromStream(TextReader reader) internal IniDocument(IniSection[] sections) { - Sections = sections; + Sections = IniSection.MergeIniSections(sections); } /// diff --git a/extensions/Sisk.IniConfiguration/IniSection.cs b/extensions/Sisk.IniConfiguration/IniSection.cs index cbfc347..879bcaa 100644 --- a/extensions/Sisk.IniConfiguration/IniSection.cs +++ b/extensions/Sisk.IniConfiguration/IniSection.cs @@ -16,6 +16,34 @@ public sealed class IniSection : IReadOnlyDictionary /// public string Name { get; } + internal static IniSection[] MergeIniSections(IniSection[] sections) + { + var sectionNames = sections + .DistinctBy(s => s.Name, IniReader.IniNamingComparer) + .Select(s => s.Name) + .ToArray(); + + List result = new List(sectionNames.Length); + for (int i = 0; i < sectionNames.Length; i++) + { + string currentName = sectionNames[i]; + List<(string, string)> allProperties = new(); + + for (int j = 0; j < sections.Length; j++) + { + IniSection s = sections[j]; + if (IniReader.IniNamingComparer.Compare(s.Name, currentName) == 0) + { + allProperties.AddRange(s.items); + } + } + + result.Add(new IniSection(currentName, allProperties.ToArray())); + } + + return result.ToArray(); + } + internal IniSection(string name, (string, string)[] items) { this.items = items;