Skip to content

Add ByteRange structure to prevent disambiguation when NSRange instead ByteRange is used - #639

Merged
jpsim merged 6 commits into
jpsim:masterfrom
PaulTaykalo:feature/add-byte-range-to-prevent-disambiguation
Jan 12, 2020
Merged

jpsim merged 6 commits into
jpsim:masterfrom
PaulTaykalo:feature/add-byte-range-to-prevent-disambiguation

Conversation

@PaulTaykalo

@PaulTaykalo PaulTaykalo commented Jan 12, 2020 •

Copy link
Copy Markdown
Collaborator

What

Add ByteRange and ByteOffset wrapper to prevent disambiguation between NSRange (byte range ) and NSRange that represents ByteOffset range

Why

While working with ranges and tokens and string in Swiftlint I realized that it is very easy to use one range when you need totally another one. And since NSRange represents both (byte and string,) and those aren't typed, the idea was to use a type that will reprensents values when youre' working in bytes and not-character/strings world.

Pros

  • It is hard to misguide NSRange with ByteRange
  • Problems can only happen where conversion is made

Cons

Breaks backward compatibility

Additional

All the data that comes from SourceKit can be wrapped with ByteOffset/ByteRange. We can choose at what level we're about to stop.

Alternatives

User ByteRange<ByteOffset, ByteOffset> types. This probably will do even better.

@jpsim

jpsim commented Jan 12, 2020

Copy link
Copy Markdown
Owner

Thanks for your continued work on this! I hope you don't mind I pushed a few changes to your branch:

  • Added a changelog entry in the "Breaking" section
  • I renamed ByteOffset to ByteCount and used it to describe lengths too, since semantically the same distinction applies. This allowed getting rid of a lot of type conversions.
  • I converted a lot more declarations that were using Int/Int64/NSRange values to represent bytes, notably the Request members.

@jpsim
jpsim merged commit b46172d into jpsim:master Jan 12, 2020
Comment thread CHANGELOG.md

* None.
* Replace all uses of `Int`/`Int64`/`NSRange` representing byte offsets
to use newly introduced `ByteOffset` and `ByteRange` values instead.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ByteCount :)

public func range(toEnd end: SourceLocation) -> NSRange {
return NSRange(location: Int(offset), length: Int(end.offset - offset))
public func range(toEnd end: SourceLocation) -> ByteRange {
return ByteRange(location: ByteCount(Int(offset)), length: ByteCount(Int(end.offset - offset)))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ByteCount(Int(offset)) -> ByteCount(offset)

ByteCount can be created with Int64

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset in this case is an Int32 and I didn't think it was worth adding a new initializer for that.

public func substringWithSourceRange(start: SourceLocation, end: SourceLocation) -> String? {
return substringWithByteRange(start: Int(start.offset), length: Int(end.offset - start.offset))
let byteRange = ByteRange(location: ByteCount(Int(start.offset)),
length: ByteCount(Int(end.offset - start.offset)))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here as well, we can use ByteCount(Int(start.offset)) -> ByteCount(start.offset)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int32.

@PaulTaykalo

Copy link
Copy Markdown
Collaborator Author

Few minors with naming and unneeded casting.


- returns: An equivalent `NSRange`.
*/
public func byteRangeToNSRange(start: Int, length: Int) -> NSRange? {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it seems, that for backward better backward compatibility it is a good ideat o leave

byteRangeToNSRange(start: ByteCount, length: ByteCount)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, since the function name hasn't changed, the compiler error should be enough to let users know how to migrate their code.

jpsim added a commit to realm/SwiftLint that referenced this pull request Jan 16, 2020
New APIs were introduced in SourceKitten to allow for a more typesafe distinction between integers meaning NSString-based distances and byte-based distances.

* jpsim/SourceKitten#639
* jpsim/SourceKitten#642

This PR migrates SwiftLint's use of those APIs.
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