Beginnings of a text buffer.

This commit is contained in:
Nathan Vegdahl 2014-12-13 02:02:51 -08:00
parent 8ce0631155
commit 05a9016714
2 changed files with 65 additions and 1 deletions

53
src/buffer.rs Normal file
View File

@ -0,0 +1,53 @@
use std::cmp::min;
/// A block of text, contiguous in memory
pub struct TextBlock {
pub data: Vec<u8>,
}
impl TextBlock {
pub fn new() -> TextBlock {
TextBlock {
data: Vec::<u8>::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<TextBuffer>, Box<TextBuffer>)
// }
//
// impl TextBuffer {
// pub fn new() -> TextBuffer {
// TextBuffer::Block(TextBlock::new())
// }
// }

View File

@ -3,6 +3,9 @@ extern crate docopt;
extern crate serialize; extern crate serialize;
use docopt::Docopt; use docopt::Docopt;
use buffer::TextBlock;
mod buffer;
// Usage documentation string // Usage documentation string
@ -27,5 +30,13 @@ fn main() {
// Get command-line arguments // Get command-line arguments
let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); 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());
} }