MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / applyRunLengthEncoding

Function applyRunLengthEncoding

src/integrations/misc/extract-text.ts:310–362  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

308 * @returns The compressed text with run-length encoding applied
309 */
310export function applyRunLengthEncoding(content: string): string {
311 if (!content) {
312 return content
313 }
314
315 let result = ""
316 let pos = 0
317 let repeatCount = 0
318 let prevLine = null
319
320 while (pos < content.length) {
321 const nextNewlineIdx = content.indexOf("\n", pos) // Find next line feed (\n) index
322 const currentLine = nextNewlineIdx === -1 ? content.slice(pos) : content.slice(pos, nextNewlineIdx + 1)
323
324 if (prevLine === null) {
325 prevLine = currentLine
326 } else if (currentLine === prevLine) {
327 repeatCount++
328 } else {
329 if (repeatCount > 0) {
330 const compressionDesc = `<previous line repeated ${repeatCount} additional times>\n`
331 if (compressionDesc.length < prevLine.length * (repeatCount + 1)) {
332 result += prevLine + compressionDesc
333 } else {
334 for (let i = 0; i <= repeatCount; i++) {
335 result += prevLine
336 }
337 }
338 repeatCount = 0
339 } else {
340 result += prevLine
341 }
342 prevLine = currentLine
343 }
344
345 pos = nextNewlineIdx === -1 ? content.length : nextNewlineIdx + 1
346 }
347
348 if (repeatCount > 0 && prevLine !== null) {
349 const compressionDesc = `<previous line repeated ${repeatCount} additional times>\n`
350 if (compressionDesc.length < prevLine.length * repeatCount) {
351 result += prevLine + compressionDesc
352 } else {
353 for (let i = 0; i <= repeatCount; i++) {
354 result += prevLine
355 }
356 }
357 } else if (prevLine !== null) {
358 result += prevLine
359 }
360
361 return result
362}
363
364/**
365 * Processes carriage returns (\r) in terminal output to simulate how a real terminal would display content.

Callers 3

compressTerminalOutputFunction · 0.90
runBenchmarkFunction · 0.90

Calls

no outgoing calls

Tested by 1

runBenchmarkFunction · 0.72