Commit graph

178 commits

Author SHA1 Message Date
asynts 6351a56d27 AK+Format: Do some housekeeping in the format implementation. 2020-10-02 20:48:19 +02:00
asynts fb7a94c959 AK: Add formatter for pointer types. 2020-10-02 13:44:42 +02:00
asynts ebafc5b4d2 AK: Add formatter for boolean values. 2020-10-02 13:44:42 +02:00
asynts 1175ecf1dd AK+Format: Add support for integer to character casts.
Now the following is possible:

    outf("{:c}", 75); // K
2020-09-29 16:14:58 +02:00
asynts 71b7ef0992 AK+Format: Support all format specifiers for strings.
The following is now possible:

    outf("{:.4}", "abcdef"); // abcd
    outf("{:*<8}", "abcdef"); // abcdef**
2020-09-29 16:14:58 +02:00
asynts 13ce24de13 AK+Format: Support default index in replacement field.
The following does now work:

    outf("{:0{}}", 1, 3);      // 001
2020-09-28 17:41:27 +02:00
asynts 56bfefabb6 AK+Format: Keep type information for integers in TypeErasedParameter.
It's now save to pass a signed integer as parameter and then use it as
replacement field (previously, this would just cast it to size_t which
would be bad.)
2020-09-28 10:53:16 +02:00
asynts 6a2f5f4522 AK+Format: Clean up format specifier parsing using GenericLexer.
Also adds support for replacement fields.
2020-09-28 10:53:16 +02:00
asynts 01915a3027 AK+Format: Use the new format backend in the implementation. 2020-09-26 17:19:04 +02:00
asynts 2111fc5f63 AK+Format: Add new integer to string backend.
I put this into the <AK/PrintfImplementation.h> header in the hope that
it could be re-used by the printf implementation. That would not be
super trivial though, so I am not doing that now.
2020-09-26 17:19:04 +02:00
Ben Wiederhake 8940bc3503 Meta+AK: Make clang-format-10 clean 2020-09-25 21:18:17 +02:00
asynts 1cb8be9906 Applications+IRCClient: Use new format functions. 2020-09-23 21:45:28 +02:00
asynts b7a4c4482f AK: Resolve format related circular dependencies properly.
With this commit, <AK/Format.h> has a more supportive role and isn't
used directly.

Essentially, there now is a public 'vformat' function ('v' for vector)
which takes already type erased parameters. The name is choosen to
indicate that this function behaves similar to C-style functions taking
a va_list equivalent.

The interface for frontend users are now 'String::formatted' and
'StringBuilder::appendff'.
2020-09-23 21:45:28 +02:00
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 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 2bda21318c AK: Add format function like std::format or fmt::format. 2020-09-21 20:17:36 +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 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 70dd97c46e AK: Remove FixedArray class. 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
Tom efac3d27d2 AK: Add Bitmap::count_in_range and Bitmap::fill_range 2020-09-06 21:20:08 +02:00
asynts 4c317a94c7 LibCompress: Simplify logic in deflate implementation. 2020-09-06 12:54:45 +02:00
asynts 7efd2a6d59 AK: Add OutputMemoryStream class. 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
asynts e68b158a52 AK: Don't swap endianness when writing endian wrappers to stream. 2020-08-29 17:44:34 +02:00
Ben Wiederhake e5807d17b2 Tests: Document 'missing' tests
It's up for grabs. Anyone wants to write them? :)
2020-08-27 10:12:04 +02:00
asynts a82fead38a AK: Add CircularDuplexStream class. 2020-08-26 21:07:53 +02:00
asynts 8e08d9f70a AK: Fix the signature of binary_search. 2020-08-26 21:07:53 +02:00
Ben Wiederhake 0e27a6e39e AK: Demonstrate and fix Checked
Specifically:
- post-increment actually implemented pre-increment
- helper-templates that provided operator{+,-,*,/}() couldn't possibly work,
  because the interface of add (etc) were incompatible (not taking a Checked<>,
  and returning void)
2020-08-26 00:55:13 +02:00
Ben Wiederhake 0944f56181 AK: Fix human_readable_size corner cases
In particular: consistent rounding and extreme values.

Before, rounding was something like 'away from 0.999...', which led to
surprising corner cases in which the value was rounded up.

Now, rounding is always 'down'.
This even works for 0xffffffff, and also for 0xffffffffffffffffULL on 64-bit.
2020-08-23 11:24:55 +02:00
Ben Wiederhake f697d35fb1 AK: Add tests for human_readable_size() 2020-08-23 11:24:55 +02:00
Ben Wiederhake 4acdb60ba3 AK: Prevent confusing silent misuse of ByteBuffer
Thankfully, this hasn't happened in any other code yet, but it happened
while I was trying something out. Using '==' on two ByteBuffers to check
whether they're equal seemed straight-forward, so I ran into the trap.
2020-08-22 17:18:14 +02:00
Ben Wiederhake 901ed9b85d AK: Demonstrate surprising ByteBuffer behavior
This seems to be because ByteBuffer implements 'operator bool', and C++
considers bool to be an integer type. Thus, when trying to find a way to
evaluate '==', it attempts integer promotion, which in turn finds 'operator bool'.

This explains why all non-empty buffers seem to be equal, but different from the
empty one. Also, why comparison seems to be implemented.
2020-08-22 17:18:14 +02:00
asynts d5999c3811 AK: Move include <AK/TestSuite.h> to the top.
clang-format automatically sorts include statements that are in a
'block'. Adding a whitespace prevents this. It is crutial that
<AK/TestSuite.h> is included first because it redefines some macros.
2020-08-22 10:46:56 +02:00
asynts 39b464dcfd AK: Remove test case that doesn't test anything.
Currently, there is no way to check that an assert fails. This test
passes regardless of the assert. (AK/HashTable.h:93)
2020-08-22 10:46:56 +02:00
asynts 8bbb7e25e6 LibCompress: Turn the DEFLATE implementation into a stream.
Previously, the implementation would produce one Vector<u8> which
would contain the whole decompressed data. That can be a lot and
even exhaust memory.

With these changes it is still necessary to store the whole input data
in one piece (I am working on this next,) but the output can be read
block by block. (That's not optimal either because blocks can be
arbitrarily large, but it's good for now.)
2020-08-20 16:28:31 +02:00
asynts 30abadcff9 AK: Add DuplexMemoryStream class.
This class is similar to BufferStream because it is possible to both
read and write to it. However, it differs in the following ways:

  - DuplexMemoryStream keeps a history of 64KiB and discards the rest,
    BufferStream always keeps everything around.

  - DuplexMemoryStream tracks reading and writing seperately, the
    following is valid:

        DuplexMemoryStream stream;
        stream << 42;
        int value;
        stream >> value;

    For BufferStream it would read:

        BufferStream stream;
        stream << 42;
        int value;
        stream.seek(0);
        stream >> value;

In the future I would like to replace all usages of BufferStream with
InputMemoryStream, OutputMemoryStream (doesn't exist yet) and
DuplexMemoryStream. For now I just add DuplexMemoryStream though.
2020-08-20 16:28:31 +02:00
asynts bc332aca33 AK: Rename error() to has_error() for streams. 2020-08-20 16:28:31 +02:00
asynts 6d15318560 AK: Remove fatal() from InputStream.
Fatal errors can not be handeled and lead to an assertion error when the
stream is destroyed. It makes no sense to delay the assertion failure,
instead of setting m_fatal, an assertion should be done directly.
2020-08-20 16:28:31 +02:00
asynts 31e7f435cb AK: Rename TestMemoryStream.cpp to TestStream.cpp. 2020-08-20 16:28:31 +02:00
Ben Wiederhake 5fe6ca75ca AK: Mark compilation-unit-only functions as static
This enables a nice warning in case a function becomes dead code. Also, add forgotten
header to Base64.cpp, which would cause an issue later when we enable -Wmissing-declarations.
2020-08-12 20:40:59 +02:00
Linus Groh 62ea2c5437 AK: Add test case for duplicate JsonObject keys 2020-08-10 14:48:45 +02:00
Ben Wiederhake cc6d5242d1 AK: Implement and test DistinctNumeric class
This template class allows for easy generation of incompatible numeric types.
This is useful whenever code has to handle heterogenous data (like meters and
seconds) but the underlying data types are compatible (like int and int).

The motivation comes from the Kernel's inconsistent use of pid_t for process and
thread IDs even though the ID spaces are incompatible, and translating forth/back
is nontrivial.

Other uses could be units (as described above), or incompatible index systems.
A popular use in real life is image manipulation, when there are multiple
coordinate systems.
2020-08-10 11:51:45 +02:00
asynts 5bfa7749c3 AK: Add InputStream abstraction and InputMemoryStream implementation. 2020-08-06 10:33:16 +02:00