Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
fixed_string.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstring>
6#include <format>
7#include <ostream>
8#include <string>
9#include <string_view>
10
11namespace slick::sim::utils {
12
23template<size_t N>
24inline bool copy_wire_field(char (&dst)[N], std::string_view src) noexcept {
25 const auto len = std::min(src.size(), N - 1);
26 if (len) {
27 // Guarded because a default-constructed string_view has a null data()
28 // pointer, and memcpy from null is undefined even for zero bytes.
29 std::memcpy(dst, src.data(), len);
30 }
31 std::memset(dst + len, 0, N - len);
32 return len < src.size();
33}
34
47template<size_t N>
49 static_assert(N > 1, "fixed_string needs room for at least one character and a terminator");
50
51 char data_[N]{};
52
53public:
54 fixed_string() = default;
55 fixed_string(std::string_view value) noexcept { assign(value); }
56 fixed_string(const char *value) noexcept { assign(value ? std::string_view(value) : std::string_view()); }
57
58 fixed_string &operator=(std::string_view value) noexcept { assign(value); return *this; }
59 fixed_string &operator=(const char *value) noexcept {
60 assign(value ? std::string_view(value) : std::string_view());
61 return *this;
62 }
63
67 void assign(std::string_view value) noexcept { copy_wire_field(data_, value); }
68
69 void clear() noexcept { std::memset(data_, 0, N); }
70
71 const char *c_str() const noexcept { return data_; }
72 const char *data() const noexcept { return data_; }
73 char *data() noexcept { return data_; }
74
75 size_t size() const noexcept { return ::strnlen(data_, N); }
76 bool empty() const noexcept { return data_[0] == '\0'; }
77 static constexpr size_t capacity() noexcept { return N - 1; }
78 static constexpr size_t buffer_size() noexcept { return N; }
79
80 std::string_view view() const noexcept { return std::string_view(data_, size()); }
81 operator std::string_view() const noexcept { return view(); }
82 std::string str() const { return std::string(view()); }
83
84 friend bool operator==(const fixed_string &lhs, std::string_view rhs) noexcept {
85 return lhs.view() == rhs;
86 }
87 friend bool operator==(const fixed_string &lhs, const fixed_string &rhs) noexcept {
88 return std::memcmp(lhs.data_, rhs.data_, N) == 0;
89 }
90
91 friend std::ostream &operator<<(std::ostream &os, const fixed_string &value) {
92 return os << value.view();
93 }
94};
95
103 using is_transparent = void;
104 size_t operator()(std::string_view value) const noexcept {
105 return std::hash<std::string_view>{}(value);
106 }
107};
108
110 using is_transparent = void;
111 bool operator()(std::string_view lhs, std::string_view rhs) const noexcept {
112 return lhs == rhs;
113 }
114};
115
116} // namespace slick::sim::utils
117
120template<size_t N, class CharT>
121struct std::formatter<slick::sim::utils::fixed_string<N>, CharT> : std::formatter<std::string_view, CharT> {
122 template<class FormatContext>
123 auto format(const slick::sim::utils::fixed_string<N> &value, FormatContext &ctx) const {
124 return std::formatter<std::string_view, CharT>::format(value.view(), ctx);
125 }
126};
A null-terminated character buffer of fixed capacity, stored inline.
const char * data() const noexcept
fixed_string & operator=(std::string_view value) noexcept
fixed_string(std::string_view value) noexcept
static constexpr size_t capacity() noexcept
fixed_string(const char *value) noexcept
friend bool operator==(const fixed_string &lhs, const fixed_string &rhs) noexcept
static constexpr size_t buffer_size() noexcept
friend bool operator==(const fixed_string &lhs, std::string_view rhs) noexcept
void assign(std::string_view value) noexcept
Truncates anything that does not fit and zero-fills the tail, so the buffer is always null-terminated...
size_t size() const noexcept
friend std::ostream & operator<<(std::ostream &os, const fixed_string &value)
fixed_string & operator=(const char *value) noexcept
const char * c_str() const noexcept
std::string_view view() const noexcept
bool empty() const noexcept
Conversions between wire decimals with a constant exponent of -9 (CME PRICE9, ICE Decimal9) and the s...
Definition decimal.hpp:9
bool copy_wire_field(char(&dst)[N], std::string_view src) noexcept
Copies a name into a fixed-width wire field.
bool operator()(std::string_view lhs, std::string_view rhs) const noexcept
Transparent hash/equality for maps keyed by fixed_string.
size_t operator()(std::string_view value) const noexcept
auto format(const slick::sim::utils::fixed_string< N > &value, FormatContext &ctx) const