Skip to content
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

Fix the trimming of trailing characters. #351

Merged
merged 3 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Source/SourceKittenFramework/String+SourceKitten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,15 @@ extension NSString {
- parameter characterSet: Character set to check for membership.
*/
public func trimmingTrailingCharacters(in characterSet: CharacterSet) -> String {
if length == 0 {
guard length > 0 else {
return ""
}
var charBuffer = [unichar](repeating: 0, count: length)
getCharacters(&charBuffer, range: NSRange(location: 0, length: charBuffer.count))
for newLength in (1...length).reversed() {
if !characterSet.contains(UnicodeScalar(charBuffer[newLength - 1])!) {
return substring(with: NSRange(location: 0, length: newLength))
var string = self.bridge()
let endIndex = string.unicodeScalars.endIndex
for offset in 1...string.unicodeScalars.count {
let index = string.unicodeScalars.index(endIndex, offsetBy: -offset)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This creates the index from the end of the string every time.
Can you reuse the index created in the previous loop to reduce computational cost?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right. Thanks for the input. I think there's even a much easier implementation (at least for me it's much easier to wrap my head around 😉 ):

        var unicodeScalars = self.bridge().unicodeScalars
        while let scalar = unicodeScalars.last {
            if !characterSet.contains(scalar) {
                return String(unicodeScalars)
            }
            unicodeScalars.removeLast()
        }

Do you have concerns?

Copy link
Owner

Choose a reason for hiding this comment

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

Looks fine to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

if !characterSet.contains(string.unicodeScalars[index]), let stringIndex = index.samePosition(in: string) {
return string.substring(to: string.index(stringIndex, offsetBy: 1))
}
}
return ""
Expand Down
1 change: 1 addition & 0 deletions Tests/SourceKittenFrameworkTests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class StringTests: XCTestCase {
XCTAssertEqual(" a ".bridge().trimmingTrailingCharacters(in: .whitespacesAndNewlines), " a")
XCTAssertEqual(" ".bridge().trimmingTrailingCharacters(in: .whitespacesAndNewlines), "")
XCTAssertEqual("a".bridge().trimmingTrailingCharacters(in: .whitespacesAndNewlines), "a")
XCTAssertEqual("Foobar💣".bridge().trimmingTrailingCharacters(in: .whitespacesAndNewlines), "Foobar💣")
}

func testCommentBody() {
Expand Down