Re: replace bug or error in the documentation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: replace bug or error in the documentation

John Doe
According to the docs re:replace returns iodata | list | binary
But sometimes with I think invalid unicode (see below) it returns {error, binary(), binary()} - the same error which would return unicode:characters_to_list(Binary, utf8).

I would say that it is an error in the docs, but it happens only when the Search parameter is a compiled regexp. With regexp as string it returns binary. So I think it is a bug.

1> S = <<"fml.ölm vcrd rd hb kh ouniun">>.                           
<<"fml.ölm vcrd rd hb kh ouniun">>
2> {ok, Re} = re:compile(<<>>, [unicode, caseless]).   
{ok,{re_pattern,0,1,0,
               <<69,82,67,80,71,0,0,0,1,8,0,0,1,0,0,0,255,255,255,255,
                 255,255,...>>}}
3> re:replace(S, Re, <<>>,  [global, {return, binary}]).
{error,<<"fml.">>,<<"ölm vcrd rd hb kh ouniun">>}
4> re:replace(S, <<>>, <<>>,  [global, {return, binary}]).
<<"fml.ölm vcrd rd hb kh ouniun">>




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: replace bug or error in the documentation

Guilherme Andrade


On 18 April 2018 at 22:57, John Doe <[hidden email]> wrote:
> S = <<"fml.ölm vcrd rd hb kh ouniun">>.                           


It seems to work properly when one declares the proper encoding of the string literal, e.g.:

   <<"fml.ölm vcrd rd hb kh ouniun"/utf8>>

In any case, the error is reproducible with the non-compiled regex if you pass 'unicode' option when calling :replace.

   re:replace(S, <<>>, <<>>,  [unicode, global, {return, binary}])

That is: the non-compiled call wasn't interpreting the input as UTF8. Which is why it worked.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions