Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[core] Add number-format expression - #14840

Merged
jmalanen merged 1 commit into
masterfrom
jmalanen-number-format
Jun 27, 2019
Merged

jmalanen merged 1 commit into
masterfrom
jmalanen-number-format

Conversation

@jmalanen

@jmalanen jmalanen commented Jun 4, 2019

Copy link
Copy Markdown
Contributor

Fixes: #13632

@jmalanen

jmalanen commented Jun 4, 2019

Copy link
Copy Markdown
Contributor Author

The number-format/currency test is still disabled because there are differences in the output between GL-JS and GL-Native:

Original
f([{},{"properties":{"locale":"ja-JP","currency":"JPY"}}])
Expected: "JP¥ 123,457"
Actual: "¥123,457"
f([{},{"properties":{"locale":"de-DE","currency":"EUR"}}])
Expected: "€ 123,456.79"
Actual: "123.456,79 €"

Roundtripped through serialize()
f([{},{"properties":{"locale":"ja-JP","currency":"JPY"}}])
Expected: "JP¥ 123,457"
Actual: "¥123,457"
f([{},{"properties":{"locale":"de-DE","currency":"EUR"}}])
Expected: "€ 123,456.79"
Actual: "123.456,79 €"

@jmalanen
jmalanen force-pushed the jmalanen-number-format branch 5 times, most recently from 31fa02e to fbde1db Compare June 4, 2019 15:13
@jmalanen

jmalanen commented Jun 4, 2019

Copy link
Copy Markdown
Contributor Author

I disabled also the remaining tests. The CI test system doesn't have "en-US" locale installed, which is expected by the GL-JS tests. This causes tests to fail since the outputs don't match.

Serialized

  • 123,456.789+ 123456.789Original
    f([{},{}])
    Expected: "123,456.789"
    Actual: "123456.789"

Roundtripped through serialize()
f([{},{}])
Expected: "123,456.789"
Actual: "123456.789"

  • failed number-format/default

Original
f([{},{"properties":{"locale":"en-US","min":15,"max":20}}])
Expected: "987,654,321.234568000000000"
Actual: "987654321.234568000000000"
f([{},{"properties":{"locale":"en-US","min":2,"max":4}}])
Expected: "987,654,321.2346"
Actual: "987654321.2346"

Roundtripped through serialize()
f([{},{"properties":{"locale":"en-US","min":15,"max":20}}])
Expected: "987,654,321.234568000000000"
Actual: "987654321.234568000000000"
f([{},{"properties":{"locale":"en-US","min":2,"max":4}}])
Expected: "987,654,321.2346"
Actual: "987654321.2346"

  • failed number-format/precision

@jmalanen
jmalanen force-pushed the jmalanen-number-format branch 3 times, most recently from a700d8c to 44bd11f Compare June 5, 2019 07:58

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

options seem to have just a few items, looks like std::unordered_map is an overkill, better use std::map

@pozdnyakov

Copy link
Copy Markdown
Contributor

There is a quite significant binary size increase with the patch, removal of unneeded optional could help to fix it.

@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from 44bd11f to ef6b53b Compare June 5, 2019 09:07
@pozdnyakov

Copy link
Copy Markdown
Contributor

@jmalanen pls see @kkaefer's patch removing using <locale> and <iostream>. Using of these classes must be causing the binary size increase.

@1ec5

1ec5 commented Jun 5, 2019

Copy link
Copy Markdown
Contributor

Use of the platforms’ built-in internationalization functionality will result in much less binary size increase compared to the current implementation in this PR.

@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from ef6b53b to 807f62a Compare June 6, 2019 12:54
@jmalanen
jmalanen requested a review from a team June 6, 2019 12:54

@1ec5 1ec5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for implementing the iOS/macOS side of things! We’re close, but we do need to figure out the right way to manage these formatter objects, as @julianrex points out.

