Commit graph

1121 commits

Author SHA1 Message Date
asynts 3224fb7d55 AK: Allow calling format without arguments. 2020-09-23 21:45:28 +02:00
asynts 675b482fe7 AK: Add missing overload to format.
I had this in #3580 but I must have lost it during rebasing.
2020-09-22 19:06:06 +02:00
asynts eaeb793454 AK: Use format in String::number. 2020-09-22 15:06:40 +02:00
asynts 90536a1558 AK: Consider long and unsigned long as integral types.
Two things I hate about C++:

 1. 'int', 'signed int' and 'unsigned int' are two distinct types while
    'char, 'signed char' and 'unsigned char' are *three* distinct types.

    This is because 'signed int' is an alias for 'int' but 'signed char'
    can't be an alias for 'char' because on some weird systems 'char' is
    unsigned.

    One might think why not do it the other way around, make 'int' an
    alias for 'signed int' and 'char' an alias for whatever that is on
    the platform, or make 'char' signed on all platforms. But who am I
    to ask?

 2. 'unsigned long' and 'unsigned long long' are always different types,
    even if both are 64 bit numbers.

This commit fixes a few bugs that coming from this.

See Also: 1b3169f405.
2020-09-22 15:06:40 +02:00
asynts e5497a326a AK: Add StringBuilder::appendff using the new format.
StringBuilder::appendf was already used, thus this name. If we some day
replace all usages of printf, we could rename this method.
2020-09-22 15:06:40 +02:00
asynts 4fcdc19b14 AK: Remove strtoull dependency from format.
This function is not avaliable in the kernel.

In the future it would be nice to have some sort of <charconv> header
that does this for all integer types and then call it in strtoull and et
cetera.

The difference would be that this function say 'from_chars' would return
an Optional and not just interpret anything invalid as zero.
2020-09-22 15:06:40 +02:00
asynts 2bda21318c AK: Add format function like std::format or fmt::format. 2020-09-21 20:17:36 +02:00
asynts b5ca74e78a AK: Add template deduction guides for Array. 2020-09-21 20:17:36 +02:00
asynts d831b5738d AK: Add StringView::substring_view(size_t) overload. 2020-09-21 20:17:36 +02:00
asynts 31bb107922 AK: Remove BufferStream class.
There are three classes avaliable that share the functionality of
BufferStream:

 1. InputMemoryStream is for reading from static buffers. Example:

        Bytes input = /* ... */;
        InputMemoryStream stream { input };

        LittleEndian<u32> little_endian_value;
        input >> little_endian_value;

        u32 host_endian_value;
        input >> host_endian_value;

        SomeComplexStruct complex_struct;
        input >> Bytes { &complex_struct, sizeof(complex_struct) };

 2. OutputMemoryStream is for writing to static buffers. Example:

        Array<u8, 4096> buffer;
        OutputMemoryStream stream;

        stream << LittleEndian<u32> { 42 };
        stream << ReadonlyBytes { &complex_struct, sizeof(complex_struct) };

        foo(stream.bytes());

 3. DuplexMemoryStream for writing to dynamic buffers, can also be used
    as an intermediate buffer by reading from it directly. Example:

        DuplexMemoryStream stream;

        stream << NetworkOrdered<u32> { 13 };
        stream << NetowkrOrdered<u64> { 22 };

        NetworkOrdered<u32> value;
        stream >> value;
        ASSERT(value == 13);

        foo(stream.copy_into_contiguous_buffer());

Unlike BufferStream these streams do not use a fixed endianness
(BufferStream used little endian) these have to be explicitly specified.
There are helper types in <AK/Endian.h>.
2020-09-21 09:37:49 +02:00
asynts fa43bf92e4 LibAudio: Use InputMemoryStream instead of BufferStream. 2020-09-21 09:37:49 +02:00
asynts ae9f0e1cd8 AK: Add missing const in Span::operator==. 2020-09-21 09:37:49 +02:00
asynts 1a277ac291 AK: Add OutputMemoryStream::is_end. 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 83d0803861 AK: Re-add OutputMemoryStream for static buffers only. 2020-09-15 20:36:45 +02:00
asynts f18e927827 AK: Remove OutputMemoryStream for DuplexMemoryStream.
OutputMemoryStream was originally a proxy for DuplexMemoryStream that
did not expose any reading API.

