Skip to content

Commit 59b1919

Browse files
committed
Use more explicit casts in standard library
1 parent 2e37b3c commit 59b1919

File tree

8 files changed

+21
-25
lines changed

8 files changed

+21
-25
lines changed

Diff for: examples/condots.nelua

+9-9
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ end
133133
local function blend_pixel(x: integer, y: integer, col: vec4b <const>, opacity: number)
134134
local pcol = &pixels[SCREEN_HEIGHT - y - 1][x]
135135
local iopacity = 1-opacity
136-
pcol.x = math.floor(pcol.x * iopacity + col.x*opacity)
137-
pcol.y = math.floor(pcol.y * iopacity + col.y*opacity)
138-
pcol.z = math.floor(pcol.z * iopacity + col.z*opacity)
136+
pcol.x = math.ifloor(pcol.x * iopacity + col.x*opacity)
137+
pcol.y = math.ifloor(pcol.y * iopacity + col.y*opacity)
138+
pcol.z = math.ifloor(pcol.z * iopacity + col.z*opacity)
139139
end
140140

141141
local function lerp(v0: number, v1: number, t: number): number
@@ -171,10 +171,10 @@ end
171171
local INTERP_RADIUS <comptime> = 1
172172

173173
local function draw_circle(p: vec2, radius: number)
174-
local sx: integer = math.floor(math.max(p.x-radius-INTERP_RADIUS, 0))
175-
local ex: integer = math.floor(math.min(p.x+radius+INTERP_RADIUS, SCREEN_WIDTH-1))
176-
local sy: integer = math.floor(math.max(p.y-radius-INTERP_RADIUS, 0))
177-
local ey: integer = math.floor(math.min(p.y+radius+INTERP_RADIUS, SCREEN_HEIGHT-1))
174+
local sx: integer = math.ifloor(math.max(p.x-radius-INTERP_RADIUS, 0))
175+
local ex: integer = math.ifloor(math.min(p.x+radius+INTERP_RADIUS, SCREEN_WIDTH-1))
176+
local sy: integer = math.ifloor(math.max(p.y-radius-INTERP_RADIUS, 0))
177+
local ey: integer = math.ifloor(math.min(p.y+radius+INTERP_RADIUS, SCREEN_HEIGHT-1))
178178
for iy=sy,ey do
179179
for ix=sx,ex do
180180
local d = math.sqrt((ix-p.x)*(ix-p.x) + (iy-p.y)*(iy-p.y))
@@ -200,10 +200,10 @@ local function draw_line(a: vec2, b: vec2, radius: number, opacity: number)
200200
local sx, sy = delta.x >= 0 and 1 or -1, delta.y >= 0 and 1 or -1
201201
if math.abs(delta.y) < 1 or math.abs(delta.x) < 1 then return end
202202
for j=-INTERP_RADIUS-radius,math.abs(delta.x)+radius+INTERP_RADIUS do
203-
local ix: integer = math.floor(a.x + j*sx)
203+
local ix: integer = math.ifloor(a.x + j*sx)
204204
if ix >= 0 and ix < SCREEN_WIDTH then
205205
for i=-INTERP_RADIUS-radius,math.abs(delta.y)+radius+INTERP_RADIUS do
206-
local iy: integer = math.floor(a.y + i*sy)
206+
local iy: integer = math.ifloor(a.y + i*sy)
207207
if iy >= 0 and iy < SCREEN_HEIGHT then
208208
local p = vec2{ix, iy}
209209
local d = line_dist(a, b, p)

Diff for: examples/fibonacci.nelua

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ do -- Analytic solution.
3636
local function fibonacci(n: integer): integer
3737
local p = (1.0 + math.sqrt(5.0)) / 2.0
3838
local q = 1.0 / p
39-
return math.floor((p^n + q^n) / math.sqrt(5.0))
39+
return (@integer)(math.floor((p^n + q^n) / math.sqrt(5.0)))
4040
end
4141
print(fibonacci(10))
4242
end

