[swift-dev] [SourceKit] NSRange, Swift.String, and NSString

Ben Langmuir blangmuir at apple.com
Fri Mar 24 13:08:51 CDT 2017


> 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

> 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.

> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev



More information about the swift-dev mailing list