From 7e5dcecaa88e579fdc6ac4b4115080780b86a710 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Mon, 16 Dec 2024 14:41:46 -0800 Subject: [PATCH] Add numbering_system getter to FixedDecimalFormatter (#5904) Part of #5900 --- components/decimal/src/lib.rs | 44 +++++++++++++++++++ .../src/extensions/unicode/value.rs | 13 ++++++ ffi/capi/bindings/c/FixedDecimalFormatter.h | 2 + .../cpp/icu4x/FixedDecimalFormatter.d.hpp | 2 + .../cpp/icu4x/FixedDecimalFormatter.hpp | 10 +++++ .../dart/FixedDecimalFormatter.g.dart | 12 +++++ .../demo_gen/FixedDecimalFormatter.d.ts | 1 + .../demo_gen/FixedDecimalFormatter.mjs | 19 ++++++++ ffi/capi/bindings/demo_gen/index.mjs | 21 +++++++++ .../bindings/js/FixedDecimalFormatter.d.ts | 2 + .../bindings/js/FixedDecimalFormatter.mjs | 13 ++++++ ffi/capi/src/decimal.rs | 5 +++ 12 files changed, 144 insertions(+) diff --git a/components/decimal/src/lib.rs b/components/decimal/src/lib.rs index 49f706aa8d8..b1cdce69120 100644 --- a/components/decimal/src/lib.rs +++ b/components/decimal/src/lib.rs @@ -104,6 +104,7 @@ pub use format::FormattedFixedDecimal; use alloc::string::String; use fixed_decimal::SignedFixedDecimal; +use icu_locale_core::extensions::unicode::Value; use icu_locale_core::locale; use icu_locale_core::preferences::{ define_preferences, extensions::unicode::keywords::NumberingSystem, @@ -214,4 +215,47 @@ impl FixedDecimalFormatter { pub fn format_to_string(&self, value: &SignedFixedDecimal) -> String { self.format(value).write_to_string().into_owned() } + + /// Gets the resolved numbering system identifier of this formatter. + /// + /// # Examples + /// + /// ``` + /// use icu::decimal::FixedDecimalFormatter; + /// use icu::locale::locale; + /// + /// let fmt_en = FixedDecimalFormatter::try_new( + /// locale!("en").into(), + /// Default::default() + /// ) + /// .unwrap(); + /// + /// let fmt_bn = FixedDecimalFormatter::try_new( + /// locale!("bn").into(), + /// Default::default() + /// ) + /// .unwrap(); + /// + /// let fmt_zh_nu = FixedDecimalFormatter::try_new( + /// locale!("zh-u-nu-hanidec").into(), + /// Default::default() + /// ) + /// .unwrap(); + /// + /// assert_eq!(fmt_en.numbering_system(), "latn"); + /// assert_eq!(fmt_bn.numbering_system(), "beng"); + /// assert_eq!(fmt_zh_nu.numbering_system(), "hanidec"); + /// ``` + pub fn numbering_system(&self) -> Value { + match Value::try_from_str(self.symbols.get().numsys()) { + Ok(v) => v, + Err(e) => { + debug_assert!( + false, + "Problem converting numbering system ID to Value: {e}" + ); + Value::new_empty() + } + } + } } diff --git a/components/locale_core/src/extensions/unicode/value.rs b/components/locale_core/src/extensions/unicode/value.rs index 02c2e9df21b..818659909a3 100644 --- a/components/locale_core/src/extensions/unicode/value.rs +++ b/components/locale_core/src/extensions/unicode/value.rs @@ -142,6 +142,19 @@ impl Value { self.0.len() } + /// Creates an empty [`Value`], which corresponds to a "true" value. + /// + /// # Examples + /// + /// ``` + /// use icu::locale::extensions::unicode::{value, Value}; + /// + /// assert_eq!(value!("true"), Value::new_empty()); + /// ``` + pub const fn new_empty() -> Self { + Self(ShortBoxSlice::new()) + } + /// Returns `true` if the Value has no subtags. /// /// # Examples diff --git a/ffi/capi/bindings/c/FixedDecimalFormatter.h b/ffi/capi/bindings/c/FixedDecimalFormatter.h index c7e4468b73d..644a66bdff5 100644 --- a/ffi/capi/bindings/c/FixedDecimalFormatter.h +++ b/ffi/capi/bindings/c/FixedDecimalFormatter.h @@ -31,6 +31,8 @@ icu4x_FixedDecimalFormatter_create_with_manual_data_mv1_result icu4x_FixedDecima void icu4x_FixedDecimalFormatter_format_mv1(const FixedDecimalFormatter* self, const SignedFixedDecimal* value, DiplomatWrite* write); +void icu4x_FixedDecimalFormatter_numbering_system_mv1(const FixedDecimalFormatter* self, DiplomatWrite* write); + void icu4x_FixedDecimalFormatter_destroy_mv1(FixedDecimalFormatter* self); diff --git a/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.d.hpp b/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.d.hpp index c9661b3b11d..1db040fed4b 100644 --- a/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.d.hpp +++ b/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.d.hpp @@ -41,6 +41,8 @@ class FixedDecimalFormatter { inline std::string format(const icu4x::SignedFixedDecimal& value) const; + inline std::string numbering_system() const; + inline const icu4x::capi::FixedDecimalFormatter* AsFFI() const; inline icu4x::capi::FixedDecimalFormatter* AsFFI(); inline static const icu4x::FixedDecimalFormatter* FromFFI(const icu4x::capi::FixedDecimalFormatter* ptr); diff --git a/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.hpp b/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.hpp index cfd8b63b972..64d7c7c7a35 100644 --- a/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.hpp +++ b/ffi/capi/bindings/cpp/icu4x/FixedDecimalFormatter.hpp @@ -32,6 +32,8 @@ namespace capi { void icu4x_FixedDecimalFormatter_format_mv1(const icu4x::capi::FixedDecimalFormatter* self, const icu4x::capi::SignedFixedDecimal* value, diplomat::capi::DiplomatWrite* write); + void icu4x_FixedDecimalFormatter_numbering_system_mv1(const icu4x::capi::FixedDecimalFormatter* self, diplomat::capi::DiplomatWrite* write); + void icu4x_FixedDecimalFormatter_destroy_mv1(FixedDecimalFormatter* self); @@ -76,6 +78,14 @@ inline std::string icu4x::FixedDecimalFormatter::format(const icu4x::SignedFixed return output; } +inline std::string icu4x::FixedDecimalFormatter::numbering_system() const { + std::string output; + diplomat::capi::DiplomatWrite write = diplomat::WriteFromString(output); + icu4x::capi::icu4x_FixedDecimalFormatter_numbering_system_mv1(this->AsFFI(), + &write); + return output; +} + inline const icu4x::capi::FixedDecimalFormatter* icu4x::FixedDecimalFormatter::AsFFI() const { return reinterpret_cast(this); } diff --git a/ffi/capi/bindings/dart/FixedDecimalFormatter.g.dart b/ffi/capi/bindings/dart/FixedDecimalFormatter.g.dart index 6ef3a34bfa4..c97fc5299da 100644 --- a/ffi/capi/bindings/dart/FixedDecimalFormatter.g.dart +++ b/ffi/capi/bindings/dart/FixedDecimalFormatter.g.dart @@ -72,6 +72,13 @@ final class FixedDecimalFormatter implements ffi.Finalizable { _icu4x_FixedDecimalFormatter_format_mv1(_ffi, value._ffi, write._ffi); return write.finalize(); } + + /// See the [Rust documentation for `numbering_system`](https://docs.rs/icu/latest/icu/decimal/struct.FixedDecimalFormatter.html#method.numbering_system) for more information. + String numberingSystem() { + final write = _Write(); + _icu4x_FixedDecimalFormatter_numbering_system_mv1(_ffi, write._ffi); + return write.finalize(); + } } @meta.RecordUse() @@ -98,3 +105,8 @@ external _ResultOpaqueInt32 _icu4x_FixedDecimalFormatter_create_with_manual_data @ffi.Native, ffi.Pointer, ffi.Pointer)>(isLeaf: true, symbol: 'icu4x_FixedDecimalFormatter_format_mv1') // ignore: non_constant_identifier_names external void _icu4x_FixedDecimalFormatter_format_mv1(ffi.Pointer self, ffi.Pointer value, ffi.Pointer write); + +@meta.RecordUse() +@ffi.Native, ffi.Pointer)>(isLeaf: true, symbol: 'icu4x_FixedDecimalFormatter_numbering_system_mv1') +// ignore: non_constant_identifier_names +external void _icu4x_FixedDecimalFormatter_numbering_system_mv1(ffi.Pointer self, ffi.Pointer write); diff --git a/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.d.ts b/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.d.ts index 0742354294a..867900b6360 100644 --- a/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.d.ts +++ b/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.d.ts @@ -2,3 +2,4 @@ import { FixedDecimalFormatter } from "icu4x" import { Locale } from "icu4x" import { SignedFixedDecimal } from "icu4x" export function format(name: string, groupingStrategy: FixedDecimalGroupingStrategy, f: number, magnitude: number); +export function numberingSystem(name: string, groupingStrategy: FixedDecimalGroupingStrategy); diff --git a/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.mjs b/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.mjs index fd0f6062631..74ce26c81f1 100644 --- a/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.mjs +++ b/ffi/capi/bindings/demo_gen/FixedDecimalFormatter.mjs @@ -27,3 +27,22 @@ export function format(name, groupingStrategy, f, magnitude) { ] ); } +export function numberingSystem(name, groupingStrategy) { + return (function (...args) { return args[0].numberingSystem(...args.slice(1)) }).apply( + null, + [ + FixedDecimalFormatter.createWithGroupingStrategy.apply( + null, + [ + Locale.fromString.apply( + null, + [ + name + ] + ), + groupingStrategy + ] + ) + ] + ); +} diff --git a/ffi/capi/bindings/demo_gen/index.mjs b/ffi/capi/bindings/demo_gen/index.mjs index 29ea392dd0a..e3053f423ac 100644 --- a/ffi/capi/bindings/demo_gen/index.mjs +++ b/ffi/capi/bindings/demo_gen/index.mjs @@ -1087,6 +1087,27 @@ let termini = Object.assign({ ] }, + "FixedDecimalFormatter.numberingSystem": { + func: FixedDecimalFormatterDemo.numberingSystem, + // For avoiding webpacking minifying issues: + funcName: "FixedDecimalFormatter.numberingSystem", + parameters: [ + + { + name: "Self:Locale:Name", + type: "string", + typeUse: "string" + }, + + { + name: "Self:GroupingStrategy", + type: "FixedDecimalGroupingStrategy", + typeUse: "enumerator" + } + + ] + }, + "SignedFixedDecimal.toString": { func: SignedFixedDecimalDemo.toString, // For avoiding webpacking minifying issues: diff --git a/ffi/capi/bindings/js/FixedDecimalFormatter.d.ts b/ffi/capi/bindings/js/FixedDecimalFormatter.d.ts index 396e47dead8..12d54cd908c 100644 --- a/ffi/capi/bindings/js/FixedDecimalFormatter.d.ts +++ b/ffi/capi/bindings/js/FixedDecimalFormatter.d.ts @@ -23,4 +23,6 @@ export class FixedDecimalFormatter { static createWithManualData(plusSignPrefix: string, plusSignSuffix: string, minusSignPrefix: string, minusSignSuffix: string, decimalSeparator: string, groupingSeparator: string, primaryGroupSize: number, secondaryGroupSize: number, minGroupSize: number, digits: Array, groupingStrategy: FixedDecimalGroupingStrategy | null): FixedDecimalFormatter; format(value: SignedFixedDecimal): string; + + numberingSystem(): string; } \ No newline at end of file diff --git a/ffi/capi/bindings/js/FixedDecimalFormatter.mjs b/ffi/capi/bindings/js/FixedDecimalFormatter.mjs index 20547087ee0..e83d1edd461 100644 --- a/ffi/capi/bindings/js/FixedDecimalFormatter.mjs +++ b/ffi/capi/bindings/js/FixedDecimalFormatter.mjs @@ -127,4 +127,17 @@ export class FixedDecimalFormatter { write.free(); } } + + numberingSystem() { + const write = new diplomatRuntime.DiplomatWriteBuf(wasm); + wasm.icu4x_FixedDecimalFormatter_numbering_system_mv1(this.ffiValue, write.buffer); + + try { + return write.readString8(); + } + + finally { + write.free(); + } + } } \ No newline at end of file diff --git a/ffi/capi/src/decimal.rs b/ffi/capi/src/decimal.rs index 79f0610f565..c4ae961d4ee 100644 --- a/ffi/capi/src/decimal.rs +++ b/ffi/capi/src/decimal.rs @@ -176,5 +176,10 @@ pub mod ffi { ) { let _infallible = self.0.format(&value.0).write_to(write); } + + #[diplomat::rust_link(icu::decimal::FixedDecimalFormatter::numbering_system, FnInStruct)] + pub fn numbering_system(&self, write: &mut diplomat_runtime::DiplomatWrite) { + let _infallible = self.0.numbering_system().write_to(write); + } } }