1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

string: bring back old index() without an allocation

This commit is contained in:
Alexander Medvednikov 2019-09-23 23:34:42 +03:00
parent d74c91616f
commit f7d8fb0cf4

View File

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