3 Commits

Author SHA1 Message Date
ge 34ed972930 fix tests 2026-05-12 19:55:19 +03:00
ge a055360436 mod: bump version 2026-03-24 21:44:20 +03:00
ge 93a610fa02 feat: add bounds(), make Range private back 2026-03-24 21:43:46 +03:00
3 changed files with 32 additions and 13 deletions
+6 -3
View File
@@ -3,9 +3,7 @@ module ranges
import strconv import strconv
import math.big import math.big
@[noinit] struct Range[T] {
pub struct Range[T] {
pub:
start T start T
end T end T
step T step T
@@ -31,6 +29,11 @@ pub fn (mut r Range[T]) reset() {
r.cur = r.start r.cur = r.start
} }
// bounds returns the start, end and step values of range.
pub fn (r Range[T]) bounds() (T, T, T) {
return r.start, r.end, r.step
}
// with_step returns copy of the range with new step value. // with_step returns copy of the range with new step value.
pub fn (r Range[T]) with_step[T](step T) Range[T] { pub fn (r Range[T]) with_step[T](step T) Range[T] {
return Range[T]{ return Range[T]{
+25 -9
View File
@@ -1,3 +1,5 @@
// See bug: https://github.com/vlang/v/issues/27147
// vtest vflags: -no-skip-unused
import ranges import ranges
import math.big import math.big
@@ -111,23 +113,29 @@ fn (a Int) == (b Int) bool {
} }
fn test_range_custom_type() { fn test_range_custom_type() {
// vfmt off
mut result := []Int{} mut result := []Int{}
for i in ranges.range[Int](Int{ val: 0 }, Int{ val: 5 }, Int{ val: 1 }) { start := Int{0}
end := Int{5}
step := Int{1}
for i in ranges.range[Int](start, end, step) {
result << i result << i
} }
assert result == [Int{0}, Int{1}, Int{2}, Int{3}, Int{4}, Int{5}] assert result == [
// vfmt on Int{0},
Int{1},
Int{2},
Int{3},
Int{4},
Int{5},
]
} }
//
// Note this bug: https://github.com/vlang/v/issues/26156
//
fn test_range_from_string_custom_type() { fn test_range_from_string_custom_type() {
assert ranges.from_string_custom[Int]('0-5', fn (s string) !Int { assert ranges.from_string_custom[Int]('0-5', fn (s string) !Int {
if s.is_int() { if s.is_int() {
return Int{ val: s.int() } return Int{
val: s.int()
}
} else { } else {
return error('invalid integer value: ${s}') return error('invalid integer value: ${s}')
} }
@@ -179,3 +187,11 @@ fn test_range_new_with_step() {
} }
assert result == [0, 2, 4] assert result == [0, 2, 4]
} }
fn test_range_bounds() {
r := ranges.range(0, 10, 2)
a, b, c := r.bounds()
assert a == 0
assert b == 10
assert c == 2
}
+1 -1
View File
@@ -1,7 +1,7 @@
Module { Module {
name: 'ranges' name: 'ranges'
description: 'Operating with ranges of numbers' description: 'Operating with ranges of numbers'
version: '0.4.0' version: '0.5.0'
license: 'Unlicense' license: 'Unlicense'
dependencies: [] dependencies: []
} }