N3545: Quoted strings
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@190032 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
147
include/iomanip
147
include/iomanip
@@ -26,6 +26,17 @@ template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl =
|
||||
template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
|
||||
template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
|
||||
|
||||
template <class charT>
|
||||
T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
|
||||
|
||||
template <class charT, class traits, class Allocator>
|
||||
T12 quoted(const basic_string<charT, traits, Allocator>& s,
|
||||
charT delim=charT('"'), charT escape=charT('\\')); // C++14
|
||||
|
||||
template <class charT, class traits, class Allocator>
|
||||
T13 quoted(basic_string<charT, traits, Allocator>& s,
|
||||
charT delim=charT('"'), charT escape=charT('\\')); // C++14
|
||||
|
||||
} // std
|
||||
|
||||
*/
|
||||
@@ -499,6 +510,142 @@ put_time(const tm* __tm, const _CharT* __fmt)
|
||||
return __iom_t10<_CharT>(__tm, __fmt);
|
||||
}
|
||||
|
||||
#if _LIBCPP_STD_VER > 11
|
||||
|
||||
template <class _CharT, class _Traits, class _ForwardIterator>
|
||||
std::basic_ostream<_CharT, _Traits> &
|
||||
__quoted_output ( basic_ostream<_CharT, _Traits> &__os,
|
||||
_ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape )
|
||||
{
|
||||
__os << __delim;
|
||||
for ( ; __first != __last; ++ __first )
|
||||
{
|
||||
if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
|
||||
__os << __escape;
|
||||
__os << *__first;
|
||||
}
|
||||
__os << __delim;
|
||||
return __os;
|
||||
}
|
||||
|
||||
template <class _CharT, class _Traits, class _String>
|
||||
basic_istream<_CharT, _Traits> &
|
||||
__quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
|
||||
{
|
||||
__string.clear ();
|
||||
_CharT __c;
|
||||
__is >> __c;
|
||||
if ( __is.fail ())
|
||||
return __is;
|
||||
|
||||
if (!_Traits::eq (__c, __delim)) // no delimiter, read the whole string
|
||||
{
|
||||
__is.unget ();
|
||||
__is >> __string;
|
||||
return __is;
|
||||
}
|
||||
|
||||
__save_flags<_CharT, _Traits> sf(__is);
|
||||
noskipws (__is);
|
||||
while (true)
|
||||
{
|
||||
__is >> __c;
|
||||
if ( __is.fail ())
|
||||
break;
|
||||
if (_Traits::eq (__c, __escape))
|
||||
{
|
||||
__is >> __c;
|
||||
if ( __is.fail ())
|
||||
break;
|
||||
}
|
||||
else if (_Traits::eq (__c, __delim))
|
||||
break;
|
||||
__string.push_back ( __c );
|
||||
}
|
||||
return __is;
|
||||
}
|
||||
|
||||
|
||||
template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>>
|
||||
struct __quoted_output_proxy
|
||||
{
|
||||
_Iter __first;
|
||||
_Iter __last;
|
||||
_CharT __delim;
|
||||
_CharT __escape;
|
||||
|
||||
__quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
|
||||
: __first(__f), __last(__l), __delim(__d), __escape(__e) {}
|
||||
// This would be a nice place for a string_ref
|
||||
};
|
||||
|
||||
template <class _CharT, class _Traits, class _Iter>
|
||||
basic_ostream<_CharT, _Traits>& operator<<(
|
||||
basic_ostream<_CharT, _Traits>& __os,
|
||||
const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
|
||||
{
|
||||
return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
|
||||
}
|
||||
|
||||
template <class _CharT, class _Traits, class _Allocator>
|
||||
struct __quoted_proxy
|
||||
{
|
||||
basic_string<_CharT, _Traits, _Allocator> &__string;
|
||||
_CharT __delim;
|
||||
_CharT __escape;
|
||||
|
||||
__quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
|
||||
: __string(__s), __delim(__d), __escape(__e) {}
|
||||
};
|
||||
|
||||
template <class _CharT, class _Traits, class _Allocator>
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
basic_ostream<_CharT, _Traits>& operator<<(
|
||||
basic_ostream<_CharT, _Traits>& __os,
|
||||
const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
|
||||
{
|
||||
return __quoted_output (__os, __proxy.string.cbegin (), __proxy.string.cend (), __proxy.__delim, __proxy.__escape);
|
||||
}
|
||||
|
||||
// extractor for non-const basic_string& proxies
|
||||
template <class _CharT, class _Traits, class _Allocator>
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
basic_istream<_CharT, _Traits>& operator>>(
|
||||
basic_istream<_CharT, _Traits>& __is,
|
||||
const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
|
||||
{
|
||||
return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
|
||||
}
|
||||
|
||||
|
||||
template <class _CharT>
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
__quoted_output_proxy<_CharT, const _CharT *>
|
||||
quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
|
||||
{
|
||||
const _CharT *__end = __s;
|
||||
while ( *__end ) ++__end;
|
||||
return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
|
||||
}
|
||||
|
||||
template <class _CharT, class _Traits, class _Allocator>
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
|
||||
quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
|
||||
{
|
||||
return __quoted_output_proxy<_CharT,
|
||||
typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
|
||||
( __s.cbegin(), __s.cend (), __delim, __escape );
|
||||
}
|
||||
|
||||
template <class _CharT, class _Traits, class _Allocator>
|
||||
__quoted_proxy<_CharT, _Traits, _Allocator>
|
||||
quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
|
||||
{
|
||||
return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
|
||||
}
|
||||
#endif
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP_IOMANIP
|
||||
|
Reference in New Issue
Block a user