AK: Add operator""_{short_,}string to create a String from a literal
We briefly discussed this when adding the new String type but couldn't settle on a name. However, having to use String::from_utf8() on every literal string is a bit unwieldy, so let's have these options available! Naming-wise '_string' is not as short as 'sv' but should be relatively clear; it also matches '_bigint' and '_ubigint' in length. '_short_string' may be longer than the actual string itself, but it's still an improvement over the static function :^) Since our C++ source files are UTF-8 encoded anyway, it should be impossible to create a string literal with invalid UTF-8, so including that in the name is not as important as in the function that can receive arbitrary data.
This commit is contained in:
parent
956450df00
commit
85414d9338
Notes:
sideshowbarker
2024-07-17 14:33:07 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/85414d9338 Pull-request: https://github.com/SerenityOS/serenity/pull/17619 Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/MacDue Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/trflynn89
2 changed files with 15 additions and 0 deletions
|
@ -69,6 +69,11 @@ struct Formatter<FlyString> : Formatter<StringView> {
|
|||
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE AK::ErrorOr<AK::FlyString> operator""_fly_string(char const* cstring, size_t length)
|
||||
{
|
||||
return AK::FlyString::from_utf8(AK::StringView(cstring, length));
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::FlyString;
|
||||
#endif
|
||||
|
|
10
AK/String.h
10
AK/String.h
|
@ -259,3 +259,13 @@ struct Formatter<String> : Formatter<StringView> {
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE AK::ErrorOr<AK::String> operator""_string(char const* cstring, size_t length)
|
||||
{
|
||||
return AK::String::from_utf8(AK::StringView(cstring, length));
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE consteval AK::String operator""_short_string(char const* cstring, size_t length)
|
||||
{
|
||||
return AK::String::from_utf8_short_string(AK::StringView(cstring, length));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue