Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
Kate
Commits
387b0088
Commit
387b0088
authored
Jan 03, 2021
by
Waqar Ahmed
Browse files
Use QStringView instead of QChar*s and make sure we stay in range
parent
c7b32006
Changes
2
Hide whitespace changes
Inline
Side-by-side
kate/katequickopen.cpp
View file @
387b0088
...
...
@@ -46,22 +46,27 @@ public:
{}
protected:
bool
lessThan
(
const
QModelIndex
&
source
_l
eft
,
const
QModelIndex
&
source
_r
ight
)
const
override
bool
lessThan
(
const
QModelIndex
&
source
L
eft
,
const
QModelIndex
&
source
R
ight
)
const
override
{
int
l
=
source
_l
eft
.
data
(
KateQuickOpenModel
::
Score
).
toInt
();
int
r
=
source
_r
ight
.
data
(
KateQuickOpenModel
::
Score
).
toInt
();
int
l
=
source
L
eft
.
data
(
KateQuickOpenModel
::
Score
).
toInt
();
int
r
=
source
R
ight
.
data
(
KateQuickOpenModel
::
Score
).
toInt
();
return
l
<
r
;
}
bool
filterAcceptsRow
(
int
sourceRow
,
const
QModelIndex
&
sourceParent
)
const
override
{
if
(
filterStrings
.
isEmpty
())
if
(
pattern
.
isEmpty
())
return
true
;
const
QString
fileName
=
sourceModel
()
->
index
(
sourceRow
,
0
,
sourceParent
).
data
().
toString
();
int
score
;
auto
res
=
kfts
::
fuzzy_match
(
filterStrings
.
constData
(),
fileName
.
constData
(),
score
);
// match
int
score
=
0
;
auto
res
=
kfts
::
fuzzy_match
(
pattern
,
fileName
,
score
);
// store the score for sorting later
auto
idx
=
sourceModel
()
->
index
(
sourceRow
,
0
,
sourceParent
);
sourceModel
()
->
setData
(
idx
,
score
,
KateQuickOpenModel
::
Score
);
return
res
;
}
...
...
@@ -69,12 +74,12 @@ public Q_SLOTS:
void
setFilterText
(
const
QString
&
text
)
{
beginResetModel
();
filterStrings
=
text
;
pattern
=
text
;
endResetModel
();
}
private:
QString
filterStrings
;
QString
pattern
;
};
class
QuickOpenStyleDelegate
:
public
QStyledItemDelegate
{
...
...
kate/kfts_fuzzy_match.h
View file @
387b0088
...
...
@@ -16,65 +16,70 @@
*/
namespace
kfts
{
static
bool
fuzzy_match_simple
(
const
Q
Char
*
pattern
,
const
Q
Char
*
str
);
static
bool
fuzzy_match
(
const
Q
Char
*
pattern
,
const
Q
Char
*
str
,
int
&
outScore
);
static
bool
fuzzy_match
(
const
Q
Char
*
pattern
,
const
Q
Char
*
str
,
int
&
outScore
,
uint8_t
*
matches
,
int
maxMatches
);
static
bool
fuzzy_match_simple
(
const
Q
StringView
pattern
,
const
Q
StringView
str
);
static
bool
fuzzy_match
(
const
Q
StringView
pattern
,
const
Q
StringView
str
,
int
&
outScore
);
static
bool
fuzzy_match
(
const
Q
StringView
pattern
,
const
Q
StringView
str
,
int
&
outScore
,
uint8_t
*
matches
,
int
maxMatches
);
}
namespace
kfts
{
// Forward declarations for "private" implementation
namespace
fuzzy_internal
{
static
bool
fuzzy_match_recursive
(
const
QChar
*
pattern
,
const
QChar
*
str
,
int
&
outScore
,
const
QChar
*
strBegin
,
static
bool
fuzzy_match_recursive
(
QStringView
::
const_iterator
pattern
,
QStringView
::
const_iterator
str
,
int
&
outScore
,
const
QStringView
::
const_iterator
strBegin
,
const
QStringView
::
const_iterator
strEnd
,
const
QStringView
::
const_iterator
patternEnd
,
uint8_t
const
*
srcMatches
,
uint8_t
*
newMatches
,
int
maxMatches
,
int
nextMatch
,
int
&
recursionCount
,
int
recursionLimit
);
int
&
recursionCount
);
}
// Public interface
static
bool
fuzzy_match_simple
(
const
Q
Char
*
pattern
,
const
Q
Char
*
str
)
static
bool
fuzzy_match_simple
(
const
Q
StringView
pattern
,
const
Q
StringView
str
)
{
while
(
!
pattern
->
isNull
()
&&
!
str
->
isNull
())
{
if
(
pattern
->
toLower
()
==
str
->
toLower
())
++
pattern
;
++
str
;
auto
patternIt
=
pattern
.
cbegin
();
for
(
auto
strIt
=
str
.
cbegin
();
strIt
!=
str
.
cend
()
&&
patternIt
!=
pattern
.
cend
();
++
strIt
)
{
if
(
strIt
->
toLower
()
==
patternIt
->
toLower
())
++
patternIt
;
}
return
pattern
->
isNull
()
?
true
:
false
;
return
patternIt
==
pattern
.
cend
();
}
static
bool
fuzzy_match
(
const
Q
Char
*
pattern
,
const
Q
Char
*
str
,
int
&
outScore
)
static
bool
fuzzy_match
(
const
Q
StringView
pattern
,
const
Q
StringView
str
,
int
&
outScore
)
{
uint8_t
matches
[
3
2
];
uint8_t
matches
[
2
56
];
return
fuzzy_match
(
pattern
,
str
,
outScore
,
matches
,
sizeof
(
matches
));
}
static
bool
fuzzy_match
(
const
Q
Char
*
pattern
,
const
Q
Char
*
str
,
int
&
outScore
,
uint8_t
*
matches
,
int
maxMatches
)
static
bool
fuzzy_match
(
const
Q
StringView
pattern
,
const
Q
StringView
str
,
int
&
outScore
,
uint8_t
*
matches
,
int
maxMatches
)
{
int
recursionCount
=
0
;
int
recursionLimit
=
10
;
return
fuzzy_internal
::
fuzzy_match_recursive
(
pattern
,
str
,
outScore
,
str
,
nullptr
,
matches
,
maxMatches
,
0
,
recursionCount
,
recursionLimit
);
auto
strIt
=
str
.
cbegin
();
auto
patternIt
=
pattern
.
cbegin
();
const
auto
patternEnd
=
pattern
.
cend
();
const
auto
strEnd
=
str
.
cend
();
return
fuzzy_internal
::
fuzzy_match_recursive
(
patternIt
,
strIt
,
outScore
,
strIt
,
strEnd
,
patternEnd
,
nullptr
,
matches
,
maxMatches
,
0
,
recursionCount
);
}
// Private implementation
static
bool
fuzzy_internal
::
fuzzy_match_recursive
(
const
QChar
*
pattern
,
const
QChar
*
str
,
int
&
outScore
,
const
QChar
*
strBegin
,
uint8_t
const
*
srcMatches
,
uint8_t
*
matches
,
static
bool
fuzzy_internal
::
fuzzy_match_recursive
(
QStringView
::
const_iterator
pattern
,
QStringView
::
const_iterator
str
,
int
&
outScore
,
const
QStringView
::
const_iterator
strBegin
,
const
QStringView
::
const_iterator
strEnd
,
const
QStringView
::
const_iterator
patternEnd
,
const
uint8_t
*
srcMatches
,
uint8_t
*
matches
,
int
maxMatches
,
int
nextMatch
,
int
&
recursionCount
,
int
recursionLimit
)
int
&
recursionCount
)
{
// Count recursions
static
constexpr
int
recursionLimit
=
10
;
++
recursionCount
;
if
(
recursionCount
>=
recursionLimit
)
return
false
;
// Detect end of strings
if
(
pattern
->
isNull
()
||
str
->
isNull
()
)
if
(
pattern
==
patternEnd
||
str
==
strEnd
)
return
false
;
// Recursion params
...
...
@@ -84,7 +89,7 @@ namespace kfts {
// Loop through pattern and str looking for a match
bool
first_match
=
true
;
while
(
!
pattern
->
isNull
()
&&
!
str
->
isNull
()
)
{
while
(
pattern
!=
patternEnd
&&
str
!=
strEnd
)
{
// Found match
if
(
pattern
->
toLower
()
==
str
->
toLower
())
{
...
...
@@ -102,7 +107,8 @@ namespace kfts {
// Recursive call that "skips" this match
uint8_t
recursiveMatches
[
256
];
int
recursiveScore
;
if
(
fuzzy_match_recursive
(
pattern
,
str
+
1
,
recursiveScore
,
strBegin
,
matches
,
recursiveMatches
,
sizeof
(
recursiveMatches
),
nextMatch
,
recursionCount
,
recursionLimit
))
{
auto
strNextChar
=
std
::
next
(
str
);
if
(
fuzzy_match_recursive
(
pattern
,
strNextChar
,
recursiveScore
,
strBegin
,
strEnd
,
patternEnd
,
matches
,
recursiveMatches
,
sizeof
(
recursiveMatches
),
nextMatch
,
recursionCount
))
{
// Pick best recursive score
if
(
!
recursiveMatch
||
recursiveScore
>
bestRecursiveScore
)
{
...
...
@@ -113,14 +119,14 @@ namespace kfts {
}
// Advance
matches
[
nextMatch
++
]
=
(
uint8_t
)(
st
r
-
strBegin
);
matches
[
nextMatch
++
]
=
(
uint8_t
)(
st
d
::
distance
(
strBegin
,
str
)
);
++
pattern
;
}
++
str
;
}
// Determine if full pattern was matched
bool
matched
=
pattern
->
isNull
()
?
true
:
false
;
bool
matched
=
pattern
==
patternEnd
?
true
:
false
;
// Calculate score
if
(
matched
)
{
...
...
@@ -134,7 +140,7 @@ namespace kfts {
static
constexpr
int
unmatched_letter_penalty
=
-
1
;
// penalty for every letter that doesn't matter
// Iterate str to end
while
(
!
str
->
isNull
()
)
while
(
str
!=
strEnd
)
++
str
;
// Initialize score
...
...
@@ -147,7 +153,7 @@ namespace kfts {
outScore
+=
penalty
;
// Apply unmatched penalty
int
unmatched
=
(
int
)(
st
r
-
strBegin
)
-
nextMatch
;
const
int
unmatched
=
(
int
)(
st
d
::
distance
(
strBegin
,
str
)
)
-
nextMatch
;
outScore
+=
unmatched_letter_penalty
*
unmatched
;
// Apply ordering bonuses
...
...
@@ -165,8 +171,8 @@ namespace kfts {
// Check for bonuses based on neighbor character value
if
(
currIdx
>
0
)
{
// Camel case
QChar
neighbor
=
strBegin
[
currIdx
-
1
]
;
QChar
curr
=
strBegin
[
currIdx
]
;
QChar
neighbor
=
*
(
strBegin
+
currIdx
-
1
)
;
QChar
curr
=
*
(
strBegin
+
currIdx
)
;
if
(
neighbor
.
isLower
()
&&
curr
.
isUpper
())
outScore
+=
camel_bonus
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment