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>
27 #include <type_traits>
30 #include <boost/algorithm/string/trim.hpp>
31 #include <boost/range/concepts.hpp>
33 #include <folly/portability/GTest.h>
34 #include <folly/portability/Memory.h>
35 #include <folly/portability/SysMman.h>
37 using namespace folly;
38 using namespace folly::detail;
41 static_assert(std::is_literal_type<StringPiece>::value, "");
43 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
45 TEST(StringPiece, All) {
46 const char* foo = "foo";
47 const char* foo2 = "foo";
51 // we expect the compiler to optimize things so that there's only one copy
52 // of the string literal "foo", even though we've got it in multiple places
53 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
54 // comparison rather than lexical
56 // the string object creates copies though, so the c_str of these should be
58 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
60 // test the basic StringPiece functionality
62 EXPECT_EQ(s.size(), 3);
64 EXPECT_EQ(s.start(), foo); // ptr comparison
65 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
66 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
68 EXPECT_EQ(s.toString(), foo); // lexical comparison
69 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
70 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
72 EXPECT_EQ(s, foo); // lexical comparison
73 EXPECT_EQ(s, fooStr); // lexical comparison
74 EXPECT_EQ(s, foo2Str); // lexical comparison
77 // check using StringPiece to reference substrings
78 const char* foobarbaz = "foobarbaz";
80 // the full "foobarbaz"
81 s.reset(foobarbaz, strlen(foobarbaz));
82 EXPECT_EQ(s.size(), 9);
83 EXPECT_EQ(s.start(), foobarbaz);
84 EXPECT_EQ(s, "foobarbaz");
87 s.assign(foobarbaz, foobarbaz + 3);
88 EXPECT_EQ(s.size(), 3);
89 EXPECT_EQ(s.start(), foobarbaz);
93 s.reset(foobarbaz, strlen(foobarbaz));
94 EXPECT_EQ(s.find("bar"), 3);
95 EXPECT_EQ(s.find("ba", 3), 3);
96 EXPECT_EQ(s.find("ba", 4), 6);
97 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
98 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
99 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
100 // starting pos that is obviously past the end -- This works for std::string
101 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
102 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
103 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
105 EXPECT_EQ(s.find(""), std::string().find(""));
106 EXPECT_EQ(s.find(""), 0);
109 EXPECT_EQ(s.find('b'), 3);
110 EXPECT_EQ(s.find('b', 3), 3);
111 EXPECT_EQ(s.find('b', 4), 6);
112 EXPECT_EQ(s.find('o', 2), 2);
113 EXPECT_EQ(s.find('y'), StringPiece::npos);
114 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
115 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
116 EXPECT_TRUE(s.contains('z'));
117 // starting pos that is obviously past the end -- This works for std::string
118 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
119 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
120 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
122 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
123 EXPECT_EQ(s.find('\0'), StringPiece::npos);
124 EXPECT_FALSE(s.contains('\0'));
126 // single char rfinds
127 EXPECT_EQ(s.rfind('b'), 6);
128 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
129 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
130 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
131 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
133 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
134 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
137 s.reset(foobarbaz, strlen(foobarbaz));
138 EXPECT_EQ(s.find_first_of("bar"), 3);
139 EXPECT_EQ(s.find_first_of("ba", 3), 3);
140 EXPECT_EQ(s.find_first_of("ba", 4), 4);
141 EXPECT_TRUE(s.contains("bar"));
142 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
143 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
144 EXPECT_FALSE(s.contains("xyxy"));
145 // starting position too far
146 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
147 // starting pos that is obviously past the end -- This works for std::string
148 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
149 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
150 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
151 // empty needle. Note that this returns npos, while find() returns 0!
152 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
153 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
155 // single char find_first_ofs
156 EXPECT_EQ(s.find_first_of('b'), 3);
157 EXPECT_EQ(s.find_first_of('b', 3), 3);
158 EXPECT_EQ(s.find_first_of('b', 4), 6);
159 EXPECT_EQ(s.find_first_of('o', 2), 2);
160 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
161 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
162 // starting position too far
163 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
164 // starting pos that is obviously past the end -- This works for std::string
165 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
166 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
167 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
169 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
170 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
173 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
174 EXPECT_EQ(s.size(), 6);
175 EXPECT_EQ(s.start(), foobarbaz + 3);
176 EXPECT_EQ(s, "barbaz");
179 s.reset(foobarbaz + 3, 3);
180 EXPECT_EQ(s.size(), 3);
185 EXPECT_EQ(s.toString(), "");
187 // test an empty StringPiece
189 EXPECT_EQ(s2.size(), 0);
191 // Test comparison operators
225 void expectLT(const T& a, const T& b) {
228 EXPECT_FALSE(a == b);
229 EXPECT_FALSE(a >= b);
233 EXPECT_FALSE(b <= a);
239 void expectEQ(const T& a, const T& b) {
247 TEST(StringPiece, EightBitComparisons) {
248 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
249 constexpr size_t count = sizeof(values) / sizeof(values[0]);
250 for (size_t i = 0; i < count; ++i) {
251 std::string a(1, values[i]);
252 // Defeat copy-on-write
253 std::string aCopy(a.data(), a.size());
255 expectEQ(StringPiece(a), StringPiece(aCopy));
257 for (size_t j = i + 1; j < count; ++j) {
258 std::string b(1, values[j]);
260 expectLT(StringPiece(a), StringPiece(b));
265 TEST(StringPiece, ToByteRange) {
266 StringPiece a("hello");
269 static_cast<const void*>(a.begin()), static_cast<const void*>(b.begin()));
271 static_cast<const void*>(a.end()), static_cast<const void*>(b.end()));
273 // and convert back again
275 EXPECT_EQ(a.begin(), c.begin());
276 EXPECT_EQ(a.end(), c.end());
279 TEST(StringPiece, InvalidRange) {
280 StringPiece a("hello");
281 EXPECT_EQ(a, a.subpiece(0, 10));
282 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
283 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
284 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
285 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
286 EXPECT_THROW(a.subpiece(6), std::out_of_range);
288 std::string b("hello");
289 EXPECT_EQ(a, StringPiece(b, 0, 10));
290 EXPECT_EQ("ello", a.subpiece(1));
291 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
292 EXPECT_EQ("ell", a.subpiece(1, 3));
293 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
294 EXPECT_THROW(a.subpiece(6), std::out_of_range);
297 TEST(StringPiece, Constexpr) {
298 constexpr const char* helloArray = "hello";
300 constexpr StringPiece hello1("hello");
301 EXPECT_EQ("hello", hello1);
302 static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
304 constexpr StringPiece hello2(helloArray);
305 EXPECT_EQ("hello", hello2);
306 static_assert(hello2.size() == 5, "hello size should be 5 at compile time");
309 TEST(StringPiece, Prefix) {
310 StringPiece a("hello");
311 EXPECT_TRUE(a.startsWith(""));
312 EXPECT_TRUE(a.startsWith("h"));
313 EXPECT_TRUE(a.startsWith('h'));
314 EXPECT_TRUE(a.startsWith("hello"));
315 EXPECT_FALSE(a.startsWith("hellox"));
316 EXPECT_FALSE(a.startsWith('x'));
317 EXPECT_FALSE(a.startsWith("x"));
319 EXPECT_TRUE(a.startsWith("", folly::AsciiCaseInsensitive()));
320 EXPECT_TRUE(a.startsWith("hello", folly::AsciiCaseInsensitive()));
321 EXPECT_TRUE(a.startsWith("hellO", folly::AsciiCaseInsensitive()));
322 EXPECT_TRUE(a.startsWith("HELL", folly::AsciiCaseInsensitive()));
323 EXPECT_TRUE(a.startsWith("H", folly::AsciiCaseInsensitive()));
324 EXPECT_FALSE(a.startsWith("HELLOX", folly::AsciiCaseInsensitive()));
325 EXPECT_FALSE(a.startsWith("x", folly::AsciiCaseInsensitive()));
326 EXPECT_FALSE(a.startsWith("X", folly::AsciiCaseInsensitive()));
330 EXPECT_TRUE(b.removePrefix(""));
331 EXPECT_EQ("hello", b);
335 EXPECT_TRUE(b.removePrefix("h"));
336 EXPECT_EQ("ello", b);
340 EXPECT_TRUE(b.removePrefix('h'));
341 EXPECT_EQ("ello", b);
345 EXPECT_TRUE(b.removePrefix("hello"));
350 EXPECT_FALSE(b.removePrefix("hellox"));
351 EXPECT_EQ("hello", b);
355 EXPECT_FALSE(b.removePrefix("x"));
356 EXPECT_EQ("hello", b);
360 EXPECT_FALSE(b.removePrefix('x'));
361 EXPECT_EQ("hello", b);
365 TEST(StringPiece, Suffix) {
366 StringPiece a("hello");
367 EXPECT_TRUE(a.endsWith(""));
368 EXPECT_TRUE(a.endsWith("o"));
369 EXPECT_TRUE(a.endsWith('o'));
370 EXPECT_TRUE(a.endsWith("hello"));
371 EXPECT_FALSE(a.endsWith("xhello"));
372 EXPECT_FALSE(a.endsWith("x"));
373 EXPECT_FALSE(a.endsWith('x'));
375 EXPECT_TRUE(a.endsWith("", folly::AsciiCaseInsensitive()));
376 EXPECT_TRUE(a.endsWith("o", folly::AsciiCaseInsensitive()));
377 EXPECT_TRUE(a.endsWith("O", folly::AsciiCaseInsensitive()));
378 EXPECT_TRUE(a.endsWith("hello", folly::AsciiCaseInsensitive()));
379 EXPECT_TRUE(a.endsWith("hellO", folly::AsciiCaseInsensitive()));
380 EXPECT_FALSE(a.endsWith("xhello", folly::AsciiCaseInsensitive()));
381 EXPECT_FALSE(a.endsWith("Xhello", folly::AsciiCaseInsensitive()));
382 EXPECT_FALSE(a.endsWith("x", folly::AsciiCaseInsensitive()));
383 EXPECT_FALSE(a.endsWith("X", folly::AsciiCaseInsensitive()));
387 EXPECT_TRUE(b.removeSuffix(""));
388 EXPECT_EQ("hello", b);
392 EXPECT_TRUE(b.removeSuffix("o"));
393 EXPECT_EQ("hell", b);
397 EXPECT_TRUE(b.removeSuffix('o'));
398 EXPECT_EQ("hell", b);
402 EXPECT_TRUE(b.removeSuffix("hello"));
407 EXPECT_FALSE(b.removeSuffix("xhello"));
408 EXPECT_EQ("hello", b);
412 EXPECT_FALSE(b.removeSuffix("x"));
413 EXPECT_EQ("hello", b);
417 EXPECT_FALSE(b.removeSuffix('x'));
418 EXPECT_EQ("hello", b);
422 TEST(StringPiece, Equals) {
423 StringPiece a("hello");
425 EXPECT_TRUE(a.equals("HELLO", AsciiCaseInsensitive()));
426 EXPECT_FALSE(a.equals("HELLOX", AsciiCaseInsensitive()));
429 TEST(StringPiece, PrefixEmpty) {
431 EXPECT_TRUE(a.startsWith(""));
432 EXPECT_FALSE(a.startsWith("a"));
433 EXPECT_FALSE(a.startsWith('a'));
434 EXPECT_TRUE(a.removePrefix(""));
436 EXPECT_FALSE(a.removePrefix("a"));
438 EXPECT_FALSE(a.removePrefix('a'));
442 TEST(StringPiece, SuffixEmpty) {
444 EXPECT_TRUE(a.endsWith(""));
445 EXPECT_FALSE(a.endsWith("a"));
446 EXPECT_FALSE(a.endsWith('a'));
447 EXPECT_TRUE(a.removeSuffix(""));
449 EXPECT_FALSE(a.removeSuffix("a"));
451 EXPECT_FALSE(a.removeSuffix('a'));
455 TEST(StringPiece, erase) {
456 StringPiece a("hello");
460 EXPECT_EQ("ello", a);
469 EXPECT_THROW(a.erase(b, e), std::out_of_range);
488 boost::algorithm::trim(a);
489 EXPECT_EQ(a, "hello");
492 TEST(StringPiece, split_step_char_delimiter) {
494 // 012345678901234567890123456
495 auto const s = "this is just a test string";
496 auto const e = std::next(s, std::strlen(s));
499 folly::StringPiece p(s);
500 EXPECT_EQ(s, p.begin());
501 EXPECT_EQ(e, p.end());
504 auto x = p.split_step(' ');
505 EXPECT_EQ(std::next(s, 5), p.begin());
506 EXPECT_EQ(e, p.end());
507 EXPECT_EQ("this", x);
509 x = p.split_step(' ');
510 EXPECT_EQ(std::next(s, 8), p.begin());
511 EXPECT_EQ(e, p.end());
514 x = p.split_step('u');
515 EXPECT_EQ(std::next(s, 10), p.begin());
516 EXPECT_EQ(e, p.end());
519 x = p.split_step(' ');
520 EXPECT_EQ(std::next(s, 13), p.begin());
521 EXPECT_EQ(e, p.end());
524 x = p.split_step(' ');
525 EXPECT_EQ(std::next(s, 14), p.begin());
526 EXPECT_EQ(e, p.end());
529 x = p.split_step(' ');
530 EXPECT_EQ(std::next(s, 16), p.begin());
531 EXPECT_EQ(e, p.end());
534 x = p.split_step(' ');
535 EXPECT_EQ(std::next(s, 21), p.begin());
536 EXPECT_EQ(e, p.end());
537 EXPECT_EQ("test", x);
539 x = p.split_step(' ');
540 EXPECT_EQ(e, p.begin());
541 EXPECT_EQ(e, p.end());
542 EXPECT_EQ("string", x);
544 x = p.split_step(' ');
545 EXPECT_EQ(e, p.begin());
546 EXPECT_EQ(e, p.end());
550 TEST(StringPiece, split_step_range_delimiter) {
552 // 0123456789012345678901234567890123
553 auto const s = "this is just a test string";
554 auto const e = std::next(s, std::strlen(s));
557 folly::StringPiece p(s);
558 EXPECT_EQ(s, p.begin());
559 EXPECT_EQ(e, p.end());
562 auto x = p.split_step(" ");
563 EXPECT_EQ(std::next(s, 6), p.begin());
564 EXPECT_EQ(e, p.end());
565 EXPECT_EQ("this", x);
567 x = p.split_step(" ");
568 EXPECT_EQ(std::next(s, 10), p.begin());
569 EXPECT_EQ(e, p.end());
572 x = p.split_step("u");
573 EXPECT_EQ(std::next(s, 12), p.begin());
574 EXPECT_EQ(e, p.end());
577 x = p.split_step(" ");
578 EXPECT_EQ(std::next(s, 16), p.begin());
579 EXPECT_EQ(e, p.end());
582 x = p.split_step(" ");
583 EXPECT_EQ(std::next(s, 18), p.begin());
584 EXPECT_EQ(e, p.end());
587 x = p.split_step(" ");
588 EXPECT_EQ(std::next(s, 21), p.begin());
589 EXPECT_EQ(e, p.end());
592 x = p.split_step(" ");
593 EXPECT_EQ(std::next(s, 28), p.begin());
594 EXPECT_EQ(e, p.end());
595 EXPECT_EQ(" test", x);
597 x = p.split_step(" ");
598 EXPECT_EQ(e, p.begin());
599 EXPECT_EQ(e, p.end());
600 EXPECT_EQ("string", x);
602 x = p.split_step(" ");
603 EXPECT_EQ(e, p.begin());
604 EXPECT_EQ(e, p.end());
607 x = p.split_step(" ");
608 EXPECT_EQ(e, p.begin());
609 EXPECT_EQ(e, p.end());
613 void split_step_with_process_noop(folly::StringPiece) {}
615 TEST(StringPiece, split_step_with_process_char_delimiter) {
617 // 012345678901234567890123456
618 auto const s = "this is just a test string";
619 auto const e = std::next(s, std::strlen(s));
622 folly::StringPiece p(s);
623 EXPECT_EQ(s, p.begin());
624 EXPECT_EQ(e, p.end());
627 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
628 EXPECT_EQ(std::next(s, 5), p.begin());
629 EXPECT_EQ(e, p.end());
630 EXPECT_EQ("this", x);
634 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
635 EXPECT_EQ(std::next(s, 8), p.begin());
636 EXPECT_EQ(e, p.end());
641 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
642 EXPECT_EQ(std::next(s, 10), p.begin());
643 EXPECT_EQ(e, p.end());
648 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
649 EXPECT_EQ(std::next(s, 13), p.begin());
650 EXPECT_EQ(e, p.end());
655 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
656 EXPECT_EQ(std::next(s, 14), p.begin());
657 EXPECT_EQ(e, p.end());
662 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
663 EXPECT_EQ(std::next(s, 16), p.begin());
664 EXPECT_EQ(e, p.end());
669 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
670 EXPECT_EQ(std::next(s, 21), p.begin());
671 EXPECT_EQ(e, p.end());
672 EXPECT_EQ("test", x);
676 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
677 EXPECT_EQ(e, p.begin());
678 EXPECT_EQ(e, p.end());
679 EXPECT_EQ("string", x);
683 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
684 EXPECT_EQ(e, p.begin());
685 EXPECT_EQ(e, p.end());
693 decltype(p.split_step(' ', split_step_with_process_noop))>::value));
695 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
698 TEST(StringPiece, split_step_with_process_range_delimiter) {
700 // 0123456789012345678901234567890123
701 auto const s = "this is just a test string";
702 auto const e = std::next(s, std::strlen(s));
705 folly::StringPiece p(s);
706 EXPECT_EQ(s, p.begin());
707 EXPECT_EQ(e, p.end());
710 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
711 EXPECT_EQ(std::next(s, 6), p.begin());
712 EXPECT_EQ(e, p.end());
713 EXPECT_EQ("this", x);
717 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
718 EXPECT_EQ(std::next(s, 10), p.begin());
719 EXPECT_EQ(e, p.end());
724 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
725 EXPECT_EQ(std::next(s, 12), p.begin());
726 EXPECT_EQ(e, p.end());
731 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
732 EXPECT_EQ(std::next(s, 16), p.begin());
733 EXPECT_EQ(e, p.end());
738 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
739 EXPECT_EQ(std::next(s, 18), p.begin());
740 EXPECT_EQ(e, p.end());
745 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
746 EXPECT_EQ(std::next(s, 21), p.begin());
747 EXPECT_EQ(e, p.end());
752 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
753 EXPECT_EQ(std::next(s, 28), p.begin());
754 EXPECT_EQ(e, p.end());
755 EXPECT_EQ(" test", x);
759 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
760 EXPECT_EQ(e, p.begin());
761 EXPECT_EQ(e, p.end());
762 EXPECT_EQ("string", x);
766 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
767 EXPECT_EQ(e, p.begin());
768 EXPECT_EQ(e, p.end());
773 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
774 EXPECT_EQ(e, p.begin());
775 EXPECT_EQ(e, p.end());
783 decltype(p.split_step(' ', split_step_with_process_noop))>::value));
785 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
788 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
790 // 012345678901234567890123456
791 auto const s = "this is just a test string";
792 auto const e = std::next(s, std::strlen(s));
793 auto const delimiter = ' ';
796 folly::StringPiece p(s);
797 EXPECT_EQ(s, p.begin());
798 EXPECT_EQ(e, p.end());
801 auto const functor = [](folly::StringPiece s, folly::StringPiece expected) {
802 EXPECT_EQ(expected, s);
806 auto const checker = [&](folly::StringPiece expected) {
807 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
820 EXPECT_TRUE(p.empty());
823 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
825 // 0123456789012345678901234567890123
826 auto const s = "this is just a test string";
827 auto const e = std::next(s, std::strlen(s));
828 auto const delimiter = " ";
831 folly::StringPiece p(s);
832 EXPECT_EQ(s, p.begin());
833 EXPECT_EQ(e, p.end());
836 auto const functor = [](folly::StringPiece s, folly::StringPiece expected) {
837 EXPECT_EQ(expected, s);
841 auto const checker = [&](folly::StringPiece expected) {
842 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
855 EXPECT_TRUE(p.empty());
858 TEST(StringPiece, NoInvalidImplicitConversions) {
860 bool operator()(folly::Range<int*>) {
863 bool operator()(folly::StringPiece) {
868 std::string s = "hello";
869 EXPECT_TRUE(IsString()(s));
872 TEST(qfind, UInt32_Ranges) {
873 vector<uint32_t> a({1, 2, 3, 260, 5});
874 vector<uint32_t> b({2, 3, 4});
876 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
877 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
879 EXPECT_EQ(qfind(a_range, b_range), string::npos);
882 EXPECT_EQ(qfind(a_range, b_range), 1);
885 template <typename NeedleFinder>
886 class NeedleFinderTest : public ::testing::Test {
888 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
889 return NeedleFinder::find_first_byte_of(haystack, needles);
893 struct SseNeedleFinder {
894 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
895 // This will only use the SSE version if it is supported on this CPU
896 // (selected using ifunc).
897 return detail::qfind_first_byte_of(haystack, needles);
901 struct NoSseNeedleFinder {
902 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
903 return detail::qfind_first_byte_of_nosse(haystack, needles);
907 struct ByteSetNeedleFinder {
908 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
909 return detail::qfind_first_byte_of_byteset(haystack, needles);
913 using NeedleFinders =
914 ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, ByteSetNeedleFinder>;
915 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
917 TYPED_TEST(NeedleFinderTest, Null) {
918 { // null characters in the string
919 string s(10, char(0));
921 string delims("abc");
922 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
924 { // null characters in delim
926 string delims(10, char(0));
929 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
931 { // range not terminated by null character
932 string buf = "abcdefghijklmnopqrstuvwxyz";
933 StringPiece s(buf.data() + 5, 3);
934 StringPiece delims("z");
935 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
939 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
940 string delims(1000, 'b');
941 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
942 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
945 TYPED_TEST(NeedleFinderTest, Empty) {
948 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
949 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
950 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
953 TYPED_TEST(NeedleFinderTest, Unaligned) {
954 // works correctly even if input buffers are not 16-byte aligned
955 string s = "0123456789ABCDEFGH";
956 for (size_t i = 0; i < s.size(); ++i) {
957 StringPiece a(s.c_str() + i);
958 for (size_t j = 0; j < s.size(); ++j) {
959 StringPiece b(s.c_str() + j);
960 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
965 // for some algorithms (specifically those that create a set of needles),
966 // we check for the edge-case of _all_ possible needles being sought.
967 TYPED_TEST(NeedleFinderTest, Needles256) {
969 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
970 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
971 // make the size ~big to avoid any edge-case branches for tiny haystacks
972 const int haystackSize = 50;
973 for (int i = minValue; i <= maxValue; i++) { // <=
974 needles.push_back(i);
976 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
977 for (int i = minValue; i <= maxValue; i++) {
978 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
981 needles.append("these are redundant characters");
982 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
983 for (int i = minValue; i <= maxValue; i++) {
984 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
988 TYPED_TEST(NeedleFinderTest, Base) {
989 for (size_t i = 0; i < 32; ++i) {
990 for (int j = 0; j < 32; ++j) {
991 string s = string(i, 'X') + "abca" + string(i, 'X');
992 string delims = string(j, 'Y') + "a" + string(j, 'Y');
993 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
998 const size_t kPageSize = 4096;
999 // Updates contents so that any read accesses past the last byte will
1000 // cause a SIGSEGV. It accomplishes this by changing access to the page that
1001 // begins immediately after the end of the contents (as allocators and mmap()
1002 // all operate on page boundaries, this is a reasonable assumption).
1003 // This function will also initialize buf, which caller must free().
1004 void createProtectedBuf(StringPiece& contents, char** buf) {
1005 ASSERT_LE(contents.size(), kPageSize);
1006 char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
1007 if (pageAlignedBuf == nullptr) {
1010 // Protect the page after the first full page-aligned region of the
1012 mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
1013 size_t newBegin = kPageSize - contents.size();
1014 memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
1015 contents.reset(pageAlignedBuf + newBegin, contents.size());
1016 *buf = pageAlignedBuf;
1019 void freeProtectedBuf(char* buf) {
1020 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
1024 TYPED_TEST(NeedleFinderTest, NoSegFault) {
1025 const string base = string(32, 'a') + string("b");
1026 const string delims = string(32, 'c') + string("b");
1027 for (int i = 0; i <= 32; i++) {
1028 for (int j = 0; j <= 33; j++) {
1029 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1030 StringPiece s1(base);
1032 ASSERT_TRUE(!s1.empty());
1036 StringPiece s2(delims);
1040 createProtectedBuf(s1, &buf1);
1041 createProtectedBuf(s2, &buf2);
1042 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
1043 // string(s1.data(), s1.size()).c_str(), s1.size(),
1044 // string(s2.data(), s2.size()).c_str(), s2.size());
1045 auto r1 = this->find_first_byte_of(s1, s2);
1047 std::find_first_of(s1.begin(), s1.end(), s2.begin(), s2.end());
1048 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
1050 auto r2 = this->find_first_byte_of(s2, s1);
1052 std::find_first_of(s2.begin(), s2.end(), s1.begin(), s1.end());
1053 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1055 freeProtectedBuf(buf1);
1056 freeProtectedBuf(buf2);
1062 TEST(NonConstTest, StringPiece) {
1063 std::string hello("hello");
1064 MutableStringPiece sp(&hello.front(), hello.size());
1066 EXPECT_EQ("xello", hello);
1069 EXPECT_EQ("xello", s);
1073 MutableByteRange r2(sp);
1077 // Similar to the begin() template functions, but instread of returing
1078 // an iterator, return a pointer to data.
1079 template <class Container>
1080 typename Container::value_type* dataPtr(Container& cont) {
1081 // NOTE: &cont[0] is undefined if cont is empty (it creates a
1082 // reference to nullptr - which is not dereferenced, but still UBSAN).
1085 template <class T, size_t N>
1086 constexpr T* dataPtr(T (&arr)[N]) noexcept {
1091 void testRangeFunc(C&& x, size_t n) {
1093 // type, conversion checks
1094 Range<int*> r1 = range(std::forward<C>(x));
1095 Range<const int*> r2 = range(std::forward<C>(x));
1096 Range<const int*> r3 = range(cx);
1097 Range<const int*> r5 = range(std::move(cx));
1098 EXPECT_EQ(r1.begin(), dataPtr(x));
1099 EXPECT_EQ(r1.end(), dataPtr(x) + n);
1100 EXPECT_EQ(n, r1.size());
1101 EXPECT_EQ(n, r2.size());
1102 EXPECT_EQ(n, r3.size());
1103 EXPECT_EQ(n, r5.size());
1106 TEST(RangeFunc, Vector) {
1108 testRangeFunc(x, 0);
1110 testRangeFunc(x, 1);
1111 testRangeFunc(std::vector<int>{1, 2}, 2);
1114 TEST(RangeFunc, Array) {
1115 std::array<int, 3> x;
1116 testRangeFunc(x, 3);
1119 TEST(RangeFunc, CArray) {
1120 int x[]{1, 2, 3, 4};
1121 testRangeFunc(x, 4);
1124 TEST(RangeFunc, ConstexprCArray) {
1125 static constexpr const int numArray[4] = {3, 17, 1, 9};
1126 constexpr const auto numArrayRange = range(numArray);
1127 EXPECT_EQ(17, numArrayRange[1]);
1128 constexpr const auto numArrayRangeSize = numArrayRange.size();
1129 EXPECT_EQ(4, numArrayRangeSize);
1132 TEST(RangeFunc, ConstexprStdArray) {
1133 static constexpr const std::array<int, 4> numArray = {{3, 17, 1, 9}};
1134 constexpr const auto numArrayRange = range(numArray);
1135 EXPECT_EQ(17, numArrayRange[1]);
1136 constexpr const auto numArrayRangeSize = numArrayRange.size();
1137 EXPECT_EQ(4, numArrayRangeSize);
1140 TEST(RangeFunc, ConstexprStdArrayZero) {
1141 static constexpr const std::array<int, 0> numArray = {};
1142 constexpr const auto numArrayRange = range(numArray);
1143 constexpr const auto numArrayRangeSize = numArrayRange.size();
1144 EXPECT_EQ(0, numArrayRangeSize);
1147 TEST(RangeFunc, ConstexprIteratorPair) {
1148 static constexpr const int numArray[4] = {3, 17, 1, 9};
1149 constexpr const auto numPtr = static_cast<const int*>(numArray);
1150 constexpr const auto numIterRange = range(numPtr + 1, numPtr + 3);
1151 EXPECT_EQ(1, numIterRange[1]);
1152 constexpr const auto numIterRangeSize = numIterRange.size();
1153 EXPECT_EQ(2, numIterRangeSize);
1156 TEST(RangeFunc, ConstexprCollection) {
1157 class IntCollection {
1159 constexpr IntCollection(const int* d, size_t s) : data_(d), size_(s) {}
1160 constexpr const int* data() const {
1163 constexpr size_t size() const {
1171 static constexpr const int numArray[4] = {3, 17, 1, 9};
1172 constexpr const auto numPtr = static_cast<const int*>(numArray);
1173 constexpr const auto numColl = IntCollection(numPtr + 1, 2);
1174 constexpr const auto numCollRange = range(numColl);
1175 EXPECT_EQ(1, numCollRange[1]);
1176 constexpr const auto numCollRangeSize = numCollRange.size();
1177 EXPECT_EQ(2, numCollRangeSize);
1180 std::string get_rand_str(
1182 std::uniform_int_distribution<>& dist,
1183 std::mt19937& gen) {
1184 std::string ret(size, '\0');
1185 for (size_t i = 0; i < size; ++i) {
1186 ret[i] = static_cast<char>(dist(gen));
1193 bool operator==(MutableStringPiece mp, StringPiece sp) {
1194 return mp.compare(sp) == 0;
1197 bool operator==(StringPiece sp, MutableStringPiece mp) {
1198 return mp.compare(sp) == 0;
1200 } // namespace folly
1202 TEST(ReplaceAt, exhaustiveTest) {
1203 char input[] = "this is nice and long input";
1204 auto msp = MutableStringPiece(input);
1205 auto str = std::string(input);
1206 std::random_device rd;
1207 std::mt19937 gen(rd());
1208 std::uniform_int_distribution<> dist('a', 'z');
1210 for (int i = 0; i < 100; ++i) {
1211 for (size_t j = 1; j <= msp.size(); ++j) {
1212 auto replacement = get_rand_str(j, dist, gen);
1213 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1214 msp.replaceAt(pos, replacement);
1215 str.replace(pos, replacement.size(), replacement);
1216 EXPECT_EQ(msp.compare(str), 0);
1222 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1225 TEST(ReplaceAll, basicTest) {
1226 char input[] = "this is nice and long input";
1227 auto orig = std::string(input);
1228 auto msp = MutableStringPiece(input);
1230 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1231 EXPECT_EQ("thsi si nice and long input", msp);
1232 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1233 EXPECT_EQ(msp, orig);
1235 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1236 EXPECT_EQ(msp, orig);
1238 // at the very beginning
1239 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1240 EXPECT_EQ("siht is nice and long input", msp);
1241 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1242 EXPECT_EQ(msp, orig);
1245 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1246 EXPECT_EQ("this is nice and long soput", msp);
1247 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1248 EXPECT_EQ(msp, orig);
1251 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1252 EXPECT_EQ("this@is@nice@and@long@input", msp);
1253 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1254 EXPECT_EQ(msp, orig);
1257 TEST(ReplaceAll, randomTest) {
1258 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1259 auto orig = std::string(input);
1260 auto msp = MutableStringPiece(input);
1262 std::random_device rd;
1263 std::mt19937 gen(rd());
1264 std::uniform_int_distribution<> dist('A', 'Z');
1266 for (int i = 0; i < 100; ++i) {
1267 for (size_t j = 1; j <= orig.size(); ++j) {
1268 auto replacement = get_rand_str(j, dist, gen);
1269 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1270 auto piece = orig.substr(pos, j);
1271 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1272 EXPECT_EQ(msp.find(replacement), pos);
1273 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1274 EXPECT_EQ(msp, orig);
1280 TEST(ReplaceAll, BadArg) {
1282 auto fst = "longer";
1284 char input[] = "meh meh meh";
1285 auto all = MutableStringPiece(input);
1288 all.replaceAll(fst, snd);
1289 } catch (std::invalid_argument&) {
1294 all.replaceAll(snd, fst);
1295 } catch (std::invalid_argument&) {
1299 EXPECT_EQ(count, 2);
1302 TEST(Range, Constructors) {
1303 vector<int> c = {1, 2, 3};
1304 typedef Range<vector<int>::iterator> RangeType;
1305 typedef Range<vector<int>::const_iterator> ConstRangeType;
1306 RangeType cr(c.begin(), c.end());
1307 auto subpiece1 = ConstRangeType(cr, 1, 5);
1308 auto subpiece2 = ConstRangeType(cr, 1);
1309 EXPECT_EQ(subpiece1.size(), 2);
1310 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1311 EXPECT_EQ(subpiece1.end(), subpiece2.end());
1314 TEST(Range, ArrayConstructors) {
1315 auto charArray = std::array<char, 4>{{'t', 'e', 's', 't'}};
1316 auto constCharArray = std::array<char, 6>{{'f', 'o', 'o', 'b', 'a', 'r'}};
1317 auto emptyArray = std::array<char, 0>{};
1319 auto sp1 = StringPiece{charArray};
1320 EXPECT_EQ(4, sp1.size());
1321 EXPECT_EQ(charArray.data(), sp1.data());
1323 auto sp2 = StringPiece(constCharArray);
1324 EXPECT_EQ(6, sp2.size());
1325 EXPECT_EQ(constCharArray.data(), sp2.data());
1327 auto msp = MutableStringPiece(charArray);
1328 EXPECT_EQ(4, msp.size());
1329 EXPECT_EQ(charArray.data(), msp.data());
1331 auto esp = StringPiece(emptyArray);
1332 EXPECT_EQ(0, esp.size());
1333 EXPECT_EQ(nullptr, esp.data());
1335 auto emsp = MutableStringPiece(emptyArray);
1336 EXPECT_EQ(0, emsp.size());
1337 EXPECT_EQ(nullptr, emsp.data());
1339 static constexpr std::array<int, 4> numArray = {{3, 17, 1, 9}};
1340 constexpr auto numRange = Range<const int*>{numArray};
1341 EXPECT_EQ(17, numRange[1]);
1343 static constexpr std::array<int, 0> emptyNumArray{};
1344 constexpr auto emptyNumRange = Range<const int*>{emptyNumArray};
1345 EXPECT_EQ(0, emptyNumRange.size());
1348 TEST(Range, ConstexprAccessors) {
1349 constexpr StringPiece piece = range("hello");
1350 static_assert(piece.size() == 6u, "");
1351 static_assert(piece.end() - piece.begin() == 6u, "");
1352 static_assert(piece.data() == piece.begin(), "");
1353 static_assert(piece.start() == piece.begin(), "");
1354 static_assert(piece.cbegin() == piece.begin(), "");
1355 static_assert(piece.cend() == piece.end(), "");
1356 static_assert(*piece.begin() == 'h', "");
1357 static_assert(*(piece.end() - 1) == '\0', "");
1360 TEST(Range, LiteralSuffix) {
1361 constexpr auto literalPiece = "hello"_sp;
1362 constexpr StringPiece piece = "hello";
1363 EXPECT_EQ(literalPiece, piece);
1364 constexpr auto literalPiece8 = u8"hello"_sp;
1365 constexpr Range<char const*> piece8 = u8"hello";
1366 EXPECT_EQ(literalPiece8, piece8);
1367 constexpr auto literalPiece16 = u"hello"_sp;
1368 constexpr Range<char16_t const*> piece16{u"hello", 5};
1369 EXPECT_EQ(literalPiece16, piece16);
1370 constexpr auto literalPiece32 = U"hello"_sp;
1371 constexpr Range<char32_t const*> piece32{U"hello", 5};
1372 EXPECT_EQ(literalPiece32, piece32);
1373 constexpr auto literalPieceW = L"hello"_sp;
1374 constexpr Range<wchar_t const*> pieceW{L"hello", 5};
1375 EXPECT_EQ(literalPieceW, pieceW);