File: string.cpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (286 lines) | stat: -rw-r--r-- 10,423 bytes parent folder | download | duplicates (2)
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
#include "utils/string.hpp"

#include "common/test.hpp"

using namespace polybar;

TEST(String, ends_with) {
  EXPECT_TRUE(string_util::ends_with("foo", "foo"));
  EXPECT_TRUE(string_util::ends_with("foobar", "bar"));
  EXPECT_TRUE(string_util::ends_with("foobar", ""));
  EXPECT_FALSE(string_util::ends_with("foo", "bar"));
  EXPECT_FALSE(string_util::ends_with("foo", "Foo"));
  EXPECT_FALSE(string_util::ends_with("", "Foo"));
}

TEST(String, upper) {
  EXPECT_EQ("FOO", string_util::upper("FOO"));
  EXPECT_EQ("FOO", string_util::upper("FoO"));
  EXPECT_EQ("FOO", string_util::upper("FOo"));
  EXPECT_EQ("FOO", string_util::upper("Foo"));
}

TEST(String, lower) {
  EXPECT_EQ("bar", string_util::lower("BAR"));
}

TEST(String, compare) {
  EXPECT_TRUE(string_util::compare("foo", "foo"));
  EXPECT_TRUE(string_util::compare("foo", "Foo"));
  EXPECT_FALSE(string_util::compare("foo", "bar"));
}

TEST(String, contains) {
  EXPECT_TRUE(string_util::contains("fooooobar", "foo"));
  EXPECT_TRUE(string_util::contains("barrrrrrfoo", "foo"));
  EXPECT_TRUE(string_util::contains("barrfoobazzz", "foo"));
  EXPECT_TRUE(string_util::contains("foo", "foo"));
  EXPECT_TRUE(string_util::contains("foobar", "foo"));
  EXPECT_TRUE(string_util::contains("foobar", "bar"));
  EXPECT_FALSE(string_util::contains("foo", "Foo"));
  EXPECT_FALSE(string_util::contains("foo", "bar"));
  EXPECT_FALSE(string_util::contains("foobar", "baz"));
  EXPECT_FALSE(string_util::contains("foobAr", "bar"));
}

TEST(String, contains_ignore_case) {
  EXPECT_TRUE(string_util::contains_ignore_case("fooooobar", "foo"));
  EXPECT_TRUE(string_util::contains_ignore_case("barrrrrrfoo", "foo"));
  EXPECT_TRUE(string_util::contains_ignore_case("barrfoobazzz", "foo"));
  EXPECT_TRUE(string_util::contains_ignore_case("fooooobar", "fOO"));
  EXPECT_TRUE(string_util::contains_ignore_case("barrrrrrfoo", "FOo"));
  EXPECT_TRUE(string_util::contains_ignore_case("barrfoobazzz", "FoO"));
  EXPECT_TRUE(string_util::contains_ignore_case("foo", "Foo"));
  EXPECT_FALSE(string_util::contains_ignore_case("foo", "bar"));
  EXPECT_TRUE(string_util::contains_ignore_case("foo", ""));
  EXPECT_FALSE(string_util::contains_ignore_case("", "bar"));
}

TEST(String, replace) {
  EXPECT_EQ("a.c", string_util::replace("abc", "b", "."));
  EXPECT_EQ("a.a", string_util::replace("aaa", "a", ".", 1, 2));
  EXPECT_EQ(".aa", string_util::replace("aaa", "a", ".", 0, 2));
  EXPECT_EQ("Foo bxr baz", string_util::replace("Foo bar baz", "a", "x"));
  EXPECT_EQ("foxoobar", string_util::replace("foooobar", "o", "x", 2, 3));
  EXPECT_EQ("foooobar", string_util::replace("foooobar", "o", "x", 0, 1));
}

TEST(String, replaceAll) {
  EXPECT_EQ("Foo bxr bxzx", string_util::replace_all("Foo bar baza", "a", "x"));
  EXPECT_EQ("hoohoohoo", string_util::replace_all("hehehe", "he", "hoo"));
  EXPECT_EQ("hoohehe", string_util::replace_all("hehehe", "he", "hoo", 0, 2));
  EXPECT_EQ("hehehoo", string_util::replace_all("hehehe", "he", "hoo", 4));
  EXPECT_EQ("hehehe", string_util::replace_all("hehehe", "he", "hoo", 0, 1));
  EXPECT_EQ("113113113", string_util::replace_all("131313", "3", "13"));
}

TEST(String, squeeze) {
  EXPECT_EQ("Squeze", string_util::squeeze("Squeeeeeze", 'e'));
  EXPECT_EQ("bar baz foobar", string_util::squeeze("bar  baz   foobar", ' '));
}

