Skip to content

Commit a8b66ec

Browse files
committed
typetraits: add toSigned, toUnsigned
1 parent c41af7e commit a8b66ec

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

lib/pure/typetraits.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ runnableExamples:
3030
type C[T] = enum h0 = 2, h1 = 4
3131
assert C[float] is HoleyEnum
3232

33+
runnableExamples:
34+
# `toSigned`, `toUnsigned`
35+
assert int8.toSigned is int8
36+
assert int8.toUnsigned is uint8
37+
assert uint.toUnsigned is uint
38+
assert int.toUnsigned is uint
39+
assert $int32.toUnsigned == "uint32"
40+
3341
proc name*(t: typedesc): string {.magic: "TypeTrait".} =
3442
## Returns the name of the given type.
3543
##
@@ -284,3 +292,7 @@ proc hasClosure*(fn: NimNode): bool {.since: (1, 5, 1).} =
284292
## arguments and not `untyped` arguments.
285293
expectKind fn, nnkSym
286294
result = hasClosureImpl(fn)
295+
296+
from std/private/vmutils import toUnsigned, toSigned
297+
298+
export toUnsigned, toSigned

lib/std/private/vmutils.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# xxx rename to something more meaningful
2+
13
template forwardImpl*(impl, arg) {.dirty.} =
24
when sizeof(x) <= 4:
35
when x is SomeSignedInt:

lib/system/strmantle.nim

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Compilerprocs for strings that do not depend on the string implementation.
1111

1212
import std/private/digitsutils
13-
import std/private/vmutils
13+
1414

1515
proc cmpStrings(a, b: string): int {.inline, compilerproc.} =
1616
let alen = a.len
@@ -43,17 +43,6 @@ proc hashString(s: string): int {.compilerproc.} =
4343
h = h + h shl 15
4444
result = cast[int](h)
4545

46-
proc absUnsigned[T](x: T): auto {.inline.} =
47-
## computes `abs(x)` as unsigned without branching, taking care of `T.low`
48-
type T2 = toUnsigned(T)
49-
result = (not cast[T2](x)) + 1
50-
when false:
51-
# faster than branching via:
52-
if x == low(T):
53-
num = T2(x)
54-
else:
55-
num = T2(-x)
56-
5746
proc addInt*(result: var string; x: int64) =
5847
## Converts integer to its string representation and appends it to `result`.
5948
##
@@ -63,9 +52,12 @@ proc addInt*(result: var string; x: int64) =
6352
## b = 45
6453
## a.addInt(b) # a <- "12345"
6554
var num: uint64
55+
6656
if x < 0:
67-
# num = absUnsigned(x)
68-
num = (not cast[uint64](x)) + 1
57+
if x == low(int64):
58+
num = uint64(x)
59+
else:
60+
num = uint64(-x)
6961
let base = result.len
7062
setLen(result, base + 1)
7163
result[base] = '-'

tests/metatype/ttypetraits.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import typetraits
22
import macros
33

4+
block: # toUnsigned, toSigned
5+
var a1: toSigned(int16)
6+
doAssert a1 is int16
7+
var a2: toSigned(uint16)
8+
doAssert $a2.typeof == "int16"
9+
doAssert toSigned(uint32) is int32
10+
doAssert uint64.toSigned is int64
11+
doAssert int64.toSigned is int64
12+
doAssert int64.toUnsigned is uint64
13+
doAssert int.toUnsigned is uint
14+
doAssert $uint.toUnsigned == "uint"
15+
416
block: # isNamedTuple
517
type Foo1 = (a:1,).type
618
type Foo2 = (Field0:1,).type

tests/stdlib/ttypetraits.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ discard """
22
targets: "c cpp js"
33
"""
44

5-
import std/typetraits
6-
5+
# xxx merge with tests/metatype/ttypetraits.nim
76

7+
import std/typetraits
88

99
macro testClosure(fn: typed, flag: static bool) =
1010
if flag:

0 commit comments

Comments
 (0)