MCPcopy Index your code
hub / github.com/codeaashu/claude-code / readFileInRangeFast

Function readFileInRangeFast

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

Source from the content-addressed store, hash-verified

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

Callers 1

readFileInRangeFunction · 0.85

Calls 1

tryPushFunction · 0.85

Tested by

no test coverage detected