system: add toOwned for char/byte views#25769
Open
ZoomRmc wants to merge 1 commit intonim-lang:develfrom
Open
Conversation
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.
Contributor
Author
|
Errors all look IC/NIF-caused, don't think they're caused by this patch. |
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.
Implements RFC #558 by separating two currently conflated operations on
openArray[char]:substr(openArray[char])now behaves like the othersubstroverloads and performs substringing with clamping.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 thesubstrfamily, As noted in RFC #558:substr(string; first, last)selects a range and clamps indicessubstr(string; first = 0)selects a suffixsubstr(openArray[char])simply copied the entire input view unchangedThis PR eliminates this semantic mismatch and makes
substr(openArray[char])consistent with the existing string overloads, giving the copy operation its own name.Why
toOwnedI chose
toOwnedovertoStringor justcopybecause this operation is not generic stringification or formatting. The name emphasizes the ownership transfer and data copying performed.toStringwould be less clear and would raise questions "why not$?".toOwnedmakes the important properties explicit:stringBehaviour and backward compatibility
substr(openArray[char]; first = 0; last = int.high)selects a range within a view and clampsfirst/lastlike the string overloads do.substr()with default arguments still produces the same result astoOwned(), but the APIs now communicate different intent:toOwned: copy this exact view, in fullsubstr: select a range from this view, with clampingDue 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.nimNew file preferred over
tsystem_miscas the whole matrix check is required.