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

Conversation

Lukas-Stuehrk
Copy link
Contributor

When the string ended with extended grapheme clusters, the method
crashed because of the force-unwrapping. This refactoring adds support
for handling extended grapheme clusters.

This fixes #350

When the string ended with extended grapheme clusters, the method
crashed because of the force-unwrapping. This refactoring adds support
for handling extended grapheme clusters.
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.

👍

@norio-nomura
Copy link
Collaborator

Could you please add a changelog entry? Thanks!

@Lukas-Stuehrk
Copy link
Contributor Author

Sure! Done.

@jpsim
Copy link
Owner

jpsim commented Mar 30, 2017

Thanks @Lukas-Stuehrk!

@jpsim jpsim merged commit 3d468d0 into jpsim:master Mar 30, 2017
@Lukas-Stuehrk
Copy link
Contributor Author

You're welcome. I'm happy to help on such a handy and important piece in the Swift tooling. Thanks for the fast reviews, good feedback and all your work.

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.

SourceKitten crashes when a documentation comment ends with an extended grapheme cluster.
3 participants