forked from mm304321141/zzz_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_test.cpp
114 lines (104 loc) · 3.27 KB
/
split_test.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
#define _SCL_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING
#include "split_iterator.h"
#include "chash_set.h"
#include <chrono>
#include <iostream>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <string>
#define assert(exp) assert_proc(exp, #exp, __FILE__, __LINE__)
auto assert_proc = [](bool no_error, char const *query, char const *file, size_t line)
{
if(!no_error)
{
struct hasher
{
size_t operator()(std::tuple<char const *, char const *, size_t> const &ref) const
{
return
std::hash<std::uintptr_t>()(reinterpret_cast<std::uintptr_t>(std::get<0>(ref))) ^
std::hash<std::uintptr_t>()(reinterpret_cast<std::uintptr_t>(std::get<1>(ref))) ^
std::hash<size_t>()(std::get<2>(ref));
}
};
static chash_set<std::tuple<char const *, char const *, size_t>, hasher> check;
if(check.emplace(query, file, line).second)
{
printf("%s(%zd):%s\n", file, line, query);
}
}
};
int main()
{
auto test_to_real = [](char const *s)
{
double l = std::atof(s);
double r = string_ref<>(s).to_value<double>();
printf("%f\n%f\n%f\n\n", l, r, (l - r) / l);
};
test_to_real("1234567890.1234567890");
test_to_real("-1234567890.1234567890e3");
test_to_real("1234567890.1234567890e-3");
test_to_real("1234567890123456789012345678901234567890.1234567890");
test_to_real("-1234567890123456789012345678901234567890.1234567890e3");
test_to_real("1234567890123456789012345678901234567890.1234567890e-3");
test_to_real("0.000000000000000000000000000000000000123456789012345678901234567890");
test_to_real("-0.000000000000000000000000000000000000123456789012345678901234567890e3");
test_to_real("0.000000000000000000000000000000000000123456789012345678901234567890e-3");
[]
{
std::string str = "1234,5678.9012 3456";
for(auto item : make_split_any_of(str, ",. "))
{
if(item != "1234")
{
std::cout << item.to_value<float>() << std::endl;
}
}
for(auto item : make_split(str, "34"))
{
std::cout << item.to_value<int>() << std::endl;
}
}();
[]
{
std::string str = "/1//23/456///";
auto split = make_split(str, '/');
std::vector<std::string> token;
std::copy(split.begin(), split.end(), std::back_inserter(token));
for(auto item : token)
{
std::cout << item << std::endl;
}
}();
[]
{
std::string str = "/1//23/456///";
auto split = make_split(str, '/');
for(size_t i = 0; i <= split.size(); ++i)
{
std::cout << split[i].to_value<int>() << std::endl;
}
}();
[]
{
auto str = "aaa,111,ccc";
std::string v1;
int v2;
string_ref<> v3;
make_split(string_ref<>(str), ',').fill(v1, v2, v3);
}();
[]
{
std::wstring str = L"aaa,111,ccc";
std::wstring v1;
float v2;
string_ref<wchar_t> v3;
make_split(str, L',').fill(v1, v2, v3);
}();
system("pause");
}