OnixS C++ eSpeed ITCH Market Data Handler 1.7.3
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
33
35
37typedef char Char;
38
40class StrRef
41{
42public:
44 typedef const Char* Iterator;
45
47 ONIXS_ESPEED_ITCH_CONSTEXPR
50 : items_(ONIXS_ESPEED_ITCH_NULLPTR)
51 , size_(0)
52 {
53 }
54
57 const Char* chars,
58 size_t size)
60 : items_(chars)
61 , size_(size)
62 {
63 }
64
66 template <size_t Size>
67 explicit
68 ONIXS_ESPEED_ITCH_CONSTEXPR
69 StrRef(const char (&value)[Size])
71 : items_(value)
72 , size_(Size - 1)
73 {
74 }
75
77 explicit
79 const std::string& string)
81 : items_(string.c_str())
82 , size_(string.size())
83 {
84 }
85
91 const StrRef& other)
93 : items_(other.items_)
94 , size_(other.size_)
95 {
96 }
97
98#if defined(ONIXS_ESPEED_ITCH_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_ESPEED_ITCH_COMPILER_CXX_RVALUE_REFERENCES
99
100 ONIXS_ESPEED_ITCH_CONSTEXPR
101 StrRef(
102 StrRef&& other)
104 : items_(std::move(other.items_))
105 , size_(std::move(other.size_))
106 {
107 }
108
109 StrRef& operator= (StrRef&& other)
111 {
112 if (this == &other)
113 return *this;
114
115 items_ = std::move(other.items_);
116 size_ = std::move(other.size_);
117
118 return *this;
119 }
120
121#endif
122
124 bool empty() const
126 {
127 return (0 == size_);
128 }
129
131 const Char* items() const
133 {
134 return items_;
135 }
136
138 size_t size() const
140 {
141 return size_;
142 }
143
147 {
148 return items_;
149 }
150
152 Iterator end() const
154 {
155 return (items_ + size_);
156 }
157
159 void reset()
161 {
162 reset(ONIXS_ESPEED_ITCH_NULLPTR, 0);
163 }
164
166 void
168 const Char* chars,
169 size_t size)
171 {
172 items_ = chars;
173 size_ = size;
174 }
175
176 const Char&
178 size_t index) const
179 {
180 return items_[index];
181 }
182
183 const Char&
185 size_t index) const
186 {
187 if (index < size_)
188 return items_[index];
189
190 throw std::invalid_argument("index");
191 }
192
194 StrRef&
196 const StrRef& other)
198 {
199 items_ = other.items_;
200 size_ = other.size_;
201
202 return *this;
203 }
204
206 void
208 StrRef& other)
210 {
211 std::swap(items_, other.items_);
212 std::swap(size_, other.size_);
213 }
214
215 // Get trimmed presentation of the current string
216 StrRef trim() const
218 {
219 size_t length = size();
220
221 while(length > 0 && isspace(operator[](length - 1)))
222 --length;
223
224 return StrRef(items(), length);
225 }
226
227private:
229 const Char* items_;
230
232 size_t size_;
233};
234
236inline
237 StrRef
239 const std::string& str)
240{
241 return StrRef(str.data(), str.size());
242}
243
245inline
246 StrRef
248 const Char* cStr)
249{
250 return
251 StrRef(
252 cStr,
253 cStr
254 ? strlen(cStr)
255 : 0
256 );
257}
258
260inline
261 std::string
263 const StrRef& ref)
264{
265 return std::string(ref.items(), ref.size());
266}
267
269inline
270 void
272 std::string& str,
273 const StrRef& ref)
274{
275 str.append(ref.items(), ref.size());
276}
277
279inline
280 std::string
282 Char character)
283{
284 return std::string(1, character);
285}
286
288inline
289 void
291 std::string& str,
292 Char character)
293{
294 str.append(1, character);
295}
296
298inline
299 void
301 std::string& str,
302 const std::string& value)
303{
304 str.append(value);
305}
306
308inline
309 bool
311 const StrRef& left,
312 const StrRef& right)
313{
314 return (
315 left.size() == right.size()
316 &&
317 0 == memcmp(
318 left.items(), right.items(), left.size()));
319}
320
322inline
323 bool
325 const StrRef& left,
326 const StrRef& right)
327{
328 return !(left == right);
329}
330
332inline
333 bool
335 const StrRef& ref,
336 const std::string& str)
337{
338 return ref == toStrRef(str);
339}
340
342inline
343 bool
345 const StrRef& ref,
346 const std::string& str)
347{
348 return ref != toStrRef(str);
349}
350
352inline
353 bool
355 const std::string& str,
356 const StrRef& ref)
357{
358 return ref == toStrRef(str);
359}
360
362inline
363 bool
365 const std::string& str,
366 const StrRef& ref)
367{
368 return ref != toStrRef(str);
369}
370
372inline
373 bool
375 const StrRef& ref,
376 const Char* str)
377{
378 return ref == toStrRef(str);
379}
380
382inline
383 bool
385 const StrRef& ref,
386 const Char* str)
387{
388 return ref != toStrRef(str);
389}
390
392inline
393 bool
395 const Char* str,
396 const StrRef& ref)
397{
398 return ref == toStrRef(str);
399}
400
402inline
403 bool
405 const Char* str,
406 const StrRef& ref)
407{
408 return ref != toStrRef(str);
409}
410
412inline
413 bool
415 const StrRef& left,
416 const StrRef& right)
417{
418 ptrdiff_t
419 compareResult =
420 memcmp(
421 left.items(),
422 right.items(),
423 left.size() < right.size()
424 ? left.size()
425 : right.size());
426
427 if (0 == compareResult)
428 {
429 compareResult =
430 static_cast<ptrdiff_t>(
431 left.size() - right.size());
432 }
433
434 return (0 > compareResult);
435}
436
438inline
439 bool
441 const StrRef& left,
442 const StrRef& right)
443{
444 return (right < left);
445}
446
448inline
449 std::ostream&
451 std::ostream& stream,
452 const StrRef& text)
453{
454 stream.write(text.items(), text.size());
455
456 return stream;
457}
458
#define ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN
Definition Bootstrap.h:27
#define ONIXS_ESPEED_ITCH_NAMESPACE_END
Definition Bootstrap.h:31
#define ONIXS_ESPEED_ITCH_NOTHROW
Definition Compiler.h:27
ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN typedef char Char
Character type alias.
Definition String.h:37
std::string toStr(const StrRef &ref)
Constructs std::string instance from StrRef one.
Definition String.h:262
bool operator>(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition String.h:440
bool operator<(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition String.h:414
bool operator==(const StrRef &left, const StrRef &right)
Compares StrRef instance with another one.
Definition String.h:310
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition String.h:238
bool operator!=(const StrRef &left, const StrRef &right)
Compares with another instance.
Definition String.h:324
std::ostream & operator<<(std::ostream &stream, const StrRef &text)
StrRef serialization operator.
Definition String.h:450
Provides efficient way of accessing text-based FIX field values.
Definition String.h:41
bool empty() const ONIXS_ESPEED_ITCH_NOTHROW
Indicates whether array of zero length.
Definition String.h:124
StrRef(const StrRef &other) ONIXS_ESPEED_ITCH_NOTHROW
Definition String.h:90
const Char * items() const ONIXS_ESPEED_ITCH_NOTHROW
Read-only content.
Definition String.h:131
StrRef(const Char *chars, size_t size) ONIXS_ESPEED_ITCH_NOTHROW
Full initialization.
Definition String.h:56
ONIXS_ESPEED_ITCH_CONSTEXPR StrRef() ONIXS_ESPEED_ITCH_NOTHROW
Initializes blank instance.
Definition String.h:48
void swap(StrRef &other) ONIXS_ESPEED_ITCH_NOTHROW
Swaps content with other instance.
Definition String.h:207
void reset() ONIXS_ESPEED_ITCH_NOTHROW
Resets reference to nothing.
Definition String.h:159
const Char * Iterator
STL-like iterator.
Definition String.h:44
size_t size() const ONIXS_ESPEED_ITCH_NOTHROW
Number of chars.
Definition String.h:138
StrRef & operator=(const StrRef &other) ONIXS_ESPEED_ITCH_NOTHROW
Reinitializes from another instance.
Definition String.h:195
void reset(const Char *chars, size_t size) ONIXS_ESPEED_ITCH_NOTHROW
Updates data being referenced.
Definition String.h:167
Iterator end() const ONIXS_ESPEED_ITCH_NOTHROW
STL-like end().
Definition String.h:152
ONIXS_ESPEED_ITCH_CONSTEXPR StrRef(const char(&value)[Size]) ONIXS_ESPEED_ITCH_NOTHROW
Full initialization.
Definition String.h:69
const Char & operator[](size_t index) const
Definition String.h:177
StrRef trim() const ONIXS_ESPEED_ITCH_NOTHROW
Definition String.h:216
StrRef(const std::string &string) ONIXS_ESPEED_ITCH_NOTHROW
Full initialization.
Definition String.h:78
const Char & at(size_t index) const
Definition String.h:184
Iterator begin() const ONIXS_ESPEED_ITCH_NOTHROW
STL-like begin().
Definition String.h:145