mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[String] [Foundation] perf: UTF8 String -> Data
Currently, `str.data(using:allowLossyConversion)` always bridges via `NSString`. As `String` is now natively UTF8 we can fastpath this conversion in the case where the user requests UTF8 encoding. A benchmark for this was previously added in #22648.
This commit is contained in:
@@ -800,9 +800,14 @@ extension StringProtocol where Index == String.Index {
|
||||
using encoding: String.Encoding,
|
||||
allowLossyConversion: Bool = false
|
||||
) -> Data? {
|
||||
return _ns.data(
|
||||
using: encoding.rawValue,
|
||||
allowLossyConversion: allowLossyConversion)
|
||||
switch encoding {
|
||||
case .utf8:
|
||||
return Data(self.utf8)
|
||||
default:
|
||||
return _ns.data(
|
||||
using: encoding.rawValue,
|
||||
allowLossyConversion: allowLossyConversion)
|
||||
}
|
||||
}
|
||||
|
||||
// @property NSString* decomposedStringWithCanonicalMapping;
|
||||
|
||||
Reference in New Issue
Block a user