Commit Graph

3390 Commits

Author SHA1 Message Date
Niklas Poslovski
485ae28ba7 AK: Add support for backtraces on Haiku 2023-09-29 15:11:52 +01:00
kleines Filmröllchen
5b2496e522 AK: Make writability violation of FixedMemoryStream non-fatal
Writing to a read-only file is not a program-crashing error either, so
we just return the standard EBADFD (see write(2)) here.
2023-09-27 03:22:56 +02:00
kleines Filmröllchen
bbeabe929b AK: Allow specifying writability of a FixedMemoryStream 2023-09-27 03:22:56 +02:00
kleines Filmröllchen
644d9c784d AK: Make FixedMemoryStream non-final 2023-09-27 03:22:56 +02:00
Daniel Bertalan
4d2af7c3d6 AK: Implement reverse iterators for OrderedHashTable 2023-09-24 23:36:43 +02:00
Aliaksandr Kalenik
4b06cef93a AK: Add copy assignment operator for SourceLocation 2023-09-24 14:55:32 +02:00
Hendiadyoin1
3273ef1e3f AK: Add helper to make DistinctNumeric types comparable to enums 2023-09-22 18:39:37 -06:00
Ali Mohammad Pur
1b2007cc7f AK: Use outline Function storage if alignment requirements are not met
Instead of ballooning the size of the Function object, simply place the
callable on the heap with a properly aligned address.
This brings the alignment of Function down from ridiculous sizes like 64
bytes down to a manageable 8 bytes.
2023-09-22 22:10:16 +03:30
Ali Mohammad Pur
5e0a28c947 Revert "AK: Align Function storage to __BIGGEST_ALIGNMENT__ outside…
…kernel"
This reverts commit d4d92184b3.
The alignemnt requirements imposed by this are overkill at best and
ridiculous at worst, a future commit will tackle this problem in a
different, more space-efficient way.
2023-09-22 22:10:16 +03:30
Kemal Zebari
f6c52f622d AK: Number the spec step comments in URL::serialize_path() 2023-09-19 21:51:31 +01:00
kleines Filmröllchen
8bcf25561b AK: Fix MemoryStream seek from end
The seek offset is still applied positively when seeking from the end;
see the Kernel's seek implementation.
2023-09-17 17:13:52 -06:00
nipos
b99e0b95c0 AK: Defined _COARSE clocks as the normal ones on Haiku 2023-09-17 13:38:12 -06:00
nipos
fa4357b7ea AK: Use the Linux implementation of StackInfo() on Haiku 2023-09-17 13:38:12 -06:00
nipos
479e57582a AK: Implement fill_with_random() for Haiku 2023-09-17 13:38:12 -06:00
nipos
faa5660c7b AK: Add Haiku to platform detection 2023-09-17 13:38:12 -06:00
Andrew Kaster
10d7ec2027 AK: Enable backtrace() on Android API level 33 and higher
``<execinfo.h>`` was added to bionic in Android 13 (API 33)
2023-09-15 14:18:52 -06:00
Andrew Kaster
0c5a546dca AK: Send dbgln/outln/warnln to the NDK logger on Android
The embedding application is responsible for setting the android log
tag. If it doesn't a default of "Serenity" will be used for any Lagom
code.
2023-09-15 14:18:52 -06:00
Kemal Zebari
b6b4e59bf7 AK: Implement URL::serialize_path() to spec
This commit also reverts db5ad0c since code outside of the web spec
expects serialized paths to be percent decoded.

Also, there are issues trying to implement the concept "opaque
path". For now, we still use the old cannot_be_a_base_url(), but its
usage needs to be removed in favor of a has_opaque_path() as the spec
has changed since then.
2023-09-15 11:15:43 -06:00
Jelle Raaijmakers
9240233378 Revert "AK: Refill a BufferedStream when it has less than the
...requested size"

This reverts commit 13573a6c4b.

Some clients of `BufferedStream` expect a non-blocking read by
`read_some` which the commit above made impossible by potentially
performing a blocking read. For example, the following command hangs:

    pro http://ipecho.net/plain

This is caused by the HTTP job expecting to read the body of the
response which is included in the active buffer, but since the buffered
data size is less than the buffer passed into `read_some`, another
blocking read is performed before anything is returned.

By reverting this commit, all tests still pass and `pro` no longer
hangs. Note that because of another bug related to `stdout` / `stderr`
mixing and the absence of a line ending, there is no output to the
command above except a progress update.
2023-09-11 21:38:24 -04:00
Bastiaan van der Plaat
494a8cb816 AK: Add to_radians and to_degrees math functions 2023-09-10 08:38:29 +01:00
kleines Filmröllchen
b432674923 LibAudio: Prevent overflows during prediction
Saturating arithmetic leads to less screwed up audio in these cases.
2023-09-09 11:23:57 -06:00
Sergey Bugaev
54265cec1c AK: Add InvokeResult 2023-09-09 11:21:15 -06:00
Sergey Bugaev
79431b32b2 AK: Add AK_OS_GNU_HURD :^)
This is defined when building on GNU/Hurd, the GNU operating system with
the Hurd as its kernel (as it was designed originally, before Linux and
GNU/Linux came to be).

Also, define the corresponding part of User-Agent.
2023-09-06 11:41:16 -06:00
Andreas Kling
e4d14e1afc AK: Add FlyString::operator<=>(FlyString const&)
Same as String already has.
2023-09-06 11:29:03 -04:00
Andreas Kling
1e820385d9 AK: Add case-insensitive hashing for the new String classes
Bringing over this functionality from DeprecatedString.
2023-09-06 11:29:03 -04:00
Sergey Bugaev
88a3b4da9e AK: Add back ARCH(I386)
This now checks whether the platform is i386-based (but not x86_64).
2023-09-06 07:21:07 -06:00
Hendiadyoin1
35e3e3d483 AK: Make FixedPoint work on platforms without __int128 2023-09-06 07:17:03 -06:00
Sergey Bugaev
4825919bcf AK, LibCore: Enable misc things on glibc
program_invocation_name, pthread_getattr_np(), EXECINFO_BACKTRACE are
all glibc specifics (that other libcs may also have).
2023-09-06 07:14:35 -06:00
Sergey Bugaev
0143e5a36d AK: Enable arc4random_buf on glibc 2.36+
We have arc4random starting from 2.36, no reason not to use it :^)
2023-09-06 07:14:35 -06:00
Sergey Bugaev
3f90e9a8a0 AK: Add AK_LIBC_GLIBC
This is defined when we're compiling against the GNU C Library, whether
on Linux or on other kernels that glibc works on. More AK_LIBC_xxxx
definitions could be potentially added in the future.

Also add AK_LIBC_GLIBC_PREREQ(), which checks for a specific glibc
version.
2023-09-06 07:14:35 -06:00
Niklas Poslovski
d93e916d0c AK: Use arc4random_buf() for Random on all BSDs 2023-09-05 19:36:53 -04:00
Sam Atkins
f527e55046 AK: Allow case-insensitive FlyString/StringView comparison 2023-09-05 14:27:23 +02:00
Andrew Kaster
6e8f1549a3 AK+LibCore: Don't use unsupported features on Android 2023-09-03 11:38:51 +02:00
Andrew Kaster
aa03f73c2e AK: Demangle symbols on assertion failure on Linux as well
While macOS backtrace(3) puts a space directly after the mangled symbol
name, some versions of glibc put a + directly after it. This new logic
accounts for both situations when trying to demangle.

Co-Authored-By: Andreas Kling <kling@serenityos.org>
2023-09-03 07:51:03 +02:00
Andrew Kaster
4641af7873 AK: Always use our assertion failure method, and add backtrace to it
On platforms that support it, enable using ``<execinfo.h>`` to get
backtrace(3) to dump a backtrace on assertion failure. This should make
debugging things like WebContent crashes in Lagom much easier.
2023-09-01 11:50:47 +02:00
Shannon Booth
50d8ef94ba AK: Port percent_encode_after_encoding to String 2023-08-31 11:02:18 +02:00
Shannon Booth
cb4c279e90 AK: Percent encode URL fragments when parsed
This fixes URL fragments containing characters in the fragment encoding
set that were not being correctly percent encoded.
2023-08-31 11:02:18 +02:00
Shannon Booth
e233de1e48 AK: Report validation error for URLs with invalid percent encodings
This part of the spec is mostly useful for our debugging for now, but
could eventually be hooked up so that the user can see any reported
validation errors.
2023-08-31 11:02:18 +02:00
Shannon Booth
f53dfdd6ac AK: Write scheme state of URL parsing closer to spec steps
The main change here is that we now follow the spec by asserting the URL
is not special. Previously I could not find a way to enable this without
getting assertions when browsing the web - but that seems to no longer
be the case (probably from some other fixes which have since been made).
2023-08-31 11:02:18 +02:00
Shannon Booth
23e82114b4 AK: Do not consider port of 0 as a null port
This fixes an issue where if a port number of 0 was given for a non
special scheme the port number was being dropped.
2023-08-31 11:02:18 +02:00
ronak69
2caf68fd03 AK: Add binary and octal mode formatting for FixedPoint 2023-08-29 11:10:45 +02:00
ronak69
352480338e AK: Refactor FixedPoint's formatter
The main change is the simplification of the expression
`(10^precision * fraction) / 2^precision` to `5^precision * fraction`.

Those expressions overflow or not depends on the value of `precision`
and `fraction`. For the maximum value of `fraction`, the following table
shows for which value of `precision` overflow will occur.

            Old   New
    u32      08    10
    u64      15    20
    u128     30    39

As of now `u64` type is used to calculate the result of the expression.
Meaning that before, only FixedPoints with `precision` less than 15
could be accurately rendered (for every value of fraction) in decimal.
Now, this limit gets increased to 20.

This refactor also fixes, broken decimal render for explicitly specified
precision width in format string, and broken hexadecimal render.
2023-08-29 11:10:45 +02:00
ronak69
d40807681d AK: Cast lhs of left shifts to FixedPoint's underlying integer type
By default, `1` is of the type `int` which is 32-bits wide at max.
Because of that, if `precision` of a `FixedPoint` is greater than 32,
the expression `1 << precision` will get clamped at 32-bits and the
result will always be zero. Casting `1` to the wider underlying type
will make the expression not overflow.
2023-08-29 11:10:45 +02:00
ronak69
4c6ea4a963 AK: Fix off-by-one error in round-to-even logic of FixedPoint
Because of the off-by-one error, the second bit of the fraction was
getting ignored in differentiating between fractions equal to 0.5 or
greater than 0.5. This resulted in numbers like 2.75 being considered
as having fraction equal to 0.5 and getting rounded incorrectly (to 2).
2023-08-29 11:10:45 +02:00
Andreas Kling
b256e52586 AK: Make Formatter for NonnullOwnPtr<T> format the T
This mirrors the behavior of NonnullRefPtr<T>. If you want to format
the pointer address, call .ptr() on it.
2023-08-25 20:10:47 +02:00
Liav A
72231b405a AK+Kernel: Introduce StdLib function to copy FixedStringBuffer to user
This new Kernel StdLib function will be used to copy contents of a
FixedStringBuffer with a null character to a user process.

The first user of this new function is the prctl option of
PR_GET_PROCESS_NAME which would copy a process name including a null
character to a user provided buffer.
2023-08-25 11:51:52 +02:00
Karol Kosek
9663ccff77 AK: Use Iterator's trait when comparing a value
It's not needed, from what I can gather, but I changed it just for
the correctness sake.
2023-08-23 20:21:09 +02:00
Karol Kosek
e575ee4462 AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:

                 | Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
   ============= | ======================= | =======================
   uses equals() | HashMap                 | Vector, HashTable
defines equals() | *String[^1]             | ByteBuffer

[^1]: String, DeprecatedString, their Fly-type equivalents and KString.

This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.

I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.

With this change, each of the following lines will now compile
successfully:

    Vector<String>().contains_slow("WHF!"sv);
    HashTable<String>().contains("WHF!"sv);
    HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-23 20:21:09 +02:00
Andreas Kling
0b83717ea2 AK: Make SourceGenerator::fork() infallible 2023-08-22 13:08:24 +02:00
Andreas Kling
244516142a AK: Remove fallible SourceGenerator::try_append()
And fall back to the infallible append().
2023-08-22 13:08:24 +02:00
Andreas Kling
a1d694ccdc AK: Remove fallible SourceGenerator::try_appendln()
And fall back to the infallible appendln().
2023-08-22 13:08:24 +02:00
Andreas Kling
8b936b5912 AK: Make SourceGenerator::set() infallible 2023-08-22 13:08:24 +02:00
Sönke Holz
e300da4db4 AK: Don't use GCC's __builtin_ffs on riscv64 without Zbb extension
GCC redirects `__builtin_ffs` to `ffs` on RISC-V without the Zbb
extension, causing a linker error.
2023-08-21 13:28:22 +02:00
Sönke Holz
d572ad38ac AK: Add riscv64 support 2023-08-21 13:28:22 +02:00
Ali Mohammad Pur
3c176bafee LibWasm: Implement a few SIMD instructions 2023-08-21 13:39:32 +03:30
Ali Mohammad Pur
4f0f1c7c72 AK: Add support for Little/BigEndian<UFixedBigInteger<M>> 2023-08-21 13:39:32 +03:30
Ali Mohammad Pur
94f5389934 AK: Add a 'HostIsLittleEndian' constant and use it instead of BYTE_ORDER
Previously we were using the preprocessor everywhere we needed this
constant, so let's move away from that and use a constexpr constant.
2023-08-21 13:39:32 +03:30
Andrew Kaster
f87a488b22 AK: Silence -Wimplicit-const-int-float-conversion in clamp_to_int 2023-08-19 21:05:06 -06:00
Daniel Bertalan
075ce53d14 AK: Fix Clang 18 -Wdeprecated-literal-operator warning
The proper syntax for defining user-defined literals does not require a
space between the `operator""` token and the operator name:

> error: identifier 'sv' preceded by whitespace in a literal operator
>        declaration is deprecated
2023-08-19 11:07:12 +02:00
Aliaksandr Kalenik
469aea5a5b AK+LibJS: Introduce JS::HeapFunction
This change introduces HeapFunction, which is intended to be used as a
replacement for SafeFunction. The new type behaves like a regular
GC-allocated object, which means it needs to be visited from
visit_edges, and unlike SafeFunction, it does not create new roots for
captured parameters.

Co-Authored-By: Andreas Kling <kling@serenityos.org>
2023-08-19 05:03:17 +02:00
Daniel Bertalan
192ee4594c AK: Fix volatile qualifier in Atomic<T*>::ptr()
It's the pointer that should be volatile, not the pointed-to object.
2023-08-18 16:20:13 +02:00
Dan Klishch
39dee6ad67 AK: Add missing include in RecursionDecision.h 2023-08-18 08:58:51 +03:30
Dan Klishch
3556c27d2d AK: Add StringView::count(char) 2023-08-18 08:58:51 +03:30
kleines Filmröllchen
3d0da734ee AK: Allow Statistics to be used with any container type
This not only facilitates using non-owning types, but also those that
cannot be default-constructed.
2023-08-16 01:10:35 +02:00
Nicolas Ramz
fda5590313 LibGfx/ILBM: Add an IFF-ILBM decoder :)
IFF was a generic container fileformat that was popular on the Amiga
since it was the only file format supported by Deluxe Paint.

ILBM is an image format popular in the late eighties/nineties
that uses the IFF container.

