36[[nodiscard]]
constexpr DecimalError classify(std::string_view s)
noexcept {
38 std::size_t i = (s[0] ==
'-' || s[0] ==
'+') ? 1 : 0;
39 std::size_t int_digits = 0;
40 for (; i < s.size() && s[i] !=
'.'; ++i) {
45 for (++i; i < s.size(); ++i) {
56[[nodiscard]]
inline Result<F, DecimalError>
parse_fixed(std::string_view s)
noexcept {
57 if (
const auto v = F::from_decimal(s))
return *v;
58 return fail(detail::classify(s));
61 return parse_fixed<Price>(s);
69 if (
const auto v = F::from_decimal(s))
return *v;
70 const std::size_t dot = s.find(
'.');
72 if (dot == std::string_view::npos || s.size() - dot - 1 <= kKeep)
73 return fail(detail::classify(s));
74 for (std::size_t i = dot + 1; i < s.size(); ++i) {
77 const auto v = F::from_decimal(s.substr(0, dot + 1 + kKeep));
78 if (!v)
return fail(detail::classify(s));
79 if (s[dot + 1 + kKeep] <
'5')
return *v;
80 const std::int64_t up = v->raw < 0 || (v->raw == 0 && s[0] ==
'-') ? -1 : 1;
81 if (up > 0 && v->raw == std::numeric_limits<std::int64_t>::max())
83 if (up < 0 && v->raw == std::numeric_limits<std::int64_t>::min())
85 return F::from_raw(v->raw + up);
88[[nodiscard]]
inline Result<Price, DecimalError>
parse_avg_price(std::string_view s)
noexcept {
89 return parse_rounded<Price>(s);
93 return parse_rounded<Notional>(s);
96 return parse_fixed<Qty>(s);
99 return parse_fixed<Notional>(s);
113 constexpr std::uint64_t kMax =
114 static_cast<std::uint64_t
>(std::numeric_limits<std::int64_t>::max());
115 for (; i < s.size(); ++i) {
118 const auto d =
static_cast<std::uint64_t
>(c -
'0');
122 return neg ? -
static_cast<std::int64_t
>(v) : static_cast<std::int64_t>(v);
126[[nodiscard]]
constexpr Timestamp
ts_from_ms(std::int64_t ms)
noexcept {
127 return Timestamp{ms * 1'000'000};
131 if (!v)
return fail(v.error());
142 [[nodiscard]] std::string_view
view() const noexcept {
return {buf_, len_}; }
143 [[nodiscard]]
const char*
c_str() const noexcept {
return buf_; }
144 [[nodiscard]] std::size_t
size() const noexcept {
return len_; }
152[[nodiscard]]
constexpr std::size_t
format_int64(std::int64_t v,
char* out)
noexcept {
155 std::uint64_t mag = v < 0 ? 0 -
static_cast<std::uint64_t
>(v) : static_cast<std::uint64_t>(v);
157 tmp[n++] =
static_cast<char>(
'0' + mag % 10);
161 if (v < 0) out[w++] =
'-';
162 while (n > 0) out[w++] = tmp[--n];
Stack formatting buffer: DecimalText t(px); send(t.view()); - no heap, no locale.
Definition decimal.hpp:138
std::string_view view() const noexcept
Definition decimal.hpp:144
const char * c_str() const noexcept
Definition decimal.hpp:145
std::size_t size() const noexcept
Definition decimal.hpp:146
Minimal expected<T, E>: the hot path never throws, it returns Result.
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Result< F, DecimalError > parse_rounded(std::string_view s) noexcept
A value the venue computed rather than one an order carries can have more fraction digits than 8: Bin...
Definition decimal.hpp:70
constexpr Timestamp ts_from_ms(std::int64_t ms) noexcept
Milliseconds since the Unix epoch -> Timestamp (ns).
Definition decimal.hpp:128
Result< Price, DecimalError > parse_avg_price(std::string_view s) noexcept
A position's entry price, an average fill price.
Definition decimal.hpp:90
DecimalError
Definition decimal.hpp:19
@ Malformed
non-digit, exponent, whitespace, > 8 significant fraction digits
@ Overflow
does not fit the int64 1e-8 range
Result< Price, DecimalError > parse_price(std::string_view s) noexcept
Definition decimal.hpp:62
Result< Notional, DecimalError > parse_notional(std::string_view s) noexcept
Definition decimal.hpp:100
Result< Timestamp, DecimalError > parse_ts_ms(std::string_view s) noexcept
Definition decimal.hpp:131
constexpr std::string_view to_string(ChannelState s) noexcept
Definition venue.hpp:51
Result< Qty, DecimalError > parse_qty(std::string_view s) noexcept
Definition decimal.hpp:97
constexpr std::size_t format_int64(std::int64_t v, char *out) noexcept
Formats an int64 into a caller buffer (>= 21 chars); returns the length.
Definition decimal.hpp:154
Result< std::int64_t, DecimalError > parse_int64(std::string_view s) noexcept
"[-]digits" -> int64; rejects empty, signs other than a leading '-', overflow.
Definition decimal.hpp:105
Result< F, DecimalError > parse_fixed(std::string_view s) noexcept
Definition decimal.hpp:58
Result< Notional, DecimalError > parse_fee(std::string_view s) noexcept
A commission, which the venue computes from the fill.
Definition decimal.hpp:94
@ Malformed
JSON error or missing/invalid field.
@ Overflow
ring full (market data) or output buffer too small
constexpr int kFixedDecimals
Definition fixed_point.hpp:24
constexpr Unexpected< std::decay_t< E > > fail(E &&e) noexcept
Definition result.hpp:25
constexpr std::size_t kMaxDecimalChars
sign + 11 integer digits + '.' + 8 fraction digits + NUL
Definition fixed_point.hpp:26
Definition fixed_point.hpp:150