Back

myString.h coverage

overall coverage: 100.0%

line# code(run#)
1 #ifndef STRING_H_HF
2 #define STRING_H_HF
3
4 #include <cstddef>
5 #include <ostream>
6 #include <istream>
7
8 #ifndef NO_MEMTRACE
9 #include "memtrace.h"
10 #endif
11
12 /**
13 * @brief Own string class.
14 *
15 */
16
17 class String
18 {
19 friend std::ostream &operator<<(std::ostream &os, const String &s);
20 friend std::istream &operator>>(std::istream &is, String &s);
21
22 /**
23 * @brief Linked list element to store data.
24 *
25 */
26 class DataBlock
27 {
28 public:
29 DataBlock *prev = nullptr;
30 char data[20];
31 DataBlock *next = nullptr;
32 };
33
34 /**
35 * @brief Cache for indexing string.
36 *
37 */
38 class DataCache(311)
39 {
40 String &string;
41 size_t dataBlockIndex = 0;
42 DataBlock *dataBlock;
43
44 public:
45 /**
46 * @brief Construct a new DataCache object from a string.
47 *
48 * @param s string for caching
49 */
50 explicit DataCache(String &s);
51
52 /**
53 * @brief Copy constructor for DataCache.
54 *
55 * @param c DataCache to be copied
56 */
57 DataCache(const DataCache &c);
58
59 /**
60 * @brief Get a character from the string.
61 * Does not check for overindexing.
62 * @param idx index of the desired character
63 * @return reference for the indexed character
64 */
65 char &getChar(size_t idx);
66
67 /**
68 * @brief reset the cache to the original state.
69 * Recommended to run when the linked list changes.
70 */
71 void reset();
72
73 /**
74 * @brief Destroy the DataCache object.
75 *
76 */
77 ~DataCache();
78 };
79
80 size_t size;
81 DataBlock *startBlock;
82 DataBlock *endBlock;
83 mutable DataCache cache;
84 mutable char *cString = nullptr;
85
86 /**
87 * @brief Deletes linked list of datablocks from a given point
88 *
89 * @param from delete from this datablock, this will also be deleted
90 */
91 void deleteDataBlockList(DataBlock *from);
92
93 public:
94 /**
95 * @brief Construct a new empty String object.
96 *
97 */
98 String();
99
100 /**
101 * @brief Construct a new String object from a single char.
102 *
103 * @param c character
104 */
105 explicit String(char c);
106
107 /**
108 * @brief Construct a new String object from a const char *.
109 *
110 * @param s pointer to a c-string, must include \0 at the end
111 */
112 explicit String(const char *s);
113
114 /**
115 * @brief Copy constructor for string.
116 *
117 * @param s string to be copied
118 */
119 String(const String &s);
120
121 /**
122 * @brief Assign the value of a single char to the string.
123 * The previous contents of the string are discarded.
124 * @param c character
125 * @return reference of the current string, can be daisy-chained
126 */
127 String &operator=(char c);
128
129 /**
130 * @brief Assign the value of a c-string to the string.
131 * The previous contents of the string are discarded.
132 * @param s c-string
133 * @return reference of the current string, can be daisy-chained
134 */
135 String &operator=(const char *s);
136
137 /**
138 * @brief Assign the value of another string to the string.
139 * The previous contents of the string are discarded.
140 * @param s string that the value shoule be coiped from
141 * @return reference of the current string, can be daisy-chained
142 */
143 String &operator=(const String &s);
144
145 /**
146 * @brief Indexing operator.
147 * Throws an std::out_of_range exception in case of over or underindexing.
148 * @param idx index of the desired character
149 * @return reference to the indexed character
150 */
151 char &operator[](size_t idx);
152
153 /**
154 * @brief Constant indexing operator.
155 * Throws an std::out_of_range exception in case of over or underindexing.
156 * @param idx index of the desired character
157 * @return indexed character
158 */
159 char operator[](size_t idx) const;
160
161 /**
162 * @brief Compare with a single character.
163 * Only can be equal if the string has a length of 1
164 * @param c character to be compared with
165 * @return true string and character are the same
166 * @return false otherwise
167 */
168 bool operator==(char c) const;
169
170 /**
171 * @brief Compare whti a c-string.
172 * Only can be equal if the length is the same.
173 * @param s c-string to be compared with
174 * @return true string and c-string are equal
175 * @return false otherwise
176 */
177 bool operator==(const char *s) const;
178
179 /**
180 * @brief Compare with another string.
181 * Only can be equal if the length is the same.
182 * @param s string to be compared with
183 * @return true two strings are the same
184 * @return false otherwise
185 */
186 bool operator==(const String &s) const;
187
188 /**
189 * @brief Compare with a single character.
190 * Only can be equal if the string has a length of 1
191 * @param c character to be compared with
192 * @return false string and character are the same
193 * @return true otherwise
194 */
195 bool operator!=(char c) const;
196
197 /**
198 * @brief Compare whti a c-string.
199 * Only can be equal if the length is the same.
200 * @param s c-string to be compared with
201 * @return false string and c-string are equal
202 * @return true otherwise
203 */
204 bool operator!=(const char *s) const;
205
206 /**
207 * @brief Compare with another string.
208 * Only can be equal if the length is the same.
209 * @param s string to be compared with
210 * @return false two strings are the same
211 * @return true otherwise
212 */
213 bool operator!=(const String &s) const;
214
215 /**
216 * @brief Add a character to the end of the string.
217 * Does not change original string.
218 * @param c character to be added
219 * @return a new string with the character on the end
220 */
221 String operator+(char c) const;
222
223 /**
224 * @brief Add a c-string to the end of the string.
225 * Does not change original string.
226 * @param s c-string to be added
227 * @return a new string with the c-string added onto the end
228 */
229 String operator+(const char *s) const;
230
231 /**
232 * @brief Add a string to the end of the string.
233 * Does not change original string.
234 * @param s string to be added
235 * @return a new string with the string added to the end
236 */
237 String operator+(const String &s) const;
238
239 /**
240 * @brief Append a character to the current string.
241 * Changes the current string.
242 * @param c character to be added
243 * @return reference to the current string, which includes the character
244 */
245 String &operator+=(char c);
246
247 /**
248 * @brief Append a c-string to the current string.
249 * Changes the current string.
250 * @param s c-string to be added
251 * @return reference to the current string, which includes the c-string
252 */
253 String &operator+=(const char *s);
254
255 /**
256 * @brief Append a string to the current string.
257 * Changes the current string.
258 * @param s string to be added
259 * @return reference to the current string, which includes the string
260 */
261 String &operator+=(const String &s);
262
263 /**
264 * @brief Create a substring from the string.
265 *
266 * @param from index of the first character of the substring, inclusive
267 * @param length count of the characters of the substring
268 * @return a new string containing the desired part of the string
269 */
270 String substr(size_t from, size_t length) const;
271
272 /**
273 * @brief Create a c-string closed by a \0.
274 *
275 * @param autoFree true: the c-string will be automatically deleted when the string destructs
276 * false: the user has to delete the memory used for the c-string
277 * @return pointer to the c-string
278 */
279 const char *c_str(bool autoFree = true) const;
280
281 /**
282 * @brief Free the memory allocated by c_string() before the string destructs.
283 *
284 */
285 void free_c_str() const;
286
287 /**
288 * @brief Length of the string.
289 *
290 * @return length
291 */
292 size_t length() const;
293
294 /**
295 * @brief Find a character in the string.
296 * Returns on first match.
297 * Throws an std::out_of_range if it was not found.
298 * @param c character to be found
299 * @return index of the character
300 */
301 size_t find(char c) const;
302
303 /**
304 * @brief Find a c-string in the string.
305 * Returns on first match.
306 * Throws an std::out_of_range if it was not found.
307 * @param s c-string to be found
308 * @return the index of the first character of the c-string
309 */
310 size_t find(const char *s) const;
311
312 /**
313 * @brief Find a string in the string.
314 * Returns on first match.
315 * Throws an std::out_of_range if it was not found.
316 * @param s string to be found
317 * @return index of the first character of the string
318 */
319 size_t find(const String &s) const;
320
321 class ConstIterator;
322
323 /**
324 * @brief Iterator for string.
325 *
326 */
327 class Iterator(5)
328 {
329 friend String;
330
331 size_t idx;
332 mutable DataCache cache;
333
334 /**
335 * @brief Construct a new Iterator object.
336 *
337 * @param i index of the character
338 * @param s reference to the string
339 */
340 Iterator(size_t i, String &s);
341
342 public:
343 /**
344 * @brief Copy constructor for iterator.
345 *
346 * @param it iterator to be copied
347 */
348 Iterator(const Iterator &it);
349
350 /**
351 * @brief Increment iterator.
352 *
353 * @return reference to the incremented iterator
354 */
355 Iterator &operator++();
356
357 /**
358 * @brief Decrement iterator.
359 *
360 * @return Reference to the decremented iterator
361 */
362 Iterator &operator--();
363
364 /**
365 * @brief Get the character referenced by the iterator.
366 *
367 * @return reference to the character
368 */
369 char &operator*() const;
370
371 /**
372 * @brief Compare with another iterator.
373 *
374 * @param it iterator to be compared with
375 * @return true iterators point to the same character
376 * @return false otherwise
377 */
378 bool operator==(const Iterator &it) const;
379 /**
380 * @brief Compare with another const iterator.
381 *
382 * @param it iterator to be compared with
383 * @return true iterators point to the same character
384 * @return false otherwise
385 */
386 bool operator==(const ConstIterator &it) const;
387
388 /**
389 * @brief Compare with another iterator.
390 *
391 * @param it iterator to be compared with
392 * @return false iterators point to the same character
393 * @return true otherwise
394 */
395 bool operator!=(const Iterator &it) const;
396
397 /**
398 * @brief Compare with another const iterator.
399 *
400 * @param it iterator to be compared with
401 * @return false iterators point to the same character
402 * @return true otherwise
403 */
404 bool operator!=(const ConstIterator &it) const;
405
406 /**
407 * @brief Destroy the Iterator object
408 *
409 */
410 ~Iterator();
411 };
412
413 /**
414 * @brief Iterator for constant string.
415 *
416 */
417 class ConstIterator(6)
418 {
419 friend String;
420
421 size_t idx;
422 mutable DataCache cache;
423
424 /**
425 * @brief Construct a new Iterator object.
426 *
427 * @param i index of the character
428 * @param s reference to the string
429 */
430 ConstIterator(size_t i, String &s);
431
432 public:
433 /**
434 * @brief Copy constructor for const iterator.
435 *
436 * @param it iterator to be copied
437 */
438 ConstIterator(const ConstIterator &it);
439
440 /**
441 * @brief Construct a new Const Iterator object from Iterator.
442 *
443 * @param it iterator to be copied
444 */
445 explicit ConstIterator(const Iterator &it);
446
447 /**
448 * @brief Increment iterator.
449 *
450 * @return reference to the incremented iterator
451 */
452 ConstIterator &operator++();
453
454 /**
455 * @brief Decrement iterator.
456 *
457 * @return Reference to the decremented iterator
458 */
459 ConstIterator &operator--();
460
461 /**
462 * @brief Get the character referenced by the iterator.
463 *
464 * @return character
465 */
466 char operator*() const;
467
468 /**
469 * @brief Compare with another iterator.
470 *
471 * @param it iterator to be compared with
472 * @return true iterators point to the same character
473 * @return false otherwise
474 */
475 bool operator==(const Iterator &it) const;
476
477 /**
478 * @brief Compare with another const iterator.
479 *
480 * @param it iterator to be compared with
481 * @return true iterators point to the same character
482 * @return false otherwise
483 */
484 bool operator==(const ConstIterator &it) const;
485
486 /**
487 * @brief Compare with another iterator.
488 *
489 * @param it iterator to be compared with
490 * @return false iterators point to the same character
491 * @return true otherwise
492 */
493 bool operator!=(const Iterator &it) const;
494
495 /**
496 * @brief Compare with another const iterator.
497 *
498 * @param it iterator to be compared with
499 * @return false iterators point to the same character
500 * @return true otherwise
501 */
502 bool operator!=(const ConstIterator &it) const;
503
504 /**
505 * @brief Destroy the Const Iterator object
506 *
507 */
508 ~ConstIterator();
509 };
510
511 /**
512 * @brief Get an iterator pointing to the start of the string.
513 *
514 * @return iterator
515 */
516 Iterator begin();
517
518 /**
519 * @brief Get an iterator pointing to the start of the const string
520 *
521 * @return const iterator
522 */
523 ConstIterator begin() const;
524
525 /**
526 * @brief Get an iterator pointing to the character after the end of the string.
527 *
528 * @return iterator
529 */
530 Iterator end();
531
532 /**
533 * @brief Get an iterator pointing to the character after the end of the const string.
534 *
535 * @return const iterator
536 */
537 ConstIterator end() const;
538
539 /**
540 * @brief Destroy the String object
541 *
542 */
543 ~String();
544 };
545
546 /**
547 * @brief Print string to std::ostream
548 *
549 * @param os stream to print the string to
550 * @param s string to print
551 * @return reference to the original stream
552 */
553 std::ostream &operator<<(std::ostream &os, const String &s);
554
555 /**
556 * @brief Read a line from std::istream.
557 * Reads until \r or \\n or \0
558 * @param is stream to read from
559 * @param s string to read into
560 * @return std::istream&
561 */
562 std::istream &operator>>(std::istream &is, String &s);
563
564 #endif