Skip to content

Better support for string convertions #76

@konard

Description

@konard
std::string -> std::u8string
std::u8string -> std::string

https://godbolt.org/z/hvbWsohjh

We should also support possible conversions between:

std::u8string
std::u16string
std::u32string

And somewhere here:

template<typename TSource>
std::string to_string(const TSource& source)
{
static constexpr auto to_hex_pointer = [](auto* self)
{
std::ostringstream stream;
stream << self;
return stream.str();
};
if constexpr (std::same_as<std::nullptr_t, TSource>)
{
return "null pointer";
}
if constexpr (std::is_pointer_v<TSource>)
{
if (source == nullptr)
{
return "null pointer";
}
else
{
if constexpr (std::same_as<TSource, void*>)
{
return std::string("void pointer <")
.append(to_hex_pointer(source))
.append(1, '>');
}
else
{
return std::string("pointer <")
.append(to_hex_pointer(source))
.append("> to <")
.append(to_string(*source))
.append(1, '>');
}
}
}
if constexpr (requires { static_cast<std::string>(source); })
{
return static_cast<std::string>(source);
}
if constexpr (requires { std::to_string(source); })
{
return std::to_string(source);
}
if constexpr (requires(std::ostream& stream) { stream << source; })
{
std::ostringstream stream;
stream << source;
return stream.str();
}
// TODO maybe use demangled name
return std::string("instance of ")
.append(typeid(TSource).name());
}

We should support these converstions:

std::u8string -> std::string
std::u16string -> std::u8string -> std::string
std::u32string -> std::u8string -> std::string

But only if std::string is set up to use UTF-8 encoding by compiler.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions