OnixS CME Drop Copy Handler C++ library  5.7.1
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 protected by copyright law
4 // and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable OnixS Software
7 // Services Agreement (the Agreement) and Customer end user license agreements granting
8 // a non-assignable, non-transferable and non-exclusive license to use the software
9 // for it's own data processing purposes under the terms defined in the Agreement.
10 //
11 // Except as otherwise granted within the terms of the Agreement, copying or reproduction of any
12 // part of this source code or associated reference material to any other location for further
13 // reproduction or redistribution, and any amendments to this copyright notice, are expressly
14 // prohibited.
15 //
16 // Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 // the terms of the Agreement is a violation of copyright law.
18 //
19 
20 #pragma once
21 
22 #include "OnixS/CME/DropCopy/Export.h"
24 
25 #include <cstring>
26 #include <functional>
27 #include <ostream>
28 #include <stdexcept>
29 #include <string>
30 
31 namespace OnixS { namespace CME { namespace DropCopy {
32 
33 class StringRef;
34 
35 /// Zero-terminated sequence of characters.
36 class ONIXS_CME_DROP_COPY_EXPORT String
37 {
38 public:
39  /// Read-write access over characters.
40  typedef char* iterator;
41 
42  /// Read-only access over characters.
43  typedef const char* ConstIterator;
44 
45  /// Initializes empty string.
46  String();
47 
48  /// Initializes as copy of another instance.
49  String(const String& other);
50 
51  /// Initializes as copy of what is
52  /// referred by StringRef instance.
53  explicit String(const StringRef& strRef);
54 
55  /// Initializes as copy of std::string instance.
56  explicit String(const std::string& stdStr);
57 
58  /// Initializes as copy of null-terminated C string.
59  explicit String(const char* cStr);
60 
61  /// Initializes from sequence of give size.
62  String(const char* text, size_t textSize);
63 
64  /// Cleans up internal resources.
65  ~String();
66 
67  /// Indicates whether sequence is empty.
68  bool empty() const;
69 
70  /// Number of characters in the sequence.
71  size_t size() const;
72 
73  /// Number of characters that can be put into
74  /// sequence without reallocating internal store.
75  size_t capacity() const;
76 
77  /// Direct read-only access to buffer
78  /// which holds the sequence.
79  const char* data() const;
80 
81  /// Returns string as C string.
82  const char* c_str() const;
83 
84  /// Points to the first element in the sequence.
85  iterator begin();
86 
87  /// Points to the first element in the sequence.
88  ConstIterator begin() const;
89 
90  /// Points to the location succeeding
91  /// the last element in the sequence.
92  iterator end();
93 
94  /// Points to the location succeeding
95  /// the last element in the sequence.
96  ConstIterator end() const;
97 
98  /// Reference to the character located at specified index.
99  /// \note For a performance considerations does NOT check
100  /// bounds of the character being accessed.
101  char& operator[](size_t index);
102 
103  /// Const reference to the character located at specified index.
104  /// \note For a performance considerations does NOT check
105  /// bounds of the character being accessed.
106  const char& operator[](size_t index) const;
107 
108  /// Provides safe access to the character
109  /// via its reference located at specified index.
110  char& at(size_t index);
111 
112  /// Provides safe access to the character
113  /// via its reference located at specified index.
114  const char& at(size_t index) const;
115 
116  /// Makes internal store to be capable of storing
117  /// at least specified number of characters without
118  /// necessity to reallocate it.
119  void reserve(size_t capacity);
120 
121  /// Specifies new size for the sequence,
122  /// appending or erasing elements as required.
123  void resize(size_t size);
124 
125  /// Specifies new size for the sequence,
126  /// appending or erasing elements as required.
127  void resize(size_t size, char filler);
128 
129  /// Inserts subsequence at specified position.
130  String& insert(size_t offset, const char* text, size_t textSize);
131 
132  /// Appends subsequence to the end.
133  String& append(const char* text, size_t textSize);
134 
135  /// Re-initializes instance with given string.
136  String& assign(const char* text, size_t textSize);
137 
138  /// Replaces subsequence of specified length at
139  /// specified position with the new subsequence.
140  String& replace(size_t offset, size_t count, const char* text, size_t textSize);
141 
142  /// Erases subsequence of specified
143  /// length at specified position.
144  void erase(size_t offset, size_t count);
145 
146  /// Erases all characters in the string.
147  void clear();
148 
149  /// Implicit conversion to std::string type.
150  operator std::string() const;
151 
152  /// Compares with other instance for equality.
153  bool operator==(const String& other) const;
154 
155  /// Compares with other instance for inequality.
156  bool operator!=(const String& other) const;
157 
158  /// Reinitializes instance as copy of another one.
159  String& operator=(const String& other);
160 
161  /// Reinitializes instance as copy of another one.
162  String& operator=(const std::string& other);
163 
164  /// Reinitializes instance as copy of another one.
165  String& operator=(const char* other);
166 
167  /// Reinitializes instance as copy of another one.
168  String& operator=(char other);
169 
170  /// Appends other string to the end.
171  String& operator+=(const String& text);
172 
173  /// Appends other string to the end.
174  String& operator+=(const char* text);
175 
176  /// Appends other string to the end.
177  String& operator+=(const std::string& text);
178 
179  /// Appends other string to the end.
180  String& operator+=(char character);
181 
182 private:
183  enum Traits
184  {
185  PreallocatedSize = 3 * sizeof(size_t)
186  };
187 
188  char* text_;
189  size_t size_;
190  size_t capacity_;
191  char preallocated_[PreallocatedSize];
192 
193  void grow(size_t delta);
194  void shrink(size_t delta);
195 };
196 
197 inline bool String::empty() const
198 {
199  return (0 == size_);
200 }
201 
202 inline size_t String::size() const
203 {
204  return size_;
205 }
206 
207 inline size_t String::capacity() const
208 {
209  return (capacity_ - 1);
210 }
211 
212 inline const char* String::data() const
213 {
214  return text_;
215 }
216 
217 inline const char* String::c_str() const
218 {
219  return text_;
220 }
221 
223 {
224  return text_;
225 }
226 
228 {
229  return text_;
230 }
231 
233 {
234  return (text_ + size_);
235 }
236 
238 {
239  return (text_ + size_);
240 }
241 
242 inline char& String::operator[](size_t index)
243 {
244  return text_[index];
245 }
246 
247 inline const char& String::operator[](size_t index) const
248 {
249  return text_[index];
250 }
251 
252 inline char& String::at(size_t index)
253 {
254  if (index < size_)
255  {
256  return text_[index];
257  }
258 
259  throw std::out_of_range("index");
260 }
261 
262 inline const char& String::at(size_t index) const
263 {
264  if (index < size_)
265  {
266  return text_[index];
267  }
268 
269  throw std::out_of_range("index");
270 }
271 
272 inline String::operator std::string() const
273 {
274  return std::string(text_, size_);
275 }
276 
277 inline String& String::operator=(const std::string& other)
278 {
279  return assign(other.data(), other.size());
280 }
281 
282 inline String& String::operator=(const char* other)
283 {
284  if (other)
285  {
286  assign(other, strlen(other));
287  }
288 
289  return *this;
290 }
291 
292 inline String& String::operator=(char other)
293 {
294  return assign(&other, 1);
295 }
296 
297 inline String& String::operator+=(const String& text)
298 {
299  return append(text.data(), text.size());
300 }
301 
302 inline String& String::operator+=(const char* text)
303 {
304  if (text)
305  {
306  append(text, strlen(text));
307  }
308 
309  return *this;
310 }
311 
312 inline String& String::operator+=(const std::string& text)
313 {
314  return append(text.data(), text.size());
315 }
316 
317 inline String& String::operator+=(char character)
318 {
319  return append(&character, 1);
320 }
321 
322 /// Provides efficient way of accessing text-based FIX field values.
323 class ONIXS_CME_DROP_COPY_EXPORT StringRef
324 {
325 public:
326  /// Immutable iterator over chars.
327  typedef const char* ConstIterator;
328 
329  /// Initializes blank instance.
331  : chars_(NULL)
332  , size_(0)
333  {
334  }
335 
336  /// Full initialization.
337  StringRef(const char* chars, size_t size)
338  : chars_(chars)
339  , size_(size)
340  {
341  }
342 
343  /// Initializes as clone of other instance.
344  /// Data referenced by clone is not copied.
345  /// Instead both instances will refer to same
346  /// text segment.
347  StringRef(const StringRef& other)
348  : chars_(other.chars_)
349  , size_(other.size_)
350  {
351  }
352 
353  /// Initializes instance from string content.
354  explicit StringRef(const std::string& stdStr)
355  : chars_(stdStr.c_str())
356  , size_(stdStr.size())
357  {
358  }
359 
360  /// Initializes instance from string content.
361  explicit StringRef(const String& ownStr)
362  : chars_(ownStr.data())
363  , size_(ownStr.size())
364  {
365  }
366 
367  /// Initializes instance from zero-terminated string.
368  explicit StringRef(const char* cStr)
369  : chars_(cStr)
370  , size_(cStr ? strlen(cStr) : 0)
371  {
372  }
373 
374  /// Indicates whether array of zero length.
375  bool empty() const
376  {
377  return (0 == size_);
378  }
379 
380  /// Read-only content.
381  const char* data() const
382  {
383  return chars_;
384  }
385 
386  /// STL-like begin().
387  ConstIterator begin() const
388  {
389  return chars_;
390  }
391 
392  /// STL-like end().
393  ConstIterator end() const
394  {
395  return chars_ + size_;
396  }
397 
398  /// Number of chars.
399  size_t size() const
400  {
401  return size_;
402  }
403 
404  /// Resets reference to nothing.
405  void reset()
406  {
407  reset(NULL, 0);
408  }
409 
410  /// Updates data being referenced.
411  void reset(const char* chars, size_t size)
412  {
413  chars_ = chars;
414  size_ = size;
415  }
416 
417  const char& operator[](size_t index) const
418  {
419  return chars_[index];
420  }
421 
422  const char& at(size_t index) const
423  {
424  if (index < size_)
425  {
426  return chars_[index];
427  }
428 
429  throw std::invalid_argument("index");
430  }
431 
432  /// Returns number if text is string
433  /// representation of an integer.
434  template <typename NumericType>
435  bool toNumber(NumericType& number) const
436  {
437  return OnixS::CME::DropCopy::Number::tryParse(chars_, size_, number);
438  }
439 
440  // Appends copy of text to the std::string.
441  void toString(std::string& str) const
442  {
443  str.append(chars_, size_);
444  }
445 
446  // Return copy of text as std::string.
447  std::string toString() const
448  {
449  return std::string(chars_, size_);
450  }
451 
452  /// Implicit conversion to std::string.
453  operator std::string() const
454  {
455  return std::string(chars_, size_);
456  }
457 
458  /// Compares with another instance.
459  bool operator==(const StringRef& other) const
460  {
461  return (size_ == other.size_ && 0 == memcmp(chars_, other.chars_, size_));
462  }
463 
464  /// Compares with another instance.
465  bool operator!=(const StringRef& other) const
466  {
467  return (size_ != other.size_ || 0 != memcmp(chars_, other.chars_, size_));
468  }
469 
470  /// Reinitializes from another instance.
472  {
473  chars_ = other.chars_;
474  size_ = other.size_;
475 
476  return *this;
477  }
478 
479  /// Swaps content with other instance.
480  void swap(StringRef& other)
481  {
482  std::swap(chars_, other.chars_);
483  std::swap(size_, other.size_);
484  }
485 
486 private:
487  /// Items being referenced.
488  const char* chars_;
489 
490  /// Number of chars being referenced.
491  size_t size_;
492 };
493 
494 inline bool operator==(const StringRef& ref, const String& str)
495 {
496  return ref == StringRef(str);
497 }
498 
499 inline bool operator!=(const StringRef& ref, const String& str)
500 {
501  return ref != StringRef(str);
502 }
503 
504 inline bool operator==(const String& str, const StringRef& ref)
505 {
506  return ref == StringRef(str);
507 }
508 
509 inline bool operator!=(const String& str, const StringRef& ref)
510 {
511  return ref != StringRef(str);
512 }
513 
514 inline bool operator==(const StringRef& ref, const std::string& str)
515 {
516  return ref == StringRef(str);
517 }
518 
519 inline bool operator!=(const StringRef& ref, const std::string& str)
520 {
521  return ref != StringRef(str);
522 }
523 
524 inline bool operator==(const std::string& str, const StringRef& ref)
525 {
526  return ref == StringRef(str);
527 }
528 
529 inline bool operator!=(const std::string& str, const StringRef& ref)
530 {
531  return ref != StringRef(str);
532 }
533 
534 inline bool operator==(const StringRef& ref, const char* str)
535 {
536  return ref == StringRef(str);
537 }
538 
539 inline bool operator!=(const StringRef& ref, const char* str)
540 {
541  return ref != StringRef(str);
542 }
543 
544 inline bool operator==(const char* str, const StringRef& ref)
545 {
546  return ref == StringRef(str);
547 }
548 
549 inline bool operator!=(const char* str, const StringRef& ref)
550 {
551  return ref != StringRef(str);
552 }
553 
554 }}}
555 
556 namespace std {
557 
558 /// Allows using of StringRef in collections like std::map.
559 template <>
560 struct ONIXS_CME_DROP_COPY_EXPORT less<OnixS::CME::DropCopy::StringRef>
561 {
565  ) const
566  {
567  ptrdiff_t compareResult = memcmp(
568  left.data(), right.data(), left.size() < right.size() ? left.size() : right.size()
569  );
570 
571  if (0 == compareResult)
572  {
573  compareResult = static_cast<ptrdiff_t>(left.size() - right.size());
574  }
575 
576  return (0 > compareResult);
577  }
578 };
579 
580 /// Allows using of StringRef in collections like std::map.
581 template <>
582 struct ONIXS_CME_DROP_COPY_EXPORT less<OnixS::CME::DropCopy::String>
583 {
585  const OnixS::CME::DropCopy::String& left,
586  const OnixS::CME::DropCopy::String& right
587  ) const
588  {
591  );
592  }
593 };
594 
595 inline std::ostream& operator<<(std::ostream& stream, const OnixS::CME::DropCopy::StringRef& text)
596 {
597  stream.write(text.data(), text.size());
598  return stream;
599 }
600 
601 inline std::ostream& operator<<(std::ostream& stream, const OnixS::CME::DropCopy::String& text)
602 {
603  stream.write(text.data(), text.size());
604  return stream;
605 }
606 
607 }
const char * c_str() const
Returns string as C string.
Definition: String.h:217
char * iterator
Read-write access over characters.
Definition: String.h:40
StringRef(const char *chars, size_t size)
Full initialization.
Definition: String.h:337
ConstIterator end() const
STL-like end().
Definition: String.h:393
bool empty() const
Indicates whether array of zero length.
Definition: String.h:375
size_t size() const
Number of characters in the sequence.
Definition: String.h:202
std::ostream & operator<<(std::ostream &, const OnixS::CME::DropCopy::Error &)
ConstIterator begin() const
STL-like begin().
Definition: String.h:387
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:323
char & operator[](size_t index)
Definition: String.h:242
String & operator+=(const String &text)
Appends other string to the end.
Definition: String.h:297
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
const char * ConstIterator
Read-only access over characters.
Definition: String.h:43
bool operator()(const OnixS::CME::DropCopy::String &left, const OnixS::CME::DropCopy::String &right) const
Definition: String.h:584
std::string toString() const
Definition: String.h:447
void toString(std::string &str) const
Definition: String.h:441
bool operator==(const FieldValueRef &ref, const std::string &str)
Definition: Messaging.h:188
STL namespace.
Allows using of StringRef in collections like std::map.
Definition: String.h:560
bool operator!=(const FieldValueRef &ref, const std::string &str)
Definition: Messaging.h:193
StringRef(const StringRef &other)
Definition: String.h:347
const char * ConstIterator
Immutable iterator over chars.
Definition: String.h:327
void reset(const char *chars, size_t size)
Updates data being referenced.
Definition: String.h:411
StringRef(const String &ownStr)
Initializes instance from string content.
Definition: String.h:361
void swap(StringRef &other)
Swaps content with other instance.
Definition: String.h:480
void reset()
Resets reference to nothing.
Definition: String.h:405
char & at(size_t index)
Definition: String.h:252
bool operator!=(const StringRef &other) const
Compares with another instance.
Definition: String.h:465
bool empty() const
Indicates whether sequence is empty.
Definition: String.h:197
size_t size() const
Number of chars.
Definition: String.h:399
bool less(const Decimal &lhs, const Decimal &rhs)
Definition: Numeric.h:389
const char * data() const
Definition: String.h:212
StringRef()
Initializes blank instance.
Definition: String.h:330
iterator begin()
Points to the first element in the sequence.
Definition: String.h:222
bool toNumber(NumericType &number) const
Definition: String.h:435
StringRef(const char *cStr)
Initializes instance from zero-terminated string.
Definition: String.h:368
size_t capacity() const
Definition: String.h:207
bool operator==(const StringRef &other) const
Compares with another instance.
Definition: String.h:459
Zero-terminated sequence of characters.
Definition: String.h:36
const char * data() const
Read-only content.
Definition: String.h:381
bool operator()(const OnixS::CME::DropCopy::StringRef &left, const OnixS::CME::DropCopy::StringRef &right) const
Definition: String.h:562
StringRef & operator=(const StringRef &other)
Reinitializes from another instance.
Definition: String.h:471
const char & operator[](size_t index) const
Definition: String.h:417
StringRef(const std::string &stdStr)
Initializes instance from string content.
Definition: String.h:354
const char & at(size_t index) const
Definition: String.h:422
String & operator=(const String &other)
Reinitializes instance as copy of another one.