Skip to content

Commit 890cc7c

Browse files
committed
Switch numeric values in tokens from f64/i64 to f32/i32
1 parent 4808d0e commit 890cc7c

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/color.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,18 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
309309
// Either integers or percentages, but all the same type.
310310
match try!(arguments.next()) {
311311
Token::Number(ref v) if v.int_value.is_some() => {
312-
red = (v.value / 255.) as f32;
312+
red = v.value / 255.;
313313
try!(arguments.expect_comma());
314314
green = try!(arguments.expect_integer()) as f32 / 255.;
315315
try!(arguments.expect_comma());
316316
blue = try!(arguments.expect_integer()) as f32 / 255.;
317317
}
318318
Token::Percentage(ref v) => {
319-
red = v.unit_value as f32;
319+
red = v.unit_value;
320320
try!(arguments.expect_comma());
321-
green = try!(arguments.expect_percentage()) as f32;
321+
green = try!(arguments.expect_percentage());
322322
try!(arguments.expect_comma());
323-
blue = try!(arguments.expect_percentage()) as f32;
323+
blue = try!(arguments.expect_percentage());
324324
}
325325
_ => return Err(())
326326
};
@@ -333,7 +333,7 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
333333
let lightness = (try!(arguments.expect_percentage())).max(0.).min(1.);
334334

335335
// http://www.w3.org/TR/css3-color/#hsl-color
336-
fn hue_to_rgb(m1: f64, m2: f64, mut h: f64) -> f64 {
336+
fn hue_to_rgb(m1: f32, m2: f32, mut h: f32) -> f32 {
337337
if h < 0. { h += 1. }
338338
if h > 1. { h -= 1. }
339339

@@ -345,14 +345,14 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
345345
let m2 = if lightness <= 0.5 { lightness * (saturation + 1.) }
346346
else { lightness + saturation - lightness * saturation };
347347
let m1 = lightness * 2. - m2;
348-
red = hue_to_rgb(m1, m2, hue + 1. / 3.) as f32;
349-
green = hue_to_rgb(m1, m2, hue) as f32;
350-
blue = hue_to_rgb(m1, m2, hue - 1. / 3.) as f32;
348+
red = hue_to_rgb(m1, m2, hue + 1. / 3.);
349+
green = hue_to_rgb(m1, m2, hue);
350+
blue = hue_to_rgb(m1, m2, hue - 1. / 3.);
351351
}
352352

353353
let alpha = if has_alpha {
354354
try!(arguments.expect_comma());
355-
(try!(arguments.expect_number())).max(0.).min(1.) as f32
355+
(try!(arguments.expect_number())).max(0.).min(1.)
356356
} else {
357357
1.
358358
};

src/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl<'i, 't> Parser<'i, 't> {
495495

496496
/// Parse a <number-token> and return the integer value.
497497
#[inline]
498-
pub fn expect_number(&mut self) -> Result<f64, ()> {
498+
pub fn expect_number(&mut self) -> Result<f32, ()> {
499499
match try!(self.next()) {
500500
Token::Number(NumericValue { value, .. }) => Ok(value),
501501
_ => Err(())
@@ -504,7 +504,7 @@ impl<'i, 't> Parser<'i, 't> {
504504

505505
/// Parse a <number-token> that does not have a fractional part, and return the integer value.
506506
#[inline]
507-
pub fn expect_integer(&mut self) -> Result<i64, ()> {
507+
pub fn expect_integer(&mut self) -> Result<i32, ()> {
508508
match try!(self.next()) {
509509
Token::Number(NumericValue { int_value, .. }) => int_value.ok_or(()),
510510
_ => Err(())
@@ -514,7 +514,7 @@ impl<'i, 't> Parser<'i, 't> {
514514
/// Parse a <percentage-token> and return the value.
515515
/// `0%` and `100%` map to `0.0` and `1.0` (not `100.0`), respectively.
516516
#[inline]
517-
pub fn expect_percentage(&mut self) -> Result<f64, ()> {
517+
pub fn expect_percentage(&mut self) -> Result<f32, ()> {
518518
match try!(self.next()) {
519519
Token::Percentage(PercentageValue { unit_value, .. }) => Ok(unit_value),
520520
_ => Err(())

src/tokenizer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ pub enum Token<'a> {
162162
#[derive(PartialEq, Debug, Copy, Clone)]
163163
pub struct NumericValue {
164164
/// The value as a float
165-
pub value: f64,
165+
pub value: f32,
166166

167167
/// If the origin source did not include a fractional part, the value as an integer.
168-
pub int_value: Option<i64>,
168+
pub int_value: Option<i32>,
169169

170170
/// Whether the number had a `+` or `-` sign.
171171
///
@@ -178,10 +178,10 @@ pub struct NumericValue {
178178
#[derive(PartialEq, Debug, Copy, Clone)]
179179
pub struct PercentageValue {
180180
/// The value as a float, divided by 100 so that the nominal range is 0.0 to 1.0.
181-
pub unit_value: f64,
181+
pub unit_value: f32,
182182

183183
/// If the origin source did not include a fractional part, the value as an integer. It is **not** divided by 100.
184-
pub int_value: Option<i64>,
184+
pub int_value: Option<i32>,
185185

186186
/// Whether the number had a `+` or `-` sign.
187187
pub signed: bool,
@@ -685,8 +685,8 @@ fn consume_numeric<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
685685
repr = &repr[1..]
686686
}
687687
// TODO: handle overflow
688-
(repr.parse::<f64>().unwrap(), if is_integer {
689-
Some(repr.parse::<i64>().unwrap())
688+
(repr.parse::<f32>().unwrap(), if is_integer {
689+
Some(repr.parse::<i32>().unwrap())
690690
} else {
691691
None
692692
})

0 commit comments

Comments
 (0)