fchabouis

fchabouis

IO.binstream is not unicode safe. What does it mean?

Hello,
I have read the IO module documentation and I must say I am confused about it and all the types related to strings.
In particular, I read for the binstream functio doc that is is “Unicode unsafe”. And that “Finally, do not use this function on IO devices in Unicode mode as it will return the wrong result.”

Now if I try:

{:ok, pid} = "àéb€%$\nùç*µ" |> StringIO.open()
> {:ok, #PID<0.114.0>}
pid |> IO.binstream(:line) |> Enum.to_list()  
> ["àéb€%$\n", "ùç*µ"]

It seems to work. Is that just lucky ?

If I create to IO device with StringIO.open(), I understand it is a unicode IO device. Am I supposed to stream it with IO.stream instead of IO.binstream? Does it make a difference?

Thanks!

PS: I read for binstream that “The device is iterated by the given number of bytes or line by line if :line is given. This reads from the IO device as a raw binary.” What’s a raw binary ? Binary data with non-unicode encoding for example? But how can binstream detect new lines if the encoding is unknown?

#io

Most Liked

josevalim

josevalim

Creator of Elixir

\n might work by accident because in almost all encodings it is the 10 byte. The issue with binstream will manifest in cases where the device supports multiple encodings, such as files. Try this:

  1. Write Unicode to a file
  2. Open the file with :utf8 flag
  3. Read the file with binread

In a nutshell, if a file was opened without the utf8 flag, use binread, otherwise read.

PS: yes, a raw binary is meant to be a string that is not in utf8 encoding. Improvements to the docs are welcome!

fchabouis

fchabouis

Thanks for the reply José.

I did the test you suggested, and using binstream on a file opened with :utf8 effectively gives wrong results.

From what I understand, the doc in binstream implicitely refers to the doc of File.open. But in my case, the input did not come from a File but from a string. Thus I was reading the documentation of StringIO.open, which also has an encoding option.

When the IO device comes from StringIO.open, no matter what encoding (:unicode or :latin1) is chosen, or wich function is used to stream (IO.binstream or IO.stream), everything seems to work fine.

Thanks for your help!

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement