New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
a built-in startswith($string, $prefix) function #16200
Comments
From @maukeCreated by @maukeCurrently Perl has no good way to check whether a string starts with another - index($string, $prefix) == 0 Inefficient because it has to search the whole (potentially large) $string, - $string =~ /^\Q$prefix/ Inefficient because it has to pass the whole $prefix through quotemeta, then - substr($string, 0, length $prefix) eq $prefix I don't know if this actually makes a copy of the beginning of $string, but I want to be able to express my intent (check prefixes) directly. As a bonus, That's why I think it would be nice to have startswith($string, $prefix) in core (and maybe endswith($x, $y)). Perl Info
|
From @demerphqOn 14 October 2017 at 16:18, l.mai@web.de <perlbug-followup@perl.org> wrote:
Interesting observation. With Dave M's patches to how we construct a In theory there is no reason that we could not optimize this kind of
I don't think it does in the simple case.
At $work this is something we would put in an XS module, alongside On the other hand I think you have a point about / ... \Q$str\E ... / It would be nice if we did not have to either actually escape the Yves -- |
The RT System itself - Status changed from 'new' to 'open' |
From @cpansproutOn Sat, 14 Oct 2017 11:11:57 -0700, demerphq wrote:
Similarly, we could optimise the index()== comparison. (Didn’t Dave Mitchell do something similar to that? I don’t remember what it was.)
It does, unless the code changed when I wasn’t looking.
++ -- Father Chrysostomos |
From @cpansproutOn Sat, 14 Oct 2017 12:13:32 -0700, sprout wrote:
See commit 400ffcf. It was: index(...) == -1 that got optimised. How hard would it be to extend this to ==0 as well? -- Father Chrysostomos |
From blgl@stacken.kth.seQuoth l . mai @ web . de:
+ rindex($string, $prefix, 0) == 0 Efficient because the range of potential start positions is 0..0 /Bo Lindbergh |
Migrated from rt.perl.org#132301 (status was 'open')
Searchable as RT132301$
The text was updated successfully, but these errors were encountered: