diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-21 12:34:01 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-22 01:03:13 +0000 |
commit | 12c8bc3e856300b7a42c2027df042ee22f21077f (patch) | |
tree | 5aa34c89c1fb163a1912d09346874bdcfdfc7e6d /AK/String.h | |
parent | 8aca8e82cbaefc92f7622e8f793c95aa7d07411f (diff) | |
download | serenity-12c8bc3e856300b7a42c2027df042ee22f21077f.zip |
AK: Add a String factory to create a string from a single code point
Diffstat (limited to 'AK/String.h')
-rw-r--r-- | AK/String.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/AK/String.h b/AK/String.h index dc73fe7d55..7aca7b50dc 100644 --- a/AK/String.h +++ b/AK/String.h @@ -1,11 +1,13 @@ /* * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include <AK/CharacterTypes.h> #include <AK/Concepts.h> #include <AK/Format.h> #include <AK/Forward.h> @@ -16,6 +18,7 @@ #include <AK/StringView.h> #include <AK/Traits.h> #include <AK/Types.h> +#include <AK/UnicodeUtils.h> #include <AK/Vector.h> namespace AK { @@ -74,6 +77,22 @@ public: return String { short_string }; } + // Creates a new String from a single code point. + static constexpr String from_code_point(u32 code_point) + { + VERIFY(is_unicode(code_point)); + + ShortString short_string; + size_t i = 0; + + auto length = UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) { + short_string.storage[i++] = static_cast<u8>(byte); + }); + short_string.byte_count_and_short_string_flag = (length << 1) | SHORT_STRING_FLAG; + + return String { short_string }; + } + // Creates a new String by case-transforming this String. Using these methods require linking LibUnicode into your application. ErrorOr<String> to_lowercase(Optional<StringView> const& locale = {}) const; ErrorOr<String> to_uppercase(Optional<StringView> const& locale = {}) const; |