Skip to content

Commit

Permalink
Fix tests, implement CR
Browse files Browse the repository at this point in the history
  • Loading branch information
OfekShilon committed Jul 13, 2024
1 parent b472256 commit 18a8218
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
10 changes: 6 additions & 4 deletions lib/demangler/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ export class BaseDemangler extends AsmRegex {
asm.text = newText;
// We try to represent in labels.ranges the exact modifications done by replaceAll,
// but replaceAll sometimes modifies substrings not recognized as labels :(
for (const label of asm.labels!) {
if (mapRanges[label.range.startCol])
label.range = mapRanges[label.range.startCol][label.range.endCol] || label.range;
label.name = mapNames[label.name] || label.name;
if (asm.labels) {
for (const label of asm.labels) {
if (mapRanges[label.range.startCol])
label.range = mapRanges[label.range.startCol][label.range.endCol] || label.range;
label.name = mapNames[label.name] || label.name;
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/demangler/prefix-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type charRange = {
endCol: number;
};

export type replaceAction = {
newText: string;
mapRanges: Record<number, Record<number, charRange>>;
mapNames: Record<string, string>;
};

export class PrefixTree {
root: Node = [];

Expand Down Expand Up @@ -91,11 +97,7 @@ export class PrefixTree {
}

// Replace all matches (longest match first) in a line.
replaceAll(line: string): {
newText: string;
mapRanges: Record<number, Record<number, charRange>>;
mapNames: Record<string, string>;
} {
replaceAll(line: string): replaceAction {
let newText = '';
let idxInOld = 0;
let idxInNew = 0;
Expand Down
18 changes: 9 additions & 9 deletions test/demangler-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,27 +354,27 @@ describe('Demangler prefix tree', () => {
replacements.add('aa', 'long_a');
replacements.add('aa_shouldnotmatch', 'ERROR');
it('should replace a short match', () => {
expect(replacements.replaceAll('a')).toEqual('short_a');
expect(replacements.replaceAll('a').newText).toEqual('short_a');
});
it('should replace using the longest match', () => {
expect(replacements.replaceAll('aa')).toEqual('long_a');
expect(replacements.replaceAll('aa').newText).toEqual('long_a');
});
it('should replace using both', () => {
expect(replacements.replaceAll('aaa')).toEqual('long_ashort_a');
expect(replacements.replaceAll('aaa').newText).toEqual('long_ashort_a');
});
it('should replace using both', () => {
expect(replacements.replaceAll('a aa a aa')).toEqual('short_a long_a short_a long_a');
expect(replacements.replaceAll('a aa a aa').newText).toEqual('short_a long_a short_a long_a');
});
it('should work with empty replacements', () => {
expect(new PrefixTree([]).replaceAll('Testing 123')).toEqual('Testing 123');
expect(new PrefixTree([]).replaceAll('Testing 123').newText).toEqual('Testing 123');
});
it('should leave unmatching text alone', () => {
expect(replacements.replaceAll('Some text with none of the first letter of the ordered letter list')).toEqual(
'Some text with none of the first letter of the ordered letter list',
);
expect(
replacements.replaceAll('Some text with none of the first letter of the ordered letter list').newText,
).toEqual('Some text with none of the first letter of the ordered letter list');
});
it('should handle a mixture', () => {
expect(replacements.replaceAll('Everyone loves an aardvark')).toEqual(
expect(replacements.replaceAll('Everyone loves an aardvark').newText).toEqual(
'Everyone loves short_an long_ardvshort_ark',
);
});
Expand Down

0 comments on commit 18a8218

Please sign in to comment.