JS-semantics `str.replace(/^ /gm, "")`: try the \A-anchored `pat` at position 0 and after every JS line terminator, left to right, resuming after each match's end — a faithful /g replace. (Remaining known divergence: JS `\s` includes U+FEFF, Rust's does not; an embedded BOM inside a comment is accepted as unreachable.)
(s: &str, pat: &Regex)
| 76 | /// divergence: JS `\s` includes U+FEFF, Rust's does not; an embedded BOM |
| 77 | /// inside a comment is accepted as unreachable.) |
| 78 | fn js_multiline_strip(s: &str, pat: &Regex) -> String { |
| 79 | let mut out = String::with_capacity(s.len()); |
| 80 | let mut last = 0usize; |
| 81 | let mut pos = 0usize; |
| 82 | while pos <= s.len() { |
| 83 | let at_line_start = pos == 0 |
| 84 | || s[..pos].chars().next_back().is_some_and(is_js_line_terminator); |
| 85 | if at_line_start { |
| 86 | if let Some(m) = pat.find(&s[pos..]) { |
| 87 | if !m.is_empty() { |
| 88 | out.push_str(&s[last..pos]); |
| 89 | last = pos + m.end(); |
| 90 | pos = last; |
| 91 | continue; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | match s[pos..].chars().next() { |
| 96 | Some(c) => pos += c.len_utf8(), |
| 97 | None => break, |
| 98 | } |
| 99 | } |
| 100 | out.push_str(&s[last..]); |
| 101 | out |
| 102 | } |
| 103 | |
| 104 | /// cleanCommentMarkers — strip comment syntax, keep the prose. |
| 105 | pub fn clean_comment_markers(comment: &str) -> String { |
no test coverage detected