Comment thread platform/darwin/src/string_nsstring.mm Outdated
uint8_t minFractionDigits,
uint8_t maxFractionDigits) {
NSNumberFormatter *_numberFormatter;
_numberFormatter = [[NSNumberFormatter alloc] init];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this method always get called on the same thread? If so, we can initialize the object just once using dispatch_once, hold onto it statically, and modify the options each time the method is called. Otherwise, we’ll need to lazily create a different formatter for each combination of options.

Alternatively, we can drop down to CFNumberFormatter, which might not have the same overhead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Previously, I had suggest _numberFormatter as the variable name with the assumption that the variable would be global rather than local. The underscore is unnecessary for a local variable.

(Also, you can combine a local variable declaration and definition on the same line.)

@jmalanen jmalanen Jun 19, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comments. Now the object is initialized just once using dispatch_once.

Comment thread platform/darwin/src/string_nsstring.mm Outdated
uint8_t maxFractionDigits) {
NSNumberFormatter *_numberFormatter;
_numberFormatter = [[NSNumberFormatter alloc] init];
_numberFormatter.locale = !localeId.empty() ? [NSLocale localeWithLocaleIdentifier:@(localeId.c_str())] : nil;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should make sure the default locale matches the default locale of the comparison expression operators as well as the number formatter in GL JS. nil results in either the application’s current locale or the system preferred locale; can’t remember which off the top of my head.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I checked the NSNumberFormatter documentation, but couldn't find what's the default locale in case of nil.

@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from 807f62a to 7027e47 Compare June 10, 2019 08:29
@1ec5 1ec5 added Core The cross-platform C++ core, aka mbgl expressions labels Jun 11, 2019
@jmalanen
jmalanen force-pushed the jmalanen-number-format branch 11 times, most recently from f4fa5bd to 855e48f Compare June 19, 2019 12:33
@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from 855e48f to cffc1fb Compare June 24, 2019 08:47
@jmalanen
jmalanen requested a review from 1ec5 June 24, 2019 11:04
@jmalanen
jmalanen force-pushed the jmalanen-number-format branch 3 times, most recently from 141a46b to 252658f Compare June 25, 2019 10:55
@jmalanen
jmalanen requested a review from tmpsantos June 25, 2019 11:27
@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from 252658f to a543cf8 Compare June 26, 2019 07:41

@tmpsantos tmpsantos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, just concerned about adding a dependency on QML for Qt.

Comment thread platform/qt/src/format_number.cpp Resolved
optional<std::unique_ptr<Expression>> locale_,
optional<std::unique_ptr<Expression>> currency_,
optional<std::unique_ptr<Expression>> minFractionDigits_,
optional<std::unique_ptr<Expression>> maxFractionDigits_)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not just std::unique_ptr<Expression> maxFractionDigits_ , maxFractionDigits(std::move(maxFractionDigits_)); (same for others) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

optional<std::unique_ptr<Expression>> locale_,
optional<std::unique_ptr<Expression>> currency_,
optional<std::unique_ptr<Expression>> minFractionDigits_,
optional<std::unique_ptr<Expression>> maxFractionDigits_);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

complex class needs not-inlined destructor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from a543cf8 to 0d6c3bd Compare June 27, 2019 11:38
@jmalanen
jmalanen force-pushed the jmalanen-number-format branch from 0d6c3bd to 50bdbea Compare June 27, 2019 11:44

@pozdnyakov pozdnyakov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm, thanks for following


QString formatted;
// Qt Locale::toString() API takes only one precision argument
(void)minFractionDigits;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: you don't need to do this, you can just remove minFractionDigits from the function signature.

@jmalanen
jmalanen merged commit 0ca8ea6 into master Jun 27, 2019
@jmalanen
jmalanen deleted the jmalanen-number-format branch June 28, 2019 11:30
@chloekraw chloekraw modified the milestone: release-ristretto Aug 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Core The cross-platform C++ core, aka mbgl expressions iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Port 'number-format' expression to native

6 participants