forked from idealvin/coost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.cc
140 lines (115 loc) · 4.48 KB
/
str.cc
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
#include "co/unitest.h"
#include "co/str.h"
namespace test {
DEF_test(str) {
DEF_case(split) {
auto v = str::split("x||y", '|');
EXPECT_EQ(v.size(), 3);
EXPECT_EQ(v[0], "x");
EXPECT_EQ(v[1], "");
EXPECT_EQ(v[2], "y");
auto u = str::split(fastring("x||y"), '|');
EXPECT(u == v);
v = str::split("x||y", '|', 1);
EXPECT_EQ(v[0], "x");
EXPECT_EQ(v[1], "|y");
u = str::split(fastring("x||y"), '|', 1);
EXPECT(u == v);
v = str::split("x y", ' ');
EXPECT_EQ(v.size(), 2);
EXPECT_EQ(v[0], "x");
EXPECT_EQ(v[1], "y");
v = str::split("\nx\ny\n", '\n');
EXPECT_EQ(v.size(), 3);
EXPECT_EQ(v[0], "");
EXPECT_EQ(v[1], "x");
EXPECT_EQ(v[2], "y");
u = str::split(fastring("\nx\ny\n"), '\n');
EXPECT(u == v);
v = str::split("||x||y||", "||");
EXPECT_EQ(v.size(), 3);
EXPECT_EQ(v[0], "");
EXPECT_EQ(v[1], "x");
EXPECT_EQ(v[2], "y");
u = str::split(fastring("||x||y||"), "||");
EXPECT(u == v);
v = str::split("||x||y||", "||", 2);
EXPECT_EQ(v[0], "");
EXPECT_EQ(v[1], "x");
EXPECT_EQ(v[2], "y||");
}
DEF_case(replace) {
EXPECT_EQ(str::replace("$@xx$@", "$@", "#"), "#xx#");
EXPECT_EQ(str::replace("$@xx$@", "$@", "#", 1), "#xx$@");
fastring s("hello world");
EXPECT_EQ(str::replace(s, "l", "x"), "hexxo worxd");
EXPECT_EQ(str::replace(s, "o", "x"), "hellx wxrld");
}
DEF_case(strip) {
EXPECT_EQ(str::strip(" \txx\t \n"), "xx");
EXPECT_EQ(str::strip("$@xx@", "$@"), "xx");
EXPECT_EQ(str::strip("$@xx@", "$@", 'l'), "xx@");
EXPECT_EQ(str::strip("$@xx@", "$@", 'r'), "$@xx");
EXPECT_EQ(str::strip("xx", ""), "xx");
EXPECT_EQ(str::strip("", "xx"), "");
EXPECT_EQ(str::strip("$@xx@", '$'), "@xx@");
EXPECT_EQ(str::strip("$@xx@", '$', 'r'), "$@xx@");
EXPECT_EQ(str::strip("$@xx@", '$', 'l'), "@xx@");
EXPECT_EQ(str::strip("$@xx@", '@'), "$@xx");
EXPECT_EQ(str::strip("", '\0'), "");
EXPECT_EQ(str::strip(fastring("$@xx@"), "$@"), "xx");
EXPECT_EQ(str::strip(fastring("$@xx@"), "$@", 'l'), "xx@");
EXPECT_EQ(str::strip(fastring("$@xx@"), "$@", 'r'), "$@xx");
EXPECT_EQ(str::strip(fastring("$@xx@"), '$'), "@xx@");
EXPECT_EQ(str::strip(fastring("$@xx@"), '$', 'r'), "$@xx@");
EXPECT_EQ(str::strip(fastring("$@xx@"), '$', 'l'), "@xx@");
EXPECT_EQ(str::strip(fastring("$@xx@"), '@'), "$@xx");
EXPECT_EQ(str::strip(fastring("$@xx@"), fastring("$@")), "xx");
EXPECT_EQ(str::strip(fastring("$@xx@"), fastring("$@"), 'l'), "xx@");
EXPECT_EQ(str::strip(fastring("$@xx@"), fastring("$@"), 'r'), "$@xx");
EXPECT_EQ(str::strip(fastring("\0xx\0", 4), '\0'), "xx");
EXPECT_EQ(str::strip(fastring("\0xx\0", 4), fastring("x\0", 2)), "");
EXPECT_EQ(str::strip(fastring("\0xox\0", 5), fastring("x\0", 2), 'l'), fastring("ox\0", 3));
}
DEF_case(to) {
EXPECT_EQ(str::to_bool("true"), true);
EXPECT_EQ(str::to_bool("1"), true);
EXPECT_EQ(str::to_bool("false"), false);
EXPECT_EQ(str::to_bool("0"), false);
EXPECT_EQ(str::to_int32("-32"), -32);
EXPECT_EQ(str::to_int32("-4k"), -4096);
EXPECT_EQ(str::to_int64("-64"), -64);
EXPECT_EQ(str::to_int64("-8G"), -(8LL << 30));
EXPECT_EQ(str::to_uint32("32"), 32);
EXPECT_EQ(str::to_uint32("4K"), 4096);
EXPECT_EQ(str::to_uint64("64"), 64);
EXPECT_EQ(str::to_uint64("8t"), 8ULL << 40);
EXPECT_EQ(str::to_double("3.14159"), 3.14159);
}
DEF_case(from) {
EXPECT_EQ(str::from(3.14), "3.14");
EXPECT_EQ(str::from(false), "false");
EXPECT_EQ(str::from(true), "true");
EXPECT_EQ(str::from(1024), "1024");
EXPECT_EQ(str::from(-1024), "-1024");
}
DEF_case(dbg) {
std::vector<fastring> v {
"xx", "yy"
};
EXPECT_EQ(str::dbg(v), "[\"xx\",\"yy\"]");
std::set<int> s {
7, 0, 3
};
EXPECT_EQ(str::dbg(s), "{0,3,7}");
std::map<int, int> m {
{1, 1}, {2, 2}, {3, 3}
};
EXPECT_EQ(str::dbg(m), "{1:1,2:2,3:3}");
std::map<int, fastring> ms {
{1, "1"}, {2, "2"}, {3, "3"}
};
EXPECT_EQ(str::dbg(ms), "{1:\"1\",2:\"2\",3:\"3\"}");
}
}
} // namespace test