summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/support/iomanip_quoted.h
blob: a8ca38bb6b1a807f09d8dd25660f0b425536cae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**-*-C++-*-
 * \file
 * \brief Dumb implementation of something that might look like C++14
 * std::quoted.
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 */

#ifndef IRONY_MODE_SERVER_SUPPORT_IOMANIP_QUOTED_H_
#define IRONY_MODE_SERVER_SUPPORT_IOMANIP_QUOTED_H_

#include <ostream>
#include <string>

namespace support {
namespace detail {

struct QuotedStringProxy {
  QuotedStringProxy(const std::string &s) : s(s) {
  }

  std::string s;
};

std::ostream &operator<<(std::ostream &os, const QuotedStringProxy &q) {
  const std::string &s = q.s;

  os << '"';
  if (s.find_first_of("\"\\") == std::string::npos) {
    os << s;
  } else {
    for (auto ch : s) {
      if (ch == '\\' || ch == '"')
        os << '\\';

      os << ch;
    }
  }
  os << '"';
  return os;
}

} // namespace detail

detail::QuotedStringProxy quoted(const std::string &s) {
  return detail::QuotedStringProxy(s);
}

} // namespace support

#endif // IRONY_MODE_SERVER_SUPPORT_IOMANIP_QUOTED_H_