cpp-netlib/uri

network::detail::decode function not compiling

Closed this issue · 1 comments

Hello!

I have been trying to use your library for a project of mine in order to parse the query string of a request. The problem I had was that I receive the request URL encoded and I have to decode it. I saw that your library offers such capabilities and I have been trying to use them, but unfortunately, it doesn't compile and fails with the following error:

/my-project/third-party/cpp-netlib-uri/include/network/uri/detail/decode.hpp:64:11: error: no viable overloaded '='
      out = c;
      ~~~ ^ ~
/my-project/third-party/cpp-netlib-uri/include/network/uri/uri.hpp:590:20: note: in instantiation of function template specialization 'network::detail::decode<std::__1::__wrap_iter<char *>, std::__1::__wrap_iter<char *> >' requested
      here
    return detail::decode(first, last, out);
                   ^
src/my-file.cpp:60:12: note: in instantiation of function template specialization 'network::uri::decode<std::__1::__wrap_iter<char *>, std::__1::__wrap_iter<char *> >' requested here
      uri::decode(requestUri.begin(), requestUri.end(), decodedRequestUri.begin());
           ^
/usr/include/c++/v1/iterator:1258:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'char' to 'const std::__1::__wrap_iter<char *>' for 1st argument
class __wrap_iter
      ^
/usr/include/c++/v1/iterator:1258:7: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'char' to 'std::__1::__wrap_iter<char *>' for 1st argument

Looking through the code of decode function:

template <class InputIterator, class OutputIterator>
OutputIterator decode(InputIterator in_begin, InputIterator in_end,
                      OutputIterator out_begin) {
  auto it = in_begin;
  auto out = out_begin;
  while (it != in_end) {
    if (*it == '%') {
      if (std::distance(it, in_end) < 3) {
        throw percent_decoding_error(uri_error::not_enough_input);
      }
      char c = '\0';
      it = decode_char(it, &c);
      out = c;
      ++out;
    } else {
      *out++ = *it++;
    }
  }
  return out;
}

it looks like the out variable is an iterator when the c variable is of type char. A possible fix would be to replace

out = c;

with:

*out = c;

and everything would be fine.

I am looking forward to your answer! Thank you!

I applied the fix you suggested and pushed to master (I agree, this is definitely a bug). Can you let me know if this works for you?