Skip to content

Commit 359cd0a

Browse files
committed
Bugfix: irc_text:truncate/3 goes out of bounds with a non-empty Ellipsis.
1 parent 962c2ae commit 359cd0a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/lib/irc_text.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ truncate(Text, Limit) -> truncate(Text, Limit, <<>>).
2525

2626
truncate(Text, Limit, Ellipsis) ->
2727
E = unicode:characters_to_binary([Ellipsis]),
28-
truncate(Text, Limit + byte_size(E), E, 0, []).
28+
truncate(Text, Limit - byte_size(E), E, 0, []).
2929

3030
truncate(Text, Limit, Ellipsis, Size, Acc) ->
3131
case string:next_grapheme(Text) of

test/irc_text_tests.erl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
%%--------------------------------------------------------------------
2+
%% Copyright (C) 2023 hyperimpose.org
3+
%%
4+
%% This file is part of irc.
5+
%%
6+
%% This program is free software: you can redistribute it and/or modify
7+
%% it under the terms of the GNU Affero General Public License as published
8+
%% by the Free Software Foundation, version 3.
9+
%%
10+
%% This program is distributed in the hope that it will be useful,
11+
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
%% GNU Affero General Public License for more details.
14+
%%
15+
%% You should have received a copy of the GNU Affero General Public License
16+
%% along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
%%--------------------------------------------------------------------
18+
19+
-module(irc_text_tests).
20+
21+
-include_lib("eunit/include/eunit.hrl").
22+
23+
24+
%%% truncate/2, truncate/3
25+
26+
truncate_bounds_test_() ->
27+
L = fun (X) -> byte_size(unicode:characters_to_binary(X)) end,
28+
29+
[%% Without Ellipsis
30+
?_assert(11 >= L(irc_text:truncate("Hello, World!", 11))),
31+
?_assert(26 >= L(irc_text:truncate("Γειά σου, Κόσμε!", 26))),
32+
%% With Ellipsis
33+
?_assert(11 >= L(irc_text:truncate("Hello, World!", 11, <<"...">>))),
34+
?_assert(26 >= L(irc_text:truncate("Γειά σου, Κόσμε!", 26, <<"...">>)))].

0 commit comments

Comments
 (0)