summary refs log tree commit diff stats
path: root/tests/views/tsplit_into_openarray.nim
blob: 3ea290d89cd0703e2520474b2d4b2a8d14e3b901 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()