Skip to content

Commit

Permalink
Escape more things.
Browse files Browse the repository at this point in the history
Fixes #66.
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Jun 7, 2020
1 parent 8eec723 commit bcdcd6e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
5 changes: 5 additions & 0 deletions gnuplot/Cargo.toml
Expand Up @@ -115,5 +115,10 @@ path = "examples/multiplot_options.rs"
name = "multiple_axes"
path = "examples/multiple_axes.rs"

[[example]]

name = "text"
path = "examples/text.rs"

[dependencies]
byteorder = "1.3.1"
65 changes: 65 additions & 0 deletions gnuplot/examples/text.rs
@@ -0,0 +1,65 @@
// This file is released into Public Domain.
use crate::common::*;
use gnuplot::*;

mod common;

fn example(c: Common)
{
let mut fg = Figure::new();
let _ax = fg
.axes2d()
.label(
"multi\nline string",
Coordinate::Graph(0.5),
Coordinate::Graph(0.9),
&[],
)
.label(
"x^2 x_2 {/Times*2 abc} \\{\\}\\^\\_",
Coordinate::Graph(0.5),
Coordinate::Graph(0.8),
&[],
)
.label(
"Monospace",
Coordinate::Graph(0.5),
Coordinate::Graph(0.6),
&[Font("Monospace", 32.)],
)
.label(
"Align Right",
Coordinate::Graph(0.5),
Coordinate::Graph(0.5),
&[TextAlign(AlignRight)],
)
.label(
"Align Centre",
Coordinate::Graph(0.5),
Coordinate::Graph(0.4),
&[TextAlign(AlignCenter)],
)
.label(
"~{Over}{Print}", // Why does gnuplot have this feature?
Coordinate::Graph(0.5),
Coordinate::Graph(0.3),
&[TextAlign(AlignCenter)],
)
.label(
"Tab\tCharacter", // Strange rendering on this one
Coordinate::Graph(0.5),
Coordinate::Graph(0.2),
&[TextAlign(AlignCenter)],
)
.lines(&[-2., -2.], &[-3., 3.], &[])
.set_x_ticks(None, &[], &[])
.set_y_ticks(None, &[], &[])
.set_border(true, &[], &[]);

c.show(&mut fg, "text");
}

fn main()
{
Common::new().map(|c| example(c));
}
18 changes: 15 additions & 3 deletions gnuplot/src/util.rs
Expand Up @@ -65,9 +65,20 @@ impl<'l, T: OneWayOwned> OneWayOwned for &'l [T]

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

for c in s.chars()
{
match c
{
'\\' => res.push_str(r"\\"),
'\n' => res.push_str(r"\n"),
'\t' => res.push_str(r"\t"),
'"' => res.push_str(r#"\""#),
c @ _ => res.push(c),
}
}
res
}

#[test]
Expand All @@ -77,4 +88,5 @@ fn escape_test()
assert_eq!(r"\\\\", escape(r"\\"));
assert_eq!(r#"\\\""#, escape(r#"\""#));
assert_eq!(r#"\"\""#, escape(r#""""#));
assert_eq!(r"\n", escape("\n"));
}

0 comments on commit bcdcd6e

Please sign in to comment.