diff options
| author | FourteenBrush <74827262+FourteenBrush@users.noreply.github.com> | 2025-07-03 16:51:24 +0200 |
|---|---|---|
| committer | FourteenBrush <74827262+FourteenBrush@users.noreply.github.com> | 2025-07-03 16:51:38 +0200 |
| commit | 5811525592e072aa07eb4b262874da70f2404098 (patch) | |
| tree | a36b99c011984e68872f9f5715d9441ccd13f0d9 /core/slice | |
| parent | 1a4139b25c8d7ed780641a94ef628e8867a5e332 (diff) | |
Add `slice.suffix_length`
Diffstat (limited to 'core/slice')
| -rw-r--r-- | core/slice/slice.odin | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/core/slice/slice.odin b/core/slice/slice.odin index c328fd267..8337a9728 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -387,6 +387,25 @@ has_prefix :: proc(array: $T/[]$E, needle: T) -> bool where intrinsics.type_is_c return false } +/* + return the suffix length common between slices `a` and `b`. + + slice.suffix_length([]u8{1, 2, 3, 4}, []u8{1, 2, 3, 4}) -> 4 + slice.suffix_length([]u8{1, 2, 3, 4}, []u8{3, 4}) -> 2 + slice.suffix_length([]u8{1, 2, 3, 4}, []u8{1}) -> 0 + slice.suffix_length([]u8{1, 2, 3, 4}, []u8{1, 3, 5}) -> 0 + slice.suffix_length([]u8{3, 4, 5}, []u8{3, 5}) -> 1 +*/ +@(require_results) +suffix_length :: proc(a, b: $T/[]$E) -> (n: int) where intrinsics.type_is_comparable(E) { + len_a, len_b := len(a), len(b) + _len := builtin.min(len_a, len_b) + + #no_bounds_check for i := 1; i <= _len && a[len_a - i] == b[len_b - i]; i += 1 { + n += 1 + } + return +} @(require_results) has_suffix :: proc(array: $T/[]$E, needle: T) -> bool where intrinsics.type_is_comparable(E) { |