00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/str_split.h"
00016
00017 #include <deque>
00018 #include <initializer_list>
00019 #include <list>
00020 #include <map>
00021 #include <memory>
00022 #include <string>
00023 #include <type_traits>
00024 #include <unordered_map>
00025 #include <unordered_set>
00026 #include <vector>
00027
00028 #include "gmock/gmock.h"
00029 #include "gtest/gtest.h"
00030 #include "absl/base/dynamic_annotations.h"
00031 #include "absl/base/macros.h"
00032 #include "absl/strings/numbers.h"
00033
00034 namespace {
00035
00036 using ::testing::ElementsAre;
00037 using ::testing::Pair;
00038 using ::testing::UnorderedElementsAre;
00039
00040 TEST(Split, TraitsTest) {
00041 static_assert(!absl::strings_internal::SplitterIsConvertibleTo<int>::value,
00042 "");
00043 static_assert(
00044 !absl::strings_internal::SplitterIsConvertibleTo<std::string>::value, "");
00045 static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00046 std::vector<std::string>>::value,
00047 "");
00048 static_assert(
00049 !absl::strings_internal::SplitterIsConvertibleTo<std::vector<int>>::value,
00050 "");
00051 static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00052 std::vector<absl::string_view>>::value,
00053 "");
00054 static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00055 std::map<std::string, std::string>>::value,
00056 "");
00057 static_assert(absl::strings_internal::SplitterIsConvertibleTo<
00058 std::map<absl::string_view, absl::string_view>>::value,
00059 "");
00060 static_assert(!absl::strings_internal::SplitterIsConvertibleTo<
00061 std::map<int, std::string>>::value,
00062 "");
00063 static_assert(!absl::strings_internal::SplitterIsConvertibleTo<
00064 std::map<std::string, int>>::value,
00065 "");
00066 }
00067
00068
00069
00070
00071
00072 TEST(Split, APIExamples) {
00073 {
00074
00075 std::vector<std::string> v = absl::StrSplit("a,b,c", ",");
00076 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00077
00078
00079 using absl::ByString;
00080 v = absl::StrSplit("a,b,c", ByString(","));
00081 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00082
00083
00084 EXPECT_THAT(absl::StrSplit("a,b,c", ByString(",")),
00085 ElementsAre("a", "b", "c"));
00086 }
00087
00088 {
00089
00090 std::vector<std::string> v = absl::StrSplit("a,b,c", ',');
00091 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00092
00093
00094 using absl::ByChar;
00095 v = absl::StrSplit("a,b,c", ByChar(','));
00096 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00097 }
00098
00099 {
00100
00101 const std::vector<std::string> v = absl::StrSplit("a=>b=>c", "=>");
00102 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00103 }
00104
00105 {
00106
00107 std::vector<absl::string_view> v = absl::StrSplit("a,b,c", ',');
00108 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00109 }
00110
00111 {
00112
00113 std::vector<std::string> v = absl::StrSplit(",a,b,c,", ',');
00114 EXPECT_THAT(v, ElementsAre("", "a", "b", "c", ""));
00115 }
00116
00117 {
00118
00119 std::vector<std::string> v = absl::StrSplit("abc", ',');
00120 EXPECT_THAT(v, ElementsAre("abc"));
00121 }
00122
00123 {
00124
00125
00126 std::vector<std::string> v = absl::StrSplit("abc", "");
00127 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00128 }
00129
00130 {
00131
00132
00133
00134
00135
00136 std::string embedded_nulls("a\0b\0c", 5);
00137 std::string null_delim("\0", 1);
00138 std::vector<std::string> v = absl::StrSplit(embedded_nulls, null_delim);
00139 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00140 }
00141
00142 {
00143
00144 std::pair<std::string, std::string> p = absl::StrSplit("a,b,c", ',');
00145 EXPECT_EQ("a", p.first);
00146 EXPECT_EQ("b", p.second);
00147
00148 }
00149
00150 {
00151
00152 std::set<std::string> v = absl::StrSplit("a,b,c,a,b,c,a,b,c", ',');
00153 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00154 }
00155
00156 {
00157
00158 char a[] = ",";
00159 char* d = a + 0;
00160 std::vector<std::string> v = absl::StrSplit("a,b,c", d);
00161 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00162 }
00163
00164 {
00165
00166 using absl::ByAnyChar;
00167 std::vector<std::string> v = absl::StrSplit("a,b;c", ByAnyChar(",;"));
00168 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00169 }
00170
00171 {
00172
00173 using absl::SkipWhitespace;
00174 std::vector<std::string> v =
00175 absl::StrSplit(" a , ,,b,", ',', SkipWhitespace());
00176 EXPECT_THAT(v, ElementsAre(" a ", "b"));
00177 }
00178
00179 {
00180
00181 using absl::ByLength;
00182 std::vector<std::string> v = absl::StrSplit("abcdefg", ByLength(3));
00183 EXPECT_THAT(v, ElementsAre("abc", "def", "g"));
00184 }
00185
00186 {
00187
00188 std::vector<std::string> v1 = absl::StrSplit("a,b,c", ',');
00189 EXPECT_THAT(v1, ElementsAre("a", "b", "c"));
00190 std::vector<std::string> v2(absl::StrSplit("a,b,c", ','));
00191 EXPECT_THAT(v2, ElementsAre("a", "b", "c"));
00192 auto v3 = std::vector<std::string>(absl::StrSplit("a,b,c", ','));
00193 EXPECT_THAT(v3, ElementsAre("a", "b", "c"));
00194 v3 = absl::StrSplit("a,b,c", ',');
00195 EXPECT_THAT(v3, ElementsAre("a", "b", "c"));
00196 }
00197
00198 {
00199
00200 std::map<std::string, std::string> m = absl::StrSplit("a,1,b,2,a,3", ',');
00201 EXPECT_EQ(2, m.size());
00202 EXPECT_EQ("3", m["a"]);
00203 EXPECT_EQ("2", m["b"]);
00204 }
00205
00206 {
00207
00208 std::multimap<std::string, std::string> m =
00209 absl::StrSplit("a,1,b,2,a,3", ',');
00210 EXPECT_EQ(3, m.size());
00211 auto it = m.find("a");
00212 EXPECT_EQ("1", it->second);
00213 ++it;
00214 EXPECT_EQ("3", it->second);
00215 it = m.find("b");
00216 EXPECT_EQ("2", it->second);
00217 }
00218
00219 {
00220
00221 std::string s = "x,x,x,x,x,x,x";
00222 for (absl::string_view sp : absl::StrSplit(s, ',')) {
00223 EXPECT_EQ("x", sp);
00224 }
00225 }
00226
00227 {
00228
00229 using absl::SkipWhitespace;
00230 std::string s = " ,x,,x,,x,x,x,,";
00231 for (absl::string_view sp : absl::StrSplit(s, ',', SkipWhitespace())) {
00232 EXPECT_EQ("x", sp);
00233 }
00234 }
00235
00236 {
00237
00238
00239
00240
00241 std::map<std::string, std::string> m;
00242 for (absl::string_view sp : absl::StrSplit("a=b=c,d=e,f=,g", ',')) {
00243 m.insert(absl::StrSplit(sp, absl::MaxSplits('=', 1)));
00244 }
00245 EXPECT_EQ("b=c", m.find("a")->second);
00246 EXPECT_EQ("e", m.find("d")->second);
00247 EXPECT_EQ("", m.find("f")->second);
00248 EXPECT_EQ("", m.find("g")->second);
00249 }
00250 }
00251
00252
00253
00254
00255
00256 TEST(SplitIterator, Basics) {
00257 auto splitter = absl::StrSplit("a,b", ',');
00258 auto it = splitter.begin();
00259 auto end = splitter.end();
00260
00261 EXPECT_NE(it, end);
00262 EXPECT_EQ("a", *it);
00263 ++it;
00264 EXPECT_NE(it, end);
00265 EXPECT_EQ("b",
00266 std::string(it->data(), it->size()));
00267 it++;
00268 EXPECT_EQ(it, end);
00269 }
00270
00271
00272 class Skip {
00273 public:
00274 explicit Skip(const std::string& s) : s_(s) {}
00275 bool operator()(absl::string_view sp) { return sp != s_; }
00276
00277 private:
00278 std::string s_;
00279 };
00280
00281 TEST(SplitIterator, Predicate) {
00282 auto splitter = absl::StrSplit("a,b,c", ',', Skip("b"));
00283 auto it = splitter.begin();
00284 auto end = splitter.end();
00285
00286 EXPECT_NE(it, end);
00287 EXPECT_EQ("a", *it);
00288 ++it;
00289 EXPECT_NE(it, end);
00290 EXPECT_EQ("c",
00291 std::string(it->data(), it->size()));
00292 it++;
00293 EXPECT_EQ(it, end);
00294 }
00295
00296 TEST(SplitIterator, EdgeCases) {
00297
00298 struct {
00299 std::string in;
00300 std::vector<std::string> expect;
00301 } specs[] = {
00302 {"", {""}},
00303 {"foo", {"foo"}},
00304 {",", {"", ""}},
00305 {",foo", {"", "foo"}},
00306 {"foo,", {"foo", ""}},
00307 {",foo,", {"", "foo", ""}},
00308 {"foo,bar", {"foo", "bar"}},
00309 };
00310
00311 for (const auto& spec : specs) {
00312 SCOPED_TRACE(spec.in);
00313 auto splitter = absl::StrSplit(spec.in, ',');
00314 auto it = splitter.begin();
00315 auto end = splitter.end();
00316 for (const auto& expected : spec.expect) {
00317 EXPECT_NE(it, end);
00318 EXPECT_EQ(expected, *it++);
00319 }
00320 EXPECT_EQ(it, end);
00321 }
00322 }
00323
00324 TEST(Splitter, Const) {
00325 const auto splitter = absl::StrSplit("a,b,c", ',');
00326 EXPECT_THAT(splitter, ElementsAre("a", "b", "c"));
00327 }
00328
00329 TEST(Split, EmptyAndNull) {
00330
00331
00332
00333
00334
00335
00336 EXPECT_THAT(absl::StrSplit(absl::string_view(""), '-'), ElementsAre(""));
00337 EXPECT_THAT(absl::StrSplit(absl::string_view(), '-'), ElementsAre());
00338 }
00339
00340 TEST(SplitIterator, EqualityAsEndCondition) {
00341 auto splitter = absl::StrSplit("a,b,c", ',');
00342 auto it = splitter.begin();
00343 auto it2 = it;
00344
00345
00346 ++it2;
00347 ++it2;
00348 EXPECT_EQ("c", *it2);
00349
00350
00351
00352
00353
00354 std::vector<absl::string_view> v;
00355 for (; it != it2; ++it) {
00356 v.push_back(*it);
00357 }
00358 EXPECT_THAT(v, ElementsAre("a", "b"));
00359 }
00360
00361
00362
00363
00364
00365 TEST(Splitter, RangeIterators) {
00366 auto splitter = absl::StrSplit("a,b,c", ',');
00367 std::vector<absl::string_view> output;
00368 for (const absl::string_view p : splitter) {
00369 output.push_back(p);
00370 }
00371 EXPECT_THAT(output, ElementsAre("a", "b", "c"));
00372 }
00373
00374
00375 template <typename ContainerType, typename Splitter>
00376 void TestConversionOperator(const Splitter& splitter) {
00377 ContainerType output = splitter;
00378 EXPECT_THAT(output, UnorderedElementsAre("a", "b", "c", "d"));
00379 }
00380
00381 template <typename MapType, typename Splitter>
00382 void TestMapConversionOperator(const Splitter& splitter) {
00383 MapType m = splitter;
00384 EXPECT_THAT(m, UnorderedElementsAre(Pair("a", "b"), Pair("c", "d")));
00385 }
00386
00387 template <typename FirstType, typename SecondType, typename Splitter>
00388 void TestPairConversionOperator(const Splitter& splitter) {
00389 std::pair<FirstType, SecondType> p = splitter;
00390 EXPECT_EQ(p, (std::pair<FirstType, SecondType>("a", "b")));
00391 }
00392
00393 TEST(Splitter, ConversionOperator) {
00394 auto splitter = absl::StrSplit("a,b,c,d", ',');
00395
00396 TestConversionOperator<std::vector<absl::string_view>>(splitter);
00397 TestConversionOperator<std::vector<std::string>>(splitter);
00398 TestConversionOperator<std::list<absl::string_view>>(splitter);
00399 TestConversionOperator<std::list<std::string>>(splitter);
00400 TestConversionOperator<std::deque<absl::string_view>>(splitter);
00401 TestConversionOperator<std::deque<std::string>>(splitter);
00402 TestConversionOperator<std::set<absl::string_view>>(splitter);
00403 TestConversionOperator<std::set<std::string>>(splitter);
00404 TestConversionOperator<std::multiset<absl::string_view>>(splitter);
00405 TestConversionOperator<std::multiset<std::string>>(splitter);
00406 TestConversionOperator<std::unordered_set<std::string>>(splitter);
00407
00408
00409
00410 TestMapConversionOperator<std::map<absl::string_view, absl::string_view>>(
00411 splitter);
00412 TestMapConversionOperator<std::map<absl::string_view, std::string>>(splitter);
00413 TestMapConversionOperator<std::map<std::string, absl::string_view>>(splitter);
00414 TestMapConversionOperator<std::map<std::string, std::string>>(splitter);
00415 TestMapConversionOperator<
00416 std::multimap<absl::string_view, absl::string_view>>(splitter);
00417 TestMapConversionOperator<std::multimap<absl::string_view, std::string>>(
00418 splitter);
00419 TestMapConversionOperator<std::multimap<std::string, absl::string_view>>(
00420 splitter);
00421 TestMapConversionOperator<std::multimap<std::string, std::string>>(splitter);
00422 TestMapConversionOperator<std::unordered_map<std::string, std::string>>(
00423 splitter);
00424
00425
00426
00427 TestPairConversionOperator<absl::string_view, absl::string_view>(splitter);
00428 TestPairConversionOperator<absl::string_view, std::string>(splitter);
00429 TestPairConversionOperator<std::string, absl::string_view>(splitter);
00430 TestPairConversionOperator<std::string, std::string>(splitter);
00431 }
00432
00433
00434
00435
00436
00437 TEST(Splitter, ToPair) {
00438 {
00439
00440 std::pair<std::string, std::string> p = absl::StrSplit("", ',');
00441 EXPECT_EQ("", p.first);
00442 EXPECT_EQ("", p.second);
00443 }
00444
00445 {
00446
00447 std::pair<std::string, std::string> p = absl::StrSplit("a", ',');
00448 EXPECT_EQ("a", p.first);
00449 EXPECT_EQ("", p.second);
00450 }
00451
00452 {
00453
00454 std::pair<std::string, std::string> p = absl::StrSplit(",b", ',');
00455 EXPECT_EQ("", p.first);
00456 EXPECT_EQ("b", p.second);
00457 }
00458
00459 {
00460
00461 std::pair<std::string, std::string> p = absl::StrSplit("a,b", ',');
00462 EXPECT_EQ("a", p.first);
00463 EXPECT_EQ("b", p.second);
00464 }
00465
00466 {
00467
00468 std::pair<std::string, std::string> p = absl::StrSplit("a,b,c", ',');
00469 EXPECT_EQ("a", p.first);
00470 EXPECT_EQ("b", p.second);
00471
00472 }
00473 }
00474
00475 TEST(Splitter, Predicates) {
00476 static const char kTestChars[] = ",a, ,b,";
00477 using absl::AllowEmpty;
00478 using absl::SkipEmpty;
00479 using absl::SkipWhitespace;
00480
00481 {
00482
00483 auto splitter = absl::StrSplit(kTestChars, ',');
00484 std::vector<std::string> v = splitter;
00485 EXPECT_THAT(v, ElementsAre("", "a", " ", "b", ""));
00486 }
00487
00488 {
00489
00490 auto splitter = absl::StrSplit(kTestChars, ',', AllowEmpty());
00491 std::vector<std::string> v_allowempty = splitter;
00492 EXPECT_THAT(v_allowempty, ElementsAre("", "a", " ", "b", ""));
00493
00494
00495 auto splitter_nopredicate = absl::StrSplit(kTestChars, ',');
00496 std::vector<std::string> v_nopredicate = splitter_nopredicate;
00497 EXPECT_EQ(v_allowempty, v_nopredicate);
00498 }
00499
00500 {
00501
00502 auto splitter = absl::StrSplit(kTestChars, ',', SkipEmpty());
00503 std::vector<std::string> v = splitter;
00504 EXPECT_THAT(v, ElementsAre("a", " ", "b"));
00505 }
00506
00507 {
00508
00509 auto splitter = absl::StrSplit(kTestChars, ',', SkipWhitespace());
00510 std::vector<std::string> v = splitter;
00511 EXPECT_THAT(v, ElementsAre("a", "b"));
00512 }
00513 }
00514
00515
00516
00517
00518
00519 TEST(Split, Basics) {
00520 {
00521
00522
00523 absl::StrSplit("a,b,c", ',');
00524 }
00525
00526 {
00527 std::vector<absl::string_view> v = absl::StrSplit("a,b,c", ',');
00528 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00529 }
00530
00531 {
00532 std::vector<std::string> v = absl::StrSplit("a,b,c", ',');
00533 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00534 }
00535
00536 {
00537
00538
00539 std::vector<std::string> v;
00540 v = absl::StrSplit("a,b,c", ',');
00541
00542 EXPECT_THAT(v, ElementsAre("a", "b", "c"));
00543 std::map<std::string, std::string> m;
00544 m = absl::StrSplit("a,b,c", ',');
00545 EXPECT_EQ(2, m.size());
00546 std::unordered_map<std::string, std::string> hm;
00547 hm = absl::StrSplit("a,b,c", ',');
00548 EXPECT_EQ(2, hm.size());
00549 }
00550 }
00551
00552 absl::string_view ReturnStringView() { return "Hello World"; }
00553 const char* ReturnConstCharP() { return "Hello World"; }
00554 char* ReturnCharP() { return const_cast<char*>("Hello World"); }
00555
00556 TEST(Split, AcceptsCertainTemporaries) {
00557 std::vector<std::string> v;
00558 v = absl::StrSplit(ReturnStringView(), ' ');
00559 EXPECT_THAT(v, ElementsAre("Hello", "World"));
00560 v = absl::StrSplit(ReturnConstCharP(), ' ');
00561 EXPECT_THAT(v, ElementsAre("Hello", "World"));
00562 v = absl::StrSplit(ReturnCharP(), ' ');
00563 EXPECT_THAT(v, ElementsAre("Hello", "World"));
00564 }
00565
00566 TEST(Split, Temporary) {
00567
00568
00569
00570 const char input[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u";
00571 EXPECT_LT(sizeof(std::string), ABSL_ARRAYSIZE(input))
00572 << "Input should be larger than fits on the stack.";
00573
00574
00575 auto splitter = absl::StrSplit(std::string(input), ',');
00576 std::string expected = "a";
00577 for (absl::string_view letter : splitter) {
00578 EXPECT_EQ(expected, letter);
00579 ++expected[0];
00580 }
00581 EXPECT_EQ("v", expected);
00582
00583
00584 auto std_splitter = absl::StrSplit(std::string(input), ',');
00585 expected = "a";
00586 for (absl::string_view letter : std_splitter) {
00587 EXPECT_EQ(expected, letter);
00588 ++expected[0];
00589 }
00590 EXPECT_EQ("v", expected);
00591 }
00592
00593 template <typename T>
00594 static std::unique_ptr<T> CopyToHeap(const T& value) {
00595 return std::unique_ptr<T>(new T(value));
00596 }
00597
00598 TEST(Split, LvalueCaptureIsCopyable) {
00599 std::string input = "a,b";
00600 auto heap_splitter = CopyToHeap(absl::StrSplit(input, ','));
00601 auto stack_splitter = *heap_splitter;
00602 heap_splitter.reset();
00603 std::vector<std::string> result = stack_splitter;
00604 EXPECT_THAT(result, testing::ElementsAre("a", "b"));
00605 }
00606
00607 TEST(Split, TemporaryCaptureIsCopyable) {
00608 auto heap_splitter = CopyToHeap(absl::StrSplit(std::string("a,b"), ','));
00609 auto stack_splitter = *heap_splitter;
00610 heap_splitter.reset();
00611 std::vector<std::string> result = stack_splitter;
00612 EXPECT_THAT(result, testing::ElementsAre("a", "b"));
00613 }
00614
00615 TEST(Split, SplitterIsCopyableAndMoveable) {
00616 auto a = absl::StrSplit("foo", '-');
00617
00618
00619 auto b = a;
00620 auto c = std::move(a);
00621 b = c;
00622 c = std::move(b);
00623
00624 EXPECT_THAT(c, ElementsAre("foo"));
00625 }
00626
00627 TEST(Split, StringDelimiter) {
00628 {
00629 std::vector<absl::string_view> v = absl::StrSplit("a,b", ',');
00630 EXPECT_THAT(v, ElementsAre("a", "b"));
00631 }
00632
00633 {
00634 std::vector<absl::string_view> v = absl::StrSplit("a,b", std::string(","));
00635 EXPECT_THAT(v, ElementsAre("a", "b"));
00636 }
00637
00638 {
00639 std::vector<absl::string_view> v =
00640 absl::StrSplit("a,b", absl::string_view(","));
00641 EXPECT_THAT(v, ElementsAre("a", "b"));
00642 }
00643 }
00644
00645 #if !defined(__cpp_char8_t)
00646 #if defined(__clang__)
00647 #pragma clang diagnostic push
00648 #pragma clang diagnostic ignored "-Wc++2a-compat"
00649 #endif
00650 TEST(Split, UTF8) {
00651
00652 std::string utf8_string = u8"\u03BA\u1F79\u03C3\u03BC\u03B5";
00653 {
00654
00655 std::string to_split = "a," + utf8_string;
00656 std::vector<absl::string_view> v = absl::StrSplit(to_split, ',');
00657 EXPECT_THAT(v, ElementsAre("a", utf8_string));
00658 }
00659
00660 {
00661
00662 std::string to_split = "a," + utf8_string + ",b";
00663 std::string unicode_delimiter = "," + utf8_string + ",";
00664 std::vector<absl::string_view> v =
00665 absl::StrSplit(to_split, unicode_delimiter);
00666 EXPECT_THAT(v, ElementsAre("a", "b"));
00667 }
00668
00669 {
00670
00671 std::vector<absl::string_view> v =
00672 absl::StrSplit(u8"Foo h\u00E4llo th\u4E1Ere", absl::ByAnyChar(" \t"));
00673 EXPECT_THAT(v, ElementsAre("Foo", u8"h\u00E4llo", u8"th\u4E1Ere"));
00674 }
00675 }
00676 #if defined(__clang__)
00677 #pragma clang diagnostic pop
00678 #endif
00679 #endif // !defined(__cpp_char8_t)
00680
00681 TEST(Split, EmptyStringDelimiter) {
00682 {
00683 std::vector<std::string> v = absl::StrSplit("", "");
00684 EXPECT_THAT(v, ElementsAre(""));
00685 }
00686
00687 {
00688 std::vector<std::string> v = absl::StrSplit("a", "");
00689 EXPECT_THAT(v, ElementsAre("a"));
00690 }
00691
00692 {
00693 std::vector<std::string> v = absl::StrSplit("ab", "");
00694 EXPECT_THAT(v, ElementsAre("a", "b"));
00695 }
00696
00697 {
00698 std::vector<std::string> v = absl::StrSplit("a b", "");
00699 EXPECT_THAT(v, ElementsAre("a", " ", "b"));
00700 }
00701 }
00702
00703 TEST(Split, SubstrDelimiter) {
00704 std::vector<absl::string_view> results;
00705 absl::string_view delim("//");
00706
00707 results = absl::StrSplit("", delim);
00708 EXPECT_THAT(results, ElementsAre(""));
00709
00710 results = absl::StrSplit("//", delim);
00711 EXPECT_THAT(results, ElementsAre("", ""));
00712
00713 results = absl::StrSplit("ab", delim);
00714 EXPECT_THAT(results, ElementsAre("ab"));
00715
00716 results = absl::StrSplit("ab//", delim);
00717 EXPECT_THAT(results, ElementsAre("ab", ""));
00718
00719 results = absl::StrSplit("ab/", delim);
00720 EXPECT_THAT(results, ElementsAre("ab/"));
00721
00722 results = absl::StrSplit("a/b", delim);
00723 EXPECT_THAT(results, ElementsAre("a/b"));
00724
00725 results = absl::StrSplit("a//b", delim);
00726 EXPECT_THAT(results, ElementsAre("a", "b"));
00727
00728 results = absl::StrSplit("a///b", delim);
00729 EXPECT_THAT(results, ElementsAre("a", "/b"));
00730
00731 results = absl::StrSplit("a////b", delim);
00732 EXPECT_THAT(results, ElementsAre("a", "", "b"));
00733 }
00734
00735 TEST(Split, EmptyResults) {
00736 std::vector<absl::string_view> results;
00737
00738 results = absl::StrSplit("", '#');
00739 EXPECT_THAT(results, ElementsAre(""));
00740
00741 results = absl::StrSplit("#", '#');
00742 EXPECT_THAT(results, ElementsAre("", ""));
00743
00744 results = absl::StrSplit("#cd", '#');
00745 EXPECT_THAT(results, ElementsAre("", "cd"));
00746
00747 results = absl::StrSplit("ab#cd#", '#');
00748 EXPECT_THAT(results, ElementsAre("ab", "cd", ""));
00749
00750 results = absl::StrSplit("ab##cd", '#');
00751 EXPECT_THAT(results, ElementsAre("ab", "", "cd"));
00752
00753 results = absl::StrSplit("ab##", '#');
00754 EXPECT_THAT(results, ElementsAre("ab", "", ""));
00755
00756 results = absl::StrSplit("ab#ab#", '#');
00757 EXPECT_THAT(results, ElementsAre("ab", "ab", ""));
00758
00759 results = absl::StrSplit("aaaa", 'a');
00760 EXPECT_THAT(results, ElementsAre("", "", "", "", ""));
00761
00762 results = absl::StrSplit("", '#', absl::SkipEmpty());
00763 EXPECT_THAT(results, ElementsAre());
00764 }
00765
00766 template <typename Delimiter>
00767 static bool IsFoundAtStartingPos(absl::string_view text, Delimiter d,
00768 size_t starting_pos, int expected_pos) {
00769 absl::string_view found = d.Find(text, starting_pos);
00770 return found.data() != text.data() + text.size() &&
00771 expected_pos == found.data() - text.data();
00772 }
00773
00774
00775
00776
00777
00778
00779 template <typename Delimiter>
00780 static bool IsFoundAt(absl::string_view text, Delimiter d, int expected_pos) {
00781 const std::string leading_text = ",x,y,z,";
00782 return IsFoundAtStartingPos(text, d, 0, expected_pos) &&
00783 IsFoundAtStartingPos(leading_text + std::string(text), d,
00784 leading_text.length(),
00785 expected_pos + leading_text.length());
00786 }
00787
00788
00789
00790
00791
00792
00793 template <typename Delimiter>
00794 void TestComma(Delimiter d) {
00795 EXPECT_TRUE(IsFoundAt(",", d, 0));
00796 EXPECT_TRUE(IsFoundAt("a,", d, 1));
00797 EXPECT_TRUE(IsFoundAt(",b", d, 0));
00798 EXPECT_TRUE(IsFoundAt("a,b", d, 1));
00799 EXPECT_TRUE(IsFoundAt("a,b,", d, 1));
00800 EXPECT_TRUE(IsFoundAt("a,b,c", d, 1));
00801 EXPECT_FALSE(IsFoundAt("", d, -1));
00802 EXPECT_FALSE(IsFoundAt(" ", d, -1));
00803 EXPECT_FALSE(IsFoundAt("a", d, -1));
00804 EXPECT_FALSE(IsFoundAt("a b c", d, -1));
00805 EXPECT_FALSE(IsFoundAt("a;b;c", d, -1));
00806 EXPECT_FALSE(IsFoundAt(";", d, -1));
00807 }
00808
00809 TEST(Delimiter, ByString) {
00810 using absl::ByString;
00811 TestComma(ByString(","));
00812
00813
00814 ByString comma_string(",");
00815 TestComma(comma_string);
00816
00817
00818
00819
00820
00821
00822 absl::string_view abc("abc");
00823 EXPECT_EQ(0, abc.find(""));
00824 ByString empty("");
00825 EXPECT_FALSE(IsFoundAt("", empty, 0));
00826 EXPECT_FALSE(IsFoundAt("a", empty, 0));
00827 EXPECT_TRUE(IsFoundAt("ab", empty, 1));
00828 EXPECT_TRUE(IsFoundAt("abc", empty, 1));
00829 }
00830
00831 TEST(Split, ByChar) {
00832 using absl::ByChar;
00833 TestComma(ByChar(','));
00834
00835
00836 ByChar comma_char(',');
00837 TestComma(comma_char);
00838 }
00839
00840
00841
00842
00843
00844 TEST(Delimiter, ByAnyChar) {
00845 using absl::ByAnyChar;
00846 ByAnyChar one_delim(",");
00847
00848 EXPECT_TRUE(IsFoundAt(",", one_delim, 0));
00849 EXPECT_TRUE(IsFoundAt("a,", one_delim, 1));
00850 EXPECT_TRUE(IsFoundAt("a,b", one_delim, 1));
00851 EXPECT_TRUE(IsFoundAt(",b", one_delim, 0));
00852
00853 EXPECT_FALSE(IsFoundAt("", one_delim, -1));
00854 EXPECT_FALSE(IsFoundAt(" ", one_delim, -1));
00855 EXPECT_FALSE(IsFoundAt("a", one_delim, -1));
00856 EXPECT_FALSE(IsFoundAt("a;b;c", one_delim, -1));
00857 EXPECT_FALSE(IsFoundAt(";", one_delim, -1));
00858
00859 ByAnyChar two_delims(",;");
00860
00861 EXPECT_TRUE(IsFoundAt(",", two_delims, 0));
00862 EXPECT_TRUE(IsFoundAt(";", two_delims, 0));
00863 EXPECT_TRUE(IsFoundAt(",;", two_delims, 0));
00864 EXPECT_TRUE(IsFoundAt(";,", two_delims, 0));
00865 EXPECT_TRUE(IsFoundAt(",;b", two_delims, 0));
00866 EXPECT_TRUE(IsFoundAt(";,b", two_delims, 0));
00867 EXPECT_TRUE(IsFoundAt("a;,", two_delims, 1));
00868 EXPECT_TRUE(IsFoundAt("a,;", two_delims, 1));
00869 EXPECT_TRUE(IsFoundAt("a;,b", two_delims, 1));
00870 EXPECT_TRUE(IsFoundAt("a,;b", two_delims, 1));
00871
00872 EXPECT_FALSE(IsFoundAt("", two_delims, -1));
00873 EXPECT_FALSE(IsFoundAt(" ", two_delims, -1));
00874 EXPECT_FALSE(IsFoundAt("a", two_delims, -1));
00875 EXPECT_FALSE(IsFoundAt("a=b=c", two_delims, -1));
00876 EXPECT_FALSE(IsFoundAt("=", two_delims, -1));
00877
00878
00879
00880
00881 ByAnyChar empty("");
00882 EXPECT_FALSE(IsFoundAt("", empty, 0));
00883 EXPECT_FALSE(IsFoundAt("a", empty, 0));
00884 EXPECT_TRUE(IsFoundAt("ab", empty, 1));
00885 EXPECT_TRUE(IsFoundAt("abc", empty, 1));
00886 }
00887
00888
00889
00890
00891
00892 TEST(Delimiter, ByLength) {
00893 using absl::ByLength;
00894
00895 ByLength four_char_delim(4);
00896
00897
00898 EXPECT_TRUE(IsFoundAt("abcde", four_char_delim, 4));
00899 EXPECT_TRUE(IsFoundAt("abcdefghijklmnopqrstuvwxyz", four_char_delim, 4));
00900 EXPECT_TRUE(IsFoundAt("a b,c\nd", four_char_delim, 4));
00901
00902 EXPECT_FALSE(IsFoundAt("", four_char_delim, 0));
00903 EXPECT_FALSE(IsFoundAt("a", four_char_delim, 0));
00904 EXPECT_FALSE(IsFoundAt("ab", four_char_delim, 0));
00905 EXPECT_FALSE(IsFoundAt("abc", four_char_delim, 0));
00906 EXPECT_FALSE(IsFoundAt("abcd", four_char_delim, 0));
00907 }
00908
00909 TEST(Split, WorksWithLargeStrings) {
00910 if (sizeof(size_t) > 4) {
00911 std::string s((uint32_t{1} << 31) + 1, 'x');
00912 s.back() = '-';
00913 std::vector<absl::string_view> v = absl::StrSplit(s, '-');
00914 EXPECT_EQ(2, v.size());
00915
00916
00917 EXPECT_EQ('x', v[0][0]);
00918 EXPECT_EQ('x', v[0][1]);
00919 EXPECT_EQ('x', v[0][3]);
00920 EXPECT_EQ("", v[1]);
00921 }
00922 }
00923
00924 TEST(SplitInternalTest, TypeTraits) {
00925 EXPECT_FALSE(absl::strings_internal::HasMappedType<int>::value);
00926 EXPECT_TRUE(
00927 (absl::strings_internal::HasMappedType<std::map<int, int>>::value));
00928 EXPECT_FALSE(absl::strings_internal::HasValueType<int>::value);
00929 EXPECT_TRUE(
00930 (absl::strings_internal::HasValueType<std::map<int, int>>::value));
00931 EXPECT_FALSE(absl::strings_internal::HasConstIterator<int>::value);
00932 EXPECT_TRUE(
00933 (absl::strings_internal::HasConstIterator<std::map<int, int>>::value));
00934 EXPECT_FALSE(absl::strings_internal::IsInitializerList<int>::value);
00935 EXPECT_TRUE((absl::strings_internal::IsInitializerList<
00936 std::initializer_list<int>>::value));
00937 }
00938
00939 }