Commit Graph

99 Commits

Author SHA1 Message Date
Tobias Christiansen
4016e04061 AK: Move bijective-base-conversion into AK/String
This allows everybody to create a String version of their number
in a arbitrary bijective base. Bijective base meaning that the mapping
doesn't have a 0. In the usual mapping to the alphabet the follower
after 'Z' is 'AA'.
The mapping using the (uppercase) alphabet is used as a standard but
can be overridden specifying 'base' and 'map'.

The code was directly yanked from the Spreadsheet.
2021-05-01 01:19:40 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Andreas Kling
edf0b14e23 AK: Remove String::format()
There are no more clients of this function, everyone has been converted
to String::formatted().
2021-04-21 23:49:03 +02:00
Andreas Kling
873da38d0e AK: Remove String-from-StringView optimization
We had an unusual optimization in AK::StringView where constructing
a StringView from a String would cause it to remember the internal
StringImpl pointer of the String.

This was used to make constructing a String from a StringView fast
and copy-free.

I tried removing this optimization and indeed we started seeing a
ton of allocation traffic. However, all of it was due to a silly
pattern where functions would take a StringView and then go on
to create a String from it.

I've gone through most of the code and updated those functions to
simply take a String directly instead, which now makes this
optimization unnecessary, and indeed a source of bloat instead.

So, let's get rid of it and make StringView a little smaller. :^)
2021-04-17 01:27:31 +02:00
Andreas Kling
5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
Linus Groh
4fafe14691 AK: Add String{,Utils}::to_snakecase()
This is an improved version of WrapperGenerator's snake_name(), which
seems like the kind of thing that could be useful elsewhere but would
end up getting duplicated - so let's add this to AK::String instead,
like to_{lowercase,uppercase}().
2021-02-21 19:47:47 +01:00
Ben Wiederhake
fa2b5ca6f6 Everywhere: Remove unnecessary headers 2/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).
2021-02-08 18:03:57 +01:00
AnotherTest
39442e6d4f AK: Add String{View,}::find(StringView)
I personally mistook `find_first_of(StringView)` to be analogous to this
so let's add a `find()` method that actually searches the string.
2021-01-12 23:36:20 +01:00
Lenny Maiorani
e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
Andrew Kaster
0c51778510 AK: Move String::number entirely to header file
Use SFINAE to enforce the fact that it's supposed to only be called for
Arithmetic types, rather than counting on the linker to tell us that an
instantiation of String::number(my_arg) was not found. This also adds
String::number for floating point types as a side-effect.
2020-12-30 11:32:20 +01:00
Stephan Unverwerth
d3524f47a0 LibJS: Implement (mostly) spec compliant version of Number.toString() 2020-12-27 23:04:09 +01:00
Sahan Fernando
37df4bbd90 AK: Generalize AK::String::to_int() for more types 2020-12-21 00:15:44 +01:00
Andreas Kling
5d0fda3d39 AK: Add String::substring(start)
This is a convenience API when you just want the rest of the string
starting at some index. We already had substring_view() in the same
flavor, so this is a complement to that.
2020-12-10 20:42:11 +01:00
asynts
78eff163ea AK: Add String::substring_view(size_t). 2020-12-09 21:05:06 +01:00
Andreas Kling
adabcf24ec Everywhere: Add missing <AK/ByteBuffer.h> includes
All of these files were getting ByteBuffer.h from someone else and then
using it. Let's include it explicitly.
2020-11-15 13:11:21 +01:00
AnotherTest
0801b1fada AK: Make String::matches() capable of reporting match positions too
Also, rewrite StringUtils::match(), because the old implementation was
fairly broken, e.g. "acdcxb" would *not* match "a*?b".
2020-10-29 11:53:01 +01:00
Tom
25e7225782 AK: Enhance String::contains to allow case-insensitive searches 2020-10-22 15:23:45 +02:00
Nico Weber
cc765e14ca AK: Move StringImpl::operator== implementation into StringImpl 2020-10-05 17:35:27 +02:00
Linus Groh
bcfc6f0c57 Everywhere: Fix more typos 2020-10-03 12:36:49 +02:00
asynts
6351a56d27 AK+Format: Do some housekeeping in the format implementation. 2020-10-02 20:48:19 +02:00
AnotherTest
5fbec2b003 AK: Move trim_whitespace() into StringUtils and add it to StringView
No behaviour change; also patches use of `String::TrimMode` in LibJS.
2020-09-27 21:14:18 +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
eaeb793454 AK: Use format in String::number. 2020-09-22 15:06:40 +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
Ben Wiederhake
01dabb65f2 AK: Remove redundant declaration in String.cpp
It already includes AK/Memory.h, which includes Kernel/StdLib.h, which.
declares strstr().
2020-08-24 00:45:03 +02:00
Ben Wiederhake
04f494fc44 AK: Prefer snprintf over sprintf 2020-08-22 20:55:10 +02:00
Peter Elliott
e57a432118 AK: Make String::substring() return non-null for 0-length strings
This also makes String::split() give non-null strings when keep_empty is
true.
2020-07-28 17:07:22 +02:00
Luke
a5ecb9bd6b AK: Add case insensitive version of starts_with 2020-07-21 01:08:32 +02:00
Nico Weber
97cea9e61c AK: Give String::index_of() an optional second "start" argument 2020-07-13 15:00:14 +02:00
Andreas Kling
fdfda6dec2 AK: Make string-to-number conversion helpers return Optional
Get rid of the weird old signature:

