2020-03-02 13:23:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-02 13:23:11 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-26 07:25:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
enum class CaseSensitivity {
|
|
|
|
CaseInsensitive,
|
|
|
|
CaseSensitive,
|
|
|
|
};
|
|
|
|
|
2020-09-20 13:35:04 +00:00
|
|
|
enum class TrimMode {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Both
|
|
|
|
};
|
|
|
|
|
2021-06-18 16:21:27 +00:00
|
|
|
enum class TrimWhitespace {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2020-10-25 05:34:39 +00:00
|
|
|
struct MaskSpan {
|
|
|
|
size_t start;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
bool operator==(const MaskSpan& other) const
|
|
|
|
{
|
|
|
|
return start == other.start && length == other.length;
|
|
|
|
}
|
|
|
|
bool operator!=(const MaskSpan& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-26 07:25:24 +00:00
|
|
|
namespace StringUtils {
|
|
|
|
|
2020-10-25 05:34:39 +00:00
|
|
|
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive, Vector<MaskSpan>* match_spans = nullptr);
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T = int>
|
2021-06-18 16:21:27 +00:00
|
|
|
Optional<T> convert_to_int(const StringView&, TrimWhitespace = TrimWhitespace::Yes);
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T = unsigned>
|
2021-06-18 16:21:27 +00:00
|
|
|
Optional<T> convert_to_uint(const StringView&, TrimWhitespace = TrimWhitespace::Yes);
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T = unsigned>
|
2021-06-18 16:21:27 +00:00
|
|
|
Optional<T> convert_to_uint_from_hex(const StringView&, TrimWhitespace = TrimWhitespace::Yes);
|
2020-03-22 12:07:45 +00:00
|
|
|
bool equals_ignoring_case(const StringView&, const StringView&);
|
2020-05-26 09:12:18 +00:00
|
|
|
bool ends_with(const StringView& a, const StringView& b, CaseSensitivity);
|
2020-07-18 16:59:38 +00:00
|
|
|
bool starts_with(const StringView&, const StringView&, CaseSensitivity);
|
2020-10-20 21:07:03 +00:00
|
|
|
bool contains(const StringView&, const StringView&, CaseSensitivity);
|
2021-01-02 23:26:02 +00:00
|
|
|
bool is_whitespace(const StringView&);
|
2021-05-25 07:42:01 +00:00
|
|
|
StringView trim(const StringView& string, const StringView& characters, TrimMode mode);
|
|
|
|
StringView trim_whitespace(const StringView& string, TrimMode mode);
|
2021-07-01 12:58:37 +00:00
|
|
|
|
|
|
|
Optional<size_t> find(StringView const& haystack, char needle, size_t start = 0);
|
|
|
|
Optional<size_t> find(StringView const& haystack, StringView const& needle, size_t start = 0);
|
|
|
|
Optional<size_t> find_last(StringView const& haystack, char needle);
|
|
|
|
Vector<size_t> find_all(StringView const& haystack, StringView const& needle);
|
2021-07-01 16:12:21 +00:00
|
|
|
enum class SearchDirection {
|
|
|
|
Forward,
|
|
|
|
Backward
|
|
|
|
};
|
|
|
|
Optional<size_t> find_any_of(StringView const& haystack, StringView const& needles, SearchDirection);
|
2021-07-01 12:58:37 +00:00
|
|
|
|
2021-02-20 21:39:22 +00:00
|
|
|
String to_snakecase(const StringView&);
|
2021-08-26 17:55:41 +00:00
|
|
|
String to_titlecase(StringView const&);
|
2021-02-20 21:39:22 +00:00
|
|
|
|
2021-09-10 22:02:24 +00:00
|
|
|
size_t count(StringView const&, StringView const& needle);
|
|
|
|
|
2020-02-26 07:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::CaseSensitivity;
|
2020-09-20 13:35:04 +00:00
|
|
|
using AK::TrimMode;
|
2021-06-18 16:21:27 +00:00
|
|
|
using AK::TrimWhitespace;
|