anchor.diff:  parse_ipv4_bin(?STRING_REST($?, Rest), Acc, URI) ->
       _ = validate_ipv4_address(lists:reverse(Acc)),
       {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
  -    Query = calculate_parsed_query(Rest, T),
  +    Query = calculate_parsed_query_fragment(Rest, T),
       {Rest, URI1#{query => decode_query(Query)}};
   parse_ipv4_bin(?STRING_REST($#, Rest), Acc, URI) ->
       _ = validate_ipv4_address(lists:reverse(Acc)),
       {T, URI1} = parse_fragment(Rest, URI),  % path-empty
  -    Fragment = calculate_parsed_fragment(Rest, T),
  +    Fragment = calculate_parsed_query_fragment(Rest, T),
       {Rest, URI1#{fragment => decode_fragment(Fragment)}};
   parse_ipv4_bin(?STRING_REST(Char, Rest), Acc, URI) ->
       case is_ipv4(Char) of
  
positive.code: %%-------------------------------------------------------------------------
  %% Helper functions for calculating the parsed binary.
  %%-------------------------------------------------------------------------
  
  %% Returns the parsed binary based on Input and the Unparsed part.
  %% Handles the following special cases:
  %%
  %%    #{host => [],path => "/",query => []} = uri_string:parse("///?")
  %%    #{fragment => [],host => [],path => "/"} = uri_string:parse("///#")
  %%
  -spec calculate_parsed_part(binary(), binary()) -> binary().
  calculate_parsed_part(<<$?>>, _) -> <<>>;
  calculate_parsed_part(<<$#>>, _) -> <<>>;
  calculate_parsed_part(<<>>, _) -> <<>>;
  calculate_parsed_part(Input, <<>>) ->
      case binary:last(Input) of
          $? ->
              init_binary(Input);
          $# ->
              init_binary(Input);
          _Else ->
              Input
      end;
  calculate_parsed_part(Input, Unparsed) ->
      {First, _} = split_binary(Input, byte_size(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  
  -spec calculate_parsed_userinfo(binary(), binary()) -> binary().
  calculate_parsed_userinfo(<<$?>>, _) -> <<>>;
  calculate_parsed_userinfo(<<$#>>, _) -> <<>>;
  calculate_parsed_userinfo(<<>>, _) -> <<>>;
  calculate_parsed_userinfo(Input, <<>>) ->
      case binary:last(Input) of
          $? ->
              init_binary(Input);
          $# ->
              init_binary(Input);
          $@ ->
              init_binary(Input);
          _Else ->
              Input
      end;
  calculate_parsed_userinfo(Input, Unparsed) ->
      {First, _} = split_binary(Input, byte_size(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  
  -spec calculate_parsed_host(binary(), binary()) -> binary().
  calculate_parsed_host(<<$?>>, _) -> <<>>;
  calculate_parsed_host(<<$#>>, _) -> <<>>;
  calculate_parsed_host(<<>>, _) -> <<>>;
  calculate_parsed_host(Input, <<>>) ->
      case binary:last(Input) of
          $? ->
              init_binary(Input);
          $# ->
              init_binary(Input);
          $/ ->
              init_binary(Input);
          _Else ->
              Input
      end;
  calculate_parsed_host(Input, Unparsed) ->
      {First, _} = split_binary(Input, byte_size(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  
  -spec calculate_parsed_port(binary(), binary()) -> binary().
  calculate_parsed_port(<<$?>>, _) -> <<>>;
  calculate_parsed_port(<<$#>>, _) -> <<>>;
  calculate_parsed_port(<<>>, _) -> <<>>;
  calculate_parsed_port(Input, <<>>) ->
      case binary:last(Input) of
          $? ->
              init_binary(Input);
          $# ->
              init_binary(Input);
          $/ ->
              init_binary(Input);
          _Else ->
              Input
      end;
  calculate_parsed_port(Input, Unparsed) ->
      {First, _} = split_binary(Input, byte_size(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  
  calculate_parsed_query(<<$#>>, _) -> <<>>;
  calculate_parsed_query(<<>>, _) -> <<>>;
  calculate_parsed_query(Input, <<>>) ->
      case binary:last(Input) of
          $# ->
              init_binary(Input);
          _Else ->
              Input
      end;
  calculate_parsed_query(Input, Unparsed) ->
      {First, _} = split_binary(Input, byte_size(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  
  -spec calculate_parsed_fragment(binary(), binary()) -> binary().
  calculate_parsed_fragment(<<$#>>, _) -> <<>>;
  calculate_parsed_fragment(Input, Unparsed) ->
      {First, _} = split_binary(Input, byte_size(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  
  %% Returns the parsed binary based on Input and the Unparsed part.
  %% Used when parsing authority.
  %%
  %% Handles the following special cases:
  %%
  %%    #{host => "foo",query => []} = uri_string:parse("//foo?")
  %%    #{fragment => [],host => "foo"} = uri_string:parse("//foo#")
  %%    #{host => "foo",path => "/"} = uri_string:parse("//foo/")
  %%    #{host => "foo",query => [],scheme => "http"} = uri_string:parse("http://foo?")
  %%    #{fragment => [],host => "foo",scheme => "http"} = uri_string:parse("http://foo#")
  %%    #{host => "foo",path => "/",scheme => "http"} = uri_string:parse("http://foo/")
  %%
  -spec calculate_parsed_part_sl(binary(), binary()) -> binary().
  calculate_parsed_part_sl(<<$?>>, _) -> <<>>;
  calculate_parsed_part_sl(<<$#>>, _) -> <<>>;
  calculate_parsed_part_sl(<<>>, _) -> <<>>;
  calculate_parsed_part_sl(Input, <<>>) ->
      case binary:last(Input) of
          $? ->
              init_binary(Input);
          $# ->
              init_binary(Input);
          $/ ->
              init_binary(Input);
          _Else ->
              Input
      end;
  calculate_parsed_part_sl(Input, Unparsed) ->
      {First, _} =
          split_binary(Input, byte_size_exl_single_slash(Input) - byte_size_exl_head(Unparsed)),
      First.
  
  %% Return all bytes of the binary except the last one. The binary must be non-empty.
  init_binary(B) ->
  
negative.code:     {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
  parse_reg_name(?STRING_REST($?, Rest), URI) ->
      {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
      Query = calculate_parsed_query_fragment(Rest, T),
      {Rest, URI1#{query => decode_query(Query)}};
  parse_reg_name(?STRING_REST($#, Rest), URI) ->
      {T, URI1} = parse_fragment(Rest, URI),  % path-empty
      Fragment = calculate_parsed_query_fragment(Rest, T),
      {Rest, URI1#{fragment => decode_fragment(Fragment)}};
  parse_reg_name(?STRING_REST(Char, Rest), URI) ->
      case is_reg_name(Char) of
  