summary refs log blame commit diff stats
path: root/tests/misc/tpos.nim
blob: bedb62e624d7c310ff76e8aabcdcb59409ca576b (plain) (tree)
} /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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