Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tonda-kriz committed Nov 3, 2024
1 parent 65b3a11 commit 60a35a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 38 deletions.
11 changes: 4 additions & 7 deletions include/spb/json/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,19 @@ struct ostream
{
char buffer[ 8 ] = { };
auto size = snprintf( buffer, sizeof( buffer ), "\\u%04x", codepoint );
write( std::string_view( buffer, size ) );
return write( std::string_view( buffer, size ) );
}
else if( codepoint <= 0x10FFFF )
if( codepoint <= 0x10FFFF )
{
codepoint -= 0x10000;

auto high = static_cast< uint16_t >( ( codepoint >> 10 ) + 0xD800 );
auto low = static_cast< uint16_t >( ( codepoint & 0x3FF ) + 0xDC00 );
char buffer[ 16 ] = { };
auto size = snprintf( buffer, sizeof( buffer ), "\\u%04x\\u%04x", high, low );
write( std::string_view( buffer, size ) );
}
else
{
throw std::invalid_argument( "invalid utf8" );
return write( std::string_view( buffer, size ) );
}
throw std::invalid_argument( "invalid utf8" );
}

void write( std::string_view str )
Expand Down
7 changes: 7 additions & 0 deletions include/spb/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ static auto inline decode_point( uint32_t * state, uint32_t * codep, uint32_t by
return *state;
}

/**
* @brief encode codepoint to utf8
*
* @param unicode codepoint
* @param utf8 output
* @return size of output in bytes, 0 on error
*/
static inline auto encode_point( uint32_t unicode, char utf8[ 4 ] ) -> uint32_t
{
if( unicode <= 0x7F )
Expand Down
32 changes: 1 addition & 31 deletions test/gpb-compatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,37 +372,7 @@ TEST_CASE( "string" )
continue;
}

auto gpb = Test::Scalar::gpb::ReqString( );
auto json_string = std::string( );
gpb.set_value( value );

auto print_options = google::protobuf::util::JsonPrintOptions( );
print_options.preserve_proto_field_names = true;
REQUIRE( MessageToJsonString( gpb, &json_string, print_options ).ok( ) );

auto spb = spb::json::deserialize< Test::Scalar::ReqString >( json_string );
REQUIRE( spb.value == gpb.value( ) );
gpb_compatibility< Test::Scalar::gpb::ReqString >( spb );
}

for( auto i = 0U; i <= 0xff; i++ )
{
auto value = Test::Scalar::ReqString{ .value = { char( i ) } };
if( spb::detail::utf8::is_valid( value.value ) )
{
gpb_compatibility< Test::Scalar::gpb::ReqString >( value );
}
}
for( auto i = 0U; i <= 0xffff; i++ )
{
if( i < 0xc280 || i > 0xDFBF )
{
auto value = Test::Scalar::ReqString{ .value = { char( i >> CHAR_BIT ), char( i & 0xff ) } };
if( spb::detail::utf8::is_valid( value.value ) )
{
gpb_compatibility< Test::Scalar::gpb::ReqString >( value );
}
}
gpb_compatibility< Test::Scalar::gpb::ReqString >( Test::Scalar::ReqString{ .value = value } );
}
}
SUBCASE( "required" )
Expand Down

0 comments on commit 60a35a9

Please sign in to comment.