OnixS C++ CME MDP Conflated UDP Handler  1.1.2
API documentation
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 
35 /// Character type alias.
36 typedef char Char;
37 
38 /// Provides efficient way of accessing
39 /// text-based values without copying
40 /// content of the text being referred.
42 {
43  // Items being referenced.
44  const Char* items_;
45 
46  // Number of chars being referenced.
47  size_t size_;
48 
49 public:
50  /// STL-like iterator.
51  typedef const Char* Iterator;
52 
53  /// Initializes the instance referring to nothing.
55  : items_(0)
56  , size_(0)
57  {
58  }
59 
60  /// Explicit reference initialization.
62  const Char* chars,
63  size_t size)
64  : items_(chars)
65  , size_(size)
66  {
67  }
68 
69  /// Initializes as the clone of other instance.
70  /// Data referenced by the instance is not copied.
71  /// Instead both instances will refer to same text.
73  const StrRef& other)
74  : items_(other.items_)
75  , size_(other.size_)
76  {
77  }
78 
79  /// Indicates whether the referenced text is empty.
80  bool empty() const
81  {
82  return (0 == size_);
83  }
84 
85  /// Read-only content.
86  const Char* items() const
87  {
88  return items_;
89  }
90 
91  /// Number of chars.
92  size_t size() const
93  {
94  return size_;
95  }
96 
97  /// STL-like begin().
98  Iterator begin() const
99  {
100  return items_;
101  }
102 
103  /// STL-like end().
104  Iterator end() const
105  {
106  return (items_ + size_);
107  }
108 
109  /// Resets the reference to nothing.
110  void reset()
111  {
112  reset(0, 0);
113  }
114 
115  /// Updates the given instance
116  /// to refer to the new text.
117  void
119  const Char* chars,
120  size_t size)
121  {
122  items_ = chars;
123  size_ = size;
124  }
125 
126  /// Provides per-item access.
127  const Char&
128  operator [](
129  size_t index) const
130  {
131  return items_[index];
132  }
133 
134  /// Provides bound-checked per-item access.
135  const Char&
136  at(
137  size_t index) const
138  {
139  if (index < size_)
140  return items_[index];
141 
142  throw std::invalid_argument("index");
143  }
144 
145  /// Reinitializes from the other instance.
146  StrRef&
147  operator =(
148  const StrRef& other)
149  {
150  items_ = other.items_;
151  size_ = other.size_;
152 
153  return *this;
154  }
155 
156  /// Swaps content with the other instance.
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 
171 /// Constructs StrRef instance over std::string content.
172 inline
173 StrRef
175  const std::string& str)
176 {
177  return StrRef(str.data(), str.size());
178 }
179 
180 /// Initializes instance from zero-terminated/C-like string.
181 inline
182 StrRef
184  const Char* cStr)
185 {
186  return
187  StrRef(
188  cStr,
189  cStr
190  ? strlen(cStr)
191  : 0
192  );
193 }
194 
195 /// Constructs std::string instance from StrRef one.
196 inline
197 std::string
199  const StrRef& ref)
200 {
201  return std::string(ref.items(), ref.size());
202 }
203 
204 /// Appends text referenced by StrRef to given std::string instance.
205 inline
206 void
208  std::string& str,
209  const StrRef& ref)
210 {
211  str.append(ref.items(), ref.size());
212 }
213 
214 /// Constructs std::string from a character.
215 inline
216 std::string
218  Char character)
219 {
220  return std::string(1, character);
221 }
222 
223 /// Appends character to given std::string instance.
224 inline
225 void
227  std::string& str,
228  Char character)
229 {
230  str.append(1, character);
231 }
232 
233 /// Appends one string another one.
234 inline
235 void
237  std::string& str,
238  const std::string& value)
239 {
240  str.append(value);
241 }
242 
243 /// Compares StrRef instance with another one.
244 inline
245 bool
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 
257 /// Compares with another instance.
258 inline
259 bool
261  const StrRef& left,
262  const StrRef& right)
263 {
264  return !(left == right);
265 }
266 
267 /// Compares StrRef with std::string.
268 inline
269 bool
271  const StrRef& ref,
272  const std::string& str)
273 {
274  return ref == toStrRef(str);
275 }
276 
277 /// Compares StrRef with std::string.
278 inline
279 bool
281  const StrRef& ref,
282  const std::string& str)
283 {
284  return ref != toStrRef(str);
285 }
286 
287 /// Compares StrRef with std::string.
288 inline
289 bool
291  const std::string& str,
292  const StrRef& ref)
293 {
294  return ref == toStrRef(str);
295 }
296 
297 /// Compares StrRef with std::string.
298 inline
299 bool
301  const std::string& str,
302  const StrRef& ref)
303 {
304  return ref != toStrRef(str);
305 }
306 
307 /// Compares StrRef with zero-terminated/C-like string.
308 inline
309 bool
311  const StrRef& ref,
312  const Char* str)
313 {
314  return ref == toStrRef(str);
315 }
316 
317 /// Compares StrRef with zero-terminated/C-like string.
318 inline
319 bool
321  const StrRef& ref,
322  const Char* str)
323 {
324  return ref != toStrRef(str);
325 }
326 
327 /// Compares StrRef with zero-terminated/C-like string.
328 inline
329 bool
331  const Char* str,
332  const StrRef& ref)
333 {
334  return ref == toStrRef(str);
335 }
336 
337 /// Compares StrRef with zero-terminated/C-like string.
338 inline
339 bool
341  const Char* str,
342  const StrRef& ref)
343 {
344  return ref != toStrRef(str);
345 }
346 
347 /// Establishes order over string refs.
348 inline
349 bool
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 
370 /// Establishes order over string refs.
371 inline
372 bool
374  const StrRef& left,
375  const StrRef& right)
376 {
377  return (right < left);
378 }
379 
380 /// StrRef serialization operator.
381 inline
382 std::ostream&
384  std::ostream& stream,
385  const StrRef& text)
386 {
387  stream.write(text.items(), text.size());
388 
389  return stream;
390 }
391 
const Char & at(size_t index) const
Provides bound-checked per-item access.
Definition: String.h:136
StrRef(const Char *chars, size_t size)
Explicit reference initialization.
Definition: String.h:61
void reset()
Resets the reference to nothing.
Definition: String.h:110
bool operator>(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:373
Iterator end() const
STL-like end().
Definition: String.h:104
bool operator==(const Char *str, const StrRef &ref)
Compares StrRef with zero-terminated/C-like string.
Definition: String.h:330
StrRef toStrRef(const Char *cStr)
Initializes instance from zero-terminated/C-like string.
Definition: String.h:183
const Char * items() const
Read-only content.
Definition: String.h:86
size_t size() const
Number of chars.
Definition: String.h:92
void swap(StrRef &other)
Swaps content with the other instance.
Definition: String.h:158
char Char
Character type alias.
Definition: String.h:36
bool empty() const
Indicates whether the referenced text is empty.
Definition: String.h:80
bool operator!=(const Char *str, const StrRef &ref)
Compares StrRef with zero-terminated/C-like string.
Definition: String.h:340
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition: Bootstrap.h:157
std::ostream & operator<<(std::ostream &stream, const StrRef &text)
StrRef serialization operator.
Definition: String.h:383
StrRef(const StrRef &other)
Definition: String.h:72
bool operator<(const StrRef &left, const StrRef &right)
Establishes order over string refs.
Definition: String.h:350
void toStr(std::string &str, const std::string &value)
Appends one string another one.
Definition: String.h:236
bool value(Number &number, const MultiContainer &container, Tag tag)
void reset(const Char *chars, size_t size)
Definition: String.h:118
#define ONIXS_CONFLATEDUDP_LTWT_CLASS
Definition: Bootstrap.h:95
Iterator begin() const
STL-like begin().
Definition: String.h:98
StrRef()
Initializes the instance referring to nothing.
Definition: String.h:54
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition: Bootstrap.h:153
const Char * Iterator
STL-like iterator.
Definition: String.h:51