On 14/02/2023 15:32, Thomas Hruska wrote:
Hello Internals,
I would like to start the discussion on adding several functions and
parameters to existing functions for improved substring handling in PHP:
https://wiki.php.net/rfc/working_with_substrings
Hi Thomas,
Thanks for your effort on this, I think efficient string handling
functions would be a major help for the ecosystem, allowing library
authors to do things in plain PHP code which currently defer to C
extensions just for performance.
My first thought opening the RFC was to see a function signature with 9
arguments and immediately wonder how to refactor it into something more
manageable. Just writing *tests* for all the combinations sounds like a
nightmare, let alone understanding code that uses them all.
As I read through, I had a similar feeling about the need to
copy-and-paste the same two parameters onto so many functions.
Luckily, I think the RFC contains the seed of the solution to both
problems: what you refer to as "virtual buffers". These seem to be
crying out to be a new data type, with their own API - probably using OO
style, given general fashions.
Framed around that, I think we can split out a few different concerns:
* Methods to take a string, and make a new, writeable buffer pointing at
all or part of it
* Methods to access parts of a buffer, as a string or another buffer
* Methods to efficiently write to, delete from, or overwrite, parts of a
buffer
* Methods to explicitly manage the memory used by the buffer
* Finally, support for writing to, or reading from, a buffer instead of
a string in a number of existing functions
Thinking about exactly what those methods should look like leads me to
my next thought: we should be learning from prior art here. Are there
other languages which already do this well, which PHP could emulate? Are
there other languages which already do this *badly*, whose mistakes PHP
could explicitly learn from?
What comes to my mind immediately is that both Java and C# have
"StringBuilder" classes, which cover at least some of these use cases.
C#, in particular, had a lot of very smart people paid to design it,
able to learn from mistakes Java had already made.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php