Commit Graph

2061 Commits

Author SHA1 Message Date
Brian Gianforcaro
0bb3d83a48 AK: Add copy_to span method for AK::MACAddress 2021-07-27 01:23:37 +04:30
brapru
7e40c17460 AK: Create MACAddress from string
Previously there was no way to create a MACAddress by passing a direct
address as a string. This will allow programs like the arp utility to
create a MACAddress instance by user-passed addresses.
2021-07-25 17:57:08 +02:00
Lenny Maiorani
2cf4781d14 AK: Reimplement all_of in terms of find_if
Problem:
- Now that a generic free-function form of `find_if` is implemented
  the code in `all_of` is redundant.

Solution:
- Follow the "don't repeat yourself" mantra and make the code DRY by
  implementing `all_of` in terms of `find_if`.
- One tricky part is that since captures are not permitted in
  `constexpr` lambdas, the lambda created to negate the predicate
  needs to be created by a function which does not capture and takes
  the predicate at run-time instead. This allows `all_of` to continue
  to work in a `constexpr` context.
2021-07-24 13:11:13 +04:30
Timothy Flynn
0e6375558d AK+LibRegex: Partially implement case insensitive UTF-16 comparison
This will work for ASCII code points. Unicode case folding will be
needed for non-ASCII.
2021-07-23 23:06:57 +01:00
Timothy Flynn
2e45e52993 AK: Add UTF-16 helper methods required for use within LibRegex
To be used as a RegexStringView variant, Utf16View must provide a couple
more helper methods. It must also not default its assignment operators,
because that implicitly deletes move/copy constructors.
2021-07-23 23:06:57 +01:00
Andreas Kling
3f9e018d9a CrashDaemon: Remove BACKTRACE_DEBUG debugging code
This thing seems to work fine, no need to hang on to old debug code.
2021-07-22 23:34:33 +02:00
Hendiadyoin1
1d74742c29 AK: Add char SIMD types
These are used in intrinsics, which do not recognize any signed version
of the char type
2021-07-22 23:33:21 +02:00
Ali Mohammad Pur
73fc2b3748 AK: Rewrite {AnyOf,AllOf,Find}.h to use the IteratorPairWith concept
This makes it so these algorithms are usable with arbitrary iterators,
as opposed to just instances of AK::SimpleIterator.
This commit also makes the requirement of ::index() in find_index()
explicit, as previously it was accepting any iterator.
2021-07-22 22:56:20 +02:00
Ali Mohammad Pur
2dc31c503e AK: Convert AnyOf/AllOf to east-const style 2021-07-22 22:56:20 +02:00
Ali Mohammad Pur
d40d10aae7 AK: Implement {any,all}_of(IterableContainer&&, Predicate)
This is a generally nicer-to-use version of the existing {any,all}_of()
that doesn't require the user to explicitly provide two iterators.
As a bonus, it also allows arbitrary iterators (as opposed to the hard
requirement of providing SimpleIterators in the iterator version).
2021-07-22 22:56:20 +02:00
Ali Mohammad Pur
f879e04133 AK: Add a concept for iterable containers
This concept describes a type with a begin()/end() pair that can
function as an iterator, given the following criteria:
- The return type of begin() is comparable with the return type of
  end(), and the comparison (with operator!=) yields a bool
- The object returned from begin() can be pre-incremented
- The iterator has an operator*() implementation
2021-07-22 22:56:20 +02:00
Ali Mohammad Pur
2891dca0cd AK: Add a deduction guide to Vector
Note that this does not generate a vector with inline capacity.
2021-07-22 22:56:20 +02:00
Ali Mohammad Pur
6c9ef20010 AK: Add a CommonType<Ts...> type trait
Also adds a simple-ish test for CommonType.
2021-07-22 22:56:20 +02:00
Linus Groh
42b6bffbf2 AK: Make TypeBoundsChecker<UnsignedIntegralT, FloatingPointT> work
By replacing MakeUnsigned<Source> in this specific specialization with a
simple negativity check this now works for floating point source types.
Previously it would attempt a comparison of the destination type and
void.
2021-07-22 21:19:40 +01:00
Timothy Flynn
9b83cd1abf AK: Add Utf16View for decoding UTF-16 strings
Also includes a way to transcode from and to UTF-8 strings.
2021-07-22 09:10:44 +02:00
Gunnar Beutner
bcf5e5ee5a AK: Disable clang-format for AK/Time.h
clang-format >=12 format this file incorrectly/differently.
2021-07-22 08:57:01 +02:00
Gunnar Beutner
11e02f222d AK: Add a getter to JsonValue to get machine-native addresses 2021-07-22 08:57:01 +02:00
Andreas Kling
8ea029405f AK: Sprinkle [[nodiscard]] on AK::Array 2021-07-21 18:22:10 +02:00
Andreas Kling
4b0498e38b AK: Convert Array to east-const style 2021-07-21 18:21:15 +02:00
Andreas Kling
a940a8bf37 AK: Remove unused private HashTable::lookup_for_reading() 2021-07-21 18:18:51 +02:00
Andreas Kling
f65b039c44 AK: Sprinkle [[nodiscard]] on HashMap and HashTable 2021-07-21 18:18:29 +02:00
Andreas Kling
583d6741ed AK: Sprinkle [[nodiscard]] on AK::Bitmap 2021-07-21 18:09:01 +02:00
Andreas Kling
a5c9a31001 AK: Remove unused HashMap::remove_one_randomly() 2021-07-21 18:08:11 +02:00
Hendiadyoin1
ed46d52252 Everywhere: Use AK/Math.h if applicable
AK's version should see better inlining behaviors, than the LibM one.
We avoid mixed usage for now though.

Also clean up some stale math includes and improper floatingpoint usage.
2021-07-19 16:34:21 +04:30
Hendiadyoin1
c5f6ba6e71 AK: Introduce Math.h
This is to implement constexpr template based implementations for
mathematical functions

This also changes math.cpp to use these implementations.

Also adds a fastpath for floating point trucation for values smaller
than the signed 64 bit limit.
2021-07-19 16:34:21 +04:30
Andrew Kaster
64aac345d3 AK: Use new Formatter for each element in Formatter<Vector<T>>
The state of the formatter for the previous element should be thrown
away for each iteration. This showed up when trying to format a
Vector<String>, since Formatter<StringView> was unhappy about some state
that gets set when it's called. Add a test for Formatter<Vector>.
2021-07-19 05:17:05 +04:30
Andrew Kaster
4842c8c902 AK: Use StringView literals in Format to avoid strlen
We don't want to be constructing StringViews at runtime if we don't have
to in Formatter code.
2021-07-19 05:17:05 +04:30
Ali Mohammad Pur
a245ea1743 AK: Add the at()/operator[]() getter to Utf32View
This is trivial, and makes it easier to get the code point compared to
the previous `.code_points()[index]` (which was not actually checked
for in-bounds length).
2021-07-18 21:10:55 +04:30
Ali Mohammad Pur
55fa51b4e2 AK: Add a is_null() method to Utf{8,32}View
Both of these can be null as well as empty, and there's a difference.
2021-07-18 21:10:55 +04:30
Ali Mohammad Pur
0060fa48d4 AK: Don't return a null Utf32View when a zero-length one is requested
There is still an offset to consider, a zero-length view is very
different from a nonexistent string :P

Co-authored-by: Timothy Flynn <trflynn89@pm.me>
2021-07-18 21:10:55 +04:30
Gunnar Beutner
7e94b090fe Kernel: Introduce basic pre-kernel environment
This implements a simple bootloader that is capable of loading ELF64
kernel images. It does this by using QEMU/GRUB to load the kernel image
from disk and pass it to our bootloader as a Multiboot module.

The bootloader then parses the ELF image and sets it up appropriately.
The kernel's entry point is a C++ function with architecture-native
code.

Co-authored-by: Liav A <liavalb@gmail.com>
2021-07-18 17:31:13 +02:00
Sahan Fernando
217415226e AK: Add helper type for serializing structures into buffer 2021-07-18 19:58:17 +04:30
Timothy Flynn
06ddfcde89 AK: Allow setting both width and precision when formatting a string 2021-07-18 02:57:49 +02:00
Timothy Flynn
87848cdf7d AK: Track byte length, rather than code point length, in Utf8View::trim
Utf8View::trim uses Utf8View::substring_view to return its result, which
requires the input to be a byte offset/length rather than code point
length.
2021-07-17 16:59:59 +01:00
Brian Gianforcaro
a2a5af9745 AK: Restrict timespec comparison operator overloads in AK::Time
The previous implementation was too generic, and would cause conflicting
operator overload errors when included in certain code paths. Fix this
by restricting the template parameters to types which have the same
member names as `struct timespec`.
2021-07-17 17:39:12 +04:30
Brian Gianforcaro
6059d69249 AK: Mark RedBlackTree functions as [[nodiscard]] 2021-07-17 13:02:09 +02:00
Brian Gianforcaro
af65c4d8b7 AK: Mark AK::IntrusiveRedBlackTree as final 2021-07-17 13:02:09 +02:00
Brian Gianforcaro
9a6e065b36 AK: Mark AK::IntrusiveList Non copyable and movable 2021-07-17 13:02:09 +02:00
Brian Gianforcaro
deda32628a AK: Mark RedBlackTree as final 2021-07-17 13:02:09 +02:00
Brian Gianforcaro
86b02a678c AK: Mark RedBlackTree find APIs as [[nodiscard]] 2021-07-17 13:02:09 +02:00
Daniel Bertalan
3099a6bf2a Kernel+AK: Generate compile-time error for non-sized delete
This is a much more ergonomic option than getting a
`VERIFY_NOT_REACHED()` failure at run-time. I encountered this issue
with Clang, where sized deallocation is not the default due to ABI
breakage concerns.

