vlib: run vfmt over vlib files, so that `v doc -m vlib/` can run without warnings

pull/6665/head
Delyan Angelov 2020-10-21 12:23:03 +03:00
parent 5b1ab3b0bb
commit dab66593fc
27 changed files with 343 additions and 334 deletions

View File

@ -18,6 +18,7 @@ const (
'vlib/builtin/js/jsfns.js.v',
'vlib/builtin/js/jsfns_browser.js.v',
'vlib/builtin/bare/linuxsys_bare.v', // error: expr(): bad token `asm`, on `asm {}`
'vlib/picoev/picoev.v', // the fn args are removed, then `cb fn (picohttpparser.Request, mut picohttpparser.Response)` can not be reparsed
'vlib/os/os.v',
]
)

View File

@ -89,7 +89,7 @@ pub fn shuffle<T>(mut a []T, n int) {
// merge two sorted arrays (ascending) and maintain sorted order
[direct_array_access]
pub fn merge<T>(a, b []T) []T {
pub fn merge<T>(a []T, b []T) []T {
mut m := []T{len: a.len + b.len}
mut ia := 0
mut ib := 0

View File

@ -160,7 +160,7 @@ fn has_command(cmd cli.Command, name string) bool {
return false
}
fn compare_arrays(array0, array1 []string) bool {
fn compare_arrays(array0 []string, array1 []string) bool {
if array0.len != array1.len {
return false
}

View File

@ -93,7 +93,7 @@ pub fn (flags []Flag) get_string(name string) ?string {
return flag.get_string()
}
pub fn (flags []Flag) get_string_or(name, or_value string) string {
pub fn (flags []Flag) get_string_or(name string, or_value string) string {
value := flags.get_string(name) or {
return or_value
}

View File

@ -165,7 +165,7 @@ fn pretty_description(s string, indent_len int) string {
return acc.str()
}
fn max(a, b int) int {
fn max(a int, b int) int {
res := if a > b { a } else { b }
return res
}

View File

@ -1,14 +1,12 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module cipher
// NOTE: Implement other versions (joe-c)
// xor_bytes xors the bytes in a and b. The destination should have enough
// space, otherwise xor_bytes will panic. Returns the number of bytes xor'd.
pub fn xor_bytes(mut dst []byte, a, b []byte) int {
pub fn xor_bytes(mut dst []byte, a []byte, b []byte) int {
mut n := a.len
if b.len < n {
n = b.len
@ -16,21 +14,19 @@ pub fn xor_bytes(mut dst []byte, a, b []byte) int {
if n == 0 {
return 0
}
safe_xor_bytes(mut dst, a, b, n)
return n
}
// n needs to be smaller or equal than the length of a and b.
pub fn safe_xor_bytes(mut dst []byte, a, b []byte, n int) {
for i in 0..n {
pub fn safe_xor_bytes(mut dst []byte, a []byte, b []byte, n int) {
for i in 0 .. n {
dst[i] = a[i] ^ b[i]
}
}
// fast_xor_words XORs multiples of 4 or 8 bytes (depending on architecture.)
// The slice arguments a and b are assumed to be of equal length.
pub fn xor_words(mut dst []byte, a, b []byte) {
pub fn xor_words(mut dst []byte, a []byte, b []byte) {
safe_xor_bytes(mut dst, a, b, b.len)
}

View File

@ -11,7 +11,7 @@ const (
)
// Returns an HMAC byte array, depending on the hash algorithm used
pub fn new(key, data []byte, hash_func fn (bytes []byte) []byte, blocksize int) []byte {
pub fn new(key []byte, data []byte, hash_func fn (bytes []byte) []byte, blocksize int) []byte {
mut b_key := []byte{}
if key.len <= blocksize {
b_key = key.clone() // TODO: remove .clone() once https://github.com/vlang/v/issues/6604 gets fixed
@ -39,6 +39,6 @@ pub fn new(key, data []byte, hash_func fn (bytes []byte) []byte, blocksize int)
// equal compares 2 MACs for equality, without leaking timing info
// NB: if the lengths of the 2 MACs are different, probably a completely different
// hash function was used to generate them => no useful timing information.
pub fn equal(mac1, mac2 []byte) bool {
pub fn equal(mac1 []byte, mac2 []byte) bool {
return subtle.constant_time_compare(mac1, mac2) == 1
}

View File

@ -1,23 +1,18 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Package subtle implements functions that are often useful in cryptographic
// code but require careful thought to use correctly.
module subtle
// NOTE: require unsafe in future
// any_overlap reports whether x and y share memory at any (not necessarily
// corresponding) index. The memory beyond the slice length is ignored.
pub fn any_overlap(x, y []byte) bool {
pub fn any_overlap(x []byte, y []byte) bool {
// NOTE: Remember to come back to this (joe-c)
return x.len > 0 && y.len > 0 &&
// &x.data[0] <= &y.data[y.len-1] &&
// &y.data[0] <= &x.data[x.len-1]
unsafe { &x[0] <= &y[y.len-1] &&
&y[0] <= &x[x.len-1] }
return x.len > 0 && y.len > 0 && // &x.data[0] <= &y.data[y.len-1] &&
// &y.data[0] <= &x.data[x.len-1]
unsafe {&x[0] <= &y[y.len - 1] && &y[0] <= &x[x.len - 1]}
}
// inexact_overlap reports whether x and y share memory at any non-corresponding
@ -26,8 +21,8 @@ pub fn any_overlap(x, y []byte) bool {
//
// inexact_overlap can be used to implement the requirements of the crypto/cipher
// AEAD, Block, BlockMode and Stream interfaces.
pub fn inexact_overlap(x, y []byte) bool {
if x.len == 0 || y.len == 0 || unsafe { &x[0] == &y[0] } {
pub fn inexact_overlap(x []byte, y []byte) bool {
if x.len == 0 || y.len == 0 || unsafe {&x[0] == &y[0]} {
return false
}
return any_overlap(x, y)

View File

@ -1,25 +1,25 @@
module subtle
// constant_time_byte_eq returns 1 when x == y.
pub fn constant_time_byte_eq(x, y byte) int {
pub fn constant_time_byte_eq(x byte, y byte) int {
return int((u32(x ^ y) - 1) >> 31)
}
// constant_time_eq returns 1 when x == y.
pub fn constant_time_eq(x, y int) int {
pub fn constant_time_eq(x int, y int) int {
return int((u64(u32(x ^ y)) - 1) >> 63)
}
// constant_time_select returns x when v == 1, and y when v == 0.
// it is undefined when v is any other value
pub fn constant_time_select(v, x, y int) int {
pub fn constant_time_select(v int, x int, y int) int {
return (~(v - 1) & x) | ((v - 1) & y)
}
// constant_time_compare returns 1 when x and y have equal contents.
// The runtime of this function is proportional of the length of x and y.
// It is *NOT* dependent on their content.
pub fn constant_time_compare(x, y []byte) int {
pub fn constant_time_compare(x []byte, y []byte) int {
if x.len != y.len {
return 0
}
@ -46,7 +46,7 @@ pub fn constant_time_copy(v int, mut x []byte, y []byte) {
// constant_time_less_or_eq returns 1 if x <= y, and 0 otherwise.
// it is undefined when x or y are negative, or > (2^32 - 1)
pub fn constant_time_less_or_eq(x, y int) int {
pub fn constant_time_less_or_eq(x int, y int) int {
x32 := int(x)
y32 := int(y)
return int(((x32 - y32 - 1) >> 31) & 1)

View File

@ -1,16 +1,13 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's
// Applied Cryptography.
//
// RC4 is cryptographically broken and should not be used for secure
// applications.
// Based off: https://github.com/golang/go/blob/master/src/crypto/rc4
// Last commit: https://github.com/golang/go/commit/b35dacaac57b039205d9b07ea24098e2c3fcb12e
module rc4
import crypto.internal.subtle
@ -30,14 +27,14 @@ pub fn new_cipher(key []byte) ?Cipher {
return error('crypto.rc4: invalid key size ' + key.len.str())
}
mut c := Cipher{
s: []u32{len:(256)}
s: []u32{len: (256)}
}
for i in 0..256 {
for i in 0 .. 256 {
c.s[i] = u32(i)
}
mut j := byte(0)
for i in 0..256 {
j += byte(c.s[i]) + key[i%key.len]
for i in 0 .. 256 {
j += byte(c.s[i]) + key[i % key.len]
tmp := c.s[i]
c.s[i] = c.s[j]
c.s[j] = tmp
@ -59,7 +56,7 @@ pub fn (mut c Cipher) reset() {
// xor_key_stream sets dst to the result of XORing src with the key stream.
// Dst and src must overlap entirely or not at all.
pub fn (mut c Cipher) xor_key_stream(mut dst, src []byte) {
pub fn (mut c Cipher) xor_key_stream(mut dst []byte, mut src []byte) {
if src.len == 0 {
return
}
@ -75,7 +72,7 @@ pub fn (mut c Cipher) xor_key_stream(mut dst, src []byte) {
y := c.s[j]
c.s[i] = y
c.s[j] = x
dst[k] = v ^ byte(c.s[byte(x+y)])
dst[k] = v ^ byte(c.s[byte(x + y)])
}
c.i = i
c.j = j

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module glm
import math
@ -33,17 +32,17 @@ struct Vec3 {
z f32
}
pub fn vec3(x, y, z f32) Vec3 {
res := Vec3 {
x: x,
y: y,
z: z,
pub fn vec3(x f32, y f32, z f32) Vec3 {
res := Vec3{
x: x
y: y
z: z
}
return res
}
fn mat4(f &f32) Mat4 {
res := Mat4 {
res := Mat4{
data: f
}
return res
@ -59,11 +58,11 @@ pub fn (v Vec2) str() string {
pub fn (m Mat4) str() string {
mut s := '[ '
for i in 0..4 {
for i in 0 .. 4 {
if i != 0 {
s += ' '
}
for j in 0..4 {
for j in 0 .. 4 {
val := unsafe {m.data[i * 4 + j]}
s += '${val:5.2f} '
}
@ -75,28 +74,28 @@ pub fn (m Mat4) str() string {
return s
}
fn vec2(x, y int) Vec2 {
res := Vec2 {
x: f32(x),
y: f32(y),
fn vec2(x int, y int) Vec2 {
res := Vec2{
x: f32(x)
y: f32(y)
}
return res
}
fn (a Vec3) add(b Vec3) Vec3 {
res := Vec3 {
x: a.x + b.x,
y: a.y + b.y,
z: a.z + b.z,
res := Vec3{
x: a.x + b.x
y: a.y + b.y
z: a.z + b.z
}
return res
}
fn (a Vec3) sub(b Vec3) Vec3 {
res := Vec3 {
x: a.x - b.x,
y: a.y - b.y,
z: a.z - b.z,
res := Vec3{
x: a.x - b.x
y: a.y - b.y
z: a.z - b.z
}
return res
}
@ -105,10 +104,10 @@ fn (a Vec3) sub(b Vec3) Vec3 {
// # return myglm_mult(a,b);
// }
fn (a Vec3) mult_scalar(b f32) Vec3 {
res := Vec3 {
x: a.x * b,
y: a.y * b,
z: a.z * b,
res := Vec3{
x: a.x * b
y: a.y * b
z: a.z * b
}
return res
}
@ -117,7 +116,7 @@ fn (a Vec3) print() {
x := a.x
y := a.y
z := a.z
C.printf('vec3{%f,%f,%f}\n',x,y,z)
C.printf('vec3{%f,%f,%f}\n', x, y, z)
// println('vec3{$x,$y,$z}')
}
@ -129,10 +128,10 @@ fn rotate(m Mat4, angle f32, vec Vec3) Mat4 {
return Mat4{}
}
*/
fn f32_calloc(n int) &f32 {
return voidptr(vcalloc(n * int(sizeof(f32))))
}
// fn translate(vec Vec3) *f32 {
pub fn translate(m Mat4, v Vec3) Mat4 {
// # return glm__mat4(myglm_translate(vec.x,vec.y,vec.z) );
@ -142,12 +141,30 @@ pub fn translate(m Mat4, v Vec3) Mat4 {
y := v.y
z := v.z
unsafe {
a00 := a[0] a01 := a[1] a02 := a[2] a03 := a[3]
a10 := a[4] a11 := a[5] a12 := a[6] a13 := a[7]
a20 := a[8] a21 := a[9] a22 := a[10] a23 := a[11]
out[0] = a00 out[1] = a01 out[2] = a02 out[3] = a03
out[4] = a10 out[5] = a11 out[6] = a12 out[7] = a13
out[8] = a20 out[9] = a21 out[10] = a22 out[11] = a23
a00 := a[0]
a01 := a[1]
a02 := a[2]
a03 := a[3]
a10 := a[4]
a11 := a[5]
a12 := a[6]
a13 := a[7]
a20 := a[8]
a21 := a[9]
a22 := a[10]
a23 := a[11]
out[0] = a00
out[1] = a01
out[2] = a02
out[3] = a03
out[4] = a10
out[5] = a11
out[6] = a12
out[7] = a13
out[8] = a20
out[9] = a21
out[10] = a22
out[11] = a23
out[12] = a00 * x + a10 * y + a20 * z + a[12]
out[13] = a01 * x + a11 * y + a21 * z + a[13]
out[14] = a02 * x + a12 * y + a22 * z + a[14]
@ -163,8 +180,8 @@ fn normalize(vec Vec3) Vec3 {
}
*/
// https://github.com/g-truc/glm/blob/0ceb2b755fb155d593854aefe3e45d416ce153a4/glm/ext/matrix_clip_space.inl
pub fn ortho(left, right, bottom, top f32) Mat4 {
//println('glm ortho($left, $right, $bottom, $top)')
pub fn ortho(left f32, right f32, bottom f32, top f32) Mat4 {
// println('glm ortho($left, $right, $bottom, $top)')
// mat<4, 4, T, defaultp> Result(static_cast<T>(1));
n := 16
mut res := f32_calloc(n)
@ -172,16 +189,16 @@ pub fn ortho(left, right, bottom, top f32) Mat4 {
res[0] = 2.0 / (right - left)
res[5] = 2.0 / (top - bottom)
res[10] = 1.0
res[12] = - (right + left) / (right - left)
res[13] = - (top + bottom) / (top - bottom)
res[12] = -(right + left) / (right - left)
res[13] = -(top + bottom) / (top - bottom)
res[15] = 1.0
}
return mat4(res)
}
// https://github.com/g-truc/glm/blob/0ceb2b755fb155d593854aefe3e45d416ce153a4/glm/ext/matrix_clip_space.inl
pub fn ortho_zo(left, right, bottom, top, zNear, zFar f32) Mat4 {
//println('glm ortho($left, $right, $bottom, $top)')
pub fn ortho_zo(left f32, right f32, bottom f32, top f32, zNear f32, zFar f32) Mat4 {
// println('glm ortho($left, $right, $bottom, $top)')
// mat<4, 4, T, defaultp> Result(static_cast<T>(1));
n := 16
mut res := f32_calloc(n)
@ -189,9 +206,9 @@ pub fn ortho_zo(left, right, bottom, top, zNear, zFar f32) Mat4 {
res[0] = 2.0 / (right - left)
res[5] = 2.0 / (top - bottom)
res[10] = 1.0
res[12] = - (right + left) / (right - left)
res[13] = - (top + bottom) / (top - bottom)
res[14] = - zNear / (zFar - zNear)
res[12] = -(right + left) / (right - left)
res[13] = -(top + bottom) / (top - bottom)
res[14] = -zNear / (zFar - zNear)
res[15] = 1.0
}
return mat4(res)
@ -226,16 +243,16 @@ pub fn scale(m Mat4, v Vec3) Mat4 {
}
// multiplicates two matrices
pub fn mult(a, b Mat4) Mat4 {
pub fn mult(a Mat4, b Mat4) Mat4 {
mut out := f32_calloc(16)
for i in 0..4 {
for r in 0..4 {
for i in 0 .. 4 {
for r in 0 .. 4 {
mut prod := f32(0)
for c in 0..4 {
prod += unsafe {a.data[c*4+r] * b.data[i*4+c]}
for c in 0 .. 4 {
prod += unsafe {a.data[c * 4 + r] * b.data[i * 4 + c]}
}
unsafe {
out[i*4+r] = prod
out[i * 4 + r] = prod
}
}
}
@ -246,48 +263,44 @@ pub fn rotate(angle f32, axis Vec3, src Mat4) Mat4 {
c := f32(math.cos(angle))
s := f32(math.sin(angle))
oneminusc := f32(1.0) - c
xy := axis.x * axis.y
yz := axis.y * axis.z
xz := axis.x * axis.z
xs := axis.x * s
ys := axis.y * s
zs := axis.z * s
f00 := axis.x * axis.x * oneminusc + c
f01 := xy * oneminusc + zs
f02 := xz * oneminusc - ys
f10 := xy * oneminusc-zs
f10 := xy * oneminusc - zs
f11 := axis.y * axis.y * oneminusc + c
f12 := yz * oneminusc + xs
f20 := xz * oneminusc + ys
f21 := yz * oneminusc - xs
f22 := axis.z *axis.z * oneminusc + c
f22 := axis.z * axis.z * oneminusc + c
data := src.data
unsafe {
t00 := data[0] * f00 + data[4] * f01 + data[8] * f02
t01 := data[1] * f00 + data[5] * f01 + data[9] * f02
t02 := data[2] * f00 + data[6] * f01 + data[10] * f02
t03 := data[3] * f00 + data[7] * f01 + data[11] * f02
t10 := data[0] * f10 + data[4] * f11 + data[8] * f12
t11 := data[1] * f10 + data[5] * f11 + data[9] * f12
t12 := data[2] * f10 + data[6] * f11 + data[10] * f12
t13 := data[3] * f10 + data[7] * f11 + data[11] * f12
mut dest := src.data
dest[8] = data[0] * f20 + data[4] * f21 + data[8] * f22
dest[9] = data[1] * f20 + data[5] * f21 + data[9] * f22
dest[10] = data[2] * f20 + data[6] * f21 + data[10] * f22
dest[11] = data[3] * f20 + data[7] * f21 + data[11] * f22
dest[0] = t00 dest[1] = t01 dest[2] = t02 dest[3] = t03
dest[4] = t10 dest[5] = t11 dest[6] = t12 dest[7] = t13
dest[0] = t00
dest[1] = t01
dest[2] = t02
dest[3] = t03
dest[4] = t10
dest[5] = t11
dest[6] = t12
dest[7] = t13
return mat4(dest)
}
}
@ -357,23 +370,19 @@ pub fn identity2(mut res &f32) {
}
pub fn identity3() []f32 {
res := [f32(1.0), 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]
res := [f32(1.0), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
return res
}
// https://github.com/toji/gl-matrix/blob/1549cf21dfa14a2bc845993485343d519cf064fe/src/gl-matrix/mat4.js
fn ortho_js(left, right, bottom, top f32) &f32 {
// mynear := 1
// myfar := 1
fn ortho_js(left f32, right f32, bottom f32, top f32) &f32 {
// mynear := 1
// myfar := 1
lr := 1.0 / (left - right)
bt := 1.0 / (bottom - top)
nf := f32(1.0) / 1.0// (mynear -myfar)
nf := f32(1.0) / 1.0 // (mynear -myfar)
unsafe {
mut out := &f32( malloc (int(sizeof(f32) * 16)))
mut out := &f32(malloc(int(sizeof(f32) * 16)))
out[0] = -2.0 * lr
out[1] = 0
out[2] = 0
@ -388,18 +397,18 @@ fn ortho_js(left, right, bottom, top f32) &f32 {
out[11] = 0
out[12] = (left + right) * lr
out[13] = (top + bottom) * bt
out[14] = 1.0 * nf//(far + near) * nf;
out[14] = 1.0 * nf // (far + near) * nf;
out[15] = 1
return out
}
//f := 0.0
//return &f
// f := 0.0
// return &f
}
// fn ortho_old(a, b, c, d f32) *f32 {
// # return myglm_ortho(a,b,c,d);
// }
fn cross(a, b Vec3) Vec3 {
fn cross(a Vec3, b Vec3) Vec3 {
// # return myglm_cross(a,b);
return Vec3{}
}

View File

@ -1,40 +1,40 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// might need special case for this
// import gl
import glm
fn cmp(a, b f32) bool {
fn cmp(a f32, b f32) bool {
return int(a * 1000) == int(b * 1000)
}
fn test_ortho() {
projection := glm.ortho(0, 200, 400, 0)
$if debug {
println(projection.data[0])
println(unsafe {projection.data[0]})
}
unsafe {
assert cmp(projection.data[0], 0.01)
assert cmp(projection.data[1], 0.000000)
assert cmp(projection.data[2], 0.000000)
assert cmp(projection.data[3], 0.000000)
assert cmp(projection.data[4], 0.000000)
assert cmp(projection.data[5], -0.005000)
assert cmp(projection.data[6], 0.000000)
assert cmp(projection.data[7], 0.000000)
assert cmp(projection.data[8], 0.000000)
assert cmp(projection.data[9], 0.000000)
assert cmp(projection.data[10], 1.000000)
assert cmp(projection.data[11], 0.000000)
assert cmp(projection.data[12], -1.000000)
assert cmp(projection.data[13], 1.000000)
assert cmp(projection.data[14], 0.000000)
assert cmp(projection.data[15], 1.000000)
}
assert cmp(projection.data[0], 0.01)
assert cmp(projection.data[1], 0.000000)
assert cmp(projection.data[2], 0.000000)
assert cmp(projection.data[3], 0.000000)
assert cmp(projection.data[4], 0.000000)
assert cmp(projection.data[5], - 0.005000)
assert cmp(projection.data[6], 0.000000)
assert cmp(projection.data[7], 0.000000)
assert cmp(projection.data[8], 0.000000)
assert cmp(projection.data[9], 0.000000)
assert cmp(projection.data[10], 1.000000)
assert cmp(projection.data[11], 0.000000)
assert cmp(projection.data[12], - 1.000000)
assert cmp(projection.data[13], 1.000000)
assert cmp(projection.data[14], 0.000000)
assert cmp(projection.data[15], 1.000000)
// f := gg.ortho(1,2,3,4)
/*
// for debugging broken tetris in gg.o
/*
// for debugging broken tetris in gg.o
# projection.data[0]=0.010000;
# projection.data[1]=0.000000;
# projection.data[2]=0.000000;
@ -51,7 +51,7 @@ fn test_ortho() {
# projection.data[13]=1.000000;
# projection.data[14]=0.000000;
# projection.data[15]=1.000000;
*/
*/
}
fn test_rotate() {
@ -67,16 +67,13 @@ fn test_rotate() {
$if debug {
println(m)
}
mut m1 := glm.identity()
mut m2 := glm.identity()
m1 = glm.rotate(1, glm.vec3(1, 0, 0), m1)
m2 = glm.rotate(1, glm.vec3(0, 1, 0), m2)
mut same := true
for i in 0..15 {
if m1.data[i] != m2.data[i] {
for i in 0 .. 15 {
if unsafe {m1.data[i]} != unsafe {m2.data[i]} {
same = false
}
}
@ -85,29 +82,30 @@ fn test_rotate() {
fn test_translate() {
mut m := glm.identity()
m = glm.translate(m, glm.vec3(0, 0, - 0.5))
m = glm.translate(m, glm.vec3(0, 0, -0.5))
$if debug {
println(m)
}
assert m.data[0] == 1.0
assert m.data[1] == 0.0
assert m.data[2] == 0.0
assert m.data[3] == 0.0
//
assert m.data[4] == 0.0
assert m.data[5] == 1.0
assert m.data[6] == 0.0
assert m.data[7] == 0.0
assert m.data[8] == 0.0
assert m.data[9] == 0.0
assert m.data[10] == 1.0
assert m.data[11] == 0.0
//
assert m.data[12] == 0.0
assert m.data[13] == 0.0
assert m.data[14] == -0.5
assert m.data[15] == 1.0
unsafe {
assert m.data[0] == 1.0
assert m.data[1] == 0.0
assert m.data[2] == 0.0
assert m.data[3] == 0.0
//
assert m.data[4] == 0.0
assert m.data[5] == 1.0
assert m.data[6] == 0.0
assert m.data[7] == 0.0
assert m.data[8] == 0.0
assert m.data[9] == 0.0
assert m.data[10] == 1.0
assert m.data[11] == 0.0
//
assert m.data[12] == 0.0
assert m.data[13] == 0.0
assert m.data[14] == -0.5
assert m.data[15] == 1.0
}
}
fn f32_calloc(n int) &f32 {
@ -116,42 +114,42 @@ fn f32_calloc(n int) &f32 {
fn test_mult1() {
mut adata := f32_calloc(16)
adata[1*4+1] = 6
adata[2*4+3] = 2
adata[0*4+2] = 3
adata[2*4+1] = 1
unsafe {
adata[1 * 4 + 1] = 6
adata[2 * 4 + 3] = 2
adata[0 * 4 + 2] = 3
adata[2 * 4 + 1] = 1
}
mut bdata := f32_calloc(16)
bdata[1*4+1] = -2
bdata[2*4+3] = 1
bdata[0*4+2] = 6
bdata[2*4+1] = -3
unsafe {
bdata[1 * 4 + 1] = -2
bdata[2 * 4 + 3] = 1
bdata[0 * 4 + 2] = 6
bdata[2 * 4 + 1] = -3
}
mut expected := f32_calloc(16)
expected[0*4+0] = 0 /* 0*0+0*0+0*6+0*0 */
expected[0*4+1] = 6 /* 0*0+0*6+1*6+0*0 */
expected[0*4+2] = 0 /* 3*0+0*0+0*6+0*0 */
expected[0*4+3] = 12 /* 0*0+0*0+2*6+0*0 */
expected[1*4+0] = 0 /* 0*0+0*-2+0*0+0*0 */
expected[1*4+1] = -12 /* 0*0­+6*-2+1*0­+0*0 */
expected[1*4+2] = 0 /* 3*0­+0*-2­+0*0­+0*0 */
expected[1*4+3] = 0 /* 0*0­+0*-2­+2*0­+0*0 */
expected[2*4+0] = 0 /* 0*0­+0*-3­+0*0­+0*1 */
expected[2*4+1] = -18 /* 0*0­+6*-3­+1*0­+0*1 */
expected[2*4+2] = 0 /* 3*0­+0*-3+0*0­+0*1 */
expected[2*4+3] = 0 /* 0*0­+0*-3­+2*0­+0*1 */
expected[3*4+0] = 0 /* 0*0­+0*0­+0*0­+0*0 */
expected[3*4+1] = 0 /* 0*0­+6*0­+1*0­+0*0 */
expected[3*4+2] = 0 /* 3*0­+0*0­+0*0­+0*0 */
expected[3*4+3] = 0 /* 0*0­+0*0­+2*0­+0*0 */
unsafe {
expected[0 * 4 + 0] = 0 // 0*0+0*0+0*6+0*0
expected[0 * 4 + 1] = 6 // 0*0+0*6+1*6+0*0
expected[0 * 4 + 2] = 0 // 3*0+0*0+0*6+0*0
expected[0 * 4 + 3] = 12 // 0*0+0*0+2*6+0*0
expected[1 * 4 + 0] = 0 // 0*0+0*-2+0*0+0*0
expected[1 * 4 + 1] = -12 // 0*0­+6*-2+1*0­+0*0
expected[1 * 4 + 2] = 0 // 3*0­+0*-2­+0*0­+0*0
expected[1 * 4 + 3] = 0 // 0*0­+0*-2­+2*0­+0*0
expected[2 * 4 + 0] = 0 // 0*0­+0*-3­+0*0­+0*1
expected[2 * 4 + 1] = -18 // 0*0­+6*-3­+1*0­+0*1
expected[2 * 4 + 2] = 0 // 3*0­+0*-3+0*0­+0*1
expected[2 * 4 + 3] = 0 // 0*0­+0*-3­+2*0­+0*1
expected[3 * 4 + 0] = 0 // 0*0­+0*0­+0*0­+0*0
expected[3 * 4 + 1] = 0 // 0*0­+6*0­+1*0­+0*0
expected[3 * 4 + 2] = 0 // 3*0­+0*0­+0*0­+0*0
expected[3 * 4 + 3] = 0 // 0*0­+0*0­+2*0­+0*0
}
mut a := glm.Mat4{adata}
b := glm.Mat4{bdata}
a = glm.mult(a, b)
for i in 0..15 {
assert a.data[i] == expected[i]
for i in 0 .. 15 {
assert unsafe {a.data[i]} == unsafe {expected[i]}
}
}

View File

@ -77,7 +77,7 @@ pub fn approximate(val f64) Fraction {
}
// approximate_with_eps returns a Fraction
pub fn approximate_with_eps(val, eps f64) Fraction {
pub fn approximate_with_eps(val f64, eps f64) Fraction {
if val == 0.0 {
return zero
}

View File

@ -25,7 +25,7 @@ pub:
// to ensure that the denominator is non-zero. It automatically converts
// the negative denominator to positive and adjusts the numerator.
// NOTE: Fractions created are not reduced by default.
pub fn fraction(n, d i64) Fraction {
pub fn fraction(n i64, d i64) Fraction {
if d == 0 {
panic('Denominator cannot be zero')
}
@ -54,7 +54,7 @@ pub fn (f Fraction) str() string {
//
// Returns a correctly reduced result for both addition and subtraction
// NOTE: requires reduced inputs
fn general_addition_result(f1, f2 Fraction, addition bool) Fraction {
fn general_addition_result(f1 Fraction, f2 Fraction, addition bool) Fraction {
d1 := math.gcd(f1.d, f2.d)
// d1 happends to be 1 around 600/(pi)^2 or 61 percent of the time (Theorem 4.5.2D)
if d1 == 1 {
@ -93,7 +93,7 @@ pub fn (f1 Fraction) -(f2 Fraction) Fraction {
// Returns a correctly reduced result for both multiplication and division
// NOTE: requires reduced inputs
fn general_multiplication_result(f1, f2 Fraction, multiplication bool) Fraction {
fn general_multiplication_result(f1 Fraction, f2 Fraction, multiplication bool) Fraction {
// * Theorem: If f1 and f2 are reduced i.e. gcd(f1.n, f1.d) == 1 and gcd(f2.n, f2.d) == 1,
// then gcd(f1.n * f2.n, f1.d * f2.d) == gcd(f1.n, f2.d) * gcd(f1.d, f2.n)
// * Knuth poses this an exercise for 4.5.1. - Exercise 2
@ -216,7 +216,7 @@ fn abs(num i64) i64 {
}
}
fn cmp_i64s(a, b i64) int {
fn cmp_i64s(a i64, b i64) int {
if a == b {
return 0
} else if a > b {
@ -226,7 +226,7 @@ fn cmp_i64s(a, b i64) int {
}
}
fn cmp_f64s(a, b f64) int {
fn cmp_f64s(a f64, b f64) int {
// V uses epsilon comparison internally
if a == b {
return 0
@ -239,11 +239,11 @@ fn cmp_f64s(a, b f64) int {
// Two integers are safe to multiply when their bit lengths
// sum up to less than 64 (conservative estimate).
fn safe_to_multiply(a, b i64) bool {
fn safe_to_multiply(a i64, b i64) bool {
return (bits.len_64(u64(abs(a))) + bits.len_64(u64(abs(b)))) < 64
}
fn cmp(f1, f2 Fraction) int {
fn cmp(f1 Fraction, f2 Fraction) int {
if safe_to_multiply(f1.n, f2.d) && safe_to_multiply(f2.n, f1.d) {
return cmp_i64s(f1.n * f2.d, f2.n * f1.d)
} else {

View File

@ -48,18 +48,15 @@ fn (dtp DTP) read() []byte {
break
}
for i in 0 .. len {
data << unsafe { buf[i] }
}
unsafe {
free(buf)
data << unsafe {buf[i]}
}
unsafe {free(buf)}
}
return data
}
fn (dtp DTP) close() {
dtp.sock.close() or {
}
dtp.sock.close() or { }
}
struct FTP {
@ -116,7 +113,7 @@ pub fn (mut ftp FTP) connect(ip string) bool {
return false
}
pub fn (ftp FTP) login(user, passwd string) bool {
pub fn (ftp FTP) login(user string, passwd string) bool {
ftp.write('USER $user') or {
$if debug {
println('ERROR sending user')
@ -145,10 +142,8 @@ pub fn (ftp FTP) login(user, passwd string) bool {
pub fn (ftp FTP) close() {
send_quit := 'QUIT\r\n'
ftp.sock.send_string(send_quit) or {
}
ftp.sock.close() or {
}
ftp.sock.send_string(send_quit) or { }
ftp.sock.close() or { }
}
pub fn (ftp FTP) pwd() string {
@ -201,8 +196,7 @@ fn new_dtp(msg string) ?DTP {
}
fn (ftp FTP) pasv() ?DTP {
ftp.write('PASV') or {
}
ftp.write('PASV') or { }
code, data := ftp.read()
$if debug {
println('pass: $data')
@ -210,7 +204,7 @@ fn (ftp FTP) pasv() ?DTP {
if code != passive_mode {
return error('pasive mode not allowed')
}
dtp := new_dtp(data)?
dtp := new_dtp(data) ?
return dtp
}
@ -218,8 +212,7 @@ pub fn (ftp FTP) dir() ?[]string {
dtp := ftp.pasv() or {
return error('cannot establish data connection')
}
ftp.write('LIST') or {
}
ftp.write('LIST') or { }
code, _ := ftp.read()
if code == denied {
return error('LIST denied')
@ -248,8 +241,7 @@ pub fn (ftp FTP) get(file string) ?[]byte {
dtp := ftp.pasv() or {
return error('Cannot stablish data connection')
}
ftp.write('RETR $file') or {
}
ftp.write('RETR $file') or { }
code, _ := ftp.read()
if code == denied {
return error('Permission denied')

View File

@ -39,7 +39,7 @@ fn is_close_tag(tag &Tag) bool {
return false
}
fn (mut dom DocumentObjectModel) where_is(item_name, attribute_name string) int {
fn (mut dom DocumentObjectModel) where_is(item_name string, attribute_name string) int {
if !(attribute_name in dom.attributes) {
temp_array := []string{}
dom.attributes[attribute_name] = temp_array
@ -101,7 +101,7 @@ fn (mut dom DocumentObjectModel) add_tag_by_attribute(tag &Tag) {
}
}
fn compare_string(a, b string) bool { // for some reason == doesn't work
fn compare_string(a string, b string) bool { // for some reason == doesn't work
if a.len != b.len {
return false
}
@ -113,7 +113,7 @@ fn compare_string(a, b string) bool { // for some reason == doesn't work
return true
}
fn (mut dom DocumentObjectModel) construct(tag_list []Tag_ptr) {
fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.constructed = true
mut temp_map := map[string]int{}
mut temp_int := C.INT_MIN
@ -179,7 +179,7 @@ fn (mut dom DocumentObjectModel) construct(tag_list []Tag_ptr) {
dom.root = tag_list[0]
}
pub fn (mut dom DocumentObjectModel) get_by_attribute_value(name, value string) []Tag_ptr {
pub fn (mut dom DocumentObjectModel) get_by_attribute_value(name string, value string) []&Tag {
location := dom.where_is(value, name)
if dom.tag_attributes[name].len > location {
return dom.tag_attributes[name][location]
@ -187,14 +187,14 @@ pub fn (mut dom DocumentObjectModel) get_by_attribute_value(name, value string)
return []&Tag{}
}
pub fn (dom DocumentObjectModel) get_by_tag(name string) []Tag_ptr {
pub fn (dom DocumentObjectModel) get_by_tag(name string) []&Tag {
if name in dom.tag_type {
return dom.tag_type[name]
}
return []&Tag{}
}
pub fn (dom DocumentObjectModel) get_by_attribute(name string) []Tag_ptr {
pub fn (dom DocumentObjectModel) get_by_attribute(name string) []&Tag {
if name in dom.all_attributes {
return dom.all_attributes[name]
}
@ -205,12 +205,14 @@ pub fn (dom DocumentObjectModel) get_root() &Tag {
return dom.root
}
pub fn (dom DocumentObjectModel) get_all_tags() []Tag_ptr {
pub fn (dom DocumentObjectModel) get_all_tags() []&Tag {
return dom.all_tags
}
/*pub fn (dom DocumentObjectModel) get_xpath() XPath {
/*
pub fn (dom DocumentObjectModel) get_xpath() XPath {
return XPath{
dom: dom
}
}*/
}
*/

View File

@ -11,19 +11,16 @@ import picohttpparser
#include <netinet/tcp.h>
#include <fcntl.h>
#include <signal.h>
#flag -I @VROOT/thirdparty/picoev
#flag -L @VROOT/thirdparty/picoev
#flag @VROOT/thirdparty/picoev/picoev.o
#include "src/picoev.h"
const (
max_fds = 1024
max_fds = 1024
timeout_secs = 8
max_timeout = 10
max_read = 4096
max_write = 8192
max_timeout = 10
max_read = 4096
max_write = 8192
)
struct C.in_addr {
@ -38,31 +35,48 @@ mut:
sin_addr C.in_addr
}
struct C.sockaddr_storage {}
struct C.sockaddr_storage {
}
fn C.atoi() int
fn C.strncasecmp() int
fn C.socket() int
fn C.setsockopt() int
fn C.htonl() int
fn C.htons() int
fn C.bind() int
fn C.listen() int
fn C.accept() int
fn C.getaddrinfo() int
fn C.connect() int
fn C.send() int
fn C.recv() int
//fn C.read() int
// fn C.read() int
fn C.shutdown() int
//fn C.close() int
// fn C.close() int
fn C.ntohs() int
fn C.getsockname() int
fn C.fcntl() int
//fn C.write() int
struct C.picoev_loop {}
// fn C.write() int
struct C.picoev_loop {
}
struct Picoev {
loop &C.picoev_loop
@ -76,15 +90,25 @@ mut:
}
fn C.picoev_del(&C.picoev_loop, int) int
fn C.picoev_set_timeout(&C.picoev_loop, int, int)
fn C.picoev_add(&C.picoev_loop, int, int, int, &C.picoev_handler, voidptr) int
fn C.picoev_init(int) int
fn C.picoev_create_loop(int) &C.picoev_loop
fn C.picoev_loop_once(&C.picoev_loop, int) int
fn C.picoev_destroy_loop(&C.picoev_loop) int
fn C.picoev_deinit() int
fn C.phr_parse_request() int
fn C.phr_parse_request_path_pipeline() int
fn C.phr_parse_request_path() int
[inline]
@ -105,27 +129,26 @@ fn close_conn(loop &C.picoev_loop, fd int) {
}
[inline]
fn myread(fd int, b byteptr, max_len, idx int) int {
fn myread(fd int, b byteptr, max_len int, idx int) int {
unsafe {
return C.read(fd, b + idx, max_len - idx)
}
}
[inline]
fn mysubstr(s byteptr, from, len int) string {
fn mysubstr(s byteptr, from int, len int) string {
unsafe {
return tos(s + from, len)
}
}
fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
fn rw_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
mut p := &Picoev(cb_arg)
if (events & C.PICOEV_TIMEOUT) != 0 {
close_conn(loop, fd)
p.idx[fd] = 0
return
}
else if (events & C.PICOEV_READ) != 0 {
} else if (events & C.PICOEV_READ) != 0 {
C.picoev_set_timeout(loop, fd, timeout_secs)
mut buf := p.buf
unsafe {
@ -138,7 +161,7 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
p.idx[fd] = 0
return
} else if r == -1 {
if false { //errno == C.EAGAIN || errno == C.EWOULDBLOCK {
if false { // errno == C.EAGAIN || errno == C.EWOULDBLOCK {
// TODO
} else {
close_conn(loop, fd)
@ -165,24 +188,25 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
for {
pret := req.parse_request(s, 100)
if pret <= 0 && s.len > 0 {
unsafe { C.memmove(buf, s.str, s.len) }
unsafe {C.memmove(buf, s.str, s.len)}
p.idx[fd] = s.len
p.oidx[fd] = int(res.buf) - int(res.buf_start)
break
}
c0 := unsafe { req.method.str[0] }
if c0 ==`p` || c0 == `P` || c0 == `d` || c0 == `D` {
c0 := unsafe {req.method.str[0]}
if c0 == `p` || c0 == `P` || c0 == `d` || c0 == `D` {
mut j := 0
for {
if j == req.num_headers {
break
}
if req.headers[j].name_len == 14 && C.strncasecmp(req.headers[j].name, "content-length", 14) == 0 {
//cont_length := C.atoi(tos(req.headers[j].value, req.headers[j].value_len).str)
//println('$cont_length')
//TODO need to maintain state of incomplete request to collect body later
if req.headers[j].name_len == 14 &&
C.strncasecmp(req.headers[j].name, 'content-length', 14) == 0 {
// cont_length := C.atoi(tos(req.headers[j].value, req.headers[j].value_len).str)
// println('$cont_length')
// TODO need to maintain state of incomplete request to collect body later
}
j = j+1
j = j + 1
}
}
p.cb(req, mut &res)
@ -201,7 +225,7 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
}
}
fn accept_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
fn accept_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
newfd := C.accept(fd, 0, 0)
if newfd != -1 {
setup_sock(newfd)
@ -212,7 +236,6 @@ fn accept_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
pub fn new(port int, cb voidptr) &Picoev {
fd := C.socket(C.AF_INET, C.SOCK_STREAM, 0)
assert fd != -1
flag := 1
assert C.setsockopt(fd, C.SOL_SOCKET, C.SO_REUSEADDR, &flag, sizeof(int)) == 0
assert C.setsockopt(fd, C.SOL_SOCKET, C.SO_REUSEPORT, &flag, sizeof(int)) == 0
@ -223,7 +246,6 @@ pub fn new(port int, cb voidptr) &Picoev {
queue_len := 4096
assert C.setsockopt(fd, C.IPPROTO_TCP, C.TCP_FASTOPEN, &queue_len, sizeof(int)) == 0
}
mut addr := C.sockaddr_in{}
addr.sin_family = C.AF_INET
addr.sin_port = C.htons(port)
@ -231,12 +253,9 @@ pub fn new(port int, cb voidptr) &Picoev {
size := 16 // sizeof(C.sockaddr_in)
bind_res := C.bind(fd, &addr, size)
assert bind_res == 0
listen_res := C.listen(fd, C.SOMAXCONN)
assert listen_res == 0
setup_sock(fd)
C.picoev_init(max_fds)
loop := C.picoev_create_loop(max_timeout)
mut pv := &Picoev{
@ -247,9 +266,7 @@ pub fn new(port int, cb voidptr) &Picoev {
out: malloc(max_fds * max_write + 1)
}
C.picoev_add(loop, fd, C.PICOEV_READ, 0, accept_callback, pv)
go update_date(mut pv)
return pv
}

View File

@ -1,18 +1,21 @@
module picohttpparser
[inline] [unsafe]
fn cpy(dst, src byteptr, len int) int {
unsafe { C.memcpy(dst, src, len) }
[inline]
[unsafe]
fn cpy(dst byteptr, src byteptr, len int) int {
unsafe {C.memcpy(dst, src, len)}
return len
}
[inline]
pub fn cmp(dst, src string) bool {
if dst.len != src.len { return false }
return unsafe { C.memcmp(dst.str, src.str, src.len) == 0 }
pub fn cmp(dst string, src string) bool {
if dst.len != src.len {
return false
}
return unsafe {C.memcmp(dst.str, src.str, src.len) == 0}
}
[inline]
pub fn cmpn(dst, src string, n int) bool {
return unsafe { C.memcmp(dst.str, src.str, n) == 0 }
pub fn cmpn(dst string, src string, n int) bool {
return unsafe {C.memcmp(dst.str, src.str, n) == 0}
}

View File

@ -1,12 +1,12 @@
module picohttpparser
pub struct Response {
fd int
fd int
pub:
date byteptr
date byteptr
buf_start byteptr
pub mut:
buf byteptr
buf byteptr
}
[inline]
@ -19,68 +19,68 @@ fn (mut r Response) write_str(s string) {
[inline]
pub fn (mut r Response) http_ok() &Response {
r.write_str("HTTP/1.1 200 OK\r\n")
r.write_str('HTTP/1.1 200 OK\r\n')
return r
}
[inline]
pub fn (mut r Response) header(k, v string) &Response {
pub fn (mut r Response) header(k string, v string) &Response {
r.write_str(k)
r.write_str(": ")
r.write_str(': ')
r.write_str(v)
r.write_str("\r\n")
r.write_str('\r\n')
return r
}
[inline]
pub fn (mut r Response) header_date() &Response {
r.write_str("Date: ")
r.write_str('Date: ')
unsafe {
r.buf += cpy(r.buf, r.date, 29)
}
r.write_str("\r\n")
r.write_str('\r\n')
return r
}
[inline]
pub fn (mut r Response) header_server() &Response {
r.write_str("Server: V\r\n")
r.write_str('Server: V\r\n')
return r
}
[inline]
pub fn (mut r Response) content_type(s string) &Response {
r.write_str("Content-Type: ")
r.write_str('Content-Type: ')
r.write_str(s)
r.write_str("\r\n")
r.write_str('\r\n')
return r
}
[inline]
pub fn (mut r Response) html() &Response {
r.write_str("Content-Type: text/html\r\n")
r.write_str('Content-Type: text/html\r\n')
return r
}
[inline]
pub fn (mut r Response) plain() &Response {
r.write_str("Content-Type: text/plain\r\n")
r.write_str('Content-Type: text/plain\r\n')
return r
}
[inline]
pub fn (mut r Response) json() &Response {
r.write_str("Content-Type: application/json\r\n")
r.write_str('Content-Type: application/json\r\n')
return r
}
[inline]
pub fn (mut r Response) body(body string) {
r.write_str("Content-Length: ")
r.write_str('Content-Length: ')
unsafe {
r.buf += C.u64toa(r.buf, body.len)
}
r.write_str("\r\n\r\n")
r.write_str('\r\n\r\n')
r.write_str(body)
}

View File

@ -210,7 +210,7 @@ pub fn (mut rng MT19937RNG) u64n(max u64) u64 {
// rng.u32n(min, max) returns a pseudorandom u32 value that is guaranteed to be in [min, max)
[inline]
pub fn (mut rng MT19937RNG) u32_in_range(min, max u32) u32 {
pub fn (mut rng MT19937RNG) u32_in_range(min u32, max u32) u32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -220,7 +220,7 @@ pub fn (mut rng MT19937RNG) u32_in_range(min, max u32) u32 {
// rng.u64n(min, max) returns a pseudorandom u64 value that is guaranteed to be in [min, max)
[inline]
pub fn (mut rng MT19937RNG) u64_in_range(min, max u64) u64 {
pub fn (mut rng MT19937RNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -250,7 +250,7 @@ pub fn (mut rng MT19937RNG) i64n(max i64) i64 {
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
[inline]
pub fn (mut rng MT19937RNG) int_in_range(min, max int) int {
pub fn (mut rng MT19937RNG) int_in_range(min int, max int) int {
if max <= min {
eprintln('max must be greater than min.')
exit(1)
@ -260,7 +260,7 @@ pub fn (mut rng MT19937RNG) int_in_range(min, max int) int {
// rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
[inline]
pub fn (mut rng MT19937RNG) i64_in_range(min, max i64) i64 {
pub fn (mut rng MT19937RNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
eprintln('max must be greater than min.')
exit(1)
@ -302,7 +302,7 @@ pub fn (mut rng MT19937RNG) f64n(max f64) f64 {
// rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline]
pub fn (mut rng MT19937RNG) f32_in_range(min, max f32) f32 {
pub fn (mut rng MT19937RNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -312,7 +312,7 @@ pub fn (mut rng MT19937RNG) f32_in_range(min, max f32) f32 {
// rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (mut rng MT19937RNG) f64_in_range(min, max f64) f64 {
pub fn (mut rng MT19937RNG) f64_in_range(min f64, max f64) f64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)

View File

@ -102,7 +102,7 @@ pub fn (mut rng MuslRNG) u64n(max u64) u64 {
// rn.u32_in_range(min, max) - return a pseudorandom 32 bit unsigned u32 in [min, max)
[inline]
pub fn (mut rng MuslRNG) u32_in_range(min, max u64) u64 {
pub fn (mut rng MuslRNG) u32_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -112,7 +112,7 @@ pub fn (mut rng MuslRNG) u32_in_range(min, max u64) u64 {
// rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
[inline]
pub fn (mut rng MuslRNG) u64_in_range(min, max u64) u64 {
pub fn (mut rng MuslRNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -166,7 +166,7 @@ pub fn (mut rng MuslRNG) i64n(max i64) i64 {
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
[inline]
pub fn (mut rng MuslRNG) int_in_range(min, max int) int {
pub fn (mut rng MuslRNG) int_in_range(min int, max int) int {
if max <= min {
eprintln('max must be greater than min.')
exit(1)
@ -176,7 +176,7 @@ pub fn (mut rng MuslRNG) int_in_range(min, max int) int {
// rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
[inline]
pub fn (mut rng MuslRNG) i64_in_range(min, max i64) i64 {
pub fn (mut rng MuslRNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
eprintln('max must be greater than min.')
exit(1)
@ -218,7 +218,7 @@ pub fn (mut rng MuslRNG) f64n(max f64) f64 {
// rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline]
pub fn (mut rng MuslRNG) f32_in_range(min, max f32) f32 {
pub fn (mut rng MuslRNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -228,7 +228,7 @@ pub fn (mut rng MuslRNG) f32_in_range(min, max f32) f32 {
// rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (mut rng MuslRNG) f64_in_range(min, max f64) f64 {
pub fn (mut rng MuslRNG) f64_in_range(min f64, max f64) f64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)

View File

@ -90,7 +90,7 @@ pub fn (mut rng PCG32RNG) u64n(max u64) u64 {
// rn.u32_in_range(min, max) - return a pseudorandom 32 bit unsigned u32 in [min, max)
[inline]
pub fn (mut rng PCG32RNG) u32_in_range(min, max u64) u64 {
pub fn (mut rng PCG32RNG) u32_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -100,7 +100,7 @@ pub fn (mut rng PCG32RNG) u32_in_range(min, max u64) u64 {
// rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
[inline]
pub fn (mut rng PCG32RNG) u64_in_range(min, max u64) u64 {
pub fn (mut rng PCG32RNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -154,7 +154,7 @@ pub fn (mut rng PCG32RNG) i64n(max i64) i64 {
// rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
[inline]
pub fn (mut rng PCG32RNG) int_in_range(min, max int) int {
pub fn (mut rng PCG32RNG) int_in_range(min int, max int) int {
if max <= min {
eprintln('max must be greater than min.')
exit(1)
@ -164,7 +164,7 @@ pub fn (mut rng PCG32RNG) int_in_range(min, max int) int {
// rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
[inline]
pub fn (mut rng PCG32RNG) i64_in_range(min, max i64) i64 {
pub fn (mut rng PCG32RNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
eprintln('max must be greater than min.')
exit(1)
@ -206,7 +206,7 @@ pub fn (mut rng PCG32RNG) f64n(max f64) f64 {
// rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline]
pub fn (mut rng PCG32RNG) f32_in_range(min, max f32) f32 {
pub fn (mut rng PCG32RNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -216,7 +216,7 @@ pub fn (mut rng PCG32RNG) f32_in_range(min, max f32) f32 {
// rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (mut rng PCG32RNG) f64_in_range(min, max f64) f64 {
pub fn (mut rng PCG32RNG) f64_in_range(min f64, max f64) f64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)

View File

@ -88,7 +88,7 @@ pub fn (mut rng SplitMix64RNG) u64n(bound u64) u64 {
// rng.u32n(min, max) returns a pseudorandom u32 value that is guaranteed to be in [min, max)
[inline]
pub fn (mut rng SplitMix64RNG) u32_in_range(min, max u32) u32 {
pub fn (mut rng SplitMix64RNG) u32_in_range(min u32, max u32) u32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -98,7 +98,7 @@ pub fn (mut rng SplitMix64RNG) u32_in_range(min, max u32) u32 {
// rng.u64n(min, max) returns a pseudorandom u64 value that is guaranteed to be in [min, max)
[inline]
pub fn (mut rng SplitMix64RNG) u64_in_range(min, max u64) u64 {
pub fn (mut rng SplitMix64RNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -152,7 +152,7 @@ pub fn (mut rng SplitMix64RNG) i64n(max i64) i64 {
// rng.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)
[inline]
pub fn (mut rng SplitMix64RNG) int_in_range(min, max int) int {
pub fn (mut rng SplitMix64RNG) int_in_range(min int, max int) int {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -163,7 +163,7 @@ pub fn (mut rng SplitMix64RNG) int_in_range(min, max int) int {
// rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (mut rng SplitMix64RNG) i64_in_range(min, max i64) i64 {
pub fn (mut rng SplitMix64RNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -205,7 +205,7 @@ pub fn (mut rng SplitMix64RNG) f64n(max f64) f64 {
// rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline]
pub fn (mut rng SplitMix64RNG) f32_in_range(min, max f32) f32 {
pub fn (mut rng SplitMix64RNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -215,7 +215,7 @@ pub fn (mut rng SplitMix64RNG) f32_in_range(min, max f32) f32 {
// rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (mut rng SplitMix64RNG) f64_in_range(min, max f64) f64 {
pub fn (mut rng SplitMix64RNG) f64_in_range(min f64, max f64) f64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)

View File

@ -46,7 +46,7 @@ pub fn (mut r SysRNG) seed(seed_data []u32) {
exit(1)
}
r.seed = seed_data[0]
unsafe { C.srand(int(r.seed)) }
unsafe {C.srand(int(r.seed))}
}
// r.default_rand() exposes the default behavior of the system's RNG
@ -142,7 +142,7 @@ pub fn (r SysRNG) u64n(max u64) u64 {
// r.u32n(min, max) returns a pseudorandom u32 value that is guaranteed to be in [min, max)
[inline]
pub fn (r SysRNG) u32_in_range(min, max u32) u32 {
pub fn (r SysRNG) u32_in_range(min u32, max u32) u32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -152,7 +152,7 @@ pub fn (r SysRNG) u32_in_range(min, max u32) u32 {
// r.u64n(min, max) returns a pseudorandom u64 value that is guaranteed to be in [min, max)
[inline]
pub fn (r SysRNG) u64_in_range(min, max u64) u64 {
pub fn (r SysRNG) u64_in_range(min u64, max u64) u64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -206,7 +206,7 @@ pub fn (r SysRNG) i64n(max i64) i64 {
// r.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)
[inline]
pub fn (r SysRNG) int_in_range(min, max int) int {
pub fn (r SysRNG) int_in_range(min int, max int) int {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -217,7 +217,7 @@ pub fn (r SysRNG) int_in_range(min, max int) int {
// r.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (r SysRNG) i64_in_range(min, max i64) i64 {
pub fn (r SysRNG) i64_in_range(min i64, max i64) i64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -259,7 +259,7 @@ pub fn (r SysRNG) f64n(max f64) f64 {
// r.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline]
pub fn (r SysRNG) f32_in_range(min, max f32) f32 {
pub fn (r SysRNG) f32_in_range(min f32, max f32) f32 {
if max <= min {
eprintln('max must be greater than min')
exit(1)
@ -269,7 +269,7 @@ pub fn (r SysRNG) f32_in_range(min, max f32) f32 {
// r.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline]
pub fn (r SysRNG) f64_in_range(min, max f64) f64 {
pub fn (r SysRNG) f64_in_range(min f64, max f64) f64 {
if max <= min {
eprintln('max must be greater than min')
exit(1)

View File

@ -6,11 +6,10 @@ module audio
#flag linux -lasound
#flag darwin -framework AudioToolbox
#flag windows -lole32
//
pub type FNStreamingCB = fn (buffer &f32, num_frames, num_channels int)
pub type FNStreamingCB = fn (buffer &f32, num_frames int, num_channels int)
pub type FnStreamingCBWithUserData = fn (buffer &f32, num_frames, num_channels int, user_data voidptr)
pub type FnStreamingCBWithUserData = fn (buffer &f32, num_frames int, num_channels int, user_data voidptr)
pub fn (x FNStreamingCB) str() string {
return '&FNStreamingCB{ ${ptr_str(x)} }'
@ -104,7 +103,7 @@ pub fn push(frames &f32, num_frames int) int {
//
[inline]
pub fn fclamp(x, flo, fhi f32) f32 {
pub fn fclamp(x f32, flo f32, fhi f32) f32 {
if x > fhi {
return fhi
}
@ -114,14 +113,14 @@ pub fn fclamp(x, flo, fhi f32) f32 {
return x
}
pub fn min(x, y int) int {
pub fn min(x int, y int) int {
if x < y {
return x
}
return y
}
pub fn max(x, y int) int {
pub fn max(x int, y int) int {
if x < y {
return y
}

View File

@ -5,9 +5,9 @@
//
// The receive threads add all received numbers and send them to the
// main thread where the total sum is compare to the expected value.
import time
import os
import sync
fn do_rec(ch chan int, resch chan i64, n int) {
mut sum := i64(0)
@ -18,7 +18,7 @@ fn do_rec(ch chan int, resch chan i64, n int) {
resch <- sum
}
fn do_send(ch chan int, start, end int) {
fn do_send(ch chan int, start int, end int) {
for i in start .. end {
ch <- i
}
@ -56,10 +56,10 @@ fn main() {
sum += <-resch
}
elapsed := stopwatch.elapsed()
rate := f64(nobj)/elapsed*time.microsecond
println('$nobj objects in ${f64(elapsed)/time.second} s (${rate:.2f} objs/µs)')
rate := f64(nobj) / elapsed * time.microsecond
println('$nobj objects in ${f64(elapsed) / time.second} s (${rate:.2f} objs/µs)')
// use sum formula by Gauß to calculate the expected result
expected_sum := i64(nobj)*(nobj-1)/2
expected_sum := i64(nobj) * (nobj - 1) / 2
println('got: $sum, expected: $expected_sum')
assert sum == expected_sum
}

View File

@ -151,7 +151,7 @@ fn (am AssetManager) include(asset_type string, combine bool) string {
// dont return option until size limit is removed
// fn (mut am AssetManager) add(asset_type, file string) ?bool {
fn (mut am AssetManager) add(asset_type, file string) bool {
fn (mut am AssetManager) add(asset_type string, file string) bool {
if !os.exists(file) {
// return error('vweb.assets: cannot add asset $file, it does not exist')
return false
@ -172,7 +172,7 @@ fn (mut am AssetManager) add(asset_type, file string) bool {
return true
}
fn (am AssetManager) exists(asset_type, file string) bool {
fn (am AssetManager) exists(asset_type string, file string) bool {
assets := am.get_assets(asset_type)
for asset in assets {
if asset.file_path == file {