-
Notifications
You must be signed in to change notification settings - Fork 226
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
Optimize NSRange
conversion
#120
Changes from 4 commits
a4a2d6a
034d598
76444d2
63161b3
229c991
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,11 +80,17 @@ extension NSString { | |
*/ | ||
public func byteRangeToNSRange(start start: Int, length: Int) -> NSRange? { | ||
let string = self as String | ||
return string.indexOfByteOffset(start).flatMap { stringStart in | ||
return string.indexOfByteOffset(start + length).map { stringEnd in | ||
return NSRange(location: stringStart, length: stringEnd - stringStart) | ||
} | ||
let startUTF8Index = string.utf8.startIndex.advancedBy(start) | ||
let endUTF8Index = startUTF8Index.advancedBy(length) | ||
|
||
guard let startUTF16Index = startUTF8Index.samePositionIn(string.utf16), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first since I filed #119, I tested caching full results of offsets. But that was not efficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's not what I mean. I mean are e.g. is it more efficient to do this: let utf16View = string.utf16
guard let startUTF16Index = startUTF8Index.samePositionIn(utf16View),
let endUTF16Index = endUTF8Index.samePositionIn(utf16View) else {
return nil
} or is that equivalent to this (current code): guard let startUTF16Index = startUTF8Index.samePositionIn(string.utf16),
let endUTF16Index = endUTF8Index.samePositionIn(string.utf16) else {
return nil
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested with changes of that and got same performance. |
||
let endUTF16Index = endUTF8Index.samePositionIn(string.utf16) else { | ||
return nil | ||
} | ||
|
||
let location = string.utf16.startIndex.distanceTo(startUTF16Index) | ||
let length = startUTF16Index.distanceTo(endUTF16Index) | ||
return NSRange(location: location, length: length) | ||
} | ||
|
||
/** | ||
|
@@ -98,11 +104,17 @@ extension NSString { | |
*/ | ||
public func NSRangeToByteRange(start start: Int, length: Int) -> NSRange? { | ||
let string = self as String | ||
return string.byteOffsetAtIndex(start).flatMap { stringStart in | ||
return string.byteOffsetAtIndex(start + length).map { stringEnd in | ||
return NSRange(location: stringStart, length: stringEnd - stringStart) | ||
} | ||
let startUTF16Index = string.utf16.startIndex.advancedBy(start) | ||
let endUTF16Index = startUTF16Index.advancedBy(length) | ||
|
||
guard let startUTF8Index = startUTF16Index.samePositionIn(string.utf8), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing about caching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe caching exact results will not be efficient for most use case (I don't know other than SwiftLint) because inputs are matching results and those are almost unique. But caching latest index might be efficient for SwiftLint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mean caching the results of this method, but rather storing |
||
let endUTF8Index = endUTF16Index.samePositionIn(string.utf8) else { | ||
return nil | ||
} | ||
|
||
let location = string.utf8.startIndex.distanceTo(startUTF8Index) | ||
let length = startUTF8Index.distanceTo(endUTF8Index) | ||
return NSRange(location: location, length: length) | ||
} | ||
|
||
/** | ||
|
@@ -188,28 +200,6 @@ extension NSString { | |
} | ||
|
||
extension String { | ||
/** | ||
UTF16 index equivalent to byte offset. | ||
|
||
- parameter offset: Byte offset. | ||
|
||
- returns: UTF16 index, if any. | ||
*/ | ||
private func indexOfByteOffset(offset: Int) -> Int? { | ||
return utf8.startIndex.advancedBy(offset).samePositionIn(utf16).map(utf16.startIndex.distanceTo) | ||
} | ||
|
||
/** | ||
Byte offset equivalent to UTF16 index. | ||
|
||
- parameter index: UTF16 index. | ||
|
||
- returns: Byte offset, if any. | ||
*/ | ||
private func byteOffsetAtIndex(index: Int) -> Int? { | ||
return utf16.startIndex.advancedBy(index).samePositionIn(utf8).map(utf8.startIndex.distanceTo) | ||
} | ||
|
||
/// Returns the `#pragma mark`s in the string. | ||
/// Just the content; no leading dashes or leading `#pragma mark`. | ||
public func pragmaMarks(filename: String, excludeRanges: [NSRange], limitRange: NSRange?) -> [SourceDeclaration] { | ||
|
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.
Please refer to the issues this addresses (#119) rather than this PR number (as explained in https://github.com/jpsim/SourceKitten/blob/master/CONTRIBUTING.md).