Commit graph

139 commits

Author SHA1 Message Date
Andreas Kling 4e80f22cc0 AK: Make some StringView constructors constexpr 2020-05-30 17:47:50 +02:00
AnotherTest f9bf2c7e78 AK: Add StringView::split_view() taking a StringView
Since the task of splitting a string via another is pretty common, we
might as well have this overload of split_view() as well.
2020-05-28 11:01:08 +02:00
Brian Gianforcaro 129462cca7 AK: Unify FlyString/StringView::ends_with implementation on StringUtils::ends_with
This creates a unified implementation of ends_with with case sensitivity
across String/StringView/FlyString.
2020-05-26 13:17:19 +02:00
Linus Groh 1277290a4a AK: Add StringView::equals_ignoring_case()
StringUtils::equals_ignoring_case() already operates on a StringView&,
so StringView should have the method directly without having to go
through a temporary String (which also has the method).
2020-05-13 19:25:49 +02:00
Emanuel Sprung 8fe821fae2 AK: Add to_string() method to StringView
This allows easy creation of a new string from an existing StringView.
Can be used e.g. for output with printf(..., view.to_string().characters())
instead of writing printf(..., String{view}.characters()).
2020-05-06 19:07:22 +02:00
Andrew Kaster 3eb3c2477f AK: Add StringView::find_first/last_of
These methods search from the beginning or end of a string for the
first character in the input StringView and returns the position in
the string of the first match. Note that this is not a substring match.
Each comes with single char overloads for efficiency.
2020-05-04 09:39:05 +02:00
Andreas Kling 888e35f0fe AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macros
It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
2020-04-30 11:43:25 +02:00
Sergey Bugaev 279cf9294a AK: Always inline trivial StringView constructors 2020-04-30 11:30:27 +02:00
Sergey Bugaev 135d29b498 AK: Assert that we don't create StringViews of negative length
Due to us using size_t for the length, the actual value will always be positive.
If, for example, we calculate the length as "0 - 1", we'll get SIZE_T_MAX. What
we can do is check that adding the characters pointer and the length together
doesn't overflow.
2020-04-30 11:30:27 +02:00
Stephan Unverwerth b77ceecb02 AK: Add StringView::contains(char) 2020-04-17 15:22:31 +02:00
Andreas Kling 26bc3d4ea0 AK: Add FlyString::equals_ignoring_case(StringView)
And share the code with String by moving the logic to StringUtils. :^)
2020-03-22 13:07:45 +01: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
howar6hill e07f50c398 AK: Add begin() and end() to String and StringView
Now it's possible to use range-based for loops with String and StringView.
2020-03-10 10:25:21 +01:00
Andreas Kling 900f51ccd0 AK: Move memory stuff (fast memcpy, etc) to a separate header
Move the "fast memcpy" stuff out of StdLibExtras.h and into Memory.h.
This will break a ton of things that were relying on StdLibExtras.h
to include a bunch of other headers. Fix will follow immediately after.

This makes it possible to include StdLibExtras.h from Types.h, which is
the main point of this exercise.
2020-03-08 13:06:51 +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
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
Andreas Kling 3bbf4610d2 AK: Add a forward declaration header
You can now #include <AK/Forward.h> to get most of the AK types as
forward declarations.

Header dependency explosion is one of the main contributors to compile
times at the moment, so this is a step towards smaller include graphs.
2020-02-14 23:31:18 +01:00
Andreas Kling 268000e166 AK: Always inline StringView(const char*)
Also use strlen() instead of manually walking the string. This allows
GCC to optimize away the strlen() entirely for string literals. :^)
2020-02-01 13:54:13 +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 24bc674d94 AK: Add StringView::ends_with function 2019-12-29 22:04:22 +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
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
Sergey Bugaev 127d168def AK: Add a keep_empty argument to String[View]::substring{_view} 2019-09-28 18:29:42 +02:00
MinusGix 05f641a5a9 StringView: Add starts_with method 2019-09-13 09:22:30 +02:00
Andreas Kling d38bd3935b AK: Add StringView::hash()
This grabs the hash from the underlying StringImpl if there is one,
otherwise it's computed on the fly.
2019-08-25 06:45:31 +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 cce2ea9bb0 AK: Add StringView::to_int()
This is a shameless copy-paste of String::to_int(). We should find some
way to share this code between String and StringView instead of having
two duplicate copies like this.
2019-08-04 11:44:20 +02:00
Andreas Kling 0e75aba7c3 StringView: Rename characters() to characters_without_null_termination().
This should make you think twice before trying to use the const char* from
a StringView as if it's a null-terminated string.
2019-07-08 15:38:44 +02:00
Andreas Kling 50677a58d4 StringView: Make it easy to construct from a ByteBuffer. 2019-06-29 12:07:46 +02:00
Sergey Bugaev 1a697f70db AK: Add more StringView utilities for making substrings.
These two allow making a new substring view starting from,
or starting after, an existing substring view.

Also make use of one of them in the kernel.
2019-06-14 06:24:02 +02:00
Andreas Kling cdb44be703 StringView: Store a StringImpl* rather than a String*. 2019-06-08 23:55:13 +02:00
Andreas Kling 6a51093ab1 AK: Add String::operator==(const char*).
Without this function, comparing a String to a const char* will instantiate
a temporary String which is obviously not great.

Also add some missing null checks to StringView::operator==(const char*).
2019-06-08 18:32:09 +02:00
Andreas Kling de9edb0169 StringView: operator==(const char*) needs to stop when the view ends.
We were comparing past the end of the view, which was clearly not correct.
2019-06-07 19:22:58 +02:00
Robin Burchell f9ba7adae2 StringView: Make construction of String from a StringView containing a String cheaper
... at the cost of an additional pointer per view.
2019-06-03 20:27:05 +02:00
Robin Burchell b55b6cd7fc AK: Add implicit String -> StringView conversion
And tidy up existing view() users.
2019-06-02 12:55:51 +02:00
Robin Burchell 0dc9af5f7e Add clang-format file
Also run it across the whole tree to get everything using the One True Style.
We don't yet run this in an automated fashion as it's a little slow, but
there is a snippet to do so in makeall.sh.
2019-05-28 17:31:20 +02:00
Andreas Kling 33920df299 AK: Try to use StringViews more for substrings and splitting. 2019-04-16 02:39:16 +02:00
Andreas Kling 461aa550eb AK: Add a StringView class. 2019-04-15 14:56:37 +02:00