better support for slices as views (#15414)
* moved view tests to tests/views * refactoring * more refactorings * better support for system.toOpenArray for first class view types
This commit is contained in:
parent
e6616115e6
commit
57b7841c18
6 changed files with 100 additions and 44 deletions
37
tests/views/tsplit_into_openarray.nim
Normal file
37
tests/views/tsplit_into_openarray.nim
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
discard """
|
||||
output: '''asdf
|
||||
231
|
||||
'''
|
||||
cmd: "nim c --gc:arc -d:useMalloc -g $file"
|
||||
valgrind: true
|
||||
"""
|
||||
|
||||
{.experimental: "views".}
|
||||
|
||||
const
|
||||
Whitespace = {' ', '\t', '\n', '\r'}
|
||||
|
||||
iterator split*(s: string, seps: set[char] = Whitespace,
|
||||
maxsplit: int = -1): openArray[char] =
|
||||
var last = 0
|
||||
var splits = maxsplit
|
||||
|
||||
while last <= len(s):
|
||||
var first = last
|
||||
while last < len(s) and s[last] notin seps:
|
||||
inc(last)
|
||||
if splits == 0: last = len(s)
|
||||
yield toOpenArray(s, first, last-1)
|
||||
if splits == 0: break
|
||||
dec(splits)
|
||||
inc(last)
|
||||
|
||||
proc `$`(x: openArray[char]): string =
|
||||
result = newString(x.len)
|
||||
for i in 0..<x.len: result[i] = x[i]
|
||||
|
||||
proc main() =
|
||||
for x in split("asdf 231"):
|
||||
echo x
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue