]> git.lizzy.rs Git - rust.git/blobdiff - src/string.rs
Merge pull request #3240 from Xanewok/parser-panic
[rust.git] / src / string.rs
index f98c4189ea53735e1041ed12db7254c44f9e11c1..668324b4d8fe598e43998311a0b5b97e163d720e 100644 (file)
@@ -62,7 +62,7 @@ fn max_chars_with_indent(&self) -> Option<usize> {
         )
     }
 
-    /// Like max_chars_with_indent but the indentation is not substracted.
+    /// Like max_chars_with_indent but the indentation is not subtracted.
     /// This allows to fit more graphemes from the string on a line when
     /// SnippetState::EndWithLineFeed.
     fn max_chars_without_indent(&self) -> Option<usize> {
@@ -107,7 +107,7 @@ pub fn rewrite_string<'a>(
             for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
                 if is_line_feed(grapheme) {
                     // take care of blank lines
-                    result = trim_right_but_line_feed(fmt.trim_end, result);
+                    result = trim_end_but_line_feed(fmt.trim_end, result);
                     result.push_str("\n");
                     if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
                         result.push_str(&indent_without_newline);
@@ -117,7 +117,7 @@ pub fn rewrite_string<'a>(
                     result.push_str(grapheme);
                 }
             }
-            result = trim_right_but_line_feed(fmt.trim_end, result);
+            result = trim_end_but_line_feed(fmt.trim_end, result);
             break;
         }
 
@@ -138,7 +138,7 @@ pub fn rewrite_string<'a>(
             }
             SnippetState::EndWithLineFeed(line, len) => {
                 if line == "\n" && fmt.trim_end {
-                    result = result.trim_right().to_string();
+                    result = result.trim_end().to_string();
                 }
                 result.push_str(&line);
                 if is_bareline_ok {
@@ -172,7 +172,7 @@ fn detect_url(s: &[&str], index: usize) -> Option<usize> {
     if s.len() < start + 8 {
         return None;
     }
-    let prefix = s[start..start + 8].join("");
+    let prefix = s[start..start + 8].concat();
     if prefix.starts_with("https://")
         || prefix.starts_with("http://")
         || prefix.starts_with("ftp://")
@@ -188,11 +188,11 @@ fn detect_url(s: &[&str], index: usize) -> Option<usize> {
 }
 
 /// Trims whitespaces to the right except for the line feed character.
-fn trim_right_but_line_feed(trim_end: bool, result: String) -> String {
+fn trim_end_but_line_feed(trim_end: bool, result: String) -> String {
     let whitespace_except_line_feed = |c: char| c.is_whitespace() && c != '\n';
     if trim_end && result.ends_with(whitespace_except_line_feed) {
         result
-            .trim_right_matches(whitespace_except_line_feed)
+            .trim_end_matches(whitespace_except_line_feed)
             .to_string()
     } else {
         result
@@ -236,15 +236,15 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
             .iter()
             .rposition(|grapheme| not_whitespace_except_line_feed(grapheme))
             .unwrap_or(index);
-        // Take into account newlines occuring in input[0..=index], i.e., the possible next new
+        // Take into account newlines occurring in input[0..=index], i.e., the possible next new
         // line. If there is one, then text after it could be rewritten in a way that the available
         // space is fully used.
         for (i, grapheme) in input[0..=index].iter().enumerate() {
             if is_line_feed(grapheme) {
                 if i <= index_minus_ws {
-                    let mut line = input[0..i].join("");
+                    let mut line = &input[0..i].concat()[..];
                     if trim_end {
-                        line = line.trim_right().to_string();
+                        line = line.trim_end();
                     }
                     return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
                 }
@@ -256,7 +256,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
         for (i, grapheme) in input[index + 1..].iter().enumerate() {
             if !trim_end && is_line_feed(grapheme) {
                 return SnippetState::EndWithLineFeed(
-                    input[0..=index + 1 + i].join("").to_string(),
+                    input[0..=index + 1 + i].concat(),
                     index + 2 + i,
                 );
             } else if not_whitespace_except_line_feed(grapheme) {
@@ -266,15 +266,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
         }
 
         if trim_end {
-            SnippetState::LineEnd(
-                input[0..=index_minus_ws].join("").to_string(),
-                index_plus_ws + 1,
-            )
+            SnippetState::LineEnd(input[0..=index_minus_ws].concat(), index_plus_ws + 1)
         } else {
-            SnippetState::LineEnd(
-                input[0..=index_plus_ws].join("").to_string(),
-                index_plus_ws + 1,
-            )
+            SnippetState::LineEnd(input[0..=index_plus_ws].concat(), index_plus_ws + 1)
         }
     };
 
@@ -298,15 +292,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
                 .position(|grapheme| not_whitespace_except_line_feed(grapheme))
                 .unwrap_or(0);
         return if trim_end {
-            SnippetState::LineEnd(
-                input[..=url_index_end].join("").to_string(),
-                index_plus_ws + 1,
-            )
+            SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1)
         } else {
-            return SnippetState::LineEnd(
-                input[..=index_plus_ws].join("").to_string(),
-                index_plus_ws + 1,
-            );
+            return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1);
         };
     }
     match input[0..max_chars]
@@ -331,7 +319,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]
                 // A boundary was found after the line limit
                 Some(index) => break_at(max_chars + index),
                 // No boundary to the right, the input cannot be broken
-                None => SnippetState::EndOfInput(input.join("").to_string()),
+                None => SnippetState::EndOfInput(input.concat()),
             },
         },
     }