Skip to content

Commit

Permalink
Centralize definitions of uppercase and lowercasing
Browse files Browse the repository at this point in the history
  • Loading branch information
nixprime committed May 6, 2015
1 parent 6ccaf70 commit 5a6c130
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ Matcher::Matcher(std::string query, MatcherOpts opts)
}
// Queries are smartcased (case-sensitive only if any uppercase appears in the
// query). Casing only applies to ASCII letters.
is_case_sensitive_ =
std::any_of(query_.begin(), query_.end(),
[](char32_t c) -> bool { return c >= 'A' && c <= 'Z'; });
is_case_sensitive_ = std::any_of(query_.begin(), query_.end(), is_uppercase);
cur_file_parts_= path_components_of(opts_.cur_file);
// Keeping the filename in cur_file_parts_ causes the path distance metric to
// favor the currently open file. While we don't want to exclude the
Expand Down Expand Up @@ -116,8 +114,8 @@ bool Matcher::append_match(boost::string_ref const item,
// The query must not contain any uppercase ASCII letters since otherwise
// the query would be case-sensitive.
for (char32_t& c : key_part_chars_) {
if (c >= 'A' && c <= 'Z') {
c += ('a' - 'A');
if (is_uppercase(c)) {
c = to_lowercase(c);
}
}
}
Expand Down Expand Up @@ -153,10 +151,12 @@ bool Matcher::append_match(boost::string_ref const item,
if (i == 0) {
return true;
}
if (is_alnum(key_part_chars_[i]) && !is_alnum(key_part_chars_[i-1])) {
if (is_alphanumeric(key_part_chars_[i]) &&
!is_alphanumeric(key_part_chars_[i - 1])) {
return true;
}
if (is_upcase(key_part_chars_[i]) && !is_upcase(key_part_chars_[i-1])) {
if (is_uppercase(key_part_chars_[i]) &&
!is_uppercase(key_part_chars_[i - 1])) {
return true;
}
return false;
Expand Down
8 changes: 6 additions & 2 deletions src/str_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ void decompose_utf8_string(boost::string_ref str,
}
}

bool is_alnum(char32_t const c) {
bool is_alphanumeric(char32_t const c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z');
}

bool is_upcase(char32_t const c) {
bool is_uppercase(char32_t const c) {
return c >= 'A' && c <= 'Z';
}

char32_t to_lowercase(char32_t c) {
return c + ('a' - 'A');
}

} // namespace cpsm
7 changes: 5 additions & 2 deletions src/str_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ inline std::string copy_string_ref(boost::string_ref const sref) {
void decompose_utf8_string(boost::string_ref str, std::vector<char32_t>& chars);

// Returns true if the given code point represents a letter or number.
bool is_alnum(char32_t c);
bool is_alphanumeric(char32_t c);

// Returns true if the given code point represents a uppercase letter.
bool is_upcase(char32_t c);
bool is_uppercase(char32_t c);

// Returns the lowercased version of c. c must be an uppercase letter.
char32_t to_lowercase(char32_t c);

} // namespace cpsm

Expand Down

0 comments on commit 5a6c130

Please sign in to comment.