Now I need to add another class that is like OutputMemoryStream but only
for static buffers. My first idea was to make OutputMemoryStream do that
too, but I think it's much better to have a distinct class for that.

I originally wanted to call that class FixedOutputMemoryStream but that
name is really cumbersome and it's a bit unintuitive because
InputMemoryStream is already reading from a fixed buffer.

So let's just use DuplexMemoryStream instead of OutputMemoryStream for
any dynamic stuff and create a new OutputMemoryStream for static
buffers.
2020-09-15 20:36:45 +02:00
asynts 96edcbc27c AK: Lower the requirements for InputStream::eof and rename it.
Consider the following snippet:

    void foo(InputStream& stream) {
        if(!stream.eof()) {
            u8 byte;
            stream >> byte;
        }
    }

There is a very subtle bug in this snippet, for some input streams eof()
might return false even if no more data can be read. In this case an
error flag would be set on the stream.

Until now I've always ensured that this is not the case, but this made
the implementation of eof() unnecessarily complicated.
InputFileStream::eof had to keep a ByteBuffer around just to make this
possible. That meant a ton of unnecessary copies just to get a reliable
eof().

In most cases it isn't actually necessary to have a reliable eof()
implementation.

In most other cases a reliable eof() is avaliable anyways because in
some cases like InputMemoryStream it is very easy to implement.
2020-09-14 20:58:12 +02:00
AnotherTest 1674903dcc AK: Fix PrintfImplementation "%x" handling for u32
This also fixes an issue with the color input value being messed up.
oops :P
2020-09-12 15:01:19 +02:00
Ben Wiederhake d16f510805 AK: Fix forward-declaration of Array 2020-09-12 13:46:15 +02:00
Ben Wiederhake 0d3a8d5397 AK: Fix accidentally-recursive call in BitStream 2020-09-12 00:13:29 +02:00
AnotherTest 72edb33670 AK: Generalise 'PrintfImplementation'
This makes PrintfImplementation usable with any sequence, provided that
a 'next element' function can be written for it.
Does not affect the behaviour of printf() and co.
2020-09-11 21:41:23 +02:00
asynts 049f709d0b AK: Calculate the chunk index correctly in DuplexMemoryStream. 2020-09-11 16:07:45 +02:00
asynts 0055a28710 AK: Replace LogStream operator for ReadonlyBytes with dump_bytes.
It wasn't actually possible to call

    const LogStream& operator<<(const LogStream&, ReadonlyBytes);

because it was shadowed by

    template<typename T>
    const LogStream& operator<<(const LogStream& stream, Span<T> span);

not sure how I didn't find this when I added the overload.

It would be possible to use SFINAE to disable the other overload,
however, I think it is better to use a different method entirely because
the output can be very verbose:

    void dump_bytes(ReadonlyBytes);
2020-09-10 14:15:02 +02:00
asynts a7f786fc0a AK: Use TypedTransfer in Span::copy_to. 2020-09-09 20:15:50 +02:00
asynts 910924f559 AK: Moved TypedTransfer into it's own header. 2020-09-09 20:15:50 +02:00
Tom 92e400c7f9 AK: Add Bitmap::find_one_anywhere and optimize Bitmap::find_first
Leverage constexpr and __builtin_ffs for Bitmap::find_first. Also add
a variant Bitmap::find_one_anywhere that can start scanning at a
provided hint.

