diff --git a/src/buffer.rs b/src/buffer.rs new file mode 100644 index 0000000..180748a --- /dev/null +++ b/src/buffer.rs @@ -0,0 +1,53 @@ +use std::cmp::min; + + +/// A block of text, contiguous in memory +pub struct TextBlock { + pub data: Vec, +} + +impl TextBlock { + pub fn new() -> TextBlock { + TextBlock { + data: Vec::::new() + } + } + + pub fn insert_text(&mut self, text: &str, pos: uint) { + let ins = min(pos, self.data.len()); + + // Grow data size + self.data.grow(text.len(), 0); + + // Move old bytes forward + let mut from = self.data.len() - text.len(); + let mut to = self.data.len(); + while from > ins { + from -= 1; + to -= 1; + + self.data[to] = self.data[from]; + } + + // Copy new bytes in + let mut i = ins; + for b in text.bytes() { + self.data[i] = b; + i += 1 + } + } +} + + +// /// A rope text storage buffer, using TextBlocks for its underlying text +// /// storage. +// pub enum TextBuffer { +// Block(TextBlock), +// Node(Box, Box) +// } +// +// impl TextBuffer { +// pub fn new() -> TextBuffer { +// TextBuffer::Block(TextBlock::new()) +// } +// } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 092e623..980c1e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,9 @@ extern crate docopt; extern crate serialize; use docopt::Docopt; +use buffer::TextBlock; + +mod buffer; // Usage documentation string @@ -27,5 +30,13 @@ fn main() { // Get command-line arguments let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); - println!("Hello! {}", args.arg_file); + let mut tb = TextBlock::new(); + + for _ in range(0i, 50) { + tb.insert_text("Hello", 0); + tb.insert_text("Goodbye", 0); + tb.insert_text(args.arg_file.as_slice(), 0); + } + + println!("{}", std::str::from_utf8(tb.data.as_slice()).unwrap()); } \ No newline at end of file