Skip to content

[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

Merged
merged 2 commits into from
Mar 16, 2018

Conversation

milseman
Copy link
Member

String._fromCodeUnits is about to disappear; use already existing
String.init calls instead.

String._fromCodeUnits is about to disappear; use already existing
String.init calls instead.
@milseman
Copy link
Member Author

@swift-ci please test linux platform

@milseman
Copy link
Member Author

@phausler who is the right person to review this change?

@milseman milseman requested a review from itaiferber March 15, 2018 20:22
@milseman
Copy link
Member Author

@itaiferber can you review?

Copy link
Contributor

@itaiferber itaiferber left a 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)
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sounds reasonable, then!

@@ -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)
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

@milseman
Copy link
Member Author

@swift-ci please test

@milseman
Copy link
Member Author

@swift-ci please test

@milseman milseman merged commit b3e1bd0 into swiftlang:master Mar 16, 2018
@milseman milseman deleted the string_weaning branch March 16, 2018 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants