Skip to content

Commit

Permalink
Avoid unnecessary escaping of ~ in formatted terms
Browse files Browse the repository at this point in the history
  • Loading branch information
eproxus committed Sep 27, 2023
1 parent b2611c2 commit 7a5850c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ The format is based on [Keep a Changelog], and this project adheres to

## [Unreleased]

## [0.4.3] - 2023-09-27

### Fixed

- Fix crashing when formatting term containing multiple `~` control sequences
(thanks @maxno-kivra!)

## [0.4.2] - 2023-09-05

### Fixed
Expand Down Expand Up @@ -88,7 +95,9 @@ The format is based on [Keep a Changelog], and this project adheres to
- Fix for older Erlang version [\#2](https://github.com/eproxus/unite/pull/2) ([JonGretar](https://github.com/JonGretar))


[Unreleased]: https://github.com/eproxus/unite/compare/v0.4.1...HEAD
[Unreleased]: https://github.com/eproxus/unite/compare/v0.4.3...HEAD
[0.4.3]: https://github.com/eproxus/unite/compare/v0.4.2...v0.4.3
[0.4.2]: https://github.com/eproxus/unite/compare/v0.4.1...v0.4.2
[0.4.1]: https://github.com/eproxus/unite/compare/v0.4.0...v0.4.1
[0.4.0]: https://github.com/eproxus/unite/compare/v0.3.2...v0.4.0
[0.3.2]: https://github.com/eproxus/unite/compare/v0.3.1...v0.3.2
Expand Down
10 changes: 3 additions & 7 deletions src/unite_compact.erl
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ format_info(Failure, {cancelled, undefined}) ->
diff_prep_term(Term) ->
Pretty = format_term(Term, 0, 0),
TermSplit = "([,\\[\\]\\{\\}]|\\s+=>\\s+|#\\{)",
re:split(string:replace(Pretty, "~", "~~"), TermSplit, [trim]).
re:split(Pretty, TermSplit, [trim]).

format_term(Term, Indent, Outer) ->
io_lib_pretty:print(Term, Indent, columns() - Outer, -1).

Expand Down Expand Up @@ -499,12 +500,7 @@ format(Format) -> format("~ts", [Format]).
format(Format, Data) -> io:format(iolist_to_binary(Format), Data).

format_exception(I, Class, Reason, StackTrace, StackFun, FormatFun) ->
escape_exception(?ERROR_FORMATTER:format_exception(I, Class, Reason, StackTrace, StackFun, FormatFun)).

escape_exception(String) ->
% If the stack trace contained a call to io:format with a control sequence,
% we need to escape it since we use io:format ourselves later on:
string:replace(String, "~", "~~", all).
?ERROR_FORMATTER:format_exception(I, Class, Reason, StackTrace, StackFun, FormatFun).

group_by(Fun, List) ->
lists:foldl(fun(Item, Acc) ->
Expand Down
6 changes: 3 additions & 3 deletions test/unite_demo_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ assert_equal_test() ->
value => 1,
list => [a, b, c, d, e],
tuple => {foo, bar},
binary => <<"test string ~p">>,
binary => <<"test string ~p ~s">>,
map => #{a => 1, b => 2, c => value}
},
(fun() -> #{
value => 2,
list => [a, b, x, d, e],
tuple => {foo, baz},
binary => <<"test binary ~p">>,
binary => <<"test binary ~p ~s">>,
map => #{foo => baz, bar => qux}
} end)()
).
Expand Down Expand Up @@ -69,7 +69,7 @@ bad_test_instantiator_test_() -> {foreach, fun() -> ok end, [fun(_) -> ok end]}.
io_format_test() ->
% If a control code sneaks into the stack trace, we have to make sure we can
% io:format it properly when Unite pretty prints:
apply(io, format, ["~p", []]).
apply(io, format, ["~p ~s", []]).

% Generators

Expand Down

0 comments on commit 7a5850c

Please sign in to comment.