This is a very first version of the decoder that only supports
(byterun) compressed files with bpp <= 8.

Only the minimal chunks are decoded: CMAP, BODY, BMHD.

I am planning to add support for the following variants:

- EHB (32 colours + lighter 32 colours)
- HAM6 / HAM8 (special mode that allowed to display the whole Amiga
4096 colours / 262 144 colours palette)
- TrueColor (24bit)

Things that could be fun to do:

- Still images could be animated using color cycle information
2023-08-15 18:36:11 +01:00
Hendiadyoin1
394529b7d0 AK: Fix formatting of negative whole fixed point numbers
Instead of `-2` we were printing `-2.1`

Co-Authored-By: Daniel Bertalan <dani@danielbertalan.dev>
2023-08-14 14:20:45 -06:00
Hendiadyoin1
daacc5c6c2 AK: Rename AK::FixedPoint::round to rint and fix a rounding error
`rint` is a more accurate name for the roudning mode as the fixme above
stated
2023-08-14 14:20:45 -06:00
Hendiadyoin1
05c959e40b AK: Change standard casting method of FixedPoint to truncation
This matches what floats do.

Also fix typo `trunk`->`trunc`
2023-08-14 14:20:45 -06:00
Hendiadyoin1
8526791617 AK: Use wider type for FixedPoint division
This allows us to shift first and then divide, preserving more precision
2023-08-14 14:20:45 -06:00
Hendiadyoin1
e609ac74a3 AK: Fix FixedPoint multiplication rounding and overflow behaviour
We now preform the multiplication in a widened type which makes it
overflow-safe and use the correct bit for rounding direction detection.
2023-08-14 14:20:45 -06:00
Shannon Booth
582784d0cf AK: Remove unused ApplyPercentDecoding enum from URL class 2023-08-13 15:03:53 -06:00
Shannon Booth
9d60f23abc AK: Port URL::m_fragment from DeprecatedString to String 2023-08-13 15:03:53 -06:00
Shannon Booth
5663a2d3b4 AK+LibWeb: Do not percent encode/decode in URL fragment setter/getters
The web specs do not expect decoding or decoding to happen when calling
these helpers. This allows us to remove the raw_fragment helper function
from the URL class.
2023-08-13 15:03:53 -06:00
Shannon Booth
c25485700a AK: Port URL scheme from DeprecatedString to String 2023-08-13 15:03:53 -06:00
Shannon Booth
21fe86d235 AK: Port URL::m_query from DeprecatedString to String 2023-08-13 15:03:53 -06:00
Shannon Booth
55a01e72ca AK: Port URL username/password from DeprecatedString to String
And for cases that just need to check whether the password/username is
empty, add a raw_{password,username} helper to avoid any allocation.
2023-08-13 15:03:53 -06:00
Shannon Booth
d2fe657879 AK: Remove unused URL::m_path member 2023-08-13 15:03:53 -06:00
kleines Filmröllchen
625aac2367 LibAudio: Add a FLAC encoder
This encoder can handle all integer formats and sample rates, though
only two channels well. It uses fixed LPC and performs a
close-to-optimal parameter search on the LPC order and residual Rice
parameter, leading to decent compression already.
2023-08-12 12:25:26 -06:00
kleines Filmröllchen
fb37587efe LibCrypto: Implement a generic 16-bit CRC
This is mostly a 16-bit version of the 8-bit CRC, using the same general
byte-LUT algorithm.
2023-08-12 12:25:26 -06:00
kleines Filmröllchen
001ea22917 AK+LibCore: Make output buffered stream seekable
Just like with input buffered streams, we don't currently have a use
case for output buffered streams which aren't seekable, since the main
application are files.
2023-08-12 12:25:26 -06:00
Liav A
b2fd51f561 AK: Implement string formatting for FixedStringBuffers
To ensure this happens without duplicating code, we allow forcing a
StringBuilder object to only use the inline buffer, so the code in the
AK/Format.cpp file doesn't need to deal with different underlying
storage types (expandable or inline-fixed) at all.
2023-08-12 11:48:48 -06:00
Valtteri Koskivuori
8451c4d91c LibIMAP: Add useful dbgln_if() printouts to IMAP::Parser
I couldn't run the parser in a debugger like I normally would, so I
added printouts to understand where the parser is failing.
More could be added, but these are enough to get a good idea of what
the parser is doing. It's very spammy, though, so enable it by flicking
the IMAP_PARSER_DEBUG switch :^)
2023-08-12 11:45:52 -06:00
Sam Atkins
28aa4ca767 AK: Add URL::raw_fragment()
This is a little hackish. Part of the algorithm to get the indicated
part of a DOM::Document
https://html.spec.whatwg.org/multipage/browsing-the-web.html#the-indicated-part-of-the-document
wants to first get the URL's fragment, use it, and then later
percent-decode it and use that. So, we need a way to get that
un-decoded fragment.
2023-08-12 08:39:04 +02:00
Liav A
3fd4997fc2 Kernel: Don't allocate memory for names of processes and threads
Instead, use the FixedCharBuffer class to ensure we always use a static
buffer storage for these names. This ensures that if a Process or a
Thread were created, there's a guarantee that setting a new name will
never fail, as only copying of strings should be done to that static
storage.

