MCPcopy Create free account
hub / github.com/Effect-TS/effect / splitLinesArray

Function splitLinesArray

packages/effect/src/Channel.ts:6514–6563  ·  view source on GitHub ↗
(chunk: Arr.NonEmptyReadonlyArray<string>)

Source from the content-addressed store, hash-verified

6512 let done = Option.none<Done>()
6513
6514 function splitLinesArray(chunk: Arr.NonEmptyReadonlyArray<string>): Arr.NonEmptyReadonlyArray<string> | null {
6515 const chunkBuilder: Array<string> = []
6516
6517 function pushLine(segment: string): void {
6518 if (stringBuilder.length === 0) {
6519 chunkBuilder.push(segment)
6520 } else {
6521 chunkBuilder.push(stringBuilder + segment)
6522 stringBuilder = ""
6523 }
6524 }
6525
6526 for (let i = 0; i < chunk.length; i++) {
6527 const str = chunk[i]
6528 if (str.length !== 0) {
6529 let from = 0
6530 let indexOfCR = str.indexOf("\r")
6531 let indexOfLF = str.indexOf("\n")
6532 if (midCRLF) {
6533 if (indexOfLF === 0) {
6534 pushLine("")
6535 from = 1
6536 indexOfLF = str.indexOf("\n", from)
6537 } else {
6538 pushLine("")
6539 }
6540 midCRLF = false
6541 }
6542 while (indexOfCR !== -1 || indexOfLF !== -1) {
6543 if (indexOfCR === -1 || (indexOfLF !== -1 && indexOfLF < indexOfCR)) {
6544 pushLine(str.substring(from, indexOfLF))
6545 from = indexOfLF + 1
6546 indexOfLF = str.indexOf("\n", from)
6547 } else {
6548 if (str.length === indexOfCR + 1) {
6549 midCRLF = true
6550 indexOfCR = -1
6551 } else {
6552 pushLine(str.substring(from, indexOfCR))
6553 from = indexOfCR + (indexOfLF === indexOfCR + 1 ? 2 : 1)
6554 indexOfCR = str.indexOf("\r", from)
6555 indexOfLF = str.indexOf("\n", from)
6556 }
6557 }
6558 }
6559 stringBuilder = stringBuilder + str.substring(from, str.length - (midCRLF ? 1 : 0))
6560 }
6561 }
6562 return Arr.isReadonlyArrayNonEmpty(chunkBuilder) ? chunkBuilder : null
6563 }
6564
6565 const pullOrFlush: Pull.Pull<Arr.NonEmptyReadonlyArray<string>, Err, Done> = Effect.suspend(() => {
6566 if (done._tag === "Some") {

Callers 1

loopFunction · 0.85

Calls 1

pushLineFunction · 0.85

Tested by

no test coverage detected