diff options
author | bptato <nincsnevem662@gmail.com> | 2024-04-26 16:19:09 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-04-26 16:24:27 +0200 |
commit | c1bcb8a4d833d035eb199546aabc44dbe10252c6 (patch) | |
tree | 4a2acaadf7f4ef4d4ccb32e3e6e558141d26d73e | |
parent | d93c5700d432afc265fab1fbfef663f77361635d (diff) | |
download | chawan-c1bcb8a4d833d035eb199546aabc44dbe10252c6.tar.gz |
Remove unnecessary unsigned casts
Unsigned operations and conversions to unsigned types always wrap/narrow without checks, so no need to manually mask/cast/etc. them.
-rw-r--r-- | src/img/png.nim | 32 | ||||
-rw-r--r-- | src/js/fromjs.nim | 2 | ||||
-rw-r--r-- | src/local/term.nim | 4 | ||||
-rw-r--r-- | src/types/color.nim | 42 | ||||
-rw-r--r-- | src/types/url.nim | 4 | ||||
-rw-r--r-- | src/utils/twtstr.nim | 2 |
6 files changed, 43 insertions, 43 deletions
diff --git a/src/img/png.nim b/src/img/png.nim index d41360a3..0b486f23 100644 --- a/src/img/png.nim +++ b/src/img/png.nim @@ -257,7 +257,7 @@ proc readtRNS(reader: var PNGReader) = for i in 0 ..< reader.palette.len: reader.palette[i].a = reader.readU8() -func paethPredictor(a, b, c: uint16): uint16 = +func paethPredictor(a, b, c: uint8): uint8 = let pa0 = int(b) - int(c) let pb0 = int(a) - int(c) let pa = abs(pa0) @@ -273,36 +273,36 @@ proc unfilter(reader: var PNGReader; irow: openArray[uint8]; bpp: int) = for i in 1 ..< irow.len: let j = i - 1 # skip filter byte let aidx = j - bpp - let x = uint16(irow[i]) - let a = if aidx >= 0: uint16(reader.uprow[aidx]) else: 0u16 - reader.uprow[j] = uint8((x + a) and 0xFF) + let x = irow[i] + let a = if aidx >= 0: reader.uprow[aidx] else: 0u8 + reader.uprow[j] = x + a of 2u8: # up for i in 1 ..< irow.len: let j = i - 1 # skip filter byte - let x = uint16(irow[i]) - let b = uint16(reader.uprow[j]) - reader.uprow[j] = uint8((x + b) and 0xFF) + let x = irow[i] + let b = reader.uprow[j] + reader.uprow[j] = x + b of 3u8: # average for i in 1 ..< irow.len: let j = i - 1 # skip filter byte let aidx = j - bpp - let x = uint16(irow[i]) - let a = if aidx >= 0: uint16(reader.uprow[aidx]) else: 0u16 - let b = uint16(reader.uprow[j]) - reader.uprow[j] = uint8((x + (a + b) div 2) and 0xFF) + let x = irow[i] + let a = if aidx >= 0: reader.uprow[aidx] else: 0u8 + let b = reader.uprow[j] + reader.uprow[j] = x + uint8((uint16(a) + uint16(b)) div 2) of 4u8: # paeth - var cmap: array[8, uint16] # max bpp is 16 bit with true color = 4 * 2 + var cmap: array[8, uint8] # max bpp is 16 bit with true color = 4 * 2 var k = 0 for i in 1 ..< irow.len: let j = i - 1 # skip filter byte let aidx = j - bpp - let x = uint16(irow[i]) - let a = if aidx >= 0: uint16(reader.uprow[aidx]) else: 0u16 - let b = uint16(reader.uprow[j]) + let x = irow[i] + let a = if aidx >= 0: reader.uprow[aidx] else: 0u8 + let b = reader.uprow[j] let kk = k mod bpp let c = cmap[kk] cmap[kk] = b - reader.uprow[j] = uint8((x + paethPredictor(a, b, c)) and 0xFF) + reader.uprow[j] = x + paethPredictor(a, b, c) inc k else: reader.err "got invalid filter" diff --git a/src/js/fromjs.nim b/src/js/fromjs.nim index 4cdc18d1..527b6221 100644 --- a/src/js/fromjs.nim +++ b/src/js/fromjs.nim @@ -108,7 +108,7 @@ func fromJSInt[T: SomeInteger](ctx: JSContext; val: JSValue): var ret: uint32 if JS_ToUint32(ctx, addr ret, val) < 0: return err() - return ok(cast[uint64](ret)) + return ok(uint64(ret)) proc fromJSFloat64(ctx: JSContext; val: JSValue): JSResult[float64] = var f64: float64 diff --git a/src/local/term.nim b/src/local/term.nim index b9eda236..98c7fd91 100644 --- a/src/local/term.nim +++ b/src/local/term.nim @@ -335,7 +335,7 @@ proc correctContrast(term: Terminal; bgcolor, fgcolor: CellColor): CellColor = fgY = bgY - contrast if fgY < 0: fgY = 255 - let newrgb = YUV(cast[uint8](fgY), fgcolor.U, fgcolor.V) + let newrgb = YUV(uint8(fgY), fgcolor.U, fgcolor.V) case term.colorMode of cmTrueColor: return cellColor(newrgb) @@ -635,7 +635,7 @@ proc compressSixel(data: openArray[uint8]): string = var n = 0 var c = char(0) for u in data: - let cc = char((u + 0x3F) and 0xFF) + let cc = char(u + 0x3F) if c != cc: if n > 3: outs &= '!' & $n & c diff --git a/src/types/color.nim b/src/types/color.nim index df3a447c..922201da 100644 --- a/src/types/color.nim +++ b/src/types/color.nim @@ -48,7 +48,7 @@ func rgbacolor*(color: CellColor): RGBAColor = cast[RGBAColor](color.n) func color*(color: CellColor): uint8 = - cast[uint8](color.n) + uint8(color.n) func eightbit*(color: CellColor): EightBitColor = EightBitColor(color.color) @@ -236,16 +236,16 @@ const ColorsRGB* = block: res func r*(c: RGBAColor): uint8 = - return cast[uint8]((uint32(c) shr 16) and 0xff) + return uint8(uint32(c) shr 16) func g*(c: RGBAColor): uint8 = - return cast[uint8]((uint32(c) shr 8) and 0xff) + return uint8(uint32(c) shr 8) func b*(c: RGBAColor): uint8 = - return cast[uint8](uint32(c) and 0xff) + return uint8(uint32(c)) func a*(c: RGBAColor): uint8 = - return cast[uint8]((uint32(c) shr 24) and 0xff) + return uint8(uint32(c) shr 24) proc `r=`*(c: var RGBAColor, r: uint8) = c = RGBAColor(uint32(c) or (uint32(r) shl 16)) @@ -329,10 +329,10 @@ func blend*(c0, c1: RGBAColor): RGBAColor = let pc1 = c1.premul() let k = 255 - pc1.a let mc = fastmul1(pc0, uint32(k)) - let rr = cast[uint8](uint16(pc1.r) + uint16(mc.r)) - let rg = cast[uint8](uint16(pc1.g) + uint16(mc.g)) - let rb = cast[uint8](uint16(pc1.b) + uint16(mc.b)) - let ra = cast[uint8](uint16(pc1.a) + uint16(mc.a)) + let rr = pc1.r + mc.r + let rg = pc1.g + mc.g + let rb = pc1.b + mc.b + let ra = pc1.a + mc.a let pres = rgba(rr, rg, rb, ra) let res = straight(pres) return res @@ -341,32 +341,32 @@ func rgb*(r, g, b: uint8): RGBColor = return RGBColor((uint32(r) shl 16) or (uint32(g) shl 8) or uint32(b)) func r*(c: RGBColor): uint8 = - return cast[uint8]((uint32(c) shr 16) and 0xff) + return uint8(uint32(c) shr 16) func g*(c: RGBColor): uint8 = - return cast[uint8]((uint32(c) shr 8) and 0xff) + return uint8(uint32(c) shr 8) func b*(c: RGBColor): uint8 = - return cast[uint8](uint32(c) and 0xff) + return uint8(uint32(c)) # see https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms893078(v=msdn.10) func Y*(c: RGBColor): uint8 = let rmul = uint16(c.r) * 66u16 let gmul = uint16(c.g) * 129u16 let bmul = uint16(c.b) * 25u16 - return cast[uint8](((rmul + gmul + bmul + 128) shr 8) + 16) + return uint8(((rmul + gmul + bmul + 128) shr 8) + 16) func U*(c: RGBColor): uint8 = let rmul = uint16(c.r) * 38u16 let gmul = uint16(c.g) * 74u16 let bmul = uint16(c.b) * 112u16 - return cast[uint8](((128 + bmul - rmul - gmul) shr 8) + 128) + return uint8(((128 + bmul - rmul - gmul) shr 8) + 128) func V*(c: RGBColor): uint8 = let rmul = uint16(c.r) * 112u16 let gmul = uint16(c.g) * 94u16 let bmul = uint16(c.b) * 18u16 - return cast[uint8](((128 + rmul - gmul - bmul) shr 8) + 128) + return uint8(((128 + rmul - gmul - bmul) shr 8) + 128) func YUV*(Y, U, V: uint8): RGBColor = let C = int(Y) - 16 @@ -375,7 +375,7 @@ func YUV*(Y, U, V: uint8): RGBColor = let r = max(min((298 * C + 409 * E + 128) shr 8, 255), 0) let g = max(min((298 * C - 100 * D - 208 * E + 128) shr 8, 255), 0) let b = max(min((298 * C + 516 * D + 128) shr 8, 255), 0) - return rgb(cast[uint8](r), cast[uint8](g), cast[uint8](b)) + return rgb(uint8(r), uint8(g), uint8(b)) func rgba*(r, g, b, a: uint8): RGBAColor = return RGBAColor((uint32(a) shl 24) or (uint32(r) shl 16) or @@ -394,10 +394,10 @@ func toRGB*(param0: EightBitColor): RGBColor = if u in 16u8..231u8: #16 + 36 * r + 6 * g + b let n = u - 16 - let r = cast[uint8](int(n div 36) * 255 div 5) + let r = uint8(int(n div 36) * 255 div 5) let m = int(n mod 36) - let g = cast[uint8](((m div 6) * 255) div 5) - let b = cast[uint8](((m mod 6) * 255) div 5) + let g = uint8(((m div 6) * 255) div 5) + let b = uint8(((m mod 6) * 255) div 5) return rgb(r, g, b) else: # 232..255 let n = (u - 232) * 10 + 8 @@ -415,9 +415,9 @@ func toEightBit*(rgb: RGBColor): EightBitColor = return EightBitColor(16) if r > 248: return EightBitColor(231) - return EightBitColor(cast[uint8](((r - 8) * 24 div 247) + 232)) + return EightBitColor(uint8(((r - 8) * 24 div 247) + 232)) #16 + 36 * r + 6 * g + b - return EightBitColor(cast[uint8](16 + 36 * (r * 5 div 255) + + return EightBitColor(uint8(16 + 36 * (r * 5 div 255) + 6 * (g * 5 div 255) + (b * 5 div 255))) template `$`*(rgbcolor: RGBColor): string = diff --git a/src/types/url.nim b/src/types/url.nim index 3426bf39..860134c8 100644 --- a/src/types/url.nim +++ b/src/types/url.nim @@ -324,7 +324,7 @@ func processIdna(str: string; beStrict: bool): Option[string] = # CheckHyphens is false if x0.len > 0: let cps = cast[ptr UncheckedArray[u32pair]](cr.points) - let c = cast[uint32](x0[0]) + let c = uint32(x0[0]) let L = cr.len div 2 - 1 if cps.toOpenArray(0, L).binarySearch(c, cmpRange) != -1: return none(string) #error @@ -664,7 +664,7 @@ proc basicParseURL*(input: string; base = none(URL); url: URL = URL(); let i = parseInt32(buffer) if i.isNone or i.get notin 0..65535: return none(URL) - let port = cast[uint16](i.get).some + let port = uint16(i.get).some url.port = if url.is_special and url.default_port == port: none(uint16) else: diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 6cab9ac7..b1c6d1c9 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -91,7 +91,7 @@ func pushHex*(buf: var string; u: uint8) = buf &= HexCharsUpper[u and 0xF] func pushHex*(buf: var string; c: char) = - buf.pushHex(cast[uint8](c)) + buf.pushHex(uint8(c)) func toHexLower*(u: uint16): string = var x = u |