TEST(String, strip) {
  EXPECT_EQ("Strp", string_util::strip("Striip", 'i'));
  EXPECT_EQ("test\n", string_util::strip_trailing_newline("test\n\n"));
}

TEST(String, trim) {
  EXPECT_EQ("x x", string_util::trim("  x x "));
  EXPECT_EQ("testxx", string_util::ltrim("xxtestxx", 'x'));
  EXPECT_EQ("xxtest", string_util::rtrim("xxtestxx", 'x'));
  EXPECT_EQ("test", string_util::trim("xxtestxx", 'x'));
}

TEST(String, trimPredicate) {
  EXPECT_EQ("x\t x", string_util::trim("\t  x\t x   ", isspace));
  EXPECT_EQ("x\t x", string_util::trim("x\t x   ", isspace));
}

TEST(String, join) {
  EXPECT_EQ("A, B, C", string_util::join({"A", "B", "C"}, ", "));
}

TEST(String, split) {
  {
    vector<string> strings = string_util::split("A,B,C", ',');
    EXPECT_EQ(3, strings.size());
    EXPECT_EQ("A", strings[0]);
    EXPECT_EQ("B", strings[1]);
    EXPECT_EQ("C", strings[2]);
  }

  {
    vector<string> strings = string_util::split(",A,,B,,C,", ',');
    EXPECT_EQ(3, strings.size());
    EXPECT_EQ("A", strings[0]);
    EXPECT_EQ("B", strings[1]);
    EXPECT_EQ("C", strings[2]);
  }
}

TEST(String, tokenize) {
  {
    vector<string> strings = string_util::tokenize("A,B,C", ',');
    EXPECT_EQ(3, strings.size());
    EXPECT_EQ("A", strings[0]);
    EXPECT_EQ("B", strings[1]);
    EXPECT_EQ("C", strings[2]);
  }

  {
    using namespace std::string_literals;
    vector<string> strings = string_util::tokenize(",A,,B,,C,", ',');
    vector<string> result{""s, "A"s, ""s, "B"s, ""s, "C"s, ""s};

    EXPECT_TRUE(strings == result);
  }
}

TEST(String, findNth) {
  EXPECT_EQ(0, string_util::find_nth("foobarfoobar", 0, "f", 1));
  EXPECT_EQ(6, string_util::find_nth("foobarfoobar", 0, "f", 2));
  EXPECT_EQ(7, string_util::find_nth("foobarfoobar", 0, "o", 3));
}

TEST(String, hash) {
  unsigned long hashA1{string_util::hash("foo")};
  unsigned long hashA2{string_util::hash("foo")};
  unsigned long hashB1{string_util::hash("Foo")};
  unsigned long hashB2{string_util::hash("Bar")};
  EXPECT_EQ(hashA2, hashA1);
  EXPECT_NE(hashB1, hashA1);
  EXPECT_NE(hashB2, hashA1);
  EXPECT_NE(hashB2, hashB1);
}

TEST(String, floatingPoint) {
  EXPECT_EQ("1.26", string_util::floating_point(1.2599, 2));
  EXPECT_EQ("2", string_util::floating_point(1.7, 0));
  EXPECT_EQ("1.7770000000", string_util::floating_point(1.777, 10));
}

TEST(String, filesize) {
  EXPECT_EQ("3.000 MiB", string_util::filesize_mib(3 * 1024, 3));
  EXPECT_EQ("3.195 MiB", string_util::filesize_mib(3 * 1024 + 200, 3));
  EXPECT_EQ("3 MiB", string_util::filesize_mib(3 * 1024 + 400));
  EXPECT_EQ("4 MiB", string_util::filesize_mib(3 * 1024 + 800));
  EXPECT_EQ("3.195 GiB", string_util::filesize_gib(3 * 1024 * 1024 + 200 * 1024, 3));
  EXPECT_EQ("3 GiB", string_util::filesize_gib(3 * 1024 * 1024 + 400 * 1024));
  EXPECT_EQ("4 GiB", string_util::filesize_gib(3 * 1024 * 1024 + 800 * 1024));
  EXPECT_EQ("3 B", string_util::filesize(3));
  EXPECT_EQ("3 KB", string_util::filesize(3 * 1024));
  EXPECT_EQ("3 MB", string_util::filesize(3 * 1024 * 1024));
  EXPECT_EQ("3 GB", string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024));
  EXPECT_EQ("3 TB", string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024 * 1024));
}

// utf8_to_ucs4 {{{
class Utf8ToUCS4AsciiTest : public testing::TestWithParam<string> {};

const vector<string> utf8_to_ucs4_ascii_list = {"", "Hello World", "\n", "\0", "\u007f"};

INSTANTIATE_TEST_SUITE_P(Inst, Utf8ToUCS4AsciiTest, testing::ValuesIn(utf8_to_ucs4_ascii_list));

/**
 * Test that the conversion to ucs4 works correctly with pure ASCII strings.
 */
TEST_P(Utf8ToUCS4AsciiTest, correctness) {
  string_util::unicode_charlist result_list{};
  string str = GetParam();

  bool valid = string_util::utf8_to_ucs4(str, result_list);
  ASSERT_TRUE(valid);

  ASSERT_EQ(str.size(), result_list.size());

  for (size_t i = 0; i < str.size(); i++) {
    const auto& unicode_char = result_list[i];
    auto c = str[i];

    // Matches the single byte character
    EXPECT_EQ(c, unicode_char.codepoint);
    // Is at the same offset as in the original string
    EXPECT_EQ(i, unicode_char.offset);
    // Only takes a single byte
    EXPECT_EQ(1, unicode_char.length);
  }
}

// String containing a single codepoint and the expected numerical codepoint
using single_test_t = std::pair<string, uint32_t>;
class Utf8ToUCS4SingleTest : public testing::TestWithParam<single_test_t> {};

const vector<single_test_t> utf8_to_ucs4_single_list = {
    {" ", 0x20},              // Single ASCII character
    {"\u007f", 0x7f},         // End of 1 byte range
    {"\u0080", 0x80},         // Start of 2 byte range
    {"\u07ff", 0x7ff},        // End of 2 byte range
    {"\u0800", 0x800},        // Start of 3 byte range
    {"\uffff", 0xffff},       // End of 3 byte range
    {"\U00010000", 0x10000},  // Start of 4 byte range
    {"\U0010ffff", 0x10ffff}, // End of 4 byte range
    {"\U0001f600", 0x1f600},  // Grinning face emoji
};

INSTANTIATE_TEST_SUITE_P(Inst, Utf8ToUCS4SingleTest, testing::ValuesIn(utf8_to_ucs4_single_list));

/**
 * Test that the conversion to ucs4 works correctly with a single UTF8 character
 */
TEST_P(Utf8ToUCS4SingleTest, correctness) {
  string_util::unicode_charlist result_list{};
  const auto [str, codepoint] = GetParam();

  bool valid = string_util::utf8_to_ucs4(str, result_list);
  ASSERT_TRUE(valid);

  ASSERT_EQ(1, result_list.size());

  auto unicode_char = result_list.front();

  EXPECT_EQ(0, unicode_char.offset);
  // Must encompass entire string
  EXPECT_EQ(str.size(), unicode_char.length);
  // Must match expected codepoint
  EXPECT_EQ(codepoint, unicode_char.codepoint);
}

class Utf8ToUCS4InvalidTest : public testing::TestWithParam<string> {};

const vector<string> utf8_to_ucs4_invalid_list = {
    "\x80",         // continuation byte without leading byte
    "\xa0",         // 2 byte code point with only leading byte
    "\xe0",         // 3 byte code point with only leading byte
    "\xf0",         // 4 byte code point with only leading byte
    "\xf0\x80\x80", // 4 byte code point with only 3 bytes
    "\xe0\xf0\x80", // 3 byte code point, 2nd byte has no continuation prefix
};

INSTANTIATE_TEST_SUITE_P(Inst, Utf8ToUCS4InvalidTest, testing::ValuesIn(utf8_to_ucs4_invalid_list));

/**
 * Tests that the conversion correctly returns false for invalid strings.
 */
TEST_P(Utf8ToUCS4InvalidTest, correctness) {
  string_util::unicode_charlist result_list{};
  const auto str = GetParam();
  bool valid = string_util::utf8_to_ucs4(str, result_list);
  EXPECT_FALSE(valid);
  EXPECT_EQ(0, result_list.size());
}

/**
 * Tests that the conversion works with partially valid strings and that invalid parts are dropped.
 */
TEST(String, utf8ToUCS4Partial) {
  string_util::unicode_charlist result_list{};
  string str = "\xe0\x70\x80"; // a valid ascii character between two invalid characters
  bool valid = string_util::utf8_to_ucs4(str, result_list);
  EXPECT_FALSE(valid);
  EXPECT_EQ(1, result_list.size());

  EXPECT_EQ(0x70, result_list[0].codepoint);
  EXPECT_EQ(1, result_list[0].offset);
  EXPECT_EQ(1, result_list[0].length);
}
// }}}