2 * Copyright 2016 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/Memory.h>
30 #include <type_traits>
32 #include <boost/range/concepts.hpp>
33 #include <boost/algorithm/string/trim.hpp>
34 #include <gtest/gtest.h>
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 constexpr char helloArray[] = "hello";
298 TEST(StringPiece, Constexpr) {
299 constexpr StringPiece hello1("hello");
300 EXPECT_EQ("hello", hello1);
302 constexpr StringPiece hello2(helloArray);
303 EXPECT_EQ("hello", hello2);
306 TEST(StringPiece, Prefix) {
307 StringPiece a("hello");
308 EXPECT_TRUE(a.startsWith(""));
309 EXPECT_TRUE(a.startsWith("h"));
310 EXPECT_TRUE(a.startsWith('h'));
311 EXPECT_TRUE(a.startsWith("hello"));
312 EXPECT_FALSE(a.startsWith("hellox"));
313 EXPECT_FALSE(a.startsWith('x'));
314 EXPECT_FALSE(a.startsWith("x"));
318 EXPECT_TRUE(b.removePrefix(""));
319 EXPECT_EQ("hello", b);
323 EXPECT_TRUE(b.removePrefix("h"));
324 EXPECT_EQ("ello", b);
328 EXPECT_TRUE(b.removePrefix('h'));
329 EXPECT_EQ("ello", b);
333 EXPECT_TRUE(b.removePrefix("hello"));
338 EXPECT_FALSE(b.removePrefix("hellox"));
339 EXPECT_EQ("hello", b);
343 EXPECT_FALSE(b.removePrefix("x"));
344 EXPECT_EQ("hello", b);
348 EXPECT_FALSE(b.removePrefix('x'));
349 EXPECT_EQ("hello", b);
353 TEST(StringPiece, Suffix) {
354 StringPiece a("hello");
355 EXPECT_TRUE(a.endsWith(""));
356 EXPECT_TRUE(a.endsWith("o"));
357 EXPECT_TRUE(a.endsWith('o'));
358 EXPECT_TRUE(a.endsWith("hello"));
359 EXPECT_FALSE(a.endsWith("xhello"));
360 EXPECT_FALSE(a.endsWith("x"));
361 EXPECT_FALSE(a.endsWith('x'));
365 EXPECT_TRUE(b.removeSuffix(""));
366 EXPECT_EQ("hello", b);
370 EXPECT_TRUE(b.removeSuffix("o"));
371 EXPECT_EQ("hell", b);
375 EXPECT_TRUE(b.removeSuffix('o'));
376 EXPECT_EQ("hell", b);
380 EXPECT_TRUE(b.removeSuffix("hello"));
385 EXPECT_FALSE(b.removeSuffix("xhello"));
386 EXPECT_EQ("hello", b);
390 EXPECT_FALSE(b.removeSuffix("x"));
391 EXPECT_EQ("hello", b);
395 EXPECT_FALSE(b.removeSuffix('x'));
396 EXPECT_EQ("hello", b);
400 TEST(StringPiece, PrefixEmpty) {
402 EXPECT_TRUE(a.startsWith(""));
403 EXPECT_FALSE(a.startsWith("a"));
404 EXPECT_FALSE(a.startsWith('a'));
405 EXPECT_TRUE(a.removePrefix(""));
407 EXPECT_FALSE(a.removePrefix("a"));
409 EXPECT_FALSE(a.removePrefix('a'));
413 TEST(StringPiece, SuffixEmpty) {
415 EXPECT_TRUE(a.endsWith(""));
416 EXPECT_FALSE(a.endsWith("a"));
417 EXPECT_FALSE(a.endsWith('a'));
418 EXPECT_TRUE(a.removeSuffix(""));
420 EXPECT_FALSE(a.removeSuffix("a"));
422 EXPECT_FALSE(a.removeSuffix('a'));
426 TEST(StringPiece, erase) {
427 StringPiece a("hello");
431 EXPECT_EQ("ello", a);
440 EXPECT_THROW(a.erase(b, e), std::out_of_range);
459 boost::algorithm::trim(a);
460 EXPECT_EQ(a, "hello");
463 TEST(StringPiece, split_step_char_delimiter) {
465 // 012345678901234567890123456
466 auto const s = "this is just a test string";
467 auto const e = std::next(s, std::strlen(s));
470 folly::StringPiece p(s);
471 EXPECT_EQ(s, p.begin());
472 EXPECT_EQ(e, p.end());
475 auto x = p.split_step(' ');
476 EXPECT_EQ(std::next(s, 5), p.begin());
477 EXPECT_EQ(e, p.end());
478 EXPECT_EQ("this", x);
480 x = p.split_step(' ');
481 EXPECT_EQ(std::next(s, 8), p.begin());
482 EXPECT_EQ(e, p.end());
485 x = p.split_step('u');
486 EXPECT_EQ(std::next(s, 10), p.begin());
487 EXPECT_EQ(e, p.end());
490 x = p.split_step(' ');
491 EXPECT_EQ(std::next(s, 13), p.begin());
492 EXPECT_EQ(e, p.end());
495 x = p.split_step(' ');
496 EXPECT_EQ(std::next(s, 14), p.begin());
497 EXPECT_EQ(e, p.end());
500 x = p.split_step(' ');
501 EXPECT_EQ(std::next(s, 16), p.begin());
502 EXPECT_EQ(e, p.end());
505 x = p.split_step(' ');
506 EXPECT_EQ(std::next(s, 21), p.begin());
507 EXPECT_EQ(e, p.end());
508 EXPECT_EQ("test", x);
510 x = p.split_step(' ');
511 EXPECT_EQ(e, p.begin());
512 EXPECT_EQ(e, p.end());
513 EXPECT_EQ("string", x);
515 x = p.split_step(' ');
516 EXPECT_EQ(e, p.begin());
517 EXPECT_EQ(e, p.end());
521 TEST(StringPiece, split_step_range_delimiter) {
523 // 0123456789012345678901234567890123
524 auto const s = "this is just a test string";
525 auto const e = std::next(s, std::strlen(s));
528 folly::StringPiece p(s);
529 EXPECT_EQ(s, p.begin());
530 EXPECT_EQ(e, p.end());
533 auto x = p.split_step(" ");
534 EXPECT_EQ(std::next(s, 6), p.begin());
535 EXPECT_EQ(e, p.end());
536 EXPECT_EQ("this", x);
538 x = p.split_step(" ");
539 EXPECT_EQ(std::next(s, 10), p.begin());
540 EXPECT_EQ(e, p.end());
543 x = p.split_step("u");
544 EXPECT_EQ(std::next(s, 12), p.begin());
545 EXPECT_EQ(e, p.end());
548 x = p.split_step(" ");
549 EXPECT_EQ(std::next(s, 16), p.begin());
550 EXPECT_EQ(e, p.end());
553 x = p.split_step(" ");
554 EXPECT_EQ(std::next(s, 18), p.begin());
555 EXPECT_EQ(e, p.end());
558 x = p.split_step(" ");
559 EXPECT_EQ(std::next(s, 21), p.begin());
560 EXPECT_EQ(e, p.end());
563 x = p.split_step(" ");
564 EXPECT_EQ(std::next(s, 28), p.begin());
565 EXPECT_EQ(e, p.end());
566 EXPECT_EQ(" test", x);
568 x = p.split_step(" ");
569 EXPECT_EQ(e, p.begin());
570 EXPECT_EQ(e, p.end());
571 EXPECT_EQ("string", x);
573 x = p.split_step(" ");
574 EXPECT_EQ(e, p.begin());
575 EXPECT_EQ(e, p.end());
578 x = p.split_step(" ");
579 EXPECT_EQ(e, p.begin());
580 EXPECT_EQ(e, p.end());
584 void split_step_with_process_noop(folly::StringPiece) {}
586 TEST(StringPiece, split_step_with_process_char_delimiter) {
588 // 012345678901234567890123456
589 auto const s = "this is just a test string";
590 auto const e = std::next(s, std::strlen(s));
593 folly::StringPiece p(s);
594 EXPECT_EQ(s, p.begin());
595 EXPECT_EQ(e, p.end());
598 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
599 EXPECT_EQ(std::next(s, 5), p.begin());
600 EXPECT_EQ(e, p.end());
601 EXPECT_EQ("this", x);
605 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
606 EXPECT_EQ(std::next(s, 8), p.begin());
607 EXPECT_EQ(e, p.end());
612 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
613 EXPECT_EQ(std::next(s, 10), p.begin());
614 EXPECT_EQ(e, p.end());
619 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
620 EXPECT_EQ(std::next(s, 13), p.begin());
621 EXPECT_EQ(e, p.end());
626 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
627 EXPECT_EQ(std::next(s, 14), p.begin());
628 EXPECT_EQ(e, p.end());
633 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
634 EXPECT_EQ(std::next(s, 16), p.begin());
635 EXPECT_EQ(e, p.end());
640 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
641 EXPECT_EQ(std::next(s, 21), p.begin());
642 EXPECT_EQ(e, p.end());
643 EXPECT_EQ("test", x);
647 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
648 EXPECT_EQ(e, p.begin());
649 EXPECT_EQ(e, p.end());
650 EXPECT_EQ("string", x);
654 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
655 EXPECT_EQ(e, p.begin());
656 EXPECT_EQ(e, p.end());
661 EXPECT_TRUE((std::is_same<
663 decltype(p.split_step(' ', split_step_with_process_noop))
666 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
669 TEST(StringPiece, split_step_with_process_range_delimiter) {
671 // 0123456789012345678901234567890123
672 auto const s = "this is just a test string";
673 auto const e = std::next(s, std::strlen(s));
676 folly::StringPiece p(s);
677 EXPECT_EQ(s, p.begin());
678 EXPECT_EQ(e, p.end());
681 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
682 EXPECT_EQ(std::next(s, 6), p.begin());
683 EXPECT_EQ(e, p.end());
684 EXPECT_EQ("this", x);
688 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
689 EXPECT_EQ(std::next(s, 10), p.begin());
690 EXPECT_EQ(e, p.end());
695 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
696 EXPECT_EQ(std::next(s, 12), p.begin());
697 EXPECT_EQ(e, p.end());
702 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
703 EXPECT_EQ(std::next(s, 16), p.begin());
704 EXPECT_EQ(e, p.end());
709 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
710 EXPECT_EQ(std::next(s, 18), p.begin());
711 EXPECT_EQ(e, p.end());
716 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
717 EXPECT_EQ(std::next(s, 21), p.begin());
718 EXPECT_EQ(e, p.end());
723 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
724 EXPECT_EQ(std::next(s, 28), p.begin());
725 EXPECT_EQ(e, p.end());
726 EXPECT_EQ(" test", x);
730 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
731 EXPECT_EQ(e, p.begin());
732 EXPECT_EQ(e, p.end());
733 EXPECT_EQ("string", x);
737 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
738 EXPECT_EQ(e, p.begin());
739 EXPECT_EQ(e, p.end());
744 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
745 EXPECT_EQ(e, p.begin());
746 EXPECT_EQ(e, p.end());
751 EXPECT_TRUE((std::is_same<
753 decltype(p.split_step(' ', split_step_with_process_noop))
756 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
759 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
761 // 012345678901234567890123456
762 auto const s = "this is just a test string";
763 auto const e = std::next(s, std::strlen(s));
764 auto const delimiter = ' ';
767 folly::StringPiece p(s);
768 EXPECT_EQ(s, p.begin());
769 EXPECT_EQ(e, p.end());
772 auto const functor = [](
773 folly::StringPiece s,
774 folly::StringPiece expected
776 EXPECT_EQ(expected, s);
780 auto const checker = [&](folly::StringPiece expected) {
781 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
794 EXPECT_TRUE(p.empty());
797 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
799 // 0123456789012345678901234567890123
800 auto const s = "this is just a test string";
801 auto const e = std::next(s, std::strlen(s));
802 auto const delimiter = " ";
805 folly::StringPiece p(s);
806 EXPECT_EQ(s, p.begin());
807 EXPECT_EQ(e, p.end());
810 auto const functor = [](
811 folly::StringPiece s,
812 folly::StringPiece expected
814 EXPECT_EQ(expected, s);
818 auto const checker = [&](folly::StringPiece expected) {
819 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
832 EXPECT_TRUE(p.empty());
835 TEST(StringPiece, NoInvalidImplicitConversions) {
837 bool operator()(folly::Range<int*>) { return false; }
838 bool operator()(folly::StringPiece) { return true; }
841 std::string s = "hello";
842 EXPECT_TRUE(IsString()(s));
845 TEST(qfind, UInt32_Ranges) {
846 vector<uint32_t> a({1, 2, 3, 260, 5});
847 vector<uint32_t> b({2, 3, 4});
849 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
850 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
852 EXPECT_EQ(qfind(a_range, b_range), string::npos);
855 EXPECT_EQ(qfind(a_range, b_range), 1);
858 template <typename NeedleFinder>
859 class NeedleFinderTest : public ::testing::Test {
861 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
862 return NeedleFinder::find_first_byte_of(haystack, needles);
866 struct SseNeedleFinder {
867 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
868 // This will only use the SSE version if it is supported on this CPU
869 // (selected using ifunc).
870 return detail::qfind_first_byte_of(haystack, needles);
874 struct NoSseNeedleFinder {
875 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
876 return detail::qfind_first_byte_of_nosse(haystack, needles);
880 struct ByteSetNeedleFinder {
881 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
882 return detail::qfind_first_byte_of_byteset(haystack, needles);
886 typedef ::testing::Types<SseNeedleFinder,
888 ByteSetNeedleFinder> NeedleFinders;
889 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
891 TYPED_TEST(NeedleFinderTest, Null) {
892 { // null characters in the string
893 string s(10, char(0));
895 string delims("abc");
896 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
898 { // null characters in delim
900 string delims(10, char(0));
903 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
905 { // range not terminated by null character
906 string buf = "abcdefghijklmnopqrstuvwxyz";
907 StringPiece s(buf.data() + 5, 3);
908 StringPiece delims("z");
909 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
913 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
914 string delims(1000, 'b');
915 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
916 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
919 TYPED_TEST(NeedleFinderTest, Empty) {
922 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
923 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
924 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
927 TYPED_TEST(NeedleFinderTest, Unaligned) {
928 // works correctly even if input buffers are not 16-byte aligned
929 string s = "0123456789ABCDEFGH";
930 for (size_t i = 0; i < s.size(); ++i) {
931 StringPiece a(s.c_str() + i);
932 for (size_t j = 0; j < s.size(); ++j) {
933 StringPiece b(s.c_str() + j);
934 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
939 // for some algorithms (specifically those that create a set of needles),
940 // we check for the edge-case of _all_ possible needles being sought.
941 TYPED_TEST(NeedleFinderTest, Needles256) {
943 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
944 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
945 // make the size ~big to avoid any edge-case branches for tiny haystacks
946 const int haystackSize = 50;
947 for (size_t i = minValue; i <= maxValue; i++) { // <=
948 needles.push_back(i);
950 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
951 for (size_t i = minValue; i <= maxValue; i++) {
952 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
955 needles.append("these are redundant characters");
956 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
957 for (size_t i = minValue; i <= maxValue; i++) {
958 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
962 TYPED_TEST(NeedleFinderTest, Base) {
963 for (size_t i = 0; i < 32; ++i) {
964 for (int j = 0; j < 32; ++j) {
965 string s = string(i, 'X') + "abca" + string(i, 'X');
966 string delims = string(j, 'Y') + "a" + string(j, 'Y');
967 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
972 const size_t kPageSize = 4096;
973 // Updates contents so that any read accesses past the last byte will
974 // cause a SIGSEGV. It accomplishes this by changing access to the page that
975 // begins immediately after the end of the contents (as allocators and mmap()
976 // all operate on page boundaries, this is a reasonable assumption).
977 // This function will also initialize buf, which caller must free().
978 void createProtectedBuf(StringPiece& contents, char** buf) {
979 ASSERT_LE(contents.size(), kPageSize);
980 const size_t kSuccess = 0;
981 char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
982 if (pageAlignedBuf == nullptr) {
985 // Protect the page after the first full page-aligned region of the
987 mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
988 size_t newBegin = kPageSize - contents.size();
989 memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
990 contents.reset(pageAlignedBuf + newBegin, contents.size());
991 *buf = pageAlignedBuf;
994 void freeProtectedBuf(char* buf) {
995 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
999 TYPED_TEST(NeedleFinderTest, NoSegFault) {
1000 const string base = string(32, 'a') + string("b");
1001 const string delims = string(32, 'c') + string("b");
1002 for (int i = 0; i <= 32; i++) {
1003 for (int j = 0; j <= 33; j++) {
1004 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1005 StringPiece s1(base);
1007 ASSERT_TRUE(!s1.empty());
1011 StringPiece s2(delims);
1015 createProtectedBuf(s1, &buf1);
1016 createProtectedBuf(s2, &buf2);
1017 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
1018 // string(s1.data(), s1.size()).c_str(), s1.size(),
1019 // string(s2.data(), s2.size()).c_str(), s2.size());
1020 auto r1 = this->find_first_byte_of(s1, s2);
1021 auto f1 = std::find_first_of(s1.begin(), s1.end(),
1022 s2.begin(), s2.end());
1023 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
1025 auto r2 = this->find_first_byte_of(s2, s1);
1026 auto f2 = std::find_first_of(s2.begin(), s2.end(),
1027 s1.begin(), s1.end());
1028 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1030 freeProtectedBuf(buf1);
1031 freeProtectedBuf(buf2);
1037 TEST(NonConstTest, StringPiece) {
1038 std::string hello("hello");
1039 MutableStringPiece sp(&hello.front(), hello.size());
1041 EXPECT_EQ("xello", hello);
1044 EXPECT_EQ("xello", s);
1048 MutableByteRange r2(sp);
1052 // Similar to the begin() template functions, but instread of returing
1053 // an iterator, return a pointer to data.
1054 template <class Container>
1055 typename Container::value_type* dataPtr(Container& cont) {
1056 // NOTE: &cont[0] is undefined if cont is empty (it creates a
1057 // reference to nullptr - which is not dereferenced, but still UBSAN).
1060 template <class T, size_t N>
1061 constexpr T* dataPtr(T (&arr)[N]) noexcept {
1066 void testRangeFunc(C&& x, size_t n) {
1068 // type, conversion checks
1069 Range<int*> r1 = range(std::forward<C>(x));
1070 Range<const int*> r2 = range(std::forward<C>(x));
1071 Range<const int*> r3 = range(cx);
1072 Range<const int*> r5 = range(std::move(cx));
1073 EXPECT_EQ(r1.begin(), dataPtr(x));
1074 EXPECT_EQ(r1.end(), dataPtr(x) + n);
1075 EXPECT_EQ(n, r1.size());
1076 EXPECT_EQ(n, r2.size());
1077 EXPECT_EQ(n, r3.size());
1078 EXPECT_EQ(n, r5.size());
1081 TEST(RangeFunc, Vector) {
1083 testRangeFunc(x, 0);
1085 testRangeFunc(x, 1);
1086 testRangeFunc(std::vector<int>{1, 2}, 2);
1089 TEST(RangeFunc, Array) {
1090 std::array<int, 3> x;
1091 testRangeFunc(x, 3);
1094 TEST(RangeFunc, CArray) {
1095 int x[] {1, 2, 3, 4};
1096 testRangeFunc(x, 4);
1099 std::string get_rand_str(size_t size,
1100 std::uniform_int_distribution<>& dist,
1101 std::mt19937& gen) {
1102 std::string ret(size, '\0');
1103 for (size_t i = 0; i < size; ++i) {
1104 ret[i] = static_cast<char>(dist(gen));
1111 bool operator==(MutableStringPiece mp, StringPiece sp) {
1112 return mp.compare(sp) == 0;
1115 bool operator==(StringPiece sp, MutableStringPiece mp) {
1116 return mp.compare(sp) == 0;
1120 TEST(ReplaceAt, exhaustiveTest) {
1121 char input[] = "this is nice and long input";
1122 auto msp = MutableStringPiece(input);
1123 auto str = std::string(input);
1124 std::random_device rd;
1125 std::mt19937 gen(rd());
1126 std::uniform_int_distribution<> dist('a', 'z');
1128 for (int i=0; i < 100; ++i) {
1129 for (size_t j = 1; j <= msp.size(); ++j) {
1130 auto replacement = get_rand_str(j, dist, gen);
1131 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1132 msp.replaceAt(pos, replacement);
1133 str.replace(pos, replacement.size(), replacement);
1134 EXPECT_EQ(msp.compare(str), 0);
1140 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1143 TEST(ReplaceAll, basicTest) {
1144 char input[] = "this is nice and long input";
1145 auto orig = std::string(input);
1146 auto msp = MutableStringPiece(input);
1148 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1149 EXPECT_EQ("thsi si nice and long input", msp);
1150 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1151 EXPECT_EQ(msp, orig);
1153 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1154 EXPECT_EQ(msp, orig);
1156 // at the very beginning
1157 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1158 EXPECT_EQ("siht is nice and long input", msp);
1159 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1160 EXPECT_EQ(msp, orig);
1163 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1164 EXPECT_EQ("this is nice and long soput", msp);
1165 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1166 EXPECT_EQ(msp, orig);
1169 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1170 EXPECT_EQ("this@is@nice@and@long@input", msp);
1171 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1172 EXPECT_EQ(msp, orig);
1175 TEST(ReplaceAll, randomTest) {
1176 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1177 auto orig = std::string(input);
1178 auto msp = MutableStringPiece(input);
1180 std::random_device rd;
1181 std::mt19937 gen(rd());
1182 std::uniform_int_distribution<> dist('A', 'Z');
1184 for (int i=0; i < 100; ++i) {
1185 for (size_t j = 1; j <= orig.size(); ++j) {
1186 auto replacement = get_rand_str(j, dist, gen);
1187 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1188 auto piece = orig.substr(pos, j);
1189 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1190 EXPECT_EQ(msp.find(replacement), pos);
1191 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1192 EXPECT_EQ(msp, orig);
1198 TEST(ReplaceAll, BadArg) {
1200 auto fst = "longer";
1202 char input[] = "meh meh meh";
1203 auto all = MutableStringPiece(input);
1206 all.replaceAll(fst, snd);
1207 } catch (std::invalid_argument&) {
1212 all.replaceAll(snd, fst);
1213 } catch (std::invalid_argument&) {
1217 EXPECT_EQ(count, 2);
1220 TEST(Range, Constructors) {
1221 vector<int> c = {1, 2, 3};
1222 typedef Range<vector<int>::iterator> RangeType;
1223 typedef Range<vector<int>::const_iterator> ConstRangeType;
1224 RangeType cr(c.begin(), c.end());
1225 auto subpiece1 = ConstRangeType(cr, 1, 5);
1226 auto subpiece2 = ConstRangeType(cr, 1);
1227 EXPECT_EQ(subpiece1.size(), 2);
1228 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1229 EXPECT_EQ(subpiece1.end(), subpiece2.end());