From 2ef0f15b3405ef859e11340283edd726da62eeb8 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 14 Jun 2020 17:24:15 +0800 Subject: [PATCH] string: fix contains behaviour (closes #5371) --- vlib/builtin/string.v | 3 +++ vlib/builtin/string_test.v | 2 ++ 2 files changed, 5 insertions(+) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 0bc5b8475e..8e8d306867 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -716,6 +716,9 @@ pub fn (s string) count(substr string) int { } pub fn (s string) contains(p string) bool { + if p.len == 0 { + return true + } s.index(p) or { return false } diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index e7a66e5fc2..bb7f0c8e96 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -357,6 +357,8 @@ fn test_contains() { s := 'view.v' assert s.contains('vi') assert !s.contains('random') + assert ''.contains('') + assert 'abc'.contains('') } fn test_arr_contains() {