-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.hpp
175 lines (145 loc) · 4.2 KB
/
Line.hpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef JM_LINE_HPP
#define JM_LINE_HPP
#include <regex>
#include <string>
#include <vector>
#include <iostream>
#include "ArgManager.hpp"
typedef std::string str;
typedef std::vector<unsigned> uvec;
class Line {
public:
Line() {}
Line(const str& line);
void process(const Arguments& args);
std::string join(const str& delimiter, const uvec& fields);
std::string getValue() const;
unsigned getNum() const;
bool isEmpty() const;
private:
std::string m_line = std::string();
std::string m_joined = std::string();
std::vector<str> m_parts;
bool m_empty = true;
unsigned m_line_num = 0u;
private:
void joinList(const str& delimiter, const uvec& fields);
void joinAll(const str& delimiter);
unsigned getNumParts() const;
void processPart(int part_num, const str& re_search, const str& re_replace);
void processPart(int part_num, const str& regex);
bool find(unsigned needle, const uvec& haystack) const;
void split(const str& delimiter);
std::vector<unsigned> splitFields(const std::string& arg_val) const;
};
Line::Line(const str& line) : m_line(line), m_empty(false)
{
static auto line_num = 0;
m_line_num = ++line_num;
}
void Line::split(const str& delimiter)
{
auto pos_start = 0u;
auto pos_end = str::npos;
while((pos_end = m_line.find(delimiter,pos_start)) != str::npos) {
m_parts.push_back(m_line.substr(pos_start, (pos_end-pos_start)));
pos_start = pos_end + 1;
}
m_parts.push_back(m_line.substr(pos_start, (pos_end-pos_start)));
}
std::string Line::getValue() const
{
return m_line;
}
bool Line::isEmpty() const
{
return m_empty;
}
unsigned Line::getNum() const
{
return m_line_num;
}
unsigned Line::getNumParts() const
{
return m_parts.size();
}
void Line::process(const Arguments& args)
{
static const auto fields = splitFields(args.get("-f"));
static const auto re_fields = splitFields(args.get("-p"));
static const auto delimiter = args.get("-d");
static const auto inverse = args.get("-i");
static const auto re_search = args.get("-xs");
static const auto re_replace = args.get("-xr");
// split the word
split(delimiter);
for (auto i = 0u; i<getNumParts(); ++i) {
auto process = false;
if (re_search.empty()) {
process = false;
} else if (re_fields.size() == 0) {
process = true;
} else if (find(i+1, re_fields)) {
process = (inverse == "0");
} else {
process = (inverse == "1");
}
if (process) {
processPart(i, re_search, re_replace);
}
}
// join requested fields and save string
m_line = join(delimiter, fields);
}
bool Line::find(unsigned needle, const uvec& haystack) const
{
return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
}
void Line::processPart(int part_num, const str& re_search, const str& re_replace)
{
try {
static const std::regex r(re_search);
m_parts[part_num] = std::regex_replace(m_parts[part_num], r, re_replace);
} catch (...) {}
}
str Line::join(const str& delimiter, const uvec& fields)
{
if (fields.size() == 0) {
joinAll(delimiter);
} else {
joinList(delimiter, fields);
}
return m_joined;
}
void Line::joinList(const str& delimiter, const uvec& fields)
{
bool first = true;
for (auto i : fields) {
if (i<=getNumParts()) {
m_joined += first ? "" : delimiter;
m_joined += m_parts[i-1];
first = false;
}
}
}
void Line::joinAll(const str& delimiter)
{
for (auto i = 0u; i<getNumParts(); ++i) {
m_joined += (i == 0) ? "" : delimiter;
m_joined += m_parts[i];
}
}
std::vector<unsigned> Line::splitFields(const std::string& s) const
{
std::vector<unsigned> fields;
auto regex = std::regex(",");
auto begin = std::sregex_token_iterator(s.begin(), s.end(), regex, -1);
auto end = std::sregex_token_iterator();
std::for_each(begin, end, [&](const std::string& m) {
if (!m.empty()) {
fields.push_back(std::stoul(m));
}
});
return fields;
}
#endif //JM_LINE_HPP