summaryrefslogtreecommitdiff
path: root/libstd/strfind.myr
blob: 2ad25f71d52aeee1788e541abad148bce818d5d2 (plain)
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
use "types.use"
use "option.use"

pkg std =
	const strfind	: (haystack : byte[:], needle : byte[:] -> option(size))
;;

const strfind = {haystack, needle
	var i, j

	for i = 0; i < haystack.len; i++
		if i + needle.len > haystack.len
			-> `None
		;;
		if haystack[i] == needle[0]
			for j = 0; j < needle.len; j++
				if haystack[i + j] != needle[j]
					goto nextiter
				;;
			;;
			-> `Some i
		;;
:nextiter
	;;
	-> `None
}