Diff for: lib/detail/strpack.nelua

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ local function packint(sb: *stringbuilder, a: auto, size: usize, maxalign: usize
4747
if not sb:writebyte(0, pads) then return false end
4848
local buff: span(byte) = sb:prepare(size)
4949
if buff:empty() then return false end
50-
buff[islittle and 0 or size - 1] = n & 0xff -- first byte
50+
buff[islittle and 0_usize or size - 1] = (@byte)(n & 0xff) -- first byte
5151
for i: usize=1,<size do
5252
n = n >> 8
53-
buff[islittle and i or size-1-i] = n & 0xff
53+
buff[islittle and i or size-1-i] = (@byte)(n & 0xff)
5454
end
5555
if size > #@uint64 and neg then -- negative number, need sign extension?
5656
for i: usize=#@uint64,<size do

Diff for: lib/hash.nelua

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function hash.hash(v: auto): usize <nosideeffect>
9797
if #v > 0 then -- hash all elements
9898
h = hash.hash(v[0])
9999
for i:isize=1,<#v do
100-
h = hash.combine(h, v[i])
100+
h = hash.combine(h, hash.hash(v[i]))
101101
end
102102
end
103103
return h

Diff for: lib/os.nelua

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ function os.sleep(secs: number): boolean
684684
local function usleep(usec: cuint): cint <cimport,cinclude'@unistd.h'> end
685685
local us: uint64 <nodce> = (@uint64)(secs * 1000000)
686686
if us > 0 then
687-
ok = usleep(us) == 0
687+
ok = usleep((@cuint)(us)) == 0
688688
else
689689
ok = true
690690
end

Diff for: lib/string.nelua

+1-1
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ A third, optional argument specifies where to start the search, its default valu
834834
]]
835835
function string.match(s: string, pattern: string, init: facultative(isize)): (boolean, sequence(string))
836836
local ok: boolean, seq: sequence(string) = string_match(s, pattern, init)
837-
for i: isize = 1, #seq do
837+
for i: usize = 1, (@usize)(#seq) do
838838
seq[i] = string.copy(seq[i])
839839
end
840840
return ok, seq

Diff for: lib/stringbuilder.nelua

+4-8
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ end
257257
end
258258
## end if primtypes.clonglong:is_convertible_from(arg1.type) then
259259
if c == 'd'_b or c == 'i'_b then
260-
local lenmod: cstring = 'lli'
261-
addlenmod(form, lenmod, #lenmod - 1)
260+
addlenmod(form, 'lli', 2)
262261
local n: clonglong = (@clonglong)(arg1)
263262
nb = strprintf.snprintf((@cstring)(buf.data), MAX_ITEM, &form[0], n)
264263
goto next
@@ -267,8 +266,7 @@ end
267266
if c == 'o'_b or
268267
c == 'u'_b or
269268
c == 'x'_b or c == 'X'_b then
270-
local lenmod: cstring = 'llu'
271-
addlenmod(form, lenmod, #lenmod - 1)
269+
addlenmod(form, 'llu', 2)
272270
## if arg1.type.is_integral then
273271
local n: culonglong = (@culonglong)((#[arg1.type:unsigned_type()]#)(arg1))
274272
## else
@@ -283,14 +281,12 @@ end
283281
c == 'e'_b or c == 'E'_b or
284282
c == 'g'_b or c == 'G'_b then
285283
## if arg1.type.is_clongdouble then
286-
local lenmod: cstring = 'Lf'
287-
addlenmod(form, lenmod, #lenmod - 1)
284+
addlenmod(form, 'Lf', 1)
288285
nb = strprintf.snprintf((@cstring)(buf.data), MAX_ITEM, &form[0], arg1)
289286
## elseif arg1.type.is_float128 then
290287
## linklib 'quadmath'
291288
local function quadmath_snprintf(s: cstring, maxlen: csize, format: cstring, ...: cvarargs): cint <cimport,cinclude'<quadmath.h>'> end
292-
local lenmod: cstring = 'Qf'
293-
addlenmod(form, lenmod, #lenmod - 1)
289+
addlenmod(form, 'Qf', 1)
294290
nb = quadmath_snprintf((@cstring)(buf.data), MAX_ITEM, &form[0], arg1)
295291
## else
296292
local n: float64 = (@float64)(arg1)

Diff for: lib/utf8.nelua

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ local function utf8esc(x: uint32): ([8]byte, int32)
6363
until x <= mfb -- still needs continuation byte?
6464
buf[8 - n] = (@byte)((~mfb << 1) | x) -- add first byte
6565
end
66-
memory.move(&buf[0], &buf[8-n], n)
67-
memory.zero(&buf[n], 8-n)
66+
memory.move(&buf[0], &buf[8-n], (@usize)(n))
67+
memory.zero(&buf[n], (@usize)(8-n))
6868
return buf, n
6969
end
7070

0 commit comments

Comments
 (0)