UnsafePointer<Int8> and C-String


(Rien) #1

The documentation at https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html shows that it is possible to use String where an UnsafePointer<Int8> is needed.
API calls to C that need a char* are translated to UnsafePointer<Int8>.
It follows that we can use a String where a char* is needed.

However in C a string is not only a char*, it is also null-terminated.

What I cannot find though is the guarantee that the buffer where the String is converted to an UTF8 sequence is always null-terminated.

I very much suspect it is, and all my tests did find a null-terminated string.

Question: Does anybody know for sure that the buffer is always null-terminated? (a link to where I can check this would be most appreciated)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl


(Jeremy Pereira) #2

The documentation at https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html shows that it is possible to use String where an UnsafePointer<Int8> is needed.
API calls to C that need a char* are translated to UnsafePointer<Int8>.
It follows that we can use a String where a char* is needed.

However in C a string is not only a char*, it is also null-terminated.

What I cannot find though is the guarantee that the buffer where the String is converted to an UTF8 sequence is always null-terminated.

I very much suspect it is, and all my tests did find a null-terminated string.

Question: Does anybody know for sure that the buffer is always null-terminated? (a link to where I can check this would be most appreciated)

String has a property called utf8CString which is documented as "A contiguously stored null-terminated UTF-8 representation of the string.” I’d use that.

···

On 3 Jan 2017, at 12:28, Rien via swift-users <swift-users@swift.org> wrote:

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Ole Begemann) #3

What I cannot find though is the guarantee that the buffer where the
String is converted to an UTF8 sequence is always null-terminated.

I very much suspect it is, and all my tests did find a null-terminated
string.

Question: Does anybody know for sure that the buffer is always
null-terminated? (a link to where I can check this would be most
appreciated)

I'm not 100% certain, but I think the compiler uses this function to convert a String argument to a pointer:

/// Derive a UTF-8 pointer argument from a value string parameter.
public // COMPILER_INTRINSIC
func _convertConstStringToUTF8PointerArgument<
   ToPointer : _Pointer
>(_ str: String) -> (AnyObject?, ToPointer) {
   let utf8 = Array(str.utf8CString)
   return _convertConstArrayToPointerArgument(utf8)
}

Source: Pointer.swift [1]

This in turn calls `String.utf8CString` [2], which indeed returns a null-terminated string.

[1]: https://github.com/apple/swift/blob/master/stdlib/public/core/Pointer.swift#L85-L92
[2]: https://github.com/apple/swift/blob/master/stdlib/public/core/StringUTF8.swift#L383-L405


(Rien) #4

Thanks Jeremy & Ole,

That was indeed my expectation, but I had not thought of looking in the source code!

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl

···

On 03 Jan 2017, at 16:31, Ole Begemann <ole@oleb.net> wrote:

What I cannot find though is the guarantee that the buffer where the
String is converted to an UTF8 sequence is always null-terminated.

I very much suspect it is, and all my tests did find a null-terminated
string.

Question: Does anybody know for sure that the buffer is always
null-terminated? (a link to where I can check this would be most
appreciated)

I'm not 100% certain, but I think the compiler uses this function to convert a String argument to a pointer:

/// Derive a UTF-8 pointer argument from a value string parameter.
public // COMPILER_INTRINSIC
func _convertConstStringToUTF8PointerArgument<
ToPointer : _Pointer
>(_ str: String) -> (AnyObject?, ToPointer) {
let utf8 = Array(str.utf8CString)
return _convertConstArrayToPointerArgument(utf8)
}

Source: Pointer.swift [1]

This in turn calls `String.utf8CString` [2], which indeed returns a null-terminated string.

[1]: https://github.com/apple/swift/blob/master/stdlib/public/core/Pointer.swift#L85-L92
[2]: https://github.com/apple/swift/blob/master/stdlib/public/core/StringUTF8.swift#L383-L405