Skip to content

Commit d241b1d

Browse files
committed
test:解决单元测试发现的一些问题
1 parent 57cd77e commit d241b1d

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

src/fuzzyMatch.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,44 +131,59 @@ export function removeSymbolSpaces(strContentIn: string): { content: string; map
131131
}
132132

133133
// 辅助函数3:将换行符改为空格,并合并连续的空格
134-
export function normalizeWhitespace(content: string): { content: string; mapping: number[] } {
135-
let newContent = '';
136-
const mapping: number[] = [];
137-
let inNonNewlineWhitespace = false; // 跟踪连续的非换行空白字符
138-
let inNewlineSequence = false; // 跟踪连续的换行符
139-
140-
for (let i = 0; i < content.length; i++) {
141-
const char = content[i];
142-
143-
if (char === '\n') {
144-
if (!inNewlineSequence) {
145-
// 第一个换行符,保留
146-
newContent += '\n';
147-
mapping.push(i);
148-
inNewlineSequence = true;
134+
export function normalizeWhitespace(content: string): { content: string; mapping: number[] }
135+
{
136+
let strNewContent: string = "";
137+
let arrMapping: number[] = [];
138+
let bAtLineStart: boolean = true; // 标记当前是否处于行首
139+
let nPendingSpaceIndex: number | null = null; // 待添加空格的原始索引
140+
141+
for (let nIdx = 0; nIdx < content.length; nIdx++)
142+
{
143+
const chChar: string = content[nIdx];
144+
145+
if (chChar === '\n')
146+
{
147+
// 遇到换行符时,丢弃待添加的空格(避免行尾空格)
148+
nPendingSpaceIndex = null;
149+
// 如果输出为空或上一个字符不是换行符,则添加换行符
150+
if (strNewContent.length === 0 || strNewContent[strNewContent.length - 1] !== '\n')
151+
{
152+
strNewContent += '\n';
153+
arrMapping.push(nIdx);
149154
}
150-
// 重置非换行空白字符状态
151-
inNonNewlineWhitespace = false;
152-
} else if (/\s/.test(char)) { // 非换行空白字符(如空格、制表符)
153-
if (!inNonNewlineWhitespace) {
154-
// 第一个非换行空白字符,转换为单个空格
155-
newContent += ' ';
156-
mapping.push(i);
157-
inNonNewlineWhitespace = true;
155+
bAtLineStart = true;
156+
}
157+
else if (/\s/.test(chChar))
158+
{
159+
// 遇到非换行空白字符:如果在行首,则忽略;否则,记录第一个空白字符索引
160+
if (!bAtLineStart)
161+
{
162+
if (nPendingSpaceIndex === null)
163+
{
164+
nPendingSpaceIndex = nIdx;
165+
}
158166
}
159-
// 重置换行符状态
160-
inNewlineSequence = false;
161-
} else {
162-
// 非空白字符,直接添加
163-
newContent += char;
164-
mapping.push(i);
165-
inNonNewlineWhitespace = false;
166-
inNewlineSequence = false;
167+
}
168+
else
169+
{
170+
// 遇到非空白字符时,如果有待添加的空格则先输出一个空格
171+
if (nPendingSpaceIndex !== null)
172+
{
173+
strNewContent += ' ';
174+
arrMapping.push(nPendingSpaceIndex);
175+
nPendingSpaceIndex = null;
176+
}
177+
strNewContent += chChar;
178+
arrMapping.push(nIdx);
179+
bAtLineStart = false;
167180
}
168181
}
169-
return { content: newContent, mapping };
182+
183+
return { content: strNewContent, mapping: arrMapping };
170184
}
171185

186+
172187
export function normalizePattern(pattern: string): string {
173188
const { content } = normalizeContent(pattern);
174189
return content;

0 commit comments

Comments
 (0)