OnixS C++ LSE GTP Market Data Handler 1.0.6
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
24
25#include <cstddef>
26#include <cstring>
27#include <string>
28#include <stdexcept>
29
30#include <algorithm>
31#include <ostream>
32
33namespace OnixS
34{
35 namespace LSE
36 {
37 namespace MarketData
38 {
39 namespace GTP
40 {
42 typedef char Char;
43
45 class StrRef
46 {
47 public:
49 typedef const Char* Iterator;
50
54 : items_(ONIXS_LSE_GTP_NULLPTR)
55 , size_(0)
56 {
57 }
58
59 template <size_t Size>
60 explicit
62 StrRef(const char (&value)[Size])
64 : items_(value)
65 , size_(Size - 1)
66 {
67 }
68
71 const Char* chars,
72 size_t size)
74 : items_(chars)
75 , size_(size)
76 {
77 }
78
84 const StrRef& other)
86 : items_(other.items_)
87 , size_(other.size_)
88 {
89 }
90
91#if defined(ONIXS_LSE_GTP_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_LSE_GTP_COMPILER_CXX_RVALUE_REFERENCES
92
93 StrRef(
94 StrRef&& other)
96 : items_(std::move(other.items_))
97 , size_(std::move(other.size_))
98 {
99 }
100
101 StrRef& operator= (StrRef&& other)
103 {
104 if (this == &other)
105 return *this;
106
107 items_ = std::move(other.items_);
108 size_ = std::move(other.size_);
109
110 return *this;
111 }
112
113#endif
114
116 bool empty() const
118 {
119 return (0 == size_);
120 }
121
123 const Char* items() const
125 {
126 return items_;
127 }
128
130 size_t size() const
132 {
133 return size_;
134 }
135
139 {
140 return items_;
141 }
142
144 Iterator end() const
146 {
147 return (items_ + size_);
148 }
149
156
158 void
160 const Char* chars,
161 size_t size)
163 {
164 items_ = chars;
165 size_ = size;
166 }
167
168 const Char&
170 size_t index) const
171 {
172 return items_[index];
173 }
174
175 const Char&
177 size_t index) const
178 {
179 if (index < size_)
180 return items_[index];
181
182 throw std::invalid_argument("index");
183 }
184
186 StrRef&
188 const StrRef& other)
190 {
191 items_ = other.items_;
192 size_ = other.size_;
193
194 return *this;
195 }
196
198 void
200 StrRef& other)
202 {
203 std::swap(items_, other.items_);
204 std::swap(size_, other.size_);
205 }
206
207 // Get trimmed presentation of the current string
208 StrRef trim() const
210 {
211 size_t length = size();
212
213 while(length > 0 && isspace(operator[](length - 1)))
214 --length;
215
216 return StrRef(items(), length);
217 }
218
219 private:
221 const Char* items_;
222
224 size_t size_;
225 };
226
228 inline
229 StrRef
231 const std::string& str)
232 {
233 return StrRef(str.data(), str.size());
234 }
235
237 inline
238 StrRef
240 const Char* cStr)
241 {
242 return
243 StrRef(
244 cStr,
245 cStr
246 ? strlen(cStr)
247 : 0
248 );
249 }
250
252 inline
253 std::string
255 const StrRef& ref)
256 {
257 return std::string(ref.items(), ref.size());
258 }
259
261 inline
262 void
264 std::string& str,
265 const StrRef& ref)
266 {
267 str.append(ref.items(), ref.size());
268 }
269
271 inline
272 std::string
274 Char character)
275 {
276 return std::string(1, character);
277 }
278
280 inline
281 void
283 std::string& str,
284 Char character)
285 {
286 str.append(1, character);
287 }
288
290 inline
291 void
293 std::string& str,
294 const std::string& value)
295 {
296 str.append(value);
297 }
298
300 inline
301 bool
303 const StrRef& left,
304 const StrRef& right)
305 {
306 return (
307 left.size() == right.size()
308 &&
309 0 == memcmp(
310 left.items(), right.items(), left.size()));
311 }
312
314 inline
315 bool
317 const StrRef& left,
318 const StrRef& right)
319 {
320 return !(left == right);
321 }
322
324 inline
325 bool
327 const StrRef& ref,
328 const std::string& str)
329 {
330 return ref == toStrRef(str);
331 }
332
334 inline
335 bool
337 const StrRef& ref,
338 const std::string& str)
339 {
340 return ref != toStrRef(str);
341 }
342
344 inline
345 bool
347 const std::string& str,
348 const StrRef& ref)
349 {
350 return ref == toStrRef(str);
351 }
352
354 inline
355 bool
357 const std::string& str,
358 const StrRef& ref)
359 {
360 return ref != toStrRef(str);
361 }
362
364 inline
365 bool
367 const StrRef& ref,
368 const Char* str)
369 {
370 return ref == toStrRef(str);
371 }
372
374 inline
375 bool
377 const StrRef& ref,
378 const Char* str)
379 {
380 return ref != toStrRef(str);
381 }
382
384 inline
385 bool
387 const Char* str,
388 const StrRef& ref)
389 {
390 return ref == toStrRef(str);
391 }
392
394 inline
395 bool
397 const Char* str,
398 const StrRef& ref)
399 {
400 return ref != toStrRef(str);
401 }
402
404 inline
405 bool
407 const StrRef& left,
408 const StrRef& right)
409 {
410 ptrdiff_t
411 compareResult =
412 memcmp(
413 left.items(),
414 right.items(),
415 left.size() < right.size()
416 ? left.size()
417 : right.size());
418
419 if (0 == compareResult)
420 {
421 compareResult =
422 static_cast<ptrdiff_t>(
423 left.size() - right.size());
424 }
425
426 return (0 > compareResult);
427 }
428
430 inline
431 bool
433 const StrRef& left,
434 const StrRef& right)
435 {
436 return (right < left);
437 }
438
440 inline
441 std::ostream&
443 std::ostream& stream,
444 const StrRef& text)
445 {
446 stream.write(text.items(), text.size());
447
448 return stream;
449 }
450
451 }
452 }
453 }
454}
#define ONIXS_LSE_GTP_NULLPTR
Definition Compiler.h:132
#define ONIXS_LSE_GTP_NOTHROW
Definition Compiler.h:118
#define ONIXS_LSE_GTP_CONSTEXPR
Definition Compiler.h:124
Provides efficient way of accessing text-based FIX field values.
Definition String.h:46
StrRef & operator=(const StrRef &other) ONIXS_LSE_GTP_NOTHROW
Reinitializes from another instance.
Definition String.h:187
ONIXS_LSE_GTP_CONSTEXPR StrRef(const char(&value)[Size]) ONIXS_LSE_GTP_NOTHROW
Definition String.h:62
StrRef trim() const ONIXS_LSE_GTP_NOTHROW
Definition String.h:208
Iterator end() const ONIXS_LSE_GTP_NOTHROW
STL-like end().
Definition String.h:144
StrRef() ONIXS_LSE_GTP_NOTHROW
Initializes blank instance.
Definition String.h:52
Iterator begin() const ONIXS_LSE_GTP_NOTHROW
STL-like begin().
Definition String.h:137
void reset(const Char *chars, size_t size) ONIXS_LSE_GTP_NOTHROW
Updates data being referenced.
Definition String.h:159
const Char * Iterator
STL-like iterator.
Definition String.h:49
StrRef(const Char *chars, size_t size) ONIXS_LSE_GTP_NOTHROW
Full initialization.
Definition String.h:70
void swap(StrRef &other) ONIXS_LSE_GTP_NOTHROW
Swaps content with other instance.
Definition String.h:199
StrRef(const StrRef &other) ONIXS_LSE_GTP_NOTHROW
Definition String.h:83
size_t size() const ONIXS_LSE_GTP_NOTHROW
Number of chars.
Definition String.h:130
void reset() ONIXS_LSE_GTP_NOTHROW
Resets reference to nothing.
Definition String.h:151
bool empty() const ONIXS_LSE_GTP_NOTHROW
Indicates whether array of zero length.
Definition String.h:116
const Char & operator[](size_t index) const
Definition String.h:169
const Char * items() const ONIXS_LSE_GTP_NOTHROW
Read-only content.
Definition String.h:123
const Char & at(size_t index) const
Definition String.h:176
bool operator==(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for equality.
Definition Time.h:326
bool operator>(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval greater than right one.
Definition Time.h:356
char Char
Character type alias.
Definition String.h:42
FixedPointDecimal< UInt64, IntegralConstant< Int8, -8 > > Size
Little-Endian encoded 64 bit unsigned integer with eight implied decimal places.
Definition Defines.h:111
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition String.h:230
bool operator<(const TimeSpan &left, const TimeSpan &right)
Checks whether left time interval less than right one.
Definition Time.h:346
ONIXS_LSE_GTP_API std::ostream & operator<<(std::ostream &stream, const ServiceDescriptor &descriptor)
ONIXS_LSE_GTP_API void toStr(std::string &, EventCode::Enum)
Appends string presentation of object.
bool operator!=(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for in-equality.
Definition Time.h:336