Skip to content

Commit 9218bda

Browse files
parkerajmschonfeld
andauthored
Only use the underscored utf16 function when we are a contiguous UTF8 string. (#685)
Co-authored-by: Jeremy Schonfeld <jschonfeld@apple.com>
1 parent 6e3d8b0 commit 9218bda

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Sources/FoundationEssentials/String/StringProtocol+Essentials.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,18 @@ extension String {
9999
// Put the BOM in, then copy the UTF16 bytes to the buffer after it.
100100
utf16Buffer[0] = bom
101101
let afterBOMBuffer = UnsafeMutableBufferPointer(rebasing: utf16Buffer[1..<utf16Buffer.endIndex])
102-
self._copyUTF16CodeUnits(into: afterBOMBuffer, range: 0..<inputCount)
102+
if self.isContiguousUTF8 {
103+
self._copyUTF16CodeUnits(into: afterBOMBuffer, range: 0..<inputCount)
104+
} else {
105+
_ = afterBOMBuffer.initialize(fromContentsOf: self.utf16)
106+
}
103107
} else {
104-
self._copyUTF16CodeUnits(into: utf16Buffer, range: 0..<inputCount)
105-
}
106-
108+
if self.isContiguousUTF8 {
109+
self._copyUTF16CodeUnits(into: utf16Buffer, range: 0..<inputCount)
110+
} else {
111+
_ = utf16Buffer.initialize(fromContentsOf: self.utf16)
112+
}
113+
}
107114

108115
// If we need to swap endianness, we do it as a second pass over the data
109116
if swap {

0 commit comments

Comments
 (0)