-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/intl: IntlDateFormatter::parse adds new optional argument. #13779
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
Conversation
bacd94e
to
fbd4334
Compare
@@ -154,10 +154,11 @@ public static function formatObject($datetime, $format = null, ?string $locale = | |||
|
|||
/** | |||
* @param int $offset | |||
* @param bool $updateCalendar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
* @tentative-return-type | ||
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valid points, thanks. Will update later.
fbd4334
to
4f3af87
Compare
ext/intl/php_intl.stub.php
Outdated
@@ -377,6 +377,9 @@ function datefmt_format_object($datetime, $format = null, ?string $locale = null | |||
/** @param int $offset */ | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lately, the procedural interface is not extended anymore 🤔 See https://externals.io/message/114473#114673
4f3af87
to
10aeb6d
Compare
ping :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
RETURN_FALSE; | ||
} | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are negative indexes supported?
Z_PARAM_ZVAL(z_parse_pos) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
object = getThis(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ZEND_THIS
?
f15e633
to
d69a34a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -159,6 +159,12 @@ public static function formatObject($datetime, $format = null, ?string $locale = | |||
*/ | |||
public function parse(string $string, &$offset = null): int|float|false {} | |||
|
|||
/** | |||
* @param int $offset | |||
* @tentative-return-type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for the tentative return type, as it's a brand new method :)
bc58213
to
03d4443
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.