Also, merge Bitmap::fill_range into the already existing Bitmap::set_range
2020-09-09 13:02:14 +02:00
asynts a7cbc7fcb2 AK: Remove empty destructor from JsonParser. 2020-09-08 14:01:21 +02:00
asynts 70dd97c46e AK: Remove FixedArray class. 2020-09-08 14:01:21 +02:00
asynts ec1080b18a Refactor: Replace usages of FixedArray with Vector. 2020-09-08 14:01:21 +02:00
asynts 9c83d6ff46 Refactor: Replace usages of FixedArray with Array. 2020-09-08 14:01:21 +02:00
asynts 76e37e8c96 AK: Add Array<T, Size> template. 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
Muhammad Zahalqa 125ea6a214 AK: Vector use Traits<T>::equals in find
Use Traits<T>::equals for equality checking in search
functions instead of  operator==
2020-09-06 21:56:32 +02:00
Muhammad Zahalqa ad3e6ef344 AK: SinglyLinkedList use Traits<T>::equals in find
Use Traits<T>::equals for equality checking in search
functions instead of  operator==
2020-09-06 21:56:32 +02:00
Tom efac3d27d2 AK: Add Bitmap::count_in_range and Bitmap::fill_range 2020-09-06 21:20:08 +02:00
asynts cd2815ed87 AK: Add LogStream overload for ReadonlyBytes.
This is extremely useful for debugging.
2020-09-06 20:47:35 +02:00
Andreas Kling 9dafbc82ff AK: Add JsonObject::remove() 2020-09-06 16:09:09 +02:00
asynts 4c317a94c7 LibCompress: Simplify logic in deflate implementation. 2020-09-06 12:54:45 +02:00
asynts 6de63782c7 Streams: Consistent behaviour when reading from stream with error.
The streaming operator doesn't short-circuit, consider the following
snippet:

    void foo(InputStream& stream) {
        int a, b;
        stream >> a >> b;
    }

If the first read fails, the second is called regardless. It should be
well defined what happens in this case: nothing.
2020-09-06 12:54:45 +02:00
asynts 359fcf348f AK: Add Buffered<T> which wraps a stream, adding input buffering. 2020-09-06 12:54:45 +02:00
asynts b011f87d34 AK: Add log stream operator overload for Span. 2020-09-06 12:54:45 +02:00
Muhammad Zahalqa fad0c8e712
AK: Make all DoublyLinkedList search methods use Traits<T>::equals (#3404) 2020-09-05 14:17:14 +02:00
asynts 7efd2a6d59 AK: Add OutputMemoryStream class. 2020-09-01 17:25:26 +02:00
asynts 3a2658951b AK: Add DuplexMemoryStream::copy_into_contiguous_buffer. 2020-09-01 17:25:26 +02:00
asynts b68a873067 AK: Move memory streams into their own header. 2020-09-01 17:25:26 +02:00
asynts f9516a99bf AK: Remove history from DuplexMemoryStream.
That feature was really only useful for Compress::DeflateDecompressor
but that is now using CircularDuplexBuffer instead.
2020-09-01 17:25:26 +02:00
asynts 9ce4475907 Streams: Distinguish recoverable and fatal errors. 2020-09-01 17:25:26 +02:00
AnotherTest 441807f96d AK: Add is_any_of(StringView) to GenericLexer 2020-08-31 23:05:58 +02:00
Nico Weber f2135d7d00 AK: Make %llx work in printf 2020-08-30 17:37:20 +02:00
Sergey Bugaev be6cce5530 AK: Add String::copy_characters_to_buffer()
This is a strcpy()-like method with actually sane semantics:

* It accepts a non-empty buffer along with its size in bytes.
* It copies as much of the string as fits into the buffer.
* It always null-terminates the result.
* It returns, as a non-discardable boolean, whether the whole string has been
copied.

Intended usage looks like this:

bool fits = string.copy_characters_to_buffer(buffer, sizeof(buffer));

and then either

if (!fits) {
    fprintf(stderr, "The name does not fit!!11");
    return nullptr;
}

or, if you're sure the buffer is large enough,

// I'm totally sure it fits because [reasons go here].
ASSERT(fits);

or if you're feeling extremely adventurous,

(void)fits;

but don't do that, please.
2020-08-30 17:35:27 +02:00