Haven't tested it, but should fix half of the spacing tests

This commit is contained in:
lemon-sh 2022-01-24 00:54:28 +01:00
parent 05bff33438
commit ba089c87c9

View file

@ -1,6 +1,6 @@
use std::{env, io, process}; use std::{env, io, process};
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter}; use std::io::{BufRead, BufReader, BufWriter, Lines};
use arrayvec::{ArrayString, ArrayVec}; use arrayvec::{ArrayString, ArrayVec};
use png::{BitDepth, ColorType, EncodingError}; use png::{BitDepth, ColorType, EncodingError};
use thiserror::Error; use thiserror::Error;
@ -69,6 +69,15 @@ fn join_to_arraystring<const C: usize>(elems: &[&str], str: &mut ArrayString<C>)
Ok(()) Ok(())
} }
fn lines_read_noempty<T: BufRead>(lines: &mut Lines<T>) -> Result<Option<String>> {
while let Some(s) = lines.next().transpose()? {
if !s.is_empty() {
return Ok(Some(s))
}
}
Ok(None)
}
fn unfuck_cif_number(number: &str) -> Result<u32> { fn unfuck_cif_number(number: &str) -> Result<u32> {
let mut buf = ArrayString::<2048>::new(); let mut buf = ArrayString::<2048>::new();
let split = number.split_whitespace().collect::<ArrayVec<_, 64>>(); let split = number.split_whitespace().collect::<ArrayVec<_, 64>>();
@ -141,14 +150,14 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
return Err(InvalidSizeError(size_string.to_string())) return Err(InvalidSizeError(size_string.to_string()))
} }
let width = unfuck_cif_number(size_split[0].trim().strip_prefix("szerokość:") let width = unfuck_cif_number(size_split[0].trim_start().strip_prefix("szerokość:")
.ok_or_else(|| InvalidSizeError(size_string.to_string()))?.trim())?; .ok_or_else(|| InvalidSizeError(size_string.to_string()))?.trim_start())?;
let height = unfuck_cif_number(size_split[1].trim().strip_prefix("wysokość:") let height = unfuck_cif_number(size_split[1].trim_start().strip_prefix("wysokość:")
.ok_or_else(|| InvalidSizeError(size_string.to_string()))?.trim())?; .ok_or_else(|| InvalidSizeError(size_string.to_string()))?.trim_start())?;
let format = match unfuck_cif_number_fast( let format = match unfuck_cif_number_fast(
size_split[2].trim().strip_prefix("bitów_na_piksel:") size_split[2].trim_start().strip_prefix("bitów_na_piksel:")
.ok_or_else(|| InvalidSizeError(size_string.to_string()))?.trim() .ok_or_else(|| InvalidSizeError(size_string.to_string()))?.trim_start()
)? { )? {
24 => ColorType::Rgb, 24 => ColorType::Rgb,
32 => ColorType::Rgba, 32 => ColorType::Rgba,
@ -190,7 +199,7 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
return Err(InvalidPixelError(first.to_string())) return Err(InvalidPixelError(first.to_string()))
} }
for px in px_split { for px in px_split {
pixmap.push(unfuck_cif_number_fast(px.trim())?); pixmap.push(unfuck_cif_number_fast(px.trim_start())?);
} }
} }