OnixS C++ CME MDP Premium Market Data Handler 5.9.0
API Documentation
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <cstddef>
24#include <cstring>
25#include <string>
26#include <stdexcept>
27
28#include <algorithm>
29#include <ostream>
30
32
34
36typedef char Char;
37
42{
43public:
45 typedef const Char* Iterator;
46
49 : items_(ONIXS_CMEMDH_NULLPTR)
50 , size_(0)
51 {
52 }
53
55 StrRef(const Char* chars, size_t size)
56 : items_(chars)
57 , size_(size)
58 {
59 }
60
64 StrRef(const StrRef& other)
65 : items_(other.items_)
66 , size_(other.size_)
67 {
68 }
69
71 bool empty() const
72 {
73 return (0 == size_);
74 }
75
77 const Char* items() const
78 {
79 return items_;
80 }
81
83 size_t size() const
84 {
85 return size_;
86 }
87
90 {
91 return items_;
92 }
93
95 Iterator end() const
96 {
97 return (items_ + size_);
98 }
99
101 void reset()
102 {
104 }
105
108 void reset(const Char* chars, size_t size)
109 {
110 items_ = chars;
111 size_ = size;
112 }
113
115 const Char& operator[](size_t index) const
116 {
117 return items_[index];
118 }
119
121 const Char& at(size_t index) const
122 {
123 if (index < size_)
124 return items_[index];
125
126 throw std::invalid_argument("index");
127 }
128
130 StrRef& operator=(const StrRef& other)
131 {
132 items_ = other.items_;
133 size_ = other.size_;
134
135 return *this;
136 }
137
139 void swap(StrRef& other)
140 {
141 std::swap(items_, other.items_);
142
143 std::swap(size_, other.size_);
144 }
145
146private:
147 // Items being referenced.
148 const Char* items_;
149
150 // Number of chars being referenced.
151 size_t size_;
152};
153
155inline StrRef toStrRef(const std::string& str)
156{
157 return StrRef(str.data(), str.size());
158}
159
161inline StrRef toStrRef(const Char* cStr)
162{
163 return StrRef(cStr, cStr ? strlen(cStr) : 0);
164}
165
167inline std::string toStr(const StrRef& ref)
168{
169 return std::string(ref.items(), ref.size());
170}
171
173inline void toStr(std::string& str, const StrRef& ref)
174{
175 str.append(ref.items(), ref.size());
176}
177
179inline std::string toStr(Char character)
180{
181 return std::string(1, character);
182}
183
185inline void toStr(std::string& str, Char character)
186{
187 str.append(1, character);
188}
189
191inline void toStr(std::string& str, const std::string& value)
192{
193 str.append(value);
194}
195
197inline bool operator==(const StrRef& left, const StrRef& right)
198{
199 return (left.size() == right.size() && 0 == memcmp(left.items(), right.items(), left.size()));
200}
201
203inline bool operator!=(const StrRef& left, const StrRef& right)
204{
205 return !(left == right);
206}
207
209inline bool operator==(const StrRef& ref, const std::string& str)
210{
211 return ref == toStrRef(str);
212}
213
215inline bool operator!=(const StrRef& ref, const std::string& str)
216{
217 return ref != toStrRef(str);
218}
219
221inline bool operator==(const std::string& str, const StrRef& ref)
222{
223 return ref == toStrRef(str);
224}
225
227inline bool operator!=(const std::string& str, const StrRef& ref)
228{
229 return ref != toStrRef(str);
230}
231
233inline bool operator==(const StrRef& ref, const Char* str)
234{
235 return ref == toStrRef(str);
236}
237
239inline bool operator!=(const StrRef& ref, const Char* str)
240{
241 return ref != toStrRef(str);
242}
243
245inline bool operator==(const Char* str, const StrRef& ref)
246{
247 return ref == toStrRef(str);
248}
249
251inline bool operator!=(const Char* str, const StrRef& ref)
252{
253 return ref != toStrRef(str);
254}
255
257inline bool operator<(const StrRef& left, const StrRef& right)
258{
259 const int result = memcmp(left.items(), right.items(), left.size() < right.size() ? left.size() : right.size());
260
261 return (0 != result ? (0 > result) : (left.size() < right.size()));
262}
263
265inline bool operator>(const StrRef& left, const StrRef& right)
266{
267 return (right < left);
268}
269
271inline std::ostream& operator<<(std::ostream& stream, const StrRef& text)
272{
273 stream.write(text.items(), text.size());
274
275 return stream;
276}
277
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:67
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:46
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:68
#define ONIXS_CMEMDH_NULLPTR
Definition Compiler.h:178
Provides efficient way of accessing text-based values without copying content of the text being refer...
Definition String.h:42
const Char * items() const
Read-only content.
Definition String.h:77
Iterator begin() const
STL-like begin().
Definition String.h:89
StrRef(const Char *chars, size_t size)
Explicit reference initialization.
Definition String.h:55
size_t size() const
Number of chars.
Definition String.h:83
void reset(const Char *chars, size_t size)
Updates the given instance to refer to the new text.
Definition String.h:108
Iterator end() const
STL-like end().
Definition String.h:95
bool empty() const
Indicates whether the referenced text is empty.
Definition String.h:71
const Char * Iterator
STL-like iterator.
Definition String.h:45
void swap(StrRef &other)
Swaps content with the other instance.
Definition String.h:139
StrRef()
Initializes the instance referring to nothing.
Definition String.h:48
void reset()
Resets the reference to nothing.
Definition String.h:101
const Char & operator[](size_t index) const
Provides per-item access.
Definition String.h:115
StrRef & operator=(const StrRef &other)
Reinitializes from the other instance.
Definition String.h:130
StrRef(const StrRef &other)
Initializes as the clone of other instance.
Definition String.h:64
const Char & at(size_t index) const
Provides bound-checked per-item access.
Definition String.h:121
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:110
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:96
char Char
Character type alias.
Definition String.h:36
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:89
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition String.h:155
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:103
std::ostream & operator<<(std::ostream &stream, const IssueArgs &args)