Created
August 2, 2019 16:21
-
-
Save GeorgeTsiokos/686e7481f3dd8f558d42d608e79213ee to your computer and use it in GitHub Desktop.
c# SimpleFixParser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static KeyValuePair<ushort, string>[] Parse(Span<byte> message) | |
{ | |
var values = new List<KeyValuePair<ushort, string>>(); | |
const byte Soh = 1; | |
const byte Equals = 61; | |
while (0 < message.Length) | |
{ | |
var soh = message.IndexOf(Soh); | |
if (-1 == soh) | |
break; | |
var kvp = message.Slice(0, soh); | |
var equals = kvp.IndexOf(Equals); | |
if (-1 != equals && Utf8Parser.TryParse(kvp.Slice(0, equals), out ushort key, out _)) | |
{ | |
var value = Encoding.ASCII.GetString(kvp.Slice(equals + 1, kvp.Length - equals - 1)); | |
values.Add(new KeyValuePair<ushort, string>(key, value)); | |
} | |
message = message.Slice(soh + 1); | |
} | |
return values.ToArray(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SimpleFixParser | |
{ | |
public static IEnumerable<KeyValuePair<ushort, string>> Parse(string message) | |
{ | |
var values = message.Split((char) 1, StringSplitOptions.RemoveEmptyEntries); | |
foreach (var kvp in values) | |
{ | |
var deliminator = kvp.IndexOf('='); | |
var tag = ushort.Parse(kvp.Substring(0, deliminator)); | |
var value = kvp.Substring(deliminator + 1); | |
yield return new KeyValuePair<ushort, string>(tag, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment