[librsvg: 2/5] Have PathBuilder::parse instead of path_parser::parse_path_into_builder




commit 8813da581b611a998b4d950b1ca81915e5b736d3
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Jun 7 11:47:42 2021 -0500

    Have PathBuilder::parse instead of path_parser::parse_path_into_builder
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/542>

 benches/path_parser.rs |  6 +++---
 src/lib.rs             |  2 +-
 src/path_builder.rs    | 12 ++++++++++--
 src/path_parser.rs     | 17 ++++-------------
 src/shapes.rs          |  3 +--
 5 files changed, 19 insertions(+), 21 deletions(-)
---
diff --git a/benches/path_parser.rs b/benches/path_parser.rs
index 55f63655..510f44a1 100644
--- a/benches/path_parser.rs
+++ b/benches/path_parser.rs
@@ -2,8 +2,8 @@
 extern crate criterion;
 use criterion::{black_box, Criterion};
 
+use librsvg::bench_only::Lexer;
 use librsvg::bench_only::PathBuilder;
-use librsvg::bench_only::{parse_path_into_builder, Lexer};
 
 static INPUT: &'static str = "M10 20 C 30,40 50 60-70,80,90 100,110 120,130,140";
 
@@ -37,10 +37,10 @@ fn lex_path(input: &str) {
 fn path_parser(c: &mut Criterion) {
     c.bench_function("parse path into builder", |b| {
         let input = black_box(INPUT);
-        let mut builder = PathBuilder::default();
 
         b.iter(|| {
-            let _ = parse_path_into_builder(&input, &mut builder);
+            let mut builder = PathBuilder::default();
+            let _ = builder.parse(&input);
         });
     });
 
diff --git a/src/lib.rs b/src/lib.rs
index 3ec03ed0..825ac3ea 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -231,7 +231,7 @@ mod xml;
 #[doc(hidden)]
 pub mod bench_only {
     pub use crate::path_builder::PathBuilder;
-    pub use crate::path_parser::{parse_path_into_builder, Lexer};
+    pub use crate::path_parser::Lexer;
 }
 
 #[doc(hidden)]
diff --git a/src/path_builder.rs b/src/path_builder.rs
index 4f091e44..811660ec 100644
--- a/src/path_builder.rs
+++ b/src/path_builder.rs
@@ -25,6 +25,7 @@ use std::f64::consts::*;
 use std::slice;
 
 use crate::float_eq_cairo::ApproxEqCairo;
+use crate::path_parser::{ParseError, PathParser};
 use crate::util::clamp;
 
 /// Whether an arc's sweep should be >= 180 degrees, or smaller.
@@ -425,8 +426,10 @@ impl PathCommand {
 
 /// Constructs a path out of commands.
 ///
-/// When you are finished constructing a path builder, turn it into a `Path` with
-/// `into_path`.  You can then iterate on that `Path`'s commands with its methods.
+/// Create this with `PathBuilder::default`; you can then add commands to it or call the
+/// `parse` method.  When you are finished constructing a path builder, turn it into a
+/// `Path` with `into_path`.  You can then iterate on that `Path`'s commands with its
+/// methods.
 #[derive(Default)]
 pub struct PathBuilder {
     path_commands: TinyVec<[PathCommand; 32]>,
@@ -492,6 +495,11 @@ impl PackedCommand {
 }
 
 impl PathBuilder {
+    pub fn parse(&mut self, path_str: &str) -> Result<(), ParseError> {
+        let mut parser = PathParser::new(self, path_str);
+        parser.parse()
+    }
+
     /// Consumes the `PathBuilder` and returns a compact, immutable representation as a `Path`.
     pub fn into_path(self) -> Path {
         let num_coords = self
diff --git a/src/path_parser.rs b/src/path_parser.rs
index 56746a02..7a66981f 100644
--- a/src/path_parser.rs
+++ b/src/path_parser.rs
@@ -189,7 +189,7 @@ impl Iterator for Lexer<'_> {
     }
 }
 
-struct PathParser<'b> {
+pub struct PathParser<'b> {
     tokens: Lexer<'b>,
     current_pos_and_token: Option<(usize, Result<Token, LexError>)>,
 
@@ -239,7 +239,7 @@ struct PathParser<'b> {
 //     M.1-2,3E2-4
 //     M 0.1 -2 300 -4
 impl<'b> PathParser<'b> {
-    fn new(builder: &'b mut PathBuilder, path_str: &'b str) -> PathParser<'b> {
+    pub fn new(builder: &'b mut PathBuilder, path_str: &'b str) -> PathParser<'b> {
         let mut lexer = Lexer::new(path_str);
         let pt = lexer.next();
         PathParser {
@@ -379,7 +379,7 @@ impl<'b> PathParser<'b> {
     // This is the entry point for parsing a given blob of path data.
     // All the parsing just uses various match_* methods to consume tokens
     // and retrieve the values.
-    fn parse(&mut self) -> Result<(), ParseError> {
+    pub fn parse(&mut self) -> Result<(), ParseError> {
         if self.current_pos_and_token.is_none() {
             return Ok(());
         }
@@ -875,15 +875,6 @@ impl fmt::Display for ParseError {
     }
 }
 
-pub fn parse_path_into_builder(
-    path_str: &str,
-    builder: &mut PathBuilder,
-) -> Result<(), ParseError> {
-    let mut parser = PathParser::new(builder, path_str);
-
-    parser.parse()
-}
-
 #[cfg(test)]
 #[rustfmt::skip]
 mod tests {
@@ -917,7 +908,7 @@ mod tests {
         let expected_result = make_parse_result(error_pos_str, expected_error_kind);
 
         let mut builder = PathBuilder::default();
-        let result = parse_path_into_builder(path_str, &mut builder);
+        let result = builder.parse(path_str);
 
         let path = builder.into_path();
         let commands = path.iter().collect::<Vec<_>>();
diff --git a/src/shapes.rs b/src/shapes.rs
index e68e58af..5ac8a51e 100644
--- a/src/shapes.rs
+++ b/src/shapes.rs
@@ -17,7 +17,6 @@ use crate::length::*;
 use crate::node::{CascadedValues, Node, NodeBorrow};
 use crate::parsers::{optional_comma, Parse, ParseValue};
 use crate::path_builder::{LargeArc, Path as SvgPath, PathBuilder, Sweep};
-use crate::path_parser;
 use crate::xml::Attributes;
 
 #[derive(PartialEq)]
@@ -213,7 +212,7 @@ impl SetAttributes for Path {
         for (attr, value) in attrs.iter() {
             if attr.expanded() == expanded_name!("", "d") {
                 let mut builder = PathBuilder::default();
-                if let Err(e) = path_parser::parse_path_into_builder(value, &mut builder) {
+                if let Err(e) = builder.parse(value) {
                     // FIXME: we don't propagate errors upstream, but creating a partial
                     // path is OK per the spec
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]