5 Commits

Author SHA1 Message Date
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
ge fe67a0c5a6 mod: bump version 2026-03-24 21:05:50 +03:00
ge 808870ff09 feat: add start-end[/step] syntax support, make Range public @[noinit] 2026-03-24 21:04:32 +03:00
ge 39314b474b use struct update syntax in with_step() 2026-01-11 08:41:46 +03:00
3 changed files with 39 additions and 20 deletions
+27 -17
View File
@@ -7,7 +7,7 @@ struct Range[T] {
start T
end T
step T
is_neg bool
is_neg bool // Is set to true if range end value is lesser than start value.
mut:
cur T
}
@@ -29,13 +29,16 @@ pub fn (mut r Range[T]) reset() {
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.
pub fn (r Range[T]) with_step[T](step T) Range[T] {
return Range[T]{
start: r.start
end: r.end
...r
step: step
is_neg: r.is_neg
cur: r.start
}
}
@@ -71,28 +74,28 @@ pub:
// Use from_string_custom if you want to use custom type with special string
// convertion rules.
//
// Supported string formats are `start-end` and `start[:step]end`. start and end
// values are sepatared by 'sep' which is hypen (`-`) by default. Single number
// will be interpreted as range of one element. Several ranges can be specified
// in a line, separated by 'group_sep' (comma by default). 'sep' and 'group_sep'
// can be overrided by user.
// Supported string formats are `start-end[/step]` and `start[:step]:end`. start
// and end values are sepatared by 'sep' which is hypen (`-`) by default. Single
// number will be interpreted as range of one element. Several ranges can be
// specified in a line, separated by 'group_sep' (comma by default). 'sep' and
// 'group_sep' can be overrided by user.
//
// Some example input strings:
//
// * `5` - range from 5 to 5 (single element).
// * `0-10` - range from 0 to 10.
// * `0-100/5` - range in Cron-style syntax from 0 to 100 with step 5.
// * `15:-1:0` - range in MathLab-style syntax from 15 to 0 with negative step -1.
// * `1..8` - range from 1 to 8 with '..' sep.
// * `0-7,64-71` - multiple ranges: from 0 to 7 and from 64 to 71.
//
// Only MathLab-style syntax allows you to specify a step directly in the string.
// For all other cases, the step is equal to one.
// If the step value is not specified, it will be set to one.
//
// Example: assert ranges.from_string[int]('1-7')! == [ranges.range(1, 7, 1)]
pub fn from_string[T](s string, config RangeFromStringConfig) ![]Range[T] {
mut result := []Range[T]{}
for i in s.split(config.group_sep) {
range_str := parse_string(i, config.sep)!
range_str := split_string(i, config.sep)!
// vfmt off
result << range[T](
convert_string[T](range_str[0])!,
@@ -135,7 +138,7 @@ pub type StringConvertFn[T] = fn (s string) !T
pub fn from_string_custom[T](s string, conv StringConvertFn[T], config RangeFromStringConfig) ![]Range[T] {
mut result := []Range[T]{}
for i in s.split(config.group_sep) {
range_str := parse_string(i, config.sep)!
range_str := split_string(i, config.sep)!
start := conv[T](range_str[0])!
end := conv[T](range_str[1])!
step := conv[T](range_str[2])!
@@ -144,7 +147,7 @@ pub fn from_string_custom[T](s string, conv StringConvertFn[T], config RangeFrom
return result
}
fn parse_string(s string, sep string) ![]string {
fn split_string(s string, sep string) ![]string {
parts := s.split(sep)
if parts.any(|x| x.is_blank()) || parts.len !in [1, 2, 3] {
return error('`start${sep}end` or `start[:step]:end`' +
@@ -152,12 +155,19 @@ fn parse_string(s string, sep string) ![]string {
}
if parts.len == 1 {
return [parts[0], parts[0], '1']
} else if parts.len == 2 {
}
if parts.len == 2 {
if parts[1].contains('/') {
end, step := parts[1].split_once('/') or { '', '' }
return [parts[0], end, step]
}
return [parts[0], parts[1], '1']
} else if sep == ':' && parts.len == 3 {
}
if sep == ':' && parts.len == 3 {
return [parts[0], parts[2], parts[1]]
}
return error('invalid range string: ${s}')
return error('invalid range string: expected `start[${sep}step]${sep}end` ' +
'or `start${sep}end[/step]` format, got `${s}`')
}
fn convert_string[T](s string) !T {
+9
View File
@@ -87,6 +87,7 @@ fn test_range_from_string() {
assert ranges.from_string[f32]('0.0..99.99', sep: '..')! == [
ranges.range[f32](0.0, 99.99, 1),
]
assert ranges.from_string[int]('0-100/5')! == [ranges.range(0, 100, 5)]
}
struct Int {
@@ -178,3 +179,11 @@ fn test_range_new_with_step() {
}
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 {
name: 'ranges'
description: 'Operating with ranges of numbers'
version: '0.3.0'
version: '0.5.0'
license: 'Unlicense'
dependencies: []
}