Stream: ideas

Topic: Str.fromUtf8Lossy


view this post on Zulip Brendan Hansknecht (Jun 20 2023 at 23:57):

Would we want Str.fromUtf8Lossy in the standard library?

It would mostly be used for printing bytes. If the bytes contains values that are not printable in utf8 or don't match utf8 in general, they will be replaced with a filler character.

It would match this rust function: https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8_lossy

view this post on Zulip Richard Feldman (Jun 21 2023 at 11:14):

I definitely think so, although I wonder if there's a better name

view this post on Zulip Brian Carroll (Jun 21 2023 at 16:31):

It's called the "replacement character", so how about fromUtf8WithReplacement?

view this post on Zulip Brendan Hansknecht (Jun 22 2023 at 17:33):

My main issue with WithReplacement is that makes me think of Str.replaceEach. As such, it feels like it should take extra parameters about what to replace or something of that nature.

As such, I think that it would likely make the method less discoverable and more confusing, especially at a glance.

view this post on Zulip Brian Carroll (Jun 22 2023 at 17:44):

fromUtf8WithFallback?

view this post on Zulip Brian Carroll (Jun 22 2023 at 17:45):

fromUtf8WithReplacementChar

view this post on Zulip Brendan Hansknecht (Jun 22 2023 at 17:47):

Ok yeah, those both sound better to me.

view this post on Zulip Luke Boswell (Sep 26 2024 at 08:03):

I just wanted to clarify, do we want to add Str.fromUtf8Lossy : List U8 -> Str to the builtins?

My understanding is that this will replace anything that can't be represented in a string with the Unicode Replacement Character (U+FFFD) �

Can this be done in pure roc, or does it need to be a low level in zig?

view this post on Zulip Luke Boswell (Sep 26 2024 at 08:04):

Lol, I created what I thought was a new thread in ideas ... turns out there was one from a previous discussion.

view this post on Zulip Luke Boswell (Sep 26 2024 at 08:04):

I'm adding an Issue for this

view this post on Zulip Luke Boswell (Sep 26 2024 at 08:10):

Also, do we want to add Str.fromUtf16WithFallback?

view this post on Zulip Luke Boswell (Sep 26 2024 at 08:16):

GH Issue to track this https://github.com/roc-lang/roc/issues/7117


Last updated: Jun 16 2026 at 16:19 UTC