Skip to content

Commit 2cf8fc2

Browse files
authored
fix(binary): honour split trim and scope options
Trim removes trailing empty fields without removing leading or internal empty fields. Scope limits matching while preserving the subject’s prefix and suffix. Unknown options, invalid scopes and partial-byte subjects raise badarg.
1 parent 0b17ca8 commit 2cf8fc2

6 files changed

Lines changed: 167 additions & 39 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
All notable milestones for the nera project are documented in this file.
44

5+
## Binary split options match BEAM
6+
7+
- `binary:split/3` now supports `trim` to remove trailing empty parts and
8+
`{scope, {Start, Length}}` to limit matching, including negative lengths.
9+
Bytes outside the scope remain in the returned parts; matches must fit
10+
wholly inside the scope. `trim_all` still removes every empty part.
11+
- Unknown options, malformed or out-of-range scopes, and partial-byte
12+
subjects now raise `badarg` instead of being ignored or rounded to bytes.
13+
`binary:split/2` rejects partial-byte subjects too.
14+
- `binary_split_options` runs the same assertions natively and on BEAM,
15+
including empty inputs, scope boundaries, duplicate options, sub-binaries
16+
and direct, apply and fun-reference calls.
17+
518
## MFA spawn runs erlang BIFs in the child
619

720
- `spawn/3`, `spawn_link/3` and `spawn_monitor/3` now dispatch erlang BIFs

