2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author Kristina Holst (kholst@fb.com)
18 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
20 #include <folly/Range.h>
22 #include <folly/portability/GTest.h>
23 #include <folly/portability/Memory.h>
24 #include <folly/portability/SysMman.h>
31 #include <type_traits>
33 #include <boost/range/concepts.hpp>
34 #include <boost/algorithm/string/trim.hpp>
36 using namespace folly;
37 using namespace folly::detail;
40 static_assert(std::is_literal_type<StringPiece>::value, "");
42 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
44 TEST(StringPiece, All) {
45 const char* foo = "foo";
46 const char* foo2 = "foo";
50 // we expect the compiler to optimize things so that there's only one copy
51 // of the string literal "foo", even though we've got it in multiple places
52 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
53 // comparison rather than lexical
55 // the string object creates copies though, so the c_str of these should be
57 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
59 // test the basic StringPiece functionality
61 EXPECT_EQ(s.size(), 3);
63 EXPECT_EQ(s.start(), foo); // ptr comparison
64 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
65 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
67 EXPECT_EQ(s.toString(), foo); // lexical comparison
68 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
69 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
71 EXPECT_EQ(s, foo); // lexical comparison
72 EXPECT_EQ(s, fooStr); // lexical comparison
73 EXPECT_EQ(s, foo2Str); // lexical comparison
76 // check using StringPiece to reference substrings
77 const char* foobarbaz = "foobarbaz";
79 // the full "foobarbaz"
80 s.reset(foobarbaz, strlen(foobarbaz));
81 EXPECT_EQ(s.size(), 9);
82 EXPECT_EQ(s.start(), foobarbaz);
83 EXPECT_EQ(s, "foobarbaz");
86 s.assign(foobarbaz, foobarbaz + 3);
87 EXPECT_EQ(s.size(), 3);
88 EXPECT_EQ(s.start(), foobarbaz);
92 s.reset(foobarbaz, strlen(foobarbaz));
93 EXPECT_EQ(s.find("bar"), 3);
94 EXPECT_EQ(s.find("ba", 3), 3);
95 EXPECT_EQ(s.find("ba", 4), 6);
96 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
97 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
98 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
99 // starting pos that is obviously past the end -- This works for std::string
100 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
101 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
102 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
104 EXPECT_EQ(s.find(""), std::string().find(""));
105 EXPECT_EQ(s.find(""), 0);
108 EXPECT_EQ(s.find('b'), 3);
109 EXPECT_EQ(s.find('b', 3), 3);
110 EXPECT_EQ(s.find('b', 4), 6);
111 EXPECT_EQ(s.find('o', 2), 2);
112 EXPECT_EQ(s.find('y'), StringPiece::npos);
113 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
114 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
115 EXPECT_TRUE(s.contains('z'));
116 // starting pos that is obviously past the end -- This works for std::string
117 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
118 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
119 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
121 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
122 EXPECT_EQ(s.find('\0'), StringPiece::npos);
123 EXPECT_FALSE(s.contains('\0'));
125 // single char rfinds
126 EXPECT_EQ(s.rfind('b'), 6);
127 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
128 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
129 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
130 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
132 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
133 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
136 s.reset(foobarbaz, strlen(foobarbaz));
137 EXPECT_EQ(s.find_first_of("bar"), 3);
138 EXPECT_EQ(s.find_first_of("ba", 3), 3);
139 EXPECT_EQ(s.find_first_of("ba", 4), 4);
140 EXPECT_TRUE(s.contains("bar"));
141 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
142 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
143 EXPECT_FALSE(s.contains("xyxy"));
144 // starting position too far
145 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
146 // starting pos that is obviously past the end -- This works for std::string
147 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
148 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
149 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
150 // empty needle. Note that this returns npos, while find() returns 0!
151 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
152 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
154 // single char find_first_ofs
155 EXPECT_EQ(s.find_first_of('b'), 3);
156 EXPECT_EQ(s.find_first_of('b', 3), 3);
157 EXPECT_EQ(s.find_first_of('b', 4), 6);
158 EXPECT_EQ(s.find_first_of('o', 2), 2);
159 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
160 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
161 // starting position too far
162 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
163 // starting pos that is obviously past the end -- This works for std::string
164 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
165 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
166 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
168 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
169 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
172 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
173 EXPECT_EQ(s.size(), 6);
174 EXPECT_EQ(s.start(), foobarbaz + 3);
175 EXPECT_EQ(s, "barbaz");
178 s.reset(foobarbaz + 3, 3);
179 EXPECT_EQ(s.size(), 3);
184 EXPECT_EQ(s.toString(), "");
186 // test an empty StringPiece
188 EXPECT_EQ(s2.size(), 0);
190 // Test comparison operators
224 void expectLT(const T& a, const T& b) {
227 EXPECT_FALSE(a == b);
228 EXPECT_FALSE(a >= b);
232 EXPECT_FALSE(b <= a);
238 void expectEQ(const T& a, const T& b) {
246 TEST(StringPiece, EightBitComparisons) {
247 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
248 constexpr size_t count = sizeof(values) / sizeof(values[0]);
249 for (size_t i = 0; i < count; ++i) {
250 std::string a(1, values[i]);
251 // Defeat copy-on-write
252 std::string aCopy(a.data(), a.size());
254 expectEQ(StringPiece(a), StringPiece(aCopy));
256 for (size_t j = i + 1; j < count; ++j) {
257 std::string b(1, values[j]);
259 expectLT(StringPiece(a), StringPiece(b));
264 TEST(StringPiece, ToByteRange) {
265 StringPiece a("hello");
267 EXPECT_EQ(static_cast<const void*>(a.begin()),
268 static_cast<const void*>(b.begin()));
269 EXPECT_EQ(static_cast<const void*>(a.end()),
270 static_cast<const void*>(b.end()));
272 // and convert back again
274 EXPECT_EQ(a.begin(), c.begin());
275 EXPECT_EQ(a.end(), c.end());
278 TEST(StringPiece, InvalidRange) {
279 StringPiece a("hello");
280 EXPECT_EQ(a, a.subpiece(0, 10));
281 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
282 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
283 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
284 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
285 EXPECT_THROW(a.subpiece(6), std::out_of_range);
287 std::string b("hello");
288 EXPECT_EQ(a, StringPiece(b, 0, 10));
289 EXPECT_EQ("ello", a.subpiece(1));
290 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
291 EXPECT_EQ("ell", a.subpiece(1, 3));
292 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
293 EXPECT_THROW(a.subpiece(6), std::out_of_range);
296 TEST(StringPiece, Constexpr) {
297 constexpr const char* helloArray = "hello";
299 constexpr StringPiece hello1("hello");
300 EXPECT_EQ("hello", hello1);
301 static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
303 constexpr StringPiece hello2(helloArray);
304 EXPECT_EQ("hello", hello2);
305 static_assert(hello2.size() == 5, "hello size should be 5 at compile time");
308 TEST(StringPiece, Prefix) {
309 StringPiece a("hello");
310 EXPECT_TRUE(a.startsWith(""));
311 EXPECT_TRUE(a.startsWith("h"));
312 EXPECT_TRUE(a.startsWith('h'));
313 EXPECT_TRUE(a.startsWith("hello"));
314 EXPECT_FALSE(a.startsWith("hellox"));
315 EXPECT_FALSE(a.startsWith('x'));
316 EXPECT_FALSE(a.startsWith("x"));
318 EXPECT_TRUE(a.startsWith("", folly::AsciiCaseInsensitive()));
319 EXPECT_TRUE(a.startsWith("hello", folly::AsciiCaseInsensitive()));
320 EXPECT_TRUE(a.startsWith("hellO", folly::AsciiCaseInsensitive()));
321 EXPECT_TRUE(a.startsWith("HELL", folly::AsciiCaseInsensitive()));
322 EXPECT_TRUE(a.startsWith("H", folly::AsciiCaseInsensitive()));
323 EXPECT_FALSE(a.startsWith("HELLOX", folly::AsciiCaseInsensitive()));
324 EXPECT_FALSE(a.startsWith("x", folly::AsciiCaseInsensitive()));
325 EXPECT_FALSE(a.startsWith("X", folly::AsciiCaseInsensitive()));
329 EXPECT_TRUE(b.removePrefix(""));
330 EXPECT_EQ("hello", b);
334 EXPECT_TRUE(b.removePrefix("h"));
335 EXPECT_EQ("ello", b);
339 EXPECT_TRUE(b.removePrefix('h'));
340 EXPECT_EQ("ello", b);
344 EXPECT_TRUE(b.removePrefix("hello"));
349 EXPECT_FALSE(b.removePrefix("hellox"));
350 EXPECT_EQ("hello", b);
354 EXPECT_FALSE(b.removePrefix("x"));
355 EXPECT_EQ("hello", b);
359 EXPECT_FALSE(b.removePrefix('x'));
360 EXPECT_EQ("hello", b);
364 TEST(StringPiece, Suffix) {
365 StringPiece a("hello");
366 EXPECT_TRUE(a.endsWith(""));
367 EXPECT_TRUE(a.endsWith("o"));
368 EXPECT_TRUE(a.endsWith('o'));
369 EXPECT_TRUE(a.endsWith("hello"));
370 EXPECT_FALSE(a.endsWith("xhello"));
371 EXPECT_FALSE(a.endsWith("x"));
372 EXPECT_FALSE(a.endsWith('x'));
374 EXPECT_TRUE(a.endsWith("", folly::AsciiCaseInsensitive()));
375 EXPECT_TRUE(a.endsWith("o", folly::AsciiCaseInsensitive()));
376 EXPECT_TRUE(a.endsWith("O", folly::AsciiCaseInsensitive()));
377 EXPECT_TRUE(a.endsWith("hello", folly::AsciiCaseInsensitive()));
378 EXPECT_TRUE(a.endsWith("hellO", folly::AsciiCaseInsensitive()));
379 EXPECT_FALSE(a.endsWith("xhello", folly::AsciiCaseInsensitive()));
380 EXPECT_FALSE(a.endsWith("Xhello", folly::AsciiCaseInsensitive()));
381 EXPECT_FALSE(a.endsWith("x", folly::AsciiCaseInsensitive()));
382 EXPECT_FALSE(a.endsWith("X", folly::AsciiCaseInsensitive()));
386 EXPECT_TRUE(b.removeSuffix(""));
387 EXPECT_EQ("hello", b);
391 EXPECT_TRUE(b.removeSuffix("o"));
392 EXPECT_EQ("hell", b);
396 EXPECT_TRUE(b.removeSuffix('o'));
397 EXPECT_EQ("hell", b);
401 EXPECT_TRUE(b.removeSuffix("hello"));
406 EXPECT_FALSE(b.removeSuffix("xhello"));
407 EXPECT_EQ("hello", b);
411 EXPECT_FALSE(b.removeSuffix("x"));
412 EXPECT_EQ("hello", b);
416 EXPECT_FALSE(b.removeSuffix('x'));
417 EXPECT_EQ("hello", b);
421 TEST(StringPiece, PrefixEmpty) {
423 EXPECT_TRUE(a.startsWith(""));
424 EXPECT_FALSE(a.startsWith("a"));
425 EXPECT_FALSE(a.startsWith('a'));
426 EXPECT_TRUE(a.removePrefix(""));
428 EXPECT_FALSE(a.removePrefix("a"));
430 EXPECT_FALSE(a.removePrefix('a'));
434 TEST(StringPiece, SuffixEmpty) {
436 EXPECT_TRUE(a.endsWith(""));
437 EXPECT_FALSE(a.endsWith("a"));
438 EXPECT_FALSE(a.endsWith('a'));
439 EXPECT_TRUE(a.removeSuffix(""));
441 EXPECT_FALSE(a.removeSuffix("a"));
443 EXPECT_FALSE(a.removeSuffix('a'));
447 TEST(StringPiece, erase) {
448 StringPiece a("hello");
452 EXPECT_EQ("ello", a);
461 EXPECT_THROW(a.erase(b, e), std::out_of_range);
480 boost::algorithm::trim(a);
481 EXPECT_EQ(a, "hello");
484 TEST(StringPiece, split_step_char_delimiter) {
486 // 012345678901234567890123456
487 auto const s = "this is just a test string";
488 auto const e = std::next(s, std::strlen(s));
491 folly::StringPiece p(s);
492 EXPECT_EQ(s, p.begin());
493 EXPECT_EQ(e, p.end());
496 auto x = p.split_step(' ');
497 EXPECT_EQ(std::next(s, 5), p.begin());
498 EXPECT_EQ(e, p.end());
499 EXPECT_EQ("this", x);
501 x = p.split_step(' ');
502 EXPECT_EQ(std::next(s, 8), p.begin());
503 EXPECT_EQ(e, p.end());
506 x = p.split_step('u');
507 EXPECT_EQ(std::next(s, 10), p.begin());
508 EXPECT_EQ(e, p.end());
511 x = p.split_step(' ');
512 EXPECT_EQ(std::next(s, 13), p.begin());
513 EXPECT_EQ(e, p.end());
516 x = p.split_step(' ');
517 EXPECT_EQ(std::next(s, 14), p.begin());
518 EXPECT_EQ(e, p.end());
521 x = p.split_step(' ');
522 EXPECT_EQ(std::next(s, 16), p.begin());
523 EXPECT_EQ(e, p.end());
526 x = p.split_step(' ');
527 EXPECT_EQ(std::next(s, 21), p.begin());
528 EXPECT_EQ(e, p.end());
529 EXPECT_EQ("test", x);
531 x = p.split_step(' ');
532 EXPECT_EQ(e, p.begin());
533 EXPECT_EQ(e, p.end());
534 EXPECT_EQ("string", x);
536 x = p.split_step(' ');
537 EXPECT_EQ(e, p.begin());
538 EXPECT_EQ(e, p.end());
542 TEST(StringPiece, split_step_range_delimiter) {
544 // 0123456789012345678901234567890123
545 auto const s = "this is just a test string";
546 auto const e = std::next(s, std::strlen(s));
549 folly::StringPiece p(s);
550 EXPECT_EQ(s, p.begin());
551 EXPECT_EQ(e, p.end());
554 auto x = p.split_step(" ");
555 EXPECT_EQ(std::next(s, 6), p.begin());
556 EXPECT_EQ(e, p.end());
557 EXPECT_EQ("this", x);
559 x = p.split_step(" ");
560 EXPECT_EQ(std::next(s, 10), p.begin());
561 EXPECT_EQ(e, p.end());
564 x = p.split_step("u");
565 EXPECT_EQ(std::next(s, 12), p.begin());
566 EXPECT_EQ(e, p.end());
569 x = p.split_step(" ");
570 EXPECT_EQ(std::next(s, 16), p.begin());
571 EXPECT_EQ(e, p.end());
574 x = p.split_step(" ");
575 EXPECT_EQ(std::next(s, 18), p.begin());
576 EXPECT_EQ(e, p.end());
579 x = p.split_step(" ");
580 EXPECT_EQ(std::next(s, 21), p.begin());
581 EXPECT_EQ(e, p.end());
584 x = p.split_step(" ");
585 EXPECT_EQ(std::next(s, 28), p.begin());
586 EXPECT_EQ(e, p.end());
587 EXPECT_EQ(" test", x);
589 x = p.split_step(" ");
590 EXPECT_EQ(e, p.begin());
591 EXPECT_EQ(e, p.end());
592 EXPECT_EQ("string", x);
594 x = p.split_step(" ");
595 EXPECT_EQ(e, p.begin());
596 EXPECT_EQ(e, p.end());
599 x = p.split_step(" ");
600 EXPECT_EQ(e, p.begin());
601 EXPECT_EQ(e, p.end());
605 void split_step_with_process_noop(folly::StringPiece) {}
607 TEST(StringPiece, split_step_with_process_char_delimiter) {
609 // 012345678901234567890123456
610 auto const s = "this is just a test string";
611 auto const e = std::next(s, std::strlen(s));
614 folly::StringPiece p(s);
615 EXPECT_EQ(s, p.begin());
616 EXPECT_EQ(e, p.end());
619 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
620 EXPECT_EQ(std::next(s, 5), p.begin());
621 EXPECT_EQ(e, p.end());
622 EXPECT_EQ("this", x);
626 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
627 EXPECT_EQ(std::next(s, 8), p.begin());
628 EXPECT_EQ(e, p.end());
633 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
634 EXPECT_EQ(std::next(s, 10), p.begin());
635 EXPECT_EQ(e, p.end());
640 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
641 EXPECT_EQ(std::next(s, 13), p.begin());
642 EXPECT_EQ(e, p.end());
647 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
648 EXPECT_EQ(std::next(s, 14), p.begin());
649 EXPECT_EQ(e, p.end());
654 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
655 EXPECT_EQ(std::next(s, 16), p.begin());
656 EXPECT_EQ(e, p.end());
661 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
662 EXPECT_EQ(std::next(s, 21), p.begin());
663 EXPECT_EQ(e, p.end());
664 EXPECT_EQ("test", x);
668 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
669 EXPECT_EQ(e, p.begin());
670 EXPECT_EQ(e, p.end());
671 EXPECT_EQ("string", x);
675 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
676 EXPECT_EQ(e, p.begin());
677 EXPECT_EQ(e, p.end());
682 EXPECT_TRUE((std::is_same<
684 decltype(p.split_step(' ', split_step_with_process_noop))
687 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
690 TEST(StringPiece, split_step_with_process_range_delimiter) {
692 // 0123456789012345678901234567890123
693 auto const s = "this is just a test string";
694 auto const e = std::next(s, std::strlen(s));
697 folly::StringPiece p(s);
698 EXPECT_EQ(s, p.begin());
699 EXPECT_EQ(e, p.end());
702 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
703 EXPECT_EQ(std::next(s, 6), p.begin());
704 EXPECT_EQ(e, p.end());
705 EXPECT_EQ("this", x);
709 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
710 EXPECT_EQ(std::next(s, 10), p.begin());
711 EXPECT_EQ(e, p.end());
716 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
717 EXPECT_EQ(std::next(s, 12), p.begin());
718 EXPECT_EQ(e, p.end());
723 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
724 EXPECT_EQ(std::next(s, 16), p.begin());
725 EXPECT_EQ(e, p.end());
730 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
731 EXPECT_EQ(std::next(s, 18), p.begin());
732 EXPECT_EQ(e, p.end());
737 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
738 EXPECT_EQ(std::next(s, 21), p.begin());
739 EXPECT_EQ(e, p.end());
744 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
745 EXPECT_EQ(std::next(s, 28), p.begin());
746 EXPECT_EQ(e, p.end());
747 EXPECT_EQ(" test", x);
751 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
752 EXPECT_EQ(e, p.begin());
753 EXPECT_EQ(e, p.end());
754 EXPECT_EQ("string", x);
758 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
759 EXPECT_EQ(e, p.begin());
760 EXPECT_EQ(e, p.end());
765 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
766 EXPECT_EQ(e, p.begin());
767 EXPECT_EQ(e, p.end());
772 EXPECT_TRUE((std::is_same<
774 decltype(p.split_step(' ', split_step_with_process_noop))
777 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
780 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
782 // 012345678901234567890123456
783 auto const s = "this is just a test string";
784 auto const e = std::next(s, std::strlen(s));
785 auto const delimiter = ' ';
788 folly::StringPiece p(s);
789 EXPECT_EQ(s, p.begin());
790 EXPECT_EQ(e, p.end());
793 auto const functor = [](
794 folly::StringPiece s,
795 folly::StringPiece expected
797 EXPECT_EQ(expected, s);
801 auto const checker = [&](folly::StringPiece expected) {
802 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
815 EXPECT_TRUE(p.empty());
818 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
820 // 0123456789012345678901234567890123
821 auto const s = "this is just a test string";
822 auto const e = std::next(s, std::strlen(s));
823 auto const delimiter = " ";
826 folly::StringPiece p(s);
827 EXPECT_EQ(s, p.begin());
828 EXPECT_EQ(e, p.end());
831 auto const functor = [](
832 folly::StringPiece s,
833 folly::StringPiece expected
835 EXPECT_EQ(expected, s);
839 auto const checker = [&](folly::StringPiece expected) {
840 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
853 EXPECT_TRUE(p.empty());
856 TEST(StringPiece, NoInvalidImplicitConversions) {
858 bool operator()(folly::Range<int*>) { return false; }
859 bool operator()(folly::StringPiece) { return true; }
862 std::string s = "hello";
863 EXPECT_TRUE(IsString()(s));
866 TEST(qfind, UInt32_Ranges) {
867 vector<uint32_t> a({1, 2, 3, 260, 5});
868 vector<uint32_t> b({2, 3, 4});
870 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
871 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
873 EXPECT_EQ(qfind(a_range, b_range), string::npos);
876 EXPECT_EQ(qfind(a_range, b_range), 1);
879 template <typename NeedleFinder>
880 class NeedleFinderTest : public ::testing::Test {
882 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
883 return NeedleFinder::find_first_byte_of(haystack, needles);
887 struct SseNeedleFinder {
888 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
889 // This will only use the SSE version if it is supported on this CPU
890 // (selected using ifunc).
891 return detail::qfind_first_byte_of(haystack, needles);
895 struct NoSseNeedleFinder {
896 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
897 return detail::qfind_first_byte_of_nosse(haystack, needles);
901 struct ByteSetNeedleFinder {
902 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
903 return detail::qfind_first_byte_of_byteset(haystack, needles);
907 typedef ::testing::Types<SseNeedleFinder,
909 ByteSetNeedleFinder> NeedleFinders;
910 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
912 TYPED_TEST(NeedleFinderTest, Null) {
913 { // null characters in the string
914 string s(10, char(0));
916 string delims("abc");
917 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
919 { // null characters in delim
921 string delims(10, char(0));
924 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
926 { // range not terminated by null character
927 string buf = "abcdefghijklmnopqrstuvwxyz";
928 StringPiece s(buf.data() + 5, 3);
929 StringPiece delims("z");
930 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
934 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
935 string delims(1000, 'b');
936 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
937 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
940 TYPED_TEST(NeedleFinderTest, Empty) {
943 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
944 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
945 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
948 TYPED_TEST(NeedleFinderTest, Unaligned) {
949 // works correctly even if input buffers are not 16-byte aligned
950 string s = "0123456789ABCDEFGH";
951 for (size_t i = 0; i < s.size(); ++i) {
952 StringPiece a(s.c_str() + i);
953 for (size_t j = 0; j < s.size(); ++j) {
954 StringPiece b(s.c_str() + j);
955 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
960 // for some algorithms (specifically those that create a set of needles),
961 // we check for the edge-case of _all_ possible needles being sought.
962 TYPED_TEST(NeedleFinderTest, Needles256) {
964 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
965 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
966 // make the size ~big to avoid any edge-case branches for tiny haystacks
967 const int haystackSize = 50;
968 for (int i = minValue; i <= maxValue; i++) { // <=
969 needles.push_back(i);
971 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
972 for (int i = minValue; i <= maxValue; i++) {
973 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
976 needles.append("these are redundant characters");
977 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
978 for (int i = minValue; i <= maxValue; i++) {
979 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
983 TYPED_TEST(NeedleFinderTest, Base) {
984 for (size_t i = 0; i < 32; ++i) {
985 for (int j = 0; j < 32; ++j) {
986 string s = string(i, 'X') + "abca" + string(i, 'X');
987 string delims = string(j, 'Y') + "a" + string(j, 'Y');
988 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
993 const size_t kPageSize = 4096;
994 // Updates contents so that any read accesses past the last byte will
995 // cause a SIGSEGV. It accomplishes this by changing access to the page that
996 // begins immediately after the end of the contents (as allocators and mmap()
997 // all operate on page boundaries, this is a reasonable assumption).
998 // This function will also initialize buf, which caller must free().
999 void createProtectedBuf(StringPiece& contents, char** buf) {
1000 ASSERT_LE(contents.size(), kPageSize);
1001 const size_t kSuccess = 0;
1002 char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
1003 if (pageAlignedBuf == nullptr) {
1006 // Protect the page after the first full page-aligned region of the
1008 mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
1009 size_t newBegin = kPageSize - contents.size();
1010 memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
1011 contents.reset(pageAlignedBuf + newBegin, contents.size());
1012 *buf = pageAlignedBuf;
1015 void freeProtectedBuf(char* buf) {
1016 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
1020 TYPED_TEST(NeedleFinderTest, NoSegFault) {
1021 const string base = string(32, 'a') + string("b");
1022 const string delims = string(32, 'c') + string("b");
1023 for (int i = 0; i <= 32; i++) {
1024 for (int j = 0; j <= 33; j++) {
1025 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1026 StringPiece s1(base);
1028 ASSERT_TRUE(!s1.empty());
1032 StringPiece s2(delims);
1036 createProtectedBuf(s1, &buf1);
1037 createProtectedBuf(s2, &buf2);
1038 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
1039 // string(s1.data(), s1.size()).c_str(), s1.size(),
1040 // string(s2.data(), s2.size()).c_str(), s2.size());
1041 auto r1 = this->find_first_byte_of(s1, s2);
1042 auto f1 = std::find_first_of(s1.begin(), s1.end(),
1043 s2.begin(), s2.end());
1044 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
1046 auto r2 = this->find_first_byte_of(s2, s1);
1047 auto f2 = std::find_first_of(s2.begin(), s2.end(),
1048 s1.begin(), s1.end());
1049 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1051 freeProtectedBuf(buf1);
1052 freeProtectedBuf(buf2);
1058 TEST(NonConstTest, StringPiece) {
1059 std::string hello("hello");
1060 MutableStringPiece sp(&hello.front(), hello.size());
1062 EXPECT_EQ("xello", hello);
1065 EXPECT_EQ("xello", s);
1069 MutableByteRange r2(sp);
1073 // Similar to the begin() template functions, but instread of returing
1074 // an iterator, return a pointer to data.
1075 template <class Container>
1076 typename Container::value_type* dataPtr(Container& cont) {
1077 // NOTE: &cont[0] is undefined if cont is empty (it creates a
1078 // reference to nullptr - which is not dereferenced, but still UBSAN).
1081 template <class T, size_t N>
1082 constexpr T* dataPtr(T (&arr)[N]) noexcept {
1087 void testRangeFunc(C&& x, size_t n) {
1089 // type, conversion checks
1090 Range<int*> r1 = range(std::forward<C>(x));
1091 Range<const int*> r2 = range(std::forward<C>(x));
1092 Range<const int*> r3 = range(cx);
1093 Range<const int*> r5 = range(std::move(cx));
1094 EXPECT_EQ(r1.begin(), dataPtr(x));
1095 EXPECT_EQ(r1.end(), dataPtr(x) + n);
1096 EXPECT_EQ(n, r1.size());
1097 EXPECT_EQ(n, r2.size());
1098 EXPECT_EQ(n, r3.size());
1099 EXPECT_EQ(n, r5.size());
1102 TEST(RangeFunc, Vector) {
1104 testRangeFunc(x, 0);
1106 testRangeFunc(x, 1);
1107 testRangeFunc(std::vector<int>{1, 2}, 2);
1110 TEST(RangeFunc, Array) {
1111 std::array<int, 3> x;
1112 testRangeFunc(x, 3);
1115 TEST(RangeFunc, CArray) {
1116 int x[] {1, 2, 3, 4};
1117 testRangeFunc(x, 4);
1120 TEST(RangeFunc, ConstexprCArray) {
1121 static constexpr const int numArray[4] = {3, 17, 1, 9};
1122 constexpr const auto numArrayRange = range(numArray);
1123 EXPECT_EQ(17, numArrayRange[1]);
1124 constexpr const auto numArrayRangeSize = numArrayRange.size();
1125 EXPECT_EQ(4, numArrayRangeSize);
1128 TEST(RangeFunc, ConstexprStdArray) {
1129 static constexpr const std::array<int, 4> numArray = {{3, 17, 1, 9}};
1130 constexpr const auto numArrayRange = range(numArray);
1131 EXPECT_EQ(17, numArrayRange[1]);
1132 constexpr const auto numArrayRangeSize = numArrayRange.size();
1133 EXPECT_EQ(4, numArrayRangeSize);
1136 TEST(RangeFunc, ConstexprStdArrayZero) {
1137 static constexpr const std::array<int, 0> numArray = {};
1138 constexpr const auto numArrayRange = range(numArray);
1139 constexpr const auto numArrayRangeSize = numArrayRange.size();
1140 EXPECT_EQ(0, numArrayRangeSize);
1143 TEST(RangeFunc, ConstexprIteratorPair) {
1144 static constexpr const int numArray[4] = {3, 17, 1, 9};
1145 constexpr const auto numPtr = static_cast<const int*>(numArray);
1146 constexpr const auto numIterRange = range(numPtr + 1, numPtr + 3);
1147 EXPECT_EQ(1, numIterRange[1]);
1148 constexpr const auto numIterRangeSize = numIterRange.size();
1149 EXPECT_EQ(2, numIterRangeSize);
1152 TEST(RangeFunc, ConstexprCollection) {
1153 class IntCollection {
1155 constexpr IntCollection(const int* d, size_t s) : data_(d), size_(s) {}
1156 constexpr const int* data() const {
1159 constexpr size_t size() const {
1167 static constexpr const int numArray[4] = {3, 17, 1, 9};
1168 constexpr const auto numPtr = static_cast<const int*>(numArray);
1169 constexpr const auto numColl = IntCollection(numPtr + 1, 2);
1170 constexpr const auto numCollRange = range(numColl);
1171 EXPECT_EQ(1, numCollRange[1]);
1172 constexpr const auto numCollRangeSize = numCollRange.size();
1173 EXPECT_EQ(2, numCollRangeSize);
1176 std::string get_rand_str(size_t size,
1177 std::uniform_int_distribution<>& dist,
1178 std::mt19937& gen) {
1179 std::string ret(size, '\0');
1180 for (size_t i = 0; i < size; ++i) {
1181 ret[i] = static_cast<char>(dist(gen));
1188 bool operator==(MutableStringPiece mp, StringPiece sp) {
1189 return mp.compare(sp) == 0;
1192 bool operator==(StringPiece sp, MutableStringPiece mp) {
1193 return mp.compare(sp) == 0;
1197 TEST(ReplaceAt, exhaustiveTest) {
1198 char input[] = "this is nice and long input";
1199 auto msp = MutableStringPiece(input);
1200 auto str = std::string(input);
1201 std::random_device rd;
1202 std::mt19937 gen(rd());
1203 std::uniform_int_distribution<> dist('a', 'z');
1205 for (int i=0; i < 100; ++i) {
1206 for (size_t j = 1; j <= msp.size(); ++j) {
1207 auto replacement = get_rand_str(j, dist, gen);
1208 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1209 msp.replaceAt(pos, replacement);
1210 str.replace(pos, replacement.size(), replacement);
1211 EXPECT_EQ(msp.compare(str), 0);
1217 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1220 TEST(ReplaceAll, basicTest) {
1221 char input[] = "this is nice and long input";
1222 auto orig = std::string(input);
1223 auto msp = MutableStringPiece(input);
1225 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1226 EXPECT_EQ("thsi si nice and long input", msp);
1227 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1228 EXPECT_EQ(msp, orig);
1230 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1231 EXPECT_EQ(msp, orig);
1233 // at the very beginning
1234 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1235 EXPECT_EQ("siht is nice and long input", msp);
1236 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1237 EXPECT_EQ(msp, orig);
1240 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1241 EXPECT_EQ("this is nice and long soput", msp);
1242 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1243 EXPECT_EQ(msp, orig);
1246 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1247 EXPECT_EQ("this@is@nice@and@long@input", msp);
1248 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1249 EXPECT_EQ(msp, orig);
1252 TEST(ReplaceAll, randomTest) {
1253 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1254 auto orig = std::string(input);
1255 auto msp = MutableStringPiece(input);
1257 std::random_device rd;
1258 std::mt19937 gen(rd());
1259 std::uniform_int_distribution<> dist('A', 'Z');
1261 for (int i=0; i < 100; ++i) {
1262 for (size_t j = 1; j <= orig.size(); ++j) {
1263 auto replacement = get_rand_str(j, dist, gen);
1264 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1265 auto piece = orig.substr(pos, j);
1266 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1267 EXPECT_EQ(msp.find(replacement), pos);
1268 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1269 EXPECT_EQ(msp, orig);
1275 TEST(ReplaceAll, BadArg) {
1277 auto fst = "longer";
1279 char input[] = "meh meh meh";
1280 auto all = MutableStringPiece(input);
1283 all.replaceAll(fst, snd);
1284 } catch (std::invalid_argument&) {
1289 all.replaceAll(snd, fst);
1290 } catch (std::invalid_argument&) {
1294 EXPECT_EQ(count, 2);
1297 TEST(Range, Constructors) {
1298 vector<int> c = {1, 2, 3};
1299 typedef Range<vector<int>::iterator> RangeType;
1300 typedef Range<vector<int>::const_iterator> ConstRangeType;
1301 RangeType cr(c.begin(), c.end());
1302 auto subpiece1 = ConstRangeType(cr, 1, 5);
1303 auto subpiece2 = ConstRangeType(cr, 1);
1304 EXPECT_EQ(subpiece1.size(), 2);
1305 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1306 EXPECT_EQ(subpiece1.end(), subpiece2.end());
1309 TEST(Range, ArrayConstructors) {
1310 auto charArray = std::array<char, 4>{{'t', 'e', 's', 't'}};
1311 auto constCharArray = std::array<char, 6>{{'f', 'o', 'o', 'b', 'a', 'r'}};
1312 auto emptyArray = std::array<char, 0>{};
1314 auto sp1 = StringPiece{charArray};
1315 EXPECT_EQ(4, sp1.size());
1316 EXPECT_EQ(charArray.data(), sp1.data());
1318 auto sp2 = StringPiece(constCharArray);
1319 EXPECT_EQ(6, sp2.size());
1320 EXPECT_EQ(constCharArray.data(), sp2.data());
1322 auto msp = MutableStringPiece(charArray);
1323 EXPECT_EQ(4, msp.size());
1324 EXPECT_EQ(charArray.data(), msp.data());
1326 auto esp = StringPiece(emptyArray);
1327 EXPECT_EQ(0, esp.size());
1328 EXPECT_EQ(nullptr, esp.data());
1330 auto emsp = MutableStringPiece(emptyArray);
1331 EXPECT_EQ(0, emsp.size());
1332 EXPECT_EQ(nullptr, emsp.data());
1334 static constexpr std::array<int, 4> numArray = {{3, 17, 1, 9}};
1335 constexpr auto numRange = Range<const int*>{numArray};
1336 EXPECT_EQ(17, numRange[1]);
1338 static constexpr std::array<int, 0> emptyNumArray{};
1339 constexpr auto emptyNumRange = Range<const int*>{emptyNumArray};
1340 EXPECT_EQ(0, emptyNumRange.size());
1343 TEST(Range, ConstexprAccessors) {
1344 constexpr StringPiece piece = range("hello");
1345 static_assert(piece.size() == 6u, "");
1346 static_assert(piece.end() - piece.begin() == 6u, "");
1347 static_assert(piece.data() == piece.begin(), "");
1348 static_assert(piece.start() == piece.begin(), "");
1349 static_assert(piece.cbegin() == piece.begin(), "");
1350 static_assert(piece.cend() == piece.end(), "");
1351 static_assert(*piece.begin() == 'h', "");
1352 static_assert(*(piece.end() - 1) == '\0', "");