forked from jasp-stats/jasp-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnutils.cpp
337 lines (261 loc) · 7.73 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "columnutils.h"
#ifndef IGNORE_BOOST
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/algorithm/string/predicate.hpp>
#endif
#include <codecvt>
#include <regex>
using namespace std;
using namespace boost::posix_time;
using namespace boost;
///Internally the following actual missing values are used for scalar, ordinal/nominal and text: NAN, std::numeric_limits<int>::lowest() and ""
/// However, users might like to see something else hence we allow `emptyValue` to be changed through the settings.
string ColumnUtils::emptyValue = "";
const stringset ColumnUtils::_defaultEmptyValues = {"NaN", "nan", ".", "NA"};
doublevec ColumnUtils::_currentDoubleEmptyValues = {};
stringset ColumnUtils::_currentEmptyValues = ColumnUtils::_defaultEmptyValues;
void ColumnUtils::setEmptyValues(const stringset &emptyvalues)
{
JASPTIMER_SCOPE(ColumnUtils::setEmptyValues);
_currentEmptyValues = emptyvalues;
processEmptyValues();
}
void ColumnUtils::processEmptyValues()
{
JASPTIMER_SCOPE(ColumnUtils::processEmptyValues);
_currentDoubleEmptyValues.clear();
for (const std::string & curEmptyVal : _currentEmptyValues)
{
double doubleValue;
if (ColumnUtils::getDoubleValue(curEmptyVal, doubleValue))
_currentDoubleEmptyValues.push_back(doubleValue);
}
}
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 >= std::numeric_limits<int>::lowest())
{
intValue = int(intPart);
return true;
}
}
}
catch (...) {}
return false;
}
bool ColumnUtils::getDoubleValue(const string &value, double &doubleValue)
{
try
{
doubleValue = boost::lexical_cast<double>(value);
return true;
}
catch (...) {}
return false;
}
bool ColumnUtils::isDoubleValue(const string &value)
{
try
{
boost::lexical_cast<double>(value);
return true;
}
catch (...) {}
return false;
}
void 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;
}
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;
}
}
bool ColumnUtils::isEmptyValue(const std::string& val)
{
if (val.empty())
return true;
return _currentEmptyValues.count(val);
}
bool ColumnUtils::isEmptyValue(const double &val)
{
JASPTIMER_SCOPE(ColumnUtils::isEmptyValue double);
if (std::isnan(val)) return true;
return std::find(_currentDoubleEmptyValues.begin(), _currentDoubleEmptyValues.end(), val) != _currentDoubleEmptyValues.end();
}
bool ColumnUtils::convertValueToIntForImport(const std::string &strValue, int &intValue)
{
JASPTIMER_SCOPE(ColumnUtils::convertValueToIntForImport);
if (!isEmptyValue(strValue))
{
if (!ColumnUtils::getIntValue(strValue, intValue))
return false;
}
else
intValue = std::numeric_limits<int>::lowest();
return true;
}
bool ColumnUtils::convertValueToDoubleForImport(const std::string & strValue, double & doubleValue)
{
std::string v = strValue;
_deEuropeaniseForImport(v);
if(isEmptyValue(v))
doubleValue = NAN;
else if (!ColumnUtils::getDoubleValue(v, doubleValue))
return false;
return true;
}
std::string ColumnUtils::doubleToString(double dbl, int precision)
{
JASPTIMER_SCOPE(ColumnUtils::doubleToString);
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();
}
std::string ColumnUtils::doubleToDisplayString(double dbl, bool fancyEmptyValue)
{
JASPTIMER_SCOPE(ColumnUtils::doubleToDisplayString);
if (dbl > std::numeric_limits<double>::max()) return "∞";
else if (dbl < std::numeric_limits<double>::lowest()) return "-∞";
else if (ColumnUtils::isEmptyValue(dbl)) return fancyEmptyValue ? ColumnUtils::emptyValue : "";
else return ColumnUtils::doubleToString(dbl);
}
bool ColumnUtils::convertVecToInt(const std::vector<std::string> &values, std::vector<int> &intValues, std::set<int> &uniqueValues, std::map<int, std::string> &emptyValuesMap)
{
JASPTIMER_SCOPE(ColumnUtils::convertVecToInt);
emptyValuesMap.clear();
uniqueValues.clear();
intValues.clear();
intValues.reserve(values.size());
int row = 0;
for (const std::string &value : values)
{
int intValue = std::numeric_limits<int>::lowest();
if (ColumnUtils::convertValueToIntForImport(value, intValue))
{
if (intValue != std::numeric_limits<int>::lowest()) uniqueValues.insert(intValue);
else if (!value.empty()) emptyValuesMap.insert(make_pair(row, value));
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, intstrmap & emptyValuesMap)
{
JASPTIMER_SCOPE(ColumnUtils::convertVecToDouble);
emptyValuesMap.clear();
doubleValues.clear();
doubleValues.resize(values.size());
int row = 0;
for (const std::string & value : values)
{
double doubleValue = static_cast<double>(NAN);
if (ColumnUtils::convertValueToDoubleForImport(value, doubleValue))
{
doubleValues[row] = doubleValue;
if (std::isnan(doubleValue) && value != ColumnUtils::emptyValue)
emptyValuesMap.insert(std::make_pair(row, value));
}
else
{
std::vector<double>().swap(doubleValues); //this clears doubleValues and guarentees its memory is released
return false;
}
row++;
}
return true;
}
// 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;
}
}