The limits which are set are 32 characters for processes' names and 64
characters for thread names - this is because threads' names could be
more verbose than processes' names.
2023-08-09 21:06:54 -06:00
Liav A
0d30f558f4 AK+Kernel: Add the FixedStringBuffer class and StdLib functions for it
This class encapsulates a fixed Array with compile-time size definition
for storing ASCII characters.

There are also new Kernel StdLib functions to copy user data into such
objects so this class will be useful later on.
2023-08-09 21:06:54 -06:00
Timothy Flynn
66e1f8cdab AK: Support serializing opaque hosts
The spec indicates we should support serializing opaque hosts, but we
were assuming the host contained a String. Opaque hosts are represented
with Empty. Return an empty string here instead to prevent crashing on
an invalid variant access.
2023-08-09 05:33:24 +02:00
Lucas CHOLLET
fde26c53f0 AK: Remove the API to explicitly construct short strings
Now that ""_string is infallible, the only benefit of explicitly
constructing a short string is the ability to do it at compile-time. But
we never do that, so let's simplify the API and remove this
implementation detail from it.
2023-08-08 07:37:21 +02:00
Lucas CHOLLET
3f35ffb648 Userland: Prefer _string over _short_string
As `_string` can't fail anymore (since 3434412), there are no real
benefits to use the short variant in most cases.
2023-08-08 07:37:21 +02:00
Andreas Kling
25eee91811 AK: Make "foo"_fly_string infallible
Stop worrying about tiny OOMs.

Work towards #20405.
2023-08-07 16:03:27 +02:00
Andreas Kling
34344120f2 AK: Make "foo"_string infallible
Stop worrying about tiny OOMs.

Work towards #20405.
2023-08-07 16:03:27 +02:00
aryanbaburajan
a94c0eea94 AK: Add trim_ascii_whitespace method to String 2023-08-06 22:21:10 +02:00
Shannon Booth
faf9d08371 AK: Fix IPv6 serialization on multiple '0' parts ending in a '0' part
This could happen if a sequence of '0' parts was followed by a longer
sequence of '0' parts at the end of the host. The first sequence was
being used for the compress, and not the second.

For example, [1:1:0:0:1:0:0:0] was being serialized as: [1:1::1:0:0:0]
instead of [1:1:0:0:1::].

Fix this by checking at the end of the loop if we are in the middle of a
sequence of '0' parts that is longer than the current longest.
2023-08-06 10:53:32 +02:00
Shannon Booth
db5ad0c2b0 AK: Remove ApplyPercentDecoding from URL
Nowhere was setting this flag from the default.
2023-08-06 08:57:23 +02:00
Shannon Booth
98666b012d AK: Remove URL::ApplyPercentEncoding
Everywhere only ever expects percent encoding to occur, so let's just
remove this flag altogether. At the same time, replace some
DeprecatedString with StringView.
2023-08-06 08:57:23 +02:00
Shannon Booth
c4d7be100e AK: Directly append URL paths where applicable
This is a little closer to the spec text, and helps us avoid using
the ApplyPercentEncoding flag.
2023-08-06 08:57:23 +02:00
Hendiadyoin1
127b966219 AK: Expose Checked::saturating_[add|sub] as static helpers 2023-08-05 20:03:09 +02:00
Hendiadyoin1
af161a8b83 AK+LibWeb: Round to int in clamp_to_int instead of truncating
This caused inaccuracies in float->CssPixel conversions
2023-08-05 20:03:09 +02:00
Karol Kosek
eb41f0144b AK: Decode data URLs to separate class (and parse like every other URL)
Parsing 'data:' URLs took it's own route. It never set standard URL
fields like path, query or fragment (except for scheme) and instead
gave us separate methods called `data_payload()`, `data_mime_type()`,
and `data_payload_is_base64()`.

Because parsing 'data:' didn't use standard fields, running the
following JS code:

    new URL('#a', 'data:text/plain,hello').toString()

not only cleared the path as URLParser doesn't check for data from
data_payload() function (making the result be 'data:#a'), but it also
crashes the program because we forbid having an empty MIME type when we
serialize to string.

With this change, 'data:' URLs will be parsed like every other URLs.
To decode the 'data:' URL contents, one needs to call process_data_url()
on a URL, which will return a struct containing MIME type with already
decoded data! :^)
2023-08-01 14:19:05 +02:00
Karol Kosek
58017a0581 AK: Clear buffer after leaving CannotBeABaseUrlPath in URLParser
By not clearing the buffer, we were leaking the path part of a URL into
the query for URLs without an authority component (no '//host').

This could be seen most noticeably in mailto: URLs with header fields
set, as the query part of `mailto:user@example.com?subject=test` was
parsed to `user@example.comsubject=test`.

data: URLs didn't have this problem, because we have a special case for
parsing them.
2023-08-01 10:10:07 +02:00
Shannon Booth
aa7ca80d7c AK: Fix missing step step for serialization of IPv6 hosts
This was resulting in the incorrect host serialization of:

http://[0:1:0:1:0:1:0:1] to [::1:0:1:0:1:0:1]

and:

