Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:19
erlang
0969-stdlib-fix-lists-sublist-3-if-Start-length...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0969-stdlib-fix-lists-sublist-3-if-Start-length-L-1.patch of Package erlang
From 2d312773d995eb2177037c7530d12fb7c55f8308 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram <shanti@shanti.im> Date: Thu, 20 Aug 2020 22:18:50 -0400 Subject: [PATCH] stdlib: fix lists:sublist/3 if Start>length(L)+1 lists:sublist/3 fails when Start (the 2nd argument) was greater than len(List)+1, due to nthtail/2 throwing an exception. This change replaces the use of nthtail/2 with a recursive function definition. --- lib/stdlib/src/lists.erl | 8 ++++++-- lib/stdlib/test/lists_SUITE.erl | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl index 06c90c0280..f44ed726ca 100644 --- a/lib/stdlib/src/lists.erl +++ b/lib/stdlib/src/lists.erl @@ -341,8 +341,12 @@ max([], Max) -> Max. Len :: non_neg_integer(), T :: term(). -sublist(List, S, L) when is_integer(L), L >= 0 -> - sublist(nthtail(S-1, List), L). +sublist(List, 1, L) when is_list(List), is_integer(L), L >= 0 -> + sublist(List, L); +sublist([], S, _L) when is_integer(S), S >= 2 -> + []; +sublist([_H|T], S, L) when is_integer(S), S >= 2 -> + sublist(T, S-1, L). -spec sublist(List1, Len) -> List2 when List1 :: [T], diff --git a/lib/stdlib/test/lists_SUITE.erl b/lib/stdlib/test/lists_SUITE.erl index 837ab4e97e..88b07df663 100644 --- a/lib/stdlib/test/lists_SUITE.erl +++ b/lib/stdlib/test/lists_SUITE.erl @@ -2215,6 +2215,7 @@ sublist_2_e(Config) when is_list(Config) -> sublist_3(Config) when is_list(Config) -> [] = lists:sublist([], 1, 0), [] = lists:sublist([], 1, 1), + [] = lists:sublist([], 2, 0), [] = lists:sublist([a], 1, 0), [a] = lists:sublist([a], 1, 1), [a] = lists:sublist([a], 1, 2), @@ -2228,6 +2229,7 @@ sublist_3(Config) when is_list(Config) -> [] = lists:sublist([a], 2, 1), [] = lists:sublist([a], 2, 2), [] = lists:sublist([a], 2, 79), + [] = lists:sublist([a], 3, 1), [] = lists:sublist([a,b|c], 1, 0), [] = lists:sublist([a,b|c], 2, 0), [a] = lists:sublist([a,b|c], 1, 1), -- 2.26.2
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor