@@ -30,24 +30,25 @@ extension String : _ObjectiveCBridgeable {
30
30
} else if type ( of: source) == _NSCFString. self {
31
31
let cf = unsafeBitCast ( source, to: CFString . self)
32
32
let length = CFStringGetLength ( cf)
33
- if let str = CFStringGetCStringPtr ( cf, CFStringEncoding ( kCFStringEncodingUTF8) ) {
34
- result = str. withMemoryRebound ( to: UInt8 . self, capacity: length) { ptr in
35
- let buffer = UnsafeBufferPointer ( start: ptr, count: length)
36
- return String ( decoding: buffer, as: UTF8 . self)
33
+ if length == 0 {
34
+ result = " "
35
+ } else if let ptr = CFStringGetCStringPtr ( cf, CFStringEncoding ( kCFStringEncodingUTF8) ) {
36
+ result = ptr. withMemoryRebound ( to: UInt8 . self, capacity: length) {
37
+ return String ( decoding: UnsafeBufferPointer ( start: $0, count: length) , as: UTF8 . self)
37
38
}
39
+ } else if let ptr = CFStringGetCharactersPtr ( cf) {
40
+ result = String ( decoding: UnsafeBufferPointer ( start: ptr, count: length) , as: UTF16 . self)
38
41
} else {
39
42
let buffer = UnsafeMutablePointer< UniChar> . allocate( capacity: length)
40
43
CFStringGetCharacters ( cf, CFRangeMake ( 0 , length) , buffer)
41
44
42
- let str = String ( decoding: UnsafeBufferPointer ( start: buffer, count: length) , as: UTF16 . self)
45
+ result = String ( decoding: UnsafeBufferPointer ( start: buffer, count: length) , as: UTF16 . self)
43
46
buffer. deinitialize ( count: length)
44
47
buffer. deallocate ( )
45
- result = str
46
48
}
47
49
} else if type ( of: source) == _NSCFConstantString. self {
48
50
let conststr = unsafeDowncast ( source, to: _NSCFConstantString. self)
49
- let str = String ( decoding: UnsafeBufferPointer ( start: conststr. _ptr, count: Int ( conststr. _length) ) , as: UTF8 . self)
50
- result = str
51
+ result = String ( decoding: UnsafeBufferPointer ( start: conststr. _ptr, count: Int ( conststr. _length) ) , as: UTF8 . self)
51
52
} else {
52
53
let len = source. length
53
54
var characters = [ unichar] ( repeating: 0 , count: len)
0 commit comments