1313/// The length a syntax node spans in the source code. From any AbsolutePosition
1414/// you reach a node's end location by adding its UTF-8 length.
1515public struct SourceLength : Comparable {
16+ /// The length in bytes when the text is represented as UTF-8.
1617 public let utf8Length : Int
1718
1819 /// Construct the source length of a given text
1920 public init ( of text: String ) {
2021 self . utf8Length = text. utf8. count
2122 }
2223
24+ /// Construct a source length that takes up `utf8Length` bytes when represented as UTF-8.
2325 public init ( utf8Length: Int ) {
2426 self . utf8Length = utf8Length
2527 }
@@ -28,6 +30,7 @@ public struct SourceLength: Comparable {
2830 public static let zero : SourceLength =
2931 SourceLength ( utf8Length: 0 )
3032
33+ /// Returns `true` if `lhs` is shorter than `rhs`.
3134 public static func < ( lhs: SourceLength , rhs: SourceLength ) -> Bool {
3235 return lhs. utf8Length < rhs. utf8Length
3336 }
@@ -38,15 +41,18 @@ public struct SourceLength: Comparable {
3841 return SourceLength ( utf8Length: utf8Length)
3942 }
4043
44+ /// Extend `lhs` by `rhs` bytes.
4145 public static func += ( lhs: inout SourceLength , rhs: SourceLength ) {
4246 lhs = lhs + rhs
4347 }
4448
49+ /// Return a source length that's `rhs` bytes shorter than `lhs`.
4550 public static func - ( lhs: SourceLength , rhs: SourceLength ) -> SourceLength {
4651 let utf8Length = lhs. utf8Length - rhs. utf8Length
4752 return SourceLength ( utf8Length: utf8Length)
4853 }
4954
55+ /// Change `lhs` to be `rhs` bytes shorter than `lhs`.
5056 public static func -= ( lhs: inout SourceLength , rhs: SourceLength ) {
5157 lhs = lhs - rhs
5258 }
@@ -62,10 +68,12 @@ extension AbsolutePosition {
6268 return AbsolutePosition ( utf8Offset: utf8Offset)
6369 }
6470
71+ /// Advance `lhs` by `rhs`, i.e. changing it to the position `rhs` bytes after `lhs`.
6572 public static func += ( lhs: inout AbsolutePosition , rhs: SourceLength ) {
6673 lhs = lhs + rhs
6774 }
6875
76+ /// Return the position `rhs` bytes before `lhs`.
6977 public static func - (
7078 lhs: AbsolutePosition ,
7179 rhs: SourceLength
@@ -74,6 +82,7 @@ extension AbsolutePosition {
7482 return AbsolutePosition ( utf8Offset: utf8Offset)
7583 }
7684
85+ /// Reverse `lhs` by `rhs`, i.e. changing it `lhs` to the position `rhs` bytes before `lhs`.
7786 public static func -= ( lhs: inout AbsolutePosition , rhs: SourceLength ) {
7887 lhs = lhs - rhs
7988 }
0 commit comments