blob: e432b6c4517dce89534a4746b51aca7aa0c60d17 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* File: fix/parser.hpp
* Purpose: Financial Information Exchange parser in C++
* Author: Amlal El Mahrouss (founder@snu.systems)
* Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved.
*/
#ifndef _SNU_FIX_PARSER_HPP
#define _SNU_FIX_PARSER_HPP
#include <cstddef>
#include <cassert>
#include <utility>
#include <string>
#include <vector>
namespace snu::fix
{
template <typename char_type>
struct visitor;
template <typename char_type>
struct range;
template <typename char_type>
struct range_data;
/// @brief Buffer+Length structure
template <typename char_type>
using range_ptr_t = range<char_type>;
namespace detail
{
template <typename char_type>
const char_type* begin_fix();
template <>
inline const char* begin_fix<char>()
{
return "FIX.";
}
template <>
inline const char16_t* begin_fix<char16_t>()
{
return u"FIX.";
}
template <>
inline const char8_t* begin_fix<char8_t>()
{
return u8"FIX.";
}
} // namespace detail
template <typename char_type>
struct range final
{
char_type* bytes_;
size_t length_;
bool is_valid()
{
return bytes_ && length_ > 0;
}
operator bool()
{
return this->is_valid();
}
};
/// @brief Convert range to usable string.
/// @note This function assumes that the range is valid and contains ASCII bytes.
template <typename char_type>
inline std::basic_string<char_type> to_string(range<char_type>& range) noexcept
{
if (range.length_ < 0)
return std::basic_string<char_type>{};
return std::basic_string<char_type>(range.ascii_bytes_, range.length_);
}
/// @brief a range object containing the FIX packet values.
template <typename char_type>
class range_data final
{
public:
std::basic_string<char_type> msg_magic_;
std::size_t msg_body_len_;
std::vector<std::pair<std::basic_string<char_type>, std::basic_string<char_type>>> msg_body_;
static inline const char_type* begin = detail::begin_fix<char_type>();
explicit range_data() = default;
~range_data() = default;
range_data& operator=(const range_data&) = default;
range_data(const range_data&) = default;
std::basic_string<char_type> operator[](const std::basic_string<char_type>& key)
{
if (key.empty())
{
return std::basic_string<char_type>{};
}
for (const auto& pair : msg_body_)
{
if (pair.first == key)
{
return pair.second;
}
}
return std::basic_string<char_type>{};
}
bool is_valid()
{
return msg_magic_.starts_with(range_data<char_type>::begin);
}
operator bool()
{
return this->is_valid();
}
};
/// @brief visitor object which returns a fix::range_data instance.
template <typename char_type>
class visitor final
{
public:
static constexpr const char_type soh = '|';
static constexpr const char_type eq = '=';
static constexpr uint32_t base = 10U;
explicit visitor() = default;
~visitor() = default;
visitor& operator=(const visitor&) = default;
visitor(const visitor&) = default;
range<char_type> operator()(const std::basic_string<char_type>& in)
{
return this->visit(in);
}
range_data<char_type> visit(const std::basic_string<char_type>& in)
{
thread_local range_data<char_type> ret{};
if (in.empty())
return ret;
thread_local std::basic_string<char_type> in_tmp{};
in_tmp.reserve(in.size());
try
{
for (auto& ch : in)
{
if (ch != visitor::soh)
{
in_tmp += ch;
continue;
}
std::basic_string<char_type> key = in_tmp.substr(0, in_tmp.find(visitor::eq));
std::basic_string<char_type> val = in_tmp.substr(in_tmp.find(visitor::eq) + 1);
if (ret.msg_magic_.empty())
{
ret.msg_magic_ = val;
}
else
{
ret.msg_body_.emplace_back(std::make_pair(key, val));
ret.msg_body_len_ += in_tmp.size();
}
in_tmp.clear();
}
}
catch (...)
{
in_tmp.clear();
return ret;
}
in_tmp.clear();
return ret;
}
};
} // namespace snu::fix
#endif // ifndef _SNU_FIX_PARSER_HPP
|