From 8a380f4699752849bbba070d62594c78b4ee4bb4 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 8 May 2021 13:32:29 +0300 Subject: [PATCH] tools: make `v test-cleancode` test everything by default (#10050) --- .github/workflows/ci.yml | 12 +- .github/workflows/ci_sanitized.yml | 10 +- cmd/tools/modules/testing/common.v | 95 +- cmd/tools/vtest-cleancode.v | 77 +- cmd/tools/vtest-fmt.v | 18 +- cmd/tools/vtest-self.v | 2 +- cmd/tools/vtest.v | 2 +- vlib/clipboard/x11/clipboard.c.v | 18 +- vlib/crypto/aes/aes.v | 14 +- vlib/crypto/aes/aes_test.v | 3 +- vlib/crypto/aes/block_generic.v | 30 +- vlib/crypto/aes/cypher_generic.v | 4 +- vlib/crypto/crypto.v | 1 - vlib/crypto/hmac/hmac.v | 8 +- vlib/crypto/hmac/hmac_test.v | 78 +- vlib/crypto/internal/subtle/aliasing.v | 6 +- vlib/crypto/md5/md5.v | 22 +- vlib/crypto/md5/md5_test.v | 1 - vlib/crypto/md5/md5block_generic.v | 162 +- vlib/crypto/rand/rand_linux.c.v | 13 +- vlib/crypto/rand/rand_solaris.c.v | 14 +- vlib/crypto/rand/rand_windows.c.v | 6 +- vlib/crypto/rc4/rc4_test.v | 1 - vlib/crypto/sha1/sha1.v | 24 +- vlib/crypto/sha1/sha1_test.v | 1 - vlib/crypto/sha1/sha1block_generic.v | 18 +- vlib/crypto/sha256/sha256.v | 54 +- vlib/crypto/sha256/sha256_test.v | 4 +- vlib/crypto/sha256/sha256block_generic.v | 6 +- vlib/crypto/sha512/sha512.v | 102 +- vlib/encoding/base64/base64.v | 18 +- vlib/encoding/base64/base64_test.v | 30 +- vlib/encoding/binary/binary.v | 57 +- vlib/encoding/csv/reader_test.v | 36 +- vlib/eventbus/eventbus.v | 4 +- vlib/glm/glm_test.v | 6 +- vlib/gx/color.v | 46 +- vlib/gx/image.v | 2 +- vlib/hash/crc32/crc32.v | 16 +- vlib/hash/fnv1a/fnv1a.v | 28 +- vlib/hash/hash_wyhash_test.v | 16 +- vlib/hash/wyhash.c.v | 4 +- vlib/hash/wyhash.v | 87 +- vlib/io/io.v | 6 +- vlib/io/io_test.v | 4 +- vlib/io/util/util_test.v | 4 +- vlib/json/json_test.v | 4 +- vlib/math/bits.v | 23 +- vlib/math/bits/bits_tables.v | 140 +- vlib/math/bits/bits_test.v | 118 +- vlib/math/complex/complex.v | 270 +-- vlib/math/complex/complex_test.v | 531 +++--- vlib/math/const.v | 34 +- vlib/math/factorial/factorial.v | 56 +- vlib/math/factorial/factorial_tables.v | 719 ++++---- vlib/math/fractions/approximations.v | 10 +- vlib/math/fractions/approximations_test.v | 27 +- vlib/math/math.js.v | 40 +- vlib/math/stats/stats.v | 38 +- vlib/math/stats/stats_test.v | 109 +- vlib/math/unsafe.v | 9 +- vlib/mysql/_cdefs.c.v | 46 +- vlib/mysql/result.v | 6 +- vlib/mysql/utils.v | 4 +- vlib/os2/keep_vfmt_happy.v | 2 +- vlib/os2/os2_darwin.c.v | 16 +- vlib/os2/os2_test.v | 4 +- vlib/pg/pg.v | 20 +- vlib/picohttpparser/misc.v | 11 +- vlib/picohttpparser/picohttpparser.v | 4 +- vlib/picohttpparser/response.v | 8 +- vlib/rand/rand.v | 6 +- vlib/regex/regex.v | 1489 ++++++++--------- vlib/regex/regex_opt.v | 51 +- vlib/regex/regex_util.v | 238 ++- vlib/sokol/audio/audio.v | 6 + vlib/sokol/f/f.v | 2 +- vlib/sokol/gfx/enums.v | 436 +++-- vlib/sokol/gfx/gfx.v | 6 +- vlib/sokol/gfx/gfx_funcs.v | 5 +- vlib/sokol/gfx/gfx_utils.v | 4 +- vlib/sokol/sapp/enums.v | 304 ++-- vlib/sokol/sapp/sapp.v | 4 +- vlib/sokol/sapp/sapp_funcs.v | 94 +- vlib/sokol/sapp/sapp_structs.v | 60 +- vlib/sokol/sfons/sfons.v | 1 - vlib/sokol/sgl/sgl.v | 17 +- vlib/sokol/sgl/sgl_funcs.v | 16 +- vlib/sokol/sgl/sgl_structs.v | 16 +- vlib/sokol/sokol.v | 3 +- vlib/sqlite/sqlite_test.v | 12 +- vlib/stbi/stbi.v | 13 +- vlib/sync/atomic2/atomic_test.v | 2 +- vlib/sync/bench/channel_bench_v.v | 1 - .../many_writers_and_receivers_on_1_channel.v | 4 +- vlib/sync/channel_1_test.v | 2 +- vlib/sync/channel_2_test.v | 2 +- vlib/sync/channel_array_mut_test.v | 4 +- vlib/sync/channel_close_test.v | 16 +- vlib/sync/channel_fill_test.v | 2 +- vlib/sync/channel_polling_test.v | 1 - vlib/sync/channel_push_or_1_test.v | 16 +- vlib/sync/channel_push_or_2_test.v | 4 +- vlib/sync/channel_select_3_test.v | 4 +- vlib/sync/channel_select_4_test.v | 6 +- vlib/sync/channel_select_5_test.v | 10 +- vlib/sync/channel_select_6_test.v | 6 +- vlib/sync/channel_select_test.v | 8 +- vlib/sync/channel_try_buf_test.v | 2 +- vlib/sync/channel_try_unbuf_test.v | 2 +- vlib/sync/pool/pool.v | 15 +- vlib/sync/select_close_test.v | 4 +- vlib/sync/sync_macos.c.v | 39 +- vlib/sync/sync_windows.c.v | 35 +- vlib/sync/waitgroup.v | 4 +- vlib/szip/szip.v | 2 +- vlib/term/term.js.v | 6 +- vlib/term/term_test.v | 8 +- vlib/term/term_windows.c.v | 2 +- vlib/term/ui/color.v | 8 +- vlib/term/ui/input.v | 2 +- vlib/term/ui/input_windows.c.v | 92 +- vlib/term/ui/termios_nix.c.v | 5 +- vlib/term/ui/ui.v | 32 +- vlib/v/ast/ast.v | 12 +- vlib/v/gen/c/cheaders.v | 6 +- vlib/v/tests/fn_test.v | 4 +- vlib/x/openssl/declrarations.v | 25 + vlib/x/openssl/openssl.v | 43 +- vlib/x/ttf/common.v | 2 +- vlib/x/ttf/render_bmp.v | 4 +- vlib/x/ttf/ttf.v | 2 +- 132 files changed, 3218 insertions(+), 3428 deletions(-) create mode 100644 vlib/x/openssl/declrarations.v diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 176531477..4ccb282f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,7 +50,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev ## The following is needed for examples/wkhtmltopdf.v wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb sudo apt-get install xfonts-75dpi xfonts-base @@ -112,7 +112,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install --quiet -y libgc-dev ## The following is needed for examples/wkhtmltopdf.v wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb @@ -168,7 +168,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install --quiet -y xfonts-75dpi xfonts-base - name: Build v run: make @@ -213,7 +213,7 @@ jobs: - name: Install C++ dependencies run: | sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install --quiet -y valgrind g++-9 - name: Build V run: make -j4 @@ -341,7 +341,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev - name: Build V run: make -j4 && ./v -cc gcc -cg -cstrict -o v cmd/v - name: Valgrind v.c @@ -436,7 +436,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install clang - name: Build V run: make -j4 && ./v -cc clang -cg -cstrict -o v cmd/v diff --git a/.github/workflows/ci_sanitized.yml b/.github/workflows/ci_sanitized.yml index 7e7a89804..67f4b1606 100644 --- a/.github/workflows/ci_sanitized.yml +++ b/.github/workflows/ci_sanitized.yml @@ -25,7 +25,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install clang - name: Build V run: make -j4 && ./v -cg -cstrict -o v cmd/v @@ -50,7 +50,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev - name: Build V run: make -j4 && ./v -cg -cstrict -o v cmd/v - name: Self tests (-fsanitize=undefined) @@ -74,7 +74,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install clang - name: Build V run: make -j4 && ./v -cg -cstrict -o v cmd/v @@ -129,7 +129,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install clang - name: Build V run: make -j4 && ./v -cg -cstrict -o v cmd/v @@ -158,7 +158,7 @@ jobs: run: | sudo apt update sudo apt-get install --quiet -y postgresql libpq-dev libssl-dev sqlite3 libsqlite3-dev valgrind - sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev libgl-dev + sudo apt-get install --quiet -y libfreetype6-dev libxi-dev libxcursor-dev libgl-dev sudo apt-get install clang - name: Build V run: make -j4 && ./v -cc clang -cg -cstrict -o v cmd/v diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index 4ccc330f9..ac9ab76ae 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -12,6 +12,10 @@ const github_job = os.getenv('GITHUB_JOB') const show_start = os.getenv('VTEST_SHOW_START') == '1' +const hide_skips = os.getenv('VTEST_HIDE_SKIP') == '1' + +const hide_oks = os.getenv('VTEST_HIDE_OK') == '1' + pub struct TestSession { pub mut: files []string @@ -108,43 +112,50 @@ pub fn (mut ts TestSession) print_messages() { } } -pub fn new_test_session(_vargs string) TestSession { +pub fn new_test_session(_vargs string, will_compile bool) TestSession { mut skip_files := []string{} - $if solaris { - skip_files << 'examples/gg/gg2.v' - skip_files << 'examples/pico/pico.v' - skip_files << 'examples/sokol/fonts.v' - skip_files << 'examples/sokol/drawing.v' - } - $if macos { - skip_files << 'examples/database/mysql.v' - skip_files << 'examples/database/orm.v' - skip_files << 'examples/database/pg/customer.v' - } - $if windows { - skip_files << 'examples/database/mysql.v' - skip_files << 'examples/database/orm.v' - skip_files << 'examples/websocket/ping.v' // requires OpenSSL - skip_files << 'examples/websocket/client-server/client.v' // requires OpenSSL - skip_files << 'examples/websocket/client-server/server.v' // requires OpenSSL - $if tinyc { - skip_files << 'examples/database/orm.v' // try fix it + if will_compile { + $if solaris { + skip_files << 'examples/gg/gg2.v' + skip_files << 'examples/pico/pico.v' + skip_files << 'examples/sokol/fonts.v' + skip_files << 'examples/sokol/drawing.v' + } + $if macos { + skip_files << 'examples/database/mysql.v' + skip_files << 'examples/database/orm.v' + skip_files << 'examples/database/pg/customer.v' + } + $if windows { + skip_files << 'examples/database/mysql.v' + skip_files << 'examples/database/orm.v' + skip_files << 'examples/websocket/ping.v' // requires OpenSSL + skip_files << 'examples/websocket/client-server/client.v' // requires OpenSSL + skip_files << 'examples/websocket/client-server/server.v' // requires OpenSSL + $if tinyc { + skip_files << 'examples/database/orm.v' // try fix it + } + } + if testing.github_job != 'sokol-shaders-can-be-compiled' { + // These examples need .h files that are produced from the supplied .glsl files, + // using by the shader compiler tools in https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz + skip_files << 'examples/sokol/02_cubes_glsl/cube_glsl.v' + skip_files << 'examples/sokol/03_march_tracing_glsl/rt_glsl.v' + skip_files << 'examples/sokol/04_multi_shader_glsl/rt_glsl.v' + skip_files << 'examples/sokol/05_instancing_glsl/rt_glsl.v' + // Skip obj_viewer code in the CI + skip_files << 'examples/sokol/06_obj_viewer/show_obj.v' + } + if testing.github_job != 'ubuntu-tcc' { + skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases + // the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now + skip_files << 'vlib/x/ttf/ttf_test.v' + } + if testing.github_job != 'audio-examples' { + skip_files << 'examples/sokol/sounds/melody.v' + skip_files << 'examples/sokol/sounds/wav_player.v' + skip_files << 'examples/sokol/sounds/simple_sin_tones.v' } - } - if testing.github_job != 'sokol-shaders-can-be-compiled' { - // These examples need .h files that are produced from the supplied .glsl files, - // using by the shader compiler tools in https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz - skip_files << 'examples/sokol/02_cubes_glsl/cube_glsl.v' - skip_files << 'examples/sokol/03_march_tracing_glsl/rt_glsl.v' - skip_files << 'examples/sokol/04_multi_shader_glsl/rt_glsl.v' - skip_files << 'examples/sokol/05_instancing_glsl/rt_glsl.v' - // Skip obj_viewer code in the CI - skip_files << 'examples/sokol/06_obj_viewer/show_obj.v' - } - if testing.github_job != 'ubuntu-tcc' { - skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases - // the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now - skip_files << 'vlib/x/ttf/ttf_test.v' } vargs := _vargs.replace('-progress', '').replace('-progress', '') vexe := pref.vexe_path() @@ -268,7 +279,9 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr { if relative_file.replace('\\', '/') in ts.skip_files { ts.benchmark.skip() tls_bench.skip() - ts.append_message(.skip, tls_bench.step_message_skip(relative_file)) + if !testing.hide_skips { + ts.append_message(.skip, tls_bench.step_message_skip(relative_file)) + } return pool.no_result } if show_stats { @@ -304,7 +317,9 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr { } else { ts.benchmark.ok() tls_bench.ok() - ts.append_message(.ok, tls_bench.step_message_ok(relative_file)) + if !testing.hide_oks { + ts.append_message(.ok, tls_bench.step_message_ok(relative_file)) + } } } if os.exists(generated_binary_fpath) { @@ -336,7 +351,7 @@ pub fn prepare_test_session(zargs string, folder string, oskipped []string, main if vargs.len > 0 { eprintln('v compiler args: "$vargs"') } - mut session := new_test_session(vargs) + mut session := new_test_session(vargs, true) files := os.walk_ext(os.join_path(parent_dir, folder), '.v') mut mains := []string{} mut skipped := oskipped.clone() @@ -414,7 +429,9 @@ pub fn building_any_v_binaries_failed() bool { continue } bmark.ok() - eprintln(bmark.step_message_ok('command: $cmd')) + if !testing.hide_oks { + eprintln(bmark.step_message_ok('command: $cmd')) + } } bmark.stop() eprintln(term.h_divider('-')) diff --git a/cmd/tools/vtest-cleancode.v b/cmd/tools/vtest-cleancode.v index 25df7972c..7ea471cc4 100644 --- a/cmd/tools/vtest-cleancode.v +++ b/cmd/tools/vtest-cleancode.v @@ -3,6 +3,7 @@ module main import os import testing import v.util +import arrays const ( vet_known_failing_exceptions = []string{} @@ -31,70 +32,31 @@ const ( 'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */, 'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */, 'vlib/v/tests/fn_high_test.v', /* param name removed */ - 'vlib/v/tests/fn_test.v', /* bad comment formatting */ 'vlib/v/tests/generics_return_generics_struct_test.v', /* generic fn param removed */ 'vlib/v/tests/interop_test.v', /* bad comment formatting */ 'vlib/v/tests/generics_test.v', /* multi_generic_args, Foo >(...) becomes .... Foo>(...) which does not parse */ 'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */, 'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */ - 'examples/c_interop_wkhtmltopdf.v', /* &charptr --> &&char */ - 'vlib/v/gen/c/cheaders.v' /* infix wrapping error */, + 'examples/c_interop_wkhtmltopdf.v' /* &charptr --> &&char */, ] vfmt_verify_list = [ 'cmd/', 'examples/', 'tutorials/', - 'vlib/arrays/', - 'vlib/benchmark/', - 'vlib/bitfield/', - 'vlib/builtin/', - 'vlib/cli/', - 'vlib/dl/', - 'vlib/encoding/utf8/', - 'vlib/flag/', - 'vlib/gg/', - 'vlib/math/bits/bits.v', - 'vlib/orm/', - 'vlib/runtime/', - 'vlib/term/colors.v', - 'vlib/term/term.v', - 'vlib/v/ast/', - 'vlib/v/builder/', - 'vlib/v/cflag/', - 'vlib/v/checker/', - 'vlib/v/depgraph/', - 'vlib/v/doc/', - 'vlib/v/embed_file/', - 'vlib/v/errors/', - 'vlib/v/eval/', - 'vlib/v/fmt/', - 'vlib/v/gen/c/', - 'vlib/v/gen/js/', - 'vlib/v/gen/native/', - 'vlib/v/live/', - 'vlib/v/markused/', - 'vlib/v/parser/', - 'vlib/v/pkgconfig/', - 'vlib/v/pref/', - 'vlib/v/preludes', - 'vlib/v/scanner/', - 'vlib/v/tests/', - 'vlib/v/token/', - 'vlib/v/util/', - 'vlib/v/vcache/', - 'vlib/v/vet/', - 'vlib/v/vmod/', - 'vlib/cli/', - 'vlib/flag/', - 'vlib/math/big/', - 'vlib/os/', - 'vlib/semver/', - 'vlib/strings/', - 'vlib/time/', - 'vlib/vweb/', - 'vlib/x/json2', - 'vlib/x/websocket/', + 'vlib/', ] + vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [ + 'vlib/strconv/' /* prevent conflicts, till the new pure V string interpolation is merged */, + 'vlib/net/' /* prevent conflicts, till ipv6 support is merged */, + 'vlib/math/math_test.v', /* prevent conflict, till the sign PR is merged */ + 'vlib/term/ui/input.v' /* comment after a struct embed is removed */, + 'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */, + 'vlib/readline/readline_test.v' /* vfmt eats `{ Readline }` from `import readline { Readline }` */, + 'vlib/glm/glm.v' /* `mut res &f32` => `mut res f32`, which then fails to compile */, + 'vlib/fontstash/fontstash_structs.v' /* eats fn arg names for inline callback types in struct field declarations */, + 'vlib/crypto/sha512/sha512block_generic.v' /* formatting of large constant arrays wraps to too many lines */, + 'vlib/crypto/aes/const.v' /* formatting of large constant arrays wraps to too many lines */, + ]) ) const ( @@ -113,7 +75,7 @@ fn tsession(vargs string, tool_source string, tool_cmd string, tool_args string, os.chdir(vroot) title_message := 'running $tool_cmd over most .v files' testing.eheader(title_message) - mut test_session := testing.new_test_session('$vargs $tool_args') + mut test_session := testing.new_test_session('$vargs $tool_args', false) test_session.files << flist test_session.skip_files << slist util.prepare_tool_when_needed(tool_source) @@ -128,10 +90,11 @@ fn tsession(vargs string, tool_source string, tool_cmd string, tool_args string, fn v_test_vetting(vargs string) { expanded_vet_list := util.find_all_v_files(vet_folders) or { return } vet_session := tsession(vargs, 'vvet', 'v vet', 'vet', expanded_vet_list, vet_known_failing_exceptions) + // fmt_cmd, fmt_args := if is_fix { 'v fmt -w', 'fmt -w' } else { 'v fmt -verify', 'fmt -verify' } - expanded_vfmt_list := util.find_all_v_files(vfmt_verify_list) or { return } - verify_session := tsession(vargs, 'vfmt.v', fmt_cmd, fmt_args, expanded_vfmt_list, - verify_known_failing_exceptions) + vfmt_list := util.find_all_v_files(vfmt_verify_list) or { return } + exceptions := util.find_all_v_files(vfmt_known_failing_exceptions) or { return } + verify_session := tsession(vargs, 'vfmt.v', fmt_cmd, fmt_args, vfmt_list, exceptions) // if vet_session.benchmark.nfail > 0 || verify_session.benchmark.nfail > 0 { eprintln('\n') diff --git a/cmd/tools/vtest-fmt.v b/cmd/tools/vtest-fmt.v index 5dcdf0c4a..b1ebb81ef 100644 --- a/cmd/tools/vtest-fmt.v +++ b/cmd/tools/vtest-fmt.v @@ -6,21 +6,7 @@ import v.util const ( known_failing_exceptions = [ - 'vlib/crypto/aes/const.v', - // multiple narrow columns of []string turned to 1 long single column, otherwise works - 'vlib/v/gen/js/tests/life.v', - // error: unexpected `,`, expecting ), on JS.setInterval(fn () { show(game) game = step(game) }, 500) - 'vlib/builtin/js/builtin.v', - // JS.console.error(s) => JS.error(s), JS.process.exit(c) => JS.exit(c) - 'vlib/builtin/js/jsfns_node.js.v', - 'vlib/builtin/js/jsfns.js.v', - 'vlib/builtin/js/jsfns_browser.js.v', - // total chaos (duplicated code several times) in array_eq_test.v - 'vlib/builtin/array_eq_test.v', - // the fn args are removed, then `cb fn (picohttpparser.Request, mut picohttpparser.Response)` can not be reparsed - 'vlib/picoev/picoev.v', - // the preprocessor directives are formated to the V standard, even though they are in a string literal - 'vlib/v/gen/c/cheaders.v', + 'vlib/crypto/aes/const.v' /* const array wrapped in too many lines */, ] ) @@ -33,7 +19,7 @@ fn v_test_formatting(vargs string) { all_v_files := v_files() util.prepare_tool_when_needed('vfmt.v') testing.eheader('Run "v fmt" over all .v files') - mut vfmt_test_session := testing.new_test_session('$vargs fmt -worker') + mut vfmt_test_session := testing.new_test_session('$vargs fmt -worker', false) vfmt_test_session.files << all_v_files vfmt_test_session.skip_files << known_failing_exceptions vfmt_test_session.test() diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 8fed9e885..2d07c1efe 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -105,7 +105,7 @@ fn main() { title := 'testing vlib' all_test_files := os.walk_ext(os.join_path(vroot, 'vlib'), '_test.v') testing.eheader(title) - mut tsession := testing.new_test_session(cmd_prefix) + mut tsession := testing.new_test_session(cmd_prefix, true) tsession.files << all_test_files tsession.skip_files << skip_test_files mut werror := false diff --git a/cmd/tools/vtest.v b/cmd/tools/vtest.v index 416ff66f3..f995da06b 100644 --- a/cmd/tools/vtest.v +++ b/cmd/tools/vtest.v @@ -22,7 +22,7 @@ fn main() { backend_pos := args_before.index('-b') backend := if backend_pos == -1 { '.c' } else { args_before[backend_pos + 1] } // this giant mess because closures are not implemented - mut ts := testing.new_test_session(args_before.join(' ')) + mut ts := testing.new_test_session(args_before.join(' '), true) for targ in args_after { if os.is_dir(targ) { // Fetch all tests from the directory diff --git a/vlib/clipboard/x11/clipboard.c.v b/vlib/clipboard/x11/clipboard.c.v index e1daa6e7e..f8d0c8874 100644 --- a/vlib/clipboard/x11/clipboard.c.v +++ b/vlib/clipboard/x11/clipboard.c.v @@ -36,17 +36,17 @@ fn C.XChangeProperty(d &C.Display, requestor Window, property Atom, typ Atom, fo fn C.XSendEvent(d &C.Display, requestor Window, propogate int, mask i64, event &C.XEvent) -fn C.XInternAtom(d &C.Display, typ byteptr, only_if_exists int) Atom +fn C.XInternAtom(d &C.Display, typ &byte, only_if_exists int) Atom fn C.XCreateSimpleWindow(d &C.Display, root Window, x int, y int, width u32, height u32, border_width u32, border u64, background u64) Window -fn C.XOpenDisplay(name byteptr) &C.Display +fn C.XOpenDisplay(name &byte) &C.Display fn C.XConvertSelection(d &C.Display, selection Atom, target Atom, property Atom, requestor Window, time int) int fn C.XSync(d &C.Display, discard int) int -fn C.XGetWindowProperty(d &C.Display, w Window, property Atom, offset i64, length i64, delete int, req_type Atom, actual_type_return &Atom, actual_format_return &int, nitems &u64, bytes_after_return &u64, prop_return &byteptr) int +fn C.XGetWindowProperty(d &C.Display, w Window, property Atom, offset i64, length i64, delete int, req_type Atom, actual_type_return &Atom, actual_format_return &int, nitems &u64, bytes_after_return &u64, prop_return &&byte) int fn C.XDeleteProperty(d &C.Display, w Window, property Atom) int @@ -149,7 +149,7 @@ struct Property { actual_type Atom actual_format int nitems u64 - data byteptr + data &byte } // new_clipboard returns a new `Clipboard` instance allocated on the heap. @@ -359,7 +359,7 @@ fn (mut cb Clipboard) start_listener() { if cb.is_supported_target(prop.actual_type) { cb.got_text = true unsafe { - cb.text = byteptr(prop.data).vstring() // TODO: return byteptr to support other mimetypes + cb.text = prop.data.vstring() // TODO: return byteptr to support other mimetypes } } cb.mutex.unlock() @@ -393,14 +393,14 @@ fn read_property(d &C.Display, w Window, p Atom) Property { actual_format := 0 nitems := u64(0) bytes_after := u64(0) - ret := byteptr(0) + ret := &byte(0) mut read_bytes := 1024 for { if ret != 0 { C.XFree(ret) } - C.XGetWindowProperty(d, w, p, 0, read_bytes, 0, 0, &actual_type, - &actual_format, &nitems, &bytes_after, &ret) + C.XGetWindowProperty(d, w, p, 0, read_bytes, 0, 0, &actual_type, &actual_format, + &nitems, &bytes_after, &ret) read_bytes *= 2 if bytes_after == 0 { break @@ -421,7 +421,7 @@ fn (cb &Clipboard) pick_target(prop Property) Atom { // next instead as the lowest common denominator return cb.get_atom(.xa_string) } else { - atom_list := &Atom(prop.data) + atom_list := &Atom(voidptr(prop.data)) mut to_be_requested := Atom(0) diff --git a/vlib/crypto/aes/aes.v b/vlib/crypto/aes/aes.v index 5d8eba738..f7e2cecbc 100644 --- a/vlib/crypto/aes/aes.v +++ b/vlib/crypto/aes/aes.v @@ -40,20 +40,20 @@ pub fn new_cipher(key []byte) AesCipher { // block_size returns the block size of the checksum in bytes. pub fn (c &AesCipher) block_size() int { - return block_size + return aes.block_size } // encrypt encrypts the blocks in `src` to `dst`. // Please note: `dst` and `src` are both mutable for performance reasons. pub fn (c &AesCipher) encrypt(mut dst []byte, mut src []byte) { - if src.len < block_size { + if src.len < aes.block_size { panic('crypto.aes: input not full block') } - if dst.len < block_size { + if dst.len < aes.block_size { panic('crypto.aes: output not full block') } // if subtle.inexact_overlap(dst[:block_size], src[:block_size]) { - if subtle.inexact_overlap((*dst)[..block_size], (*src)[..block_size]) { + if subtle.inexact_overlap((*dst)[..aes.block_size], (*src)[..aes.block_size]) { panic('crypto.aes: invalid buffer overlap') } // for now use generic version @@ -63,13 +63,13 @@ pub fn (c &AesCipher) encrypt(mut dst []byte, mut src []byte) { // decrypt decrypts the blocks in `src` to `dst`. // Please note: `dst` and `src` are both mutable for performance reasons. pub fn (c &AesCipher) decrypt(mut dst []byte, mut src []byte) { - if src.len < block_size { + if src.len < aes.block_size { panic('crypto.aes: input not full block') } - if dst.len < block_size { + if dst.len < aes.block_size { panic('crypto.aes: output not full block') } - if subtle.inexact_overlap((*dst)[..block_size], (*src)[..block_size]) { + if subtle.inexact_overlap((*dst)[..aes.block_size], (*src)[..aes.block_size]) { panic('crypto.aes: invalid buffer overlap') } // for now use generic version diff --git a/vlib/crypto/aes/aes_test.v b/vlib/crypto/aes/aes_test.v index 38e773b4c..ca85d0608 100644 --- a/vlib/crypto/aes/aes_test.v +++ b/vlib/crypto/aes/aes_test.v @@ -22,7 +22,6 @@ fn test_crypto_aes() { mode := aes.new_cbc(block, iv) cipher_clone := ciphertext.clone() mode.encrypt_blocks(mut ciphertext, cipher_clone) - assert ciphertext.hex() == - 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1' + assert ciphertext.hex() == 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1' println('ok') } diff --git a/vlib/crypto/aes/block_generic.v b/vlib/crypto/aes/block_generic.v index 3b52c3b19..5da938e0d 100644 --- a/vlib/crypto/aes/block_generic.v +++ b/vlib/crypto/aes/block_generic.v @@ -69,14 +69,10 @@ fn encrypt_block_generic(xk []u32, mut dst []byte, src []byte) { s3 = t3 } // Last round uses s-box directly and XORs to produce output. - s0 = s_box0[t0 >> 24] << 24 | - s_box0[t1 >> 16 & 0xff] << 16 | u32(s_box0[t2 >> 8 & 0xff] << 8) | s_box0[t3 & u32(0xff)] - s1 = s_box0[t1 >> 24] << 24 | - s_box0[t2 >> 16 & 0xff] << 16 | u32(s_box0[t3 >> 8 & 0xff] << 8) | s_box0[t0 & u32(0xff)] - s2 = s_box0[t2 >> 24] << 24 | - s_box0[t3 >> 16 & 0xff] << 16 | u32(s_box0[t0 >> 8 & 0xff] << 8) | s_box0[t1 & u32(0xff)] - s3 = s_box0[t3 >> 24] << 24 | - s_box0[t0 >> 16 & 0xff] << 16 | u32(s_box0[t1 >> 8 & 0xff] << 8) | s_box0[t2 & u32(0xff)] + s0 = s_box0[t0 >> 24] << 24 | s_box0[t1 >> 16 & 0xff] << 16 | u32(s_box0[t2 >> 8 & 0xff] << 8) | s_box0[t3 & u32(0xff)] + s1 = s_box0[t1 >> 24] << 24 | s_box0[t2 >> 16 & 0xff] << 16 | u32(s_box0[t3 >> 8 & 0xff] << 8) | s_box0[t0 & u32(0xff)] + s2 = s_box0[t2 >> 24] << 24 | s_box0[t3 >> 16 & 0xff] << 16 | u32(s_box0[t0 >> 8 & 0xff] << 8) | s_box0[t1 & u32(0xff)] + s3 = s_box0[t3 >> 24] << 24 | s_box0[t0 >> 16 & 0xff] << 16 | u32(s_box0[t1 >> 8 & 0xff] << 8) | s_box0[t2 & u32(0xff)] s0 ^= xk[k + 0] s1 ^= xk[k + 1] s2 ^= xk[k + 2] @@ -120,14 +116,10 @@ fn decrypt_block_generic(xk []u32, mut dst []byte, src []byte) { s3 = t3 } // Last round uses s-box directly and XORs to produce output. - s0 = u32(s_box1[t0 >> 24]) << 24 | - u32(s_box1[t3 >> 16 & 0xff]) << 16 | u32(s_box1[t2 >> 8 & 0xff] << 8) | u32(s_box1[t1 & u32(0xff)]) - s1 = u32(s_box1[t1 >> 24]) << 24 | - u32(s_box1[t0 >> 16 & 0xff]) << 16 | u32(s_box1[t3 >> 8 & 0xff] << 8) | u32(s_box1[t2 & u32(0xff)]) - s2 = u32(s_box1[t2 >> 24]) << 24 | - u32(s_box1[t1 >> 16 & 0xff]) << 16 | u32(s_box1[t0 >> 8 & 0xff] << 8) | u32(s_box1[t3 & u32(0xff)]) - s3 = u32(s_box1[t3 >> 24]) << 24 | - u32(s_box1[t2 >> 16 & 0xff]) << 16 | u32(s_box1[t1 >> 8 & 0xff] << 8) | u32(s_box1[t0 & u32(0xff)]) + s0 = u32(s_box1[t0 >> 24]) << 24 | u32(s_box1[t3 >> 16 & 0xff]) << 16 | u32(s_box1[t2 >> 8 & 0xff] << 8) | u32(s_box1[t1 & u32(0xff)]) + s1 = u32(s_box1[t1 >> 24]) << 24 | u32(s_box1[t0 >> 16 & 0xff]) << 16 | u32(s_box1[t3 >> 8 & 0xff] << 8) | u32(s_box1[t2 & u32(0xff)]) + s2 = u32(s_box1[t2 >> 24]) << 24 | u32(s_box1[t1 >> 16 & 0xff]) << 16 | u32(s_box1[t0 >> 8 & 0xff] << 8) | u32(s_box1[t3 & u32(0xff)]) + s3 = u32(s_box1[t3 >> 24]) << 24 | u32(s_box1[t2 >> 16 & 0xff]) << 16 | u32(s_box1[t1 >> 8 & 0xff] << 8) | u32(s_box1[t0 & u32(0xff)]) s0 ^= xk[k + 0] s1 ^= xk[k + 1] s2 ^= xk[k + 2] @@ -141,8 +133,7 @@ fn decrypt_block_generic(xk []u32, mut dst []byte, src []byte) { // Apply s_box0 to each byte in w. fn subw(w u32) u32 { - return u32(s_box0[w >> 24]) << 24 | u32(s_box0[w >> 16 & 0xff] << 16) | u32(s_box0[w >> 8 & - 0xff] << 8) | u32(s_box0[w & u32(0xff)]) + return u32(s_box0[w >> 24]) << 24 | u32(s_box0[w >> 16 & 0xff] << 16) | u32(s_box0[w >> 8 & 0xff] << 8) | u32(s_box0[w & u32(0xff)]) } // Rotate @@ -184,8 +175,7 @@ fn expand_key_generic(key []byte, mut enc []u32, mut dec []u32) { for j in 0 .. 4 { mut x := enc[ei + j] if i > 0 && i + 4 < n { - x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^ - td3[s_box0[x & u32(0xff)]] + x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^ td3[s_box0[x & u32(0xff)]] } dec[i + j] = x } diff --git a/vlib/crypto/aes/cypher_generic.v b/vlib/crypto/aes/cypher_generic.v index a1b10070b..fb8bb0866 100644 --- a/vlib/crypto/aes/cypher_generic.v +++ b/vlib/crypto/aes/cypher_generic.v @@ -8,8 +8,8 @@ module aes fn new_cipher_generic(key []byte) AesCipher { n := key.len + 28 mut c := AesCipher{ - enc: []u32{len: (n)} - dec: []u32{len: (n)} + enc: []u32{len: n} + dec: []u32{len: n} } expand_key_generic(key, mut c.enc, mut c.dec) return c diff --git a/vlib/crypto/crypto.v b/vlib/crypto/crypto.v index 7c0e4fbe5..12a092fcb 100644 --- a/vlib/crypto/crypto.v +++ b/vlib/crypto/crypto.v @@ -21,4 +21,3 @@ pub enum Hash { blake2b_384 blake2b_512 } - diff --git a/vlib/crypto/hmac/hmac.v b/vlib/crypto/hmac/hmac.v index bc9160af5..3f45467fe 100644 --- a/vlib/crypto/hmac/hmac.v +++ b/vlib/crypto/hmac/hmac.v @@ -11,7 +11,7 @@ const ( ) // new returns a HMAC byte array, depending on the hash algorithm used. -pub fn new(key []byte, data []byte, hash_func fn (bytes []byte) []byte, blocksize int) []byte { +pub fn new(key []byte, data []byte, hash_func fn ([]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 @@ -19,16 +19,16 @@ pub fn new(key []byte, data []byte, hash_func fn (bytes []byte) []byte, blocksiz b_key = hash_func(key) } if b_key.len < blocksize { - b_key << npad[..blocksize - b_key.len] + b_key << hmac.npad[..blocksize - b_key.len] } mut inner := []byte{} - for i, b in ipad[..blocksize] { + for i, b in hmac.ipad[..blocksize] { inner << b_key[i] ^ b } inner << data inner_hash := hash_func(inner) mut outer := []byte{cap: b_key.len} - for i, b in opad[..blocksize] { + for i, b in hmac.opad[..blocksize] { outer << b_key[i] ^ b } outer << inner_hash diff --git a/vlib/crypto/hmac/hmac_test.v b/vlib/crypto/hmac/hmac_test.v index 9c0c3b37c..3b78420ce 100644 --- a/vlib/crypto/hmac/hmac_test.v +++ b/vlib/crypto/hmac/hmac_test.v @@ -21,31 +21,37 @@ import crypto.sha512 // import crypto.blake2b_512 const ( keys = [[byte(0xb), 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb], - 'Jefe'.bytes(), [byte(0xAA), 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA], [byte(0x01), 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, - 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], - [byte(0x0c), 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, - 0x0c, - ], [byte(0xaa), 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa], [byte(0xaa), 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa]] + 'Jefe'.bytes(), + [byte(0xAA), 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, + 0xAA, 0xAA, 0xAA, 0xAA, 0xAA], + [byte(0x01), 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], + [byte(0x0c), 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c], + [byte(0xaa), 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + [byte(0xaa), 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + ] data = ['Hi There'.bytes(), 'what do ya want for nothing?'.bytes(), - [byte(0xDD), 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, - 0xDD, 0xDD, 0xDD], [byte(0xcd), 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd], - 'Test With Truncation'.bytes(), 'Test Using Larger Than Block-Size Key - Hash Key First'.bytes(), + [byte(0xDD), 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, + 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD], + [byte(0xcd), 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd], + 'Test With Truncation'.bytes(), + 'Test Using Larger Than Block-Size Key - Hash Key First'.bytes(), 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'.bytes(), ] ) @@ -61,8 +67,8 @@ fn test_hmac_md5() { '6f630fad67cda0ee1fb1f562db3aa53e', ] mut result := '' - for i, key in keys { - result = hmac.new(key, data[i], md5.sum, md5.block_size).hex() + for i, key in hmac.keys { + result = hmac.new(key, hmac.data[i], md5.sum, md5.block_size).hex() assert result == md5_expected_results[i] } } @@ -78,8 +84,8 @@ fn test_hmac_sha1() { 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91', ] mut result := '' - for i, key in keys { - result = hmac.new(key, data[i], sha1.sum, sha1.block_size).hex() + for i, key in hmac.keys { + result = hmac.new(key, hmac.data[i], sha1.sum, sha1.block_size).hex() assert result == sha1_expected_results[i] } } @@ -95,8 +101,8 @@ fn test_hmac_sha224() { '7358939e58683a448ac5065196d33191a1c1d33d4b8b0304dc60f5e0', ] mut result := '' - for i, key in keys { - result = hmac.new(key, data[i], sha256.sum224, sha256.block_size).hex() + for i, key in hmac.keys { + result = hmac.new(key, hmac.data[i], sha256.sum224, sha256.block_size).hex() assert result == sha224_expected_results[i] } } @@ -112,8 +118,8 @@ fn test_hmac_sha256() { '6355ac22e890d0a3c8481a5ca4825bc884d3e7a1ff98a2fc2ac7d8e064c3b2e6', ] mut result := '' - for i, key in keys { - result = hmac.new(key, data[i], sha256.sum, sha256.block_size).hex() + for i, key in hmac.keys { + result = hmac.new(key, hmac.data[i], sha256.sum, sha256.block_size).hex() assert result == sha256_expected_results[i] } } @@ -129,8 +135,8 @@ fn test_hmac_sha384() { '34f065bdedc2487c30a634d9a49cf42116f78bb386ea4d498aea05c0077f05373cfdaa9b59a7b0481bced9e3f55016a9', ] mut result := '' - for i, key in keys { - result = hmac.new(key, data[i], sha512.sum384, sha512.block_size).hex() + for i, key in hmac.keys { + result = hmac.new(key, hmac.data[i], sha512.sum384, sha512.block_size).hex() assert result == sha384_expected_results[i] } } @@ -146,8 +152,8 @@ fn test_hmac_sha512() { '09441cda584ed2f4d2f5b519c71baf3c79cce19dfc89a548e73b3bb382a9124d6e792b77bf57903ff5858e5d111d15f45d6fd118eea023f28d2eb234ebe62f85', ] mut result := '' - for i, key in keys { - result = hmac.new(key, data[i], sha512.sum512, sha512.block_size).hex() + for i, key in hmac.keys { + result = hmac.new(key, hmac.data[i], sha512.sum512, sha512.block_size).hex() assert result == sha512_expected_results[i] } } diff --git a/vlib/crypto/internal/subtle/aliasing.v b/vlib/crypto/internal/subtle/aliasing.v index 4108809ad..e09748ee1 100644 --- a/vlib/crypto/internal/subtle/aliasing.v +++ b/vlib/crypto/internal/subtle/aliasing.v @@ -10,9 +10,9 @@ module subtle // corresponding) index. The memory beyond the slice length is ignored. 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] && + 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]} + 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 @@ -22,7 +22,7 @@ pub fn any_overlap(x []byte, 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 []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 any_overlap(x, y) diff --git a/vlib/crypto/md5/md5.v b/vlib/crypto/md5/md5.v index 2d56d5513..17796b16e 100644 --- a/vlib/crypto/md5/md5.v +++ b/vlib/crypto/md5/md5.v @@ -35,11 +35,11 @@ mut: fn (mut d Digest) reset() { d.s = []u32{len: (4)} - d.x = []byte{len: (block_size)} - d.s[0] = u32(init0) - d.s[1] = u32(init1) - d.s[2] = u32(init2) - d.s[3] = u32(init3) + d.x = []byte{len: md5.block_size} + d.s[0] = u32(md5.init0) + d.s[1] = u32(md5.init1) + d.s[2] = u32(md5.init2) + d.s[3] = u32(md5.init3) d.nx = 0 d.len = 0 } @@ -60,7 +60,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int { if d.nx > 0 { n := copy(d.x[d.nx..], p) d.nx += n - if d.nx == block_size { + if d.nx == md5.block_size { block(mut d, d.x) d.nx = 0 } @@ -70,8 +70,8 @@ pub fn (mut d Digest) write(p_ []byte) ?int { p = p[n..] } } - if p.len >= block_size { - n := p.len & ~(block_size - 1) + if p.len >= md5.block_size { + n := p.len & ~(md5.block_size - 1) block(mut d, p[..n]) if n >= p.len { p = [] @@ -116,7 +116,7 @@ pub fn (mut d Digest) checksum() []byte { if d.nx != 0 { panic('d.nx != 0') } - mut digest := []byte{len: (size)} + mut digest := []byte{len: md5.size} binary.little_endian_put_u32(mut digest, d.s[0]) binary.little_endian_put_u32(mut digest[4..], d.s[1]) binary.little_endian_put_u32(mut digest[8..], d.s[2]) @@ -139,12 +139,12 @@ fn block(mut dig Digest, p []byte) { // size returns the size of the checksum in bytes. pub fn (d &Digest) size() int { - return size + return md5.size } // block_size returns the block size of the checksum in bytes. pub fn (d &Digest) block_size() int { - return block_size + return md5.block_size } // hexhash returns a hexadecimal MD5 hash sum `string` of `s`. diff --git a/vlib/crypto/md5/md5_test.v b/vlib/crypto/md5/md5_test.v index 60530d9b5..fd43c1c71 100644 --- a/vlib/crypto/md5/md5_test.v +++ b/vlib/crypto/md5/md5_test.v @@ -1,7 +1,6 @@ // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - import crypto.md5 fn test_crypto_md5() { diff --git a/vlib/crypto/md5/md5block_generic.v b/vlib/crypto/md5/md5block_generic.v index e2dd6029b..ebc4a8dd3 100644 --- a/vlib/crypto/md5/md5block_generic.v +++ b/vlib/crypto/md5/md5block_generic.v @@ -18,7 +18,7 @@ fn block_generic(mut dig Digest, p []byte) { mut c := dig.s[2] mut d := dig.s[3] - for i := 0; i <= p.len-block_size; i += block_size { + for i := 0; i <= p.len - block_size; i += block_size { mut q := p[i..] q = q[..block_size] // save current state @@ -28,94 +28,94 @@ fn block_generic(mut dig Digest, p []byte) { dd := d // load input block - x0 := binary.little_endian_u32(q[4*0x0..]) - x1 := binary.little_endian_u32(q[4*0x1..]) - x2 := binary.little_endian_u32(q[4*0x2..]) - x3 := binary.little_endian_u32(q[4*0x3..]) - x4 := binary.little_endian_u32(q[4*0x4..]) - x5 := binary.little_endian_u32(q[4*0x5..]) - x6 := binary.little_endian_u32(q[4*0x6..]) - x7 := binary.little_endian_u32(q[4*0x7..]) - x8 := binary.little_endian_u32(q[4*0x8..]) - x9 := binary.little_endian_u32(q[4*0x9..]) - xa := binary.little_endian_u32(q[4*0xa..]) - xb := binary.little_endian_u32(q[4*0xb..]) - xc := binary.little_endian_u32(q[4*0xc..]) - xd := binary.little_endian_u32(q[4*0xd..]) - xe := binary.little_endian_u32(q[4*0xe..]) - xf := binary.little_endian_u32(q[4*0xf..]) + x0 := binary.little_endian_u32(q[4 * 0x0..]) + x1 := binary.little_endian_u32(q[4 * 0x1..]) + x2 := binary.little_endian_u32(q[4 * 0x2..]) + x3 := binary.little_endian_u32(q[4 * 0x3..]) + x4 := binary.little_endian_u32(q[4 * 0x4..]) + x5 := binary.little_endian_u32(q[4 * 0x5..]) + x6 := binary.little_endian_u32(q[4 * 0x6..]) + x7 := binary.little_endian_u32(q[4 * 0x7..]) + x8 := binary.little_endian_u32(q[4 * 0x8..]) + x9 := binary.little_endian_u32(q[4 * 0x9..]) + xa := binary.little_endian_u32(q[4 * 0xa..]) + xb := binary.little_endian_u32(q[4 * 0xb..]) + xc := binary.little_endian_u32(q[4 * 0xc..]) + xd := binary.little_endian_u32(q[4 * 0xd..]) + xe := binary.little_endian_u32(q[4 * 0xe..]) + xf := binary.little_endian_u32(q[4 * 0xf..]) // round 1 - a = b + bits.rotate_left_32((((c^d)&b)^d)+a+x0+u32(0xd76aa478), 7) - d = a + bits.rotate_left_32((((b^c)&a)^c)+d+x1+u32(0xe8c7b756), 12) - c = d + bits.rotate_left_32((((a^b)&d)^b)+c+x2+u32(0x242070db), 17) - b = c + bits.rotate_left_32((((d^a)&c)^a)+b+x3+u32(0xc1bdceee), 22) - a = b + bits.rotate_left_32((((c^d)&b)^d)+a+x4+u32(0xf57c0faf), 7) - d = a + bits.rotate_left_32((((b^c)&a)^c)+d+x5+u32(0x4787c62a), 12) - c = d + bits.rotate_left_32((((a^b)&d)^b)+c+x6+u32(0xa8304613), 17) - b = c + bits.rotate_left_32((((d^a)&c)^a)+b+x7+u32(0xfd469501), 22) - a = b + bits.rotate_left_32((((c^d)&b)^d)+a+x8+u32(0x698098d8), 7) - d = a + bits.rotate_left_32((((b^c)&a)^c)+d+x9+u32(0x8b44f7af), 12) - c = d + bits.rotate_left_32((((a^b)&d)^b)+c+xa+u32(0xffff5bb1), 17) - b = c + bits.rotate_left_32((((d^a)&c)^a)+b+xb+u32(0x895cd7be), 22) - a = b + bits.rotate_left_32((((c^d)&b)^d)+a+xc+u32(0x6b901122), 7) - d = a + bits.rotate_left_32((((b^c)&a)^c)+d+xd+u32(0xfd987193), 12) - c = d + bits.rotate_left_32((((a^b)&d)^b)+c+xe+u32(0xa679438e), 17) - b = c + bits.rotate_left_32((((d^a)&c)^a)+b+xf+u32(0x49b40821), 22) + a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x0 + u32(0xd76aa478), 7) + d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x1 + u32(0xe8c7b756), 12) + c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + x2 + u32(0x242070db), 17) + b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + x3 + u32(0xc1bdceee), 22) + a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x4 + u32(0xf57c0faf), 7) + d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x5 + u32(0x4787c62a), 12) + c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + x6 + u32(0xa8304613), 17) + b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + x7 + u32(0xfd469501), 22) + a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x8 + u32(0x698098d8), 7) + d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x9 + u32(0x8b44f7af), 12) + c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + xa + u32(0xffff5bb1), 17) + b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + xb + u32(0x895cd7be), 22) + a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + xc + u32(0x6b901122), 7) + d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + xd + u32(0xfd987193), 12) + c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + xe + u32(0xa679438e), 17) + b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + xf + u32(0x49b40821), 22) // round 2 - a = b + bits.rotate_left_32((((b^c)&d)^c)+a+x1+u32(0xf61e2562), 5) - d = a + bits.rotate_left_32((((a^b)&c)^b)+d+x6+u32(0xc040b340), 9) - c = d + bits.rotate_left_32((((d^a)&b)^a)+c+xb+u32(0x265e5a51), 14) - b = c + bits.rotate_left_32((((c^d)&a)^d)+b+x0+u32(0xe9b6c7aa), 20) - a = b + bits.rotate_left_32((((b^c)&d)^c)+a+x5+u32(0xd62f105d), 5) - d = a + bits.rotate_left_32((((a^b)&c)^b)+d+xa+u32(0x02441453), 9) - c = d + bits.rotate_left_32((((d^a)&b)^a)+c+xf+u32(0xd8a1e681), 14) - b = c + bits.rotate_left_32((((c^d)&a)^d)+b+x4+u32(0xe7d3fbc8), 20) - a = b + bits.rotate_left_32((((b^c)&d)^c)+a+x9+u32(0x21e1cde6), 5) - d = a + bits.rotate_left_32((((a^b)&c)^b)+d+xe+u32(0xc33707d6), 9) - c = d + bits.rotate_left_32((((d^a)&b)^a)+c+x3+u32(0xf4d50d87), 14) - b = c + bits.rotate_left_32((((c^d)&a)^d)+b+x8+u32(0x455a14ed), 20) - a = b + bits.rotate_left_32((((b^c)&d)^c)+a+xd+u32(0xa9e3e905), 5) - d = a + bits.rotate_left_32((((a^b)&c)^b)+d+x2+u32(0xfcefa3f8), 9) - c = d + bits.rotate_left_32((((d^a)&b)^a)+c+x7+u32(0x676f02d9), 14) - b = c + bits.rotate_left_32((((c^d)&a)^d)+b+xc+u32(0x8d2a4c8a), 20) + a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x1 + u32(0xf61e2562), 5) + d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + x6 + u32(0xc040b340), 9) + c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + xb + u32(0x265e5a51), 14) + b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x0 + u32(0xe9b6c7aa), 20) + a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x5 + u32(0xd62f105d), 5) + d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + xa + u32(0x02441453), 9) + c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + xf + u32(0xd8a1e681), 14) + b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x4 + u32(0xe7d3fbc8), 20) + a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x9 + u32(0x21e1cde6), 5) + d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + xe + u32(0xc33707d6), 9) + c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + x3 + u32(0xf4d50d87), 14) + b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x8 + u32(0x455a14ed), 20) + a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + xd + u32(0xa9e3e905), 5) + d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + x2 + u32(0xfcefa3f8), 9) + c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + x7 + u32(0x676f02d9), 14) + b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + xc + u32(0x8d2a4c8a), 20) // round 3 - a = b + bits.rotate_left_32((b^c^d)+a+x5+u32(0xfffa3942), 4) - d = a + bits.rotate_left_32((a^b^c)+d+x8+u32(0x8771f681), 11) - c = d + bits.rotate_left_32((d^a^b)+c+xb+u32(0x6d9d6122), 16) - b = c + bits.rotate_left_32((c^d^a)+b+xe+u32(0xfde5380c), 23) - a = b + bits.rotate_left_32((b^c^d)+a+x1+u32(0xa4beea44), 4) - d = a + bits.rotate_left_32((a^b^c)+d+x4+u32(0x4bdecfa9), 11) - c = d + bits.rotate_left_32((d^a^b)+c+x7+u32(0xf6bb4b60), 16) - b = c + bits.rotate_left_32((c^d^a)+b+xa+u32(0xbebfbc70), 23) - a = b + bits.rotate_left_32((b^c^d)+a+xd+u32(0x289b7ec6), 4) - d = a + bits.rotate_left_32((a^b^c)+d+x0+u32(0xeaa127fa), 11) - c = d + bits.rotate_left_32((d^a^b)+c+x3+u32(0xd4ef3085), 16) - b = c + bits.rotate_left_32((c^d^a)+b+x6+u32(0x04881d05), 23) - a = b + bits.rotate_left_32((b^c^d)+a+x9+u32(0xd9d4d039), 4) - d = a + bits.rotate_left_32((a^b^c)+d+xc+u32(0xe6db99e5), 11) - c = d + bits.rotate_left_32((d^a^b)+c+xf+u32(0x1fa27cf8), 16) - b = c + bits.rotate_left_32((c^d^a)+b+x2+u32(0xc4ac5665), 23) + a = b + bits.rotate_left_32((b ^ c ^ d) + a + x5 + u32(0xfffa3942), 4) + d = a + bits.rotate_left_32((a ^ b ^ c) + d + x8 + u32(0x8771f681), 11) + c = d + bits.rotate_left_32((d ^ a ^ b) + c + xb + u32(0x6d9d6122), 16) + b = c + bits.rotate_left_32((c ^ d ^ a) + b + xe + u32(0xfde5380c), 23) + a = b + bits.rotate_left_32((b ^ c ^ d) + a + x1 + u32(0xa4beea44), 4) + d = a + bits.rotate_left_32((a ^ b ^ c) + d + x4 + u32(0x4bdecfa9), 11) + c = d + bits.rotate_left_32((d ^ a ^ b) + c + x7 + u32(0xf6bb4b60), 16) + b = c + bits.rotate_left_32((c ^ d ^ a) + b + xa + u32(0xbebfbc70), 23) + a = b + bits.rotate_left_32((b ^ c ^ d) + a + xd + u32(0x289b7ec6), 4) + d = a + bits.rotate_left_32((a ^ b ^ c) + d + x0 + u32(0xeaa127fa), 11) + c = d + bits.rotate_left_32((d ^ a ^ b) + c + x3 + u32(0xd4ef3085), 16) + b = c + bits.rotate_left_32((c ^ d ^ a) + b + x6 + u32(0x04881d05), 23) + a = b + bits.rotate_left_32((b ^ c ^ d) + a + x9 + u32(0xd9d4d039), 4) + d = a + bits.rotate_left_32((a ^ b ^ c) + d + xc + u32(0xe6db99e5), 11) + c = d + bits.rotate_left_32((d ^ a ^ b) + c + xf + u32(0x1fa27cf8), 16) + b = c + bits.rotate_left_32((c ^ d ^ a) + b + x2 + u32(0xc4ac5665), 23) // round 4 - a = b + bits.rotate_left_32((c^(b|~d))+a+x0+u32(0xf4292244), 6) - d = a + bits.rotate_left_32((b^(a|~c))+d+x7+u32(0x432aff97), 10) - c = d + bits.rotate_left_32((a^(d|~b))+c+xe+u32(0xab9423a7), 15) - b = c + bits.rotate_left_32((d^(c|~a))+b+x5+u32(0xfc93a039), 21) - a = b + bits.rotate_left_32((c^(b|~d))+a+xc+u32(0x655b59c3), 6) - d = a + bits.rotate_left_32((b^(a|~c))+d+x3+u32(0x8f0ccc92), 10) - c = d + bits.rotate_left_32((a^(d|~b))+c+xa+u32(0xffeff47d), 15) - b = c + bits.rotate_left_32((d^(c|~a))+b+x1+u32(0x85845dd1), 21) - a = b + bits.rotate_left_32((c^(b|~d))+a+x8+u32(0x6fa87e4f), 6) - d = a + bits.rotate_left_32((b^(a|~c))+d+xf+u32(0xfe2ce6e0), 10) - c = d + bits.rotate_left_32((a^(d|~b))+c+x6+u32(0xa3014314), 15) - b = c + bits.rotate_left_32((d^(c|~a))+b+xd+u32(0x4e0811a1), 21) - a = b + bits.rotate_left_32((c^(b|~d))+a+x4+u32(0xf7537e82), 6) - d = a + bits.rotate_left_32((b^(a|~c))+d+xb+u32(0xbd3af235), 10) - c = d + bits.rotate_left_32((a^(d|~b))+c+x2+u32(0x2ad7d2bb), 15) - b = c + bits.rotate_left_32((d^(c|~a))+b+x9+u32(0xeb86d391), 21) + a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x0 + u32(0xf4292244), 6) + d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + x7 + u32(0x432aff97), 10) + c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + xe + u32(0xab9423a7), 15) + b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x5 + u32(0xfc93a039), 21) + a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + xc + u32(0x655b59c3), 6) + d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + x3 + u32(0x8f0ccc92), 10) + c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + xa + u32(0xffeff47d), 15) + b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x1 + u32(0x85845dd1), 21) + a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x8 + u32(0x6fa87e4f), 6) + d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + xf + u32(0xfe2ce6e0), 10) + c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + x6 + u32(0xa3014314), 15) + b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + xd + u32(0x4e0811a1), 21) + a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x4 + u32(0xf7537e82), 6) + d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + xb + u32(0xbd3af235), 10) + c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + x2 + u32(0x2ad7d2bb), 15) + b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x9 + u32(0xeb86d391), 21) // add saved state a += aa diff --git a/vlib/crypto/rand/rand_linux.c.v b/vlib/crypto/rand/rand_linux.c.v index d3da4e02a..5cacccf0e 100644 --- a/vlib/crypto/rand/rand_linux.c.v +++ b/vlib/crypto/rand/rand_linux.c.v @@ -4,6 +4,7 @@ module rand #include + const ( read_batch_size = 256 ) @@ -15,7 +16,11 @@ pub fn read(bytes_needed int) ?[]byte { mut remaining_bytes := bytes_needed // getrandom syscall wont block if requesting <= 256 bytes for bytes_read < bytes_needed { - batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes } + batch_size := if remaining_bytes > rand.read_batch_size { + rand.read_batch_size + } else { + remaining_bytes + } rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } if rbytes == -1 { unsafe { free(buffer) } @@ -23,12 +28,12 @@ pub fn read(bytes_needed int) ?[]byte { } bytes_read += rbytes } - return unsafe {buffer.vbytes(bytes_needed)} + return unsafe { buffer.vbytes(bytes_needed) } } fn getrandom(bytes_needed int, buffer voidptr) int { - if bytes_needed > read_batch_size { - panic('getrandom() dont request more than $read_batch_size bytes at once.') + if bytes_needed > rand.read_batch_size { + panic('getrandom() dont request more than $rand.read_batch_size bytes at once.') } return unsafe { C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0) } } diff --git a/vlib/crypto/rand/rand_solaris.c.v b/vlib/crypto/rand/rand_solaris.c.v index d2e84faf3..cec2e9a84 100644 --- a/vlib/crypto/rand/rand_solaris.c.v +++ b/vlib/crypto/rand/rand_solaris.c.v @@ -6,7 +6,7 @@ module rand #include -fn C.getrandom(p byteptr, n size_t, flags u32) int +fn C.getrandom(p &byte, n size_t, flags u32) int const ( read_batch_size = 256 @@ -19,7 +19,11 @@ pub fn read(bytes_needed int) ?[]byte { mut remaining_bytes := bytes_needed // getrandom syscall wont block if requesting <= 256 bytes for bytes_read < bytes_needed { - batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes } + batch_size := if remaining_bytes > rand.read_batch_size { + rand.read_batch_size + } else { + remaining_bytes + } rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } if rbytes == -1 { unsafe { free(buffer) } @@ -27,12 +31,12 @@ pub fn read(bytes_needed int) ?[]byte { } bytes_read += rbytes } - return unsafe {buffer.vbytes(bytes_needed)} + return unsafe { buffer.vbytes(bytes_needed) } } fn v_getrandom(bytes_needed int, buffer voidptr) int { - if bytes_needed > read_batch_size { - panic('getrandom() dont request more than $read_batch_size bytes at once.') + if bytes_needed > rand.read_batch_size { + panic('getrandom() dont request more than $rand.read_batch_size bytes at once.') } return C.getrandom(buffer, bytes_needed, 0) } diff --git a/vlib/crypto/rand/rand_windows.c.v b/vlib/crypto/rand/rand_windows.c.v index a64dfc5a4..075a3eddd 100644 --- a/vlib/crypto/rand/rand_windows.c.v +++ b/vlib/crypto/rand/rand_windows.c.v @@ -15,10 +15,10 @@ const ( // read returns an array of `bytes_needed` random bytes read from the OS. pub fn read(bytes_needed int) ?[]byte { - mut buffer := []byte{ len: bytes_needed } + mut buffer := []byte{len: bytes_needed} // use bcrypt_use_system_preferred_rng because we passed null as algo - status := C.BCryptGenRandom(0, buffer.data, bytes_needed, bcrypt_use_system_preferred_rng) - if status != status_success { + status := C.BCryptGenRandom(0, buffer.data, bytes_needed, rand.bcrypt_use_system_preferred_rng) + if status != rand.status_success { return IError(&ReadError{}) } return buffer diff --git a/vlib/crypto/rc4/rc4_test.v b/vlib/crypto/rc4/rc4_test.v index d7ccc2028..dd9ac0f90 100644 --- a/vlib/crypto/rc4/rc4_test.v +++ b/vlib/crypto/rc4/rc4_test.v @@ -1,7 +1,6 @@ // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - import crypto.rc4 fn test_crypto_rc4() { diff --git a/vlib/crypto/sha1/sha1.v b/vlib/crypto/sha1/sha1.v index 2e4b6fe66..d792eb14f 100644 --- a/vlib/crypto/sha1/sha1.v +++ b/vlib/crypto/sha1/sha1.v @@ -36,13 +36,13 @@ mut: } fn (mut d Digest) reset() { - d.x = []byte{len: (chunk)} + d.x = []byte{len: sha1.chunk} d.h = []u32{len: (5)} - d.h[0] = u32(init0) - d.h[1] = u32(init1) - d.h[2] = u32(init2) - d.h[3] = u32(init3) - d.h[4] = u32(init4) + d.h[0] = u32(sha1.init0) + d.h[1] = u32(sha1.init1) + d.h[2] = u32(sha1.init2) + d.h[3] = u32(sha1.init3) + d.h[4] = u32(sha1.init4) d.nx = 0 d.len = 0 } @@ -64,7 +64,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int { if d.nx > 0 { n := copy(d.x[d.nx..], p) d.nx += n - if d.nx == chunk { + if d.nx == sha1.chunk { block(mut d, d.x) d.nx = 0 } @@ -74,8 +74,8 @@ pub fn (mut d Digest) write(p_ []byte) ?int { p = p[n..] } } - if p.len >= chunk { - n := p.len & ~(chunk - 1) + if p.len >= sha1.chunk { + n := p.len & ~(sha1.chunk - 1) block(mut d, p[..n]) if n >= p.len { p = [] @@ -117,7 +117,7 @@ fn (mut d Digest) checksum() []byte { len <<= 3 binary.big_endian_put_u64(mut tmp, len) d.write(tmp[..8]) or { panic(err) } - mut digest := []byte{len: (size)} + mut digest := []byte{len: sha1.size} binary.big_endian_put_u32(mut digest, d.h[0]) binary.big_endian_put_u32(mut digest[4..], d.h[1]) binary.big_endian_put_u32(mut digest[8..], d.h[2]) @@ -141,12 +141,12 @@ fn block(mut dig Digest, p []byte) { // size returns the size of the checksum in bytes. pub fn (d &Digest) size() int { - return size + return sha1.size } // block_size returns the block size of the checksum in bytes. pub fn (d &Digest) block_size() int { - return block_size + return sha1.block_size } // hexhash returns a hexadecimal SHA1 hash sum `string` of `s`. diff --git a/vlib/crypto/sha1/sha1_test.v b/vlib/crypto/sha1/sha1_test.v index fb11c7fe5..b52ea063a 100644 --- a/vlib/crypto/sha1/sha1_test.v +++ b/vlib/crypto/sha1/sha1_test.v @@ -1,7 +1,6 @@ // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - import crypto.sha1 fn test_crypto_sha1() { diff --git a/vlib/crypto/sha1/sha1block_generic.v b/vlib/crypto/sha1/sha1block_generic.v index 9f2f3b4b5..a0dc92cb7 100644 --- a/vlib/crypto/sha1/sha1block_generic.v +++ b/vlib/crypto/sha1/sha1block_generic.v @@ -42,7 +42,7 @@ fn block_generic(mut dig Digest, p_ []byte) { mut i := 0 for i < 16 { f := b & c | (~b) & d - t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k0) + t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k0) e = d d = c c = bits.rotate_left_32(b, 30) @@ -51,10 +51,10 @@ fn block_generic(mut dig Digest, p_ []byte) { i++ } for i < 20 { - tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf] + tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf] w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1)) f := b & c | (~b) & d - t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k0) + t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k0) e = d d = c c = bits.rotate_left_32(b, 30) @@ -63,10 +63,10 @@ fn block_generic(mut dig Digest, p_ []byte) { i++ } for i < 40 { - tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf] + tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf] w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1)) f := b ^ c ^ d - t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k1) + t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k1) e = d d = c c = bits.rotate_left_32(b, 30) @@ -75,10 +75,10 @@ fn block_generic(mut dig Digest, p_ []byte) { i++ } for i < 60 { - tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf] + tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf] w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1)) f := ((b | c) & d) | (b & c) - t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k2) + t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k2) e = d d = c c = bits.rotate_left_32(b, 30) @@ -87,10 +87,10 @@ fn block_generic(mut dig Digest, p_ []byte) { i++ } for i < 80 { - tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf] + tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf] w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1)) f := b ^ c ^ d - t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k3) + t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k3) e = d d = c c = bits.rotate_left_32(b, 30) diff --git a/vlib/crypto/sha256/sha256.v b/vlib/crypto/sha256/sha256.v index c8ea3a8e0..106df9495 100644 --- a/vlib/crypto/sha256/sha256.v +++ b/vlib/crypto/sha256/sha256.v @@ -50,25 +50,25 @@ mut: fn (mut d Digest) reset() { d.h = []u32{len: (8)} - d.x = []byte{len: (chunk)} + d.x = []byte{len: sha256.chunk} if !d.is224 { - d.h[0] = u32(init0) - d.h[1] = u32(init1) - d.h[2] = u32(init2) - d.h[3] = u32(init3) - d.h[4] = u32(init4) - d.h[5] = u32(init5) - d.h[6] = u32(init6) - d.h[7] = u32(init7) + d.h[0] = u32(sha256.init0) + d.h[1] = u32(sha256.init1) + d.h[2] = u32(sha256.init2) + d.h[3] = u32(sha256.init3) + d.h[4] = u32(sha256.init4) + d.h[5] = u32(sha256.init5) + d.h[6] = u32(sha256.init6) + d.h[7] = u32(sha256.init7) } else { - d.h[0] = u32(init0_224) - d.h[1] = u32(init1_224) - d.h[2] = u32(init2_224) - d.h[3] = u32(init3_224) - d.h[4] = u32(init4_224) - d.h[5] = u32(init5_224) - d.h[6] = u32(init6_224) - d.h[7] = u32(init7_224) + d.h[0] = u32(sha256.init0_224) + d.h[1] = u32(sha256.init1_224) + d.h[2] = u32(sha256.init2_224) + d.h[3] = u32(sha256.init3_224) + d.h[4] = u32(sha256.init4_224) + d.h[5] = u32(sha256.init5_224) + d.h[6] = u32(sha256.init6_224) + d.h[7] = u32(sha256.init7_224) } d.nx = 0 d.len = 0 @@ -98,7 +98,7 @@ fn (mut d Digest) write(p_ []byte) ?int { if d.nx > 0 { n := copy(d.x[d.nx..], p) d.nx += n - if d.nx == chunk { + if d.nx == sha256.chunk { block(mut d, d.x) d.nx = 0 } @@ -108,8 +108,8 @@ fn (mut d Digest) write(p_ []byte) ?int { p = p[n..] } } - if p.len >= chunk { - n := p.len & ~(chunk - 1) + if p.len >= sha256.chunk { + n := p.len & ~(sha256.chunk - 1) block(mut d, p[..n]) if n >= p.len { p = [] @@ -130,7 +130,7 @@ pub fn (d &Digest) sum(b_in []byte) []byte { hash := d0.checksum() mut b_out := b_in.clone() if d0.is224 { - for b in hash[..size224] { + for b in hash[..sha256.size224] { b_out << b } } else { @@ -158,7 +158,7 @@ fn (mut d Digest) checksum() []byte { if d.nx != 0 { panic('d.nx != 0') } - mut digest := []byte{len: (size)} + mut digest := []byte{len: sha256.size} binary.big_endian_put_u32(mut digest, d.h[0]) binary.big_endian_put_u32(mut digest[4..], d.h[1]) binary.big_endian_put_u32(mut digest[8..], d.h[2]) @@ -190,8 +190,8 @@ pub fn sum224(data []byte) []byte { mut d := new224() d.write(data) or { panic(err) } sum := d.checksum() - sum224 := []byte{len: (size224)} - copy(sum224, sum[..size224]) + sum224 := []byte{len: sha256.size224} + copy(sum224, sum[..sha256.size224]) return sum224 } @@ -204,14 +204,14 @@ fn block(mut dig Digest, p []byte) { // size returns the size of the checksum in bytes. pub fn (d &Digest) size() int { if !d.is224 { - return size + return sha256.size } - return size224 + return sha256.size224 } // block_size returns the block size of the checksum in bytes. pub fn (d &Digest) block_size() int { - return block_size + return sha256.block_size } // hexhash returns a hexadecimal SHA256 hash sum `string` of `s`. diff --git a/vlib/crypto/sha256/sha256_test.v b/vlib/crypto/sha256/sha256_test.v index 0f4c5ed64..be674eaa8 100644 --- a/vlib/crypto/sha256/sha256_test.v +++ b/vlib/crypto/sha256/sha256_test.v @@ -1,12 +1,10 @@ // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - import crypto.sha256 fn test_crypto_sha256() { - assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() == - 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727' + assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727' } fn test_crypto_sha256_writer() { diff --git a/vlib/crypto/sha256/sha256block_generic.v b/vlib/crypto/sha256/sha256block_generic.v index 1a20a5438..e3989ccdb 100644 --- a/vlib/crypto/sha256/sha256block_generic.v +++ b/vlib/crypto/sha256/sha256block_generic.v @@ -115,9 +115,9 @@ fn block_generic(mut dig Digest, p_ []byte) { for i in 0 .. 64 { t1 := h + ((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) + - ((e & f) ^ (~e & g)) + u32(_k[i]) + w[i] - t2 := ((bits.rotate_left_32(a, -2)) ^ - (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) + + ((e & f) ^ (~e & g)) + u32(sha256._k[i]) + w[i] + t2 := + ((bits.rotate_left_32(a, -2)) ^ (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) + ((a & b) ^ (a & c) ^ (b & c)) h = g g = f diff --git a/vlib/crypto/sha512/sha512.v b/vlib/crypto/sha512/sha512.v index 0ddff114e..dd5d3a928 100644 --- a/vlib/crypto/sha512/sha512.v +++ b/vlib/crypto/sha512/sha512.v @@ -72,47 +72,47 @@ mut: fn (mut d Digest) reset() { d.h = []u64{len: (8)} - d.x = []byte{len: (chunk)} + d.x = []byte{len: sha512.chunk} match d.function { .sha384 { - d.h[0] = init0_384 - d.h[1] = init1_384 - d.h[2] = init2_384 - d.h[3] = init3_384 - d.h[4] = init4_384 - d.h[5] = init5_384 - d.h[6] = init6_384 - d.h[7] = init7_384 + d.h[0] = sha512.init0_384 + d.h[1] = sha512.init1_384 + d.h[2] = sha512.init2_384 + d.h[3] = sha512.init3_384 + d.h[4] = sha512.init4_384 + d.h[5] = sha512.init5_384 + d.h[6] = sha512.init6_384 + d.h[7] = sha512.init7_384 } .sha512_224 { - d.h[0] = init0_224 - d.h[1] = init1_224 - d.h[2] = init2_224 - d.h[3] = init3_224 - d.h[4] = init4_224 - d.h[5] = init5_224 - d.h[6] = init6_224 - d.h[7] = init7_224 + d.h[0] = sha512.init0_224 + d.h[1] = sha512.init1_224 + d.h[2] = sha512.init2_224 + d.h[3] = sha512.init3_224 + d.h[4] = sha512.init4_224 + d.h[5] = sha512.init5_224 + d.h[6] = sha512.init6_224 + d.h[7] = sha512.init7_224 } .sha512_256 { - d.h[0] = init0_256 - d.h[1] = init1_256 - d.h[2] = init2_256 - d.h[3] = init3_256 - d.h[4] = init4_256 - d.h[5] = init5_256 - d.h[6] = init6_256 - d.h[7] = init7_256 + d.h[0] = sha512.init0_256 + d.h[1] = sha512.init1_256 + d.h[2] = sha512.init2_256 + d.h[3] = sha512.init3_256 + d.h[4] = sha512.init4_256 + d.h[5] = sha512.init5_256 + d.h[6] = sha512.init6_256 + d.h[7] = sha512.init7_256 } else { - d.h[0] = init0 - d.h[1] = init1 - d.h[2] = init2 - d.h[3] = init3 - d.h[4] = init4 - d.h[5] = init5 - d.h[6] = init6 - d.h[7] = init7 + d.h[0] = sha512.init0 + d.h[1] = sha512.init1 + d.h[2] = sha512.init2 + d.h[3] = sha512.init3 + d.h[4] = sha512.init4 + d.h[5] = sha512.init5 + d.h[6] = sha512.init6 + d.h[7] = sha512.init7 } } d.nx = 0 @@ -157,7 +157,7 @@ fn (mut d Digest) write(p_ []byte) ?int { if d.nx > 0 { n := copy(d.x[d.nx..], p) d.nx += n - if d.nx == chunk { + if d.nx == sha512.chunk { block(mut d, d.x) d.nx = 0 } @@ -167,8 +167,8 @@ fn (mut d Digest) write(p_ []byte) ?int { p = p[n..] } } - if p.len >= chunk { - n := p.len & ~(chunk - 1) + if p.len >= sha512.chunk { + n := p.len & ~(sha512.chunk - 1) block(mut d, p[..n]) if n >= p.len { p = [] @@ -190,17 +190,17 @@ fn (d &Digest) sum(b_in []byte) []byte { mut b_out := b_in.clone() match d0.function { .sha384 { - for b in hash[..size384] { + for b in hash[..sha512.size384] { b_out << b } } .sha512_224 { - for b in hash[..size224] { + for b in hash[..sha512.size224] { b_out << b } } .sha512_256 { - for b in hash[..size256] { + for b in hash[..sha512.size256] { b_out << b } } @@ -231,7 +231,7 @@ fn (mut d Digest) checksum() []byte { if d.nx != 0 { panic('d.nx != 0') } - mut digest := []byte{len: (size)} + mut digest := []byte{len: sha512.size} binary.big_endian_put_u64(mut digest, d.h[0]) binary.big_endian_put_u64(mut digest[8..], d.h[1]) binary.big_endian_put_u64(mut digest[16..], d.h[2]) @@ -257,8 +257,8 @@ pub fn sum384(data []byte) []byte { mut d := new_digest(.sha384) d.write(data) or { panic(err) } sum := d.checksum() - sum384 := []byte{len: (size384)} - copy(sum384, sum[..size384]) + sum384 := []byte{len: sha512.size384} + copy(sum384, sum[..sha512.size384]) return sum384 } @@ -267,8 +267,8 @@ pub fn sum512_224(data []byte) []byte { mut d := new_digest(.sha512_224) d.write(data) or { panic(err) } sum := d.checksum() - sum224 := []byte{len: (size224)} - copy(sum224, sum[..size224]) + sum224 := []byte{len: sha512.size224} + copy(sum224, sum[..sha512.size224]) return sum224 } @@ -277,8 +277,8 @@ pub fn sum512_256(data []byte) []byte { mut d := new_digest(.sha512_256) d.write(data) or { panic(err) } sum := d.checksum() - sum256 := []byte{len: (size256)} - copy(sum256, sum[..size256]) + sum256 := []byte{len: sha512.size256} + copy(sum256, sum[..sha512.size256]) return sum256 } @@ -291,16 +291,16 @@ fn block(mut dig Digest, p []byte) { // size returns the size of the checksum in bytes. pub fn (d &Digest) size() int { match d.function { - .sha512_224 { return size224 } - .sha512_256 { return size256 } - .sha384 { return size384 } - else { return size } + .sha512_224 { return sha512.size224 } + .sha512_256 { return sha512.size256 } + .sha384 { return sha512.size384 } + else { return sha512.size } } } // block_size returns the block size of the checksum in bytes. pub fn (d &Digest) block_size() int { - return block_size + return sha512.block_size } // hexhash returns a hexadecimal SHA512 hash sum `string` of `s`. diff --git a/vlib/encoding/base64/base64.v b/vlib/encoding/base64/base64.v index cfba97bac..55fbe67a0 100644 --- a/vlib/encoding/base64/base64.v +++ b/vlib/encoding/base64/base64.v @@ -55,7 +55,7 @@ pub fn encode_str(data string) string { // alloc_and_encode is a private function that allocates and encodes data into a string // Used by encode and encode_str -fn alloc_and_encode(src byteptr, len int) string { +fn alloc_and_encode(src &byte, len int) string { size := 4 * ((len + 2) / 3) if size <= 0 { return '' @@ -107,7 +107,7 @@ pub fn url_encode_str(data string) string { // Please note: The `buffer` should be large enough (i.e. 3/4 of the data.len, or larger) // to hold the decoded data. // Please note: This function does NOT allocate new memory, and is thus suitable for handling very large strings. -pub fn decode_in_buffer(data &string, buffer byteptr) int { +pub fn decode_in_buffer(data &string, buffer &byte) int { mut padding := 0 if data.ends_with('=') { if data.ends_with('==') { @@ -125,8 +125,8 @@ pub fn decode_in_buffer(data &string, buffer byteptr) int { mut b := &byte(0) mut d := &byte(0) unsafe { - d = byteptr(data.str) - b = byteptr(buffer) + d = &byte(data.str) + b = &byte(buffer) } for i < input_length { mut char_a := 0 @@ -165,7 +165,7 @@ pub fn decode_in_buffer(data &string, buffer byteptr) int { // encode_in_buffer returns the size of the encoded data in the buffer. // Please note: The buffer should be large enough (i.e. 4/3 of the data.len, or larger) to hold the encoded data. // Please note: The function does NOT allocate new memory, and is suitable for handling very large strings. -pub fn encode_in_buffer(data []byte, buffer byteptr) int { +pub fn encode_in_buffer(data []byte, buffer &byte) int { return encode_from_buffer(buffer, data.data, data.len) } @@ -173,16 +173,16 @@ pub fn encode_in_buffer(data []byte, buffer byteptr) int { // and write the bytes into `dest`. // Please note: The `dest` buffer should be large enough (i.e. 4/3 of the src_len, or larger) to hold the encoded data. // Please note: This function is for internal base64 encoding -fn encode_from_buffer(dest byteptr, src byteptr, src_len int) int { +fn encode_from_buffer(dest &byte, src &byte, src_len int) int { input_length := src_len output_length := 4 * ((input_length + 2) / 3) mut i := 0 mut j := 0 - mut d := src - mut b := dest - mut etable := byteptr(base64.enc_table.str) + mut d := unsafe { src } + mut b := unsafe { dest } + mut etable := base64.enc_table.str for i < input_length { mut octet_a := 0 mut octet_b := 0 diff --git a/vlib/encoding/base64/base64_test.v b/vlib/encoding/base64/base64_test.v index 34ff1ead3..e461330b9 100644 --- a/vlib/encoding/base64/base64_test.v +++ b/vlib/encoding/base64/base64_test.v @@ -6,12 +6,11 @@ struct TestPair { } const ( - pairs = [ + pairs = [ // RFC 3548 examples TestPair{'\x14\xfb\x9c\x03\xd9\x7e', 'FPucA9l+'}, TestPair{'\x14\xfb\x9c\x03\xd9', 'FPucA9k='}, TestPair{'\x14\xfb\x9c\x03', 'FPucAw=='}, - // RFC 4648 examples TestPair{'', ''}, TestPair{'f', 'Zg=='}, @@ -20,7 +19,6 @@ const ( TestPair{'foob', 'Zm9vYg=='}, TestPair{'fooba', 'Zm9vYmE='}, TestPair{'foobar', 'Zm9vYmFy'}, - // Wikipedia examples TestPair{'sure.', 'c3VyZS4='}, TestPair{'sure', 'c3VyZQ=='}, @@ -32,11 +30,7 @@ const ( TestPair{'sure.', 'c3VyZS4='}, ] - man_pair = TestPair{ - 'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.', - 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=' - } - + man_pair = TestPair{'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.', 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='} ) fn test_decode() { @@ -50,7 +44,7 @@ fn test_decode() { for i, p in pairs { got := base64.decode(p.encoded) if got != p.decoded.bytes() { - eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}') + eprintln('pairs[$i]: expected = $p.decoded, got = $got') assert false } } @@ -67,7 +61,7 @@ fn test_decode_str() { for i, p in pairs { got := base64.decode_str(p.encoded) if got != p.decoded { - eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}') + eprintln('pairs[$i]: expected = $p.decoded, got = $got') assert false } } @@ -79,7 +73,7 @@ fn test_encode() { for i, p in pairs { got := base64.encode(p.decoded.bytes()) if got != p.encoded { - eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}') + eprintln('pairs[$i]: expected = $p.encoded, got = $got') assert false } } @@ -91,7 +85,7 @@ fn test_encode_str() { for i, p in pairs { got := base64.encode_str(p.decoded) if got != p.encoded { - eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}') + eprintln('pairs[$i]: expected = $p.encoded, got = $got') assert false } } @@ -108,31 +102,31 @@ fn test_url_encode_str() { } fn test_url_decode() { - test := base64.url_decode("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ") + test := base64.url_decode('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ') assert test == 'Hello Base64Url encoding!'.bytes() } fn test_url_decode_str() { - test := base64.url_decode_str("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ") + test := base64.url_decode_str('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ') assert test == 'Hello Base64Url encoding!' } fn test_encode_null_byte() { - assert base64.encode([byte(`A`) 0 `C`]) == 'QQBD' + assert base64.encode([byte(`A`), 0, `C`]) == 'QQBD' } fn test_encode_null_byte_str() { // While this works, bytestr() does a memcpy - s := [byte(`A`) 0 `C`].bytestr() + s := [byte(`A`), 0, `C`].bytestr() assert base64.encode_str(s) == 'QQBD' } fn test_decode_null_byte() { - assert base64.decode('QQBD') == [byte(`A`) 0 `C`] + assert base64.decode('QQBD') == [byte(`A`), 0, `C`] } fn test_decode_null_byte_str() { // While this works, bytestr() does a memcpy - s := [byte(`A`) 0 `C`].bytestr() + s := [byte(`A`), 0, `C`].bytestr() assert base64.decode_str('QQBD') == s } diff --git a/vlib/encoding/binary/binary.v b/vlib/encoding/binary/binary.v index 3c46d1baa..d7fe29834 100644 --- a/vlib/encoding/binary/binary.v +++ b/vlib/encoding/binary/binary.v @@ -2,98 +2,99 @@ // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. module binary + // Little Endian [inline] pub fn little_endian_u16(b []byte) u16 { _ = b[1] // bounds check - return u16(b[0]) | (u16(b[1])<>u16(8)) + b[1] = byte(v >> u16(8)) } [inline] pub fn little_endian_u32(b []byte) u32 { _ = b[3] // bounds check - return u32(b[0]) | (u32(b[1])<>u32(8)) - b[2] = byte(v>>u32(16)) - b[3] = byte(v>>u32(24)) + b[1] = byte(v >> u32(8)) + b[2] = byte(v >> u32(16)) + b[3] = byte(v >> u32(24)) } [inline] pub fn little_endian_u64(b []byte) u64 { _ = b[7] // bounds check - return u64(b[0]) | (u64(b[1])<>u64(8)) - b[2] = byte(v>>u64(16)) - b[3] = byte(v>>u64(24)) - b[4] = byte(v>>u64(32)) - b[5] = byte(v>>u64(40)) - b[6] = byte(v>>u64(48)) - b[7] = byte(v>>u64(56)) + b[1] = byte(v >> u64(8)) + b[2] = byte(v >> u64(16)) + b[3] = byte(v >> u64(24)) + b[4] = byte(v >> u64(32)) + b[5] = byte(v >> u64(40)) + b[6] = byte(v >> u64(48)) + b[7] = byte(v >> u64(56)) } // Big Endian [inline] pub fn big_endian_u16(b []byte) u16 { _ = b[1] // bounds check - return u16(b[1]) | (u16(b[0])<>u16(8)) + b[0] = byte(v >> u16(8)) b[1] = byte(v) } [inline] pub fn big_endian_u32(b []byte) u32 { _ = b[3] // bounds check - return u32(b[3]) | (u32(b[2])<>u32(24)) - b[1] = byte(v>>u32(16)) - b[2] = byte(v>>u32(8)) + b[0] = byte(v >> u32(24)) + b[1] = byte(v >> u32(16)) + b[2] = byte(v >> u32(8)) b[3] = byte(v) } [inline] pub fn big_endian_u64(b []byte) u64 { _ = b[7] // bounds check - return u64(b[7]) | (u64(b[6])<>u64(56)) - b[1] = byte(v>>u64(48)) - b[2] = byte(v>>u64(40)) - b[3] = byte(v>>u64(32)) - b[4] = byte(v>>u64(24)) - b[5] = byte(v>>u64(16)) - b[6] = byte(v>>u64(8)) + b[0] = byte(v >> u64(56)) + b[1] = byte(v >> u64(48)) + b[2] = byte(v >> u64(40)) + b[3] = byte(v >> u64(32)) + b[4] = byte(v >> u64(24)) + b[5] = byte(v >> u64(16)) + b[6] = byte(v >> u64(8)) b[7] = byte(v) } diff --git a/vlib/encoding/csv/reader_test.v b/vlib/encoding/csv/reader_test.v index 3644bfe4e..86ecf96de 100644 --- a/vlib/encoding/csv/reader_test.v +++ b/vlib/encoding/csv/reader_test.v @@ -5,9 +5,7 @@ fn test_encoding_csv_reader() { mut csv_reader := csv.new_reader(data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -35,9 +33,7 @@ fn test_line_break_lf() { mut csv_reader := csv.new_reader(lf_data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -55,9 +51,7 @@ fn test_line_break_cr() { mut csv_reader := csv.new_reader(cr_data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -75,9 +69,7 @@ fn test_line_break_crlf() { mut csv_reader := csv.new_reader(crlf_data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -95,9 +87,7 @@ fn test_no_line_ending() { mut csv_reader := csv.new_reader(data) mut row_count := 0 for { - csv_reader.read() or { - break - } + csv_reader.read() or { break } row_count++ } assert row_count == 2 @@ -108,9 +98,7 @@ fn test_last_field_empty() { mut csv_reader := csv.new_reader(data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -133,9 +121,7 @@ fn test_empty_line() { mut csv_reader := csv.new_reader(data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -160,9 +146,7 @@ fn test_field_multiple_line() { mut csv_reader := csv.new_reader(data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'name' @@ -182,9 +166,7 @@ fn test_field_quotes_for_parts() { mut csv_reader := csv.new_reader(data) mut row_count := 0 for { - row := csv_reader.read() or { - break - } + row := csv_reader.read() or { break } row_count++ if row_count == 1 { assert row[0] == 'a1' diff --git a/vlib/eventbus/eventbus.v b/vlib/eventbus/eventbus.v index fbcdf7478..69cf1188a 100644 --- a/vlib/eventbus/eventbus.v +++ b/vlib/eventbus/eventbus.v @@ -83,7 +83,7 @@ pub fn (mut s Subscriber) subscribe_method(name string, handler EventHandlerFn, } } -// unsubscribe_method unsubscribe a receiver for only one method +// unsubscribe_method unsubscribe a receiver for only one method pub fn (mut s Subscriber) unsubscribe_method(name string, receiver voidptr) { s.registry.events = s.registry.events.filter(!(it.name == name && it.receiver == receiver)) } @@ -105,8 +105,6 @@ pub fn (s &Subscriber) is_subscribed(name string) bool { return s.registry.check_subscriber(name) } - - // is_subscribed_method checks whether a receiver was already subscribed for any events pub fn (s &Subscriber) is_subscribed_method(name string, receiver voidptr) bool { return s.registry.events.any(it.name == name && it.receiver == receiver) diff --git a/vlib/glm/glm_test.v b/vlib/glm/glm_test.v index c7888bff0..c2a426671 100644 --- a/vlib/glm/glm_test.v +++ b/vlib/glm/glm_test.v @@ -12,7 +12,7 @@ fn cmp(a f32, b f32) bool { fn test_ortho() { projection := glm.ortho(0, 200, 400, 0) $if debug { - println(unsafe {projection.data[0]}) + println(unsafe { projection.data[0] }) } unsafe { assert cmp(projection.data[0], 0.01) @@ -73,7 +73,7 @@ fn test_rotate() { m2 = glm.rotate(1, glm.vec3(0, 1, 0), m2) mut same := true for i in 0 .. 15 { - if unsafe {m1.data[i]} != unsafe {m2.data[i]} { + if unsafe { m1.data[i] } != unsafe { m2.data[i] } { same = false } } @@ -150,6 +150,6 @@ fn test_mult1() { b := glm.Mat4{bdata} a = glm.mult(a, b) for i in 0 .. 15 { - assert unsafe {a.data[i]} == unsafe {expected[i]} + assert unsafe { a.data[i] } == unsafe { expected[i] } } } diff --git a/vlib/gx/color.v b/vlib/gx/color.v index fa7252c1d..5d7566b2c 100644 --- a/vlib/gx/color.v +++ b/vlib/gx/color.v @@ -1,92 +1,92 @@ module gx pub const ( - blue = Color{ + blue = Color{ r: 0 g: 0 b: 255 } - red = Color{ + red = Color{ r: 255 g: 0 b: 0 } - green = Color{ + green = Color{ r: 0 g: 255 b: 0 } - yellow = Color{ + yellow = Color{ r: 255 g: 255 b: 0 } - orange = Color{ + orange = Color{ r: 255 g: 165 b: 0 } - purple = Color{ + purple = Color{ r: 128 g: 0 b: 128 } - black = Color{ + black = Color{ r: 0 g: 0 b: 0 } - gray = Color{ + gray = Color{ r: 128 g: 128 b: 128 } - indigo = Color{ + indigo = Color{ r: 75 g: 0 b: 130 } - pink = Color{ + pink = Color{ r: 255 g: 192 b: 203 } - violet = Color{ + violet = Color{ r: 238 g: 130 b: 238 } - white = Color{ + white = Color{ r: 255 g: 255 b: 255 } - dark_blue = Color{ + dark_blue = Color{ r: 0 g: 0 b: 139 } - dark_gray = Color{ + dark_gray = Color{ r: 169 g: 169 b: 169 } - dark_green = Color{ + dark_green = Color{ r: 0 g: 100 b: 0 } - dark_red = Color{ + dark_red = Color{ r: 139 g: 0 b: 0 } - light_blue = Color{ + light_blue = Color{ r: 173 g: 216 b: 230 } - light_gray = Color{ + light_gray = Color{ r: 211 g: 211 b: 211 @@ -96,7 +96,7 @@ pub const ( g: 238 b: 144 } - light_red = Color{ + light_red = Color{ r: 255 g: 204 b: 203 @@ -118,7 +118,7 @@ pub fn hex(color int) Color { r: byte((color >> 24) & 0xFF) g: byte((color >> 16) & 0xFF) b: byte((color >> 8) & 0xFF) - a: byte((color) & 0xFF) + a: byte(color & 0xFF) } } @@ -197,7 +197,7 @@ pub fn (c Color) bgra8() int { return (int(c.b) << 24) + (int(c.g) << 16) + (int(c.r) << 8) + int(c.a) } -// abgr8 - convert a color value to an int in the ABGR8 order. +// abgr8 - convert a color value to an int in the ABGR8 order. // see https://developer.apple.com/documentation/coreimage/ciformat [inline] pub fn (c Color) abgr8() int { @@ -205,7 +205,7 @@ pub fn (c Color) abgr8() int { } const ( - string_colors = { + string_colors = map{ 'black': black 'blue': blue 'red': red @@ -213,5 +213,5 @@ const ( ) pub fn color_from_string(s string) Color { - return string_colors[s] + return gx.string_colors[s] } diff --git a/vlib/gx/image.v b/vlib/gx/image.v index d5ef44986..4a20ea1fd 100644 --- a/vlib/gx/image.v +++ b/vlib/gx/image.v @@ -2,7 +2,7 @@ module gx pub struct Image { mut: - obj voidptr + obj voidptr pub: id int width int diff --git a/vlib/hash/crc32/crc32.v b/vlib/hash/crc32/crc32.v index 0333f86a4..d29aa124b 100644 --- a/vlib/hash/crc32/crc32.v +++ b/vlib/hash/crc32/crc32.v @@ -23,10 +23,10 @@ mut: table []u32 } -fn(mut c Crc32) generate_table(poly int) { - for i in 0..256 { +fn (mut c Crc32) generate_table(poly int) { + for i in 0 .. 256 { mut crc := u32(i) - for _ in 0..8 { + for _ in 0 .. 8 { if crc & u32(1) == u32(1) { crc = (crc >> 1) ^ u32(poly) } else { @@ -37,15 +37,15 @@ fn(mut c Crc32) generate_table(poly int) { } } -fn(c &Crc32) sum32(b []byte) u32 { +fn (c &Crc32) sum32(b []byte) u32 { mut crc := ~u32(0) - for i in 0..b.len { - crc = c.table[byte(crc)^b[i]] ^ (crc >> 8) + for i in 0 .. b.len { + crc = c.table[byte(crc) ^ b[i]] ^ (crc >> 8) } return ~crc } -pub fn(c &Crc32) checksum(b []byte) u32 { +pub fn (c &Crc32) checksum(b []byte) u32 { return c.sum32(b) } @@ -58,6 +58,6 @@ pub fn new(poly int) &Crc32 { // calculate crc32 using ieee pub fn sum(b []byte) u32 { - c := new(int(ieee)) + c := new(int(crc32.ieee)) return c.sum32(b) } diff --git a/vlib/hash/fnv1a/fnv1a.v b/vlib/hash/fnv1a/fnv1a.v index 83cca7c09..275c8a238 100644 --- a/vlib/hash/fnv1a/fnv1a.v +++ b/vlib/hash/fnv1a/fnv1a.v @@ -1,44 +1,44 @@ module fnv1a const ( - fnv64_prime = u64(1099511628211) + fnv64_prime = u64(1099511628211) fnv64_offset_basis = u64(14695981039346656037) fnv32_offset_basis = u32(2166136261) - fnv32_prime = u32(16777619) + fnv32_prime = u32(16777619) ) [inline] pub fn sum32_string(data string) u32 { - mut hash := fnv32_offset_basis - for i in 0..data.len { - hash = (hash ^ u32(data[i])) * fnv32_prime + mut hash := fnv1a.fnv32_offset_basis + for i in 0 .. data.len { + hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime } return hash } [inline] pub fn sum32(data []byte) u32 { - mut hash := fnv32_offset_basis - for i in 0..data.len { - hash = (hash ^ u32(data[i])) * fnv32_prime + mut hash := fnv1a.fnv32_offset_basis + for i in 0 .. data.len { + hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime } return hash } [inline] pub fn sum64_string(data string) u64 { - mut hash := fnv64_offset_basis - for i in 0..data.len { - hash = (hash ^ u64(data[i])) * fnv64_prime + mut hash := fnv1a.fnv64_offset_basis + for i in 0 .. data.len { + hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime } return hash } [inline] pub fn sum64(data []byte) u64 { - mut hash := fnv64_offset_basis - for i in 0..data.len { - hash = (hash ^ u64(data[i])) * fnv64_prime + mut hash := fnv1a.fnv64_offset_basis + for i in 0 .. data.len { + hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime } return hash } diff --git a/vlib/hash/hash_wyhash_test.v b/vlib/hash/hash_wyhash_test.v index a3ea54010..1abfbbdd7 100644 --- a/vlib/hash/hash_wyhash_test.v +++ b/vlib/hash/hash_wyhash_test.v @@ -7,18 +7,10 @@ struct WyHashTest { } fn test_wyhash() { - tests := [WyHashTest{ - '',0,0x0}, - WyHashTest{ - 'v',1,0xc72a8f8bdfdd82}, - WyHashTest{ - 'is',2,0xa1099c1c58fc13e}, - WyHashTest{ - 'the best',3,0x1b1215ef0b0b94c}, - WyHashTest{ - 'abcdefghijklmnopqrstuvwxyz',4,0x6db0e773d1503fac}, - WyHashTest{ - 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5,0xe062dfda99413626}, + tests := [WyHashTest{'', 0, 0x0}, WyHashTest{'v', 1, 0xc72a8f8bdfdd82}, + WyHashTest{'is', 2, 0xa1099c1c58fc13e}, WyHashTest{'the best', 3, 0x1b1215ef0b0b94c}, + WyHashTest{'abcdefghijklmnopqrstuvwxyz', 4, 0x6db0e773d1503fac}, + WyHashTest{'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5, 0xe062dfda99413626}, ] for test in tests { got := wyhash.sum64(test.s.bytes(), test.seed) diff --git a/vlib/hash/wyhash.c.v b/vlib/hash/wyhash.c.v index acd6eb8ab..0c55e95da 100644 --- a/vlib/hash/wyhash.c.v +++ b/vlib/hash/wyhash.c.v @@ -2,11 +2,11 @@ module hash //#flag -I @VEXEROOT/thirdparty/wyhash //#include "wyhash.h" -fn C.wyhash(byteptr, u64, u64, &u64) u64 +fn C.wyhash(&byte, u64, u64, &u64) u64 fn C.wyhash64(u64, u64) u64 [inline] -pub fn wyhash_c(key byteptr, len u64, seed u64) u64 { +pub fn wyhash_c(key &byte, len u64, seed u64) u64 { return C.wyhash(key, len, seed, &u64(C._wyp)) } diff --git a/vlib/hash/wyhash.v b/vlib/hash/wyhash.v index c25934aff..160c8f1b7 100644 --- a/vlib/hash/wyhash.v +++ b/vlib/hash/wyhash.v @@ -30,39 +30,46 @@ pub fn sum64_string(key string, seed u64) u64 { [inline] pub fn sum64(key []byte, seed u64) u64 { - return wyhash64(byteptr(key.data), u64(key.len), seed) + return wyhash64(&byte(key.data), u64(key.len), seed) } [inline] -fn wyhash64(key byteptr, len u64, seed_ u64) u64 { +fn wyhash64(key &byte, len u64, seed_ u64) u64 { if len == 0 { return 0 } - mut p := key + mut p := unsafe { key } mut seed := seed_ mut i := len & 63 - seed = unsafe{match i { - 0...3 { - wymum(wyr3(p, i) ^ seed ^ wyp0, seed ^ wyp1) + seed = unsafe { + match i { + 0...3 { + wymum(wyr3(p, i) ^ seed ^ hash.wyp0, seed ^ hash.wyp1) + } + 4...8 { + wymum(wyr4(p) ^ seed ^ hash.wyp0, wyr4(p + i - 4) ^ seed ^ hash.wyp1) + } + 9...16 { + wymum(wyr8(p) ^ seed ^ hash.wyp0, wyr8(p + i - 8) ^ seed ^ hash.wyp1) + } + 17...24 { + wymum(wyr8(p) ^ seed ^ hash.wyp0, wyr8(p + 8) ^ seed ^ hash.wyp1) ^ wymum(wyr8(p + i - 8) ^ seed ^ hash.wyp2, + seed ^ hash.wyp3) + } + 25...32 { + wymum(wyr8(p) ^ seed ^ hash.wyp0, wyr8(p + 8) ^ seed ^ hash.wyp1) ^ wymum(wyr8(p + + 16) ^ seed ^ hash.wyp2, wyr8(p + i - 8) ^ seed ^ hash.wyp3) + } + else { + wymum(wyr8(p) ^ seed ^ hash.wyp0, wyr8(p + 8) ^ seed ^ hash.wyp1) ^ wymum(wyr8(p + + 16) ^ seed ^ hash.wyp2, wyr8(p + 24) ^ seed ^ hash.wyp3) ^ wymum(wyr8(p + i - 32) ^ seed ^ hash.wyp1, + wyr8(p + i - 24) ^ seed ^ hash.wyp2) ^ wymum(wyr8(p + i - 16) ^ seed ^ hash.wyp3, + wyr8(p + i - 8) ^ seed ^ hash.wyp0) + } } - 4...8 { - wymum(wyr4(p) ^ seed ^ wyp0, wyr4(p + i - 4) ^ seed ^ wyp1) - } - 9...16 { - wymum(wyr8(p) ^ seed ^ wyp0, wyr8(p + i - 8) ^ seed ^ wyp1) - } - 17...24 { - wymum(wyr8(p) ^ seed ^ wyp0, wyr8(p + 8) ^ seed ^ wyp1) ^ wymum(wyr8(p + i - 8) ^ seed ^ wyp2, seed ^ wyp3) - } - 25...32 { - wymum(wyr8(p) ^ seed ^ wyp0, wyr8(p + 8) ^ seed ^ wyp1) ^ wymum(wyr8(p + 16) ^ seed ^ wyp2, wyr8(p + i - 8) ^ seed ^ wyp3) - } - else { - wymum(wyr8(p) ^ seed ^ wyp0, wyr8(p + 8) ^ seed ^ wyp1) ^ wymum(wyr8(p + 16) ^ seed ^ wyp2, wyr8(p + 24) ^ seed ^ wyp3) ^ wymum(wyr8(p + i - 32) ^ seed ^ wyp1, wyr8(p + i - 24) ^ seed ^ wyp2) ^ wymum(wyr8(p + i - 16) ^ seed ^ wyp3, wyr8(p + i - 8) ^ seed ^ wyp0) - } - }} + } if i == len { - return wymum(seed, len ^ wyp4) + return wymum(seed, len ^ hash.wyp4) } mut see1 := seed mut see2 := seed @@ -70,19 +77,19 @@ fn wyhash64(key byteptr, len u64, seed_ u64) u64 { unsafe { p = p + i for i = len - i; i >= 64; i -= 64 { - seed = wymum(wyr8(p) ^ seed ^ wyp0, wyr8(p + 8) ^ seed ^ wyp1) - see1 = wymum(wyr8(p + 16) ^ see1 ^ wyp2, wyr8(p + 24) ^ see1 ^ wyp3) - see2 = wymum(wyr8(p + 32) ^ see2 ^ wyp1, wyr8(p + 40) ^ see2 ^ wyp2) - see3 = wymum(wyr8(p + 48) ^ see3 ^ wyp3, wyr8(p + 56) ^ see3 ^ wyp0) + seed = wymum(wyr8(p) ^ seed ^ hash.wyp0, wyr8(p + 8) ^ seed ^ hash.wyp1) + see1 = wymum(wyr8(p + 16) ^ see1 ^ hash.wyp2, wyr8(p + 24) ^ see1 ^ hash.wyp3) + see2 = wymum(wyr8(p + 32) ^ see2 ^ hash.wyp1, wyr8(p + 40) ^ see2 ^ hash.wyp2) + see3 = wymum(wyr8(p + 48) ^ see3 ^ hash.wyp3, wyr8(p + 56) ^ see3 ^ hash.wyp0) p = p + 64 } } - return wymum(seed ^ see1 ^ see2, see3 ^ len ^ wyp4) + return wymum(seed ^ see1 ^ see2, see3 ^ len ^ hash.wyp4) } [inline] fn wyrotr(v u64, k u32) u64 { - return (v>>k) | (v<<(64 - k)) + return (v >> k) | (v << (64 - k)) } [inline] @@ -94,36 +101,36 @@ pub fn wymum(a u64, b u64) u64 { */ mask32 := u32(4294967295) x0 := a & mask32 - x1 := a>>32 + x1 := a >> 32 y0 := b & mask32 - y1 := b>>32 + y1 := b >> 32 w0 := x0 * y0 - t := x1 * y0 + (w0>>32) + t := x1 * y0 + (w0 >> 32) mut w1 := t & mask32 - w2 := t>>32 + w2 := t >> 32 w1 += x0 * y1 - hi := x1 * y1 + w2 + (w1>>32) + hi := x1 * y1 + w2 + (w1 >> 32) lo := a * b return hi ^ lo } [inline] -fn wyr3(p byteptr, k u64) u64 { +fn wyr3(p &byte, k u64) u64 { unsafe { - return (u64(p[0])<<16) | (u64(p[k>>1])<<8) | u64(p[k - 1]) + return (u64(p[0]) << 16) | (u64(p[k >> 1]) << 8) | u64(p[k - 1]) } } [inline] -fn wyr4(p byteptr) u64 { +fn wyr4(p &byte) u64 { unsafe { - return u32(p[0]) | (u32(p[1])< { +struct Foo { pub: name string data T diff --git a/vlib/math/bits.v b/vlib/math/bits.v index d26091a8b..0626504bc 100644 --- a/vlib/math/bits.v +++ b/vlib/math/bits.v @@ -4,25 +4,26 @@ module math const ( - uvnan = u64(0x7FF8000000000001) - uvinf = u64(0x7FF0000000000000) - uvneginf = u64(0xFFF0000000000000) - uvone = u64(0x3FF0000000000000) - mask = 0x7FF - shift = 64 - 11 - 1 - bias = 1023 - sign_mask = (u64(1)<<63) - frac_mask = ((u64(1)<= 0, negative infinity if sign < 0. pub fn inf(sign int) f64 { - v := if sign >= 0 { uvinf } else { uvneginf } + v := if sign >= 0 { math.uvinf } else { math.uvneginf } return f64_from_bits(v) } // nan returns an IEEE 754 ``not-a-number'' value. pub fn nan() f64 { - return f64_from_bits(uvnan) + return f64_from_bits(math.uvnan) } // is_nan reports whether f is an IEEE 754 ``not-a-number'' value. diff --git a/vlib/math/bits/bits_tables.v b/vlib/math/bits/bits_tables.v index 8744a8220..830e1e833 100644 --- a/vlib/math/bits/bits_tables.v +++ b/vlib/math/bits/bits_tables.v @@ -4,72 +4,76 @@ module bits const ( - ntz_8_tab = [byte(0x08), 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x07, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, - ] - pop_8_tab = [byte(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, - 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, - 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, - 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, - 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, - 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, - 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, - 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, - 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, - 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, - 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, - 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, - 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, - 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, - 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, - 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08, - ] - rev_8_tab = [byte(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, - 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, - 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, - 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, - 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, - 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, - 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, - 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, - 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, - 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, - 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, - 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, - 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, - 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, - 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, - 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, - ] - len_8_tab = [byte(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - ] + ntz_8_tab = [byte(0x08), 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, + 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, + 0x00, 0x02, 0x00, 0x01, 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, + 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, + 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, + 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, + 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, + 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x07, 0x00, 0x01, 0x00, + 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, + 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x05, 0x00, + 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, + 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, + 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, + 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, + 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, + 0x02, 0x00, 0x01, 0x00] + pop_8_tab = [byte(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, + 0x02, 0x03, 0x03, 0x04, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, + 0x04, 0x03, 0x04, 0x04, 0x05, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, + 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, + 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, + 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, + 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, + 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x03, 0x04, 0x04, 0x05, 0x04, + 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x01, 0x02, 0x02, 0x03, + 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x02, 0x03, 0x03, + 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x02, 0x03, + 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x03, + 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, + 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, + 0x06, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, + 0x06, 0x07, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, + 0x06, 0x06, 0x07, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, + 0x06, 0x07, 0x07, 0x08] + rev_8_tab = [byte(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, + 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, + 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, + 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, + 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, + 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, + 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, + 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, + 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, + 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 0x09, 0x89, 0x49, + 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, + 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, + 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, + 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, + 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, + 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, + 0xb7, 0x77, 0xf7, 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, + 0x3f, 0xbf, 0x7f, 0xff] + len_8_tab = [byte(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08] ) diff --git a/vlib/math/bits/bits_test.v b/vlib/math/bits/bits_test.v index b2f809fe3..23a01b30d 100644 --- a/vlib/math/bits/bits_test.v +++ b/vlib/math/bits/bits_test.v @@ -3,9 +3,9 @@ // module bits -fn test_bits(){ +fn test_bits() { mut i := 0 - mut i1:= u64(0) + mut i1 := u64(0) // // --- LeadingZeros --- @@ -13,29 +13,29 @@ fn test_bits(){ // 8 bit i = 1 - for x in 0..8 { - //C.printf("x:%02x lz: %d cmp: %d\n", i << x, leading_zeros_8(i << x), 7-x) + for x in 0 .. 8 { + // C.printf("x:%02x lz: %d cmp: %d\n", i << x, leading_zeros_8(i << x), 7-x) assert leading_zeros_8(byte(i << x)) == 7 - x } // 16 bit i = 1 - for x in 0..16 { - //C.printf("x:%04x lz: %d cmp: %d\n", u16(i) << x, leading_zeros_16(u16(i) << x), 15-x) + for x in 0 .. 16 { + // C.printf("x:%04x lz: %d cmp: %d\n", u16(i) << x, leading_zeros_16(u16(i) << x), 15-x) assert leading_zeros_16(u16(i) << x) == 15 - x } // 32 bit i = 1 - for x in 0..32 { - //C.printf("x:%08x lz: %d cmp: %d\n", u32(i) << x, leading_zeros_32(u32(i) << x), 31-x) + for x in 0 .. 32 { + // C.printf("x:%08x lz: %d cmp: %d\n", u32(i) << x, leading_zeros_32(u32(i) << x), 31-x) assert leading_zeros_32(u32(i) << x) == 31 - x } // 64 bit i = 1 - for x in 0..64 { - //C.printf("x:%016llx lz: %llu cmp: %d\n", u64(i) << x, leading_zeros_64(u64(i) << x), 63-x) + for x in 0 .. 64 { + // C.printf("x:%016llx lz: %llu cmp: %d\n", u64(i) << x, leading_zeros_64(u64(i) << x), 63-x) assert leading_zeros_64(u64(i) << x) == 63 - x } @@ -45,32 +45,32 @@ fn test_bits(){ // 8 bit i = 0 - for x in 0..9 { - //C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), ones_count_8(byte(i)), x) + for x in 0 .. 9 { + // C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), ones_count_8(byte(i)), x) assert ones_count_8(byte(i)) == x i = (i << 1) + 1 } // 16 bit i = 0 - for x in 0..17 { - //C.printf("x:%04x lz: %llu cmp: %d\n", u16(i), ones_count_16(u16(i)), x) + for x in 0 .. 17 { + // C.printf("x:%04x lz: %llu cmp: %d\n", u16(i), ones_count_16(u16(i)), x) assert ones_count_16(u16(i)) == x i = (i << 1) + 1 } // 32 bit i = 0 - for x in 0..33 { - //C.printf("x:%08x lz: %llu cmp: %d\n", u32(i), ones_count_32(u32(i)), x) + for x in 0 .. 33 { + // C.printf("x:%08x lz: %llu cmp: %d\n", u32(i), ones_count_32(u32(i)), x) assert ones_count_32(u32(i)) == x i = (i << 1) + 1 } // 64 bit i1 = 0 - for x in 0..65 { - //C.printf("x:%016llx lz: %llu cmp: %d\n", u64(i1), ones_count_64(u64(i1)), x) + for x in 0 .. 65 { + // C.printf("x:%016llx lz: %llu cmp: %d\n", u64(i1), ones_count_64(u64(i1)), x) assert ones_count_64(i1) == x i1 = (i1 << 1) + 1 } @@ -78,10 +78,10 @@ fn test_bits(){ // // --- rotate_left/right --- // - assert rotate_left_8( 0x12 , 4) == 0x21 - assert rotate_left_16( 0x1234 , 8) == 0x3412 - assert rotate_left_32( 0x12345678 , 16) == 0x56781234 - assert rotate_left_64( 0x1234567887654321 , 32) == 0x8765432112345678 + assert rotate_left_8(0x12, 4) == 0x21 + assert rotate_left_16(0x1234, 8) == 0x3412 + assert rotate_left_32(0x12345678, 16) == 0x56781234 + assert rotate_left_64(0x1234567887654321, 32) == 0x8765432112345678 // // --- reverse --- @@ -89,7 +89,7 @@ fn test_bits(){ // 8 bit i = 0 - for _ in 0..9 { + for _ in 0 .. 9 { mut rv := byte(0) mut bc := 0 mut n := i @@ -98,14 +98,14 @@ fn test_bits(){ bc++ n = n >> 1 } - //C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), reverse_8(byte(i)), rv) + // C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), reverse_8(byte(i)), rv) assert reverse_8(byte(i)) == rv i = (i << 1) + 1 } // 16 bit i = 0 - for _ in 0..17 { + for _ in 0 .. 17 { mut rv := u16(0) mut bc := 0 mut n := i @@ -114,14 +114,14 @@ fn test_bits(){ bc++ n = n >> 1 } - //C.printf("x:%04x lz: %llu cmp: %d\n", u16(i), reverse_16(u16(i)), rv) + // C.printf("x:%04x lz: %llu cmp: %d\n", u16(i), reverse_16(u16(i)), rv) assert reverse_16(u16(i)) == rv i = (i << 1) + 1 } // 32 bit i = 0 - for _ in 0..33 { + for _ in 0 .. 33 { mut rv := u32(0) mut bc := 0 mut n := i @@ -130,14 +130,14 @@ fn test_bits(){ bc++ n = n >> 1 } - //C.printf("x:%08x lz: %llu cmp: %d\n", u32(i), reverse_32(u32(i)), rv) + // C.printf("x:%08x lz: %llu cmp: %d\n", u32(i), reverse_32(u32(i)), rv) assert reverse_32(u32(i)) == rv i = (i << 1) + 1 } // 64 bit i1 = 0 - for _ in 0..64 { + for _ in 0 .. 64 { mut rv := u64(0) mut bc := 0 mut n := i1 @@ -146,7 +146,7 @@ fn test_bits(){ bc++ n = n >> 1 } - //C.printf("x:%016llx lz: %016llx cmp: %016llx\n", u64(i1), reverse_64(u64(i1)), rv) + // C.printf("x:%016llx lz: %016llx cmp: %016llx\n", u64(i1), reverse_64(u64(i1)), rv) assert reverse_64(i1) == rv i1 = (i1 << 1) + 1 } @@ -157,10 +157,10 @@ fn test_bits(){ // 32 bit i = 1 - for x in 0..32 { + for x in 0 .. 32 { v := u32(i) << x - sum,carry := add_32(v, v, u32(0)) - //C.printf("x:%08x [%llu,%llu] %llu\n", u32(i) << x, sum, carry, u64(v) + u64(v)) + sum, carry := add_32(v, v, u32(0)) + // C.printf("x:%08x [%llu,%llu] %llu\n", u32(i) << x, sum, carry, u64(v) + u64(v)) assert ((u64(carry) << 32) | u64(sum)) == u64(v) + u64(v) } mut sum_32t, mut carry_32t := add_32(0x8000_0000, 0x8000_0000, u32(0)) @@ -173,13 +173,14 @@ fn test_bits(){ // 64 bit i = 1 - for x in 0..63 { + for x in 0 .. 63 { v := u64(i) << x - sum,carry := add_64(v, v, u64(0)) - //C.printf("x:%16x [%llu,%llu] %llu\n", u64(i) << x, sum, carry, u64(v >> 32) + u64(v >> 32)) + sum, carry := add_64(v, v, u64(0)) + // C.printf("x:%16x [%llu,%llu] %llu\n", u64(i) << x, sum, carry, u64(v >> 32) + u64(v >> 32)) assert ((carry << 32) | sum) == v + v } - mut sum_64t, mut carry_64t := add_64(0x8000_0000_0000_0000, 0x8000_0000_0000_0000, u64(0)) + mut sum_64t, mut carry_64t := add_64(0x8000_0000_0000_0000, 0x8000_0000_0000_0000, + u64(0)) assert sum_64t == u64(0) assert carry_64t == u64(1) @@ -193,39 +194,39 @@ fn test_bits(){ // 32 bit i = 1 - for x in 1..32 { + for x in 1 .. 32 { v0 := u32(i) << x v1 := v0 >> 1 mut diff, mut borrow_out := sub_32(v0, v1, u32(0)) - //C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v0 - v1) + // C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v0 - v1) assert diff == v1 diff, borrow_out = sub_32(v0, v1, u32(1)) - //C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v0 - v1) + // C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v0 - v1) assert diff == (v1 - 1) assert borrow_out == u32(0) diff, borrow_out = sub_32(v1, v0, u32(1)) - //C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v1 - v0) + // C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v1 - v0) assert borrow_out == u32(1) } // 64 bit i = 1 - for x in 1..64 { + for x in 1 .. 64 { v0 := u64(i) << x v1 := v0 >> 1 mut diff, mut borrow_out := sub_64(v0, v1, u64(0)) - //C.printf("x:%08x [%llu,%llu] %08x\n", u64(i) << x, diff, borrow_out, v0 - v1) + // C.printf("x:%08x [%llu,%llu] %08x\n", u64(i) << x, diff, borrow_out, v0 - v1) assert diff == v1 diff, borrow_out = sub_64(v0, v1, u64(1)) - //C.printf("x:%08x [%llu,%llu] %08x\n", u64(i) << x, diff, borrow_out, v0 - v1) + // C.printf("x:%08x [%llu,%llu] %08x\n", u64(i) << x, diff, borrow_out, v0 - v1) assert diff == (v1 - 1) assert borrow_out == u64(0) diff, borrow_out = sub_64(v1, v0, u64(1)) - //C.printf("x:%08x [%llu,%llu] %08x\n",u64(i) << x, diff, borrow_out, v1 - v0) + // C.printf("x:%08x [%llu,%llu] %08x\n",u64(i) << x, diff, borrow_out, v1 - v0) assert borrow_out == u64(1) } @@ -235,7 +236,7 @@ fn test_bits(){ // 32 bit i = 1 - for x in 0..32 { + for x in 0 .. 32 { v0 := u32(i) << x v1 := v0 - 1 hi, lo := mul_32(v0, v1) @@ -244,12 +245,12 @@ fn test_bits(){ // 64 bit i = 1 - for x in 0..64 { + for x in 0 .. 64 { v0 := u64(i) << x v1 := v0 - 1 hi, lo := mul_64(v0, v1) - //C.printf("v0: %llu v1: %llu [%llu,%llu] tt: %llu\n", v0, v1, hi, lo, (v0 >> 32) * (v1 >> 32)) - assert (hi & 0xFFFF_FFFF_0000_0000) == (((v0 >> 32)*(v1 >> 32)) & 0xFFFF_FFFF_0000_0000) + // C.printf("v0: %llu v1: %llu [%llu,%llu] tt: %llu\n", v0, v1, hi, lo, (v0 >> 32) * (v1 >> 32)) + assert (hi & 0xFFFF_FFFF_0000_0000) == (((v0 >> 32) * (v1 >> 32)) & 0xFFFF_FFFF_0000_0000) assert (lo & 0x0000_0000_FFFF_FFFF) == (((v0 & 0x0000_0000_FFFF_FFFF) * (v1 & 0x0000_0000_FFFF_FFFF)) & 0x0000_0000_FFFF_FFFF) } @@ -259,12 +260,12 @@ fn test_bits(){ // 32 bit i = 1 - for x in 0..31 { + for x in 0 .. 31 { hi := u32(i) << x lo := hi - 1 - y := u32(3) << x + y := u32(3) << x quo, rem := div_32(hi, lo, y) - //C.printf("[%08x_%08x] %08x (%08x,%08x)\n", hi, lo, y, quo, rem) + // C.printf("[%08x_%08x] %08x (%08x,%08x)\n", hi, lo, y, quo, rem) tst := ((u64(hi) << 32) | u64(lo)) assert quo == (tst / u64(y)) assert rem == (tst % u64(y)) @@ -273,16 +274,15 @@ fn test_bits(){ // 64 bit i = 1 - for x in 0..62 { + for x in 0 .. 62 { hi := u64(i) << x - lo := u64(2) //hi - 1 - y := u64(0x4000_0000_0000_0000) + lo := u64(2) // hi - 1 + y := u64(0x4000_0000_0000_0000) quo, rem := div_64(hi, lo, y) - //C.printf("[%016llx_%016llx] %016llx (%016llx,%016llx)\n", hi, lo, y, quo, rem) - assert quo == u64(2)<<(x+1) - _, rem1 := div_64(hi%y, lo, y) + // C.printf("[%016llx_%016llx] %016llx (%016llx,%016llx)\n", hi, lo, y, quo, rem) + assert quo == u64(2) << (x + 1) + _, rem1 := div_64(hi % y, lo, y) assert rem == rem1 assert rem == rem_64(hi, lo, y) } - } diff --git a/vlib/math/complex/complex.v b/vlib/math/complex/complex.v index afe4866bb..f5917f169 100644 --- a/vlib/math/complex/complex.v +++ b/vlib/math/complex/complex.v @@ -19,12 +19,7 @@ pub fn complex(re f64, im f64) Complex { // To String method pub fn (c Complex) str() string { mut out := '${c.re:f}' - out += if c.im >= 0 { - '+${c.im:f}' - } - else { - '${c.im:f}' - } + out += if c.im >= 0 { '+${c.im:f}' } else { '${c.im:f}' } out += 'i' return out } @@ -34,11 +29,11 @@ pub fn (c Complex) str() string { pub fn (c Complex) abs() f64 { return C.hypot(c.re, c.im) } + pub fn (c Complex) mod() f64 { return c.abs() } - // Complex Angle pub fn (c Complex) angle() f64 { return math.atan2(c.im, c.re) @@ -56,19 +51,14 @@ pub fn (c1 Complex) - (c2 Complex) Complex { // Complex Multiplication c1 * c2 pub fn (c1 Complex) * (c2 Complex) Complex { - return Complex{ - (c1.re * c2.re) + ((c1.im * c2.im) * -1), - (c1.re * c2.im) + (c1.im * c2.re) - } + return Complex{(c1.re * c2.re) + ((c1.im * c2.im) * -1), (c1.re * c2.im) + (c1.im * c2.re)} } // Complex Division c1 / c2 pub fn (c1 Complex) / (c2 Complex) Complex { denom := (c2.re * c2.re) + (c2.im * c2.im) - return Complex { - ((c1.re * c2.re) + ((c1.im * -c2.im) * -1))/denom, - ((c1.re * -c2.im) + (c1.im * c2.re))/denom - } + return Complex{((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom, ((c1.re * -c2.im) + + (c1.im * c2.re)) / denom} } // Complex Addition c1.add(c2) @@ -83,23 +73,18 @@ pub fn (c1 Complex) subtract(c2 Complex) Complex { // Complex Multiplication c1.multiply(c2) pub fn (c1 Complex) multiply(c2 Complex) Complex { - return Complex{ - (c1.re * c2.re) + ((c1.im * c2.im) * -1), - (c1.re * c2.im) + (c1.im * c2.re) - } + return Complex{(c1.re * c2.re) + ((c1.im * c2.im) * -1), (c1.re * c2.im) + (c1.im * c2.re)} } // Complex Division c1.divide(c2) pub fn (c1 Complex) divide(c2 Complex) Complex { denom := (c2.re * c2.re) + (c2.im * c2.im) - return Complex { - ((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom, - ((c1.re * -c2.im) + (c1.im * c2.re)) / denom - } + return Complex{((c1.re * c2.re) + ((c1.im * -c2.im) * -1)) / denom, ((c1.re * -c2.im) + + (c1.im * c2.re)) / denom} } // Complex Conjugate -pub fn (c Complex) conjugate() Complex{ +pub fn (c Complex) conjugate() Complex { return Complex{c.re, -c.im} } @@ -114,10 +99,7 @@ pub fn (c Complex) addinv() Complex { // Based on // http://tutorial.math.lamar.edu/Extras/ComplexPrimer/Arithmetic.aspx pub fn (c Complex) mulinv() Complex { - return Complex { - c.re / (c.re * c.re + c.im * c.im), - -c.im / (c.re * c.re + c.im * c.im) - } + return Complex{c.re / (c.re * c.re + c.im * c.im), -c.im / (c.re * c.re + c.im * c.im)} } // Complex Power @@ -126,10 +108,7 @@ pub fn (c Complex) mulinv() Complex { pub fn (c Complex) pow(n f64) Complex { r := math.pow(c.abs(), n) angle := c.angle() - return Complex { - r * math.cos(n * angle), - r * math.sin(n * angle) - } + return Complex{r * math.cos(n * angle), r * math.sin(n * angle)} } // Complex nth root @@ -143,20 +122,14 @@ pub fn (c Complex) root(n f64) Complex { // https://www.math.wisc.edu/~angenent/Free-Lecture-Notes/freecomplexnumbers.pdf pub fn (c Complex) exp() Complex { a := math.exp(c.re) - return Complex { - a * math.cos(c.im), - a * math.sin(c.im) - } + return Complex{a * math.cos(c.im), a * math.sin(c.im)} } // Complex Natural Logarithm // Based on // http://www.chemistrylearning.com/logarithm-of-complex-number/ pub fn (c Complex) ln() Complex { - return Complex { - math.log(c.abs()), - c.angle() - } + return Complex{math.log(c.abs()), c.angle()} } // Complex Log Base Complex @@ -170,7 +143,7 @@ pub fn (c Complex) log(base Complex) Complex { // Based on // http://mathworld.wolfram.com/ComplexArgument.html pub fn (c Complex) arg() f64 { - return math.atan2(c.im,c.re) + return math.atan2(c.im, c.re) } // Complex raised to Complex Power @@ -178,33 +151,24 @@ pub fn (c Complex) arg() f64 { // http://mathworld.wolfram.com/ComplexExponentiation.html pub fn (c Complex) cpow(p Complex) Complex { a := c.arg() - b := math.pow(c.re,2) + math.pow(c.im,2) - d := p.re * a + (1.0/2) * p.im * math.log(b) - t1 := math.pow(b,p.re/2) * math.exp(-p.im*a) - return Complex{ - t1 * math.cos(d), - t1 * math.sin(d) - } + b := math.pow(c.re, 2) + math.pow(c.im, 2) + d := p.re * a + (1.0 / 2) * p.im * math.log(b) + t1 := math.pow(b, p.re / 2) * math.exp(-p.im * a) + return Complex{t1 * math.cos(d), t1 * math.sin(d)} } // Complex Sin // Based on // http://www.milefoot.com/math/complex/functionsofi.htm pub fn (c Complex) sin() Complex { - return Complex{ - math.sin(c.re) * math.cosh(c.im), - math.cos(c.re) * math.sinh(c.im) - } + return Complex{math.sin(c.re) * math.cosh(c.im), math.cos(c.re) * math.sinh(c.im)} } // Complex Cosine // Based on // http://www.milefoot.com/math/complex/functionsofi.htm pub fn (c Complex) cos() Complex { - return Complex{ - math.cos(c.re) * math.cosh(c.im), - -(math.sin(c.re) * math.sinh(c.im)) - } + return Complex{math.cos(c.re) * math.cosh(c.im), -(math.sin(c.re) * math.sinh(c.im))} } // Complex Tangent @@ -225,102 +189,71 @@ pub fn (c Complex) cot() Complex { // Based on // http://www.suitcaseofdreams.net/Trigonometric_Functions.htm pub fn (c Complex) sec() Complex { - return complex(1,0).divide(c.cos()) + return complex(1, 0).divide(c.cos()) } // Complex Cosecant // Based on // http://www.suitcaseofdreams.net/Trigonometric_Functions.htm pub fn (c Complex) csc() Complex { - return complex(1,0).divide(c.sin()) + return complex(1, 0).divide(c.sin()) } // Complex Arc Sin / Sin Inverse // Based on // http://www.milefoot.com/math/complex/summaryops.htm pub fn (c Complex) asin() Complex { - return complex(0,-1).multiply( - complex(0,1) - .multiply(c) - .add( - complex(1,0) - .subtract(c.pow(2)) - .root(2) - ) - .ln() - ) + return complex(0, -1).multiply(complex(0, 1).multiply(c).add(complex(1, 0).subtract(c.pow(2)).root(2)).ln()) } // Complex Arc Consine / Consine Inverse // Based on // http://www.milefoot.com/math/complex/summaryops.htm pub fn (c Complex) acos() Complex { - return complex(0,-1).multiply( - c.add( - complex(0,1) - .multiply( - complex(1,0) - .subtract(c.pow(2)) - .root(2) - ) - ) - .ln() - ) + return complex(0, -1).multiply(c.add(complex(0, 1).multiply(complex(1, 0).subtract(c.pow(2)).root(2))).ln()) } // Complex Arc Tangent / Tangent Inverse // Based on // http://www.milefoot.com/math/complex/summaryops.htm pub fn (c Complex) atan() Complex { - i := complex(0,1) - return complex(0,1.0/2).multiply( - i.add(c) - .divide( - i.subtract(c) - ) - .ln() - ) + i := complex(0, 1) + return complex(0, 1.0 / 2).multiply(i.add(c).divide(i.subtract(c)).ln()) } // Complex Arc Cotangent / Cotangent Inverse // Based on // http://www.suitcaseofdreams.net/Inverse_Functions.htm pub fn (c Complex) acot() Complex { - return complex(1,0).divide(c).atan() + return complex(1, 0).divide(c).atan() } // Complex Arc Secant / Secant Inverse // Based on // http://www.suitcaseofdreams.net/Inverse_Functions.htm pub fn (c Complex) asec() Complex { - return complex(1,0).divide(c).acos() + return complex(1, 0).divide(c).acos() } // Complex Arc Cosecant / Cosecant Inverse // Based on // http://www.suitcaseofdreams.net/Inverse_Functions.htm pub fn (c Complex) acsc() Complex { - return complex(1,0).divide(c).asin() + return complex(1, 0).divide(c).asin() } // Complex Hyperbolic Sin // Based on // http://www.milefoot.com/math/complex/functionsofi.htm pub fn (c Complex) sinh() Complex { - return Complex{ - math.cos(c.im) * math.sinh(c.re), - math.sin(c.im) * math.cosh(c.re) - } + return Complex{math.cos(c.im) * math.sinh(c.re), math.sin(c.im) * math.cosh(c.re)} } // Complex Hyperbolic Cosine // Based on // http://www.milefoot.com/math/complex/functionsofi.htm pub fn (c Complex) cosh() Complex { - return Complex{ - math.cos(c.im) * math.cosh(c.re), - math.sin(c.im) * math.sinh(c.re) - } + return Complex{math.cos(c.im) * math.cosh(c.re), math.sin(c.im) * math.sinh(c.re)} } // Complex Hyperbolic Tangent @@ -341,25 +274,21 @@ pub fn (c Complex) coth() Complex { // Based on // http://www.suitcaseofdreams.net/Hyperbolic_Functions.htm pub fn (c Complex) sech() Complex { - return complex(1,0).divide(c.cosh()) + return complex(1, 0).divide(c.cosh()) } // Complex Hyperbolic Cosecant // Based on // http://www.suitcaseofdreams.net/Hyperbolic_Functions.htm pub fn (c Complex) csch() Complex { - return complex(1,0).divide(c.sinh()) + return complex(1, 0).divide(c.sinh()) } // Complex Hyperbolic Arc Sin / Sin Inverse // Based on // http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm pub fn (c Complex) asinh() Complex { - return c.add( - c.pow(2) - .add(complex(1,0)) - .root(2) - ).ln() + return c.add(c.pow(2).add(complex(1, 0)).root(2)).ln() } // Complex Hyperbolic Arc Consine / Consine Inverse @@ -367,22 +296,10 @@ pub fn (c Complex) asinh() Complex { // http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm pub fn (c Complex) acosh() Complex { if c.re > 1 { - return c.add( - c.pow(2) - .subtract(complex(1,0)) - .root(2) - ).ln() - } - else { - one := complex(1,0) - return c.add( - c.add(one) - .root(2) - .multiply( - c.subtract(one) - .root(2) - ) - ).ln() + return c.add(c.pow(2).subtract(complex(1, 0)).root(2)).ln() + } else { + one := complex(1, 0) + return c.add(c.add(one).root(2).multiply(c.subtract(one).root(2))).ln() } } @@ -390,29 +307,11 @@ pub fn (c Complex) acosh() Complex { // Based on // http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm pub fn (c Complex) atanh() Complex { - one := complex(1,0) + one := complex(1, 0) if c.re < 1 { - return complex(1.0/2,0).multiply( - one - .add(c) - .divide( - one - .subtract(c) - ) - .ln() - ) - } - else { - return complex(1.0/2,0).multiply( - one - .add(c) - .ln() - .subtract( - one - .subtract(c) - .ln() - ) - ) + return complex(1.0 / 2, 0).multiply(one.add(c).divide(one.subtract(c)).ln()) + } else { + return complex(1.0 / 2, 0).multiply(one.add(c).ln().subtract(one.subtract(c).ln())) } } @@ -420,29 +319,12 @@ pub fn (c Complex) atanh() Complex { // Based on // http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm pub fn (c Complex) acoth() Complex { - one := complex(1,0) + one := complex(1, 0) if c.re < 0 || c.re > 1 { - return complex(1.0/2,0).multiply( - c - .add(one) - .divide( - c.subtract(one) - ) - .ln() - ) - } - else { + return complex(1.0 / 2, 0).multiply(c.add(one).divide(c.subtract(one)).ln()) + } else { div := one.divide(c) - return complex(1.0/2,0).multiply( - one - .add(div) - .ln() - .subtract( - one - .subtract(div) - .ln() - ) - ) + return complex(1.0 / 2, 0).multiply(one.add(div).ln().subtract(one.subtract(div).ln())) } } @@ -452,51 +334,37 @@ pub fn (c Complex) acoth() Complex { // For certain scenarios, Result mismatch in crossverification with Wolfram Alpha - analysis pending // pub fn (c Complex) asech() Complex { // one := complex(1,0) - // if(c.re < -1.0) { - // return one.subtract( - // one.subtract( - // c.pow(2) - // ) - // .root(2) - // ) - // .divide(c) - // .ln() - // } - // else { - // return one.add( - // one.subtract( - // c.pow(2) - // ) - // .root(2) - // ) - // .divide(c) - // .ln() - // } +// if(c.re < -1.0) { +// return one.subtract( +// one.subtract( +// c.pow(2) +// ) +// .root(2) +// ) +// .divide(c) +// .ln() +// } +// else { +// return one.add( +// one.subtract( +// c.pow(2) +// ) +// .root(2) +// ) +// .divide(c) +// .ln() +// } // } // Complex Hyperbolic Arc Cosecant / Cosecant Inverse // Based on // http://www.suitcaseofdreams.net/Inverse__Hyperbolic_Functions.htm pub fn (c Complex) acsch() Complex { - one := complex(1,0) + one := complex(1, 0) if c.re < 0 { - return one.subtract( - one.add( - c.pow(2) - ) - .root(2) - ) - .divide(c) - .ln() + return one.subtract(one.add(c.pow(2)).root(2)).divide(c).ln() } else { - return one.add( - one.add( - c.pow(2) - ) - .root(2) - ) - .divide(c) - .ln() + return one.add(one.add(c.pow(2)).root(2)).divide(c).ln() } } diff --git a/vlib/math/complex/complex_test.v b/vlib/math/complex/complex_test.v index 65cac0e65..5347f3398 100644 --- a/vlib/math/complex/complex_test.v +++ b/vlib/math/complex/complex_test.v @@ -11,124 +11,124 @@ fn tst_res(str1 string, str2 string) bool { fn test_complex_addition() { // Test is based on and verified from practice examples of Khan Academy // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers - mut c1 := cmplx.complex(0,-10) - mut c2 := cmplx.complex(-40,8) + mut c1 := cmplx.complex(0, -10) + mut c2 := cmplx.complex(-40, 8) mut result := c1 + c2 - assert result.equals(cmplx.complex(-40,-2)) - c1 = cmplx.complex(-71,2) - c2 = cmplx.complex(88,-12) + assert result.equals(cmplx.complex(-40, -2)) + c1 = cmplx.complex(-71, 2) + c2 = cmplx.complex(88, -12) result = c1 + c2 - assert result.equals(cmplx.complex(17,-10)) - c1 = cmplx.complex(0,-30) - c2 = cmplx.complex(52,-30) + assert result.equals(cmplx.complex(17, -10)) + c1 = cmplx.complex(0, -30) + c2 = cmplx.complex(52, -30) result = c1 + c2 - assert result.equals(cmplx.complex(52,-60)) - c1 = cmplx.complex(12,-9) - c2 = cmplx.complex(32,-6) + assert result.equals(cmplx.complex(52, -60)) + c1 = cmplx.complex(12, -9) + c2 = cmplx.complex(32, -6) result = c1 + c2 - assert result.equals(cmplx.complex(44,-15)) + assert result.equals(cmplx.complex(44, -15)) } fn test_complex_subtraction() { // Test is based on and verified from practice examples of Khan Academy // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers - mut c1 := cmplx.complex(-8,0) - mut c2 := cmplx.complex(6,30) + mut c1 := cmplx.complex(-8, 0) + mut c2 := cmplx.complex(6, 30) mut result := c1 - c2 - assert result.equals(cmplx.complex(-14,-30)) - c1 = cmplx.complex(-19,7) - c2 = cmplx.complex(29,32) + assert result.equals(cmplx.complex(-14, -30)) + c1 = cmplx.complex(-19, 7) + c2 = cmplx.complex(29, 32) result = c1 - c2 - assert result.equals(cmplx.complex(-48,-25)) - c1 = cmplx.complex(12,0) - c2 = cmplx.complex(23,13) + assert result.equals(cmplx.complex(-48, -25)) + c1 = cmplx.complex(12, 0) + c2 = cmplx.complex(23, 13) result = c1 - c2 - assert result.equals(cmplx.complex(-11,-13)) - c1 = cmplx.complex(-14,3) - c2 = cmplx.complex(0,14) + assert result.equals(cmplx.complex(-11, -13)) + c1 = cmplx.complex(-14, 3) + c2 = cmplx.complex(0, 14) result = c1 - c2 - assert result.equals(cmplx.complex(-14,-11)) + assert result.equals(cmplx.complex(-14, -11)) } fn test_complex_multiplication() { // Test is based on and verified from practice examples of Khan Academy // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers - mut c1 := cmplx.complex(1,2) - mut c2 := cmplx.complex(1,-4) + mut c1 := cmplx.complex(1, 2) + mut c2 := cmplx.complex(1, -4) mut result := c1 * c2 - assert result.equals(cmplx.complex(9,-2)) - c1 = cmplx.complex(-4,-4) - c2 = cmplx.complex(-5,-3) + assert result.equals(cmplx.complex(9, -2)) + c1 = cmplx.complex(-4, -4) + c2 = cmplx.complex(-5, -3) result = c1 * c2 - assert result.equals(cmplx.complex(8,32)) - c1 = cmplx.complex(4,4) - c2 = cmplx.complex(-2,-5) + assert result.equals(cmplx.complex(8, 32)) + c1 = cmplx.complex(4, 4) + c2 = cmplx.complex(-2, -5) result = c1 * c2 - assert result.equals(cmplx.complex(12,-28)) - c1 = cmplx.complex(2,-2) - c2 = cmplx.complex(4,-4) + assert result.equals(cmplx.complex(12, -28)) + c1 = cmplx.complex(2, -2) + c2 = cmplx.complex(4, -4) result = c1 * c2 - assert result.equals(cmplx.complex(0,-16)) + assert result.equals(cmplx.complex(0, -16)) } fn test_complex_division() { // Test is based on and verified from practice examples of Khan Academy // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers - mut c1 := cmplx.complex(-9,-6) - mut c2 := cmplx.complex(-3,-2) + mut c1 := cmplx.complex(-9, -6) + mut c2 := cmplx.complex(-3, -2) mut result := c1 / c2 - assert result.equals(cmplx.complex(3,0)) - c1 = cmplx.complex(-23,11) - c2 = cmplx.complex(5,1) + assert result.equals(cmplx.complex(3, 0)) + c1 = cmplx.complex(-23, 11) + c2 = cmplx.complex(5, 1) result = c1 / c2 - assert result.equals(cmplx.complex(-4,3)) - c1 = cmplx.complex(8,-2) - c2 = cmplx.complex(-4,1) + assert result.equals(cmplx.complex(-4, 3)) + c1 = cmplx.complex(8, -2) + c2 = cmplx.complex(-4, 1) result = c1 / c2 - assert result.equals(cmplx.complex(-2,0)) - c1 = cmplx.complex(11,24) - c2 = cmplx.complex(-4,-1) + assert result.equals(cmplx.complex(-2, 0)) + c1 = cmplx.complex(11, 24) + c2 = cmplx.complex(-4, -1) result = c1 / c2 - assert result.equals(cmplx.complex(-4,-5)) + assert result.equals(cmplx.complex(-4, -5)) } fn test_complex_conjugate() { // Test is based on and verified from practice examples of Khan Academy // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers - mut c1 := cmplx.complex(0,8) + mut c1 := cmplx.complex(0, 8) mut result := c1.conjugate() - assert result.equals(cmplx.complex(0,-8)) - c1 = cmplx.complex(7,3) + assert result.equals(cmplx.complex(0, -8)) + c1 = cmplx.complex(7, 3) result = c1.conjugate() - assert result.equals(cmplx.complex(7,-3)) - c1 = cmplx.complex(2,2) + assert result.equals(cmplx.complex(7, -3)) + c1 = cmplx.complex(2, 2) result = c1.conjugate() - assert result.equals(cmplx.complex(2,-2)) - c1 = cmplx.complex(7,0) + assert result.equals(cmplx.complex(2, -2)) + c1 = cmplx.complex(7, 0) result = c1.conjugate() - assert result.equals(cmplx.complex(7,0)) + assert result.equals(cmplx.complex(7, 0)) } fn test_complex_equals() { - mut c1 := cmplx.complex(0,8) - mut c2 := cmplx.complex(0,8) + mut c1 := cmplx.complex(0, 8) + mut c2 := cmplx.complex(0, 8) assert c1.equals(c2) - c1 = cmplx.complex(-3,19) - c2 = cmplx.complex(-3,19) + c1 = cmplx.complex(-3, 19) + c2 = cmplx.complex(-3, 19) assert c1.equals(c2) } fn test_complex_abs() { - mut c1 := cmplx.complex(3,4) + mut c1 := cmplx.complex(3, 4) assert c1.abs() == 5 - c1 = cmplx.complex(1,2) + c1 = cmplx.complex(1, 2) assert c1.abs() == math.sqrt(5) assert c1.abs() == c1.conjugate().abs() - c1 = cmplx.complex(7,0) + c1 = cmplx.complex(7, 0) assert c1.abs() == 7 } -fn test_complex_angle(){ +fn test_complex_angle() { // Test is based on and verified from practice examples of Khan Academy // https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers mut c := cmplx.complex(1, 0) @@ -145,52 +145,51 @@ fn test_complex_angle(){ assert cc.angle() + c.angle() == 0 } - fn test_complex_addinv() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(-5,-7) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(-5, -7) mut result := c1.addinv() assert result.equals(c2) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(3,-4) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(3, -4) result = c1.addinv() assert result.equals(c2) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(1,2) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(1, 2) result = c1.addinv() assert result.equals(c2) } fn test_complex_mulinv() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.067568,-0.094595) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.067568, -0.094595) mut result := c1.mulinv() // Some issue with precision comparison in f64 using == operator hence serializing to string println(c2.str()) println(result.str()) assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.12,-0.16) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.12, -0.16) result = c1.mulinv() assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.2,0.4) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.2, 0.4) result = c1.mulinv() assert result.equals(c2) } fn test_complex_mod() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) + mut c1 := cmplx.complex(5, 7) mut result := c1.mod() // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(result.str(), '8.602325') - c1 = cmplx.complex(-3,4) + c1 = cmplx.complex(-3, 4) result = c1.mod() assert result == 5 - c1 = cmplx.complex(-1,-2) + c1 = cmplx.complex(-1, -2) result = c1.mod() // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(result.str(), '2.236068') @@ -198,18 +197,18 @@ fn test_complex_mod() { fn test_complex_pow() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(-24.0,70.0) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(-24.0, 70.0) mut result := c1.pow(2) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(117,44) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(117, 44) result = c1.pow(3) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-7,-24) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-7, -24) result = c1.pow(4) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -217,18 +216,18 @@ fn test_complex_pow() { fn test_complex_root() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(2.607904,1.342074) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(2.607904, 1.342074) mut result := c1.root(2) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(1.264953,1.150614) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(1.264953, 1.150614) result = c1.root(3) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(1.068059,-0.595482) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(1.068059, -0.595482) result = c1.root(4) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -236,18 +235,18 @@ fn test_complex_root() { fn test_complex_exp() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(111.889015,97.505457) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(111.889015, 97.505457) mut result := c1.exp() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.032543,-0.037679) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.032543, -0.037679) result = c1.exp() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.153092,-0.334512) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.153092, -0.334512) result = c1.exp() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -255,18 +254,18 @@ fn test_complex_exp() { fn test_complex_ln() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(2.152033,0.950547) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(2.152033, 0.950547) mut result := c1.ln() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(1.609438,2.214297) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(1.609438, 2.214297) result = c1.ln() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(0.804719,-2.034444) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(0.804719, -2.034444) result = c1.ln() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -274,18 +273,18 @@ fn test_complex_ln() { fn test_complex_arg() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(2.152033,0.950547) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(2.152033, 0.950547) mut result := c1.arg() // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(result.str(), '0.950547') - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(1.609438,2.214297) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(1.609438, 2.214297) result = c1.arg() // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(result.str(), '2.214297') - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(0.804719,-2.034444) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(0.804719, -2.034444) result = c1.arg() // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(result.str(), '-2.034444') @@ -293,21 +292,21 @@ fn test_complex_arg() { fn test_complex_log() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut b1 := cmplx.complex(-6,-2) - mut c2 := cmplx.complex(0.232873,-1.413175) + mut c1 := cmplx.complex(5, 7) + mut b1 := cmplx.complex(-6, -2) + mut c2 := cmplx.complex(0.232873, -1.413175) mut result := c1.log(b1) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - b1 = cmplx.complex(3,-1) - c2 = cmplx.complex(0.152198,-0.409312) + c1 = cmplx.complex(-3, 4) + b1 = cmplx.complex(3, -1) + c2 = cmplx.complex(0.152198, -0.409312) result = c1.log(b1) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - b1 = cmplx.complex(0,9) - c2 = cmplx.complex(-0.298243,1.197981) + c1 = cmplx.complex(-1, -2) + b1 = cmplx.complex(0, 9) + c2 = cmplx.complex(-0.298243, 1.197981) result = c1.log(b1) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -315,21 +314,21 @@ fn test_complex_log() { fn test_complex_cpow() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut r1 := cmplx.complex(2,2) - mut c2 := cmplx.complex(11.022341,-0.861785) + mut c1 := cmplx.complex(5, 7) + mut r1 := cmplx.complex(2, 2) + mut c2 := cmplx.complex(11.022341, -0.861785) mut result := c1.cpow(r1) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - r1 = cmplx.complex(-4,-2) - c2 = cmplx.complex(0.118303,0.063148) + c1 = cmplx.complex(-3, 4) + r1 = cmplx.complex(-4, -2) + c2 = cmplx.complex(0.118303, 0.063148) result = c1.cpow(r1) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - r1 = cmplx.complex(8,-9) - c2 = cmplx.complex(-0.000000,0.000007) + c1 = cmplx.complex(-1, -2) + r1 = cmplx.complex(8, -9) + c2 = cmplx.complex(-0.000000, 0.000007) result = c1.cpow(r1) // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -337,18 +336,18 @@ fn test_complex_cpow() { fn test_complex_sin() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(-525.794515,155.536550) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(-525.794515, 155.536550) mut result := c1.sin() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-3.853738,-27.016813) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-3.853738, -27.016813) result = c1.sin() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-3.165779,-1.959601) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-3.165779, -1.959601) result = c1.sin() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -356,18 +355,18 @@ fn test_complex_sin() { fn test_complex_cos() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(155.536809,525.793641) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(155.536809, 525.793641) mut result := c1.cos() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-27.034946,3.851153) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-27.034946, 3.851153) result = c1.cos() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(2.032723,-3.051898) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(2.032723, -3.051898) result = c1.cos() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -375,18 +374,18 @@ fn test_complex_cos() { fn test_complex_tan() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(-0.000001,1.000001) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(-0.000001, 1.000001) mut result := c1.tan() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(0.000187,0.999356) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(0.000187, 0.999356) result = c1.tan() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.033813,-1.014794) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.033813, -1.014794) result = c1.tan() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -394,18 +393,18 @@ fn test_complex_tan() { fn test_complex_cot() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(-0.000001,-0.999999) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(-0.000001, -0.999999) mut result := c1.cot() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(0.000188,-1.000644) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(0.000188, -1.000644) result = c1.cot() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.032798,0.984329) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.032798, 0.984329) result = c1.cot() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -413,18 +412,18 @@ fn test_complex_cot() { fn test_complex_sec() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.000517,-0.001749) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.000517, -0.001749) mut result := c1.sec() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.036253,-0.005164) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.036253, -0.005164) result = c1.sec() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(0.151176,0.226974) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(0.151176, 0.226974) result = c1.sec() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -432,18 +431,18 @@ fn test_complex_sec() { fn test_complex_csc() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(-0.001749,-0.000517) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(-0.001749, -0.000517) mut result := c1.csc() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.005174,0.036276) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.005174, 0.036276) result = c1.csc() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.228375,0.141363) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.228375, 0.141363) result = c1.csc() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -451,18 +450,18 @@ fn test_complex_csc() { fn test_complex_asin() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.617064,2.846289) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.617064, 2.846289) mut result := c1.asin() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.633984,2.305509) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.633984, 2.305509) result = c1.asin() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.427079,-1.528571) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.427079, -1.528571) result = c1.asin() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -470,18 +469,18 @@ fn test_complex_asin() { fn test_complex_acos() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.953732,-2.846289) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.953732, -2.846289) mut result := c1.acos() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(2.204780,-2.305509) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(2.204780, -2.305509) result = c1.acos() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(1.997875,1.528571) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(1.997875, 1.528571) result = c1.acos() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -489,18 +488,18 @@ fn test_complex_acos() { fn test_complex_atan() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(1.502727,0.094441) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(1.502727, 0.094441) mut result := c1.atan() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-1.448307,0.158997) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-1.448307, 0.158997) result = c1.atan() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-1.338973,-0.402359) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-1.338973, -0.402359) result = c1.atan() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -508,18 +507,18 @@ fn test_complex_atan() { fn test_complex_acot() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.068069,-0.094441) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.068069, -0.094441) mut result := c1.acot() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.122489,-0.158997) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.122489, -0.158997) result = c1.acot() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.231824,0.402359) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.231824, 0.402359) result = c1.acot() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -527,18 +526,18 @@ fn test_complex_acot() { fn test_complex_asec() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(1.503480,0.094668) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(1.503480, 0.094668) mut result := c1.asec() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(1.689547,0.160446) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(1.689547, 0.160446) result = c1.asec() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(1.757114,-0.396568) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(1.757114, -0.396568) result = c1.asec() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -546,18 +545,18 @@ fn test_complex_asec() { fn test_complex_acsc() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.067317,-0.094668) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.067317, -0.094668) mut result := c1.acsc() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.118751,-0.160446) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.118751, -0.160446) result = c1.acsc() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.186318,0.396568) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.186318, 0.396568) result = c1.acsc() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -565,18 +564,18 @@ fn test_complex_acsc() { fn test_complex_sinh() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(55.941968,48.754942) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(55.941968, 48.754942) mut result := c1.sinh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(6.548120,-7.619232) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(6.548120, -7.619232) result = c1.sinh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(0.489056,-1.403119) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(0.489056, -1.403119) result = c1.sinh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -584,18 +583,18 @@ fn test_complex_sinh() { fn test_complex_cosh() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(55.947047,48.750515) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(55.947047, 48.750515) mut result := c1.cosh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-6.580663,7.581553) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-6.580663, 7.581553) result = c1.cosh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.642148,1.068607) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.642148, 1.068607) result = c1.cosh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -603,18 +602,18 @@ fn test_complex_cosh() { fn test_complex_tanh() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.999988,0.000090) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.999988, 0.000090) mut result := c1.tanh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-1.000710,0.004908) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-1.000710, 0.004908) result = c1.tanh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-1.166736,0.243458) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-1.166736, 0.243458) result = c1.tanh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -622,18 +621,18 @@ fn test_complex_tanh() { fn test_complex_coth() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(1.000012,-0.000090) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(1.000012, -0.000090) mut result := c1.coth() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.999267,-0.004901) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.999267, -0.004901) result = c1.coth() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.821330,-0.171384) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.821330, -0.171384) result = c1.coth() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -641,18 +640,18 @@ fn test_complex_coth() { fn test_complex_sech() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.010160,-0.008853) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.010160, -0.008853) mut result := c1.sech() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.065294,-0.075225) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.065294, -0.075225) result = c1.sech() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.413149,-0.687527) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.413149, -0.687527) result = c1.sech() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -660,18 +659,18 @@ fn test_complex_sech() { fn test_complex_csch() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.010159,-0.008854) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.010159, -0.008854) mut result := c1.csch() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(0.064877,0.075490) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(0.064877, 0.075490) result = c1.csch() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(0.221501,0.635494) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(0.221501, 0.635494) result = c1.csch() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -679,18 +678,18 @@ fn test_complex_csch() { fn test_complex_asinh() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(2.844098,0.947341) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(2.844098, 0.947341) mut result := c1.asinh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-2.299914,0.917617) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-2.299914, 0.917617) result = c1.asinh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-1.469352,-1.063440) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-1.469352, -1.063440) result = c1.asinh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -698,18 +697,18 @@ fn test_complex_asinh() { fn test_complex_acosh() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(2.846289,0.953732) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(2.846289, 0.953732) mut result := c1.acosh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(2.305509,2.204780) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(2.305509, 2.204780) result = c1.acosh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(1.528571,-1.997875) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(1.528571, -1.997875) result = c1.acosh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -717,18 +716,18 @@ fn test_complex_acosh() { fn test_complex_atanh() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.067066,1.476056) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.067066, 1.476056) mut result := c1.atanh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.117501,1.409921) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.117501, 1.409921) result = c1.atanh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.173287,-1.178097) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.173287, -1.178097) result = c1.atanh() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -736,18 +735,18 @@ fn test_complex_atanh() { fn test_complex_acoth() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.067066,-0.094740) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.067066, -0.094740) mut result := c1.acoth() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.117501,-0.160875) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.117501, -0.160875) result = c1.acoth() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.173287,0.392699) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.173287, 0.392699) result = c1.acoth() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) @@ -774,18 +773,18 @@ fn test_complex_acoth() { fn test_complex_acsch() { // Tests were also verified on Wolfram Alpha - mut c1 := cmplx.complex(5,7) - mut c2 := cmplx.complex(0.067819,-0.094518) + mut c1 := cmplx.complex(5, 7) + mut c2 := cmplx.complex(0.067819, -0.094518) mut result := c1.acsch() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-3,4) - c2 = cmplx.complex(-0.121246,-0.159507) + c1 = cmplx.complex(-3, 4) + c2 = cmplx.complex(-0.121246, -0.159507) result = c1.acsch() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) - c1 = cmplx.complex(-1,-2) - c2 = cmplx.complex(-0.215612,0.401586) + c1 = cmplx.complex(-1, -2) + c2 = cmplx.complex(-0.215612, 0.401586) result = c1.acsch() // Some issue with precision comparison in f64 using == operator hence serializing to string assert result.str().eq(c2.str()) diff --git a/vlib/math/const.v b/vlib/math/const.v index 09eef1e78..7049680e4 100644 --- a/vlib/math/const.v +++ b/vlib/math/const.v @@ -4,33 +4,35 @@ module math pub const ( - e = 2.71828182845904523536028747135266249775724709369995957496696763 - pi = 3.14159265358979323846264338327950288419716939937510582097494459 - phi = 1.61803398874989484820458683436563811772030917980576286213544862 - tau = 6.28318530717958647692528676655900576839433879875021164194988918 - sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 - sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931 - sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779 + e = 2.71828182845904523536028747135266249775724709369995957496696763 + pi = 3.14159265358979323846264338327950288419716939937510582097494459 + phi = 1.61803398874989484820458683436563811772030917980576286213544862 + tau = 6.28318530717958647692528676655900576839433879875021164194988918 + sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 + sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931 + sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779 sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357 sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038 - ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 - log2_e = 1.0 / ln2 - ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 - log10_e = 1.0 / ln10 + ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 + log2_e = 1.0 / ln2 + ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 + log10_e = 1.0 / ln10 ) + // Floating-point limit values // max is the largest finite value representable by the type. // smallest_non_zero is the smallest positive, non-zero value representable by the type. pub const ( - max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23 + max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23 smallest_non_zero_f32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23) - max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52 + max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52 smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52) ) + // Integer limit values pub const ( - max_i8 = 127 - min_i8 = -128 + max_i8 = 127 + min_i8 = -128 max_i16 = 32767 min_i16 = -32768 max_i32 = 2147483647 @@ -40,7 +42,7 @@ pub const ( // consecutive subtraction by 1 min_i64 = i64(-9223372036854775807 - 1) max_i64 = i64(9223372036854775807) - max_u8 = 255 + max_u8 = 255 max_u16 = 65535 max_u32 = u32(4294967295) max_u64 = u64(18446744073709551615) diff --git a/vlib/math/factorial/factorial.v b/vlib/math/factorial/factorial.v index 7e36d35e5..9668d5d4c 100644 --- a/vlib/math/factorial/factorial.v +++ b/vlib/math/factorial/factorial.v @@ -14,7 +14,7 @@ pub fn factorial(n f64) f64 { // For a large postive argument (n >= FACTORIALS.len) return max_f64 if n >= factorials_table.len { - return math.max_f64 + return math.max_f64 } // Otherwise return n!. @@ -30,51 +30,51 @@ pub fn log_factorial(n f64) f64 { // For a large postive argument (n < 0) return max_f64 if n < 0 { - return -math.max_f64 + return -math.max_f64 } // If n < N then return ln(n!). if n != f64(i64(n)) { - return math.log_gamma(n+1) + return math.log_gamma(n + 1) } else if n < log_factorials_table.len { - return log_factorials_table[i64(n)] - } + return log_factorials_table[i64(n)] + } // Otherwise return asymptotic expansion of ln(n!). - return log_factorial_asymptotic_expansion(int(n)) + return log_factorial_asymptotic_expansion(int(n)) } fn log_factorial_asymptotic_expansion(n int) f64 { - m := 6 - mut term := []f64{} - xx := f64((n + 1) * (n + 1)) - mut xj := f64(n + 1) + m := 6 + mut term := []f64{} + xx := f64((n + 1) * (n + 1)) + mut xj := f64(n + 1) - log_factorial := log_sqrt_2pi - xj + (xj - 0.5) * math.log(xj) + log_factorial := log_sqrt_2pi - xj + (xj - 0.5) * math.log(xj) - mut i := 0 + mut i := 0 - for i = 0; i < m; i++ { - term << b_numbers[i] / xj - xj *= xx - } + for i = 0; i < m; i++ { + term << b_numbers[i] / xj + xj *= xx + } - mut sum := term[m-1] + mut sum := term[m - 1] - for i = m - 2; i >= 0; i-- { - if math.abs(sum) <= math.abs(term[i]) { - break - } + for i = m - 2; i >= 0; i-- { + if math.abs(sum) <= math.abs(term[i]) { + break + } - sum = term[i] - } + sum = term[i] + } - for i >= 0 { - sum += term[i] - i-- - } + for i >= 0 { + sum += term[i] + i-- + } - return log_factorial + sum + return log_factorial + sum } diff --git a/vlib/math/factorial/factorial_tables.v b/vlib/math/factorial/factorial_tables.v index 9104091b0..a669b0919 100644 --- a/vlib/math/factorial/factorial_tables.v +++ b/vlib/math/factorial/factorial_tables.v @@ -5,370 +5,371 @@ module factorial const ( - log_sqrt_2pi = 9.18938533204672741780329736e-1 + log_sqrt_2pi = 9.18938533204672741780329736e-1 - b_numbers = [ - /* Bernoulli numbers B(2),B(4),B(6),...,B(20). Only B(2),...,B(10) currently + b_numbers = [ + /* + Bernoulli numbers B(2),B(4),B(6),...,B(20). Only B(2),...,B(10) currently * used. - */ - f64(1.0 / (6.0 * 2.0 * 1.0)), - -1.0 / (30.0 * 4.0 * 3.0), - 1.0 / (42.0 * 6.0 * 5.0), - -1.0 / (30.0 * 8.0 * 7.0), - 5.0 / (66.0 * 10.0 * 9.0), - -691.0 / (2730.0 * 12.0 * 11.0), - 7.0 / (6.0 * 14.0 * 13.0), - -3617.0 / (510.0 * 16.0 * 15.0), - 43867.0 / (796.0 * 18.0 * 17.0), - -174611.0 / (330.0 * 20.0 * 19.0) - ] + */ + f64(1.0 / (6.0 * 2.0 * 1.0)), + -1.0 / (30.0 * 4.0 * 3.0), + 1.0 / (42.0 * 6.0 * 5.0), + -1.0 / (30.0 * 8.0 * 7.0), + 5.0 / (66.0 * 10.0 * 9.0), + -691.0 / (2730.0 * 12.0 * 11.0), + 7.0 / (6.0 * 14.0 * 13.0), + -3617.0 / (510.0 * 16.0 * 15.0), + 43867.0 / (796.0 * 18.0 * 17.0), + -174611.0 / (330.0 * 20.0 * 19.0), + ] - factorials_table = [ - f64(1.000000000000000000000e+0), /* 0! */ - 1.000000000000000000000e+0, /* 1! */ - 2.000000000000000000000e+0, /* 2! */ - 6.000000000000000000000e+0, /* 3! */ - 2.400000000000000000000e+1, /* 4! */ - 1.200000000000000000000e+2, /* 5! */ - 7.200000000000000000000e+2, /* 6! */ - 5.040000000000000000000e+3, /* 7! */ - 4.032000000000000000000e+4, /* 8! */ - 3.628800000000000000000e+5, /* 9! */ - 3.628800000000000000000e+6, /* 10! */ - 3.991680000000000000000e+7, /* 11! */ - 4.790016000000000000000e+8, /* 12! */ - 6.227020800000000000000e+9, /* 13! */ - 8.717829120000000000000e+10, /* 14! */ - 1.307674368000000000000e+12, /* 15! */ - 2.092278988800000000000e+13, /* 16! */ - 3.556874280960000000000e+14, /* 17! */ - 6.402373705728000000000e+15, /* 18! */ - 1.216451004088320000000e+17, /* 19! */ - 2.432902008176640000000e+18, /* 20! */ - 5.109094217170944000000e+19, /* 21! */ - 1.124000727777607680000e+21, /* 22! */ - 2.585201673888497664000e+22, /* 23! */ - 6.204484017332394393600e+23, /* 24! */ - 1.551121004333098598400e+25, /* 25! */ - 4.032914611266056355840e+26, /* 26! */ - 1.088886945041835216077e+28, /* 27! */ - 3.048883446117138605015e+29, /* 28! */ - 8.841761993739701954544e+30, /* 29! */ - 2.652528598121910586363e+32, /* 30! */ - 8.222838654177922817726e+33, /* 31! */ - 2.631308369336935301672e+35, /* 32! */ - 8.683317618811886495518e+36, /* 33! */ - 2.952327990396041408476e+38, /* 34! */ - 1.033314796638614492967e+40, /* 35! */ - 3.719933267899012174680e+41, /* 36! */ - 1.376375309122634504632e+43, /* 37! */ - 5.230226174666011117600e+44, /* 38! */ - 2.039788208119744335864e+46, /* 39! */ - 8.159152832478977343456e+47, /* 40! */ - 3.345252661316380710817e+49, /* 41! */ - 1.405006117752879898543e+51, /* 42! */ - 6.041526306337383563736e+52, /* 43! */ - 2.658271574788448768044e+54, /* 44! */ - 1.196222208654801945620e+56, /* 45! */ - 5.502622159812088949850e+57, /* 46! */ - 2.586232415111681806430e+59, /* 47! */ - 1.241391559253607267086e+61, /* 48! */ - 6.082818640342675608723e+62, /* 49! */ - 3.041409320171337804361e+64, /* 50! */ - 1.551118753287382280224e+66, /* 51! */ - 8.065817517094387857166e+67, /* 52! */ - 4.274883284060025564298e+69, /* 53! */ - 2.308436973392413804721e+71, /* 54! */ - 1.269640335365827592597e+73, /* 55! */ - 7.109985878048634518540e+74, /* 56! */ - 4.052691950487721675568e+76, /* 57! */ - 2.350561331282878571829e+78, /* 58! */ - 1.386831185456898357379e+80, /* 59! */ - 8.320987112741390144276e+81, /* 60! */ - 5.075802138772247988009e+83, /* 61! */ - 3.146997326038793752565e+85, /* 62! */ - 1.982608315404440064116e+87, /* 63! */ - 1.268869321858841641034e+89, /* 64! */ - 8.247650592082470666723e+90, /* 65! */ - 5.443449390774430640037e+92, /* 66! */ - 3.647111091818868528825e+94, /* 67! */ - 2.480035542436830599601e+96, /* 68! */ - 1.711224524281413113725e+98, /* 69! */ - 1.197857166996989179607e+100, /* 70! */ - 8.504785885678623175212e+101, /* 71! */ - 6.123445837688608686152e+103, /* 72! */ - 4.470115461512684340891e+105, /* 73! */ - 3.307885441519386412260e+107, /* 74! */ - 2.480914081139539809195e+109, /* 75! */ - 1.885494701666050254988e+111, /* 76! */ - 1.451830920282858696341e+113, /* 77! */ - 1.132428117820629783146e+115, /* 78! */ - 8.946182130782975286851e+116, /* 79! */ - 7.156945704626380229481e+118, /* 80! */ - 5.797126020747367985880e+120, /* 81! */ - 4.753643337012841748421e+122, /* 82! */ - 3.945523969720658651190e+124, /* 83! */ - 3.314240134565353266999e+126, /* 84! */ - 2.817104114380550276949e+128, /* 85! */ - 2.422709538367273238177e+130, /* 86! */ - 2.107757298379527717214e+132, /* 87! */ - 1.854826422573984391148e+134, /* 88! */ - 1.650795516090846108122e+136, /* 89! */ - 1.485715964481761497310e+138, /* 90! */ - 1.352001527678402962552e+140, /* 91! */ - 1.243841405464130725548e+142, /* 92! */ - 1.156772507081641574759e+144, /* 93! */ - 1.087366156656743080274e+146, /* 94! */ - 1.032997848823905926260e+148, /* 95! */ - 9.916779348709496892096e+149, /* 96! */ - 9.619275968248211985333e+151, /* 97! */ - 9.426890448883247745626e+153, /* 98! */ - 9.332621544394415268170e+155, /* 99! */ - 9.332621544394415268170e+157, /* 100! */ - 9.425947759838359420852e+159, /* 101! */ - 9.614466715035126609269e+161, /* 102! */ - 9.902900716486180407547e+163, /* 103! */ - 1.029901674514562762385e+166, /* 104! */ - 1.081396758240290900504e+168, /* 105! */ - 1.146280563734708354534e+170, /* 106! */ - 1.226520203196137939352e+172, /* 107! */ - 1.324641819451828974500e+174, /* 108! */ - 1.443859583202493582205e+176, /* 109! */ - 1.588245541522742940425e+178, /* 110! */ - 1.762952551090244663872e+180, /* 111! */ - 1.974506857221074023537e+182, /* 112! */ - 2.231192748659813646597e+184, /* 113! */ - 2.543559733472187557120e+186, /* 114! */ - 2.925093693493015690688e+188, /* 115! */ - 3.393108684451898201198e+190, /* 116! */ - 3.969937160808720895402e+192, /* 117! */ - 4.684525849754290656574e+194, /* 118! */ - 5.574585761207605881323e+196, /* 119! */ - 6.689502913449127057588e+198, /* 120! */ - 8.094298525273443739682e+200, /* 121! */ - 9.875044200833601362412e+202, /* 122! */ - 1.214630436702532967577e+205, /* 123! */ - 1.506141741511140879795e+207, /* 124! */ - 1.882677176888926099744e+209, /* 125! */ - 2.372173242880046885677e+211, /* 126! */ - 3.012660018457659544810e+213, /* 127! */ - 3.856204823625804217357e+215, /* 128! */ - 4.974504222477287440390e+217, /* 129! */ - 6.466855489220473672507e+219, /* 130! */ - 8.471580690878820510985e+221, /* 131! */ - 1.118248651196004307450e+224, /* 132! */ - 1.487270706090685728908e+226, /* 133! */ - 1.992942746161518876737e+228, /* 134! */ - 2.690472707318050483595e+230, /* 135! */ - 3.659042881952548657690e+232, /* 136! */ - 5.012888748274991661035e+234, /* 137! */ - 6.917786472619488492228e+236, /* 138! */ - 9.615723196941089004197e+238, /* 139! */ - 1.346201247571752460588e+241, /* 140! */ - 1.898143759076170969429e+243, /* 141! */ - 2.695364137888162776589e+245, /* 142! */ - 3.854370717180072770522e+247, /* 143! */ - 5.550293832739304789551e+249, /* 144! */ - 8.047926057471991944849e+251, /* 145! */ - 1.174997204390910823948e+254, /* 146! */ - 1.727245890454638911203e+256, /* 147! */ - 2.556323917872865588581e+258, /* 148! */ - 3.808922637630569726986e+260, /* 149! */ - 5.713383956445854590479e+262, /* 150! */ - 8.627209774233240431623e+264, /* 151! */ - 1.311335885683452545607e+267, /* 152! */ - 2.006343905095682394778e+269, /* 153! */ - 3.089769613847350887959e+271, /* 154! */ - 4.789142901463393876336e+273, /* 155! */ - 7.471062926282894447084e+275, /* 156! */ - 1.172956879426414428192e+278, /* 157! */ - 1.853271869493734796544e+280, /* 158! */ - 2.946702272495038326504e+282, /* 159! */ - 4.714723635992061322407e+284, /* 160! */ - 7.590705053947218729075e+286, /* 161! */ - 1.229694218739449434110e+289, /* 162! */ - 2.004401576545302577600e+291, /* 163! */ - 3.287218585534296227263e+293, /* 164! */ - 5.423910666131588774984e+295, /* 165! */ - 9.003691705778437366474e+297, /* 166! */ - 1.503616514864999040201e+300, /* 167! */ - 2.526075744973198387538e+302, /* 168! */ - 4.269068009004705274939e+304, /* 169! */ - 7.257415615307998967397e+306 /* 170! */ + factorials_table = [ + f64(1.000000000000000000000e+0), /* 0! */ + 1.000000000000000000000e+0, /* 1! */ + 2.000000000000000000000e+0, /* 2! */ + 6.000000000000000000000e+0, /* 3! */ + 2.400000000000000000000e+1, /* 4! */ + 1.200000000000000000000e+2, /* 5! */ + 7.200000000000000000000e+2, /* 6! */ + 5.040000000000000000000e+3, /* 7! */ + 4.032000000000000000000e+4, /* 8! */ + 3.628800000000000000000e+5, /* 9! */ + 3.628800000000000000000e+6, /* 10! */ + 3.991680000000000000000e+7, /* 11! */ + 4.790016000000000000000e+8, /* 12! */ + 6.227020800000000000000e+9, /* 13! */ + 8.717829120000000000000e+10, /* 14! */ + 1.307674368000000000000e+12, /* 15! */ + 2.092278988800000000000e+13, /* 16! */ + 3.556874280960000000000e+14, /* 17! */ + 6.402373705728000000000e+15, /* 18! */ + 1.216451004088320000000e+17, /* 19! */ + 2.432902008176640000000e+18, /* 20! */ + 5.109094217170944000000e+19, /* 21! */ + 1.124000727777607680000e+21, /* 22! */ + 2.585201673888497664000e+22, /* 23! */ + 6.204484017332394393600e+23, /* 24! */ + 1.551121004333098598400e+25, /* 25! */ + 4.032914611266056355840e+26, /* 26! */ + 1.088886945041835216077e+28, /* 27! */ + 3.048883446117138605015e+29, /* 28! */ + 8.841761993739701954544e+30, /* 29! */ + 2.652528598121910586363e+32, /* 30! */ + 8.222838654177922817726e+33, /* 31! */ + 2.631308369336935301672e+35, /* 32! */ + 8.683317618811886495518e+36, /* 33! */ + 2.952327990396041408476e+38, /* 34! */ + 1.033314796638614492967e+40, /* 35! */ + 3.719933267899012174680e+41, /* 36! */ + 1.376375309122634504632e+43, /* 37! */ + 5.230226174666011117600e+44, /* 38! */ + 2.039788208119744335864e+46, /* 39! */ + 8.159152832478977343456e+47, /* 40! */ + 3.345252661316380710817e+49, /* 41! */ + 1.405006117752879898543e+51, /* 42! */ + 6.041526306337383563736e+52, /* 43! */ + 2.658271574788448768044e+54, /* 44! */ + 1.196222208654801945620e+56, /* 45! */ + 5.502622159812088949850e+57, /* 46! */ + 2.586232415111681806430e+59, /* 47! */ + 1.241391559253607267086e+61, /* 48! */ + 6.082818640342675608723e+62, /* 49! */ + 3.041409320171337804361e+64, /* 50! */ + 1.551118753287382280224e+66, /* 51! */ + 8.065817517094387857166e+67, /* 52! */ + 4.274883284060025564298e+69, /* 53! */ + 2.308436973392413804721e+71, /* 54! */ + 1.269640335365827592597e+73, /* 55! */ + 7.109985878048634518540e+74, /* 56! */ + 4.052691950487721675568e+76, /* 57! */ + 2.350561331282878571829e+78, /* 58! */ + 1.386831185456898357379e+80, /* 59! */ + 8.320987112741390144276e+81, /* 60! */ + 5.075802138772247988009e+83, /* 61! */ + 3.146997326038793752565e+85, /* 62! */ + 1.982608315404440064116e+87, /* 63! */ + 1.268869321858841641034e+89, /* 64! */ + 8.247650592082470666723e+90, /* 65! */ + 5.443449390774430640037e+92, /* 66! */ + 3.647111091818868528825e+94, /* 67! */ + 2.480035542436830599601e+96, /* 68! */ + 1.711224524281413113725e+98, /* 69! */ + 1.197857166996989179607e+100, /* 70! */ + 8.504785885678623175212e+101, /* 71! */ + 6.123445837688608686152e+103, /* 72! */ + 4.470115461512684340891e+105, /* 73! */ + 3.307885441519386412260e+107, /* 74! */ + 2.480914081139539809195e+109, /* 75! */ + 1.885494701666050254988e+111, /* 76! */ + 1.451830920282858696341e+113, /* 77! */ + 1.132428117820629783146e+115, /* 78! */ + 8.946182130782975286851e+116, /* 79! */ + 7.156945704626380229481e+118, /* 80! */ + 5.797126020747367985880e+120, /* 81! */ + 4.753643337012841748421e+122, /* 82! */ + 3.945523969720658651190e+124, /* 83! */ + 3.314240134565353266999e+126, /* 84! */ + 2.817104114380550276949e+128, /* 85! */ + 2.422709538367273238177e+130, /* 86! */ + 2.107757298379527717214e+132, /* 87! */ + 1.854826422573984391148e+134, /* 88! */ + 1.650795516090846108122e+136, /* 89! */ + 1.485715964481761497310e+138, /* 90! */ + 1.352001527678402962552e+140, /* 91! */ + 1.243841405464130725548e+142, /* 92! */ + 1.156772507081641574759e+144, /* 93! */ + 1.087366156656743080274e+146, /* 94! */ + 1.032997848823905926260e+148, /* 95! */ + 9.916779348709496892096e+149, /* 96! */ + 9.619275968248211985333e+151, /* 97! */ + 9.426890448883247745626e+153, /* 98! */ + 9.332621544394415268170e+155, /* 99! */ + 9.332621544394415268170e+157, /* 100! */ + 9.425947759838359420852e+159, /* 101! */ + 9.614466715035126609269e+161, /* 102! */ + 9.902900716486180407547e+163, /* 103! */ + 1.029901674514562762385e+166, /* 104! */ + 1.081396758240290900504e+168, /* 105! */ + 1.146280563734708354534e+170, /* 106! */ + 1.226520203196137939352e+172, /* 107! */ + 1.324641819451828974500e+174, /* 108! */ + 1.443859583202493582205e+176, /* 109! */ + 1.588245541522742940425e+178, /* 110! */ + 1.762952551090244663872e+180, /* 111! */ + 1.974506857221074023537e+182, /* 112! */ + 2.231192748659813646597e+184, /* 113! */ + 2.543559733472187557120e+186, /* 114! */ + 2.925093693493015690688e+188, /* 115! */ + 3.393108684451898201198e+190, /* 116! */ + 3.969937160808720895402e+192, /* 117! */ + 4.684525849754290656574e+194, /* 118! */ + 5.574585761207605881323e+196, /* 119! */ + 6.689502913449127057588e+198, /* 120! */ + 8.094298525273443739682e+200, /* 121! */ + 9.875044200833601362412e+202, /* 122! */ + 1.214630436702532967577e+205, /* 123! */ + 1.506141741511140879795e+207, /* 124! */ + 1.882677176888926099744e+209, /* 125! */ + 2.372173242880046885677e+211, /* 126! */ + 3.012660018457659544810e+213, /* 127! */ + 3.856204823625804217357e+215, /* 128! */ + 4.974504222477287440390e+217, /* 129! */ + 6.466855489220473672507e+219, /* 130! */ + 8.471580690878820510985e+221, /* 131! */ + 1.118248651196004307450e+224, /* 132! */ + 1.487270706090685728908e+226, /* 133! */ + 1.992942746161518876737e+228, /* 134! */ + 2.690472707318050483595e+230, /* 135! */ + 3.659042881952548657690e+232, /* 136! */ + 5.012888748274991661035e+234, /* 137! */ + 6.917786472619488492228e+236, /* 138! */ + 9.615723196941089004197e+238, /* 139! */ + 1.346201247571752460588e+241, /* 140! */ + 1.898143759076170969429e+243, /* 141! */ + 2.695364137888162776589e+245, /* 142! */ + 3.854370717180072770522e+247, /* 143! */ + 5.550293832739304789551e+249, /* 144! */ + 8.047926057471991944849e+251, /* 145! */ + 1.174997204390910823948e+254, /* 146! */ + 1.727245890454638911203e+256, /* 147! */ + 2.556323917872865588581e+258, /* 148! */ + 3.808922637630569726986e+260, /* 149! */ + 5.713383956445854590479e+262, /* 150! */ + 8.627209774233240431623e+264, /* 151! */ + 1.311335885683452545607e+267, /* 152! */ + 2.006343905095682394778e+269, /* 153! */ + 3.089769613847350887959e+271, /* 154! */ + 4.789142901463393876336e+273, /* 155! */ + 7.471062926282894447084e+275, /* 156! */ + 1.172956879426414428192e+278, /* 157! */ + 1.853271869493734796544e+280, /* 158! */ + 2.946702272495038326504e+282, /* 159! */ + 4.714723635992061322407e+284, /* 160! */ + 7.590705053947218729075e+286, /* 161! */ + 1.229694218739449434110e+289, /* 162! */ + 2.004401576545302577600e+291, /* 163! */ + 3.287218585534296227263e+293, /* 164! */ + 5.423910666131588774984e+295, /* 165! */ + 9.003691705778437366474e+297, /* 166! */ + 1.503616514864999040201e+300, /* 167! */ + 2.526075744973198387538e+302, /* 168! */ + 4.269068009004705274939e+304, /* 169! */ + 7.257415615307998967397e+306, /* 170! */ ] log_factorials_table = [ - f64(0.000000000000000000000e+0), /* 0! */ - 0.000000000000000000000e+0, /* 1! */ - 6.931471805599453094172e-1, /* 2! */ - 1.791759469228055000812e+0, /* 3! */ - 3.178053830347945619647e+0, /* 4! */ - 4.787491742782045994248e+0, /* 5! */ - 6.579251212010100995060e+0, /* 6! */ - 8.525161361065414300166e+0, /* 7! */ - 1.060460290274525022842e+1, /* 8! */ - 1.280182748008146961121e+1, /* 9! */ - 1.510441257307551529523e+1, /* 10! */ - 1.750230784587388583929e+1, /* 11! */ - 1.998721449566188614952e+1, /* 12! */ - 2.255216385312342288557e+1, /* 13! */ - 2.519122118273868150009e+1, /* 14! */ - 2.789927138384089156609e+1, /* 15! */ - 3.067186010608067280376e+1, /* 16! */ - 3.350507345013688888401e+1, /* 17! */ - 3.639544520803305357622e+1, /* 18! */ - 3.933988418719949403622e+1, /* 19! */ - 4.233561646075348502966e+1, /* 20! */ - 4.538013889847690802616e+1, /* 21! */ - 4.847118135183522387964e+1, /* 22! */ - 5.160667556776437357045e+1, /* 23! */ - 5.478472939811231919009e+1, /* 24! */ - 5.800360522298051993929e+1, /* 25! */ - 6.126170176100200198477e+1, /* 26! */ - 6.455753862700633105895e+1, /* 27! */ - 6.788974313718153498289e+1, /* 28! */ - 7.125703896716800901007e+1, /* 29! */ - 7.465823634883016438549e+1, /* 30! */ - 7.809222355331531063142e+1, /* 31! */ - 8.155795945611503717850e+1, /* 32! */ - 8.505446701758151741396e+1, /* 33! */ - 8.858082754219767880363e+1, /* 34! */ - 9.213617560368709248333e+1, /* 35! */ - 9.571969454214320248496e+1, /* 36! */ - 9.933061245478742692933e+1, /* 37! */ - 1.029681986145138126988e+2, /* 38! */ - 1.066317602606434591262e+2, /* 39! */ - 1.103206397147573954291e+2, /* 40! */ - 1.140342117814617032329e+2, /* 41! */ - 1.177718813997450715388e+2, /* 42! */ - 1.215330815154386339623e+2, /* 43! */ - 1.253172711493568951252e+2, /* 44! */ - 1.291239336391272148826e+2, /* 45! */ - 1.329525750356163098828e+2, /* 46! */ - 1.368027226373263684696e+2, /* 47! */ - 1.406739236482342593987e+2, /* 48! */ - 1.445657439463448860089e+2, /* 49! */ - 1.484777669517730320675e+2, /* 50! */ - 1.524095925844973578392e+2, /* 51! */ - 1.563608363030787851941e+2, /* 52! */ - 1.603311282166309070282e+2, /* 53! */ - 1.643201122631951814118e+2, /* 54! */ - 1.683274454484276523305e+2, /* 55! */ - 1.723527971391628015638e+2, /* 56! */ - 1.763958484069973517152e+2, /* 57! */ - 1.804562914175437710518e+2, /* 58! */ - 1.845338288614494905025e+2, /* 59! */ - 1.886281734236715911873e+2, /* 60! */ - 1.927390472878449024360e+2, /* 61! */ - 1.968661816728899939914e+2, /* 62! */ - 2.010093163992815266793e+2, /* 63! */ - 2.051681994826411985358e+2, /* 64! */ - 2.093425867525368356464e+2, /* 65! */ - 2.135322414945632611913e+2, /* 66! */ - 2.177369341139542272510e+2, /* 67! */ - 2.219564418191303339501e+2, /* 68! */ - 2.261905483237275933323e+2, /* 69! */ - 2.304390435657769523214e+2, /* 70! */ - 2.347017234428182677427e+2, /* 71! */ - 2.389783895618343230538e+2, /* 72! */ - 2.432688490029827141829e+2, /* 73! */ - 2.475729140961868839366e+2, /* 74! */ - 2.518904022097231943772e+2, /* 75! */ - 2.562211355500095254561e+2, /* 76! */ - 2.605649409718632093053e+2, /* 77! */ - 2.649216497985528010421e+2, /* 78! */ - 2.692910976510198225363e+2, /* 79! */ - 2.736731242856937041486e+2, /* 80! */ - 2.780675734403661429141e+2, /* 81! */ - 2.824742926876303960274e+2, /* 82! */ - 2.868931332954269939509e+2, /* 83! */ - 2.913239500942703075662e+2, /* 84! */ - 2.957666013507606240211e+2, /* 85! */ - 3.002209486470141317540e+2, /* 86! */ - 3.046868567656687154726e+2, /* 87! */ - 3.091641935801469219449e+2, /* 88! */ - 3.136528299498790617832e+2, /* 89! */ - 3.181526396202093268500e+2, /* 90! */ - 3.226634991267261768912e+2, /* 91! */ - 3.271852877037752172008e+2, /* 92! */ - 3.317178871969284731381e+2, /* 93! */ - 3.362611819791984770344e+2, /* 94! */ - 3.408150588707990178690e+2, /* 95! */ - 3.453794070622668541074e+2, /* 96! */ - 3.499541180407702369296e+2, /* 97! */ - 3.545390855194408088492e+2, /* 98! */ - 3.591342053695753987760e+2, /* 99! */ - 3.637393755555634901441e+2, /* 100! */ - 3.683544960724047495950e+2, /* 101! */ - 3.729794688856890206760e+2, /* 102! */ - 3.776141978739186564468e+2, /* 103! */ - 3.822585887730600291111e+2, /* 104! */ - 3.869125491232175524822e+2, /* 105! */ - 3.915759882173296196258e+2, /* 106! */ - 3.962488170517915257991e+2, /* 107! */ - 4.009309482789157454921e+2, /* 108! */ - 4.056222961611448891925e+2, /* 109! */ - 4.103227765269373054205e+2, /* 110! */ - 4.150323067282496395563e+2, /* 111! */ - 4.197508055995447340991e+2, /* 112! */ - 4.244781934182570746677e+2, /* 113! */ - 4.292143918666515701285e+2, /* 114! */ - 4.339593239950148201939e+2, /* 115! */ - 4.387129141861211848399e+2, /* 116! */ - 4.434750881209189409588e+2, /* 117! */ - 4.482457727453846057188e+2, /* 118! */ - 4.530248962384961351041e+2, /* 119! */ - 4.578123879812781810984e+2, /* 120! */ - 4.626081785268749221865e+2, /* 121! */ - 4.674121995716081787447e+2, /* 122! */ - 4.722243839269805962399e+2, /* 123! */ - 4.770446654925856331047e+2, /* 124! */ - 4.818729792298879342285e+2, /* 125! */ - 4.867092611368394122258e+2, /* 126! */ - 4.915534482232980034989e+2, /* 127! */ - 4.964054784872176206648e+2, /* 128! */ - 5.012652908915792927797e+2, /* 129! */ - 5.061328253420348751997e+2, /* 130! */ - 5.110080226652360267439e+2, /* 131! */ - 5.158908245878223975982e+2, /* 132! */ - 5.207811737160441513633e+2, /* 133! */ - 5.256790135159950627324e+2, /* 134! */ - 5.305842882944334921812e+2, /* 135! */ - 5.354969431801695441897e+2, /* 136! */ - 5.404169241059976691050e+2, /* 137! */ - 5.453441777911548737966e+2, /* 138! */ - 5.502786517242855655538e+2, /* 139! */ - 5.552202941468948698523e+2, /* 140! */ - 5.601690540372730381305e+2, /* 141! */ - 5.651248810948742988613e+2, /* 142! */ - 5.700877257251342061414e+2, /* 143! */ - 5.750575390247102067619e+2, /* 144! */ - 5.800342727671307811636e+2, /* 145! */ - 5.850178793888391176022e+2, /* 146! */ - 5.900083119756178539038e+2, /* 147! */ - 5.950055242493819689670e+2, /* 148! */ - 6.000094705553274281080e+2, /* 149! */ - 6.050201058494236838580e+2, /* 150! */ - 6.100373856862386081868e+2, /* 151! */ - 6.150612662070848845750e+2, /* 152! */ - 6.200917041284773200381e+2, /* 153! */ - 6.251286567308909491967e+2, /* 154! */ - 6.301720818478101958172e+2, /* 155! */ - 6.352219378550597328635e+2, /* 156! */ - 6.402781836604080409209e+2, /* 157! */ - 6.453407786934350077245e+2, /* 158! */ - 6.504096828956552392500e+2, /* 159! */ - 6.554848567108890661717e+2, /* 160! */ - 6.605662610758735291676e+2, /* 161! */ - 6.656538574111059132426e+2, /* 162! */ - 6.707476076119126755767e+2, /* 163! */ - 6.758474740397368739994e+2, /* 164! */ - 6.809534195136374546094e+2, /* 165! */ - 6.860654073019939978423e+2, /* 166! */ - 6.911834011144107529496e+2, /* 167! */ - 6.963073650938140118743e+2, /* 168! */ - 7.014372638087370853465e+2, /* 169! */ - 7.065730622457873471107e+2, /* 170! */ - 7.117147258022900069535e+2, /* 171! */ + f64(0.000000000000000000000e+0), /* 0! */ + 0.000000000000000000000e+0, /* 1! */ + 6.931471805599453094172e-1, /* 2! */ + 1.791759469228055000812e+0, /* 3! */ + 3.178053830347945619647e+0, /* 4! */ + 4.787491742782045994248e+0, /* 5! */ + 6.579251212010100995060e+0, /* 6! */ + 8.525161361065414300166e+0, /* 7! */ + 1.060460290274525022842e+1, /* 8! */ + 1.280182748008146961121e+1, /* 9! */ + 1.510441257307551529523e+1, /* 10! */ + 1.750230784587388583929e+1, /* 11! */ + 1.998721449566188614952e+1, /* 12! */ + 2.255216385312342288557e+1, /* 13! */ + 2.519122118273868150009e+1, /* 14! */ + 2.789927138384089156609e+1, /* 15! */ + 3.067186010608067280376e+1, /* 16! */ + 3.350507345013688888401e+1, /* 17! */ + 3.639544520803305357622e+1, /* 18! */ + 3.933988418719949403622e+1, /* 19! */ + 4.233561646075348502966e+1, /* 20! */ + 4.538013889847690802616e+1, /* 21! */ + 4.847118135183522387964e+1, /* 22! */ + 5.160667556776437357045e+1, /* 23! */ + 5.478472939811231919009e+1, /* 24! */ + 5.800360522298051993929e+1, /* 25! */ + 6.126170176100200198477e+1, /* 26! */ + 6.455753862700633105895e+1, /* 27! */ + 6.788974313718153498289e+1, /* 28! */ + 7.125703896716800901007e+1, /* 29! */ + 7.465823634883016438549e+1, /* 30! */ + 7.809222355331531063142e+1, /* 31! */ + 8.155795945611503717850e+1, /* 32! */ + 8.505446701758151741396e+1, /* 33! */ + 8.858082754219767880363e+1, /* 34! */ + 9.213617560368709248333e+1, /* 35! */ + 9.571969454214320248496e+1, /* 36! */ + 9.933061245478742692933e+1, /* 37! */ + 1.029681986145138126988e+2, /* 38! */ + 1.066317602606434591262e+2, /* 39! */ + 1.103206397147573954291e+2, /* 40! */ + 1.140342117814617032329e+2, /* 41! */ + 1.177718813997450715388e+2, /* 42! */ + 1.215330815154386339623e+2, /* 43! */ + 1.253172711493568951252e+2, /* 44! */ + 1.291239336391272148826e+2, /* 45! */ + 1.329525750356163098828e+2, /* 46! */ + 1.368027226373263684696e+2, /* 47! */ + 1.406739236482342593987e+2, /* 48! */ + 1.445657439463448860089e+2, /* 49! */ + 1.484777669517730320675e+2, /* 50! */ + 1.524095925844973578392e+2, /* 51! */ + 1.563608363030787851941e+2, /* 52! */ + 1.603311282166309070282e+2, /* 53! */ + 1.643201122631951814118e+2, /* 54! */ + 1.683274454484276523305e+2, /* 55! */ + 1.723527971391628015638e+2, /* 56! */ + 1.763958484069973517152e+2, /* 57! */ + 1.804562914175437710518e+2, /* 58! */ + 1.845338288614494905025e+2, /* 59! */ + 1.886281734236715911873e+2, /* 60! */ + 1.927390472878449024360e+2, /* 61! */ + 1.968661816728899939914e+2, /* 62! */ + 2.010093163992815266793e+2, /* 63! */ + 2.051681994826411985358e+2, /* 64! */ + 2.093425867525368356464e+2, /* 65! */ + 2.135322414945632611913e+2, /* 66! */ + 2.177369341139542272510e+2, /* 67! */ + 2.219564418191303339501e+2, /* 68! */ + 2.261905483237275933323e+2, /* 69! */ + 2.304390435657769523214e+2, /* 70! */ + 2.347017234428182677427e+2, /* 71! */ + 2.389783895618343230538e+2, /* 72! */ + 2.432688490029827141829e+2, /* 73! */ + 2.475729140961868839366e+2, /* 74! */ + 2.518904022097231943772e+2, /* 75! */ + 2.562211355500095254561e+2, /* 76! */ + 2.605649409718632093053e+2, /* 77! */ + 2.649216497985528010421e+2, /* 78! */ + 2.692910976510198225363e+2, /* 79! */ + 2.736731242856937041486e+2, /* 80! */ + 2.780675734403661429141e+2, /* 81! */ + 2.824742926876303960274e+2, /* 82! */ + 2.868931332954269939509e+2, /* 83! */ + 2.913239500942703075662e+2, /* 84! */ + 2.957666013507606240211e+2, /* 85! */ + 3.002209486470141317540e+2, /* 86! */ + 3.046868567656687154726e+2, /* 87! */ + 3.091641935801469219449e+2, /* 88! */ + 3.136528299498790617832e+2, /* 89! */ + 3.181526396202093268500e+2, /* 90! */ + 3.226634991267261768912e+2, /* 91! */ + 3.271852877037752172008e+2, /* 92! */ + 3.317178871969284731381e+2, /* 93! */ + 3.362611819791984770344e+2, /* 94! */ + 3.408150588707990178690e+2, /* 95! */ + 3.453794070622668541074e+2, /* 96! */ + 3.499541180407702369296e+2, /* 97! */ + 3.545390855194408088492e+2, /* 98! */ + 3.591342053695753987760e+2, /* 99! */ + 3.637393755555634901441e+2, /* 100! */ + 3.683544960724047495950e+2, /* 101! */ + 3.729794688856890206760e+2, /* 102! */ + 3.776141978739186564468e+2, /* 103! */ + 3.822585887730600291111e+2, /* 104! */ + 3.869125491232175524822e+2, /* 105! */ + 3.915759882173296196258e+2, /* 106! */ + 3.962488170517915257991e+2, /* 107! */ + 4.009309482789157454921e+2, /* 108! */ + 4.056222961611448891925e+2, /* 109! */ + 4.103227765269373054205e+2, /* 110! */ + 4.150323067282496395563e+2, /* 111! */ + 4.197508055995447340991e+2, /* 112! */ + 4.244781934182570746677e+2, /* 113! */ + 4.292143918666515701285e+2, /* 114! */ + 4.339593239950148201939e+2, /* 115! */ + 4.387129141861211848399e+2, /* 116! */ + 4.434750881209189409588e+2, /* 117! */ + 4.482457727453846057188e+2, /* 118! */ + 4.530248962384961351041e+2, /* 119! */ + 4.578123879812781810984e+2, /* 120! */ + 4.626081785268749221865e+2, /* 121! */ + 4.674121995716081787447e+2, /* 122! */ + 4.722243839269805962399e+2, /* 123! */ + 4.770446654925856331047e+2, /* 124! */ + 4.818729792298879342285e+2, /* 125! */ + 4.867092611368394122258e+2, /* 126! */ + 4.915534482232980034989e+2, /* 127! */ + 4.964054784872176206648e+2, /* 128! */ + 5.012652908915792927797e+2, /* 129! */ + 5.061328253420348751997e+2, /* 130! */ + 5.110080226652360267439e+2, /* 131! */ + 5.158908245878223975982e+2, /* 132! */ + 5.207811737160441513633e+2, /* 133! */ + 5.256790135159950627324e+2, /* 134! */ + 5.305842882944334921812e+2, /* 135! */ + 5.354969431801695441897e+2, /* 136! */ + 5.404169241059976691050e+2, /* 137! */ + 5.453441777911548737966e+2, /* 138! */ + 5.502786517242855655538e+2, /* 139! */ + 5.552202941468948698523e+2, /* 140! */ + 5.601690540372730381305e+2, /* 141! */ + 5.651248810948742988613e+2, /* 142! */ + 5.700877257251342061414e+2, /* 143! */ + 5.750575390247102067619e+2, /* 144! */ + 5.800342727671307811636e+2, /* 145! */ + 5.850178793888391176022e+2, /* 146! */ + 5.900083119756178539038e+2, /* 147! */ + 5.950055242493819689670e+2, /* 148! */ + 6.000094705553274281080e+2, /* 149! */ + 6.050201058494236838580e+2, /* 150! */ + 6.100373856862386081868e+2, /* 151! */ + 6.150612662070848845750e+2, /* 152! */ + 6.200917041284773200381e+2, /* 153! */ + 6.251286567308909491967e+2, /* 154! */ + 6.301720818478101958172e+2, /* 155! */ + 6.352219378550597328635e+2, /* 156! */ + 6.402781836604080409209e+2, /* 157! */ + 6.453407786934350077245e+2, /* 158! */ + 6.504096828956552392500e+2, /* 159! */ + 6.554848567108890661717e+2, /* 160! */ + 6.605662610758735291676e+2, /* 161! */ + 6.656538574111059132426e+2, /* 162! */ + 6.707476076119126755767e+2, /* 163! */ + 6.758474740397368739994e+2, /* 164! */ + 6.809534195136374546094e+2, /* 165! */ + 6.860654073019939978423e+2, /* 166! */ + 6.911834011144107529496e+2, /* 167! */ + 6.963073650938140118743e+2, /* 168! */ + 7.014372638087370853465e+2, /* 169! */ + 7.065730622457873471107e+2, /* 170! */ + 7.117147258022900069535e+2, /* 171! */ ] ) diff --git a/vlib/math/fractions/approximations.v b/vlib/math/fractions/approximations.v index dc016b036..dd4f85563 100644 --- a/vlib/math/fractions/approximations.v +++ b/vlib/math/fractions/approximations.v @@ -73,13 +73,13 @@ fn eval_cf(whole i64, den []i64) Fraction { // within the default epsilon value (1.0e-4). This means the result will // be accurate to 3 places after the decimal. pub fn approximate(val f64) Fraction { - return approximate_with_eps(val, default_eps) + return approximate_with_eps(val, fractions.default_eps) } // approximate_with_eps returns a Fraction pub fn approximate_with_eps(val f64, eps f64) Fraction { if val == 0.0 { - return zero + return fractions.zero } if eps < 0.0 { panic('Epsilon value cannot be negative.') @@ -96,12 +96,12 @@ pub fn approximate_with_eps(val f64, eps f64) Fraction { return fraction(whole, 1) } mut d := []i64{} - mut partial := zero + mut partial := fractions.zero // We must complete the approximation within the maximum number of // itertations allowed. If we can't panic. // Empirically tested: the hardest constant to approximate is the // golden ratio (math.phi) and for f64s, it only needs 38 iterations. - for _ in 0 .. max_iterations { + for _ in 0 .. fractions.max_iterations { // We calculate the reciprocal. That's why the numerator is // always 1. frac = 1.0 / frac @@ -115,5 +115,5 @@ pub fn approximate_with_eps(val f64, eps f64) Fraction { } frac -= f64(den) } - panic("Couldn\'t converge. Please create an issue on https://github.com/vlang/v") + panic("Couldn't converge. Please create an issue on https://github.com/vlang/v") } diff --git a/vlib/math/fractions/approximations_test.v b/vlib/math/fractions/approximations_test.v index 40dbeecdf..5ee92bf04 100644 --- a/vlib/math/fractions/approximations_test.v +++ b/vlib/math/fractions/approximations_test.v @@ -69,27 +69,33 @@ fn test_140710_232() { } fn test_pi_1_digit() { - assert fractions.approximate_with_eps(math.pi, 5.0e-2).equals(fractions.fraction(22, 7)) + assert fractions.approximate_with_eps(math.pi, 5.0e-2).equals(fractions.fraction(22, + 7)) } fn test_pi_2_digits() { - assert fractions.approximate_with_eps(math.pi, 5.0e-3).equals(fractions.fraction(22, 7)) + assert fractions.approximate_with_eps(math.pi, 5.0e-3).equals(fractions.fraction(22, + 7)) } fn test_pi_3_digits() { - assert fractions.approximate_with_eps(math.pi, 5.0e-4).equals(fractions.fraction(333, 106)) + assert fractions.approximate_with_eps(math.pi, 5.0e-4).equals(fractions.fraction(333, + 106)) } fn test_pi_4_digits() { - assert fractions.approximate_with_eps(math.pi, 5.0e-5).equals(fractions.fraction(355, 113)) + assert fractions.approximate_with_eps(math.pi, 5.0e-5).equals(fractions.fraction(355, + 113)) } fn test_pi_5_digits() { - assert fractions.approximate_with_eps(math.pi, 5.0e-6).equals(fractions.fraction(355, 113)) + assert fractions.approximate_with_eps(math.pi, 5.0e-6).equals(fractions.fraction(355, + 113)) } fn test_pi_6_digits() { - assert fractions.approximate_with_eps(math.pi, 5.0e-7).equals(fractions.fraction(355, 113)) + assert fractions.approximate_with_eps(math.pi, 5.0e-7).equals(fractions.fraction(355, + 113)) } fn test_pi_7_digits() { @@ -123,15 +129,18 @@ fn test_pi_12_digits() { } fn test_phi_1_digit() { - assert fractions.approximate_with_eps(math.phi, 5.0e-2).equals(fractions.fraction(5, 3)) + assert fractions.approximate_with_eps(math.phi, 5.0e-2).equals(fractions.fraction(5, + 3)) } fn test_phi_2_digits() { - assert fractions.approximate_with_eps(math.phi, 5.0e-3).equals(fractions.fraction(21, 13)) + assert fractions.approximate_with_eps(math.phi, 5.0e-3).equals(fractions.fraction(21, + 13)) } fn test_phi_3_digits() { - assert fractions.approximate_with_eps(math.phi, 5.0e-4).equals(fractions.fraction(55, 34)) + assert fractions.approximate_with_eps(math.phi, 5.0e-4).equals(fractions.fraction(55, + 34)) } fn test_phi_4_digits() { diff --git a/vlib/math/math.js.v b/vlib/math/math.js.v index 776172744..437e8ab05 100644 --- a/vlib/math/math.js.v +++ b/vlib/math/math.js.v @@ -6,7 +6,10 @@ module math // TODO : The commented out functions need either a native V implementation, a // JS specific implementation, or use some other JS math library, such as // https://github.com/josdejong/mathjs -fn JS.Math.abs(x f64) f64 // Replaces C.fabs + +// Replaces C.fabs +fn JS.Math.abs(x f64) f64 + fn JS.Math.acos(x f64) f64 fn JS.Math.asin(x f64) f64 fn JS.Math.atan(x f64) f64 @@ -15,23 +18,28 @@ fn JS.Math.cbrt(x f64) f64 fn JS.Math.ceil(x f64) f64 fn JS.Math.cos(x f64) f64 fn JS.Math.cosh(x f64) f64 -//fn JS.Math.erf(x f64) f64 // Not in standard JS Math object -//fn JS.Math.erfc(x f64) f64 // Not in standard JS Math object + +// fn JS.Math.erf(x f64) f64 // Not in standard JS Math object +// fn JS.Math.erfc(x f64) f64 // Not in standard JS Math object fn JS.Math.exp(x f64) f64 -//fn JS.Math.exp2(x f64) f64 // Not in standard JS Math object + +// fn JS.Math.exp2(x f64) f64 // Not in standard JS Math object fn JS.Math.floor(x f64) f64 -//fn JS.Math.fmod(x f64, y f64) f64 // Not in standard JS Math object -//fn JS.Math.hypot(x f64, y f64) f64 // Not in standard JS Math object + +// fn JS.Math.fmod(x f64, y f64) f64 // Not in standard JS Math object +// fn JS.Math.hypot(x f64, y f64) f64 // Not in standard JS Math object fn JS.Math.log(x f64) f64 -//fn JS.Math.log2(x f64) f64 // Not in standard JS Math object -//fn JS.Math.log10(x f64) f64 // Not in standard JS Math object -//fn JS.Math.lgamma(x f64) f64 // Not in standard JS Math object + +// fn JS.Math.log2(x f64) f64 // Not in standard JS Math object +// fn JS.Math.log10(x f64) f64 // Not in standard JS Math object +// fn JS.Math.lgamma(x f64) f64 // Not in standard JS Math object fn JS.Math.pow(x f64, y f64) f64 fn JS.Math.round(x f64) f64 fn JS.Math.sin(x f64) f64 fn JS.Math.sinh(x f64) f64 fn JS.Math.sqrt(x f64) f64 -//fn JS.Math.tgamma(x f64) f64 // Not in standard JS Math object + +// fn JS.Math.tgamma(x f64) f64 // Not in standard JS Math object fn JS.Math.tan(x f64) f64 fn JS.Math.tanh(x f64) f64 fn JS.Math.trunc(x f64) f64 @@ -66,7 +74,7 @@ pub fn atan(a f64) f64 { // atan2 calculates inverse tangent with two arguments, returns the angle between the X axis and the point. [inline] -pub fn atan2(a, b f64) f64 { +pub fn atan2(a f64, b f64) f64 { return JS.Math.atan2(a, b) } @@ -132,7 +140,7 @@ pub fn floor(a f64) f64 { // fmod returns the floating-point remainder of number / denom (rounded towards zero): [inline] -pub fn fmod(a, b f64) f64 { +pub fn fmod(a f64, b f64) f64 { return JS.Math.fmod(a, b) } @@ -144,7 +152,7 @@ pub fn gamma(a f64) f64 { // Returns hypotenuse of a right triangle. [inline] -pub fn hypot(a, b f64) f64 { +pub fn hypot(a f64, b f64) f64 { return JS.Math.hypot(a, b) } @@ -174,19 +182,19 @@ pub fn log_gamma(a f64) f64 { // log_n calculates base-N logarithm of the provided value. [inline] -pub fn log_n(a, b f64) f64 { +pub fn log_n(a f64, b f64) f64 { return JS.Math.log(a) / JS.Math.log(b) } // pow returns base raised to the provided power. [inline] -pub fn pow(a, b f64) f64 { +pub fn pow(a f64, b f64) f64 { return JS.Math.pow(a, b) } // powf returns base raised to the provided power. (float32) [inline] -pub fn powf(a, b f32) f32 { +pub fn powf(a f32, b f32) f32 { return f32(JS.Math.pow(a, b)) } diff --git a/vlib/math/stats/stats.v b/vlib/math/stats/stats.v index b13b27d37..d7317bf1e 100644 --- a/vlib/math/stats/stats.v +++ b/vlib/math/stats/stats.v @@ -26,7 +26,6 @@ import math // range - Range of the Array ( max - min ) // ----------------------------------------------------------------------- - // Measure of Occurance // Frequency of a given number // Based on @@ -56,7 +55,7 @@ pub fn mean(arr []f64) f64 { for v in arr { sum += v } - return sum/f64(arr.len) + return sum / f64(arr.len) } // Measure of Central Tendancy @@ -71,7 +70,7 @@ pub fn geometric_mean(arr []f64) f64 { for v in arr { sum *= v } - return math.pow(sum,f64(1)/arr.len) + return math.pow(sum, f64(1) / arr.len) } // Measure of Central Tendancy @@ -84,9 +83,9 @@ pub fn harmonic_mean(arr []f64) f64 { } mut sum := f64(0) for v in arr { - sum += f64(1)/v + sum += f64(1) / v } - return f64(arr.len)/sum + return f64(arr.len) / sum } // Measure of Central Tendancy @@ -98,11 +97,10 @@ pub fn median(arr []f64) f64 { return f64(0) } if arr.len % 2 == 0 { - mid := (arr.len/2)-1 - return (arr[mid] + arr[mid+1])/f64(2) - } - else { - return arr[((arr.len-1)/2)] + mid := (arr.len / 2) - 1 + return (arr[mid] + arr[mid + 1]) / f64(2) + } else { + return arr[((arr.len - 1) / 2)] } } @@ -116,10 +114,10 @@ pub fn mode(arr []f64) f64 { } mut freqs := []int{} for v in arr { - freqs< freqs[max] { max = i } @@ -136,9 +134,9 @@ pub fn rms(arr []f64) f64 { } mut sum := f64(0) for v in arr { - sum += math.pow(v,2) + sum += math.pow(v, 2) } - return math.sqrt(sum/f64(arr.len)) + return math.sqrt(sum / f64(arr.len)) } // Measure of Dispersion / Spread @@ -152,9 +150,9 @@ pub fn population_variance(arr []f64) f64 { m := mean(arr) mut sum := f64(0) for v in arr { - sum += math.pow(v-m,2) + sum += math.pow(v - m, 2) } - return sum/f64(arr.len) + return sum / f64(arr.len) } // Measure of Dispersion / Spread @@ -168,9 +166,9 @@ pub fn sample_variance(arr []f64) f64 { m := mean(arr) mut sum := f64(0) for v in arr { - sum += math.pow(v-m,2) + sum += math.pow(v - m, 2) } - return sum/f64(arr.len-1) + return sum / f64(arr.len - 1) } // Measure of Dispersion / Spread @@ -206,9 +204,9 @@ pub fn mean_absdev(arr []f64) f64 { amean := mean(arr) mut sum := f64(0) for v in arr { - sum += math.abs(v-amean) + sum += math.abs(v - amean) } - return sum/f64(arr.len) + return sum / f64(arr.len) } // Minimum of the given input array diff --git a/vlib/math/stats/stats_test.v b/vlib/math/stats/stats_test.v index 7b4bbbe75..9e1d882cb 100644 --- a/vlib/math/stats/stats_test.v +++ b/vlib/math/stats/stats_test.v @@ -3,12 +3,12 @@ import math fn test_freq() { // Tests were also verified on Wolfram Alpha - data := [f64(10.0),f64(10.0),f64(5.9),f64(2.7)] - mut o := stats.freq(data,10.0) + data := [f64(10.0), f64(10.0), f64(5.9), f64(2.7)] + mut o := stats.freq(data, 10.0) assert o == 2 - o = stats.freq(data,2.7) + o = stats.freq(data, 2.7) assert o == 1 - o = stats.freq(data,15) + o = stats.freq(data, 15) assert o == 0 } @@ -21,15 +21,15 @@ fn tst_res(str1 string, str2 string) bool { fn test_mean() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '5.762500') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '17.650000') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '37.708000') @@ -37,31 +37,32 @@ fn test_mean() { fn test_geometric_mean() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.geometric_mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string - assert tst_res(o.str(),'5.15993') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + assert tst_res(o.str(), '5.15993') + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.geometric_mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string - assert o.str().eq('nan') || o.str().eq('-nan') || o.str().eq('-1.#IND00') || o == f64(0) || o.str().eq('-nan(ind)') // Because in math it yields a complex number - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + assert o.str().eq('nan') || o.str().eq('-nan') || o.str().eq('-1.#IND00') || o == f64(0) + || o.str().eq('-nan(ind)') // Because in math it yields a complex number + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.geometric_mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string - assert tst_res(o.str(),'25.064496') + assert tst_res(o.str(), '25.064496') } fn test_harmonic_mean() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.harmonic_mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '4.626519') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.harmonic_mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '9.134577') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.harmonic_mean(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '16.555477') @@ -72,56 +73,58 @@ fn test_median() { // Assumes sorted array // Even - mut data := [f64(2.7),f64(4.45),f64(5.9),f64(10.0)] + mut data := [f64(2.7), f64(4.45), f64(5.9), f64(10.0)] mut o := stats.median(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '5.175000') - data = [f64(-3.0),f64(1.89),f64(4.4),f64(67.31)] + data = [f64(-3.0), f64(1.89), f64(4.4), f64(67.31)] o = stats.median(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '3.145000') - data = [f64(7.88),f64(12.0),f64(54.83),f64(76.122)] + data = [f64(7.88), f64(12.0), f64(54.83), f64(76.122)] o = stats.median(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '33.415000') // Odd - data = [f64(2.7),f64(4.45),f64(5.9),f64(10.0),f64(22)] + data = [f64(2.7), f64(4.45), f64(5.9), f64(10.0), f64(22)] o = stats.median(data) assert o == f64(5.9) - data = [f64(-3.0),f64(1.89),f64(4.4),f64(9),f64(67.31)] + data = [f64(-3.0), f64(1.89), f64(4.4), f64(9), f64(67.31)] o = stats.median(data) assert o == f64(4.4) - data = [f64(7.88),f64(3.3),f64(12.0),f64(54.83),f64(76.122)] + data = [f64(7.88), f64(3.3), f64(12.0), f64(54.83), f64(76.122)] o = stats.median(data) assert o == f64(12.0) } fn test_mode() { // Tests were also verified on Wolfram Alpha - mut data := [f64(2.7),f64(2.7),f64(4.45),f64(5.9),f64(10.0)] + mut data := [f64(2.7), f64(2.7), f64(4.45), f64(5.9), f64(10.0)] mut o := stats.mode(data) assert o == f64(2.7) - data = [f64(-3.0),f64(1.89),f64(1.89),f64(1.89),f64(9),f64(4.4),f64(4.4),f64(9),f64(67.31)] + data = [f64(-3.0), f64(1.89), f64(1.89), f64(1.89), f64(9), f64(4.4), f64(4.4), f64(9), + f64(67.31), + ] o = stats.mode(data) assert o == f64(1.89) // Testing greedy nature - data = [f64(2.0),f64(4.0),f64(2.0),f64(4.0)] + data = [f64(2.0), f64(4.0), f64(2.0), f64(4.0)] o = stats.mode(data) assert o == f64(2.0) } fn test_rms() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.rms(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '6.362046') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.rms(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '33.773393') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.rms(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '47.452561') @@ -129,15 +132,15 @@ fn test_rms() { fn test_population_variance() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.population_variance(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '7.269219') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.population_variance(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '829.119550') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.population_variance(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '829.852282') @@ -145,15 +148,15 @@ fn test_population_variance() { fn test_sample_variance() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.sample_variance(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '9.692292') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.sample_variance(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '1105.492733') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.sample_variance(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '1106.469709') @@ -161,15 +164,15 @@ fn test_sample_variance() { fn test_population_stddev() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.population_stddev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '2.696149') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.population_stddev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '28.794436') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.population_stddev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '28.807157') @@ -177,15 +180,15 @@ fn test_population_stddev() { fn test_sample_stddev() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.sample_stddev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '3.113245') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.sample_stddev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '33.248951') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.sample_stddev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '33.263639') @@ -193,15 +196,15 @@ fn test_sample_stddev() { fn test_mean_absdev() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.mean_absdev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '2.187500') - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.mean_absdev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '24.830000') - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.mean_absdev(data) // Some issue with precision comparison in f64 using == operator hence serializing to string assert tst_res(o.str(), '27.768000') @@ -209,46 +212,46 @@ fn test_mean_absdev() { fn test_min() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.min(data) assert o == f64(2.7) - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.min(data) assert o == f64(-3.0) - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.min(data) assert o == f64(7.88) } fn test_max() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.max(data) assert o == f64(10.0) - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.max(data) assert o == f64(67.31) - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.max(data) assert o == f64(76.122) } fn test_range() { // Tests were also verified on Wolfram Alpha - mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)] + mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)] mut o := stats.range(data) assert o == f64(7.3) - data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)] + data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] o = stats.range(data) assert o == f64(70.31) - data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)] + data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] o = stats.range(data) assert o == f64(68.242) } fn test_passing_empty() { data := []f64{} - assert stats.freq(data,0) == 0 + assert stats.freq(data, 0) == 0 assert stats.mean(data) == f64(0) assert stats.geometric_mean(data) == f64(0) assert stats.harmonic_mean(data) == f64(0) diff --git a/vlib/math/unsafe.v b/vlib/math/unsafe.v index db2be8bd7..e6ebc6f28 100644 --- a/vlib/math/unsafe.v +++ b/vlib/math/unsafe.v @@ -2,11 +2,12 @@ // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. module math + // f32_bits returns the IEEE 754 binary representation of f, // with the sign bit of f and the result in the same bit position. // f32_bits(f32_from_bits(x)) == x. pub fn f32_bits(f f32) u32 { - p := *unsafe {&u32(&f)} + p := *unsafe { &u32(&f) } return p } @@ -15,7 +16,7 @@ pub fn f32_bits(f f32) u32 { // and the result in the same bit position. // f32_from_bits(f32_bits(x)) == x. pub fn f32_from_bits(b u32) f32 { - p := *unsafe {&f32(&b)} + p := *unsafe { &f32(&b) } return p } @@ -23,7 +24,7 @@ pub fn f32_from_bits(b u32) f32 { // with the sign bit of f and the result in the same bit position, // and f64_bits(f64_from_bits(x)) == x. pub fn f64_bits(f f64) u64 { - p := *unsafe {&u64(&f)} + p := *unsafe { &u64(&f) } return p } @@ -32,6 +33,6 @@ pub fn f64_bits(f f64) u64 { // and the result in the same bit position. // f64_from_bits(f64_bits(x)) == x. pub fn f64_from_bits(b u64) f64 { - p := *unsafe {&f64(&b)} + p := *unsafe { &f64(&b) } return p } diff --git a/vlib/mysql/_cdefs.c.v b/vlib/mysql/_cdefs.c.v index 5fe43733d..fe070236e 100644 --- a/vlib/mysql/_cdefs.c.v +++ b/vlib/mysql/_cdefs.c.v @@ -10,15 +10,15 @@ struct C.MYSQL_RES { [typedef] struct C.MYSQL_FIELD { - name byteptr // Name of column - org_name byteptr // Original column name, if an alias - table byteptr // Table of column if column was a field - org_table byteptr // Org table name, if table was an alias - db byteptr // Database for table - catalog byteptr // Catalog for table - def byteptr // Default value (set by mysql_list_fields) - length int // Width of column (create length) - max_length int // Max width for selected set + name &byte // Name of column + org_name &byte // Original column name, if an alias + table &byte // Table of column if column was a field + org_table &byte // Org table name, if table was an alias + db &byte // Database for table + catalog &byte // Catalog for table + def &byte // Default value (set by mysql_list_fields) + length int // Width of column (create length) + max_length int // Max width for selected set name_length u32 org_name_length u32 table_length u32 @@ -34,15 +34,15 @@ struct C.MYSQL_FIELD { fn C.mysql_init(mysql &C.MYSQL) &C.MYSQL -fn C.mysql_real_connect(mysql &C.MYSQL, host charptr, user charptr, passwd charptr, db charptr, port u32, unix_socket charptr, client_flag ConnectionFlag) &C.MYSQL +fn C.mysql_real_connect(mysql &C.MYSQL, host &char, user &char, passwd &char, db &char, port u32, unix_socket &char, client_flag ConnectionFlag) &C.MYSQL -fn C.mysql_query(mysql &C.MYSQL, q byteptr) int +fn C.mysql_query(mysql &C.MYSQL, q &byte) int -fn C.mysql_real_query(mysql &C.MYSQL, q byteptr, len u32) int +fn C.mysql_real_query(mysql &C.MYSQL, q &byte, len u32) int -fn C.mysql_select_db(mysql &C.MYSQL, db byteptr) int +fn C.mysql_select_db(mysql &C.MYSQL, db &byte) int -fn C.mysql_change_user(mysql &C.MYSQL, user byteptr, password byteptr, db byteptr) bool +fn C.mysql_change_user(mysql &C.MYSQL, user &byte, password &byte, db &byte) bool fn C.mysql_affected_rows(mysql &C.MYSQL) u64 @@ -50,7 +50,7 @@ fn C.mysql_options(mysql &C.MYSQL, option int, arg voidptr) int fn C.mysql_get_option(mysql &C.MYSQL, option int, arg voidptr) int -fn C.mysql_list_tables(mysql &C.MYSQL, wild byteptr) &C.MYSQL_RES +fn C.mysql_list_tables(mysql &C.MYSQL, wild &byte) &C.MYSQL_RES fn C.mysql_num_fields(res &C.MYSQL_RES) int @@ -66,34 +66,34 @@ fn C.mysql_ping(mysql &C.MYSQL) int fn C.mysql_store_result(mysql &C.MYSQL) &C.MYSQL_RES -fn C.mysql_fetch_row(res &C.MYSQL_RES) &byteptr +fn C.mysql_fetch_row(res &C.MYSQL_RES) &&byte fn C.mysql_fetch_fields(res &C.MYSQL_RES) &C.MYSQL_FIELD fn C.mysql_free_result(res &C.MYSQL_RES) -fn C.mysql_real_escape_string_quote(mysql &C.MYSQL, to byteptr, from byteptr, len u64, quote byte) u64 +fn C.mysql_real_escape_string_quote(mysql &C.MYSQL, to &byte, from &byte, len u64, quote byte) u64 fn C.mysql_close(sock &C.MYSQL) // INFO & VERSION -fn C.mysql_info(mysql &C.MYSQL) byteptr +fn C.mysql_info(mysql &C.MYSQL) &byte -fn C.mysql_get_host_info(mysql &C.MYSQL) byteptr +fn C.mysql_get_host_info(mysql &C.MYSQL) &byte -fn C.mysql_get_server_info(mysql &C.MYSQL) byteptr +fn C.mysql_get_server_info(mysql &C.MYSQL) &byte fn C.mysql_get_server_version(mysql &C.MYSQL) u64 fn C.mysql_get_client_version() u64 -fn C.mysql_get_client_info() byteptr +fn C.mysql_get_client_info() &byte // DEBUG & ERROR INFO -fn C.mysql_error(mysql &C.MYSQL) byteptr +fn C.mysql_error(mysql &C.MYSQL) &byte fn C.mysql_errno(mysql &C.MYSQL) int fn C.mysql_dump_debug_info(mysql &C.MYSQL) int -fn C.mysql_debug(debug byteptr) +fn C.mysql_debug(debug &byte) diff --git a/vlib/mysql/result.v b/vlib/mysql/result.v index 3fc6bf66a..e9e1909d8 100644 --- a/vlib/mysql/result.v +++ b/vlib/mysql/result.v @@ -33,7 +33,7 @@ pub struct Field { } // fetch_row - fetches the next row from a result. -pub fn (r Result) fetch_row() &byteptr { +pub fn (r Result) fetch_row() &&byte { return C.mysql_fetch_row(r.result) } @@ -58,7 +58,7 @@ pub fn (r Result) rows() []Row { if unsafe { rr[i] == 0 } { row.vals << '' } else { - row.vals << mystring(unsafe { byteptr(rr[i]) }) + row.vals << mystring(unsafe { &byte(rr[i]) }) } } rows << row @@ -66,7 +66,7 @@ pub fn (r Result) rows() []Row { return rows } -// maps - returns an array of maps, each containing a set of +// maps - returns an array of maps, each containing a set of // field name: field value pairs. pub fn (r Result) maps() []map[string]string { mut array_map := []map[string]string{} diff --git a/vlib/mysql/utils.v b/vlib/mysql/utils.v index 30fa66cca..2d6332781 100644 --- a/vlib/mysql/utils.v +++ b/vlib/mysql/utils.v @@ -11,7 +11,7 @@ fn get_errno(conn &C.MYSQL) int { } // resolve_nil_str - returns an empty string if passed value is a nil pointer. -fn resolve_nil_str(ptr byteptr) string { +fn resolve_nil_str(ptr &byte) string { if isnil(ptr) { return '' } @@ -19,7 +19,7 @@ fn resolve_nil_str(ptr byteptr) string { } [inline] -fn mystring(b byteptr) string { +fn mystring(b &byte) string { unsafe { return b.vstring() } diff --git a/vlib/os2/keep_vfmt_happy.v b/vlib/os2/keep_vfmt_happy.v index b0955216c..4b719f90a 100644 --- a/vlib/os2/keep_vfmt_happy.v +++ b/vlib/os2/keep_vfmt_happy.v @@ -1,2 +1,2 @@ -//this keeps vfmt happy +// this keeps vfmt happy module os2 diff --git a/vlib/os2/os2_darwin.c.v b/vlib/os2/os2_darwin.c.v index 82a30e5a3..b04ee115a 100644 --- a/vlib/os2/os2_darwin.c.v +++ b/vlib/os2/os2_darwin.c.v @@ -1,30 +1,26 @@ module os2 #include + struct File { fd int } -fn C.perror(charptr) - - -fn C.open(byteptr, int, int) int +fn C.perror(&char) +fn C.open(&byte, int, int) int -fn C.write(voidptr, byteptr, int) int - +fn C.write(voidptr, &byte, int) int fn C.close(int) int - pub fn create(path string) ?File { - fd := C.open(path.str, C.O_CREAT | C.O_TRUNC | C.O_WRONLY, 0644) // 511 + fd := C.open(path.str, C.O_CREAT | C.O_TRUNC | C.O_WRONLY, o644) // 511 if fd == -1 { return error('failed to create "$path":') // os.print_c_errno() } - return File{ - fd} + return File{fd} } pub fn (f File) writeln(s string) { diff --git a/vlib/os2/os2_test.v b/vlib/os2/os2_test.v index de2cedf0a..997e3413b 100644 --- a/vlib/os2/os2_test.v +++ b/vlib/os2/os2_test.v @@ -1,4 +1,4 @@ -//import os2 +// import os2 fn test_open() { /* @@ -8,4 +8,4 @@ fn test_open() { f.close() } */ -} +} diff --git a/vlib/pg/pg.v b/vlib/pg/pg.v index 81d8ed4d3..9e2d7e79b 100644 --- a/vlib/pg/pg.v +++ b/vlib/pg/pg.v @@ -36,11 +36,11 @@ pub: dbname string } -fn C.PQconnectdb(a byteptr) &C.PGconn +fn C.PQconnectdb(a &byte) &C.PGconn -fn C.PQerrorMessage(voidptr) byteptr +fn C.PQerrorMessage(voidptr) &byte -fn C.PQgetvalue(&C.PGResult, int, int) byteptr +fn C.PQgetvalue(&C.PGResult, int, int) &byte fn C.PQstatus(voidptr) int @@ -50,20 +50,20 @@ fn C.PQntuples(&C.PGResult) int fn C.PQnfields(&C.PGResult) int -fn C.PQexec(voidptr, byteptr) &C.PGResult +fn C.PQexec(voidptr, &byte) &C.PGResult // Params: // const Oid *paramTypes // const char *const *paramValues // const int *paramLengths // const int *paramFormats -fn C.PQexecParams(conn voidptr, command byteptr, nParams int, paramTypes int, paramValues byteptr, paramLengths int, paramFormats int, resultFormat int) &C.PGResult +fn C.PQexecParams(conn voidptr, command &byte, nParams int, paramTypes int, paramValues &byte, paramLengths int, paramFormats int, resultFormat int) &C.PGResult -fn C.PQputCopyData(conn voidptr, buffer byteptr, nbytes int) int +fn C.PQputCopyData(conn voidptr, buffer &byte, nbytes int) int fn C.PQputCopyEnd(voidptr, &byte) int -fn C.PQgetCopyData(conn voidptr, buffer &byteptr, async int) int +fn C.PQgetCopyData(conn voidptr, buffer &&byte, async int) int fn C.PQclear(&C.PGResult) voidptr @@ -186,7 +186,7 @@ pub fn (db DB) exec_one(query string) ?Row { // exec_param_many executes a query with the provided parameters pub fn (db DB) exec_param_many(query string, params []string) ?[]Row { - mut param_vals := []charptr{len: params.len} + mut param_vals := []&char{len: params.len} for i in 0 .. params.len { param_vals[i] = params[i].str } @@ -252,11 +252,11 @@ pub fn (db DB) copy_expert(query string, file io.ReaderWriter) ?int { } } else if status == C.PGRES_COPY_OUT { for { - address := byteptr(0) + address := &byte(0) n_bytes := C.PQgetCopyData(db.conn, &address, 0) if n_bytes > 0 { mut local_buf := []byte{len: n_bytes} - unsafe { C.memcpy(byteptr(local_buf.data), address, n_bytes) } + unsafe { C.memcpy(&byte(local_buf.data), address, n_bytes) } file.write(local_buf) or { C.PQfreemem(address) return err diff --git a/vlib/picohttpparser/misc.v b/vlib/picohttpparser/misc.v index 0834300e4..6c8e1e7b7 100644 --- a/vlib/picohttpparser/misc.v +++ b/vlib/picohttpparser/misc.v @@ -1,9 +1,8 @@ module picohttpparser -[inline] -[unsafe] -fn cpy(dst byteptr, src byteptr, len int) int { - unsafe {C.memcpy(dst, src, len)} +[inline; unsafe] +fn cpy(dst &byte, src &byte, len int) int { + unsafe { C.memcpy(dst, src, len) } return len } @@ -12,10 +11,10 @@ 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} + return unsafe { C.memcmp(dst.str, src.str, src.len) == 0 } } [inline] 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 } } diff --git a/vlib/picohttpparser/picohttpparser.v b/vlib/picohttpparser/picohttpparser.v index 85db6551f..a8c93e626 100644 --- a/vlib/picohttpparser/picohttpparser.v +++ b/vlib/picohttpparser/picohttpparser.v @@ -31,5 +31,5 @@ fn C.phr_parse_request_path(buf_start &char, len size_t, method PPchar, method_l fn C.phr_parse_request_path_pipeline(buf_start &char, len size_t, method PPchar, method_len &size_t, path PPchar, path_len &size_t) int fn C.get_date() &byte -// char * u64toa(uint64_t value, char *buffer) -fn C.u64toa(buffer &char, value u64) &char +// static inline int u64toa(char* buf, uint64_t value) { +fn C.u64toa(buffer &char, value u64) int diff --git a/vlib/picohttpparser/response.v b/vlib/picohttpparser/response.v index 6198ae463..5c490eaad 100644 --- a/vlib/picohttpparser/response.v +++ b/vlib/picohttpparser/response.v @@ -1,12 +1,12 @@ module picohttpparser pub struct Response { - fd int + fd int pub: - date byteptr - buf_start byteptr + date &byte = 0 + buf_start &byte = 0 pub mut: - buf byteptr + buf &byte = 0 } [inline] diff --git a/vlib/rand/rand.v b/vlib/rand/rand.v index 19be11ab3..d9c40ed8b 100644 --- a/vlib/rand/rand.v +++ b/vlib/rand/rand.v @@ -43,7 +43,9 @@ pub interface PRNG { f64_in_range(min f64, max f64) f64 } -__global ( default_rng &PRNG ) +__global ( + default_rng &PRNG +) // init initializes the default RNG. fn init() { @@ -63,7 +65,7 @@ pub fn get_current_rng() &PRNG { } // set_rng changes the default RNG from wyrand.WyRandRNG (or whatever the last RNG was) to the one -// provided by the user. Note that this new RNG must be seeded manually with a constant seed or the +// provided by the user. Note that this new RNG must be seeded manually with a constant seed or the // `seed.time_seed_array()` method. Also, it is recommended to store the old RNG in a variable and // should be restored if work with the custom RNG is complete. It is not necessary to restore if the // program terminates soon afterwards. diff --git a/vlib/regex/regex.v b/vlib/regex/regex.v index 51fb21f6f..b84e45eee 100644 --- a/vlib/regex/regex.v +++ b/vlib/regex/regex.v @@ -1,5 +1,4 @@ /* - regex 1.0 alpha Copyright (c) 2019-2021 Dario Deledda. All rights reserved. @@ -12,87 +11,77 @@ Know limitation: - find is implemented in a trivial way - not full compliant PCRE - not compliant POSIX ERE - */ module regex -import strings -pub const( - v_regex_version = "1.0 alpha" // regex module version +import strings - max_code_len = 256 // default small base code len for the regex programs - max_quantifier = 1073741824 // default max repetitions allowed for the quantifiers = 2^30 +pub const ( + v_regex_version = '1.0 alpha' // regex module version + max_code_len = 256 // default small base code len for the regex programs + max_quantifier = 1073741824 // default max repetitions allowed for the quantifiers = 2^30 // spaces chars (here only westerns!!) TODO: manage all the spaces from unicode - spaces = [` `, `\t`, `\n`, `\r`, `\v`, `\f`] + spaces = [` `, `\t`, `\n`, `\r`, `\v`, `\f`] // new line chars for now only '\n' - new_line_list = [`\n`, `\r`] + new_line_list = [`\n`, `\r`] // Results - no_match_found = -1 + no_match_found = -1 // Errors - compile_ok = 0 // the regex string compiled, all ok - err_char_unknown = -2 // the char used is unknow to the system - err_undefined = -3 // the compiler symbol is undefined - err_internal_error = -4 // Bug in the regex system!! - err_cc_alloc_overflow = -5 // memory for char class full!! - err_syntax_error = -6 // syntax error in regex compiling - err_groups_overflow = -7 // max number of groups reached - err_groups_max_nested = -8 // max number of nested group reached - err_group_not_balanced = -9 // group not balanced - err_group_qm_notation = -10 // group invalid notation + compile_ok = 0 // the regex string compiled, all ok + err_char_unknown = -2 // the char used is unknow to the system + err_undefined = -3 // the compiler symbol is undefined + err_internal_error = -4 // Bug in the regex system!! + err_cc_alloc_overflow = -5 // memory for char class full!! + err_syntax_error = -6 // syntax error in regex compiling + err_groups_overflow = -7 // max number of groups reached + err_groups_max_nested = -8 // max number of nested group reached + err_group_not_balanced = -9 // group not balanced + err_group_qm_notation = -10 // group invalid notation ) -const( +const ( //************************************* // regex program instructions //************************************* - ist_simple_char = u32(0x7FFFFFFF) // single char instruction, 31 bit available to char - + ist_simple_char = u32(0x7FFFFFFF) // single char instruction, 31 bit available to char // char class 11 0100 AA xxxxxxxx // AA = 00 regular class // AA = 01 Negated class ^ char - ist_char_class = 0xD1000000 // MASK - ist_char_class_pos = 0xD0000000 // char class normal [abc] - ist_char_class_neg = 0xD1000000 // char class negate [^abc] - + ist_char_class = 0xD1000000 // MASK + ist_char_class_pos = 0xD0000000 // char class normal [abc] + ist_char_class_neg = 0xD1000000 // char class negate [^abc] // dot char 10 0110 xx xxxxxxxx - ist_dot_char = 0x98000000 // match any char except \n - + ist_dot_char = 0x98000000 // match any char except \n // backslash chars 10 0100 xx xxxxxxxx - ist_bsls_char = 0x90000000 // backslash char - + ist_bsls_char = 0x90000000 // backslash char // OR | 10 010Y xx xxxxxxxx - ist_or_branch = 0x91000000 // OR case - + ist_or_branch = 0x91000000 // OR case // groups 10 010Y xx xxxxxxxx - ist_group_start = 0x92000000 // group start ( - ist_group_end = 0x94000000 // group end ) - + ist_group_start = 0x92000000 // group start ( + ist_group_end = 0x94000000 // group end ) // control instructions - ist_prog_end = u32(0x88000000) //10 0010 xx xxxxxxxx - //************************************* + ist_prog_end = u32(0x88000000) // 10 0010 xx xxxxxxxx + //************************************* ) /* - General Utilities - */ // utf8util_char_len calculate the length in bytes of a utf8 char [inline] fn utf8util_char_len(b byte) int { - return (( 0xe5000000 >> (( b >> 3 ) & 0x1e )) & 3 ) + 1 + return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1 } // get_char get a char from position i and return an u32 with the unicode code -[inline] -[direct_array_access] -fn (re RE) get_char(in_txt string, i int) (u32,int) { - ini := unsafe {in_txt.str[i]} +[direct_array_access; inline] +fn (re RE) get_char(in_txt string, i int) (u32, int) { + ini := unsafe { in_txt.str[i] } // ascii 8 bit - if (re.flag & f_bin) !=0 || ini & 0x80 == 0 { + if (re.flag & regex.f_bin) != 0 || ini & 0x80 == 0 { return u32(ini), 1 } // unicode char @@ -100,40 +89,47 @@ fn (re RE) get_char(in_txt string, i int) (u32,int) { mut tmp := 0 mut ch := u32(0) for tmp < char_len { - ch = (ch << 8) | unsafe {in_txt.str[i + tmp]} + ch = (ch << 8) | unsafe { in_txt.str[i + tmp] } tmp++ } - return ch,char_len + return ch, char_len } // get_charb get a char from position i and return an u32 with the unicode code -[inline] -[direct_array_access] -fn (re RE) get_charb(in_txt &byte, i int) (u32,int) { +[direct_array_access; inline] +fn (re RE) get_charb(in_txt &byte, i int) (u32, int) { // ascii 8 bit - if (re.flag & f_bin) !=0 || unsafe {in_txt[i]} & 0x80 == 0 { - return u32(unsafe {in_txt[i]}), 1 + if (re.flag & regex.f_bin) != 0 || unsafe { in_txt[i] } & 0x80 == 0 { + return u32(unsafe { in_txt[i] }), 1 } // unicode char - char_len := utf8util_char_len(unsafe {in_txt[i]}) + char_len := utf8util_char_len(unsafe { in_txt[i] }) mut tmp := 0 mut ch := u32(0) for tmp < char_len { - ch = (ch << 8) | unsafe {in_txt[i + tmp]} + ch = (ch << 8) | unsafe { in_txt[i + tmp] } tmp++ } - return ch,char_len + return ch, char_len } [inline] fn is_alnum(in_char byte) bool { mut tmp := in_char - `A` - if tmp <= 25 { return true } + if tmp <= 25 { + return true + } tmp = in_char - `a` - if tmp <= 25 { return true } + if tmp <= 25 { + return true + } tmp = in_char - `0` - if tmp <= 9 { return true } - if tmp == `_` { return true } + if tmp <= 9 { + return true + } + if tmp == `_` { + return true + } return false } @@ -144,7 +140,7 @@ fn is_not_alnum(in_char byte) bool { [inline] fn is_space(in_char byte) bool { - return in_char in spaces + return in_char in regex.spaces } [inline] @@ -187,18 +183,18 @@ fn is_upper(in_char byte) bool { pub fn (re RE) get_parse_error_string(err int) string { match err { - compile_ok { return "compile_ok" } - no_match_found { return "no_match_found" } - err_char_unknown { return "err_char_unknown" } - err_undefined { return "err_undefined" } - err_internal_error { return "err_internal_error" } - err_cc_alloc_overflow { return "err_cc_alloc_overflow" } - err_syntax_error { return "err_syntax_error" } - err_groups_overflow { return "err_groups_overflow" } - err_groups_max_nested { return "err_groups_max_nested" } - err_group_not_balanced { return "err_group_not_balanced" } - err_group_qm_notation { return "err_group_qm_notation" } - else { return "err_unknown" } + regex.compile_ok { return 'compile_ok' } + regex.no_match_found { return 'no_match_found' } + regex.err_char_unknown { return 'err_char_unknown' } + regex.err_undefined { return 'err_undefined' } + regex.err_internal_error { return 'err_internal_error' } + regex.err_cc_alloc_overflow { return 'err_cc_alloc_overflow' } + regex.err_syntax_error { return 'err_syntax_error' } + regex.err_groups_overflow { return 'err_groups_overflow' } + regex.err_groups_max_nested { return 'err_groups_max_nested' } + regex.err_group_not_balanced { return 'err_group_not_balanced' } + regex.err_group_qm_notation { return 'err_group_qm_notation' } + else { return 'err_unknown' } } } @@ -206,11 +202,11 @@ pub fn (re RE) get_parse_error_string(err int) string { [inline] fn utf8_str(ch rune) string { mut i := 4 - mut res := "" + mut res := '' for i > 0 { v := byte((ch >> ((i - 1) * 8)) & 0xFF) - if v != 0{ - res += "${v:1c}" + if v != 0 { + res += '${v:1c}' } i-- } @@ -228,39 +224,32 @@ fn simple_log(txt string) { * ******************************************************************************/ pub type FnValidator = fn (byte) bool -struct Token{ + +struct Token { mut: ist rune - // char - ch rune // char of the token if any - ch_len byte // char len - + ch rune // char of the token if any + ch_len byte // char len // Quantifiers / branch - rep_min int // used also for jump next in the OR branch [no match] pc jump - rep_max int // used also for jump next in the OR branch [ match] pc jump - greedy bool // greedy quantifier flag - + rep_min int // used also for jump next in the OR branch [no match] pc jump + rep_max int // used also for jump next in the OR branch [ match] pc jump + greedy bool // greedy quantifier flag // Char class - cc_index int = -1 - + cc_index int = -1 // counters for quantifier check (repetitions) - rep int - + rep int // validator function pointer - validator FnValidator - + validator FnValidator // groups variables - group_rep int // repetition of the group - group_id int = -1 // id of the group - goto_pc int = -1 // jump to this PC if is needed - + group_rep int // repetition of the group + group_id int = -1 // id of the group + goto_pc int = -1 // jump to this PC if is needed // OR flag for the token - next_is_or bool // true if the next token is an OR - + next_is_or bool // true if the next token is an OR // dot_char token variables - dot_check_pc int = -1 // pc of the next token to check - last_dot_flag bool // if true indicate that is the last dot_char in the regex + dot_check_pc int = -1 // pc of the next token to check + last_dot_flag bool // if true indicate that is the last dot_char in the regex } [inline] @@ -274,78 +263,71 @@ fn (mut tok Token) reset() { * ******************************************************************************/ pub const ( - f_nl = 0x00000001 // end the match when find a new line symbol - f_ms = 0x00000002 // match true only if the match is at the start of the string - f_me = 0x00000004 // match true only if the match is at the end of the string - - f_efm = 0x00000100 // exit on first token matched, used by search - f_bin = 0x00000200 // work only on bytes, ignore utf-8 + f_nl = 0x00000001 // end the match when find a new line symbol + f_ms = 0x00000002 // match true only if the match is at the start of the string + f_me = 0x00000004 // match true only if the match is at the end of the string + f_efm = 0x00000100 // exit on first token matched, used by search + f_bin = 0x00000200 // work only on bytes, ignore utf-8 // behaviour modifier flags - f_src = 0x00020000 // search mode enabled + f_src = 0x00020000 // search mode enabled ) -struct StateDotObj{ +struct StateDotObj { mut: - i int = -1 // char index in the input buffer - pc int = -1 // program counter saved - mi int = -1 // match_index saved - group_stack_index int = -1 // continuous save on capturing groups + i int = -1 // char index in the input buffer + pc int = -1 // program counter saved + mi int = -1 // match_index saved + group_stack_index int = -1 // continuous save on capturing groups } pub type FnLog = fn (string) -pub -struct RE { +pub struct RE { pub mut: - prog []Token - prog_len int // regex program len - + prog []Token + prog_len int // regex program len // char classes storage - cc []CharClass // char class list - cc_index int // index - + cc []CharClass // char class list + cc_index int // index // groups - group_count int // number of groups in this regex struct - groups []int // groups index results - group_max_nested int = 3 // max nested group - group_max int = 8 // max allowed number of different groups + group_count int // number of groups in this regex struct + groups []int // groups index results + group_max_nested int = 3 // max nested group + group_max int = 8 // max allowed number of different groups - state_list []StateObj + state_list []StateObj - group_csave_flag bool // flag to enable continuous saving - group_csave []int //= []int{} // groups continuous save list + group_csave_flag bool // flag to enable continuous saving + group_csave []int //= []int{} // groups continuous save list - group_map map[string]int // groups names map - - group_stack []int - group_data []int + group_map map[string]int // groups names map + group_stack []int + group_data []int // flags - flag int // flag for optional parameters - + flag int // flag for optional parameters // Debug/log - debug int // enable in order to have the unroll of the code 0 = NO_DEBUG, 1 = LIGHT 2 = VERBOSE - log_func FnLog = simple_log // log function, can be customized by the user - query string // query string + debug int // enable in order to have the unroll of the code 0 = NO_DEBUG, 1 = LIGHT 2 = VERBOSE + log_func FnLog = simple_log // log function, can be customized by the user + query string // query string } // Reset RE object -[inline] -[direct_array_access] -fn (mut re RE) reset(){ +[direct_array_access; inline] +fn (mut re RE) reset() { re.cc_index = 0 mut i := 0 for i < re.prog_len { re.prog[i].group_rep = 0 // clear repetition of the group - re.prog[i].rep = 0 // clear repetition of the token + re.prog[i].rep = 0 // clear repetition of the token i++ } // init groups array if re.group_count > 0 { - re.groups = []int{len: re.group_count*2, init: -1} + re.groups = []int{len: re.group_count * 2, init: -1} } // reset group_csave @@ -358,11 +340,11 @@ fn (mut re RE) reset(){ // gcc bug, dont use [inline] or go 5 time slower //[inline] [direct_array_access] -fn (mut re RE) reset_src(){ +fn (mut re RE) reset_src() { mut i := 0 for i < re.prog_len { re.prog[i].group_rep = 0 // clear repetition of the group - re.prog[i].rep = 0 // clear repetition of the token + re.prog[i].rep = 0 // clear repetition of the token i++ } } @@ -373,11 +355,11 @@ fn (mut re RE) reset_src(){ * ******************************************************************************/ struct BslsStruct { - ch rune // meta char - validator FnValidator // validator function pointer + ch rune // meta char + validator FnValidator // validator function pointer } -const( +const ( bsls_validator_array = [ BslsStruct{`w`, is_alnum}, BslsStruct{`W`, is_not_alnum}, @@ -390,18 +372,19 @@ const( ] // these chars are escape if preceded by a \ - bsls_escape_list = [`\\`, `|`, `.`, `:`, `*`, `+`, `-`, `{`, `}`, `[`, `]`, `(`, `)`, `?`, `^`, `!`] + bsls_escape_list = [`\\`, `|`, `.`, `:`, `*`, `+`, `-`, `{`, `}`, `[`, `]`, `(`, `)`, `?`, + `^`, `!`] ) enum BSLS_parse_state { - start - bsls_found - bsls_char - normal_char + start + bsls_found + bsls_char + normal_char } // parse_bsls return (index, str_len) bsls_validator_array index, len of the backslash sequence if present -fn (re RE) parse_bsls(in_txt string, in_i int) (int,int){ +fn (re RE) parse_bsls(in_txt string, in_i int) (int, int) { mut status := BSLS_parse_state.start mut i := in_i @@ -418,9 +401,9 @@ fn (re RE) parse_bsls(in_txt string, in_i int) (int,int){ // check if is our bsls char, for now only one length sequence if status == .bsls_found { - for c,x in bsls_validator_array { + for c, x in regex.bsls_validator_array { if x.ch == ch { - return c, i-in_i+1 + return c, i - in_i + 1 } } status = .normal_char @@ -429,18 +412,17 @@ fn (re RE) parse_bsls(in_txt string, in_i int) (int,int){ // no BSLS validator, manage as normal escape char char if status == .normal_char { - if ch in bsls_escape_list { - return no_match_found, i-in_i+1 + if ch in regex.bsls_escape_list { + return regex.no_match_found, i - in_i + 1 } - return err_syntax_error, i-in_i+1 + return regex.err_syntax_error, i - in_i + 1 } // at the present time we manage only one char after the \ break - } // not our bsls return KO - return err_syntax_error, i + return regex.err_syntax_error, i } /****************************************************************************** @@ -448,20 +430,20 @@ fn (re RE) parse_bsls(in_txt string, in_i int) (int,int){ * Char class * ******************************************************************************/ -const( - cc_null = 0 // empty cc token - cc_char = 1 // simple char: a - cc_int = 2 // char interval: a-z - cc_bsls = 3 // backslash char - cc_end = 4 // cc sequence terminator +const ( + cc_null = 0 // empty cc token + cc_char = 1 // simple char: a + cc_int = 2 // char interval: a-z + cc_bsls = 3 // backslash char + cc_end = 4 // cc sequence terminator ) struct CharClass { mut: - cc_type int = cc_null // type of cc token - ch0 rune // first char of the interval a-b a in this case - ch1 rune // second char of the interval a-b b in this case - validator FnValidator // validator function pointer + cc_type int = regex.cc_null // type of cc token + ch0 rune // first char of the interval a-b a in this case + ch1 rune // second char of the interval a-b b in this case + validator FnValidator // validator function pointer } enum CharClass_parse_state { @@ -473,26 +455,24 @@ enum CharClass_parse_state { } fn (re RE) get_char_class(pc int) string { - buf := []byte{len:(re.cc.len)} - mut buf_ptr := unsafe {&byte(&buf)} + buf := []byte{len: (re.cc.len)} + mut buf_ptr := unsafe { &byte(&buf) } mut cc_i := re.prog[pc].cc_index mut i := 0 mut tmp := 0 - for cc_i >= 0 && cc_i < re.cc.len && re.cc[cc_i].cc_type != cc_end { - - if re.cc[cc_i].cc_type == cc_bsls { + for cc_i >= 0 && cc_i < re.cc.len && re.cc[cc_i].cc_type != regex.cc_end { + if re.cc[cc_i].cc_type == regex.cc_bsls { unsafe { buf_ptr[i] = `\\` i++ buf_ptr[i] = byte(re.cc[cc_i].ch0) i++ } - } - else if re.cc[cc_i].ch0 == re.cc[cc_i].ch1 { + } else if re.cc[cc_i].ch0 == re.cc[cc_i].ch1 { tmp = 3 for tmp >= 0 { - x := byte((re.cc[cc_i].ch0 >> (tmp*8)) & 0xFF) + x := byte((re.cc[cc_i].ch0 >> (tmp * 8)) & 0xFF) if x != 0 { unsafe { buf_ptr[i] = x @@ -501,11 +481,10 @@ fn (re RE) get_char_class(pc int) string { } tmp-- } - } - else { + } else { tmp = 3 for tmp >= 0 { - x := byte((re.cc[cc_i].ch0 >> (tmp*8)) & 0xFF) + x := byte((re.cc[cc_i].ch0 >> (tmp * 8)) & 0xFF) if x != 0 { unsafe { buf_ptr[i] = x @@ -520,7 +499,7 @@ fn (re RE) get_char_class(pc int) string { } tmp = 3 for tmp >= 0 { - x := byte((re.cc[cc_i].ch1 >> (tmp*8)) & 0xFF) + x := byte((re.cc[cc_i].ch1 >> (tmp * 8)) & 0xFF) if x != 0 { unsafe { buf_ptr[i] = x @@ -535,19 +514,17 @@ fn (re RE) get_char_class(pc int) string { unsafe { buf_ptr[i] = byte(0) } - - return unsafe { tos_clone( buf_ptr ) } + return unsafe { tos_clone(buf_ptr) } } fn (re RE) check_char_class(pc int, ch rune) bool { mut cc_i := re.prog[pc].cc_index - for cc_i >= 0 && cc_i < re.cc.len && re.cc[cc_i].cc_type != cc_end { - if re.cc[cc_i].cc_type == cc_bsls { + for cc_i >= 0 && cc_i < re.cc.len && re.cc[cc_i].cc_type != regex.cc_end { + if re.cc[cc_i].cc_type == regex.cc_bsls { if re.cc[cc_i].validator(byte(ch)) { return true } - } - else if ch >= re.cc[cc_i].ch0 && ch <= re.cc[cc_i].ch1 { + } else if ch >= re.cc[cc_i].ch0 && ch <= re.cc[cc_i].ch1 { return true } cc_i++ @@ -561,35 +538,34 @@ fn (mut re RE) parse_char_class(in_txt string, in_i int) (int, int, rune) { mut i := in_i mut tmp_index := re.cc_index - res_index := re.cc_index + res_index := re.cc_index - mut cc_type := u32(ist_char_class_pos) + mut cc_type := u32(regex.ist_char_class_pos) for i < in_txt.len { - // check if we are out of memory for char classes if tmp_index >= re.cc.len { - return err_cc_alloc_overflow, 0, u32(0) + return regex.err_cc_alloc_overflow, 0, u32(0) } // get our char - char_tmp,char_len := re.get_char(in_txt,i) + char_tmp, char_len := re.get_char(in_txt, i) ch := byte(char_tmp) - //println("CC #${i:3d} ch: ${ch:c}") + // println("CC #${i:3d} ch: ${ch:c}") // negation if status == .start && ch == `^` { - cc_type = u32(ist_char_class_neg) + cc_type = u32(regex.ist_char_class_neg) i += char_len continue } // minus symbol if status == .start && ch == `-` { - re.cc[tmp_index].cc_type = cc_char - re.cc[tmp_index].ch0 = char_tmp - re.cc[tmp_index].ch1 = char_tmp + re.cc[tmp_index].cc_type = regex.cc_char + re.cc[tmp_index].ch0 = char_tmp + re.cc[tmp_index].ch1 = char_tmp i += char_len tmp_index++ continue @@ -597,21 +573,21 @@ fn (mut re RE) parse_char_class(in_txt string, in_i int) (int, int, rune) { // bsls if (status == .start || status == .in_char) && ch == `\\` { - //println("CC bsls.") + // println("CC bsls.") status = .in_bsls i += char_len continue } if status == .in_bsls { - //println("CC bsls validation.") - for c,x in bsls_validator_array { + // println("CC bsls validation.") + for c, x in regex.bsls_validator_array { if x.ch == ch { - //println("CC bsls found [${ch:c}]") - re.cc[tmp_index].cc_type = cc_bsls - re.cc[tmp_index].ch0 = bsls_validator_array[c].ch - re.cc[tmp_index].ch1 = bsls_validator_array[c].ch - re.cc[tmp_index].validator = bsls_validator_array[c].validator + // println("CC bsls found [${ch:c}]") + re.cc[tmp_index].cc_type = regex.cc_bsls + re.cc[tmp_index].ch0 = regex.bsls_validator_array[c].ch + re.cc[tmp_index].ch1 = regex.bsls_validator_array[c].ch + re.cc[tmp_index].validator = regex.bsls_validator_array[c].validator i += char_len tmp_index++ status = .in_char @@ -620,28 +596,26 @@ fn (mut re RE) parse_char_class(in_txt string, in_i int) (int, int, rune) { } if status == .in_bsls { // manage as a simple char - //println("CC bsls not found [${ch:c}]") - re.cc[tmp_index].cc_type = cc_char - re.cc[tmp_index].ch0 = char_tmp - re.cc[tmp_index].ch1 = char_tmp + // println("CC bsls not found [${ch:c}]") + re.cc[tmp_index].cc_type = regex.cc_char + re.cc[tmp_index].ch0 = char_tmp + re.cc[tmp_index].ch1 = char_tmp i += char_len tmp_index++ status = .in_char continue - }else { + } else { continue } } // simple char - if (status == .start || status == .in_char) && - ch != `-` && ch != `]` - { + if (status == .start || status == .in_char) && ch != `-` && ch != `]` { status = .in_char - re.cc[tmp_index].cc_type = cc_char - re.cc[tmp_index].ch0 = char_tmp - re.cc[tmp_index].ch1 = char_tmp + re.cc[tmp_index].cc_type = regex.cc_char + re.cc[tmp_index].ch0 = char_tmp + re.cc[tmp_index].ch1 = char_tmp i += char_len tmp_index++ @@ -658,25 +632,25 @@ fn (mut re RE) parse_char_class(in_txt string, in_i int) (int, int, rune) { // check range end if status == .separator && ch != `]` && ch != `-` { status = .in_char - re.cc[tmp_index-1].cc_type = cc_int - re.cc[tmp_index-1].ch1 = char_tmp + re.cc[tmp_index - 1].cc_type = regex.cc_int + re.cc[tmp_index - 1].ch1 = char_tmp i += char_len continue } // char class end if status == .in_char && ch == `]` { - re.cc[tmp_index].cc_type = cc_end - re.cc[tmp_index].ch0 = 0 - re.cc[tmp_index].ch1 = 0 - re.cc_index = tmp_index+1 + re.cc[tmp_index].cc_type = regex.cc_end + re.cc[tmp_index].ch0 = 0 + re.cc[tmp_index].ch1 = 0 + re.cc_index = tmp_index + 1 - return res_index, i-in_i+2, cc_type + return res_index, i - in_i + 2, cc_type } i++ } - return err_syntax_error,0,u32(0) + return regex.err_syntax_error, 0, u32(0) } /****************************************************************************** @@ -711,12 +685,11 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { unsafe { ch = in_txt.str[i] } - - //println("${ch:c} status: $status") + // println("${ch:c} status: $status") // exit on no compatible char with {} quantifier if utf8util_char_len(ch) != 1 { - return err_syntax_error, i, 0, false + return regex.err_syntax_error, i, 0, false } // min parsing skip if comma present @@ -727,7 +700,7 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { continue } - if status == .start && is_digit( ch ) { + if status == .start && is_digit(ch) { status = .min_parse q_min *= 10 q_min += int(ch - `0`) @@ -735,7 +708,7 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { continue } - if status == .min_parse && is_digit( ch ) { + if status == .min_parse && is_digit(ch) { q_min *= 10 q_min += int(ch - `0`) i++ @@ -758,13 +731,13 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { // end without max if status == .comma_checked && ch == `}` { - q_max = max_quantifier + q_max = regex.max_quantifier status = .greedy continue } // start max parsing - if status == .comma_checked && is_digit( ch ) { + if status == .comma_checked && is_digit(ch) { status = .max_parse q_max *= 10 q_max += int(ch - `0`) @@ -773,7 +746,7 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { } // parse the max - if status == .max_parse && is_digit( ch ) { + if status == .max_parse && is_digit(ch) { q_max *= 10 q_max += int(ch - `0`) i++ @@ -788,30 +761,30 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { // check if greedy flag char ? is present if status == .greedy { - if i+1 < in_txt.len { + if i + 1 < in_txt.len { i++ status = .gredy_parse continue } - return q_min, q_max, i-in_i+2, false + return q_min, q_max, i - in_i + 2, false } // check the greedy flag if status == .gredy_parse { if ch == `?` { - return q_min, q_max, i-in_i+2, true + return q_min, q_max, i - in_i + 2, true } else { i-- - return q_min, q_max, i-in_i+2, false + return q_min, q_max, i - in_i + 2, false } } // not a {} quantifier, exit - return err_syntax_error, i, 0, false + return regex.err_syntax_error, i, 0, false } // not a conform {} quantifier - return err_syntax_error, i, 0, false + return regex.err_syntax_error, i, 0, false } // @@ -819,12 +792,12 @@ fn (re RE) parse_quantifier(in_txt string, in_i int) (int, int, int, bool) { // enum Group_parse_state { start - q_mark // (? - q_mark1 // (?:|P checking - p_status // (?P - p_start // (?P< - p_end // (?P<...> - p_in_name // (?P<... + q_mark // (? + q_mark1 // (?:|P checking + p_status // (?P + p_start // (?P< + p_end // (?P<...> + p_in_name // (?P<... finish } @@ -835,9 +808,8 @@ fn (re RE) parse_groups(in_txt string, in_i int) (int, bool, string, int) { mut name := '' for i < in_txt.len && status != .finish { - // get our char - char_tmp,char_len := re.get_char(in_txt,i) + char_tmp, char_len := re.get_char(in_txt, i) ch := byte(char_tmp) // start @@ -869,8 +841,8 @@ fn (re RE) parse_groups(in_txt string, in_i int) (int, bool, string, int) { // not a valid q mark found if status == .q_mark1 { - //println("NO VALID Q MARK") - return -2 , true, name, i + // println("NO VALID Q MARK") + return -2, true, name, i } if status == .p_status && ch == `<` { @@ -881,14 +853,14 @@ fn (re RE) parse_groups(in_txt string, in_i int) (int, bool, string, int) { if status == .p_start && ch != `>` { status = .p_in_name - name += "${ch:1c}" // TODO: manage utf8 chars + name += '${ch:1c}' // TODO: manage utf8 chars i += char_len continue } // colect name if status == .p_in_name && ch != `>` && is_alnum(ch) { - name += "${ch:1c}" // TODO: manage utf8 chars + name += '${ch:1c}' // TODO: manage utf8 chars i += char_len continue } @@ -901,77 +873,76 @@ fn (re RE) parse_groups(in_txt string, in_i int) (int, bool, string, int) { // error on name group if status == .p_in_name { - return -2 , true, name, i + return -2, true, name, i } // normal group, nothig to do, exit - return 0 , true, name, i + return 0, true, name, i } - /* UNREACHABLE */ - //println("ERROR!! NOT MEANT TO BE HERE!!1") - return -2 , true, name, i + // UNREACHABLE + // println("ERROR!! NOT MEANT TO BE HERE!!1") + return -2, true, name, i } // // main compiler // // compile return (return code, index) where index is the index of the error in the query string if return code is an error code -fn (mut re RE) impl_compile(in_txt string) (int,int) { - mut i := 0 // input string index - mut pc := 0 // program counter +fn (mut re RE) impl_compile(in_txt string) (int, int) { + mut i := 0 // input string index + mut pc := 0 // program counter // group management variables - mut group_count := -1 - mut group_stack := []int{len: re.group_max_nested, init: 0} + mut group_count := -1 + mut group_stack := []int{len: re.group_max_nested, init: 0} mut group_stack_txt_index := []int{len: re.group_max_nested, init: -1} - mut group_stack_index := -1 + mut group_stack_index := -1 - re.query = in_txt // save the query string + re.query = in_txt // save the query string i = 0 for i < in_txt.len { mut char_tmp := u32(0) mut char_len := 0 - //println("i: ${i:3d} ch: ${in_txt.str[i]:c}") + // println("i: ${i:3d} ch: ${in_txt.str[i]:c}") - char_tmp,char_len = re.get_char(in_txt,i) + char_tmp, char_len = re.get_char(in_txt, i) // // check special cases: $ ^ // if char_len == 1 && i == 0 && byte(char_tmp) == `^` { - re.flag = f_ms + re.flag = regex.f_ms i = i + char_len continue } - if char_len == 1 && i == (in_txt.len-1) && byte(char_tmp) == `$` { - re.flag = f_me + if char_len == 1 && i == (in_txt.len - 1) && byte(char_tmp) == `$` { + re.flag = regex.f_me i = i + char_len continue } // ist_group_start if char_len == 1 && pc >= 0 && byte(char_tmp) == `(` { - - //check max groups allowed + // check max groups allowed if group_count > re.group_max { - return err_groups_overflow, i+1 + return regex.err_groups_overflow, i + 1 } group_stack_index++ // check max nested groups allowed if group_stack_index > re.group_max_nested { - return err_groups_max_nested, i+1 + return regex.err_groups_max_nested, i + 1 } - tmp_res, cgroup_flag, cgroup_name, next_i := re.parse_groups(in_txt,i) + tmp_res, cgroup_flag, cgroup_name, next_i := re.parse_groups(in_txt, i) // manage question mark format error if tmp_res < -1 { - return err_group_qm_notation, next_i + return regex.err_group_qm_notation, next_i } - //println("Parse group: [$tmp_res, $cgroup_flag, ($i,$next_i), '${in_txt[i..next_i]}' ]") + // println("Parse group: [$tmp_res, $cgroup_flag, ($i,$next_i), '${in_txt[i..next_i]}' ]") i = next_i if cgroup_flag == true { @@ -983,8 +954,8 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { // NOTE: **** the group index is +1 because map return 0 when not found!! **** mut group_id := group_count if cgroup_name.len > 0 { - //println("GROUP NAME: ${cgroup_name}") - if cgroup_name in re.group_map{ + // println("GROUP NAME: ${cgroup_name}") + if cgroup_name in re.group_map { group_id = re.group_map[cgroup_name] - 1 group_count-- } else { @@ -995,13 +966,13 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { group_stack_txt_index[group_stack_index] = i group_stack[group_stack_index] = pc - re.prog[pc].ist = u32(0) | ist_group_start + re.prog[pc].ist = u32(0) | regex.ist_group_start re.prog[pc].rep_min = 1 re.prog[pc].rep_max = 1 // set the group id if cgroup_flag == false { - //println("NO CAPTURE GROUP") + // println("NO CAPTURE GROUP") re.prog[pc].group_id = -1 } else { re.prog[pc].group_id = group_id @@ -1012,23 +983,23 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { } // ist_group_end - if char_len==1 && pc > 0 && byte(char_tmp) == `)` { + if char_len == 1 && pc > 0 && byte(char_tmp) == `)` { if group_stack_index < 0 { - return err_group_not_balanced, i+1 + return regex.err_group_not_balanced, i + 1 } goto_pc := group_stack[group_stack_index] group_stack_index-- - re.prog[pc].ist = u32(0) | ist_group_end + re.prog[pc].ist = u32(0) | regex.ist_group_end re.prog[pc].rep_min = 1 re.prog[pc].rep_max = 1 - re.prog[pc].goto_pc = goto_pc // PC where to jump if a group need - re.prog[pc].group_id = re.prog[goto_pc].group_id // id of this group, used for storing data + re.prog[pc].goto_pc = goto_pc // PC where to jump if a group need + re.prog[pc].group_id = re.prog[goto_pc].group_id // id of this group, used for storing data - re.prog[goto_pc].goto_pc = pc // start goto point to the end group pc - //re.prog[goto_pc].group_id = group_count // id of this group, used for storing data + re.prog[goto_pc].goto_pc = pc // start goto point to the end group pc + // re.prog[goto_pc].group_id = group_count // id of this group, used for storing data pc = pc + 1 i = i + char_len @@ -1036,8 +1007,8 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { } // ist_dot_char match any char except the following token - if char_len==1 && pc >= 0 && byte(char_tmp) == `.` { - re.prog[pc].ist = u32(0) | ist_dot_char + if char_len == 1 && pc >= 0 && byte(char_tmp) == `.` { + re.prog[pc].ist = u32(0) | regex.ist_dot_char re.prog[pc].rep_min = 1 re.prog[pc].rep_max = 1 pc = pc + 1 @@ -1046,52 +1017,48 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { } // OR branch - if char_len==1 && pc > 0 && byte(char_tmp) == `|` { + if char_len == 1 && pc > 0 && byte(char_tmp) == `|` { // two consecutive ist_dot_char are an error - if pc > 0 && re.prog[pc-1].ist == ist_or_branch { - return err_syntax_error,i + if pc > 0 && re.prog[pc - 1].ist == regex.ist_or_branch { + return regex.err_syntax_error, i } - re.prog[pc].ist = u32(0) | ist_or_branch + re.prog[pc].ist = u32(0) | regex.ist_or_branch pc = pc + 1 i = i + char_len continue } // Quantifiers - if char_len==1 && pc > 0{ + if char_len == 1 && pc > 0 { mut quant_flag := true match byte(char_tmp) { `?` { - //println("q: ${char_tmp:c}") - re.prog[pc-1].rep_min = 0 - re.prog[pc-1].rep_max = 1 + // println("q: ${char_tmp:c}") + re.prog[pc - 1].rep_min = 0 + re.prog[pc - 1].rep_max = 1 } - `+` { - //println("q: ${char_tmp:c}") - re.prog[pc-1].rep_min = 1 - re.prog[pc-1].rep_max = max_quantifier + // println("q: ${char_tmp:c}") + re.prog[pc - 1].rep_min = 1 + re.prog[pc - 1].rep_max = regex.max_quantifier } - `*` { - //println("q: ${char_tmp:c}") - re.prog[pc-1].rep_min = 0 - re.prog[pc-1].rep_max = max_quantifier + // println("q: ${char_tmp:c}") + re.prog[pc - 1].rep_min = 0 + re.prog[pc - 1].rep_max = regex.max_quantifier } - `{` { - min, max, tmp, greedy := re.parse_quantifier(in_txt, i+1) + min, max, tmp, greedy := re.parse_quantifier(in_txt, i + 1) // it is a quantifier if min >= 0 { - //println("{$min,$max}\n str:[${in_txt[i..i+tmp]}] greedy:$greedy") + // println("{$min,$max}\n str:[${in_txt[i..i+tmp]}] greedy:$greedy") i = i + tmp - re.prog[pc-1].rep_min = min - re.prog[pc-1].rep_max = max - re.prog[pc-1].greedy = greedy + re.prog[pc - 1].rep_min = min + re.prog[pc - 1].rep_max = max + re.prog[pc - 1].greedy = greedy continue - } - else { - return min,i + } else { + return min, i } // TODO: decide if the open bracket can be conform without the close bracket /* @@ -1101,7 +1068,7 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { } */ } - else{ + else { quant_flag = false } } @@ -1113,20 +1080,19 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { } // IST_CHAR_CLASS_* - if char_len==1 && pc >= 0{ + if char_len == 1 && pc >= 0 { if byte(char_tmp) == `[` { - cc_index,tmp,cc_type := re.parse_char_class(in_txt, i+1) + cc_index, tmp, cc_type := re.parse_char_class(in_txt, i + 1) if cc_index >= 0 { - //println("index: $cc_index str:${in_txt[i..i+tmp]}") + // println("index: $cc_index str:${in_txt[i..i+tmp]}") i = i + tmp - re.prog[pc].ist = u32(0) | cc_type + re.prog[pc].ist = u32(0) | cc_type re.prog[pc].cc_index = cc_index - re.prog[pc].rep_min = 1 - re.prog[pc].rep_max = 1 + re.prog[pc].rep_min = 1 + re.prog[pc].rep_max = 1 pc = pc + 1 continue } - // cc_class vector memory full else if cc_index < 0 { return cc_index, i @@ -1135,57 +1101,57 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { } // ist_bsls_char - if char_len==1 && pc >= 0{ + if char_len == 1 && pc >= 0 { if byte(char_tmp) == `\\` { - bsls_index,tmp := re.parse_bsls(in_txt,i) - //println("index: $bsls_index str:${in_txt[i..i+tmp]}") + bsls_index, tmp := re.parse_bsls(in_txt, i) + // println("index: $bsls_index str:${in_txt[i..i+tmp]}") if bsls_index >= 0 { i = i + tmp - re.prog[pc].ist = u32(0) | ist_bsls_char - re.prog[pc].rep_min = 1 - re.prog[pc].rep_max = 1 - re.prog[pc].validator = bsls_validator_array[bsls_index].validator - re.prog[pc].ch = bsls_validator_array[bsls_index].ch + re.prog[pc].ist = u32(0) | regex.ist_bsls_char + re.prog[pc].rep_min = 1 + re.prog[pc].rep_max = 1 + re.prog[pc].validator = regex.bsls_validator_array[bsls_index].validator + re.prog[pc].ch = regex.bsls_validator_array[bsls_index].ch pc = pc + 1 continue } // this is an escape char, skip the bsls and continue as a normal char - else if bsls_index == no_match_found { + else if bsls_index == regex.no_match_found { i += char_len - char_tmp,char_len = re.get_char(in_txt,i) + char_tmp, char_len = re.get_char(in_txt, i) // continue as simple char } // if not an escape or a bsls char then it is an error (at least for now!) else { - return bsls_index, i+tmp + return bsls_index, i + tmp } } } // ist_simple_char - re.prog[pc].ist = ist_simple_char - re.prog[pc].ch = char_tmp - re.prog[pc].ch_len = byte(char_len) + re.prog[pc].ist = regex.ist_simple_char + re.prog[pc].ch = char_tmp + re.prog[pc].ch_len = byte(char_len) re.prog[pc].rep_min = 1 re.prog[pc].rep_max = 1 - //println("char: ${char_tmp:c}") - pc = pc +1 + // println("char: ${char_tmp:c}") + pc = pc + 1 - i+=char_len + i += char_len } // add end of the program - re.prog[pc].ist = ist_prog_end + re.prog[pc].ist = regex.ist_prog_end re.prog_len = pc // check for unbalanced groups if group_stack_index != -1 { - return err_group_not_balanced, group_stack_txt_index[group_stack_index]+1 + return regex.err_group_not_balanced, group_stack_txt_index[group_stack_index] + 1 } // check for OR at the end of the program - if pc > 0 && re.prog[pc-1].ist == ist_or_branch { - return err_syntax_error,in_txt.len + if pc > 0 && re.prog[pc - 1].ist == regex.ist_or_branch { + return regex.err_syntax_error, in_txt.len } // store the number of groups in the query @@ -1204,17 +1170,19 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { mut dot_char_count := 0 mut last_dot_char_pc := -1 for pc1 < pc { - if re.prog[pc1].ist == ist_dot_char { - //println("Dot_char pc: $pc1") + if re.prog[pc1].ist == regex.ist_dot_char { + // println("Dot_char pc: $pc1") last_dot_char_pc = pc1 dot_char_count++ mut pc2 := pc1 + 1 for pc2 < pc { - if re.prog[pc2].ist == ist_dot_char { - return err_syntax_error,0 + if re.prog[pc2].ist == regex.ist_dot_char { + return regex.err_syntax_error, 0 } - if re.prog[pc2].ist !in [rune(ist_prog_end), ist_group_end, ist_group_start] { - //println("Next dot char check is PC: ${pc2}") + if re.prog[pc2].ist !in [rune(regex.ist_prog_end), regex.ist_group_end, + regex.ist_group_start, + ] { + // println("Next dot char check is PC: ${pc2}") re.prog[pc1].dot_check_pc = pc2 break } @@ -1224,12 +1192,12 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { pc1++ } - //println("last_dot_char_pc: $last_dot_char_pc") + // println("last_dot_char_pc: $last_dot_char_pc") if last_dot_char_pc >= 0 { pc1 = last_dot_char_pc + 1 mut is_last_dot := true for pc1 < pc { - if re.prog[pc1].ist !in [rune(ist_prog_end), ist_group_end] { + if re.prog[pc1].ist !in [rune(regex.ist_prog_end), regex.ist_group_end] { is_last_dot = false break } @@ -1248,42 +1216,41 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { // a,b,c branches // set the jump in the right places pc1 = 0 - for pc1 < pc-2 { - //println("Here $pc1 ${pc-2}") + for pc1 < pc - 2 { + // println("Here $pc1 ${pc-2}") // two consecutive OR are a syntax error - if re.prog[pc1+1].ist == ist_or_branch && re.prog[pc1+2].ist == ist_or_branch { - return err_syntax_error, i + if re.prog[pc1 + 1].ist == regex.ist_or_branch + && re.prog[pc1 + 2].ist == regex.ist_or_branch { + return regex.err_syntax_error, i } // manange a|b chains like a|(b)|c|d... // standard solution - if re.prog[pc1].ist != ist_or_branch && - re.prog[pc1+1].ist == ist_or_branch && - re.prog[pc1+2].ist != ist_or_branch - { - re.prog[pc1].next_is_or = true // set that the next token is an OR - re.prog[pc1+1].rep_min = pc1+2 // failed match jump + if re.prog[pc1].ist != regex.ist_or_branch && re.prog[pc1 + 1].ist == regex.ist_or_branch + && re.prog[pc1 + 2].ist != regex.ist_or_branch { + re.prog[pc1].next_is_or = true // set that the next token is an OR + re.prog[pc1 + 1].rep_min = pc1 + 2 // failed match jump // match jump, if an OR chain the next token will be an OR token - mut pc2 := pc1+2 - for pc2 < pc-1 { + mut pc2 := pc1 + 2 + for pc2 < pc - 1 { ist := re.prog[pc2].ist - if ist == ist_group_start { - re.prog[pc1+1].rep_max = re.prog[pc2].goto_pc + 1 + if ist == regex.ist_group_start { + re.prog[pc1 + 1].rep_max = re.prog[pc2].goto_pc + 1 break } - if ist != ist_or_branch { - re.prog[pc1+1].rep_max = pc2 + 1 + if ist != regex.ist_or_branch { + re.prog[pc1 + 1].rep_max = pc2 + 1 break } pc2++ } // special case query of few chars, teh true can't go on the first instruction - if re.prog[pc1+1].rep_max == pc1 { - re.prog[pc1+1].rep_max = 3 + if re.prog[pc1 + 1].rep_max == pc1 { + re.prog[pc1 + 1].rep_max = 3 } - //println("Compile OR postproc. [$pc1,OR ${pc1+1},$pc2]") + // println("Compile OR postproc. [$pc1,OR ${pc1+1},$pc2]") pc1 = pc2 continue } @@ -1296,115 +1263,115 @@ fn (mut re RE) impl_compile(in_txt string) (int,int) { //****************************************** if re.debug > 0 { gc := re.get_code() - re.log_func( gc ) + re.log_func(gc) } //****************************************** - return compile_ok, 0 + return regex.compile_ok, 0 } // get_code return the compiled code as regex string, note: may be different from the source! pub fn (re RE) get_code() string { - mut pc1 := 0 - mut res := strings.new_builder(re.cc.len*2*re.prog.len) - res.write_string("========================================\nv RegEx compiler v $v_regex_version output:\n") - - mut stop_flag := false - - for pc1 <= re.prog.len { - tk := re.prog[pc1] - res.write_string("PC:${pc1:3d}") - - res.write_string(" ist: ") - res.write_string("${tk.ist:8x}".replace(" ","0") ) - res.write_string(" ") - ist :=tk.ist - if ist == ist_bsls_char { - res.write_string("[\\${tk.ch:1c}] BSLS") - } else if ist == ist_prog_end { - res.write_string("PROG_END") - stop_flag = true - } else if ist == ist_or_branch { - res.write_string("OR ") - } else if ist == ist_char_class_pos { - res.write_string("[${re.get_char_class(pc1)}] CHAR_CLASS_POS") - } else if ist == ist_char_class_neg { - res.write_string("[^${re.get_char_class(pc1)}] CHAR_CLASS_NEG") - } else if ist == ist_dot_char { - res.write_string(". DOT_CHAR nx chk: ${tk.dot_check_pc}") - if tk.last_dot_flag == true { - res.write_string(" last!") - } - } else if ist == ist_group_start { - res.write_string("( GROUP_START #:${tk.group_id}") - if tk.group_id == -1 { - res.write_string(" ?:") - } else { - for x in re.group_map.keys() { - if re.group_map[x] == (tk.group_id+1) { - res.write_string(" ?P<${x}>") - break - } + mut pc1 := 0 + mut res := strings.new_builder(re.cc.len * 2 * re.prog.len) + res.write_string('========================================\nv RegEx compiler v $regex.v_regex_version output:\n') + + mut stop_flag := false + + for pc1 <= re.prog.len { + tk := re.prog[pc1] + res.write_string('PC:${pc1:3d}') + + res.write_string(' ist: ') + res.write_string('${tk.ist:8x}'.replace(' ', '0')) + res.write_string(' ') + ist := tk.ist + if ist == regex.ist_bsls_char { + res.write_string('[\\${tk.ch:1c}] BSLS') + } else if ist == regex.ist_prog_end { + res.write_string('PROG_END') + stop_flag = true + } else if ist == regex.ist_or_branch { + res.write_string('OR ') + } else if ist == regex.ist_char_class_pos { + res.write_string('[${re.get_char_class(pc1)}] CHAR_CLASS_POS') + } else if ist == regex.ist_char_class_neg { + res.write_string('[^${re.get_char_class(pc1)}] CHAR_CLASS_NEG') + } else if ist == regex.ist_dot_char { + res.write_string('. DOT_CHAR nx chk: $tk.dot_check_pc') + if tk.last_dot_flag == true { + res.write_string(' last!') + } + } else if ist == regex.ist_group_start { + res.write_string('( GROUP_START #:$tk.group_id') + if tk.group_id == -1 { + res.write_string(' ?:') + } else { + for x in re.group_map.keys() { + if re.group_map[x] == (tk.group_id + 1) { + res.write_string(' ?P<$x>') + break } } - } else if ist == ist_group_end { - res.write_string(") GROUP_END #:${tk.group_id}") - } else if ist == ist_simple_char { - res.write_string("[${tk.ch:1c}] query_ch") } + } else if ist == regex.ist_group_end { + res.write_string(') GROUP_END #:$tk.group_id') + } else if ist == regex.ist_simple_char { + res.write_string('[${tk.ch:1c}] query_ch') + } - if tk.rep_max == max_quantifier { - res.write_string(" {${tk.rep_min:3d},MAX}") - }else{ - if ist == ist_or_branch { - res.write_string(" if false go: ${tk.rep_min:3d} if true go: ${tk.rep_max:3d}") - } else { - res.write_string(" {${tk.rep_min:3d},${tk.rep_max:3d}}") - } - if tk.greedy == true { - res.write_string("?") - } + if tk.rep_max == regex.max_quantifier { + res.write_string(' {${tk.rep_min:3d},MAX}') + } else { + if ist == regex.ist_or_branch { + res.write_string(' if false go: ${tk.rep_min:3d} if true go: ${tk.rep_max:3d}') + } else { + res.write_string(' {${tk.rep_min:3d},${tk.rep_max:3d}}') } - - res.write_string("\n") - if stop_flag { - break + if tk.greedy == true { + res.write_string('?') } - pc1++ } - res.write_string("========================================\n") - return res.str() + res.write_string('\n') + if stop_flag { + break + } + pc1++ + } + + res.write_string('========================================\n') + return res.str() } // get_query return a string with a reconstruction of the query starting from the regex program code pub fn (re RE) get_query() string { - mut res := strings.new_builder(re.query.len*2) + mut res := strings.new_builder(re.query.len * 2) - if (re.flag & f_ms) != 0 { - res.write_string("^") + if (re.flag & regex.f_ms) != 0 { + res.write_string('^') } mut i := 0 - for i < re.prog.len && re.prog[i].ist != ist_prog_end && re.prog[i].ist != 0{ + for i < re.prog.len && re.prog[i].ist != regex.ist_prog_end && re.prog[i].ist != 0 { tk := unsafe { &re.prog[i] } ch := tk.ist // GROUP start - if ch == ist_group_start { + if ch == regex.ist_group_start { if re.debug == 0 { - res.write_string("(") + res.write_string('(') } else { if tk.group_id == -1 { - res.write_string("(?:") // non capturing group + res.write_string('(?:') // non capturing group } else { - res.write_string("#${tk.group_id}(") + res.write_string('#${tk.group_id}(') } } for x in re.group_map.keys() { - if re.group_map[x] == (tk.group_id+1) { - res.write_string("?P<${x}>") + if re.group_map[x] == (tk.group_id + 1) { + res.write_string('?P<$x>') break } } @@ -1414,71 +1381,71 @@ pub fn (re RE) get_query() string { } // GROUP end - if ch == ist_group_end { - res.write_string(")") + if ch == regex.ist_group_end { + res.write_string(')') } // OR branch - if ch == ist_or_branch { - res.write_string("|") + if ch == regex.ist_or_branch { + res.write_string('|') if re.debug > 0 { - res.write_string("{${tk.rep_min},${tk.rep_max}}") + res.write_string('{$tk.rep_min,$tk.rep_max}') } i++ continue } // char class - if ch == ist_char_class_neg || ch == ist_char_class_pos { - res.write_string("[") - if ch == ist_char_class_neg { - res.write_string("^") + if ch == regex.ist_char_class_neg || ch == regex.ist_char_class_pos { + res.write_string('[') + if ch == regex.ist_char_class_neg { + res.write_string('^') } - res.write_string("${re.get_char_class(i)}") - res.write_string("]") + res.write_string('${re.get_char_class(i)}') + res.write_string(']') } // bsls char - if ch == ist_bsls_char { - res.write_string("\\${tk.ch:1c}") + if ch == regex.ist_bsls_char { + res.write_string('\\${tk.ch:1c}') } // ist_dot_char - if ch == ist_dot_char { - res.write_string(".") + if ch == regex.ist_dot_char { + res.write_string('.') } // char alone - if ch == ist_simple_char { - if byte(ch) in bsls_escape_list { - res.write_string("\\") + if ch == regex.ist_simple_char { + if byte(ch) in regex.bsls_escape_list { + res.write_string('\\') } - res.write_string("${tk.ch:c}") + res.write_string('${tk.ch:c}') } // quantifier if !(tk.rep_min == 1 && tk.rep_max == 1) { if tk.rep_min == 0 && tk.rep_max == 1 { - res.write_string("?") - } else if tk.rep_min == 1 && tk.rep_max == max_quantifier { - res.write_string("+") - } else if tk.rep_min == 0 && tk.rep_max == max_quantifier { - res.write_string("*") + res.write_string('?') + } else if tk.rep_min == 1 && tk.rep_max == regex.max_quantifier { + res.write_string('+') + } else if tk.rep_min == 0 && tk.rep_max == regex.max_quantifier { + res.write_string('*') } else { - if tk.rep_max == max_quantifier { - res.write_string("{${tk.rep_min},MAX}") + if tk.rep_max == regex.max_quantifier { + res.write_string('{$tk.rep_min,MAX}') } else { - res.write_string("{${tk.rep_min},${tk.rep_max}}") + res.write_string('{$tk.rep_min,$tk.rep_max}') } if tk.greedy == true { - res.write_string("?") + res.write_string('?') } } } i++ } - if (re.flag & f_me) != 0 { - res.write_string("$") + if (re.flag & regex.f_me) != 0 { + res.write_string('$') } return res.str() @@ -1499,15 +1466,13 @@ fn (mut re RE) group_continuous_save(g_index int) { re.group_csave << 0 } - gi := g_index >> 1 + gi := g_index >> 1 start := re.groups[g_index] - end := re.groups[g_index+1] + end := re.groups[g_index + 1] // check if we are simply increasing the size ot the found group - if re.group_csave.len >=4 && - gi == re.group_csave[re.group_csave.len - 3] && - start == re.group_csave[re.group_csave.len - 2] - { + if re.group_csave.len >= 4 && gi == re.group_csave[re.group_csave.len - 3] + && start == re.group_csave[re.group_csave.len - 2] { re.group_csave[re.group_csave.len - 1] = end return } @@ -1517,9 +1482,9 @@ fn (mut re RE) group_continuous_save(g_index int) { // increment counter re.group_csave[0]++ // save the record - re.group_csave << (g_index >> 1) // group id - re.group_csave << re.groups[g_index] // start - re.group_csave << re.groups[g_index+1] // end + re.group_csave << (g_index >> 1) // group id + re.group_csave << re.groups[g_index] // start + re.group_csave << re.groups[g_index + 1] // end } } @@ -1528,161 +1493,154 @@ fn (mut re RE) group_continuous_save(g_index int) { * Matching * ******************************************************************************/ -enum Match_state{ +enum Match_state { start = 0 stop end new_line - - ist_load // load and execute instruction - ist_next // go to next instruction - ist_next_ks // go to next instruction without clenaning the state - ist_quant_p // match positive ,quantifier check - ist_quant_n // match negative, quantifier check + ist_load // load and execute instruction + ist_next // go to next instruction + ist_next_ks // go to next instruction without clenaning the state + ist_quant_p // match positive ,quantifier check + ist_quant_n // match negative, quantifier check ist_quant_pg // match positive ,group quantifier check ist_quant_ng // match negative ,group quantifier check } fn state_str(s Match_state) string { - match s{ - .start { return "start" } - .stop { return "stop" } - .end { return "end" } - .new_line { return "new line" } - - .ist_load { return "ist_load" } - .ist_next { return "ist_next" } - .ist_next_ks { return "ist_next_ks" } - .ist_quant_p { return "ist_quant_p" } - .ist_quant_n { return "ist_quant_n" } - .ist_quant_pg { return "ist_quant_pg" } - .ist_quant_ng { return "ist_quant_ng" } + match s { + .start { return 'start' } + .stop { return 'stop' } + .end { return 'end' } + .new_line { return 'new line' } + .ist_load { return 'ist_load' } + .ist_next { return 'ist_next' } + .ist_next_ks { return 'ist_next_ks' } + .ist_quant_p { return 'ist_quant_p' } + .ist_quant_n { return 'ist_quant_n' } + .ist_quant_pg { return 'ist_quant_pg' } + .ist_quant_ng { return 'ist_quant_ng' } } } struct StateObj { pub mut: - group_index int = -1 // group id used to know how many groups are open - match_flag bool // indicate if we are in a match condition - match_index int = -1 // index of the last match - first_match int = -1 // index of the first match - pc int = -1 // program counter - i int = -1 // source string index - char_len int // last char legth - last_dot_pc int = -1 // last dot chat pc + group_index int = -1 // group id used to know how many groups are open + match_flag bool // indicate if we are in a match condition + match_index int = -1 // index of the last match + first_match int = -1 // index of the first match + pc int = -1 // program counter + i int = -1 // source string index + char_len int // last char legth + last_dot_pc int = -1 // last dot chat pc } [direct_array_access] -pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { +pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int) (int, int) { // result status - mut result := no_match_found // function return + mut result := regex.no_match_found // function return - mut ch := rune(0) // examinated char - mut char_len := 0 // utf8 examinated char len - mut m_state := Match_state.start // start point for the matcher FSM - mut src_end := false + mut ch := rune(0) // examinated char + mut char_len := 0 // utf8 examinated char len + mut m_state := Match_state.start // start point for the matcher FSM + mut src_end := false mut last_fnd_pc := -1 - mut state := StateObj{} // actual state - mut ist := rune(0) // actual instruction - mut l_ist := rune(0) // last matched instruction + mut state := StateObj{} // actual state + mut ist := rune(0) // actual instruction + mut l_ist := rune(0) // last matched instruction - mut step_count := 0 // stats for debug - mut dbg_line := 0 // count debug line printed + mut step_count := 0 // stats for debug + mut dbg_line := 0 // count debug line printed re.reset() - if re.debug>0 { + if re.debug > 0 { // print header mut h_buf := strings.new_builder(32) - h_buf.write_string("flags: ") - h_buf.write_string("${re.flag:8x}".replace(" ","0")) - h_buf.write_string("\n") + h_buf.write_string('flags: ') + h_buf.write_string('${re.flag:8x}'.replace(' ', '0')) + h_buf.write_string('\n') sss := h_buf.str() re.log_func(sss) } for m_state != .end { - if state.pc >= 0 && state.pc < re.prog.len { ist = re.prog[state.pc].ist - }else if state.pc >= re.prog.len { - //println("ERROR!! PC overflow!!") - return err_internal_error, state.i + } else if state.pc >= re.prog.len { + // println("ERROR!! PC overflow!!") + return regex.err_internal_error, state.i } //****************************************** // DEBUG LOG //****************************************** - if re.debug>0 { + if re.debug > 0 { mut buf2 := strings.new_builder(re.cc.len + 128) // print all the instructions // end of the input text if state.i >= in_txt_len { - buf2.write_string("# ${step_count:3d} END OF INPUT TEXT\n") + buf2.write_string('# ${step_count:3d} END OF INPUT TEXT\n') sss := buf2.str() re.log_func(sss) - }else{ - + } else { // print only the exe instruction - if (re.debug == 1 && m_state == .ist_load) || - re.debug == 2 - { - if ist == ist_prog_end { - buf2.write_string("# ${step_count:3d} PROG_END\n") - } - else if ist == 0 || m_state in [.start,.ist_next,.stop] { - buf2.write_string("# ${step_count:3d} s: ${state_str(m_state):12s} PC: NA\n") - }else{ + if (re.debug == 1 && m_state == .ist_load) || re.debug == 2 { + if ist == regex.ist_prog_end { + buf2.write_string('# ${step_count:3d} PROG_END\n') + } else if ist == 0 || m_state in [.start, .ist_next, .stop] { + buf2.write_string('# ${step_count:3d} s: ${state_str(m_state):12s} PC: NA\n') + } else { ch, char_len = re.get_charb(in_txt, state.i) - buf2.write_string("# ${step_count:3d} s: ${state_str(m_state):12s} PC: ${state.pc:3d}=>") - buf2.write_string("${ist:8x}".replace(" ","0")) - buf2.write_string(" i,ch,len:[${state.i:3d},'${utf8_str(ch)}',${char_len}] f.m:[${state.first_match:3d},${state.match_index:3d}] ") + buf2.write_string('# ${step_count:3d} s: ${state_str(m_state):12s} PC: ${state.pc:3d}=>') + buf2.write_string('${ist:8x}'.replace(' ', '0')) + buf2.write_string(" i,ch,len:[${state.i:3d},'${utf8_str(ch)}',$char_len] f.m:[${state.first_match:3d},${state.match_index:3d}] ") - if ist == ist_simple_char { - buf2.write_string("query_ch: [${re.prog[state.pc].ch:1c}]") + if ist == regex.ist_simple_char { + buf2.write_string('query_ch: [${re.prog[state.pc].ch:1c}]') } else { - if ist == ist_bsls_char { - buf2.write_string("BSLS [\\${re.prog[state.pc].ch:1c}]") - } else if ist == ist_prog_end { - buf2.write_string("PROG_END") - } else if ist == ist_or_branch { - buf2.write_string("OR") - } else if ist == ist_char_class_pos { - buf2.write_string("CHAR_CLASS_POS[${re.get_char_class(state.pc)}]") - } else if ist == ist_char_class_neg { - buf2.write_string("CHAR_CLASS_NEG[${re.get_char_class(state.pc)}]") - } else if ist == ist_dot_char { - buf2.write_string("DOT_CHAR") - } else if ist == ist_group_start { - tmp_gi :=re.prog[state.pc].group_id + if ist == regex.ist_bsls_char { + buf2.write_string('BSLS [\\${re.prog[state.pc].ch:1c}]') + } else if ist == regex.ist_prog_end { + buf2.write_string('PROG_END') + } else if ist == regex.ist_or_branch { + buf2.write_string('OR') + } else if ist == regex.ist_char_class_pos { + buf2.write_string('CHAR_CLASS_POS[${re.get_char_class(state.pc)}]') + } else if ist == regex.ist_char_class_neg { + buf2.write_string('CHAR_CLASS_NEG[${re.get_char_class(state.pc)}]') + } else if ist == regex.ist_dot_char { + buf2.write_string('DOT_CHAR') + } else if ist == regex.ist_group_start { + tmp_gi := re.prog[state.pc].group_id tmp_gr := re.prog[re.prog[state.pc].goto_pc].group_rep - buf2.write_string("GROUP_START #:${tmp_gi} rep:${tmp_gr} ") - } else if ist == ist_group_end { - buf2.write_string("GROUP_END #:${re.prog[state.pc].group_id} deep:${state.group_index}") + buf2.write_string('GROUP_START #:$tmp_gi rep:$tmp_gr ') + } else if ist == regex.ist_group_end { + buf2.write_string('GROUP_END #:${re.prog[state.pc].group_id} deep:$state.group_index') } } - if re.prog[state.pc].rep_max == max_quantifier { - buf2.write_string("{${re.prog[state.pc].rep_min},MAX}:${re.prog[state.pc].rep}") + if re.prog[state.pc].rep_max == regex.max_quantifier { + buf2.write_string('{${re.prog[state.pc].rep_min},MAX}:${re.prog[state.pc].rep}') } else { - buf2.write_string("{${re.prog[state.pc].rep_min},${re.prog[state.pc].rep_max}}:${re.prog[state.pc].rep}") + buf2.write_string('{${re.prog[state.pc].rep_min},${re.prog[state.pc].rep_max}}:${re.prog[state.pc].rep}') } if re.prog[state.pc].greedy == true { - buf2.write_string("?") + buf2.write_string('?') } - buf2.write_string(" (#${state.group_index})") + buf2.write_string(' (#$state.group_index)') - if ist == ist_dot_char { - buf2.write_string(" last!") + if ist == regex.ist_dot_char { + buf2.write_string(' last!') } - buf2.write_string("\n") + buf2.write_string('\n') } sss2 := buf2.str() - re.log_func( sss2 ) + re.log_func(sss2) } } step_count++ @@ -1690,31 +1648,32 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { } //****************************************** - if ist == ist_prog_end { - //println("HERE we end!") + if ist == regex.ist_prog_end { + // println("HERE we end!") break } // we're out of text, manage it if state.i >= in_txt_len || m_state == .new_line { - //println("Finished text!!") + // println("Finished text!!") src_end = true // manage groups if state.group_index >= 0 && state.match_index >= 0 { - //println("End text with open groups!") + // println("End text with open groups!") // close the groups for state.group_index >= 0 { tmp_pc := re.group_data[state.group_index] re.prog[tmp_pc].group_rep++ - //println("Closing group $state.group_index {${re.prog[tmp_pc].rep_min},${re.prog[tmp_pc].rep_max}}:${re.prog[tmp_pc].group_rep}") + // println("Closing group $state.group_index {${re.prog[tmp_pc].rep_min},${re.prog[tmp_pc].rep_max}}:${re.prog[tmp_pc].group_rep}") - if re.prog[tmp_pc].group_rep >= re.prog[tmp_pc].rep_min && re.prog[tmp_pc].group_id >= 0{ - start_i := re.group_stack[state.group_index] - re.group_stack[state.group_index]=-1 + if re.prog[tmp_pc].group_rep >= re.prog[tmp_pc].rep_min + && re.prog[tmp_pc].group_id >= 0 { + start_i := re.group_stack[state.group_index] + re.group_stack[state.group_index] = -1 - // save group results - g_index := re.prog[tmp_pc].group_id*2 + // save group results + g_index := re.prog[tmp_pc].group_id * 2 if start_i >= 0 { re.groups[g_index] = start_i } else { @@ -1722,25 +1681,25 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { } // we have fished the text, we must manage out pf bound indexes if state.i >= in_txt_len { - state.i = in_txt_len-1 + state.i = in_txt_len - 1 } - re.groups[g_index+1] = state.i + re.groups[g_index + 1] = state.i - if re.groups[g_index+1] >= in_txt_len { - //println("clamp group on stop!") - re.groups[g_index+1] = in_txt_len-1 + if re.groups[g_index + 1] >= in_txt_len { + // println("clamp group on stop!") + re.groups[g_index + 1] = in_txt_len - 1 } // continuous save, save until we have space re.group_continuous_save(g_index) - } + } state.group_index-- } } // the text is finished and the groups closed and we are the last group, ok exit - if ist == ist_group_end && re.prog[state.pc+1].ist == ist_prog_end { - //println("Last group end") + if ist == regex.ist_group_end && re.prog[state.pc + 1].ist == regex.ist_prog_end { + // println("Last group end") return state.first_match, state.i } @@ -1748,64 +1707,61 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { state.pc = last_fnd_pc } - //println("Finished text!!") - //println("Instruction: ${ist:08x} pc: $state.pc") - //println("min_rep: ${re.prog[state.pc].rep_min} max_rep: ${re.prog[state.pc].rep_max} rep: ${re.prog[state.pc].rep}") + // println("Finished text!!") + // println("Instruction: ${ist:08x} pc: $state.pc") + // println("min_rep: ${re.prog[state.pc].rep_min} max_rep: ${re.prog[state.pc].rep_max} rep: ${re.prog[state.pc].rep}") // program end - if ist == ist_prog_end { - //println("Program end on end of text!") + if ist == regex.ist_prog_end { + // println("Program end on end of text!") return state.first_match, state.i } // we are in a last dot_ char case - if l_ist == ist_dot_char { - //println("***** We have a last dot_char") - //println("PC: ${state.pc} last_dot_flag:${re.prog[state.pc].last_dot_flag}") - //println("rep: ${re.prog[state.pc].group_rep} min: ${re.prog[state.pc].rep_min} max: ${re.prog[state.pc].rep_max}") - //println("first match: ${state.first_match}") - if re.prog[state.pc].last_dot_flag == true && - re.prog[state.pc].rep >= re.prog[state.pc].rep_min && - re.prog[state.pc].rep <= re.prog[state.pc].rep_max - { + if l_ist == regex.ist_dot_char { + // println("***** We have a last dot_char") + // println("PC: ${state.pc} last_dot_flag:${re.prog[state.pc].last_dot_flag}") + // println("rep: ${re.prog[state.pc].group_rep} min: ${re.prog[state.pc].rep_min} max: ${re.prog[state.pc].rep_max}") + // println("first match: ${state.first_match}") + if re.prog[state.pc].last_dot_flag == true + && re.prog[state.pc].rep >= re.prog[state.pc].rep_min + && re.prog[state.pc].rep <= re.prog[state.pc].rep_max { return state.first_match, state.i } - //println("Not fitted!!") + // println("Not fitted!!") } - //m_state = .end - //break - return no_match_found,0 + // m_state = .end + // break + return regex.no_match_found, 0 } // starting and init if m_state == .start { state.pc = -1 - state.i = 0 + state.i = 0 m_state = .ist_next continue } - // ist_next, next instruction reseting its state else if m_state == .ist_next { state.pc = state.pc + 1 re.prog[state.pc].reset() // check if we are in the program bounds if state.pc < 0 || state.pc > re.prog.len { - //println("ERROR!! PC overflow!!") - return err_internal_error, state.i + // println("ERROR!! PC overflow!!") + return regex.err_internal_error, state.i } m_state = .ist_load continue } - // ist_next_ks, next instruction keeping its state else if m_state == .ist_next_ks { state.pc = state.pc + 1 // check if we are in the program bounds if state.pc < 0 || state.pc > re.prog.len { - //println("ERROR!! PC overflow!!") - return err_internal_error, state.i + // println("ERROR!! PC overflow!!") + return regex.err_internal_error, state.i } m_state = .ist_load continue @@ -1815,16 +1771,14 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { ch, char_len = re.get_charb(in_txt, state.i) // check new line if flag f_nl enabled - if (re.flag & f_nl) != 0 && char_len == 1 && byte(ch) in new_line_list { + if (re.flag & regex.f_nl) != 0 && char_len == 1 && byte(ch) in regex.new_line_list { m_state = .new_line continue } - // check if stop else if m_state == .stop { - // we are in search mode, don't exit until the end - if ((re.flag & f_src) != 0) && (ist != ist_prog_end) { + if ((re.flag & regex.f_src) != 0) && (ist != regex.ist_prog_end) { last_fnd_pc = state.pc state.pc = -1 state.i += char_len @@ -1836,18 +1790,18 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { continue } - if ist == ist_prog_end { + if ist == regex.ist_prog_end { return state.first_match, state.i } // manage here dot char if re.state_list.len > 0 { - //println("Here we are, with stop: state buffer: [${re.state_list.len}]") + // println("Here we are, with stop: state buffer: [${re.state_list.len}]") state = re.state_list.pop() state.match_flag = true - l_ist = u32(ist_dot_char) + l_ist = u32(regex.ist_dot_char) if state.first_match < 0 { state.first_match = state.i @@ -1861,14 +1815,12 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { } // exit on no match - return result,0 + return result, 0 } - // ist_load else if m_state == .ist_load { - // program end - if ist == ist_prog_end { + if ist == regex.ist_prog_end { // if we are in match exit well if state.group_index >= 0 && state.match_index >= 0 { @@ -1877,33 +1829,30 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { m_state = .stop continue - } - // check GROUP start, no quantifier is checkd for this token!! - else if ist == ist_group_start { + else if ist == regex.ist_group_start { state.group_index++ - re.group_data[state.group_index] = re.prog[state.pc].goto_pc // save where is ist_group_end, we will use it for escape - re.group_stack[state.group_index] = state.i // index where we start to manage - //println("group_index $state.group_index rep ${re.prog[re.prog[state.pc].goto_pc].group_rep}") + re.group_data[state.group_index] = re.prog[state.pc].goto_pc // save where is ist_group_end, we will use it for escape + re.group_stack[state.group_index] = state.i // index where we start to manage + // println("group_index $state.group_index rep ${re.prog[re.prog[state.pc].goto_pc].group_rep}") m_state = .ist_next continue } - // check GROUP end - else if ist == ist_group_end { + else if ist == regex.ist_group_end { // we are in matching streak - //println("Group END!! last ist: ${l_ist:08x}") + // println("Group END!! last ist: ${l_ist:08x}") if state.match_index >= 0 { // restore txt index stack and save the group data - //println("g.id: ${re.prog[state.pc].group_id} group_index: ${state.group_index}") + // println("g.id: ${re.prog[state.pc].group_id} group_index: ${state.group_index}") if state.group_index >= 0 && re.prog[state.pc].group_id >= 0 { - start_i := re.group_stack[state.group_index] + start_i := re.group_stack[state.group_index] - // save group results - g_index := re.prog[state.pc].group_id*2 + // save group results + g_index := re.prog[state.pc].group_id * 2 if start_i >= 0 { re.groups[g_index] = start_i @@ -1911,111 +1860,106 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { re.groups[g_index] = 0 } - re.groups[g_index+1] = state.i + re.groups[g_index + 1] = state.i - if g_index > 0 && re.groups[g_index] <= re.groups[g_index-1] { - re.groups[g_index] = re.groups[g_index-1] + if g_index > 0 && re.groups[g_index] <= re.groups[g_index - 1] { + re.groups[g_index] = re.groups[g_index - 1] } - if re.groups[g_index+1] >= in_txt_len { - //println("clamp group!") - re.groups[g_index+1] = in_txt_len-1 + if re.groups[g_index + 1] >= in_txt_len { + // println("clamp group!") + re.groups[g_index + 1] = in_txt_len - 1 } - //println("GROUP ${re.prog[state.pc].group_id} END [${re.groups[g_index]}, ${re.groups[g_index+1]}] i: $state.i in_txt_len: $in_txt_len") + // println("GROUP ${re.prog[state.pc].group_id} END [${re.groups[g_index]}, ${re.groups[g_index+1]}] i: $state.i in_txt_len: $in_txt_len") // continuous save, save until we have space re.group_continuous_save(g_index) } re.prog[state.pc].group_rep++ // increase repetitions - //println("GROUP $group_index END ${re.prog[state.pc].group_rep}") + // println("GROUP $group_index END ${re.prog[state.pc].group_rep}") m_state = .ist_quant_pg continue - } m_state = .ist_quant_ng continue } - // check OR - else if ist == ist_or_branch { + else if ist == regex.ist_or_branch { if state.match_index >= 0 { state.pc = re.prog[state.pc].rep_max - //println("ist_or_branch True pc: $state.pc") - }else{ + // println("ist_or_branch True pc: $state.pc") + } else { state.pc = re.prog[state.pc].rep_min - //println("ist_or_branch False pc: $state.pc") + // println("ist_or_branch False pc: $state.pc") } re.prog[state.pc].reset() m_state = .ist_load continue } - // check ist_dot_char - else if ist == ist_dot_char { - //println("ist_dot_char rep: ${re.prog[state.pc].rep}") + else if ist == regex.ist_dot_char { + // println("ist_dot_char rep: ${re.prog[state.pc].rep}") // check next token to be false mut next_check_flag := false // if we are done with max go on dot char are dedicated case!! - if re.prog[state.pc].rep >= re.prog[state.pc].rep_max - { + if re.prog[state.pc].rep >= re.prog[state.pc].rep_max { re.state_list.pop() m_state = .ist_next continue } - if re.prog[state.pc].dot_check_pc >= 0 && re.prog[state.pc].rep >= re.prog[state.pc].rep_min { + if re.prog[state.pc].dot_check_pc >= 0 + && re.prog[state.pc].rep >= re.prog[state.pc].rep_min { // load the char - //ch_t, _ := re.get_charb(in_txt, state.i+char_len) + // ch_t, _ := re.get_charb(in_txt, state.i+char_len) ch_t := ch chk_pc := re.prog[state.pc].dot_check_pc // simple char - if re.prog[chk_pc].ist == ist_simple_char { + if re.prog[chk_pc].ist == regex.ist_simple_char { if re.prog[chk_pc].ch == ch_t { next_check_flag = true } - //println("Check [ist_simple_char] [${re.prog[chk_pc].ch}]==[${ch_t:c}] => $next_check_flag") + // println("Check [ist_simple_char] [${re.prog[chk_pc].ch}]==[${ch_t:c}] => $next_check_flag") } - // char char_class - else if re.prog[chk_pc].ist == ist_char_class_pos || re.prog[chk_pc].ist == ist_char_class_neg { + else if re.prog[chk_pc].ist == regex.ist_char_class_pos + || re.prog[chk_pc].ist == regex.ist_char_class_neg { mut cc_neg := false - if re.prog[chk_pc].ist == ist_char_class_neg { + if re.prog[chk_pc].ist == regex.ist_char_class_neg { cc_neg = true } - mut cc_res := re.check_char_class(chk_pc,ch_t) + mut cc_res := re.check_char_class(chk_pc, ch_t) if cc_neg { cc_res = !cc_res } next_check_flag = cc_res - //println("Check [ist_char_class] => $next_check_flag") + // println("Check [ist_char_class] => $next_check_flag") } - // check bsls - else if re.prog[chk_pc].ist == ist_bsls_char { + else if re.prog[chk_pc].ist == regex.ist_bsls_char { next_check_flag = re.prog[chk_pc].validator(byte(ch_t)) - //println("Check [ist_bsls_char] => $next_check_flag") + // println("Check [ist_bsls_char] => $next_check_flag") } - } // check if we must continue or pass to the next IST - if next_check_flag == true && re.prog[state.pc+1].ist != ist_prog_end { - //println("save the state!!") - mut dot_state := StateObj { + if next_check_flag == true && re.prog[state.pc + 1].ist != regex.ist_prog_end { + // println("save the state!!") + mut dot_state := StateObj{ group_index: state.group_index - match_flag: state.match_flag + match_flag: state.match_flag match_index: state.match_index first_match: state.first_match - pc: state.pc - i: state.i + char_len - char_len: char_len + pc: state.pc + i: state.i + char_len + char_len: char_len last_dot_pc: state.pc } // if we are mananging a .* stay on the same char on return @@ -2026,12 +1970,12 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { re.state_list << dot_state m_state = .ist_quant_n - //println("dot_char stack len: ${re.state_list.len}") + // println("dot_char stack len: ${re.state_list.len}") continue } state.match_flag = true - l_ist = u32(ist_dot_char) + l_ist = u32(regex.ist_dot_char) if state.first_match < 0 { state.first_match = state.i @@ -2042,18 +1986,16 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { state.i += char_len m_state = .ist_quant_p continue - } - // char class IST - else if ist == ist_char_class_pos || ist == ist_char_class_neg { + else if ist == regex.ist_char_class_pos || ist == regex.ist_char_class_neg { state.match_flag = false mut cc_neg := false - if ist == ist_char_class_neg { + if ist == regex.ist_char_class_neg { cc_neg = true } - mut cc_res := re.check_char_class(state.pc,ch) + mut cc_res := re.check_char_class(state.pc, ch) if cc_neg { cc_res = !cc_res @@ -2061,7 +2003,7 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { if cc_res { state.match_flag = true - l_ist = u32(ist_char_class_pos) + l_ist = u32(regex.ist_char_class_pos) if state.first_match < 0 { state.first_match = state.i @@ -2077,15 +2019,14 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { m_state = .ist_quant_n continue } - // check bsls - else if ist == ist_bsls_char { + else if ist == regex.ist_bsls_char { state.match_flag = false tmp_res := re.prog[state.pc].validator(byte(ch)) - //println("BSLS in_ch: ${ch:c} res: $tmp_res") + // println("BSLS in_ch: ${ch:c} res: $tmp_res") if tmp_res { state.match_flag = true - l_ist = u32(ist_bsls_char) + l_ist = u32(regex.ist_bsls_char) if state.first_match < 0 { state.first_match = state.i @@ -2101,21 +2042,19 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { m_state = .ist_quant_n continue } - // simple char IST - else if ist == ist_simple_char { - //println("ist_simple_char") + else if ist == regex.ist_simple_char { + // println("ist_simple_char") state.match_flag = false - if re.prog[state.pc].ch == ch - { + if re.prog[state.pc].ch == ch { state.match_flag = true - l_ist = ist_simple_char + l_ist = regex.ist_simple_char if state.first_match < 0 { state.first_match = state.i } - //println("state.match_index: ${state.match_index}") + // println("state.match_index: ${state.match_index}") state.match_index = state.i re.prog[state.pc].rep++ // increase repetitions @@ -2126,55 +2065,50 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { m_state = .ist_quant_n continue } - /* UNREACHABLE */ - //println("PANIC2!! state: $m_state") - return err_internal_error, state.i - + // UNREACHABLE + // println("PANIC2!! state: $m_state") + return regex.err_internal_error, state.i } - /*********************************** * Quantifier management ***********************************/ // ist_quant_ng => quantifier negative test on group else if m_state == .ist_quant_ng { - // we are finished here if state.group_index < 0 { - //println("Early stop!") - result = no_match_found + // println("Early stop!") + result = regex.no_match_found m_state = .stop continue } - tmp_pc := re.group_data[state.group_index] // PC to the end of the group token - rep := re.prog[tmp_pc].group_rep // use a temp variable - re.prog[tmp_pc].group_rep = 0 // clear the repetitions + tmp_pc := re.group_data[state.group_index] // PC to the end of the group token + rep := re.prog[tmp_pc].group_rep // use a temp variable + re.prog[tmp_pc].group_rep = 0 // clear the repetitions - //println(".ist_quant_ng group_pc_end: $tmp_pc rep: $rep") + // println(".ist_quant_ng group_pc_end: $tmp_pc rep: $rep") if rep >= re.prog[tmp_pc].rep_min { - //println("ist_quant_ng GROUP CLOSED OK group_index: $state.group_index") + // println("ist_quant_ng GROUP CLOSED OK group_index: $state.group_index") state.i = re.group_stack[state.group_index] state.pc = tmp_pc state.group_index-- m_state = .ist_next continue - } - else if re.prog[tmp_pc].next_is_or { - //println("ist_quant_ng OR Negative branch") + } else if re.prog[tmp_pc].next_is_or { + // println("ist_quant_ng OR Negative branch") state.i = re.group_stack[state.group_index] - state.pc = re.prog[tmp_pc+1].rep_min -1 + state.pc = re.prog[tmp_pc + 1].rep_min - 1 state.group_index-- m_state = .ist_next continue - } - else if rep>0 && rep < re.prog[tmp_pc].rep_min { - //println("ist_quant_ng UNDER THE MINIMUM g.i: $state.group_index") + } else if rep > 0 && rep < re.prog[tmp_pc].rep_min { + // println("ist_quant_ng UNDER THE MINIMUM g.i: $state.group_index") // check if we are inside a group, if yes exit from the nested groups - if state.group_index > 0{ + if state.group_index > 0 { state.group_index-- state.pc = tmp_pc m_state = .ist_quant_ng //.ist_next @@ -2188,33 +2122,31 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { continue } - result = no_match_found + result = regex.no_match_found m_state = .stop continue - } - else if rep==0 && rep < re.prog[tmp_pc].rep_min { - //println("ist_quant_ng c_zero UNDER THE MINIMUM g.i: $state.group_index") + } else if rep == 0 && rep < re.prog[tmp_pc].rep_min { + // println("ist_quant_ng c_zero UNDER THE MINIMUM g.i: $state.group_index") - if state.group_index > 0{ + if state.group_index > 0 { state.group_index-- state.pc = tmp_pc m_state = .ist_quant_ng //.ist_next continue } - result = no_match_found + result = regex.no_match_found m_state = .stop continue } - //println("DO NOT STAY HERE!! {${re.prog[tmp_pc].rep_min},${re.prog[tmp_pc].rep_max}}:$rep") - /* UNREACHABLE */ - return err_internal_error, state.i - + // println("DO NOT STAY HERE!! {${re.prog[tmp_pc].rep_min},${re.prog[tmp_pc].rep_max}}:$rep") + // UNREACHABLE + return regex.err_internal_error, state.i } // ist_quant_pg => quantifier positive test on group else if m_state == .ist_quant_pg { - //println(".ist_quant_pg") + // println(".ist_quant_pg") mut tmp_pc := state.pc if state.group_index >= 0 { tmp_pc = re.group_data[state.group_index] @@ -2223,21 +2155,19 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { rep := re.prog[tmp_pc].group_rep if rep < re.prog[tmp_pc].rep_min { - //println("ist_quant_pg UNDER RANGE") + // println("ist_quant_pg UNDER RANGE") state.pc = re.prog[tmp_pc].goto_pc m_state = .ist_next continue - } - else if rep == re.prog[tmp_pc].rep_max { - //println("ist_quant_pg MAX RANGE") + } else if rep == re.prog[tmp_pc].rep_max { + // println("ist_quant_pg MAX RANGE") re.prog[tmp_pc].group_rep = 0 // clear the repetitions state.group_index-- m_state = .ist_next continue - } - else if rep >= re.prog[tmp_pc].rep_min { - //println("ist_quant_pg IN RANGE group_index:$state.group_index") + } else if rep >= re.prog[tmp_pc].rep_min { + // println("ist_quant_pg IN RANGE group_index:$state.group_index") // check greedy flag, if true exit on minimum if re.prog[tmp_pc].greedy == true { @@ -2253,32 +2183,31 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { continue } - /* UNREACHABLE */ - //println("PANIC3!! state: $m_state") - return err_internal_error, state.i + // UNREACHABLE + // println("PANIC3!! state: $m_state") + return regex.err_internal_error, state.i } - // ist_quant_n => quantifier negative test on token else if m_state == .ist_quant_n { rep := re.prog[state.pc].rep - //println("Here!! PC $state.pc is_next_or: ${re.prog[state.pc].next_is_or}") + // println("Here!! PC $state.pc is_next_or: ${re.prog[state.pc].next_is_or}") // zero quantifier * or ? if rep == 0 && re.prog[state.pc].rep_min == 0 { - //println("ist_quant_n c_zero RANGE MIN") + // println("ist_quant_n c_zero RANGE MIN") m_state = .ist_next // go to next ist continue } // match + or * else if rep >= re.prog[state.pc].rep_min { - //println("ist_quant_n MATCH RANGE") + // println("ist_quant_n MATCH RANGE") m_state = .ist_next continue } // check the OR if present if re.prog[state.pc].next_is_or { - //println("OR present on failing") + // println("OR present on failing") state.match_index = -1 m_state = .ist_next continue @@ -2286,38 +2215,36 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { // we are in a group manage no match from here if state.group_index >= 0 { - //println("ist_quant_n FAILED insied a GROUP group_index:$state.group_index") + // println("ist_quant_n FAILED insied a GROUP group_index:$state.group_index") m_state = .ist_quant_ng continue } // no other options - //println("ist_quant_n no_match_found") - result = no_match_found + // println("ist_quant_n no_match_found") + result = regex.no_match_found m_state = .stop continue - //return no_match_found, 0 + // return no_match_found, 0 } - // ist_quant_p => quantifier positive test on token else if m_state == .ist_quant_p { // exit on first match - if (re.flag & f_efm) != 0 { - return state.i, state.i+1 + if (re.flag & regex.f_efm) != 0 { + return state.i, state.i + 1 } rep := re.prog[state.pc].rep // under range if rep > 0 && rep < re.prog[state.pc].rep_min { - //println("ist_quant_p UNDER RANGE") + // println("ist_quant_p UNDER RANGE") m_state = .ist_load // continue the loop continue } - // range ok, continue loop else if rep >= re.prog[state.pc].rep_min && rep < re.prog[state.pc].rep_max { - //println("ist_quant_p IN RANGE") + // println("ist_quant_p IN RANGE") // check greedy flag, if true exit on minimum if re.prog[state.pc].greedy == true { @@ -2327,65 +2254,61 @@ pub fn (mut re RE) match_base(in_txt &byte, in_txt_len int ) (int,int) { m_state = .ist_load continue } - // max reached else if rep == re.prog[state.pc].rep_max { - //println("ist_quant_p MAX RANGE") + // println("ist_quant_p MAX RANGE") m_state = .ist_next continue } - } - /* UNREACHABLE */ - //println("PANIC4!! state: $m_state") - return err_internal_error, state.i + // UNREACHABLE + // println("PANIC4!! state: $m_state") + return regex.err_internal_error, state.i } - //println("Check end of text!") + // println("Check end of text!") // Check the results if state.match_index >= 0 { if state.group_index < 0 { + if re.prog[state.pc].ist == regex.ist_prog_end { + // println("program ended!!") - if re.prog[state.pc].ist == ist_prog_end { - //println("program ended!!") - - if (re.flag & f_src) != 0 { - //println("find return") + if (re.flag & regex.f_src) != 0 { + // println("find return") return state.first_match, state.i } else { - //println("Here!!") + // println("Here!!") return 0, state.i } } - //println("No Group here, natural end [$state.first_match,$state.i] state: ${state_str(m_state)} ist: $ist pgr_end: $re.prog.len") + // println("No Group here, natural end [$state.first_match,$state.i] state: ${state_str(m_state)} ist: $ist pgr_end: $re.prog.len") - if re.prog[state.pc+1].ist == ist_prog_end || re.prog[state.pc].ist == ist_prog_end{ + if re.prog[state.pc + 1].ist == regex.ist_prog_end + || re.prog[state.pc].ist == regex.ist_prog_end { rep := re.prog[state.pc].rep - //println("rep: $rep re.prog[state.pc].rep_min: ${re.prog[state.pc].rep_min} re.prog[state.pc].rep_max: ${re.prog[state.pc].rep_max}") + // println("rep: $rep re.prog[state.pc].rep_min: ${re.prog[state.pc].rep_min} re.prog[state.pc].rep_max: ${re.prog[state.pc].rep_max}") if rep >= re.prog[state.pc].rep_min && rep <= re.prog[state.pc].rep_max { return state.first_match, state.i } - //println("Program not finished! ") - return no_match_found, 0 + // println("Program not finished! ") + return regex.no_match_found, 0 } if src_end { - //println("program end") + // println("program end") return state.first_match, state.i } - //print("No match found!!") - return no_match_found, 0 - - + // print("No match found!!") + return regex.no_match_found, 0 } else { - //println("Group match! OK") - //println("first_match: $state.first_match, i: $state.i") + // println("Group match! OK") + // println("first_match: $state.first_match, i: $state.i") - //println("Skip last group") - return state.first_match,state.i - //return state.first_match,re.group_stack[state.group_index--] + // println("Skip last group") + return state.first_match, state.i + // return state.first_match,re.group_stack[state.group_index--] } } - //println("no_match_found, natural end") - return no_match_found, 0 + // println("no_match_found, natural end") + return regex.no_match_found, 0 } diff --git a/vlib/regex/regex_opt.v b/vlib/regex/regex_opt.v index cf1a6ffff..2aaa88f7f 100644 --- a/vlib/regex/regex_opt.v +++ b/vlib/regex/regex_opt.v @@ -1,17 +1,18 @@ module regex + import strings // compile_opt compile RE pattern string pub fn (mut re RE) compile_opt(pattern string) ? { - re_err,err_pos := re.impl_compile(pattern) - + re_err, err_pos := re.impl_compile(pattern) + if re_err != compile_ok { mut err_msg := strings.new_builder(300) - err_msg.write_string("\nquery: $pattern\n") - line := "-".repeat(err_pos) - err_msg.write_string("err : ${line}^\n") + err_msg.write_string('\nquery: $pattern\n') + line := '-'.repeat(err_pos) + err_msg.write_string('err : $line^\n') err_str := re.get_parse_error_string(re_err) - err_msg.write_string("ERROR: $err_str\n") + err_msg.write_string('ERROR: $err_str\n') return error_with_code(err_msg.str(), re_err) } } @@ -19,15 +20,15 @@ pub fn (mut re RE) compile_opt(pattern string) ? { // new_regex create a RE of small size, usually sufficient for ordinary use pub fn new() RE { // init regex - mut re := regex.RE{} - re.prog = []Token {len: max_code_len + 1} // max program length, can not be longer then the pattern - re.cc = []CharClass{len: max_code_len} // can not be more char class the the length of the pattern - re.group_csave_flag = false // enable continuos group saving - re.group_max_nested = 128 // set max 128 group nested - re.group_max = max_code_len >> 1 // we can't have more groups than the half of the pattern legth + mut re := RE{} + re.prog = []Token{len: max_code_len + 1} // max program length, can not be longer then the pattern + re.cc = []CharClass{len: max_code_len} // can not be more char class the the length of the pattern + re.group_csave_flag = false // enable continuos group saving + re.group_max_nested = 128 // set max 128 group nested + re.group_max = max_code_len >> 1 // we can't have more groups than the half of the pattern legth - re.group_stack = []int{len: re.group_max, init: -1} - re.group_data = []int{len: re.group_max, init: -1} + re.group_stack = []int{len: re.group_max, init: -1} + re.group_data = []int{len: re.group_max, init: -1} return re } @@ -35,18 +36,18 @@ pub fn new() RE { // regex_opt create new RE object from RE pattern string pub fn regex_opt(pattern string) ?RE { // init regex - mut re := regex.RE{} - re.prog = []Token {len: pattern.len + 1} // max program length, can not be longer then the pattern - re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern - re.group_csave_flag = false // enable continuos group saving - re.group_max_nested = 128 // set max 128 group nested - re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth + mut re := RE{} + re.prog = []Token{len: pattern.len + 1} // max program length, can not be longer then the pattern + re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern + re.group_csave_flag = false // enable continuos group saving + re.group_max_nested = 128 // set max 128 group nested + re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth - re.group_stack = []int{len: re.group_max, init: -1} - re.group_data = []int{len: re.group_max, init: -1} + re.group_stack = []int{len: re.group_max, init: -1} + re.group_data = []int{len: re.group_max, init: -1} - // compile the pattern - re.compile_opt(pattern)? + // compile the pattern + re.compile_opt(pattern) ? - return re + return re } diff --git a/vlib/regex/regex_util.v b/vlib/regex/regex_util.v index 44b97cd1b..6f29dfcdd 100644 --- a/vlib/regex/regex_util.v +++ b/vlib/regex/regex_util.v @@ -1,13 +1,12 @@ /* - regex 1.0 alpha Copyright (c) 2019-2021 Dario Deledda. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. - */ module regex + import strings /****************************************************************************** @@ -16,19 +15,19 @@ import strings * ******************************************************************************/ // regex create a regex object from the query string, retunr RE object and errors as re_err, err_pos -pub fn regex_base(pattern string) (RE,int,int){ +pub fn regex_base(pattern string) (RE, int, int) { // init regex - mut re := regex.RE{} - re.prog = []Token {len: pattern.len + 1} // max program length, can not be longer then the pattern - re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern - re.group_csave_flag = false // enable continuos group saving - re.group_max_nested = 128 // set max 128 group nested - re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth + mut re := RE{} + re.prog = []Token{len: pattern.len + 1} // max program length, can not be longer then the pattern + re.cc = []CharClass{len: pattern.len} // can not be more char class the the length of the pattern + re.group_csave_flag = false // enable continuos group saving + re.group_max_nested = 128 // set max 128 group nested + re.group_max = pattern.len >> 1 // we can't have more groups than the half of the pattern legth - re.group_stack = []int{len: re.group_max, init: -1} - re.group_data = []int{len: re.group_max, init: -1} + re.group_stack = []int{len: re.group_max, init: -1} + re.group_data = []int{len: re.group_max, init: -1} - re_err,err_pos := re.impl_compile(pattern) + re_err, err_pos := re.impl_compile(pattern) return re, re_err, err_pos } @@ -40,10 +39,10 @@ pub fn regex_base(pattern string) (RE,int,int){ // get_group_bounds_by_name get a group boundaries by its name pub fn (re RE) get_group_bounds_by_name(group_name string) (int, int) { if group_name in re.group_map { - tmp_index := re.group_map[group_name]-1 - start := re.groups[tmp_index * 2] - end := re.groups[tmp_index * 2 + 1] - return start,end + tmp_index := re.group_map[group_name] - 1 + start := re.groups[tmp_index * 2] + end := re.groups[tmp_index * 2 + 1] + return start, end } return -1, -1 } @@ -51,14 +50,14 @@ pub fn (re RE) get_group_bounds_by_name(group_name string) (int, int) { // get_group_by_name get a group boundaries by its name pub fn (re RE) get_group_by_name(in_txt string, group_name string) string { if group_name in re.group_map { - tmp_index := re.group_map[group_name]-1 - start := re.groups[tmp_index * 2] - end := re.groups[tmp_index * 2 + 1] + tmp_index := re.group_map[group_name] - 1 + start := re.groups[tmp_index * 2] + end := re.groups[tmp_index * 2 + 1] if start >= 0 && end > start { return in_txt[start..end] } } - return "" + return '' } // get_group_by_id get a group string by its id @@ -66,16 +65,16 @@ pub fn (re RE) get_group_by_id(in_txt string, group_id int) string { if group_id < (re.groups.len >> 1) { index := group_id << 1 start := re.groups[index] - end := re.groups[index + 1] + end := re.groups[index + 1] if start >= 0 && end > start { return in_txt[start..end] } } - return "" + return '' } // get_group_by_id get a group boundaries by its id -pub fn (re RE) get_group_bounds_by_id(group_id int) (int,int) { +pub fn (re RE) get_group_bounds_by_id(group_id int) (int, int) { if group_id < re.group_count { index := group_id << 1 return re.groups[index], re.groups[index + 1] @@ -83,8 +82,7 @@ pub fn (re RE) get_group_bounds_by_id(group_id int) (int,int) { return -1, -1 } -pub -struct Re_group { +pub struct Re_group { pub: start int = -1 end int = -1 @@ -94,17 +92,20 @@ pub: pub fn (re RE) get_group_list() []Re_group { mut res := []Re_group{len: re.groups.len >> 1} mut gi := 0 - //println("len: ${re.groups.len} groups: ${re.groups}") + // println("len: ${re.groups.len} groups: ${re.groups}") for gi < re.groups.len { if re.groups[gi] >= 0 { txt_st := re.groups[gi] - txt_en := re.groups[gi+1] - - //println("#${gi/2} start: ${re.groups[gi]} end: ${re.groups[gi + 1]} ") - if txt_st >= 0 && txt_en > txt_st { - tmp := Re_group{ start: re.groups[gi], end: re.groups[gi + 1]} - //println(tmp) + txt_en := re.groups[gi + 1] + + // println("#${gi/2} start: ${re.groups[gi]} end: ${re.groups[gi + 1]} ") + if txt_st >= 0 && txt_en > txt_st { + tmp := Re_group{ + start: re.groups[gi] + end: re.groups[gi + 1] + } + // println(tmp) res[gi >> 1] = tmp } else { res[gi >> 1] = Re_group{} @@ -122,8 +123,7 @@ pub fn (re RE) get_group_list() []Re_group { ******************************************************************************/ // match_string Match the pattern with the in_txt string [direct_array_access] -pub fn (mut re RE) match_string(in_txt string) (int,int) { - +pub fn (mut re RE) match_string(in_txt string) (int, int) { start, mut end := re.match_base(in_txt.str, in_txt.len + 1) if end > in_txt.len { end = in_txt.len @@ -144,7 +144,6 @@ pub fn (mut re RE) match_string(in_txt string) (int,int) { return start, end } - /****************************************************************************** * * Finders @@ -173,9 +172,9 @@ fn (mut re RE) find_imp(in_txt string) (int,int) { // find try to find the first match in the input string [direct_array_access] -pub fn (mut re RE) find(in_txt string) (int,int) { - //old_flag := re.flag - //re.flag |= f_src // enable search mode +pub fn (mut re RE) find(in_txt string) (int, int) { + // old_flag := re.flag + // re.flag |= f_src // enable search mode mut i := 0 for i < in_txt.len { @@ -183,30 +182,29 @@ pub fn (mut re RE) find(in_txt string) (int,int) { mut s := -1 mut e := -1 unsafe { - tmp_str := tos(in_txt.str+i, in_txt.len-i) - s,e = re.match_string(tmp_str) + tmp_str := tos(in_txt.str + i, in_txt.len - i) + s, e = re.match_string(tmp_str) } //------------------------ - //s,e := re.find_imp(in_txt[i..]) + // s,e := re.find_imp(in_txt[i..]) //------------------------ if s >= 0 && e > s { - //println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]") - //re.flag = old_flag - return i+s, i+e + // println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]") + // re.flag = old_flag + return i + s, i + e } else { i++ } - } - //re.flag = old_flag + // re.flag = old_flag return -1, -1 } // find try to find the first match in the input string strarting from start index [direct_array_access] -pub fn (mut re RE) find_from(in_txt string, start int) (int,int) { +pub fn (mut re RE) find_from(in_txt string, start int) (int, int) { old_flag := re.flag - re.flag |= f_src // enable search mode + re.flag |= f_src // enable search mode mut i := start if i < 0 { @@ -214,25 +212,24 @@ pub fn (mut re RE) find_from(in_txt string, start int) (int,int) { } for i < in_txt.len { //--- speed references --- - + mut s := -1 mut e := -1 - + unsafe { - tmp_str := tos(in_txt.str+i, in_txt.len-i) - s,e = re.match_string(tmp_str) + tmp_str := tos(in_txt.str + i, in_txt.len - i) + s, e = re.match_string(tmp_str) } //------------------------ - //s,e = re.find_imp(in_txt[i..]) + // s,e = re.find_imp(in_txt[i..]) //------------------------ if s >= 0 && e > s { - //println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]") + // println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}]") re.flag = old_flag - return i+s, i+e + return i + s, i + e } else { i++ } - } re.flag = old_flag return -1, -1 @@ -241,8 +238,8 @@ pub fn (mut re RE) find_from(in_txt string, start int) (int,int) { // find_all find all the non overlapping occurrences of the match pattern [direct_array_access] pub fn (mut re RE) find_all(in_txt string) []int { - //old_flag := re.flag - //re.flag |= f_src // enable search mode + // old_flag := re.flag + // re.flag |= f_src // enable search mode mut i := 0 mut res := []int{} @@ -253,25 +250,24 @@ pub fn (mut re RE) find_all(in_txt string) []int { mut s := -1 mut e := -1 unsafe { - tmp_str := tos(in_txt.str+i, in_txt.len-i) - s,e = re.match_string(tmp_str) + tmp_str := tos(in_txt.str + i, in_txt.len - i) + s, e = re.match_string(tmp_str) } //------------------------ - //s,e := re.find_imp(in_txt[i..]) + // s,e := re.find_imp(in_txt[i..]) //------------------------ - if s >= 0 && e > s && i+s > ls { - //println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}] ls:$ls") - res << i+s - res << i+e - ls = i+s - i = i+e + if s >= 0 && e > s && i + s > ls { + // println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}] ls:$ls") + res << i + s + res << i + e + ls = i + s + i = i + e continue } else { i++ } - } - //re.flag = old_flag + // re.flag = old_flag return res } @@ -287,25 +283,25 @@ pub fn (mut re RE) find_all_str(in_txt string) []string { mut s := -1 mut e := -1 unsafe { - tmp_str := tos(in_txt.str+i, in_txt.len-i) - s,e = re.find(tmp_str) + tmp_str := tos(in_txt.str + i, in_txt.len - i) + s, e = re.find(tmp_str) } //------------------------ - //s,e := re.find(in_txt[i..]) + // s,e := re.find(in_txt[i..]) //------------------------ - if s >= 0 && e > s && i+s > ls { - //println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}] ls:$ls") - res << in_txt[i+s..i+e] - ls = i+s - i = i+e + if s >= 0 && e > s && i + s > ls { + // println("find match in: ${i+s},${i+e} [${in_txt[i+s..i+e]}] ls:$ls") + res << in_txt[i + s..i + e] + ls = i + s + i = i + e continue } else { i++ } - } return res } + /****************************************************************************** * * Replacers @@ -316,7 +312,7 @@ pub fn (mut re RE) replace_simple(in_txt string, repl string) string { pos := re.find_all(in_txt) if pos.len > 0 { - mut res := "" + mut res := '' mut i := 0 mut s1 := 0 @@ -325,7 +321,7 @@ pub fn (mut re RE) replace_simple(in_txt string, repl string) string { for i < pos.len { e1 = pos[i] res += in_txt[s1..e1] + repl - s1 = pos[i+1] + s1 = pos[i + 1] i += 2 } @@ -335,7 +331,6 @@ pub fn (mut re RE) replace_simple(in_txt string, repl string) string { return in_txt } - // type of function used for custom replace // in_txt source text // start index of the start of the match in in_txt @@ -345,38 +340,38 @@ pub type FnReplace = fn (re RE, in_txt string, start int, end int) string // replace_by_fn return a string where the matches are replaced with the string from the repl_fn callback function pub fn (mut re RE) replace_by_fn(in_txt string, repl_fn FnReplace) string { - mut i := 0 + mut i := 0 mut res := strings.new_builder(in_txt.len) - mut last_end := 0 + mut last_end := 0 for i < in_txt.len { - //println("Find Start. $i [${in_txt[i..]}]") - s, e := re.find_from(in_txt,i) - //println("Find End.") - if s >= 0 && e > s { - //println("find match in: ${s},${e} [${in_txt[s..e]}]") - + // println("Find Start. $i [${in_txt[i..]}]") + s, e := re.find_from(in_txt, i) + // println("Find End.") + if s >= 0 && e > s { + // println("find match in: ${s},${e} [${in_txt[s..e]}]") + if last_end < s { res.write_string(in_txt[last_end..s]) } - for g_i in 0..re.group_count { - re.groups[g_i << 1 ] += i + for g_i in 0 .. re.group_count { + re.groups[g_i << 1] += i re.groups[(g_i << 1) + 1] += i } - + repl := repl_fn(re, in_txt, s, e) - //println("repl res: $repl") + // println("repl res: $repl") res.write_string(repl) - //res.write_string("[[${in_txt[s..e]}]]") - + // res.write_string("[[${in_txt[s..e]}]]") + last_end = e i = e } else { break - //i++ + // i++ } - //println(i) + // println(i) } if last_end >= 0 && last_end < in_txt.len { res.write_string(in_txt[last_end..]) @@ -384,66 +379,65 @@ pub fn (mut re RE) replace_by_fn(in_txt string, repl_fn FnReplace) string { return res.str() } - fn (re RE) parsed_replace_string(in_txt string, repl string) string { - str_lst := repl.split("\\") + str_lst := repl.split('\\') mut res := str_lst[0] mut i := 1 for i < str_lst.len { tmp := str_lst[i] - //println("tmp: ${tmp}") + // println("tmp: ${tmp}") if tmp.len > 0 && tmp[0] >= `0` && tmp[0] <= `9` { group_id := int(tmp[0] - `0`) group := re.get_group_by_id(in_txt, group_id) - //println("group: $group_id [$group]") - res += "${group}${tmp[1..]}" + // println("group: $group_id [$group]") + res += '$group${tmp[1..]}' } else { - res += '\\'+tmp + res += '\\' + tmp } i++ } return res } -// replace return a string where the matches are replaced with the repl_str string, +// replace return a string where the matches are replaced with the repl_str string, // this function support use groups in the replace string pub fn (mut re RE) replace(in_txt string, repl_str string) string { - mut i := 0 + mut i := 0 mut res := strings.new_builder(in_txt.len) - mut last_end := 0 + mut last_end := 0 for i < in_txt.len { - //println("Find Start. $i [${in_txt[i..]}]") - s, e := re.find_from(in_txt,i) - //println("Find End.") - if s >= 0 && e > s { - //println("find match in: ${s},${e} [${in_txt[s..e]}]") - + // println("Find Start. $i [${in_txt[i..]}]") + s, e := re.find_from(in_txt, i) + // println("Find End.") + if s >= 0 && e > s { + // println("find match in: ${s},${e} [${in_txt[s..e]}]") + if last_end < s { res.write_string(in_txt[last_end..s]) } - for g_i in 0..re.group_count { - re.groups[g_i << 1 ] += i + for g_i in 0 .. re.group_count { + re.groups[g_i << 1] += i re.groups[(g_i << 1) + 1] += i } - - //repl := repl_fn(re, in_txt, s, e) + + // repl := repl_fn(re, in_txt, s, e) repl := re.parsed_replace_string(in_txt, repl_str) - //println("repl res: $repl") + // println("repl res: $repl") res.write_string(repl) - //res.write_string("[[${in_txt[s..e]}]]") - + // res.write_string("[[${in_txt[s..e]}]]") + last_end = e i = e } else { break - //i++ + // i++ } - //println(i) + // println(i) } if last_end >= 0 && last_end < in_txt.len { res.write_string(in_txt[last_end..]) } return res.str() -} \ No newline at end of file +} diff --git a/vlib/sokol/audio/audio.v b/vlib/sokol/audio/audio.v index f3f11e61b..50acb7f5d 100644 --- a/vlib/sokol/audio/audio.v +++ b/vlib/sokol/audio/audio.v @@ -1,11 +1,17 @@ module audio +$if linux { + // provide a nicer error for the user that does not have ALSA installed + #include # Please install the `libasound2-dev` package +} + #flag -I @VEXEROOT/thirdparty/sokol #define SOKOL_IMPL #include "sokol_audio.h" #flag linux -lasound #flag darwin -framework AudioToolbox #flag windows -lole32 + // pub type FNStreamingCB = fn (buffer &f32, num_frames int, num_channels int) diff --git a/vlib/sokol/f/f.v b/vlib/sokol/f/f.v index 615c4b303..5c5e714ec 100644 --- a/vlib/sokol/f/f.v +++ b/vlib/sokol/f/f.v @@ -4,7 +4,7 @@ import fontstash import sokol.c pub const ( - used_import = fontstash.used_import + c.used_import + used_import = fontstash.used_import + c.used_import ) #flag linux -I. diff --git a/vlib/sokol/gfx/enums.v b/vlib/sokol/gfx/enums.v index 4d0339e68..fbe336ed4 100644 --- a/vlib/sokol/gfx/enums.v +++ b/vlib/sokol/gfx/enums.v @@ -1,128 +1,120 @@ module gfx pub enum Backend { - glcore33 - gles2 - gles3 - d3d11 - metal_ios - metal_macos - metal_simulator - dummy + glcore33 + gles2 + gles3 + d3d11 + metal_ios + metal_macos + metal_simulator + dummy } pub enum PixelFormat { - _default /* value 0 reserved for default-init */ - @none - - r8 - r8sn - r8ui - r8si - - r16 - r16sn - r16ui - r16si - r16f - rg8 - rg8sn - rg8ui - rg8si - - r32ui - r32si - r32f - rg16 - rg16sn - rg16ui - rg16si - rg16f - rgba8 - rgba8sn - rgba8ui - rgba8si - bgra8 - rgb10a2 - rg11b10f - - rg32ui - rg32si - rg32f - rgba16 - rgba16sn - rgba16ui - rgba16si - rgba16f - - rgba32ui - rgba32si - rgba32f - - depth - depth_stencil - - bc1_rgba - bc2_rgba - bc3_rgba - bc4_r - bc4_rsn - bc5_rg - bc5_rgsn - bc6h_rgbf - bc6h_rgbuf - bc7_rgba - pvrtc_rgb_2bpp - pvrtc_rgb_4bpp - pvrtc_rgba_2bpp - pvrtc_rgba_4bpp - etc2_rgb8 - etc2_rgb8a1 - etc2_rgba8 - etc2_rg11 - etc2_rg11sn - - _num + _default // value 0 reserved for default-init + @none + r8 + r8sn + r8ui + r8si + r16 + r16sn + r16ui + r16si + r16f + rg8 + rg8sn + rg8ui + rg8si + r32ui + r32si + r32f + rg16 + rg16sn + rg16ui + rg16si + rg16f + rgba8 + rgba8sn + rgba8ui + rgba8si + bgra8 + rgb10a2 + rg11b10f + rg32ui + rg32si + rg32f + rgba16 + rgba16sn + rgba16ui + rgba16si + rgba16f + rgba32ui + rgba32si + rgba32f + depth + depth_stencil + bc1_rgba + bc2_rgba + bc3_rgba + bc4_r + bc4_rsn + bc5_rg + bc5_rgsn + bc6h_rgbf + bc6h_rgbuf + bc7_rgba + pvrtc_rgb_2bpp + pvrtc_rgb_4bpp + pvrtc_rgba_2bpp + pvrtc_rgba_4bpp + etc2_rgb8 + etc2_rgb8a1 + etc2_rgba8 + etc2_rg11 + etc2_rg11sn + _num } pub enum ResourceState { - initial - alloc - valid - failed - invalid + initial + alloc + valid + failed + invalid } pub enum Usage { - _default /* value 0 reserved for default-init */ - immutable - dynamic - stream - _num + _default // value 0 reserved for default-init + immutable + dynamic + stream + _num } pub enum BufferType { - _default /* value 0 reserved for default-init */ - vertexbuffer - indexbuffer - _num + _default // value 0 reserved for default-init + vertexbuffer + indexbuffer + _num } pub enum IndexType { - _default /* value 0 reserved for default-init */ - @none - uint16 - uint32 - _num + _default // value 0 reserved for default-init + @none + uint16 + uint32 + _num } pub enum ImageType { - _default /* value 0 reserved for default-init */ - _2d - cube - _3d - array - _num + _default // value 0 reserved for default-init + _2d + cube + _3d + array + _num } pub enum CubeFace { @@ -142,164 +134,164 @@ pub enum ShaderStage { } pub enum PrimitiveType { - _default /* value 0 reserved for default-init */ - points - lines - line_strip - triangles - triangle_strip - _num + _default // value 0 reserved for default-init + points + lines + line_strip + triangles + triangle_strip + _num } pub enum Filter { - _default /* value 0 reserved for default-init */ - nearest - linear - nearest_mipmap_nearest - nearest_mipmap_linear - linear_mipmap_nearest - linear_mipmap_linear - _num + _default // value 0 reserved for default-init + nearest + linear + nearest_mipmap_nearest + nearest_mipmap_linear + linear_mipmap_nearest + linear_mipmap_linear + _num } pub enum Wrap { - _default /* value 0 reserved for default-init */ - repeat - clamp_to_edge - clamp_to_border - mirrored_repeat - _num + _default // value 0 reserved for default-init + repeat + clamp_to_edge + clamp_to_border + mirrored_repeat + _num } pub enum BorderColor { - _default /* value 0 reserved for default-init */ - transparent_black - opaque_black - opaque_white - _num + _default // value 0 reserved for default-init + transparent_black + opaque_black + opaque_white + _num } pub enum VertexFormat { - invalid - float - float2 - float3 - float4 - byte4 - byte4n - ubyte4 - ubyte4n - short2 - short2n - ushort2n - short4 - short4n - ushort4n - uint10_n2 - _num + invalid + float + float2 + float3 + float4 + byte4 + byte4n + ubyte4 + ubyte4n + short2 + short2n + ushort2n + short4 + short4n + ushort4n + uint10_n2 + _num } pub enum VertexStep { - _default /* value 0 reserved for default-init */ - per_vertex - per_instance - _num + _default // value 0 reserved for default-init + per_vertex + per_instance + _num } pub enum UniformType { - invalid - float - float2 - float3 - float4 - mat4 - _num + invalid + float + float2 + float3 + float4 + mat4 + _num } pub enum CullMode { - _default /* value 0 reserved for default-init */ - @none - front - back - _num + _default // value 0 reserved for default-init + @none + front + back + _num } pub enum FaceWinding { - _facewinding_default /* value 0 reserved for default-init */ - facewinding_ccw - facewinding_cw - _facewinding_num + _facewinding_default // value 0 reserved for default-init + facewinding_ccw + facewinding_cw + _facewinding_num } pub enum CompareFunc { - _default /* value 0 reserved for default-init */ - never - less - equal - less_equal - greater - not_equal - greater_equal - always - _num + _default // value 0 reserved for default-init + never + less + equal + less_equal + greater + not_equal + greater_equal + always + _num } pub enum StencilOp { - _default /* value 0 reserved for default-init */ - keep - zero - replace - incr_clamp - decr_clamp - invert - incr_wrap - decr_wrap - _num + _default // value 0 reserved for default-init + keep + zero + replace + incr_clamp + decr_clamp + invert + incr_wrap + decr_wrap + _num } pub enum BlendFactor { - _default /* value 0 reserved for default-init */ - zero - one - src_color - one_minus_src_color - src_alpha - one_minus_src_alpha - dst_color - one_minus_dst_color - dst_alpha - one_minus_dst_alpha - src_alpha_saturated - blend_color - one_minus_blend_color - blend_alpha - one_minus_blend_alpha - _num + _default // value 0 reserved for default-init + zero + one + src_color + one_minus_src_color + src_alpha + one_minus_src_alpha + dst_color + one_minus_dst_color + dst_alpha + one_minus_dst_alpha + src_alpha_saturated + blend_color + one_minus_blend_color + blend_alpha + one_minus_blend_alpha + _num } pub enum BlendOp { - _default /* value 0 reserved for default-init */ - add - subtract - reverse_subtract - _num + _default // value 0 reserved for default-init + add + subtract + reverse_subtract + _num } pub enum ColorMask { - _default = 0 /* value 0 reserved for default-init */ - @none = 0x10 /* special value for 'all channels disabled */ - r = 1 - g = 2 - b = 4 - a = 8 - rgb = 0x7 - rgba = 0xF + _default = 0 // value 0 reserved for default-init + @none = 0x10 // special value for 'all channels disabled + r = 1 + g = 2 + b = 4 + a = 8 + rgb = 0x7 + rgba = 0xF } pub enum Action { - _default - clear - load - dontcare - _num + _default + clear + load + dontcare + _num } diff --git a/vlib/sokol/gfx/gfx.v b/vlib/sokol/gfx/gfx.v index 8997da499..948017376 100644 --- a/vlib/sokol/gfx/gfx.v +++ b/vlib/sokol/gfx/gfx.v @@ -3,7 +3,7 @@ module gfx import sokol.c pub const ( - version = 1 + version = 1 used_import = c.used_import ) @@ -171,7 +171,7 @@ pub fn query_pixelformat(fmt PixelFormat) C.sg_pixelformat_info { return C.sg_query_pixelformat(fmt) } -/* get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID) */ +// get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID) [inline] pub fn query_buffer_state(buf C.sg_buffer) C.sg_resource_state { return C.sg_query_buffer_state(buf) @@ -249,7 +249,7 @@ pub fn query_pass_defaults(desc &C.sg_pass) C.sg_pass_desc { return C.sg_query_pass_defaults(unsafe { &C.sg_pass_desc(desc) }) } -/* rendering contexts (optional) */ +// rendering contexts (optional) [inline] pub fn setup_context() C.sg_context { return C.sg_setup_context() diff --git a/vlib/sokol/gfx/gfx_funcs.v b/vlib/sokol/gfx/gfx_funcs.v index c0133c07a..eb4e99b39 100644 --- a/vlib/sokol/gfx/gfx_funcs.v +++ b/vlib/sokol/gfx/gfx_funcs.v @@ -30,6 +30,7 @@ fn C.sg_apply_scissor_rect(x int, y int, width int, height int, origin_top_left fn C.sg_apply_scissor_rectf(x f32, y f32, width f32, height f32, origin_top_left bool) fn C.sg_apply_pipeline(pip C.sg_pipeline) fn C.sg_apply_bindings(bindings &C.sg_bindings) + // stage == sg_shader_stage fn C.sg_apply_uniforms(stage int, ub_index int, data &C.sg_range) fn C.sg_draw(base_element int, num_elements int, num_instances int) @@ -43,7 +44,7 @@ fn C.sg_query_features() C.sg_features fn C.sg_query_limits() C.sg_limits fn C.sg_query_pixelformat(fmt PixelFormat) C.sg_pixelformat_info -/* get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID) */ +// get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID) fn C.sg_query_buffer_state(buf C.sg_buffer) C.sg_resource_state fn C.sg_query_image_state(img C.sg_image) C.sg_resource_state fn C.sg_query_shader_state(shd C.sg_shader) C.sg_resource_state @@ -64,7 +65,7 @@ fn C.sg_query_shader_defaults(desc &C.sg_shader_desc) C.sg_shader_desc fn C.sg_query_pipeline_defaults(desc &C.sg_pipeline_desc) C.sg_pipeline_desc fn C.sg_query_pass_defaults(desc &C.sg_pass_desc) C.sg_pass_desc -/* rendering contexts (optional) */ +// rendering contexts (optional) fn C.sg_setup_context() C.sg_context fn C.sg_activate_context(ctx_id C.sg_context) fn C.sg_discard_context(ctx_id C.sg_context) diff --git a/vlib/sokol/gfx/gfx_utils.v b/vlib/sokol/gfx/gfx_utils.v index 0c872ceee..c95ff69c8 100644 --- a/vlib/sokol/gfx/gfx_utils.v +++ b/vlib/sokol/gfx/gfx_utils.v @@ -2,8 +2,8 @@ module gfx pub fn create_clear_pass(r f32, g f32, b f32, a f32) C.sg_pass_action { mut color_action := C.sg_color_attachment_action{ - action: gfx.Action(C.SG_ACTION_CLEAR) - value: C.sg_color { + action: Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ r: r g: g b: b diff --git a/vlib/sokol/sapp/enums.v b/vlib/sokol/sapp/enums.v index 3cb20b9ff..8e0efd796 100644 --- a/vlib/sokol/sapp/enums.v +++ b/vlib/sokol/sapp/enums.v @@ -1,165 +1,165 @@ module sapp pub enum EventType { - invalid - key_down - key_up - char - mouse_down - mouse_up - mouse_scroll - mouse_move - mouse_enter - mouse_leave - touches_began - touches_moved - touches_ended - touches_cancelled - resized - iconified - restored - suspended - resumed - update_cursor - quit_requested - clipboard_pasted - num + invalid + key_down + key_up + char + mouse_down + mouse_up + mouse_scroll + mouse_move + mouse_enter + mouse_leave + touches_began + touches_moved + touches_ended + touches_cancelled + resized + iconified + restored + suspended + resumed + update_cursor + quit_requested + clipboard_pasted + num } pub enum MouseButton { - invalid = -1 - left = 0 - right = 1 - middle = 2 + invalid = -1 + left = 0 + right = 1 + middle = 2 } pub enum Modifier { - shift = 1 //(1<<0) - ctrl = 2 //(1<<1) - alt = 4 //(1<<2) - super = 8 //(1<<3) + shift = 1 //(1<<0) + ctrl = 2 //(1<<1) + alt = 4 //(1<<2) + super = 8 //(1<<3) } pub enum KeyCode { - invalid = 0 - space = 32 - apostrophe = 39 /* ' */ - comma = 44 /* , */ - minus = 45 /* - */ - period = 46 /* . */ - slash = 47 /* / */ - _0 = 48 - _1 = 49 - _2 = 50 - _3 = 51 - _4 = 52 - _5 = 53 - _6 = 54 - _7 = 55 - _8 = 56 - _9 = 57 - semicolon = 59 /* ; */ - equal = 61 /* = */ - a = 65 - b = 66 - c = 67 - d = 68 - e = 69 - f = 70 - g = 71 - h = 72 - i = 73 - j = 74 - k = 75 - l = 76 - m = 77 - n = 78 - o = 79 - p = 80 - q = 81 - r = 82 - s = 83 - t = 84 - u = 85 - v = 86 - w = 87 - x = 88 - y = 89 - z = 90 - left_bracket = 91 /* [ */ - backslash = 92 /* \ */ - right_bracket = 93 /* ] */ - grave_accent = 96 /* ` */ - world_1 = 161 /* non-us #1 */ - world_2 = 162 /* non-us #2 */ - escape = 256 - enter = 257 - tab = 258 - backspace = 259 - insert = 260 - delete = 261 - right = 262 - left = 263 - down = 264 - up = 265 - page_up = 266 - page_down = 267 - home = 268 - end = 269 - caps_lock = 280 - scroll_lock = 281 - num_lock = 282 - print_screen = 283 - pause = 284 - f1 = 290 - f2 = 291 - f3 = 292 - f4 = 293 - f5 = 294 - f6 = 295 - f7 = 296 - f8 = 297 - f9 = 298 - f10 = 299 - f11 = 300 - f12 = 301 - f13 = 302 - f14 = 303 - f15 = 304 - f16 = 305 - f17 = 306 - f18 = 307 - f19 = 308 - f20 = 309 - f21 = 310 - f22 = 311 - f23 = 312 - f24 = 313 - f25 = 314 - kp_0 = 320 - kp_1 = 321 - kp_2 = 322 - kp_3 = 323 - kp_4 = 324 - kp_5 = 325 - kp_6 = 326 - kp_7 = 327 - kp_8 = 328 - kp_9 = 329 - kp_decimal = 330 - kp_divide = 331 - kp_multiply = 332 - kp_subtract = 333 - kp_add = 334 - kp_enter = 335 - kp_equal = 336 - left_shift = 340 - left_control = 341 - left_alt = 342 - left_super = 343 - right_shift = 344 - right_control = 345 - right_alt = 346 - right_super = 347 - menu = 348 + invalid = 0 + space = 32 + apostrophe = 39 //' + comma = 44 //, + minus = 45 //- + period = 46 //. + slash = 47 /// + _0 = 48 + _1 = 49 + _2 = 50 + _3 = 51 + _4 = 52 + _5 = 53 + _6 = 54 + _7 = 55 + _8 = 56 + _9 = 57 + semicolon = 59 //; + equal = 61 //= + a = 65 + b = 66 + c = 67 + d = 68 + e = 69 + f = 70 + g = 71 + h = 72 + i = 73 + j = 74 + k = 75 + l = 76 + m = 77 + n = 78 + o = 79 + p = 80 + q = 81 + r = 82 + s = 83 + t = 84 + u = 85 + v = 86 + w = 87 + x = 88 + y = 89 + z = 90 + left_bracket = 91 //[ + backslash = 92 //\ + right_bracket = 93 //] + grave_accent = 96 //` + world_1 = 161 // non-us #1 + world_2 = 162 // non-us #2 + escape = 256 + enter = 257 + tab = 258 + backspace = 259 + insert = 260 + delete = 261 + right = 262 + left = 263 + down = 264 + up = 265 + page_up = 266 + page_down = 267 + home = 268 + end = 269 + caps_lock = 280 + scroll_lock = 281 + num_lock = 282 + print_screen = 283 + pause = 284 + f1 = 290 + f2 = 291 + f3 = 292 + f4 = 293 + f5 = 294 + f6 = 295 + f7 = 296 + f8 = 297 + f9 = 298 + f10 = 299 + f11 = 300 + f12 = 301 + f13 = 302 + f14 = 303 + f15 = 304 + f16 = 305 + f17 = 306 + f18 = 307 + f19 = 308 + f20 = 309 + f21 = 310 + f22 = 311 + f23 = 312 + f24 = 313 + f25 = 314 + kp_0 = 320 + kp_1 = 321 + kp_2 = 322 + kp_3 = 323 + kp_4 = 324 + kp_5 = 325 + kp_6 = 326 + kp_7 = 327 + kp_8 = 328 + kp_9 = 329 + kp_decimal = 330 + kp_divide = 331 + kp_multiply = 332 + kp_subtract = 333 + kp_add = 334 + kp_enter = 335 + kp_equal = 336 + left_shift = 340 + left_control = 341 + left_alt = 342 + left_super = 343 + right_shift = 344 + right_control = 345 + right_alt = 346 + right_super = 347 + menu = 348 } diff --git a/vlib/sokol/sapp/sapp.v b/vlib/sokol/sapp/sapp.v index 46936b969..a5264a268 100644 --- a/vlib/sokol/sapp/sapp.v +++ b/vlib/sokol/sapp/sapp.v @@ -7,7 +7,9 @@ pub const ( ) // Android needs a global reference to `g_desc` -__global ( g_desc C.sapp_desc ) +__global ( + g_desc C.sapp_desc +) pub fn create_desc() C.sg_desc { metal_desc := C.sg_metal_context_desc{ diff --git a/vlib/sokol/sapp/sapp_funcs.v b/vlib/sokol/sapp/sapp_funcs.v index f49382d66..f05c06564 100644 --- a/vlib/sokol/sapp/sapp_funcs.v +++ b/vlib/sokol/sapp/sapp_funcs.v @@ -1,73 +1,99 @@ module sapp -/* returns true after sokol-app has been initialized */ +// returns true after sokol-app has been initialized fn C.sapp_isvalid() bool -/* returns the current framebuffer width in pixels */ + +// returns the current framebuffer width in pixels fn C.sapp_width() int fn C.sapp_widthf() f32 -/* returns the current framebuffer height in pixels */ + +// returns the current framebuffer height in pixels fn C.sapp_height() int fn C.sapp_heightf() f32 -/* returns true when high_dpi was requested and actually running in a high-dpi scenario */ + +// returns true when high_dpi was requested and actually running in a high-dpi scenario fn C.sapp_high_dpi() bool -/* returns the dpi scaling factor (window pixels to framebuffer pixels) */ + +// returns the dpi scaling factor (window pixels to framebuffer pixels) fn C.sapp_dpi_scale() f32 -/* show or hide the mobile device onscreen keyboard */ + +// show or hide the mobile device onscreen keyboard fn C.sapp_show_keyboard(visible bool) -/* return true if the mobile device onscreen keyboard is currently shown */ + +// return true if the mobile device onscreen keyboard is currently shown fn C.sapp_keyboard_shown() bool -/* show or hide the mouse cursor */ + +// show or hide the mouse cursor fn C.sapp_show_mouse(visible bool) -/* show or hide the mouse cursor */ + +// show or hide the mouse cursor fn C.sapp_mouse_shown() bool -/* return the userdata pointer optionally provided in sapp_desc */ + +// return the userdata pointer optionally provided in sapp_desc fn C.sapp_userdata() voidptr -/* return a copy of the sapp_desc structure */ + +// return a copy of the sapp_desc structure fn C.sapp_query_desc() C.sapp_desc -/* initiate a "soft quit" (sends SAPP_EVENTTYPE_QUIT_REQUESTED) */ + +// initiate a "soft quit" (sends SAPP_EVENTTYPE_QUIT_REQUESTED) fn C.sapp_request_quit() -/* cancel a pending quit (when SAPP_EVENTTYPE_QUIT_REQUESTED has been received) */ + +// cancel a pending quit (when SAPP_EVENTTYPE_QUIT_REQUESTED has been received) fn C.sapp_cancel_quit() -/* intiate a "hard quit" (quit application without sending SAPP_EVENTTYPE_QUIT_REQUSTED) */ + +// intiate a "hard quit" (quit application without sending SAPP_EVENTTYPE_QUIT_REQUSTED) fn C.sapp_quit() -/* call from inside event callback to consume the current event (don't forward to platform) */ + +// call from inside event callback to consume the current event (don't forward to platform) fn C.sapp_consume_event() -/* get the current frame counter (for comparison with sapp_event.frame_count) */ + +// get the current frame counter (for comparison with sapp_event.frame_count) fn C.sapp_frame_count() u64 -/* write string into clipboard */ -fn C.sapp_set_clipboard_string(str byteptr) -/* read string from clipboard (usually during SAPP_EVENTTYPE_CLIPBOARD_PASTED) */ -fn C.sapp_get_clipboard_string() byteptr -/* special run-function for SOKOL_NO_ENTRY (in standard mode this is an empty stub) */ +// write string into clipboard +fn C.sapp_set_clipboard_string(str &byte) + +// read string from clipboard (usually during SAPP_EVENTTYPE_CLIPBOARD_PASTED) +fn C.sapp_get_clipboard_string() &byte + +// special run-function for SOKOL_NO_ENTRY (in standard mode this is an empty stub) fn C.sapp_run(desc &C.sapp_desc) int -/* GL: return true when GLES2 fallback is active (to detect fallback from GLES3) */ +// GL: return true when GLES2 fallback is active (to detect fallback from GLES3) fn C.sapp_gles2() bool -/* HTML5: enable or disable the hardwired "Leave Site?" dialog box */ +// HTML5: enable or disable the hardwired "Leave Site?" dialog box fn C.sapp_html5_ask_leave_site(ask bool) -/* Metal: get ARC-bridged pointer to Metal device object */ +// Metal: get ARC-bridged pointer to Metal device object fn C.sapp_metal_get_device() voidptr -/* Metal: get ARC-bridged pointer to this frame's renderpass descriptor */ + +// Metal: get ARC-bridged pointer to this frame's renderpass descriptor fn C.sapp_metal_get_renderpass_descriptor() voidptr -/* Metal: get ARC-bridged pointer to current drawable */ + +// Metal: get ARC-bridged pointer to current drawable fn C.sapp_metal_get_drawable() voidptr -/* macOS: get ARC-bridged pointer to macOS NSWindow */ + +// macOS: get ARC-bridged pointer to macOS NSWindow fn C.sapp_macos_get_window() voidptr -/* iOS: get ARC-bridged pointer to iOS UIWindow */ + +// iOS: get ARC-bridged pointer to iOS UIWindow fn C.sapp_ios_get_window() voidptr -/* D3D11: get pointer to ID3D11Device object */ +// D3D11: get pointer to ID3D11Device object fn C.sapp_d3d11_get_device() voidptr -/* D3D11: get pointer to ID3D11DeviceContext object */ + +// D3D11: get pointer to ID3D11DeviceContext object fn C.sapp_d3d11_get_device_context() voidptr -/* D3D11: get pointer to ID3D11RenderTargetView object */ + +// D3D11: get pointer to ID3D11RenderTargetView object fn C.sapp_d3d11_get_render_target_view() voidptr -/* D3D11: get pointer to ID3D11DepthStencilView */ + +// D3D11: get pointer to ID3D11DepthStencilView fn C.sapp_d3d11_get_depth_stencil_view() voidptr -/* Win32: get the HWND window handle */ + +// Win32: get the HWND window handle fn C.sapp_win32_get_hwnd() voidptr -/* Android: get native activity handle */ + +// Android: get native activity handle fn C.sapp_android_get_native_activity() voidptr diff --git a/vlib/sokol/sapp/sapp_structs.v b/vlib/sokol/sapp/sapp_structs.v index b0cbfa69b..a8ff8b1af 100644 --- a/vlib/sokol/sapp/sapp_structs.v +++ b/vlib/sokol/sapp/sapp_structs.v @@ -2,46 +2,46 @@ module sapp pub struct C.sapp_desc { pub: - init_cb fn () // these are the user-provided callbacks without user data - frame_cb fn () - cleanup_cb fn () - event_cb fn (&C.sapp_event) //&sapp_event) - fail_cb fn (&byte) + init_cb fn () // these are the user-provided callbacks without user data + frame_cb fn () + cleanup_cb fn () + event_cb fn (&C.sapp_event) //&sapp_event) + fail_cb fn (&byte) - user_data voidptr // these are the user-provided callbacks with user data - init_userdata_cb fn (voidptr) - frame_userdata_cb fn (voidptr) - cleanup_userdata_cb fn (voidptr) - event_userdata_cb fn (&C.sapp_event, voidptr) - fail_userdata_cb fn (&char, voidptr) + user_data voidptr // these are the user-provided callbacks with user data + init_userdata_cb fn (voidptr) + frame_userdata_cb fn (voidptr) + cleanup_userdata_cb fn (voidptr) + event_userdata_cb fn (&C.sapp_event, voidptr) + fail_userdata_cb fn (&char, voidptr) - width int // the preferred width of the window / canvas - height int // the preferred height of the window / canvas - sample_count int // MSAA sample count - swap_interval int // the preferred swap interval (ignored on some platforms) - high_dpi bool // whether the rendering canvas is full-resolution on HighDPI displays - fullscreen bool // whether the window should be created in fullscreen mode - alpha bool // whether the framebuffer should have an alpha channel (ignored on some platforms) - window_title &char // the window title as UTF-8 encoded string - user_cursor bool // if true, user is expected to manage cursor image in SAPP_EVENTTYPE_UPDATE_CURSOR - enable_clipboard bool // enable clipboard access, default is false - clipboard_size int // max size of clipboard content in bytes - enable_dragndrop bool // enable file dropping (drag'n'drop), default is false - max_dropped_files int // max number of dropped files to process (default: 1) - max_dropped_file_path_length int // max length in bytes of a dropped UTF-8 file path (default: 2048) - /* backend-specific options */ + width int // the preferred width of the window / canvas + height int // the preferred height of the window / canvas + sample_count int // MSAA sample count + swap_interval int // the preferred swap interval (ignored on some platforms) + high_dpi bool // whether the rendering canvas is full-resolution on HighDPI displays + fullscreen bool // whether the window should be created in fullscreen mode + alpha bool // whether the framebuffer should have an alpha channel (ignored on some platforms) + window_title &char // the window title as UTF-8 encoded string + user_cursor bool // if true, user is expected to manage cursor image in SAPP_EVENTTYPE_UPDATE_CURSOR + enable_clipboard bool // enable clipboard access, default is false + clipboard_size int // max size of clipboard content in bytes + enable_dragndrop bool // enable file dropping (drag'n'drop), default is false + max_dropped_files int // max number of dropped files to process (default: 1) + max_dropped_file_path_length int // max length in bytes of a dropped UTF-8 file path (default: 2048) + // backend-specific options gl_force_gles2 bool // if true, setup GLES2/WebGL even if GLES3/WebGL2 is available win32_console_utf8 bool // if true, set the output console codepage to UTF-8 - win32_console_create bool // if true, attach stdout/stderr to a new console window - win32_console_attach bool // if true, attach stdout/stderr to parent process + win32_console_create bool // if true, attach stdout/stderr to a new console window + win32_console_attach bool // if true, attach stdout/stderr to parent process html5_canvas_name &char // the name (id) of the HTML5 canvas element, default is "canvas" html5_canvas_resize bool // if true, the HTML5 canvas size is set to sapp_desc.width/height, otherwise canvas size is tracked html5_preserve_drawing_buffer bool // HTML5 only: whether to preserve default framebuffer content between frames html5_premultiplied_alpha bool // HTML5 only: whether the rendered pixels use premultiplied alpha convention html5_ask_leave_site bool // initial state of the internal html5_ask_leave_site flag (see sapp_html5_ask_leave_site()) ios_keyboard_resizes_canvas bool // if true, showing the iOS keyboard shrinks the canvas - /* V patches */ - __v_native_render bool // V patch to allow for native rendering + // V patches + __v_native_render bool // V patch to allow for native rendering } pub struct Event { diff --git a/vlib/sokol/sfons/sfons.v b/vlib/sokol/sfons/sfons.v index 0c46e0e1b..08d2dc6a8 100644 --- a/vlib/sokol/sfons/sfons.v +++ b/vlib/sokol/sfons/sfons.v @@ -26,4 +26,3 @@ pub fn rgba(r byte, g byte, b byte, a byte) u32 { pub fn flush(ctx &C.FONScontext) { C.sfons_flush(ctx) } - diff --git a/vlib/sokol/sgl/sgl.v b/vlib/sokol/sgl/sgl.v index a1c31f673..0129cbfb2 100644 --- a/vlib/sokol/sgl/sgl.v +++ b/vlib/sokol/sgl/sgl.v @@ -6,7 +6,7 @@ pub const ( version = gfx.version + 1 ) -/* setup/shutdown/misc */ +// setup/shutdown/misc [inline] pub fn setup(desc &C.sgl_desc_t) { C.sgl_setup(desc) @@ -37,7 +37,7 @@ pub fn deg(rad f32) f32 { return C.sgl_deg(rad) } -/* create and destroy pipeline objects */ +// create and destroy pipeline objects [inline] pub fn make_pipeline(desc &C.sg_pipeline_desc) C.sgl_pipeline { return C.sgl_make_pipeline(desc) @@ -48,7 +48,7 @@ pub fn destroy_pipeline(pip C.sgl_pipeline) { C.sgl_destroy_pipeline(pip) } -/* render state functions */ +// render state functions [inline] pub fn viewport(x int, y int, w int, h int, origin_top_left bool) { C.sgl_viewport(x, y, w, h, origin_top_left) @@ -74,7 +74,7 @@ pub fn texture(img C.sg_image) { C.sgl_texture(img) } -/* pipeline stack functions */ +// pipeline stack functions [inline] pub fn default_pipeline() { C.sgl_default_pipeline() @@ -95,7 +95,7 @@ pub fn pop_pipeline() { C.sgl_pop_pipeline() } -/* matrix stack functions */ +// matrix stack functions [inline] pub fn matrix_mode_modelview() { C.sgl_matrix_mode_modelview() @@ -181,7 +181,7 @@ pub fn pop_matrix() { C.sgl_pop_matrix() } -/* these functions only set the internal 'current texcoord / color' (valid inside or outside begin/end) */ +// these functions only set the internal 'current texcoord / color' (valid inside or outside begin/end) [inline] pub fn t2f(u f32, v f32) { C.sgl_t2f(u, v) @@ -212,7 +212,7 @@ pub fn c1i(rgba u32) { C.sgl_c1i(rgba) } -/* define primitives, each begin/end is one draw command */ +// define primitives, each begin/end is one draw command [inline] pub fn begin_points() { C.sgl_begin_points() @@ -368,9 +368,8 @@ pub fn end() { C.sgl_end() } -/* render everything */ +// render everything [inline] pub fn draw() { C.sgl_draw() } - diff --git a/vlib/sokol/sgl/sgl_funcs.v b/vlib/sokol/sgl/sgl_funcs.v index 6991eea01..16e980dd6 100644 --- a/vlib/sokol/sgl/sgl_funcs.v +++ b/vlib/sokol/sgl/sgl_funcs.v @@ -1,6 +1,6 @@ module sgl -/* setup/shutdown/misc */ +// setup/shutdown/misc fn C.sgl_setup(desc &C.sgl_desc_t) fn C.sgl_shutdown() fn C.sgl_error() C.sgl_error_t @@ -8,11 +8,11 @@ fn C.sgl_defaults() fn C.sgl_rad(deg f32) f32 fn C.sgl_deg(rad f32) f32 -/* create and destroy pipeline objects */ +// create and destroy pipeline objects fn C.sgl_make_pipeline(desc &C.sg_pipeline_desc) C.sgl_pipeline fn C.sgl_destroy_pipeline(pip C.sgl_pipeline) -/* render state functions */ +// render state functions fn C.sgl_viewport(x int, y int, w int, h int, origin_top_left bool) fn C.sgl_viewportf(x f32, y f32, w f32, h f32, origin_top_left bool) fn C.sgl_scissor_rect(x int, y int, w int, h int, origin_top_left bool) @@ -21,13 +21,13 @@ fn C.sgl_enable_texture() fn C.sgl_disable_texture() fn C.sgl_texture(img C.sg_image) -/* pipeline stack functions */ +// pipeline stack functions fn C.sgl_default_pipeline() fn C.sgl_load_pipeline(pip C.sgl_pipeline) fn C.sgl_push_pipeline() fn C.sgl_pop_pipeline() -/* matrix stack functions */ +// matrix stack functions fn C.sgl_matrix_mode_modelview() fn C.sgl_matrix_mode_projection() fn C.sgl_matrix_mode_texture() @@ -46,7 +46,7 @@ fn C.sgl_lookat(eye_x f32, eye_y f32, eye_z f32, center_x f32, center_y f32, cen fn C.sgl_push_matrix() fn C.sgl_pop_matrix() -/* these functions only set the internal 'current texcoord / color' (valid inside or outside begin/end) */ +// these functions only set the internal 'current texcoord / color' (valid inside or outside begin/end) fn C.sgl_t2f(u f32, v f32) fn C.sgl_c3f(r f32, g f32, b f32) fn C.sgl_c4f(r f32, g f32, b f32, a f32) @@ -54,7 +54,7 @@ fn C.sgl_c3b(r byte, g byte, b byte) fn C.sgl_c4b(r byte, g byte, b byte, a byte) fn C.sgl_c1i(rgba u32) -/* define primitives, each begin/end is one draw command */ +// define primitives, each begin/end is one draw command fn C.sgl_begin_points() fn C.sgl_begin_lines() fn C.sgl_begin_line_strip() @@ -87,5 +87,5 @@ fn C.sgl_v3f_t2f_c4b(x f32, y f32, z f32, u f32, v f32, r byte, g byte, b byte, fn C.sgl_v3f_t2f_c1i(x f32, y f32, z f32, u f32, v f32, rgba u32) fn C.sgl_end() -/* render everything */ +// render everything fn C.sgl_draw() diff --git a/vlib/sokol/sgl/sgl_structs.v b/vlib/sokol/sgl/sgl_structs.v index d7256e594..25753c41b 100644 --- a/vlib/sokol/sgl/sgl_structs.v +++ b/vlib/sokol/sgl/sgl_structs.v @@ -2,7 +2,7 @@ module sgl // should be in a proper module pub enum SglError { - no_error + no_error vertices_full commands_full stack_overflow @@ -14,11 +14,11 @@ pub struct C.sgl_pipeline { } pub struct C.sgl_desc_t { - max_vertices int /* size for vertex buffer */ - max_commands int /* size of uniform- and command-buffers */ - pipeline_pool_size int /* size of the internal pipeline pool, default is 64 */ - color_format C.sg_pixel_format - depth_format C.sg_pixel_format - sample_count int - face_winding C.sg_face_winding /* default front face winding is CCW */ + max_vertices int // size for vertex buffer + max_commands int // size of uniform- and command-buffers + pipeline_pool_size int // size of the internal pipeline pool, default is 64 + color_format C.sg_pixel_format + depth_format C.sg_pixel_format + sample_count int + face_winding C.sg_face_winding // default front face winding is CCW } diff --git a/vlib/sokol/sokol.v b/vlib/sokol/sokol.v index 10ddf256c..8c9a632b1 100644 --- a/vlib/sokol/sokol.v +++ b/vlib/sokol/sokol.v @@ -4,7 +4,7 @@ import sokol.c import sokol.f pub const ( - used_import = c.used_import + f.used_import + used_import = c.used_import + f.used_import ) /* @@ -17,4 +17,3 @@ pub enum Key { space = C.SAPP_KEYCODE_SPACE } */ - diff --git a/vlib/sqlite/sqlite_test.v b/vlib/sqlite/sqlite_test.v index ebcd3aa10..e66db08ba 100644 --- a/vlib/sqlite/sqlite_test.v +++ b/vlib/sqlite/sqlite_test.v @@ -4,9 +4,7 @@ fn test_sqlite() { $if !linux { return } - mut db := sqlite.connect(':memory:') or { - panic(err) - } + mut db := sqlite.connect(':memory:') or { panic(err) } assert db.is_open db.exec('drop table if exists users') db.exec("create table users (id integer primary key, name text default '');") @@ -22,12 +20,8 @@ fn test_sqlite() { assert code == 101 code = db.exec_none('vacuum') assert code == 101 - user := db.exec_one('select * from users where id = 3') or { - panic(err) - } + user := db.exec_one('select * from users where id = 3') or { panic(err) } assert user.vals.len == 2 - db.close() or { - panic(err) - } + db.close() or { panic(err) } assert !db.is_open } diff --git a/vlib/stbi/stbi.v b/vlib/stbi/stbi.v index a8d5139a4..da72dca22 100644 --- a/vlib/stbi/stbi.v +++ b/vlib/stbi/stbi.v @@ -18,13 +18,13 @@ pub mut: ext string } -fn C.stbi_load(filename charptr, x &int, y &int, channels_in_file &int, desired_channels int) byteptr +fn C.stbi_load(filename &char, x &int, y &int, channels_in_file &int, desired_channels int) &byte -fn C.stbi_load_from_file(f voidptr, x &int, y &int, channels_in_file &int, desired_channels int) byteptr +fn C.stbi_load_from_file(f voidptr, x &int, y &int, channels_in_file &int, desired_channels int) &byte -fn C.stbi_load_from_memory(buffer byteptr, len int, x &int, y &int, channels_in_file &int, desired_channels int) byteptr +fn C.stbi_load_from_memory(buffer &byte, len int, x &int, y &int, channels_in_file &int, desired_channels int) &byte -fn C.stbi_image_free(retval_from_stbi_load byteptr) +fn C.stbi_image_free(retval_from_stbi_load &byte) fn C.stbi_set_flip_vertically_on_load(should_flip int) @@ -41,7 +41,8 @@ pub fn load(path string) ?Image { } // flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 } desired_channels := if ext == 'png' { 4 } else { 0 } - res.data = C.stbi_load(&char(path.str), &res.width, &res.height, &res.nr_channels, desired_channels) + res.data = C.stbi_load(&char(path.str), &res.width, &res.height, &res.nr_channels, + desired_channels) if desired_channels == 4 && res.nr_channels == 3 { // Fix an alpha png bug res.nr_channels = 4 @@ -52,7 +53,7 @@ pub fn load(path string) ?Image { return res } -pub fn load_from_memory(buf byteptr, bufsize int) ?Image { +pub fn load_from_memory(buf &byte, bufsize int) ?Image { mut res := Image{ ok: true data: 0 diff --git a/vlib/sync/atomic2/atomic_test.v b/vlib/sync/atomic2/atomic_test.v index f71ce8da8..7a5ffd80a 100644 --- a/vlib/sync/atomic2/atomic_test.v +++ b/vlib/sync/atomic2/atomic_test.v @@ -1,4 +1,4 @@ -import atomic2 +import sync.atomic2 import sync const ( diff --git a/vlib/sync/bench/channel_bench_v.v b/vlib/sync/bench/channel_bench_v.v index de310819f..aa1189f73 100644 --- a/vlib/sync/bench/channel_bench_v.v +++ b/vlib/sync/bench/channel_bench_v.v @@ -7,7 +7,6 @@ // 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) diff --git a/vlib/sync/bench/many_writers_and_receivers_on_1_channel.v b/vlib/sync/bench/many_writers_and_receivers_on_1_channel.v index ce18c8fe8..d7db2b40d 100644 --- a/vlib/sync/bench/many_writers_and_receivers_on_1_channel.v +++ b/vlib/sync/bench/many_writers_and_receivers_on_1_channel.v @@ -27,8 +27,8 @@ mut: n_readers int n_writers int // - pops_wg &sync.WaitGroup - pops []Event + pops_wg &sync.WaitGroup + pops []Event // pushes_wg &sync.WaitGroup pushes []Event diff --git a/vlib/sync/channel_1_test.v b/vlib/sync/channel_1_test.v index 5d8c96861..07383b951 100644 --- a/vlib/sync/channel_1_test.v +++ b/vlib/sync/channel_1_test.v @@ -15,5 +15,5 @@ fn test_channel_buffered() { for _ in 0 .. num_iterations { sum += <-ch } - assert sum == u64(num_iterations)*(num_iterations-1)/2 + assert sum == u64(num_iterations) * (num_iterations - 1) / 2 } diff --git a/vlib/sync/channel_2_test.v b/vlib/sync/channel_2_test.v index acf624505..5e8251d86 100644 --- a/vlib/sync/channel_2_test.v +++ b/vlib/sync/channel_2_test.v @@ -15,5 +15,5 @@ fn test_channel_unbuffered() { for _ in 0 .. num_iterations { sum += <-ch } - assert sum == u64(num_iterations)*(num_iterations-1)/2 + assert sum == u64(num_iterations) * (num_iterations - 1) / 2 } diff --git a/vlib/sync/channel_array_mut_test.v b/vlib/sync/channel_array_mut_test.v index adf10189b..bfd53a1aa 100644 --- a/vlib/sync/channel_array_mut_test.v +++ b/vlib/sync/channel_array_mut_test.v @@ -14,9 +14,7 @@ mut: // this function gets an array of channels for `St` references fn do_rec_calc_send(chs []chan mut St) { for { - mut s := <-chs[0] or { - break - } + mut s := <-chs[0] or { break } s.n++ chs[1] <- s } diff --git a/vlib/sync/channel_close_test.v b/vlib/sync/channel_close_test.v index ba5070e2e..a31bfa91c 100644 --- a/vlib/sync/channel_close_test.v +++ b/vlib/sync/channel_close_test.v @@ -3,9 +3,7 @@ import time fn do_rec(ch chan int, resch chan i64) { mut sum := i64(0) for { - a := <-ch or { - break - } + a := <-ch or { break } sum += a } assert ch.closed == true @@ -32,7 +30,7 @@ fn test_channel_close_buffered_multi() { go do_send(ch) mut sum := i64(0) for _ in 0 .. 4 { - sum += <- resch + sum += <-resch } assert sum == i64(8000) * (8000 - 1) / 2 } @@ -77,9 +75,8 @@ fn test_channel_send_close_buffered() { t := go fn (ch chan int) { ch <- 31 mut x := 45 - ch <- 17 or { - x = -133 - } + ch <- 17 or { x = -133 } + assert x == -133 }(ch) time.sleep(100 * time.millisecond) @@ -95,9 +92,8 @@ fn test_channel_send_close_unbuffered() { ch := chan int{} t := go fn (ch chan int) { mut x := 31 - ch <- 177 or { - x = -71 - } + ch <- 177 or { x = -71 } + assert x == -71 }(ch) time.sleep(100 * time.millisecond) diff --git a/vlib/sync/channel_fill_test.v b/vlib/sync/channel_fill_test.v index 2f02153b2..b4eabc0a7 100644 --- a/vlib/sync/channel_fill_test.v +++ b/vlib/sync/channel_fill_test.v @@ -1,7 +1,7 @@ import sync const ( - queue_len = 1000 + queue_len = 1000 queue_fill = 763 ) diff --git a/vlib/sync/channel_polling_test.v b/vlib/sync/channel_polling_test.v index f1a7329c7..846dcfd34 100644 --- a/vlib/sync/channel_polling_test.v +++ b/vlib/sync/channel_polling_test.v @@ -1,4 +1,3 @@ - // Channel Benchmark // // `nobj` integers are sent thru a channel with queue length`buflen` diff --git a/vlib/sync/channel_push_or_1_test.v b/vlib/sync/channel_push_or_1_test.v index c9d107532..1551d830d 100644 --- a/vlib/sync/channel_push_or_1_test.v +++ b/vlib/sync/channel_push_or_1_test.v @@ -1,4 +1,5 @@ const n = 1000 + const c = 100 fn f(ch chan int) { @@ -13,9 +14,8 @@ fn test_push_or_unbuffered() { go f(ch) mut j := 0 for { - ch <- j or { - break - } + ch <- j or { break } + j++ } assert j == n @@ -26,9 +26,8 @@ fn test_push_or_buffered() { go f(ch) mut j := 0 for { - ch <- j or { - break - } + ch <- j or { break } + j++ } // we don't know how many elements are in the buffer when the channel @@ -40,9 +39,8 @@ fn test_push_or_buffered() { fn g(ch chan int, res chan int) { mut j := 0 for { - ch <- j or { - break - } + ch <- j or { break } + j++ } println('done $j') diff --git a/vlib/sync/channel_push_or_2_test.v b/vlib/sync/channel_push_or_2_test.v index 7004e2c86..8f9539518 100644 --- a/vlib/sync/channel_push_or_2_test.v +++ b/vlib/sync/channel_push_or_2_test.v @@ -19,9 +19,7 @@ fn test_push_propargate() { go f(ch) mut s := 1.0 for { - s = do_send(ch, s) or { - break - } + s = do_send(ch, s) or { break } } assert s == f64(n + 1) } diff --git a/vlib/sync/channel_select_3_test.v b/vlib/sync/channel_select_3_test.v index 26dda99ac..6a6c503e3 100644 --- a/vlib/sync/channel_select_3_test.v +++ b/vlib/sync/channel_select_3_test.v @@ -91,8 +91,8 @@ fn test_select_blocks() { sem.wait() elapsed_ms := f64(stopwatch.elapsed()) / time.millisecond // https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/high-resolution-timers - // > For example, for Windows running on an x86 processor, the default interval between - // > system clock ticks is typically about 15 milliseconds, and the minimum interval + // > For example, for Windows running on an x86 processor, the default interval between + // > system clock ticks is typically about 15 milliseconds, and the minimum interval // > between system clock ticks is about 1 millisecond. assert elapsed_ms >= 280.0 // 300 - (15ms + 5ms just in case) diff --git a/vlib/sync/channel_select_4_test.v b/vlib/sync/channel_select_4_test.v index 646e8ac7c..77cd5574e 100644 --- a/vlib/sync/channel_select_4_test.v +++ b/vlib/sync/channel_select_4_test.v @@ -31,13 +31,13 @@ fn test_select() { } } // Use Gauß' formula - expected_sum := i64(30000) * (30000 - 1) / 2 + expected_sum := i64(30000) * (30000 - 1) / 2 assert sum == expected_sum - mut sumrec := <- chsum + mut sumrec := <-chsum // Empty receive buffer for _ in 0 .. recch.cap { - sumrec += <- recch + sumrec += <-recch } assert sumrec == i64(30000 + recch.cap) * (30000 + recch.cap - 1) / 2 } diff --git a/vlib/sync/channel_select_5_test.v b/vlib/sync/channel_select_5_test.v index fbe2392ce..97228fe73 100644 --- a/vlib/sync/channel_select_5_test.v +++ b/vlib/sync/channel_select_5_test.v @@ -47,15 +47,15 @@ fn test_select() { } } // Use Gauß' formula - expected_sum := i64(30000) * (30000 - 1) / 2 + expected_sum := i64(30000) * (30000 - 1) / 2 assert sum == expected_sum - mut sumrec := <- chsum - sumrec += <- chsum - sumrec += <- chsum + mut sumrec := <-chsum + sumrec += <-chsum + sumrec += <-chsum // Empty receive buffer for _ in 0 .. recch.cap { - sumrec += <- recch + sumrec += <-recch } assert sumrec == i64(30000 + recch.cap) * (30000 + recch.cap - 1) / 2 } diff --git a/vlib/sync/channel_select_6_test.v b/vlib/sync/channel_select_6_test.v index 2c3573c1e..e1b4ff884 100644 --- a/vlib/sync/channel_select_6_test.v +++ b/vlib/sync/channel_select_6_test.v @@ -7,7 +7,7 @@ fn do_select(ch1 chan int, ch2 chan int, chf1 chan f64, chf2 chan f64, sumch1 ch mut sum1 := i64(0) mut sum2 := i64(0) f1 := 17. - f2 := 7. + f2 := 7. for _ in 0 .. 20000 + chf1.cap / 3 { select { chf1 <- f1 {} @@ -31,7 +31,7 @@ fn do_send_int(ch chan int, factor int) { } fn do_rec_f64(ch chan f64, sumch chan f64) { - mut sum:= 0. + mut sum := 0. for _ in 0 .. 10000 { sum += <-ch } @@ -67,7 +67,7 @@ fn test_select() { } sumf2 := <-chsumf2 + <-chsumf2 // Use Gauß' formula - expected_sum := i64(10000) * (10000 - 1) / 2 + expected_sum := i64(10000) * (10000 - 1) / 2 assert sum1 == 3 * expected_sum assert sum2 == (7 + 17) * expected_sum assert sumf1 == 17. * f64(10000 + chf1.cap) diff --git a/vlib/sync/channel_select_test.v b/vlib/sync/channel_select_test.v index d24854197..71f5efcca 100644 --- a/vlib/sync/channel_select_test.v +++ b/vlib/sync/channel_select_test.v @@ -1,10 +1,10 @@ /* - * ATTENTION! Do not use this file as an example! +* ATTENTION! Do not use this file as an example! * For that, please look at `channel_select_2_test.v` or `channel_select_3_test.v` * * This test case uses the implementation in `sync/channels.v` directly * in order to test it independently from the support in the core language - */ +*/ module sync @@ -77,8 +77,6 @@ fn test_select() { } // Use Gauß' formula for the first 2 contributions // the 3rd contribution is `byte` and must be seen modulo 256 - expected_sum := 2 * (300 * (300 - 1) / 2) + - 256 * (256 - 1) / 2 + - 44 * (44 - 1) / 2 + expected_sum := 2 * (300 * (300 - 1) / 2) + 256 * (256 - 1) / 2 + 44 * (44 - 1) / 2 assert sum == expected_sum } diff --git a/vlib/sync/channel_try_buf_test.v b/vlib/sync/channel_try_buf_test.v index 0d718386a..e52131480 100644 --- a/vlib/sync/channel_try_buf_test.v +++ b/vlib/sync/channel_try_buf_test.v @@ -1,6 +1,6 @@ fn test_channel_try_buffered() { ch := chan int{cap: 5} - for z in 2..13 { + for z in 2 .. 13 { if ch.try_push(z) == .not_ready { assert z == 7 break diff --git a/vlib/sync/channel_try_unbuf_test.v b/vlib/sync/channel_try_unbuf_test.v index 9b710f20f..ee11468a6 100644 --- a/vlib/sync/channel_try_unbuf_test.v +++ b/vlib/sync/channel_try_unbuf_test.v @@ -1,6 +1,6 @@ fn test_channel_try_unbuffered() { ch := chan int{} - for z in 5..8 { + for z in 5 .. 8 { if ch.try_push(z) == .not_ready { assert z == 5 break diff --git a/vlib/sync/pool/pool.v b/vlib/sync/pool/pool.v index 5d33f7730..7171d3a73 100644 --- a/vlib/sync/pool/pool.v +++ b/vlib/sync/pool/pool.v @@ -1,7 +1,6 @@ module pool import sync - import runtime [trusted] @@ -12,7 +11,7 @@ pub const ( ) pub struct PoolProcessor { - thread_cb voidptr + thread_cb voidptr mut: njobs int items []voidptr @@ -46,7 +45,7 @@ pub fn new_pool_processor(context PoolProcessorConfig) &PoolProcessor { if isnil(context.callback) { panic('You need to pass a valid callback to new_pool_processor.') } - mut pool := &PoolProcessor { + mut pool := &PoolProcessor{ items: [] results: [] shared_context: voidptr(0) @@ -74,7 +73,7 @@ pub fn (mut pool PoolProcessor) set_max_jobs(njobs int) { // work_on_items returns *after* all threads finish. // You can optionally call get_results after that. pub fn (mut pool PoolProcessor) work_on_items(items []T) { - pool.work_on_pointers( unsafe { items.pointers() } ) + pool.work_on_pointers(unsafe { items.pointers() }) } pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) { @@ -86,15 +85,15 @@ pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) { pool.results = [] pool.thread_contexts = [] pool.items << items - pool.results = []voidptr{len:(pool.items.len)} - pool.thread_contexts << []voidptr{len:(pool.items.len)} + pool.results = []voidptr{len: (pool.items.len)} + pool.thread_contexts << []voidptr{len: (pool.items.len)} pool.waitgroup.add(njobs) for i := 0; i < njobs; i++ { if njobs > 1 { - go process_in_thread(mut pool,i) + go process_in_thread(mut pool, i) } else { // do not run concurrently, just use the same thread: - process_in_thread(mut pool,i) + process_in_thread(mut pool, i) } } pool.waitgroup.wait() diff --git a/vlib/sync/select_close_test.v b/vlib/sync/select_close_test.v index 5e6030932..3a40e2d4e 100644 --- a/vlib/sync/select_close_test.v +++ b/vlib/sync/select_close_test.v @@ -87,8 +87,6 @@ fn test_select() { } // Use Gauß' formula for the first 2 contributions // the 3rd contribution is `byte` and must be seen modulo 256 - expected_sum := 2 * (300 * (300 - 1) / 2) + - 256 * (256 - 1) / 2 + - 44 * (44 - 1) / 2 + expected_sum := 2 * (300 * (300 - 1) / 2) + 256 * (256 - 1) / 2 + 44 * (44 - 1) / 2 assert sum == expected_sum } diff --git a/vlib/sync/sync_macos.c.v b/vlib/sync/sync_macos.c.v index c62b133a8..9ee55dfac 100644 --- a/vlib/sync/sync_macos.c.v +++ b/vlib/sync/sync_macos.c.v @@ -49,11 +49,13 @@ struct CondAttr { attr C.pthread_condattr_t } -/* MacOSX has no unnamed semaphores and no `timed_wait()` at all - so we emulate the behaviour with other devices */ +/* +MacOSX has no unnamed semaphores and no `timed_wait()` at all + so we emulate the behaviour with other devices +*/ [heap] struct Semaphore { - mtx C.pthread_mutex_t + mtx C.pthread_mutex_t cond C.pthread_cond_t mut: count u32 @@ -136,7 +138,9 @@ pub fn (mut sem Semaphore) init(n u32) { pub fn (mut sem Semaphore) post() { mut c := C.atomic_load_u32(&sem.count) for c > 1 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c+1) { return } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c + 1) { + return + } } C.pthread_mutex_lock(&sem.mtx) c = C.atomic_fetch_add_u32(&sem.count, 1) @@ -149,18 +153,20 @@ pub fn (mut sem Semaphore) post() { pub fn (mut sem Semaphore) wait() { mut c := C.atomic_load_u32(&sem.count) for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { + return + } } C.pthread_mutex_lock(&sem.mtx) c = C.atomic_load_u32(&sem.count) -outer: - for { + + outer: for { if c == 0 { C.pthread_cond_wait(&sem.cond, &sem.mtx) c = C.atomic_load_u32(&sem.count) } for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { if c > 1 { C.pthread_cond_signal(&sem.cond) } @@ -174,7 +180,9 @@ outer: pub fn (mut sem Semaphore) try_wait() bool { mut c := C.atomic_load_u32(&sem.count) for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return true } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { + return true + } } return false } @@ -182,14 +190,16 @@ pub fn (mut sem Semaphore) try_wait() bool { pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool { mut c := C.atomic_load_u32(&sem.count) for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return true } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { + return true + } } C.pthread_mutex_lock(&sem.mtx) t_spec := timeout.timespec() mut res := 0 c = C.atomic_load_u32(&sem.count) -outer: - for { + + outer: for { if c == 0 { res = C.pthread_cond_timedwait(&sem.cond, &sem.mtx, &t_spec) if res == C.ETIMEDOUT { @@ -198,7 +208,7 @@ outer: c = C.atomic_load_u32(&sem.count) } for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { if c > 1 { C.pthread_cond_signal(&sem.cond) } @@ -211,6 +221,5 @@ outer: } pub fn (mut sem Semaphore) destroy() bool { - return C.pthread_cond_destroy(&sem.cond) == 0 && - C.pthread_mutex_destroy(&sem.mtx) == 0 + return C.pthread_cond_destroy(&sem.cond) == 0 && C.pthread_mutex_destroy(&sem.mtx) == 0 } diff --git a/vlib/sync/sync_windows.c.v b/vlib/sync/sync_windows.c.v index 115d1b805..799533647 100644 --- a/vlib/sync/sync_windows.c.v +++ b/vlib/sync/sync_windows.c.v @@ -16,6 +16,7 @@ fn C.SleepConditionVariableSRW(voidptr, voidptr, u32, u32) int // Mutex HANDLE type MHANDLE = voidptr + // Semaphore HANDLE type SHANDLE = voidptr @@ -25,18 +26,18 @@ type SHANDLE = voidptr [heap] pub struct Mutex { mut: - mx C.SRWLOCK // mutex handle + mx C.SRWLOCK // mutex handle } [heap] pub struct RwMutex { mut: - mx C.SRWLOCK // mutex handle + mx C.SRWLOCK // mutex handle } [heap] struct Semaphore { - mtx C.SRWLOCK + mtx C.SRWLOCK cond C.CONDITION_VARIABLE mut: count u32 @@ -113,7 +114,9 @@ pub fn (mut sem Semaphore) init(n u32) { pub fn (mut sem Semaphore) post() { mut c := C.atomic_load_u32(&sem.count) for c > 1 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c+1) { return } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c + 1) { + return + } } C.AcquireSRWLockExclusive(&sem.mtx) c = C.atomic_fetch_add_u32(&sem.count, 1) @@ -126,18 +129,20 @@ pub fn (mut sem Semaphore) post() { pub fn (mut sem Semaphore) wait() { mut c := C.atomic_load_u32(&sem.count) for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { + return + } } C.AcquireSRWLockExclusive(&sem.mtx) c = C.atomic_load_u32(&sem.count) -outer: - for { + + outer: for { if c == 0 { C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, C.INFINITE, 0) c = C.atomic_load_u32(&sem.count) } for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { if c > 1 { C.WakeConditionVariable(&sem.cond) } @@ -151,7 +156,9 @@ outer: pub fn (mut sem Semaphore) try_wait() bool { mut c := C.atomic_load_u32(&sem.count) for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return true } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { + return true + } } return false } @@ -159,14 +166,16 @@ pub fn (mut sem Semaphore) try_wait() bool { pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool { mut c := C.atomic_load_u32(&sem.count) for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return true } + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { + return true + } } C.AcquireSRWLockExclusive(&sem.mtx) t_ms := u32(timeout / time.millisecond) mut res := 0 c = C.atomic_load_u32(&sem.count) -outer: - for { + + outer: for { if c == 0 { res = C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, t_ms, 0) if res == 0 { @@ -175,7 +184,7 @@ outer: c = C.atomic_load_u32(&sem.count) } for c > 0 { - if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { + if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) { if c > 1 { C.WakeConditionVariable(&sem.cond) } diff --git a/vlib/sync/waitgroup.v b/vlib/sync/waitgroup.v index d9d4aca66..e9b3d170e 100644 --- a/vlib/sync/waitgroup.v +++ b/vlib/sync/waitgroup.v @@ -15,13 +15,13 @@ fn C.atomic_fetch_add_u32(voidptr, u32) u32 // `wg.wait()` to wait for all jobs to have finished // // in each parallel job: -// `wg.done()` when finished +// `wg.done()` when finished // // [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function. [heap] struct WaitGroup { mut: - task_count u32 // current task count - reading/writing should be atomic + task_count u32 // current task count - reading/writing should be atomic sem Semaphore // This blocks wait() until tast_countreleased by add() } diff --git a/vlib/szip/szip.v b/vlib/szip/szip.v index 4929aa23b..059c98547 100644 --- a/vlib/szip/szip.v +++ b/vlib/szip/szip.v @@ -234,7 +234,7 @@ pub fn zip_files(path_to_file []string, path_to_export_zip string) ? { } /* -TODO add +TODO add // zip all files in directory to zip file pub fn zip_folder(path_to_dir string, path_to_export_zip string) { diff --git a/vlib/term/term.js.v b/vlib/term/term.js.v index b524e7ca8..be85eca0e 100644 --- a/vlib/term/term.js.v +++ b/vlib/term/term.js.v @@ -2,7 +2,7 @@ module term // get_terminal_size returns a number of colums and rows of terminal window. pub fn get_terminal_size() (int, int) { - // TODO Find a way to get proper width & height of the terminal - // on a Javascript environment - return default_columns_size, default_rows_size + // TODO Find a way to get proper width & height of the terminal + // on a Javascript environment + return default_columns_size, default_rows_size } diff --git a/vlib/term/term_test.v b/vlib/term/term_test.v index e26b6cbec..200ef5e55 100644 --- a/vlib/term/term_test.v +++ b/vlib/term/term_test.v @@ -63,16 +63,16 @@ fn test_get_cursor_position() { assert original_position.x == cursor_position_1.x assert original_position.y == cursor_position_1.y // - term.set_cursor_position({ + term.set_cursor_position( x: 10 y: 11 - }) + ) cursor_position_2 := term.get_cursor_position() // - term.set_cursor_position({ + term.set_cursor_position( x: 5 y: 6 - }) + ) cursor_position_3 := term.get_cursor_position() // term.set_cursor_position(original_position) diff --git a/vlib/term/term_windows.c.v b/vlib/term/term_windows.c.v index bb65f123d..1d2f0329f 100644 --- a/vlib/term/term_windows.c.v +++ b/vlib/term/term_windows.c.v @@ -39,7 +39,7 @@ mut: [typedef] struct C.CHAR_INFO { mut: - Char C.uChar + Char C.uChar Attributes u16 } diff --git a/vlib/term/ui/color.v b/vlib/term/ui/color.v index e0ce7f282..3e0a0bbf1 100644 --- a/vlib/term/ui/color.v +++ b/vlib/term/ui/color.v @@ -31,9 +31,9 @@ fn init_color_table() []int { color_table_[15] = 0xffffff // color palette for i in 0 .. 216 { - r := value_range[(i / 36) % 6] - g := value_range[(i / 6) % 6] - b := value_range[i % 6] + r := ui.value_range[(i / 36) % 6] + g := ui.value_range[(i / 6) % 6] + b := ui.value_range[i % 6] color_table_[i + 16] = ((r << 16) & 0xffffff) + ((g << 8) & 0xffff) + (b & 0xff) } // grayscale @@ -70,7 +70,7 @@ fn lookup_rgb(r int, g int, b int) int { color := (r << 16) + (g << 8) + b // lookup extended colors only, coz non-extended can be changed by users. for i in 16 .. 256 { - if color_table[i] == color { + if ui.color_table[i] == color { return i } } diff --git a/vlib/term/ui/input.v b/vlib/term/ui/input.v index a44b0bc22..30a28822f 100644 --- a/vlib/term/ui/input.v +++ b/vlib/term/ui/input.v @@ -179,7 +179,7 @@ pub: pub struct Context { ExtraContext // contains fields specific to an implementation pub: - cfg Config + cfg Config // adsasdas mut: print_buf []byte paused bool diff --git a/vlib/term/ui/input_windows.c.v b/vlib/term/ui/input_windows.c.v index 0a6d21940..3453ef7d0 100644 --- a/vlib/term/ui/input_windows.c.v +++ b/vlib/term/ui/input_windows.c.v @@ -21,13 +21,13 @@ mut: } fn restore_terminal_state() { - if ctx_ptr != 0 { - if ctx_ptr.cfg.use_alternate_buffer { + if ui.ctx_ptr != 0 { + if ui.ctx_ptr.cfg.use_alternate_buffer { // clear the terminal and set the cursor to the origin print('\x1b[2J\x1b[3J') print('\x1b[?1049l') } - C.SetConsoleMode(ctx_ptr.stdin_handle, stdin_at_startup) + C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup) } load_title() os.flush() @@ -44,7 +44,7 @@ pub fn init(cfg Config) &Context { panic('could not get stdin handle') } // save the current input mode, to be restored on exit - if C.GetConsoleMode(stdin_handle, &stdin_at_startup) == 0 { + if C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) == 0 { panic('could not get stdin console mode') } @@ -77,14 +77,13 @@ pub fn init(cfg Config) &Context { } unsafe { - x := &ctx_ptr + x := &ui.ctx_ptr *x = ctx } - C.atexit(restore_terminal_state) for code in ctx.cfg.reset { - os.signal(code, fn() { - mut c := ctx_ptr + os.signal(code, fn () { + mut c := ui.ctx_ptr if c != 0 { c.cleanup() } @@ -129,10 +128,12 @@ fn (mut ctx Context) parse_events() { if !C.GetNumberOfConsoleInputEvents(ctx.stdin_handle, &nr_events) { panic('could not get number of events in stdin') } - if nr_events < 1 { return } + if nr_events < 1 { + return + } // print('$nr_events | ') - if !C.ReadConsoleInput(ctx.stdin_handle, &ctx.read_buf[0], buf_size, &nr_events) { + if !C.ReadConsoleInput(ctx.stdin_handle, &ctx.read_buf[0], ui.buf_size, &nr_events) { panic('could not read from stdin') } for i in 0 .. nr_events { @@ -142,13 +143,16 @@ fn (mut ctx Context) parse_events() { e := unsafe { ctx.read_buf[i].Event.KeyEvent } ch := e.wVirtualKeyCode ascii := unsafe { e.uChar.AsciiChar } - if e.bKeyDown == 0 { continue } // we don't handle key_up events because they don't exist on linux... + if e.bKeyDown == 0 { + continue + } + // we don't handle key_up events because they don't exist on linux... // see: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes code := match int(ch) { C.VK_BACK { KeyCode.backspace } C.VK_RETURN { KeyCode.enter } C.VK_PRIOR { KeyCode.page_up } - 14 ... 20 { KeyCode.null } + 14...20 { KeyCode.null } C.VK_NEXT { KeyCode.page_down } C.VK_END { KeyCode.end } C.VK_HOME { KeyCode.home } @@ -158,17 +162,23 @@ fn (mut ctx Context) parse_events() { C.VK_DOWN { KeyCode.down } C.VK_INSERT { KeyCode.insert } C.VK_DELETE { KeyCode.delete } - 65 ... 90 { KeyCode(ch + 32) } // letters - 91 ... 93 { KeyCode.null } // special keys - 96 ... 105 { KeyCode(ch - 48) } // numpad numbers - 112 ... 135 { KeyCode(ch + 178) } // f1 - f24 + 65...90 { KeyCode(ch + 32) } // letters + 91...93 { KeyCode.null } // special keys + 96...105 { KeyCode(ch - 48) } // numpad numbers + 112...135 { KeyCode(ch + 178) } // f1 - f24 else { KeyCode(ascii) } } mut modifiers := Modifiers{} - if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers.set(.alt) } - if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers.set(.ctrl) } - if e.dwControlKeyState & 0x10 != 0 { modifiers.set(.shift) } + if e.dwControlKeyState & (0x1 | 0x2) != 0 { + modifiers.set(.alt) + } + if e.dwControlKeyState & (0x4 | 0x8) != 0 { + modifiers.set(.ctrl) + } + if e.dwControlKeyState & 0x10 != 0 { + modifiers.set(.shift) + } mut event := &Event{ typ: .key_down @@ -190,9 +200,15 @@ fn (mut ctx Context) parse_events() { x := e.dwMousePosition.X + 1 y := int(e.dwMousePosition.Y) - sb_info.srWindow.Top + 1 mut modifiers := Modifiers{} - if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers.set(.alt) } - if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers.set(.ctrl) } - if e.dwControlKeyState & 0x10 != 0 { modifiers.set(.shift) } + if e.dwControlKeyState & (0x1 | 0x2) != 0 { + modifiers.set(.alt) + } + if e.dwControlKeyState & (0x4 | 0x8) != 0 { + modifiers.set(.ctrl) + } + if e.dwControlKeyState & 0x10 != 0 { + modifiers.set(.shift) + } // TODO: handle capslock/numlock/etc?? events exist for those keys match int(e.dwEventFlags) { C.MOUSE_MOVED { @@ -220,23 +236,34 @@ fn (mut ctx Context) parse_events() { button: button modifiers: modifiers }) - } C.MOUSE_WHEELED { + } + C.MOUSE_WHEELED { ctx.event(&Event{ typ: .mouse_scroll - direction: if i16(e.dwButtonState >> 16) < 0 { Direction.up } else { Direction.down } + direction: if i16(e.dwButtonState >> 16) < 0 { + Direction.up + } else { + Direction.down + } x: x y: y modifiers: modifiers }) - } 0x0008 /* C.MOUSE_HWHEELED */ { + } + 0x0008 /* C.MOUSE_HWHEELED */ { ctx.event(&Event{ typ: .mouse_scroll - direction: if i16(e.dwButtonState >> 16) < 0 { Direction.right } else { Direction.left } + direction: if i16(e.dwButtonState >> 16) < 0 { + Direction.right + } else { + Direction.left + } x: x y: y modifiers: modifiers }) - } 0 /* CLICK */, C.DOUBLE_CLICK { + } + 0 /* CLICK */, C.DOUBLE_CLICK { button := match int(e.dwButtonState) { 0 { ctx.mouse_down } 1 { MouseButton.left } @@ -251,7 +278,8 @@ fn (mut ctx Context) parse_events() { button: button modifiers: modifiers }) - } else {} + } + else {} } } C.WINDOW_BUFFER_SIZE_EVENT { @@ -287,12 +315,12 @@ fn (mut ctx Context) parse_events() { [inline] fn save_title() { - // restore the previously saved terminal title - print('\x1b[22;0t') + // restore the previously saved terminal title + print('\x1b[22;0t') } [inline] fn load_title() { - // restore the previously saved terminal title - print('\x1b[23;0t') + // restore the previously saved terminal title + print('\x1b[23;0t') } diff --git a/vlib/term/ui/termios_nix.c.v b/vlib/term/ui/termios_nix.c.v index a39c3d0d6..cacb1266b 100644 --- a/vlib/term/ui/termios_nix.c.v +++ b/vlib/term/ui/termios_nix.c.v @@ -60,7 +60,8 @@ fn (mut ctx Context) termios_setup() ? { // store the current title, so restore_terminal_state can get it back save_title() - if !ctx.cfg.skip_init_checks && !(os.is_atty(C.STDIN_FILENO) != 0 && os.is_atty(C.STDOUT_FILENO) != 0) { + if !ctx.cfg.skip_init_checks && !(os.is_atty(C.STDIN_FILENO) != 0 + && os.is_atty(C.STDOUT_FILENO) != 0) { return error('not running under a TTY') } @@ -230,7 +231,7 @@ fn (mut ctx Context) termios_loop() { sw.restart() if ctx.cfg.event_fn != voidptr(0) { unsafe { - len := C.read(C.STDIN_FILENO, byteptr(ctx.read_buf.data) + ctx.read_buf.len, + len := C.read(C.STDIN_FILENO, &byte(ctx.read_buf.data) + ctx.read_buf.len, ctx.read_buf.cap - ctx.read_buf.len) ctx.resize_arr(ctx.read_buf.len + len) } diff --git a/vlib/term/ui/ui.v b/vlib/term/ui/ui.v index 70279adb1..6ba3d7c35 100644 --- a/vlib/term/ui/ui.v +++ b/vlib/term/ui/ui.v @@ -23,8 +23,8 @@ const ( esu = '\x1bP=2s\x1b\\' ) -[inline] // write puts the string `s` into the print buffer. +[inline] pub fn (mut ctx Context) write(s string) { if s == '' { return @@ -32,16 +32,16 @@ pub fn (mut ctx Context) write(s string) { unsafe { ctx.print_buf.push_many(s.str, s.len) } } -[inline] // flush displays the accumulated print buffer to the screen. +[inline] pub fn (mut ctx Context) flush() { // TODO: Diff the previous frame against this one, and only render things that changed? if !ctx.enable_su { C.write(1, ctx.print_buf.data, ctx.print_buf.len) } else { - C.write(1, bsu.str, bsu.len) + C.write(1, ui.bsu.str, ui.bsu.len) C.write(1, ctx.print_buf.data, ctx.print_buf.len) - C.write(1, esu.str, esu.len) + C.write(1, ui.esu.str, ui.esu.len) } ctx.print_buf.clear() } @@ -52,14 +52,14 @@ pub fn (mut ctx Context) bold() { ctx.write('\x1b[1m') } -[inline] // set_cursor_position positions the cusor at the given coordinates `x`,`y`. +[inline] pub fn (mut ctx Context) set_cursor_position(x int, y int) { ctx.write('\x1b[$y;${x}H') } -[inline] // show_cursor will make the cursor appear if it is not already visible +[inline] pub fn (mut ctx Context) show_cursor() { ctx.write('\x1b[?25h') } @@ -70,8 +70,8 @@ pub fn (mut ctx Context) hide_cursor() { ctx.write('\x1b[?25l') } -[inline] // set_color sets the current foreground color used by any succeeding `draw_*` calls. +[inline] pub fn (mut ctx Context) set_color(c Color) { if ctx.enable_rgb { ctx.write('\x1b[38;2;${int(c.r)};${int(c.g)};${int(c.b)}m') @@ -80,8 +80,8 @@ pub fn (mut ctx Context) set_color(c Color) { } } -[inline] // set_color sets the current background color used by any succeeding `draw_*` calls. +[inline] pub fn (mut ctx Context) set_bg_color(c Color) { if ctx.enable_rgb { ctx.write('\x1b[48;2;${int(c.r)};${int(c.g)};${int(c.b)}m') @@ -90,20 +90,20 @@ pub fn (mut ctx Context) set_bg_color(c Color) { } } -[inline] // reset_color sets the current foreground color back to it's default value. +[inline] pub fn (mut ctx Context) reset_color() { ctx.write('\x1b[39m') } -[inline] // reset_bg_color sets the current background color back to it's default value. +[inline] pub fn (mut ctx Context) reset_bg_color() { ctx.write('\x1b[49m') } -[inline] // reset restores the state of all colors and text formats back to their default values. +[inline] pub fn (mut ctx Context) reset() { ctx.write('\x1b[0m') } @@ -113,21 +113,21 @@ pub fn (mut ctx Context) clear() { ctx.write('\x1b[2J\x1b[3J') } -[inline] // set_window_title sets the string `s` as the window title. +[inline] pub fn (mut ctx Context) set_window_title(s string) { print('\x1b]0;$s\x07') } -[inline] // draw_point draws a point at position `x`,`y`. +[inline] pub fn (mut ctx Context) draw_point(x int, y int) { ctx.set_cursor_position(x, y) ctx.write(' ') } -[inline] // draw_text draws the string `s`, starting from position `x`,`y`. +[inline] pub fn (mut ctx Context) draw_text(x int, y int, s string) { ctx.set_cursor_position(x, y) ctx.write(s) @@ -248,9 +248,9 @@ pub fn (mut ctx Context) draw_empty_rect(x int, y int, x2 int, y2 int) { ctx.draw_line(x2, y, x2, y2) } -[inline] // horizontal_separator draws a horizontal separator, spanning the width of the screen. +[inline] pub fn (mut ctx Context) horizontal_separator(y int) { ctx.set_cursor_position(0, y) - ctx.write(strings.repeat(/* `⎽` */`-`, ctx.window_width)) + ctx.write(strings.repeat(`-`, ctx.window_width)) // /* `⎽` */ } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5bc0ec5c2..3b3c320db 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -249,15 +249,9 @@ pub mut: pub struct Embed { pub: - typ Type - pos token.Position -} - -pub struct StructEmbedding { -pub: - name string - typ Type - pos token.Position + typ Type + pos token.Position + comments []Comment } pub struct InterfaceEmbedding { diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index f1a1a0714..e5592a1c0 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -660,7 +660,8 @@ static void* g_live_info = NULL; #ifdef _VFREESTANDING #undef _VFREESTANDING #endif -' + c_wyhash +' + + c_wyhash c_builtin_types = ' //================================== builtin types ================================*/ typedef int64_t i64; @@ -734,5 +735,6 @@ static voidptr memfreedup(voidptr ptr, voidptr src, int sz) { return memdup(src, sz); } -' + c_wyhash +' + + c_wyhash ) diff --git a/vlib/v/tests/fn_test.v b/vlib/v/tests/fn_test.v index 25cbea8ba..591f6b15b 100644 --- a/vlib/v/tests/fn_test.v +++ b/vlib/v/tests/fn_test.v @@ -1,5 +1,6 @@ import time // 1 line comment // 1 line comment + /* multi line comment (1) multi line comment (2) @@ -43,11 +44,12 @@ type F6 = fn (int, int) type F7 = fn (time.Time, int) -fn C.atoi(byteptr) int +fn C.atoi(&byte) int fn C.freec(ptr voidptr) [trusted] fn C.exitc(code int) + // above checks attribute doesn't conflict with `freec` return type fn foo() { diff --git a/vlib/x/openssl/declrarations.v b/vlib/x/openssl/declrarations.v new file mode 100644 index 000000000..b2d7d4f2c --- /dev/null +++ b/vlib/x/openssl/declrarations.v @@ -0,0 +1,25 @@ +module openssl + +import time + +enum Select { + read + write + except +} + +pub struct SSLConn { +mut: + sslctx &C.SSL_CTX + ssl &C.SSL + handle int + duration time.Duration +} + +pub fn new_ssl_conn() &SSLConn { + return &SSLConn{ + sslctx: 0 + ssl: 0 + handle: 0 + } +} diff --git a/vlib/x/openssl/openssl.v b/vlib/x/openssl/openssl.v index 67e330499..2bab69614 100644 --- a/vlib/x/openssl/openssl.v +++ b/vlib/x/openssl/openssl.v @@ -1,34 +1,9 @@ module openssl -import net.openssl import net +import net.openssl as nssl import time -// const ( -// is_used = openssl.is_used -// ) -pub struct SSLConn { -mut: - sslctx &C.SSL_CTX - ssl &C.SSL - handle int - duration time.Duration -} - -enum Select { - read - write - except -} - -pub fn new_ssl_conn() &SSLConn { - return &SSLConn{ - sslctx: 0 - ssl: 0 - handle: 0 - } -} - // shutdown closes the ssl connection and do clean up pub fn (mut s SSLConn) shutdown() ? { if s.ssl != 0 { @@ -36,7 +11,7 @@ pub fn (mut s SSLConn) shutdown() ? { for { res = C.SSL_shutdown(voidptr(s.ssl)) if res < 0 { - err_res := openssl.ssl_error(res, s.ssl) or { + err_res := nssl.ssl_error(res, s.ssl) or { break // We break to free rest of resources } if err_res == .ssl_error_want_read { @@ -102,7 +77,7 @@ pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ? { // C.SSL_CTX_set_options(s.sslctx, flags) // mut res := C.SSL_CTX_load_verify_locations(s.sslctx, 'random-org-chain.pem', 0) - s.ssl = unsafe { &C.SSL(C.SSL_new(voidptr(s.sslctx))) } + s.ssl = unsafe { &C.SSL(C.SSL_new(s.sslctx)) } if s.ssl == 0 { return error("Couldn't create OpenSSL instance.") } @@ -124,7 +99,7 @@ pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ? { for { res = C.SSL_connect(voidptr(s.ssl)) if res != 1 { - err_res := openssl.ssl_error(res, s.ssl) ? + err_res := nssl.ssl_error(res, s.ssl) ? if err_res == .ssl_error_want_read { for { ready := @select(s.handle, .read, s.duration) ? @@ -148,12 +123,12 @@ pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ? { } } -pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr byteptr, len int) ?int { +pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &byte, len int) ?int { mut res := 0 for { res = C.SSL_read(voidptr(s.ssl), buf_ptr, len) if res < 0 { - err_res := openssl.ssl_error(res, s.ssl) ? + err_res := nssl.ssl_error(res, s.ssl) ? if err_res == .ssl_error_want_read { for { ready := @select(s.handle, .read, s.duration) ? @@ -181,21 +156,21 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr byteptr, len int) ?int { } pub fn (mut s SSLConn) read_into(mut buffer []byte) ?int { - res := s.socket_read_into_ptr(byteptr(buffer.data), buffer.len) ? + res := s.socket_read_into_ptr(&byte(buffer.data), buffer.len) ? return res } // write number of bytes to SSL connection pub fn (mut s SSLConn) write(bytes []byte) ?int { unsafe { - mut ptr_base := byteptr(bytes.data) + mut ptr_base := &byte(bytes.data) mut total_sent := 0 for total_sent < bytes.len { ptr := ptr_base + total_sent remaining := bytes.len - total_sent mut sent := C.SSL_write(voidptr(s.ssl), ptr, remaining) if sent <= 0 { - err_res := openssl.ssl_error(sent, s.ssl) ? + err_res := nssl.ssl_error(sent, s.ssl) ? if err_res == .ssl_error_want_read { for { ready := @select(s.handle, .read, s.duration) ? diff --git a/vlib/x/ttf/common.v b/vlib/x/ttf/common.v index e3bf827e5..6fec0b76f 100644 --- a/vlib/x/ttf/common.v +++ b/vlib/x/ttf/common.v @@ -62,7 +62,7 @@ fn (mut bmp BitMap) format_texture() { b_a := byte(bmp.bg_color & 0xFF) // trasform buffer in a texture - x := byteptr(bmp.buf) + x := bmp.buf unsafe { mut i := 0 for i < bmp.buf_size { diff --git a/vlib/x/ttf/render_bmp.v b/vlib/x/ttf/render_bmp.v index 6cceff022..c0cf6dc5a 100644 --- a/vlib/x/ttf/render_bmp.v +++ b/vlib/x/ttf/render_bmp.v @@ -20,8 +20,8 @@ import math.mathutil as mu pub struct BitMap { pub mut: tf &TTF_File - buf byteptr // pointer to the memory buffer - buf_size int // allocated buf size in bytes + buf &byte = 0 // pointer to the memory buffer + buf_size int // allocated buf size in bytes width int = 1 // width of the buffer height int = 1 // height of the buffer bp int = 4 // byte per pixel of the buffer diff --git a/vlib/x/ttf/ttf.v b/vlib/x/ttf/ttf.v index 5f88b3865..8f700910e 100644 --- a/vlib/x/ttf/ttf.v +++ b/vlib/x/ttf/ttf.v @@ -540,7 +540,7 @@ fn (mut tf TTF_File) get_fixed() f32 { fn (mut tf TTF_File) get_string(length int) string { tmp_pos := u64(tf.pos) tf.pos += u32(length) - return unsafe { tos(byteptr(u64(tf.buf.data) + tmp_pos), length) } + return unsafe { tos(&byte(u64(tf.buf.data) + tmp_pos), length) } } fn (mut tf TTF_File) get_unicode_string(length int) string { -- 2.30.2