summaryrefslogtreecommitdiff
path: root/AK/Utf8View.cpp
AgeCommit message (Collapse)Author
2022-11-24AK: Add Utf8View::iterator_at_byte_offset_without_validation()Andreas Kling
Unlike iterator_at_byte_offset(), this function assumes the provided byte offset is a valid offset into the UTF-8 character stream. This avoids walking the stream from the start.
2022-10-09AK+Tests: Avoid creating invalid code points from malformed UTF-8Ben Wiederhake
Instead of doing anything reasonable, Utf8CodePointIterator returned invalid code points, for example U+123456. However, many callers of this iterator assume that a code point is always at most 0x10FFFF. In fact, this is one of two reasons for the following OSS Fuzz issue: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=49184 This is probably a very old bug. In the particular case of URLParser, AK::is_url_code_point got confused: return /* ... */ || code_point >= 0xA0; If code_point is a "code point" beyond 0x10FFFF, this violates the condition given in the preceding comment, but satisfies the given condition, which eventually causes URLParser to crash. This commit fixes *only* the erroneous UTF-8 decoding, and does not fully resolve OSS-Fuzz#49184.
2022-04-27AK: Put invalid UTF8 debug spam behind a flagkleines Filmröllchen
This is very annoying if we're (intentionally) passing invalid UTF8 into Utf8View.
2022-04-05AK: Invalidate UTF-8 encoded code points larger than U+10ffffTimothy Flynn
On oss-fuzz, the LibJS REPL is provided a file encoded with Windows-1252 with the following contents: /ô¡°½/ The REPL assumes the input file is UTF-8. So in Windows-1252, the above is represented as [0x2f 0xf4 0xa1 0xb0 0xbd 0x2f]. The inner 4 bytes are actually a valid UTF-8 encoding if we only look at the most significant bits to parse leading/continuation bytes. However, it decodes to the code point U+121c3d, which is not a valid code point. This commit adds additional validation to ensure the decoded code point itself is also valid.
2022-04-01Everywhere: Run clang-formatIdan Horowitz
2021-09-18AK: Inline all the trivial Utf8View functionsAndreas Kling
This improves parsing time on a large chunk of JS by ~3%.
2021-09-18AK: Make Utf8View constructors inline and remove C string constructorAndreas Kling
Using StringView instead of C strings is basically always preferable. The only reason to use a C string is because you are calling a C API.
2021-08-18AK: Add Utf8View::byte_offset_of overload for code point index lookupsTimothy Flynn
2021-07-17AK: Track byte length, rather than code point length, in Utf8View::trimTimothy Flynn
Utf8View::trim uses Utf8View::substring_view to return its result, which requires the input to be a byte offset/length rather than code point length.
2021-06-16AK: Add the Utf8View::{contains, trim} helper methodsIdan Horowitz
2021-06-08AK: Utf8CodePointIterator: Don't output full string to debug outputMax Wipfli
When a code point is invalid, the full string was outputted to the debug output. For large strings, this can make the system quite slow. Furthermore, one of the cases incorrectly assumed the data to be null terminated. This patch modifies the debug statements not to print the full string. This fixes oss-fuzz issue 35050.
2021-06-03AK: Do not VERIFY on invalid code point bytes in UTF8ViewDexesTTP
The previous behavior was to always VERIFY that the UTF-8 bytes were valid when iterating over the code points of an UTF8View. This change makes it so we instead output the 0xFFFD 'REPLACEMENT CHARACTER' code point when encountering invalid bytes, and keep iterating the view after skipping one byte. Leaving the decision to the consumer would break symmetry with the UTF32View API, which would in turn require heavy refactoring and/or code duplication in generic code such as the one found in Gfx::Painter and the Shell. To make it easier for the consumers to detect the original bytes, we provide a new method on the iterator that returns a Span over the data that has been decoded. This method is immediately used in the TextNode::compute_text_for_rendering method, which previously did this in a ad-hoc waay. This also add tests for the new behavior in TestUtf8.cpp, as well as reinforcements to the existing tests to check if the underlying bytes match up with their expected values.
2021-06-01Everywhere: codepoint => code pointAndreas Kling
2021-06-01AK: Rename Utf8CodepointIterator => Utf8CodePointIteratorAndreas Kling
2021-06-01AK: Implement Utf8CodepointIterator::peek(size_t)Max Wipfli
This adds a peek method for Utf8CodepointIterator, which enables it to be used in some parsing cases where peeking is necessary. peek(0) is equivalent to operator*, expect that peek() does not contain any assertions and will just return an empty Optional<u32>. This also implements a test case for iterating UTF-8.
2021-05-21AK: Add Utf8View::iterator_at_byte_offset methodMax Wipfli
This implements a method to get a Utf8CodepointIterator at a specified byte offset.
2021-05-21AK: Add substring methods to Utf8ViewMax Wipfli
This patch implements a Unicode-safe substring method, which can be used when offset and length should be specified in actual characters instead of bytes. This can be used to mitigate issues where a string is split in the middle of a UTF-8 multi-byte character, which leads to invalid UTF-8. Furthermore, it implements to common shorthands for substring methods which take only an offset and return the substring until the end of the string.
2021-05-21AK: Change some argument and return types in Utf8View from int to size_tMax Wipfli
This changes the return type of Utf8View::byte_length and the argument types of substring_view from int to size_t.
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-03-25AK: Add starts_with to Utf8ViewIdan Horowitz
Unlike String/StringView::starts_with this compares utf8 code points instead of "characters" (bytes), which is important when handling aribtary utf-8 input that could include overlong characters.
2021-03-12Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)Andreas Kling
Good-bye LogStream. Long live AK::Format!
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.
2021-01-02AK: Use size_t in methods of Utf8View.asynts
2020-12-28LibGfx+AK: Make text elision work with multi-byte charactersAndreas Kling
This was causing WindowServer and Taskbar to crash sometimes when the stars aligned and we tried cutting off a string ending with "..." right on top of an emoji. :^)
2020-12-21Everywhere: Switch from (void) to [[maybe_unused]] (#4473)Lenny Maiorani
Problem: - `(void)` simply casts the expression to void. This is understood to indicate that it is ignored, but this is really a compiler trick to get the compiler to not generate a warning. Solution: - Use the `[[maybe_unused]]` attribute to indicate the value is unused. Note: - Functions taking a `(void)` argument list have also been changed to `()` because this is not needed and shows up in the same grep command.
2020-10-22AK: Make Utf8View and Utf32View more consistentTom
This enables use of these classes in templated code.
2020-10-08AK: Use new format functions.asynts
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-05-18AK: Add a way to get the number of valid bytes in a Utf8ViewAnotherTest
2020-05-17AK: Add Utf8View::length_in_codepoints()Andreas Kling
2020-02-25AK, LibGfx, LibGUI: Initialize various variables to zero.Emanuel Sprung
The not initialized variables can lead to compiler warnings that become errors with the -Werror flag.
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-01-24Meta: Claim copyright for files created by meSergey Bugaev
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-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-10-18UTF-8: Add Utf8CodepointIterator::codepoint_length_in_bytes()Andreas Kling
This allows you to retrieve the length (in bytes) of the codepoint the iterator is currently pointing at.
2019-09-15Utf8View: Don't print potentially unterminated string in debug messageAndreas Kling
2019-09-08AK: Fix buffer overrun in Utf8CodepointIterator::operator++Sergey Bugaev
The old implementation tried to move forward as long as the current byte looks like a UTF-8 character continuation byte (has its two most significant bits set to 10). This is correct as long as we assume the string is actually valid UTF-8, which we do (we also have a separate method that can check whether it is the case). We can't, however, assume that the data after the end of our string is also valid UTF-8 (in fact, we're not even allowed to look at data outside out string, but it happens to a valid memory region most of the time). If the byte after the end of our string also has its most significant bits set to 10, we would move one byte forward, and then fail the m_length > 0 assertion. One way to fix this would be to add a length check inside the loop condition. The other one, implemented in this commit, is to reimplement the whole function in terms of decode_first_byte(), which gives us the length as encoded in the first byte. This also brings it more in line with the other functions around it that do UTF-8 decoding.
2019-09-05Utf8View: Try fixing the travis-ci buildAndreas Kling
There's some overload ambiguity when doing Utf8View("literal")
2019-09-05AK: Log UTF-8 validation errorsSergey Bugaev
2019-09-05AK: Add some more utility methods to Utf8ViewSergey Bugaev
2019-08-28AK: Add a Utf8View type for iterating over UTF-8 codepointsSergey Bugaev
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