OnixS CME Drop Copy Handler C++ library 5.7.1
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 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
31namespace OnixS { namespace CME { namespace DropCopy {
32
33class StringRef;
34
36class ONIXS_CME_DROP_COPY_EXPORT String
37{
38public:
40 typedef char* iterator;
41
43 typedef const char* ConstIterator;
44
47
49 String(const String& other);
50
53 explicit String(const StringRef& strRef);
54
56 explicit String(const std::string& stdStr);
57
59 explicit String(const char* cStr);
60
62 String(const char* text, size_t textSize);
63
66
68 bool empty() const;
69
71 size_t size() const;
72
75 size_t capacity() const;
76
79 const char* data() const;
80
82 const char* c_str() const;
83
86
88 ConstIterator begin() const;
89
92 iterator end();
93
96 ConstIterator end() const;
97
101 char& operator[](size_t index);
102
106 const char& operator[](size_t index) const;
107
110 char& at(size_t index);
111
114 const char& at(size_t index) const;
115
119 void reserve(size_t capacity);
120
123 void resize(size_t size);
124
127 void resize(size_t size, char filler);
128
130 String& insert(size_t offset, const char* text, size_t textSize);
131
133 String& append(const char* text, size_t textSize);
134
136 String& assign(const char* text, size_t textSize);
137
140 String& replace(size_t offset, size_t count, const char* text, size_t textSize);
141
144 void erase(size_t offset, size_t count);
145
147 void clear();
148
150 operator std::string() const;
151
153 bool operator==(const String& other) const;
154
156 bool operator!=(const String& other) const;
157
159 String& operator=(const String& other);
160
162 String& operator=(const std::string& other);
163
165 String& operator=(const char* other);
166
168 String& operator=(char other);
169
171 String& operator+=(const String& text);
172
174 String& operator+=(const char* text);
175
177 String& operator+=(const std::string& text);
178
180 String& operator+=(char character);
181
182private:
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
197inline bool String::empty() const
198{
199 return (0 == size_);
200}
201
202inline size_t String::size() const
203{
204 return size_;
205}
206
207inline size_t String::capacity() const
208{
209 return (capacity_ - 1);
210}
211
212inline const char* String::data() const
213{
214 return text_;
215}
216
217inline 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
242inline char& String::operator[](size_t index)
243{
244 return text_[index];
245}
246
247inline const char& String::operator[](size_t index) const
248{
249 return text_[index];
250}
251
252inline 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
262inline 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
272inline String::operator std::string() const
273{
274 return std::string(text_, size_);
275}
276
277inline String& String::operator=(const std::string& other)
278{
279 return assign(other.data(), other.size());
280}
281
282inline String& String::operator=(const char* other)
283{
284 if (other)
285 {
286 assign(other, strlen(other));
287 }
288
289 return *this;
290}
291
292inline String& String::operator=(char other)
293{
294 return assign(&other, 1);
295}
296
297inline String& String::operator+=(const String& text)
298{
299 return append(text.data(), text.size());
300}
301
302inline String& String::operator+=(const char* text)
303{
304 if (text)
305 {
306 append(text, strlen(text));
307 }
308
309 return *this;
310}
311
312inline String& String::operator+=(const std::string& text)
313{
314 return append(text.data(), text.size());
315}
316
317inline String& String::operator+=(char character)
318{
319 return append(&character, 1);
320}
321
323class ONIXS_CME_DROP_COPY_EXPORT StringRef
324{
325public:
327 typedef const char* ConstIterator;
328
331 : chars_(NULL)
332 , size_(0)
333 {
334 }
335
337 StringRef(const char* chars, size_t size)
338 : chars_(chars)
339 , size_(size)
340 {
341 }
342
347 StringRef(const StringRef& other)
348 : chars_(other.chars_)
349 , size_(other.size_)
350 {
351 }
352
354 explicit StringRef(const std::string& stdStr)
355 : chars_(stdStr.c_str())
356 , size_(stdStr.size())
357 {
358 }
359
361 explicit StringRef(const String& ownStr)
362 : chars_(ownStr.data())
363 , size_(ownStr.size())
364 {
365 }
366
368 explicit StringRef(const char* cStr)
369 : chars_(cStr)
370 , size_(cStr ? strlen(cStr) : 0)
371 {
372 }
373
375 bool empty() const
376 {
377 return (0 == size_);
378 }
379
381 const char* data() const
382 {
383 return chars_;
384 }
385
388 {
389 return chars_;
390 }
391
394 {
395 return chars_ + size_;
396 }
397
399 size_t size() const
400 {
401 return size_;
402 }
403
405 void reset()
406 {
407 reset(NULL, 0);
408 }
409
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
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
453 operator std::string() const
454 {
455 return std::string(chars_, size_);
456 }
457
459 bool operator==(const StringRef& other) const
460 {
461 return (size_ == other.size_ && 0 == memcmp(chars_, other.chars_, size_));
462 }
463
465 bool operator!=(const StringRef& other) const
466 {
467 return (size_ != other.size_ || 0 != memcmp(chars_, other.chars_, size_));
468 }
469
472 {
473 chars_ = other.chars_;
474 size_ = other.size_;
475
476 return *this;
477 }
478
480 void swap(StringRef& other)
481 {
482 std::swap(chars_, other.chars_);
483 std::swap(size_, other.size_);
484 }
485
486private:
488 const char* chars_;
489
491 size_t size_;
492};
493
494inline bool operator==(const StringRef& ref, const String& str)
495{
496 return ref == StringRef(str);
497}
498
499inline bool operator!=(const StringRef& ref, const String& str)
500{
501 return ref != StringRef(str);
502}
503
504inline bool operator==(const String& str, const StringRef& ref)
505{
506 return ref == StringRef(str);
507}
508
509inline bool operator!=(const String& str, const StringRef& ref)
510{
511 return ref != StringRef(str);
512}
513
514inline bool operator==(const StringRef& ref, const std::string& str)
515{
516 return ref == StringRef(str);
517}
518
519inline bool operator!=(const StringRef& ref, const std::string& str)
520{
521 return ref != StringRef(str);
522}
523
524inline bool operator==(const std::string& str, const StringRef& ref)
525{
526 return ref == StringRef(str);
527}
528
529inline bool operator!=(const std::string& str, const StringRef& ref)
530{
531 return ref != StringRef(str);
532}
533
534inline bool operator==(const StringRef& ref, const char* str)
535{
536 return ref == StringRef(str);
537}
538
539inline bool operator!=(const StringRef& ref, const char* str)
540{
541 return ref != StringRef(str);
542}
543
544inline bool operator==(const char* str, const StringRef& ref)
545{
546 return ref == StringRef(str);
547}
548
549inline bool operator!=(const char* str, const StringRef& ref)
550{
551 return ref != StringRef(str);
552}
553
554}}}
555
556namespace std {
557
559template <>
560struct 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
581template <>
582struct ONIXS_CME_DROP_COPY_EXPORT less<OnixS::CME::DropCopy::String>
583{
587 ) const
588 {
589 return std::less<OnixS::CME::DropCopy::StringRef>()(
591 );
592 }
593};
594
595inline 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
601inline 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}
Provides efficient way of accessing text-based FIX field values.
Definition String.h:324
bool toNumber(NumericType &number) const
Definition String.h:435
bool operator==(const StringRef &other) const
Compares with another instance.
Definition String.h:459
StringRef & operator=(const StringRef &other)
Reinitializes from another instance.
Definition String.h:471
std::string toString() const
Definition String.h:447
size_t size() const
Number of chars.
Definition String.h:399
StringRef()
Initializes blank instance.
Definition String.h:330
void toString(std::string &str) const
Definition String.h:441
StringRef(const String &ownStr)
Initializes instance from string content.
Definition String.h:361
bool empty() const
Indicates whether array of zero length.
Definition String.h:375
ConstIterator end() const
STL-like end().
Definition String.h:393
bool operator!=(const StringRef &other) const
Compares with another instance.
Definition String.h:465
const char * ConstIterator
Immutable iterator over chars.
Definition String.h:327
StringRef(const char *cStr)
Initializes instance from zero-terminated string.
Definition String.h:368
void swap(StringRef &other)
Swaps content with other instance.
Definition String.h:480
StringRef(const StringRef &other)
Definition String.h:347
const char & operator[](size_t index) const
Definition String.h:417
const char * data() const
Read-only content.
Definition String.h:381
const char & at(size_t index) const
Definition String.h:422
StringRef(const char *chars, size_t size)
Full initialization.
Definition String.h:337
void reset()
Resets reference to nothing.
Definition String.h:405
ConstIterator begin() const
STL-like begin().
Definition String.h:387
void reset(const char *chars, size_t size)
Updates data being referenced.
Definition String.h:411
StringRef(const std::string &stdStr)
Initializes instance from string content.
Definition String.h:354
Zero-terminated sequence of characters.
Definition String.h:37
void reserve(size_t capacity)
const char * c_str() const
Returns string as C string.
Definition String.h:217
String & replace(size_t offset, size_t count, const char *text, size_t textSize)
String & operator=(const String &other)
Reinitializes instance as copy of another one.
String & assign(const char *text, size_t textSize)
Re-initializes instance with given string.
size_t size() const
Number of characters in the sequence.
Definition String.h:202
String & operator+=(const String &text)
Appends other string to the end.
Definition String.h:297
bool operator==(const String &other) const
Compares with other instance for equality.
String(const String &other)
Initializes as copy of another instance.
String & append(const char *text, size_t textSize)
Appends subsequence to the end.
String(const char *text, size_t textSize)
Initializes from sequence of give size.
void resize(size_t size, char filler)
String & insert(size_t offset, const char *text, size_t textSize)
Inserts subsequence at specified position.
bool operator!=(const String &other) const
Compares with other instance for inequality.
void resize(size_t size)
void erase(size_t offset, size_t count)
String(const StringRef &strRef)
bool empty() const
Indicates whether sequence is empty.
Definition String.h:197
const char * ConstIterator
Read-only access over characters.
Definition String.h:43
size_t capacity() const
Definition String.h:207
String()
Initializes empty string.
~String()
Cleans up internal resources.
const char * data() const
Definition String.h:212
iterator begin()
Points to the first element in the sequence.
Definition String.h:222
char * iterator
Read-write access over characters.
Definition String.h:40
void clear()
Erases all characters in the string.
String(const char *cStr)
Initializes as copy of null-terminated C string.
String(const std::string &stdStr)
Initializes as copy of std::string instance.
char & operator[](size_t index)
Definition String.h:242
char & at(size_t index)
Definition String.h:252
bool operator==(const FieldValueRef &ref, const std::string &str)
Definition Messaging.h:188
bool operator!=(const FieldValueRef &ref, const std::string &str)
Definition Messaging.h:193
STL namespace.
std::ostream & operator<<(std::ostream &, const OnixS::CME::DropCopy::Error &)
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
bool operator()(const OnixS::CME::DropCopy::StringRef &left, const OnixS::CME::DropCopy::StringRef &right) const
Definition String.h:562
bool operator()(const OnixS::CME::DropCopy::String &left, const OnixS::CME::DropCopy::String &right) const
Definition String.h:584