forked from jasp-stats/jasp-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumnutils.cpp
270 lines (214 loc) · 5.81 KB
/
columnutils.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "columnutils.h"
#include "utils.h"
//#include "emptyvalues.h"
#ifndef IGNORE_BOOST
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/algorithm/string/predicate.hpp>
#endif
#include <codecvt>
#include <regex>
#include "emptyvalues.h"
#include "timers.h"
using namespace std;
using namespace boost::posix_time;
using namespace boost;
bool ColumnUtils::getIntValue(const string &value, int &intValue)
{
try
{
intValue = boost::lexical_cast<int>(value);
return true;
}
catch (...) {}
return false;
}
bool ColumnUtils::isIntValue(const string &value)
{
try
{
boost::lexical_cast<int>(value);
return true;
}
catch (...) {}
return false;
}
bool ColumnUtils::getIntValue(const double &value, int &intValue)
{
JASPTIMER_SCOPE(ColumnUtils::getIntValue);
try
{
double intPart;
if (modf(value, &intPart) == 0.0)
{
if (intPart <= std::numeric_limits<int>::max() && intPart >= EmptyValues::missingValueInteger)
{
intValue = int(intPart);
return true;
}
}
}
catch (...) {}
return false;
}
bool ColumnUtils::getDoubleValue(const string &value, double &doubleValue)
{
doubleValue = EmptyValues::missingValueDouble;
if(value == "∞" || value == "-∞")
{
doubleValue = std::numeric_limits<double>::infinity() * (value == "-∞" ? -1 : 1);
return true;
}
try
{
doubleValue = boost::lexical_cast<double>(deEuropeaniseForImport(value));
return true;
}
catch (...) {}
return false;
}
doubleset ColumnUtils::getDoubleValues(const stringset & values, bool stripNAN)
{
doubleset result;
for (const std::string & val : values)
{
double doubleValue;
if (getDoubleValue(val, doubleValue) && !std::isnan(doubleValue))
result.insert(doubleValue);
}
return result;
}
bool ColumnUtils::isDoubleValue(const string &value)
{
static double last;
return getDoubleValue(value, last);
}
bool ColumnUtils::convertVecToInt(const stringvec &values, intvec & intValues, intset & uniqueValues)
{
JASPTIMER_SCOPE(ColumnUtils::convertVecToInt);
uniqueValues.clear();
intValues.clear();
intValues.reserve(values.size());
int row = 0;
for (const std::string &value : values)
{
int intValue = EmptyValues::missingValueInteger;
if (ColumnUtils::getIntValue(value, intValue))
{
if (intValue != EmptyValues::missingValueInteger)
uniqueValues.insert(intValue);
intValues.push_back(intValue);
}
else
{
std::vector<int>().swap(intValues); //this clears intValues and guarentees its memory is released
return false;
}
row++;
}
return true;
}
bool ColumnUtils::convertVecToDouble(const stringvec & values, doublevec & doubleValues)
{
JASPTIMER_SCOPE(ColumnUtils::convertVecToDouble);
doubleValues.clear();
doubleValues.resize(values.size());
int row = 0;
for (const std::string & value : values)
{
double doubleValue = static_cast<double>(EmptyValues::missingValueDouble);
if (ColumnUtils::getDoubleValue(value, doubleValue))
doubleValues[row] = doubleValue;
else
{
std::vector<double>().swap(doubleValues); //this clears doubleValues and guarentees its memory is released
return false;
}
row++;
}
return true;
}
std::string ColumnUtils::deEuropeaniseForImport(std::string value)
{
int dots = 0,
commas = 0;
for (const char & k : value)
if (k == '.') dots++;
else if (k == ',') commas++;
if(commas == 1 && dots == 0)
for (char & k : value)
if(k == ',')
{
k = '.';
return value;
}
if (commas > 0)
{
std::string uneurope = value;
if (dots > 0)
{
size_t i = 0,
j = 0;
for (;i < value.size(); i++)
if (value[i] != '.')
{
uneurope[j] = value[i];
j++;
}
uneurope.resize(j);
}
for (size_t i = 0; i < uneurope.length(); i++)
if (uneurope[i] == ',')
{
uneurope[i] = '.';
break;
}
value = uneurope;
}
return value;
}
std::string ColumnUtils::doubleToStringMaxPrec(double dbl)
{
constexpr auto max_precision{std::numeric_limits<long double>::digits10 + 1};
return doubleToString(dbl, max_precision);
}
std::string ColumnUtils::doubleToString(double dbl, int precision)
{
JASPTIMER_SCOPE(ColumnUtils::doubleToString);
if (dbl > std::numeric_limits<double>::max()) return "∞";
if (dbl < std::numeric_limits<double>::lowest()) return "-∞";
std::stringstream conv; //Use this instead of std::to_string to make sure there are no trailing zeroes (and to get full precision)
conv << std::setprecision(precision);
conv << dbl;
return conv.str();
}
// hex should be 4 hexadecimals characters
std::string ColumnUtils::_convertEscapedUnicodeToUTF8(std::string hex)
{
JASPTIMER_SCOPE(ColumnUtils::_convertEscapedUnicodeToUTF8);
std::istringstream iss(hex);
uint32_t bytes;
#ifdef _WIN32
static std::wstring_convert<std::codecvt_utf8<unsigned int>, unsigned int> conv;
#else
static std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
#endif
// Read the 4 hexadecimals as bytes, and convert these bytes into UTF8.
if (iss >> std::hex >> bytes) hex = conv.to_bytes(char32_t(bytes));
return hex;
}
// Replace all <U+FFFF> in str by their UT8 characters.
void ColumnUtils::convertEscapedUnicodeToUTF8(std::string& inputStr)
{
JASPTIMER_SCOPE(ColumnUtils::convertEscapedUnicodeToUTF8);
static const std::regex unicodeExpression ("<U\\+([0-9a-fA-F]{4})>");
std::smatch match;
auto begin = inputStr.cbegin();
while (std::regex_search(begin, inputStr.cend(), match, unicodeExpression))
{
std::string utf8 = _convertEscapedUnicodeToUTF8(match[1].str()); // match 1 is the first group of the regexp: that is the 4 hexadecimals.
auto pos = match.position(0); // position of the whole sequence in str.
inputStr.replace(begin + pos, begin + pos + 8, utf8); // 8 is the number of characters of '<U+FFFF>'
// str iterators cannot be trusted after replace. They must be recalculated from str.
begin = inputStr.begin() + pos;
}
}