Changing how binaries are printed: was RE: XML and Erlang

Joe Armstrong (AL/EAB) joe.armstrong@REDACTED
Thu Jun 23 14:14:07 CEST 2005

Richard A. O'Keefe wrote:

> I've just written some code to measure how big a parsed document
> would be using several different representations.  This is the same
> DTD I used as an example yesterday, except that a couple of attributes
> are really (large) enumerations and I didn't show you those.
> There's a 5.4 ratio between best and worst overall.
> But the really important factor is NOT whether you use a 
> general-purpose
> XML representation or one tailored to your particular problem,

Yes :-)

Of course you're right....

So I changed my XML parser to represent both atoms and string as binaries, I ran it
and - I couldn't read the results :-)

So - I *changed* io_lib.erl to pretty print binaries containing printable strings correctly :-)

With this change the binary <<"cat">> prints as <<"cat">> and NOT <<99,97,116>>

Suddenly I can read and *debug* programs with binaries in them.

I realise that my reluctance to use binaries was due to the difficulty of debugging
programs with binaries in them.

So if you patch io_lib.erl as follows

just define

> write_binary(B, D) ->
>     S = size(B),
>     {B2, Dots} = if 
> 		     S > D ->
> 			 {B1, _} = split_binary(B, D),
> 			 {B1, "..."};
> 		     true ->
> 			 {B, ""}
> 		 end,
>     List = binary_to_list(B2),
>     case printable_list(List) of
> 	true ->
> 	    ["<<\"",List,Dots,"\">>"];
> 	false ->
> 	    write_binary1(B, D)
>     end.

and rename the old write_binry as write_binary1

Then binaries will be printed in a readable manner

IMHO this should be added to the standard distribution


More information about the erlang-questions mailing list