From b14cc1e3b2d8462018e1d2a8e92dfcb42398713e Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 30 Oct 2017 14:45:57 +0100 Subject: [PATCH] fixes #6631 --- changelog.md | 5 ++-- lib/impure/nre.nim | 26 +++++++++---------- lib/pure/algorithm.nim | 4 +-- lib/pure/collections/sharedstrings.nim | 2 +- lib/pure/matchers.nim | 2 +- lib/pure/random.nim | 2 +- lib/system.nim | 36 ++++++++++++++------------ tests/array/troof1.nim | 26 ++++++++++++++++++- 8 files changed, 65 insertions(+), 38 deletions(-) diff --git a/changelog.md b/changelog.md index c113505cdf..f8ad888425 100644 --- a/changelog.md +++ b/changelog.md @@ -24,8 +24,9 @@ use the ``pred`` proc. - We changed how array accesses "from backwards" like ``a[^1]`` or ``a[0..^1]`` are implemented. These are now implemented purely in ``system.nim`` without compiler - support. ``system.Slice`` now takes 2 generic parameters so that it can - take ``BackwardsIndex`` indices. ``BackwardsIndex`` is produced by ``system.^``. + support. There is a new "heterogenous" slice type ``system.HSlice`` that takes 2 + generic parameters which can be ``BackwardsIndex`` indices. ``BackwardsIndex`` is + produced by ``system.^``. This means if you overload ``[]`` or ``[]=`` you need to ensure they also work with ``system.BackwardsIndex`` (if applicable for the accessors). - ``mod`` and bitwise ``and`` do not produce ``range`` subtypes anymore. This diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim index 7df62f3b3a..3d4afc0ae1 100644 --- a/lib/impure/nre.nim +++ b/lib/impure/nre.nim @@ -155,7 +155,7 @@ type ## - ``"abc".match(re"(?\w)").captures["letter"] == "a"`` ## - ``"abc".match(re"(\w)\w").captures[-1] == "ab"`` ## - ## ``captureBounds[]: Option[Slice[int, int]]`` + ## ``captureBounds[]: Option[HSlice[int, int]]`` ## gets the bounds of the given capture according to the same rules as ## the above. If the capture is not filled, then ``None`` is returned. ## The bounds are both inclusive. @@ -167,7 +167,7 @@ type ## ``match: string`` ## the full text of the match. ## - ## ``matchBounds: Slice[int, int]`` + ## ``matchBounds: HSlice[int, int]`` ## the bounds of the match, as in ``captureBounds[]`` ## ## ``(captureBounds|captures).toTable`` @@ -182,7 +182,7 @@ type ## Not nil. str*: string ## The string that was matched against. ## Not nil. - pcreMatchBounds: seq[Slice[cint, cint]] ## First item is the bounds of the match + pcreMatchBounds: seq[HSlice[cint, cint]] ## First item is the bounds of the match ## Other items are the captures ## `a` is inclusive start, `b` is exclusive end @@ -251,13 +251,13 @@ proc captureBounds*(pattern: RegexMatch): CaptureBounds = return CaptureBounds(p proc captures*(pattern: RegexMatch): Captures = return Captures(pattern) -proc `[]`*(pattern: CaptureBounds, i: int): Option[Slice[int, int]] = +proc `[]`*(pattern: CaptureBounds, i: int): Option[HSlice[int, int]] = let pattern = RegexMatch(pattern) if pattern.pcreMatchBounds[i + 1].a != -1: let bounds = pattern.pcreMatchBounds[i + 1] return some(int(bounds.a) .. int(bounds.b-1)) else: - return none(Slice[int, int]) + return none(HSlice[int, int]) proc `[]`*(pattern: Captures, i: int): string = let pattern = RegexMatch(pattern) @@ -272,10 +272,10 @@ proc `[]`*(pattern: Captures, i: int): string = proc match*(pattern: RegexMatch): string = return pattern.captures[-1] -proc matchBounds*(pattern: RegexMatch): Slice[int, int] = +proc matchBounds*(pattern: RegexMatch): HSlice[int, int] = return pattern.captureBounds[-1].get -proc `[]`*(pattern: CaptureBounds, name: string): Option[Slice[int, int]] = +proc `[]`*(pattern: CaptureBounds, name: string): Option[HSlice[int, int]] = let pattern = RegexMatch(pattern) return pattern.captureBounds[pattern.pattern.captureNameToId.fget(name)] @@ -295,9 +295,9 @@ proc toTable*(pattern: Captures, default: string = nil): Table[string, string] = result = initTable[string, string]() toTableImpl(nextVal == nil) -proc toTable*(pattern: CaptureBounds, default = none(Slice[int, int])): - Table[string, Option[Slice[int, int]]] = - result = initTable[string, Option[Slice[int, int]]]() +proc toTable*(pattern: CaptureBounds, default = none(HSlice[int, int])): + Table[string, Option[HSlice[int, int]]] = + result = initTable[string, Option[HSlice[int, int]]]() toTableImpl(nextVal.isNone) template itemsImpl(cond: untyped) {.dirty.} = @@ -309,13 +309,13 @@ template itemsImpl(cond: untyped) {.dirty.} = yield nextYieldVal -iterator items*(pattern: CaptureBounds, default = none(Slice[int, int])): Option[Slice[int, int]] = +iterator items*(pattern: CaptureBounds, default = none(HSlice[int, int])): Option[HSlice[int, int]] = itemsImpl(nextVal.isNone) iterator items*(pattern: Captures, default: string = nil): string = itemsImpl(nextVal == nil) -proc toSeq*(pattern: CaptureBounds, default = none(Slice[int, int])): seq[Option[Slice[int, int]]] = +proc toSeq*(pattern: CaptureBounds, default = none(HSlice[int, int])): seq[Option[HSlice[int, int]]] = accumulateResult(pattern.items(default)) proc toSeq*(pattern: Captures, default: string = nil): seq[string] = @@ -462,7 +462,7 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt # 1x capture count as slack space for PCRE let vecsize = (pattern.captureCount() + 1) * 3 # div 2 because each element is 2 cints long - myResult.pcreMatchBounds = newSeq[Slice[cint, cint]](ceil(vecsize / 2).int) + myResult.pcreMatchBounds = newSeq[HSlice[cint, cint]](ceil(vecsize / 2).int) myResult.pcreMatchBounds.setLen(vecsize div 3) let strlen = if endpos == int.high: str.len else: endpos+1 diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 02b381ad69..fdf2d7cbb8 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -425,7 +425,7 @@ proc rotatedInternal[T](arg: openarray[T]; first, middle, last: int): seq[T] = for i in last ..< arg.len: result[i] = arg[i] -proc rotateLeft*[T](arg: var openarray[T]; slice: Slice[int, int]; dist: int): int = +proc rotateLeft*[T](arg: var openarray[T]; slice: HSlice[int, int]; dist: int): int = ## Performs a left rotation on a range of elements. If you want to rotate right, use a negative ``dist``. ## Specifically, ``rotateLeft`` rotates the elements at ``slice`` by ``dist`` positions. ## The element at index ``slice.a + dist`` will be at index ``slice.a``. @@ -457,7 +457,7 @@ proc rotateLeft*[T](arg: var openarray[T]; dist: int): int = let distLeft = ((dist mod arglen) + arglen) mod arglen arg.rotateInternal(0, distLeft, arglen) -proc rotatedLeft*[T](arg: openarray[T]; slice: Slice[int, int], dist: int): seq[T] = +proc rotatedLeft*[T](arg: openarray[T]; slice: HSlice[int, int], dist: int): seq[T] = ## same as ``rotateLeft``, just with the difference that it does ## not modify the argument. It creates a new ``seq`` instead let sliceLen = slice.b + 1 - slice.a diff --git a/lib/pure/collections/sharedstrings.nim b/lib/pure/collections/sharedstrings.nim index a9e194fb44..83edf8d942 100644 --- a/lib/pure/collections/sharedstrings.nim +++ b/lib/pure/collections/sharedstrings.nim @@ -55,7 +55,7 @@ proc `[]=`*(s: var SharedString; i: Natural; value: char) = if i < s.len: s.buffer.data[i+s.first] = value else: raise newException(IndexError, "index out of bounds") -proc `[]`*(s: SharedString; ab: Slice[int]): SharedString = +proc `[]`*(s: SharedString; ab: HSlice[int, int]): SharedString = #incRef(src.buffer) if ab.a < s.len: result.buffer = s.buffer diff --git a/lib/pure/matchers.nim b/lib/pure/matchers.nim index 36daef8d17..6366fee1ab 100644 --- a/lib/pure/matchers.nim +++ b/lib/pure/matchers.nim @@ -48,7 +48,7 @@ proc validEmailAddress*(s: string): bool {.noSideEffect, "aero", "jobs", "museum": return true else: return false -proc parseInt*(s: string, value: var int, validRange: Slice[int, int]) {. +proc parseInt*(s: string, value: var int, validRange: HSlice[int, int]) {. noSideEffect, rtl, extern: "nmatchParseInt".} = ## parses `s` into an integer in the range `validRange`. If successful, ## `value` is modified to contain the result. Otherwise no exception is diff --git a/lib/pure/random.nim b/lib/pure/random.nim index 2c406faa1d..e6a9162c58 100644 --- a/lib/pure/random.nim +++ b/lib/pure/random.nim @@ -93,7 +93,7 @@ proc random*(max: float): float {.benign.} = let u = (0x3FFu64 shl 52u64) or (x shr 12u64) result = (cast[float](u) - 1.0) * max -proc random*[T](x: Slice[T, T]): T = +proc random*[T](x: HSlice[T, T]): T = ## For a slice `a .. b` returns a value in the range `a .. b-1`. result = T(random(x.b - x.a)) + x.a diff --git a/lib/system.nim b/lib/system.nim index 955159c2eb..eefad5e9c0 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -308,14 +308,12 @@ when defined(nimArrIdx): shallowCopy(x, y) type - Slice*[T, U] = object ## builtin slice type + HSlice*[T, U] = object ## "heterogenous" slice type a*: T ## the lower bound (inclusive) b*: U ## the upper bound (inclusive) + Slice*[T] = HSlice[T, T] ## an alias for ``HSlice[T, T]`` -when defined(nimalias): - {.deprecated: [TSlice: Slice].} - -proc `..`*[T, U](a: T, b: U): Slice[T, U] {.noSideEffect, inline, magic: "DotDot".} = +proc `..`*[T, U](a: T, b: U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDot".} = ## `slice`:idx: operator that constructs an interval ``[a, b]``, both `a` ## and `b` are inclusive. Slices can also be used in the set constructor ## and in ordinal case statements, but then they are special-cased by the @@ -323,7 +321,7 @@ proc `..`*[T, U](a: T, b: U): Slice[T, U] {.noSideEffect, inline, magic: "DotDot result.a = a result.b = b -proc `..`*[T](b: T): Slice[int, T] {.noSideEffect, inline, magic: "DotDot".} = +proc `..`*[T](b: T): HSlice[int, T] {.noSideEffect, inline, magic: "DotDot".} = ## `slice`:idx: operator that constructs an interval ``[default(T), b]`` result.b = b @@ -1169,7 +1167,7 @@ proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.} ## is achieved by reversing the parameters for ``contains``; ``in`` then ## passes its arguments in reverse order. -proc contains*[T](s: Slice[T, T], value: T): bool {.noSideEffect, inline.} = +proc contains*[T](s: HSlice[T, T], value: T): bool {.noSideEffect, inline.} = ## Checks if `value` is within the range of `s`; returns true iff ## `value >= s.a and value <= s.b` ## @@ -2088,7 +2086,7 @@ proc clamp*[T](x, a, b: T): T = if x > b: return b return x -proc len*[T: Ordinal](x: Slice[T, T]): int {.noSideEffect, inline.} = +proc len*[T: Ordinal](x: HSlice[T, T]): int {.noSideEffect, inline.} = ## length of ordinal slice, when x.b < x.a returns zero length ## ## .. code-block:: Nim @@ -2156,7 +2154,7 @@ iterator items*(E: typedesc[enum]): E = for v in low(E)..high(E): yield v -iterator items*[T](s: Slice[T, T]): T = +iterator items*[T](s: HSlice[T, T]): T = ## iterates over the slice `s`, yielding each value between `s.a` and `s.b` ## (inclusively). for x in s.a..s.b: @@ -3457,7 +3455,7 @@ template `^^`(s, i: untyped): untyped = (when i is BackwardsIndex: s.len - int(i) else: int(i)) when hasAlloc or defined(nimscript): - proc `[]`*[T, U](s: string, x: Slice[T, U]): string {.inline.} = + proc `[]`*[T, U](s: string, x: HSlice[T, U]): string {.inline.} = ## slice operation for strings. ## returns the inclusive range [s[x.a], s[x.b]]: ## @@ -3466,7 +3464,7 @@ when hasAlloc or defined(nimscript): ## assert s[1..3] == "bcd" result = s.substr(s ^^ x.a, s ^^ x.b) - proc `[]=`*[T, U](s: var string, x: Slice[T, U], b: string) = + proc `[]=`*[T, U](s: var string, x: HSlice[T, U], b: string) = ## slice assignment for strings. If ## ``b.len`` is not exactly the number of elements that are referred to ## by `x`, a `splice`:idx: is performed: @@ -3482,7 +3480,7 @@ when hasAlloc or defined(nimscript): else: spliceImpl(s, a, L, b) -proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: Slice[U, V]): seq[T] = +proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: HSlice[U, V]): seq[T] = ## slice operation for arrays. ## returns the inclusive range [a[x.a], a[x.b]]: ## @@ -3494,7 +3492,7 @@ proc `[]`*[Idx, T, U, V](a: array[Idx, T], x: Slice[U, V]): seq[T] = result = newSeq[T](L) for i in 0..