Skip to content

Commit

Permalink
fix: regex in binary files by using ascii for regex matches
Browse files Browse the repository at this point in the history
Fixes #406
  • Loading branch information
connor4312 committed Feb 2, 2024
1 parent 1e59a5c commit 19364d7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/searchRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HexDocument } from "./hexDocument";
import { LiteralSearchQuery, RegExpSearchQuery, SearchResult, SearchResultsWithProgress } from "../shared/protocol";
import { Disposable } from "vscode";
import { utf8Length } from "./util";
import { caseInsensitiveEquivalency, LiteralSearch, Wildcard } from "./literalSearch";
import { LiteralSearchQuery, RegExpSearchQuery, SearchResult, SearchResultsWithProgress } from "../shared/protocol";
import { Uint8ArrayMap } from "../shared/util/uint8ArrayMap";
import { HexDocument } from "./hexDocument";
import { LiteralSearch, Wildcard, caseInsensitiveEquivalency } from "./literalSearch";

/** Type that defines a search request created from the {@link SearchProvider} */
export interface ISearchRequest extends Disposable {
Expand Down Expand Up @@ -148,7 +147,7 @@ export class RegexSearchRequest implements ISearchRequest {
let strStart = 0;

const { re, document } = this;
const decoder = new TextDecoder();
const decoder = new TextDecoder("ascii");
const encoder = new TextEncoder();
const collector = new ResultsCollector(await document.size(), this.cap);

Expand All @@ -162,8 +161,8 @@ export class RegexSearchRequest implements ISearchRequest {

let lastReIndex = 0;
for (const match of str.matchAll(re)) {
const start = strStart + utf8Length(str.slice(0, match.index!));
const length = utf8Length(match[0]);
const start = strStart + str.slice(0, match.index!).length;
const length = match[0].length;
collector.push(encoder.encode(match[0]), start, start + length);
lastReIndex = match.index! + match[0].length;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getRangeSelectionsFromStack, Range } from "../../shared/util/range";

describe("Range", () => {

describe.only("getRangeSelectionsFromStack", () => {
describe("getRangeSelectionsFromStack", () => {
it("works for a single", () => {
expect(getRangeSelectionsFromStack([new Range(10, 20)])).to.deep.equal([new Range(10, 20)]);
});
Expand Down

0 comments on commit 19364d7

Please sign in to comment.