tlx
Loading...
Searching...
No Matches
join_quoted.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/string/join_quoted.cpp
3 *
4 * Part of tlx - http://panthema.net/tlx
5 *
6 * Copyright (C) 2016-2018 Timo Bingmann <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
12
13namespace tlx {
14
15std::string join_quoted(
16 const std::vector<std::string>& vec, char sep, char quote, char escape) {
17
18 std::string out;
19 if (vec.empty()) return out;
20
21 for (size_t i = 0; i < vec.size(); ++i) {
22 if (i != 0)
23 out += sep;
24
25 if (vec[i].find(sep) != std::string::npos) {
26 out += quote;
27 for (std::string::const_iterator it = vec[i].begin();
28 it != vec[i].end(); ++it) {
29 if (*it == quote || *it == escape) {
30 out += escape, out += *it;
31 }
32 else if (*it == '\n') {
33 out += escape, out += 'n';
34 }
35 else if (*it == '\r') {
36 out += escape, out += 'r';
37 }
38 else if (*it == '\t') {
39 out += escape, out += 't';
40 }
41 else {
42 out += *it;
43 }
44 }
45 out += quote;
46 }
47 else {
48 out += vec[i];
49 }
50 }
51
52 return out;
53}
54
55std::string join_quoted(const std::vector<std::string>& vec) {
56 return join_quoted(vec, ' ', '"', '\\');
57}
58
59} // namespace tlx
60
61/******************************************************************************/
std::string join_quoted(const std::vector< std::string > &vec, char sep, char quote, char escape)
Join a vector of strings using a separator character.