summaryrefslogtreecommitdiff
path: root/AK/StringBuilder.cpp
AgeCommit message (Collapse)Author
2023-03-09AK: Remove infallible version of StringBuilder::to_byte_bufferLinus Groh
Also drop the try_ prefix from the fallible function, as it is no longer needed to distinguish the two.
2023-03-09AK: Introduce a fallible version of StringBuilder::to_byte_bufferKarol Baraniecki
Name it StringBuilder::try_to_byte_buffer accordingly :^)
2023-02-15AK: Add StringBuilder::to_fly_string()Sam Atkins
2023-01-27AK: Remove StringBuilder::build() in favor of to_deprecated_string()Linus Groh
Having an alias function that only wraps another one is silly, and keeping the more obvious name should flush out more uses of deprecated strings. No behavior change.
2023-01-08AK: Make StringBuilder::try_append_code_point actually fallibleTimothy Flynn
It currently uses the non-fallible `append` method to append each UTF-8 encoded byte of the code point.
2023-01-02Everywhere: Remove unused includes of AK/StdLibExtras.hBen Wiederhake
These instances were detected by searching for files that include AK/StdLibExtras.h, but don't match the regex: \\b(abs|AK_REPLACED_STD_NAMESPACE|array_size|ceil_div|clamp|exchange|for ward|is_constant_evaluated|is_power_of_two|max|min|mix|move|_RawPtr|RawP tr|round_up_to_power_of_two|swap|to_underlying)\\b (Without the linebreaks.) This regex is pessimistic, so there might be more files that don't actually use any "extra stdlib" functions. In theory, one might use LibCPP to detect things like this automatically, but let's do this one step after another.
2022-12-11AK: Add a fallible StringBuilder::create() factory functionAli Mohammad Pur
This is nice, and is also used by the Jakt runtime.
2022-12-06AK: Introduce the new String, replacement for DeprecatedStringAndreas Kling
DeprecatedString (formerly String) has been with us since the start, and it has served us well. However, it has a number of shortcomings that I'd like to address. Some of these issues are hard if not impossible to solve incrementally inside of DeprecatedString, so instead of doing that, let's build a new String class and then incrementally move over to it instead. Problems in DeprecatedString: - It assumes string allocation never fails. This makes it impossible to use in allocation-sensitive contexts, and is the reason we had to ban DeprecatedString from the kernel entirely. - The awkward null state. DeprecatedString can be null. It's different from the empty state, although null strings are considered empty. All code is immediately nicer when using Optional<DeprecatedString> but DeprecatedString came before Optional, which is how we ended up like this. - The encoding of the underlying data is ambiguous. For the most part, we use it as if it's always UTF-8, but there have been cases where we pass around strings in other encodings (e.g ISO8859-1) - operator[] and length() are used to iterate over DeprecatedString one byte at a time. This is done all over the codebase, and will *not* give the right results unless the string is all ASCII. How we solve these issues in the new String: - Functions that may allocate now return ErrorOr<String> so that ENOMEM errors can be passed to the caller. - String has no null state. Use Optional<String> when needed. - String is always UTF-8. This is validated when constructing a String. We may need to add a bypass for this in the future, for cases where you have a known-good string, but for now: validate all the things! - There is no operator[] or length(). You can get the underlying data with bytes(), but for iterating over code points, you should be using an UTF-8 iterator. Furthermore, it has two nifty new features: - String implements a small string optimization (SSO) for strings that can fit entirely within a pointer. This means up to 3 bytes on 32-bit platforms, and 7 bytes on 64-bit platforms. Such small strings will not be heap-allocated. - String can create substrings without making a deep copy of the substring. Instead, the superstring gets +1 refcount from the substring, and it acts like a view into the superstring. To make substrings like this, use the substring_with_shared_superstring() API. One caveat: - String does not guarantee that the underlying data is null-terminated like DeprecatedString does today. While this was nifty in a handful of places where we were calling C functions, it did stand in the way of shared-superstring substrings.
2022-12-06Everywhere: Rename to_{string => deprecated_string}() where applicableLinus Groh
This will make it easier to support both string types at the same time while we convert code, and tracking down remaining uses. One big exception is Value::to_string() in LibJS, where the name is dictated by the ToString AO.
2022-12-06AK+Everywhere: Rename String to DeprecatedStringLinus Groh
We have a new, improved string type coming up in AK (OOM aware, no null state), and while it's going to use UTF-8, the name UTF8String is a mouthful - so let's free up the String name by renaming the existing class. Making the old one have an annoying name will hopefully also help with quick adoption :^)
2022-09-15StringBuilder: Add try_append_repeated() and append_repeated()Lucas CHOLLET
This two methods add the character as many times as specified by the second parameter.
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