Feature: Case-insensitive "find files in repo" (#21269)

This (short) PR builds upon #15028 and makes the file search
case-insensitive.

Previously, having a file named `TestFile.cs` would not be shown if
`test` was typed in the search box.
This now changes the matching function to be case-insensitive (without
affecting the UI).

The matching function, `strSubMatch`, is only used for this feature (it
has been introduced by #15028), meaning that this PR does not affect the
behaviour of any unrelated functionality of Gitea.
This commit is contained in:
HeySora 2022-09-28 00:55:15 +02:00 committed by GitHub
parent 8cd3237a9e
commit dabc06d13b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 13 deletions

View file

@ -64,19 +64,17 @@ export function parseIssueHref(href) {
export function strSubMatch(full, sub) { export function strSubMatch(full, sub) {
const res = ['']; const res = [''];
let i = 0, j = 0; let i = 0, j = 0;
while (i < sub.length && j < full.length) { const subLower = sub.toLowerCase(), fullLower = full.toLowerCase();
while (j < full.length) { while (i < subLower.length && j < fullLower.length) {
if (sub[i] === full[j]) { if (subLower[i] === fullLower[j]) {
if (res.length % 2 !== 0) res.push(''); if (res.length % 2 !== 0) res.push('');
res[res.length - 1] += full[j]; res[res.length - 1] += full[j];
j++; j++;
i++; i++;
} else { } else {
if (res.length % 2 === 0) res.push(''); if (res.length % 2 === 0) res.push('');
res[res.length - 1] += full[j]; res[res.length - 1] += full[j];
j++; j++;
break;
}
} }
} }
if (i !== sub.length) { if (i !== sub.length) {

View file

@ -95,6 +95,9 @@ test('strSubMatch', () => {
expect(strSubMatch('abc', 'z')).toEqual(['abc']); expect(strSubMatch('abc', 'z')).toEqual(['abc']);
expect(strSubMatch('abc', 'az')).toEqual(['abc']); expect(strSubMatch('abc', 'az')).toEqual(['abc']);
expect(strSubMatch('abc', 'aC')).toEqual(['', 'a', 'b', 'c']);
expect(strSubMatch('abC', 'ac')).toEqual(['', 'a', 'b', 'C']);
expect(strSubMatch('aabbcc', 'abc')).toEqual(['', 'a', 'a', 'b', 'b', 'c', 'c']); expect(strSubMatch('aabbcc', 'abc')).toEqual(['', 'a', 'a', 'b', 'b', 'c', 'c']);
expect(strSubMatch('the/directory', 'hedir')).toEqual(['t', 'he', '/', 'dir', 'ectory']); expect(strSubMatch('the/directory', 'hedir')).toEqual(['t', 'he', '/', 'dir', 'ectory']);
}); });