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

Remove negate #2060

Merged
merged 13 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ffi/diplomat/c/examples/fixeddecimal/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main() {
return 1;
}

ICU4XFixedDecimal_negate(decimal);
ICU4XFixedDecimal_set_sign(decimal, ICU4XFixedDecimalSign_Negative);

write = diplomat_simple_writeable(output, 40);

Expand Down
3 changes: 2 additions & 1 deletion ffi/diplomat/c/include/ICU4XFixedDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {

typedef struct ICU4XFixedDecimal ICU4XFixedDecimal;
#include "diplomat_result_box_ICU4XFixedDecimal_ICU4XError.h"
#include "ICU4XFixedDecimalSign.h"

ICU4XFixedDecimal* ICU4XFixedDecimal_create(int32_t v);

Expand All @@ -25,7 +26,7 @@ diplomat_result_box_ICU4XFixedDecimal_ICU4XError ICU4XFixedDecimal_create_fromst

bool ICU4XFixedDecimal_multiply_pow10(ICU4XFixedDecimal* self, int16_t power);

void ICU4XFixedDecimal_negate(ICU4XFixedDecimal* self);
void ICU4XFixedDecimal_set_sign(ICU4XFixedDecimal* self, ICU4XFixedDecimalSign sign);

void ICU4XFixedDecimal_pad_left(ICU4XFixedDecimal* self, int16_t position);

Expand Down
24 changes: 24 additions & 0 deletions ffi/diplomat/c/include/ICU4XFixedDecimalSign.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ICU4XFixedDecimalSign_H
#define ICU4XFixedDecimalSign_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum ICU4XFixedDecimalSign {
ICU4XFixedDecimalSign_None = 0,
ICU4XFixedDecimalSign_Negative = 1,
ICU4XFixedDecimalSign_Positive = 2,
} ICU4XFixedDecimalSign;

void ICU4XFixedDecimalSign_destroy(ICU4XFixedDecimalSign* self);

#ifdef __cplusplus
}
#endif
#endif
23 changes: 20 additions & 3 deletions ffi/diplomat/cpp/docs/source/fixed_decimal_ffi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
Multiply the :cpp:class:`ICU4XFixedDecimal` by a given power of ten.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.multiply_pow10>`__ for more information.

.. cpp:function:: void negate()
.. cpp:function:: void set_sign(ICU4XFixedDecimalSign sign)

Invert the sign of the :cpp:class:`ICU4XFixedDecimal`.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.negate>`__ for more information.
Set the sign of the :cpp:class:`ICU4XFixedDecimal`.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.set_sign>`__ for more information.

.. cpp:function:: void pad_left(int16_t position)

Expand All @@ -64,3 +64,20 @@

Format the :cpp:class:`ICU4XFixedDecimal` as a string.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.write_to>`__ for more information.

.. cpp:enum-struct:: ICU4XFixedDecimalSign

