Skip to content

Add slice and unsafeSlice for ByteArray#8

Merged
jappeace merged 3 commits intojappeace:masterfrom
jappeace-sloth:add-slice-functions
Mar 28, 2026
Merged

Add slice and unsafeSlice for ByteArray#8
jappeace merged 3 commits intojappeace:masterfrom
jappeace-sloth:add-slice-functions

Conversation

@jappeace-sloth
Copy link
Copy Markdown

Summary

  • Adds slice :: ByteArray bs => bs -> Word -> Word -> Maybe bs — extracts sub-range [start, end), returns Nothing when indices exceed length
  • Adds unsafeSlice :: ByteArray bs => bs -> Word -> Word -> bs — same but clamps out-of-bounds indices
  • Both swap indices automatically if end < start
  • Closes Extracting of a part #7

Test plan

  • Property test: slice matches take (end-start) . drop start
  • Property test: out-of-bounds indices return Nothing
  • Property test: unsafeSlice clamps to valid range
  • Property test: slice a b == slice b a (swap symmetry)
  • All tests pass on both Bytes and ScrubbedBytes backends
  • nix-build nix/ci.nix passes

🤖 Generated with Claude Code

jappeace-sloth and others added 2 commits March 27, 2026 18:07
- slice: extracts sub-range [start, end) with Word indices, returns Maybe
  (Nothing when indices exceed length)
- unsafeSlice: same but clamps out-of-bounds indices to valid range
- Both swap indices if end < start
- Added property tests: equivalence with take/drop, out-of-bounds returns
  Nothing, clamping behaviour, and index swap symmetry

Prompt: Implement the plan to add slice/unsafeSlice to ram (issue jappeace#7)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jappeace
Copy link
Copy Markdown
Owner

@kazu-yamamoto please review this as well, see if it's like how you expect

@kazu-yamamoto
Copy link
Copy Markdown

Just a moment.
I will try to use this function in tls and feedback my experience.

@kazu-yamamoto
Copy link
Copy Markdown

Two minor observations:

Word is difficult to use. The calling function needs to use fromIntegral extensively. Using Int and adding more guards would be more user-friendly.

The end index is not intuitive because the element indicated by start index is included but the element indicated by the end index is not.

For example, slice ba 1 1 doesn't intuitively mean 0 bytes. Also, when copying the entire array, it becomes slice ba 0 (length ba), where the end value overflows, which feels awkward.

I found the length to be more clear in meaning.

@kazu-yamamoto
Copy link
Copy Markdown

I guess that unsafeSlice should call error on error conditions so that users can notify the error quickly.

…or on invalid

- Change slice/unsafeSlice signatures from Word to Int
- Change from (start, end) to (offset, length) semantics
- unsafeSlice now calls error instead of clamping on invalid args
- slice returns Nothing for negative offset/length or out-of-bounds
- Update tests: new semantics, negative input tests, error assertion
- Update CHANGELOG entry

Addresses review comments from kazu-yamamoto on PR jappeace#8.

Prompt: Implement the following plan: Address PR jappeace#8 review comments
Tokens: ~40k input, ~8k output

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jappeace
Copy link
Copy Markdown
Owner

@kazu-yamamoto I changed it according to your suggestions, please review if this is better for you?

@kazu-yamamoto
Copy link
Copy Markdown

Very good.
Thanks!

@jappeace jappeace merged commit fb16285 into jappeace:master Mar 28, 2026
16 checks passed
@kazu-yamamoto
Copy link
Copy Markdown

If you release a new version, I will also release tls.

@jappeace
Copy link
Copy Markdown
Owner

yeah it's released: https://hackage.haskell.org/package/ram
🚢

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.

Extracting of a part

3 participants