mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-04 02:24:29 +00:00
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
This commit is contained in:
@ -14,6 +14,7 @@
|
||||
#ifndef LLVM_ADT_STRINGEXTRAS_H
|
||||
#define LLVM_ADT_STRINGEXTRAS_H
|
||||
|
||||
#include <iterator>
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
|
||||
@ -159,6 +160,48 @@ static inline StringRef getOrdinalSuffix(unsigned Val) {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename IteratorT>
|
||||
inline std::string join_impl(IteratorT Begin, IteratorT End,
|
||||
StringRef Separator, std::input_iterator_tag) {
|
||||
std::string S;
|
||||
if (Begin == End)
|
||||
return S;
|
||||
|
||||
S += (*Begin);
|
||||
while (++Begin != End) {
|
||||
S += Separator;
|
||||
S += (*Begin);
|
||||
}
|
||||
return S;
|
||||
}
|
||||
|
||||
template <typename IteratorT>
|
||||
inline std::string join_impl(IteratorT Begin, IteratorT End,
|
||||
StringRef Separator, std::forward_iterator_tag) {
|
||||
std::string S;
|
||||
if (Begin == End)
|
||||
return S;
|
||||
|
||||
size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
|
||||
for (IteratorT I = Begin; I != End; ++I)
|
||||
Len += (*Begin).size();
|
||||
S.reserve(Len);
|
||||
S += (*Begin);
|
||||
while (++Begin != End) {
|
||||
S += Separator;
|
||||
S += (*Begin);
|
||||
}
|
||||
return S;
|
||||
}
|
||||
|
||||
/// Joins the strings in the range [Begin, End), adding Separator between
|
||||
/// the elements.
|
||||
template <typename IteratorT>
|
||||
inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
|
||||
typedef typename std::iterator_traits<IteratorT>::iterator_category tag;
|
||||
return join_impl(Begin, End, Separator, tag());
|
||||
}
|
||||
|
||||
} // End llvm namespace
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user