Re: [PHP-DEV][RFC] grapheme cluster for str_split, grapheme_str_split function

2024-03-24 Thread youkidearitai
2024年3月9日(土) 15:26 youkidearitai :
>
> Hello, Internals
>
> I created an wiki for `grapheme_str_split` function.
> Please see:
> https://wiki.php.net/rfc/grapheme_str_split
>
> I would like to "Under Discussion" section.
>
> Best Regards
> Yuya
>
> --
> ---
> Yuya Hamada (tekimen)
> - https://tekitoh-memdhoi.info
> - https://github.com/youkidearitai
> -

Hello, Internals

I want to go to "Voting" phase if nothing any comment.
I will start at tomorrow(26th) to "Voting" phase.

Thank you
Yuya

-- 
---
Yuya Hamada (tekimen)
- https://tekitoh-memdhoi.info
- https://github.com/youkidearitai
-


Re: [PHP-DEV] Proposal: Make $offset of substr_replace null by default

2024-03-24 Thread Rowan Tommins [IMSoP]
[Aside: please don't use "reply" when starting a new thread. Although 
GMail and its imitators frequently ignore it, a reply contains a header 
telling clients where to add it to an existing thread. I've pasted your 
full text into a new e-mail rather than replying, so it reliably shows 
as its own thread.]



On 23/03/2024 22:58, mickmackusa wrote:

> substr_replace() has the following signature:
>
> substr_replace(
> array|string $string,
> array|string $replace,
> array|int $offset,
> array|int|null $length = null
> ): string|array
>
> Was it deliberate to not allow a null value as the third parameter?  
If permitted to amend this signature, I think it would be sensible to 
set null as the default value for $offset and adopt the same logic as 
the $length parameter.

>
> I have recently stumbled upon what I assume is a code smell in 
multiple SO scripts that use:

>
> $prefixed = preg_filter('/^/', 'prefix_', $array);
>
> It smells because regardless of the passed in array values' types, 
there will always be a starting position of each values which are 
coerced to strings. In other words, the destructive feature of 
preg_filter() is never utilized.

>
> This means that for this task, preg_filter() can be unconditionally 
replaced with preg_replace().

>
> $prefixed = preg_replace('/^/', 'prefix_', $array);
>
> But wait, regex isn't even needed for this task.  It can be coded 
more efficiently as:

>
> $prefixed = substr_replace($array, 'prefix_', 0, 0)
>
> Next, my mind shifted to suffixing/postfixing. By using $ in the pattern.
>
> $prefixed = preg_replace('/$/', 'prefix_', $array);
>
> However, there isn't a convenient way to append a string to each 
value using substr_replace() with the current signature.

>
> If the $offset parameter worked like the $length parameter, then the 
language would provide a native, non-regex tool for appending a static 
string to all array elements.

>
> $suffixed = substr_replace($array, '_suffix');
>
> Finally, I wish to flag the observation that null values inside of an 
array are happily coerced to strings inside of the aforementioned 
functions, but null is not consumable if singularly passed in.

>
> Some examples for context: https://3v4l.org/ENVip
>
> I look forward to hearing feedback/concerns.


Not being familiar with the variations supported by substr_replace, it 
took me a while to understand what was being proposed here.


In case anyone else is similarly lost, a null $length is equivalent to 
strlen($string), meaning "replace to the end"; so a null $offset having 
the same meaning would give "append to the end". On its own, this would 
be pretty pointless:


$foo = substr_replace('abc', 'xyz', null);
// a long-winded way of writing
$foo = 'abc' . 'xyz';

But the function also has built-in mapping over arrays, so it could be 
used to append the same string to multiple inputs:


$foo = substr_replace(['hello', 'goodbye'], '!', null);

Or append each entry from one list onto each entry in the other:

$foo = substr_replace(['one', 'two'], [' - uno', ' - dos'], null);

Demo: https://3v4l.org/6eEIG


While I can see the logic, it would never occur to me to use any of the 
functions mentioned for this task, rather than using array_map and a 
regular concatenation:


$foo = array_map(fn($string) => $string . '!', ['hello', 'goodbye']);

$foo = array_map(fn($string, $suffix) => $string . $suffix, ['one', 
'two'], [' - uno', ' - dos']);


Which of course extends to more complex cases:

$foo = array_map(fn($english, $spanish) => "'$english' en Español es 
'$spanish'", ['one', 'two'], ['uno', 'dos']);


$foo = array_map(fn($english, $spanish, $german) => "$english - $spanish 
- $german", ['one', 'two'], ['uno', 'dos'], ['ein', 'zwei']);


https://3v4l.org/d55kT


So, I'm not opposed to the change, but its value seems marginal.


Regards,

--
Rowan Tommins
[IMSoP]


[PHP-DEV] [RFC] [Discussion] Support object type in BCMath

2024-03-24 Thread Saki Takamachi
Hi internals,

I want to start the discussion on the PHP RFC: Support object type in BCMath.

https://wiki.php.net/rfc/support_object_type_in_bcmath

Regards.

Saki