From 9089c0c1c5092a7d1c1ff1419a5e21329fa85917 Mon Sep 17 00:00:00 2001 From: zakuro Date: Mon, 2 Oct 2023 09:08:21 +0900 Subject: [PATCH] soruce: add `range.contains` --- kaico/source/range.v | 5 +++++ kaico/source/range_test.v | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/kaico/source/range.v b/kaico/source/range.v index 35e227b..59ac0f5 100644 --- a/kaico/source/range.v +++ b/kaico/source/range.v @@ -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)) +} diff --git a/kaico/source/range_test.v b/kaico/source/range_test.v index 3ce00d3..05864fb 100644 --- a/kaico/source/range_test.v +++ b/kaico/source/range_test.v @@ -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() { @@ -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)) +}