diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 6b3fd8eb7c..5306555efb 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1287,12 +1287,12 @@ pub fn (s string) index_after(p string, start int) int { return -1 } -// index_byte returns the index of byte `c` if found in the string. -// index_byte returns -1 if the byte can not be found. +// index_u8 returns the index of byte `c` if found in the string. +// index_u8 returns -1 if the byte can not be found. [direct_array_access] pub fn (s string) index_u8(c u8) int { - for i in 0 .. s.len { - if unsafe { s.str[i] } == c { + for i, b in s { + if b == c { return i } } @@ -1348,22 +1348,33 @@ pub fn (s string) count(substr string) int { return 0 // TODO can never get here - v doesn't know that } +// contains_u8 returns `true` if the string contains the byte value `x`. +// See also: [`string.index_u8`](#string.index_u8) , to get the index of the byte as well. +pub fn (s string) contains_u8(x u8) bool { + for c in s { + if x == c { + return true + } + } + return false +} + // contains returns `true` if the string contains `substr`. // See also: [`string.index`](#string.index) pub fn (s string) contains(substr string) bool { if substr.len == 0 { return true } - if s.index_(substr) == -1 { - return false + if substr.len == 1 { + return s.contains_u8(unsafe { substr.str[0] }) } - return true + return s.index_(substr) != -1 } // contains_any returns `true` if the string contains any chars in `chars`. pub fn (s string) contains_any(chars string) bool { for c in chars { - if s.contains(c.ascii_str()) { + if s.contains_u8(c) { return true } } diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 20867c937d..e0d1f3b188 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -1,5 +1,3 @@ -import strings - // Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. @@ -1213,3 +1211,35 @@ fn test_indent_width() { assert '\t\tabc'.indent_width() == 2 assert '\t\t abc'.indent_width() == 3 } + +fn test_index_u8() { + assert 'abcabca'.index_u8(`a`) == 0 + assert 'abcabca'.index_u8(`b`) == 1 + assert 'abcabca'.index_u8(`c`) == 2 + // + assert 'abc'.index_u8(`d`) == -1 + assert 'abc'.index_u8(`A`) == -1 + assert 'abc'.index_u8(`B`) == -1 + assert 'abc'.index_u8(`C`) == -1 + // +} + +fn test_last_index_u8() { + assert 'abcabca'.last_index_u8(`a`) == 6 + assert 'abcabca'.last_index_u8(`c`) == 5 + assert 'abcabca'.last_index_u8(`b`) == 4 + assert 'Zabcabca'.last_index_u8(`Z`) == 0 + // + assert 'abc'.index_u8(`d`) == -1 + assert 'abc'.index_u8(`A`) == -1 + assert 'abc'.index_u8(`B`) == -1 + assert 'abc'.index_u8(`C`) == -1 +} + +fn test_contains_byte() { + assert 'abc abca'.contains_u8(`a`) + assert 'abc abca'.contains_u8(`b`) + assert 'abc abca'.contains_u8(`c`) + assert 'abc abca'.contains_u8(` `) + assert !'abc abca'.contains_u8(`A`) +}