[librsvg] rsvg_css_parse_number_optional_number(): Move to Rust



commit 2f8629e873cd68856714cedf95379fe60b927981
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Jun 13 09:00:23 2017 -0500

    rsvg_css_parse_number_optional_number(): Move to Rust
    
    Also, make it return a gboolean for errors.

 rsvg-css.c          |   19 -------------------
 rsvg-css.h          |    4 +++-
 rust/src/lib.rs     |    4 ++++
 rust/src/parsers.rs |   33 +++++++++++++++++++++++++++++++++
 4 files changed, 40 insertions(+), 20 deletions(-)
---
diff --git a/rsvg-css.c b/rsvg-css.c
index ab11247..199792a 100644
--- a/rsvg-css.c
+++ b/rsvg-css.c
@@ -347,25 +347,6 @@ rsvg_css_parse_number_list (const char *in_str, guint * out_list_len)
     return output;
 }
 
-void
-rsvg_css_parse_number_optional_number (const char *str, double *x, double *y)
-{
-    char *endptr;
-
-    /* TODO: some error checking */
-
-    *x = g_ascii_strtod (str, &endptr);
-
-    if (endptr && *endptr != '\0')
-        while (g_ascii_isspace (*endptr) && *endptr)
-            endptr++;
-
-    if (endptr && *endptr)
-        *y = g_ascii_strtod (endptr, NULL);
-    else
-        *y = *x;
-}
-
 gboolean
 rsvg_css_parse_overflow (const char *str, gboolean * inherit)
 {
diff --git a/rsvg-css.h b/rsvg-css.h
index f9f6109..353bc11 100644
--- a/rsvg-css.h
+++ b/rsvg-css.h
@@ -127,8 +127,10 @@ PangoStretch rsvg_css_parse_font_stretch    (const char *str, gboolean * inherit
 G_GNUC_INTERNAL
 const char  *rsvg_css_parse_font_family            (const char *str, gboolean * inherit);
 
+/* Implemented in rust/src/parsers.rs */
 G_GNUC_INTERNAL
-void          rsvg_css_parse_number_optional_number    (const char *str, double *x, double *y);
+gboolean rsvg_css_parse_number_optional_number (const char *str, double *out_x, double *out_y);
+
 G_GNUC_INTERNAL
 gchar       **rsvg_css_parse_list           (const char *in_str, guint * out_list_len);
 G_GNUC_INTERNAL
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index c25a2f6..1323d1e 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -78,6 +78,10 @@ pub use opacity::{
     rsvg_css_parse_opacity
 };
 
+pub use parsers::{
+    rsvg_css_parse_number_optional_number
+};
+
 pub use path_builder::{
     rsvg_path_builder_add_to_cairo_context
 };
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index bb6eaf7..b81f103 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -1,4 +1,7 @@
+use ::libc;
 use ::cssparser::{Parser, Token, BasicParseError};
+use ::glib::translate::*;
+use ::glib_sys;
 use ::nom::{IResult, double, is_alphabetic};
 
 use std::str;
@@ -147,6 +150,36 @@ pub fn number_optional_number (s: &str) -> Result <(f64, f64), ParseError> {
     }
 }
 
+#[no_mangle]
+pub extern fn rsvg_css_parse_number_optional_number (s: *const libc::c_char,
+                                                     out_x: *mut f64,
+                                                     out_y: *mut f64) -> glib_sys::gboolean {
+    assert! (!s.is_null ());
+    assert! (!out_x.is_null ());
+    assert! (!out_y.is_null ());
+
+    let string = unsafe { String::from_glib_none (s) };
+
+    match number_optional_number (&string) {
+        Ok ((x, y)) => {
+            unsafe {
+                *out_x = x;
+                *out_y = y;
+            }
+            true
+        },
+
+        Err (_) => {
+            unsafe {
+                *out_x = 0.0;
+                *out_y = 0.0;
+            }
+            false
+        }
+    }.to_glib ()
+}
+
+
 // Parse a list-of-points as for polyline and polygon elements
 // https://www.w3.org/TR/SVG/shapes.html#PointsBNF
 


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