From 230cb3b0cb9664839c56f80d6dff3706fffc2243 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 28 Jan 2023 21:08:14 +0100 Subject: [PATCH] AK: Add DeprecatedString::from_utf8() This will be used in Jakt to help transition off of DeprecatedString. --- AK/DeprecatedString.cpp | 7 +++++++ AK/DeprecatedString.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/AK/DeprecatedString.cpp b/AK/DeprecatedString.cpp index 0b1064f9900..9c45cced074 100644 --- a/AK/DeprecatedString.cpp +++ b/AK/DeprecatedString.cpp @@ -455,4 +455,11 @@ DeprecatedStringCodePointIterator DeprecatedString::code_points() const return DeprecatedStringCodePointIterator(*this); } +ErrorOr DeprecatedString::from_utf8(ReadonlyBytes bytes) +{ + if (!Utf8View(bytes).validate()) + return Error::from_string_literal("DeprecatedString::from_utf8: Input was not valid UTF-8"); + return DeprecatedString { StringImpl::create(bytes) }; +} + } diff --git a/AK/DeprecatedString.h b/AK/DeprecatedString.h index 0155fcdf19a..35474f9bffc 100644 --- a/AK/DeprecatedString.h +++ b/AK/DeprecatedString.h @@ -95,6 +95,8 @@ public: DeprecatedString(DeprecatedFlyString const&); + static ErrorOr from_utf8(ReadonlyBytes); + [[nodiscard]] static DeprecatedString repeated(char, size_t count); [[nodiscard]] static DeprecatedString repeated(StringView, size_t count);