From 396921817d2b17e8e69f4113498f16b889a232c1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 1 Sep 2019 08:37:22 +1000 Subject: [PATCH] Have ractool drange syntax accept i..=j --- cmd/ractool/main.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cmd/ractool/main.go b/cmd/ractool/main.go index 41d94bec..3ac3df2e 100644 --- a/cmd/ractool/main.go +++ b/cmd/ractool/main.go @@ -290,6 +290,9 @@ func parseNumber(s string) int64 { // parseRange parses a string like "1..23", returning i=1 and j=23. Either or // both numbers can be missing, in which case i and/or j will be negative, and // it is up to the caller to interpret that placeholder value meaningfully. +// +// Like Rust range syntax, it also accepts "i..=j", not just "i..j", in which +// case the upper bound is inclusive, not exclusive. func parseRange(s string) (i int64, j int64, ok bool) { n := strings.Index(s, "..") if n < 0 { @@ -302,10 +305,24 @@ func parseRange(s string) (i int64, j int64, ok bool) { return 0, 0, false } - if n+2 >= len(s) { + // Look for "i..j" versus "i..=j". + eq := 0 + if (n+2 < len(s)) && (s[n+2] == '=') { + eq = 1 + } + + if n+2+eq >= len(s) { + if eq > 0 { + return 0, 0, false + } j = -1 - } else if j = parseNumber(s[n+2:]); j < 0 { + } else if j = parseNumber(s[n+2+eq:]); j < 0 { return 0, 0, false + } else { + j += int64(eq) + if j < 0 { + return 0, 0, false + } } if (i >= 0) && (j >= 0) && (i > j) {