Skip to content

Commit 5b55939

Browse files
committed
chore: fix narrow conversions in getExtensionScore
1 parent 378959d commit 5b55939

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/path_scorer.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ constexpr Score file_coeff = 2.5;
1313

1414
extern Score scorePath(const CandidateString &subject, const CandidateString &subject_lw, Score fullPathScore, const Options &options);
1515

16-
extern Score getExtensionScore(const CandidateString &candidate, const CandidateString &ext, int startPos, int endPos, int maxDepth);
16+
extern Score getExtensionScore(const CandidateString &candidate, const CandidateString &ext, const int startPos, const int endPos, const int maxDepth);
1717

1818
Element ToLower(const Element &s) {
1919
Element /* copy */ snew = s;
@@ -141,7 +141,8 @@ CandidateString getExtension(const CandidateString &str) {
141141
}
142142

143143

144-
Score getExtensionScore(const CandidateString &candidate, const CandidateString &ext, int startPos, int endPos, int maxDepth) {
144+
Score getExtensionScore(const CandidateString &candidate, const CandidateString &ext, const int startPos, const int endPos, const int maxDepth) {
145+
// TODO make startPos and endPos size_t and m, n, pos auto
145146
// startPos is the position of last slash of candidate, -1 if absent.
146147

147148
if (ext.empty()) {
@@ -150,6 +151,7 @@ Score getExtensionScore(const CandidateString &candidate, const CandidateString
150151

151152
// Check that (a) extension exist, (b) it is after the start of the basename
152153
int pos = candidate.rfind('.', endPos);
154+
// assert(pos >= 0u);
153155
if (pos <= startPos) {
154156
return 0;// (note that startPos >= -1)
155157
}
@@ -165,18 +167,21 @@ Score getExtensionScore(const CandidateString &candidate, const CandidateString
165167

166168
//place cursor after dot & count number of matching characters in extension
167169
pos++;
168-
auto matched = -1;
169-
while (++matched < n) {
170-
if (candidate[pos + matched] != ext[matched]) {
170+
// assert(pos >= 1u);
171+
auto matched = 0;
172+
while (matched < n) {
173+
// assert(matched >=0); // fuzz: if n==0, does not enter while and matched==0
174+
if (candidate[pos + matched] != ext[matched]) {// TODO candidate upper bound
171175
break;
172176
}
177+
++matched;
173178
}
174179

175180
// if nothing found, try deeper for multiple extensions, with some penalty for depth
176-
if (matched == 0 && maxDepth > 0) {
177-
return 0.9 * getExtensionScore(candidate, ext, startPos, pos - 2, maxDepth - 1);
181+
if (matched == 0u && maxDepth > 0) {
182+
return 0.9f * getExtensionScore(candidate, ext, startPos, pos - 2, maxDepth - 1);
178183
}
179184

180185
// cannot divide by zero because m is the largest extension length and we return if either is 0
181-
return static_cast<Score>(matched) / m;
186+
return static_cast<Score>(matched) / static_cast<Score>(m);
182187
}

0 commit comments

Comments
 (0)