Debian Codegraph @

3rdparty/xspublic/xstypes/xsstringarray.c

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 #include "xsstringarray.h"
34 #include "xsstring.h"
35
36 /*! \struct XsStringArray
37 \brief A list of XsString values
38 \sa XsArray
39 */
40
41 //! \brief Descriptor for XsStringArray
42 XsArrayDescriptor const g_xsStringArrayDescriptor = {
43 sizeof(XsString),
44 XSEXPCASTITEMSWAP XsArray_swap,
45 XSEXPCASTITEMMAKE XsString_construct,
46 XSEXPCASTITEMCOPY XsArray_copyConstruct,
47 XSEXPCASTITEMMAKE XsArray_destruct,
48 XSEXPCASTITEMCOPY XsArray_copy,
49 XSEXPCASTITEMCOMP XsArray_compare,
50 0
51 };
52
53 /*! \copydoc XsArray_constructDerived
54 \note Specialization for XsStringArray
55 */
56 void XsStringArray_construct(XsStringArray* thisPtr, XsSize count, XsString const* src)
57 {
58 XsArray_construct(thisPtr, &g_xsStringArrayDescriptor, count, src);
59 }
60
61 /*! \relates XsStringArray
62 \brief Splice the supplied string and put the resulting substrings in the string array
63 \details The source string will be searched for instances of characters in \a separators and spliced whereever
64 one was found. The spliced list will not include characters in \a separators and empty substrings will be
65 discarded.
66 \param src The source string to splice
67 \param separators A list of separator characters that will be used to splice \a src.
68 */
69 void XsStringArray_fromSplicedString(struct XsStringArray* thisPtr, struct XsString const* src, struct XsString const* separators)
70 {
71 XsString s;
72 XsString_construct(&s);
73 XsArray_destruct(thisPtr);
74 if (src->m_size > 0)
75 {
76 // check against 1 because an empty string can either be size 0 or size 1 (just the null-terminator)
77 if (separators->m_size <= 1)
78 {
79 // no separator
80 XsArray_insert(thisPtr, 0, 1, src);
81 }
82 else
83 {
84 char const* sep = (char const*) separators->m_data;
85 char const* idx = (char const*) src->m_data;
86 char const* newIdx = strpbrk(idx, sep);
87 while (newIdx && *idx)
88 {
89 if (newIdx != idx)
90 {
91 XsString_assign(&s, (XsSize)(newIdx-idx), idx);
92 XsArray_insert(thisPtr, thisPtr->m_size, 1, &s);
93 }
94 idx = newIdx+1;
95 newIdx = strpbrk(idx, sep);
96 }
97 if (*idx)
98 {
99 XsString_assignCharArray(&s, idx);
100 XsArray_insert(thisPtr, thisPtr->m_size, 1, &s);
101 }
102 }
103 }
104 XsString_destruct(&s);
105 }
106
107 /*! \relates XsStringArray
108 \brief Join the string array into a single string, inserting \a separator between substrings.
109 \param result The result of the join
110 \param separator The separator to insert between successive items
111 */
112 void XsStringArray_join(struct XsStringArray const* thisPtr, struct XsString* result, struct XsString const* separator)
113 {
114 // determine required buffer size
115 XsSize i;
116 XsSize chars = (thisPtr->m_size ? (thisPtr->m_size-1) : 0) * (separator->m_size ? separator->m_size-1 : 0);
117 for (i = 0; i < thisPtr->m_size; ++i)
118 {
119 XsSize sz = ((const XsString*)XsArray_at(thisPtr, i))->m_size;
120 chars += (sz ? sz-1 : 0);
121 }
122
123 XsArray_destruct(result);
124 if (chars)
125 {
126 XsArray_reserve(result, chars+1);
127 for (i = 0; i < thisPtr->m_size; ++i)
128 {
129 const XsString* s = (const XsString*)XsArray_at(thisPtr, i);
130 if (s->m_size > 1)
131 {
132 if (result->m_size > 1)
133 XsString_append(result, separator);
134 XsString_append(result, s);
135 }
136 }
137 }
138 }