-
Notifications
You must be signed in to change notification settings - Fork 2
/
tabletext.cpp
173 lines (162 loc) · 4.38 KB
/
tabletext.cpp
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
#include "tabletext.h"
#include "stringreg.h"
#include <algorithm>
#include <cstring>
#include <stdexcept>
using namespace cab;
TableText::TableText(const std::string &multilineText)
: d_text(multilineText),
d_numRows(0u)
{
countSpaces();
}
TableText::TableText(const char *multilineText)
: d_text(multilineText),
d_numRows(0u)
{
countSpaces();
}
namespace {
/// short lines are treated like they are padded with spaces at the end
void
padShortLines(std::vector<int> &spaces, std::size_t curCol)
{
while (curCol < spaces.size()) {
++spaces[curCol];
++curCol;
}
}
}
void
TableText::countSpaces()
{
std::size_t curCol(0u);
d_spaceCounts.reserve(128u);
for(const char ch : d_text) { // iterate through whole string
if ('\n' == ch) {
padShortLines(d_spaceCounts, curCol);
// increment row count and reset for next line
++d_numRows;
curCol = 0u;
}
else {
if (curCol >= d_spaceCounts.size()) {
d_spaceCounts.resize(curCol+1, d_numRows);
}
if (' ' == ch) {
++d_spaceCounts[curCol];
}
++curCol;
}
}
if (!d_text.empty() && ('\n' != d_text.back())) {
++d_numRows;
}
}
std::vector<int>
TableText::uniqueSpaceCounts() const
{
std::vector<int> spaces(d_spaceCounts);
spaces.push_back(0);
std::sort(spaces.begin(), spaces.end());
auto it = std::unique(spaces.begin(), spaces.end());
spaces.resize(std::distance(spaces.begin(), it));
return spaces;
}
TableText::RowAndColumnList
TableText::tabulate(unsigned minCols) const
{
std::vector<int> spaces(uniqueSpaceCounts());
std::vector<ColumnRange> columns;
columns.reserve(minCols);
for(auto rit=spaces.rbegin(); rit != spaces.rend(); ++rit) {
const int minSpaceForColEnd(*rit);
findColumns(minSpaceForColEnd, columns);
if (columns.size() >= minCols) {
return copyColumns(columns);
}
}
throw std::out_of_range("Unable to find enough columns");
}
void
TableText::findColumns(const int minSpaceForColEnd,
std::vector<ColumnRange> &table) const
{
const std::size_t maxWidth(d_spaceCounts.size());
const int numLines(static_cast<int>(d_numRows));
const int startColumnThreshold((3*numLines)/4);
table.clear();
std::size_t pos=0;
while (pos < maxWidth) {
ColumnRange cr;
// find start
while ((pos < maxWidth) && (d_spaceCounts[pos] >= numLines)) {
++pos;
}
if (pos < maxWidth) {
int maxSpaces(startColumnThreshold);
cr.begin = pos;
// find end
while (pos < maxWidth) {
for(++pos; (pos < maxWidth) && (d_spaceCounts[pos] < minSpaceForColEnd); ++pos) {
if (d_spaceCounts[pos] > maxSpaces) {
maxSpaces = d_spaceCounts[pos];
}
}
if ((pos + 1) < maxWidth) {
if ((d_spaceCounts[pos] >= numLines) && (d_spaceCounts[pos+1] >= numLines)) {
// two consecutive columns of all spaces => column must be done
break;
}
++pos;
if (d_spaceCounts[pos] < maxSpaces) {
// pos + 1 looks like the start of another column
break;
}
}
else {
if ((pos < maxWidth) &&
((d_spaceCounts[pos] <= startColumnThreshold) ||
(d_spaceCounts[pos] == numLines))) {
break;
++pos;
}
}
}
cr.end = pos;
table.push_back(cr);
}
}
}
TableText::RowAndColumnList::value_type
TableText::fieldsFromLine(const std::vector<ColumnRange> &table,
const std::string &line) const
{
std::vector<std::string> columnList;
columnList.reserve(table.size());
for(const ColumnRange &cr : table) {
if (cr.begin < line.size()) {
std::string newCol(cab::trim(line.substr(cr.begin, cr.end-cr.begin)));
columnList.push_back(newCol);
}
else {
columnList.push_back(std::string());
}
}
return columnList;
}
TableText::RowAndColumnList
TableText::copyColumns(const std::vector<ColumnRange> &table) const
{
RowAndColumnList result;
result.reserve(d_numRows);
std::size_t cur(0u), next;
while (std::string::npos != (next = d_text.find('\n', cur))) {
result.push_back(std::move(fieldsFromLine(table, d_text.substr(cur, next-cur))));
cur = next+1;
}
if (cur < d_text.size()) {
result.push_back(std::move(fieldsFromLine(table, d_text.substr(cur))));
}
return result;
}