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