summaryrefslogtreecommitdiff
path: root/Utilities/std/cm/iomanip
blob: 602b91258123fbc49216b14bfd2169fcce399873 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// -*-c++-*-
// vim: set ft=cpp:

/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#pragma once

#include <iomanip> // IWYU pragma: export
#if __cplusplus < 201402L || defined(_MSVC_LANG) && _MSVC_LANG < 201402L
#  include <ios>
#  include <iostream>
#  include <sstream>
#  include <string>
#  include <type_traits>
#endif
#if __cplusplus < 201703L || defined(_MSVC_LANG) && _MSVC_LANG < 201703L
#  include <cm/string_view>
#endif

namespace cm {

#if __cplusplus >= 201402L || defined(_MSVC_LANG) && _MSVC_LANG >= 201402L

using std::quoted;

#  if __cplusplus < 201703L || defined(_MSVC_LANG) && _MSVC_LANG < 201703L

inline auto quoted(cm::string_view str, char delim = '"', char escape = '\\')
{
  return std::quoted(static_cast<std::string>(str), delim, escape);
}

#  endif

#else

namespace internals {

// Struct for delimited strings.
template <typename String, typename Char>
struct quoted_string
{
  static_assert(std::is_reference<String>::value ||
                  std::is_pointer<String>::value,
                "String type must be pointer or reference");

  quoted_string(String str, Char del, Char esc)
    : string_(str)
    , delim_{ del }
    , escape_{ esc }
  {
  }

  quoted_string& operator=(quoted_string&) = delete;

  String string_;
  Char delim_;
  Char escape_;
};

template <>
struct quoted_string<cm::string_view, char>
{
  quoted_string(cm::string_view str, char del, char esc)
    : string_(str)
    , delim_{ del }
    , escape_{ esc }
  {
  }

  quoted_string& operator=(quoted_string&) = delete;

  cm::string_view string_;
  char delim_;
  char escape_;
};

template <typename Char, typename Traits>
std::basic_ostream<Char, Traits>& operator<<(
  std::basic_ostream<Char, Traits>& os,
  const quoted_string<const Char*, Char>& str)
{
  std::basic_ostringstream<Char, Traits> ostr;
  ostr << str.delim_;
  for (const Char* c = str.string_; *c; ++c) {
    if (*c == str.delim_ || *c == str.escape_)
      ostr << str.escape_;
    ostr << *c;
  }
  ostr << str.delim_;

  return os << ostr.str();
}

template <typename Char, typename Traits, typename String>
std::basic_ostream<Char, Traits>& operator<<(
  std::basic_ostream<Char, Traits>& os, const quoted_string<String, Char>& str)
{
  std::basic_ostringstream<Char, Traits> ostr;
  ostr << str.delim_;
  for (auto c : str.string_) {
    if (c == str.delim_ || c == str.escape_)
      ostr << str.escape_;
    ostr << c;
  }
  ostr << str.delim_;

  return os << ostr.str();
}

template <typename Char, typename Traits, typename Alloc>
std::basic_istream<Char, Traits>& operator>>(
  std::basic_istream<Char, Traits>& is,
  const quoted_string<std::basic_string<Char, Traits, Alloc>&, Char>& str)
{
  Char c;
  is >> c;
  if (!is.good())
    return is;
  if (c != str.delim_) {
    is.unget();
    is >> str.string_;
    return is;
  }
  str.string_.clear();
  std::ios_base::fmtflags flags =
    is.flags(is.flags() & ~std::ios_base::skipws);
  do {
    is >> c;
    if (!is.good())
      break;
    if (c == str.escape_) {
      is >> c;
      if (!is.good())
        break;
    } else if (c == str.delim_)
      break;
    str.string_ += c;
  } while (true);
  is.setf(flags);

  return is;
}
}

template <typename Char>
inline internals::quoted_string<const Char*, Char> quoted(
  const Char* str, Char delim = Char('"'), Char escape = Char('\\'))
{
  return internals::quoted_string<const Char*, Char>(str, delim, escape);
}

template <typename Char, typename Traits, typename Alloc>
inline internals::quoted_string<const std::basic_string<Char, Traits, Alloc>&,
                                Char>
quoted(const std::basic_string<Char, Traits, Alloc>& str,
       Char delim = Char('"'), Char escape = Char('\\'))
{
  return internals::quoted_string<
    const std::basic_string<Char, Traits, Alloc>&, Char>(str, delim, escape);
}

template <typename Char, typename Traits, typename Alloc>
inline internals::quoted_string<std::basic_string<Char, Traits, Alloc>&, Char>
quoted(std::basic_string<Char, Traits, Alloc>& str, Char delim = Char('"'),
       Char escape = Char('\\'))
{
  return internals::quoted_string<std::basic_string<Char, Traits, Alloc>&,
                                  Char>(str, delim, escape);
}

inline internals::quoted_string<cm::string_view, char> quoted(
  cm::string_view str, char delim = '"', char escape = '\\')
{
  return internals::quoted_string<cm::string_view, char>(str, delim, escape);
}

#endif

} // namespace cm