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/GMock.h>
34 #include <folly/portability/GTest.h>
35 #include <folly/portability/Memory.h>
36 #include <folly/portability/SysMman.h>
38 using namespace folly;
39 using namespace folly::detail;
42 static_assert(std::is_literal_type<StringPiece>::value, "");
44 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<StringPiece>));
46 TEST(StringPiece, All) {
47 const char* foo = "foo";
48 const char* foo2 = "foo";
52 // we expect the compiler to optimize things so that there's only one copy
53 // of the string literal "foo", even though we've got it in multiple places
54 EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
55 // comparison rather than lexical
57 // the string object creates copies though, so the c_str of these should be
59 EXPECT_NE(fooStr.c_str(), foo2Str.c_str());
61 // test the basic StringPiece functionality
63 EXPECT_EQ(s.size(), 3);
65 EXPECT_EQ(s.start(), foo); // ptr comparison
66 EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
67 EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
69 EXPECT_EQ(s.toString(), foo); // lexical comparison
70 EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
71 EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
73 EXPECT_EQ(s, foo); // lexical comparison
74 EXPECT_EQ(s, fooStr); // lexical comparison
75 EXPECT_EQ(s, foo2Str); // lexical comparison
78 // check using StringPiece to reference substrings
79 const char* foobarbaz = "foobarbaz";
81 // the full "foobarbaz"
82 s.reset(foobarbaz, strlen(foobarbaz));
83 EXPECT_EQ(s.size(), 9);
84 EXPECT_EQ(s.start(), foobarbaz);
85 EXPECT_EQ(s, "foobarbaz");
88 s.assign(foobarbaz, foobarbaz + 3);
89 EXPECT_EQ(s.size(), 3);
90 EXPECT_EQ(s.start(), foobarbaz);
94 s.reset(foobarbaz, strlen(foobarbaz));
95 EXPECT_EQ(s.find("bar"), 3);
96 EXPECT_EQ(s.find("ba", 3), 3);
97 EXPECT_EQ(s.find("ba", 4), 6);
98 EXPECT_EQ(s.find("notfound"), StringPiece::npos);
99 EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
100 EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
101 // starting pos that is obviously past the end -- This works for std::string
102 EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
103 EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
104 EXPECT_EQ(s.find("z", 55), StringPiece::npos);
106 EXPECT_EQ(s.find(""), std::string().find(""));
107 EXPECT_EQ(s.find(""), 0);
110 EXPECT_EQ(s.find('b'), 3);
111 EXPECT_EQ(s.find('b', 3), 3);
112 EXPECT_EQ(s.find('b', 4), 6);
113 EXPECT_EQ(s.find('o', 2), 2);
114 EXPECT_EQ(s.find('y'), StringPiece::npos);
115 EXPECT_EQ(s.find('y', 1), StringPiece::npos);
116 EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
117 EXPECT_TRUE(s.contains('z'));
118 // starting pos that is obviously past the end -- This works for std::string
119 EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
120 EXPECT_EQ(s.find('z', s.size()), StringPiece::npos);
121 EXPECT_EQ(s.find('z', 55), StringPiece::npos);
123 EXPECT_EQ(s.find('\0'), std::string().find('\0'));
124 EXPECT_EQ(s.find('\0'), StringPiece::npos);
125 EXPECT_FALSE(s.contains('\0'));
127 // single char rfinds
128 EXPECT_EQ(s.rfind('b'), 6);
129 EXPECT_EQ(s.rfind('y'), StringPiece::npos);
130 EXPECT_EQ(s.str().rfind('y'), StringPiece::npos);
131 EXPECT_EQ(ByteRange(s).rfind('b'), 6);
132 EXPECT_EQ(ByteRange(s).rfind('y'), StringPiece::npos);
134 EXPECT_EQ(s.rfind('\0'), s.str().rfind('\0'));
135 EXPECT_EQ(s.rfind('\0'), StringPiece::npos);
138 s.reset(foobarbaz, strlen(foobarbaz));
139 EXPECT_EQ(s.find_first_of("bar"), 3);
140 EXPECT_EQ(s.find_first_of("ba", 3), 3);
141 EXPECT_EQ(s.find_first_of("ba", 4), 4);
142 EXPECT_TRUE(s.contains("bar"));
143 EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos);
144 EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos);
145 EXPECT_FALSE(s.contains("xyxy"));
146 // starting position too far
147 EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos);
148 // starting pos that is obviously past the end -- This works for std::string
149 EXPECT_EQ(s.toString().find_first_of("xyxy", 55), StringPiece::npos);
150 EXPECT_EQ(s.find_first_of("z", s.size()), StringPiece::npos);
151 EXPECT_EQ(s.find_first_of("z", 55), StringPiece::npos);
152 // empty needle. Note that this returns npos, while find() returns 0!
153 EXPECT_EQ(s.find_first_of(""), std::string().find_first_of(""));
154 EXPECT_EQ(s.find_first_of(""), StringPiece::npos);
156 // single char find_first_ofs
157 EXPECT_EQ(s.find_first_of('b'), 3);
158 EXPECT_EQ(s.find_first_of('b', 3), 3);
159 EXPECT_EQ(s.find_first_of('b', 4), 6);
160 EXPECT_EQ(s.find_first_of('o', 2), 2);
161 EXPECT_EQ(s.find_first_of('y'), StringPiece::npos);
162 EXPECT_EQ(s.find_first_of('y', 1), StringPiece::npos);
163 // starting position too far
164 EXPECT_EQ(s.find_first_of('o', 4), StringPiece::npos);
165 // starting pos that is obviously past the end -- This works for std::string
166 EXPECT_EQ(s.toString().find_first_of('y', 55), StringPiece::npos);
167 EXPECT_EQ(s.find_first_of('z', s.size()), StringPiece::npos);
168 EXPECT_EQ(s.find_first_of('z', 55), StringPiece::npos);
170 EXPECT_EQ(s.find_first_of('\0'), std::string().find_first_of('\0'));
171 EXPECT_EQ(s.find_first_of('\0'), StringPiece::npos);
174 s.reset(foobarbaz + 3, strlen(foobarbaz + 3));
175 EXPECT_EQ(s.size(), 6);
176 EXPECT_EQ(s.start(), foobarbaz + 3);
177 EXPECT_EQ(s, "barbaz");
180 s.reset(foobarbaz + 3, 3);
181 EXPECT_EQ(s.size(), 3);
186 EXPECT_EQ(s.toString(), "");
188 // test an empty StringPiece
190 EXPECT_EQ(s2.size(), 0);
192 // Test comparison operators
226 void expectLT(const T& a, const T& b) {
229 EXPECT_FALSE(a == b);
230 EXPECT_FALSE(a >= b);
234 EXPECT_FALSE(b <= a);
240 void expectEQ(const T& a, const T& b) {
248 TEST(StringPiece, EightBitComparisons) {
249 char values[] = {'\x00', '\x20', '\x40', '\x7f', '\x80', '\xc0', '\xff'};
250 constexpr size_t count = sizeof(values) / sizeof(values[0]);
251 for (size_t i = 0; i < count; ++i) {
252 std::string a(1, values[i]);
253 // Defeat copy-on-write
254 std::string aCopy(a.data(), a.size());
256 expectEQ(StringPiece(a), StringPiece(aCopy));
258 for (size_t j = i + 1; j < count; ++j) {
259 std::string b(1, values[j]);
261 expectLT(StringPiece(a), StringPiece(b));
266 TEST(StringPiece, ToByteRange) {
267 StringPiece a("hello");
270 static_cast<const void*>(a.begin()), static_cast<const void*>(b.begin()));
272 static_cast<const void*>(a.end()), static_cast<const void*>(b.end()));
274 // and convert back again
276 EXPECT_EQ(a.begin(), c.begin());
277 EXPECT_EQ(a.end(), c.end());
280 TEST(StringPiece, InvalidRange) {
281 StringPiece a("hello");
282 EXPECT_EQ(a, a.subpiece(0, 10));
283 EXPECT_EQ(StringPiece("ello"), a.subpiece(1));
284 EXPECT_EQ(StringPiece("ello"), a.subpiece(1, std::string::npos));
285 EXPECT_EQ(StringPiece("ell"), a.subpiece(1, 3));
286 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
287 EXPECT_THROW(a.subpiece(6), std::out_of_range);
289 std::string b("hello");
290 EXPECT_EQ(a, StringPiece(b, 0, 10));
291 EXPECT_EQ("ello", a.subpiece(1));
292 EXPECT_EQ("ello", a.subpiece(1, std::string::npos));
293 EXPECT_EQ("ell", a.subpiece(1, 3));
294 EXPECT_THROW(a.subpiece(6, 7), std::out_of_range);
295 EXPECT_THROW(a.subpiece(6), std::out_of_range);
298 TEST(StringPiece, Constexpr) {
299 constexpr const char* helloArray = "hello";
301 constexpr StringPiece hello1("hello");
302 EXPECT_EQ("hello", hello1);
303 static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
305 constexpr StringPiece hello2(helloArray);
306 EXPECT_EQ("hello", hello2);
307 static_assert(hello2.size() == 5, "hello size should be 5 at compile time");
310 TEST(StringPiece, Prefix) {
311 StringPiece a("hello");
312 EXPECT_TRUE(a.startsWith(""));
313 EXPECT_TRUE(a.startsWith("h"));
314 EXPECT_TRUE(a.startsWith('h'));
315 EXPECT_TRUE(a.startsWith("hello"));
316 EXPECT_FALSE(a.startsWith("hellox"));
317 EXPECT_FALSE(a.startsWith('x'));
318 EXPECT_FALSE(a.startsWith("x"));
320 EXPECT_TRUE(a.startsWith("", folly::AsciiCaseInsensitive()));
321 EXPECT_TRUE(a.startsWith("hello", folly::AsciiCaseInsensitive()));
322 EXPECT_TRUE(a.startsWith("hellO", folly::AsciiCaseInsensitive()));
323 EXPECT_TRUE(a.startsWith("HELL", folly::AsciiCaseInsensitive()));
324 EXPECT_TRUE(a.startsWith("H", folly::AsciiCaseInsensitive()));
325 EXPECT_FALSE(a.startsWith("HELLOX", folly::AsciiCaseInsensitive()));
326 EXPECT_FALSE(a.startsWith("x", folly::AsciiCaseInsensitive()));
327 EXPECT_FALSE(a.startsWith("X", folly::AsciiCaseInsensitive()));
331 EXPECT_TRUE(b.removePrefix(""));
332 EXPECT_EQ("hello", b);
336 EXPECT_TRUE(b.removePrefix("h"));
337 EXPECT_EQ("ello", b);
341 EXPECT_TRUE(b.removePrefix('h'));
342 EXPECT_EQ("ello", b);
346 EXPECT_TRUE(b.removePrefix("hello"));
351 EXPECT_FALSE(b.removePrefix("hellox"));
352 EXPECT_EQ("hello", b);
356 EXPECT_FALSE(b.removePrefix("x"));
357 EXPECT_EQ("hello", b);
361 EXPECT_FALSE(b.removePrefix('x'));
362 EXPECT_EQ("hello", b);
366 TEST(StringPiece, Suffix) {
367 StringPiece a("hello");
368 EXPECT_TRUE(a.endsWith(""));
369 EXPECT_TRUE(a.endsWith("o"));
370 EXPECT_TRUE(a.endsWith('o'));
371 EXPECT_TRUE(a.endsWith("hello"));
372 EXPECT_FALSE(a.endsWith("xhello"));
373 EXPECT_FALSE(a.endsWith("x"));
374 EXPECT_FALSE(a.endsWith('x'));
376 EXPECT_TRUE(a.endsWith("", folly::AsciiCaseInsensitive()));
377 EXPECT_TRUE(a.endsWith("o", folly::AsciiCaseInsensitive()));
378 EXPECT_TRUE(a.endsWith("O", folly::AsciiCaseInsensitive()));
379 EXPECT_TRUE(a.endsWith("hello", folly::AsciiCaseInsensitive()));
380 EXPECT_TRUE(a.endsWith("hellO", folly::AsciiCaseInsensitive()));
381 EXPECT_FALSE(a.endsWith("xhello", folly::AsciiCaseInsensitive()));
382 EXPECT_FALSE(a.endsWith("Xhello", folly::AsciiCaseInsensitive()));
383 EXPECT_FALSE(a.endsWith("x", folly::AsciiCaseInsensitive()));
384 EXPECT_FALSE(a.endsWith("X", folly::AsciiCaseInsensitive()));
388 EXPECT_TRUE(b.removeSuffix(""));
389 EXPECT_EQ("hello", b);
393 EXPECT_TRUE(b.removeSuffix("o"));
394 EXPECT_EQ("hell", b);
398 EXPECT_TRUE(b.removeSuffix('o'));
399 EXPECT_EQ("hell", b);
403 EXPECT_TRUE(b.removeSuffix("hello"));
408 EXPECT_FALSE(b.removeSuffix("xhello"));
409 EXPECT_EQ("hello", b);
413 EXPECT_FALSE(b.removeSuffix("x"));
414 EXPECT_EQ("hello", b);
418 EXPECT_FALSE(b.removeSuffix('x'));
419 EXPECT_EQ("hello", b);
423 TEST(StringPiece, Equals) {
424 StringPiece a("hello");
426 EXPECT_TRUE(a.equals("HELLO", AsciiCaseInsensitive()));
427 EXPECT_FALSE(a.equals("HELLOX", AsciiCaseInsensitive()));
430 TEST(StringPiece, PrefixEmpty) {
432 EXPECT_TRUE(a.startsWith(""));
433 EXPECT_FALSE(a.startsWith("a"));
434 EXPECT_FALSE(a.startsWith('a'));
435 EXPECT_TRUE(a.removePrefix(""));
437 EXPECT_FALSE(a.removePrefix("a"));
439 EXPECT_FALSE(a.removePrefix('a'));
443 TEST(StringPiece, SuffixEmpty) {
445 EXPECT_TRUE(a.endsWith(""));
446 EXPECT_FALSE(a.endsWith("a"));
447 EXPECT_FALSE(a.endsWith('a'));
448 EXPECT_TRUE(a.removeSuffix(""));
450 EXPECT_FALSE(a.removeSuffix("a"));
452 EXPECT_FALSE(a.removeSuffix('a'));
456 TEST(StringPiece, erase) {
457 StringPiece a("hello");
461 EXPECT_EQ("ello", a);
470 EXPECT_THROW(a.erase(b, e), std::out_of_range);
489 boost::algorithm::trim(a);
490 EXPECT_EQ(a, "hello");
493 TEST(StringPiece, split_step_char_delimiter) {
495 // 012345678901234567890123456
496 auto const s = "this is just a test string";
497 auto const e = std::next(s, std::strlen(s));
500 folly::StringPiece p(s);
501 EXPECT_EQ(s, p.begin());
502 EXPECT_EQ(e, p.end());
505 auto x = p.split_step(' ');
506 EXPECT_EQ(std::next(s, 5), p.begin());
507 EXPECT_EQ(e, p.end());
508 EXPECT_EQ("this", x);
510 x = p.split_step(' ');
511 EXPECT_EQ(std::next(s, 8), p.begin());
512 EXPECT_EQ(e, p.end());
515 x = p.split_step('u');
516 EXPECT_EQ(std::next(s, 10), p.begin());
517 EXPECT_EQ(e, p.end());
520 x = p.split_step(' ');
521 EXPECT_EQ(std::next(s, 13), p.begin());
522 EXPECT_EQ(e, p.end());
525 x = p.split_step(' ');
526 EXPECT_EQ(std::next(s, 14), p.begin());
527 EXPECT_EQ(e, p.end());
530 x = p.split_step(' ');
531 EXPECT_EQ(std::next(s, 16), p.begin());
532 EXPECT_EQ(e, p.end());
535 x = p.split_step(' ');
536 EXPECT_EQ(std::next(s, 21), p.begin());
537 EXPECT_EQ(e, p.end());
538 EXPECT_EQ("test", x);
540 x = p.split_step(' ');
541 EXPECT_EQ(e, p.begin());
542 EXPECT_EQ(e, p.end());
543 EXPECT_EQ("string", x);
545 x = p.split_step(' ');
546 EXPECT_EQ(e, p.begin());
547 EXPECT_EQ(e, p.end());
551 TEST(StringPiece, split_step_range_delimiter) {
553 // 0123456789012345678901234567890123
554 auto const s = "this is just a test string";
555 auto const e = std::next(s, std::strlen(s));
558 folly::StringPiece p(s);
559 EXPECT_EQ(s, p.begin());
560 EXPECT_EQ(e, p.end());
563 auto x = p.split_step(" ");
564 EXPECT_EQ(std::next(s, 6), p.begin());
565 EXPECT_EQ(e, p.end());
566 EXPECT_EQ("this", x);
568 x = p.split_step(" ");
569 EXPECT_EQ(std::next(s, 10), p.begin());
570 EXPECT_EQ(e, p.end());
573 x = p.split_step("u");
574 EXPECT_EQ(std::next(s, 12), p.begin());
575 EXPECT_EQ(e, p.end());
578 x = p.split_step(" ");
579 EXPECT_EQ(std::next(s, 16), p.begin());
580 EXPECT_EQ(e, p.end());
583 x = p.split_step(" ");
584 EXPECT_EQ(std::next(s, 18), p.begin());
585 EXPECT_EQ(e, p.end());
588 x = p.split_step(" ");
589 EXPECT_EQ(std::next(s, 21), p.begin());
590 EXPECT_EQ(e, p.end());
593 x = p.split_step(" ");
594 EXPECT_EQ(std::next(s, 28), p.begin());
595 EXPECT_EQ(e, p.end());
596 EXPECT_EQ(" test", x);
598 x = p.split_step(" ");
599 EXPECT_EQ(e, p.begin());
600 EXPECT_EQ(e, p.end());
601 EXPECT_EQ("string", x);
603 x = p.split_step(" ");
604 EXPECT_EQ(e, p.begin());
605 EXPECT_EQ(e, p.end());
608 x = p.split_step(" ");
609 EXPECT_EQ(e, p.begin());
610 EXPECT_EQ(e, p.end());
614 void split_step_with_process_noop(folly::StringPiece) {}
616 TEST(StringPiece, split_step_with_process_char_delimiter) {
618 // 012345678901234567890123456
619 auto const s = "this is just a test string";
620 auto const e = std::next(s, std::strlen(s));
623 folly::StringPiece p(s);
624 EXPECT_EQ(s, p.begin());
625 EXPECT_EQ(e, p.end());
628 EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
629 EXPECT_EQ(std::next(s, 5), p.begin());
630 EXPECT_EQ(e, p.end());
631 EXPECT_EQ("this", x);
635 EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
636 EXPECT_EQ(std::next(s, 8), p.begin());
637 EXPECT_EQ(e, p.end());
642 EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
643 EXPECT_EQ(std::next(s, 10), p.begin());
644 EXPECT_EQ(e, p.end());
649 EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
650 EXPECT_EQ(std::next(s, 13), p.begin());
651 EXPECT_EQ(e, p.end());
656 EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
657 EXPECT_EQ(std::next(s, 14), p.begin());
658 EXPECT_EQ(e, p.end());
663 EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
664 EXPECT_EQ(std::next(s, 16), p.begin());
665 EXPECT_EQ(e, p.end());
670 EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
671 EXPECT_EQ(std::next(s, 21), p.begin());
672 EXPECT_EQ(e, p.end());
673 EXPECT_EQ("test", x);
677 EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
678 EXPECT_EQ(e, p.begin());
679 EXPECT_EQ(e, p.end());
680 EXPECT_EQ("string", x);
684 EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
685 EXPECT_EQ(e, p.begin());
686 EXPECT_EQ(e, p.end());
694 decltype(p.split_step(' ', split_step_with_process_noop))>::value));
696 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
699 TEST(StringPiece, split_step_with_process_range_delimiter) {
701 // 0123456789012345678901234567890123
702 auto const s = "this is just a test string";
703 auto const e = std::next(s, std::strlen(s));
706 folly::StringPiece p(s);
707 EXPECT_EQ(s, p.begin());
708 EXPECT_EQ(e, p.end());
711 EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
712 EXPECT_EQ(std::next(s, 6), p.begin());
713 EXPECT_EQ(e, p.end());
714 EXPECT_EQ("this", x);
718 EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
719 EXPECT_EQ(std::next(s, 10), p.begin());
720 EXPECT_EQ(e, p.end());
725 EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
726 EXPECT_EQ(std::next(s, 12), p.begin());
727 EXPECT_EQ(e, p.end());
732 EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
733 EXPECT_EQ(std::next(s, 16), p.begin());
734 EXPECT_EQ(e, p.end());
739 EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
740 EXPECT_EQ(std::next(s, 18), p.begin());
741 EXPECT_EQ(e, p.end());
746 EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
747 EXPECT_EQ(std::next(s, 21), p.begin());
748 EXPECT_EQ(e, p.end());
753 EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
754 EXPECT_EQ(std::next(s, 28), p.begin());
755 EXPECT_EQ(e, p.end());
756 EXPECT_EQ(" test", x);
760 EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
761 EXPECT_EQ(e, p.begin());
762 EXPECT_EQ(e, p.end());
763 EXPECT_EQ("string", x);
767 EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
768 EXPECT_EQ(e, p.begin());
769 EXPECT_EQ(e, p.end());
774 EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
775 EXPECT_EQ(e, p.begin());
776 EXPECT_EQ(e, p.end());
784 decltype(p.split_step(' ', split_step_with_process_noop))>::value));
786 EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
789 TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
791 // 012345678901234567890123456
792 auto const s = "this is just a test string";
793 auto const e = std::next(s, std::strlen(s));
794 auto const delimiter = ' ';
797 folly::StringPiece p(s);
798 EXPECT_EQ(s, p.begin());
799 EXPECT_EQ(e, p.end());
802 auto const functor = [](folly::StringPiece s, folly::StringPiece expected) {
803 EXPECT_EQ(expected, s);
807 auto const checker = [&](folly::StringPiece expected) {
808 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
821 EXPECT_TRUE(p.empty());
824 TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
826 // 0123456789012345678901234567890123
827 auto const s = "this is just a test string";
828 auto const e = std::next(s, std::strlen(s));
829 auto const delimiter = " ";
832 folly::StringPiece p(s);
833 EXPECT_EQ(s, p.begin());
834 EXPECT_EQ(e, p.end());
837 auto const functor = [](folly::StringPiece s, folly::StringPiece expected) {
838 EXPECT_EQ(expected, s);
842 auto const checker = [&](folly::StringPiece expected) {
843 EXPECT_EQ(expected, p.split_step(delimiter, functor, expected));
856 EXPECT_TRUE(p.empty());
859 TEST(StringPiece, NoInvalidImplicitConversions) {
861 bool operator()(folly::Range<int*>) {
864 bool operator()(folly::StringPiece) {
869 std::string s = "hello";
870 EXPECT_TRUE(IsString()(s));
873 TEST(qfind, UInt32_Ranges) {
874 vector<uint32_t> a({1, 2, 3, 260, 5});
875 vector<uint32_t> b({2, 3, 4});
877 auto a_range = folly::Range<const uint32_t*>(&a[0], a.size());
878 auto b_range = folly::Range<const uint32_t*>(&b[0], b.size());
880 EXPECT_EQ(qfind(a_range, b_range), string::npos);
883 EXPECT_EQ(qfind(a_range, b_range), 1);
886 template <typename NeedleFinder>
887 class NeedleFinderTest : public ::testing::Test {
889 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
890 return NeedleFinder::find_first_byte_of(haystack, needles);
894 struct SseNeedleFinder {
895 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
896 // This will only use the SSE version if it is supported on this CPU
897 // (selected using ifunc).
898 return detail::qfind_first_byte_of(haystack, needles);
902 struct NoSseNeedleFinder {
903 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
904 return detail::qfind_first_byte_of_nosse(haystack, needles);
908 struct ByteSetNeedleFinder {
909 static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
910 return detail::qfind_first_byte_of_byteset(haystack, needles);
914 using NeedleFinders =
915 ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, ByteSetNeedleFinder>;
916 TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
918 TYPED_TEST(NeedleFinderTest, Null) {
919 { // null characters in the string
920 string s(10, char(0));
922 string delims("abc");
923 EXPECT_EQ(5, this->find_first_byte_of(s, delims));
925 { // null characters in delim
927 string delims(10, char(0));
930 EXPECT_EQ(1, this->find_first_byte_of(s, delims));
932 { // range not terminated by null character
933 string buf = "abcdefghijklmnopqrstuvwxyz";
934 StringPiece s(buf.data() + 5, 3);
935 StringPiece delims("z");
936 EXPECT_EQ(string::npos, this->find_first_byte_of(s, delims));
940 TYPED_TEST(NeedleFinderTest, DelimDuplicates) {
941 string delims(1000, 'b');
942 EXPECT_EQ(1, this->find_first_byte_of("abc", delims));
943 EXPECT_EQ(string::npos, this->find_first_byte_of("ac", delims));
946 TYPED_TEST(NeedleFinderTest, Empty) {
949 EXPECT_EQ(string::npos, this->find_first_byte_of(a, b));
950 EXPECT_EQ(string::npos, this->find_first_byte_of(b, a));
951 EXPECT_EQ(string::npos, this->find_first_byte_of(b, b));
954 TYPED_TEST(NeedleFinderTest, Unaligned) {
955 // works correctly even if input buffers are not 16-byte aligned
956 string s = "0123456789ABCDEFGH";
957 for (size_t i = 0; i < s.size(); ++i) {
958 StringPiece a(s.c_str() + i);
959 for (size_t j = 0; j < s.size(); ++j) {
960 StringPiece b(s.c_str() + j);
961 EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
966 // for some algorithms (specifically those that create a set of needles),
967 // we check for the edge-case of _all_ possible needles being sought.
968 TYPED_TEST(NeedleFinderTest, Needles256) {
970 const auto minValue = std::numeric_limits<StringPiece::value_type>::min();
971 const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
972 // make the size ~big to avoid any edge-case branches for tiny haystacks
973 const int haystackSize = 50;
974 for (int i = minValue; i <= maxValue; i++) { // <=
975 needles.push_back(i);
977 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
978 for (int i = minValue; i <= maxValue; i++) {
979 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
982 needles.append("these are redundant characters");
983 EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
984 for (int i = minValue; i <= maxValue; i++) {
985 EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
989 TYPED_TEST(NeedleFinderTest, Base) {
990 for (size_t i = 0; i < 32; ++i) {
991 for (int j = 0; j < 32; ++j) {
992 string s = string(i, 'X') + "abca" + string(i, 'X');
993 string delims = string(j, 'Y') + "a" + string(j, 'Y');
994 EXPECT_EQ(i, this->find_first_byte_of(s, delims));
999 const size_t kPageSize = 4096;
1000 // Updates contents so that any read accesses past the last byte will
1001 // cause a SIGSEGV. It accomplishes this by changing access to the page that
1002 // begins immediately after the end of the contents (as allocators and mmap()
1003 // all operate on page boundaries, this is a reasonable assumption).
1004 // This function will also initialize buf, which caller must free().
1005 void createProtectedBuf(StringPiece& contents, char** buf) {
1006 ASSERT_LE(contents.size(), kPageSize);
1007 char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
1008 if (pageAlignedBuf == nullptr) {
1011 // Protect the page after the first full page-aligned region of the
1013 mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
1014 size_t newBegin = kPageSize - contents.size();
1015 memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
1016 contents.reset(pageAlignedBuf + newBegin, contents.size());
1017 *buf = pageAlignedBuf;
1020 void freeProtectedBuf(char* buf) {
1021 mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
1025 TYPED_TEST(NeedleFinderTest, NoSegFault) {
1026 const string base = string(32, 'a') + string("b");
1027 const string delims = string(32, 'c') + string("b");
1028 for (int i = 0; i <= 32; i++) {
1029 for (int j = 0; j <= 33; j++) {
1030 for (int shouldFind = 0; shouldFind <= 1; ++shouldFind) {
1031 StringPiece s1(base);
1033 ASSERT_TRUE(!s1.empty());
1037 StringPiece s2(delims);
1041 createProtectedBuf(s1, &buf1);
1042 createProtectedBuf(s2, &buf2);
1043 // printf("s1: '%s' (%ld) \ts2: '%s' (%ld)\n",
1044 // string(s1.data(), s1.size()).c_str(), s1.size(),
1045 // string(s2.data(), s2.size()).c_str(), s2.size());
1046 auto r1 = this->find_first_byte_of(s1, s2);
1048 std::find_first_of(s1.begin(), s1.end(), s2.begin(), s2.end());
1049 auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
1051 auto r2 = this->find_first_byte_of(s2, s1);
1053 std::find_first_of(s2.begin(), s2.end(), s1.begin(), s1.end());
1054 auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
1056 freeProtectedBuf(buf1);
1057 freeProtectedBuf(buf2);
1063 TEST(NonConstTest, StringPiece) {
1064 std::string hello("hello");
1065 MutableStringPiece sp(&hello.front(), hello.size());
1067 EXPECT_EQ("xello", hello);
1070 EXPECT_EQ("xello", s);
1074 MutableByteRange r2(sp);
1078 // Similar to the begin() template functions, but instread of returing
1079 // an iterator, return a pointer to data.
1080 template <class Container>
1081 typename Container::value_type* dataPtr(Container& cont) {
1082 // NOTE: &cont[0] is undefined if cont is empty (it creates a
1083 // reference to nullptr - which is not dereferenced, but still UBSAN).
1086 template <class T, size_t N>
1087 constexpr T* dataPtr(T (&arr)[N]) noexcept {
1092 void testRangeFunc(C&& x, size_t n) {
1094 // type, conversion checks
1096 _t<std::conditional<_t<std::is_reference<C>>::value, int*, int const*>>;
1097 Range<R1Iter> r1 = range(std::forward<C>(x));
1098 Range<const int*> r2 = range(std::forward<C>(x));
1099 Range<const int*> r3 = range(cx);
1100 Range<const int*> r5 = range(std::move(cx));
1101 EXPECT_EQ(r1.begin(), dataPtr(x));
1102 EXPECT_EQ(r1.end(), dataPtr(x) + n);
1103 EXPECT_EQ(n, r1.size());
1104 EXPECT_EQ(n, r2.size());
1105 EXPECT_EQ(n, r3.size());
1106 EXPECT_EQ(n, r5.size());
1109 TEST(RangeFunc, Vector) {
1111 testRangeFunc(x, 0);
1113 testRangeFunc(x, 1);
1114 testRangeFunc(std::vector<int>{1, 2}, 2);
1117 TEST(RangeFunc, Array) {
1118 std::array<int, 3> x;
1119 testRangeFunc(x, 3);
1122 TEST(RangeFunc, CArray) {
1123 int x[]{1, 2, 3, 4};
1124 testRangeFunc(x, 4);
1127 TEST(RangeFunc, ConstexprCArray) {
1128 static constexpr const int numArray[4] = {3, 17, 1, 9};
1129 constexpr const auto numArrayRange = range(numArray);
1130 EXPECT_EQ(17, numArrayRange[1]);
1131 constexpr const auto numArrayRangeSize = numArrayRange.size();
1132 EXPECT_EQ(4, numArrayRangeSize);
1135 TEST(RangeFunc, ConstexprStdArray) {
1136 static constexpr const std::array<int, 4> numArray = {{3, 17, 1, 9}};
1137 constexpr const auto numArrayRange = range(numArray);
1138 EXPECT_EQ(17, numArrayRange[1]);
1139 constexpr const auto numArrayRangeSize = numArrayRange.size();
1140 EXPECT_EQ(4, numArrayRangeSize);
1143 TEST(RangeFunc, ConstexprStdArrayZero) {
1144 static constexpr const std::array<int, 0> numArray = {};
1145 constexpr const auto numArrayRange = range(numArray);
1146 constexpr const auto numArrayRangeSize = numArrayRange.size();
1147 EXPECT_EQ(0, numArrayRangeSize);
1150 TEST(RangeFunc, ConstexprIteratorPair) {
1151 static constexpr const int numArray[4] = {3, 17, 1, 9};
1152 constexpr const auto numPtr = static_cast<const int*>(numArray);
1153 constexpr const auto numIterRange = range(numPtr + 1, numPtr + 3);
1154 EXPECT_EQ(1, numIterRange[1]);
1155 constexpr const auto numIterRangeSize = numIterRange.size();
1156 EXPECT_EQ(2, numIterRangeSize);
1159 TEST(RangeFunc, ConstexprCollection) {
1160 class IntCollection {
1162 constexpr IntCollection(const int* d, size_t s) : data_(d), size_(s) {}
1163 constexpr const int* data() const {
1166 constexpr size_t size() const {
1174 static constexpr const int numArray[4] = {3, 17, 1, 9};
1175 constexpr const auto numPtr = static_cast<const int*>(numArray);
1176 constexpr const auto numColl = IntCollection(numPtr + 1, 2);
1177 constexpr const auto numCollRange = range(numColl);
1178 EXPECT_EQ(1, numCollRange[1]);
1179 constexpr const auto numCollRangeSize = numCollRange.size();
1180 EXPECT_EQ(2, numCollRangeSize);
1183 TEST(CRangeFunc, CArray) {
1184 int numArray[4] = {3, 17, 1, 9};
1185 auto const numArrayRange = crange(numArray);
1187 (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
1188 EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
1191 TEST(CRangeFunc, StdArray) {
1192 std::array<int, 4> numArray = {{3, 17, 1, 9}};
1193 auto const numArrayRange = crange(numArray);
1195 (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
1196 EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
1199 TEST(CRangeFunc, StdArrayZero) {
1200 std::array<int, 0> numArray = {};
1201 auto const numArrayRange = crange(numArray);
1203 (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
1204 EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
1207 TEST(CRangeFunc, Collection) {
1208 class IntCollection {
1210 constexpr IntCollection(int* d, size_t s) : data_(d), size_(s) {}
1211 constexpr int* data() {
1214 constexpr int const* data() const {
1217 constexpr size_t size() const {
1225 int numArray[4] = {3, 17, 1, 9};
1226 auto numPtr = static_cast<int*>(numArray);
1227 auto numColl = IntCollection(numPtr + 1, 2);
1228 auto const numCollRange = crange(numColl);
1230 (std::is_same<int const*, decltype(numCollRange)::iterator>::value));
1231 EXPECT_THAT(numCollRange, testing::ElementsAreArray({17, 1}));
1234 std::string get_rand_str(
1236 std::uniform_int_distribution<>& dist,
1237 std::mt19937& gen) {
1238 std::string ret(size, '\0');
1239 for (size_t i = 0; i < size; ++i) {
1240 ret[i] = static_cast<char>(dist(gen));
1247 bool operator==(MutableStringPiece mp, StringPiece sp) {
1248 return mp.compare(sp) == 0;
1251 bool operator==(StringPiece sp, MutableStringPiece mp) {
1252 return mp.compare(sp) == 0;
1254 } // namespace folly
1256 TEST(ReplaceAt, exhaustiveTest) {
1257 char input[] = "this is nice and long input";
1258 auto msp = MutableStringPiece(input);
1259 auto str = std::string(input);
1260 std::random_device rd;
1261 std::mt19937 gen(rd());
1262 std::uniform_int_distribution<> dist('a', 'z');
1264 for (int i = 0; i < 100; ++i) {
1265 for (size_t j = 1; j <= msp.size(); ++j) {
1266 auto replacement = get_rand_str(j, dist, gen);
1267 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1268 msp.replaceAt(pos, replacement);
1269 str.replace(pos, replacement.size(), replacement);
1270 EXPECT_EQ(msp.compare(str), 0);
1276 EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
1279 TEST(ReplaceAll, basicTest) {
1280 char input[] = "this is nice and long input";
1281 auto orig = std::string(input);
1282 auto msp = MutableStringPiece(input);
1284 EXPECT_EQ(msp.replaceAll("is", "si"), 2);
1285 EXPECT_EQ("thsi si nice and long input", msp);
1286 EXPECT_EQ(msp.replaceAll("si", "is"), 2);
1287 EXPECT_EQ(msp, orig);
1289 EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
1290 EXPECT_EQ(msp, orig);
1292 // at the very beginning
1293 EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
1294 EXPECT_EQ("siht is nice and long input", msp);
1295 EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
1296 EXPECT_EQ(msp, orig);
1299 EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
1300 EXPECT_EQ("this is nice and long soput", msp);
1301 EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
1302 EXPECT_EQ(msp, orig);
1305 EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
1306 EXPECT_EQ("this@is@nice@and@long@input", msp);
1307 EXPECT_EQ(msp.replaceAll("@", " "), 5);
1308 EXPECT_EQ(msp, orig);
1311 TEST(ReplaceAll, randomTest) {
1312 char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
1313 auto orig = std::string(input);
1314 auto msp = MutableStringPiece(input);
1316 std::random_device rd;
1317 std::mt19937 gen(rd());
1318 std::uniform_int_distribution<> dist('A', 'Z');
1320 for (int i = 0; i < 100; ++i) {
1321 for (size_t j = 1; j <= orig.size(); ++j) {
1322 auto replacement = get_rand_str(j, dist, gen);
1323 for (size_t pos = 0; pos < msp.size() - j; ++pos) {
1324 auto piece = orig.substr(pos, j);
1325 EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
1326 EXPECT_EQ(msp.find(replacement), pos);
1327 EXPECT_EQ(msp.replaceAll(replacement, piece), 1);
1328 EXPECT_EQ(msp, orig);
1334 TEST(ReplaceAll, BadArg) {
1336 auto fst = "longer";
1338 char input[] = "meh meh meh";
1339 auto all = MutableStringPiece(input);
1342 all.replaceAll(fst, snd);
1343 } catch (std::invalid_argument&) {
1348 all.replaceAll(snd, fst);
1349 } catch (std::invalid_argument&) {
1353 EXPECT_EQ(count, 2);
1356 TEST(Range, Constructors) {
1357 vector<int> c = {1, 2, 3};
1358 typedef Range<vector<int>::iterator> RangeType;
1359 typedef Range<vector<int>::const_iterator> ConstRangeType;
1360 RangeType cr(c.begin(), c.end());
1361 auto subpiece1 = ConstRangeType(cr, 1, 5);
1362 auto subpiece2 = ConstRangeType(cr, 1);
1363 EXPECT_EQ(subpiece1.size(), 2);
1364 EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
1365 EXPECT_EQ(subpiece1.end(), subpiece2.end());
1368 TEST(Range, ArrayConstructors) {
1369 auto charArray = std::array<char, 4>{{'t', 'e', 's', 't'}};
1370 auto constCharArray = std::array<char, 6>{{'f', 'o', 'o', 'b', 'a', 'r'}};
1371 auto emptyArray = std::array<char, 0>{};
1373 auto sp1 = StringPiece{charArray};
1374 EXPECT_EQ(4, sp1.size());
1375 EXPECT_EQ(charArray.data(), sp1.data());
1377 auto sp2 = StringPiece(constCharArray);
1378 EXPECT_EQ(6, sp2.size());
1379 EXPECT_EQ(constCharArray.data(), sp2.data());
1381 auto msp = MutableStringPiece(charArray);
1382 EXPECT_EQ(4, msp.size());
1383 EXPECT_EQ(charArray.data(), msp.data());
1385 auto esp = StringPiece(emptyArray);
1386 EXPECT_EQ(0, esp.size());
1387 EXPECT_EQ(nullptr, esp.data());
1389 auto emsp = MutableStringPiece(emptyArray);
1390 EXPECT_EQ(0, emsp.size());
1391 EXPECT_EQ(nullptr, emsp.data());
1393 static constexpr std::array<int, 4> numArray = {{3, 17, 1, 9}};
1394 constexpr auto numRange = Range<const int*>{numArray};
1395 EXPECT_EQ(17, numRange[1]);
1397 static constexpr std::array<int, 0> emptyNumArray{};
1398 constexpr auto emptyNumRange = Range<const int*>{emptyNumArray};
1399 EXPECT_EQ(0, emptyNumRange.size());
1402 TEST(Range, ConstexprAccessors) {
1403 constexpr StringPiece piece = range("hello");
1404 static_assert(piece.size() == 6u, "");
1405 static_assert(piece.end() - piece.begin() == 6u, "");
1406 static_assert(piece.data() == piece.begin(), "");
1407 static_assert(piece.start() == piece.begin(), "");
1408 static_assert(piece.cbegin() == piece.begin(), "");
1409 static_assert(piece.cend() == piece.end(), "");
1410 static_assert(*piece.begin() == 'h', "");
1411 static_assert(*(piece.end() - 1) == '\0', "");
1414 TEST(Range, LiteralSuffix) {
1415 constexpr auto literalPiece = "hello"_sp;
1416 constexpr StringPiece piece = "hello";
1417 EXPECT_EQ(literalPiece, piece);
1418 constexpr auto literalPiece8 = u8"hello"_sp;
1419 constexpr Range<char const*> piece8 = u8"hello";
1420 EXPECT_EQ(literalPiece8, piece8);
1421 constexpr auto literalPiece16 = u"hello"_sp;
1422 constexpr Range<char16_t const*> piece16{u"hello", 5};
1423 EXPECT_EQ(literalPiece16, piece16);
1424 constexpr auto literalPiece32 = U"hello"_sp;
1425 constexpr Range<char32_t const*> piece32{U"hello", 5};
1426 EXPECT_EQ(literalPiece32, piece32);
1427 constexpr auto literalPieceW = L"hello"_sp;
1428 constexpr Range<wchar_t const*> pieceW{L"hello", 5};
1429 EXPECT_EQ(literalPieceW, pieceW);