summary refs log tree commit diff stats
path: root/tests/misc/tpos.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/misc/tpos.nim')
-rw-r--r--tests/misc/tpos.nim33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/misc/tpos.nim b/tests/misc/tpos.nim
new file mode 100644
index 000000000..f7607d643
--- /dev/null
+++ b/tests/misc/tpos.nim
@@ -0,0 +1,33 @@
+discard """
+  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))
+write stdout, "\n"
+#OUT 6