summaryrefslogtreecommitdiff
path: root/AK/StringBuilder.cpp
AgeCommit message (Collapse)Author
2022-07-12Everywhere: Add sv suffix to strings relying on StringView(char const*)sin-ack
Each of these strings would previously rely on StringView's char const* constructor overload, which would call __builtin_strlen on the string. Since we now have operator ""sv, we can replace these with much simpler versions. This opens the door to being able to remove StringView(char const*). No functional changes.
2022-02-27AK: Add a try variant of StringBuilder::append_escaped_for_jsonIdan Horowitz
This will allow us to make a fallible version of the JSON serializers.
2022-02-16AK: Exclude StringBuilder String APIs from the KernelIdan Horowitz
These APIs are only used by userland, and String is OOM-infallible, so let's just ifdef it out of the Kernel.
2022-02-13AK: Use ByteBuffer::append(u8) in StringBuilder single-char appendAndreas Kling
2022-01-24Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOrSam Atkins
Apologies for the enormous commit, but I don't see a way to split this up nicely. In the vast majority of cases it's a simple change. A few extra places can use TRY instead of manual error checking though. :^)
2021-12-30Kernel+AK: Eliminate a couple of temporary String allocationsDaniel Bertalan
2021-11-17AK: Add failable try_* functions to StringBuilderAndreas Kling
These will allow us to start using TRY() with StringBuilder operations.
2021-11-11Everywhere: Pass AK::StringView by valueAndreas Kling
2021-11-10AK: Make ByteBuffer::try_* functions return ErrorOr<void>Andreas Kling
Same as Vector, ByteBuffer now also signals allocation failure by returning an ENOMEM Error instead of a bool, allowing us to use the TRY() and MUST() patterns.
2021-10-15AK: Use UnicodeUtils::code_point_to_utf8 in StringBuilderDaniel Bertalan
2021-09-13AK+Kernel: Avoid unescaped control chars in append_escaped_for_json()Ali Mohammad Pur
Otherwise it could produce invalid JSON.
2021-09-06Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safeAli Mohammad Pur
2021-09-06Everywhere: Use OOM-safe ByteBuffer APIs where possibleAli Mohammad Pur
If we can easily communicate failure, let's avoid asserting and report failure instead.
2021-08-10AK+Kernel: Add StringBuilder::append overload for UTF-16 viewsTimothy Flynn
Currently, to append a UTF-16 view to a StringBuilder, callers must first convert the view to UTF-8 and then append the copy. Add a UTF-16 overload so callers do not need to hold an entire copy in memory.
2021-08-10AK: Convert StringBuilder to use east-constTimothy Flynn
2021-05-31AK: Remove the m_length member for StringBuilderGunnar Beutner
Instead we can just use ByteBuffer::size() which already keeps track of the buffer's size.
2021-05-31AK: Fix accidentally-quadratic behavior in StringBuilderGunnar Beutner
Found by OSS Fuzz: Related commit: 3908a49661a00e15621748dcb2b0424f29433571 Co-authored-by: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
2021-05-31AK: Use ByteBuffer::append for the StringBuilder classGunnar Beutner
Previously the StringBuilder class would use memcpy() to write directly into the ByteBuffer's buffer. Instead we should use the append() method which ensures we don't overrun the buffer.
2021-05-31AK: Replace ByteBuffer::grow with resize()/ensure_capacity()Gunnar Beutner
Previously ByteBuffer::grow() behaved like Vector<T>::resize(). However the function name was somewhat ambiguous - and so this patch updates ByteBuffer to behave more like Vector<T> by replacing grow() with resize() and adding an ensure_capacity() method. This also lets the user change the buffer's capacity without affecting the size which was not previously possible. Additionally this patch makes the capacity() method public (again).
2021-05-30Revert "AK: Fix accidentally-quadratic behavior in StringBuilder"Ben Wiederhake
This reverts commit 2d011961c94ac81700c366537f52208a4c55db92.
2021-05-30AK: Fix accidentally-quadratic behavior in StringBuilderBen Wiederhake
Found by OSS Fuzz: #34451 (old bug) Related commit: 3908a49661a00e15621748dcb2b0424f29433571
2021-05-18AK: StringBuilder should prefer to use its inline capacity firstGunnar Beutner
Previously StringBuilder would start allocating an external buffer once the caller has used up more than half of the inline buffer's capacity. Instead we should prefer to use the inline buffer until it is full and only then start to allocate an external buffer.
2021-05-18AK: Implement StringBuilder::append_as_lowercase(char ch)Max Wipfli
This patch adds a convenience method to AK::StringBuilder which converts ASCII uppercase characters to lowercase before appending them.
2021-05-18AK: Revert removal of StringBuilder::will_append optimizationGunnar Beutner
This was removed as part of the ByteBuffer changes but the allocation optimization is still necessary at least for non-SerenityOS targets where malloc_good_size() isn't supported or returns a small value and causes a whole bunch of unnecessary reallocations.
2021-05-16AK: Turn ByteBuffer into a value typeGunnar Beutner
Previously ByteBuffer would internally hold a RefPtr to the byte buffer and would behave like a reference type, i.e. copying a ByteBuffer would not create a duplicate byte buffer, but rather two objects which refer to the same internal buffer. This also changes ByteBuffer so that it has some internal capacity much like the Vector<T> type. Unlike Vector<T> however a byte buffer's data may be uninitialized. With this commit ByteBuffer makes use of the kmalloc_good_size() API to pick an optimal allocation size for its internal buffer.
2021-05-07AK: Remove StringBuilder::appendf()Andreas Kling
All users have been converted to using AK::Format via appendff().
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
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-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...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.
2020-11-24AK: Add some inline capacity to StringBuilderAndreas Kling
This patch adds a 128-byte inline buffer that we use before switching to using a dynamically growing ByteBuffer. This allows us to avoid heap allocations in many cases, and totally incidentally also speeds up @nico's favorite test, "disasm /bin/id" more than 2x. :^)
2020-11-15Everywhere: Add missing <AK/ByteBuffer.h> includesAndreas Kling
All of these files were getting ByteBuffer.h from someone else and then using it. Let's include it explicitly.
2020-11-02AK+Kernel: Escape JSON keys & valuesAndreas Kling
Grab the escaping logic from JSON string value serialization and use it for serializing all keys and values. Fixes #3917.
2020-08-05Unicode: Try s/codepoint/code_point/g againNico Weber
This time, without trailing 's'. Ran: git grep -l 'codepoint' | xargs sed -ie 's/codepoint/code_point/g
2020-08-05Revert "Unicode: s/codepoint/code_point/g"Nico Weber
This reverts commit ea9ac3155d1774f13ac4e9a96605c0e85a8f299e. It replaced "codepoint" with "code_points", not "code_point".
2020-08-03Unicode: s/codepoint/code_point/gAndreas Kling
Unicode calls them "code points" so let's follow their style.
2020-06-04AK: Add StringBuilder::append_codepoint(u32)Andreas Kling
Also, implement append(Utf32View) using it.
2020-05-17AK: Add StringBuilder::append(Utf32View)Andreas Kling
This encodes the incoming UTF-32 sequence as UTF-8.
2020-05-15AK: StringBuilder with 0 initial capacity shouldn't build null StringAndreas Kling
With 0 initial capacity, we don't allocate an underlying ByteBuffer for the StringBuilder, which would then lead to a null String() being returned from to_string(). This patch makes sure we always build a valid String.
2020-03-23AK: Reduce header dependency graph of String.hAndreas Kling
String.h no longer pulls in StringView.h. We do this by moving a bunch of String functions out-of-line.
2020-03-08AK: Reduce code duplication in StringBuilderhowar6hill
2020-03-08AK: Improve the API of StringBuilderhowar6hill
2020-03-08AK: Move memory stuff (fast memcpy, etc) to a separate headerAndreas Kling
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-01AK: Remove unnecessary casts to size_t, after Vector changesAndreas Kling
Now that Vector uses size_t, we can remove a whole bunch of redundant casts to size_t.
2020-02-14AK: Add a forward declaration headerAndreas Kling
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-09AK: Make StringBuilder::to_string() non-destructiveAndreas Kling
This was an artifact of an earlier design of StringBuilder where I had attempted to preserve the same allocation from build to final String.
2020-01-18Meta: Add license header to source filesAndreas Kling
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.
2019-12-09AK: Use size_t for the length of stringsAndreas Kling
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-09-30ByteBuffer: Remove pointer() in favor of data()Andreas Kling
We had two ways to get the data inside a ByteBuffer. That was silly.
2019-09-28AK: Add StringBuilder::string_view() and StringBuilder::clear()Sergey Bugaev
The former allows you to inspect the string while it's being built. It's an explicit method rather than `operator StringView()` because you must remember you can only look at it in between modifications; appending to the StringBuilder invalidates the StringView. The latter lets you clear the state of a StringBuilder explicitly, to start from an empty string again.
2019-07-08StringBuilder: Reset the internal builder length after building.Andreas Kling
This puts the StringBuilder back into a pristine state, allowing you to use it to build more strings after you've built one.
2019-07-08StringView: Rename characters() to characters_without_null_termination().Andreas Kling
This should make you think twice before trying to use the const char* from a StringView as if it's a null-terminated string.