Skip to content

Commit

Permalink
Use display trait for gap buffer string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacdonald committed Feb 10, 2024
1 parent 8130c32 commit 635c2bd
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/buffer/gap_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::Position;
use super::Range;
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt;
use unicode_segmentation::UnicodeSegmentation;

/// A UTF-8 string buffer designed to minimize reallocations,
Expand Down Expand Up @@ -128,21 +129,6 @@ impl GapBuffer {
Some(data)
}

/// Returns a string representation of the buffer data (without gap).
///
/// # Examples
///
/// ```
/// use scribe::buffer::GapBuffer;
///
/// let mut buffer = GapBuffer::new("my data".to_string());
/// assert_eq!(buffer.to_string(), "my data");
/// ```
pub fn to_string(&self) -> String {
String::from_utf8_lossy(&self.data[..self.gap_start]).to_string() +
&*String::from_utf8_lossy(&self.data[self.gap_start+self.gap_length..])
}

/// Removes the specified range of data from the buffer.
///
/// # Examples
Expand Down Expand Up @@ -304,6 +290,15 @@ impl GapBuffer {
}
}

impl fmt::Display for GapBuffer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let first_half = String::from_utf8_lossy(&self.data[..self.gap_start]);
let second_half = String::from_utf8_lossy(&self.data[self.gap_start+self.gap_length..]);

write!(f, "{}{}", first_half, second_half)
}
}

#[cfg(test)]
mod tests {
use crate::buffer::{GapBuffer, Position, Range};
Expand Down

0 comments on commit 635c2bd

Please sign in to comment.