(src: string)
| 96 | * character literal. |
| 97 | */ |
| 98 | export function blankRustCode(src: string): string { |
| 99 | const n = src.length; |
| 100 | const parts: string[] = []; |
| 101 | let last = 0; |
| 102 | // `src[from, to)` as spaces, its newlines kept; the text before it as it is. |
| 103 | const blank = (from: number, to: number): void => { |
| 104 | if (to <= from) return; |
| 105 | parts.push(src.slice(last, from)); |
| 106 | const chunk = src.slice(from, to); |
| 107 | parts.push(chunk.includes('\n') ? chunk.replace(/[^\n]/g, ' ') : ' '.repeat(to - from)); |
| 108 | last = to; |
| 109 | }; |
| 110 | const special = /[/"']/g; |
| 111 | for (let m = special.exec(src); m; m = special.exec(src)) { |
| 112 | const i = m.index; |
| 113 | const c = src[i]; |
| 114 | const d = src[i + 1]; |
| 115 | if (c === '/') { |
| 116 | if (d === '/') { |
| 117 | const end = src.indexOf('\n', i); |
| 118 | const to = end < 0 ? n : end; |
| 119 | blank(i, to); |
| 120 | special.lastIndex = to; |
| 121 | } else if (d === '*') { |
| 122 | let depth = 1; |
| 123 | let j = i + 2; |
| 124 | while (j < n && depth > 0) { |
| 125 | if (src[j] === '/' && src[j + 1] === '*') { depth++; j += 2; } |
| 126 | else if (src[j] === '*' && src[j + 1] === '/') { depth--; j += 2; } |
| 127 | else j++; |
| 128 | } |
| 129 | blank(i, j); |
| 130 | special.lastIndex = j; |
| 131 | } |
| 132 | continue; |
| 133 | } |
| 134 | if (c === '"') { |
| 135 | // A raw string: `r"…"`, `r#"…"#`, also after a `b` or `c` prefix — |
| 136 | // the `r` no end of a longer name. |
| 137 | let k = i - 1; |
| 138 | while (k >= 0 && src[k] === '#') k--; |
| 139 | let p = k - 1; |
| 140 | if (src[p] === 'b' || src[p] === 'c') p--; |
| 141 | if (src[k] === 'r' && (p < 0 || !isIdentChar(src[p]))) { |
| 142 | const close = `"${'#'.repeat(i - 1 - k)}`; |
| 143 | const end = src.indexOf(close, i + 1); |
| 144 | const to = end < 0 ? n : end; |
| 145 | blank(i + 1, to); |
| 146 | special.lastIndex = Math.min(n, to + close.length); |
| 147 | continue; |
| 148 | } |
| 149 | let j = i + 1; |
| 150 | while (j < n && src[j] !== '"') j += src[j] === '\\' ? 2 : 1; |
| 151 | blank(i + 1, Math.min(j, n)); |
| 152 | special.lastIndex = Math.min(n, j + 1); |
| 153 | continue; |
| 154 | } |
| 155 | // `'\n'`, `'\u{1F600}'`; `'x'`; a character outside the BMP; else a lifetime or a label. |
no test coverage detected