script/svgpath/number.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
// Copyright 2018 the SVG Types Authors
// Copyright 2025 the Servo Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
use std::str::FromStr;
use crate::svgpath::{Error, Stream};
/// An [SVG number](https://www.w3.org/TR/SVG2/types.html#InterfaceSVGNumber).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Number(pub f32);
impl std::str::FromStr for Number {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let mut s = Stream::from(text);
let n = s.parse_number()?;
s.skip_spaces();
if !s.at_end() {
return Err(Error);
}
Ok(Self(n))
}
}
impl Stream<'_> {
/// Parses number from the stream.
///
/// This method will detect a number length and then
/// will pass a substring to the `f64::from_str` method.
///
/// <https://www.w3.org/TR/SVG2/types.html#InterfaceSVGNumber>
pub fn parse_number(&mut self) -> Result<f32, Error> {
// Strip off leading whitespaces.
self.skip_spaces();
if self.at_end() {
return Err(Error);
}
self.parse_number_impl().map_err(|_| Error)
}
fn parse_number_impl(&mut self) -> Result<f32, Error> {
let start = self.pos();
let mut c = self.curr_byte()?;
// Consume sign.
if matches!(c, b'+' | b'-') {
self.advance(1);
c = self.curr_byte()?;
}
// Consume integer.
match c {
b'0'..=b'9' => self.skip_digits(),
b'.' => {},
_ => return Err(Error),
}
// Consume fraction.
if let Ok(b'.') = self.curr_byte() {
self.advance(1);
self.skip_digits();
}
if let Ok(c) = self.curr_byte() {
if matches!(c, b'e' | b'E') {
let c2 = self.next_byte()?;
// Check for `em`/`ex`.
if c2 != b'm' && c2 != b'x' {
self.advance(1);
match self.curr_byte()? {
b'+' | b'-' => {
self.advance(1);
self.skip_digits();
},
b'0'..=b'9' => self.skip_digits(),
_ => {
return Err(Error);
},
}
}
}
}
let s = self.slice_back(start);
// Use the default f32 parser now.
if let Ok(n) = f32::from_str(s) {
// inf, nan, etc. are an error.
if n.is_finite() {
return Ok(n);
}
}
Err(Error)
}
/// Parses number from a list of numbers.
pub fn parse_list_number(&mut self) -> Result<f32, Error> {
if self.at_end() {
return Err(Error);
}
let n = self.parse_number()?;
self.skip_spaces();
self.parse_list_separator();
Ok(n)
}
}
/// A pull-based [`<list-of-numbers>`] parser.
///
/// # Examples
///
/// ```
/// use svgtypes::NumberListParser;
///
/// let mut p = NumberListParser::from("10, 20 -50");
/// assert_eq!(p.next().unwrap().unwrap(), 10.0);
/// assert_eq!(p.next().unwrap().unwrap(), 20.0);
/// assert_eq!(p.next().unwrap().unwrap(), -50.0);
/// assert_eq!(p.next().is_none(), true);
/// ```
///
/// [`<list-of-numbers>`]: https://www.w3.org/TR/SVG2/types.html#InterfaceSVGNumberList
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NumberListParser<'a>(Stream<'a>);
impl<'a> From<&'a str> for NumberListParser<'a> {
#[inline]
fn from(v: &'a str) -> Self {
NumberListParser(Stream::from(v))
}
}
impl Iterator for NumberListParser<'_> {
type Item = Result<f32, Error>;
fn next(&mut self) -> Option<Self::Item> {
if self.0.at_end() {
None
} else {
let v = self.0.parse_list_number();
if v.is_err() {
self.0.jump_to_end();
}
Some(v)
}
}
}
#[rustfmt::skip]
#[cfg(test)]
mod tests {
use crate::svgpath::Stream;
macro_rules! test_p {
($name:ident, $text:expr, $result:expr) => (
#[test]
fn $name() {
let mut s = Stream::from($text);
assert_eq!(s.parse_number().unwrap(), $result);
}
)
}
test_p!(parse_1, "0", 0.0);
test_p!(parse_2, "1", 1.0);
test_p!(parse_3, "-1", -1.0);
test_p!(parse_4, " -1 ", -1.0);
test_p!(parse_5, " 1 ", 1.0);
test_p!(parse_6, ".4", 0.4);
test_p!(parse_7, "-.4", -0.4);
test_p!(parse_8, "-.4text", -0.4);
test_p!(parse_9, "-.01 text", -0.01);
test_p!(parse_10, "-.01 4", -0.01);
test_p!(parse_11, ".0000000000008", 0.0000000000008);
test_p!(parse_12, "1000000000000", 1000000000000.0);
test_p!(parse_13, "123456.123456", 123456.123456);
test_p!(parse_14, "+10", 10.0);
test_p!(parse_15, "1e2", 100.0);
test_p!(parse_16, "1e+2", 100.0);
test_p!(parse_17, "1E2", 100.0);
test_p!(parse_18, "1e-2", 0.01);
test_p!(parse_19, "1ex", 1.0);
test_p!(parse_20, "1em", 1.0);
test_p!(parse_21, "12345678901234567890", 12345678901234567000.0);
test_p!(parse_22, "0.", 0.0);
test_p!(parse_23, "1.3e-2", 0.013);
// test_number!(parse_24, "1e", 1.0); // TODO: this
}