EPS import: readhexstring and readline read the data in one pass

readhexstring and readline called the one-byte file read for every input
character, copying each byte into a new vector. They now scan the bytes
left in the file in one slice and advance the file by the bytes they used.

The results are unchanged: the same bytes, the same boolean, the same next
unread byte, from the program's own file (after the whitespace that ends
the last token), a decoded buffer and a filter decoded when first read.
readhexstring stops on the digit that completes the last byte and drops a
lone digit at the end of the data; readline takes CR LF as one line ending,
leaves the byte after a lone CR unread, and leaves the line ending unread
when the line fills the string.

Tests run the new readers next to copies of the byte-at-a-time loops over
13 stream forms and several hundred inputs, comparing the result and
everything left to read; an image read through a readhexstring procedure
is compared with the same samples given as a hexadecimal string. The
import fuzz tests gain readline and a case for both operators.

data_op allows clippy::cognitive_complexity. Without the allow, clippy
rates it 54 against the limit of 30; with the old loops in place it
passes.
This commit is contained in:
dotCooCoo
2026-11-16 03:39:00 -08:00
parent ed6f22a84c
commit 160207e9f2
6 changed files with 466 additions and 43 deletions
+27 -1
View File
@@ -2776,6 +2776,32 @@ proptest! {
paste_from_elsewhere("clipboard.importEps", json!({"dataBase64": vectorcraft_format::base64_encode(ps.as_bytes())}))?;
}
/// Any bytes read by `readhexstring` or `readline` into strings of any length, from the
/// program's own file, through a filter decoded when first read, or from a file being run:
/// read as data, or as an error.
#[test]
fn eps_readhexstring_and_readline_data_never_panic(
data in prop::collection::vec(prop::sample::select(vec![b'0', b'9', b'a', b'F', b'g', b' ', b'\t', b'\r', b'\n', b'>', b'%', b'(', 0u8, 0xff]), 0..300),
len in 0usize..40,
line in any::<bool>(),
source in 0usize..3,
) {
let op = if line { "readline" } else { "readhexstring" };
let file = if source == 1 { "currentfile /ASCIIHexDecode filter" } else { "currentfile" };
let reads = format!("/s {len} string def /f {file} def 30 {{ f s {op} pop pop }} repeat");
let mut bytes = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n".to_vec();
if source == 2 {
bytes.extend_from_slice(format!("currentfile 0 (endstream) /SubFileDecode filter cvx exec\n{reads}\n").as_bytes());
bytes.extend_from_slice(&data);
bytes.extend_from_slice(b"\nendstream\nshowpage\n");
} else {
bytes.extend_from_slice(format!("{reads}\n").as_bytes());
bytes.extend_from_slice(&data);
bytes.extend_from_slice(b"\nshowpage\n");
}
survive("readhexstring and readline data", || vectorcraft_eps::import(&bytes).ok().map(|r| r.document))?;
}
/// Damaged or hostile CMYK images (a CMYK JPEG, an image with a key color): read as images,
/// or as an error.
#[test]
@@ -3004,7 +3030,7 @@ fn arb_ps_token() -> impl Strategy<Value = String> {
"gsave", "grestore", "save", "restore", "concat", "translate", "scale", "rotate", "setlinewidth", "setdash", "setgray",
"setrgbcolor", "setcmykcolor", "sethsbcolor", "setcolorspace", "setcolor", "setpattern", "makepattern", "shfill", "arc", "arcn",
"arct", "arcto", "rectfill", "rectclip", "rectstroke", "image", "imagemask", "colorimage", "currentfile", "filter",
"readhexstring", "readstring", "show", "stringwidth", "charpath", "findfont", "scalefont", "makefont", "setfont", "selectfont",
"readhexstring", "readstring", "readline", "show", "stringwidth", "charpath", "findfont", "scalefont", "makefont", "setfont", "selectfont",
"def", "bind", "load", "dup", "exch", "pop", "roll", "index", "copy", "for", "repeat", "loop", "exit", "if", "ifelse", "exec",
"stopped", "begin", "end", "dict", "array", "string", "get", "put", "getinterval", "putinterval", "aload", "astore", "forall",
"[", "]", "<<", ">>", "{", "}", "matrix", "currentmatrix", "setmatrix", "transform", "itransform", "invertmatrix", "pathbbox",
+2
View File
@@ -68,6 +68,8 @@ mod tests_images;
#[cfg(test)]
mod tests_native;
#[cfg(test)]
mod tests_reads;
#[cfg(test)]
mod tests_type1;
#[cfg(test)]
mod tests_type42;
+60 -42
View File
@@ -31,6 +31,7 @@ pub(super) enum Source {
}
impl Interp<'_> {
#[allow(clippy::cognitive_complexity)] // One arm for each file operator, most of them a few lines.
pub fn data_op(&mut self, op: Op) -> Res {
use Op::*;
match op {
@@ -50,23 +51,7 @@ impl Interp<'_> {
ReadLine => {
let s = self.pop_str()?;
let Obj::File { stream: f, .. } = self.pop()? else { return ps_err("typecheck", "") };
let n = s.len();
let mut line = vec![];
let mut ended = false;
while line.len() < n {
match self.read(&f, 1)?.first() {
Some(b'\n') | None => {
ended = true;
break;
}
Some(b'\r') => {
self.skip_lf(&f);
ended = true;
break;
}
Some(b) => line.push(*b),
}
}
let (line, ended) = self.read_line(&f, s.len())?;
self.fill(s, &line)?;
self.push(Obj::Bool(ended))?;
}
@@ -219,38 +204,71 @@ impl Interp<'_> {
})
}
/// After `readline` consumes CR, consume only an immediately following LF. PostScript
/// treats CR/LF as one line ending (PLRM §3.8); raw reads must keep every other byte.
/// The preceding `read` has already materialized any pending filter.
fn skip_lf(&mut self, f: &Rc<RefCell<Stream>>) {
match &mut *f.borrow_mut() {
/// Run `scan` over the bytes left in file `f`, in one slice, and consume as many of them as
/// it says it used. A file still pending after `materialize` has no bytes left.
fn scan<T>(&mut self, f: &Rc<RefCell<Stream>>, scan: impl FnOnce(&[u8]) -> (T, usize)) -> Res<T> {
self.materialize(f)?;
Ok(match &mut *f.borrow_mut() {
Stream::Current => {
if self.lex.rest().first() == Some(&b'\n') {
self.lex.advance(1);
}
let (found, used) = scan(self.lex.data());
self.lex.advance(used);
found
}
Stream::Buf { data, pos, .. } => {
if data.get(*pos) == Some(&b'\n') {
*pos += 1;
}
let start = (*pos).min(data.len());
let (found, used) = scan(data.get(start..).unwrap_or_default());
*pos = start.saturating_add(used).min(data.len());
found
}
Stream::Pending { .. } => {}
}
Stream::Pending { .. } => scan(&[]).0,
})
}
/// Up to `n` bytes read as hexadecimal digits from `f` (other characters skipped).
pub(in super::super) fn read_hex(&mut self, f: &Rc<RefCell<Stream>>, n: usize) -> Res<Vec<u8>> {
let mut out = Vec::with_capacity(n);
let mut high: Option<u8> = None;
while out.len() < n {
let Some(&b) = self.read(f, 1)?.first() else { break };
let Some(v) = char::from(b).to_digit(16) else { continue };
match high.take() {
Some(h) => out.push(h << 4 | v as u8),
None => high = Some(v as u8),
}
/// `readline` from `f` into a string of `n` bytes: the line, and whether it ended at a line
/// ending or at the end of the data. The line ending is consumed and left out of the line.
/// CR LF is one line ending (PLRM §3.8), and a lone CR leaves the byte after it unread. A
/// line that fills the string leaves the bytes after it unread, a line ending included.
pub(in super::super) fn read_line(&mut self, f: &Rc<RefCell<Stream>>, n: usize) -> Res<(Vec<u8>, bool)> {
if n == 0 {
return Ok((vec![], false));
}
Ok(out)
self.scan(f, |data| {
let room = data.get(..n).unwrap_or(data);
match room.iter().position(|b| matches!(b, b'\n' | b'\r')) {
Some(i) => {
let crlf = room.get(i) == Some(&b'\r') && data.get(i + 1) == Some(&b'\n');
((room.get(..i).unwrap_or_default().to_vec(), true), i + 1 + usize::from(crlf))
}
None if room.len() == n => ((room.to_vec(), false), n),
None => ((room.to_vec(), true), room.len()),
}
})
}
/// Up to `n` bytes read as hexadecimal digits from `f` (other characters skipped). The read
/// stops at the digit that completes the `n`th byte and leaves what follows unread. A lone
/// digit at the end of the data gives no byte.
pub(in super::super) fn read_hex(&mut self, f: &Rc<RefCell<Stream>>, n: usize) -> Res<Vec<u8>> {
if n == 0 {
return Ok(vec![]);
}
self.scan(f, |data| {
let mut out = Vec::with_capacity(n.min(data.len() / 2));
let mut high: Option<u8> = None;
let mut used = 0;
for &b in data {
if out.len() == n {
break;
}
used += 1;
let Some(v) = char::from(b).to_digit(16) else { continue };
match high.take() {
Some(h) => out.push(h << 4 | v as u8),
None => high = Some(v as u8),
}
}
(out, used)
})
}
/// Skip the whitespace and the `>` that end hexadecimal data in `f`, if a `>` is next: the
+17
View File
@@ -106,6 +106,23 @@ fn image_data_comes_from_the_file_being_run() {
);
}
/// An image whose procedure reads its rows with `readhexstring` comes out with the same pixels as
/// the same samples given as one hexadecimal string, with the digits broken by CR LF, LF, tabs,
/// spaces and characters that are not digits, read from the program's own file and from a file
/// being run.
#[test]
fn hex_rows_read_by_a_procedure_match_a_hexadecimal_string() {
let rgb: Vec<u8> = (0..36u32).map(|i| (i * 37 % 256) as u8).collect();
let digits = hex(&rgb);
let breaks = ["\r\n", "\n", "\t", " z", "%"];
let broken: String = digits.as_bytes().chunks(5).zip(breaks.iter().cycle()).map(|(d, b)| format!("{}{b}", String::from_utf8_lossy(d))).collect();
let want = pixels(&read(&format!("10 10 scale 4 3 8 [4 0 0 -3 0 3] <{digits}> false 3 colorimage")), 4, 3);
assert_eq!(want, rgb.chunks(3).map(|p| [p[0], p[1], p[2], 255]).collect::<Vec<_>>());
let image = "10 10 scale 4 3 8 [4 0 0 -3 0 3] {currentfile 12 string readhexstring pop} false 3 colorimage";
assert_eq!(pixels(&read(&format!("{image}\r\n{broken}\n")), 4, 3), want);
assert_eq!(pixels(&read(&format!("currentfile 0 (endstream) /SubFileDecode filter cvx exec\n{image}\n{broken}\nendstream\n")), 4, 3), want);
}
/// A filter waits until it is first read, then reads the file it was made over, wherever the
/// program runs by then: one over the program's own file, read in a file being run, and one over
/// a file being run, read in a file that one runs.
+359
View File
@@ -0,0 +1,359 @@
//! `readhexstring` and `readline` (PLRM 3rd ed., §8.2) read the data left in a file in one pass.
//! The tests run the one-pass readers next to reference readers that take one byte per call, on the
//! same data in the same interpreter state: the program's own file after the whitespace that ends
//! the last token, a decoded buffer read from its start or part way in, and a filter decoded when it
//! is first read. The bytes, the result or error, and every byte left unread must agree.
use std::cell::RefCell;
use std::fmt::Debug;
use std::rc::Rc;
use std::time::{Duration, Instant};
use vectorcraft_doc::Document;
use vectorcraft_geom::{Affine, Rect};
use super::data::Stream;
use super::graphics::{GState, Out};
use super::interp::Interp;
use super::obj::{Obj, Res};
type File = Rc<RefCell<Stream>>;
/// Bytes as hexadecimal digits.
fn hex(data: &[u8]) -> String {
data.iter().map(|b| format!("{b:02x}")).collect()
}
/// An interpreter that has run `src` up to its `quit`, and the file object the program left on the
/// operand stack. What follows `quit` in `src` is still unread.
fn file_after(src: &[u8]) -> (Interp<'_>, File) {
let mut doc = Document::new(100.0, 100.0);
doc.layers.clear();
let page = Affine::new([1.0, 0.0, 0.0, -1.0, 0.0, 100.0]);
let mut it = Interp::new(src, GState::default(), Out::new(doc, page, Rect::new(0.0, 0.0, 100.0, 100.0)));
assert_eq!(it.run(), Ok(()), "{}", String::from_utf8_lossy(src));
let Ok(Obj::File { stream, .. }) = it.pop() else { panic!("no file on the stack: {}", String::from_utf8_lossy(src)) };
(it, stream)
}
/// The reference `readline`: one byte per call, a CR followed by an LF ending the line once.
fn line_by_bytes(it: &mut Interp, f: &File, n: usize) -> Res<(Vec<u8>, bool)> {
let mut line = vec![];
let mut ended = false;
while line.len() < n {
match it.read(f, 1)?.first() {
Some(b'\n') | None => {
ended = true;
break;
}
Some(b'\r') => {
match &mut *f.borrow_mut() {
Stream::Current => {
if it.lex.rest().first() == Some(&b'\n') {
it.lex.advance(1);
}
}
Stream::Buf { data, pos, .. } => {
if data.get(*pos) == Some(&b'\n') {
*pos += 1;
}
}
Stream::Pending { .. } => {}
}
ended = true;
break;
}
Some(b) => line.push(*b),
}
}
Ok((line, ended))
}
/// The reference `readhexstring`: one byte per call, characters that are not hexadecimal digits
/// skipped.
fn hex_by_bytes(it: &mut Interp, f: &File, n: usize) -> Res<Vec<u8>> {
let mut out = Vec::with_capacity(n);
let mut high: Option<u8> = None;
while out.len() < n {
let Some(&b) = it.read(f, 1)?.first() else { break };
let Some(v) = char::from(b).to_digit(16) else { continue };
match high.take() {
Some(h) => out.push(h << 4 | v as u8),
None => high = Some(v as u8),
}
}
Ok(out)
}
fn line_one_pass(it: &mut Interp, f: &File, n: usize) -> Res<(Vec<u8>, bool)> {
it.read_line(f, n)
}
fn hex_one_pass(it: &mut Interp, f: &File, n: usize) -> Res<Vec<u8>> {
it.read_hex(f, n)
}
/// Hexadecimal data as CID `StartData` reads it: the digits, then a closing `>` if one is next.
fn hex_and_end_one_pass(it: &mut Interp, f: &File, n: usize) -> Res<Vec<u8>> {
let d = it.read_hex(f, n)?;
it.skip_hex_end(f)?;
Ok(d)
}
fn hex_and_end_by_bytes(it: &mut Interp, f: &File, n: usize) -> Res<Vec<u8>> {
let d = hex_by_bytes(it, f, n)?;
it.skip_hex_end(f)?;
Ok(d)
}
/// How the program reaches the data.
#[derive(Clone, Copy, Debug)]
enum Form {
/// `currentfile`, with this whitespace after the token before the data.
Current(&'static [u8]),
/// A buffer decoded at once, with this many bytes of it read before the data.
Buf(usize),
/// An `ASCIIHexDecode` filter over `currentfile`, decoded when it is first read, with this
/// whitespace after the token before the digits.
Pending(&'static [u8]),
}
const FORMS: [Form; 13] = [
Form::Current(b" "),
Form::Current(b"\n"),
Form::Current(b"\r"),
Form::Current(b"\r\n"),
Form::Current(b"\r\r\n"),
Form::Current(b"\n\r"),
Form::Current(b"\t\t"),
Form::Buf(0),
Form::Buf(2),
Form::Buf(3),
Form::Pending(b"\n"),
Form::Pending(b"\r\n"),
Form::Pending(b" "),
];
/// A program that leaves a file over `data` on the operand stack, in form `form`.
fn program(form: Form, data: &[u8]) -> Vec<u8> {
match form {
Form::Current(lead) => [b"currentfile quit".as_slice(), lead, data].concat(),
Form::Buf(0) => format!("<{}> /ReusableStreamDecode filter quit", hex(data)).into_bytes(),
Form::Buf(skip) => {
let before = b"\r\n\r".get(..skip).unwrap_or(b"\r\n\r".as_slice());
format!("<{}{}> /ReusableStreamDecode filter dup {skip} string readstring pop pop quit", hex(before), hex(data)).into_bytes()
}
Form::Pending(lead) => [b"currentfile /ASCIIHexDecode filter quit".as_slice(), lead, hex(data).as_bytes(), b"> tail"].concat(),
}
}
type Reader<T> = fn(&mut Interp, &File, usize) -> Res<T>;
/// Read `data` in `form` with `one_pass` and with `by_bytes` in two interpreters in the same state,
/// and check that they agree on the result and on everything left to read.
fn agree<T: PartialEq + Debug>(form: Form, data: &[u8], n: usize, one_pass: Reader<T>, by_bytes: Reader<T>) {
let src = program(form, data);
let (mut a, fa) = file_after(&src);
let (mut b, fb) = file_after(&src);
let at = || format!("{form:?}, {:?}, n = {n}", String::from_utf8_lossy(data));
assert_eq!(one_pass(&mut a, &fa, n), by_bytes(&mut b, &fb, n), "result: {}", at());
assert_eq!(a.lex.rest(), b.lex.rest(), "program left unread: {}", at());
assert_eq!(a.read(&fa, usize::MAX), b.read(&fb, usize::MAX), "file left unread: {}", at());
assert_eq!(a.lex.rest(), b.lex.rest(), "program left after the file: {}", at());
a.release();
b.release();
}
/// A fixed sequence of pseudo-random numbers (xorshift), so a failure repeats.
struct Rng(u64);
impl Rng {
fn below(&mut self, n: usize) -> usize {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
(self.0 % n.max(1) as u64) as usize
}
}
/// `chosen`, then `count` strings of up to 24 bytes from `alphabet`.
fn samples(chosen: &[&[u8]], alphabet: &[u8], count: usize, rng: &mut Rng) -> Vec<Vec<u8>> {
let random = (0..count).map(|_| {
let len = rng.below(25);
(0..len).filter_map(|_| alphabet.get(rng.below(alphabet.len())).copied()).collect()
});
chosen.iter().map(|s| s.to_vec()).chain(random).collect()
}
/// String lengths to read `data` with: none, one, past its end, exactly its length and one more,
/// and one at random.
fn lengths(data: &[u8], rng: &mut Rng) -> [usize; 6] {
[0, 1, 3, data.len(), data.len() + 1, rng.below(data.len() + 3)]
}
#[test]
fn readline_reads_in_one_pass_as_byte_by_byte() {
let mut rng = Rng(0x9e37_79b9_7f4a_7c15);
let chosen: [&[u8]; 16] = [
b"",
b"\n",
b"\r",
b"\r\n",
b"\n\r",
b"\r\r\n",
b"abc",
b"abc\n",
b"abc\r",
b"abc\rX",
b"abc\r\nX",
b"abc\n\nX",
b"abcdefgh\r\nX",
b"abcdefgh\nX",
b"a\0b\xff\rX",
b"abc\r\n",
];
for data in samples(&chosen, b"ab\r\n\r\n x\0\xff", 40, &mut rng) {
for form in FORMS {
for n in lengths(&data, &mut rng) {
agree(form, &data, n, line_one_pass, line_by_bytes);
}
}
}
}
#[test]
fn readhexstring_reads_in_one_pass_as_byte_by_byte() {
let mut rng = Rng(0x2545_f491_4f6c_dd1d);
let chosen: [&[u8]; 14] = [
b"",
b"0",
b"a",
b"0a",
b"0a1",
b"0 a\r\n1b>X",
b"zz0g1",
b"ff\r\n00\nAA",
b"0a1b2c3d",
b"0a1b\r\n2c>",
b"\r\n0a",
b"a b c d e",
b"0a>",
b"0a \r\n >X",
];
for data in samples(&chosen, b"0123456789abcdefABCDEFg \r\n\t>z%", 40, &mut rng) {
for form in FORMS {
for n in lengths(&data, &mut rng) {
agree(form, &data, n, hex_one_pass, hex_by_bytes);
agree(form, &data, n, hex_and_end_one_pass, hex_and_end_by_bytes);
}
}
}
}
/// `readline` of `data` into `n` bytes, after `currentfile` and a CR LF, gives `line` and `ended`
/// and leaves `left` unread.
fn line_case(data: &[u8], n: usize, line: &[u8], ended: bool, left: &[u8]) {
let src = program(Form::Current(b"\r\n"), data);
let (mut it, f) = file_after(&src);
assert_eq!(it.read_line(&f, n), Ok((line.to_vec(), ended)), "{:?}", String::from_utf8_lossy(data));
assert_eq!(it.read(&f, usize::MAX), Ok(left.to_vec()), "{:?}", String::from_utf8_lossy(data));
it.release();
}
/// `readhexstring` of `data` into `n` bytes, after `currentfile` and a CR LF, gives `bytes` and
/// leaves `left` unread in the program.
fn hex_case(data: &[u8], n: usize, bytes: &[u8], left: &[u8]) {
let src = program(Form::Current(b"\r\n"), data);
let (mut it, f) = file_after(&src);
assert_eq!(it.read_hex(&f, n), Ok(bytes.to_vec()), "{:?}", String::from_utf8_lossy(data));
assert_eq!(it.lex.rest(), left, "{:?}", String::from_utf8_lossy(data));
it.release();
}
/// The results the one-pass readers give at the edges of the data, stated directly.
#[test]
fn one_pass_reads_stop_where_their_data_ends() {
// A CR LF ends one line; a lone CR leaves the next byte unread.
line_case(b"ab\r\nX", 8, b"ab", true, b"X");
line_case(b"ab\rX", 8, b"ab", true, b"X");
line_case(b"ab\nX", 8, b"ab", true, b"X");
// A line that fills the string leaves its line ending unread.
line_case(b"abcd\nX", 4, b"abcd", false, b"\nX");
line_case(b"abc\r\nX", 3, b"abc", false, b"\r\nX");
// The end of the data ends the line.
line_case(b"ab", 8, b"ab", true, b"");
line_case(b"", 8, b"", true, b"");
// The read stops on the digit that completes the last byte.
hex_case(b"0a\r\n1b>X", 2, &[0x0a, 0x1b], b">X");
// Characters that are not digits are skipped.
hex_case(b"z0 g\ta%", 1, &[0x0a], b"%");
// A lone digit at the end of the data gives no byte.
hex_case(b"0a1", 2, &[0x0a], b"");
hex_case(b"\r\n", 2, &[], b"");
// No byte to read takes nothing, not even the whitespace after the token.
hex_case(b"0a", 0, &[], b"\r\n0a");
}
/// The median of `runs`.
fn median(runs: &[Duration]) -> Duration {
let mut sorted = runs.to_vec();
sorted.sort();
sorted.get(sorted.len() / 2).copied().unwrap_or_default()
}
/// Read all of `src`'s data with `reader` in strings of `n` bytes, `calls` times, and how long it
/// took. The interpreter is made before the clock starts.
fn timed<T>(src: &[u8], reader: Reader<T>, n: usize, calls: usize) -> (Vec<T>, Duration) {
let (mut it, f) = file_after(src);
let start = Instant::now();
let got: Vec<T> = (0..calls).map(|_| reader(&mut it, &f, n).unwrap_or_else(|e| panic!("{e:?}"))).collect();
let took = start.elapsed();
it.release();
(got, took)
}
/// Time both readers on `src` in 7 runs, alternating which goes first, check that they read the
/// same, and print every run and the medians.
fn compare<T: PartialEq>(what: &str, src: &[u8], one_pass: Reader<T>, by_bytes: Reader<T>, n: usize, calls: usize) {
let (mut old, mut new) = (vec![], vec![]);
for run in 0..7 {
let order: [bool; 2] = if run % 2 == 0 { [false, true] } else { [true, false] };
let mut got = vec![];
for one in order {
let (read, took) = timed(src, if one { one_pass } else { by_bytes }, n, calls);
if one {
new.push(took)
} else {
old.push(took)
}
got.push(read);
}
assert!(got.first() == got.get(1), "{what}: the readers differ");
}
let ms = |d: &Duration| format!("{:.1}", d.as_secs_f64() * 1000.0);
println!("{what}: {} bytes of data, {calls} calls of {n} bytes", src.len());
println!(" byte by byte, ms: {}", old.iter().map(ms).collect::<Vec<_>>().join(" "));
println!(" one pass, ms: {}", new.iter().map(ms).collect::<Vec<_>>().join(" "));
let (o, w) = (median(&old), median(&new));
println!(" median: byte by byte {} ms, one pass {} ms, {:.1}x", ms(&o), ms(&w), o.as_secs_f64() / w.as_secs_f64().max(1e-9));
}
/// Times the one-pass readers against the byte-at-a-time reference readers on the same data in one
/// process: the hexadecimal samples of a 1000 × 1000 RGB image in lines of 64 digits, read a row
/// of 3,000 bytes at a time, and 200,000 lines of 40 bytes read by `readline` into an 80-byte
/// string. Run it in release:
/// `cargo test --release -p vectorcraft-eps --lib tests_reads::one_pass_reads_timed -- --ignored --nocapture`.
#[test]
#[ignore = "manual timing probe; wall-clock performance depends on machine load"]
fn one_pass_reads_timed() {
let rgb: Vec<u8> = (0..3_000_000u32).map(|i| (i.wrapping_mul(2_654_435_761) >> 24) as u8).collect();
let digits = hex(&rgb);
let mut data = Vec::with_capacity(digits.len() + digits.len() / 64 + 1);
for line in digits.as_bytes().chunks(64) {
data.extend_from_slice(line);
data.push(b'\n');
}
compare("readhexstring", &program(Form::Current(b"\n"), &data), hex_one_pass, hex_by_bytes, 3_000, 1_000);
let lines: Vec<u8> = (0..200_000).flat_map(|i| format!("{:<39}\n", format!("line {i}")).into_bytes()).collect();
compare("readline", &program(Form::Current(b"\n"), &lines), line_one_pass, line_by_bytes, 80, 200_000);
}
+1
View File
@@ -2040,6 +2040,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
- A copied `.ai` or `.ait` file that holds a PDF pastes the objects of its editing data as File › Open reads them: every layer's objects go into the active layer in their stacking order, with their names, hidden objects and type objects, instead of the PDF page's art with its type as outlines. When the editing data can't be read, the file pastes its page as before (`clipboard.importPdf {editingData}`). The objects of a hidden layer come in hidden, from a pasted EPS too.
- Import and export, later 2026-10-13 merges: SVGs that move objects with a CSS transform (`translate(100px, 0)`, `rotate(45deg)`) or style type with the `font` shorthand import as drawn (#1565); SVG symbols whose shapes use percentage lengths import at the size and place they are drawn, the percentages being of the symbol's `viewBox` (#1596); Inkscape's flowed text (`flowRoot`) opens as area type in its rectangle, a paragraph per `flowPara`, instead of being dropped, and a region that can't be read is left out with a warning (#1569, fixes #1568); the warning for an unsupported SVG filter names the filter, its primitives and the object, and says the object is drawn without it (#1599, fixes #1598); the pages of an Inkscape multi-page SVG become artboards, named by their labels (#1603, fixes #1601); SVG export with CSS classes or style entities stays fast in documents with many distinct styles (#1567); PDF and `.ai` import: evenly letter-spaced or tightened type on a path keeps its length along the path instead of closing up to the font's advances (#1586), and type on a path whose stroke is written glyph by glyph comes in as one text object with its stroke, not one object per glyph (#1585); the ICC profiles embedded in JPEG, TIFF and PSD exports have every tag on a 4-byte boundary (ICC.1 §7.1.2), so strict readers accept them (#1589, fixes #1584); GIF, PNG-8 and other indexed exports find each pixel's palette colour through an index instead of a scan, with identical output (#1576); a zero-length subpath draws its round cap as a dot and its projecting cap as a square, so dots in imported `.ai` and SVG files show (#1619, fixes #1613).
- Files and untrusted input, 2026-10-14 merges: EPS import: a stitched shading keeps its hard colour changes (#1654, #1653). WMF/EMF import refuses an oversized embedded PNG or JPEG from its header, before any pixels are decoded (#1776). PDF open no longer takes exponential time on a crafted name tree with shared nodes when it looks for the editing data (#1781). The size limits on a native file's embedded data also apply to files read from memory, not only streamed ones (#1783). Saving over a private file on Unix no longer leaves a readable temporary copy beside it while the file is written (#1778). An Export for Screens suffix can no longer place files outside the export folder (#1777). Package refuses a folder where it would overwrite the open document or one of its linked files (#1786). ruzstd 0.9.1 fixes a decompression bomb (RUSTSEC-2026-0344) reachable from EPS/`.ai` and Affinity import (#1807). File Info dates are stamped only after a save succeeds, so a failed save no longer changes them (#1774). Linked files that change during a drag are updated after the drag instead of being missed (#1773).
- EPS import: `readhexstring` and `readline` read the data left in a file in one pass instead of one byte per call, with the same bytes, result and next unread byte as before (about nine times less reader time on 6 MB of hex image data and on 8 MB of lines).
- **Fields, search and window, 2026-10-14 merges:** Fields commit what was typed when you click away or move to another field, as Enter does; Escape still discards it (#1787). Up and Down move through the command search results and Enter runs the highlighted one (#1798, #1757). Slider rails show on Medium Dark (#1808, #1662). On Windows the custom title bar's window stays truly borderless, so the pointer lines up with what is drawn on systems where the hidden caption offset it (#1791, #1758; awaiting the reporter's confirmation).
- **Tools, panels and type, later 2026-10-14 merges:** the Transform panel's, Properties panel's and Control bar's X, Y, W and H show and edit the bounds of the anchor points selected with Direct Selection, and a whole-object selection is unchanged (#1839, fixes #1824). With Snap to Grid on, anchors and objects snap to the grid even when Snap to Pixel is also ticked: the grid wins (#1840; #1816 stays open until the reporter confirms). A marquee with the Selection or Direct Selection tool also selects the unlocked ruler guides it crosses (#1836). The toolbar shows no group labels by default (a preference brings them back) and runs on in one or two columns; the All Tools drawer lists the tools by flyout group with their shortcuts, has a menu (Reset, Basic, Advanced) and a Show section for the Fill/Stroke, Coloring, Draw Mode and Screen Mode controls (`toolbar.show`, #1751). An action clicked in the Actions panel stays selected, so Play Current Selection and Delete Selection act on it (#1853, part of #1629; editing actions and action shortcuts remain). Redo pressed during a drag keeps the drag as a step instead of losing the redone edit (#1856, #1719). Preferences are written as soon as OK is pressed, so they survive a session that doesn't end with a normal quit (#1857; #1747 stays open until the reporter confirms). New and Redefine Character Style take the format of the selected text range, also while typing (#1634). Type inside symbols, pattern tiles, graph designs and opacity masks gets its layout bounds back when a native file opens, so warped type in a symbol looks the same after save and reopen (#1833, #1738). Editing, renaming, deleting or merging a global swatch reaches the art of opacity masks, and a swatch used only in a mask is no longer listed as unused (#1830, #1735). On macOS, Save As, Save a Copy and the first Save show their sheet again; the Format menu on the sheet (#866) is withdrawn until the format can be set before the sheet opens, and #866 is open again (#1855; #1800, #1831 and #1518 stay open until confirmed on a Mac).
- **Files and untrusted input, later 2026-10-14 merges:** SVG import applies style sheet rules with `:not()` selectors, with their nesting bounded (#1799, #1557), no longer stalls on a style sheet with many stacked descendant selectors over deep nesting (#1834, #1703), and reads an SVG's linked files within the automation roots in force again (#1870). DXF export writes the VPORT record's view direction once (group code 36 was written twice, which strict readers reject, #1789). A deeply nested file no longer overflows the stack: nesting is capped at 32 objects (deeper groups are merged with a warning; SVG past 256 elements and PDF past 256 nested arrays are refused), symbols and patterns that use themselves open without the offending instance or paint, and a document nested more than about 40 deep can no longer be saved to a file that won't open again (#1852). PDF and `.ai` files nested deeply inside compressed streams, page trees holding themselves and functions or colour spaces built on themselves no longer crash the app (hayro 0.8; the import runs on a 16 MB thread, #1871); a decompression bomb or impossible predictor parameters are refused (#1873, #1700, #1701); a brush whose art draws the brush again through a symbol or pattern no longer crashes the app, and graph designs, Affinity, DXF, metafiles and plug-ins were probed with hostile input (#1872). Hostile EPS, Affinity and native files can no longer run memory or CPU away through ASCII85 data, PostScript objects holding themselves, flat-colour pixel layers or far and overlapping data offsets (#1863: #1715, #1714, #1704, #1699), and the save fallback for folders that refuse a rename keeps the old file's content until the new one is in (#1695). A file whose ids sit at their largest value no longer breaks new objects (#1718); a failed `command.batch` puts each document back whole, its file, history and a drag in progress included (#1697); a control request that timed out before the app took it never runs later, and the reply says which case it was (#1725, [control-protocol.md](control-protocol.md)); the Eyedropper and recolouring refuse an image too large as RGBA before decoding it (#1717) (all #1862). A save or export that fails is reported in a dialog that names the file and stays until closed, with the document left modified and the old file untouched (#1685); a browser download that can't start fails the save instead of marking it saved (#1693); a file that finishes opening waits while a dialog is open, so Save As options can't save the wrong document (#1694) (all #1865). krilla is built without its text shaping, so the unmaintained rustybuzz and ttf-parser don't ship (#1874, #1730); the CI gate compiles the web app for wasm, and the web's SVG nesting cap (34) follows from its 1 MiB stack (#1875).