diff --git a/CHANGELOG.md b/CHANGELOG.md index 29d419a3..ec47a13c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] - MSRV bumped to 1.70 +### Fixed + +- gnuplot version is now correctly detected when using certain Windows binaries/configurations that used to fail + ## [0.5.1] - 2023-05-26 ### Fixed diff --git a/plot/src/lib.rs b/plot/src/lib.rs index 8f31dab6..c4953c2f 100644 --- a/plot/src/lib.rs +++ b/plot/src/lib.rs @@ -995,9 +995,32 @@ pub fn version() -> Result { return Err(VersionError::Error(error)); } - let output = String::from_utf8(command_output.stdout).map_err(|_| VersionError::OutputError)?; + parse_version_utf8(&command_output.stdout).or_else(|utf8_err| { + // gnuplot can emit UTF-16 on some systems/configurations (e.g. some Windows machines). + // If we failed to parse as UTF-8, try again as UTF-16 to account for this. + // If UTF-16 parsing also fails, return the original error we got for UTF-8 to avoid confusing matters more. + parse_version_utf16(&command_output.stdout).map_err(|_| utf8_err) + }) +} + +fn parse_version_utf8(output_bytes: &[u8]) -> Result { + let output = str::from_utf8(output_bytes).map_err(|_| VersionError::OutputError)?; + parse_version(output).map_err(|_| VersionError::ParseError(output.to_owned())) +} + +fn parse_version_utf16(output_bytes: &[u8]) -> Result { + if output_bytes.len() % 2 != 0 { + // Not an even number of bytes, so cannot be UTF-16. + return Err(VersionError::OutputError); + } + + let output_as_u16: Vec = output_bytes + .chunks_exact(2) + .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]])) + .collect(); - parse_version(&output).map_err(|_| VersionError::ParseError(output.clone())) + let output = String::from_utf16(&output_as_u16).map_err(|_| VersionError::OutputError)?; + parse_version(&output).map_err(|_| VersionError::ParseError(output.to_owned())) } fn parse_version(version_str: &str) -> Result> {