Note that we can't simply just not declare these functions, because the
C++ standard states:
> If this function with size parameter is defined, the program shall
> also define the version without the size parameter.
2021-07-16 20:51:13 +02:00
Timothy
9715311837 AK+Kernel: Implement and use EnumBits has_any_flag()
This duplicates the old functionality of has_flag and will return true
when any flags present in the mask are also in the value.
2021-07-16 11:49:50 +02:00
Timothy
03b76e4ba0 AK: Change EnumBits has_flag() to check all flags in mask are present
Co-authored-by: Brian Gianforcaro <b.gianfo@gmail.com>
2021-07-16 11:49:50 +02:00
Daniel Bertalan
4566387ba5 AK: Add workaround for clang-format 12 problems with concepts 2021-07-15 09:26:19 +02:00
Andreas Kling
980f409003 AK: Allow getting the key from a RedBlackTree iterator 2021-07-15 01:48:10 +02:00
Andreas Kling
4ff35c23d3 AK: Make RedBlackTree non-copyable and non-movable 2021-07-15 01:48:09 +02:00
Ali Mohammad Pur
5d170810db AK: Make JsonParser correctly parse unsigned values larger than u32
Prior to this, it'd try to stuff them into an i64, which could fail and
give us nothing.
Even though this is an extension we've made to JSON, the parser should
be able to correctly round-trip from whatever our serialiser has
generated.
2021-07-15 01:47:35 +02:00
Idan Horowitz
e94dfb7355 AK: Expose RedBlackTree allocation failures via try_insert
This should help with using the RedBlackTree in a more OOM-safe way in
the kernel.
2021-07-15 00:49:41 +02:00
Timothy Flynn
d9c2447999 AK: Add free function to wrap around __atomic_is_lock_free built-in
Note: this exact implementation is needed for __atomic_is_lock_free to
link with both GCC (for the SerenityOS build) and Clang (for the Fuzzer
build). The size argument must be a compile-time constant, otherwise it
fails to link with both compilers. Alternatively, the following
definition links with GCC but fails with Clang:

    template<size_t S>
    static inline bool atomic_is_lock_free(volatile void* ptr = nullptr)
    {
        return __atomic_is_lock_free(S, ptr);
    }
2021-07-14 22:13:15 +01:00
Gunnar Beutner
d92548c5b0 AK: Avoid pagefaults when repeatedly enqueing/dequeing items in a Queue
When repeatedly enqueing and dequeing a single item in a Queue we end
up faulting in all the pages for the underlying Vector. This is a
performance issue - especially where the element type is large.
2021-07-14 23:03:36 +02:00
Gunnar Beutner
3ff0a3aa4b AK: Avoid allocations for the Queue class
Previously the Queue class used a SinglyLinkedList to manage its queue
segments. This changes the Queue class to use the IntrusiveList class
instead which saves us one allocation per segment.
2021-07-14 23:03:36 +02:00
Hendiadyoin1
d761c5024b AK: Generalize ByteReader
Also use it instead of CPU.h's possibly_unaligned_data interface
2021-07-14 11:26:34 +04:30
Andreas Kling
be83b3aff4 AK: Make Bitmap::set() non-const 2021-07-13 22:40:25 +02:00
kleines Filmröllchen
1e1fa4eac4 AK: Add Formatter for Vector
For debugging purposes, it is very useful to look at a Vector in a
simple list representation. Therefore, the new Formatter for Vector
provides a string representation of the following form:

```
[ 1, 2, 3, 4, 5 ]
```

This requires the content type of Vector to be formattable with default
arguments.

The current implementation ignores width and precision, which may be
accounted for later or passed down to the content formatter.
2021-07-13 17:40:07 +02:00
ngc6302h
3b81ba7c4f HashMap: Rename finders with a more accurate and self-descripting name 2021-07-13 17:31:00 +02:00
ngc6302h
213e2af281 HashTable: Rename finders with a more accurate and self-descripting name 2021-07-13 17:31:00 +02:00
ngc6302h
de7831153f Vector: Homogenize type and parameter names for predicates 2021-07-13 17:31:00 +02:00
Ali Mohammad Pur
8776f424ac AK: Make Traits<T*> use ptr_hash() and not assume 32-bit pointers
As a nice bonus, it also simplifies the code quite a bit.
2021-07-12 23:49:59 +04:30
Ali Mohammad Pur
ad328f852b AK: Replace all explicit specialisations of Traits with a single one
This commit un-confuses the many specialisations of AK::Traits, and
makes it work correctly for all integral types.
Prior to this, `AK::Traits<size_t>` would've been instantiating the
base Traits implementation, not `Traits<u32>` or `Traits<u64>`.
2021-07-12 23:49:59 +04:30
Idan Horowitz
141c46feda AK: Add a DateTimeLexer
This is an AK::GenericLexer that exposes helper methods for parsing
date and time related literals (years, months, days, hours, minutes,
seconds, fractional seconds & more)
2021-07-12 19:05:17 +01:00
Idan Horowitz
39a9cf4bb4 AK: Add a retreat(count) method to GenericLexer
This method can be used to rewind a constant amount backwards in the
source instead of one by one with retract()
2021-07-12 19:05:17 +01:00
Andrew Kaster
fac4eab415 AK: Add load64 and load_pointer to AK::ByteReader
This lets us load misaligned 64 bit integers, and misaligned pointers
in a platform agnostic way.
2021-07-12 18:42:45 +04:30
Andrew Kaster
1455604b13 AK+Meta: Remove unused AUTOCOMPLETE_DEBUG flag 2021-07-12 12:26:52 +02:00
Andreas Kling
373b8d7cfa AK: Add FixedArray::span() 2021-07-11 17:42:31 +02:00
Andreas Kling
88c8451973 AK: Bring back FixedArray<T>
Let's bring this class back, but without the confusing resize() API.
A FixedArray<T> is simply a fixed-size array of T.

