Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read message type from fix string without regex #909

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions QuickFIXn/Message/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,34 @@ public static SessionID GetReverseSessionId(string msg)
/// <exception cref="MessageParseError">if 35 tag is missing or malformed</exception>
public static string GetMsgType(string fixstring)
{
Match match = Regex.Match(fixstring, SOH + "35=([^" + SOH + "]*)" + SOH);
if (match.Success)
return match.Groups[1].Value;
// 2=3|3=E|35=0|55=ab|545=xx| -> good message, msg type = 0
// 2=3|3=E|35=AX|55=ab|545=xx| -> good message, msg type = AX
// 35=5| -> these two are garbled messages but the old regex behavior
// |35=A| -> read them just fine, so this one does as well
var chars = fixstring.AsSpan();
var l = 0;
var r = 1;

if (chars.Length > 0 && chars[0] == SOH) l = 1;
while (r < chars.Length)
{
if (chars[r] == SOH)
{
if (r - l >= 4 &&
chars[l] == '3' &&
chars[l + 1] == '5' &&
chars[l + 2] == '=')
{
return new string(chars[(l + 3)..r]);
}
else
{
l = r + 1;
}
}

r++;
}

throw new MessageParseError("missing or malformed tag 35 in msg: " + fixstring);
}
Expand Down
8 changes: 6 additions & 2 deletions UnitTests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,14 @@ public void GetMsgTypeTest() {
+ "55=sym|268=1|269=0|272=20111012|273=22:15:30.444|10=19|").Replace('|', Message.SOH);
Assert.That(Message.GetMsgType(msgStr), Is.EqualTo("W"));

string msgStr2 = ("8=FIX.4.4|9=104|35=AW|34=3|49=sender|52=20110909-09:09:09.999|56=target"
+ "55=sym|268=1|269=0|272=20111012|273=22:15:30.444|10=19|").Replace('|', Message.SOH);
Assert.That(Message.GetMsgType(msgStr2), Is.EqualTo("AW"));

// invalid 35 value, let it ride
string msgStr2 = ("8=FIX.4.4|9=68|35=*|34=3|49=sender|52=20110909-09:09:09.999|56=target"
string msgStr3 = ("8=FIX.4.4|9=68|35=*|34=3|49=sender|52=20110909-09:09:09.999|56=target"
+ "55=sym|268=0|10=9|").Replace('|', Message.SOH);
Assert.That(Message.GetMsgType(msgStr2), Is.EqualTo("*"));
Assert.That(Message.GetMsgType(msgStr3), Is.EqualTo("*"));
}

[Test]
Expand Down