OnixS C++ CME MDP Conflated UDP Handler 1.1.2
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
42{
43 // Items being referenced.
44 const Char* items_;
45
46 // Number of chars being referenced.
47 size_t size_;
48
49public:
51 typedef const Char* Iterator;
52
55 : items_(0)
56 , size_(0)
57 {
58 }
59
62 const Char* chars,
63 size_t size)
64 : items_(chars)
65 , size_(size)
66 {
67 }
68
73 const StrRef& other)
74 : items_(other.items_)
75 , size_(other.size_)
76 {
77 }
78
80 bool empty() const
81 {
82 return (0 == size_);
83 }
84
86 const Char* items() const
87 {
88 return items_;
89 }
90
92 size_t size() const
93 {
94 return size_;
95 }
96
99 {
100 return items_;
101 }
102
104 Iterator end() const
105 {
106 return (items_ + size_);
107 }
108
110 void reset()
111 {
112 reset(0, 0);
113 }
114
117 void
119 const Char* chars,
120 size_t size)
121 {
122 items_ = chars;
123 size_ = size;
124 }
125
127 const Char&
128 operator [](
129 size_t index) const
130 {
131 return items_[index];
132 }
133
135 const Char&
137 size_t index) const
138 {
139 if (index < size_)
140 return items_[index];
141
142 throw std::invalid_argument("index");
143 }
144
146 StrRef&
147 operator =(
148 const StrRef& other)
149 {
150 items_ = other.items_;
151 size_ = other.size_;
152
153 return *this;
154 }
155
157 void
159 StrRef& other)
160 {
161 std::swap(
162 items_,
163 other.items_);
164
165 std::swap(
166 size_,
167 other.size_);
168 }
169};
170
172inline
173StrRef
175 const std::string& str)
176{
177 return StrRef(str.data(), str.size());
178}
179
181inline
182StrRef
184 const Char* cStr)
185{
186 return
187 StrRef(
188 cStr,
189 cStr
190 ? strlen(cStr)
191 : 0
192 );
193}
194
196inline
197std::string
199 const StrRef& ref)
200{
201 return std::string(ref.items(), ref.size());
202}
203
205inline
206void
208 std::string& str,
209 const StrRef& ref)
210{
211 str.append(ref.items(), ref.size());
212}
213
215inline
216std::string
218 Char character)
219{
220 return std::string(1, character);
221}
222
224inline
225void
227 std::string& str,
228 Char character)
229{
230 str.append(1, character);
231}
232
234inline
235void
237 std::string& str,
238 const std::string& value)
239{
240 str.append(value);
241}
242
244inline
245bool
246operator ==(
247 const StrRef& left,
248 const StrRef& right)
249{
250 return (
251 left.size() == right.size()
252 &&
253 0 == memcmp(
254 left.items(), right.items(), left.size()));
255}
256
258inline
259bool
260operator !=(
261 const StrRef& left,
262 const StrRef& right)
263{
264 return !(left == right);
265}
266
268inline
269bool
270operator ==(
271 const StrRef& ref,
272 const std::string& str)
273{
274 return ref == toStrRef(str);
275}
276
278inline
279bool
280operator !=(
281 const StrRef& ref,
282 const std::string& str)
283{
284 return ref != toStrRef(str);
285}
286
288inline
289bool
290operator ==(
291 const std::string& str,
292 const StrRef& ref)
293{
294 return ref == toStrRef(str);
295}
296
298inline
299bool
300operator !=(
301 const std::string& str,
302 const StrRef& ref)
303{
304 return ref != toStrRef(str);
305}
306
308inline
309bool
310operator ==(
311 const StrRef& ref,
312 const Char* str)
313{
314 return ref == toStrRef(str);
315}
316
318inline
319bool
320operator !=(
321 const StrRef& ref,
322 const Char* str)
323{
324 return ref != toStrRef(str);
325}
326
328inline
329bool
330operator ==(
331 const Char* str,
332 const StrRef& ref)
333{
334 return ref == toStrRef(str);
335}
336
338inline
339bool
340operator !=(
341 const Char* str,
342 const StrRef& ref)
343{
344 return ref != toStrRef(str);
345}
346
348inline
349bool
350operator <(
351 const StrRef& left,
352 const StrRef& right)
353{
354 const
355 int
356 result =
357 memcmp(
358 left.items(),
359 right.items(),
360 left.size() < right.size()
361 ? left.size()
362 : right.size());
363
364 return (
365 0 != result ? (0 > result)
366 : (left.size() < right.size())
367 );
368}
369
371inline
372bool
373operator >(
374 const StrRef& left,
375 const StrRef& right)
376{
377 return (right < left);
378}
379
381inline
382std::ostream&
383operator <<(
384 std::ostream& stream,
385 const StrRef& text)
386{
387 stream.write(text.items(), text.size());
388
389 return stream;
390}
391
#define ONIXS_CONFLATEDUDP_LTWT_CLASS
Definition Bootstrap.h:95
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition Bootstrap.h:157
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition Bootstrap.h:153
const Char * items() const
Read-only content.
Definition String.h:86
Iterator begin() const
STL-like begin().
Definition String.h:98
StrRef(const Char *chars, size_t size)
Explicit reference initialization.
Definition String.h:61
size_t size() const
Number of chars.
Definition String.h:92
void reset(const Char *chars, size_t size)
Definition String.h:118
Iterator end() const
STL-like end().
Definition String.h:104
bool empty() const
Indicates whether the referenced text is empty.
Definition String.h:80
const Char * Iterator
STL-like iterator.
Definition String.h:51
void swap(StrRef &other)
Swaps content with the other instance.
Definition String.h:158
StrRef()
Initializes the instance referring to nothing.
Definition String.h:54
void reset()
Resets the reference to nothing.
Definition String.h:110
StrRef(const StrRef &other)
Definition String.h:72
const Char & at(size_t index) const
Provides bound-checked per-item access.
Definition String.h:136
ONIXS_CONFLATEDUDP_EXPORTED void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
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:174