From 5e7f9fc585c3111cf33b66210275558810cd56c4 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Tue, 16 Oct 2012 17:42:29 -0700 Subject: [PATCH] Rewrite iter_indivisible_pieces_for_range in idiomatic offset+length parlance. --- src/servo/text/text_run.rs | 44 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/servo/text/text_run.rs b/src/servo/text/text_run.rs index 1a74db2fcb3..3740d0f6180 100644 --- a/src/servo/text/text_run.rs +++ b/src/servo/text/text_run.rs @@ -129,43 +129,43 @@ impl TextRun : TextRunMethods { assert offset < self.text.len(); assert offset + length <= self.text.len(); - //TODO: need a more sophisticated model of words and possible breaks - let text = str::view(self.text, offset, length); - - let mut clump_start = offset; + let mut clump_offset = offset; + let mut clump_length; loop { - // clump contiguous non-whitespace - match str::find_from(text, clump_start, |c| !char::is_whitespace(c)) { - Some(clump_end) => { - if !f(clump_start, clump_end - clump_start) { break } - clump_start = clump_end; + // find next non-whitespace byte index, then clump all whitespace before it. + match str::find_from(self.text, clump_offset, |c| !char::is_whitespace(c)) { + Some(nonws_char_offset) => { + clump_length = nonws_char_offset - clump_offset; + if !f(clump_offset, clump_length) { break } + clump_offset += clump_length; // reached end - if clump_start == offset + length { break } + if clump_offset == offset + length { break } }, None => { // nothing left, flush last piece containing only whitespace - if clump_start < offset + length { - let clump_end = offset + length - 1; - f(clump_start, clump_end - clump_start + 1); + if clump_offset < offset + length { + let clump_length = (offset + length) - clump_offset; + f(clump_offset, clump_length); } break } }; - // clump contiguous whitespace - match str::find_from(text, clump_start, |c| char::is_whitespace(c)) { - Some(clump_end) => { - if !f(clump_start, clump_end - clump_start) { break } - clump_start = clump_end; + // find next whitespace byte index, then clump all non-whitespace before it. + match str::find_from(self.text, clump_offset, |c| char::is_whitespace(c)) { + Some(ws_char_offset) => { + clump_length = ws_char_offset - clump_offset; + if !f(clump_offset, clump_length) { break } + clump_offset += clump_length; // reached end - if clump_start == offset + length { break } + if clump_offset == offset + length { break } } None => { // nothing left, flush last piece containing only non-whitespaces - if clump_start < offset + length { - let clump_end = offset + length - 1; - f(clump_start, clump_end - clump_start + 1); + if clump_offset < offset + length { + let clump_length = (offset + length) - clump_offset; + f(clump_offset, clump_length); } break }