Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape strings passed to gnuplot. #64

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/axes2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::axes_common::*;
use crate::coordinates::*;
use crate::datatype::*;
use crate::options::*;
use crate::util::OneWayOwned;
use crate::util::{escape, OneWayOwned};
use crate::writer::Writer;

struct LegendData
Expand Down Expand Up @@ -83,7 +83,7 @@ impl LegendData
Title(ref s) =>
{
w.write_str(" title \"");
w.write_str(s);
w.write_str(&escape(s));
w.write_str("\"");
}
}
Expand All @@ -92,7 +92,7 @@ impl LegendData
Font(ref f, s) =>
{
w.write_str(" font \"");
w.write_str(f);
w.write_str(&escape(f));
w.write_str(",");
w.write_str(&s.to_string()[..]);
w.write_str("\"");
Expand All @@ -102,7 +102,7 @@ impl LegendData
TextColor(ref s) =>
{
w.write_str(" textcolor rgb \"");
w.write_str(&s);
w.write_str(&escape(s));
w.write_str("\"");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/axes_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::coordinates::*;

use crate::datatype::*;
use crate::options::*;
use crate::util::OneWayOwned;
use crate::util::{escape, OneWayOwned};
use crate::writer::*;
use std::borrow::Borrow;

Expand Down Expand Up @@ -419,7 +419,7 @@ impl PlotElement
first_opt! {self.options,
Caption(ref s) =>
{
writer.write_str(&s);
writer.write_str(&escape(s));
}
}
writer.write_str("\"");
Expand Down Expand Up @@ -473,7 +473,7 @@ impl LabelData
self.label_type.write_label_str(w);

w.write_str(" \"");
w.write_str(&self.text);
w.write_str(&escape(&self.text));
w.write_str("\"");

write_out_label_options(self.label_type, &self.options[..], w);
Expand Down
15 changes: 11 additions & 4 deletions src/figure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::axes2d::*;
use crate::axes3d::*;

use crate::options::{GnuplotVersion, MultiplotFillDirection, MultiplotFillOrder};
use crate::util::escape;
use crate::writer::Writer;
use std::cell::RefCell;
use std::fs::File;
Expand Down Expand Up @@ -508,7 +509,11 @@ impl Figure

if let Some(ref output_file) = self.output_file
{
writeln!(w, "set output \"{}\"", output_file.to_str().unwrap());
writeln!(
w,
"set output \"{}\"",
escape(&output_file.to_str().unwrap())
);
}

writeln!(w, "set termoption dashed");
Expand Down Expand Up @@ -551,7 +556,7 @@ impl Figure
let title = m
.title
.as_ref()
.map_or("".to_string(), |t| format!(" title \"{}\"", t));
.map_or("".to_string(), |t| format!(" title \"{}\"", escape(t)));
let scale = m.scale_x.map_or("".to_string(), |s| {
format!(" scale {},{}", s, m.scale_y.unwrap())
});
Expand Down Expand Up @@ -604,8 +609,10 @@ impl Figure
}
}

impl Drop for Figure {
fn drop(&mut self) {
impl Drop for Figure
{
fn drop(&mut self)
{
self.close();
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,19 @@ impl<'l, T: OneWayOwned> OneWayOwned for &'l [T]
self.iter().map(|v| v.to_one_way_owned()).collect()
}
}

pub(crate) fn escape(s: &str) -> String
{
let s = s.replace(r"\", r"\\");
let s = s.replace(r#"""#, r#"\""#);
s
}

#[test]
fn escape_test()
{
assert_eq!(r"\\", escape(r"\"));
assert_eq!(r"\\\\", escape(r"\\"));
assert_eq!(r#"\\\""#, escape(r#"\""#));
assert_eq!(r#"\"\""#, escape(r#""""#));
}