Skip to content

Commit 7ca8d92

Browse files
authored
Merge pull request #11502 from dgud/dgud/stdlib/format-improvements
stdlib format improvements OTP-20232
2 parents 23ae14c + 5d1bd8f commit 7ca8d92

4 files changed

Lines changed: 167 additions & 64 deletions

File tree

lib/stdlib/src/io_lib.erl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,22 @@ string_bin_escape_latin1(_, Orig, _, Acc, Skip, Len) ->
10841084
false -> [Acc | binary_part(Orig, Skip, Len)]
10851085
end.
10861086

1087+
string_bin_escape_unicode(<<16#C2, Byte, Rest/binary>>, Orig, Q, Acc, Skip0, Len)
1088+
when Byte >= 16#80, Byte =< 16#9F ->
1089+
%% C1 control characters (U+0080-U+009F) - octal escape to match list path.
1090+
C1 = (Byte bsr 6) + $0,
1091+
C2 = ((Byte bsr 3) band 7) + $0,
1092+
C3 = (Byte band 7) + $0,
1093+
Escape = [$\\,C1,C2,C3],
1094+
case Len =:= 0 of
1095+
true ->
1096+
Skip = Skip0 + 2,
1097+
string_bin_escape_unicode(Rest, Orig, Q, [Acc | Escape], Skip, 0);
1098+
false ->
1099+
Skip = Skip0 + Len + 2,
1100+
Part = binary_part(Orig, Skip0, Len),
1101+
string_bin_escape_unicode(Rest, Orig, Q, [Acc, Part | Escape], Skip, 0)
1102+
end;
10871103
string_bin_escape_unicode(<<Byte, Rest/binary>>, Orig, Q, Acc, Skip0, Len) when Byte > 127 ->
10881104
string_bin_escape_unicode(Rest, Orig, Q, Acc, Skip0, Len+1);
10891105
string_bin_escape_unicode(<<Byte, Rest/binary>>, Orig, Q, Acc, Skip0, Len) ->

lib/stdlib/src/io_lib_format.erl

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ build(Cs) ->
103103

104104
build(Cs, Options) ->
105105
CharsLimit = get_option(chars_limit, Options, -1),
106-
Res1 = build_small(Cs),
106+
Res1 = build_small(Cs, CharsLimit),
107107
{P, S, W, Other} = count_small(Res1),
108108
case P + S + W of
109109
0 ->
@@ -128,7 +128,7 @@ build_bin(Cs) ->
128128

129129
build_bin(Cs, Options) ->
130130
CharsLimit = get_option(chars_limit, Options, -1),
131-
Res1 = build_small_bin(Cs),
131+
Res1 = build_small_bin(Cs, CharsLimit),
132132
{P, S, W, Other} = count_small(Res1),
133133
case P + S + W of
134134
0 ->
@@ -262,7 +262,12 @@ field_width(F, Fmt, Args) when F >= 0 ->
262262
{F,right,Fmt,Args}.
263263

264264
precision([$.|Fmt], Args) ->
265-
field_value(Fmt, Args);
265+
case field_value(Fmt, Args) of
266+
{P, _, _} when is_integer(P), P < 0 ->
267+
error(badarg);
268+
Result ->
269+
Result
270+
end;
266271
precision(Fmt, Args) ->
267272
{none,Fmt,Args}.
268273

@@ -344,13 +349,13 @@ count_small([], #{p := P, s := S, w := W, other := Other}) ->
344349
%% be smart when calculating indentation for characters in format.
345350

346351
build_small([#{control_char := C, args := As, width := F, adjust := Ad,
347-
precision := P, pad_char := Pad, encoding := Enc}=CC | Cs]) ->
348-
case control_small(C, As, F, Ad, P, Pad, Enc) of
349-
not_small -> [CC | build_small(Cs)];
350-
S -> lists:flatten(S) ++ build_small(Cs)
352+
precision := P, pad_char := Pad, encoding := Enc}=CC | Cs], CL) ->
353+
case control_small(C, As, limit_field(F, CL), Ad, P, Pad, Enc) of
354+
not_small -> [CC | build_small(Cs, CL)];
355+
S -> lists:flatten(S) ++ build_small(Cs, CL)
351356
end;
352-
build_small([C|Cs]) -> [C|build_small(Cs)];
353-
build_small([]) -> [].
357+
build_small([C|Cs], CL) -> [C|build_small(Cs, CL)];
358+
build_small([], _CL) -> [].
354359

355360
build_limited([#{control_char := C, args := As, width := F, adjust := Ad,
356361
precision := P, pad_char := Pad, encoding := Enc,
@@ -390,22 +395,22 @@ decr_pc($P, Pc) -> Pc - 1;
390395
decr_pc(_, Pc) -> Pc.
391396

392397
build_small_bin([#{control_char := C, args := As, width := F, adjust := Ad,
393-
precision := P, pad_char := Pad, encoding := Enc}=CC | Cs]) ->
394-
case control_small(C, As, F, Ad, P, Pad, Enc) of
398+
precision := P, pad_char := Pad, encoding := Enc}=CC | Cs], CL) ->
399+
case control_small(C, As, limit_field(F, CL), Ad, P, Pad, Enc) of
395400
not_small ->
396-
[CC | build_small_bin(Cs)];
401+
[CC | build_small_bin(Cs, CL)];
397402
[$\n|_] = NL ->
398-
[NL | build_small_bin(Cs)];
403+
[NL | build_small_bin(Cs, CL)];
399404
S ->
400405
SBin = unicode:characters_to_binary(S, Enc, unicode),
401406
true = is_binary(SBin),
402-
[SBin | build_small_bin(Cs)]
407+
[SBin | build_small_bin(Cs, CL)]
403408
end;
404-
build_small_bin([$\t|Cs]) ->
405-
[$\t | build_small_bin(Cs)];
406-
build_small_bin([C|Cs]) ->
407-
[C | build_small_bin(Cs)];
408-
build_small_bin([]) ->
409+
build_small_bin([$\t|Cs], CL) ->
410+
[$\t | build_small_bin(Cs, CL)];
411+
build_small_bin([C|Cs], CL) ->
412+
[C | build_small_bin(Cs, CL)];
413+
build_small_bin([], _CL) ->
409414
[].
410415

411416
build_limited_bin([#{control_char := C, args := As, width := F, adjust := Ad,
@@ -465,12 +470,12 @@ indentation([], I) ->
465470
indentation_bin(Bin, I) ->
466471
indentation_bin(Bin, Bin, 0, 0, I).
467472

468-
indentation_bin(<<$\n, Cs/binary>>, Orig, _Start, N,_I) ->
469-
indentation_bin(Cs, Orig, N+1, 0, 0);
473+
indentation_bin(<<$\n, Cs/binary>>, Orig, Start, N, _I) ->
474+
indentation_bin(Cs, Orig, Start+N+1, 0, 0);
470475
indentation_bin(<<$\t, Cs/binary>>, Orig, Start, N, I0) ->
471476
Part = binary:part(Orig, Start, N),
472477
PSz = string:length(Part),
473-
indentation_bin(Cs, Orig, N+1, N+1, ((I0+PSz + 8) div 8) * 8);
478+
indentation_bin(Cs, Orig, Start+N+1, 0, ((I0+PSz + 8) div 8) * 8);
474479
indentation_bin(<<_, Cs/binary>>, Orig, Start, N, I) ->
475480
indentation_bin(Cs, Orig, Start, N+1, I);
476481
indentation_bin(<<>>, Orig, Start, N, I) ->
@@ -539,13 +544,13 @@ control_limited($s, [L0], F, Adj, P, Pad, Enc, _Str, _Ord, CL, _I) ->
539544
end;
540545
control_limited($w, [A], F, Adj, P, Pad, Enc, _Str, Ord, CL, _I) ->
541546
Chars = io_lib:write(A, -1, Enc, Ord, CL),
542-
term(Chars, F, Adj, P, Pad, Enc);
547+
term(Chars, limit_field(F, CL), Adj, P, Pad, Enc);
543548
control_limited($p, [A], F, Adj, P, Pad, Enc, Str, Ord, CL, I) ->
544549
print(A, -1, F, Adj, P, Pad, Enc, list, Str, Ord, CL, I);
545550
control_limited($W, [A,Depth], F, Adj, P, Pad, Enc, _Str, Ord, CL, _I)
546551
when is_integer(Depth) ->
547552
Chars = io_lib:write(A, Depth, Enc, Ord, CL),
548-
term(Chars, F, Adj, P, Pad, Enc);
553+
term(Chars, limit_field(F, CL), Adj, P, Pad, Enc);
549554
control_limited($P, [A,Depth], F, Adj, P, Pad, Enc, Str, Ord, CL, I)
550555
when is_integer(Depth) ->
551556
print(A, Depth, F, Adj, P, Pad, Enc, list, Str, Ord, CL, I).
@@ -555,13 +560,13 @@ control_limited_bin($s, [L0], F, Adj, P, Pad, Enc, _Str, _Ord, CL, _I) ->
555560
string_bin(B, Sz, limit_field(F, CL), Adj, P, Pad, Enc);
556561
control_limited_bin($w, [A], F, Adj, P, Pad, Enc, _Str, Ord, CL, I) ->
557562
{Chars, Sz} = io_lib:write_bin(A, -1, Enc, Ord, CL),
558-
term_bin(Chars, F, Adj, P, Pad, Enc, Sz, I);
563+
term_bin(Chars, limit_field(F, CL), Adj, P, Pad, Enc, Sz, I);
559564
control_limited_bin($p, [A], F, Adj, P, Pad, Enc, Str, Ord, CL, I) ->
560565
print(A, -1, F, Adj, P, Pad, Enc, binary, Str, Ord, CL, I);
561566
control_limited_bin($W, [A,Depth], F, Adj, P, Pad, Enc, _Str, Ord, CL, I)
562567
when is_integer(Depth) ->
563568
{Chars, Sz} = io_lib:write_bin(A, Depth, Enc, Ord, CL),
564-
term_bin(Chars, F, Adj, P, Pad, Enc, Sz, I);
569+
term_bin(Chars, limit_field(F, CL), Adj, P, Pad, Enc, Sz, I);
565570
control_limited_bin($P, [A,Depth], F, Adj, P, Pad, Enc, Str, Ord, CL, I)
566571
when is_integer(Depth) ->
567572
print(A, Depth, F, Adj, P, Pad, Enc, binary, Str, Ord, CL, I).
@@ -1061,14 +1066,11 @@ adjust(Data, Pad, right) -> [Pad|Data].
10611066

10621067
%% Flatten and truncate a deep list to at most N elements.
10631068

1064-
flat_trunc(List, N, latin1) when is_list(List), is_integer(N), N >= 0 ->
1065-
{S, _} = lists:split(N, lists:flatten(List)),
1066-
S;
1067-
flat_trunc(Str, N, unicode) when is_integer(N), N >= 0 ->
1068-
string:slice(Str, 0, N);
10691069
flat_trunc(Bin, N, latin1) when is_binary(Bin), is_integer(N), N >= 0 ->
10701070
{B, _} = split_binary(Bin, N),
1071-
B.
1071+
B;
1072+
flat_trunc(Str, N, _) when is_integer(N), N >= 0 ->
1073+
string:slice(Str, 0, N).
10721074

10731075
%% A deep version of lists:duplicate/2
10741076

lib/stdlib/src/io_lib_pretty.erl

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ pp_binary(S, N, _N0, Ind) ->
436436
end.
437437

438438

439-
-define(IND(Spaces), (list_to_binary(Spaces))/binary).
439+
-define(IND(Spaces), Spaces/binary).
440440

441441
pp_bin({_S,Len,_,_} = If, Col, Ll, M, _TInd, _Ind, LD, NL, W, Pos, Acc)
442442
when Len < Ll - Col - LD, Len + W + LD =< M ->
@@ -489,29 +489,33 @@ pp_map_bin([], _Col, _Ll, _M, _TInd, _Ind, _LD, NL, _W, Pos, Acc) ->
489489
{<<Acc/binary, $}>>, NL, Pos+1}; % cannot happen
490490
pp_map_bin({dots, _, _, _}, _Col, _Ll, _M, _TInd, _Ind, _LD, NL, _W, Pos, Acc) ->
491491
{<<Acc/binary, "...}">>, NL, Pos+4}; % cannot happen
492-
pp_map_bin([P | Ps], Col, Ll, M, TInd, Ind, LD, NL0, W, Pos0, Acc) ->
493-
{PS, NL, Pos, PW} = pp_pair_bin(P, Col, Ll, M, TInd, Ind, last_depth(Ps, LD), NL0, W, Pos0, Acc),
494-
pp_pairs_tail_bin(Ps, Col, Col+PW, Ll, M, TInd, Ind, LD, NL, PW, Pos, PS).
492+
pp_map_bin([P | Ps], Col, Ll, M, TInd, Ind0, LD, NL0, W, Pos0, Acc) ->
493+
I = map_value_indent(TInd),
494+
Ind = iolist_to_binary(Ind0),
495+
ValInd = iolist_to_binary(indent(I, Ind)),
496+
{PS, NL, Pos, PW} = pp_pair_bin(P, Col, Ll, M, TInd, Ind, I, ValInd, last_depth(Ps, LD), NL0, W, Pos0, Acc),
497+
pp_pairs_tail_bin(Ps, Col, Col+PW, Ll, M, TInd, Ind, I, ValInd, LD, NL, PW, Pos, PS).
495498

496-
pp_pairs_tail_bin([], _Col0, _Col, _Ll, _M, _TInd, _Ind, _LD, NL, _W, Pos, Acc) ->
499+
pp_pairs_tail_bin([], _Col0, _Col, _Ll, _M, _TInd, _Ind, _I, _ValInd, _LD, NL, _W, Pos, Acc) ->
497500
{<<Acc/binary, $}>>, NL, Pos+1};
498-
pp_pairs_tail_bin({dots, _, _, _}, _Col0, _Col, _M, _Ll, _TInd, _Ind, _L, NL, _W, Pos, Acc) ->
501+
pp_pairs_tail_bin({dots, _, _, _}, _Col0, _Col, _M, _Ll, _TInd, _Ind, _I, _ValInd, _L, NL, _W, Pos, Acc) ->
499502
{<<Acc/binary, ",...}">>, NL, Pos+5};
500-
pp_pairs_tail_bin([{_, Len, _, _}=P | Ps], Col0, Col, Ll, M, TInd, Ind, LD, NL0, W, Pos, Acc) ->
503+
pp_pairs_tail_bin([{_, Len, _, _}=P | Ps], Col0, Col, Ll, M, TInd, Ind0, I, ValInd, LD, NL0, W, Pos, Acc) ->
501504
LD1 = last_depth(Ps, LD),
502505
ELen = 1+Len,
503506
if
504507
LD1 =:= 0, ELen+1 < Ll-Col, W+ELen+1 =< M, ?ATM_PAIR(P);
505508
LD1 > 0, ELen < Ll-Col-LD1, W+ELen+LD1 =< M, ?ATM_PAIR(P) ->
506-
pp_pairs_tail_bin(Ps, Col0, Col+ELen, Ll, M, TInd, Ind, LD, NL0, W+ELen, Pos+ELen,
509+
pp_pairs_tail_bin(Ps, Col0, Col+ELen, Ll, M, TInd, Ind0, I, ValInd, LD, NL0, W+ELen, Pos+ELen,
507510
write_bin(P, <<Acc/binary, $,>>));
508511
true ->
509-
{PS, NL, Pos1, PW} = pp_pair_bin(P, Col0, Ll, M, TInd, Ind, LD1, NL0+1, 0, Col0,
512+
Ind = iolist_to_binary(Ind0),
513+
{PS, NL, Pos1, PW} = pp_pair_bin(P, Col0, Ll, M, TInd, Ind, I, ValInd, LD1, NL0+1, 0, Col0,
510514
<<Acc/binary, $,, $\n, ?IND(Ind)>>),
511-
pp_pairs_tail_bin(Ps, Col0, Col0+PW, Ll, M, TInd, Ind, LD, NL, PW, Pos1, PS)
515+
pp_pairs_tail_bin(Ps, Col0, Col0+PW, Ll, M, TInd, Ind, I, ValInd, LD, NL, PW, Pos1, PS)
512516
end.
513517

514-
pp_pair_bin({_, Len, _, _}=Pair, Col, Ll, M, _TInd, _Ind, LD, NL, W, Pos, Acc)
518+
pp_pair_bin({_, Len, _, _}=Pair, Col, Ll, M, _TInd, _Ind, _I, _ValInd, LD, NL, W, Pos, Acc)
515519
when Len < Ll - Col - LD, Len+W+LD =< M ->
516520
{write_bin(Pair, Acc),
517521
NL,
@@ -522,12 +526,10 @@ pp_pair_bin({_, Len, _, _}=Pair, Col, Ll, M, _TInd, _Ind, LD, NL, W, Pos, Acc)
522526
true ->
523527
Ll % force nl
524528
end};
525-
pp_pair_bin({{map_pair, K, V}, _Len, _, _}, Col0, Ll, M, TInd, Ind0, LD, NL0, W, Pos0, Acc0) ->
526-
I = map_value_indent(TInd),
527-
Ind = indent(I, Ind0),
529+
pp_pair_bin({{map_pair, K, V}, _Len, _, _}, Col0, Ll, M, TInd, Ind0, I, ValInd, LD, NL0, W, Pos0, Acc0) ->
528530
{Acc1, NL1, _} = pp_bin(K, Col0, Ll, M, TInd, Ind0, LD, NL0, W, Pos0, Acc0),
529-
{Acc2, NL, Pos} = pp_bin(V, Col0+I, Ll, M, TInd, Ind, LD, NL1+1, 0, Col0+I,
530-
<<Acc1/binary, " =>\n", ?IND(Ind)>>),
531+
{Acc2, NL, Pos} = pp_bin(V, Col0+I, Ll, M, TInd, ValInd, LD, NL1+1, 0, Col0+I,
532+
<<Acc1/binary, " =>\n", ?IND(ValInd)>>),
531533
{Acc2, NL, Pos, Ll}.
532534

533535
pp_record_bin([], _Nlen, _Col, _Ll, _M, _TInd, _Ind, _LD, NL, _W, Pos, Acc) ->
@@ -536,7 +538,8 @@ pp_record_bin({dots, _, _, _}, _Nlen, _Col, _Ll, _M, _TInd, _Ind, _LD, NL, _W, P
536538
{<<Acc/binary, "...}">>, NL, Pos+4};
537539
pp_record_bin([F | Fs], Nlen, Col0, Ll, M, TInd, Ind0, LD, NL0, W0, Pos0, Acc) ->
538540
Nind = Nlen+1,
539-
{Col, Ind, S, W} = rec_indent(Nind, TInd, Col0, Ind0, W0),
541+
{Col, Ind, S0, W} = rec_indent(Nind, TInd, Col0, Ind0, W0),
542+
S = iolist_to_binary(S0),
540543
{Pos1, NL1} = if W == 0 -> {Col, NL0+1}; true -> {Pos0, NL0} end,
541544
{FS, NL, Pos, FW} = pp_field_bin(F, Col, Ll, M, TInd, Ind, last_depth(Fs, LD), NL1, W, Pos1,
542545
<<Acc/binary, ?IND(S)>>),
@@ -546,15 +549,16 @@ pp_fields_tail_bin([], _Col0, _Col, _Ll, _M, _TInd, _Ind, _LD, NL, _W, Pos, Acc)
546549
{<<Acc/binary, $}>>, NL, Pos+1};
547550
pp_fields_tail_bin({dots, _, _ ,_}, _Col0, _Col, _M, _Ll, _TInd, _Ind, _LD, NL, _W, Pos, Acc) ->
548551
{<<Acc/binary, ",...}">>, NL, Pos+5};
549-
pp_fields_tail_bin([{_, Len, _, _}=F | Fs], Col0, Col, Ll, M, TInd, Ind, LD, NL0, W, Pos0, Acc) ->
552+
pp_fields_tail_bin([{_, Len, _, _}=F | Fs], Col0, Col, Ll, M, TInd, Ind0, LD, NL0, W, Pos0, Acc) ->
550553
LD1 = last_depth(Fs, LD),
551554
ELen = 1+Len,
552555
if
553556
LD1 =:= 0, ELen+1 < Ll-Col, W+ELen+1 =< M, ?ATM_FLD(F);
554557
LD1 > 0, ELen < Ll-Col-LD1, W+ELen+LD1 =< M, ?ATM_FLD(F) ->
555-
pp_fields_tail_bin(Fs, Col0, Col+ELen, Ll, M, TInd, Ind, LD, NL0, W+ELen, Pos0+ELen,
558+
pp_fields_tail_bin(Fs, Col0, Col+ELen, Ll, M, TInd, Ind0, LD, NL0, W+ELen, Pos0+ELen,
556559
write_field_bin(F, <<Acc/binary, $,>>));
557560
true ->
561+
Ind = iolist_to_binary(Ind0),
558562
{FS, NL, Pos, FW} = pp_field_bin(F, Col0, Ll, M, TInd, Ind, LD1, NL0+1, 0, Col0,
559563
<<Acc/binary, $,, $\n, ?IND(Ind)>>),
560564
pp_fields_tail_bin(Fs, Col0, Col0+FW, Ll, M, TInd, Ind, LD, NL, FW, Pos, FS)
@@ -570,7 +574,8 @@ pp_field_bin({_, Len, _, _}=Fl, Col, Ll, M, _TInd, _Ind, LD, NL, W, Pos, Acc)
570574
true -> Ll % force nl
571575
end};
572576
pp_field_bin({{field, Name0, NameL, F},_,_, _}, Col0, Ll, M, TInd, Ind0, LD, NL0, W0, Pos0, Acc0) ->
573-
{Col, Ind, S, W} = rec_indent(NameL, TInd, Col0, Ind0, W0+NameL),
577+
{Col, Ind, S0, W} = rec_indent(NameL, TInd, Col0, Ind0, W0+NameL),
578+
S = iolist_to_binary(S0),
574579
Name = unicode:characters_to_binary(Name0),
575580
{Acc, NL, Pos} =
576581
case W of
@@ -591,15 +596,16 @@ pp_list_bin([E | Es], Col0, Ll, M, TInd, Ind, LD, S, C, NL0, W, Pos0, Acc) ->
591596

592597
pp_tail_bin([], _Col0, _Col, _Ll, _M, _TInd, _Ind, _LD, _S, C, NL, _W, Pos, Acc) ->
593598
{<<Acc/binary, C>>, NL, Pos+1};
594-
pp_tail_bin([{_, Len, _, _}=E | Es], Col0, Col, Ll, M, TInd, Ind, LD, S, C, NL0, W, Pos0, Acc) ->
599+
pp_tail_bin([{_, Len, _, _}=E | Es], Col0, Col, Ll, M, TInd, Ind0, LD, S, C, NL0, W, Pos0, Acc) ->
595600
LD1 = last_depth(Es, LD),
596601
ELen = 1+Len,
597602
if
598603
LD1 =:= 0, ELen+1 < Ll-Col, W+ELen+1 =< M, ?ATM(E);
599604
LD1 > 0, ELen < Ll-Col-LD1, W+ELen+LD1 =< M, ?ATM(E) ->
600-
pp_tail_bin(Es, Col0, Col+ELen, Ll, M, TInd, Ind, LD, S, C, NL0, W+ELen, Pos0+ELen,
605+
pp_tail_bin(Es, Col0, Col+ELen, Ll, M, TInd, Ind0, LD, S, C, NL0, W+ELen, Pos0+ELen,
601606
write_bin(E, <<Acc/binary, $,>>));
602607
true ->
608+
Ind = iolist_to_binary(Ind0),
603609
{ES, NL, Pos, WE} = pp_element_bin(E, Col0, Ll, M, TInd, Ind, LD1, NL0+1, 0, Col0,
604610
<<Acc/binary, $,, $\n, ?IND(Ind)>>),
605611
pp_tail_bin(Es, Col0, Col0+WE, Ll, M, TInd, Ind, LD, S, C, NL, WE, Pos, ES)
@@ -612,7 +618,8 @@ pp_tail_bin({_, Len, _, _}=E, _Col0, Col, Ll, M, _TInd, _Ind, LD, S, C, NL, W, P
612618
?ATM(E) ->
613619
Acc1 = write_bin(E, <<Acc/binary, S>>),
614620
{<<Acc1/binary, C>>, NL, Pos+1+Len};
615-
pp_tail_bin(E, Col0, _Col, Ll, M, TInd, Ind, LD, S, C, NL0, _W, _Pos0, Acc) ->
621+
pp_tail_bin(E, Col0, _Col, Ll, M, TInd, Ind0, LD, S, C, NL0, _W, _Pos0, Acc) ->
622+
Ind = iolist_to_binary(Ind0),
616623
{Acc1, NL, Pos} = pp_bin(E, Col0, Ll, M, TInd, Ind, LD+1, NL0+1, 0, Col0,
617624
<<Acc/binary, S, $\n, ?IND(Ind)>>),
618625
{<<Acc1/binary, C>>, NL, Pos+1}.
@@ -629,38 +636,41 @@ pp_binary_bin(Orig, Col, Ll, M, Ind, LD, NL, W, Pos, Acc) ->
629636
<<$<,$<,Rest/binary>> = Orig,
630637
pp_binary_bin_ind(Rest, Orig, Ind, 0, 2, N, N, NL, Pos, Acc).
631638

632-
pp_binary_bin_ind(<<_,$,, Rest/binary>>, Orig, Ind, S, I, N, N0, NL, Pos, Acc) ->
639+
pp_binary_bin_ind(<<_,$,, Rest/binary>>, Orig, Ind0, S, I, N, N0, NL, Pos, Acc) ->
633640
N1 = N-2,
634641
case N1 < 0 of
635642
false ->
636-
pp_binary_bin_ind(Rest, Orig, Ind, S, I+2, N1, N0, NL, Pos, Acc);
643+
pp_binary_bin_ind(Rest, Orig, Ind0, S, I+2, N1, N0, NL, Pos, Acc);
637644
true ->
638645
Part = binary:part(Orig, S, I),
646+
Ind = iolist_to_binary(Ind0),
639647
pp_binary_bin_ind(Rest, Orig, Ind, S+I, 2, N0-2, N0, NL+1, 0,
640648
<<Acc/binary, Part/binary, $\n, ?IND(Ind)>>)
641649
end;
642-
pp_binary_bin_ind(<<_,_,$,, Rest/binary>>, Orig, Ind, S, I, N, N0, NL, Pos, Acc) ->
650+
pp_binary_bin_ind(<<_,_,$,, Rest/binary>>, Orig, Ind0, S, I, N, N0, NL, Pos, Acc) ->
643651
N1 = N-3,
644652
case N1 < 0 of
645653
false ->
646-
pp_binary_bin_ind(Rest, Orig, Ind, S, I+3, N1, N0, NL, Pos, Acc);
654+
pp_binary_bin_ind(Rest, Orig, Ind0, S, I+3, N1, N0, NL, Pos, Acc);
647655
true ->
648656
Part = binary:part(Orig, S, I),
657+
Ind = iolist_to_binary(Ind0),
649658
pp_binary_bin_ind(Rest, Orig, Ind, S+I, 3, N0-3, N0, NL+1, Pos,
650659
<<Acc/binary, Part/binary, $\n, ?IND(Ind)>>)
651660
end;
652-
pp_binary_bin_ind(<<_,_,_,$,, Rest/binary>>, Orig, Ind, S, I, N, N0, NL, Pos, Acc) ->
661+
pp_binary_bin_ind(<<_,_,_,$,, Rest/binary>>, Orig, Ind0, S, I, N, N0, NL, Pos, Acc) ->
653662
N1 = N-4,
654663
case N1 < 0 of
655664
false ->
656-
pp_binary_bin_ind(Rest, Orig, Ind, S, I+4, N1, N0, NL, Pos, Acc);
665+
pp_binary_bin_ind(Rest, Orig, Ind0, S, I+4, N1, N0, NL, Pos, Acc);
657666
true ->
658667
Part = binary:part(Orig, S, I),
668+
Ind = iolist_to_binary(Ind0),
659669
pp_binary_bin_ind(Rest, Orig, Ind, S+I, 4, N0-4, N0, NL+1, Pos,
660670
<<Acc/binary, Part/binary, $\n, ?IND(Ind)>>)
661671
end;
662-
pp_binary_bin_ind(Bin, Orig, Ind, S, I, N, _, NL, Pos0, Acc) ->
663-
Col = iolist_size(Ind)+1,
672+
pp_binary_bin_ind(Bin, Orig, Ind0, S, I, N, _, NL, Pos0, Acc) ->
673+
Col = iolist_size(Ind0)+1,
664674
Pos = if Pos0 =:= 0 -> Col; true -> Pos0 end,
665675
case (byte_size(Bin)-2) > N of %% Don't count bin closing ">>"
666676
false when S =:= 0 ->
@@ -674,6 +684,7 @@ pp_binary_bin_ind(Bin, Orig, Ind, S, I, N, _, NL, Pos0, Acc) ->
674684
Part1 = binary:part(Orig, S, I),
675685
Sz = byte_size(Orig) - (S+I),
676686
Part2 = binary:part(Orig, S+I, Sz),
687+
Ind = iolist_to_binary(Ind0),
677688
{<<Acc/binary, Part1/binary, $\n, ?IND(Ind), Part2/binary>>,
678689
NL+1, Col+Sz}
679690
end.

0 commit comments

Comments
 (0)