mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Remove various unused things
This commit is contained in:
parent
5b4d071daa
commit
6321e97b09
Notes:
sideshowbarker
2024-07-17 04:57:23 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/LadybirdBrowser/ladybird/commit/6321e97b09 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/37
20 changed files with 0 additions and 995 deletions
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Span.h>
|
||||
#include <AK/StdLibExtraDetails.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
class BinaryBufferWriter {
|
||||
public:
|
||||
BinaryBufferWriter(Bytes target)
|
||||
: m_target(target)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires(IsTriviallyConstructible<T>) T& append_structure()
|
||||
{
|
||||
VERIFY((reinterpret_cast<FlatPtr>(m_target.data()) + m_offset) % alignof(T) == 0);
|
||||
VERIFY(m_offset + sizeof(T) <= m_target.size());
|
||||
T* allocated = new (m_target.data() + m_offset) T;
|
||||
m_offset += sizeof(T);
|
||||
return *allocated;
|
||||
}
|
||||
|
||||
void skip_bytes(size_t num_bytes)
|
||||
{
|
||||
VERIFY(m_offset + num_bytes <= m_target.size());
|
||||
m_offset += num_bytes;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t current_offset() const
|
||||
{
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
private:
|
||||
Bytes m_target;
|
||||
size_t m_offset { 0 };
|
||||
};
|
||||
|
||||
}
|
|
@ -11,7 +11,6 @@ set(AK_SOURCES
|
|||
FloatingPointStringConversions.cpp
|
||||
FlyString.cpp
|
||||
Format.cpp
|
||||
FuzzyMatch.cpp
|
||||
GenericLexer.cpp
|
||||
Hex.cpp
|
||||
JsonObject.cpp
|
||||
|
@ -24,7 +23,6 @@ set(AK_SOURCES
|
|||
OptionParser.cpp
|
||||
Random.cpp
|
||||
SipHash.cpp
|
||||
Slugify.cpp
|
||||
StackInfo.cpp
|
||||
Stream.cpp
|
||||
String.cpp
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/CircularQueue.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T, size_t Capacity>
|
||||
class CircularDeque : public CircularQueue<T, Capacity> {
|
||||
public:
|
||||
template<typename U = T>
|
||||
void enqueue_begin(U&& value)
|
||||
{
|
||||
auto const new_head = (this->m_head - 1 + Capacity) % Capacity;
|
||||
auto& slot = this->elements()[new_head];
|
||||
if (this->m_size == Capacity)
|
||||
slot.~T();
|
||||
else
|
||||
++this->m_size;
|
||||
|
||||
new (&slot) T(forward<U>(value));
|
||||
this->m_head = new_head;
|
||||
}
|
||||
|
||||
T dequeue_end()
|
||||
{
|
||||
VERIFY(!this->is_empty());
|
||||
auto& slot = this->elements()[(this->m_head + this->m_size - 1) % Capacity];
|
||||
T value = move(slot);
|
||||
slot.~T();
|
||||
this->m_size--;
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::CircularDeque;
|
||||
#endif
|
124
AK/FPControl.h
124
AK/FPControl.h
|
@ -1,124 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Leon Albrecht <leon.a@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
|
||||
// FIXME: Add equivalent datastructures for aarch64
|
||||
VALIDATE_IS_X86();
|
||||
|
||||
namespace AK {
|
||||
|
||||
enum class RoundingMode : u16 {
|
||||
NEAREST = 0b00,
|
||||
DOWN = 0b01,
|
||||
UP = 0b10,
|
||||
TRUNC = 0b11
|
||||
};
|
||||
|
||||
union X87ControlWord {
|
||||
u16 cw;
|
||||
struct {
|
||||
u16 mask_invalid : 1; // IM
|
||||
u16 mask_denorm : 1; // DM
|
||||
u16 mask_zero_div : 1; // ZM
|
||||
u16 mask_overflow : 1; // OM
|
||||
u16 mask_underflow : 1; // UM
|
||||
u16 mask_precision : 1; // PM
|
||||
u16 : 2; // unused
|
||||
u16 precision : 2; // PC
|
||||
RoundingMode rounding_control : 2; // RC
|
||||
u16 infinity_control : 1; // X
|
||||
u16 : 3; // unused
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(X87ControlWord) == sizeof(u16));
|
||||
|
||||
union MXCSR {
|
||||
u32 mxcsr;
|
||||
struct {
|
||||
u32 invalid_operation_flag : 1; // IE
|
||||
u32 denormal_operation_flag : 1; // DE
|
||||
u32 divide_by_zero_flag : 1; // ZE
|
||||
u32 overflow_flag : 1; // OE
|
||||
u32 underflow_flag : 1; // UE
|
||||
u32 precision_flag : 1; // PE
|
||||
u32 denormals_are_zero : 1; // DAZ
|
||||
u32 invalid_operation_mask : 1; // IM
|
||||
u32 denormal_operation_mask : 1; // DM
|
||||
u32 divide_by_zero_mask : 1; // ZM
|
||||
u32 overflow_mask : 1; // OM
|
||||
u32 underflow_mask : 1; // UM
|
||||
u32 precision_mask : 1; // PM
|
||||
RoundingMode rounding_control : 2; // RC
|
||||
u32 flush_to_zero : 1; // FTZ
|
||||
u32 __reserved : 16;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(MXCSR) == sizeof(u32));
|
||||
|
||||
ALWAYS_INLINE X87ControlWord get_cw_x87()
|
||||
{
|
||||
X87ControlWord control_word;
|
||||
asm("fnstcw %0"
|
||||
: "=m"(control_word));
|
||||
return control_word;
|
||||
}
|
||||
ALWAYS_INLINE void set_cw_x87(X87ControlWord control_word)
|
||||
{
|
||||
asm("fldcw %0" ::"m"(control_word));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE MXCSR get_mxcsr()
|
||||
{
|
||||
MXCSR mxcsr;
|
||||
asm("stmxcsr %0"
|
||||
: "=m"(mxcsr));
|
||||
return mxcsr;
|
||||
}
|
||||
ALWAYS_INLINE void set_mxcsr(MXCSR mxcsr)
|
||||
{
|
||||
asm("ldmxcsr %0" ::"m"(mxcsr));
|
||||
}
|
||||
|
||||
class X87RoundingModeScope {
|
||||
public:
|
||||
X87RoundingModeScope(RoundingMode rounding_mode)
|
||||
{
|
||||
m_cw = get_cw_x87();
|
||||
auto cw = m_cw;
|
||||
cw.rounding_control = rounding_mode;
|
||||
set_cw_x87(cw);
|
||||
}
|
||||
~X87RoundingModeScope()
|
||||
{
|
||||
set_cw_x87(m_cw);
|
||||
}
|
||||
|
||||
private:
|
||||
X87ControlWord m_cw;
|
||||
};
|
||||
|
||||
class SSERoundingModeScope {
|
||||
public:
|
||||
SSERoundingModeScope(RoundingMode rounding_mode)
|
||||
{
|
||||
m_mxcsr = get_mxcsr();
|
||||
auto mxcsr = m_mxcsr;
|
||||
mxcsr.rounding_control = rounding_mode;
|
||||
set_mxcsr(mxcsr);
|
||||
}
|
||||
~SSERoundingModeScope()
|
||||
{
|
||||
set_mxcsr(m_mxcsr);
|
||||
}
|
||||
|
||||
private:
|
||||
MXCSR m_mxcsr;
|
||||
};
|
||||
|
||||
}
|
|
@ -1,136 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/FuzzyMatch.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
static constexpr int const RECURSION_LIMIT = 10;
|
||||
static constexpr int const MAX_MATCHES = 256;
|
||||
|
||||
// Bonuses and penalties are used to build up a final score for the match.
|
||||
static constexpr int const SEQUENTIAL_BONUS = 15; // bonus for adjacent matches (needle: 'ca', haystack: 'cat')
|
||||
static constexpr int const SEPARATOR_BONUS = 30; // bonus if match occurs after a separator ('_' or ' ')
|
||||
static constexpr int const CAMEL_BONUS = 30; // bonus if match is uppercase and prev is lower (needle: 'myF' haystack: '/path/to/myFile.txt')
|
||||
static constexpr int const FIRST_LETTER_BONUS = 15; // bonus if the first letter is matched (needle: 'c' haystack: 'cat')
|
||||
static constexpr int const LEADING_LETTER_PENALTY = -5; // penalty applied for every letter in str before the first match
|
||||
static constexpr int const MAX_LEADING_LETTER_PENALTY = -15; // maximum penalty for leading letters
|
||||
static constexpr int const UNMATCHED_LETTER_PENALTY = -1; // penalty for every letter that doesn't matter
|
||||
|
||||
static int calculate_score(StringView string, u8* index_points, size_t index_points_size)
|
||||
{
|
||||
int out_score = 100;
|
||||
|
||||
int penalty = LEADING_LETTER_PENALTY * index_points[0];
|
||||
if (penalty < MAX_LEADING_LETTER_PENALTY)
|
||||
penalty = MAX_LEADING_LETTER_PENALTY;
|
||||
out_score += penalty;
|
||||
|
||||
int unmatched = string.length() - index_points_size;
|
||||
out_score += UNMATCHED_LETTER_PENALTY * unmatched;
|
||||
|
||||
for (size_t i = 0; i < index_points_size; i++) {
|
||||
u8 current_idx = index_points[i];
|
||||
|
||||
if (i > 0) {
|
||||
u8 previous_idx = index_points[i - 1];
|
||||
if (current_idx - 1 == previous_idx)
|
||||
out_score += SEQUENTIAL_BONUS;
|
||||
}
|
||||
|
||||
if (current_idx == 0) {
|
||||
out_score += FIRST_LETTER_BONUS;
|
||||
} else {
|
||||
u32 current_character = string[current_idx];
|
||||
u32 neighbor_character = string[current_idx - 1];
|
||||
|
||||
if (is_ascii_lower_alpha(neighbor_character) && is_ascii_upper_alpha(current_character))
|
||||
out_score += CAMEL_BONUS;
|
||||
|
||||
if (neighbor_character == '_' || neighbor_character == ' ')
|
||||
out_score += SEPARATOR_BONUS;
|
||||
}
|
||||
}
|
||||
|
||||
return out_score;
|
||||
}
|
||||
|
||||
static FuzzyMatchResult fuzzy_match_recursive(StringView needle, StringView haystack, size_t needle_idx, size_t haystack_idx,
|
||||
u8 const* src_matches, u8* matches, int next_match, int& recursion_count)
|
||||
{
|
||||
int out_score = 0;
|
||||
|
||||
++recursion_count;
|
||||
if (recursion_count >= RECURSION_LIMIT)
|
||||
return { false, out_score };
|
||||
|
||||
if (needle.length() == needle_idx || haystack.length() == haystack_idx)
|
||||
return { false, out_score };
|
||||
|
||||
bool had_recursive_match = false;
|
||||
constexpr size_t recursive_match_limit = 256;
|
||||
u8 best_recursive_matches[recursive_match_limit];
|
||||
int best_recursive_score = 0;
|
||||
|
||||
bool first_match = true;
|
||||
while (needle_idx < needle.length() && haystack_idx < haystack.length()) {
|
||||
|
||||
if (to_ascii_lowercase(needle[needle_idx]) == to_ascii_lowercase(haystack[haystack_idx])) {
|
||||
if (next_match >= MAX_MATCHES)
|
||||
return { false, out_score };
|
||||
|
||||
if (first_match && src_matches) {
|
||||
memcpy(matches, src_matches, next_match);
|
||||
first_match = false;
|
||||
}
|
||||
|
||||
u8 recursive_matches[recursive_match_limit] {};
|
||||
auto result = fuzzy_match_recursive(needle, haystack, needle_idx, haystack_idx + 1, matches, recursive_matches, next_match, recursion_count);
|
||||
if (result.matched) {
|
||||
if (!had_recursive_match || result.score > best_recursive_score) {
|
||||
memcpy(best_recursive_matches, recursive_matches, recursive_match_limit);
|
||||
best_recursive_score = result.score;
|
||||
}
|
||||
had_recursive_match = true;
|
||||
}
|
||||
matches[next_match++] = haystack_idx;
|
||||
needle_idx++;
|
||||
}
|
||||
haystack_idx++;
|
||||
}
|
||||
|
||||
bool matched = needle_idx == needle.length();
|
||||
if (!matched)
|
||||
return { false, out_score };
|
||||
|
||||
out_score = calculate_score(haystack, matches, next_match);
|
||||
|
||||
if (had_recursive_match && (best_recursive_score > out_score)) {
|
||||
memcpy(matches, best_recursive_matches, MAX_MATCHES);
|
||||
out_score = best_recursive_score;
|
||||
}
|
||||
|
||||
return { true, out_score };
|
||||
}
|
||||
|
||||
// This fuzzy_match algorithm is based off a similar algorithm used by Sublime Text. The key insight is that instead
|
||||
// of doing a total in the distance between characters (I.E. Levenshtein Distance), we apply some meaningful heuristics
|
||||
// related to our dataset that we're trying to match to build up a score. Scores can then be sorted and displayed
|
||||
// with the highest at the top.
|
||||
//
|
||||
// Scores are not normalized between any values and have no particular meaning. The starting value is 100 and when we
|
||||
// detect good indicators of a match we add to the score. When we detect bad indicators, we penalize the match and subtract
|
||||
// from its score. Therefore, the longer the needle/haystack the greater the range of scores could be.
|
||||
FuzzyMatchResult fuzzy_match(StringView needle, StringView haystack)
|
||||
{
|
||||
int recursion_count = 0;
|
||||
u8 matches[MAX_MATCHES] {};
|
||||
return fuzzy_match_recursive(needle, haystack, 0, 0, nullptr, matches, 0, recursion_count);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
struct FuzzyMatchResult {
|
||||
bool matched { false };
|
||||
int score { 0 };
|
||||
};
|
||||
|
||||
FuzzyMatchResult fuzzy_match(StringView needle, StringView haystack);
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::fuzzy_match;
|
||||
using AK::FuzzyMatchResult;
|
||||
#endif
|
118
AK/MACAddress.h
118
AK/MACAddress.h
|
@ -1,118 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/AllOf.h>
|
||||
#include <AK/Array.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Library/KString.h>
|
||||
#else
|
||||
# include <AK/ByteString.h>
|
||||
#endif
|
||||
|
||||
class [[gnu::packed]] MACAddress {
|
||||
static constexpr size_t s_mac_address_length = 6u;
|
||||
|
||||
public:
|
||||
constexpr MACAddress() = default;
|
||||
|
||||
constexpr MACAddress(u8 a, u8 b, u8 c, u8 d, u8 e, u8 f)
|
||||
{
|
||||
m_data[0] = a;
|
||||
m_data[1] = b;
|
||||
m_data[2] = c;
|
||||
m_data[3] = d;
|
||||
m_data[4] = e;
|
||||
m_data[5] = f;
|
||||
}
|
||||
|
||||
constexpr ~MACAddress() = default;
|
||||
|
||||
constexpr u8 const& operator[](unsigned i) const
|
||||
{
|
||||
VERIFY(i < s_mac_address_length);
|
||||
return m_data[i];
|
||||
}
|
||||
|
||||
constexpr u8& operator[](unsigned i)
|
||||
{
|
||||
VERIFY(i < s_mac_address_length);
|
||||
return m_data[i];
|
||||
}
|
||||
|
||||
constexpr bool operator==(MACAddress const& other) const
|
||||
{
|
||||
for (auto i = 0u; i < m_data.size(); ++i) {
|
||||
if (m_data[i] != other.m_data[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef KERNEL
|
||||
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
|
||||
{
|
||||
return Kernel::KString::formatted("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]);
|
||||
}
|
||||
#else
|
||||
ByteString to_byte_string() const
|
||||
{
|
||||
return ByteString::formatted("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static Optional<MACAddress> from_string(StringView string)
|
||||
{
|
||||
if (string.is_null())
|
||||
return {};
|
||||
|
||||
auto const parts = string.split_view(':');
|
||||
if (parts.size() != 6)
|
||||
return {};
|
||||
|
||||
auto a = AK::StringUtils::convert_to_uint_from_hex(parts[0]).value_or(256);
|
||||
auto b = AK::StringUtils::convert_to_uint_from_hex(parts[1]).value_or(256);
|
||||
auto c = AK::StringUtils::convert_to_uint_from_hex(parts[2]).value_or(256);
|
||||
auto d = AK::StringUtils::convert_to_uint_from_hex(parts[3]).value_or(256);
|
||||
auto e = AK::StringUtils::convert_to_uint_from_hex(parts[4]).value_or(256);
|
||||
auto f = AK::StringUtils::convert_to_uint_from_hex(parts[5]).value_or(256);
|
||||
|
||||
if (a > 255 || b > 255 || c > 255 || d > 255 || e > 255 || f > 255)
|
||||
return {};
|
||||
|
||||
return MACAddress(a, b, c, d, e, f);
|
||||
}
|
||||
|
||||
constexpr bool is_zero() const
|
||||
{
|
||||
return all_of(m_data, [](auto const octet) { return octet == 0; });
|
||||
}
|
||||
|
||||
void copy_to(Bytes destination) const
|
||||
{
|
||||
m_data.span().copy_to(destination);
|
||||
}
|
||||
|
||||
private:
|
||||
Array<u8, s_mac_address_length> m_data {};
|
||||
};
|
||||
|
||||
static_assert(sizeof(MACAddress) == 6u);
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Traits<MACAddress> : public DefaultTraits<MACAddress> {
|
||||
static unsigned hash(MACAddress const& address) { return string_hash((char const*)&address, sizeof(address)); }
|
||||
};
|
||||
|
||||
}
|
61
AK/Ptr32.h
61
AK/Ptr32.h
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class Ptr32 {
|
||||
public:
|
||||
constexpr Ptr32() = default;
|
||||
Ptr32(T* const ptr)
|
||||
: m_ptr((u32) reinterpret_cast<FlatPtr>(ptr))
|
||||
{
|
||||
VERIFY((reinterpret_cast<FlatPtr>(ptr) & 0xFFFFFFFFULL) == static_cast<FlatPtr>(m_ptr));
|
||||
}
|
||||
T& operator*() { return *static_cast<T*>(*this); }
|
||||
T const& operator*() const { return *static_cast<T const*>(*this); }
|
||||
|
||||
T* operator->() { return *this; }
|
||||
T const* operator->() const { return *this; }
|
||||
|
||||
operator T*() { return reinterpret_cast<T*>(static_cast<FlatPtr>(m_ptr)); }
|
||||
operator T const*() const { return reinterpret_cast<T const*>(static_cast<FlatPtr>(m_ptr)); }
|
||||
|
||||
T& operator[](size_t index) { return static_cast<T*>(*this)[index]; }
|
||||
T const& operator[](size_t index) const { return static_cast<T const*>(*this)[index]; }
|
||||
|
||||
constexpr explicit operator bool() { return m_ptr; }
|
||||
template<typename U>
|
||||
constexpr bool operator==(Ptr32<U> other) { return m_ptr == other.m_ptr; }
|
||||
|
||||
constexpr Ptr32<T> operator+(u32 other) const
|
||||
{
|
||||
Ptr32<T> ptr {};
|
||||
ptr.m_ptr = m_ptr + other;
|
||||
return ptr;
|
||||
}
|
||||
constexpr Ptr32<T> operator-(u32 other) const
|
||||
{
|
||||
Ptr32<T> ptr {};
|
||||
ptr.m_ptr = m_ptr - other;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
u32 m_ptr { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::Ptr32;
|
||||
#endif
|
|
@ -1,34 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class RefCountForwarder {
|
||||
public:
|
||||
void ref() { m_ref_count_target.ref(); }
|
||||
void unref() { m_ref_count_target.unref(); }
|
||||
|
||||
T& ref_count_target() { return m_ref_count_target; }
|
||||
T const& ref_count_target() const { return m_ref_count_target; }
|
||||
|
||||
protected:
|
||||
RefCountForwarder(T& target)
|
||||
: m_ref_count_target(target)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
T& m_ref_count_target;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::RefCountForwarder;
|
||||
#endif
|
36
AK/SetOnce.h
36
AK/SetOnce.h
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
class SetOnce {
|
||||
AK_MAKE_NONCOPYABLE(SetOnce);
|
||||
AK_MAKE_NONMOVABLE(SetOnce);
|
||||
|
||||
public:
|
||||
SetOnce() = default;
|
||||
|
||||
void set()
|
||||
{
|
||||
m_value = true;
|
||||
}
|
||||
|
||||
bool was_set() const { return m_value; }
|
||||
|
||||
private:
|
||||
bool m_value { false };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::SetOnce;
|
||||
#endif
|
|
@ -1,33 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Gurkirat Singh <tbhaxor@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/Slugify.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace AK {
|
||||
ErrorOr<String> slugify(String const& input, char const glue)
|
||||
{
|
||||
StringBuilder sb;
|
||||
bool just_processed_space = false;
|
||||
|
||||
for (auto const& code_point : input.code_points()) {
|
||||
if (is_ascii_alphanumeric(code_point)) {
|
||||
sb.append_code_point(to_ascii_lowercase(code_point));
|
||||
just_processed_space = false;
|
||||
} else if ((code_point == static_cast<u32>(glue) || is_ascii_space(code_point)) && !just_processed_space) {
|
||||
sb.append_code_point(glue);
|
||||
just_processed_space = true;
|
||||
}
|
||||
}
|
||||
|
||||
auto output = TRY(sb.to_string());
|
||||
if (output.ends_with(static_cast<u32>(glue))) {
|
||||
return output.trim(StringView { &glue, 1 }, TrimMode::Right);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
17
AK/Slugify.h
17
AK/Slugify.h
|
@ -1,17 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Gurkirat Singh <tbhaxor@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
|
||||
namespace AK {
|
||||
ErrorOr<String> slugify(String const& input, char glue = '-');
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::slugify;
|
||||
#endif
|
|
@ -27,7 +27,6 @@ shared_library("AK") {
|
|||
"Base64.cpp",
|
||||
"Base64.h",
|
||||
"BigIntBase.h",
|
||||
"BinaryBufferWriter.h",
|
||||
"BinaryHeap.h",
|
||||
"BinarySearch.h",
|
||||
"BitCast.h",
|
||||
|
@ -46,7 +45,6 @@ shared_library("AK") {
|
|||
"CheckedFormatString.h",
|
||||
"CircularBuffer.cpp",
|
||||
"CircularBuffer.h",
|
||||
"CircularDeque.h",
|
||||
"CircularQueue.h",
|
||||
"Complex.h",
|
||||
"Concepts.h",
|
||||
|
@ -69,7 +67,6 @@ shared_library("AK") {
|
|||
"EnumBits.h",
|
||||
"Error.cpp",
|
||||
"Error.h",
|
||||
"FPControl.h",
|
||||
"Find.h",
|
||||
"FixedArray.h",
|
||||
"FixedPoint.h",
|
||||
|
@ -82,8 +79,6 @@ shared_library("AK") {
|
|||
"Format.h",
|
||||
"Forward.h",
|
||||
"Function.h",
|
||||
"FuzzyMatch.cpp",
|
||||
"FuzzyMatch.h",
|
||||
"GenericLexer.cpp",
|
||||
"GenericLexer.h",
|
||||
"GenericShorthands.h",
|
||||
|
@ -117,7 +112,6 @@ shared_library("AK") {
|
|||
"LEB128.h",
|
||||
"LexicalPath.cpp",
|
||||
"LexicalPath.h",
|
||||
"MACAddress.h",
|
||||
"Math.h",
|
||||
"MaybeOwned.h",
|
||||
"MemMem.h",
|
||||
|
@ -146,7 +140,6 @@ shared_library("AK") {
|
|||
"Random.h",
|
||||
"RecursionDecision.h",
|
||||
"RedBlackTree.h",
|
||||
"RefCountForwarder.h",
|
||||
"RefCounted.h",
|
||||
"RefPtr.h",
|
||||
"Result.h",
|
||||
|
@ -162,8 +155,6 @@ shared_library("AK") {
|
|||
"SinglyLinkedListSizePolicy.h",
|
||||
"SipHash.cpp",
|
||||
"SipHash.h",
|
||||
"Slugify.cpp",
|
||||
"Slugify.h",
|
||||
"SourceGenerator.h",
|
||||
"SourceLocation.h",
|
||||
"Span.h",
|
||||
|
|
|
@ -18,7 +18,6 @@ tests = [
|
|||
"TestCharacterTypes",
|
||||
"TestChecked",
|
||||
"TestCircularBuffer",
|
||||
"TestCircularDeque",
|
||||
"TestCircularQueue",
|
||||
"TestComplex",
|
||||
"TestByteString",
|
||||
|
@ -50,7 +49,6 @@ tests = [
|
|||
"TestJSON",
|
||||
"TestLEB128",
|
||||
"TestLexicalPath",
|
||||
"TestMACAddress",
|
||||
"TestMemory",
|
||||
"TestMemoryStream",
|
||||
"TestNeverDestroyed",
|
||||
|
|
|
@ -17,7 +17,6 @@ set(AK_TEST_SOURCES
|
|||
TestCharacterTypes.cpp
|
||||
TestChecked.cpp
|
||||
TestCircularBuffer.cpp
|
||||
TestCircularDeque.cpp
|
||||
TestCircularQueue.cpp
|
||||
TestComplex.cpp
|
||||
TestDisjointChunks.cpp
|
||||
|
@ -33,7 +32,6 @@ set(AK_TEST_SOURCES
|
|||
TestFloatingPointParsing.cpp
|
||||
TestFlyString.cpp
|
||||
TestFormat.cpp
|
||||
TestFuzzyMatch.cpp
|
||||
TestGenericLexer.cpp
|
||||
TestHashFunctions.cpp
|
||||
TestHashMap.cpp
|
||||
|
@ -49,7 +47,6 @@ set(AK_TEST_SOURCES
|
|||
TestJSON.cpp
|
||||
TestLEB128.cpp
|
||||
TestLexicalPath.cpp
|
||||
TestMACAddress.cpp
|
||||
TestMemory.cpp
|
||||
TestMemoryStream.cpp
|
||||
TestNeverDestroyed.cpp
|
||||
|
@ -68,7 +65,6 @@ set(AK_TEST_SOURCES
|
|||
TestSegmentedVector.cpp
|
||||
TestSIMD.cpp
|
||||
TestSinglyLinkedList.cpp
|
||||
TestSlugify.cpp
|
||||
TestSourceGenerator.cpp
|
||||
TestSourceLocation.cpp
|
||||
TestSpan.cpp
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/ByteString.h>
|
||||
#include <AK/CircularDeque.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
TEST_CASE(enqueue_begin)
|
||||
{
|
||||
CircularDeque<int, 3> ints;
|
||||
|
||||
ints.enqueue_begin(0);
|
||||
EXPECT_EQ(ints.size(), 1u);
|
||||
EXPECT_EQ(ints.first(), 0);
|
||||
|
||||
ints.enqueue_begin(1);
|
||||
EXPECT_EQ(ints.size(), 2u);
|
||||
EXPECT_EQ(ints.first(), 1);
|
||||
EXPECT_EQ(ints.last(), 0);
|
||||
|
||||
ints.enqueue_begin(2);
|
||||
EXPECT_EQ(ints.size(), 3u);
|
||||
EXPECT_EQ(ints.first(), 2);
|
||||
EXPECT_EQ(ints.last(), 0);
|
||||
|
||||
ints.enqueue_begin(3);
|
||||
EXPECT_EQ(ints.size(), 3u);
|
||||
EXPECT_EQ(ints.first(), 3);
|
||||
EXPECT_EQ(ints.at(1), 2);
|
||||
EXPECT_EQ(ints.last(), 1);
|
||||
}
|
||||
|
||||
TEST_CASE(enqueue_begin_being_moved_from)
|
||||
{
|
||||
CircularDeque<ByteString, 2> strings;
|
||||
|
||||
ByteString str { "test" };
|
||||
strings.enqueue_begin(move(str));
|
||||
EXPECT(str.is_empty());
|
||||
}
|
||||
|
||||
TEST_CASE(deque_end)
|
||||
{
|
||||
CircularDeque<int, 3> ints;
|
||||
ints.enqueue(0);
|
||||
ints.enqueue(1);
|
||||
ints.enqueue(2);
|
||||
EXPECT_EQ(ints.size(), 3u);
|
||||
|
||||
EXPECT_EQ(ints.dequeue_end(), 2);
|
||||
EXPECT_EQ(ints.size(), 2u);
|
||||
|
||||
EXPECT_EQ(ints.dequeue_end(), 1);
|
||||
EXPECT_EQ(ints.size(), 1u);
|
||||
|
||||
EXPECT_EQ(ints.dequeue_end(), 0);
|
||||
EXPECT(ints.is_empty());
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/FuzzyMatch.h>
|
||||
|
||||
TEST_CASE(is_leading_letter_penalty_correctly_applied)
|
||||
{
|
||||
// Leading penalty is -5 points for each initial unmatched letter up to a maximum of -15.
|
||||
EXPECT_EQ(fuzzy_match("b"sv, "ab"sv).score, 94);
|
||||
EXPECT_EQ(fuzzy_match("c"sv, "abc"sv).score, 88);
|
||||
EXPECT_EQ(fuzzy_match("d"sv, "abcd"sv).score, 82);
|
||||
EXPECT_EQ(fuzzy_match("e"sv, "abcde"sv).score, 81);
|
||||
}
|
||||
|
||||
TEST_CASE(is_first_letter_bonus_applied_correctly)
|
||||
{
|
||||
// First letter bonus is +15 if the first letter matches.
|
||||
EXPECT_EQ(fuzzy_match("a"sv, "a"sv).score, 115);
|
||||
EXPECT_EQ(fuzzy_match("a"sv, "A"sv).score, 115);
|
||||
EXPECT_EQ(fuzzy_match(" "sv, " "sv).score, 115);
|
||||
}
|
||||
|
||||
TEST_CASE(is_sequential_bonus_applied_correctly)
|
||||
{
|
||||
// Sequential bonus is +15 for each sequential match.
|
||||
EXPECT_EQ(fuzzy_match("bc"sv, "abc"sv).score, 109);
|
||||
EXPECT_EQ(fuzzy_match("bcd"sv, "ab-cd"sv).score, 108);
|
||||
EXPECT_EQ(fuzzy_match("bcd"sv, "abcd"sv).score, 124);
|
||||
EXPECT_EQ(fuzzy_match("bcde"sv, "ab-cde"sv).score, 123);
|
||||
EXPECT_EQ(fuzzy_match("bcde"sv, "abcde"sv).score, 139);
|
||||
EXPECT_EQ(fuzzy_match("bcde"sv, "abcdef"sv).score, 138);
|
||||
}
|
||||
|
||||
TEST_CASE(is_camel_case_bonus_applied_correctly)
|
||||
{
|
||||
// Camel case bonus is +30 if the matching character is uppercase and the preceding character is lowercase.
|
||||
// These cases get no camel case bonus.
|
||||
EXPECT_EQ(fuzzy_match("b"sv, "Ab"sv).score, 94);
|
||||
EXPECT_EQ(fuzzy_match("abc"sv, "ABc"sv).score, 145);
|
||||
EXPECT_EQ(fuzzy_match("abc"sv, "ABC"sv).score, 145);
|
||||
EXPECT_EQ(fuzzy_match("abc"sv, "Abc"sv).score, 145);
|
||||
EXPECT_EQ(fuzzy_match("abcd"sv, "abcd"sv).score, 160);
|
||||
|
||||
// These cases get a camel case bonus.
|
||||
EXPECT_EQ(fuzzy_match("b"sv, "aB"sv).score, 124);
|
||||
EXPECT_EQ(fuzzy_match("abc"sv, "aBc"sv).score, 175);
|
||||
EXPECT_EQ(fuzzy_match("abc"sv, "aBC"sv).score, 175);
|
||||
EXPECT_EQ(fuzzy_match("abc"sv, "aBC-"sv).score, 174);
|
||||
EXPECT_EQ(fuzzy_match("abcd"sv, "aBcD"sv).score, 220);
|
||||
EXPECT_EQ(fuzzy_match("abcd"sv, "aBcD-"sv).score, 219);
|
||||
}
|
||||
|
||||
TEST_CASE(is_separator_bonus_applied_correctly)
|
||||
{
|
||||
// Separator bonus is +30 if the character preceding the matching character is a space or an underscore.
|
||||
EXPECT_EQ(fuzzy_match("b"sv, "a b"sv).score, 118);
|
||||
EXPECT_EQ(fuzzy_match("bc"sv, "a b c"sv).score, 147);
|
||||
EXPECT_EQ(fuzzy_match("ab cd"sv, "ab cd"sv).score, 205);
|
||||
EXPECT_EQ(fuzzy_match("ab_cd"sv, "ab_cd"sv).score, 205);
|
||||
EXPECT_EQ(fuzzy_match("abcd"sv, "a b c d"sv).score, 202);
|
||||
EXPECT_EQ(fuzzy_match("abcd"sv, "a_b_c_d"sv).score, 202);
|
||||
EXPECT_EQ(fuzzy_match("b c"sv, "ab cd"sv).score, 153);
|
||||
EXPECT_EQ(fuzzy_match("b_c"sv, "ab_cd"sv).score, 153);
|
||||
EXPECT_EQ(fuzzy_match("bc"sv, "ab cd"sv).score, 122);
|
||||
}
|
|
@ -1,124 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/MACAddress.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
TEST_CASE(should_default_construct)
|
||||
{
|
||||
constexpr MACAddress sut {};
|
||||
static_assert(sut.is_zero());
|
||||
EXPECT(sut.is_zero());
|
||||
}
|
||||
|
||||
TEST_CASE(should_braces_construct)
|
||||
{
|
||||
constexpr MACAddress sut { 1, 2, 3, 4, 5, 6 };
|
||||
static_assert(!sut.is_zero());
|
||||
EXPECT(!sut.is_zero());
|
||||
}
|
||||
|
||||
TEST_CASE(should_construct_from_6_octets)
|
||||
{
|
||||
constexpr MACAddress sut(1, 2, 3, 4, 5, 6);
|
||||
static_assert(!sut.is_zero());
|
||||
EXPECT(!sut.is_zero());
|
||||
}
|
||||
|
||||
TEST_CASE(should_provide_read_access_to_octet_by_index)
|
||||
{
|
||||
constexpr auto is_all_expected = [](auto& sut) {
|
||||
for (auto i = 0u; i < sizeof(MACAddress); ++i) {
|
||||
if (sut[i] != i + 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
constexpr MACAddress sut(1, 2, 3, 4, 5, 6);
|
||||
|
||||
static_assert(is_all_expected(sut));
|
||||
|
||||
for (auto i = 0u; i < sizeof(MACAddress); ++i) {
|
||||
EXPECT_EQ(i + 1, sut[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(should_provide_write_access_to_octet_by_index)
|
||||
{
|
||||
constexpr auto sut = [] {
|
||||
MACAddress m {};
|
||||
for (auto i = 0u; i < sizeof(MACAddress); ++i) {
|
||||
m[i] = i + 1;
|
||||
}
|
||||
return m;
|
||||
}();
|
||||
|
||||
constexpr MACAddress expected(1, 2, 3, 4, 5, 6);
|
||||
|
||||
static_assert(expected == sut);
|
||||
}
|
||||
|
||||
TEST_CASE(should_equality_compare)
|
||||
{
|
||||
constexpr MACAddress a(1, 2, 3, 4, 5, 6);
|
||||
constexpr MACAddress b(1, 2, 3, 42, 5, 6);
|
||||
|
||||
static_assert(a == a);
|
||||
static_assert(a != b);
|
||||
|
||||
EXPECT(a == a);
|
||||
EXPECT(a != b);
|
||||
}
|
||||
|
||||
TEST_CASE(should_string_format)
|
||||
{
|
||||
MACAddress sut(1, 2, 3, 4, 5, 6);
|
||||
EXPECT_EQ("01:02:03:04:05:06", sut.to_byte_string());
|
||||
}
|
||||
|
||||
TEST_CASE(should_make_mac_address_from_string_numbers)
|
||||
{
|
||||
auto const sut = MACAddress::from_string("01:02:03:04:05:06"sv);
|
||||
|
||||
EXPECT(sut.has_value());
|
||||
EXPECT_EQ(1, sut.value()[0]);
|
||||
EXPECT_EQ(2, sut.value()[1]);
|
||||
EXPECT_EQ(3, sut.value()[2]);
|
||||
EXPECT_EQ(4, sut.value()[3]);
|
||||
EXPECT_EQ(5, sut.value()[4]);
|
||||
EXPECT_EQ(6, sut.value()[5]);
|
||||
}
|
||||
|
||||
TEST_CASE(should_make_mac_address_from_string_letters)
|
||||
{
|
||||
auto const sut = MACAddress::from_string("de:ad:be:ee:ee:ef"sv);
|
||||
|
||||
EXPECT(sut.has_value());
|
||||
EXPECT_EQ(u8 { 0xDE }, sut.value()[0]);
|
||||
EXPECT_EQ(u8 { 0xAD }, sut.value()[1]);
|
||||
EXPECT_EQ(u8 { 0xBE }, sut.value()[2]);
|
||||
EXPECT_EQ(u8 { 0xEE }, sut.value()[3]);
|
||||
EXPECT_EQ(u8 { 0xEE }, sut.value()[4]);
|
||||
EXPECT_EQ(u8 { 0xEF }, sut.value()[5]);
|
||||
}
|
||||
|
||||
TEST_CASE(should_make_empty_optional_from_bad_string)
|
||||
{
|
||||
auto const sut = MACAddress::from_string("bad string"sv);
|
||||
|
||||
EXPECT(!sut.has_value());
|
||||
}
|
||||
|
||||
TEST_CASE(should_make_empty_optional_from_out_of_range_values)
|
||||
{
|
||||
auto const sut = MACAddress::from_string("de:ad:be:ee:ee:fz"sv);
|
||||
|
||||
EXPECT(!sut.has_value());
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Gurkirat Singh <tbhaxor@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Slugify.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
TEST_CASE(ignore_unicode_characters)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify("Hello World!🎉"_string)), "hello-world"_string);
|
||||
}
|
||||
|
||||
TEST_CASE(all_whitespace_empty_string)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify(" "_string)), ""_string);
|
||||
}
|
||||
|
||||
TEST_CASE(squeeze_multiple_whitespace)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify("Hello World"_string)), "hello-world"_string);
|
||||
}
|
||||
|
||||
TEST_CASE(trim_trailing_whitelist)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify("Hello World "_string)), "hello-world"_string);
|
||||
}
|
||||
|
||||
TEST_CASE(lowercase_all_result)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify("HelloWorld"_string)), "helloworld"_string);
|
||||
}
|
||||
|
||||
TEST_CASE(slug_glue_change)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify("Hello World"_string, '|')), "hello|world"_string);
|
||||
}
|
||||
|
||||
TEST_CASE(multiple_glue_squeeze)
|
||||
{
|
||||
EXPECT_EQ(MUST(slugify("Hello_ World"_string, '_')), "hello_world"_string);
|
||||
}
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefCountForwarder.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <AK/Weakable.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
|
|
Loading…
Reference in a new issue