string: bring back old index() without an allocation

pull/2089/head
Alexander Medvednikov 2019-09-23 23:34:42 +03:00
parent d74c91616f
commit f7d8fb0cf4
1 changed files with 4 additions and 6 deletions

View File

@ -355,20 +355,18 @@ pub fn (s string) substr(start, end int) string {
return res
}
pub fn (s string) index_old(p string) int {
pub fn (s string) index(p string) int {
if p.len > s.len {
return -1
}
mut i := 0
for i < s.len {
mut j := 0
mut ii := i
for j < p.len && s[ii] == p[j] {
for j < p.len && s[i + j] == p[j] {
j++
ii++
}
if j == p.len {
return i - p.len + 1
return i
}
i++
}
@ -376,7 +374,7 @@ pub fn (s string) index_old(p string) int {
}
// KMP search
pub fn (s string) index(p string) int {
pub fn (s string) index_kmp(p string) int {
if p.len > s.len {
return -1
}