Directory: | ./ |
---|---|
File: | src/string_lower_upper.cpp |
Date: | 2024-11-14 16:05:39 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 112 | 112 | 100.0% |
Branches: | 175 | 198 | 88.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | |||
8 | #include "string_lower_upper.h" | ||
9 | |||
10 | ///Tels if the character is a number or not | ||
11 | /** @param ch : character to be analysed | ||
12 | * @return true if it is a number, false otherwise | ||
13 | */ | ||
14 | 80 | bool isCharNumber(char ch){ | |
15 |
4/4✓ Branch 0 taken 78 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 55 times.
|
80 | return (ch >= 48 && ch <= 57); |
16 | } | ||
17 | |||
18 | ///Tels if std::string contains figures | ||
19 | /** @param str : std::string to be tested | ||
20 | * @return true if std::string contains only figures | ||
21 | */ | ||
22 | 10 | bool isStrNumber(const std::string & str){ | |
23 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
|
10 | if(str.size() == 0lu){return false;} |
24 | 9 | bool isMatch(true); | |
25 | 9 | long unsigned int i(0lu); | |
26 |
6/6✓ Branch 1 taken 19 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 9 times.
|
25 | while(i < str.size() && isMatch){ |
27 | 16 | isMatch = isCharNumber(str[i]); | |
28 | 16 | ++i; | |
29 | } | ||
30 | 9 | return isMatch; | |
31 | } | ||
32 | |||
33 | ///Say if the list of std::string contains a number | ||
34 | /** @param listStr : list of std::strings to be checked | ||
35 | * @return true if the list of std::string contains number, false if not | ||
36 | */ | ||
37 | 4 | bool isListStrNumber(const std::list<std::string> & listStr){ | |
38 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if(listStr.size() == 0lu){return false;} |
39 | 3 | bool isMatch(true); | |
40 | 3 | std::list<std::string>::const_iterator it(listStr.begin()); | |
41 |
6/6✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 3 times.
|
9 | while(isMatch && it != listStr.end()){ |
42 |
1/1✓ Branch 2 taken 6 times.
|
6 | isMatch &= isStrNumber(*it); |
43 | 6 | ++it; | |
44 | } | ||
45 | 3 | return isMatch; | |
46 | } | ||
47 | |||
48 | ///Tels if the character is a number or a '.' | ||
49 | /** @param ch : character to be analysed | ||
50 | * @return true if it is a number or '.', false otherwise | ||
51 | */ | ||
52 | 8 | bool isCharNumberOrDot(char ch){ | |
53 |
6/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
|
8 | return ((ch >= 48 && ch <= 57) || ch == '.'); |
54 | } | ||
55 | |||
56 | ///Tels if the character is a figure, a '.', a -, +, e or E | ||
57 | /** @param ch : character to be analysed | ||
58 | * @return true if the current char is a part of a number, false otherwise | ||
59 | */ | ||
60 | 18 | bool isCharNumberDotMinusPlusE(char ch){ | |
61 |
13/14✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
|
18 | return ((ch >= 48 && ch <= 57) || ch == '.' || ch == '-' || ch == '+' || ch == 'e' || ch == 'E'); |
62 | } | ||
63 | |||
64 | ///Tels if std::string contains figures or dots | ||
65 | /** @param str : std::string to be tested | ||
66 | * @return true if std::string contains only figures or dots | ||
67 | */ | ||
68 | 4 | bool isStrNumberOrDot(const std::string & str){ | |
69 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if(str.size() == 0l){return false;} |
70 | 3 | bool isMatch(true); | |
71 | 3 | long unsigned int i(0lu); | |
72 |
6/6✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 3 times.
|
10 | while(i < str.size() && isMatch){ |
73 | 7 | isMatch = isCharNumberOrDot(str[i]); | |
74 | 7 | ++i; | |
75 | } | ||
76 | 3 | return isMatch; | |
77 | } | ||
78 | |||
79 | ///Tels if std::string std::string contains figures, '.', -, +, e or E | ||
80 | /** @param str : std::string to be tested | ||
81 | * @return true if std::string contains only figures, '.', -, +, e or E | ||
82 | */ | ||
83 | 6 | bool isStrNumberDotMinusPlusE(const std::string & str){ | |
84 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
|
6 | if(str.size() == 0lu){return false;} |
85 | 5 | bool isMatch(true); | |
86 | 5 | long unsigned int i(0lu); | |
87 |
6/6✓ Branch 1 taken 19 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 5 times.
|
23 | while(i < str.size() && isMatch){ |
88 | 18 | isMatch = isCharNumberDotMinusPlusE(str[i]); | |
89 | 18 | ++i; | |
90 | } | ||
91 | 5 | return isMatch; | |
92 | } | ||
93 | |||
94 | ///Tels if the character is a letter or '_' | ||
95 | /** @param ch : character to be analysed | ||
96 | * @return true si c'est une lettre ou un _ , false otherwise | ||
97 | */ | ||
98 | 64 | bool isCharLetter(char ch){ | |
99 |
8/10✓ Branch 0 taken 52 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 12 times.
|
64 | return (((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122) || ((int)ch == 95)); |
100 | } | ||
101 | |||
102 | ///Tels if the character is letter, figure or '_' | ||
103 | /** @param ch : character to be analysed | ||
104 | * @return true if character is a letter, figure or '_', false otherwise | ||
105 | */ | ||
106 | 64 | bool isCharNumberOrLetter(char ch){ | |
107 |
11/14✓ Branch 0 taken 52 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
|
64 | return ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch == 95) || (ch >= 48 && ch <= 57)); |
108 | } | ||
109 | |||
110 | ///Tels if the character is a letter, number, '.' or '_' | ||
111 | /** @param ch : character to be analysed | ||
112 | * @return true if it is a letter, number, '.' or '_', false otherwise | ||
113 | */ | ||
114 | 64 | bool isCharNumberOrLetterOrDot(char ch){ | |
115 |
13/16✓ Branch 0 taken 52 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 10 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 1 times.
|
64 | return ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch == 95) || (ch >= 48 && ch <= 57) || (ch == '.')); |
116 | } | ||
117 | |||
118 | ///Tels if the character is number or star | ||
119 | /** @param ch : character to be analysed | ||
120 | * @return true if it is a number or star, false otherwise | ||
121 | */ | ||
122 | 64 | bool isCharNumberOrStar(char ch){ | |
123 |
6/6✓ Branch 0 taken 62 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 53 times.
|
64 | return ((ch >= 48 && ch <= 57) || ch == '*'); |
124 | } | ||
125 | |||
126 | ///Tels if the character is letter, star or '_' | ||
127 | /** @param ch : character to be analysed | ||
128 | * @return true is letter, star or '_', false otherwise | ||
129 | */ | ||
130 | 64 | bool isCharLetterOrStar(char ch){ | |
131 |
10/12✓ Branch 0 taken 52 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 11 times.
|
64 | return (((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122) || ((int)ch == 95) || ch == '*'); |
132 | } | ||
133 | |||
134 | ///Tels if the character is letter, number, star or '_' | ||
135 | /** @param ch : character to be analysed | ||
136 | * @return true if it is letter, number, star or '_', false otherwise | ||
137 | */ | ||
138 | 64 | bool isCharNumberOrLetterOrStar(char ch){ | |
139 |
13/16✓ Branch 0 taken 52 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 10 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 1 times.
|
64 | return ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch == 95) || (ch >= 48 && ch <= 57) || ch == '*'); |
140 | } | ||
141 | |||
142 | ///Tels if the character is upper case letter | ||
143 | /** @param ch : caractère à tester | ||
144 | * @return true if character is upper case letter, false otherwise | ||
145 | */ | ||
146 | 134 | bool isCharUpperCase(char ch){ | |
147 |
4/4✓ Branch 0 taken 117 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 49 times.
|
134 | return (ch >= 65 && ch <= 90); |
148 | } | ||
149 | |||
150 | ///Tels if the character is lower case letter | ||
151 | /** @param ch : caractère à tester | ||
152 | * @return true if the character is lower case letter, false otherwise | ||
153 | */ | ||
154 | 114 | bool isCharLowerCase(char ch){ | |
155 |
3/4✓ Branch 0 taken 64 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
|
114 | return (ch >= 97 && ch <= 122); |
156 | } | ||
157 | |||
158 | ///Say if the given std::string is in uppercase | ||
159 | /** @param str : std::string to be checked | ||
160 | * @return true if the std::string is in uppercase, false if not | ||
161 | */ | ||
162 | 1 | bool isStrUpperCase(const std::string & str){ | |
163 | 1 | bool isUpper(true); | |
164 | 1 | size_t i(0lu); | |
165 |
5/6✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 1 times.
|
5 | while(i < str.size() && isUpper){ |
166 | 4 | isUpper = isCharUpperCase(str[i]); | |
167 | 4 | ++i; | |
168 | } | ||
169 | 1 | return isUpper; | |
170 | } | ||
171 | |||
172 | ///Say if the given std::string is in lowercase | ||
173 | /** @param str : std::string to be checked | ||
174 | * @return true if the std::string is in lowercase, false if not | ||
175 | */ | ||
176 | 1 | bool isStrLowerCase(const std::string & str){ | |
177 | 1 | bool isLower(true); | |
178 | 1 | size_t i(0lu); | |
179 |
5/6✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 1 times.
|
5 | while(i < str.size() && isLower){ |
180 | 4 | isLower = isCharLowerCase(str[i]); | |
181 | 4 | ++i; | |
182 | } | ||
183 | 1 | return isLower; | |
184 | } | ||
185 | |||
186 | ///Convert std::string in lower case | ||
187 | /** @param str : std::string to be converted | ||
188 | * @return lower case std::string | ||
189 | */ | ||
190 | 3 | std::string strToLower(const std::string & str){ | |
191 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string strOut(""); |
192 | char currentChar; | ||
193 | 3 | long unsigned int size(str.size()); | |
194 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 3 times.
|
45 | for(long unsigned int i(0lu); i < size; ++i){ |
195 | 42 | currentChar = str[i]; | |
196 |
3/3✓ Branch 1 taken 42 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 10 times.
|
42 | if(isCharUpperCase(currentChar)){ |
197 |
1/1✓ Branch 1 taken 32 times.
|
32 | strOut += currentChar + (char)32; |
198 | }else{ | ||
199 |
1/1✓ Branch 1 taken 10 times.
|
10 | strOut += currentChar; |
200 | } | ||
201 | } | ||
202 | 3 | return strOut; | |
203 | } | ||
204 | |||
205 | ///Convert std::string in upper case | ||
206 | /** @param str : std::string to be converted | ||
207 | * @return lower case std::string | ||
208 | */ | ||
209 | 3 | std::string strToUpper(const std::string & str){ | |
210 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string strOut(""); |
211 | char currentChar; | ||
212 | 3 | long unsigned int size(str.size()); | |
213 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 3 times.
|
45 | for(long unsigned int i(0); i < size; ++i){ |
214 | 42 | currentChar = str[i]; | |
215 |
3/3✓ Branch 1 taken 42 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 10 times.
|
42 | if(isCharLowerCase(currentChar)){ |
216 |
1/1✓ Branch 1 taken 32 times.
|
32 | strOut += currentChar - (char)32; |
217 | }else{ | ||
218 |
1/1✓ Branch 1 taken 10 times.
|
10 | strOut += currentChar; |
219 | } | ||
220 | } | ||
221 | 3 | return strOut; | |
222 | } | ||
223 | |||
224 | ///Convert std::string in lower case and space in '_' | ||
225 | /** @param str : std::string to be converted | ||
226 | * @return std::string in lower case and space in '_' | ||
227 | */ | ||
228 | 2 | std::string strToLowerUnderscore(const std::string & str){ | |
229 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::string strOut(""); |
230 | char currentChar; | ||
231 | 2 | long unsigned int size(str.size()); | |
232 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
|
23 | for(long unsigned int i(0lu); i < size; ++i){ |
233 | 21 | currentChar = str[i]; | |
234 |
3/3✓ Branch 1 taken 21 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 17 times.
|
21 | if(isCharUpperCase(currentChar)){ |
235 |
1/1✓ Branch 1 taken 4 times.
|
4 | strOut += currentChar + (char)32; |
236 | }else{ | ||
237 |
3/3✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 1 times.
|
17 | if(currentChar == ' ') strOut += '_'; |
238 |
1/1✓ Branch 1 taken 16 times.
|
16 | else strOut += currentChar; |
239 | } | ||
240 | } | ||
241 | 2 | return strOut; | |
242 | } | ||
243 | |||
244 | ///Convert first letter of the std::string in upper case | ||
245 | /** @param str : std::string to be converted | ||
246 | * @return std::string with first letter of the std::string in upper case | ||
247 | */ | ||
248 | 4 | std::string firstToUpper(const std::string & str){ | |
249 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
4 | if(str.size() == 0lu) return ""; |
250 |
1/1✓ Branch 1 taken 4 times.
|
4 | std::string strOut(str); |
251 |
1/1✓ Branch 1 taken 4 times.
|
4 | char currentChar = strOut[0lu]; |
252 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | if(isCharLowerCase(currentChar)){ |
253 |
1/1✓ Branch 1 taken 2 times.
|
2 | strOut[0lu] = currentChar - (char)32; |
254 | } | ||
255 | 4 | return strOut; | |
256 | 4 | } | |
257 | |||
258 | ///Convert first letter of the std::string in lower case | ||
259 | /** @param str : std::string to be converted | ||
260 | * @return std::string with first letter of the std::string in lower case | ||
261 | */ | ||
262 | 3 | std::string firstToLower(const std::string & str){ | |
263 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
3 | if(str.size() == 0lu) return ""; |
264 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::string strOut(str); |
265 |
1/1✓ Branch 1 taken 3 times.
|
3 | char currentChar = strOut[0lu]; |
266 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
|
3 | if(isCharUpperCase(currentChar)){ |
267 |
1/1✓ Branch 1 taken 2 times.
|
2 | strOut[0lu] = currentChar + (char)32; |
268 | } | ||
269 | 3 | return strOut; | |
270 | 3 | } | |
271 | |||
272 | |||
273 | |||
274 |