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

make function arguments immutable by default

This commit is contained in:
Alexander Medvednikov
2019-08-07 08:19:27 +02:00
parent 06b8bd9382
commit 34e0b164eb
22 changed files with 107 additions and 83 deletions

View File

@@ -8,7 +8,7 @@ module cipher
// 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(dst, a, b []byte) int {
pub fn xor_bytes(dst mut []byte, a, b []byte) int {
mut n := a.len
if b.len < n {
n = b.len
@@ -17,13 +17,13 @@ pub fn xor_bytes(dst, a, b []byte) int {
return 0
}
safe_xor_bytes(dst, a, b, n)
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(dst, a, b []byte, n int) {
pub fn safe_xor_bytes(dst mut []byte, a, b []byte, n int) {
for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i]
}
@@ -32,5 +32,5 @@ pub fn safe_xor_bytes(dst, a, b []byte, n int) {
// 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(dst, a, b []byte) {
safe_xor_bytes(dst, a, b, b.len)
safe_xor_bytes(mut dst, a, b, b.len)
}

View File

@@ -55,7 +55,7 @@ pub fn new() *Digest {
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
pub fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
@@ -106,8 +106,8 @@ pub fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 1 + 63 + 8]
tmp[0] = 0x80
pad := (55 - int(d.len)) % 64 // calculate number of padding bytes
binary.little_endian_put_u64(tmp.right(1+pad), u64(d.len<<u64(3))) // append length in bits
d.write(tmp.left(1+pad+8))
binary.little_endian_put_u64(mut tmp.right(1+pad), u64(d.len<<u64(3))) // append length in bits
d.write(mut tmp.left(1+pad+8))
// The previous write ensures that a whole number of
// blocks (i.e. a multiple of 64 bytes) have been hashed.
@@ -117,10 +117,10 @@ pub fn (d mut Digest) checksum() []byte {
digest := [byte(0); Size]
binary.little_endian_put_u32(digest, d.s[0])
binary.little_endian_put_u32(digest.right(4), d.s[1])
binary.little_endian_put_u32(digest.right(8), d.s[2])
binary.little_endian_put_u32(digest.right(12), d.s[3])
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest.right(4), d.s[1])
binary.little_endian_put_u32(mut digest.right(8), d.s[2])
binary.little_endian_put_u32(mut digest.right(12), d.s[3])
return digest
}

View File

@@ -13,7 +13,7 @@ import (
encoding.binary
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
// load state
mut a := dig.s[0]
mut b := dig.s[1]

View File

@@ -58,7 +58,7 @@ pub fn new() &Digest {
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
pub fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
@@ -108,9 +108,9 @@ fn (d mut Digest) checksum() []byte {
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
d.write(mut tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
d.write(mut tmp.left(64+56-int(len)%64))
}
// Length in bits.

View File

@@ -17,7 +17,7 @@ const (
_K3 = 0xCA62C1D6
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u32(0); 16]
mut h0 := dig.h[0]
mut h1 := dig.h[1]

View File

@@ -51,7 +51,7 @@ mut:
is224 bool // mark if this digest is SHA-224
}
fn (d &Digest) reset() {
fn (d mut Digest) reset() {
d.h = [u32(0); 8]
d.x = [byte(0); Chunk]
if !d.is224 {
@@ -92,7 +92,7 @@ pub fn new224() *Digest {
return d
}
fn (d mut Digest) write(p []byte) ?int {
fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
@@ -145,14 +145,14 @@ fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 64]
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
d.write(mut tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
d.write(mut tmp.left(64+56-int(len)%64))
}
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, len)
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp.left(8))
if d.nx != 0 {
@@ -161,13 +161,13 @@ fn (d mut Digest) checksum() []byte {
digest := [byte(0); Size]
binary.big_endian_put_u32(digest, d.h[0])
binary.big_endian_put_u32(digest.right(4), d.h[1])
binary.big_endian_put_u32(digest.right(8), d.h[2])
binary.big_endian_put_u32(digest.right(12), d.h[3])
binary.big_endian_put_u32(digest.right(16), d.h[4])
binary.big_endian_put_u32(digest.right(20), d.h[5])
binary.big_endian_put_u32(digest.right(24), d.h[6])
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest.right(4), d.h[1])
binary.big_endian_put_u32(mut digest.right(8), d.h[2])
binary.big_endian_put_u32(mut digest.right(12), d.h[3])
binary.big_endian_put_u32(mut digest.right(16), d.h[4])
binary.big_endian_put_u32(mut digest.right(20), d.h[5])
binary.big_endian_put_u32(mut digest.right(24), d.h[6])
if !d.is224 {
binary.big_endian_put_u32(digest.right(28), d.h[7])
}

View File

@@ -80,7 +80,7 @@ const (
]
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u32(0); 64]
mut h0 := dig.h[0]

View File

@@ -146,14 +146,15 @@ fn new384() *Digest {
return _new(crypto.Hash.SHA384)
}
fn (d mut Digest) write(p []byte) ?int {
fn (d mut Digest) write(p_ []byte) ?int {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
block(d, d.x)
block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
@@ -164,7 +165,7 @@ fn (d mut Digest) write(p []byte) ?int {
}
if p.len >= Chunk {
n := p.len &~ (Chunk - 1)
block(d, p.left(n))
block(mut d, p.left(n))
if n >= p.len {
p = []byte
} else {
@@ -217,8 +218,8 @@ fn (d mut Digest) checksum() []byte {
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(tmp.right(8), len)
binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(mut tmp.right(8), len)
d.write(tmp.left(16))
if d.nx != 0 {
@@ -227,15 +228,15 @@ fn (d mut Digest) checksum() []byte {
mut digest := [byte(0); Size]
binary.big_endian_put_u64(digest, d.h[0])
binary.big_endian_put_u64(digest.right(8), d.h[1])
binary.big_endian_put_u64(digest.right(16), d.h[2])
binary.big_endian_put_u64(digest.right(24), d.h[3])
binary.big_endian_put_u64(digest.right(32), d.h[4])
binary.big_endian_put_u64(digest.right(40), d.h[5])
binary.big_endian_put_u64(mut digest, d.h[0])
binary.big_endian_put_u64(mut digest.right(8), d.h[1])
binary.big_endian_put_u64(mut digest.right(16), d.h[2])
binary.big_endian_put_u64(mut digest.right(24), d.h[3])
binary.big_endian_put_u64(mut digest.right(32), d.h[4])
binary.big_endian_put_u64(mut digest.right(40), d.h[5])
if d.function != crypto.Hash.SHA384 {
binary.big_endian_put_u64(digest.right(48), d.h[6])
binary.big_endian_put_u64(digest.right(56), d.h[7])
binary.big_endian_put_u64(mut digest.right(48), d.h[6])
binary.big_endian_put_u64(mut digest.right(56), d.h[7])
}
return digest
@@ -278,10 +279,10 @@ pub fn sum512_256(data []byte) []byte {
return sum256
}
fn block(dig &Digest, p []byte) {
fn block(dig mut Digest, p []byte) {
// For now just use block_generic until we have specific
// architecture optimized versions
block_generic(dig, p)
block_generic(mut dig, p)
}
pub fn (d &Digest) size() int {

View File

@@ -94,7 +94,7 @@ const(
]
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u64(0); 80]
mut h0 := dig.h[0]

View File

@@ -12,7 +12,7 @@ pub fn little_endian_endian_u16(b []byte) u16 {
}
pub fn little_endian_put_u16(b []byte, v u16) {
pub fn little_endian_put_u16(b mut []byte, v u16) {
_ := b[1] // bounds check
b[0] = byte(v)
b[1] = byte(v >> u16(8))
@@ -23,7 +23,7 @@ pub fn little_endian_u32(b []byte) u32 {
return u32(b[0]) | u32(u32(b[1])<<u32(8)) | u32(u32(b[2])<<u32(16)) | u32(u32(b[3])<<u32(24))
}
pub fn little_endian_put_u32(b []byte, v u32) {
pub fn little_endian_put_u32(b mut []byte, v u32) {
_ := b[3] // bounds check
b[0] = byte(v)
b[1] = byte(v >> u32(8))
@@ -31,13 +31,13 @@ pub fn little_endian_put_u32(b []byte, v u32) {
b[3] = byte(v >> u32(24))
}
pub fn little_endian_u64(b []byte) u64 {
pub fn little_endian_u64(b mut []byte) u64 {
_ := b[7] // bounds check
return u64(b[0]) | u64(u64(b[1])<<u64(8)) | u64(u64(b[2])<<u64(16)) | u64(u64(b[3])<<u64(24)) |
u64(u64(b[4])<<u64(32)) | u64(u64(b[5])<<u64(40)) | u64(u64(b[6])<<u64(48)) | u64(u64(b[7])<<u64(56))
}
pub fn little_endian_put_u64(b []byte, v u64) {
pub fn little_endian_put_u64(b mut []byte, v u64) {
_ := b[7] // bounds check
b[0] = byte(v)
b[1] = byte(v >> u64(8))
@@ -55,18 +55,18 @@ pub fn big_endian_u16(b []byte) u16 {
return u16(b[1]) | u16(u16(b[0])<<u16(8))
}
pub fn big_endian_put_u16(b []byte, v u16) {
pub fn big_endian_put_u16(b mut []byte, v u16) {
_ := b[1] // bounds check
b[0] = byte(v >> u16(8))
b[1] = byte(v)
}
pub fn big_endian_u32(b []byte) u32 {
pub fn big_endian_u32(b []byte) u32 {
_ := b[3] // bounds check
return u32(b[3]) | u32(u32(b[2])<<u32(8)) | u32(u32(b[1])<<u32(16)) | u32(u32(b[0])<<u32(24))
}
pub fn big_endian_put_u32(b []byte, v u32) {
pub fn big_endian_put_u32(b mut []byte, v u32) {
_ := b[3] // bounds check
b[0] = byte(v >> u32(24))
b[1] = byte(v >> u32(16))
@@ -80,7 +80,7 @@ pub fn big_endian_u64(b []byte) u64 {
u64(u64(b[3])<<u64(32)) | u64(u64(b[2])<<u64(40)) | u64(u64(b[1])<<u64(48)) | u64(u64(b[0])<<u64(56))
}
pub fn big_endian_put_u64(b []byte, v u64) {
pub fn big_endian_put_u64(b mut []byte, v u64) {
_ := b[7] // bounds check
b[0] = byte(v >> u64(56))
b[1] = byte(v >> u64(48))

View File

@@ -179,7 +179,9 @@ pub fn gen_buffer() u32 {
return vbo
}
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, stride int, ptr int) {
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride int, _ptr int) {
mut stride := _stride
mut ptr := _ptr
if typ == GL_FLOAT {
stride *= sizeof(f32)
ptr *= sizeof(f32)

View File

@@ -134,6 +134,7 @@ pub fn exp2(a f64) f64 {
// factorial calculates the factorial of the provided value.
// TODO bring back once multiple value functions are implemented
/*
fn recursive_product( n int, current_number_ptr &int) int{
mut m := n / 2
if (m == 0){
@@ -174,6 +175,7 @@ pub fn factorial(n int) i64 {
}
return i64((r << shift))
}
*/
// floor returns the nearest integer lower or equal of the provided value.
pub fn floor(a f64) f64 {
@@ -191,7 +193,9 @@ pub fn gamma(a f64) f64 {
}
// gcd calculates greatest common (positive) divisor (or zero if a and b are both zero).
pub fn gcd(a, b i64) i64 {
pub fn gcd(a_, b_ i64) i64 {
mut a := a_
mut b := b_
if a < 0 {
a = -a
}

View File

@@ -27,11 +27,13 @@ fn test_digits() {
assert negative_digits[2] == -1
}
/*
fn test_factorial() {
assert math.factorial(12) == 479001600
assert math.factorial(5) == 120
assert math.factorial(0) == 1
}
*/
fn test_erf() {
assert math.erf(0) == 0

View File

@@ -142,7 +142,8 @@ pub fn path_unescape(s string) ?string {
// unescape unescapes a string; the mode specifies
// which section of the URL string is being unescaped.
fn unescape(s string, mode EncodingMode) ?string {
fn unescape(s_ string, mode EncodingMode) ?string {
mut s := s_
// Count %, check that they're well-formed.
mut n := 0
mut has_plus := false
@@ -628,8 +629,9 @@ fn parse_host(host string) ?string {
h := unescape(host, .encode_host) or {
return err
}
host = h
return host
return h
//host = h
//return host
}
// set_path sets the path and raw_path fields of the URL based on the provided
@@ -640,7 +642,7 @@ fn parse_host(host string) ?string {
// - set_path('/foo%2fbar') will set path='/foo/bar' and raw_path='/foo%2fbar'
// set_path will return an error only if the provided path contains an invalid
// escaping.
fn (u &URL) set_path(p string) ?bool {
fn (u mut URL) set_path(p string) ?bool {
path := unescape(p, .encode_path) or {
return error(err)
}