|
| 1 | +// ================ 类型定义 ================ |
| 2 | +interface MatchPosition { |
| 3 | + start: number; |
| 4 | + end: number; |
| 5 | +} |
| 6 | + |
| 7 | +interface NormalizedContent { |
| 8 | + content: string; |
| 9 | + mapping: number[]; |
| 10 | +} |
| 11 | + |
| 12 | +// ================ 核心实现 ================ |
| 13 | +const MAX_EDIT_DISTANCE = 5; |
| 14 | +const SEGMENT_COUNT = MAX_EDIT_DISTANCE + 1; |
| 15 | + |
| 16 | +export function applyFuzzyGlobalReplace( |
| 17 | + strContent: string, |
| 18 | + strOldContent: string, |
| 19 | + strNewContent: string |
| 20 | +): string { |
| 21 | + // 第二阶段:模糊匹配流程 |
| 22 | + const { content: normContent, mapping } = normalizeContent(strContent); |
| 23 | + const pattern = normalizePattern(strOldContent); |
| 24 | + |
| 25 | + // 分片查找候选位置 |
| 26 | + const candidates = findCandidatePositions(normContent, pattern); |
| 27 | + |
| 28 | + // 验证并获取有效匹配 |
| 29 | + const matches = verifyMatches(normContent, pattern, candidates, mapping); |
| 30 | + |
| 31 | + if (matches.length === 0) { |
| 32 | + throw new Error(`GLOBAL-REPLACE失败:未找到允许${MAX_EDIT_DISTANCE}个字符差异的匹配`); |
| 33 | + } |
| 34 | + |
| 35 | + // 应用替换 |
| 36 | + return applyReplacements(strContent, matches, strNewContent); |
| 37 | +} |
| 38 | + |
| 39 | +// ================ 算法核心模块 ================ |
| 40 | +function normalizeContent(original: string): NormalizedContent { |
| 41 | + const mapping: number[] = []; |
| 42 | + let normalized = ""; |
| 43 | + let lastCharIsWhitespace = true; |
| 44 | + let currentPos = 0; |
| 45 | + |
| 46 | + for (const char of original) { |
| 47 | + if (/\s/.test(char)) { |
| 48 | + if (!lastCharIsWhitespace) { |
| 49 | + normalized += ' '; |
| 50 | + mapping.push(currentPos); |
| 51 | + lastCharIsWhitespace = true; |
| 52 | + } |
| 53 | + currentPos++; |
| 54 | + } else { |
| 55 | + normalized += char; |
| 56 | + mapping.push(currentPos); |
| 57 | + currentPos++; |
| 58 | + lastCharIsWhitespace = false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return { content: normalized, mapping }; |
| 63 | +} |
| 64 | + |
| 65 | +function normalizePattern(pattern: string): string { |
| 66 | + return pattern.replace(/\s+/g, ' ').trim(); |
| 67 | +} |
| 68 | + |
| 69 | +function findCandidatePositions(content: string, pattern: string): number[] { |
| 70 | + const candidates = new Set<number>(); |
| 71 | + const segments = splitPattern(pattern, SEGMENT_COUNT); |
| 72 | + |
| 73 | + // 查找每个分片的匹配位置 |
| 74 | + segments.forEach(segment => { |
| 75 | + let pos = -1; |
| 76 | + while ((pos = content.indexOf(segment, pos + 1)) !== -1) { |
| 77 | + if (pos === -1) { |
| 78 | + break; |
| 79 | + } |
| 80 | + // 向前后扩展可能的匹配范围 |
| 81 | + const start = Math.max(0, pos - pattern.length); |
| 82 | + const end = Math.min(content.length, pos + pattern.length * 2); |
| 83 | + for (let i = start; i < end; i++) { |
| 84 | + candidates.add(i); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + return Array.from(candidates).sort((a, b) => a - b); |
| 90 | +} |
| 91 | + |
| 92 | +function verifyMatches( |
| 93 | + content: string, |
| 94 | + pattern: string, |
| 95 | + candidates: number[], |
| 96 | + mapping: number[] |
| 97 | +): MatchPosition[] { |
| 98 | + const validMatches: MatchPosition[] = []; |
| 99 | + const patternLen = pattern.length; |
| 100 | + |
| 101 | + candidates.forEach(start => { |
| 102 | + const end = start + patternLen; |
| 103 | + if (end > content.length) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const substring = content.substring(start, end); |
| 108 | + const distance = calculateEditDistance(substring, pattern, MAX_EDIT_DISTANCE); |
| 109 | + |
| 110 | + if (distance <= MAX_EDIT_DISTANCE) { |
| 111 | + validMatches.push({ |
| 112 | + start: mapping[start], |
| 113 | + end: mapping[end] || mapping[mapping.length - 1] |
| 114 | + }); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + return processOverlaps(validMatches); |
| 119 | +} |
| 120 | + |
| 121 | +// ================ 工具函数 ================ |
| 122 | +function splitPattern(pattern: string, count: number): string[] { |
| 123 | + const segments: string[] = []; |
| 124 | + const baseLength = Math.floor(pattern.length / count); |
| 125 | + let remaining = pattern.length % count; |
| 126 | + let pos = 0; |
| 127 | + |
| 128 | + for (let i = 0; i < count; i++) { |
| 129 | + const length = baseLength + (remaining-- > 0 ? 1 : 0); |
| 130 | + segments.push(pattern.substr(pos, length)); |
| 131 | + pos += length; |
| 132 | + } |
| 133 | + |
| 134 | + return segments.filter(s => s.length > 0); |
| 135 | +} |
| 136 | + |
| 137 | +function calculateEditDistance(a: string, b: string, maxDistance: number): number { |
| 138 | +if (Math.abs(a.length - b.length) > maxDistance) { |
| 139 | + return Infinity; |
| 140 | +} |
| 141 | + |
| 142 | +// 使用滚动数组优化 |
| 143 | +let prevRow = Array(b.length + 1).fill(0).map((_, i) => i); |
| 144 | +let currentRow = new Array(b.length + 1); |
| 145 | + |
| 146 | +for (let i = 1; i <= a.length; i++) { |
| 147 | + currentRow[0] = i; |
| 148 | + let minInRow = i; |
| 149 | + |
| 150 | + for (let j = 1; j <= b.length; j++) { |
| 151 | + const cost = a[i - 1] === b[j - 1] ? 0 : 1; |
| 152 | + currentRow[j] = Math.min( |
| 153 | + prevRow[j] + 1, |
| 154 | + currentRow[j - 1] + 1, |
| 155 | + prevRow[j - 1] + cost |
| 156 | + ); |
| 157 | + minInRow = Math.min(minInRow, currentRow[j]); |
| 158 | + } |
| 159 | + |
| 160 | + if (minInRow > maxDistance) { |
| 161 | + return Infinity; |
| 162 | + } |
| 163 | + [prevRow, currentRow] = [currentRow, prevRow]; |
| 164 | + } |
| 165 | + |
| 166 | + return prevRow[b.length]; |
| 167 | +} |
| 168 | + |
| 169 | +function processOverlaps(matches: MatchPosition[]): MatchPosition[] { |
| 170 | + return matches |
| 171 | + .sort((a, b) => a.start - b.start) |
| 172 | + .filter((match, index, arr) => { |
| 173 | + return index === 0 || match.start >= arr[index - 1].end; |
| 174 | + }); |
| 175 | +} |
| 176 | + |
| 177 | +function applyReplacements( |
| 178 | + original: string, |
| 179 | + matches: MatchPosition[], |
| 180 | + replacement: string |
| 181 | +): string { |
| 182 | + let result = original; |
| 183 | + // 从后往前替换避免影响索引 |
| 184 | + for (let i = matches.length - 1; i >= 0; i--) { |
| 185 | + const { start, end } = matches[i]; |
| 186 | + result = result.slice(0, start) + replacement + result.slice(end); |
| 187 | + } |
| 188 | + return result; |
| 189 | +} |
0 commit comments