summary refs log tree commit diff stats
path: root/lib/system.nim
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2017-05-29 08:41:00 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-05-29 09:41:00 +0200
commitaef5b2eb54868e549082c9fd1f9a89e8edc83ee0 (patch)
tree9883a13ad01acb982d227a28a7a8f971f6623182 /lib/system.nim
parentfd0ab1df3efa51870d6746ed0ce6479f8934369d (diff)
downloadNim-aef5b2eb54868e549082c9fd1f9a89e8edc83ee0.tar.gz
Add len for Slice[T] where T is ordinal (#5847)
Diffstat (limited to 'lib/system.nim')
-rw-r--r--lib/system.nim8
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/system.nim b/lib/system.nim
index fdc4c7db0..67f088730 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2047,6 +2047,14 @@ proc clamp*[T](x, a, b: T): T =
   if x > b: return b
   return x
 
+proc len*[T: Ordinal](x: Slice[T]): int {.noSideEffect, inline.} =
+  ## length of ordinal slice, when x.b < x.a returns zero length
+  ##
+  ## .. code-block:: Nim
+  ##   assert((0..5).len == 6)
+  ##   assert((5..2).len == 0)
+  result = max(0, ord(x.b) - ord(x.a) + 1)
+
 iterator items*[T](a: openArray[T]): T {.inline.} =
   ## iterates over each item of `a`.
   var i = 0