MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / readFileInRangeFast

Function readFileInRangeFast

source code/utils/readFileInRange.ts:130–196  ·  view source on GitHub ↗
(
  raw: string,
  mtimeMs: number,
  offset: number,
  maxLines: number | undefined,
  truncateAtBytes: number | undefined,
)

Source from the content-addressed store, hash-verified

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

Callers 1

readFileInRangeFunction · 0.85

Calls 1

tryPushFunction · 0.85

Tested by

no test coverage detected