The size is provided at run-time, unlike Array<T> where the size is
provided at compile-time.
2021-07-11 17:42:31 +02:00
Andreas Kling
5087b2b32f AK: Don't forget to kfree_sized() in ByteBuffer 2021-07-11 15:15:57 +02:00
Andreas Kling
25e850ebb1 Kernel: Remove krealloc()
This was only used by a single class (AK::ByteBuffer) in the kernel
and not in an OOM-safe way.

Now that ByteBuffer no longer uses it, there's no need for the kernel
heap to burden itself with supporting this.
2021-07-11 14:14:51 +02:00
Andreas Kling
966880eb45 AK: Don't use realloc() in AK::ByteBuffer
This class is the only reason we have to support krealloc() in the
kernel heap, something which adds a lot of complexity.

Let's move towards a simpler path and do malloc+memset in the
ByteBuffer code (where we know the sizes anyway.)
2021-07-11 14:14:51 +02:00
Andreas Kling
c1e902acd9 AK: Use kfree_sized() in AK::Vector 2021-07-11 14:14:51 +02:00
Andreas Kling
c68c3fa69c AK: Use kfree_sized() in AK::StringImpl 2021-07-11 14:14:51 +02:00
Andreas Kling
3aabace9f5 AK: Use kfree_sized() in AK::HashTable 2021-07-11 14:14:51 +02:00
Andreas Kling
05a00c3978 AK: Use kfree_sized() in AK::ByteBuffer 2021-07-11 14:14:51 +02:00
Andreas Kling
6950dd220b AK: Use kfree_sized() in AK::Bitmap 2021-07-11 14:14:51 +02:00
Andreas Kling
875afb35c3 AK: Make kfree_sized() forward to kfree() in non-kernel code for now 2021-07-11 14:14:51 +02:00
Gunnar Beutner
3e53f4a3de AK: Remove unused NO_RETURN macro 2021-07-10 01:41:57 +02:00
Jan de Visser
a034774e3a LibSQL+SQLServer: Build SQLServer system service
This patch introduces the SQLServer system server. This service is
supposed to be the only process/application talking to database storage.
This makes things like locking and caching more reliable, easier to
implement, and more efficient.

In LibSQL we added a client component that does the ugly IPC nitty-
gritty for you. All that's needed is setting a number of event handler
lambdas and you can connect to databases and execute statements on them.

Applications that wish to use this SQLClient class obviously need to
link LibSQL and LibIPC.
2021-07-08 17:55:59 +04:30
Daniel Bertalan
2ee39ed5f0 AK: Fix UFixedBigInt not building with Clang
Clang does not like that we are trying to refer to our own size while
our declaration is not yet complete, and fails to compile this file.
This is fixed by introducing a function which returns the correct
sizeof. This only gets evaluated in the `requires` clause after the
whole class has been parsed, so it will compile fine.
2021-07-08 10:11:00 +02:00
Daniel Bertalan
714bd011e1 Everywhere: Use the correct literal suffixes
When performing arithmetic with long doubles/floats, we want to avoid
floating point promotion and narrowing.
2021-07-08 10:11:00 +02:00
Daniel Bertalan
c6fafd3e90 AK+Userland: Add generic AK::abs() function and use it
Previously, in LibGFX's `Point` class, calculated distances were passed
to the integer `abs` function, even if the stored type was a float. This
caused the value to unexpectedly be truncated. Luckily, this API was not
used with floating point types, but that can change in the future, so
why not fix it now :^)

Since we are in C++, we can use function overloading to make things
easy, and to automatically use the right version.

This is even better than the LibC/LibM functions, as using a bit of
hackery, they are able to be constant-evaluated. They use compiler
intrinsics, so they do not depend on external code and the compiler can
emit the most optimized code by default.

Since we aren't using the C++ standard library's trick of importing
everything into the `AK` namespace, this `abs` function cannot be
exported to the global namespace, as the names would clash.
2021-07-08 10:11:00 +02:00
Daniel Bertalan
62f84e94c8 AK+Kernel: Fix perfect forwarding constructors shadowing others
If a non-const lvalue reference is passed to these constructors, the
converting constructor will be selected instead of the desired copy/move
constructor.

Since I needed to touch `KResultOr` anyway, I made the forwarding
converting constructor use `forward<U>` instead of `move`. This meant
that previously, if a lvalue was passed to it, a move operation took
place even if no `move()` was called on it. Member initializers and
if-else statements have been changed to match our current coding style.
2021-07-08 10:11:00 +02:00
Max Wipfli
f0fcbb7751 AK: Replace usages of ctype.h with CharacterTypes.h
This replaces all remaining usages of ctype.h in AK with
CharacterTypes.h.
2021-07-07 14:05:56 +02:00
Daniel Bertalan
a482a3e609 AK: Declare operators new and delete as global functions
This fixes a build issue introduced in 23d66fe, where the compiler
statically detected that that mismatching new and delete operators were
used.

