( raw: string, mtimeMs: number, offset: number, maxLines: number | undefined, truncateAtBytes: number | undefined, )
| 140 | // --------------------------------------------------------------------------- |
| 141 | |
| 142 | function readFileInRangeFast( |
| 143 | raw: string, |
| 144 | mtimeMs: number, |
| 145 | offset: number, |
| 146 | maxLines: number | undefined, |
| 147 | truncateAtBytes: number | undefined, |
| 148 | ): ReadFileRangeResult { |
| 149 | const endLine = maxLines !== undefined ? offset + maxLines : Infinity |
| 150 | |
| 151 | // Strip BOM. |
| 152 | const text = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw |
| 153 | |
| 154 | // Split lines, strip \r, select range. |
| 155 | const selectedLines: string[] = [] |
| 156 | let lineIndex = 0 |
| 157 | let startPos = 0 |
| 158 | let newlinePos: number |
| 159 | let selectedBytes = 0 |
| 160 | let truncatedByBytes = false |
| 161 | |
| 162 | function tryPush(line: string): boolean { |
| 163 | if (truncateAtBytes !== undefined) { |
| 164 | const sep = selectedLines.length > 0 ? 1 : 0 |
| 165 | const nextBytes = selectedBytes + sep + Buffer.byteLength(line) |
| 166 | if (nextBytes > truncateAtBytes) { |
| 167 | truncatedByBytes = true |
| 168 | return false |
| 169 | } |
| 170 | selectedBytes = nextBytes |
| 171 | } |
| 172 | selectedLines.push(line) |
| 173 | return true |
| 174 | } |
| 175 | |
| 176 | while ((newlinePos = text.indexOf('\n', startPos)) !== -1) { |
| 177 | if (lineIndex >= offset && lineIndex < endLine && !truncatedByBytes) { |
| 178 | let line = text.slice(startPos, newlinePos) |
| 179 | if (line.endsWith('\r')) { |
| 180 | line = line.slice(0, -1) |
| 181 | } |
| 182 | tryPush(line) |
| 183 | } |
| 184 | lineIndex++ |
| 185 | startPos = newlinePos + 1 |
| 186 | } |
| 187 | |
| 188 | // Final fragment (no trailing newline). |
| 189 | if (lineIndex >= offset && lineIndex < endLine && !truncatedByBytes) { |
| 190 | let line = text.slice(startPos) |
| 191 | if (line.endsWith('\r')) { |
| 192 | line = line.slice(0, -1) |
| 193 | } |
| 194 | tryPush(line) |
| 195 | } |
| 196 | lineIndex++ |
| 197 | |
| 198 | const content = selectedLines.join('\n') |
| 199 | return { |
no test coverage detected