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

Implement Number.prototype.toExponential() #50

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'to_exponential'",
"cargo": {
"args": [
"test",
"--no-run",
"--test=to_exponential",
"--package=ryu-js"
],
"filter": {
"name": "to_exponential",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
Expand Down
46 changes: 46 additions & 0 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ impl Buffer {
str::from_utf8_unchecked(slice)
}
}

/// Print a floating point number into this buffer using the `Number.prototype.toExponential()` notation
/// and return a reference to its string representation within the buffer.
///
/// The `precision` argument must be between `[0, 100]` inclusive,
/// If a values value that is greater than the max is passed in will be clamped to max.
///
/// # Special cases
///
/// This function formats NaN as the string "NaN", positive infinity as
/// "Infinity", and negative infinity as "-Infinity" to match the [ECMAScript specification][spec].
///
/// [spec]: https://tc39.es/ecma262/#sec-numeric-types-number-toexponential
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn format_to_exponential<F: FloatToExponential>(&mut self, f: F, precision: u8) -> &str {
let precision = precision.min(100);

if f.is_nonfinite() {
return f.format_nonfinite();
}

unsafe {
let n = f.write_to_ryu_buffer_to_exponential(
precision,
self.bytes.as_mut_ptr().cast::<u8>(),
);
debug_assert!(n <= self.bytes.len());
let slice = slice::from_raw_parts(self.bytes.as_ptr().cast::<u8>(), n);
str::from_utf8_unchecked(slice)
}
}
}

impl Clone for Buffer {
Expand Down Expand Up @@ -155,11 +187,15 @@ impl Float for f64 {}
pub trait FloatToFixed: Sealed {}
impl FloatToFixed for f64 {}

pub trait FloatToExponential: Sealed {}
impl FloatToExponential for f64 {}

pub trait Sealed: Copy {
fn is_nonfinite(self) -> bool;
fn format_nonfinite(self) -> &'static str;
unsafe fn write_to_ryu_buffer(self, result: *mut u8) -> usize;
unsafe fn write_to_ryu_buffer_to_fixed(self, fraction_digits: u8, result: *mut u8) -> usize;
unsafe fn write_to_ryu_buffer_to_exponential(self, presission: u8, result: *mut u8) -> usize;
}

impl Sealed for f32 {
Expand Down Expand Up @@ -194,6 +230,11 @@ impl Sealed for f32 {
unsafe fn write_to_ryu_buffer_to_fixed(self, _fraction_digits: u8, _result: *mut u8) -> usize {
panic!("toFixed for f32 type is not implemented yet!")
}

#[inline]
unsafe fn write_to_ryu_buffer_to_exponential(self, _precision: u8, _result: *mut u8) -> usize {
panic!("toExponential for f32 type is not implemented yet!")
}
}

impl Sealed for f64 {
Expand Down Expand Up @@ -228,4 +269,9 @@ impl Sealed for f64 {
unsafe fn write_to_ryu_buffer_to_fixed(self, fraction_digits: u8, result: *mut u8) -> usize {
raw::format64_to_fixed(self, fraction_digits, result)
}

#[inline]
unsafe fn write_to_ryu_buffer_to_exponential(self, precision: u8, result: *mut u8) -> usize {
raw::format64_to_exponential(self, precision, result)
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub use crate::buffer::{Buffer, Float, FloatToFixed};

/// Unsafe functions that mirror the API of the C implementation of Ryū.
pub mod raw {
pub use crate::pretty::format64_to_exponential;
pub use crate::pretty::format64_to_fixed;
pub use crate::pretty::{format32, format64};
}
1 change: 1 addition & 0 deletions src/pretty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::ptr;
use no_panic::no_panic;

pub mod to_fixed;
pub use to_fixed::format64_to_exponential;
pub use to_fixed::format64_to_fixed;

/// Print f64 to the given buffer and return number of bytes written.
Expand Down
Loading
Loading