Skip to content
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

ICU-22994 Use C++ typeid() instead of Calendar::getType() for calendar type comparison #3310

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

FrankYFTang
Copy link
Contributor

@FrankYFTang FrankYFTang commented Dec 18, 2024

Use typeid for calendar type comparison instead of strcmp.

Checklist

  • Required: Issue filed: ICU-22994
  • Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-1234 Fix xyz"
  • Required: The PR description must include the link to the Jira Issue, for example by completing the URL in the first checklist item
  • Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-1234 Fix xyz"
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable

@FrankYFTang FrankYFTang force-pushed the ICU-22994-FixCalendarGetType branch from 2fb4782 to 53bbf05 Compare December 19, 2024 00:55
@jira-pull-request-webhook
Copy link

Notice: the branch changed across the force-push!

  • icu4c/source/i18n/datefmt.cpp is different

View Diff Across Force-Push

~ Your Friendly Jira-GitHub PR Checker Bot

@FrankYFTang FrankYFTang force-pushed the ICU-22994-FixCalendarGetType branch from 53bbf05 to 8f48ddf Compare December 19, 2024 00:58
@jira-pull-request-webhook
Copy link

Notice: the branch changed across the force-push!

  • icu4c/source/i18n/smpdtfmt.cpp is different

View Diff Across Force-Push

~ Your Friendly Jira-GitHub PR Checker Bot

@jira-pull-request-webhook
Copy link

Notice: the branch changed across the force-push!

  • icu4c/source/i18n/windtfmt.cpp is different

View Diff Across Force-Push

~ Your Friendly Jira-GitHub PR Checker Bot

icu4c/source/i18n/datefmt.cpp Outdated Show resolved Hide resolved
// Avoid a heap allocation and corresponding free for the common case
if (uprv_strcmp(calType, "gregorian") == 0) {
if (GregorianCalendar::getStaticClassID() == fCalendar->getDynamicClassID()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questions that I had asked on the ticket:

  • Why is using getType() bad? It seems like it’s public and advertised.
  • Is strcmp() actually slow for short strings that usually differ in the first couple of characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think such usage is bad because our code could easily misspell the string

@markusicu markusicu changed the title ICU-22994 Remove misuage of Calendar::getType ICU-22994 Remove misusage of Calendar::getType Jan 29, 2025
@markusicu
Copy link
Member

nit: commit message typo "misuage"; i just fixed the PR title

@FrankYFTang FrankYFTang requested a review from markusicu January 30, 2025 20:04
@FrankYFTang FrankYFTang changed the title ICU-22994 Remove misusage of Calendar::getType ICU-22994 Use typeid insetad of Calendar::getType for calendar type comparision Jan 30, 2025
@FrankYFTang FrankYFTang changed the title ICU-22994 Use typeid insetad of Calendar::getType for calendar type comparision ICU-22994 Use C++ typeid() insetad of Calendar::getType() for calendar type comparision Jan 30, 2025
icu4c/source/i18n/datefmt.cpp Outdated Show resolved Hide resolved
icu4c/source/i18n/smpdtfmt.cpp Outdated Show resolved Hide resolved
@FrankYFTang FrankYFTang requested a review from roubert January 30, 2025 22:03
@FrankYFTang
Copy link
Contributor Author

PTAL

richgillam
richgillam previously approved these changes Jan 30, 2025
Copy link
Contributor

@richgillam richgillam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks fine. No notes beyond those the other reviewers have already put out.

That said, it looks like all of this is calendar-specific logic in the date formatters-- that is, lots of "if this is an XXX calendar, do this special thing we only do for XXX calendars". It's clearly been that way for a long time, and I'm certainly not proposing you redesign the whole thing now, but I wonder (longer term) if we can find a way to eliminate all of that in favor of better compartmentalization between classes and more actual polymorphic behavior. I don't know what that'd look like or if it's really worth the trouble, but it really seems like the formatters have to know way too much about the internals of the calendars right now.

@FrankYFTang
Copy link
Contributor Author

This all looks fine. No notes beyond those the other reviewers have already put out.

That said, it looks like all of this is calendar-specific logic in the date formatters-- that is, lots of "if this is an XXX calendar, do this special thing we only do for XXX calendars". It's clearly been that way for a long time, and I'm certainly not proposing you redesign the whole thing now, but I wonder (longer term) if we can find a way to eliminate all of that in favor of better compartmentalization between classes and more actual polymorphic behavior. I don't know what that'd look like or if it's really worth the trouble, but it really seems like the formatters have to know way too much about the internals of the calendars right now.

I totally agree with you. It is my desire to do that later but I still have trouble to figure out how to achieve that.

@richgillam
Copy link
Contributor

I totally agree with you. It is my desire to do that later but I still have trouble to figure out how to achieve that.

Yeah, it's anything but straightforward. We'll figure something out eventually, I'm sure.

@FrankYFTang
Copy link
Contributor Author

one issue filed under https://unicode-org.atlassian.net/browse/ICU-23030

@markusicu markusicu changed the title ICU-22994 Use C++ typeid() insetad of Calendar::getType() for calendar type comparision ICU-22994 Use C++ typeid() instead of Calendar::getType() for calendar type comparison Jan 30, 2025
markusicu
markusicu previously approved these changes Jan 30, 2025
icu4c/source/i18n/smpdtfmt.cpp Outdated Show resolved Hide resolved
@FrankYFTang FrankYFTang dismissed stale reviews from markusicu and richgillam via dc9937d January 30, 2025 23:37
@FrankYFTang FrankYFTang requested a review from markusicu January 30, 2025 23:38
@markusicu markusicu dismissed roubert’s stale review January 30, 2025 23:42

ftang made the changes that roubert requested

@markusicu
Copy link
Member

I dismissed @roubert 's request for changes since @FrankYFTang made all of those changes.

Please squash, and watch for commit message typos.

@FrankYFTang FrankYFTang force-pushed the ICU-22994-FixCalendarGetType branch from dc9937d to ce491fb Compare January 30, 2025 23:43
@jira-pull-request-webhook
Copy link

Hooray! The files in the branch are the same across the force-push. 😃

~ Your Friendly Jira-GitHub PR Checker Bot

Copy link
Member

@roubert roubert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@roubert roubert merged commit ede201c into unicode-org:main Jan 31, 2025
94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants