1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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.js.v',
'vlib/builtin/js/jsfns_browser.js.v', 'vlib/builtin/js/jsfns_browser.js.v',
'vlib/builtin/bare/linuxsys_bare.v', // error: expr(): bad token `asm`, on `asm {}` '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', '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 // merge two sorted arrays (ascending) and maintain sorted order
[direct_array_access] [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 m := []T{len: a.len + b.len}
mut ia := 0 mut ia := 0
mut ib := 0 mut ib := 0

View File

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

View File

@ -93,7 +93,7 @@ pub fn (flags []Flag) get_string(name string) ?string {
return flag.get_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 { value := flags.get_string(name) or {
return or_value return or_value
} }

View File

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

View File

@ -1,14 +1,12 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module cipher module cipher
// NOTE: Implement other versions (joe-c) // NOTE: Implement other versions (joe-c)
// xor_bytes xors the bytes in a and b. The destination should have enough // 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. // 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 mut n := a.len
if b.len < n { if b.len < n {
n = b.len n = b.len
@ -16,21 +14,19 @@ pub fn xor_bytes(mut dst []byte, a, b []byte) int {
if n == 0 { if n == 0 {
return 0 return 0
} }
safe_xor_bytes(mut dst, a, b, n) safe_xor_bytes(mut dst, a, b, n)
return n return n
} }
// n needs to be smaller or equal than the length of a and b. // 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) { pub fn safe_xor_bytes(mut dst []byte, a []byte, b []byte, n int) {
for i in 0..n { for i in 0 .. n {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
} }
// fast_xor_words XORs multiples of 4 or 8 bytes (depending on architecture.) // 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. // 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) 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 // 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{} mut b_key := []byte{}
if key.len <= blocksize { if key.len <= blocksize {
b_key = key.clone() // TODO: remove .clone() once https://github.com/vlang/v/issues/6604 gets fixed 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 // equal compares 2 MACs for equality, without leaking timing info
// NB: if the lengths of the 2 MACs are different, probably a completely different // 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. // 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 return subtle.constant_time_compare(mac1, mac2) == 1
} }

View File

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

View File

@ -1,25 +1,25 @@
module subtle module subtle
// constant_time_byte_eq returns 1 when x == y. // 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) return int((u32(x ^ y) - 1) >> 31)
} }
// constant_time_eq returns 1 when x == y. // 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) return int((u64(u32(x ^ y)) - 1) >> 63)
} }
// constant_time_select returns x when v == 1, and y when v == 0. // constant_time_select returns x when v == 1, and y when v == 0.
// it is undefined when v is any other value // 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) return (~(v - 1) & x) | ((v - 1) & y)
} }
// constant_time_compare returns 1 when x and y have equal contents. // 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. // The runtime of this function is proportional of the length of x and y.
// It is *NOT* dependent on their content. // 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 { if x.len != y.len {
return 0 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. // 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) // 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) x32 := int(x)
y32 := int(y) y32 := int(y)
return int(((x32 - y32 - 1) >> 31) & 1) return int(((x32 - y32 - 1) >> 31) & 1)

View File

@ -1,16 +1,13 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's // Package rc4 implements RC4 encryption, as defined in Bruce Schneier's
// Applied Cryptography. // Applied Cryptography.
// //
// RC4 is cryptographically broken and should not be used for secure // RC4 is cryptographically broken and should not be used for secure
// applications. // applications.
// Based off: https://github.com/golang/go/blob/master/src/crypto/rc4 // Based off: https://github.com/golang/go/blob/master/src/crypto/rc4
// Last commit: https://github.com/golang/go/commit/b35dacaac57b039205d9b07ea24098e2c3fcb12e // Last commit: https://github.com/golang/go/commit/b35dacaac57b039205d9b07ea24098e2c3fcb12e
module rc4 module rc4
import crypto.internal.subtle 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()) return error('crypto.rc4: invalid key size ' + key.len.str())
} }
mut c := Cipher{ 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) c.s[i] = u32(i)
} }
mut j := byte(0) mut j := byte(0)
for i in 0..256 { for i in 0 .. 256 {
j += byte(c.s[i]) + key[i%key.len] j += byte(c.s[i]) + key[i % key.len]
tmp := c.s[i] tmp := c.s[i]
c.s[i] = c.s[j] c.s[i] = c.s[j]
c.s[j] = tmp 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. // 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. // 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 { if src.len == 0 {
return return
} }
@ -75,7 +72,7 @@ pub fn (mut c Cipher) xor_key_stream(mut dst, src []byte) {
y := c.s[j] y := c.s[j]
c.s[i] = y c.s[i] = y
c.s[j] = x 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.i = i
c.j = j c.j = j

View File

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

View File

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

View File

@ -25,7 +25,7 @@ pub:
// to ensure that the denominator is non-zero. It automatically converts // to ensure that the denominator is non-zero. It automatically converts
// the negative denominator to positive and adjusts the numerator. // the negative denominator to positive and adjusts the numerator.
// NOTE: Fractions created are not reduced by default. // 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 { if d == 0 {
panic('Denominator cannot be zero') 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 // Returns a correctly reduced result for both addition and subtraction
// NOTE: requires reduced inputs // 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 := 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) // d1 happends to be 1 around 600/(pi)^2 or 61 percent of the time (Theorem 4.5.2D)
if d1 == 1 { if d1 == 1 {
@ -93,7 +93,7 @@ pub fn (f1 Fraction) -(f2 Fraction) Fraction {
// Returns a correctly reduced result for both multiplication and division // Returns a correctly reduced result for both multiplication and division
// NOTE: requires reduced inputs // 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, // * 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) // 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 // * 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 { if a == b {
return 0 return 0
} else if a > b { } 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 // V uses epsilon comparison internally
if a == b { if a == b {
return 0 return 0
@ -239,11 +239,11 @@ fn cmp_f64s(a, b f64) int {
// Two integers are safe to multiply when their bit lengths // Two integers are safe to multiply when their bit lengths
// sum up to less than 64 (conservative estimate). // 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 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) { 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) return cmp_i64s(f1.n * f2.d, f2.n * f1.d)
} else { } else {

View File

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

View File

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

View File

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

View File

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

View File

@ -1,12 +1,12 @@
module picohttpparser module picohttpparser
pub struct Response { pub struct Response {
fd int fd int
pub: pub:
date byteptr date byteptr
buf_start byteptr buf_start byteptr
pub mut: pub mut:
buf byteptr buf byteptr
} }
[inline] [inline]
@ -19,68 +19,68 @@ fn (mut r Response) write_str(s string) {
[inline] [inline]
pub fn (mut r Response) http_ok() &Response { 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 return r
} }
[inline] [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(k)
r.write_str(": ") r.write_str(': ')
r.write_str(v) r.write_str(v)
r.write_str("\r\n") r.write_str('\r\n')
return r return r
} }
[inline] [inline]
pub fn (mut r Response) header_date() &Response { pub fn (mut r Response) header_date() &Response {
r.write_str("Date: ") r.write_str('Date: ')
unsafe { unsafe {
r.buf += cpy(r.buf, r.date, 29) r.buf += cpy(r.buf, r.date, 29)
} }
r.write_str("\r\n") r.write_str('\r\n')
return r return r
} }
[inline] [inline]
pub fn (mut r Response) header_server() &Response { pub fn (mut r Response) header_server() &Response {
r.write_str("Server: V\r\n") r.write_str('Server: V\r\n')
return r return r
} }
[inline] [inline]
pub fn (mut r Response) content_type(s string) &Response { 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(s)
r.write_str("\r\n") r.write_str('\r\n')
return r return r
} }
[inline] [inline]
pub fn (mut r Response) html() &Response { 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 return r
} }
[inline] [inline]
pub fn (mut r Response) plain() &Response { 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 return r
} }
[inline] [inline]
pub fn (mut r Response) json() &Response { 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 return r
} }
[inline] [inline]
pub fn (mut r Response) body(body string) { pub fn (mut r Response) body(body string) {
r.write_str("Content-Length: ") r.write_str('Content-Length: ')
unsafe { unsafe {
r.buf += C.u64toa(r.buf, body.len) 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) 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) // rng.u32n(min, max) returns a pseudorandom u32 value that is guaranteed to be in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.u64n(min, max) returns a pseudorandom u64 value that is guaranteed to be in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min.') eprintln('max must be greater than min.')
exit(1) 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) // rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min.') eprintln('max must be greater than min.')
exit(1) 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) // rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rn.u32_in_range(min, max) - return a pseudorandom 32 bit unsigned u32 in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min.') eprintln('max must be greater than min.')
exit(1) 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) // rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min.') eprintln('max must be greater than min.')
exit(1) 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) // rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rn.u32_in_range(min, max) - return a pseudorandom 32 bit unsigned u32 in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rn.u64_in_range(min, max) - return a pseudorandom 64 bit unsigned u64 in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.int_in_range(min, max) - return a 32bit positive int in [0, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min.') eprintln('max must be greater than min.')
exit(1) 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) // rng.i64_in_range(min, max) - return a 64bit positive i64 in [0, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min.') eprintln('max must be greater than min.')
exit(1) 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) // rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.u32n(min, max) returns a pseudorandom u32 value that is guaranteed to be in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.u64n(min, max) returns a pseudorandom u64 value that is guaranteed to be in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // rng.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) exit(1)

View File

@ -46,7 +46,7 @@ pub fn (mut r SysRNG) seed(seed_data []u32) {
exit(1) exit(1)
} }
r.seed = seed_data[0] 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 // 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) // r.u32n(min, max) returns a pseudorandom u32 value that is guaranteed to be in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // r.u64n(min, max) returns a pseudorandom u64 value that is guaranteed to be in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // r.int_in_range(min, max) returns a pseudorandom int that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // r.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // r.f32_in_range(min, max) returns a pseudorandom f32 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) 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) // r.i64_in_range(min, max) returns a pseudorandom i64 that lies in [min, max)
[inline] [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 { if max <= min {
eprintln('max must be greater than min') eprintln('max must be greater than min')
exit(1) exit(1)

View File

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

View File

@ -5,9 +5,9 @@
// //
// The receive threads add all received numbers and send them to the // The receive threads add all received numbers and send them to the
// main thread where the total sum is compare to the expected value. // main thread where the total sum is compare to the expected value.
import time import time
import os import os
import sync
fn do_rec(ch chan int, resch chan i64, n int) { fn do_rec(ch chan int, resch chan i64, n int) {
mut sum := i64(0) mut sum := i64(0)
@ -18,7 +18,7 @@ fn do_rec(ch chan int, resch chan i64, n int) {
resch <- sum 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 { for i in start .. end {
ch <- i ch <- i
} }
@ -56,10 +56,10 @@ fn main() {
sum += <-resch sum += <-resch
} }
elapsed := stopwatch.elapsed() elapsed := stopwatch.elapsed()
rate := f64(nobj)/elapsed*time.microsecond rate := f64(nobj) / elapsed * time.microsecond
println('$nobj objects in ${f64(elapsed)/time.second} s (${rate:.2f} objs/µs)') println('$nobj objects in ${f64(elapsed) / time.second} s (${rate:.2f} objs/µs)')
// use sum formula by Gauß to calculate the expected result // 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') println('got: $sum, expected: $expected_sum')
assert sum == 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 // 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, file string) bool { fn (mut am AssetManager) add(asset_type string, file string) bool {
if !os.exists(file) { if !os.exists(file) {
// return error('vweb.assets: cannot add asset $file, it does not exist') // return error('vweb.assets: cannot add asset $file, it does not exist')
return false return false
@ -172,7 +172,7 @@ fn (mut am AssetManager) add(asset_type, file string) bool {
return true 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) assets := am.get_assets(asset_type)
for asset in assets { for asset in assets {
if asset.file_path == file { if asset.file_path == file {