OnixS C++ CBOE CFE Binary Order Entry (BOE) Handler 1.12.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
33namespace OnixS
34{
35 namespace CboeCFE
36 {
37 namespace Trading
38 {
39 namespace BOE
40 {
42 typedef char Char;
43
45 class StrRef
46 {
47 public:
49 typedef const Char* Iterator;
50
53 : items_(ONIXS_BATS_BOE_NULLPTR)
54 , size_(0)
55 {
56 }
57
60 : items_(chars)
61 , size_(size)
62 {
63 }
64
66 StrRef(const std::string& data) ONIXS_BATS_BOE_NOEXCEPT
67 : items_(data.c_str())
68 , size_(data.size())
69 {
70 }
71
74 : items_(chars)
75 , size_(0)
76 {
77 size_ = strnlen(chars, 1024);
78 }
79
85 : items_(other.items_)
86 , size_(other.size_)
87 {
88 }
89
92 {
93 return (0 == size_);
94 }
95
98 {
99 return items_;
100 }
101
104 {
105 return size_;
106 }
107
110 {
111 return items_;
112 }
113
116 {
117 return (items_ + size_);
118 }
119
122 {
123 reset(ONIXS_BATS_BOE_NULLPTR, 0);
124 }
125
127 void
129 const Char* chars,
131 {
132 items_ = chars;
133 size_ = size;
134 }
135
136 const Char&
138 size_t index) const ONIXS_BATS_BOE_NOEXCEPT
139 {
140 return items_[index];
141 }
142
143 const Char&
145 size_t index) const
146 {
147 if (index < size_)
148 return items_[index];
149
150 throw std::invalid_argument("index");
151 }
152
154 StrRef&
156 const StrRef& other)
157 {
158 items_ = other.items_;
159 size_ = other.size_;
160
161 return *this;
162 }
163
165 void
168 {
169 std::swap(items_, other.items_);
170 std::swap(size_, other.size_);
171 }
172
173 private:
175 const Char* items_;
176
178 size_t size_;
179 };
180
182 inline
183 StrRef
185 const std::string& str)
186 {
187 return StrRef(str.data(), str.size());
188 }
189
191 inline
192 StrRef
194 const Char* cStr)
195 {
196 return
197 StrRef(
198 cStr,
199 cStr
200 ? strlen(cStr)
201 : 0
202 );
203 }
204
206 inline
207 std::string
209 StrRef ref)
210 {
211 return std::string(ref.items(), ref.size());
212 }
213
215 inline
216 void
218 std::string& str,
219 StrRef ref)
220 {
221 str.append(ref.items(), ref.size());
222 }
223
225 template <size_t Size>
226 void toStr(std::string& str, const char (&value)[Size])
227 {
228 str.append(value, Size - 1);
229 }
230
232 inline
233 void
235 std::string& str,
236 Char character)
237 {
238 str.append(1, character);
239 }
240
242 inline
243 void
245 std::string& str,
246 const std::string& value)
247 {
248 str.append(value);
249 }
250
252 inline
253 bool
255 const StrRef& left,
256 const StrRef& right)
257 {
258 return (
259 left.size() == right.size()
260 &&
261 0 == memcmp(
262 left.items(), right.items(), left.size()));
263 }
264
266 inline
267 bool
269 const StrRef& left,
270 const StrRef& right)
271 {
272 return !(left == right);
273 }
274
276 inline
277 bool
279 const StrRef& ref,
280 const std::string& str)
281 {
282 return ref == toStrRef(str);
283 }
284
286 inline
287 bool
289 const StrRef& ref,
290 const std::string& str)
291 {
292 return ref != toStrRef(str);
293 }
294
296 inline
297 bool
299 const std::string& str,
300 const StrRef& ref)
301 {
302 return ref == toStrRef(str);
303 }
304
306 inline
307 bool
309 const std::string& str,
310 const StrRef& ref)
311 {
312 return ref != toStrRef(str);
313 }
314
316 inline
317 bool
319 const StrRef& ref,
320 const Char* str)
321 {
322 return ref == toStrRef(str);
323 }
324
326 inline
327 bool
329 const StrRef& ref,
330 const Char* str)
331 {
332 return ref != toStrRef(str);
333 }
334
336 inline
337 bool
339 const Char* str,
340 const StrRef& ref)
341 {
342 return ref == toStrRef(str);
343 }
344
346 inline
347 bool
349 const Char* str,
350 const StrRef& ref)
351 {
352 return ref != toStrRef(str);
353 }
354
356 inline
357 bool
359 const StrRef& left,
360 const StrRef& right)
361 {
362 ptrdiff_t
363 compareResult =
364 memcmp(
365 left.items(),
366 right.items(),
367 left.size() < right.size()
368 ? left.size()
369 : right.size());
370
371 if (0 == compareResult)
372 {
373 compareResult =
374 static_cast<ptrdiff_t>(
375 left.size() - right.size());
376 }
377
378 return (0 > compareResult);
379 }
380
382 inline
383 bool
385 const StrRef& left,
386 const StrRef& right)
387 {
388 return (right < left);
389 }
390
392 inline
393 std::ostream&
395 std::ostream& stream,
396 const StrRef& text)
397 {
398 stream.write(text.items(), text.size());
399
400 return stream;
401 }
402
403 template <size_t Size>
404 inline StrRef constructStrRef(const char (&value)[Size])
405 {
406 return StrRef(value, Size - 1);
407 }
408
409 }
410 }
411 }
412}
#define ONIXS_BATS_BOE_NOEXCEPT
Definition ABI.h:49
Provides efficient way of accessing text-based field values.
Definition String.h:46
void swap(StrRef &other) ONIXS_BATS_BOE_NOEXCEPT
Swaps content with other instance.
Definition String.h:166
StrRef() ONIXS_BATS_BOE_NOEXCEPT
Initializes blank instance.
Definition String.h:52
void reset(const Char *chars, size_t size) ONIXS_BATS_BOE_NOEXCEPT
Updates data being referenced.
Definition String.h:128
Iterator end() const ONIXS_BATS_BOE_NOEXCEPT
STL-like end().
Definition String.h:115
void reset() ONIXS_BATS_BOE_NOEXCEPT
Resets reference to nothing.
Definition String.h:121
StrRef(const std::string &data) ONIXS_BATS_BOE_NOEXCEPT
Full initialization.
Definition String.h:66
const Char & operator[](size_t index) const ONIXS_BATS_BOE_NOEXCEPT
Definition String.h:137
StrRef(const StrRef &other) ONIXS_BATS_BOE_NOEXCEPT
Definition String.h:84
const Char * Iterator
STL-like iterator.
Definition String.h:49
bool empty() const ONIXS_BATS_BOE_NOEXCEPT
Indicates whether array of zero length.
Definition String.h:91
StrRef(const Char *chars) ONIXS_BATS_BOE_NOEXCEPT
Explicit initialization.
Definition String.h:73
Iterator begin() const ONIXS_BATS_BOE_NOEXCEPT
STL-like begin().
Definition String.h:109
StrRef(const Char *chars, size_t size) ONIXS_BATS_BOE_NOEXCEPT
Full initialization.
Definition String.h:59
StrRef & operator=(const StrRef &other)
Reinitializes from another instance.
Definition String.h:155
size_t size() const ONIXS_BATS_BOE_NOEXCEPT
Number of chars.
Definition String.h:103
const Char * items() const ONIXS_BATS_BOE_NOEXCEPT
Read-only content.
Definition String.h:97
const Char & at(size_t index) const
Definition String.h:144
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:131
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:117
std::ostream & operator<<(std::ostream &stream, const FixedPointDecimal< Mantissa, Exponent > &number)
Definition Decimal.h:174
StrRef constructStrRef(const char(&value)[Size])
Definition String.h:404
void toStr(std::string &str, const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition Decimal.h:156
char Char
Character type alias.
Definition String.h:42
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:110
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition String.h:184
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:124