1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 07:34:57 +00:00

AK: Add String{,View}::is_whitespace()

+Tests!
This commit is contained in:
AnotherTest 2021-01-03 02:56:02 +03:30 committed by Andreas Kling
parent b795f582cf
commit f3ecea1fb3
5 changed files with 28 additions and 11 deletions

View file

@ -31,6 +31,7 @@
#include <AK/StringUtils.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <ctype.h>
namespace AK {
@ -302,17 +303,17 @@ bool contains(const StringView& str, const StringView& needle, CaseSensitivity c
return false;
}
bool is_whitespace(const StringView& str)
{
for (auto ch : str) {
if (!isspace(ch))
return false;
}
return true;
}
StringView trim_whitespace(const StringView& str, TrimMode mode)
{
auto is_whitespace_character = [](char ch) -> bool {
return ch == '\t'
|| ch == '\n'
|| ch == '\v'
|| ch == '\f'
|| ch == '\r'
|| ch == ' ';
};
size_t substring_start = 0;
size_t substring_length = str.length();
@ -320,7 +321,7 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
for (size_t i = 0; i < str.length(); ++i) {
if (substring_length == 0)
return "";
if (!is_whitespace_character(str[i]))
if (!isspace(str[i]))
break;
++substring_start;
--substring_length;
@ -331,7 +332,7 @@ StringView trim_whitespace(const StringView& str, TrimMode mode)
for (size_t i = str.length() - 1; i > 0; --i) {
if (substring_length == 0)
return "";
if (!is_whitespace_character(str[i]))
if (!isspace(str[i]))
break;
--substring_length;
}