You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.9KB

  1. // Helpers for quoted stream manipulators -*- C++ -*-
  2. // Copyright (C) 2013-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/quoted_string.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{iomanip}
  23. */
  24. #ifndef _GLIBCXX_QUOTED_STRING_H
  25. #define _GLIBCXX_QUOTED_STRING_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <sstream>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. namespace __detail {
  35. /**
  36. * @brief Struct for delimited strings.
  37. */
  38. template<typename _String, typename _CharT>
  39. struct _Quoted_string
  40. {
  41. static_assert(is_reference<_String>::value
  42. || is_pointer<_String>::value,
  43. "String type must be pointer or reference");
  44. _Quoted_string(_String __str, _CharT __del, _CharT __esc)
  45. : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
  46. { }
  47. _Quoted_string&
  48. operator=(_Quoted_string&) = delete;
  49. _String _M_string;
  50. _CharT _M_delim;
  51. _CharT _M_escape;
  52. };
  53. #if __cplusplus >= 201703L
  54. template<typename _CharT, typename _Traits>
  55. struct _Quoted_string<basic_string_view<_CharT, _Traits>, _CharT>
  56. {
  57. _Quoted_string(basic_string_view<_CharT, _Traits> __str,
  58. _CharT __del, _CharT __esc)
  59. : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
  60. { }
  61. _Quoted_string&
  62. operator=(_Quoted_string&) = delete;
  63. basic_string_view<_CharT, _Traits> _M_string;
  64. _CharT _M_delim;
  65. _CharT _M_escape;
  66. };
  67. #endif // C++17
  68. /**
  69. * @brief Inserter for quoted strings.
  70. *
  71. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  72. * DR 2344 quoted()'s interaction with padding is unclear
  73. */
  74. template<typename _CharT, typename _Traits>
  75. std::basic_ostream<_CharT, _Traits>&
  76. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  77. const _Quoted_string<const _CharT*, _CharT>& __str)
  78. {
  79. std::basic_ostringstream<_CharT, _Traits> __ostr;
  80. __ostr << __str._M_delim;
  81. for (const _CharT* __c = __str._M_string; *__c; ++__c)
  82. {
  83. if (*__c == __str._M_delim || *__c == __str._M_escape)
  84. __ostr << __str._M_escape;
  85. __ostr << *__c;
  86. }
  87. __ostr << __str._M_delim;
  88. return __os << __ostr.str();
  89. }
  90. /**
  91. * @brief Inserter for quoted strings.
  92. *
  93. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  94. * DR 2344 quoted()'s interaction with padding is unclear
  95. */
  96. template<typename _CharT, typename _Traits, typename _String>
  97. std::basic_ostream<_CharT, _Traits>&
  98. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  99. const _Quoted_string<_String, _CharT>& __str)
  100. {
  101. std::basic_ostringstream<_CharT, _Traits> __ostr;
  102. __ostr << __str._M_delim;
  103. for (auto __c : __str._M_string)
  104. {
  105. if (__c == __str._M_delim || __c == __str._M_escape)
  106. __ostr << __str._M_escape;
  107. __ostr << __c;
  108. }
  109. __ostr << __str._M_delim;
  110. return __os << __ostr.str();
  111. }
  112. /**
  113. * @brief Extractor for delimited strings.
  114. * The left and right delimiters can be different.
  115. */
  116. template<typename _CharT, typename _Traits, typename _Alloc>
  117. std::basic_istream<_CharT, _Traits>&
  118. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  119. const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&,
  120. _CharT>& __str)
  121. {
  122. _CharT __c;
  123. __is >> __c;
  124. if (!__is.good())
  125. return __is;
  126. if (__c != __str._M_delim)
  127. {
  128. __is.unget();
  129. __is >> __str._M_string;
  130. return __is;
  131. }
  132. __str._M_string.clear();
  133. std::ios_base::fmtflags __flags
  134. = __is.flags(__is.flags() & ~std::ios_base::skipws);
  135. do
  136. {
  137. __is >> __c;
  138. if (!__is.good())
  139. break;
  140. if (__c == __str._M_escape)
  141. {
  142. __is >> __c;
  143. if (!__is.good())
  144. break;
  145. }
  146. else if (__c == __str._M_delim)
  147. break;
  148. __str._M_string += __c;
  149. }
  150. while (true);
  151. __is.setf(__flags);
  152. return __is;
  153. }
  154. } // namespace __detail
  155. _GLIBCXX_END_NAMESPACE_VERSION
  156. } // namespace std
  157. #endif // C++11
  158. #endif /* _GLIBCXX_QUOTED_STRING_H */