Skip to content

Instantly share code, notes, and snippets.

@oprypin
Last active August 29, 2015 14:13
Show Gist options
  • Save oprypin/f063fe6706201504580d to your computer and use it in GitHub Desktop.
Save oprypin/f063fe6706201504580d to your computer and use it in GitHub Desktop.
Replacement for negative indices
var s = "abcdef"
echo s[2 .. -3] # I suggest to remove this
# cd
proc `^`(x: int): int = assert false
template ex{`[]`(s, `..`(a, `^`(b)))}(s: expr{noSideEffect}, a, b: int): expr =
s[a .. s.len+b]
echo s[2 .. ^ -3] # Easy fix in code
# cd
# Or even this
proc `^-`(x: int): int = assert false
template ex{`[]`(s, `..`(a, `^-`(b)))}(s: expr{noSideEffect}, a, b: int): expr =
s[a .. s.len-b]
echo s[2 .. ^-3]
# cd
echo(@[1,2,3,4,5,6][2 .. ^-3])
# @[3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment