summary refs log tree commit diff stats
path: root/tests/misc/tpos.nim
blob: bedb62e624d7c310ff76e8aabcdcb59409ca576b (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
discard """
  file: "tpos.nim"
  output: "6"
"""
# test this particular function

proc mypos(sub, s: string, start: int = 0): int =
  var
    i, j, M, N: int
  M = sub.len
  N = s.len
  i = start
  j = 0
  if i >= N:
    result = -1
  else:
    while true:
      if s[i] == sub[j]:
        inc(i)
        inc(j)
      else:
        i = i - j + 1
        j = 0
      if (j >= M) or (i >= N): break
    if j >= M:
      result = i - M
    else:
      result = -1

var sub = "hello"
var s = "world hello"
write(stdout, mypos(sub, s))
#OUT 6