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>
|
2021-01-14 22:48:01 +00:00
|
|
|
#include <AK/Find.h>
|
2020-04-17 13:04:40 +00:00
|
|
|
#include <AK/FlyString.h>
|
2021-11-10 10:05:21 +00:00
|
|
|
#include <AK/Function.h>
|
2020-03-08 11:34:33 +00:00
|
|
|
#include <AK/Memory.h>
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.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 {
|
|
|
|
|
2019-06-08 21:55:13 +00:00
|
|
|
StringView::StringView(const String& 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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:12:55 +00:00
|
|
|
StringView::StringView(const FlyString& 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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-29 10:03:28 +00:00
|
|
|
StringView::StringView(const ByteBuffer& buffer)
|
|
|
|
: m_characters((const char*)buffer.data())
|
2020-03-01 11:35:09 +00:00
|
|
|
, m_length(buffer.size())
|
2019-06-29 10:03:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:43:37 +00:00
|
|
|
Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
|
2019-04-16 00:39:16 +00:00
|
|
|
{
|
|
|
|
if (is_empty())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
Vector<StringView> v;
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t substart = 0;
|
|
|
|
for (size_t i = 0; i < length(); ++i) {
|
2019-07-08 13:38:44 +00:00
|
|
|
char ch = characters_without_null_termination()[i];
|
2019-04-16 00:39:16 +00:00
|
|
|
if (ch == separator) {
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t sublen = i - substart;
|
2019-09-20 21:43:37 +00:00
|
|
|
if (sublen != 0 || keep_empty)
|
2019-04-16 00:39:16 +00:00
|
|
|
v.append(substring_view(substart, sublen));
|
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t taillen = length() - substart;
|
2019-09-20 21:43:37 +00:00
|
|
|
if (taillen != 0 || keep_empty)
|
2019-04-16 00:39:16 +00:00
|
|
|
v.append(substring_view(substart, taillen));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
Vector<StringView> StringView::split_view(StringView separator, bool keep_empty) const
|
2020-05-27 20:21:43 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!separator.is_empty());
|
2020-05-27 20:21:43 +00:00
|
|
|
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
StringView view { *this };
|
|
|
|
|
|
|
|
Vector<StringView> parts;
|
|
|
|
|
2021-01-12 20:00:44 +00:00
|
|
|
auto maybe_separator_index = find(separator);
|
2020-05-27 20:21:43 +00:00
|
|
|
while (maybe_separator_index.has_value()) {
|
|
|
|
auto separator_index = maybe_separator_index.value();
|
|
|
|
auto part_with_separator = view.substring_view(0, separator_index + separator.length());
|
|
|
|
if (keep_empty || separator_index > 0)
|
|
|
|
parts.append(part_with_separator.substring_view(0, separator_index));
|
|
|
|
view = view.substring_view_starting_after_substring(part_with_separator);
|
2021-01-12 20:00:44 +00:00
|
|
|
maybe_separator_index = view.find(separator);
|
2020-05-27 20:21:43 +00:00
|
|
|
}
|
|
|
|
if (keep_empty || !view.is_empty())
|
|
|
|
parts.append(view);
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-12-02 12:42:33 +00:00
|
|
|
Vector<StringView> StringView::lines(bool consider_cr) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (!consider_cr)
|
|
|
|
return split_view('\n', true);
|
|
|
|
|
|
|
|
Vector<StringView> v;
|
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;
|
|
|
|
bool split_view = false;
|
2019-12-09 16:45:40 +00:00
|
|
|
for (size_t i = 0; i < length(); ++i) {
|
2019-12-02 12:42:33 +00:00
|
|
|
char ch = characters_without_null_termination()[i];
|
|
|
|
if (ch == '\n') {
|
|
|
|
split_view = true;
|
|
|
|
if (last_ch_was_cr) {
|
|
|
|
substart = i + 1;
|
|
|
|
split_view = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch == '\r') {
|
|
|
|
split_view = true;
|
|
|
|
last_ch_was_cr = true;
|
2021-06-01 11:17:04 +00:00
|
|
|
} else {
|
|
|
|
last_ch_was_cr = false;
|
2019-12-02 12:42:33 +00:00
|
|
|
}
|
|
|
|
if (split_view) {
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t sublen = i - substart;
|
2019-12-02 19:41:15 +00:00
|
|
|
v.append(substring_view(substart, sublen));
|
2019-12-02 12:42:33 +00:00
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
split_view = false;
|
|
|
|
}
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t taillen = length() - substart;
|
2019-12-02 12:42:33 +00:00
|
|
|
if (taillen != 0)
|
|
|
|
v.append(substring_view(substart, taillen));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-10-20 21:07:03 +00:00
|
|
|
return StringUtils::contains(*this, needle, case_sensitivity);
|
2020-07-04 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool StringView::equals_ignoring_case(StringView other) const
|
2020-05-13 16:59:31 +00:00
|
|
|
{
|
|
|
|
return StringUtils::equals_ignoring_case(*this, other);
|
|
|
|
}
|
|
|
|
|
2021-07-01 11:45:59 +00:00
|
|
|
String StringView::to_lowercase_string() const
|
|
|
|
{
|
|
|
|
return StringImpl::create_lowercased(characters_without_null_termination(), length());
|
|
|
|
}
|
|
|
|
|
|
|
|
String StringView::to_uppercase_string() const
|
|
|
|
{
|
|
|
|
return StringImpl::create_uppercased(characters_without_null_termination(), length());
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:55:41 +00:00
|
|
|
String StringView::to_titlecase_string() const
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2019-07-08 13:38:44 +00:00
|
|
|
const char* 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
|
|
|
{
|
2019-07-08 13:38:44 +00:00
|
|
|
const char* 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 };
|
|
|
|
}
|
|
|
|
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T>
|
|
|
|
Optional<T> StringView::to_int() const
|
2019-08-04 09:44:20 +00:00
|
|
|
{
|
2020-12-10 13:17:30 +00:00
|
|
|
return StringUtils::convert_to_int<T>(*this);
|
2019-08-04 09:44:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:17:30 +00:00
|
|
|
template Optional<i8> StringView::to_int() const;
|
|
|
|
template Optional<i16> StringView::to_int() const;
|
|
|
|
template Optional<i32> StringView::to_int() const;
|
2021-10-01 07:00:54 +00:00
|
|
|
template Optional<long> StringView::to_int() const;
|
|
|
|
template Optional<long long> StringView::to_int() const;
|
2020-12-10 13:17:30 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Optional<T> StringView::to_uint() const
|
2019-04-16 00:39:16 +00:00
|
|
|
{
|
2020-12-10 13:17:30 +00:00
|
|
|
return StringUtils::convert_to_uint<T>(*this);
|
2019-04-16 00:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:17:30 +00:00
|
|
|
template Optional<u8> StringView::to_uint() const;
|
|
|
|
template Optional<u16> StringView::to_uint() const;
|
|
|
|
template Optional<u32> StringView::to_uint() const;
|
2021-10-01 07:00:54 +00:00
|
|
|
template Optional<unsigned long> StringView::to_uint() const;
|
|
|
|
template Optional<unsigned long long> StringView::to_uint() const;
|
2021-01-11 18:09:22 +00:00
|
|
|
template Optional<long> StringView::to_uint() const;
|
|
|
|
template Optional<long long> StringView::to_uint() const;
|
2020-12-10 13:17:30 +00:00
|
|
|
|
2020-03-23 12:45:10 +00:00
|
|
|
bool StringView::operator==(const String& string) const
|
|
|
|
{
|
|
|
|
if (string.is_null())
|
|
|
|
return !m_characters;
|
|
|
|
if (!m_characters)
|
|
|
|
return false;
|
|
|
|
if (m_length != string.length())
|
|
|
|
return false;
|
|
|
|
if (m_characters == string.characters())
|
|
|
|
return true;
|
|
|
|
return !__builtin_memcmp(m_characters, string.characters(), m_length);
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:53:05 +00:00
|
|
|
String StringView::to_string() const { return String { *this }; }
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
String StringView::replace(StringView needle, StringView replacement, bool all_occurrences) const
|
2021-09-10 23:15:44 +00:00
|
|
|
{
|
|
|
|
return StringUtils::replace(*this, needle, replacement, all_occurrences);
|
|
|
|
}
|
|
|
|
|
2021-11-10 10:05:21 +00:00
|
|
|
Vector<size_t> StringView::find_all(StringView needle) const
|
|
|
|
{
|
|
|
|
return StringUtils::find_all(*this, needle);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<StringView> StringView::split_view_if(Function<bool(char)> const& predicate, bool keep_empty) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Vector<StringView> v;
|
|
|
|
size_t substart = 0;
|
|
|
|
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)
|
|
|
|
v.append(substring_view(substart, sublen));
|
|
|
|
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
|
|
|
}
|