more text_line tests
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 25 Feb 2025 04:31:53 +0000 (20:31 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 25 Feb 2025 04:31:53 +0000 (20:31 -0800)
rust/pspp/src/output/text_line.rs

index 33d9419684a26b4a538ab9a80d6e1d384f930b1a..b739656e7ef6ccdcd520d6bf0b810692f9772300 100644 (file)
@@ -45,7 +45,6 @@ impl TextLine {
 
     pub fn put(&mut self, x0: usize, s: &str) {
         let w = Widths::new(s).sum::<usize>();
-        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::<Emphasis>() {
                 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:?}"
                 );
             }
         }