gcalderone / Gnuplot.jl

Julia interface to gnuplot
Other
119 stars 16 forks source link

gpvars() fails with StringIndexError #24

Closed lyon-fnal closed 4 years ago

lyon-fnal commented 4 years ago

When I try an example in the documentation in the Named datasets section, I get a failure in the gpvars() call...

# Generate data and some noise to simulate measurements
x = range(-2pi, stop=2pi, length=20);
y = 1.5 * sin.(0.3 .+ 0.7x);
err = 0.1 * maximum(abs.(y)) .* fill(1, size(x));
y += err .* randn(length(x));
name = "\$MyDataSet1"

@gp    "f(x) = a * sin(b + c*x)"     :- # define an analytical model
@gp :- "a=1" "b=1" "c=1"             :- # set parameter initial values
@gp :- name=>(x, y, err)             :- # define a named dataset
@gp :- "fit f(x) $name via a, b, c;"    # fit the data

vars = gpvars();

The last line generates...

StringIndexError("\"°\"", 3)

Stacktrace:
 [1] string_index_err(::String, ::Int64) at ./strings/string.jl:12
 [2] getindex at ./strings/string.jl:250 [inlined]
 [3] gpvars(::Symbol) at /Users/lyon/.julia/dev/Gnuplot/src/Gnuplot.jl:2257
 [4] gpvars() at /Users/lyon/.julia/dev/Gnuplot/src/Gnuplot.jl:2241
 [5] top-level scope at In[5]:1

I'm running Gnuplot v1.3.0

I tracked down the problem, which is at this line... https://github.com/gcalderone/Gnuplot.jl/blob/2713f8517ed7361fe921d4da364c5ca724a8a841/src/Gnuplot.jl#L2257

Arithmetic with end for a string containing (apparently) multi-byte characters is unsafe. See https://docs.julialang.org/en/v1/manual/strings/#Unicode-and-UTF-8-1 .

The way to fix this is to replace that line with,

                out[key] = s[2][2:prevind(s[2], end, 1)]

I think the 2: is safe because you know the first character in the string is a double quote (a single-byte character).

I'm a little surprised that perhaps no one else has seen this. This happens for me for the Gnuplot variable,

GPVAL_DEGREE_SIGN = \"°\"

I don't think I have anything special in my gnuplot setup. I'm running 5.2.8.

Can you make this one-line fix or do you want me to make a pull request?

Thanks!! -- Adam

gcalderone commented 4 years ago

I don't get that error on my computer (Linux, gnuplot v5.2.8), however I think your solution is more appropriate.

Thank you very much for spotting this out and for providing a PR!

lyon-fnal commented 4 years ago

Must be a Mac thing. Anyway, thanks for merging the PR so quickly!

gcalderone commented 4 years ago

I found the problem, it is related to gnuplot character encoding.

Before your PR:

Your PR fix the first issue, while the second (forcing UTF8 encoding) is now fixed here: https://github.com/gcalderone/Gnuplot.jl/blob/83c59ae682965ad3d7ef54bd9b841806a776363f/src/Gnuplot.jl#L553