css, dom, text: Stop copying so much. Also stop asserting.

This commit is contained in:
Patrick Walton 2012-11-18 19:12:34 -08:00
parent 230ac2dd17
commit 61f558dffc
3 changed files with 33 additions and 7 deletions

View file

@ -583,8 +583,14 @@ impl GlyphStore {
}
fn iter_glyphs_for_range(&self, range: &const Range, cb: fn&(uint, GlyphInfo/&) -> bool) {
assert range.begin() < self.entry_buffer.len();
assert range.end() <= self.entry_buffer.len();
if range.begin() >= self.entry_buffer.len() {
error!("iter_glyphs_for_range: range.begin beyond length!");
return;
}
if range.end() > self.entry_buffer.len() {
error!("iter_glyphs_for_range: range.end beyond length!");
return;
}
for range.eachi |i| {
if !self.iter_glyphs_for_index(i, cb) { break; }
@ -609,8 +615,12 @@ impl GlyphStore {
}
fn char_is_newline(i: uint) -> bool {
assert i < self.entry_buffer.len();
self.entry_buffer[i].char_is_newline()
if i >= self.entry_buffer.len() {
error!("char_is_newline: beyond entry buffer!");
false
} else {
self.entry_buffer[i].char_is_newline()
}
}
fn is_ligature_start(i: uint) -> bool {

View file

@ -84,9 +84,11 @@ impl NodeSelectHandler: SelectHandler<Node> {
do node.read |data| {
match *data.kind {
Element(ref data) => {
match data.get_attr("id") {
None => false,
Some(existing_id) => str::eq_slice(id, existing_id)
do data.with_attr("id") |existing_id_opt| {
match existing_id_opt {
None => false,
Some(existing_id) => str::eq_slice(id, existing_id)
}
}
}
_ => fail ~"attempting to style non-element node"

View file

@ -20,6 +20,20 @@ impl ElementData {
}
}
// Gets an attribute without copying.
//
// FIXME: Should not take a closure, but we need covariant type parameters for
// that.
fn with_attr<R>(name: &str, f: &fn(Option<&str>) -> R) -> R {
for self.attrs.each |attr| {
if name == attr.name {
let value: &str = attr.value;
return f(Some(value));
}
}
f(None)
}
fn set_attr(name: &str, value: ~str) {
let idx = do self.attrs.position |attr| { name == attr.name };
match idx {