Mercurial > code
comparison cpp/join/join.hpp @ 621:ca5912ad2909
Misc: massive refactoring, closes #717
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 20 Oct 2017 12:36:51 +0200 |
parents | modules/join/join.hpp@266f32919d0a |
children | b327391f6a62 |
comparison
equal
deleted
inserted
replaced
620:e24fc739ec74 | 621:ca5912ad2909 |
---|---|
1 /* | |
2 * join.hpp -- join lists into a string | |
3 * | |
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 /** | |
20 * \file join.hpp | |
21 * \brief Join function | |
22 */ | |
23 | |
24 /** | |
25 * Join values by a separator and return a string. | |
26 * | |
27 * \param first the first iterator | |
28 * \param last the last iterator | |
29 * \param delim the optional delimiter | |
30 * \return the string | |
31 */ | |
32 template <typename InputIt, typename DelimType = char> | |
33 std::string join(InputIt first, InputIt last, DelimType delim = ':') | |
34 { | |
35 std::ostringstream oss; | |
36 | |
37 if (first != last) { | |
38 oss << *first; | |
39 | |
40 while (++first != last) | |
41 oss << delim << *first; | |
42 } | |
43 | |
44 return oss.str(); | |
45 } | |
46 | |
47 /** | |
48 * Convenient overload. | |
49 * | |
50 * \param list the initializer list | |
51 * \param delim the delimiter | |
52 * \return the string | |
53 */ | |
54 template <typename T, typename DelimType = char> | |
55 inline std::string join(std::initializer_list<T> list, DelimType delim = ':') | |
56 { | |
57 return join(list.begin(), list.end(), delim); | |
58 } |