-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[string] Wean off of non-String-API. #1476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
String._fromCodeUnits is about to disappear; use already existing String.init calls instead.
@swift-ci please test linux platform |
@phausler who is the right person to review this change? |
@itaiferber can you review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nits but looks reasonable to me!
var uintValue = value.unicodeScalar.value | ||
super.init(String._fromWellFormedCodeUnitSequence(UTF32.self, input: UnsafeBufferPointer(start: &uintValue, count: 1))) | ||
} | ||
super.init(value.description) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does value.description
just return a String
representing StaticString
? Is there a performance hit to not initializing directly from the contents?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value.description
is direct initialization of a String with the same contents. It will be faster in the future than trying to do it yourself, as StaticString can poke at String's innards.
Currently, it will encode a single scalar as UTF-8 prior to initialization, and then allocate storage on the heap to store it. We could try to skip that transcoding temporarily, however, I'm about to land small strings and plan on supporting UTF-8 in them. In this case, we still want to encode as UTF-8 there, so we'd do a encoding anyways and skip the heap allocation. This will be better than what this code was doing before: before, even a single scalar gets a heap allocation.
It is equivalent for non-scalar StaticString.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, sounds reasonable, then!
Foundation/NSString.swift
Outdated
@@ -273,7 +273,7 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC | |||
} | |||
|
|||
public init(characters: UnsafePointer<unichar>, length: Int) { | |||
_storage = String._fromWellFormedCodeUnitSequence(UTF16.self, input: UnsafeBufferPointer(start: characters, count: length)) | |||
_storage = String.init(decoding: UnsafeBufferPointer(start: characters, count: length), as: UTF16.self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can drop .init
for brevity, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup
@swift-ci please test |
@swift-ci please test |
String._fromCodeUnits is about to disappear; use already existing
String.init calls instead.