[swift-dev] [SourceKit] NSRange, Swift.String, and NSString
Tyler Stromberg
tylers at mac.com
Thu Apr 6 09:15:32 CDT 2017
> On Mar 24, 2017, at 12:08 PM, Ben Langmuir <blangmuir at apple.com> wrote:
>
>
>> On Mar 24, 2017, at 10:59 AM, Tyler Stromberg via swift-dev <swift-dev at swift.org> wrote:
>>
>> I'm currently working on integrating SourceKit with a macOS application. AppKit APIs (e.g. NSAttributedString, NSLayoutManager, etc) deal in terms of NSRange (UTF-16 code units?). SourceKit, however, deals in terms of integer offsets and lengths (UTF-8 code units?).
>
> UTF8 byte offsets
Thanks for the clarification. =)
>> Is there a more efficient or easier way to convert back and forth between the two other than doing the index(_:offsetBy:) -> samePosition(in:) dance?
>
> If you’re doing a bunch of queries in the same file, you could build a table of line start offsets in both UTF8 and UTF16 you may get faster results by going UTF8 offset -> UTF8 line + delta -> UTF16 line + delta -> UTF16 offset. Since then the expensive part is O(line length) instead of O(file size).
>
> I don’t know of a good canned solution.
Yeah, this is what I figured I'd have to do.
Thanks for the help!
More information about the swift-dev
mailing list