( input: string, charPairs: Map<string, string>, placeholder: string, )
| 1068 | } |
| 1069 | |
| 1070 | function escapeBlocks( |
| 1071 | input: string, |
| 1072 | charPairs: Map<string, string>, |
| 1073 | placeholder: string, |
| 1074 | ): StringWithEscapedBlocks { |
| 1075 | const resultParts: string[] = []; |
| 1076 | const escapedBlocks: string[] = []; |
| 1077 | let openCharCount = 0; |
| 1078 | let nonBlockStartIndex = 0; |
| 1079 | let blockStartIndex = -1; |
| 1080 | let openChar: string | undefined; |
| 1081 | let closeChar: string | undefined; |
| 1082 | |
| 1083 | for (let i = 0; i < input.length; i++) { |
| 1084 | const char = input[i]; |
| 1085 | if (char === '\\') { |
| 1086 | i++; |
| 1087 | } else if (char === closeChar) { |
| 1088 | openCharCount--; |
| 1089 | if (openCharCount === 0) { |
| 1090 | escapedBlocks.push(input.substring(blockStartIndex, i)); |
| 1091 | resultParts.push(placeholder); |
| 1092 | nonBlockStartIndex = i; |
| 1093 | blockStartIndex = -1; |
| 1094 | openChar = closeChar = undefined; |
| 1095 | } |
| 1096 | } else if (char === openChar) { |
| 1097 | openCharCount++; |
| 1098 | } else if (openCharCount === 0 && charPairs.has(char)) { |
| 1099 | openChar = char; |
| 1100 | closeChar = charPairs.get(char); |
| 1101 | openCharCount = 1; |
| 1102 | blockStartIndex = i + 1; |
| 1103 | resultParts.push(input.substring(nonBlockStartIndex, blockStartIndex)); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | if (blockStartIndex !== -1) { |
| 1108 | escapedBlocks.push(input.substring(blockStartIndex)); |
| 1109 | resultParts.push(placeholder); |
| 1110 | } else { |
| 1111 | resultParts.push(input.substring(nonBlockStartIndex)); |
| 1112 | } |
| 1113 | |
| 1114 | return new StringWithEscapedBlocks(resultParts.join(''), escapedBlocks); |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Object containing as keys characters that should be substituted by placeholders |
no test coverage detected
searching dependent graphs…