Skip to content

Add trim, trim_all and list patterns to binary:split/2,3#2253

Merged
bettio merged 1 commit intoatomvm:release-0.7from
petermm:binary-split/1,2
Apr 3, 2026
Merged

Add trim, trim_all and list patterns to binary:split/2,3#2253
bettio merged 1 commit intoatomvm:release-0.7from
petermm:binary-split/1,2

Conversation

@petermm
Copy link
Copy Markdown
Contributor

@petermm petermm commented Apr 2, 2026

Fixes #2187 #2186

Add trim, trim_all, and list patterns to binary:split/2,3

This PR extends binary:split/2,3 with more OTP-compatible behavior.

  • Pattern can now be a non-empty list of non-empty binaries, in addition to a single binary.
  • When multiple patterns match, the earliest match wins; ties prefer the longer pattern.
  • trim removes trailing empty binaries from the result.
  • trim_all removes all empty binaries from the result.
  • Option parsing now matches BEAM behavior for improper option lists with a valid prefix, such as [global | foo].

Examples:

binary:split(<<"foobar">>, [<<"oo">>, <<"o">>]).
%% => [<<"f">>, <<"bar">>]

binary:split(<<":a::">>, <<":">>, [global, trim]).
%% => [<<>>, <<"a">>]

binary:split(<<":a::">>, <<":">>, [global, trim_all]).
%% => [<<"a">>]

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@petermm petermm changed the title Binary split/1,2 Add trim, trim_all and list patterns to binary:split/2,3 Apr 2, 2026
@petermm petermm changed the base branch from main to release-0.7 April 2, 2026 15:28
@petermm petermm force-pushed the binary-split/1,2 branch from 0c7ef09 to da36d84 Compare April 2, 2026 18:24
@bettio
Copy link
Copy Markdown
Collaborator

bettio commented Apr 3, 2026

Also, let's not forget updating CHANGELOG.md

@petermm
Copy link
Copy Markdown
Contributor Author

petermm commented Apr 3, 2026

Fixed, will squash obviously.

@petermm petermm force-pushed the binary-split/1,2 branch from 20368be to 0c1671c Compare April 3, 2026 11:37
bettio

This comment was marked as outdated.

Copy link
Copy Markdown
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's squash it.

Extend binary:split/2,3 to accept list patterns and support the trim and trim_all options.

Match OTP semantics for earliest matches, longest-pattern tie breaks, and trimming behavior, and add regression coverage in the Erlang and estdlib tests.

Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the binary-split/1,2 branch from 0c1671c to cdd11ea Compare April 3, 2026 15:21
@bettio bettio merged commit 378eb53 into atomvm:release-0.7 Apr 3, 2026
117 of 146 checks passed
@petermm petermm deleted the binary-split/1,2 branch April 3, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

binary:split/3 trim and trim_all options

2 participants