Skip to content

Commit

Permalink
soruce: add range.contains
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuro9715 committed Oct 2, 2023
1 parent 3085b52 commit 9089c0c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions kaico/source/range.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ pub fn (r Range) extend(p Pos) Range {
r
}
}

pub fn (r Range) contains(item Range) bool {
begin, end := item.begin, item.end
return begin.in_range(r) && (end == r.end || end.in_range(r))
}
27 changes: 26 additions & 1 deletion kaico/source/range_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import kaico.source { Range, Source }

const (
s = Source.from_text('')
s = Source.from_text('abcdef')
p0 = s.new_pos(offset: 0, line: 1, column: 1)
p1 = s.new_pos(offset: 1, line: 1, column: 2)
p2 = s.new_pos(offset: 2, line: 1, column: 3)
p3 = s.new_pos(offset: 3, line: 1, column: 4)
p4 = s.new_pos(offset: 4, line: 1, column: 5)
p5 = s.new_pos(offset: 5, line: 1, column: 6)
)

fn test_new() {
Expand Down Expand Up @@ -42,4 +44,27 @@ fn test_extend() {
assert Range.new(p1, p2).extend(p2) == Range.new(p1, p2)
}

fn test_contains() {
r13 := Range.new(p1, p3)
assert r13.contains(r13)

assert !r13.contains(Range.new(p0, p1))
assert !r13.contains(Range.new(p0, p2))
assert !r13.contains(Range.new(p0, p3))
assert !r13.contains(Range.new(p0, p4))
assert !r13.contains(Range.new(p0, p5))

assert r13.contains(Range.new(p1, p2))
assert r13.contains(Range.new(p1, p3))
assert !r13.contains(Range.new(p1, p4))
assert !r13.contains(Range.new(p1, p5))

assert r13.contains(Range.new(p2, p3))
assert !r13.contains(Range.new(p2, p4))
assert !r13.contains(Range.new(p2, p5))

assert !r13.contains(Range.new(p3, p4))
assert !r13.contains(Range.new(p3, p5))

assert !r13.contains(Range.new(p4, p5))
}

0 comments on commit 9089c0c

Please sign in to comment.