13
13
/// The length a syntax node spans in the source code. From any AbsolutePosition
14
14
/// you reach a node's end location by adding its UTF-8 length.
15
15
public struct SourceLength : Comparable {
16
+ /// The length in bytes when the text is represented as UTF-8.
16
17
public let utf8Length : Int
17
18
18
19
/// Construct the source length of a given text
19
20
public init ( of text: String ) {
20
21
self . utf8Length = text. utf8. count
21
22
}
22
23
24
+ /// Construct a source length that takes up `utf8Length` bytes when represented as UTF-8.
23
25
public init ( utf8Length: Int ) {
24
26
self . utf8Length = utf8Length
25
27
}
@@ -28,6 +30,7 @@ public struct SourceLength: Comparable {
28
30
public static let zero : SourceLength =
29
31
SourceLength ( utf8Length: 0 )
30
32
33
+ /// Returns `true` if `lhs` is shorter than `rhs`.
31
34
public static func < ( lhs: SourceLength , rhs: SourceLength ) -> Bool {
32
35
return lhs. utf8Length < rhs. utf8Length
33
36
}
@@ -38,15 +41,18 @@ public struct SourceLength: Comparable {
38
41
return SourceLength ( utf8Length: utf8Length)
39
42
}
40
43
44
+ /// Extend `lhs` by `rhs` bytes.
41
45
public static func += ( lhs: inout SourceLength , rhs: SourceLength ) {
42
46
lhs = lhs + rhs
43
47
}
44
48
49
+ /// Return a source length that's `rhs` bytes shorter than `lhs`.
45
50
public static func - ( lhs: SourceLength , rhs: SourceLength ) -> SourceLength {
46
51
let utf8Length = lhs. utf8Length - rhs. utf8Length
47
52
return SourceLength ( utf8Length: utf8Length)
48
53
}
49
54
55
+ /// Change `lhs` to be `rhs` bytes shorter than `lhs`.
50
56
public static func -= ( lhs: inout SourceLength , rhs: SourceLength ) {
51
57
lhs = lhs - rhs
52
58
}
@@ -62,10 +68,12 @@ extension AbsolutePosition {
62
68
return AbsolutePosition ( utf8Offset: utf8Offset)
63
69
}
64
70
71
+ /// Advance `lhs` by `rhs`, i.e. changing it to the position `rhs` bytes after `lhs`.
65
72
public static func += ( lhs: inout AbsolutePosition , rhs: SourceLength ) {
66
73
lhs = lhs + rhs
67
74
}
68
75
76
+ /// Return the position `rhs` bytes before `lhs`.
69
77
public static func - (
70
78
lhs: AbsolutePosition ,
71
79
rhs: SourceLength
@@ -74,6 +82,7 @@ extension AbsolutePosition {
74
82
return AbsolutePosition ( utf8Offset: utf8Offset)
75
83
}
76
84
85
+ /// Reverse `lhs` by `rhs`, i.e. changing it `lhs` to the position `rhs` bytes before `lhs`.
77
86
public static func -= ( lhs: inout AbsolutePosition , rhs: SourceLength ) {
78
87
lhs = lhs - rhs
79
88
}
0 commit comments