Beginnings of a text buffer.
This commit is contained in:
parent
8ce0631155
commit
05a9016714
53
src/buffer.rs
Normal file
53
src/buffer.rs
Normal 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())
|
||||||
|
// }
|
||||||
|
// }
|
13
src/main.rs
13
src/main.rs
|
@ -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());
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user