Commit Graph

55 Commits

Author SHA1 Message Date
Timothy Flynn
e4213f5767 AK: Generalize Span::contains_slow to use the Traits infrastructure
This allows, for example, checking if a Span<String> contains a value
without having to allocate a String.
2024-03-16 08:42:33 +01:00
Timothy Flynn
faf4ba63c2 AK: Don't use east-constexpr in Span methods 2024-03-16 08:42:33 +01:00
Andrew Kaster
09ce32039f AK: Use cast to const void pointer in to_readonly_span helper
This lets developers actually hex-dump print `Span<T const>` using the
helper as intended.
2024-01-06 10:13:14 +01:00
Andrew Kaster
bbdf766fb0 AK: Add helpers to convert arbitrary Spans to {Readonly}Bytes
The streams and other common APIs require byte spans to operate on
arbitrary data. This is less than helpful when wanting to serialize
spans of other data types, such as from an Array or Vector of u32s.
2023-11-24 08:41:38 +01:00
Nico Weber
bda162fc0d AK: Add Span::reverse()
It works like Vector::reverse().
2023-11-09 16:06:25 +01:00
Tim Schumacher
a2f60911fe AK: Rename GenericTraits to DefaultTraits
This feels like a more fitting name for something that provides the
default values for Traits.
2023-11-09 10:05:51 -05:00
Tim Schumacher
3526d67694 AK: Add Span<T>::matching_prefix_length 2023-07-06 15:06:20 +01:00
Daniel Bertalan
c911fb0150 AK: Add Span::align_to
This method returns a sub-span whose data pointer and size is aligned to
a specified alignment.
2023-04-29 08:24:18 +02:00
Timothy Flynn
5c045b6934 AK: Add templated Span<u8> and Span<u8 const> constructors for C-arrays
This helper constructor exists on the unspecialized Span<T> class also,
and is convenient for e.g. creating Bytes from:

    u8 buffer[64];
    Bytes bytes { buffer };
2023-04-03 15:53:49 +02:00
MacDue
63b11030f0 Everywhere: Use ReadonlySpan<T> instead of Span<T const> 2023-02-08 19:15:45 +00:00
MacDue
8483efb399 AK: Add ReadonlySpan<T> as an alias for Span<T const>
This is a little clearer than Span<T const> where it looks like it's
the T not the underlying array that's const.
2023-02-08 19:15:45 +00:00
MacDue
668204041b AK: Add Span::first()
The missing sibling to Span::last()!
2023-01-10 10:25:58 +01:00
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Andreas Kling
ae3ffdd521 AK: Make it possible to not using AK classes into the global namespace
This patch adds the `USING_AK_GLOBALLY` macro which is enabled by
default, but can be overridden by build flags.

This is a step towards integrating Jakt and AK types.
2022-11-26 15:51:34 +01:00
Ali Mohammad Pur
6970bf03a9 AK: Mark Span<T> as trivial 2022-11-17 20:13:04 +03:30
Ben Maxwell
0f2c1f804e AK: Add last() utility function to Span 2022-04-02 21:50:41 +02:00
Arne Elster
b7110c2a34 AK: Add constructor to create Span from Array
It's a convenience constructor. But it also seems more consistent
to allow a Span being made from both raw and managed arrays.
2022-02-26 16:01:26 +01:00
Linus Groh
f8ff8c8dd6 AK: Add Traits<Span<T>>::hash() 2022-02-23 21:53:30 +00:00
sin-ack
e4a1bc1542 AK: Use __builtin_memmove for ByteBuffer and Span's overwrite
__builtin_memcpy will fail when the target area and the source area
overlap. Using __builtin_memmove will handle this case as well.
2021-12-16 22:21:35 +03:30
Daniel Bertalan
9869b598d5 AK: Make Span trivially copy-constructible
There is no need to have a user-defined copy constructor that simply
calls the base class's copy constructor. By having the compiler generate
it for us, Span is made trivially copyable, so it can be passed in
registers.
2021-10-17 17:09:58 +01:00
Ali Mohammad Pur
25f43ea0a1 AK: Make Span::operator==() comply with the ISO C++ idea of operator==
This should:
- Accept const& on both sides
- Not involve implicit conversions on either side

