OnixS C++ FIX Engine 4.13.0
API Documentation
Loading...
Searching...
No Matches
StringRef.h
Go to the documentation of this file.
1#pragma once
2/*
3* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
4*
5* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
6* and international copyright treaties.
7*
8* Access to and use of the software is governed by the terms of the applicable OnixS Software
9* Services Agreement (the Agreement) and Customer end user license agreements granting
10* a non-assignable, non-transferable and non-exclusive license to use the software
11* for it's own data processing purposes under the terms defined in the Agreement.
12*
13* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
14* of this source code or associated reference material to any other location for further reproduction
15* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
16*
17* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
18* the terms of the Agreement is a violation of copyright law.
19*/
20
21#include <cstring>
22#include <functional>
23#include <ostream>
24#include <string>
25
28
29#ifdef ONIXS_FIXENGINE_CXX17
30
31#include <string_view>
32
33#endif
34
35namespace OnixS {
36namespace FIX {
37
38#ifdef ONIXS_FIXENGINE_CXX17
39
40using StringRef = std::basic_string_view<Char>;
41
42#else
43
58
59class
62{
63public:
65 typedef const char * ConstIterator;
66
70 size_(0) {
71 }
72
76 const char * chars,
77 size_t size)
78 : chars_(chars),
79 size_(size) {
80 }
81
83 explicit StringRef(
84 const std::string & stdStr)
85 : chars_(stdStr.c_str()),
86 size_(stdStr.size()) {
87 }
88
90 StringRef(const char * cStr)
91 : chars_(cStr),
92 size_(cStr ? strlen(cStr) : 0) {
93 }
94
96 bool empty() const {
97 return (0 == size_);
98 }
99
101 const char * data() const {
102 return chars_;
103 }
104
107 return chars_;
108 }
109
112 return chars_ + size_;
113 }
114
116 size_t size() const {
117 return size_;
118 }
119
121 size_t length() const {
122 return size_;
123 }
124
126 void reset() {
128 }
129
131 void
133 const char * chars,
134 size_t size) {
135 chars_ = chars;
136 size_ = size;
137 }
138
139 const char &
140 operator [](size_t index) const {
141 return chars_[index];
142 }
143
144 const char &
145 at(size_t index) const {
146 if(index < size_)
147 return chars_[index];
148
149 throw std::invalid_argument("index");
150 }
151
154 template
155 <
156 typename NumericType
157 >
158 bool
159 toNumber(NumericType & number) const {
161 tryParse(chars_, size_, number);
162 }
163
164 // Appends a copy of the text to the std::string.
165 void
166 toString(std::string & str) const {
167 str.append(chars_, size_);
168 }
169
170 // Returns a copy of the text as the std::string.
171 std::string
172 toString() const {
173 return std::string(chars_, size_);
174 }
175
177 bool
179 const StringRef & other) const {
180 return (
181 size_ == other.size_
182 &&
183 0 == std::memcmp(
184 chars_, other.chars_, size_));
185 }
186
188 bool
190 const StringRef & other) const {
191 return (
192 size_ != other.size_
193 ||
194 0 != std::memcmp(
195 chars_, other.chars_, size_));
196 }
197
199 void
200 swap(StringRef & other) {
201 std::swap(chars_, other.chars_);
202 std::swap(size_, other.size_);
203 }
204
205 friend std::ostream & operator<< (std::ostream & out, const StringRef & ref);
206
207private:
209 const char * chars_;
210
212 size_t size_;
213};
214
215inline
216bool
218 const StringRef & ref,
219 const std::string & str)
220{
221 return ref == StringRef(str);
222}
223
224inline
225bool
227 const StringRef & ref,
228 const std::string & str)
229{
230 return ref != StringRef(str);
231}
232
233inline
234bool
236 const std::string & str,
237 const StringRef & ref)
238{
239 return ref == StringRef(str);
240}
241
242inline
243bool
245 const std::string & str,
246 const StringRef & ref)
247{
248 return ref != StringRef(str);
249}
250
251//
252
253inline
254bool
256 const StringRef & ref,
257 const char * str)
258{
259 return ref == StringRef(str);
260}
261
262inline
263bool
265 const StringRef & ref,
266 const char * str)
267{
268 return ref != StringRef(str);
269}
270
271inline
272bool
274 const char * str,
275 const StringRef & ref)
276{
277 return ref == StringRef(str);
278}
279
280inline
281bool
283 const char * str,
284 const StringRef & ref)
285{
286 return ref != StringRef(str);
287}
288
289inline std::ostream & operator<< (std::ostream & out, const StringRef & ref)
290{
291 if(! ref.empty())
292 out.write(ref.chars_, ref.size_);
293
294 return out;
295}
296
297#endif
298
299template <size_t Size>
300ONIXS_FIXENGINE_NODISCARD
301inline
304{
305 return StringRef(value, Size - 1);
306}
307
308}
309}
310
311namespace std {
313template<>
314struct
316 less<OnixS::FIX::StringRef> {
317 bool
319 const OnixS::FIX::StringRef & left,
320 const OnixS::FIX::StringRef & right) const {
321 ptrdiff_t
322 compareResult = std::memcmp(
323 left.data(), right.data(),
324 left.size() < right.size()
325 ? left.size() : right.size());
326
327 if(0 == compareResult) {
328 compareResult =
329 static_cast<ptrdiff_t>(
330 left.size() - right.size());
331 }
332
333 return (0 > compareResult);
334 }
335};
336}
#define ONIXS_FIXENGINE_API
Definition ABI.h:45
#define ONIXS_FIXENGINE_NULLPTR
Definition Compiler.h:200
#define ONIXS_FIXENGINE_NOTHROW
Definition Compiler.h:186
#define ONIXS_FIXENGINE_CONSTEXPR
Definition Compiler.h:192
Provides an efficient way of accessing text-based FIX field values.
Definition StringRef.h:62
bool toNumber(NumericType &number) const
Returns the number if the text is a string representation of an integer.
Definition StringRef.h:159
std::string toString() const
Definition StringRef.h:172
size_t size() const
The number of chars.
Definition StringRef.h:116
StringRef()
Initializes the blank instance.
Definition StringRef.h:68
void toString(std::string &str) const
Definition StringRef.h:166
bool empty() const
Indicates whether the array has zero length.
Definition StringRef.h:96
ConstIterator end() const
The STL-like end().
Definition StringRef.h:111
const char * ConstIterator
An immutable iterator over chars.
Definition StringRef.h:65
size_t length() const
The number of chars.
Definition StringRef.h:121
StringRef(const char *cStr)
Initializes an instance from the zero-terminated string.
Definition StringRef.h:90
void swap(StringRef &other)
Swaps the content with another instance.
Definition StringRef.h:200
const char * data() const
Returns the read-only content.
Definition StringRef.h:101
const char & at(size_t index) const
Definition StringRef.h:145
void reset()
Resets the reference to nothing.
Definition StringRef.h:126
ConstIterator begin() const
The STL-like begin().
Definition StringRef.h:106
void reset(const char *chars, size_t size)
Updates data being referenced.
Definition StringRef.h:132
StringRef(const std::string &stdStr)
Initializes an instance from the string content.
Definition StringRef.h:83
ONIXS_FIXENGINE_CONSTEXPR StringRef(const char *chars, size_t size)
The full initialization.
Definition StringRef.h:75
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
bool operator==(const FieldValueRef &ref, const std::string &str)
ONIXS_FIXENGINE_NODISCARD ONIXS_FIXENGINE_CONSTEXPR StringRef constructStrRef(const char(&value)[Size]) ONIXS_FIXENGINE_NOTHROW
Definition StringRef.h:303
bool operator!=(const FieldValueRef &ref, const std::string &str)
STL namespace.
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
bool operator()(const OnixS::FIX::StringRef &left, const OnixS::FIX::StringRef &right) const
Definition StringRef.h:318