From: Ben Pfaff Date: Tue, 25 Feb 2025 04:31:53 +0000 (-0800) Subject: more text_line tests X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4b73cbb7ecd48473c96c5aa3caad1b0d2bf5336;p=pspp more text_line tests --- diff --git a/rust/pspp/src/output/text_line.rs b/rust/pspp/src/output/text_line.rs index 33d9419684..b739656e7e 100644 --- a/rust/pspp/src/output/text_line.rs +++ b/rust/pspp/src/output/text_line.rs @@ -45,7 +45,6 @@ impl TextLine { pub fn put(&mut self, x0: usize, s: &str) { let w = Widths::new(s).sum::(); - dbg!(w); let x1 = x0 + w; if w == 0 { // Nothing to do. @@ -64,6 +63,8 @@ impl TextLine { self.string.push_str(s); self.width = x1; } else { + dbg!(s); + dbg!(w); dbg!(x0); dbg!(x1); let span = self.find_span(x0, x1); @@ -71,9 +72,9 @@ impl TextLine { if span.columns.start < x0 || span.columns.end > x1 { let tail = self.string.split_off(span.offsets.end); self.string.truncate(span.offsets.start); - self.string.extend((span.offsets.start..x0).map(|_| '?')); + self.string.extend((span.columns.start..x0).map(|_| '?')); self.string.push_str(s); - self.string.extend((x1..span.offsets.end).map(|_| '?')); + self.string.extend((x1..span.columns.end).map(|_| '?')); self.string.push_str(&tail); } else { self.string.replace_range(span.offsets, s); @@ -82,18 +83,14 @@ impl TextLine { } fn find_span(&self, x0: usize, x1: usize) -> Position { + debug_assert!(x1 > x0); let p0 = self.find_pos(x0); - let p1 = self.find_pos(x1); - if true || p0.columns.start >= x1 { - Position { - columns: p0.columns.start..p1.columns.start, - offsets: p0.offsets.start..p1.offsets.start, - } - } else { - Position { - columns: p0.columns.start..p1.columns.end, - offsets: p0.offsets.start..p1.offsets.end, - } + let p1 = self.find_pos(x1 - 1); + dbg!(&p0); + dbg!(&p1); + Position { + columns: p0.columns.start..p1.columns.end, + offsets: p0.offsets.start..p1.offsets.end, } } @@ -413,9 +410,11 @@ mod test { for top in all::() { let mut line = TextLine::new(); // Produces `?か??くけ?さ`. + // Produces `?か?うえお`. line.put(0, &bottom.apply("あいうえお")); line.put(1, &top.apply("か")); //line.put(5, &top.apply("くけ")); + /* assert_eq!( line.str(), &format!( @@ -425,6 +424,11 @@ mod test { bottom.apply("さ") ), "bottom={bottom:?} top={top:?}" + );*/ + assert_eq!( + line.str(), + &format!("?{}?{}", top.apply("か"), bottom.apply("うえお"),), + "bottom={bottom:?} top={top:?}" ); } }