-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-6139] Implement NSString.copy() method #1291
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
[SR-6139] Implement NSString.copy() method #1291
Conversation
@swift-ci please test |
I apologize for a failed build, I rushed with solving merge conflicts in a test and missed one "}" in the end. I fixed it in the same commit. Everything should be fine now. |
@swift-ci please test |
1 similar comment
@swift-ci please test |
@sashabelonogov Could you also uncomment the following lines in
As this was the original failing test case so checking it now works would be good, thanks. |
@spevans sure! I also deleted my test because it's identical to the existing one. |
@swift-ci please test |
1 similar comment
@swift-ci please test |
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 changes - can you make them, then rebase against master and squash changes before resubmitting?
Foundation/NSString.swift
Outdated
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [ | ||
"NSDebugDescription" : "Unable to create a string using the specified encoding." | ||
]) | ||
throw NSError(domain: NSCocoaErrorDomain, |
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.
Can you revert this reformatting, since it isn't really relevant or part of this change?
TestFoundation/TestNSArray.swift
Outdated
@@ -46,7 +46,7 @@ class TestNSArray : XCTestCase { | |||
("test_mutableCopying", test_mutableCopying), | |||
("test_writeToFile", test_writeToFile), | |||
("test_initWithContentsOfFile", test_initWithContentsOfFile), | |||
("test_readWriteURL", test_readWriteURL) | |||
("test_readWriteURL", test_readWriteURL), |
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.
Although in principle having a trailing comma makes for less invasive changes in the future, since you aren't actually adding anything in this line then it's not relevant for this commit. Can you revert this line change and then squash the commits?
TestFoundation/TestNSString.swift
Outdated
@@ -1169,6 +1170,17 @@ class TestNSString : XCTestCase { | |||
let s6 = NSString(string: "Beyonce\u{301} and Tay") | |||
XCTAssertEqual(s6.substring(with: NSMakeRange(7, 9)), "\u{301} and Tay") | |||
} | |||
|
|||
func test_createCopy() { | |||
//SR-6139 |
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.
This comment isn't particularly relevant - as long as the SR-6139 is mentioned in the commit message, people will be able to identify that it came from this bug in the first place. Can you remove this comment?
@alblue, makes sense, sure. |
Foundation/NSString.swift
Outdated
} | ||
let result = NSString() | ||
result._storage = self._storage | ||
return result | ||
} |
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.
This doesn't make a lot of sense to me. Why shouldn't an immutable NSString
be allowed to return self for a copy?
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.
@parkera you're absolutely right, I didn't think about it. I updated the commit adding the case of the immutable NSString copy, what do you think about it?
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.
even though this method will not do what the name of it says, it makes sense to return self for performance reasons
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.
I think the best answer here is going to depend on if we can get a factory pattern proposal through swift-evolution... the way it works in the Darwin Foundation today is that when you use any of NSString's init methods, you're most likely getting a subclass of NSString, which implements copy by returning self.
For now, I think we should do this:
- if
type(of: self) === NSString.self
then return self - else, use one of NSString's other public initializers to create a new instance with the contents.
- Don't assume
result._storage
exists, because a subclass would not have access to that and may not use it to hold 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.
This would have some pretty serious performance concerns.
I updated the code handling the case of the immutable NString.copy() |
Seems to be OK for me now. @parkera is the proposed approach with the |
@parkera thanks for updates, that makes total sense, I didn't think about subclasses. I updated it to this:
What do you think? Or should I rather remove the _storage usage at all? I can also replace it with NSString(string: self._bridgeToSwift()) that will do pretty much the same. |
The part about The rest looks fine, I think. |
The NSMutableString part is deleted. I can't disagree with "subclasses need to override this to provide behavior that works for them". It's especially interesting to notice that most of the similar methods for other classes are implemented for both cases: mutable and immutable. Thanks for the feedback! |
@swift-ci please test |
@parkera I'm sorry for disturbing you but what is the status with this PR? I updated it with all the changes requested by you. |
Thanks for the ping. We should merge this. |
A potential fix for this issue:
https://bugs.swift.org/browse/SR-6139
Before, the method that is responsible to create the immutable copy of NSString was simply
returning self, that’s why copying didn’t happen and this test was failing:
Please, let me know what you think.