Clang generates a warning for this, for the reasons described in the
comment in `AK/kmalloc.cpp`, but GCC does not.

Besides moving the allocator functions into a `.cpp` file, declarations
in `AK/kmalloc.cpp` were reordered to have imports at the top, in order
to make the code more readable.
2021-07-05 20:23:42 +02:00
Hendiadyoin1
9b7e48c6bd Kernel: Replace raw asm functions with naked ones 2021-07-05 16:40:00 +02:00
stelar7
ce314c54bd JsonParser: Bring parser more to spec 2021-07-05 12:36:19 +02:00
Gunnar Beutner
040fe0054b Kernel: Merge the x86 and x86_64 boot code into a single file
They're mostly the same apart from some x86_64-specific parts.
2021-07-05 12:28:45 +02:00
Linus Groh
7efc52c3d3 Meta: Remove the LibJS OBJECT_DEBUG debug macro
I didn't add any debug logging to the object rewrite, so this is now
unused. It's much more correct though, so we can get away with adding
ad-hoc logging, should that ever be necessary :^)

Side note: this should have a prefix, i.e. JS_OBJECT_DEBUG. The previous
name is too generic.
2021-07-04 22:07:36 +01:00
Tobias Christiansen
87033ce7d1 AK: Add generation of roman numerals to AK::String
We now can generate roman numbers using String::roman_number_from()
similar to String::bijective_base_from().
2021-07-04 22:17:03 +02:00
Idan Horowitz
9321d9d83d AK: Explicitly require Checked types to be Integral
These were already implicitly required to be integral via the usage of
the is_within_range templated function, but making them explicit should
produce nicer error messages when building, and make the IDE highlight
the incorrect usage.
2021-07-04 20:08:28 +01:00
Timothy
e42484bb65 AK+LibIPC: Make all enums codable
If an enum has a supported underlying type we can provide encoding and
decoding for the enum as well.
2021-07-04 13:48:20 +04:30
Daniel Bertalan
39dd13fd17 AK: Destroy original value when assigning to Variant 2021-07-04 07:24:41 +04:30
Daniel Bertalan
515e2d9734 AK: Use conditionally trivial special member functions
This commit makes use of the conditionally trivial special member
functions introduced in C++20. Basically, `Optional` and `Variant`
inherits whether its wrapped type is trivially copy constructible,
trivially copy assignable or trivially destructible. This lets the
compiler optimize optimize a large number of their use cases.

The constraints have been applied to `Optional`'s converting
constructors too in order to make the API more explicit.

This feature is not supported by Clang yet, so we use conditional
compilation so that Lagom can be built on macOS. Once Clang has P0848R3
support, these can be removed.
2021-07-04 07:24:41 +04:30
Daniel Bertalan
84ddecae8a AK: Add type traits describing supported operations
This will allow us to make `Optional`, `Variant`, and possibly other
data structures conditionally trivially constructible, destructible,
copyable or movable based on their type parameters.
2021-07-04 07:24:41 +04:30
Daniel Bertalan
3c6bdb8a61 AK: Make smart pointer factories work with aggregates
Aggregate initialization with brace-enclosed parameters is a
[C++20 feature][1] not yet implemented by Clang. This caused compile
errors if we tried to use the factory functions to create smart pointers
to aggregates.

