feat: add StringReference.GetValue/GetRaw (Python parity)#34
Merged
Conversation
Closes HIGH convenience gap #4. Python's StringReference exposes .value (decoded text) and .raw (bytes) (binaryview.py:510/:514); the C# StringReference is a handle-less Type/Start/Length carrier, so callers had to separately ReadData(Start, Length) and hand-decode per StringType. Add durable GetValue(this StringReference, BinaryView) and GetRaw(this StringReference, BinaryView) extension methods with a StringType -> Encoding table mirroring Python's _decodings (Ascii->ASCII, Utf8->UTF8, Utf16->Unicode/UTF-16LE, Utf32->UTF32LE). GetValue decodes with .NET's default replacement fallback, the equivalent of Python errors='replace'. The view is a parameter because StringReference does not own one (unlike Python); extending its shape would force a FromNative signature change threaded through the TakeStructArray pipelines, so composition-only extensions are cleaner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Closes HIGH-severity convenience gap #4. Python's
StringReferenceexposes.value(decoded text) and.raw(raw bytes) (binaryview.py:510/:514). The C#StringReferenceis a handle-lessType/Start/Lengthcarrier, so callers had to separatelyReadData(Start, Length)and hand-decode perStringType.Changes
Two durable extension methods in
NavigationExtensions.cs(composition-only, no P/Invoke, no generated-file edits), plus aStringType → Encodingtable mirroring Python's_decodings:AsciiStringEncoding.ASCIIUtf8StringEncoding.UTF8Utf16StringEncoding.Unicode(UTF-16 LE)Utf32StringEncoding.UTF32(UTF-32 LE)GetRaw(sref, view)→view.ReadData(Start, Length).GetValue(sref, view)→ decodesGetRawwith the type's encoding using .NET's default replacement fallback (the equivalent of Pythonerrors='replace').The view is a parameter (not stored on
StringReference) because the C# type is a value carrier; storing a view would force aFromNativesignature change threaded through theTakeStructArraypipelines, so composition-only extensions are cleaner. Python's.viewproperty is therefore N/A here — the caller already holds the view.E2E test (harness repo)
GetValueAndRawAreConsistentForEveryStringTypePresent— runs analysis (the string list is materialized lazily), then for a sample of the 177 strings assertsGetRawreads exactly[Start, Start+Length)andGetValuedecodes with the independently-computed expected encoding. cat.bndb yields 176 ASCII + 1 UTF-16 strings, so the test exercises bothEncoding.ASCIIandEncoding.Unicodedispatch — a wrong table entry would diverge for the UTF-16 string. Full harness regression 52/52.