Debian Codegraph @

3rdparty/xspublic/xstypes/xsstring.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSSTRING_H
34 #define XSSTRING_H
35
36 #include "xstypesconfig.h"
37 #include "xsarray.h"
38 #ifndef XSENS_NO_WCHAR
39 #include <wchar.h>
40 #endif // XSENS_NO_WCHAR
41 #include <string.h>
42
43 #ifdef __cplusplus
44 #include <string>
45 #include <cstdlib>
46 #include <cstdio>
47 extern "C" {
48 #endif
49
50 extern XsArrayDescriptor const XSTYPES_DLL_API g_xsStringDescriptor;
51
52 #ifndef __cplusplus
53 #define XsString_INITIALIZER XSARRAY_INITIALIZER(&g_xsStringDescriptor)
54 XSARRAY_STRUCT(XsString, char);
55 typedef struct XsString XsString;
56 // obsolete:
57 #define XSSTRING_INITIALIZER XsString_INITIALIZER
58 #else
59 struct XsString;
60 #endif
61
62 XSTYPES_DLL_API void XsString_construct(XsString* thisPtr);
63 XSTYPES_DLL_API void XsString_destruct(XsString* thisPtr);
64 XSTYPES_DLL_API void XsString_assign(XsString* thisPtr, XsSize count, const char* src);
65 XSTYPES_DLL_API void XsString_assignCharArray(XsString* thisPtr, const char* src);
66 XSTYPES_DLL_API void XsString_resize(XsString* thisPtr, XsSize count);
67 XSTYPES_DLL_API void XsString_append(XsString* thisPtr, XsString const* other);
68 XSTYPES_DLL_API void XsString_erase(XsString* thisPtr, XsSize index, XsSize count);
69 XSTYPES_DLL_API void XsString_push_back(XsString* thisPtr, char c);
70 XSTYPES_DLL_API XsSize XsString_utf8Len(XsString const * thisPtr);
71 XSTYPES_DLL_API int XsString_endsWith(XsString const * thisPtr, XsString const* other, int caseSensitive);
72 XSTYPES_DLL_API int XsString_startsWith(XsString const * thisPtr, XsString const* other, int caseSensitive);
73 XSTYPES_DLL_API int XsString_contains(XsString const * thisPtr, XsString const* other, int caseSensitive, XsSize* offset);
74 XSTYPES_DLL_API int XsString_empty(XsString const * thisPtr);
75 XSTYPES_DLL_API void XsString_sort(XsString* thisPtr);
76 XSTYPES_DLL_API void XsString_reverse(XsString* thisPtr);
77 XSTYPES_DLL_API int XsString_findSubStr(XsString const* thisPtr, XsString const* needle);
78 XSTYPES_DLL_API void XsString_mid(XsString* thisPtr, XsString const* source, XsSize start, XsSize count);
79
80 #ifndef XSENS_NO_WCHAR
81 XSTYPES_DLL_API XsSize XsString_copyToWCharArray(const XsString* thisPtr, wchar_t* dest, XsSize size);
82 XSTYPES_DLL_API wchar_t XsString_utf8At(const XsString* thisPtr, XsSize index);
83 XSTYPES_DLL_API void XsString_assignWCharArray(XsString* thisPtr, const wchar_t* src);
84 XSTYPES_DLL_API void XsString_push_backWChar(XsString* thisPtr, wchar_t c);
85 #endif // XSENS_NO_WCHAR
86
87 #ifndef __cplusplus
88 // obsolete:
89 #define XsString_copy(thisPtr, copy) XsArray_copy(copy, thisPtr)
90 #define XsString_swap(a, b) XsArray_swap(a, b)
91 #endif
92 #ifdef __cplusplus
93 } // extern "C"
94 #endif
95
96 #ifdef __cplusplus
97 /* We need some special template implementations for strings to keep them 0-terminated
98 */
99 // this typedef is not _always_ interpreted correctly by doxygen, hence the occasional function where we're NOT using it.
100 typedef XsArrayImpl<char, g_xsStringDescriptor, XsString> XsStringType;
101
102 /*! \brief Returns the number of items currently in the array, excluding the terminating 0
103 \details This function returns the number of bytes in the underlying char array. This doesn't mean it returns the number of characters as XsString assumes the char* is a utf-8 encoded string.
104 \returns The number of items currently in the array
105 \sa reserved \sa setSize \sa resize \sa utf8Len
106 */
107 template<> inline XsSize XsStringType::size() const
108 {
109 return m_size?m_size-1:0;
110 }
111
112 //! \copydoc XsArray_reserve
113 template<> inline void XsStringType::reserve(XsSize count)
114 {
115 if (count)
116 XsArray_reserve(this, count+1);
117 else
118 XsArray_reserve(this, 0);
119 }
120
121 //! \brief Returns the reserved space in number of items
122 template<> inline XsSize XsStringType::reserved() const
123 {
124 return m_reserved?m_reserved-1:0;
125 }
126
127 /*! \brief indexed data access operator */
128 template<> inline char& XsStringType::operator[] (XsSize index)
129 {
130 assert(index < size());
131 return *ptrAt(m_data, (ptrdiff_t) index);
132 }
133
134 /*! \brief Removes \a count items from the array starting at \a index. \param index The index of the first item to remove. \param count The number of items to remove. */
135 template<> inline void XsStringType::erase(XsSize index, XsSize count)
136 {
137 XsString_erase((XsString*) this, index, count);
138 }
139
140 /*! \brief Insert \a count \a items at \a index in the string
141 \param items The items to insert, may not be 0 unless count is 0
142 \param index The index to use for inserting. Anything beyond the end of the string (ie. -1) will
143 append to the actual end of the string.
144 \param count The number of items to insert
145 */
146 template<> inline void XsArrayImpl<char, g_xsStringDescriptor, XsString>::insert(char const* items, XsSize index, XsSize count)
147 {
148 if (size())
149 {
150 if (index >= size())
151 index = size();
152 XsArray_insert(this, index, count, items);
153 }
154 else
155 XsString_assign((XsString*) this, count, items);
156 }
157
158 /*! \brief Assign the characters in \a src to the string
159 \details This function is resizes the string to fit \a count characters and copies those from \a src.
160 \param count The number of characters in src. If this value is 0 and \a scr is not 0, the value is
161 determined automatically by looking through src until a terminating 0 is found.
162 \param src The source string to copy from. If this is 0 and \a count is not 0, the string will be
163 filled with spaces instead.
164 \sa XsString_assign
165 */
166 template<> inline void XsArrayImpl<char, g_xsStringDescriptor, XsString>::assign(XsSize count, char const* src)
167 {
168 XsString_assign((XsString*) this, count, src);
169 }
170
171 /*! \brief This function resizes the contained string to the desired size, while retaining its contents
172 \param count The desired size of the string. This excludes the terminating 0 character.
173 \sa XsString_resize
174 */
175 template<> inline void XsStringType::resize(XsSize count)
176 {
177 XsString_resize((XsString*) this, count);
178 }
179
180 /*! \brief Set the size of the array to \a count.
181 \details The contents of the array after this operation are undefined.
182 \param count The desired new size fo the array.
183 \sa XsArray_assign \sa reserve \sa resize
184 */
185 template<> inline void XsStringType::setSize(XsSize count)
186 {
187 if (count != size())
188 XsString_assign((XsString*) this, count, 0);
189 }
190
191 /*! \copydoc XsArray_append \sa XsArray_append */
192 template<> inline void XsArrayImpl<char, g_xsStringDescriptor, XsString>::append(const XsStringType& other)
193 {
194 XsString_append((XsString*) this, (XsString const*) &other);
195 }
196
197 struct XsString : public XsStringType {
198 //! \brief Constructs an XsString
199 inline explicit XsString(XsSize sz = 0, char const* src = 0)
200 : XsStringType()
201 {
202 if (sz || src)
203 XsString_assign(this, sz, src);
204 }
205
206 //! \brief Constructs an XsString as a copy of \a other
207 inline XsString(const XsStringType& other)
208 : XsStringType(other)
209 {
210 }
211
212 //! \brief Constructs an XsInt64Array that references the data supplied in \a ref
213 inline explicit XsString(char* ref, XsSize sz, XsDataFlags flags /* = XSDF_None */)
214 : XsStringType(ref, sz+1, flags)
215 {
216 }
217 #ifndef XSENS_NOITERATOR
218 //! \brief Constructs an XsString with the list bound by the supplied iterators \a beginIt and \a endIt
219 template <typename Iterator>
220 inline explicit XsString(Iterator const& beginIt, Iterator const& endIt)
221 : XsStringType(beginIt, endIt)
222 {
223 }
224 #endif
225
226 //! \brief Construct an XsString from a 0-terminated character array
227 inline XsString(char const* src)
228 : XsStringType()
229 {
230 if (src && src[0])
231 XsString_assignCharArray(this, src);
232 }
233
234 #ifndef XSENS_NO_WCHAR
235 //! \brief Construct an XsString from a 0-terminated character array
236 inline XsString(wchar_t const* src)
237 : XsStringType()
238 {
239 if (src && src[0])
240 XsString_assignWCharArray(this, src);
241 }
242 #endif
243
244 #ifndef XSENS_NO_STL
245 //! \brief Construct an XsString from a std::string
246 inline XsString(std::string const& src)
247 : XsStringType()
248 {
249 if (!src.empty())
250 XsString_assign(this, (XsSize)src.size()+1, src.c_str());
251 }
252
253 //! \brief Construct an XsString from a std::wstring
254 inline XsString(std::wstring const& src)
255 : XsStringType()
256 {
257 if (!src.empty())
258 XsString_assignWCharArray(this, src.c_str());
259 }
260 #endif // XSENS_NO_STL
261
262 //! \brief Return the internal 0-terminated C-style string
263 inline char* c_str()
264 {
265 static const char nullChar = 0;
266 if (empty())
267 return const_cast<char*>(&nullChar);
268 try
269 {
270 return begin().operator ->();
271 }
272 catch(...)
273 {
274 return const_cast<char*>(&nullChar);
275 }
276 }
277
278 //! \brief Return the internal 0-terminated C-style string
279 inline char const* c_str() const
280 {
281 static const char nullChar = 0;
282 if (empty())
283 return &nullChar;
284 try
285 {
286 return begin().operator ->();
287 }
288 catch(...)
289 {
290 return &nullChar;
291 }
292 }
293
294 #ifndef XSENS_NO_STL
295 //! \brief Return the string as a std::string
296 std::string toStdString() const
297 {
298 if (empty())
299 return std::string();
300 return std::string((char const*)begin().operator ->());
301 }
302 #endif // XSENS_NO_STL
303
304 //! \brief Return \a other appended to this, without modifying this
305 XsString operator + (XsString const& other) const
306 {
307 XsString tmp;
308 tmp.reserve(size()+other.size());
309 tmp.append(*this);
310 tmp.append(other);
311 return tmp;
312 }
313
314 #ifndef XSENS_NO_STL
315 //! \brief Return the string as a std::wstring
316 std::wstring toStdWString() const
317 {
318 if (empty())
319 return std::wstring();
320 size_t s = XsString_copyToWCharArray(this, NULL, 0);
321 std::wstring w;
322 w.resize(s-1);
323 (void) XsString_copyToWCharArray(this, &w[0], (XsSize)s);
324 return w;
325 }
326 #endif // XSENS_NO_STL
327
328 /*! \cond NODOXYGEN */
329 using XsStringType::operator ==;
330 using XsStringType::operator !=;
331 #ifndef XSENS_NOITERATOR
332 using XsStringType::operator <<;
333 #endif
334 /*! \endcond */
335
336 //! \brief Return true if the contents of \a str are identical to this string
337 bool operator == (char const* str) const
338 {
339 if (!str) return empty();
340 return !strcmp(c_str(), str);
341 }
342
343 //! \brief Return true if the contents of \a str differ from this string
344 inline bool operator != (char const* str) const
345 {
346 return !(*this == str);
347 }
348
349 //! \brief Append a character array to the string in a stream-like way
350 inline XsString& operator << (char const* str)
351 {
352 if (str && str[0])
353 {
354 XsString const ref(const_cast<char*>(str), (XsSize)strlen(str), XSDF_None);
355 append(ref);
356 }
357 return *this;
358 }
359
360 //! \brief Append an integer to the string in a stream-like way
361 inline XsString& operator << (int i)
362 {
363 char buffer[32];
364 append(XsString(buffer, (XsSize) (ptrdiff_t) std::sprintf(buffer, "%d", i), XSDF_None));
365 return *this;
366 }
367
368 //! \brief Append another string to the string in a stream-like way
369 inline XsString& operator << (XsString const& s)
370 {
371 append(s);
372 return *this;
373 }
374
375 //! \brief Return true if the contents if \a str are greater then this string
376 inline bool operator < (const XsString &str) const
377 {
378 #ifdef XSENS_NO_STL
379 return (strcmp(c_str(), str.c_str()) < 0);
380 #else
381 return (this->toStdString() < str.toStdString());
382 #endif
383 }
384
385 //! \brief Return true if the contents if \a str are less then this string
386 inline bool operator > (const XsString &str) const
387 {
388 #ifdef XSENS_NO_STL
389 return (strcmp(c_str(), str.c_str()) > 0);
390 #else
391 return (this->toStdString() > str.toStdString());
392 #endif
393 }
394
395 //! \brief Append a character to the string
396 inline XsString& push_back(char c)
397 {
398 XsString_push_back(this, c);
399 return *this;
400 }
401
402 //! \brief Return true when the string is empty
403 inline bool empty() const
404 {
405 return !!XsString_empty(this);
406 }
407
408 using XsStringType::append;
409
410 //! \brief Append a char array to the string
411 inline void append(char const* other)
412 {
413 if (other)
414 {
415 XsString tmp(other);
416 XsString_append(this, &tmp);
417 }
418 }
419
420 #ifndef XSENS_NO_WCHAR
421 //! \brief Append a wchar array to the string
422 inline void append(wchar_t const* other)
423 {
424 if (other)
425 {
426 XsString tmp(other);
427 XsString_append(this, &tmp);
428 }
429 }
430 #endif
431
432 //! \returns the number of characters in a utf-8 encoded string
433 inline XsSize utf8Len() const
434 {
435 return XsString_utf8Len(this);
436 }
437
438 /*! \brief Returns whether this string ends with \a other (case-insensitive!)
439 \param other The string to match with the end of this string
440 \param caseSensitive Whether to compare case sensitive or not (case insensitive is the default)
441 \return true when the string ends with the given string
442 */
443 inline bool endsWith(XsString const& other, bool caseSensitive = false) const
444 {
445 return 0 != XsString_endsWith(this, &other, caseSensitive?1:0);
446 }
447
448 /*! \brief Returns whether this string starts with \a other (case-insensitive!)
449 \param other The string to match with the start of this string
450 \param caseSensitive Whether to compare case sensitive or not (case insensitive is the default)
451 \return true when the string starts with the given string
452 */
453 inline bool startsWith(XsString const& other, bool caseSensitive = false) const
454 {
455 return 0 != XsString_startsWith(this, &other, caseSensitive?1:0);
456 }
457
458 /*! \brief Returns whether this string contains \a other (case-insensitive!)
459 \param other The string to match with this string
460 \param caseSensitive Whether to compare case sensitive or not (case insensitive is the default)
461 \param offset When not null, this will be filled with the first index where \a other was found
462 \return true when the string contains the given string
463 */
464 inline bool contains(XsString const& other, bool caseSensitive = false, XsSize* offset = 0) const
465 {
466 return 0 != XsString_contains(this, &other, caseSensitive?1:0, offset);
467 }
468
469 #ifndef XSENS_NO_WCHAR
470 /*! \brief The decoded UTF-8 character at index \a index in the UTF-8 encoded string
471 \details http://en.wikipedia.org/wiki/Utf-8#Description
472 \param index The index of the character to return.
473 \returns the decoded UTF-8 character at index \a index in the UTF-8 encoded string
474 */
475 inline wchar_t utf8At(XsSize index) const
476 {
477 return XsString_utf8At(this, index);
478 }
479
480 //! \brief Append a unicode character to the string
481 inline XsString& push_back(wchar_t c)
482 {
483 XsString_push_backWChar(this, c);
484 return *this;
485 }
486 #endif
487
488 //! \brief Sort the characters in the string
489 inline void sort()
490 {
491 XsString_sort(this);
492 }
493
494 /*! \brief Reverse the characters in the string
495 \note This does not take into account utf-8 encoded characters
496 */
497 inline void reverse()
498 {
499 XsString_reverse(this);
500 }
501
502 /*! \brief Find the first occurrence of \a needle in the string
503 \param needle The string to find
504 \return The offset of \a needle or -1 if it was not found
505 */
506 int findSubStr(XsString const& needle) const
507 {
508 return XsString_findSubStr(this, &needle);
509 }
510
511 /*! \brief Return a substring of the string
512 \details The function returns a copy of up to \a count characters from the string, starting at offset \a start
513 \param start The offset of the first character to copy
514 \param count The maximum number of characters to copy
515 \return The requested substring
516 */
517 XsString mid(XsSize start, XsSize count) const
518 {
519 XsString rv;
520 XsString_mid(&rv, this, start, count);
521 return rv;
522 }
523 };
524
525 #ifndef XSENS_NO_STL
526 namespace std
527 {
528 template<typename _CharT, typename _Traits>
529 basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& o, XsString const& xs)
530 {
531 return (o << xs.toStdString());
532 }
533 }
534 #endif
535
536 #endif
537
538 #endif