As a (temporary) fix, [the LWG's previously proposed solution][2] is
implemented by this commit.

Now, wherever it's not possible to direct-initialize, aggregate
initialization is performed.

[1]:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0960r3.html
[2]: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2089
2021-07-03 01:56:31 +04:30
Daniel Bertalan
b9f30c6f2a Everywhere: Fix some alignment issues
When creating uninitialized storage for variables, we need to make sure
that the alignment is correct. Fixes a KUBSAN failure when running
kernels compiled with Clang.

In `Syscalls/socket.cpp`, we can simply use local variables, as
`sockaddr_un` is a POD type.

Along with moving the `alignas` specifier to the correct member,
`AK::Optional`'s internal buffer has been made non-zeroed by default.
GCC emitted bogus uninitialized memory access warnings, so we now use
`__builtin_launder` to tell the compiler that we know what we are doing.
This might disable some optimizations, but judging by how GCC failed to
notice that the memory's initialization is dependent on `m_has_value`,
I'm not sure that's a bad thing.
2021-07-03 01:56:31 +04:30
Daniel Bertalan
3162c9e214 AK: Make (Nonnull)OwnPtr work with abstract classes
Clang produced a compile error at this requires statement if `T` was an
abstract class.
2021-07-03 01:56:31 +04:30
Max Wipfli
9cc35d1ba3 AK: Implement String::find_any_of() and StringView::find_any_of()
This implements StringUtils::find_any_of() and uses it in
String::find_any_of() and StringView::find_any_of(). All uses of
find_{first,last}_of have been replaced with find_any_of(), find() or
find_last(). find_{first,last}_of have subsequently been removed.
2021-07-02 21:54:21 +02:00
Max Wipfli
17eddf3ac4 AK: Add input bounds checking to String::substring()
This checks for overflow in String::substring(). It also rearranges some
declarations in the header.
2021-07-02 21:54:21 +02:00
Max Wipfli
268d81a56c AK: Add String::find_last() and inline String::find() methods
This adds the String::find_last() as wrapper for StringUtils::find_last,
which is another step in harmonizing the String and StringView APIs
where possible.

This also inlines the find() methods, as they are simple wrappers around
StringUtils functions without any additional logic.
2021-07-02 21:54:21 +02:00
Max Wipfli
d7a104c27c AK: Implement StringView::find_all()
This implements the StringView::find_all() method by re-implemeting the
current method existing for String in StringUtils, and using that
implementation for both String and StringView.

The rewrite uses memmem() instead of strstr(), so the String::find_all()
argument type has been changed from String to StringView, as the null
byte is no longer required.
2021-07-02 21:54:21 +02:00
Max Wipfli
3bdaed501e AK+Everywhere: Remove StringView::find_{first,last}_of(char) methods
This removes StringView::find_first_of(char) and find_last_of(char) and
replaces all its usages with find and find_last respectively. This is
because those two methods are functionally equivalent.
find_{first,last}_of should only be used if searching for multiple
different characters, which is never the case with the char argument.

This also adds the [[nodiscard]] to the remaining find_{first,last}_of
methods.
2021-07-02 21:54:21 +02:00
Max Wipfli
56253bf389 AK: Reimplement StringView::find methods in StringUtils
This patch reimplements the StringView::find methods in StringUtils, so
they can also be used by String. The methods now also take an optional
start parameter, which moves their API in line with String's respective
methods.

This also implements a StringView::find_ast(char) method, which is
currently functionally equivalent to find_last_of(char). This is because
find_last_of(char) will be removed in a further commit.
2021-07-02 21:54:21 +02:00
Max Wipfli
3ea65200d8 AK: Implement StringView::to_{lower,upper}case_string
This patch refactors StringImpl::to_{lower,upper}case to use the new
static methods StringImpl::create_{lower,upper}cased if they have to use
to create a new StringImpl. This allows implementing StringView's
to_{lower,upper}case_string using the same methods.

It also replaces the usage of hand-written to_ascii_lowercase() and
similar methods with those from CharacterTypes.h.
2021-07-02 21:54:21 +02:00
Hediadyoin1
8ed3315dec AK: Support long doubles in format strings 2021-07-02 19:50:43 +02:00
Brian Gianforcaro
59ba316ac6 AK: Annotate more AK::Span methods as nodiscard 2021-07-01 22:44:06 +02:00
Andreas Kling
6f0e8f823b Kernel: Don't compile JsonValue & friends into the kernel 2021-06-30 11:31:12 +02:00
Max Wipfli
fb8bbdabb7 AK: Use canonicalized_path in LexicalPath::relative_path
This avoids construction of LexicalPath objects.
2021-06-30 11:13:54 +02:00
Max Wipfli
36c3962670 AK: Add [[nodiscard]] to LexicalPath methods construction objects 2021-06-30 11:13:54 +02:00
Max Wipfli
dde888a3d4 AK: Move path canonicalization into LexicalPath::canonicalized_path
This moves the path canonicalization from the LexicalPath constructor to
canonicalized_path. This allows canonicalized path to no longer
construct a LexicalPath object and initialize all its member variables.
2021-06-30 11:13:54 +02:00
Max Wipfli
d8be530397 AK+Everywhere: Remove "null state" of LexicalPath
This removes the default constructor of LexicalPath, and subsequently
modifies all its users to accommodate the change.
2021-06-30 11:13:54 +02:00
Max Wipfli
4c018909f7 AK: Make LexicalPath immutable
This replaces the current LexicalPath::append() API with a new method
that returns a new LexicalPath object and doesn't touch the this-object.
With this, LexicalPath is now immutable. It also adds a
LexicalPath::parent() method and the relevant test cases.
2021-06-30 11:13:54 +02:00
Max Wipfli
7405536a1a AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.

The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.

Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-30 11:13:54 +02:00
Max Wipfli
fc6d051dfd AK+Everywhere: Add and use static APIs for LexicalPath
The LexicalPath instance methods dirname(), basename(), title() and
extension() will be changed to return StringView const& in a further
commit. Due to this, users creating temporary LexicalPath objects just
to call one of those getters will recieve a StringView const& pointing
to a possible freed buffer.

To avoid this, static methods for those APIs have been added, which will
return a String by value to avoid those problems. All cases where
temporary LexicalPath objects have been used as described above haven
been changed to use the static APIs.
2021-06-30 11:13:54 +02:00
Max Wipfli
9b8f35259c AK: Remove the LexicalPath::is_valid() API
Since this is always set to true on the non-default constructor and
subsequently never modified, it is somewhat pointless. Furthermore,
there are arguably no invalid relative paths.
2021-06-30 11:13:54 +02:00
Max Wipfli
caa9daf59e AK: Use east const style in LexicalPath.{cpp,h} 2021-06-30 11:13:54 +02:00
Andreas Kling
0dbf786aed Kernel+AK: Don't compile JSON parser into the kernel
The kernel doesn't consume JSON, it only produces it. So there's no
need for the kernel to have a JSON parser built into it. :^)
2021-06-29 22:19:29 +02:00
Daniel Bertalan
23d66fe719 AK: Add RETURNS_NONNULL attribute and use it
This attribute tells compilers that the pointer returned by a function
is never null, which lets it optimize away null checks in some places.
This seems like a nice addition to `NonnullOwnPtr` and `NonnullRefPtr`.

