From 747d1aaa98d648ab10c864768f5165828951b818 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 17 Jun 2023 14:56:59 +0100 Subject: [PATCH] AK: Use String internally for SourceGenerator Also sneak in a little error propagation. We keep the existing DeprecatedString API for now. --- AK/SourceGenerator.h | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/AK/SourceGenerator.h b/AK/SourceGenerator.h index 9b36eff937..63849901d5 100644 --- a/AK/SourceGenerator.h +++ b/AK/SourceGenerator.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,6 +10,7 @@ #include #include #include +#include #include namespace AK { @@ -17,7 +19,7 @@ class SourceGenerator { AK_MAKE_NONCOPYABLE(SourceGenerator); public: - using MappingType = HashMap; + using MappingType = HashMap; explicit SourceGenerator(StringBuilder& builder, char opening = '@', char closing = '@') : m_builder(builder) @@ -37,16 +39,22 @@ public: SourceGenerator fork() { return SourceGenerator { m_builder, m_mapping, m_opening, m_closing }; } - void set(StringView key, DeprecatedString value) + ErrorOr set(StringView key, String value) { if (key.contains(m_opening) || key.contains(m_closing)) { warnln("SourceGenerator keys cannot contain the opening/closing delimiters `{}` and `{}`. (Keys are only wrapped in these when using them, not when setting them.)", m_opening, m_closing); VERIFY_NOT_REACHED(); } - m_mapping.set(key, move(value)); + TRY(m_mapping.try_set(key, move(value))); + return {}; } - DeprecatedString get(StringView key) const + void set(StringView key, DeprecatedString value) + { + MUST(set(key, MUST(String::from_deprecated_string(value)))); + } + + String get(StringView key) const { auto result = m_mapping.get(key); if (!result.has_value()) { @@ -85,7 +93,7 @@ public: } template - DeprecatedString get(char const (&key)[N]) + String get(char const (&key)[N]) { return get(StringView { key, N - 1 }); } @@ -96,6 +104,12 @@ public: set(StringView { key, N - 1 }, value); } + template + ErrorOr set(char const (&key)[N], String value) + { + return set(StringView { key, N - 1 }, value); + } + template void append(char const (&pattern)[N]) {