String.range(of...) returns a range outside the string


I'm pretty new to Swift, so I might be completely off, but I find this behaviour surprising/buggy(?):

let str = "bar"
if str.range(of: "r\u{0}")!.upperBound > str.endIndex {

I'd expect this range() to not match, and also the result of String.range() to never exceed the string's boundaries.

Am I off or is this a bug?


I think this is one for

Thanks, tracked here

Could this be related to the recent change that strings are internally stored as a nul-terminated UTF-8 sequence?

Since range(of:) is a Foundation API, you're likely better off filing a bug using Feedback Assistant.

1 Like