OnixS C++ ICE Binary Order Entry Handler 1.0.0
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
23#include <OnixS/ICE/BOE/ABI.h>
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_ICEBOE_CXX17)
38# include <string_view>
39#endif
40
41
43
44#if defined (ONIXS_ICEBOE_CXX17)
45
46using StrRef = std::basic_string_view<Char>;
47
48#else
49
50
52[[noreturn]] ONIXS_ICEBOE_EXPORTED ONIXS_ICEBOE_COLDPATH void throwInvalidIndex(size_t, size_t);
53
59class StrRef
60{
61public:
62 typedef size_t size_type;
63
65 typedef const Char* const_iterator;
66 typedef const_iterator iterator;
67 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68 typedef const_reverse_iterator reverse_iterator;
69
71 constexpr
72 StrRef()
73 : items_(nullptr)
74 , size_(0)
75 {
76 }
77
79 constexpr
80 StrRef(const Char* chars, size_t size) noexcept
81 : items_(chars)
82 , size_((ONIXS_ICEBOE_ASSERT(size <= (std::numeric_limits<size_type>::max)()), static_cast<size_type>(size)))
83 {
84 }
85
87 StrRef(const Char* chars) noexcept
88 : items_(chars)
89 , size_(0)
90 {
91 assert(chars);
92
93 constexpr size_type maxLen = (std::numeric_limits<UInt16>::max)();
94
95 size_ = numericCast<size_type>(strnlen(chars, maxLen));
96
97 assert(size_ < maxLen);
98 }
99
101 StrRef(const std::string& string)
102 : items_(string.c_str())
103 , size_(static_cast<size_type>(string.length()))
104 {
105 assert(string.length() <= (std::numeric_limits<size_type>::max)());
106 }
107
110 constexpr bool empty() const noexcept
111 {
112 return (0 == size_);
113 }
114
117 constexpr
118 const Char* data() const noexcept
119 {
120 return items_;
121 }
122
125 constexpr
126 size_type size() const noexcept
127 {
128 return size_;
129 }
130
131 constexpr
134 size_type length() const noexcept
135 {
136 return size_;
137 }
138
141 const_iterator begin() const noexcept
142 {
143 assert(items_);
144 return items_;
145 }
146
149 const_iterator end() const noexcept
150 {
151 assert(items_);
152 return (items_ + size_);
153 }
154
157 const_iterator cbegin() const noexcept
158 {
159 return begin();
160 }
161
164 const_iterator cend() const noexcept
165 {
166 return end();
167 }
168
171 const_reverse_iterator rbegin() const noexcept
172 {
173 return const_reverse_iterator(end());
174 }
175
178 const_reverse_iterator rend() const noexcept
179 {
180 return const_reverse_iterator(begin());
181 }
182
185 const_reverse_iterator crbegin() const noexcept
186 {
187 return rbegin();
188 }
189
192 const_reverse_iterator crend() const noexcept
193 {
194 return rend();
195 }
196
199 const Char& operator [](size_type index) const noexcept
200 {
201 assert(index < size_);
202 assert(items_);
203
204 return items_[index];
205 }
206
209 const Char& at(size_type index) const
210 {
211 assert(items_);
212
213 if (index < size_)
214 return items_[index];
215
216 throwInvalidIndex(index, size_);
217 }
218
220 const Char& front() const noexcept
221 {
222 assert(!empty());
223 return (*this)[0];
224 }
225
227 const Char& back() const noexcept
228 {
229 assert(!empty());
230 return (*this)[size_ - 1];
231 }
232
234 void swap(StrRef& other) noexcept
235 {
236 std::swap(items_, other.items_);
237
238 std::swap(size_, other.size_);
239 }
240
242 operator std::basic_string<Char>() const
243 {
244 return std::basic_string<Char>(data(), size());
245 }
246
247private:
249 const Char* items_;
250
252 size_type size_;
253};
254
257inline
258bool operator ==(const StrRef& left, const StrRef& right)
259{
260 if(left.size() != right.size())
261 return false;
262
263 if(left.empty())
264 {
265 assert(right.empty());
266 return true;
267 }
268
269 assert(left.data());
270 assert(right.data());
271
272 return (0 == memcmp(
273 left.data(), right.data(), left.size()));
274}
275
278inline
279bool operator !=(const StrRef& left, const StrRef& right)
280{
281 return !(left == right);
282}
283
286inline
287bool operator <(const StrRef& left, const StrRef& right)
288{
289 const int
290 result =
291 memcmp(
292 left.data(),
293 right.data(),
294 left.size() < right.size()
295 ? left.size()
296 : right.size());
297
298 return (
299 0 != result
300 ? (0 > result)
301 : (left.size() < right.size()));
302}
303
306inline
307bool operator >(const StrRef& left, const StrRef& right)
308{
309 return (right < left);
310}
311
313inline
314std::ostream& operator <<(std::ostream& stream, const StrRef& text)
315{
316 stream.write(text.data(), text.size());
317
318 return stream;
319}
320
321ONIXS_ICEBOE_NODISCARD StrRef toStrRef(const std::string&);
322ONIXS_ICEBOE_NODISCARD StrRef toStrRef(const Char*) noexcept;
323
326inline
327bool operator ==(const StrRef& ref, const std::string& str)
328{
329 return ref == toStrRef(str);
330}
331
334inline
335bool operator !=(const StrRef& ref, const std::string& str)
336{
337 return ref != toStrRef(str);
338}
339
342inline
343bool operator ==(const std::string& str, const StrRef& ref)
344{
345 return ref == toStrRef(str);
346}
347
350inline
351bool operator !=(const std::string& str, const StrRef& ref)
352{
353 return ref != toStrRef(str);
354}
355
358inline
359bool operator ==(const StrRef& ref, const Char* str)
360{
361 return ref == toStrRef(str);
362}
363
366inline
367bool operator !=(const StrRef& ref, const Char* str)
368{
369 return ref != toStrRef(str);
370}
371
374inline
375bool operator ==(const Char* str, const StrRef& ref)
376{
377 return ref == toStrRef(str);
378}
379
382inline
383bool operator !=( const Char* str, const StrRef& ref)
384{
385 return ref != toStrRef(str);
386}
387
388#endif
389
392inline StrRef toStrRef(const std::string& str)
393{
394 return StrRef(str);
395}
396
399{
400 return str;
401}
402
405inline StrRef toStrRef(const Char* cStr) noexcept
406{
407 return cStr ? StrRef(cStr) : StrRef{};
408}
409
410template <size_t Size>
412inline
413constexpr
414StrRef constructStrRef(const char (&value)[Size])
415 noexcept
416{
417 return StrRef(value, Size - 1);
418}
419
420template <size_t Size>
422inline
423constexpr
424StrRef strRefFromCharArray(const char (&value)[Size])
425noexcept
426{
427 return StrRef(value, Size);
428}
429
432inline
433std::string toStr(StrRef ref)
434{
435 return std::string(ref.data(), ref.size());
436}
437
439inline
440void toStr(std::string& str, StrRef ref)
441{
442 str.append(ref.data(), ref.size());
443}
444
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:102
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_END
Definition ABI.h:106
#define ONIXS_ICEBOE_COLDPATH
Definition Compiler.h:156
#define ONIXS_ICEBOE_EXPORTED
Definition Compiler.h:153
#define ONIXS_ICEBOE_NODISCARD
Definition Compiler.h:154
constexpr StrRef constructStrRef(const char(&value)[Size]) noexcept
Definition StrRef.h:414
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
char Char
Character type alias.
Definition String.h:31
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:424
StrRef toStrRef(const std::string &str)
Constructs a StrRef instance over th std::string content.
Definition StrRef.h:392