elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Inspect all binaries (except bitstrings) in double quotes, use octal `\000` to represent the bytes that are out of range

Open josevalim opened this issue 13 years ago • 10 comments

josevalim avatar Oct 31 '13 15:10 josevalim

Passing no_strings: true will force it to be printed as a binary. We should also rename raw: true to no_records: true and potentially add no_char_lists: true. /cc @pminten

josevalim avatar Oct 31 '13 15:10 josevalim

:+1: :rage4:

meh avatar Oct 31 '13 15:10 meh

This will be great when working with strings and highly annoying when working with binaries. Maybe have some way to set the default for iex?

pminten avatar Oct 31 '13 15:10 pminten

You can configure inspect options for IEx already. :)

josevalim avatar Oct 31 '13 15:10 josevalim

:+1:

zv avatar Dec 12 '13 17:12 zv

The problem we have today is that it is hard to find where you have bad data in your string. This change solves that pretty good. But by introducing this we make it harder to see when you have correct data. For example a binary of non-string data that is printed like << 0, 42, 100, 60 >> will look weird and garbled with this change. While I don't think we should change the default (current printing) we can still introduce these as options to inspect.

Adding these options will make it easier to debug bad strings. But we need more tools to help us, for example we could add String.chunk(string, :invalid | :non_printable) that will split the string into chunks of valid utf-8 and invalid utf-8 and vice versa for chars. We also have String.valid?/1 today.

Please add your thoughts on this.

ericmj avatar Dec 12 '13 19:12 ericmj

@ericmj this has been done, right?

josevalim avatar Jan 22 '14 20:01 josevalim

@josevalim inspect options are done but we still need String.chunk

ericmj avatar Jan 22 '14 22:01 ericmj

I can work on this. There is also an option to check the whole string and count how many printable vs non-printable characters it has.

If it is mostly plain text with occasional low ASCII codes (< 32) or some weird large codes, it makes sense to print those bad ones as octal or hexadecimal. If, however, the string looks like random binary data, it can be printed as a binary.

What was the idea with String.chunk though? Was it for something like the following?

iex> <<"jose"::binary, 1, 2, 3, "valim"::binary>>
"jose" <> <<1, 2, 3>> <> "valim"

alco avatar May 23 '14 01:05 alco

The only thing pending in this pull request is String.chunk/1. The idea of String.chunk/1 is that, if I have a large string that is meant to be unicode but it has invalid bytes, I could call String.chunk/1 and it would break it apart at the invalid bytes. It would make it easy to debug and find the invalid bytes on long binaries.

We could add the proposed heuristic though, where we detect the amount of printable characters and decided what to do from there. The only issue with this approach is that in a huge binary, the bad chunks may not show up, and then you may never know it was actually corrupted. Maybe we can postpone this?

josevalim avatar May 23 '14 08:05 josevalim