2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-04-16 23:18:39 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-14 22:48:01 +00:00
|
|
|
#include <AK/AnyOf.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2024-06-17 22:12:53 +00:00
|
|
|
#include <AK/ByteString.h>
|
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2021-01-14 22:48:01 +00:00
|
|
|
#include <AK/Find.h>
|
2024-06-17 22:12:53 +00:00
|
|
|
#include <AK/FlyString.h>
|
2021-11-10 10:05:21 +00:00
|
|
|
#include <AK/Function.h>
|
2024-06-17 22:12:53 +00:00
|
|
|
#include <AK/String.h>
|
2022-09-12 14:31:16 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/StringView.h>
|
2021-11-10 10:05:21 +00:00
|
|
|
#include <AK/Vector.h>
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2022-12-06 20:39:11 +00:00
|
|
|
StringView::StringView(String const& string)
|
2023-01-28 13:26:22 +00:00
|
|
|
: m_characters(reinterpret_cast<char const*>(string.bytes().data()))
|
|
|
|
, m_length(string.bytes().size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView::StringView(FlyString const& string)
|
2022-12-06 20:39:11 +00:00
|
|
|
: m_characters(reinterpret_cast<char const*>(string.bytes().data()))
|
|
|
|
, m_length(string.bytes().size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
StringView::StringView(ByteString const& string)
|
2021-04-16 23:18:39 +00:00
|
|
|
: m_characters(string.characters())
|
2019-06-02 10:19:21 +00:00
|
|
|
, m_length(string.length())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
StringView::StringView(DeprecatedFlyString const& string)
|
2021-04-16 23:18:39 +00:00
|
|
|
: m_characters(string.characters())
|
2020-03-22 09:12:55 +00:00
|
|
|
, m_length(string.length())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
StringView::StringView(ByteBuffer const& buffer)
|
|
|
|
: m_characters((char const*)buffer.data())
|
2020-03-01 11:35:09 +00:00
|
|
|
, m_length(buffer.size())
|
2019-06-29 10:03:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:38:21 +00:00
|
|
|
Vector<StringView> StringView::split_view(char const separator, SplitBehavior split_behavior) const
|
2019-04-16 00:39:16 +00:00
|
|
|
{
|
2022-01-09 09:51:02 +00:00
|
|
|
StringView seperator_view { &separator, 1 };
|
2022-10-22 13:38:21 +00:00
|
|
|
return split_view(seperator_view, split_behavior);
|
2019-04-16 00:39:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 13:38:21 +00:00
|
|
|
Vector<StringView> StringView::split_view(StringView separator, SplitBehavior split_behavior) const
|
2020-05-27 20:21:43 +00:00
|
|
|
{
|
|
|
|
Vector<StringView> parts;
|
2022-10-22 13:38:21 +00:00
|
|
|
for_each_split_view(separator, split_behavior, [&](StringView view) {
|
2020-05-27 20:21:43 +00:00
|
|
|
parts.append(view);
|
2022-01-09 10:26:45 +00:00
|
|
|
});
|
2020-05-27 20:21:43 +00:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2024-03-08 14:57:35 +00:00
|
|
|
template<typename Callback>
|
|
|
|
static void for_each_line(StringView string, Callback&& callback)
|
2019-12-02 12:42:33 +00:00
|
|
|
{
|
2024-03-08 14:57:35 +00:00
|
|
|
char const* characters = string.characters_without_null_termination();
|
2019-12-02 12:42:33 +00:00
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t substart = 0;
|
2019-12-02 12:42:33 +00:00
|
|
|
bool last_ch_was_cr = false;
|
2024-03-08 14:57:35 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < string.length(); ++i) {
|
|
|
|
char ch = characters[i];
|
|
|
|
bool split_view = false;
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case '\n':
|
|
|
|
if (last_ch_was_cr)
|
2019-12-02 12:42:33 +00:00
|
|
|
substart = i + 1;
|
2024-03-08 14:57:35 +00:00
|
|
|
else
|
|
|
|
split_view = true;
|
|
|
|
|
|
|
|
last_ch_was_cr = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
2019-12-02 12:42:33 +00:00
|
|
|
split_view = true;
|
|
|
|
last_ch_was_cr = true;
|
2024-03-08 14:57:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-06-01 11:17:04 +00:00
|
|
|
last_ch_was_cr = false;
|
2024-03-08 14:57:35 +00:00
|
|
|
break;
|
2019-12-02 12:42:33 +00:00
|
|
|
}
|
2024-03-08 14:57:35 +00:00
|
|
|
|
2019-12-02 12:42:33 +00:00
|
|
|
if (split_view) {
|
2024-03-08 14:57:35 +00:00
|
|
|
callback(string.substring_view(substart, i - substart));
|
2019-12-02 12:42:33 +00:00
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 14:57:35 +00:00
|
|
|
|
|
|
|
if (size_t taillen = string.length() - substart; taillen != 0)
|
|
|
|
callback(string.substring_view(substart, taillen));
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<StringView> StringView::lines(ConsiderCarriageReturn consider_carriage_return) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (consider_carriage_return == ConsiderCarriageReturn::No)
|
|
|
|
return split_view('\n', SplitBehavior::KeepEmpty);
|
|
|
|
|
|
|
|
Vector<StringView> lines;
|
|
|
|
for_each_line(*this, [&](auto line) { lines.append(line); });
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StringView::count_lines(ConsiderCarriageReturn consider_carriage_return) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (consider_carriage_return == ConsiderCarriageReturn::No)
|
|
|
|
return count('\n') + 1;
|
|
|
|
|
|
|
|
size_t lines = 0;
|
|
|
|
for_each_line(*this, [&](auto) { ++lines; });
|
|
|
|
|
|
|
|
return lines;
|
2019-12-02 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 21:14:22 +00:00
|
|
|
bool StringView::starts_with(char ch) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return false;
|
|
|
|
return ch == characters_without_null_termination()[0];
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool StringView::starts_with(StringView str, CaseSensitivity case_sensitivity) const
|
2019-09-12 11:13:07 +00:00
|
|
|
{
|
2020-07-18 16:59:38 +00:00
|
|
|
return StringUtils::starts_with(*this, str, case_sensitivity);
|
2019-09-12 11:13:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 21:14:22 +00:00
|
|
|
bool StringView::ends_with(char ch) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return false;
|
|
|
|
return ch == characters_without_null_termination()[length() - 1];
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool StringView::ends_with(StringView str, CaseSensitivity case_sensitivity) const
|
2019-12-29 12:44:30 +00:00
|
|
|
{
|
2020-05-26 10:21:34 +00:00
|
|
|
return StringUtils::ends_with(*this, str, case_sensitivity);
|
2019-12-29 12:44:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool StringView::matches(StringView mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
|
2020-10-25 05:34:39 +00:00
|
|
|
{
|
|
|
|
return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool StringView::matches(StringView mask, CaseSensitivity case_sensitivity) const
|
2020-02-26 07:25:24 +00:00
|
|
|
{
|
|
|
|
return StringUtils::matches(*this, mask, case_sensitivity);
|
|
|
|
}
|
|
|
|
|
2020-04-17 13:04:40 +00:00
|
|
|
bool StringView::contains(char needle) const
|
|
|
|
{
|
|
|
|
for (char current : *this) {
|
|
|
|
if (current == needle)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-12 14:31:16 +00:00
|
|
|
bool StringView::contains(u32 needle) const
|
|
|
|
{
|
|
|
|
// A code point should be at most four UTF-8 bytes, which easily fits into StringBuilder's inline-buffer.
|
|
|
|
// Therefore, this will not allocate.
|
|
|
|
StringBuilder needle_builder;
|
|
|
|
auto result = needle_builder.try_append_code_point(needle);
|
|
|
|
if (result.is_error()) {
|
|
|
|
// The needle is invalid, therefore the string does not contain it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return contains(needle_builder.string_view());
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool StringView::contains(StringView needle, CaseSensitivity case_sensitivity) const
|
2020-07-04 18:04:00 +00:00
|
|
|
{
|
2023-12-29 16:22:27 +00:00
|
|
|
if (needle.length() == 1)
|
|
|
|
return contains(needle.characters_without_null_termination()[0]);
|
2020-10-20 21:07:03 +00:00
|
|
|
return StringUtils::contains(*this, needle, case_sensitivity);
|
2020-07-04 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 07:48:54 +00:00
|
|
|
bool StringView::equals_ignoring_ascii_case(StringView other) const
|
2020-05-13 16:59:31 +00:00
|
|
|
{
|
2023-03-10 07:48:54 +00:00
|
|
|
return StringUtils::equals_ignoring_ascii_case(*this, other);
|
2020-05-13 16:59:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString StringView::to_lowercase_string() const
|
2021-07-01 11:45:59 +00:00
|
|
|
{
|
2023-10-10 11:30:58 +00:00
|
|
|
return StringImpl::create_lowercased(characters_without_null_termination(), length()).release_nonnull();
|
2021-07-01 11:45:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString StringView::to_uppercase_string() const
|
2021-07-01 11:45:59 +00:00
|
|
|
{
|
2023-10-10 11:30:58 +00:00
|
|
|
return StringImpl::create_uppercased(characters_without_null_termination(), length()).release_nonnull();
|
2021-07-01 11:45:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString StringView::to_titlecase_string() const
|
2021-08-26 17:55:41 +00:00
|
|
|
{
|
|
|
|
return StringUtils::to_titlecase(*this);
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
StringView StringView::substring_view_starting_from_substring(StringView substring) const
|
2019-06-13 13:30:55 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* remaining_characters = substring.characters_without_null_termination();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(remaining_characters >= m_characters);
|
|
|
|
VERIFY(remaining_characters <= m_characters + m_length);
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t remaining_length = m_length - (remaining_characters - m_characters);
|
2019-06-13 13:30:55 +00:00
|
|
|
return { remaining_characters, remaining_length };
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
StringView StringView::substring_view_starting_after_substring(StringView substring) const
|
2019-06-13 13:30:55 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* remaining_characters = substring.characters_without_null_termination() + substring.length();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(remaining_characters >= m_characters);
|
|
|
|
VERIFY(remaining_characters <= m_characters + m_length);
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t remaining_length = m_length - (remaining_characters - m_characters);
|
2019-06-13 13:30:55 +00:00
|
|
|
return { remaining_characters, remaining_length };
|
|
|
|
}
|
2022-03-29 00:52:20 +00:00
|
|
|
|
|
|
|
bool StringView::copy_characters_to_buffer(char* buffer, size_t buffer_size) const
|
|
|
|
{
|
|
|
|
// We must fit at least the NUL-terminator.
|
|
|
|
VERIFY(buffer_size > 0);
|
|
|
|
|
|
|
|
size_t characters_to_copy = min(m_length, buffer_size - 1);
|
|
|
|
__builtin_memcpy(buffer, m_characters, characters_to_copy);
|
|
|
|
buffer[characters_to_copy] = 0;
|
|
|
|
|
|
|
|
return characters_to_copy == m_length;
|
|
|
|
}
|
2019-06-13 13:30:55 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
bool StringView::operator==(ByteString const& string) const
|
2020-03-23 12:45:10 +00:00
|
|
|
{
|
2022-01-29 14:49:33 +00:00
|
|
|
return *this == string.view();
|
2020-03-23 12:45:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString StringView::to_byte_string() const { return ByteString { *this }; }
|
2020-05-06 16:53:05 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString StringView::replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const
|
2021-09-10 23:15:44 +00:00
|
|
|
{
|
2022-07-05 20:33:15 +00:00
|
|
|
return StringUtils::replace(*this, needle, replacement, replace_mode);
|
2021-09-10 23:15:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:05:21 +00:00
|
|
|
Vector<size_t> StringView::find_all(StringView needle) const
|
|
|
|
{
|
|
|
|
return StringUtils::find_all(*this, needle);
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:38:21 +00:00
|
|
|
Vector<StringView> StringView::split_view_if(Function<bool(char)> const& predicate, SplitBehavior split_behavior) const
|
2021-11-10 10:05:21 +00:00
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Vector<StringView> v;
|
|
|
|
size_t substart = 0;
|
2022-10-22 13:38:21 +00:00
|
|
|
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
|
2022-10-22 14:31:59 +00:00
|
|
|
bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
|
2021-11-10 10:05:21 +00:00
|
|
|
for (size_t i = 0; i < length(); ++i) {
|
|
|
|
char ch = characters_without_null_termination()[i];
|
|
|
|
if (predicate(ch)) {
|
|
|
|
size_t sublen = i - substart;
|
|
|
|
if (sublen != 0 || keep_empty)
|
2022-10-22 14:31:59 +00:00
|
|
|
v.append(substring_view(substart, keep_separator ? sublen + 1 : sublen));
|
2021-11-10 10:05:21 +00:00
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t taillen = length() - substart;
|
|
|
|
if (taillen != 0 || keep_empty)
|
|
|
|
v.append(substring_view(substart, taillen));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
}
|