using System.Globalization; namespace MoniteurBaie.Utils; public class Splitter { const char DELIMITER = ','; public delegate T ValueReader(ReadOnlySpan s); private readonly string _str; private int _pos = 0, _charCount = 0, _nextPos = 0; public Splitter(string str) => _str = str; public bool MoveNext() { _pos = _nextPos; var i = _str.IndexOf(DELIMITER, _pos); (_charCount, _nextPos) = i < 0 ? (_str.Length - _pos, _str.Length) : (i - _pos, i + 1); return HasNext; } public bool HasNext => _pos < _str.Length; public T Read(ValueReader reader) => MoveNext() ? reader(_str.AsSpan(_pos, _charCount)) : throw new InvalidOperationException(); public string? ReadString() => Read(static s => new string(s)); public bool ReadBool() => Read(static s => s.Length == 1 ? s[0] switch { '0' => false, '1' => true, _ => throw new InvalidDataException() } : throw new InvalidDataException()); public int ReadInt() => Read(static s => int.Parse(s, provider: CultureInfo.InvariantCulture)); public uint ReadUInt() => Read(static s => uint.Parse(s, provider: CultureInfo.InvariantCulture)); public float ReadFloat() => Read(static s => float.Parse(s, provider: CultureInfo.InvariantCulture)); }