aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrik Nyblom <[email protected]>2013-02-18 15:14:18 +0100
committerPatrik Nyblom <[email protected]>2013-02-18 15:14:18 +0100
commitb6e1fa3e42011c7845becd8a000fd77940d6ed9b (patch)
tree0285758c4a3921f29005257f92382716c3143f2b
parent568cb052c7be7e36f96ddd18001529e371790b4b (diff)
downloadotp-b6e1fa3e42011c7845becd8a000fd77940d6ed9b.tar.gz
otp-b6e1fa3e42011c7845becd8a000fd77940d6ed9b.tar.bz2
otp-b6e1fa3e42011c7845becd8a000fd77940d6ed9b.zip
Make ~tp output latin1 binaries as strings if possible
-rw-r--r--lib/stdlib/src/io_lib_pretty.erl28
1 files changed, 21 insertions, 7 deletions
diff --git a/lib/stdlib/src/io_lib_pretty.erl b/lib/stdlib/src/io_lib_pretty.erl
index 4a61b033b0..7637ad7a3d 100644
--- a/lib/stdlib/src/io_lib_pretty.erl
+++ b/lib/stdlib/src/io_lib_pretty.erl
@@ -485,13 +485,18 @@ printable_bin(Bin, Len, D, latin1) ->
false
end;
printable_bin(Bin, Len, D, _Uni) ->
- case printable_unicode(Bin, Len, [], io:printable_range()) of
- {_, <<>>, L} ->
- {byte_size(Bin) =:= length(L), L};
- {NC, Bin1, L} when D > 0, Len - NC >= D ->
- {byte_size(Bin)-byte_size(Bin1) =:= length(L), true, L};
- {_NC, _Bin, _L} ->
- false
+ case valid_utf8(Bin,Len) of
+ true ->
+ case printable_unicode(Bin, Len, [], io:printable_range()) of
+ {_, <<>>, L} ->
+ {byte_size(Bin) =:= length(L), L};
+ {NC, Bin1, L} when D > 0, Len - NC >= D ->
+ {byte_size(Bin)-byte_size(Bin1) =:= length(L), true, L};
+ {_NC, _Bin, _L} ->
+ false
+ end;
+ false ->
+ printable_bin(Bin, Len, D, latin1)
end.
printable_bin1(_Bin, _Start, 0) ->
@@ -522,6 +527,15 @@ printable_latin1_list([$\e | Cs], N) -> printable_latin1_list(Cs, N - 1);
printable_latin1_list([], _) -> all;
printable_latin1_list(_, N) -> N.
+valid_utf8(<<>>,_) ->
+ true;
+valid_utf8(_,0) ->
+ true;
+valid_utf8(<<_/utf8, R/binary>>,N) ->
+ valid_utf8(R,N-1);
+valid_utf8(_,_) ->
+ false.
+
printable_unicode(<<C/utf8, R/binary>>=Bin, I, L, Range) when I > 0 ->
case printable_char(C,Range) of
true ->