otherwise the argument order would be deemed significant, and trip this
warning.
2021-09-13 14:38:53 +04:30
Ali Mohammad Pur
a42c7757d3 AK: Switch Span.h to east-const style 2021-09-13 14:38:53 +04:30
Brian Gianforcaro
59ba316ac6 AK: Annotate more AK::Span methods as nodiscard 2021-07-01 22:44:06 +02:00
Ali Mohammad Pur
c022b6d74e AK: Add a way to slice from the end of a span 2021-05-27 17:28:41 +04:30
Matthew Olsson
af9a7b1374 AK: Add missing 'const' in Span 2021-05-10 10:32:39 +02:00
Valtteri Koskivuori
1069979ddf AK: Implement Span::starts_with()
Useful for checking for contents at the start of a span.
2021-05-07 11:46:53 +01:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
AnotherTest
a6e4482080 AK+Everywhere: Make StdLibExtras templates less wrapper-y
This commit makes the user-facing StdLibExtras templates and utilities
arguably more nice-looking by removing the need to reach into the
wrapper structs generated by them to get the value/type needed.
The C++ standard library had to invent `_v` and `_t` variants (likely
because of backwards compat), but we don't need to cater to any codebase
except our own, so might as well have good things for free. :^)
2021-04-10 21:01:31 +02:00
Andreas Kling
5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
Andreas Kling
a65f178ce8 AK: Do bounds checking (assertions) in Span::operator[] 2021-02-21 14:21:20 +01:00
Brian Gianforcaro
21a959e29b AK: Add Span<T> constructor for arrays
The array constructor allows arrays to be easily treated
as generic span of data.
2021-02-21 12:54:39 +01:00
Lenny Maiorani
e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
Andreas Kling
050eb5afa8 AK: Mark some Span functions with [[nodiscard]]
I was confused by the trim() API, thinking it would mutate the span it
was called on. Mark all const functions that return a new span with
[[nodiscard]] so we can catch such mistakes.
2020-12-19 18:29:13 +01:00
Andreas Kling
f82b0a78ef LibTLS+LibCrypto: More ByteBuffer -> Span conversion 2020-12-19 18:29:13 +01:00
Andreas Kling
8e20208dd6 LibTLS+LibCrypto: Replace a whole bunch of ByteBuffers with Spans 2020-12-19 18:29:13 +01:00
Lenny Maiorani
4c759ff751 Span: constexpr support
Problem:
- `Span` is not `constexpr` aware.

Solution:
- Add `constexpr` support for all parts that do not require
  `reinterpret_cast`.
- Modify tests which use the `constexpr` functions.
2020-10-16 17:06:47 +02:00
asynts
ae9f0e1cd8 AK: Add missing const in Span::operator==. 2020-09-21 09:37:49 +02:00
asynts
c8ed882b8e AK: Add OutputMemoryStream::fill_to_end. 2020-09-15 20:36:45 +02:00
asynts
a7f786fc0a AK: Use TypedTransfer in Span::copy_to. 2020-09-09 20:15:50 +02:00
asynts
ec1080b18a Refactor: Replace usages of FixedArray with Vector. 2020-09-08 14:01:21 +02:00
asynts
1b3ecb01a5 AK: Add generic SimpleIterator class to replace VectorIterator. 2020-09-08 14:01:21 +02:00
asynts
b1fc8d2b38 AK: Span: Fix signature of copy_to() and copy_trimmed_to().
Two changes were made

 1. copy_to() and copy_trimmed_to() now return how many bytes were
    copied.

 2. The argument was changed to Span<typename RemoveConst<T>::Type>
    because the following would not work:

        ReadonlyBytes bytes0;
        Bytes bytes1;

        // Won't work because this calls Span<const u8>::copy_to(Span<u8>)
        // but the method was defined as Span<const u8>::copy_to(Span<const u8>)
        bytes0.copy_to(bytes1);
2020-08-20 16:28:31 +02:00
asynts
df21487794 AK: Span: Allow slicing with zero length.
Previously, the following would not work:

    Bytes{}.slice(0);

because it was asserted that `start < size()`.
2020-08-20 16:28:31 +02:00
asynts
aef6f00195 AK: Always call memmove in Span instead of memcpy.
https://github.com/SerenityOS/serenity/pull/3166#discussion_r471031704
2020-08-17 00:00:14 +02:00
asynts
525d51bbb5 AK: Add slice() overload to Span. 2020-08-15 21:21:18 +02:00
asynts
36080c5964 AK: Add fill() method to Span. 2020-08-15 21:21:18 +02:00
asynts
78849bbb48 AK: Add copy_to() and move_to() methods to AK::Span. 2020-08-15 21:21:18 +02:00
asynts
d4fe63d2ce AK: Remove incorrect static assert in Span.h.
This assertion was added to prevent accitentally using stuff like
Span<int*> instead of Span<int>. But there can be spans of pointers.
2020-08-15 21:21:18 +02:00
AnotherTest
caedd05bd8 AK: Span<T>::operator=(const T&) => Span<T>::operator=(const Span<T>&) 2020-08-11 21:37:10 +02:00
asynts
5fa0fdb219 AK: Rename Span::subspan() to Span::slice(). 2020-07-27 19:58:09 +02:00