Make Segment<T> semantics compatible with ArraySegment<T> #173
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses issue #55 by making the
Segment<T>class semantically compatible with .NET'sArraySegment<T>while maintaining full backward compatibility.Key Changes Made:
Emptyproperty: Provides an empty segment instance similar toArraySegment<T>.EmptyArrayproperty: Returns the underlying array whenBaseis of typeT[], compatible withArraySegment<T>.ArraySegment(T[] array)- creates a segment covering the entire arraySegment(T[] array, int offset, int count)- creates a segment with bounds checkingSlice()methods: Create sub-segments from existing segments (both single-parameter and two-parameter overloads)ToArray()method: Copies segment contents to a new arrayBackward Compatibility
Countproperty continues to work as an alias forLength(IList requirement)Testing
Added comprehensive test suite with 13 test methods covering:
All existing tests continue to pass (31 total tests)
New implementation handles edge cases and provides appropriate error messages
Implementation Details
The implementation maintains the original design of working with any
IList<T>while adding specific optimizations and compatibility features for arrays. The newArrayproperty returnsnullwhen the underlying collection is not an array, which matches the behavior users would expect from ArraySegment-style APIs.🤖 Generated with Claude Code
Resolves #55