Commit Graph

32 Commits

Author SHA1 Message Date
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
asynts
8d2dba022e AK: Add offset() method to Span. 2020-07-27 19:58:09 +02:00
asynts
c42450786c AK: Add implicit conversion from nullptr to Span. 2020-07-27 19:58:09 +02:00
asynts
2b57891e07 AK: Add constructors to Bytes and ReadonlyBytes that take void pointers. 2020-07-27 19:58:09 +02:00
asynts
7036a9b6f7 AK: Define conversion from Span<T> to Span<const T> correctly.
I accidently wrote `Span<RemoveConst<T>>` when I meant
`Span<RemoveConst<T>::Type>`.

Changing that wouldn't be enough though, this constructor can only be
defined if T is not const, otherwise it would redefine the copy
constructor.  This can be avoided by overloading the cast operator.
2020-07-27 19:58:09 +02:00
asynts
ac9c2bc492 AK: Implement Span which represents a contiguous sequence of objects.
This makes it possible to pass one object rather than pointer and length
individually.
2020-07-26 16:49:06 +02:00