It seems that set_stream/2 is breaking the behaviour of with_output_to/2. For example, if I create a stream with output to atom and set its encoding to UTF-8:
?- with_output_to(atom(A), (set_stream(current_output,encoding(utf8)),format('hello ~w',[world]))).
A = '������������'.
Trying to output to codes gives:
?- with_output_to(codes(A), (set_stream(current_output,encoding(utf8)),format('hello ~w',[world]))).
A = [1819043176,1870078063].
Which gives a hint that internally it is still using wchar_t.
Without setting the encoding the code works as intended:
?- with_output_to(atom(A), (format('hello ~w',[world]))).
A = 'hello world'.
Do output streams created on atoms/strings/codes support set_stream(Out, encoding(x))?
That will indeed not work. Why would you want that? The temporary stream has an encoding that supports all characters that can be represented and thus you get consistent results. We could use a dedicated stream and prevent encoding switches. Not sure whether that is worth the trouble.
The reason I’m asking is that I’m trying to send RDF graph in XML format using HTTP POST request. So, I’m doing rdf_save/2 to atom and then http_post/4 like this:
If the graphs are large, memory files, which you can also pass to a POST request will be more efficient as the huge atom will only disappear on the next atom-GC (when unreachable). Not sure whether or not that fixes this problem. When really large, using file(Type,File) would be another option.
I think that makes sense. I’'ll surely accept it. Thanks.
Technically, probably yes. Not sure. Surely we can raise a permission error. I’m not sure whether ignoring is a good idea. That depends on the reasons one may have to change the encoding.