llvm.org GIT mirror llvm / f57a80f
Add a Python-like join function to merge a list of strings with a separator between each two elements. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189846 91177308-0d34-0410-b5e6-96231b3b80d8 Joerg Sonnenberger 6 years ago
2 changed file(s) with 71 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_ADT_STRINGEXTRAS_H
1414 #define LLVM_ADT_STRINGEXTRAS_H
1515
16 #include
1617 #include "llvm/ADT/StringRef.h"
1718 #include "llvm/Support/DataTypes.h"
1819
158159 }
159160 }
160161
162 template
163 inline std::string join_impl(IteratorT Begin, IteratorT End,
164 StringRef Separator, std::input_iterator_tag) {
165 std::string S;
166 if (Begin == End)
167 return S;
168
169 S += (*Begin);
170 while (++Begin != End) {
171 S += Separator;
172 S += (*Begin);
173 }
174 return S;
175 }
176
177 template
178 inline std::string join_impl(IteratorT Begin, IteratorT End,
179 StringRef Separator, std::forward_iterator_tag) {
180 std::string S;
181 if (Begin == End)
182 return S;
183
184 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
185 for (IteratorT I = Begin; I != End; ++I)
186 Len += (*Begin).size();
187 S.reserve(Len);
188 S += (*Begin);
189 while (++Begin != End) {
190 S += Separator;
191 S += (*Begin);
192 }
193 return S;
194 }
195
196 /// Joins the strings in the range [Begin, End), adding Separator between
197 /// the elements.
198 template
199 inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
200 typedef typename std::iterator_traits::iterator_category tag;
201 return join_impl(Begin, End, Separator, tag());
202 }
203
161204 } // End llvm namespace
162205
163206 #endif
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/StringExtras.h"
1011 #include "llvm/ADT/Hashing.h"
1112 #include "llvm/ADT/SmallVector.h"
1213 #include "llvm/Support/raw_ostream.h"
476477 }
477478 }
478479
479
480 static const char *join_input[] = { "a", "b", "c" };
481 static const char join_result1[] = "a";
482 static const char join_result2[] = "a:b:c";
483 static const char join_result3[] = "a::b::c";
484
485 TEST(StringRefTest, joinStrings) {
486 std::vector v1;
487 std::vector v2;
488 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
489 v1.push_back(join_input[i]);
490 v2.push_back(join_input[i]);
491 }
492
493 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
494 EXPECT_TRUE(v1_join1);
495 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
496 EXPECT_TRUE(v1_join2);
497 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
498 EXPECT_TRUE(v1_join3);
499
500 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
501 EXPECT_TRUE(v2_join1);
502 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
503 EXPECT_TRUE(v2_join2);
504 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
505 EXPECT_TRUE(v2_join3);
506 }
480507
481508 } // end anonymous namespace