mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
AK: Add case insensitive String::ends_with support
FileSystemPath::has_extension was jumping through hoops and allocating memory to do a case insensitive comparison needlessly. Extend the existing String::ends_with method to allow the caller to specify the case sensitivity required.
This commit is contained in:
parent
8e4b858b3f
commit
332f96e7ca
7 changed files with 53 additions and 12 deletions
|
@ -196,7 +196,7 @@ bool equals_ignoring_case(const StringView& a, const StringView& b)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool ends_with(const StringView& str, const StringView& end)
|
||||
bool ends_with(const StringView& str, const StringView& end, CaseSensitivity case_sensitivity)
|
||||
{
|
||||
if (end.is_empty())
|
||||
return true;
|
||||
|
@ -204,7 +204,19 @@ bool ends_with(const StringView& str, const StringView& end)
|
|||
return false;
|
||||
if (end.length() > str.length())
|
||||
return false;
|
||||
return !memcmp(str.characters_without_null_termination() + (str.length() - end.length()), end.characters_without_null_termination(), end.length());
|
||||
|
||||
if (case_sensitivity == CaseSensitivity::CaseSensitive)
|
||||
return !memcmp(str.characters_without_null_termination() + (str.length() - end.length()), end.characters_without_null_termination(), end.length());
|
||||
|
||||
auto str_chars = str.characters_without_null_termination();
|
||||
auto end_chars = end.characters_without_null_termination();
|
||||
|
||||
size_t si = str.length() - end.length();
|
||||
for (size_t ei = 0; ei < end.length(); ++si, ++ei) {
|
||||
if (to_lowercase(str_chars[si]) != to_lowercase(end_chars[ei]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue