Coverage report for ai


src/
File: src/utils/StringUtils.cpp
Date: 2024-06-25 10:57:00
Lines:
40/43
93.0%
Functions:
6/6
100.0%
Branches:
29/52
55.8%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy/ai
4 ** File description:
5 ** DisplayHelp.hpp
6 */
7
8 #include "StringUtils.hpp"
9
10 1 std::string getElementAfter(const std::string &input, char delimiter)
11 {
12 1 std::istringstream iss(input);
13 std::string token;
14
15
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 while (std::getline(iss, token, delimiter))
16 {
17
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if (iss >> token)
18 {
19 1 return token;
20 }
21 }
22
23 return "";
24 1 }
25
26 1 std::string getElementBefore(const std::string &input, char delimiter)
27 {
28 1 std::istringstream iss(input);
29 std::string token;
30
31
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 while (std::getline(iss, token, delimiter))
32 {
33
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if (iss >> token)
34 {
35
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return input.substr(0, input.find(delimiter));
36 }
37 }
38
39 return "";
40 1 }
41
42 2 std::string generateNewNumber(const std::string &oldNumber, int offset)
43 {
44 int num = std::stoi(oldNumber);
45 2 return std::to_string(num + offset);
46 }
47
48 1 std::string replaceNumbersInString(const std::string &input, int offset)
49 {
50 1 std::regex numberRegex("\\d+");
51
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::string output = input;
52
53
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::sregex_iterator currentMatch(input.begin(), input.end(), numberRegex);
54 1 std::sregex_iterator lastMatch;
55
56 1 std::vector<std::pair<size_t, std::string>> replacements;
57
58
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while (currentMatch != lastMatch)
59 {
60 std::smatch match = *currentMatch;
61
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::string oldNumber = match.str();
62
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::string newNumber = generateNewNumber(oldNumber, offset);
63 replacements.push_back(
64 {match.position(), newNumber});
65
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ++currentMatch;
66 }
67
68
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (auto it = replacements.rbegin(); it != replacements.rend(); ++it)
69 {
70
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 output.replace(it->first, it->second.length(), it->second);
71 }
72
73 1 return output;
74 2 }
75
76 3 std::string cleanCarriageReturn(const std::string &input)
77 {
78 3 std::string output = input;
79
80
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
3 if (!output.empty() && output.back() == '\n')
81 1 output.pop_back();
82 3 return output;
83 }
84
85 3 std::vector<std::string> splitByChar(const std::string &str, char delimiter)
86 {
87
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 std::vector<std::string> tokens;
88 std::string token;
89
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 std::istringstream tokenStream(str);
90
3/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3 times.
7 while (std::getline(tokenStream, token, delimiter))
91 {
92
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 tokens.push_back(token);
93 }
94 3 return tokens;
95 3 }
96