‎FEATURES.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ are not finite real numbers (such as a negative number's square root), raise
481481
| Function | Status | Notes |
482482
|-----------|--------|------------------------------|
483483
| split/2 | ✅ | BIF; binary or nonempty list of nonempty binaries, earliest then longest match |
484-
| split/3 | ✅ | BIF, same patterns; `global` and `trim_all` options (`trim` and `scope` are not supported yet) |
484+
| split/3 | ✅ | BIF, same patterns; `global`, `trim`, `trim_all` and `{scope, {Start, Length}}` (negative lengths included); invalid options/scopes and partial-byte subjects raise badarg |
485485
| part/3 | ✅ | BIF, sub-binary extraction |
486486
| at/2 | ✅ | Byte at position |
487487
| last/1 | ✅ | Final byte; rejects empty binaries and non-byte-aligned bitstrings |

‎crates/nera-runtime/src/ffi/binary_ops.rs‎

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,65 +1620,77 @@ pub unsafe extern "C" fn nera_rt_binary_split_2(
16201620

16211621
/// binary:split/3 — split binary on a pattern or list of patterns with options.
16221622
/// Each split is at the earliest match, the longest pattern at that offset.
1623-
/// Options: `global` splits on all non-overlapping matches; `trim_all` removes
1624-
/// empty parts.
1623+
/// Options: `global` splits on all non-overlapping matches; `trim` removes
1624+
/// trailing empty parts; `trim_all` removes every empty part. A `scope` limits
1625+
/// matching to a byte range without discarding the subject's prefix or suffix.
16251626
#[no_mangle]
16261627
pub unsafe extern "C" fn nera_rt_binary_split_3(
16271628
ctx: *mut ProcessContext,
16281629
subject_term: u64,
16291630
pattern_term: u64,
16301631
opts_term: u64,
16311632
) -> u64 {
1632-
let subject_bytes = match unsafe { binary_data(ErlTerm(subject_term)) } {
1633-
Some((ptr, len)) => unsafe { std::slice::from_raw_parts(ptr, len) },
1634-
None => return raise_error(ctx, ErlTerm::atom(crate::atom_table::ATOM_BADARG).0),
1635-
};
1633+
let subject = ErlTerm(subject_term);
1634+
if !unsafe { subject.is_binary() } { return raise_badarg(ctx); }
1635+
let (ptr, len) = unsafe { binary_data(subject).unwrap() };
1636+
let subject_bytes = unsafe { std::slice::from_raw_parts(ptr, len) };
16361637
let Some(patterns) = (unsafe { search_patterns(ErlTerm(pattern_term)) }) else {
16371638
return raise_badarg(ctx);
16381639
};
16391640

1640-
// Read splitting and empty-field options independently of their order.
1641-
let (global, trim_all) = {
1642-
let mut g = false;
1643-
let mut t = false;
1644-
let mut cursor = ErlTerm(opts_term);
1645-
while cursor.is_cons() {
1646-
if let Some(ptr) = cursor.as_cons_ptr() {
1647-
unsafe {
1648-
let elem = (*ptr).head;
1649-
if let Some(atom_id) = elem.as_atom_id() {
1650-
if let Some(name) = global_atom_table().resolve(atom_id) {
1651-
if name == "global" {
1652-
g = true;
1653-
} else if name == "trim_all" {
1654-
t = true;
1655-
}
1656-
}
1657-
}
1658-
cursor = (*ptr).tail;
1659-
}
1660-
} else {
1661-
break;
1641+
let (mut global, mut trim, mut trim_all) = (false, false, false);
1642+
let mut scope = 0..len;
1643+
let mut cursor = ErlTerm(opts_term);
1644+
if !cursor.is_nil() && !cursor.is_cons() { return raise_badarg(ctx); }
1645+
while let Some(ptr) = cursor.as_cons_ptr() {
1646+
let option = unsafe { (*ptr).head };
1647+
cursor = unsafe { (*ptr).tail };
1648+
let name = option.as_atom_id().and_then(|id| global_atom_table().resolve(id));
1649+
match name.as_deref() {
1650+
Some("global") => global = true,
1651+
Some("trim") => trim = true,
1652+
Some("trim_all") => trim_all = true,
1653+
_ => {
1654+
let Some([key, range]) = (unsafe { pair_elements(option) }) else { return raise_badarg(ctx); };
1655+
let key = key.as_atom_id().and_then(|id| global_atom_table().resolve(id));
1656+
if key.as_deref() != Some("scope") { return raise_badarg(ctx); }
1657+
let Some([start, length]) = (unsafe { pair_elements(range) }) else { return raise_badarg(ctx); };
1658+
let (Some(start), Some(length)) = (start.as_small_int(), length.as_small_int())
1659+
else { return raise_badarg(ctx); };
1660+
let Some(end) = start.checked_add(length) else { return raise_badarg(ctx); };
1661+
let (low, high) = if length < 0 { (end, start) } else { (start, end) };
1662+
let (Ok(low), Ok(high)) = (usize::try_from(low), usize::try_from(high))
1663+
else { return raise_badarg(ctx); };
1664+
if high > len { return raise_badarg(ctx); }
1665+
// Validate every scope, even one a later scope replaces.
1666+
scope = low..high;
16621667
}
16631668
}
1664-
(g, t)
1665-
};
1669+
}
1670+
// Like BEAM, ignore an improper tail after the final option cell.
16661671

1667-
// Split at the first match, or at every match after the previous one with
1668-
// `global`; the rest after the last match, possibly empty, ends the list.
1672+
// Search only within the scope, but keep bytes outside it in the result.
1673+
// Global matches do not overlap; the final part extends to the subject end.
16691674
let mut parts: Vec<&[u8]> = Vec::new();
1670-
let mut start = 0;
1671-
while let Some((offset, length)) = first_match(&subject_bytes[start..], &patterns) {
1672-
parts.push(&subject_bytes[start..start + offset]);
1673-
start += offset + length;
1675+
let mut part_start = 0;
1676+
let mut search_start = scope.start;
1677+
while let Some((offset, length)) = first_match(&subject_bytes[search_start..scope.end], &patterns) {
1678+
let match_start = search_start + offset;
1679+
parts.push(&subject_bytes[part_start..match_start]);
1680+
part_start = match_start + length;
1681+
search_start = part_start;
16741682
if !global {
16751683
break;
16761684
}
16771685
}
1678-
parts.push(&subject_bytes[start..]);
1686+
parts.push(&subject_bytes[part_start..]);
16791687

16801688
if trim_all {
16811689
parts.retain(|part| !part.is_empty());
1690+
} else if trim {
1691+
while parts.last().is_some_and(|part| part.is_empty()) {
1692+
parts.pop();
1693+
}
16821694
}
16831695

16841696
// Build list of binaries

‎docs/KNOWN_LIMITS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ platform and scope.
8484
arrives in the caller's mailbox. OTP's process aliases prevent that, and
8585
Nera does not use them.
8686
- **`binary`:** `compile_pattern/1`, `matches/2,3` and `replace/3,4` are
87-
missing. `binary:split/3` ignores `trim` and `{scope, ...}`.
87+
missing.
8888

8989
## Wrong results and crashes still known
9090

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
220220
| `binary_copy.erl` | binary:copy/2: size overflow, unsigned-word count validation, monitored errors, chunked/sub-binary results and dynamic calls; also run on BEAM | Table-driven |
221221
| `binary_copy_scheduler.erl` | binary:copy/2: large and repeated near-chunk copies share a scheduler; strict ordering through single_scheduler/0 with NERA_SCHEDULERS=1 | 4 workloads |
222222
| `binary_split.erl` | binary:split/2,3 with [global] | ~8 |
223+
| `binary_split_options.erl` | binary:split/3 trim, scoped matching (negative/empty windows, match boundaries, duplicate scopes and sub-binaries), invalid options/scopes and binary:split/2,3 partial-byte subjects; direct, apply and fun calls; same fixture on BEAM | Table-driven |
223224
| `binary_split_patterns.erl` | binary:split/2,3 with a list of patterns: one-element lists, Cowlib's server-sent events line and field splits, longest match at the same offset, non-overlapping global splits, trim_all, a 1,000-line stream and a view into it, apply and fun calls, badarg patterns; also run on BEAM | Table-driven |
224225
| `binary_longest_common_prefix.erl` | Complete proper-list/binary validation, shared/shifted prefixes and dynamic calls | 17 |
225226
| `binary_float.erl` | Float binary segments (<<X:64/float>>) | ~6 |

‎tests/binary_split_options.erl‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
%% EXPECT: binary_split_options_ok
2+
%% Regression: native binary:split ignored trim, scope and unknown options,
3+
%% and rounded a partial-byte subject up to a binary. The CSV and streamed
4+
%% line inputs below exercise the reported defects and BEAM's option rules.
5+
%% Run this same fixture on native Nera and BEAM; reaching the marker is required.
6+
-module(binary_split_options).
7+
-export([test/0]).
8+
9+
test() ->
10+
%% trim removes only trailing empty fields; trim_all removes every empty
11+
%% field, even when trim is also present, independent of option order.
12+
lists:foreach(fun check/1,
13+
[{<<"a,b,,">>, <<",">>, [global, trim], [<<"a">>, <<"b">>]},
14+
{<<",a,,b,,">>, <<",">>, [trim, global], [<<>>, <<"a">>, <<>>, <<"b">>]},
15+
{<<"a,,">>, <<",">>, [trim], [<<"a">>, <<",">>]},
16+
{<<"a,">>, <<",">>, [trim], [<<"a">>]},
17+
{<<",">>, <<",">>, [trim], []},
18+
{<<>>, <<",">>, [], [<<>>]},
19+
{<<>>, <<",">>, [trim], []},
20+
{<<",,">>, <<",">>, [global, trim], []},
21+
{<<"a,b">>, <<";">>, [trim], [<<"a,b">>]},
22+
{<<",a,,b,,">>, <<",">>, [trim_all, trim, global], [<<"a">>, <<"b">>]},
23+
{<<",a,,b,,">>, <<",">>, [trim, global, trim_all], [<<"a">>, <<"b">>]},
24+
{<<"a,b,,">>, <<",">>, [global, global, trim, trim], [<<"a">>, <<"b">>]},
25+
%% BEAM accepts an improper tail after the final option cell.
26+
{<<"a,b,,">>, <<",">>, [global, trim | tail], [<<"a">>, <<"b">>]},
27+
%% A scope restricts matching, not the returned subject's prefix/suffix.
28+
{<<"a,b,c">>, <<",">>, [global, {scope, {2, 3}}], [<<"a,b">>, <<"c">>]},
29+
{<<"a,b,c">>, <<",">>, [{scope, {2, 3}}], [<<"a,b">>, <<"c">>]},
30+
{<<"a,b,c">>, <<",">>, [global, {scope, {5, -3}}], [<<"a,b">>, <<"c">>]},
31+
{<<"a,b,c">>, <<",">>, [global, {scope, {5, -5}}], [<<"a">>, <<"b">>, <<"c">>]},
32+
{<<"a,b,c">>, <<",">>, [{scope, {0, 0}}], [<<"a,b,c">>]},
33+
{<<"a,b,c">>, <<",">>, [{scope, {5, 0}}, trim], [<<"a,b,c">>]},
34+
{<<"a,b,c">>, <<",">>, [{scope, {2, 1}}], [<<"a,b,c">>]},
35+
{<<>>, <<",">>, [{scope, {0, 0}}, trim], []},
36+
{<<"a,b,c">>, <<",">>, [{scope, {0, 2}}, {scope, {2, 3}}, global],
37+
[<<"a,b">>, <<"c">>]},
38+
{<<"a,b,c">>, <<",">>, [{scope, {2, 3}}, {scope, {0, 2}}, global],
39+
[<<"a">>, <<"b,c">>]},
40+
{<<"a,b,,">>, <<",">>, [global, trim, {scope, {0, 4}}], [<<"a">>, <<"b">>, <<",">>]},
41+
%% Only whole matches inside the scope count; the longest eligible
42+
%% pattern wins when a longer pattern crosses the scope's end.
43+
{<<"one\r\ntwo\r\n">>, [<<"\r\n">>], [global, {scope, {0, 4}}],
44+
[<<"one\r\ntwo\r\n">>]},
45+
{<<"one\r\ntwo\r\n">>, [<<"\r\n">>, <<"\r">>], [global, {scope, {0, 4}}],
46+
[<<"one">>, <<"\ntwo\r\n">>]},
47+
{<<"one\r\ntwo\r\n">>, [<<"\r\n">>, <<"\r">>], [global, trim, {scope, {3, 7}}],
48+
[<<"one">>, <<"two">>]}]),
49+
50+
%% A scope is relative to a sub-binary, not to its retained backing bytes.
51+
Line = <<"data: hi\r\n">>,
52+
Width = byte_size(Line),
53+
Stream = iolist_to_binary(lists:duplicate(100, Line)),
54+
Sub = binary:part(Stream, Width, 10 * Width),
55+
Prefix = iolist_to_binary([lists:duplicate(9, Line), <<"data: hi">>]),
56+
check({Sub, [<<"\r\n">>, <<"\n">>], [global, trim, {scope, {9 * Width, Width}}],
57+
[Prefix]}),
58+
<<_:3, Shifted:5/binary, _:5>> = id(<<0:3, "a,b,c", 0:5>>),
59+
check({Shifted, <<",">>, [global, {scope, {2, 3}}], [<<"a,b">>, <<"c">>]}),
60+
61+
%% Unknown cells and malformed or out-of-range scopes raise badarg, even
62+
%% when no match is possible. Every scope is checked before the last wins.
63+
lists:foreach(fun(Options) ->
64+
lists:foreach(fun(S) -> check_badarg(S, <<",">>, Options) end,
65+
[<<"a,b,c">>, <<>>])
66+
end, [[foo], [global, foo], [trim, 42], [{foo, {0, 1}}], [global, {trim}],
67+
foo, 42, {}, <<>>, [{scope}], [{scope, 1}], [{scope, {0}}],
68+
[{scope, {0, 1, 2}}], [{scope, {a, 1}}], [{scope, {0, 1.0}}],
69+
[{scope, {-1, 1}}], [{scope, {6, 0}}], [{scope, {0, 6}}],
70+
[{scope, {2, -3}}], [{scope, {0, 1 bsl 100}}],
71+
[{scope, {1 bsl 100, 0}}], [{scope, {0, -(1 bsl 100)}}],
72+
[{scope, {16#7fffffffffffffff, 1}}],
73+
[{scope, {99, 0}}, {scope, {0, 0}}],
74+
[{scope, {0, 0}}, {scope, {99, 0}}]]),
75+
lists:foreach(fun(S) ->
76+
check_badarg(S, <<"a">>, []),
77+
check_badarg(S, <<"a">>, [global, trim, {scope, {0, 0}}]),
78+
badarg = failure(fun() -> binary:split(id(S), <<"a">>) end),
79+
badarg = failure(fun() -> apply(binary, split, [S, <<"a">>]) end),
80+
badarg = failure(fun() -> (fun binary:split/2)(S, <<"a">>) end)
81+
end, [<<1:3>>, <<"data", 1:1>>, atom, 42, []]),
82+
io:format("binary_split_options_ok~n"),
83+
ok.
84+
85+
check({Subject, Pattern, Options, Expected}) ->
86+
Expected = binary:split(id(Subject), Pattern, Options),
87+
Expected = apply(binary, split, [Subject, Pattern, Options]),
88+
Expected = (fun binary:split/3)(Subject, Pattern, Options).
89+
90+
check_badarg(Subject, Pattern, Options) ->
91+
badarg = failure(fun() -> binary:split(id(Subject), Pattern, Options) end),
92+
badarg = failure(fun() -> apply(binary, split, [Subject, Pattern, Options]) end),
93+
badarg = failure(fun() -> (fun binary:split/3)(Subject, Pattern, Options) end).
94+
95+
failure(F) ->
96+
try F() of
97+
Value -> {unexpected_return, Value}
98+
catch
99+
error:Reason -> Reason
100+
end.
101+
102+
id(X) -> X.

0 commit comments

Comments
 (0)