GCC Code Coverage Report


Directory: ./
File: Core/util_string.cpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 76 109 69.7%
Branches: 51 130 39.2%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: Julien Castelneau, Dominique Chapelle, Miguel Fernandez,
13 // Jeremie Foulon, David Froger, Jean-Frederic Gerbeau,
14 // Vincent Martin, Philippe Moireau, Marina Vidrascu
15 //
16
17 // System includes
18
19 // External includes
20
21 // Project includes
22 #include "Core/util_string.hpp"
23
24 namespace felisce
25 {
26 66109 bool string2bool(const std::string& str)
27 {
28 // value is true
29
5/6
✓ Branch 1 taken 59274 times.
✓ Branch 2 taken 6835 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 59274 times.
✓ Branch 6 taken 6835 times.
✓ Branch 7 taken 59274 times.
66109 if (str=="true" || str=="True") {
30 6835 return true;
31
2/6
✗ Branch 1 not taken.
✓ Branch 2 taken 59274 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 59274 times.
✗ Branch 7 not taken.
59274 } else if (str=="false" || str== "False") { // value is false
32 59274 return false;
33 } else { // value is not a boolean, error
34 throw 0;
35 }
36 }
37
38 /***********************************************************************************/
39 /***********************************************************************************/
40
41 65100 std::string bool2string(const bool value)
42 {
43
2/2
✓ Branch 0 taken 6510 times.
✓ Branch 1 taken 58590 times.
65100 if (value)
44
1/2
✓ Branch 2 taken 6510 times.
✗ Branch 3 not taken.
6510 return "true";
45 else
46
1/2
✓ Branch 2 taken 58590 times.
✗ Branch 3 not taken.
58590 return "false";
47 }
48
49 /***********************************************************************************/
50 /***********************************************************************************/
51
52 80531 void split(const std::string& str, std::vector<std::string>& vect, const std::string& delimiters)
53 {
54 80531 vect.clear();
55
56 80531 std::string tmp;
57 std::string::size_type index_beg, index_end;
58
59 80531 index_beg = str.find_first_not_of(delimiters);
60
61
2/2
✓ Branch 0 taken 45946 times.
✓ Branch 1 taken 80531 times.
126477 while (index_beg != std::string::npos) {
62 45946 index_end = str.find_first_of(delimiters, index_beg);
63
3/4
✓ Branch 0 taken 13600 times.
✓ Branch 1 taken 32346 times.
✓ Branch 3 taken 45946 times.
✗ Branch 4 not taken.
45946 tmp = str.substr(index_beg, index_end == std::string::npos ? std::string::npos : (index_end - index_beg));
64
1/2
✓ Branch 1 taken 45946 times.
✗ Branch 2 not taken.
45946 vect.push_back(tmp);
65 45946 index_beg = str.find_first_not_of(delimiters, index_end);
66 }
67 80531 }
68
69 /***********************************************************************************/
70 /***********************************************************************************/
71
72 561 char* c_string(const std::string& str)
73 {
74
1/2
✓ Branch 1 taken 561 times.
✗ Branch 2 not taken.
561 std::string buffer(str);
75 561 std::size_t sizeChar = buffer.size() + 1;
76
1/2
✓ Branch 1 taken 561 times.
✗ Branch 2 not taken.
561 char *strChar = new char[ sizeChar ];
77 561 strncpy( strChar, buffer.c_str(), sizeChar );
78 561 return strChar;
79 561 }
80
81 /***********************************************************************************/
82 /***********************************************************************************/
83
84 28 int CountOccurenceChar(const std::string& str,char c)
85 {
86 28 int count=0;
87 28 char* ch_ptr = c_string(str);
88 28 char * start_string=ch_ptr;
89
2/2
✓ Branch 0 taken 436 times.
✓ Branch 1 taken 28 times.
464 while(*ch_ptr != '\0') {
90
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 376 times.
436 if (*ch_ptr == c) ++count;
91 436 ++ch_ptr;
92 }
93
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 delete [] start_string;
94 28 start_string = nullptr;
95 28 ch_ptr = nullptr;
96 28 return count;
97 }
98
99 /***********************************************************************************/
100 /***********************************************************************************/
101
102 std::string ReplaceWilcardByInt(const std::string& strWildcard, const int i,char c)
103 {
104 std::string strInt=strWildcard;
105 const std::size_t dim = CountOccurenceChar(strWildcard, c);
106 std::ostringstream index;
107 index << i;
108 if(index.str().size() > dim) {
109 FEL_ERROR("Integer too large for the dim of wildcard")
110 }
111 std::string zero="";
112 for(std::size_t j=0; j<dim-index.str().size(); j++) zero += "0";
113 const std::string num = zero + index.str();
114 const std::size_t start = strInt.find(c);
115 strInt.replace(start,dim,num);
116 return strInt;
117 }
118
119 /***********************************************************************************/
120 /***********************************************************************************/
121
122 bool is_integer(const std::string& str)
123 {
124 bool ans;
125
126 ans = (str.size() > 0 && std::isdigit(str[0]))
127 || (str.size() > 1 && (str[0] == '+' || str[0] == '-'));
128
129 unsigned int i(1);
130 while (i < str.size() && ans) {
131 ans = ans && isdigit(str[i]);
132 i++;
133 }
134
135 return ans;
136 }
137
138 /***********************************************************************************/
139 /***********************************************************************************/
140
141 28 bool startswith(const std::string& str, const std::string& prefix)
142 {
143 28 return (str.compare(0,prefix.size(),prefix) == 0);
144 }
145
146 /***********************************************************************************/
147 /***********************************************************************************/
148
149 1395 void ppcharFromVstring(std::vector<std::string>& vstring, int& nstr, char**& ppchar)
150 {
151 1395 nstr = vstring.size();
152
153
1/2
✓ Branch 2 taken 1395 times.
✗ Branch 3 not taken.
1395 std::vector<std::size_t> strLength(nstr);
154
155
2/2
✓ Branch 0 taken 2317 times.
✓ Branch 1 taken 1395 times.
3712 for (int istr=0 ; istr<nstr ; istr++)
156 2317 strLength[istr] = vstring[istr].size();
157
158
1/2
✓ Branch 1 taken 1395 times.
✗ Branch 2 not taken.
1395 ppcharAllocate(strLength, ppchar);
159
160
2/2
✓ Branch 0 taken 2317 times.
✓ Branch 1 taken 1395 times.
3712 for (int istr=0 ; istr<nstr ; istr++)
161 2317 strcpy( ppchar[istr], vstring[istr].c_str() );
162 1395 }
163
164 /***********************************************************************************/
165 /***********************************************************************************/
166
167 465 void ppcharToVstring(const int nstr, const char** ppchar, std::vector<std::string>& vstring)
168 {
169
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 vstring.resize(nstr);
170
171 465 std::string elt;
172
2/2
✓ Branch 0 taken 1417 times.
✓ Branch 1 taken 465 times.
1882 for (int istr=0 ; istr<nstr ; istr++) {
173
1/2
✓ Branch 1 taken 1417 times.
✗ Branch 2 not taken.
1417 elt = ppchar[istr]; // convert from char* to std::string (required)
174
1/2
✓ Branch 2 taken 1417 times.
✗ Branch 3 not taken.
1417 vstring[istr] = elt;
175 }
176 465 }
177
178 /***********************************************************************************/
179 /***********************************************************************************/
180
181 1395 void ppcharFree(int& nstr, char**& ppchar)
182 {
183
1/2
✓ Branch 0 taken 1395 times.
✗ Branch 1 not taken.
1395 if (ppchar!=nullptr) {
184
2/2
✓ Branch 0 taken 2317 times.
✓ Branch 1 taken 1395 times.
3712 for (int istr=0; istr<nstr; istr++) {
185
1/2
✓ Branch 0 taken 2317 times.
✗ Branch 1 not taken.
2317 if (ppchar[istr]!=nullptr) {
186
1/2
✓ Branch 0 taken 2317 times.
✗ Branch 1 not taken.
2317 delete[] ppchar[istr];
187 }
188 }
189
1/2
✓ Branch 0 taken 1395 times.
✗ Branch 1 not taken.
1395 delete[] ppchar;
190 1395 ppchar = nullptr;
191 1395 nstr = 0;
192 }
193 1395 }
194
195 /***********************************************************************************/
196 /***********************************************************************************/
197
198 1395 void ppcharAllocate(std::vector<std::size_t>& strLength, char**& ppchar)
199 {
200 1395 const std::size_t nstr = strLength.size();
201
3/4
✓ Branch 0 taken 1395 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2317 times.
✓ Branch 5 taken 1395 times.
3712 ppchar = new char*[nstr]();
202
2/2
✓ Branch 0 taken 2317 times.
✓ Branch 1 taken 1395 times.
3712 for (std::size_t istr=0 ; istr < nstr ; istr++)
203
2/2
✓ Branch 2 taken 161046 times.
✓ Branch 3 taken 2317 times.
163363 ppchar[istr] = new char[strLength[istr]+1]();
204 1395 }
205
206 /***********************************************************************************/
207 /***********************************************************************************/
208
209 void ppcharCopy(int nstrSrc, char** ppcharSrc,
210 int& nstrDst, char**& ppcharDst)
211 {
212 nstrDst = nstrSrc;
213
214 std::vector<std::size_t> strLength(nstrSrc);
215
216 for (int istr=0 ; istr<nstrSrc ; istr++)
217 strLength[istr] = strlen(ppcharSrc[istr]);
218
219 ppcharAllocate(strLength, ppcharDst);
220
221 for (int istr=0 ; istr<nstrSrc ; istr++)
222 strcpy( ppcharDst[istr], ppcharSrc[istr] );
223 }
224
225 /***********************************************************************************/
226 /***********************************************************************************/
227
228 16 int ppcharIndex(int nstr, const char** ppchar, const char* str)
229 {
230
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 for (int istr=0 ; istr<nstr; istr++) {
231
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 if ( strcmp(str,ppchar[istr]) == 0 )
232 16 return istr;
233 }
234
235 return -1;
236 }
237
238 /***********************************************************************************/
239 /***********************************************************************************/
240
241 3720 bool string_contains_substring(const std::string& str, const std::string& substr)
242 {
243 3720 return str.find(substr) != std::string::npos;
244 }
245
246 /***********************************************************************************/
247 /***********************************************************************************/
248
249 bool vector_string_contains(const std::vector<std::string>& vector_str, const std::string& str)
250 {
251 return std::find(vector_str.begin(), vector_str.end(), str) != vector_str.end();
252 }
253
254 }
255