1. If this constructor insists on null-terminated bytes, it should say so in the name (e.g. validatingNullTerminatedUTF8:), and it should crash deterministically if it gets non-terminated bytes, or
2. It should not require null-terminated bytes
Could we get a method that takes a [UInt8] directly and performs the same basic function? In my experience I have frequently wanted such a thing (primarily when debugging things) when working with binary protocols that have embedded ASCII data.
- Daniel
···
On Apr 6, 2016, at 9:51 PM, Dmitri Gribenko via swift-dev <swift-dev@swift.org> wrote:
On Wed, Apr 6, 2016 at 9:16 PM, Drew Crawford via swift-dev > <swift-dev@swift.org> wrote:
and it should crash
deterministically if it gets non-terminated bytes, or
It can't, how would you check for this, only given a pointer?
2. It should not require null-terminated bytes
This operation converts a C string to a Swift string, so (2) is a non-starter.
I think the root of the surprise here is that the compiler converts
[UInt8] into an unsafe pointer. This is appropriate when the callee
is a C API, but usually not appropriate when it is a Swift API. This
is not the first time when this implicit conversion causes surprise.
I think we should discuss scoping that conversion to only C and
Objective-C callees.
But I agree with you, we should have a similar operation that works on
arbitrary collections.
Dmitri
···
On Wed, Apr 6, 2016 at 9:54 PM, Daniel Dunbar <daniel_dunbar@apple.com> wrote:
Could we get a method that takes a [UInt8] directly and performs the same basic function?
Could we get a method that takes a [UInt8] directly and performs the same basic function?
I think the root of the surprise here is that the compiler converts
[UInt8] into an unsafe pointer. This is appropriate when the callee
is a C API, but usually not appropriate when it is a Swift API. This
is not the first time when this implicit conversion causes surprise.
I think we should discuss scoping that conversion to only C and
Objective-C calls.
+1 from me.
But I agree with you, we should have a similar operation that works on
arbitrary collections.
Cool, thanks.
- Daniel
···
On Apr 6, 2016, at 9:58 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:
On Wed, Apr 6, 2016 at 9:54 PM, Daniel Dunbar <daniel_dunbar@apple.com> wrote: