OnixS C++ CME iLink 3 Binary Order Entry Handler 1.18.9
API Documentation
Loading...
Searching...
No Matches
StrRef.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
27
28#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <cstring>
32#include <limits>
33#include <ostream>
34#include <stdexcept>
35#include <string>
36
37#if defined (ONIXS_ILINK3_CXX17)
38# include <string_view>
39#endif
40
41
43
44#if defined (ONIXS_ILINK3_CXX17)
45
46using StrRef = std::basic_string_view<Char>;
47
48#else
49
55void throwInvalidIndex(size_t, size_t);
56
63{
64public:
65 typedef size_t size_type;
66
68 typedef const Char* const_iterator;
69 typedef const_iterator iterator;
70 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
71 typedef const_reverse_iterator reverse_iterator;
72
75 StrRef()
76 : items_(ONIXS_ILINK3_NULLPTR)
77 , size_(0)
78 {
79 }
80
83 StrRef(const Char* chars, size_t size) ONIXS_ILINK3_NOTHROW
84 : items_(chars)
85 , size_((ONIXS_ILINK3_ASSERT(size <= (std::numeric_limits<size_type>::max)()), static_cast<size_type>(size)))
86 {
87 }
88
90 StrRef(const Char* chars) ONIXS_ILINK3_NOTHROW
91 : items_(chars)
92 , size_(0)
93 {
94 assert(chars);
95
96 ONIXS_ILINK3_CONST_OR_CONSTEXPR size_type maxLen = (std::numeric_limits<UInt16>::max)();
97
98 size_ = numericCast<size_type>(strnlen(chars, maxLen));
99
100 assert(size_ < maxLen);
101 }
102
104 StrRef(const std::string& string)
105 : items_(string.c_str())
106 , size_(static_cast<size_type>(string.length()))
107 {
108 assert(string.length() <= (std::numeric_limits<size_type>::max)());
109 }
110
113 bool empty() const ONIXS_ILINK3_NOTHROW
114 {
115 return (0 == size_);
116 }
117
121 const Char* data() const ONIXS_ILINK3_NOTHROW
122 {
123 return items_;
124 }
125
129 size_type size() const ONIXS_ILINK3_NOTHROW
130 {
131 return size_;
132 }
133
137 size_type length() const ONIXS_ILINK3_NOTHROW
138 {
139 return size_;
140 }
141
144 const_iterator begin() const ONIXS_ILINK3_NOTHROW
145 {
146 assert(items_);
147 return items_;
148 }
149
152 const_iterator end() const ONIXS_ILINK3_NOTHROW
153 {
154 assert(items_);
155 return (items_ + size_);
156 }
157
160 const_iterator cbegin() const ONIXS_ILINK3_NOTHROW
161 {
162 return begin();
163 }
164
167 const_iterator cend() const ONIXS_ILINK3_NOTHROW
168 {
169 return end();
170 }
171
174 const_reverse_iterator rbegin() const ONIXS_ILINK3_NOTHROW
175 {
176 return const_reverse_iterator(end());
177 }
178
181 const_reverse_iterator rend() const ONIXS_ILINK3_NOTHROW
182 {
183 return const_reverse_iterator(begin());
184 }
185
188 const_reverse_iterator crbegin() const ONIXS_ILINK3_NOTHROW
189 {
190 return rbegin();
191 }
192
195 const_reverse_iterator crend() const ONIXS_ILINK3_NOTHROW
196 {
197 return rend();
198 }
199
202 const Char& operator [](size_type index) const ONIXS_ILINK3_NOTHROW
203 {
204 assert(index < size_);
205 assert(items_);
206
207 return items_[index];
208 }
209
212 const Char& at(size_type index) const
213 {
214 assert(items_);
215
216 if (index < size_)
217 return items_[index];
218
219 throwInvalidIndex(index, size_);
220 }
221
223 const Char& front() const ONIXS_ILINK3_NOTHROW
224 {
225 assert(!empty());
226 return (*this)[0];
227 }
228
230 const Char& back() const ONIXS_ILINK3_NOTHROW
231 {
232 assert(!empty());
233 return (*this)[size_ - 1];
234 }
235
237 void swap(StrRef& other) ONIXS_ILINK3_NOTHROW
238 {
239 std::swap(items_, other.items_);
240
241 std::swap(size_, other.size_);
242 }
243
245 operator std::basic_string<Char>() const
246 {
247 return std::basic_string<Char>(data(), size());
248 }
249
250private:
252 const Char* items_;
253
255 size_type size_;
256};
257
260inline
261bool operator ==(const StrRef& left, const StrRef& right)
262{
263 if(left.size() != right.size())
264 return false;
265
266 if(left.empty())
267 {
268 assert(right.empty());
269 return true;
270 }
271
272 assert(left.data());
273 assert(right.data());
274
275 return (0 == memcmp(
276 left.data(), right.data(), left.size()));
277}
278
281inline
282bool operator !=(const StrRef& left, const StrRef& right)
283{
284 return !(left == right);
285}
286
289inline
290bool operator <(const StrRef& left, const StrRef& right)
291{
292 const int
293 result =
294 memcmp(
295 left.data(),
296 right.data(),
297 left.size() < right.size()
298 ? left.size()
299 : right.size());
300
301 return (
302 0 != result
303 ? (0 > result)
304 : (left.size() < right.size()));
305}
306
309inline
310bool operator >(const StrRef& left, const StrRef& right)
311{
312 return (right < left);
313}
314
316inline
317std::ostream& operator <<(std::ostream& stream, const StrRef& text)
318{
319 stream.write(text.data(), text.size());
320
321 return stream;
322}
323
324ONIXS_ILINK3_NODISCARD StrRef toStrRef(const std::string&);
326
329inline
330bool operator ==(const StrRef& ref, const std::string& str)
331{
332 return ref == toStrRef(str);
333}
334
337inline
338bool operator !=(const StrRef& ref, const std::string& str)
339{
340 return ref != toStrRef(str);
341}
342
345inline
346bool operator ==(const std::string& str, const StrRef& ref)
347{
348 return ref == toStrRef(str);
349}
350
353inline
354bool operator !=(const std::string& str, const StrRef& ref)
355{
356 return ref != toStrRef(str);
357}
358
361inline
362bool operator ==(const StrRef& ref, const Char* str)
363{
364 return ref == toStrRef(str);
365}
366
369inline
370bool operator !=(const StrRef& ref, const Char* str)
371{
372 return ref != toStrRef(str);
373}
374
377inline
378bool operator ==(const Char* str, const StrRef& ref)
379{
380 return ref == toStrRef(str);
381}
382
385inline
386bool operator !=( const Char* str, const StrRef& ref)
387{
388 return ref != toStrRef(str);
389}
390
391#endif
392
395inline StrRef toStrRef(const std::string& str)
396{
397 return StrRef(str);
398}
399
403{
404 return
405 (cStr != ONIXS_ILINK3_NULLPTR) ?
406 StrRef(cStr) :
407 StrRef()
408 ;
409}
410
411template <size_t Size>
413inline
415StrRef constructStrRef(const char (&value)[Size])
417{
418 return StrRef(value, Size - 1);
419}
420
421template <size_t Size>
423inline
425StrRef strRefFromCharArray(const char (&value)[Size])
427{
428 return StrRef(value, Size);
429}
430
433inline
434std::string toStr(const StrRef& ref)
435{
436 return std::string(ref.data(), ref.size());
437}
438
440inline
441void toStr(std::string& str, const StrRef& ref)
442{
443 str.append(ref.data(), ref.size());
444}
445
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_ILINK3_LTWT_CLASS
Definition ABI.h:84
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_ILINK3_NODISCARD
Definition Compiler.h:185
#define ONIXS_ILINK3_NORETURN
Definition Compiler.h:184
#define ONIXS_ILINK3_CONST_OR_CONSTEXPR
Definition Compiler.h:178
#define ONIXS_ILINK3_NULLPTR
Definition Compiler.h:182
#define ONIXS_ILINK3_EXPORTED
Definition Compiler.h:175
#define ONIXS_ILINK3_CONSTEXPR
Definition Compiler.h:179
#define ONIXS_ILINK3_COLDPATH
Definition Compiler.h:188
#define ONIXS_ILINK3_NOTHROW
Definition Compiler.h:176
constexpr StrRef constructStrRef(const char(&value)[Size]) noexcept
Definition StrRef.h:415
char Char
Character type alias.
Definition String.h:30
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.
std::basic_string_view< Char > StrRef
Definition StrRef.h:46
constexpr StrRef strRefFromCharArray(const char(&value)[Size]) noexcept
Definition StrRef.h:425
StrRef toStrRef(const std::string &str)
Constructs a StrRef instance over th std::string content.
Definition StrRef.h:395