Commit Graph

47 Commits

Author SHA1 Message Date
Andreas Kling
09e89cc55d Revert "AK: Made Strings reversible"
This reverts commit 26e81ad574.

We forgot to consider UTF-8 here. String is UTF-8 and we need to be
careful about things like this.
2019-09-13 14:37:25 +02:00
Jesse Buhagiar
26e81ad574 AK: Made Strings reversible
`AK::String` can now be reversed via AK::String::reverse(). This makes
life a lot easier for functions like `itoa()`, where the output
ends up being backwards. Very much not like the normal STL
(which requires an `std::reverse` object) way of doing things.

A call to reverse returns a new `AK::String` so as to not upset any
of the possible references to the same `StringImpl` shared between
Strings.
2019-09-13 13:54:07 +02:00
MinusGix
a574e1ab84 TestStringView: Add test for starts_with 2019-09-13 09:22:30 +02:00
Andreas Kling
73fdbba59c AK: Rename <AK/AKString.h> to <AK/String.h>
This was a workaround to be able to build on case-insensitive file
systems where it might get confused about <string.h> vs <String.h>.

Let's just not support building that way, so String.h can have an
objectively nicer name. :^)
2019-09-06 15:36:54 +02:00
Sergey Bugaev
5d3696174b AK: Add a Utf8View type for iterating over UTF-8 codepoints
Utf8View wraps a StringView and implements begin() and end() that
return a Utf8CodepointIterator, which parses UTF-8-encoded Unicode
codepoints and returns them as 32-bit integers.

This is the first step towards supporting emojis in Serenity ^)
https://github.com/SerenityOS/serenity/issues/490
2019-08-28 13:46:02 +02:00
Andreas Kling
56eaf9b033 AK: Make FileSystemPath better at handling relative paths
Relative paths now canonicalize into a string starting with "./"
Previously, "foo" would be canonicalized as "/foo" which was clearly
not right.
2019-08-23 19:58:16 +02:00
Andreas Kling
2349dc1a21 StringView: Add StringView::operator==(StringView)
Previously we'd implicitly convert the second StringView to a String
when comparing two StringViews, which is obviously not what we wanted.
2019-08-15 14:09:27 +02:00
Andreas Kling
1ac963b5c8 JsonParser: "" is an empty string, not a null value 2019-08-14 15:01:42 +02:00
Andreas Kling
fdcff7d15e AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()
Add the concept of a PeekType to Traits<T>. This is the type we'll
return (wrapped in an Optional) from HashMap::get().

The PeekType for OwnPtr<T> and NonnullOwnPtr<T> is const T*,
which means that HashMap::get() will return an Optional<const T*> for
maps-of-those.
2019-08-14 11:47:38 +02:00
Andreas Kling
f75b1127b2 OwnPtr: Add a way to turn an OwnPtr into a NonnullOwnPtr
Okay, so, OwnPtr<T>::release_nonnull() returns a NonnullOwnPtr<T>.
It assumes that the OwnPtr is non-null to begin with.

Note that this removes the value from the OwnPtr, as there can only be
a single owner.
2019-08-14 11:04:19 +02:00
Andreas Kling
2c7b0b8893 Vector: Use memcpy to implement remove() for trivial types
This is a lot faster than the generic code path.
Also added some unit testing for this.
2019-08-12 11:07:31 +02:00
Andreas Kling
ed43770b2f AK: Add a basic URL class to help us handle URL's
We're gonna need these as we start to write more networking programs.
2019-08-10 17:30:35 +02:00
Andreas Kling
43ec733b61 AK: Add a basic unit test for FileSystemPath
Just to make sure that things are on the up-and-up.
2019-08-07 21:07:02 +02:00
Andreas Kling
2e416b1b87 Vector: Add a test for growing a Vector beyond its inline capacity 2019-08-07 20:43:13 +02:00
Andreas Kling
5096eaa845 AK: Add a FixedArray<T> container
This is a simple array wrapper that knows its size. It has begin/end
so you can use range-for. It also has a resize() that reallocates.
2019-08-07 20:05:36 +02:00
Andreas Kling
6da6ca64d2 Vector: Use TypedTransfer in more parts of Vector
Make more Vector-of-trivial-type operations go fast :^)
2019-08-07 15:25:34 +02:00
Andreas Kling
e8e85f5457 Vector: Use memcmp for comparing two vectors with trivial elements 2019-08-07 15:05:10 +02:00
Andreas Kling
151e6a1818 AK: Fix leak in Optional(Optional&&)
We were *copying* the other Optional's value and then marking it as not
having a value. This prevented us from ever destroying the original.
2019-08-05 22:27:47 +02:00
Andreas Kling
9553ecfe01 AK: Optional::operator=(Optional&&) should clear movee's has_value bit
We were forgetting to clear m_has_value in the Optional being moved
from when using operator=(Optional&&).
2019-08-05 21:47:36 +02:00
Andreas Kling
3eb1a7f8f8 AK: Add a benchmark for parsing 4chan catalog JSON
I was able to get parsing time down to about 1/3 of the original time
by using callgrind+kcachegrind. There's definitely more improvements
that can be made here, but I'm gonna be happy with this for now. :^)
2019-08-04 11:57:32 +02:00
Andreas Kling
cbc1272810 AK: Fix ref leaks in RefPtr assignment operators.
Many of the RefPtr assignment operators would cause ref leaks when we
call them to assign a pointer that's already the one kept.
2019-08-02 11:56:55 +02:00
Andreas Kling
6db879ee66 AK: Fix ref leak in NonnullRefPtr::operator=(T&).
We would leak a ref when assigning a T& to a NonnullRefPtr that already
contains that same T.
2019-08-02 11:35:05 +02:00
Andreas Kling
a9a1a5dfa9 AK: Add a test for iterating a HashTable during clear (should assert)
Ideally we should also verify that the assertion actually happens,
but we need some support in the TestSuite framework for that.
2019-08-02 09:25:35 +02:00
Andreas Kling
6560116b67 TestSuite: Hijack the ASSERT macros during unit tests.
Instead of aborting the program when we hit an assertion, just print a
message and keep going.

This allows us to write tests that provoke assertions on purpose.
2019-08-02 09:23:03 +02:00
Andreas Kling
31793b8f3a AK: Fix typo in the WeakPtr test. Behavior was actually correct.
Also remove an unused variable.
2019-08-02 09:21:42 +02:00
Andreas Kling
4e59300650 AK: Fix typo in TestVector.cpp, oops. 2019-08-02 08:59:26 +02:00
Andreas Kling
9230b42f28 AK: Use Vector::empend() a bit in the unit tests, and fix a bug.
There was a bug in the "prepend_vector_object" test but it was masked
by us not printing failures. (The bug was that we were adding three
elements to the "objects" vector and then checking that another
vector called "more_objects" indeed had three elements. Oops!)
2019-08-01 16:24:31 +02:00
Robin Burchell
a3213659dd AK: Run host tests on make
Restructure the makefile a little so it only builds objects once, and
then run them on make clean.

This is a little slower (since we're relinking tests each makeall), but
it also ensures that it will work.
2019-07-21 18:48:44 +02:00
Andreas Kling
aeae1cb5e2 AK: Add a unit test for Vector::prepend(Vector&&) with complex T.
It's good to verify that complex objects can be moved nicely by Vector.
2019-07-21 11:35:41 +02:00
Andreas Kling
4179283562 AK: Add some basic unit tests for WeakPtr. 2019-07-21 11:34:31 +02:00
Andreas Kling
67654ec529 AK: Add Vector::prepend(Vector&&).
Also included a good boy unit test.
2019-07-20 16:10:52 +02:00
Robin Burchell
41d2c674d7 AK: Add a new TestSuite.h from my own work, adapted to match the existing one a bit
This gives a few new features:

* benchmarks
* the ability to run individual testcases easily
* timing of tests
2019-07-16 11:03:38 +02:00
Andreas Kling
d9d13f2445 AK: Support case-insensitive HashMap<String, T>.
We achieve this by allowing you to specify custom traits for the key type.
For convenience, we also provide a CaseInsensitiveStringTraits for String.
2019-07-13 11:00:29 +02:00
Lawrence Manning
c3ecf753b2 AKString: add missing comparison operators
And some trivial tests.
2019-07-11 14:13:30 +02:00
Andreas Kling
55a5c46253 AK: Add Vector::insert_before_matching(T&&, callback);
This allows you to do things like:

vector.insert_before_matching(value, [](auto& entry) {
    return value < entry;
});

Basically it scans until it finds an element that matches the condition
callback and then inserts the new value before the matching element.
2019-07-04 14:20:48 +02:00
Andreas Kling
b79112e6d6 AK: Add String::number() for creating a String from a number.
Instead of manually doing String::format("%d"/"%u") everywhere, let's have
a String API for this. It's just a wrapper around format() for now, but it
could be made more efficient in the future.
2019-07-03 14:56:27 +02:00
VAN BOSSUYT Nicolas
802d4dcb6b Meta: Removed all gitignore in the source tree only keeping the root one 2019-06-30 10:41:26 +02:00
Andreas Kling
b1d113e32a AK: Make a tiny JSON unit test based on a saved VisualBuilder form. 2019-06-29 12:07:42 +02:00
Andreas Kling
2282e89d3f AK: Use a SinglyLinkedList<T> as HashTable's bucket chain storage.
We were using a DoublyLinkedList<T> simply because it supported remove().
This patch consolidates the SinglyLinkedList iterators and adds remove().
2019-06-27 16:36:31 +02:00
Andreas Kling
516d736afe AK: Consolidate iterators for HashTable and DoublyLinkedList respectively.
Get rid of the ConstIterator classes for these containers and use templated
FooIterator<T, ...> and FooIterator<const T, ...> helpers.

This makes the HashTable class a lot easier to read.
2019-06-27 15:57:49 +02:00
Andreas Kling
50700c107f AK: Get rid of ConstVectorIterator.
We can achieve the same with just a VectorIterator<const Vector, const T>.
2019-06-27 14:52:12 +02:00
Andreas Kling
bfaa74f076 AK/Tests: Test Queue<String> with large number of elements. 2019-06-15 10:39:19 +02:00
Andreas Kling
c699d9d79d AK: Add a simple Queue<T> class.
The underlying data structure is a singly-linked list of Vector<T>.
We never shift any of the vector contents around, but we batch the memory
allocations into 1000-element segments.
2019-06-15 10:35:35 +02:00
Andreas Kling
a12751695e AK/Tests: Add a simple EXPECT_EQ macro and use it for the String test. 2019-06-14 17:52:51 +02:00
Andreas Kling
3557f277f6 AK/Tests: Add some macros for testing. 2019-06-14 17:38:17 +02:00
Andreas Kling
0589ef2886 AK/Tests: Add a couple more String tests. 2019-06-14 07:40:36 +02:00
Andreas Kling
b7cca76ca2 AK: Add an extremely primitive unit test for String.
This builds for the host system rather than for Serenity.
We need to improve on it a *lot*, but at least it's a place to start.
2019-06-14 06:42:21 +02:00