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

support DOS newlines in NSString.lineAndCharacterForByteOffset(_:) #181

Closed
wants to merge 1 commit into from

Conversation

jpsim
Copy link
Owner

@jpsim jpsim commented Mar 10, 2016

@norio-nomura
Copy link
Collaborator

As I tested, CRLF line ending does not cause the issue.
0x1A on file ending? https://en.wikipedia.org/wiki/Control-Z

@norio-nomura
Copy link
Collaborator

I can't reproduce the issue.

@norio-nomura
Copy link
Collaborator

I got reproduced the issue.

@norio-nomura
Copy link
Collaborator

I think following would be better:

diff --git a/Source/SourceKittenFramework/String+SourceKitten.swift b/Source/SourceKittenFramework/String+SourceKitten.swift
index 7f9b007..e766b89 100644
--- a/Source/SourceKittenFramework/String+SourceKitten.swift
+++ b/Source/SourceKittenFramework/String+SourceKitten.swift
@@ -176,7 +176,7 @@ extension NSString {
                 if length == line.byteRange.length {
                     character = content.utf16.count
                 } else {
-                    let endIndex = content.utf8.startIndex.advancedBy(length)
+                    let endIndex = content.utf8.startIndex.advancedBy(length, limit: content.utf8.endIndex)
                         .samePositionIn(content.utf16) ?? content.utf16.endIndex
                     character = content.utf16.startIndex.distanceTo(endIndex)
                 }

@norio-nomura
Copy link
Collaborator

Maybe we should use advancedBy(_:limit:) on all collections that exposed by String.

var str = "Hello, playground"

let array = Array(str.characters)

func advancedByCountPlusOne<C: CollectionType>(collection: C) -> C.Index.Distance {
    return collection.startIndex.distanceTo(collection.startIndex.advancedBy(collection.count + 1))
}

advancedByCountPlusOne(str.unicodeScalars)  // 17
advancedByCountPlusOne(str.utf16 )          // 18
advancedByCountPlusOne(array)               // 18
advancedByCountPlusOne(ArraySlice(array))   // 18
advancedByCountPlusOne(str.characters)      // fatal error: can not increment endIndex
advancedByCountPlusOne(str.utf8)            // fatal error: Can't increment past endIndex of String.UTF8View

@jpsim
Copy link
Owner Author

jpsim commented Mar 14, 2016

Replaced by @norio-nomura's work in #182

@jpsim jpsim closed this Mar 14, 2016
@jpsim jpsim deleted the jp-dos-newlines branch March 14, 2016 17:38
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