ext/intl: IntlDateFormatter::parse adds new optional argument.#13779
ext/intl: IntlDateFormatter::parse adds new optional argument.#13779devnexen wants to merge 3 commits intophp:masterfrom
Conversation
bacd94e to
fbd4334
Compare
|
|
||
| /** | ||
| * @param int $offset | ||
| * @param bool $updateCalendar |
There was a problem hiding this comment.
this shouldn't be added since it doesn't have more information than the param type declaration
ext/intl/php_intl.stub.php
Outdated
| function datefmt_parse(IntlDateFormatter $formatter, string $string, &$offset = null): int|float|false {} | ||
| /** | ||
| * @param int $offset | ||
| * @param bool $updateCalendar |
| * @alias datefmt_parse | ||
| */ | ||
| public function parse(string $string, &$offset = null): int|float|false {} | ||
| public function parse(string $string, &$offset = null, bool $updateCalendar = false): int|float|false {} |
There was a problem hiding this comment.
I don't want to bikeshed this much, but I'd still want to throw one more idea to think about:
public function parseToCalendar(string $string, &$offset = null): void {}
Maybe a signature like this would convey the message better that it is intended to modify the internal state. Let's wait for someone else's review and go with the signature which has more support :) I'm also fine with yours even though it's not my preference
There was a problem hiding this comment.
Oh, and I almost forgot: having a completely new method would also minimize the BC break, since IntlDateformatter::parse() is not final, so people may have been overriding it.
There was a problem hiding this comment.
valid points, thanks. Will update later.
fbd4334 to
4f3af87
Compare
ext/intl/php_intl.stub.php
Outdated
| function datefmt_parse(IntlDateFormatter $formatter, string $string, &$offset = null): int|float|false {} | ||
|
|
||
| /** @param int $offset */ | ||
| function datefmt_parse_tocalendar(IntlDateFormatter $formatter, string $string, &$offset = null): int|float|false {} |
There was a problem hiding this comment.
lately, the procedural interface is not extended anymore 🤔 See https://externals.io/message/114473#114673
4f3af87 to
10aeb6d
Compare
|
ping :) |
Girgias
left a comment
There was a problem hiding this comment.
What does this new method actually do?
| if (UNEXPECTED(update_calendar)) { | ||
| UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo)); | ||
| udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | ||
| if( text_utf16 ){ |
There was a problem hiding this comment.
Nit: CS
| if( text_utf16 ){ | |
| if (text_utf16) { |
| timestamp = ucal_getMillis( parsed_calendar, &INTL_DATA_ERROR_CODE(dfo)); | ||
| } else { | ||
| timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | ||
| if( text_utf16 ){ |
There was a problem hiding this comment.
Nit: CS
| if( text_utf16 ){ | |
| if (text_utf16) { |
| char* text_to_parse = NULL; | ||
| size_t text_len =0; | ||
| zval* z_parse_pos = NULL; | ||
| int32_t parse_pos = -1; |
There was a problem hiding this comment.
CS: Either align them properly or don't bother having any spaces, but this is just weird
| DATE_FORMAT_METHOD_INIT_VARS; | ||
|
|
||
| /* Parse parameters. */ | ||
| if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!", |
There was a problem hiding this comment.
Nit: CS
| if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!", | |
| if (zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!", |
| zval* z_parse_pos = NULL; | ||
| int32_t parse_pos = -1; | ||
|
|
||
| DATE_FORMAT_METHOD_INIT_VARS; |
There was a problem hiding this comment.
Blerg I hate those sorts of macros.... but that's an existing problem with the codebase.
| DATE_FORMAT_METHOD_INIT_VARS; | ||
|
|
||
| /* Parse parameters. */ | ||
| if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|z!", |
There was a problem hiding this comment.
You don't need to use zend_parse_method_parameters() when just adding a new method, as the getThis() is pointless and the first param being an object is also unnecessary.
| } | ||
| } | ||
| internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value); | ||
| if(z_parse_pos) { |
There was a problem hiding this comment.
Nit: CS
| if(z_parse_pos) { | |
| if (z_parse_pos) { |
| if (z_parse_pos) { | ||
| zend_long long_parse_pos; | ||
| ZVAL_DEREF(z_parse_pos); | ||
| long_parse_pos = zval_get_long(z_parse_pos); |
There was a problem hiding this comment.
Use the new zval_try_get_long() API instead?
| RETURN_FALSE; | ||
| } | ||
| parse_pos = (int32_t)long_parse_pos; | ||
| if((size_t)parse_pos > text_len) { |
There was a problem hiding this comment.
Nit: CS
| if((size_t)parse_pos > text_len) { | |
| if ((size_t)parse_pos > text_len) { |
| RETURN_FALSE; | ||
| } | ||
| } | ||
| internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value); |
There was a problem hiding this comment.
Nit: CS
| internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos?&parse_pos:NULL, true, return_value); | |
| internal_parse_to_timestamp( dfo, text_to_parse, text_len, z_parse_pos ? &parse_pos : NULL, true, return_value); |
10aeb6d to
f15e633
Compare
| } | ||
| parse_pos = (int32_t)long_parse_pos; | ||
| if((size_t)parse_pos > text_len) { | ||
| if((size_t)parse_pos > ZSTR_LEN(text_to_parse)) { |
There was a problem hiding this comment.
Are negative indexes supported?
| Z_PARAM_ZVAL(z_parse_pos) | ||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| object = getThis(); |
f15e633 to
d69a34a
Compare
Girgias
left a comment
There was a problem hiding this comment.
Minor CS nits again, and adding test cases for the unhappy path
| UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo)); | ||
| udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | ||
| if(text_utf16) { |
There was a problem hiding this comment.
Nit: CS
| UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo)); | |
| udat_parseCalendar( DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | |
| if(text_utf16) { | |
| UCalendar *parsed_calendar = (UCalendar *)udat_getCalendar(DATE_FORMAT_OBJECT(dfo)); | |
| udat_parseCalendar(DATE_FORMAT_OBJECT(dfo), parsed_calendar, text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | |
| if (text_utf16) { |
| timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | ||
| if(text_utf16) { | ||
| efree(text_utf16); | ||
| } |
There was a problem hiding this comment.
Nit: CS
| timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | |
| if(text_utf16) { | |
| efree(text_utf16); | |
| } | |
| timestamp = udat_parse(DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); | |
| if (text_utf16) { | |
| efree(text_utf16); | |
| } |
| zend_string *text_to_parse = NULL; | ||
| zval* z_parse_pos = NULL; | ||
| int32_t parse_pos = -1; |
There was a problem hiding this comment.
I wouldn't bother aligning it
| zend_string *text_to_parse = NULL; | |
| zval* z_parse_pos = NULL; | |
| int32_t parse_pos = -1; | |
| zend_string *text_to_parse = NULL; | |
| zval* z_parse_pos = NULL; | |
| int32_t parse_pos = -1; |
| RETURN_FALSE; | ||
| } | ||
| parse_pos = (int32_t)long_parse_pos; | ||
| if(parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) { |
There was a problem hiding this comment.
Nit: CS
| if(parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) { | |
| if (parse_pos != -1 && (size_t)parse_pos > ZSTR_LEN(text_to_parse)) { |
| long_parse_pos = zval_try_get_long(z_parse_pos, &failed); | ||
| if (failed || ZEND_LONG_INT_OVFL(long_parse_pos)) { |
There was a problem hiding this comment.
If the parsing fails, I'm pretty sure it always throws an Error so having another intl specific extension being thrown seems odd? Also a test for this case?
d69a34a to
bc58213
Compare
|
|
||
| /** | ||
| * @param int $offset | ||
| * @tentative-return-type |
There was a problem hiding this comment.
no need for the tentative return type, as it's a brand new method :)
bc58213 to
03d4443
Compare
Girgias
left a comment
There was a problem hiding this comment.
Final comment, but LGTM otherwise
| bool failed = false; | ||
| long_parse_pos = zval_try_get_long(z_parse_pos, &failed); | ||
| if (failed) { | ||
| zend_argument_type_error(2, "invalid offset"); |
There was a problem hiding this comment.
Just to standardize the error message
| zend_argument_type_error(2, "invalid offset"); | |
| zend_argument_type_error(2, "must be of type int, %s given", zend_zval_value_name(z_parse_pos)); |
New option to update its internal calendar.
03d4443 to
ad4cfa9
Compare
|
@devnexen Please check this related failure: https://github.com/php/php-src/actions/runs/8887832558/job/24403750313 |
|
Nevermind, fixed here: 7f3fd30. |
New option to update its internal calendar.