Add function to get next strpos of any of a specified set of needles

This commit is contained in:
2024-12-25 18:41:28 +01:00
parent fa2985463f
commit be437fcf5f
2 changed files with 76 additions and 0 deletions

View File

@ -706,4 +706,41 @@ final class MD_STD_Test extends TestCase {
self::assertEquals("en", MD_STD::get_user_lang_no_cookie(["de", "en"], "en"));
}
/**
* Data provider for returning a tmp file.
*
* @return array<string, array{0: non-empty-string, 1: non-empty-array<non-empty-string>, 2: int}>
*/
public static function strpos_multi_provider():array {
return [
"Search quotation markes on when='" => ["when='", ['"', '\''], 5],
"Search quotation markes on when=\"" => ["when=\"", ['"', '\''], 5],
"Search quotation markes on when=\"'" => ["when=\"'", ['"', '\''], 5],
"Search quotation markes on when= (non-existent)" => ["when=", ['"', '\''], -1],
];
}
/**
* Checks unlink_if_exists works.
*
* @param non-empty-string $haystack Haystack.
* @param non-empty-array<non-empty-string> $needles Needles.
* @param integer $expected Expected position.
*
* @return void
*/
#[DataProvider('strpos_multi_provider')]
public function test_strpos_multi(string $haystack, array $needles, int $expected):void {
if ($expected === -1) {
self::assertEmpty(MD_STD::strpos_multi($haystack, $needles));
}
else {
self::assertEquals($expected, MD_STD::strpos_multi($haystack, $needles)['position']);
}
}
}