Skip to content

Add str_contains() function#433

Open
Danil42Russia wants to merge 5 commits intoVKCOM:masterfrom
Danil42Russia:str_contains
Open

Add str_contains() function#433
Danil42Russia wants to merge 5 commits intoVKCOM:masterfrom
Danil42Russia:str_contains

Conversation

@Danil42Russia
Copy link
Contributor

Information

Example

str_contains("test string", "test"); // true
str_contains("test string", "hello"); // false

@Danil42Russia
Copy link
Contributor Author

Danil42Russia commented Feb 13, 2022

Error text:

Starting php to cpp transpiling...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compilation error at stage: Parse file, gen by compiler-core.cpp:277
  lib/polyfills/file2.php:10
    }

Redeclaration of function str_contains(), the previous declaration was in .../builtin-functions/_functions.txt

Compilation terminated due to errors

The error occurs due to the declaration of the str_contains function in file2.php

@tolk-vm tolk-vm added the PHP8 PHP8 feature label Feb 14, 2022
@Danil42Russia
Copy link
Contributor Author

Tests don't break anymore!

}

bool f$str_contains(const string &haystack, const string &needle) {
return haystack.contains(needle);
Copy link
Contributor

@quasilyte quasilyte Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the advantages of implementing it as a new functions instead of using already existing strpos?

PHP uses similar approach:
https://github.com/php/php-src/blob/0b6f58d907e7169f7876bbea199967262197fbaa/ext/standard/string.c#L1794

As seen in the code below, str_contains could be implemented in terms of strpos like so:

return '' === $needle || false !== strpos($haystack, $needle);

I would check what php_memnstr from PHP is. Maybe we have compatible function. If not, we could add one and use it in places where PHP uses it.

Maybe php_memnstr is identical to the newly introduced contains method, but I suggest re-checking that.

Also: you can run benchmarks using ktest utility.
https://habr.com/ru/company/vk/blog/572424/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PHP8 PHP8 feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants