moderate system cleanup & refactor (#20355)
* system refactor, move out 600 lines * compilation, slice, backwardsindex, misc_num moved out of system * some procs/types moved into arithmetics, basic_types * system no longer depends on syncio * some procs moved around to fit with their surroundings * make exceptions an import, old ops to misc_num * move instantiationInfo back * move back nim version, fix windows echo * include compilation * better docs for imported modules, fix unsigned ops also remove ze, ze64, toU8, toU16, toU32 with nimPreviewSlimSystem * fix terminal * workaround IC test & weird csize bug, changelog * move NimMajor etc back to compilation, rebase for CI * try ic fix * form single `indices`, slim out TaintedString, try fix IC * fix CI, update changelog, addQuitProc * fix CI * try fix CI * actually fix CI finally hopefully * Update lib/system/compilation.nim Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com> * update kochdocs * hopefully fix csize uses for slimsystem * fix tquit Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
parent
b463c8aedf
commit
919a889ba8
37 changed files with 1167 additions and 1102 deletions
156
lib/system/indices.nim
Normal file
156
lib/system/indices.nim
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
type
|
||||
BackwardsIndex* = distinct int ## Type that is constructed by `^` for
|
||||
## reversed array accesses.
|
||||
## (See `^ template <#^.t,int>`_)
|
||||
|
||||
template `^`*(x: int): BackwardsIndex = BackwardsIndex(x)
|
||||
## Builtin `roof`:idx: operator that can be used for convenient array access.
|
||||
## `a[^x]` is a shortcut for `a[a.len-x]`.
|
||||
##
|
||||
## ```
|
||||
## let
|
||||
## a = [1, 3, 5, 7, 9]
|
||||
## b = "abcdefgh"
|
||||
##
|
||||
## echo a[^1] # => 9
|
||||
## echo b[^2] # => g
|
||||
## ```
|
||||
|
||||
proc `[]`*[T](s: openArray[T]; i: BackwardsIndex): T {.inline.} =
|
||||
system.`[]`(s, s.len - int(i))
|
||||
|
||||
proc `[]`*[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T {.inline.} =
|
||||
a[Idx(a.len - int(i) + int low(a))]
|
||||
proc `[]`*(s: string; i: BackwardsIndex): char {.inline.} = s[s.len - int(i)]
|
||||
|
||||
proc `[]`*[T](s: var openArray[T]; i: BackwardsIndex): var T {.inline.} =
|
||||
system.`[]`(s, s.len - int(i))
|
||||
proc `[]`*[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T {.inline.} =
|
||||
a[Idx(a.len - int(i) + int low(a))]
|
||||
proc `[]`*(s: var string; i: BackwardsIndex): var char {.inline.} = s[s.len - int(i)]
|
||||
|
||||
proc `[]=`*[T](s: var openArray[T]; i: BackwardsIndex; x: T) {.inline.} =
|
||||
system.`[]=`(s, s.len - int(i), x)
|
||||
proc `[]=`*[Idx, T](a: var array[Idx, T]; i: BackwardsIndex; x: T) {.inline.} =
|
||||
a[Idx(a.len - int(i) + int low(a))] = x
|
||||
proc `[]=`*(s: var string; i: BackwardsIndex; x: char) {.inline.} =
|
||||
s[s.len - int(i)] = x
|
||||
|
||||
template `..^`*(a, b: untyped): untyped =
|
||||
## A shortcut for `.. ^` to avoid the common gotcha that a space between
|
||||
## '..' and '^' is required.
|
||||
a .. ^b
|
||||
|
||||
template `..<`*(a, b: untyped): untyped =
|
||||
## A shortcut for `a .. pred(b)`.
|
||||
## ```
|
||||
## for i in 5 ..< 9:
|
||||
## echo i # => 5; 6; 7; 8
|
||||
## ```
|
||||
a .. (when b is BackwardsIndex: succ(b) else: pred(b))
|
||||
|
||||
template `[]`*(s: string; i: int): char = arrGet(s, i)
|
||||
template `[]=`*(s: string; i: int; val: char) = arrPut(s, i, val)
|
||||
|
||||
template `^^`(s, i: untyped): untyped =
|
||||
(when i is BackwardsIndex: s.len - int(i) else: int(i))
|
||||
|
||||
template spliceImpl(s, a, L, b: untyped): untyped =
|
||||
# make room for additional elements or cut:
|
||||
var shift = b.len - max(0,L) # ignore negative slice size
|
||||
var newLen = s.len + shift
|
||||
if shift > 0:
|
||||
# enlarge:
|
||||
setLen(s, newLen)
|
||||
for i in countdown(newLen-1, a+b.len): movingCopy(s[i], s[i-shift])
|
||||
else:
|
||||
for i in countup(a+b.len, newLen-1): movingCopy(s[i], s[i-shift])
|
||||
# cut down:
|
||||
setLen(s, newLen)
|
||||
# fill the hole:
|
||||
for i in 0 ..< b.len: s[a+i] = b[i]
|
||||
|
||||
proc `[]`*[T, U: Ordinal](s: string, x: HSlice[T, U]): string {.inline.} =
|
||||
## Slice operation for strings.
|
||||
## Returns the inclusive range `[s[x.a], s[x.b]]`:
|
||||
## ```
|
||||
## var s = "abcdef"
|
||||
## assert s[1..3] == "bcd"
|
||||
## ```
|
||||
let a = s ^^ x.a
|
||||
let L = (s ^^ x.b) - a + 1
|
||||
result = newString(L)
|
||||
for i in 0 ..< L: result[i] = s[i + a]
|
||||
|
||||
proc `[]=`*[T, U: Ordinal](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:
|
||||
##
|
||||
runnableExamples:
|
||||
var s = "abcdefgh"
|
||||
s[1 .. ^2] = "xyz"
|
||||
assert s == "axyzh"
|
||||
|
||||
var a = s ^^ x.a
|
||||
var L = (s ^^ x.b) - a + 1
|
||||
if L == b.len:
|
||||
for i in 0..<L: s[i+a] = b[i]
|
||||
else:
|
||||
spliceImpl(s, a, L, b)
|
||||
|
||||
proc `[]`*[Idx, T; U, V: Ordinal](a: array[Idx, T], x: HSlice[U, V]): seq[T] =
|
||||
## Slice operation for arrays.
|
||||
## Returns the inclusive range `[a[x.a], a[x.b]]`:
|
||||
## ```
|
||||
## var a = [1, 2, 3, 4]
|
||||
## assert a[0..2] == @[1, 2, 3]
|
||||
## ```
|
||||
let xa = a ^^ x.a
|
||||
let L = (a ^^ x.b) - xa + 1
|
||||
result = newSeq[T](L)
|
||||
for i in 0..<L: result[i] = a[Idx(i + xa)]
|
||||
|
||||
proc `[]=`*[Idx, T; U, V: Ordinal](a: var array[Idx, T], x: HSlice[U, V], b: openArray[T]) =
|
||||
## Slice assignment for arrays.
|
||||
## ```
|
||||
## var a = [10, 20, 30, 40, 50]
|
||||
## a[1..2] = @[99, 88]
|
||||
## assert a == [10, 99, 88, 40, 50]
|
||||
## ```
|
||||
let xa = a ^^ x.a
|
||||
let L = (a ^^ x.b) - xa + 1
|
||||
if L == b.len:
|
||||
for i in 0..<L: a[Idx(i + xa)] = b[i]
|
||||
else:
|
||||
sysFatal(RangeDefect, "different lengths for slice assignment")
|
||||
|
||||
proc `[]`*[T; U, V: Ordinal](s: openArray[T], x: HSlice[U, V]): seq[T] =
|
||||
## Slice operation for sequences.
|
||||
## Returns the inclusive range `[s[x.a], s[x.b]]`:
|
||||
## ```
|
||||
## var s = @[1, 2, 3, 4]
|
||||
## assert s[0..2] == @[1, 2, 3]
|
||||
## ```
|
||||
let a = s ^^ x.a
|
||||
let L = (s ^^ x.b) - a + 1
|
||||
newSeq(result, L)
|
||||
for i in 0 ..< L: result[i] = s[i + a]
|
||||
|
||||
proc `[]=`*[T; U, V: Ordinal](s: var seq[T], x: HSlice[U, V], b: openArray[T]) =
|
||||
## Slice assignment for sequences.
|
||||
##
|
||||
## If `b.len` is not exactly the number of elements that are referred to
|
||||
## by `x`, a `splice`:idx: is performed.
|
||||
runnableExamples:
|
||||
var s = @"abcdefgh"
|
||||
s[1 .. ^2] = @"xyz"
|
||||
assert s == @"axyzh"
|
||||
|
||||
let a = s ^^ x.a
|
||||
let L = (s ^^ x.b) - a + 1
|
||||
if L == b.len:
|
||||
for i in 0 ..< L: s[i+a] = b[i]
|
||||
else:
|
||||
spliceImpl(s, a, L, b)
|
||||
Loading…
Add table
Add a link
Reference in a new issue