Skip to content

Commit

Permalink
chore: apply v fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
igrekus authored and spytheman committed Apr 7, 2024
1 parent 038511c commit df4d8b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
33 changes: 17 additions & 16 deletions src/sum/sum.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import os
import common
import io

const (
app_name = 'sum'
app_description = 'Print checksum and block counts for each file in ARGS.'
)
const app_name = 'sum'
const app_description = 'Print checksum and block counts for each file in ARGS.'

struct Args {
use_bsd bool
Expand All @@ -16,7 +14,7 @@ struct Args {
}

struct Sum {
checksum int
checksum int
block_count u64
mut:
file_name string
Expand All @@ -27,23 +25,25 @@ const sysv_block_size = 512
const buffer_length = 128 * 1024

// bsd_sum_stream calculates checksum using BSD algorithm
fn bsd_sum_stream (bytes []u8, checksum int, _ bool) int {
fn bsd_sum_stream(bytes []u8, checksum int, _ bool) int {
mut tmp_checksum := checksum

for b in bytes {
tmp_checksum = (tmp_checksum >> 1) + ((tmp_checksum & 1) << 15);
tmp_checksum += b;
tmp_checksum &= 0xffff;
tmp_checksum = (tmp_checksum >> 1) + ((tmp_checksum & 1) << 15)
tmp_checksum += b
tmp_checksum &= 0xffff
}

return tmp_checksum
}

// sysv_sum_stream calculates checksum using SysV algorithm
fn sysv_sum_stream (bytes []u8, checksum int, is_final_chunk bool) int {
fn sysv_sum_stream(bytes []u8, checksum int, is_final_chunk bool) int {
mut temp := checksum

for b in bytes { temp += b }
for b in bytes {
temp += b
}

if is_final_chunk {
r := (temp & 0xffff) + ((temp & 0xffffffff) >> 16)
Expand Down Expand Up @@ -95,7 +95,7 @@ fn calc_sums(args Args) []Sum {
}

mut f := os.File{}
mut buf := []u8{cap: buffer_length, len: buffer_length}
mut buf := []u8{len: buffer_length, cap: buffer_length}
mut blocks := u64(0)
mut sums := []Sum{}

Expand All @@ -104,10 +104,12 @@ fn calc_sums(args Args) []Sum {
f = os.stdin()
} else {
f = os.open(file) or { panic(err) }
defer { f.close() }
defer {
f.close()
}
}

mut rd := io.new_buffered_reader(io.BufferedReaderConfig{reader: f})
mut rd := io.new_buffered_reader(io.BufferedReaderConfig{ reader: f })
mut checksum := 0

for {
Expand All @@ -128,7 +130,7 @@ fn calc_sums(args Args) []Sum {

file_name := match file {
'-' { '' }
else { ' $file' }
else { ' ${file}' }
}
sums << Sum{checksum, blocks, file_name}
}
Expand All @@ -155,7 +157,6 @@ fn print_bsd(mut sums []Sum) {
}
}


fn parse_args() Args {
mut fp := common.flag_parser(os.args)
fp.application(app_name)
Expand Down
1 change: 0 additions & 1 deletion src/sum/sum_test.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os

import common.testing

const eol = testing.output_eol()
Expand Down

0 comments on commit df4d8b0

Please sign in to comment.