Skip to content

system: add toOwned for char/byte views#25769

Open
ZoomRmc wants to merge 1 commit intonim-lang:develfrom
ZoomRmc:tostring
Open

system: add toOwned for char/byte views#25769
ZoomRmc wants to merge 1 commit intonim-lang:develfrom
ZoomRmc:tostring

Conversation

@ZoomRmc
Copy link
Copy Markdown
Contributor

@ZoomRmc ZoomRmc commented Apr 25, 2026

Implements RFC #558 by separating two currently conflated operations on openArray[char]:

  • substr(openArray[char]) now behaves like the other substr overloads and performs substringing with clamping.
  • the old "copy this whole view into a new string" behaviour is now provided explicitly as toOwned.

This also adds toOwned(openArray[byte]), covering the common byte-view to string-copy case.

Properly closes #14810

Rationale

Before this change, substr(openArray[char]) did not actually perform substringing. It was effectively a full-copy helper for an already-formed view, which made it inconsistent with the rest of the substr family, As noted in RFC #558:

  • substr(string; first, last) selects a range and clamps indices
  • substr(string; first = 0) selects a suffix
  • substr(openArray[char]) simply copied the entire input view unchanged

This PR eliminates this semantic mismatch and makes substr(openArray[char]) consistent with the existing string overloads, giving the copy operation its own name.

Why toOwned

I chose toOwned over toString or just copy because this operation is not generic stringification or formatting. The name emphasizes the ownership transfer and data copying performed. toString would be less clear and would raise questions "why not $?".

toOwned makes the important properties explicit:

  • the result is a fresh owned string
  • the source data is copied
  • this is first of all a data-ownership/materialization operation, not a presentation operation

Behaviour and backward compatibility

  • substr(openArray[char]; first = 0; last = int.high) selects a range within a view and clamps first/last like the string overloads do.
  • substr() with default arguments still produces the same result as toOwned(), but the APIs now communicate different intent:
    • toOwned: copy this exact view, in full
    • substr: select a range from this view, with clamping

Due to added default argument values substr[openArray[char]] remains fully backward-compatible.

Tests

Added dedicated test coverage for string-view behaviour in:

  • tests/stdlib/tstringview_owned_substr.nim

New file preferred over tsystem_misc as the whole matrix check is required.

Closes RFC nim-lang#558

Makes `substr(openArray[char])` consistent with the string overloads by
giving it real substring semantics with clamping.

Moves the old whole-view copy behavior to a new `toOwned` API and extend it
to `openArray[byte]`. The new name emphasizes materializing an owned string
copy rather than generic stringification, which is the role of `$`.

Adds dedicated tests for `toOwned` and `substr` on string
views, including `static`, all backends, and the `refc/orc` matrix.
@ZoomRmc
Copy link
Copy Markdown
Contributor Author

ZoomRmc commented Apr 25, 2026

Errors all look IC/NIF-caused, don't think they're caused by this patch.

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.

Add byte-array-to-string conversion to system module

1 participant