http://[1:0:1:0:1:0:1:0] to [1::1:0:1:0:1:0]
2023-07-31 14:48:24 +02:00
Shannon Booth
4fdd4dd979 AK: Add missing default port definitions for FTP scheme URLs
This is defined in the spec, but was missing in our table. Fix this, and
add a spec comment for what is missing. Also begin a basic text based
test for URL, so we can get some coverage of LibWeb's usage of URL too.
2023-07-31 14:48:24 +02:00
Ali Mohammad Pur
4e69eb89e8 LibRegex: Generate a search tree when patterns would benefit from it
This takes the previous alternation optimisation and applies it to all
the alternation blocks instead of just the few instructions at the
start.
By generating a trie of instructions, all logically equivalent
instructions will be consolidated into a single node, allowing the
engine to avoid checking the same thing multiple times.
For instance, given the pattern /abc|ac|ab/, this optimisation would
generate the following tree:
    - a
    | - b
    | | - c
    | | | - <accept>
    | | - <accept>
    | - c
    | | - <accept>
which will attempt to match 'a' or 'b' only once, and would also limit
the number of backtrackings performed in case alternatives fails to
match.

This optimisation is currently gated behind a simple cost model that
estimates the number of instructions generated, which is pessimistic for
small patterns, though the change in performance in such patterns is not
particularly large.
2023-07-31 05:31:33 +02:00
Ali Mohammad Pur
7a471b7cf5 AK: Allow customising Trie's underlying map type
This makes it possible to use an ordered map and keep the insertion
order intact.
2023-07-31 05:31:33 +02:00
Hendiadyoin1
07e4358c63 AK: Use correct builtins for fmod and remainder
Similar to floor and ceil, we were forcing the values to be doubles here

Also adds a big FIXME about my findings trying to add a general
implementation for these
2023-07-31 05:22:12 +02:00
Hediadyoin1
c9808f0d4a AK: Use correct builtins for floor and ceil
We were using the double ones, forcing casts to and from them for floats
2023-07-31 05:22:12 +02:00
Hediadyoin1
29e0494e56 AK: Use builtins in fabs implementation and move it to the top
Both GCC and Clang inline this function to use bit-wise logic and/or
appropriate instructions even on -O0 and allow their use in a constexpr
context, see
https://godbolt.org/z/de1393vha
2023-07-31 05:22:12 +02:00
Hediadyoin1
594369121a AK: Add trunc and rint to AK/Math.h
These are useful in some algorithms, which require specific rounding.
2023-07-31 05:22:12 +02:00
Hediadyoin1
6573ace8f1 AK: Move rounding function to the top of AK/Math.h
These are useful in other algorithms, so lets move them up
2023-07-31 05:22:12 +02:00
Shannon Booth
8751be09f9 AK: Serialize URL hosts with 'concept-host-serializer'
In order to follow spec text to achieve this, we need to change the
underlying representation of a host in AK::URL to deserialized format.
Before this, we were parsing the host and then immediately serializing
it again.

Making that change resulted in a whole bunch of fallout.

After this change, callers can access the serialized data through
this concept-host-serializer. The functional end result of this
change is that IPv6 hosts are now correctly serialized to be
surrounded with '[' and ']'.
2023-07-31 05:18:51 +02:00
Shannon Booth
768f070b86 AK: Implement 'concept-host-serializer' in URL spec
This implementation will allow us to fix serialization of IPv6
addresses not being surrounded by '[' and ']'.

Nothing is calling this function yet - this will come in the next
(larger) commit where the underlying host representation inside of
AK::URL is changed from DeprecatedString to URL::Host.
2023-07-31 05:18:51 +02:00
Shannon Booth
803ca8cc80 AK: Make serialize_ipv6_address take a StringBuilder
This will allow us to implement 'concept-host-serializer' without
needing to call String::formatted.
2023-07-31 05:18:51 +02:00
Shannon Booth
a1ae701a7d AK: Move URL::cannot_have_a_username_or_password_or_port out of line
This doesn't seem trivial enough to be defining in the header like this,
and should not be a performance critical function anyhow.

Also add spec comments while we are at it, and a FIXME since we do not
seem to exactly align.
2023-07-31 05:18:51 +02:00
Shannon Booth
0c0117fc86 AK: Add typdefs for host URL definitions
And use them where applicable. This will allow us to store the host in
the deserialized format as the spec specifies.

Ideally these typdefs would instead be the existing AK interfaces, but
in the meantime, we can just use this.
2023-07-31 05:18:51 +02:00
Andrew Kaster
f5e8bba092 AK: Add argument to LexicalPath::basename to strip the extension 2023-07-30 17:50:44 -06:00
Sam Atkins
3f7d97f098 AK+Libraries: Remove FixedMemoryStream::[readonly_]bytes()
These methods are slightly more convenient than storing the Bytes
separately. However, it it feels unsanitary to reach in and access this
data directly. Both of the users of these already have the
[Readonly]Bytes available in their constructors, and can easily avoid
using these methods, so let's remove them entirely.
2023-07-30 19:32:52 +01:00
Shannon Booth
bf7af25a82 AK: Allow testing Empty instances for equality
This also makes it possible to compare `Variant<Empty, Ts...>`
objects if operator== exists for all Ts
2023-07-28 20:47:48 +03:30
kleines Filmröllchen
a0705202ea Kernel/Ext2: Write superblock backups
We don't ever read them out, but this should make fsck a lot less mad.
2023-07-28 14:51:07 +02:00
Lucas CHOLLET
18b7ddd0b5 AK: Rename the const overload of FixedMemoryStream::bytes()
Due to overload resolutions rules, this simple code provokes a crash:

ReadonlyBytes readonly_bytes{};
FixedMemoryStream stream{readonly_bytes};
ReadonlyBytes give_them_back{stream.bytes()};
    // -> Panics on VERIFY(m_writing_enabled);
    // but this is fine:
auto bytes = static_cast<FixedMemoryStream const&>(*stream).bytes()

If we need to be explicit about it, let's rename the overload instead of
adding that `static_cast`.
2023-07-27 14:40:00 +01:00
Shannon Booth
7b3902e3d5 AK: Remove unused URL::scheme_requires_port
THis function does not seem to be used anywhere, and I cannot find any
spec equivalent for this function.
2023-07-25 06:43:50 -04:00
Shannon Booth
c8da880806 AK: Add spec comments to URL::serialize 2023-07-25 06:43:50 -04:00
Shannon Booth
177b04dcfc AK: Fix url host parsing check for 'ends in a number'
I misunderstood the spec step for checking whether the host 'ends with a
number'. We can't simply check for it if ends with a number, this check
is actually an algorithm which is required to avoid detecting hosts that
end with a number from an IPv4 host.

Implement this missing step, and add a test to cover this.
2023-07-25 06:43:50 -04:00
Aliaksandr Kalenik
d216621d2a AK: Add clamp_to_int(value) in Math.h
clamp_to_int clamps value to valid range of int values so resulting
value does not overflow.

It is going to be used to clamp float or double values to int that
represents fixed-point value of CSSPixels.
2023-07-25 11:52:02 +02:00
Shannon Booth
8d2ccf0f4f AK: Implement IPV4 host URL parsing to specification
This implements both the parsing and serialization IPV4 parts from
the URL spec.
2023-07-24 17:07:16 -04:00
Shannon Booth
50359567e0 AK: Add spec comments for URL spec defined member variables 2023-07-24 17:07:16 -04:00
Timothy Flynn
685c8c3d40 Revert "AK: Automatically copy all warn/warnln logs to debug console"
This reverts commit d48c68cf3f.

Unfortunately, this currently copies some warn() invocations that we do
*not* want in the debug console, such as test-js's use of OSC command 9
to report progress.
2023-07-22 12:19:53 -04:00
Lucas CHOLLET
f79165cefe AK: Make FixedArray movable 2023-07-21 10:47:34 -06:00
Andrew Kaster
3533d3e452 AK: Enable consteval workaround for Android NDK
Android isn't shipping clang-15 yet in any NDK, so use the existing
workaround on that platform.
2023-07-19 04:22:28 -06:00
Andreas Kling
f0ec104131 AK: Implement IPv6 host parsing in URLParser
This is just a straight (and fairly inefficient) implementation of IPv6
parsing and serialization from the URL spec.

Note that we don't use AK::IPv6Address here because the URL spec
requires a specific serialization behavior.
2023-07-17 07:47:58 +02:00
Sam Atkins
d48c68cf3f AK: Automatically copy all warn/warnln logs to debug console
This is only enabled inside Serenity, as on Lagom, all out/warn/dbg logs
go to the same console anyway.
2023-07-16 00:59:13 +02:00
Shannon Booth
5625ca5cb9 AK: Rename URLParser::parse to URLParser::basic_parse
To make it more clear that this function implements
'concept-basic-url-parser' instead of 'concept-url-parser'.
2023-07-15 09:45:16 +02:00
Shannon Booth
7ef4689383 AK: Implement steps for state override in URL parser 2023-07-15 09:45:16 +02:00
Daniel Bertalan
aaf1b762ea AK: Remove redundant information from TypeErasedFormatParams
The array which contains the actual parameters is always located
immediately after the base `TypeErasedFormatParams` object of
`VariadicFormatParams`. Hence, storing a pointer to it inside a `Span`
is redundant. Changing it to a zero-length array saves 8 bytes.

Secondly, we limit the number of parameters to 256, so `m_size` and
`m_next_index` can be stored in a smaller data type than `size_t`,
saving us another 8 bytes.

This decreases the size of a single-element `VariadicFormatParams` from
48 to 32 bytes, thus reducing the code size overhead of setting up
parameters for `dbgln()`.

Note that [arrays of length zero][1] are a GNU extension, but it's used
elsewhere in the codebase already and is explicitly supported by Clang
and GCC.

[1]: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
2023-07-14 06:37:11 +02:00
Nico Weber
9fb0de1cfe AK: Mark Error nodiscard
...instead of manually marking all methods returning Error nodiscard.

No real behavior change.
2023-07-12 17:03:07 +02:00
Daniel Bertalan
cfadbcd950 AK: Work around Xcode 15 beta mishandling trailing requires clauses
Xcode 15 betas 1-3 lack https://reviews.llvm.org/D135772, which fixes a
bug that causes trailing `requires` clauses to be evaluated twice,
failing the second time. Reported as FB12284201.

This caused compile errors when instantiating types derived from RefPtr:
> error: invalid reference to function 'NonnullRefPtr': constraints not
> satisfied
> note: because substituted constraint expression is ill-formed: value
> of type '<dependent type>' is not contextually convertible to 'bool'.

