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

Hi,

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 {
    print("Weird!")
}

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?

Thanks!

I think this is one for bugs.swift.org.

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
Terms of Service

Privacy Policy

Cookie Policy