nikic / iter

Iteration primitives using generators
Other
1.12k stars 76 forks source link

[WIP] Implemented split #66 #80

Closed SamMousa closed 5 years ago

SamMousa commented 5 years ago

The implementation takes care to not use string comparison functions to prevent needless copying of data.. This does make it a little bit harder to read, so might not be worth it.

Todo:

Please check the implementation in #66 as well, it reads a lot easier but comes with the cost of having to do a substr call for every character in the data instead of only when emitting a value.

nikic commented 5 years ago

I'm not sure I understand the motivation for the manually implemented comparison, which is going to be very slow. Can't we use a strpos loop instead? Something along these lines...

$offset = 0;
while (false !== $offset = strpos($str, $deliminter, $offset)) {
    // ...
    $offset += strlen($delimiter);
}
SamMousa commented 5 years ago

I wanted to prevent substring copying; didn't think of using strpos. It is definitely a better solution that will also create more readable code.

Side note: why would the manual comparison be slow? Because it's implemented in PHP?

nikic commented 5 years ago

Side note: why would the manual comparison be slow? Because it's implemented in PHP?

For two reasons:

nikic commented 5 years ago

This one looks good, so I went ahead and merged it. We can handle other variants in a followup...