Skip to content

Commit 222f5fc

Browse files
boquanfuLastLeaf
boquanfu
authored andcommitted
feat: complete no-std support
1 parent f9ca7b8 commit 222f5fc

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ encoding_rs = "0.8"
2323
cssparser-macros = { path = "./macros", version = "0.6.1" }
2424
dtoa-short = "0.3"
2525
itoa = "1.0"
26-
phf = { version = "0.11.2", features = ["macros"] }
26+
phf = { version = "0.11.2", features = ["macros"], default-features = false }
2727
serde = { version = "1.0", features = ["derive"], optional = true }
2828
smallvec = "1.0"
29+
libm = "0.2.8"
2930

3031
[profile.profiling]
3132
inherits = "release"
@@ -37,7 +38,7 @@ bench = []
3738
dummy_match_byte = []
3839
# Useful for skipping tests when execution is slow, e.g., under miri
3940
skip_long_tests = []
40-
std = []
41+
std = ["phf/std"]
4142

4243
[workspace]
4344
members = [".", "./macros", "./color"]

src/color.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn clamp_unit_f32(val: f32) -> u8 {
4141
/// Round and clamp a single number to a u8.
4242
#[inline]
4343
pub fn clamp_floor_256_f32(val: f32) -> u8 {
44-
val.round().clamp(0., 255.) as u8
44+
crate::math::f32_round(val).clamp(0., 255.) as u8
4545
}
4646

4747
/// Serialize the alpha copmonent of a color according to the specification.
@@ -65,9 +65,9 @@ pub fn serialize_color_alpha(
6565
dest.write_str(if legacy_syntax { ", " } else { " / " })?;
6666

6767
// Try first with two decimal places, then with three.
68-
let mut rounded_alpha = (alpha * 100.).round() / 100.;
68+
let mut rounded_alpha = crate::math::f32_round(alpha * 100.) / 100.;
6969
if clamp_unit_f32(rounded_alpha) != clamp_unit_f32(alpha) {
70-
rounded_alpha = (alpha * 1000.).round() / 1000.;
70+
rounded_alpha = crate::math::f32_round(alpha * 1000.) / 1000.;
7171
}
7272

7373
rounded_alpha.to_css(dest)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ mod nth;
103103
mod parser;
104104
mod serializer;
105105
mod unicode_range;
106+
mod math;
106107

107108
#[cfg(test)]
108109
mod size_of_tests;

src/math.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
pub(crate) fn f32_trunc(val: f32) -> f32 {
3+
#[cfg(feature = "std")]
4+
{ val.round() }
5+
#[cfg(not(feature = "std"))]
6+
{ libm::roundf(val) }
7+
}
8+
9+
pub(crate) fn f32_round(val: f32) -> f32 {
10+
#[cfg(feature = "std")]
11+
{ val.round() }
12+
#[cfg(not(feature = "std"))]
13+
{ libm::roundf(val) }
14+
}
15+
16+
pub(crate) fn f64_pow(a: f64, b: f64) -> f64 {
17+
#[cfg(feature = "std")]
18+
{ f64::powf(a, b) }
19+
#[cfg(not(feature = "std"))]
20+
{ libm::pow(a, b) }
21+
}

src/serializer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ where
4949
dtoa_short::write(dest, value)?
5050
};
5151

52-
if int_value.is_none() && value.fract() == 0. && !notation.decimal_point && !notation.scientific
52+
if int_value.is_none() && value == crate::math::f32_trunc(value) && !notation.decimal_point && !notation.scientific
5353
{
5454
dest.write_str(".0")?;
5555
}

src/tokenizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ fn consume_numeric<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
10871087
break;
10881088
}
10891089
}
1090-
value *= f64::powf(10., sign * exponent);
1090+
value *= crate::math::f64_pow(10., sign * exponent);
10911091
}
10921092

10931093
let int_value = if is_integer {

0 commit comments

Comments
 (0)