2 * Copyright 2014 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>
29 #include <type_traits>
31 #include <boost/range/concepts.hpp>
32 #include <gtest/gtest.h>
34 namespace folly { namespace detail {
36 // declaration of functions in Range.cpp
37 size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
38 const StringPiece& needles);
40 size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
41 const StringPiece& needles);
45 using namespace folly;
48 static_assert(std::is_literal_type<StringPiece>::value, "");
50 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
52 TEST(StringPiece, All) {
53 const char* foo = "foo";
54 const char* foo2 = "foo";
58 // we expect the compiler to optimize things so that there's only one copy
59 // of the string literal "foo", even though we've got it in multiple places
60 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
61 // comparison rather than lexical
63 // the string object creates copies though, so the c_str of these should be
65 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
67 // test the basic StringPiece functionality
69 EXPECT_EQ(s.size(), 3);
71 EXPECT_EQ(s.start(), foo); // ptr comparison
72 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
73 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
75 EXPECT_EQ(s.toString(), foo); // lexical comparison
76 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
77 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
79 EXPECT_EQ(s, foo); // lexical comparison
80 EXPECT_EQ(s, fooStr); // lexical comparison
81 EXPECT_EQ(s, foo2Str); // lexical comparison
84 // check using StringPiece to reference substrings
85 const char* foobarbaz = "foobarbaz";
87 // the full "foobarbaz"
88 s.reset(foobarbaz, strlen(foobarbaz));
89 EXPECT_EQ(s.size(), 9);
90 EXPECT_EQ(s.start(), foobarbaz);
91 EXPECT_EQ(s, "foobarbaz");
94 s.assign(foobarbaz, foobarbaz + 3);
95 EXPECT_EQ(s.size(), 3);
96 EXPECT_EQ(s.start(), foobarbaz);
100 s.reset(foobarbaz, strlen(foobarbaz));
101 EXPECT_EQ(s.find("bar"), 3);
102 EXPECT_EQ(s.find("ba", 3), 3);
103 EXPECT_EQ(s.find("ba", 4), 6);
104 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
105 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
106 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
107 // starting pos that is obviously past the end -- This works for std::string
108 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
109 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
110 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
112 EXPECT_EQ(s.find(""), std::string().find(""));
113 EXPECT_EQ(s.find(""), 0);
116 EXPECT_EQ(s.find('b'), 3);
117 EXPECT_EQ(s.find('b', 3), 3);
118 EXPECT_EQ(s.find('b', 4), 6);
119 EXPECT_EQ(s.find('o', 2), 2);
120 EXPECT_EQ(s.find('y'), StringPiece::npos);
121 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
122 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
123 EXPECT_TRUE(s.contains('z'));
124 // starting pos that is obviously past the end -- This works for std::string
125 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
126 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
127 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
129 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
130 EXPECT_EQ(s.find('\0'), StringPiece::npos);
131 EXPECT_FALSE(s.contains('\0'));
133 // single char rfinds
134 EXPECT_EQ(s.rfind('b'), 6);
135 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
136 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
137 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
138 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
140 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
141 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
144 s.reset(foobarbaz, strlen(foobarbaz));
145 EXPECT_EQ(s.find_first_of("bar"), 3);
146 EXPECT_EQ(s.find_first_of("ba", 3), 3);
147 EXPECT_EQ(s.find_first_of("ba", 4), 4);
148 EXPECT_TRUE(s.contains("bar"));
149 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
150 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
151 EXPECT_FALSE(s.contains("xyxy"));
152 // starting position too far
153 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
154 // starting pos that is obviously past the end -- This works for std::string
155 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
156 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
157 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
158 // empty needle. Note that this returns npos, while find() returns 0!
159 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
160 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
162 // single char find_first_ofs
163 EXPECT_EQ(s.find_first_of('b'), 3);
164 EXPECT_EQ(s.find_first_of('b', 3), 3);
165 EXPECT_EQ(s.find_first_of('b', 4), 6);
166 EXPECT_EQ(s.find_first_of('o', 2), 2);
167 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
168 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
169 // starting position too far
170 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
171 // starting pos that is obviously past the end -- This works for std::string
172 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
173 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
174 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
176 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
177 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
180 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
181 EXPECT_EQ(s.size(), 6);
182 EXPECT_EQ(s.start(), foobarbaz + 3);
183 EXPECT_EQ(s, "barbaz");
186 s.reset(foobarbaz + 3, 3);
187 EXPECT_EQ(s.size(), 3);
192 EXPECT_EQ(s.toString(), "");
194 // test an empty StringPiece
196 EXPECT_EQ(s2.size(), 0);
198 // Test comparison operators
232 void expectLT(const T& a, const T& b) {
235 EXPECT_FALSE(a == b);
236 EXPECT_FALSE(a >= b);
240 EXPECT_FALSE(b <= a);
246 void expectEQ(const T& a, const T& b) {
254 TEST(StringPiece, EightBitComparisons) {
255 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
256 constexpr size_t count = sizeof(values) / sizeof(values[0]);
257 for (size_t i = 0; i < count; ++i) {
258 std::string a(1, values[i]);
259 // Defeat copy-on-write
260 std::string aCopy(a.data(), a.size());
262 expectEQ(StringPiece(a), StringPiece(aCopy));
264 for (size_t j = i + 1; j < count; ++j) {
265 std::string b(1, values[j]);
267 expectLT(StringPiece(a), StringPiece(b));
272 TEST(StringPiece, ToByteRange) {
273 StringPiece a("hello");
275 EXPECT_EQ(static_cast<const void*>(a.begin()),
276 static_cast<const void*>(b.begin()));
277 EXPECT_EQ(static_cast<const void*>(a.end()),
278 static_cast<const void*>(b.end()));
280 // and convert back again
282 EXPECT_EQ(a.begin(), c.begin());
283 EXPECT_EQ(a.end(), c.end());
286 TEST(StringPiece, InvalidRange) {
287 StringPiece a("hello");
288 EXPECT_EQ(a, a.subpiece(0, 10));
289 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
290 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
291 EXPECT_EQ(StringPiece("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);
295 std::string b("hello");
296 EXPECT_EQ(a, StringPiece(b, 0, 10));
297 EXPECT_EQ("ello", a.subpiece(1));
298 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
299 EXPECT_EQ("ell", a.subpiece(1, 3));
300 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
301 EXPECT_THROW(a.subpiece(6), std::out_of_range);
304 #if FOLLY_HAVE_CONSTEXPR_STRLEN
305 constexpr char helloArray[] = "hello";
307 TEST(StringPiece, Constexpr) {
308 constexpr StringPiece hello1("hello");
309 EXPECT_EQ("hello", hello1);
311 constexpr StringPiece hello2(helloArray);
312 EXPECT_EQ("hello", hello2);
316 TEST(StringPiece, Prefix) {
317 StringPiece a("hello");
318 EXPECT_TRUE(a.startsWith(""));
319 EXPECT_TRUE(a.startsWith("h"));
320 EXPECT_TRUE(a.startsWith('h'));
321 EXPECT_TRUE(a.startsWith("hello"));
322 EXPECT_FALSE(a.startsWith("hellox"));
323 EXPECT_FALSE(a.startsWith('x'));
324 EXPECT_FALSE(a.startsWith("x"));
328 EXPECT_TRUE(b.removePrefix(""));
329 EXPECT_EQ("hello", b);
333 EXPECT_TRUE(b.removePrefix("h"));
334 EXPECT_EQ("ello", b);
338 EXPECT_TRUE(b.removePrefix('h'));
339 EXPECT_EQ("ello", b);
343 EXPECT_TRUE(b.removePrefix("hello"));
348 EXPECT_FALSE(b.removePrefix("hellox"));
349 EXPECT_EQ("hello", b);
353 EXPECT_FALSE(b.removePrefix("x"));
354 EXPECT_EQ("hello", b);
358 EXPECT_FALSE(b.removePrefix('x'));
359 EXPECT_EQ("hello", b);
363 TEST(StringPiece, Suffix) {
364 StringPiece a("hello");
365 EXPECT_TRUE(a.endsWith(""));
366 EXPECT_TRUE(a.endsWith("o"));
367 EXPECT_TRUE(a.endsWith('o'));
368 EXPECT_TRUE(a.endsWith("hello"));
369 EXPECT_FALSE(a.endsWith("xhello"));
370 EXPECT_FALSE(a.endsWith("x"));
371 EXPECT_FALSE(a.endsWith('x'));
375 EXPECT_TRUE(b.removeSuffix(""));
376 EXPECT_EQ("hello", b);
380 EXPECT_TRUE(b.removeSuffix("o"));
381 EXPECT_EQ("hell", b);
385 EXPECT_TRUE(b.removeSuffix('o'));
386 EXPECT_EQ("hell", b);
390 EXPECT_TRUE(b.removeSuffix("hello"));
395 EXPECT_FALSE(b.removeSuffix("xhello"));
396 EXPECT_EQ("hello", b);
400 EXPECT_FALSE(b.removeSuffix("x"));
401 EXPECT_EQ("hello", b);
405 EXPECT_FALSE(b.removeSuffix('x'));
406 EXPECT_EQ("hello", b);
410 TEST(StringPiece, PrefixEmpty) {
412 EXPECT_TRUE(a.startsWith(""));
413 EXPECT_FALSE(a.startsWith("a"));
414 EXPECT_FALSE(a.startsWith('a'));
415 EXPECT_TRUE(a.removePrefix(""));
417 EXPECT_FALSE(a.removePrefix("a"));
419 EXPECT_FALSE(a.removePrefix('a'));
423 TEST(StringPiece, SuffixEmpty) {
425 EXPECT_TRUE(a.endsWith(""));
426 EXPECT_FALSE(a.endsWith("a"));
427 EXPECT_FALSE(a.endsWith('a'));
428 EXPECT_TRUE(a.removeSuffix(""));
430 EXPECT_FALSE(a.removeSuffix("a"));
432 EXPECT_FALSE(a.removeSuffix('a'));
436 TEST(StringPiece, split_step_char_delimiter) {
438 // 012345678901234567890123456
439 auto const s = "this is just a test string";
440 auto const e = std::next(s, std::strlen(s));
443 folly::StringPiece p(s);
444 EXPECT_EQ(s, p.begin());
445 EXPECT_EQ(e, p.end());
448 auto x = p.split_step(' ');
449 EXPECT_EQ(std::next(s, 5), p.begin());
450 EXPECT_EQ(e, p.end());
451 EXPECT_EQ("this", x);
453 x = p.split_step(' ');
454 EXPECT_EQ(std::next(s, 8), p.begin());
455 EXPECT_EQ(e, p.end());
458 x = p.split_step('u');
459 EXPECT_EQ(std::next(s, 10), p.begin());
460 EXPECT_EQ(e, p.end());
463 x = p.split_step(' ');
464 EXPECT_EQ(std::next(s, 13), p.begin());
465 EXPECT_EQ(e, p.end());
468 x = p.split_step(' ');
469 EXPECT_EQ(std::next(s, 14), p.begin());
470 EXPECT_EQ(e, p.end());
473 x = p.split_step(' ');
474 EXPECT_EQ(std::next(s, 16), p.begin());
475 EXPECT_EQ(e, p.end());
478 x = p.split_step(' ');
479 EXPECT_EQ(std::next(s, 21), p.begin());
480 EXPECT_EQ(e, p.end());
481 EXPECT_EQ("test", x);
483 x = p.split_step(' ');
484 EXPECT_EQ(e, p.begin());
485 EXPECT_EQ(e, p.end());
486 EXPECT_EQ("string", x);
488 x = p.split_step(' ');
489 EXPECT_EQ(e, p.begin());
490 EXPECT_EQ(e, p.end());
494 TEST(StringPiece, split_step_range_delimiter) {
496 // 0123456789012345678901234567890123
497 auto const s = "this is just a test string";
498 auto const e = std::next(s, std::strlen(s));
501 folly::StringPiece p(s);
502 EXPECT_EQ(s, p.begin());
503 EXPECT_EQ(e, p.end());
506 auto x = p.split_step(" ");
507 EXPECT_EQ(std::next(s, 6), p.begin());
508 EXPECT_EQ(e, p.end());
509 EXPECT_EQ("this", x);
511 x = p.split_step(" ");
512 EXPECT_EQ(std::next(s, 10), p.begin());
513 EXPECT_EQ(e, p.end());
516 x = p.split_step("u");
517 EXPECT_EQ(std::next(s, 12), 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, 18), p.begin());
528 EXPECT_EQ(e, p.end());
531 x = p.split_step(" ");
532 EXPECT_EQ(std::next(s, 21), p.begin());
533 EXPECT_EQ(e, p.end());
536 x = p.split_step(" ");
537 EXPECT_EQ(std::next(s, 28), p.begin());
538 EXPECT_EQ(e, p.end());
539 EXPECT_EQ(" test", x);
541 x = p.split_step(" ");
542 EXPECT_EQ(e, p.begin());
543 EXPECT_EQ(e, p.end());
544 EXPECT_EQ("string", x);
546 x = p.split_step(" ");
547 EXPECT_EQ(e, p.begin());
548 EXPECT_EQ(e, p.end());
551 x = p.split_step(" ");
552 EXPECT_EQ(e, p.begin());
553 EXPECT_EQ(e, p.end());
557 void split_step_with_process_noop(folly::StringPiece) {}
559 TEST(StringPiece, split_step_with_process_char_delimiter) {
561 // 012345678901234567890123456
562 auto const s = "this is just a test string";
563 auto const e = std::next(s, std::strlen(s));
566 folly::StringPiece p(s);
567 EXPECT_EQ(s, p.begin());
568 EXPECT_EQ(e, p.end());
571 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
572 EXPECT_EQ(std::next(s, 5), p.begin());
573 EXPECT_EQ(e, p.end());
574 EXPECT_EQ("this", x);
578 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
579 EXPECT_EQ(std::next(s, 8), p.begin());
580 EXPECT_EQ(e, p.end());
585 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
586 EXPECT_EQ(std::next(s, 10), p.begin());
587 EXPECT_EQ(e, p.end());
592 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
593 EXPECT_EQ(std::next(s, 13), p.begin());
594 EXPECT_EQ(e, p.end());
599 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
600 EXPECT_EQ(std::next(s, 14), p.begin());
601 EXPECT_EQ(e, p.end());
606 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
607 EXPECT_EQ(std::next(s, 16), p.begin());
608 EXPECT_EQ(e, p.end());
613 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
614 EXPECT_EQ(std::next(s, 21), p.begin());
615 EXPECT_EQ(e, p.end());
616 EXPECT_EQ("test", x);
620 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
621 EXPECT_EQ(e, p.begin());
622 EXPECT_EQ(e, p.end());
623 EXPECT_EQ("string", x);
627 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
628 EXPECT_EQ(e, p.begin());
629 EXPECT_EQ(e, p.end());
634 EXPECT_TRUE((std::is_same<
636 decltype(p.split_step(' ', split_step_with_process_noop))
639 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
642 TEST(StringPiece, split_step_with_process_range_delimiter) {
644 // 0123456789012345678901234567890123
645 auto const s = "this is just a test string";
646 auto const e = std::next(s, std::strlen(s));
649 folly::StringPiece p(s);
650 EXPECT_EQ(s, p.begin());
651 EXPECT_EQ(e, p.end());
654 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
655 EXPECT_EQ(std::next(s, 6), p.begin());
656 EXPECT_EQ(e, p.end());
657 EXPECT_EQ("this", x);
661 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
662 EXPECT_EQ(std::next(s, 10), p.begin());
663 EXPECT_EQ(e, p.end());
668 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
669 EXPECT_EQ(std::next(s, 12), p.begin());
670 EXPECT_EQ(e, p.end());
675 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
676 EXPECT_EQ(std::next(s, 16), p.begin());
677 EXPECT_EQ(e, p.end());
682 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
683 EXPECT_EQ(std::next(s, 18), p.begin());
684 EXPECT_EQ(e, p.end());
689 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
690 EXPECT_EQ(std::next(s, 21), p.begin());
691 EXPECT_EQ(e, p.end());
696 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
697 EXPECT_EQ(std::next(s, 28), p.begin());
698 EXPECT_EQ(e, p.end());
699 EXPECT_EQ(" test", x);
703 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
704 EXPECT_EQ(e, p.begin());
705 EXPECT_EQ(e, p.end());
706 EXPECT_EQ("string", x);
710 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
711 EXPECT_EQ(e, p.begin());
712 EXPECT_EQ(e, p.end());
717 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
718 EXPECT_EQ(e, p.begin());
719 EXPECT_EQ(e, p.end());
724 EXPECT_TRUE((std::is_same<
726 decltype(p.split_step(' ', split_step_with_process_noop))
729 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
732 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
734 // 012345678901234567890123456
735 auto const s = "this is just a test string";
736 auto const e = std::next(s, std::strlen(s));
737 auto const delimiter = ' ';
740 folly::StringPiece p(s);
741 EXPECT_EQ(s, p.begin());
742 EXPECT_EQ(e, p.end());
745 auto const functor = [](
746 folly::StringPiece s,
747 folly::StringPiece expected
749 EXPECT_EQ(expected, s);
753 auto const checker = [&](folly::StringPiece expected) {
754 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
767 EXPECT_TRUE(p.empty());
770 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
772 // 0123456789012345678901234567890123
773 auto const s = "this is just a test string";
774 auto const e = std::next(s, std::strlen(s));
775 auto const delimiter = " ";
778 folly::StringPiece p(s);
779 EXPECT_EQ(s, p.begin());
780 EXPECT_EQ(e, p.end());
783 auto const functor = [](
784 folly::StringPiece s,
785 folly::StringPiece expected
787 EXPECT_EQ(expected, s);
791 auto const checker = [&](folly::StringPiece expected) {
792 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
805 EXPECT_TRUE(p.empty());
808 TEST(qfind, UInt32_Ranges) {
809 vector<uint32_t> a({1, 2, 3, 260, 5});
810 vector<uint32_t> b({2, 3, 4});
812 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
813 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
815 EXPECT_EQ(qfind(a_range, b_range), string::npos);
818 EXPECT_EQ(qfind(a_range, b_range), 1);
821 template <typename NeedleFinder>
822 class NeedleFinderTest : public ::testing::Test {
824 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
825 return NeedleFinder::find_first_byte_of(haystack, needles);
829 struct SseNeedleFinder {
830 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
831 // This will only use the SSE version if it is supported on this CPU
832 // (selected using ifunc).
833 return detail::qfind_first_byte_of(haystack, needles);
837 struct NoSseNeedleFinder {
838 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
839 return detail::qfind_first_byte_of_nosse(haystack, needles);
843 struct MemchrNeedleFinder {
844 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
845 return detail::qfind_first_byte_of_memchr(haystack, needles);
849 struct ByteSetNeedleFinder {
850 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
851 return detail::qfind_first_byte_of_byteset(haystack, needles);
855 typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
856 ByteSetNeedleFinder> NeedleFinders;
857 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
859 TYPED_TEST(NeedleFinderTest, Null) {
860 { // null characters in the string
861 string s(10, char(0));
863 string delims("abc");
864 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
866 { // null characters in delim
868 string delims(10, char(0));
871 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
873 { // range not terminated by null character
874 string buf = "abcdefghijklmnopqrstuvwxyz";
875 StringPiece s(buf.data() + 5, 3);
876 StringPiece delims("z");
877 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
881 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
882 string delims(1000, 'b');
883 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
884 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
887 TYPED_TEST(NeedleFinderTest, Empty) {
890 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
891 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
892 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
895 TYPED_TEST(NeedleFinderTest, Unaligned) {
896 // works correctly even if input buffers are not 16-byte aligned
897 string s = "0123456789ABCDEFGH";
898 for (int i = 0; i < s.size(); ++i) {
899 StringPiece a(s.c_str() + i);
900 for (int j = 0; j < s.size(); ++j) {
901 StringPiece b(s.c_str() + j);
902 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
907 // for some algorithms (specifically those that create a set of needles),
908 // we check for the edge-case of _all_ possible needles being sought.
909 TYPED_TEST(NeedleFinderTest, Needles256) {
911 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
912 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
913 // make the size ~big to avoid any edge-case branches for tiny haystacks
914 const int haystackSize = 50;
915 for (int i = minValue; i <= maxValue; i++) { // <=
916 needles.push_back(i);
918 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
919 for (int i = minValue; i <= maxValue; i++) {
920 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
923 needles.append("these are redundant characters");
924 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
925 for (int i = minValue; i <= maxValue; i++) {
926 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
930 TYPED_TEST(NeedleFinderTest, Base) {
931 for (int i = 0; i < 32; ++i) {
932 for (int j = 0; j < 32; ++j) {
933 string s = string(i, 'X') + "abca" + string(i, 'X');
934 string delims = string(j, 'Y') + "a" + string(j, 'Y');
935 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
940 const size_t kPageSize = 4096;
941 // Updates contents so that any read accesses past the last byte will
942 // cause a SIGSEGV. It accomplishes this by changing access to the page that
943 // begins immediately after the end of the contents (as allocators and mmap()
944 // all operate on page boundaries, this is a reasonable assumption).
945 // This function will also initialize buf, which caller must free().
946 void createProtectedBuf(StringPiece& contents, char** buf) {
947 ASSERT_LE(contents.size(), kPageSize);
948 const size_t kSuccess = 0;
949 if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
952 mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
953 size_t newBegin = kPageSize - contents.size();
954 memcpy(*buf + newBegin, contents.data(), contents.size());
955 contents.reset(*buf + newBegin, contents.size());
958 void freeProtectedBuf(char* buf) {
959 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
963 TYPED_TEST(NeedleFinderTest, NoSegFault) {
964 const string base = string(32, 'a') + string("b");
965 const string delims = string(32, 'c') + string("b");
966 for (int i = 0; i <= 32; i++) {
967 for (int j = 0; j <= 33; j++) {
968 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
969 StringPiece s1(base);
971 ASSERT_TRUE(!s1.empty());
975 StringPiece s2(delims);
979 createProtectedBuf(s1, &buf1);
980 createProtectedBuf(s2, &buf2);
981 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
982 // string(s1.data(), s1.size()).c_str(), s1.size(),
983 // string(s2.data(), s2.size()).c_str(), s2.size());
984 auto r1 = this->find_first_byte_of(s1, s2);
985 auto f1 = std::find_first_of(s1.begin(), s1.end(),
986 s2.begin(), s2.end());
987 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
989 auto r2 = this->find_first_byte_of(s2, s1);
990 auto f2 = std::find_first_of(s2.begin(), s2.end(),
991 s1.begin(), s1.end());
992 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
994 freeProtectedBuf(buf1);
995 freeProtectedBuf(buf2);
1001 TEST(NonConstTest, StringPiece) {
1002 std::string hello("hello");
1003 MutableStringPiece sp(&hello.front(), hello.size());
1005 EXPECT_EQ("xello", hello);
1008 EXPECT_EQ("xello", s);
1012 MutableByteRange r2(sp);
1017 void testRangeFunc(C&& x, size_t n) {
1019 // type, conversion checks
1020 Range<int*> r1 = range(std::forward<C>(x));
1021 Range<const int*> r2 = range(std::forward<C>(x));
1022 Range<const int*> r3 = range(cx);
1023 Range<const int*> r5 = range(std::move(cx));
1024 EXPECT_EQ(r1.begin(), &x[0]);
1025 EXPECT_EQ(r1.end(), &x[n]);
1026 EXPECT_EQ(n, r1.size());
1027 EXPECT_EQ(n, r2.size());
1028 EXPECT_EQ(n, r3.size());
1029 EXPECT_EQ(n, r5.size());
1032 TEST(RangeFunc, Vector) {
1034 testRangeFunc(x, 0);
1036 testRangeFunc(x, 1);
1037 testRangeFunc(std::vector<int>{1, 2}, 2);
1040 TEST(RangeFunc, Array) {
1041 std::array<int, 3> x;
1042 testRangeFunc(x, 3);
1045 TEST(RangeFunc, CArray) {
1046 int x[] {1, 2, 3, 4};
1047 testRangeFunc(x, 4);
1050 std::string get_rand_str(
1051 int size, std::uniform_int_distribution<>& dist, std::mt19937& gen) {
1052 std::string ret(size, '\0');
1053 for (int i=0; i<size; ++i) {
1054 ret[i] = static_cast<char>(dist(gen));
1061 bool operator==(MutableStringPiece mp, StringPiece sp) {
1062 return mp.compare(sp) == 0;
1065 bool operator==(StringPiece sp, MutableStringPiece mp) {
1066 return mp.compare(sp) == 0;
1070 TEST(ReplaceAt, exhaustiveTest) {
1071 char input[] = "this is nice and long input";
1072 auto msp = MutableStringPiece(input);
1073 auto str = std::string(input);
1074 std::random_device rd;
1075 std::mt19937 gen(rd());
1076 std::uniform_int_distribution<> dist('a', 'z');
1078 for (int i=0; i < 100; ++i) {
1079 for (int j = 1; j <= msp.size(); ++j) {
1080 auto replacement = get_rand_str(j, dist, gen);
1081 for (int pos=0; pos < msp.size() - j; ++pos) {
1082 msp.replaceAt(pos, replacement);
1083 str.replace(pos, replacement.size(), replacement);
1084 EXPECT_EQ(msp.compare(str), 0);
1090 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1093 TEST(ReplaceAll, basicTest) {
1094 char input[] = "this is nice and long input";
1095 auto orig = std::string(input);
1096 auto msp = MutableStringPiece(input);
1098 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1099 EXPECT_EQ("thsi si nice and long input", msp);
1100 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1101 EXPECT_EQ(msp, orig);
1103 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1104 EXPECT_EQ(msp, orig);
1106 // at the very beginning
1107 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1108 EXPECT_EQ("siht is nice and long input", msp);
1109 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1110 EXPECT_EQ(msp, orig);
1113 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1114 EXPECT_EQ("this is nice and long soput", msp);
1115 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1116 EXPECT_EQ(msp, orig);
1119 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1120 EXPECT_EQ("this@is@nice@and@long@input", msp);
1121 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1122 EXPECT_EQ(msp, orig);
1125 TEST(ReplaceAll, randomTest) {
1126 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1127 auto orig = std::string(input);
1128 auto msp = MutableStringPiece(input);
1130 std::random_device rd;
1131 std::mt19937 gen(rd());
1132 std::uniform_int_distribution<> dist('A', 'Z');
1134 for (int i=0; i < 100; ++i) {
1135 for (int j = 1; j <= orig.size(); ++j) {
1136 auto replacement = get_rand_str(j, dist, gen);
1137 for (int pos=0; pos < msp.size() - j; ++pos) {
1138 auto piece = orig.substr(pos, j);
1139 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1140 EXPECT_EQ(msp.find(replacement), pos);
1141 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1142 EXPECT_EQ(msp, orig);
1148 TEST(ReplaceAll, BadArg) {
1150 auto fst = "longer";
1152 char input[] = "meh meh meh";
1153 auto all = MutableStringPiece(input);
1156 all.replaceAll(fst, snd);
1157 } catch (std::invalid_argument&) {
1162 all.replaceAll(snd, fst);
1163 } catch (std::invalid_argument&) {
1167 EXPECT_EQ(count, 2);