The sign of a FixedDecimal, as shown in formatting.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/enum.Sign.html>`__ for more information.

.. cpp:enumerator:: None

No sign (implicitly positive, e.g., 1729).

.. cpp:enumerator:: Negative

A negative sign, e.g., -1729.

.. cpp:enumerator:: Positive

An explicit positive sign, e.g., +1729.
2 changes: 1 addition & 1 deletion ffi/diplomat/cpp/examples/fixeddecimal/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main() {
}

decimal.multiply_pow10(2);
decimal.negate();
decimal.set_sign(ICU4XFixedDecimalSign::Negative);
out = fdf.format(decimal).ok().value();
std::cout << "Value x100 and negated is " << out << std::endl;
if (out != "-১০,০০,০০,৭০০") {
Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/cpp/examples/fixeddecimal_wasm/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int runFixedDecimal() {
}

decimal.multiply_pow10(2);
decimal.negate();
decimal.set_sign(ICU4XFixedDecimalSign::Negative);
out = fdf.format(decimal).ok().value();
std::cout << "Value x100 and negated is " << out << std::endl;
if (out != "-১০,০০,০০,৭০০") {
Expand Down
3 changes: 2 additions & 1 deletion ffi/diplomat/cpp/include/ICU4XFixedDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {

typedef struct ICU4XFixedDecimal ICU4XFixedDecimal;
#include "diplomat_result_box_ICU4XFixedDecimal_ICU4XError.h"
#include "ICU4XFixedDecimalSign.h"

ICU4XFixedDecimal* ICU4XFixedDecimal_create(int32_t v);

Expand All @@ -25,7 +26,7 @@ diplomat_result_box_ICU4XFixedDecimal_ICU4XError ICU4XFixedDecimal_create_fromst

bool ICU4XFixedDecimal_multiply_pow10(ICU4XFixedDecimal* self, int16_t power);

void ICU4XFixedDecimal_negate(ICU4XFixedDecimal* self);
void ICU4XFixedDecimal_set_sign(ICU4XFixedDecimal* self, ICU4XFixedDecimalSign sign);

void ICU4XFixedDecimal_pad_left(ICU4XFixedDecimal* self, int16_t position);

Expand Down
11 changes: 6 additions & 5 deletions ffi/diplomat/cpp/include/ICU4XFixedDecimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace capi {

class ICU4XFixedDecimal;
#include "ICU4XError.hpp"
#include "ICU4XFixedDecimalSign.hpp"

/**
* A destruction policy for using ICU4XFixedDecimal with std::unique_ptr.
Expand Down Expand Up @@ -77,11 +78,11 @@ class ICU4XFixedDecimal {
bool multiply_pow10(int16_t power);

/**
* Invert the sign of the [`ICU4XFixedDecimal`].
* Set the sign of the [`ICU4XFixedDecimal`].
*
* See the [Rust documentation](https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.negate) for more information.
* See the [Rust documentation](https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.set_sign) for more information.
*/
void negate();
void set_sign(ICU4XFixedDecimalSign sign);

/**
* Zero-pad the [`ICU4XFixedDecimal`] on the left to a particular position
Expand Down Expand Up @@ -175,8 +176,8 @@ inline diplomat::result<ICU4XFixedDecimal, ICU4XError> ICU4XFixedDecimal::create
inline bool ICU4XFixedDecimal::multiply_pow10(int16_t power) {
return capi::ICU4XFixedDecimal_multiply_pow10(this->inner.get(), power);
}
inline void ICU4XFixedDecimal::negate() {
capi::ICU4XFixedDecimal_negate(this->inner.get());
inline void ICU4XFixedDecimal::set_sign(ICU4XFixedDecimalSign sign) {
capi::ICU4XFixedDecimal_set_sign(this->inner.get(), static_cast<capi::ICU4XFixedDecimalSign>(sign));
}
inline void ICU4XFixedDecimal::pad_left(int16_t position) {
capi::ICU4XFixedDecimal_pad_left(this->inner.get(), position);
Expand Down
24 changes: 24 additions & 0 deletions ffi/diplomat/cpp/include/ICU4XFixedDecimalSign.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ICU4XFixedDecimalSign_H
#define ICU4XFixedDecimalSign_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum ICU4XFixedDecimalSign {
ICU4XFixedDecimalSign_None = 0,
ICU4XFixedDecimalSign_Negative = 1,
ICU4XFixedDecimalSign_Positive = 2,
} ICU4XFixedDecimalSign;

void ICU4XFixedDecimalSign_destroy(ICU4XFixedDecimalSign* self);

#ifdef __cplusplus
}
#endif
#endif
41 changes: 41 additions & 0 deletions ffi/diplomat/cpp/include/ICU4XFixedDecimalSign.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef ICU4XFixedDecimalSign_HPP
#define ICU4XFixedDecimalSign_HPP
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <algorithm>
#include <memory>
#include <variant>
#include <optional>
#include "diplomat_runtime.hpp"

namespace capi {
#include "ICU4XFixedDecimalSign.h"
}



/**
* The sign of a FixedDecimal, as shown in formatting.
*
* See the [Rust documentation](https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/enum.Sign.html) for more information.
*/
enum struct ICU4XFixedDecimalSign {

/**
* No sign (implicitly positive, e.g., 1729).
*/
None = 0,

/**
* A negative sign, e.g., -1729.
*/
Negative = 1,

/**
* An explicit positive sign, e.g., +1729.
*/
Positive = 2,
};

#endif
31 changes: 27 additions & 4 deletions ffi/diplomat/src/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use fixed_decimal::decimal::Sign;

#[diplomat::bridge]
pub mod ffi {
use alloc::boxed::Box;
Expand All @@ -15,6 +17,17 @@ pub mod ffi {
#[diplomat::rust_link(fixed_decimal::decimal::FixedDecimal, Struct)]
pub struct ICU4XFixedDecimal(pub FixedDecimal);

/// The sign of a FixedDecimal, as shown in formatting.
eggrobin marked this conversation as resolved.
Show resolved Hide resolved
#[diplomat::rust_link(fixed_decimal::decimal::Sign, Enum)]
pub enum ICU4XFixedDecimalSign {
/// No sign (implicitly positive, e.g., 1729).
None,
/// A negative sign, e.g., -1729.
Negative,
/// An explicit positive sign, e.g., +1729.
Positive,
}

fn convert(dec: FixedDecimal) -> Box<ICU4XFixedDecimal> {
Box::new(ICU4XFixedDecimal(dec))
}
Expand Down Expand Up @@ -80,10 +93,10 @@ pub mod ffi {
self.0.multiply_pow10(power).is_ok()
}

/// Invert the sign of the [`ICU4XFixedDecimal`].
#[diplomat::rust_link(fixed_decimal::decimal::FixedDecimal::negate, FnInStruct)]
pub fn negate(&mut self) {
self.0.negate()
/// Set the sign of the [`ICU4XFixedDecimal`].
#[diplomat::rust_link(fixed_decimal::decimal::FixedDecimal::set_sign, FnInStruct)]
pub fn set_sign(&mut self, sign: ICU4XFixedDecimalSign) {
self.0.set_sign(sign.into())
}

/// Zero-pad the [`ICU4XFixedDecimal`] on the left to a particular position
Expand Down Expand Up @@ -113,3 +126,13 @@ pub mod ffi {
}
}
}

impl From<ffi::ICU4XFixedDecimalSign> for Sign {
fn from(other: ffi::ICU4XFixedDecimalSign) -> Self {
match other {
ffi::ICU4XFixedDecimalSign::None => Self::None,
ffi::ICU4XFixedDecimalSign::Negative => Self::Negative,
ffi::ICU4XFixedDecimalSign::Positive => Self::Positive,
}
}
}
6 changes: 3 additions & 3 deletions ffi/diplomat/wasm/bench/fixed-decimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ suite = suite.add("FixedDecimal.multiply_pow10", () => {
decimal.multiply_pow10(-2);
});

suite = suite.add("FixedDecimal.negate", () => {
decimal.negate();
decimal.negate();
suite = suite.add("FixedDecimal.set_sign", () => {
decimal.set_sign("Negative");
decimal.set_sign("None");
});

suite
Expand Down
11 changes: 8 additions & 3 deletions ffi/diplomat/wasm/docs/fixed_decimal_ffi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
Multiply the :js:class:`ICU4XFixedDecimal` by a given power of ten.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.multiply_pow10>`__ for more information.

