@ -768,8 +768,18 @@ protected:
template < typename StreamType >
void escape_to_space_format ( int c , StreamType & out ) const
{
// IMPLEMENT ME
escape_to_json_format ( c , out ) ;
switch ( c )
{
case 0 : out < < ' \\ ' ; out < < ' u ' < < ' { ' < < ' 0 ' < < ' } ' ; break ;
case ' \r ' : out < < ' \\ ' ; out < < ' r ' ; break ; // 13
case ' \n ' : out < < ' \\ ' ; out < < ' n ' ; break ; // 10
case ' \\ ' : out < < ' \\ ' ; out < < ' \\ ' ; break ;
case ' " ' : out < < ' \\ ' ; out < < ' \" ' ; break ;
case ' \b ' : out < < ' \\ ' ; out < < ' b ' ; break ; // 8
case ' \f ' : out < < ' \\ ' ; out < < ' f ' ; break ; // 12
default :
out < < static_cast < typename StreamType : : char_type > ( c ) ;
}
}
@ -778,14 +788,14 @@ protected:
{
switch ( c )
{
case 0 : out < < ' \\ ' ; out < < ' 0' ; break ;
case ' \r ' : out < < ' \\ ' ; out < < ' r ' ; break ;
case ' \n ' : out < < ' \\ ' ; out < < ' n ' ; break ;
case ' \\ ' : out < < ' \\ ' ; out < < ' \\ ' ; break ;
case ' " ' : out < < ' \\ ' ; out < < ' \" ' ; break ;
//case '(': out << '\\'; out << '('; break;
//case ')': out << '\\'; out << ')'; break;
//case '=': out << '\\'; out << '='; break;
case 0 : out < < ' \\ ' ; out < < ' u' < < ' 0 ' < < ' 0 ' < < ' 0 ' < < ' 0' ; break ;
case ' \r ' : out < < ' \\ ' ; out < < ' r ' ; break ; // 13
case ' \n ' : out < < ' \\ ' ; out < < ' n ' ; break ; // 10
case ' \\ ' : out < < ' \\ ' ; out < < ' \\ ' ; break ;
case ' " ' : out < < ' \\ ' ; out < < ' \" ' ; break ;
case ' \t ' : out < < ' \\ ' ; out < < ' t ' ; break ; // 9
case ' \b ' : out < < ' \\ ' ; out < < ' b ' ; break ; // 8
case ' \f ' : out < < ' \\ ' ; out < < ' f ' ; break ; // 12
default :
out < < static_cast < typename StreamType : : char_type > ( c ) ;
}
@ -811,6 +821,22 @@ protected:
}
}
template < typename CharT , typename StreamType >
void copy_input_string_to_output ( const CharT * input_str , size_t len , StreamType & out_str , Escape escape ) const
{
for ( size_t i = 0 ; i < len ; + + i )
{
if ( escape = = Escape : : no_escape )
out_str < < static_cast < typename StreamType : : char_type > ( input_str [ i ] ) ;
else
if ( escape = = Escape : : escape_space )
escape_to_space_format ( input_str [ i ] , out_str ) ;
else
if ( escape = = Escape : : escape_json )
escape_to_json_format ( input_str [ i ] , out_str ) ;
}
}
template < typename StreamType >
void copy_input_stream_to_output ( const StreamType & input_str , StreamType & out_str , Escape escape ) const
{
@ -833,23 +859,37 @@ protected:
template < typename StreamType >
void serialize_string_buffer ( const char * input_str , StreamType & out_str , Escape escape ) const
void serialize_string_buffer ( const char * input_str , size_t len , StreamType & out_str , Escape escape ) const
{
if constexpr ( sizeof ( char ) = = sizeof ( typename StreamType : : char_type ) )
{
// input and output are char (we assume it is utf8)
copy_input_string_to_output ( input_str , out_str, escape ) ;
copy_input_string_to_output ( input_str , len, out_str, escape ) ;
}
else
{
// input is utf8 but output is wide
copy_input_string_to_output ( input_str , out_str , escape ) ; // temporarily
StreamType temp_stream ;
utf8_to_wide ( input_str , len , temp_stream , false ) ;
copy_input_stream_to_output ( temp_stream , out_str , escape ) ;
}
}
// !!!!!!!!!!!!!!!!!!! FIXME
// StreamType temp_stream;
// UTF8ToWide(input_str, temp_stream, false);
//
// copy_input_stream_to_output(temp_stream, out_str, escape);
template < typename StreamType >
void serialize_string_buffer ( const wchar_t * input_str , size_t len , StreamType & out_str , Escape escape ) const
{
if constexpr ( sizeof ( wchar_t ) = = sizeof ( typename StreamType : : char_type ) )
{
// input and output are wide characters
copy_input_string_to_output ( input_str , len , out_str , escape ) ;
}
else
{
// input is wide but output is utf8
StreamType temp_stream ;
wide_to_utf8 ( input_str , len , temp_stream , false ) ;
copy_input_stream_to_output ( temp_stream , out_str , escape ) ;
}
}
@ -864,17 +904,14 @@ protected:
}
else
{
StreamType temp_stream ;
// input is wide but output is utf8
StreamType temp_stream ;
wide_to_utf8 ( input_str , temp_stream , false ) ;
copy_input_stream_to_output ( temp_stream , out_str , escape ) ;
}
}
template < typename StreamType >
void serialize_space_null ( StreamType & str ) const
{
@ -937,7 +974,7 @@ protected:
void serialize_space_string ( StreamType & str ) const
{
str < < ' " ' ;
serialize_string_buffer ( value . value_string . c_str ( ) , str, Escape : : escape_space ) ;
serialize_string_buffer ( value . value_string . c_str ( ) , value. value_string . size ( ) , str, Escape : : escape_space ) ;
str < < ' " ' ;
}
@ -945,7 +982,7 @@ protected:
void serialize_space_wstring ( StreamType & str ) const
{
str < < ' " ' ;
serialize_string_buffer ( value . value_wstring . c_str ( ) , str, Escape : : escape_space ) ;
serialize_string_buffer ( value . value_wstring . c_str ( ) , value. value_wstring . size ( ) , str, Escape : : escape_space ) ;
str < < ' " ' ;
}
@ -1121,7 +1158,7 @@ protected:
void serialize_json_string ( StreamType & str ) const
{
str < < ' " ' ;
serialize_string_buffer ( value . value_string . c_str ( ) , str, Escape : : escape_json ) ;
serialize_string_buffer ( value . value_string . c_str ( ) , value. value_string . size ( ) , str, Escape : : escape_json ) ;
str < < ' " ' ;
}
@ -1129,7 +1166,7 @@ protected:
void serialize_json_wstring ( StreamType & str ) const
{
str < < ' " ' ;
serialize_string_buffer ( value . value_wstring . c_str ( ) , str, Escape : : escape_json ) ;
serialize_string_buffer ( value . value_wstring . c_str ( ) , value. value_wstring . size ( ) , str, Escape : : escape_json ) ;
str < < ' " ' ;
}