mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
AK: Move String::ends_with implementation to StringUtils
Centralizing so it can be used by other string implementations
This commit is contained in:
parent
d98e743568
commit
8e4b858b3f
Notes:
sideshowbarker
2024-07-19 06:07:44 +09:00
Author: https://github.com/bgianfo
Commit: 8e4b858b3f
Pull-request: https://github.com/SerenityOS/serenity/pull/2387
Reviewed-by: https://github.com/awesomekling
3 changed files with 15 additions and 7 deletions
|
@ -280,13 +280,7 @@ bool String::starts_with(char ch) const
|
||||||
|
|
||||||
bool String::ends_with(const StringView& str) const
|
bool String::ends_with(const StringView& str) const
|
||||||
{
|
{
|
||||||
if (str.is_empty())
|
return StringUtils::ends_with(*this, str);
|
||||||
return true;
|
|
||||||
if (is_empty())
|
|
||||||
return false;
|
|
||||||
if (str.length() > length())
|
|
||||||
return false;
|
|
||||||
return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool String::ends_with(char ch) const
|
bool String::ends_with(char ch) const
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Memory.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringUtils.h>
|
#include <AK/StringUtils.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
|
@ -195,6 +196,17 @@ bool equals_ignoring_case(const StringView& a, const StringView& b)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ends_with(const StringView& str, const StringView& end)
|
||||||
|
{
|
||||||
|
if (end.is_empty())
|
||||||
|
return true;
|
||||||
|
if (str.is_empty())
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,8 @@ int convert_to_int(const StringView&, bool& ok);
|
||||||
unsigned convert_to_uint(const StringView&, bool& ok);
|
unsigned convert_to_uint(const StringView&, bool& ok);
|
||||||
unsigned convert_to_uint_from_hex(const StringView&, bool& ok);
|
unsigned convert_to_uint_from_hex(const StringView&, bool& ok);
|
||||||
bool equals_ignoring_case(const StringView&, const StringView&);
|
bool equals_ignoring_case(const StringView&, const StringView&);
|
||||||
|
bool ends_with(const StringView& str, const StringView& end);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue