diff options
author | Ed Smith-Rowland <3dw4rd@verizon.net> | 2013-06-08 22:37:50 +0000 |
---|---|---|
committer | Edward Smith-Rowland <emsr@gcc.gnu.org> | 2013-06-08 22:37:50 +0000 |
commit | 83ac9249c8149990f17eea00c092e2ffdbb103f9 (patch) | |
tree | 2f5574e46f254c45a03ef70f1d5a43fe6b4b78fc /libstdc++-v3/include | |
parent | ed52a84e3ffde82c52557d0f027488d9fe69563f (diff) | |
download | gcc-83ac9249c8149990f17eea00c092e2ffdbb103f9.zip gcc-83ac9249c8149990f17eea00c092e2ffdbb103f9.tar.gz gcc-83ac9249c8149990f17eea00c092e2ffdbb103f9.tar.bz2 |
Implement N3654 - Quoted Strings Library Proposal
2013-06-08 Ed Smith-Rowland <3dw4rd@verizon.net>
Implement N3654 - Quoted Strings Library Proposal
* include/std/iomanip: Add quoted(String, Char delim, Char escape)
manipulators and supporting machinery in c++1y mode.
* testsuite/27_io/manipulators/standard/char/quoted.cc: New.
* testsuite/27_io/manipulators/standard/wchar_t/quoted.cc: New.
From-SVN: r199860
Diffstat (limited to 'libstdc++-v3/include')
-rw-r--r-- | libstdc++-v3/include/std/iomanip | 155 |
1 files changed, 154 insertions, 1 deletions
diff --git a/libstdc++-v3/include/std/iomanip b/libstdc++-v3/include/std/iomanip index 599d220..47284e8 100644 --- a/libstdc++-v3/include/std/iomanip +++ b/libstdc++-v3/include/std/iomanip @@ -334,7 +334,160 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __os; } -#endif +#if __cplusplus > 201103L + + namespace __detail { + + /** + * @brief Struct for delimited strings. + * The left and right delimiters can be different. + */ + template<typename _String, typename _CharT> + struct _Quoted_string + { + static_assert(is_reference<_String>::value + || is_pointer<_String>::value, + "String type must be pointer or reference"); + + _Quoted_string(_String __str, _CharT __del, _CharT __esc) + : _M_string(__str), _M_delim{__del}, _M_escape{__esc} + { } + + _Quoted_string& + operator=(_Quoted_string&) = delete; + + _String _M_string; + _CharT _M_delim; + _CharT _M_escape; + }; + + /** + * @brief Inserter for delimited strings. + * The left and right delimiters can be different. + */ + template<typename _CharT, typename _Traits> + auto& + operator<<(std::basic_ostream<_CharT, _Traits>& __os, + const _Quoted_string<const _CharT*, _CharT>& __str) + { + __os << __str._M_delim; + for (const _CharT* __c = __str._M_string; *__c; ++__c) + { + if (*__c == __str._M_delim || *__c == __str._M_escape) + __os << __str._M_escape; + __os << *__c; + } + __os << __str._M_delim; + + return __os; + } + + /** + * @brief Inserter for delimited strings. + * The left and right delimiters can be different. + */ + template<typename _CharT, typename _Traits, typename _String> + auto& + operator<<(std::basic_ostream<_CharT, _Traits>& __os, + const _Quoted_string<_String, _CharT>& __str) + { + __os << __str._M_delim; + for (auto& __c : __str._M_string) + { + if (__c == __str._M_delim || __c == __str._M_escape) + __os << __str._M_escape; + __os << __c; + } + __os << __str._M_delim; + + return __os; + } + + /** + * @brief Extractor for delimited strings. + * The left and right delimiters can be different. + */ + template<typename _CharT, typename _Traits, typename _Alloc> + auto& + operator>>(std::basic_istream<_CharT, _Traits>& __is, + const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&, + _CharT>& __str) + { + __str._M_string.clear(); + + _CharT __c; + __is >> __c; + if (!__is.good()) + return __is; + if (__c != __str._M_delim) + { + __is.unget(); + __is >> __str._M_string; + return __is; + } + std::ios_base::fmtflags __flags + = __is.flags(__is.flags() & ~std::ios_base::skipws); + do + { + __is >> __c; + if (!__is.good()) + break; + if (__c == __str._M_escape) + { + __is >> __c; + if (!__is.good()) + break; + } + else if (__c == __str._M_delim) + break; + __str._M_string += __c; + } + while (true); + __is.setf(__flags); + + return __is; + } + + } // namespace __detail + + /** + * @brief Manipulator for quoted strings. + * @param __str String to quote. + * @param __delim Character to quote string with. + * @param __escape Escape character to escape itself or quote character. + */ + template<typename _CharT> + inline auto + quoted(const _CharT* __string, + _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) + { + return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim, + __escape); + } + + template<typename _CharT, typename _Traits, typename _Alloc> + inline auto + quoted(const basic_string<_CharT, _Traits, _Alloc>& __string, + _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) + { + return __detail::_Quoted_string< + const basic_string<_CharT, _Traits, _Alloc>&, _CharT>( + __string, __delim, __escape); + } + + template<typename _CharT, typename _Traits, typename _Alloc> + inline auto + quoted(basic_string<_CharT, _Traits, _Alloc>& __string, + _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) + { + return __detail::_Quoted_string< + basic_string<_CharT, _Traits, _Alloc>&, _CharT>( + __string, __delim, __escape); + } + +#endif // __cplusplus > 201103L + +#endif // __cplusplus >= 201103L // Inhibit implicit instantiations for required instantiations, // which are defined via explicit instantiations elsewhere. |