Add a document to toOpenArray proc (#23905)

This commit is contained in:
Tomohiro
2024-08-01 13:27:10 +09:00
committed by GitHub
parent cb156648d6
commit 12b9680291
2 changed files with 18 additions and 0 deletions

View File

@@ -2790,6 +2790,18 @@ when not defined(js):
proc toOpenArray*[T](x: seq[T]; first, last: int): openArray[T] {.
magic: "Slice".}
## Allows passing the slice of `x` from the element at `first` to the element
## at `last` to `openArray[T]` parameters without copying it.
##
## Example:
## ```nim
## proc test(x: openArray[int]) =
## doAssert x == [1, 2, 3]
##
## let s = @[0, 1, 2, 3, 4]
## s.toOpenArray(1, 3).test
## ```
proc toOpenArray*[T](x: openArray[T]; first, last: int): openArray[T] {.
magic: "Slice".}
proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openArray[T] {.

View File

@@ -110,6 +110,9 @@ proc `[]`*[Idx, T; U, V: Ordinal](a: array[Idx, T], x: HSlice[U, V]): seq[T] {.s
## var a = [1, 2, 3, 4]
## assert a[0..2] == @[1, 2, 3]
## ```
##
## See also:
## * `toOpenArray(array[I, T];I,I) <#toOpenArray,array[I,T],I,I>`_
let xa = a ^^ x.a
let L = (a ^^ x.b) - xa + 1
result = newSeq[T](L)
@@ -136,6 +139,9 @@ proc `[]`*[T; U, V: Ordinal](s: openArray[T], x: HSlice[U, V]): seq[T] {.systemR
## var s = @[1, 2, 3, 4]
## assert s[0..2] == @[1, 2, 3]
## ```
##
## See also:
## * `toOpenArray(openArray[T];int,int) <#toOpenArray,openArray[T],int,int>`_
let a = s ^^ x.a
let L = (s ^^ x.b) - a + 1
newSeq(result, L)