Repository: LadybirdBrowser/ladybird
File: Libraries/LibUnicode/ICU.h (C/C++)

Code before:
class TimeZoneData {
public:
    static Optional<TimeZoneData&> for_time_zone(StringView time_zone);

    ALWAYS_INLINE icu::TimeZone& time_zone() { return *m_time_zone; }

private:
    explicit TimeZoneData(NonnullOwnPtr<icu::TimeZone>);

    NonnullOwnPtr<icu::TimeZone> m_time_zone;
};

constexpr bool icu_success(UErrorCode code)
{
    return static_cast<bool>(U_SUCCESS(code));
}

constexpr bool icu_failure(UErrorCode code)
{
    return static_cast<bool>(U_FAILURE(code));
}

constexpr void verify_icu_success(UErrorCode code)
{
    if (!icu_success(code)) {
        dbgln("Call to ICU library failed with code {}", u_errorName(code));
        VERIFY_NOT_REACHED();
    }
}

ALWAYS_INLINE icu::StringPiece icu_string_piece(StringView string)
{
    return { string.characters_without_null_termination(), static_cast<i32>(string.length()) };
}

ALWAYS_INLINE icu::UnicodeString icu_string(StringView string)
{
    return icu::UnicodeString::fromUTF8(icu_string_piece(string));
}

// If the Utf16View has ASCII storage, this creates an owned icu::UnicodeString. Otherwise, the icu::UnicodeString is
// unowned (i.e. it is effectively a view).
ALWAYS_INLINE icu::UnicodeString icu_string(Utf16View const& string)
{
    if (string.has_ascii_storage())
        return icu::UnicodeString::fromUTF8(icu_string_piece(string.bytes()));
    return { false, string.utf16_span().data(), static_cast<i32>(string.length_in_code_units()) };
}

Vector<icu::UnicodeString> icu_string_list(ReadonlySpan<Utf16String> strings);


Review comment (suggestion):
Last nit:
we've typically found people notice errors like these better if we make them pop more. Something like:

```c++
dbgln("\033[31;1mICU error\033[0m: {}", u_errorName(code));
```

You could add a `SourceLocation` to the function as well if you'd like, but no big deal.

Code after:

class TimeZoneData {
public:
    static Optional<TimeZoneData&> for_time_zone(StringView time_zone);

    ALWAYS_INLINE icu::TimeZone& time_zone() { return *m_time_zone; }

private:
    explicit TimeZoneData(NonnullOwnPtr<icu::TimeZone>);

    NonnullOwnPtr<icu::TimeZone> m_time_zone;
};

constexpr bool icu_success(UErrorCode code)
{
    return static_cast<bool>(U_SUCCESS(code));
}

constexpr bool icu_failure(UErrorCode code)
{
    return static_cast<bool>(U_FAILURE(code));
}

inline void verify_icu_success(UErrorCode code, SourceLocation location = SourceLocation::current())
{
    if (icu_failure(code)) [[unlikely]] {
        dbgln("\033[31;1mICU error\033[0m: {} {}", u_errorName(code), location);
        VERIFY_NOT_REACHED();
    }
}

ALWAYS_INLINE icu::StringPiece icu_string_piece(StringView string)
{
    return { string.characters_without_null_termination(), static_cast<i32>(string.length()) };
}

ALWAYS_INLINE icu::UnicodeString icu_string(StringView string)
{
    return icu::UnicodeString::fromUTF8(icu_string_piece(string));
}

// If the Utf16View has ASCII storage, this creates an owned icu::UnicodeString. Otherwise, the icu::UnicodeString is
// unowned (i.e. it is effectively a view).
ALWAYS_INLINE icu::UnicodeString icu_string(Utf16View const& string)
{
    if (string.has_ascii_storage())
        return icu::UnicodeString::fromUTF8(icu_string_piece(string.bytes()));
    return { false, string.utf16_span().data(), static_cast<i32>(string.length_in_code_units()) };
}

Vector<icu::UnicodeString> icu_string_list(ReadonlySpan<Utf16String> strings);