-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.h
138 lines (123 loc) · 3.89 KB
/
utils.h
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
127
128
129
130
131
132
133
134
135
136
137
138
/**
Compiler Front-end Project
utils.h
Purpose: contains generic multiple methods to be reused by more than one component.
@author Amr Elzawawy
@version 1.0
@date 29/3/2020
*/
#ifndef LEXGEN_UTILS_H
#define LEXGEN_UTILS_H
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <unordered_set>
namespace util {
/**
Removes trailing and starting spaces from a string. (in-place)
@param str the string to remove from.
*/
inline void trimBothEnds(std::string& str)
{
const std::string& chars = "\t\n\v\f\r ";
str.erase(str.find_last_not_of(chars)+1);
str.erase(0, str.find_first_not_of(chars));
}
/**
Removes the first and last chars whatever their value from a string. (in-place)
@param str the string to remove from.
*/
inline void stripFirstAndLastChars(std::string& str)
{
if (str.size()>2) {
str.erase(0, 1);
str.erase(str.length()-1);
}
}
/**
Removes all spaces in every position from a string. (in-place)
@param str the string to remove from.
*/
inline void removeAllSpaces(std::string& str)
{
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
}
inline void removeExtraSpaces(std::string& str)
{
bool seen_space = false;
auto end{std::remove_if(str.begin(), str.end(),
[&seen_space](unsigned ch) {
bool is_space = std::isspace(ch);
std::swap(seen_space, is_space);
return seen_space && is_space;
}
)
};
if (end!=str.begin() && std::isspace(static_cast<unsigned>(end[-1])))
--end;
str.erase(end, str.end());
}
/**
Splits a string on any given delimiter character into a vector of tokens.
@param str the string to operate on.
@return vector<string> the tokens resulted from the split of the string.
*/
inline std::vector<std::string> splitOnDelimiter(const std::string& str, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, token, delimiter))
tokens.push_back(token);
return tokens;
}
inline std::vector<std::string> splitOnFirstOfDelimiters(const std::string& str, const std::string& delimiters)
{
std::size_t current, previous = 0;
std::vector<std::string> tokens;
current = str.find_first_of(delimiters);
while (current!=std::string::npos) {
std::string token = str.substr(previous, current-previous);
tokens.push_back(token);
previous = current+1;
current = str.find_first_of(delimiters, previous);
}
tokens.push_back(str.substr(previous, current-previous));
return tokens;
}
inline std::vector<std::string> splitOnStringDelimiter(const std::string& str, const std::string& delimiter)
{
size_t pos_start = 0, pos_end, delimiter_len = delimiter.length();
std::string token;
std::vector<std::string> tokens;
while ((pos_end = str.find(delimiter, pos_start))!=std::string::npos) {
token = str.substr(pos_start, pos_end-pos_start);
pos_start = pos_end+delimiter_len;
tokens.push_back(token);
}
tokens.push_back(str.substr(pos_start));
return tokens;
}
/**
A Generic Comparable Function that works on pairs and defines comparability based on first element's length.
@param a the first pair in comparison.
@param b the second pair in comparison.
@return bool a boolean that is true if the first pair is larger and false otherwise.
*/
template<class T>
bool comparePairsAccordingToFirstLength(const std::pair<T, T>& a,
const std::pair<T, T>& b)
{
return a.first.length()>b.first.length();
}
template<typename T>
std::vector<T*> convertFrom(std::vector<T>& source)
{
std::vector<T*> target(source.size());
std::transform(source.begin(), source.end(), target.begin(), [](T& t) { return &t; });
return target;
}
}
#endif //LEXGEN_UTILS_H