OnixS C++ CME MDP Streamlined Market Data Handler 1.2.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
40{
41public:
43 typedef const Char* Iterator;
44
47 : items_(0)
48 , size_(0)
49 {
50 }
51
54 const Char* chars,
55 size_t size)
56 : items_(chars)
57 , size_(size)
58 {
59 }
60
66 const StrRef& other)
67 : items_(other.items_)
68 , size_(other.size_)
69 {
70 }
71
73 bool empty() const
74 {
75 return (0 == size_);
76 }
77
79 const Char* items() const
80 {
81 return items_;
82 }
83
85 size_t size() const
86 {
87 return size_;
88 }
89
92 {
93 return items_;
94 }
95
97 Iterator end() const
98 {
99 return (items_ + size_);
100 }
101
103 void reset()
104 {
105 reset(0, 0);
106 }
107
109 void
111 const Char* chars,
112 size_t size)
113 {
114 items_ = chars;
115 size_ = size;
116 }
117
118 const Char&
119 operator [](
120 size_t index) const
121 {
122 return items_[index];
123 }
124
125 const Char&
127 size_t index) const
128 {
129 if (index < size_)
130 return items_[index];
131
132 throw std::invalid_argument("index");
133 }
134
136 StrRef&
137 operator =(
138 const StrRef& other)
139 {
140 items_ = other.items_;
141 size_ = other.size_;
142
143 return *this;
144 }
145
147 void
149 StrRef& other)
150 {
151 std::swap(items_, other.items_);
152 std::swap(size_, other.size_);
153 }
154
155private:
157 const Char* items_;
158
160 size_t size_;
161};
162
164inline
165StrRef
167 const std::string& str)
168{
169 return StrRef(str.data(), str.size());
170}
171
173inline
174StrRef
176 const Char* cStr)
177{
178 return
179 StrRef(
180 cStr,
181 cStr
182 ? strlen(cStr)
183 : 0
184 );
185}
186
188inline
189std::string
191 const StrRef& ref)
192{
193 return std::string(ref.items(), ref.size());
194}
195
197inline
198void
200 std::string& str,
201 const StrRef& ref)
202{
203 str.append(ref.items(), ref.size());
204}
205
207inline
208std::string
210 Char character)
211{
212 return std::string(1, character);
213}
214
216inline
217void
219 std::string& str,
220 Char character)
221{
222 str.append(1, character);
223}
224
226inline
227void
229 std::string& str,
230 const std::string& value)
231{
232 str.append(value);
233}
234
236inline
237bool
238operator ==(
239 const StrRef& left,
240 const StrRef& right)
241{
242 return (
243 left.size() == right.size()
244 &&
245 0 == memcmp(
246 left.items(), right.items(), left.size()));
247}
248
250inline
251bool
252operator !=(
253 const StrRef& left,
254 const StrRef& right)
255{
256 return !(left == right);
257}
258
260inline
261bool
262operator ==(
263 const StrRef& ref,
264 const std::string& str)
265{
266 return ref == toStrRef(str);
267}
268
270inline
271bool
272operator !=(
273 const StrRef& ref,
274 const std::string& str)
275{
276 return ref != toStrRef(str);
277}
278
280inline
281bool
282operator ==(
283 const std::string& str,
284 const StrRef& ref)
285{
286 return ref == toStrRef(str);
287}
288
290inline
291bool
292operator !=(
293 const std::string& str,
294 const StrRef& ref)
295{
296 return ref != toStrRef(str);
297}
298
300inline
301bool
302operator ==(
303 const StrRef& ref,
304 const Char* str)
305{
306 return ref == toStrRef(str);
307}
308
310inline
311bool
312operator !=(
313 const StrRef& ref,
314 const Char* str)
315{
316 return ref != toStrRef(str);
317}
318
320inline
321bool
322operator ==(
323 const Char* str,
324 const StrRef& ref)
325{
326 return ref == toStrRef(str);
327}
328
330inline
331bool
332operator !=(
333 const Char* str,
334 const StrRef& ref)
335{
336 return ref != toStrRef(str);
337}
338
340inline
341bool
342operator <(
343 const StrRef& left,
344 const StrRef& right)
345{
346 ptrdiff_t
347 compareResult =
348 memcmp(
349 left.items(),
350 right.items(),
351 left.size() < right.size()
352 ? left.size()
353 : right.size());
354
355 if (0 == compareResult)
356 {
357 compareResult =
358 static_cast<ptrdiff_t>(
359 left.size() - right.size());
360 }
361
362 return (0 > compareResult);
363}
364
366inline
367bool
368operator >(
369 const StrRef& left,
370 const StrRef& right)
371{
372 return (right < left);
373}
374
376inline
377std::ostream&
378operator <<(
379 std::ostream& stream,
380 const StrRef& text)
381{
382 stream.write(text.items(), text.size());
383
384 return stream;
385}
386
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:169
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition Bootstrap.h:173
#define ONIXS_CMESTREAMLINEDMDH_LTWT_CLASS
Definition Bootstrap.h:111
Provides efficient way of accessing text-based FIX field values.
Definition String.h:40
const Char * items() const
Read-only content.
Definition String.h:79
Iterator begin() const
STL-like begin().
Definition String.h:91
StrRef(const Char *chars, size_t size)
Full initialization.
Definition String.h:53
size_t size() const
Number of chars.
Definition String.h:85
void reset(const Char *chars, size_t size)
Updates data being referenced.
Definition String.h:110
Iterator end() const
STL-like end().
Definition String.h:97
bool empty() const
Indicates whether array of zero length.
Definition String.h:73
const Char * Iterator
STL-like iterator.
Definition String.h:43
void swap(StrRef &other)
Swaps content with other instance.
Definition String.h:148
StrRef()
Initializes blank instance.
Definition String.h:46
void reset()
Resets reference to nothing.
Definition String.h:103
StrRef(const StrRef &other)
Initializes as clone of other instance.
Definition String.h:65
const Char & at(size_t index) const
Definition String.h:126
char Char
Character type alias.
Definition String.h:36
StrRef toStrRef(const std::string &str)
Constructs StrRef instance over std::string content.
Definition String.h:166
void toStr(std::string &str, const Decimal &number)
Definition Decimal.h:502