builtin: optimise the common case of s.contains("x") add s.contains_byte(x) (#17702)

This commit is contained in:
Delyan Angelov 2023-03-19 00:10:13 +02:00 committed by GitHub
parent 14148f3e52
commit 3793bf1c99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 10 deletions

View file

@ -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`)
}