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

Function splitByWhitespaces

packages/effect/src/unstable/process/ChildProcess.ts:970–1046  ·  view source on GitHub ↗
(template: string, rawTemplate: string)

Source from the content-addressed store, hash-verified

968 * 2. `template.raw` - The raw string (escape sequences are literal).
969 */
970const splitByWhitespaces = (template: string, rawTemplate: string): {
971 readonly tokens: ReadonlyArray<string>
972 readonly hasLeadingWhitespace: boolean
973 readonly hasTrailingWhitespace: boolean
974} => {
975 if (rawTemplate.length === 0) {
976 return {
977 tokens: [],
978 hasLeadingWhitespace: false,
979 hasTrailingWhitespace: false
980 }
981 }
982
983 const hasLeadingWhitespace = DELIMITERS.has(rawTemplate[0])
984 const tokens: Array<string> = []
985
986 // Given that escape sequences will have different lengths in the template
987 // versus the raw template, we must maintain two indices:
988 // - One for the index into the template string
989 // - One for the index into the raw template string
990 // We also maintain the current cursor position for where we are in the template
991 let templateCursor = 0
992 for (
993 let templateIndex = 0, rawIndex = 0;
994 templateIndex < template.length;
995 templateIndex += 1, rawIndex += 1
996 ) {
997 // Use the raw template character to check for actual whitespace
998 const rawCharacter = rawTemplate[rawIndex]
999
1000 if (DELIMITERS.has(rawCharacter)) {
1001 // Whitespace found, extract token from template if necessary
1002 if (templateCursor !== templateIndex) {
1003 tokens.push(template.slice(templateCursor, templateIndex))
1004 }
1005 // Advance the template start index to the current position
1006 templateCursor = templateIndex + 1
1007 } else if (rawCharacter === "\\") {
1008 // Escape sequence detected, check the next raw character
1009 const nextRawCharacter = rawTemplate[rawIndex + 1]
1010
1011 if (nextRawCharacter === "\n") {
1012 // Handle `\` character followed by a newline (i.e. a line continuation) by:
1013 // - Reversing the template index (backslash-newline is erased in template)
1014 // - Advancing the raw template index past the line continuation
1015 templateIndex -= 1
1016 rawIndex += 1
1017 } else if (nextRawCharacter === "u" && rawTemplate[rawIndex + 2] === "{") {
1018 // Handle variable-length unicode escape sequences (i.e. `\u{1F600}`) by:
1019 // - Advancing the raw template index past the unicode escape sequence
1020 rawIndex = rawTemplate.indexOf("}", rawIndex + 3)
1021 } else {
1022 // Advance raw template index past fixed-length escape sequences:
1023 // - \n → 2 chars (backslash + n)
1024 // - \t → 2 chars (backslash + t)
1025 // - \xHH → 4 chars (backslash + x + H + H)
1026 // - \uHHHH → 6 chars (backslash + u + H + H + H + H)
1027 rawIndex += ESCAPE_LENGTH[nextRawCharacter] ?? 1

Callers 1

parseTemplateFunction · 0.85

Calls 2

pushMethod · 0.80
hasMethod · 0.45

Tested by

no test coverage detected