Commit Graph

132 Commits

Author SHA1 Message Date
Emanuel Sprung
2577712a1c AK: Add String::replace() functionality
This adds a replace functionality that replaces a string that contains
occurences of a "needle" by a "replacement" value. With "all_occurences"
enabled, all occurences are being replaced, otherwise only the first
occurence is being replaced.
2020-04-01 21:47:23 +02:00
Andreas Kling
4f72f6b886 AK: Add FlyString, a simple flyweight string class
FlyString is a flyweight string class that wraps a RefPtr<StringImpl>
known to be unique among the set of FlyStrings. The class is very
unoptimized at the moment.

When to use FlyString:

- When you want O(1) string comparison
- When you want to deduplicate a lot of identical strings

When not to use FlyString:

- For strings that don't need either of the above features
- For strings that are likely to be unique
2020-03-22 13:03:43 +01:00
Andreas Kling
b956e2d939 AK: Remove all the AK .host.o files on "make clean" in AK/Tests
This is a bit hackish, but sometimes these files stick around and mess
up rebuilds.
2020-03-08 21:40:40 +01:00
Andreas Kling
b866582d98 AK: Fix all the warnings in the AK tests 2020-03-06 11:22:23 +01:00
Andreas Kling
214f934465 Meta: Adjust some copyright dates by Fei Wu 2020-03-02 14:24:25 +01:00
howar6hill
d75fa80a7b
AK: Move to_int(), to_uint() implementations to StringUtils (#1338)
Provide wrappers in String and StringView. Add some tests for the
implementations.
2020-03-02 14:19:33 +01:00
howar6hill
055344f346 AK: Move the wildcard-matching implementation to StringUtils
Provide wrappers in the String and StringView classes, and add some tests.
2020-03-02 10:38:08 +01:00
howar6hill
2a30a020c1
AK: Add enqueue_begin() for the CircularDeque class (#1320)
Also add tests for CircularDeque.
2020-03-02 09:50:43 +01:00
howar6hill
a19967d5c3 Tests: Fix a typo inTestRefPtr 2020-02-27 21:16:02 +01:00
Shannon Booth
854f0b9e1a AK: Add StringView::starts_with(char) & StringView::ends_with(char)
This is simply meant to be a more efficient implementation in the
case that we only need to check a single character.
2020-02-22 21:36:54 +01:00
Shannon Booth
9920d17342 AK: Add String starts_with(char) & ends_with(char)
This is simply meant to be a more efficient implementation in the
case that we only need to check a single character.
2020-02-15 11:40:05 +01:00
Sergey Bugaev
c0b32f7b76 Meta: Claim copyright for files created by me
This changes copyright holder to myself for the source code files that I've
created or have (almost) completely rewritten. Not included are the files
that were significantly changed by others even though it was me who originally
created them (think HtmlView), or the many other files I've contributed code to.
2020-01-24 15:15:16 +01:00
Andreas Kling
3de5439579 AK: Let's call decrementing reference counts "unref" instead of "deref"
It always bothered me that we're using the overloaded "dereference"
term for this. Let's call it "unreference" instead. :^)
2020-01-23 15:14:21 +01:00
Andreas Kling
502626eecb AK: Teach Vector::insert() to use memmove() for trivial types 2020-01-19 12:15:43 +01:00
Andreas Kling
39b3c0ef7e AK: Make it possible to swap() a NonnullRefPtr with itself
The generic swap() is not able to swap a NonnullRefPtr with itself,
due to its use of a temporary and NonnullRefPtr asserting when trying
to move() from an already move()'d instance.
2020-01-19 10:33:26 +01:00
Andreas Kling
7ea264a660 AK: NonnullRefPtr should allow assigning owner to ownee
Given the following situation:

    struct Object : public RefCounted<Object> {
        RefPtr<Object> parent;
    }

    NonnullRefPtr<Object> object = get_some_object();
    object = *object->parent;

We would previously crash if 'object' was the only strongly referencing
pointer to 'parent'. This happened because NonnullRefPtr would unref
the outgoing pointee before reffing the incoming pointee.

This patch fixes that by implementing NonnullRefPtr assignments using
pointer swaps, just like RefPtr already did.
2020-01-18 14:40:04 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Shannon Booth
4a6605bbe5 AK: Fix test compile warnings
These warnings are pretty harmless, but warnings nonetheless.
2020-01-06 10:43:00 +01:00
joshua stein
d61131945d Build: HOST_CXX -> USE_HOST_CXX
Allow HOST_CXX to be passed to make which will be the actual host
C++ compiler used, such as 'make HOST_CXX=clang++'.
2020-01-02 21:03:53 +01:00
joshua stein
f06d0da39f Build: AK/Tests: use Makefile.common 2020-01-01 22:21:50 +01:00
Andreas Kling
821484f170 AK: Fix JSON parser crashing when encountering UTF-8
The mechanism that caches the most recently seen string for each first
character was indexing into the cache using a 'char' subscript. Oops!
2019-12-29 22:20:21 +01:00
Shannon Booth
24bc674d94 AK: Add StringView::ends_with function 2019-12-29 22:04:22 +01:00
Andreas Kling
80556e6111 AK: Unbreak Tests Makefile. Turns out this newline was effectful :^) 2019-12-28 21:41:32 +01:00
joshua stein
0b501335f5 Build: wrap make invocations with flock(1)
Lock each directory before entering it so when using -j, the same
dependency isn't built more than once at a time.

This doesn't get full -j parallelism though, since one make child
will be sitting idle waiting for flock to receive its lock and
continue making (which should then do nothing since it will have
been built already).  Unfortunately there's not much that can be
done to fix that since it can't proceed until its dependency is
built by another make process.
2019-12-28 21:09:33 +01:00
joshua stein
ac25438d54 Build: clean up build system, use one shared Makefile
Allow everything to be built from the top level directory with just
'make', cleaned with 'make clean', and installed with 'make
install'.  Also support these in any particular subdirectory.

Specifying 'make VERBOSE=1' will print each ld/g++/etc. command as
it runs.

Kernel and early host tools (IPCCompiler, etc.) are built as
object.host.o so that they don't conflict with other things built
with the cross-compiler.
2019-12-20 20:20:54 +01:00
Andreas Kling
6f4c380d95 AK: Use size_t for the length of strings
Using int was a mistake. This patch changes String, StringImpl,
StringView and StringBuilder to use size_t instead of int for lengths.
Obviously a lot of code needs to change as a result of this.
2019-12-09 17:51:21 +01:00
Andreas Kling
8f80879676 AK: StringView::lines() should keep empty lines 2019-12-02 20:41:15 +01:00
William McPherson
d7177212fd AK: Add a BinarySearch template implementation
binary_search takes a haystack, a size, a needle and a compare function.
The compare function should return negative if a < b, positive if a > b
and 0 if a == b. The "sane default" compare function is integral_compare
which implements this with subtraction a - b.
binary_search returns a pointer to a matching element, NOT necessarily
the FIRST matching element. It returns a nullptr if the element was not
found.

This patch includes tests for binary_search.
2019-12-02 15:30:45 +01:00
Tommy Nguyen
2eb5793d55 LibMarkdown: Handle CRLF line endings
Previously, MDDocument only split on Unix-style line endings. This adds
a new function to StringView which handles LF, CR and CRLF.
2019-12-02 13:52:42 +01:00
Andreas Kling
d016d5e365 HackStudio: Start fleshing out the GUI for a GUI designer :^)
I'll be reconstructing parts of the VisualBuilder application here and
then we can retire VisualBuilder entirely once all the functionality
is available in HackStudio.
2019-11-09 00:41:00 +01:00
Andreas Kling
9a5e065229 AK: Always rebuild unit tests if AK headers change
This is a hack to avoid failing AK unit tests because it didn't even
try to rebuild.
2019-11-06 19:14:34 +01:00
Andreas Kling
0cea80218d AK: Make it possible to store complex types in a CircularQueue
Previously we would not run destructors for items in a CircularQueue,
which would lead to memory leaks.

This patch fixes that, and also adds a basic unit test for the class.
2019-10-23 12:27:43 +02:00
Andreas Kling
b74a433809 URL: Unbreak the serialization test
http:// URLs no longer include ":80" when serialized, since port 80 is
implied by the protocol. Non-standard ports are still serialized.
2019-10-21 14:46:36 +02:00
Tom
b0773a8ea6 AK: Add Atomic.h
Use gcc built-in atomics
2019-10-12 19:30:59 +02:00
Andreas Kling
4bfd4dc6c7 AK: Remove empty files JsonArray.cpp and JsonObject.cpp 2019-10-01 11:24:54 +02:00
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