servo: Do a little less copying in TextRun.split
authorBrian Anderson <banderson@mozilla.com>
Sat, 15 Sep 2012 16:31:38 -0700
changeset 471072 c13f13686c504b421bdadf3e3496dbcda33cdeb0
parent 471071 3899d9b887257e26ca9ca086b2c096c52ead4834
child 471073 a981a626b32430f8127640d4ea459fdef8a9cae1
push id44079
push userbmo:gps@mozilla.com
push dateSat, 04 Feb 2017 00:14:49 +0000
servo: Do a little less copying in TextRun.split Source-Repo: https://github.com/servo/servo Source-Revision: 4e3e5a879d4ff03ae09efb2f9ffe44ca823ed2ae
servo/src/servo/text/text_run.rs
--- a/servo/src/servo/text/text_run.rs
+++ b/servo/src/servo/text/text_run.rs
@@ -25,38 +25,44 @@ impl TextRun {
     // of the TextRun, but I'm hitting cycle collector bugs
     fn split(font: &Font, h_offset: au) -> (TextRun, TextRun) {
         assert h_offset >= self.min_break_width();
         assert h_offset <= self.size_.width;
 
         let mut curr_run = ~"";
 
         for iter_indivisible_slices(font, self.text) |slice| {
-            let mut candidate = curr_run;
+            let mut candidate = copy curr_run;
 
             if candidate.is_not_empty() {
-                candidate += " "; // FIXME: just inserting spaces between words can't be right
+                str::push_str(candidate, " "); // FIXME: just inserting spaces between words can't be right
             }
 
-            candidate += slice;
+            str::push_str(candidate, slice);
 
             let glyphs = shape_text(font, candidate);
             let size = glyph_run_size(glyphs);
             if size.width <= h_offset {
-                curr_run = candidate;
+                curr_run = move candidate;
             } else {
                 break;
             }
         }
 
         assert curr_run.is_not_empty();
 
         let first = move curr_run;
-        let second = str::slice(self.text, first.len(), self.text.len());
-        let second = second.trim_left();
+        let second_start = match str::find_from(self.text, first.len(), |c| !char::is_whitespace(c)) {
+            Some(idx) => idx,
+            None => {
+                // This will be an empty string
+                self.text.len()
+            }
+        };
+        let second = str::slice(self.text, second_start, self.text.len());
         return (TextRun(font, first), TextRun(font, second));
     }
 }
 
 fn TextRun(font: &Font, +text: ~str) -> TextRun {
     let glyphs = shape_text(font, text);
     let size = glyph_run_size(glyphs);
     let min_break_width = calc_min_break_width(font, text);