This commit works around the issue by moving the `requires` clauses
after the template parameter list.

In most cases, trailing `requires` clauses and those specified after the
template parameter list work identically, so this change should not
impact the code's behavior. The only difference is that trailing
requires clauses are evaluated *after* constrained placeholder types
(i.e. `Integral auto i` function parameter).
2023-07-12 15:43:18 +01:00
Lucas CHOLLET
398f7ae988 AK: Move chunks a single time in cleanup_unused_chunks()
All elements of the vector were moved to the left, for each element to
remove. This patch makes the function move each element exactly once.

On the same test case as the previous commit, it makes the function
disappear from the profile. These two commits combined reduce the
decompression time by 12%.
2023-07-10 21:35:10 -04:00
Lucas CHOLLET
44bedf7844 AK: Don't reuse chunks in AllocatingMemoryStream
As confusing as it may sound, reusing them is terrible performance wise.
When profiling the PNG decoder, the result (which is dominated by the
Zlib decompression) shows that the `cleanup_unused_chunks()` function
represented 14.26% of the profile before this patch and only 7.7%
afterward.

On a 6.5 MB PNG image, it reduces the decompression time by more than
5%.
2023-07-10 21:35:10 -04:00
Tim Schumacher
8a9761de20 AK: Let Array::from_span take a readonly Span
We are copying here, so there is no need to require a non-const Span.
2023-07-09 15:40:41 +01:00
Hendiadyoin1
467bc5b093 AK: Add a generic version of log2
This uses one of Sun OS's algorithms, for a comparison to other
algorithms please refer to
https://gist.github.com/Hendiadyoin1/f58346d66637deb9156ef360aa158bf9

This is used on aarch64 builds and for x86 floats and doubles
for performance gains check
https://quick-bench.com/q/_2jTykshP6cUqtgdepFaoQ53YC8
which shows approximately 2x gains

Co-Authored-By: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
Co-Authored-By: kleines Filmröllchen <filmroellchen@serenityos.org>
Co-Authored-By: Dan Klishch <danilklishch@gmail.com>
2023-07-09 15:39:52 +01:00
Ali Mohammad Pur
5a0ad6812c AK: Add a CallableAs<R, Args...> concept
This is just the concept version of the IsCallableWithArguments type
trait previously defined in Function.h.
2023-07-08 23:13:00 +01:00
Timothy Flynn
996c020b0d Everywhere: Remove 'clang-format off' comments that are no longer needed 2023-07-08 10:32:56 +01:00
Timothy Flynn
c911781c21 Everywhere: Remove needless trailing semi-colons after functions
This is a new option in clang-format-16.
2023-07-08 10:32:56 +01:00
Timothy Flynn
aff81d318b Everywhere: Run clang-format
The following command was used to clang-format these files:

    clang-format-16 -i $(find . \
        -not \( -path "./\.*" -prune \) \
        -not \( -path "./Base/*" -prune \) \
        -not \( -path "./Build/*" -prune \) \
        -not \( -path "./Toolchain/*" -prune \) \
        -not \( -path "./Ports/*" -prune \) \
        -type f -name "*.cpp" -o -name "*.h")
2023-07-08 10:32:56 +01:00
Tim Schumacher
60ac254df6 AK: Use hashing to accelerate searching a CircularBuffer 2023-07-06 15:06:20 +01:00
Tim Schumacher
4a10cf1506 AK: Make CircularBuffer::read_with_seekback const
Compared to the other read and write functions, this doesn't modify the
internal state of the circular buffer.
2023-07-06 15:06:20 +01:00
Tim Schumacher
42d01b21d8 AK: Rewrite the hint-based CircularBuffer::find_copy_in_seekback
This now searches the memory in blocks, which should be slightly more
efficient. However, it doesn't make much difference (e.g. ~1% in LZMA
compression) in most real-world applications, as the non-hint function
is more expensive by orders of magnitude.
2023-07-06 15:06:20 +01:00
Tim Schumacher
3526d67694 AK: Add Span<T>::matching_prefix_length 2023-07-06 15:06:20 +01:00
Tim Schumacher
2109f61b0d AK: Add search-related helpers to CircularBuffer
This factors out a lot of complicated math into somewhat understandable
functions.

While at it, rename `next_read_span_with_seekback` to
`next_seekback_span` to keep the naming consistent and to avoid making
function names any longer.
2023-07-06 15:06:20 +01:00
Tim Schumacher
d12036132e AK: Allow passing an offset to CircularBuffer::next_read_span() 2023-07-06 15:06:20 +01:00
Tim Schumacher
046a9faeb3 AK: Split up CircularBuffer::find_copy_in_seekback
The "operation modes" of this function have very different focuses, and
trying to combine both in a way where we share the most amount of code
probably results in the worst performance.

Instead, split up the function into "existing distances" and "no
existing distances" so that we can optimize either case separately.
2023-07-06 15:06:20 +01:00
Tim Schumacher
9e82ad758e AK: Move parts for searching CircularBuffer into a new class
We will be adding extra logic to the CircularBuffer to optimize
searching, but this would negatively impact the performance of
CircularBuffer users that don't need that functionality.
2023-07-06 15:06:20 +01:00