-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThrowStringManipulation.cxx
126 lines (107 loc) · 2.99 KB
/
ThrowStringManipulation.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* \file ThrowStringManipulation.cxx
* \brief Implementation of functions for string manipulation.
*/
// std
#include <sstream>
#include <string>
#include <vector>
// Throw
#include "Throw.h"
/**
* \ingroup StringsManipulation
* \brief Returns random string of length 6.
*/
std::string Throw::RandomString() {
std::string str = RandomString(6);
return str;
}
/**
* \ingroup StringsManipulation
* \brief Returns random string of variable length.
*
* Solution found
* <a href="https://stackoverflow.com/questions/440133">here</a>.
*/
std::string Throw::RandomString(size_t length) {
auto randchar = []() -> char {
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
/**
* \ingroup StringsManipulation
* \brief Splits string at delimiters and returns vector of strings.
*
* Solution found
* <a href="https://www.fluentcpp.com/2017/04/21/">here</a>.
*/
std::vector<std::string> Throw::SplitString(const std::string& s,
char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter)) {
tokens.emplace_back(token);
}
return tokens;
}
/**
* \ingroup StringsManipulation
* \brief Replace sub-string in first string.
*
* Solution found
* <a href="https://stackoverflow.com/questions/3418231">here</a>.
*/
bool Throw::ReplaceString(std::string& firstString,
const std::string& fromString,
const std::string& toString) {
size_t startPos = firstString.find(fromString, 0);
if (startPos == std::string::npos) {
return false;
}
firstString.replace(startPos, fromString.length(), toString);
return true;
}
/**
* \ingroup StringsManipulation
* \brief Remove last character from the first string if the first string
* contains it.
*/
bool Throw::RemoveLastCharacter(std::string& firstString,
const std::string& c) {
if (firstString.empty()) {
return false;
}
if (c.size() != 1) {
return false;
}
if (c.compare(&firstString.back()) != 0) {
return false;
}
firstString.replace(firstString.size() - 1, firstString.size(), "");
return true;
}
/**
* \ingroup StringsManipulation
* \brief Returns true if second string is found inside of the first one.
*/
bool Throw::FindString(const std::string& firstString,
const std::string& secondString) {
return firstString.find(secondString, 0) != std::string::npos;
}
/**
* \ingroup StringsManipulation
* \brief Returns true if strings match exactly.
*/
bool Throw::StringsMatch(const std::string& firstString,
const std::string& secondString) {
return firstString.compare(secondString) == 0;
}