Using this attribute causes extra UBSan checks to be emitted. To offset
its performance loss, some additional methods were marked ALWAYS_INLINE,
which lets the compiler optimize duplicate checks
2021-06-29 22:57:52 +04:30
Idan Horowitz
a768131720 AK: Ensure StringBuilder capacity in String::reverse 2021-06-29 16:55:54 +01:00
Idan Horowitz
4a2a084789 AK: Add the to_ascii_base36_digit helper method 2021-06-29 16:55:54 +01:00
Max Wipfli
ed2bf0a753 AK: Use [[nodiscard]] in JsonObject and JsonArray 2021-06-29 13:18:03 +02:00
Max Wipfli
9c8a2a5f69 AK+Spreadsheet+LibWeb: Remove JsonObject::get_or()
This removes JsonObject::get_or(), which is inefficient because it has
to copy the returned value. It was only used in a few cases, some of
which resulted in copying JsonObjects, which can become quite large.
2021-06-29 13:18:03 +02:00
Max Wipfli
e82611bb87 AK: Add JsonObject::has_* methods
This adds methods to JsonObject to check if a key exists with a certain
type. This simplifies code that validates whether a JsonObject has the
expected structure.
2021-06-29 13:18:03 +02:00
Max Wipfli
f45273649f AK+Everywhere: Change int to size_t in JsonObject and JsonArray 2021-06-29 13:18:03 +02:00
Max Wipfli
66526cbbaf AK: Return const& from JsonObject::get()
This adds a static JsonValue* s_null_value, which allows
JsonObject::get to return a reference instaed of copying the return
value. Since JsonValue is only 16 bytes, this seems like a reasonable
compromise.
2021-06-29 13:18:03 +02:00
Max Wipfli
e0ed160372 AK: Use OrderedHashMap in JsonObject
This changes JsonObject to use the new OrderedHashMap instead of an
extra vector for tracking the insertion order.

This also adds a default value for the KeyTraits template argument in
OrderedHashMap. Furthermore, it fixes two cases where code iterating
over a JsonObject relied on the value argument being copied before
invoking the callback.
2021-06-29 13:18:03 +02:00
Max Wipfli
13b2067da6 AK: Make JsonValue::as_string_or() const 2021-06-29 13:18:03 +02:00
Max Wipfli
53480180cb AK: Use east const style in Json{Array,Object}.h 2021-06-29 13:18:03 +02:00
Itamar
316fef5602 AK: Store the 'extra' field of ScopeLogger as String
It was previously stored as a StringView, which prevented us from
using temporary strings in the 'extra' argument.

The performance hit doesn't really matter because ScopeLogger is used
exclusively for debugging.
2021-06-29 00:07:19 +04:30
Itamar
87e6d5351e AK: Don't colorize the 'extra' field of ScopeLogger in dbgln()
It's easier to spot it in the debug logs this way :)
2021-06-29 00:07:19 +04:30
Gunnar Beutner
e56a0d6af7 Kernel: Fix memset() on x86_64
Previously memset() only set half of the bytes to the requested value.
2021-06-28 15:55:00 +02:00
Ali Mohammad Pur
745a1dbb5d AK: Add and use the RemoveCVReference<T> type trait 2021-06-28 01:08:41 +04:30
Ali Mohammad Pur
37b0f55104 AK: Make the constexpr StringView methods actually constexpr
Also add some tests to ensure that they _remain_ constexpr.
In general, any runtime assertions, weirdo C casts, pointer aliasing,
and such shenanigans should be gated behind the (helpfully newly added)
AK::is_constant_evaluated() function when the intention is to write
constexpr-capable code.
a.k.a. deliver promises of constexpr-ness :P
2021-06-27 20:54:59 +01:00
Ali Mohammad Pur
bda19a9ff3 AK: Add explicit Variant conversion operators
This allows converting between Variants of different types with less
pain.
2021-06-27 12:49:49 +01:00
Andreas Kling
beb43f673e AK: Undo bogus Variant::downcast() rename
I accidentally renamed these to verify_cast() when doing the global
AK::downcast() rename.
2021-06-26 21:27:58 +02:00
Itamar
eecbcff6af AK: Add NOTE about VERIFY in Function::clear 2021-06-25 18:58:34 +02:00
kleines Filmröllchen
9d4c50ca60 AK: Add big endian bit reading to InputBitStream
The existing InputBitStream methods only read in little endian, as this
is what the rest of the system requires. Two new methods allow the input
bitstream to read bits in big endian as well, while using the existing
state infrastructure.