- int StringType::to_int(bool& ok) const

And replace it with sensible new signature:

- Optional<int> StringType::to_int() const
2020-06-12 21:28:55 +02:00
Brian Gianforcaro
332f96e7ca AK: Add case insensitive String::ends_with support
FileSystemPath::has_extension was jumping through hoops and allocating
memory to do a case insensitive comparison needlessly. Extend the
existing String::ends_with method to allow the caller to specify the
case sensitivity required.
2020-05-26 13:17:19 +02:00
Brian Gianforcaro
8e4b858b3f AK: Move String::ends_with implementation to StringUtils
Centralizing so it can be used by other string implementations
2020-05-26 13:17:19 +02:00
Linus Groh
1febee768d AK: Replace String::trim_spaces() with String::trim_whitespace()
As suggested by @awesomekling in a code review and (initially) ignored
by me :^)

Implementation is roughly based on LibJS's trim_string(), but with a fix
for trimming all-whitespace strings.
2020-05-13 09:36:20 +02:00
Linus Groh
d20e26c690 AK: Add String::trim_spaces() 2020-05-11 10:48:54 +02:00
Itamar
e207de8449 LibELF: Add find_demangled_function
Also, added AK::String::index_of and fixed a bug in ELF::Loader::symbol_ptr
2020-04-13 23:20:59 +02:00
Andreas Kling
c993c7e3dc AK: String::contains() should say no if needle or haystack is null 2020-04-11 14:33:05 +02:00
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
95cc4c7e74 AK: Add some string comparison operators
Some of these are very inefficient. It's nice to have some optimization
opportunities in the future though. :^)
2020-03-28 09:11:13 +01:00
Andreas Kling
7d862dd5fc AK: Reduce header dependency graph of String.h
String.h no longer pulls in StringView.h. We do this by moving a bunch
of String functions out-of-line.
2020-03-23 13:48:44 +01:00
Andreas Kling
c4a6d6ae9f AK: Add FlyString::to_lowercase() and LogStream operator<<(FlyString) 2020-03-22 19:07:02 +01: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
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
Andreas Kling
22d0a6d92f AK: Remove unnecessary casts to size_t, after Vector changes
Now that Vector uses size_t, we can remove a whole bunch of redundant
casts to size_t.
2020-03-01 12:58:22 +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
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
3e486f75ff AK: Move escape_html_entities() from LibHTML to AK
This sort of thing can be useful to things that don't want to link with
all of LibHTML.
2020-02-13 08:46:00 +01:00
Andreas Kling
90b1dafeff AK: Break String::number() overloads into i/l/ll and u/ul/ull
Now that we're trying to be more portable, we can't only rely on using
i32/u32 and i64/u64 since different systems have different combinations
of int/long/long long and unsigned/unsigned long/unsigned long long.
2020-02-05 19:13:44 +01:00
Sergey Bugaev
6a64077ed7 AK: Also add a keep_empty argument to String::split[_limit]()
Just like String[View]::split_view() has already.
2020-01-22 21:22:23 +01:00