Skip to content

Default null-terminate ReadOnlySeString - #137

Open
goaaats wants to merge 2 commits into
NotAdam:masterfrom
goaaats:feat/ross_nt
Open

goaaats wants to merge 2 commits into
NotAdam:masterfrom
goaaats:feat/ross_nt

Conversation

@goaaats

@goaaats goaaats commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator
  • Make backing memory of ReadOnlySeString always null-terminated
  • Add helpers that allow code that know that the memory they are passing is null-terminated to still provide ReadOnlySeString members (see FromTerminatedBufferUnchecked())
  • Implement GetPinnableReference() for ReadOnlySeString which returns a single null byte if the string is empty, so that it can be used safely in a fixed statement without any consideration

public const byte Etx = 3;

// Pin target for empty strings
private static readonly byte[] EmptyTerminated = [ 0 ];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if something like MemoryMarshal.GetReference("\0"u8) is actually better here (which I think works?)

var data = Data[ (int) offset.. ];
var stringLength = data.Span.IndexOf( (byte) 0 );
var ret = new ReadOnlySeString( data[ ..stringLength ] );
var ret = ReadOnlySeString.FromTerminatedBufferUnchecked( this.data, (int) offset, stringLength );

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var ret = ReadOnlySeString.FromTerminatedBufferUnchecked( this.data, (int) offset, stringLength );
var stringLength = data.AsSpan( (int) offset ).IndexOf( (byte) 0 );
var ret = new ReadOnlySeString( data, (int) offset, stringLength ); );

doesn't shadow this.data then either

This branch has not been deployed

No deployments
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.

2 participants