Note that it can lead to issues if little endian and big endian reads
are used out of order without aligning to a byte boundary first.
2021-06-25 20:48:14 +04:30
kleines Filmröllchen
988763c0ef Toolchain: Add the AFLACLOADER_DEBUG macro
This enables FLAC debugging output, which is used
with the new FLAC loader introduced in later commits.
2021-06-25 20:48:14 +04:30
Andreas Kling
ee3a73ddbb AK: Rename downcast<T> => verify_cast<T>
This makes it much clearer what this cast actually does: it will
VERIFY that the thing we're casting is a T (using is<T>()).
2021-06-24 19:57:01 +02:00
Daniel Bertalan
2d2747cb15 LibCore+AK: Use proper atomics in Singleton 2021-06-24 17:35:49 +04:30
Daniel Bertalan
eee44c85d4 AK: Use __attribute__((name)) for functions everywhere
Clang enforces the ordering that attributes specified with the
`[[attr_name]]` syntax must comes before those defines as
`__attribute__((attr_name))`. We don't want to deal with that, so we
should stick to a single syntax (for functions, at least).

This commit favors the latter, as it's used more widely in the code
(for declaring more "exotic" options), and changing those would be a
larger effort than modifying this single file.
2021-06-24 17:35:49 +04:30
Daniel Bertalan
00915e8948 AK: Add factory methods for creating smart pointers
These functions abstract away the need to call the proper new operator
("throwing" or "non-throwing") and manually adopt the resulting raw
pointer. Modelled after the existing `NonnullOwnPtr<T> make()`
functions, these forward their parameters to the object's constructor.

Note: These can't be used in the common "factory method" idiom, as
private constructors can't be called from a standalone function.

The naming is consistent with AK's and Shell's previous implementation
of these:
- `make` creates a `NonnullOwnPtr<T>` and aborts if the allocation could
  not be performed.
- `try_make` creates an `OwnPtr<T>`, which may be null if the allocation
  failed.
- `create` creates a `NonnullRefPtr<T>`, and aborts on allocation
  failure.
- `try_create` creates a `RefPtr<T>`, which may be null if the
  allocation was not successful.
2021-06-24 17:35:49 +04:30
Daniel Bertalan
5491e0cdcc AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.

To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.

To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.

To perform fallible allocations, the following syntax should be used:

```cpp
auto ptr = new (nothrow) T;
```

As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-24 17:35:49 +04:30
Daniel Bertalan
d6138df490 AK: Specialize Atomic<Integral> for clang compatibility
While Clang claims to implement GCC's atomics libcall API, a small
incompatibility caused our builds to fail on Clang.

Clang requires requires the operands to its fixed-size functions to be
integer types, while GCC will take any type with the same size and
alignment as the various integer primitives. This was problematic, as
atomic `enum class`es would not compile.

Furthermore, Clang does not like if only one operand pointer is marked
volatile. Because it only affects the standalone atomic functions, that
will be fixed in a later commit.

As an added benefit, the code is more type-safe, as it won't let us
perform arithmetic on non-integer types. Types with overloaded
arithmetic types won't cause unexpected behavior anymore.

The constructors for the various atomic types can now be used in
constant expressions.
2021-06-24 17:35:49 +04:30
Daniel Bertalan
985adcca38 AK: Make C++ concepts support mandatory for compilers
The latest GCC and Clang versions both support this, so we can freely
use these in our code.
2021-06-24 17:35:49 +04:30
Gunnar Beutner
15e25a8c1f AK: Fix building Ptr32 on x86_64 2021-06-24 09:27:13 +02:00
Hendiadyoin1
7ca3d413f7 Kernel: Pull apart CPU.h
This does not add any functional changes
2021-06-24 00:38:23 +02:00
Ali Mohammad Pur
c38fafbf4e AK: Make {min,max,clamp}(T, U) work when U can be implicitly cast to T
It was really annoying to `static_cast` the arguments to be the same
type, so instead of doing that, just convert the second one to the first
one, and let the compiler warn about sign differences and truncation.
2021-06-23 19:04:08 +02:00
Lenny Maiorani
24225df979 AK: Reimplement any_of in terms of find_if
Problem:
- Now that a generic free-function form of `find_if` is implemented
  the code in `any_of` is redundant.

Solution:
- Follow the "don't repeat yourself" mantra and make the code DRY by
  implementing `any_of` in terms of `find_if`.
2021-06-20 10:54:09 +01:00
Idan Horowitz
5e53a690ac AK: Add support for keeping trailing zeros in fixed precision floats
This uses the same syntax as zero padding integers:
String::formatted("{:0.5}", 1.234) => "1.23400"
2021-06-19 16:13:59 +01:00
Itamar
03ef2a479a LibCoreDump: Include source locations of inlined functions in backtrace 2021-06-19 14:51:18 +02:00
Itamar
d26f4f9e8c AK: Add RedBlackTree::find_largest_not_above_iterator
It's a version of find_largest_not_above that returns an iterator.
2021-06-19 14:51:18 +02:00