*** /usr/local/src/lib/erlang/lib/stdlib-1.9.2/src/io_lib_format.erl Mon Jun 4 16:21:40 2001 --- ./io_lib_format.erl Sat Oct 20 01:51:06 2001 *************** *** 105,110 **** --- 105,111 ---- collect_cc([$e|Fmt], [A|Args]) -> {$e,[A],Fmt,Args}; collect_cc([$f|Fmt], [A|Args]) -> {$f,[A],Fmt,Args}; collect_cc([$g|Fmt], [A|Args]) -> {$g,[A],Fmt,Args}; + collect_cc([$h|Fmt], [A|Args]) -> {$h,[A],Fmt,Args}; collect_cc([$c|Fmt], [A|Args]) -> {$c,[A],Fmt,Args}; collect_cc([$~|Fmt], Args) -> {$~,[],Fmt,Args}; collect_cc([$n|Fmt], Args) -> {$n,[],Fmt,Args}; *************** *** 166,171 **** --- 167,174 ---- fwrite_f(A, F, Adj, P, Pad); control($g, [A], F, Adj, P, Pad, I) when float(A) -> fwrite_g(A, F, Adj, P, Pad); + control($h, [A], F, Adj, P, Pad, I) when integer(A); binary(A) -> + fwrite_h(A, F, Adj, P, Pad); control($c, [A], F, Adj, P, Pad, I) when integer(A) -> char(A band 255, F, Adj, P, Pad); control($~, [], F, Adj, P, Pad, I) -> char($~, F, Adj, P, Pad); *************** *** 312,317 **** --- 315,364 ---- fwrite_f(Fl, F, Adj, P-4, Pad); fwrite_g(Fl, F, Adj, P, Pad) -> fwrite_e(Fl, F, Adj, P, Pad). + + %% Hex output of integers or binaries. + %% + %% Integer support is lame; Width, alignment and precision are + %% not allowed. Nor are negative numbers. + %% + %% Binary support is less lame. Width is allowed, though not + %% below 7, which means you can't print <(7a)>. + + fwrite_h(Int, none, right, none, _Pad) when integer(Int) -> + to_hex(Int); + fwrite_h(Bin, Width, right, none, _Pad) when binary(Bin) -> + case size(Bin) * 3 - 1 + 4 > Width of + true -> + true = (Width > 6), + Headlen = (Width - 6) div 3, + true = Headlen >= 0, + <> = Bin, + ["<(", bin2hex(Head), "...)>"]; + _ -> + ["<(", bin2hex(Bin), ")>"] + end. + + bin2hex(Bin) -> + L = lists:map(fun(X) -> + [$, , hex_digit(X div 16), hex_digit(X rem 16)] + end, + binary_to_list(Bin)), + case L of + [[$,|H]|T] -> [H|T]; % remove leading comma + [] -> [] + end. + + to_hex(0) -> "0"; + to_hex(N) -> to_hex(N, ""). + to_hex(0, Acc) -> Acc; + to_hex(N, Acc) -> + Digit = hex_digit(N rem 16), + Rest = N div 16, + to_hex(Rest, [Digit|Acc]). + + hex_digit(N) -> + element(N + 1, + {$0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $a, $b, $c, $d, $e, $f}). %% string(String, Field, Adjust, Precision, PadChar)