Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions src/renderer/src/runtime/runtime-file-search-bounds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { SearchOptions, SearchResult } from '../../../shared/types'

import { measureUtf8ByteLength } from '../../../shared/utf8-byte-limits'

export const RUNTIME_FILE_SEARCH_TEXT_MAX_BYTES = 8 * 1024

export type RuntimeFileSearchRejectedField = 'query' | 'includePattern' | 'excludePattern'
Expand All @@ -8,35 +10,11 @@ export function createEmptyRuntimeFileSearchResult(): SearchResult {
return { files: [], totalMatches: 0, truncated: false }
}

function getCodePointUtf8ByteLength(codePoint: number): number {
if (codePoint <= 0x7f) {
return 1
}
if (codePoint <= 0x7ff) {
return 2
}
if (codePoint <= 0xffff) {
return 3
}
return 4
}

export function isRuntimeFileSearchTextWithinLimit(
text: string,
maxBytes = RUNTIME_FILE_SEARCH_TEXT_MAX_BYTES
): boolean {
let byteLength = 0
for (let index = 0; index < text.length; index += 1) {
const codePoint = text.codePointAt(index) ?? 0
byteLength += getCodePointUtf8ByteLength(codePoint)
if (byteLength > maxBytes) {
return false
}
if (codePoint > 0xffff) {
index += 1
}
}
return true
return !measureUtf8ByteLength(text, { stopAfterBytes: maxBytes }).exceededLimit
}

export function getRuntimeFileSearchRejectedField(
Expand Down
28 changes: 3 additions & 25 deletions src/renderer/src/runtime/runtime-provider-search-bounds.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
export const RUNTIME_PROVIDER_SEARCH_QUERY_MAX_BYTES = 8 * 1024
import { measureUtf8ByteLength } from '../../../shared/utf8-byte-limits'

function getCodePointUtf8ByteLength(codePoint: number): number {
if (codePoint <= 0x7f) {
return 1
}
if (codePoint <= 0x7ff) {
return 2
}
if (codePoint <= 0xffff) {
return 3
}
return 4
}
export const RUNTIME_PROVIDER_SEARCH_QUERY_MAX_BYTES = 8 * 1024

export function isRuntimeProviderSearchQueryWithinLimit(
query: string | null | undefined,
Expand All @@ -20,16 +9,5 @@ export function isRuntimeProviderSearchQueryWithinLimit(
if (query === null || query === undefined) {
return true
}
let byteLength = 0
for (let index = 0; index < query.length; index += 1) {
const codePoint = query.codePointAt(index) ?? 0
byteLength += getCodePointUtf8ByteLength(codePoint)
if (byteLength > maxBytes) {
return false
}
if (codePoint > 0xffff) {
index += 1
}
}
return true
return !measureUtf8ByteLength(query, { stopAfterBytes: maxBytes }).exceededLimit
}
28 changes: 3 additions & 25 deletions src/renderer/src/runtime/runtime-repo-search-bounds.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
export const RUNTIME_REPO_REF_SEARCH_QUERY_MAX_BYTES = 2 * 1024
import { measureUtf8ByteLength } from '../../../shared/utf8-byte-limits'

function getCodePointUtf8ByteLength(codePoint: number): number {
if (codePoint <= 0x7f) {
return 1
}
if (codePoint <= 0x7ff) {
return 2
}
if (codePoint <= 0xffff) {
return 3
}
return 4
}
export const RUNTIME_REPO_REF_SEARCH_QUERY_MAX_BYTES = 2 * 1024

export function isRuntimeRepoRefSearchQueryWithinLimit(
query: string,
maxBytes = RUNTIME_REPO_REF_SEARCH_QUERY_MAX_BYTES
): boolean {
let byteLength = 0
for (let index = 0; index < query.length; index += 1) {
const codePoint = query.codePointAt(index) ?? 0
byteLength += getCodePointUtf8ByteLength(codePoint)
if (byteLength > maxBytes) {
return false
}
if (codePoint > 0xffff) {
index += 1
}
}
return true
return !measureUtf8ByteLength(query, { stopAfterBytes: maxBytes }).exceededLimit
}