1 /* 2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 #ifndef NET_DCSCTP_COMMON_STR_JOIN_H_ 11 #define NET_DCSCTP_COMMON_STR_JOIN_H_ 12 13 #include <string> 14 15 #include "absl/strings/string_view.h" 16 #include "rtc_base/strings/string_builder.h" 17 18 namespace dcsctp { 19 20 template <typename Range> StrJoin(const Range & seq,absl::string_view delimiter)21std::string StrJoin(const Range& seq, absl::string_view delimiter) { 22 rtc::StringBuilder sb; 23 int idx = 0; 24 25 for (const typename Range::value_type& elem : seq) { 26 if (idx > 0) { 27 sb << delimiter; 28 } 29 sb << elem; 30 31 ++idx; 32 } 33 return sb.Release(); 34 } 35 36 template <typename Range, typename Functor> StrJoin(const Range & seq,absl::string_view delimiter,const Functor & fn)37std::string StrJoin(const Range& seq, 38 absl::string_view delimiter, 39 const Functor& fn) { 40 rtc::StringBuilder sb; 41 int idx = 0; 42 43 for (const typename Range::value_type& elem : seq) { 44 if (idx > 0) { 45 sb << delimiter; 46 } 47 fn(sb, elem); 48 49 ++idx; 50 } 51 return sb.Release(); 52 } 53 54 } // namespace dcsctp 55 56 #endif // NET_DCSCTP_COMMON_STR_JOIN_H_ 57