.. js:function:: negate()
.. js:function:: set_sign(sign)

Invert the sign of the :js:class:`ICU4XFixedDecimal`.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.negate>`__ for more information.
Set the sign of the :js:class:`ICU4XFixedDecimal`.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.set_sign>`__ for more information.

.. js:function:: pad_left(position)

Expand All @@ -59,3 +59,8 @@

Format the :js:class:`ICU4XFixedDecimal` as a string.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.write_to>`__ for more information.

.. js:class:: ICU4XFixedDecimalSign

The sign of a FixedDecimal, as shown in formatting.
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/enum.Sign.html>`__ for more information.
2 changes: 1 addition & 1 deletion ffi/diplomat/wasm/example/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ICU4XFixedDecimal, ICU4XDataProvider, ICU4XLocale, ICU4XFixedDecimalFor

const decimal = ICU4XFixedDecimal.create(1234);
decimal.multiply_pow10(-2);
decimal.negate();
decimal.set_sign("Negative");
console.log(decimal.to_string());

const dataProvider = ICU4XDataProvider.create_test().provider;
Expand Down
15 changes: 13 additions & 2 deletions ffi/diplomat/wasm/lib/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ export class ICU4XFixedDecimal {
return diplomat_out;
}

negate() {
const diplomat_out = wasm.ICU4XFixedDecimal_negate(this.underlying);
set_sign(sign) {
const diplomat_out = wasm.ICU4XFixedDecimal_set_sign(this.underlying, ICU4XFixedDecimalSign_js_to_rust[sign]);
}

pad_left(position) {
Expand Down Expand Up @@ -871,6 +871,17 @@ const ICU4XFixedDecimalGroupingStrategy_rust_to_js = {
3: "Min2",
};

const ICU4XFixedDecimalSign_js_to_rust = {
"None": 0,
"Negative": 1,
"Positive": 2,
};
const ICU4XFixedDecimalSign_rust_to_js = {
0: "None",
1: "Negative",
2: "Positive",
};

const ICU4XFixedDecimalSignDisplay_js_to_rust = {
"Auto": 0,
"Never": 1,
Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/wasm/test/fixed-decimal-format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("format a long decimal", t => {
test("format a negated, scaled decimal", t => {
const decimal = ICU4XFixedDecimal.create(1000007);
decimal.multiply_pow10(2);
decimal.negate();
decimal.set_sign("Negative");

t.is(format.format(decimal), "-১০,০০,০০,৭০০");
});
2 changes: 1 addition & 1 deletion ffi/diplomat/wasm/test/fixed-decimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test("multiply a decimal by a power of 10", t => {

test("negate a decimal", t => {
const decimal = ICU4XFixedDecimal.create(1234);
decimal.negate();
decimal.set_sign("Negative");

t.is(decimal.to_string(), "-1234");
});
Loading