///|
/// Auto-generated core modules for moonbit-eval interpreter
/// This file contains embedded RuntimePackage definitions for all moonbitlang/core modules
///|
let core_modules : Map[String, RuntimePackage] = {
"abort": moonbitlang_core_abort_module,
"moonbitlang/core/abort": moonbitlang_core_abort_module,
"archive/zip": moonbitlang_core_archive_zip_module,
"moonbitlang/core/archive/zip": moonbitlang_core_archive_zip_module,
"argparse": moonbitlang_core_argparse_module,
"moonbitlang/core/argparse": moonbitlang_core_argparse_module,
"array": moonbitlang_core_array_module,
"moonbitlang/core/array": moonbitlang_core_array_module,
"async": moonbitlang_core_async_module,
"moonbitlang/core/async": moonbitlang_core_async_module,
"async/aqueue": moonbitlang_core_async_aqueue_module,
"moonbitlang/core/async/aqueue": moonbitlang_core_async_aqueue_module,
"async/cond_var": moonbitlang_core_async_cond_var_module,
"moonbitlang/core/async/cond_var": moonbitlang_core_async_cond_var_module,
"async/external_loop_integration": moonbitlang_core_async_external_loop_integration_module,
"moonbitlang/core/async/external_loop_integration": moonbitlang_core_async_external_loop_integration_module,
"async/http": moonbitlang_core_async_http_module,
"moonbitlang/core/async/http": moonbitlang_core_async_http_module,
"async/internal/bytes_util": moonbitlang_core_async_internal_bytes_util_module,
"moonbitlang/core/async/internal/bytes_util": moonbitlang_core_async_internal_bytes_util_module,
"async/internal/cancellable_queue": moonbitlang_core_async_internal_cancellable_queue_module,
"moonbitlang/core/async/internal/cancellable_queue": moonbitlang_core_async_internal_cancellable_queue_module,
"async/internal/coroutine": moonbitlang_core_async_internal_coroutine_module,
"moonbitlang/core/async/internal/coroutine": moonbitlang_core_async_internal_coroutine_module,
"async/internal/env_util": moonbitlang_core_async_internal_env_util_module,
"moonbitlang/core/async/internal/env_util": moonbitlang_core_async_internal_env_util_module,
"async/internal/errno_util": moonbitlang_core_async_internal_errno_util_module,
"moonbitlang/core/async/internal/errno_util": moonbitlang_core_async_internal_errno_util_module,
"async/internal/event_loop": moonbitlang_core_async_internal_event_loop_module,
"moonbitlang/core/async/internal/event_loop": moonbitlang_core_async_internal_event_loop_module,
"async/internal/io_buffer": moonbitlang_core_async_internal_io_buffer_module,
"moonbitlang/core/async/internal/io_buffer": moonbitlang_core_async_internal_io_buffer_module,
"async/internal/test_util": moonbitlang_core_async_internal_test_util_module,
"moonbitlang/core/async/internal/test_util": moonbitlang_core_async_internal_test_util_module,
"async/io": moonbitlang_core_async_io_module,
"moonbitlang/core/async/io": moonbitlang_core_async_io_module,
"async/js_async": moonbitlang_core_async_js_async_module,
"moonbitlang/core/async/js_async": moonbitlang_core_async_js_async_module,
"async/os_error": moonbitlang_core_async_os_error_module,
"moonbitlang/core/async/os_error": moonbitlang_core_async_os_error_module,
"async/raw_fd": moonbitlang_core_async_raw_fd_module,
"moonbitlang/core/async/raw_fd": moonbitlang_core_async_raw_fd_module,
"async/raw_fd/internal/raw_fd_test_util": moonbitlang_core_async_raw_fd_internal_raw_fd_test_util_module,
"moonbitlang/core/async/raw_fd/internal/raw_fd_test_util": moonbitlang_core_async_raw_fd_internal_raw_fd_test_util_module,
"async/runtime_internal": moonbitlang_core_async_runtime_internal_module,
"moonbitlang/core/async/runtime_internal": moonbitlang_core_async_runtime_internal_module,
"async/semaphore": moonbitlang_core_async_semaphore_module,
"moonbitlang/core/async/semaphore": moonbitlang_core_async_semaphore_module,
"async/shell": moonbitlang_core_async_shell_module,
"moonbitlang/core/async/shell": moonbitlang_core_async_shell_module,
"async/stdio": moonbitlang_core_async_stdio_module,
"moonbitlang/core/async/stdio": moonbitlang_core_async_stdio_module,
"async/test_programs/cat": moonbitlang_core_async_test_programs_cat_module,
"moonbitlang/core/async/test_programs/cat": moonbitlang_core_async_test_programs_cat_module,
"async/test_programs/concurrent_stdio_write": moonbitlang_core_async_test_programs_concurrent_stdio_write_module,
"moonbitlang/core/async/test_programs/concurrent_stdio_write": moonbitlang_core_async_test_programs_concurrent_stdio_write_module,
"async/test_programs/dump_env": moonbitlang_core_async_test_programs_dump_env_module,
"moonbitlang/core/async/test_programs/dump_env": moonbitlang_core_async_test_programs_dump_env_module,
"async/test_programs/filter": moonbitlang_core_async_test_programs_filter_module,
"moonbitlang/core/async/test_programs/filter": moonbitlang_core_async_test_programs_filter_module,
"async/test_programs/find_ipv6_test_interface": moonbitlang_core_async_test_programs_find_ipv6_test_interface_module,
"moonbitlang/core/async/test_programs/find_ipv6_test_interface": moonbitlang_core_async_test_programs_find_ipv6_test_interface_module,
"async/test_programs/lock_file": moonbitlang_core_async_test_programs_lock_file_module,
"moonbitlang/core/async/test_programs/lock_file": moonbitlang_core_async_test_programs_lock_file_module,
"async/test_programs/pty": moonbitlang_core_async_test_programs_pty_module,
"moonbitlang/core/async/test_programs/pty": moonbitlang_core_async_test_programs_pty_module,
"async/test_programs/setup_sanitizer_check": moonbitlang_core_async_test_programs_setup_sanitizer_check_module,
"moonbitlang/core/async/test_programs/setup_sanitizer_check": moonbitlang_core_async_test_programs_setup_sanitizer_check_module,
"async/test_programs/sleep": moonbitlang_core_async_test_programs_sleep_module,
"moonbitlang/core/async/test_programs/sleep": moonbitlang_core_async_test_programs_sleep_module,
"async/test_programs/spawn": moonbitlang_core_async_test_programs_spawn_module,
"moonbitlang/core/async/test_programs/spawn": moonbitlang_core_async_test_programs_spawn_module,
"async/test_programs/ttyd": moonbitlang_core_async_test_programs_ttyd_module,
"moonbitlang/core/async/test_programs/ttyd": moonbitlang_core_async_test_programs_ttyd_module,
"async/tls": moonbitlang_core_async_tls_module,
"moonbitlang/core/async/tls": moonbitlang_core_async_tls_module,
"async/tty": moonbitlang_core_async_tty_module,
"moonbitlang/core/async/tty": moonbitlang_core_async_tty_module,
"async/types": moonbitlang_core_async_types_module,
"moonbitlang/core/async/types": moonbitlang_core_async_types_module,
"bench": moonbitlang_core_bench_module,
"moonbitlang/core/bench": moonbitlang_core_bench_module,
"bigint": moonbitlang_core_bigint_module,
"moonbitlang/core/bigint": moonbitlang_core_bigint_module,
"buffer": moonbitlang_core_buffer_module,
"moonbitlang/core/buffer": moonbitlang_core_buffer_module,
"builtin": moonbitlang_core_builtin_module,
"moonbitlang/core/builtin": moonbitlang_core_builtin_module,
"byte": moonbitlang_core_byte_module,
"moonbitlang/core/byte": moonbitlang_core_byte_module,
"bytes": moonbitlang_core_bytes_module,
"moonbitlang/core/bytes": moonbitlang_core_bytes_module,
"bytes/internal/regex_engine": moonbitlang_core_bytes_internal_regex_engine_module,
"moonbitlang/core/bytes/internal/regex_engine": moonbitlang_core_bytes_internal_regex_engine_module,
"bytes/internal/regex_engine/ast": moonbitlang_core_bytes_internal_regex_engine_ast_module,
"moonbitlang/core/bytes/internal/regex_engine/ast": moonbitlang_core_bytes_internal_regex_engine_ast_module,
"bytes/internal/regex_engine/shared_types": moonbitlang_core_bytes_internal_regex_engine_shared_types_module,
"moonbitlang/core/bytes/internal/regex_engine/shared_types": moonbitlang_core_bytes_internal_regex_engine_shared_types_module,
"bytes/internal/regex_engine/shared_types/byte_set": moonbitlang_core_bytes_internal_regex_engine_shared_types_byte_set_module,
"moonbitlang/core/bytes/internal/regex_engine/shared_types/byte_set": moonbitlang_core_bytes_internal_regex_engine_shared_types_byte_set_module,
"bytes/internal/regex_engine/symbol_map": moonbitlang_core_bytes_internal_regex_engine_symbol_map_module,
"moonbitlang/core/bytes/internal/regex_engine/symbol_map": moonbitlang_core_bytes_internal_regex_engine_symbol_map_module,
"bytes/internal/regex_parser": moonbitlang_core_bytes_internal_regex_parser_module,
"moonbitlang/core/bytes/internal/regex_parser": moonbitlang_core_bytes_internal_regex_parser_module,
"char": moonbitlang_core_char_module,
"moonbitlang/core/char": moonbitlang_core_char_module,
"cmp": moonbitlang_core_cmp_module,
"moonbitlang/core/cmp": moonbitlang_core_cmp_module,
"compress/flate": moonbitlang_core_compress_flate_module,
"moonbitlang/core/compress/flate": moonbitlang_core_compress_flate_module,
"compress/gzip": moonbitlang_core_compress_gzip_module,
"moonbitlang/core/compress/gzip": moonbitlang_core_compress_gzip_module,
"compress/zlib": moonbitlang_core_compress_zlib_module,
"moonbitlang/core/compress/zlib": moonbitlang_core_compress_zlib_module,
"debug": moonbitlang_core_debug_module,
"moonbitlang/core/debug": moonbitlang_core_debug_module,
"deque": moonbitlang_core_deque_module,
"moonbitlang/core/deque": moonbitlang_core_deque_module,
"diff": moonbitlang_core_diff_module,
"moonbitlang/core/diff": moonbitlang_core_diff_module,
"double": moonbitlang_core_double_module,
"moonbitlang/core/double": moonbitlang_core_double_module,
"encoding/ascii": moonbitlang_core_encoding_ascii_module,
"moonbitlang/core/encoding/ascii": moonbitlang_core_encoding_ascii_module,
"encoding/base64": moonbitlang_core_encoding_base64_module,
"moonbitlang/core/encoding/base64": moonbitlang_core_encoding_base64_module,
"encoding/hex": moonbitlang_core_encoding_hex_module,
"moonbitlang/core/encoding/hex": moonbitlang_core_encoding_hex_module,
"encoding/percent": moonbitlang_core_encoding_percent_module,
"moonbitlang/core/encoding/percent": moonbitlang_core_encoding_percent_module,
"encoding/utf16": moonbitlang_core_encoding_utf16_module,
"moonbitlang/core/encoding/utf16": moonbitlang_core_encoding_utf16_module,
"encoding/utf8": moonbitlang_core_encoding_utf8_module,
"moonbitlang/core/encoding/utf8": moonbitlang_core_encoding_utf8_module,
"env": moonbitlang_core_env_module,
"moonbitlang/core/env": moonbitlang_core_env_module,
"float": moonbitlang_core_float_module,
"moonbitlang/core/float": moonbitlang_core_float_module,
"hashmap": moonbitlang_core_hashmap_module,
"moonbitlang/core/hashmap": moonbitlang_core_hashmap_module,
"hashset": moonbitlang_core_hashset_module,
"moonbitlang/core/hashset": moonbitlang_core_hashset_module,
"immut/hashmap": moonbitlang_core_immut_hashmap_module,
"moonbitlang/core/immut/hashmap": moonbitlang_core_immut_hashmap_module,
"immut/hashset": moonbitlang_core_immut_hashset_module,
"moonbitlang/core/immut/hashset": moonbitlang_core_immut_hashset_module,
"immut/internal/path": moonbitlang_core_immut_internal_path_module,
"moonbitlang/core/immut/internal/path": moonbitlang_core_immut_internal_path_module,
"immut/internal/sparse_array": moonbitlang_core_immut_internal_sparse_array_module,
"moonbitlang/core/immut/internal/sparse_array": moonbitlang_core_immut_internal_sparse_array_module,
"immut/priority_queue": moonbitlang_core_immut_priority_queue_module,
"moonbitlang/core/immut/priority_queue": moonbitlang_core_immut_priority_queue_module,
"immut/sorted_map": moonbitlang_core_immut_sorted_map_module,
"moonbitlang/core/immut/sorted_map": moonbitlang_core_immut_sorted_map_module,
"immut/sorted_set": moonbitlang_core_immut_sorted_set_module,
"moonbitlang/core/immut/sorted_set": moonbitlang_core_immut_sorted_set_module,
"immut/vector": moonbitlang_core_immut_vector_module,
"moonbitlang/core/immut/vector": moonbitlang_core_immut_vector_module,
"immut/vector_map": moonbitlang_core_immut_vector_map_module,
"moonbitlang/core/immut/vector_map": moonbitlang_core_immut_vector_map_module,
"int": moonbitlang_core_int_module,
"moonbitlang/core/int": moonbitlang_core_int_module,
"int16": moonbitlang_core_int16_module,
"moonbitlang/core/int16": moonbitlang_core_int16_module,
"int64": moonbitlang_core_int64_module,
"moonbitlang/core/int64": moonbitlang_core_int64_module,
"internal/checksum": moonbitlang_core_internal_checksum_module,
"moonbitlang/core/internal/checksum": moonbitlang_core_internal_checksum_module,
"internal/edit_distance": moonbitlang_core_internal_edit_distance_module,
"moonbitlang/core/internal/edit_distance": moonbitlang_core_internal_edit_distance_module,
"internal/os_string": moonbitlang_core_internal_os_string_module,
"moonbitlang/core/internal/os_string": moonbitlang_core_internal_os_string_module,
"internal/strconv": moonbitlang_core_internal_strconv_module,
"moonbitlang/core/internal/strconv": moonbitlang_core_internal_strconv_module,
"json": moonbitlang_core_json_module,
"moonbitlang/core/json": moonbitlang_core_json_module,
"lazy": moonbitlang_core_lazy_module,
"moonbitlang/core/lazy": moonbitlang_core_lazy_module,
"lazy_list": moonbitlang_core_lazy_list_module,
"moonbitlang/core/lazy_list": moonbitlang_core_lazy_list_module,
"lexbuf": moonbitlang_core_lexbuf_module,
"moonbitlang/core/lexbuf": moonbitlang_core_lexbuf_module,
"list": moonbitlang_core_list_module,
"moonbitlang/core/list": moonbitlang_core_list_module,
"math": moonbitlang_core_math_module,
"moonbitlang/core/math": moonbitlang_core_math_module,
"option": moonbitlang_core_option_module,
"moonbitlang/core/option": moonbitlang_core_option_module,
"path": moonbitlang_core_path_module,
"moonbitlang/core/path": moonbitlang_core_path_module,
"path/error": moonbitlang_core_path_error_module,
"moonbitlang/core/path/error": moonbitlang_core_path_error_module,
"path/internal/ffi": moonbitlang_core_path_internal_ffi_module,
"moonbitlang/core/path/internal/ffi": moonbitlang_core_path_internal_ffi_module,
"path/internal/posix": moonbitlang_core_path_internal_posix_module,
"moonbitlang/core/path/internal/posix": moonbitlang_core_path_internal_posix_module,
"path/internal/unicode": moonbitlang_core_path_internal_unicode_module,
"moonbitlang/core/path/internal/unicode": moonbitlang_core_path_internal_unicode_module,
"path/internal/win32": moonbitlang_core_path_internal_win32_module,
"moonbitlang/core/path/internal/win32": moonbitlang_core_path_internal_win32_module,
"prelude": moonbitlang_core_prelude_module,
"moonbitlang/core/prelude": moonbitlang_core_prelude_module,
"priority_queue": moonbitlang_core_priority_queue_module,
"moonbitlang/core/priority_queue": moonbitlang_core_priority_queue_module,
"queue": moonbitlang_core_queue_module,
"moonbitlang/core/queue": moonbitlang_core_queue_module,
"quickcheck": moonbitlang_core_quickcheck_module,
"moonbitlang/core/quickcheck": moonbitlang_core_quickcheck_module,
"quickcheck/shrink": moonbitlang_core_quickcheck_shrink_module,
"moonbitlang/core/quickcheck/shrink": moonbitlang_core_quickcheck_shrink_module,
"quickcheck/splitmix": moonbitlang_core_quickcheck_splitmix_module,
"moonbitlang/core/quickcheck/splitmix": moonbitlang_core_quickcheck_splitmix_module,
"random": moonbitlang_core_random_module,
"moonbitlang/core/random": moonbitlang_core_random_module,
"random/internal/random_source": moonbitlang_core_random_internal_random_source_module,
"moonbitlang/core/random/internal/random_source": moonbitlang_core_random_internal_random_source_module,
"range": moonbitlang_core_range_module,
"moonbitlang/core/range": moonbitlang_core_range_module,
"ref": moonbitlang_core_ref_module,
"moonbitlang/core/ref": moonbitlang_core_ref_module,
"result": moonbitlang_core_result_module,
"moonbitlang/core/result": moonbitlang_core_result_module,
"set": moonbitlang_core_set_module,
"moonbitlang/core/set": moonbitlang_core_set_module,
"simd": moonbitlang_core_simd_module,
"moonbitlang/core/simd": moonbitlang_core_simd_module,
"sorted_map": moonbitlang_core_sorted_map_module,
"moonbitlang/core/sorted_map": moonbitlang_core_sorted_map_module,
"sorted_set": moonbitlang_core_sorted_set_module,
"moonbitlang/core/sorted_set": moonbitlang_core_sorted_set_module,
"string": moonbitlang_core_string_module,
"moonbitlang/core/string": moonbitlang_core_string_module,
"string/internal/regex_engine": moonbitlang_core_string_internal_regex_engine_module,
"moonbitlang/core/string/internal/regex_engine": moonbitlang_core_string_internal_regex_engine_module,
"string/internal/regex_engine/ast": moonbitlang_core_string_internal_regex_engine_ast_module,
"moonbitlang/core/string/internal/regex_engine/ast": moonbitlang_core_string_internal_regex_engine_ast_module,
"string/internal/regex_engine/shared_types": moonbitlang_core_string_internal_regex_engine_shared_types_module,
"moonbitlang/core/string/internal/regex_engine/shared_types": moonbitlang_core_string_internal_regex_engine_shared_types_module,
"string/internal/regex_engine/shared_types/rechar_set": moonbitlang_core_string_internal_regex_engine_shared_types_rechar_set_module,
"moonbitlang/core/string/internal/regex_engine/shared_types/rechar_set": moonbitlang_core_string_internal_regex_engine_shared_types_rechar_set_module,
"string/internal/regex_engine/symbol_map": moonbitlang_core_string_internal_regex_engine_symbol_map_module,
"moonbitlang/core/string/internal/regex_engine/symbol_map": moonbitlang_core_string_internal_regex_engine_symbol_map_module,
"string/internal/regex_parser": moonbitlang_core_string_internal_regex_parser_module,
"moonbitlang/core/string/internal/regex_parser": moonbitlang_core_string_internal_regex_parser_module,
"test": moonbitlang_core_test_module,
"moonbitlang/core/test": moonbitlang_core_test_module,
"tuple": moonbitlang_core_tuple_module,
"moonbitlang/core/tuple": moonbitlang_core_tuple_module,
"uint": moonbitlang_core_uint_module,
"moonbitlang/core/uint": moonbitlang_core_uint_module,
"uint16": moonbitlang_core_uint16_module,
"moonbitlang/core/uint16": moonbitlang_core_uint16_module,
"uint64": moonbitlang_core_uint64_module,
"moonbitlang/core/uint64": moonbitlang_core_uint64_module,
"unit": moonbitlang_core_unit_module,
"moonbitlang/core/unit": moonbitlang_core_unit_module,
"v128": moonbitlang_core_v128_module,
"moonbitlang/core/v128": moonbitlang_core_v128_module,
}
///|
fn dummy_loc() -> @basic.Location {
{
start: { fname: "", lnum: 0, bol: 0, cnum: 0, },
end: { fname: "", lnum: 0, bol: 0, cnum: 0, },
}
}
///|
let moonbitlang_core_abort_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/abort",
deps={},
files={
"deprecated.mbt": (
#|#deprecated("Use `abort` from the prelude instead of `@abort.abort`")
#|pub fn[T] abort(msg : String) -> T = "%abort"
),
},
)
///|
let moonbitlang_core_archive_zip_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/archive/zip",
deps={},
files={
"checksum.mbt": (
#|fn crc32_cancellable(
#| bytes : BytesView,
#| cancelled~ : () -> Bool,
#|) -> UInt32 raise ZipError {
#| check_cancelled(cancelled)
#| let crc = @checksum.Crc32()
#| let mut start = 0
#| while start < bytes.length() {
#| if start > 0 {
#| check_cancelled(cancelled)
#| }
#| let end = start + (bytes.length() - start).min(4096)
#| crc.update(bytes[start:end])
#| start = end
#| }
#| check_cancelled(cancelled)
#| crc.finish()
#|}
),
"conversion.mbt": (
#|fn UInt32::to_int_checked(self : UInt32) -> Int32 raise ZipError {
#| guard self <= (0x7FFFFFFF : UInt32) else {
#| raise ZipError(UnsupportedFeature, "zip: u32 exceeds the Int range")
#| }
#| self.reinterpret_as_int()
#|}
#|fn UInt64::to_int_checked(self : UInt64) -> Int32 raise ZipError {
#| guard self <= (0x7FFFFFFF : UInt64) else {
#| raise ZipError(UnsupportedFeature, "zip: u64 exceeds the Int range")
#| }
#| self.to_int()
#|}
#|fn Int32::to_uint_checked(self : Int32) -> UInt32 raise ZipError {
#| guard self >= 0 else {
#| raise ZipError(UnsupportedFeature, "zip: negative u32 value")
#| }
#| self.reinterpret_as_uint()
#|}
#|fn Int32::to_uint64_checked(self : Int32) -> UInt64 raise ZipError {
#| guard self >= 0 else {
#| raise ZipError(UnsupportedFeature, "zip: negative u64 value")
#| }
#| self.reinterpret_as_uint().to_uint64()
#|}
),
"directory.mbt": (
#|#valtype
#|struct DirectoryLimits {
#| max_directory_bytes : UInt64
#| max_entries : UInt64
#|}
#|pub fn DirectoryLimits::DirectoryLimits(
#| max_directory_bytes~ : UInt64 = 0xffff_ffff_ffff_ffffUL,
#| max_entries~ : UInt64 = 0x7fff_ffffUL,
#|) -> DirectoryLimits {
#| { max_directory_bytes, max_entries, }
#|}
#|struct Directory {
#| entries : Array[DirectoryEntry]
#| comment : Bytes
#| central_offset : UInt64
#|}
#|pub fn Directory::entries(self : Directory) -> ArrayView[DirectoryEntry] {
#| self.entries
#|}
#|pub fn Directory::comment(self : Directory) -> BytesView {
#| self.comment
#|}
#|struct DirectoryEntry {
#| name : String
#| compressed_size : UInt64
#| uncompressed_size : UInt64
#| local_offset : UInt64
#| method_id : Int32
#| flags : Int32
#| crc : UInt32
#| creator_system : Int32
#| external_attributes : UInt32
#| dos_date : Int32
#| dos_time : Int32
#|}
#|pub fn DirectoryEntry::name(self : DirectoryEntry) -> String {
#| self.name
#|}
#|pub fn DirectoryEntry::compressed_size(self : DirectoryEntry) -> UInt64 {
#| self.compressed_size
#|}
#|pub fn DirectoryEntry::uncompressed_size(self : DirectoryEntry) -> UInt64 {
#| self.uncompressed_size
#|}
#|pub fn DirectoryEntry::method_id(self : DirectoryEntry) -> Int32 {
#| self.method_id
#|}
#|pub fn DirectoryEntry::crc32(self : DirectoryEntry) -> UInt32 {
#| self.crc
#|}
#|pub fn DirectoryEntry::is_directory(self : DirectoryEntry) -> Bool {
#| self.name.has_suffix("/") ||
#| (self.external_attributes & 0x10U) != 0 ||
#| (
#| (self.creator_system == 3 || self.creator_system == 19) &&
#| ((self.external_attributes >> 16) & 0xf000U) == 0x4000U
#| )
#|}
#|pub fn DirectoryEntry::is_encrypted(self : DirectoryEntry) -> Bool {
#| (self.flags & FLAG_ENCRYPTED) != 0
#|}
#|pub fn DirectoryEntry::creator_system(self : DirectoryEntry) -> Int32 {
#| self.creator_system
#|}
#|pub fn DirectoryEntry::external_attributes(self : DirectoryEntry) -> UInt32 {
#| self.external_attributes
#|}
#|pub fn DirectoryEntry::dos_date(self : DirectoryEntry) -> Int32 {
#| self.dos_date
#|}
#|pub fn DirectoryEntry::dos_time(self : DirectoryEntry) -> Int32 {
#| self.dos_time
#|}
#|struct ReadRange {
#| offset : UInt64
#| length : Int32
#|}
#|pub fn ReadRange::offset(self : ReadRange) -> UInt64 {
#| self.offset
#|}
#|pub fn ReadRange::length(self : ReadRange) -> Int32 {
#| self.length
#|}
#|pub(all) enum DirectoryStep {
#| NeedRead(ReadRange)
#| Done(Directory)
#|}
#|priv struct ParsedDirectoryRecord {
#| entry : DirectoryEntry
#| name_len : Int32
#| central_zip64_offset_position : Int32?
#|}
#|fn parse_directory_record(
#| reader : Cursor,
#|) -> ParsedDirectoryRecord raise ZipError {
#| let record = reader.bytes[reader.pos:]
#| guard record
#| is [
#| u32le(CENTRAL_HEADER_SIG),
#| u16le(version_made_by),
#| u16le(_),
#| u16le(flags),
#| u16le(method_id),
#| u16le(dos_time),
#| u16le(dos_date),
#| u32le(crc),
#| u32le(comp_size_raw),
#| u32le(uncomp_size_raw),
#| u16le(name_len),
#| u16le(extra_len),
#| u16le(comment_len),
#| u16le(disk),
#| u16le(_),
#| u32le(external_attributes),
#| u32le(local_offset_raw),
#| ..,
#| ] else {
#| raise record_error(
#| record,
#| CENTRAL_HEADER_SIG,
#| "zip: invalid central directory signature",
#| )
#| }
#| reader.pos = reader.pos + 46
#| let name_len = name_len.reinterpret_as_int()
#| let name_bytes = reader.read_bytes(name_len)
#| let extra_bytes = reader.read_bytes(extra_len.reinterpret_as_int())
#| let name = decode_name(name_bytes)
#| reader.skip(comment_len.reinterpret_as_int())
#| guard disk == 0 || disk == 0xffff else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| let need_uncomp = uncomp_size_raw == MAX_U32
#| let need_comp = comp_size_raw == MAX_U32
#| let need_offset = local_offset_raw == MAX_U32
#| let (zip_uncomp, zip_comp, zip_offset, offset_position) = read_zip64_extra(
#| extra_bytes,
#| need_uncomp,
#| need_comp,
#| need_offset,
#| need_disk=disk == 0xffff,
#| )
#| {
#| entry: {
#| name,
#| compressed_size: resolve_zip64_field(need_comp, zip_comp, comp_size_raw),
#| uncompressed_size: resolve_zip64_field(
#| need_uncomp, zip_uncomp, uncomp_size_raw,
#| ),
#| local_offset: resolve_zip64_field(
#| need_offset, zip_offset, local_offset_raw,
#| ),
#| method_id: method_id.reinterpret_as_int(),
#| flags: flags.reinterpret_as_int(),
#| crc,
#| creator_system: (version_made_by >> 8).reinterpret_as_int(),
#| external_attributes,
#| dos_date: dos_date.reinterpret_as_int(),
#| dos_time: dos_time.reinterpret_as_int(),
#| },
#| name_len,
#| central_zip64_offset_position: offset_position.map(position => {
#| 46 + name_len + position
#| }),
#| }
#|}
),
"directory_reader.mbt": (
#|const DIRECTORY_TAIL_SIZE : Int32 = 65557
#|const DIRECTORY_CHUNK_SIZE : Int32 = 65536
#|priv struct ClassicDirectoryEnd {
#| offset : UInt64
#| disk_entries : Int32
#| entries : Int32
#| central_size : UInt32
#| central_offset : UInt32
#| comment : Bytes
#|}
#|priv struct DirectoryBounds {
#| offset : UInt64
#| size : UInt64
#| entries : UInt64
#| comment : Bytes
#|}
#|priv enum DirectoryPhase {
#| Initial
#| Tail(ReadRange)
#| Locator(ClassicDirectoryEnd)
#| Zip64(ClassicDirectoryEnd, UInt64)
#| Records(DirectoryBounds, UInt64)
#| Complete(Directory)
#| Failed(ZipErrorKind, String)
#|}
#|struct DirectoryReader {
#| archive_size : UInt64
#| limits : DirectoryLimits
#| mut phase : DirectoryPhase
#| mut entries : Array[DirectoryEntry]
#| mut pending : Bytes
#|}
#|pub fn DirectoryReader::DirectoryReader(
#| archive_size : UInt64,
#| limits~ : DirectoryLimits = DirectoryLimits(),
#|) -> DirectoryReader {
#| { archive_size, limits, phase: Initial, entries: [], pending: b"", }
#|}
#|pub fn DirectoryReader::advance(
#| self : DirectoryReader,
#| input : BytesView,
#|) -> DirectoryStep raise ZipError {
#| if self.phase is Failed(kind, message) {
#| raise ZipError(kind, message)
#| }
#| self.advance_active(input) catch {
#| ZipError(kind, message) => {
#| self.phase = Failed(kind, message)
#| self.entries = []
#| self.pending = b""
#| raise ZipError(kind, message)
#| }
#| }
#|}
#|fn expect_range_input(input : BytesView, length : Int32) -> Unit raise ZipError {
#| guard input.length() == length else {
#| raise ZipError(
#| InvalidRead(expected=length, actual=input.length()),
#| "zip: response length differs from requested range",
#| )
#| }
#|}
#|fn range_read_length(remaining : UInt64, maximum : Int32) -> Int32 {
#| if remaining < maximum.to_uint64() {
#| remaining.to_int()
#| } else {
#| maximum
#| }
#|}
#|fn check_directory_limit(
#| kind : LimitKind,
#| limit : UInt64,
#| actual : UInt64,
#|) -> Unit raise ZipError {
#| guard actual <= limit else {
#| raise ZipError(
#| LimitExceeded(kind~, limit~, observed=actual),
#| "zip: directory limit exceeded",
#| )
#| }
#|}
#|fn DirectoryReader::advance_active(
#| self : DirectoryReader,
#| input : BytesView,
#|) -> DirectoryStep raise ZipError {
#| match self.phase {
#| Initial => {
#| expect_range_input(input, 0)
#| guard self.archive_size >= 22UL else {
#| raise ZipError(
#| MissingEndOfCentral,
#| "zip: end of central directory not found",
#| )
#| }
#| let length = range_read_length(self.archive_size, DIRECTORY_TAIL_SIZE)
#| let range = { offset: self.archive_size - length.to_uint64(), length, }
#| self.phase = Tail(range)
#| NeedRead(range)
#| }
#| Tail(range) => {
#| expect_range_input(input, range.length)
#| guard find_end_of_central(input) is Some(record) else {
#| raise ZipError(
#| MissingEndOfCentral,
#| "zip: end of central directory not found",
#| )
#| }
#| guard record.disk == 0 && record.central_disk == 0 else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| let { disk_entries, entries, central_size, central_offset, .. } = record
#| let classic = {
#| offset: range.offset + record.offset.to_uint64(),
#| disk_entries,
#| entries,
#| central_size,
#| central_offset,
#| comment: record.comment.to_owned(),
#| }
#| guard !record.has_zip64(classic.offset) else {
#| guard classic.offset >= 20UL else {
#| raise ZipError(Truncated, "zip: zip64 locator out of bounds")
#| }
#| self.phase = Locator(classic)
#| return NeedRead({ offset: classic.offset - 20UL, length: 20, })
#| }
#| guard disk_entries == entries else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| self.begin_records(
#| central_offset.to_uint64(),
#| central_size.to_uint64(),
#| entries.to_uint64(),
#| classic.offset,
#| classic.comment,
#| )
#| }
#| Locator(classic) => {
#| expect_range_input(input, 20)
#| guard input
#| is [u32le(ZIP64_LOCATOR_SIG), u32le(disk), u64le(offset), u32le(disks)] else {
#| raise ZipError(InvalidSignature, "zip: invalid zip64 locator signature")
#| }
#| guard disk == 0 && disks == 1 else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| let locator = classic.offset - 20UL
#| guard offset <= locator && 56UL <= locator - offset else {
#| raise ZipError(Truncated, "zip: zip64 end record out of bounds")
#| }
#| self.phase = Zip64(classic, offset)
#| NeedRead({ offset, length: 56, })
#| }
#| Zip64(classic, offset) => {
#| expect_range_input(input, 56)
#| guard input
#| is [
#| u32le(ZIP64_EOCD_SIG),
#| u64le(record_size),
#| u16le(_),
#| u16le(_),
#| u32le(disk),
#| u32le(central_disk),
#| u64le(disk_entries),
#| u64le(entries),
#| u64le(central_size),
#| u64le(central_offset),
#| ] else {
#| raise ZipError(
#| InvalidSignature,
#| "zip: invalid zip64 end record signature",
#| )
#| }
#| guard record_size == classic.offset - 20UL - offset - 12UL else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: zip64 end record size mismatch",
#| )
#| }
#| guard disk == 0 && central_disk == 0 && disk_entries == entries else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| guard (
#| classic.disk_entries == 0xffff ||
#| classic.disk_entries.to_uint64() == entries
#| ) &&
#| (classic.entries == 0xffff || classic.entries.to_uint64() == entries) &&
#| (
#| classic.central_size == MAX_U32 ||
#| classic.central_size.to_uint64() == central_size
#| ) &&
#| (
#| classic.central_offset == MAX_U32 ||
#| classic.central_offset.to_uint64() == central_offset
#| ) else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: classic and zip64 directory fields differ",
#| )
#| }
#| self.begin_records(
#| central_offset,
#| central_size,
#| entries,
#| offset,
#| classic.comment,
#| )
#| }
#| Records(bounds, offset) => {
#| let end = bounds.offset + bounds.size
#| let length = range_read_length(end - offset, DIRECTORY_CHUNK_SIZE)
#| expect_range_input(input, length)
#| let bytes : BytesView = if self.pending.length() == 0 {
#| input
#| } else {
#| self.pending + input.to_owned()
#| }
#| let mut position = 0
#| while self.entries.length().to_uint64() < bounds.entries &&
#| bytes.length() - position >= 46 &&
#| bytes[position + 28:]
#| is [u16le(name_size), u16le(extra_size), u16le(comment_size), ..] {
#| let record_size = 46 +
#| (name_size + extra_size + comment_size).reinterpret_as_int()
#| guard record_size <= bytes.length() - position else { break }
#| let parsed = parse_directory_record(
#| Cursor(bytes[position:position + record_size], 0),
#| )
#| let entry = parsed.entry
#| guard entry.local_offset <= bounds.offset &&
#| 30UL <= bounds.offset - entry.local_offset &&
#| entry.compressed_size <= bounds.offset - entry.local_offset - 30UL else {
#| raise ZipError(
#| Truncated,
#| "zip: local record or payload out of bounds",
#| )
#| }
#| self.entries.push(entry)
#| position += record_size
#| }
#| let next = offset + length.to_uint64()
#| let entries_done = self.entries.length().to_uint64() == bounds.entries
#| if entries_done {
#| validate_directory_signature(
#| bytes[position:],
#| end - next + (bytes.length() - position).to_uint64(),
#| )
#| }
#| if next == end {
#| guard entries_done else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: central directory size/count mismatch",
#| )
#| }
#| self.complete(bounds.comment, bounds.offset)
#| } else {
#| self.pending = copy_zip_input(bytes[position:])
#| self.phase = Records(bounds, next)
#| NeedRead({
#| offset: next,
#| length: range_read_length(end - next, DIRECTORY_CHUNK_SIZE),
#| })
#| }
#| }
#| Complete(directory) => {
#| expect_range_input(input, 0)
#| Done(directory)
#| }
#| Failed(kind, message) => raise ZipError(kind, message)
#| }
#|}
#|fn validate_directory_signature(
#| bytes : BytesView,
#| remaining : UInt64,
#|) -> Unit raise ZipError {
#| guard remaining != 0UL else { return }
#| guard remaining >= 6UL && remaining <= 65541UL else {
#| raise ZipError(UnsupportedFeature, "zip: invalid directory signature size")
#| }
#| if bytes is [u32le(signature), ..] {
#| guard signature == DIRECTORY_SIGNATURE_SIG else {
#| raise ZipError(InvalidSignature, "zip: invalid directory signature")
#| }
#| }
#| if bytes is [u32le(_), u16le(size), ..] {
#| guard 6UL + size.to_uint64() == remaining else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: directory signature size mismatch",
#| )
#| }
#| }
#|}
#|fn DirectoryReader::begin_records(
#| self : DirectoryReader,
#| offset : UInt64,
#| size : UInt64,
#| entries : UInt64,
#| boundary : UInt64,
#| comment : Bytes,
#|) -> DirectoryStep raise ZipError {
#| guard offset <= boundary && size == boundary - offset else {
#| raise ZipError(Truncated, "zip: central directory out of bounds")
#| }
#| check_directory_limit(DirectoryBytes, self.limits.max_directory_bytes, size)
#| let entry_limit = if self.limits.max_entries < 0x7fff_ffffUL {
#| self.limits.max_entries
#| } else {
#| 0x7fff_ffffUL
#| }
#| check_directory_limit(Entries, entry_limit, entries)
#| guard entries <= size / 46UL else {
#| raise ZipError(
#| Truncated,
#| "zip: central directory entry count exceeds its size",
#| )
#| }
#| guard size != 0UL else { return self.complete(comment, offset) }
#| let bounds = { offset, size, entries, comment, }
#| self.phase = Records(bounds, offset)
#| NeedRead({ offset, length: range_read_length(size, DIRECTORY_CHUNK_SIZE), })
#|}
#|fn DirectoryReader::complete(
#| self : DirectoryReader,
#| comment : Bytes,
#| central_offset : UInt64,
#|) -> DirectoryStep {
#| let directory = { entries: self.entries, comment, central_offset, }
#| self.entries = []
#| self.pending = b""
#| self.phase = Complete(directory)
#| Done(directory)
#|}
),
"entry_reader.mbt": (
#|#valtype
#|struct EntryReadLimits {
#| max_output_bytes : UInt64
#|}
#|pub fn EntryReadLimits::EntryReadLimits(
#| max_output_bytes~ : UInt64 = 0xffff_ffff_ffff_ffffUL,
#|) -> EntryReadLimits {
#| { max_output_bytes, }
#|}
#|pub(all) enum EntryStep {
#| NeedRead(ReadRange)
#| NeedMoreOutput
#| Done
#|}
#|priv struct EntryLocalHeader {
#| crc : UInt32
#| compressed_size : UInt32
#| uncompressed_size : UInt32
#| name_length : Int32
#| extra_length : Int32
#|}
#|priv enum EntryPhase {
#| Initial
#| Header
#| Name(EntryLocalHeader)
#| Extra(EntryLocalHeader)
#| PayloadInput(Int32)
#| Payload
#| Descriptor(Int32)
#| Complete
#| Failed(ZipErrorKind, String)
#|}
#|struct EntryReader {
#| entry : DirectoryEntry
#| central_offset : UInt64
#| max_output : UInt64
#| crc : @checksum.Crc32
#| mut inflater : @flate.IncrementalDecoder?
#| mut phase : EntryPhase
#| mut payload_offset : UInt64
#| mut descriptor_zip64 : Bool
#| mut requested : UInt64
#| mut consumed : UInt64
#| mut produced : UInt64
#| mut step_produced : Int32
#| mut pending : Bytes
#| mut pending_pos : Int32
#|}
#|pub fn Directory::open_entry(
#| self : Directory,
#| index : Int32,
#| limits~ : EntryReadLimits = EntryReadLimits(),
#|) -> EntryReader raise ZipError {
#| guard index >= 0 && index < self.entries.length() else {
#| raise ZipError(
#| InvalidEntryIndex(index),
#| "zip: directory entry index out of bounds",
#| )
#| }
#| let entry = self.entries[index]
#| ensure_flags_supported(entry.flags)
#| let compression = method_from_code(entry.method_id)
#| guard entry.uncompressed_size <= limits.max_output_bytes else {
#| raise ZipError(
#| LimitExceeded(
#| kind=EntryUncompressedBytes,
#| limit=limits.max_output_bytes,
#| observed=entry.uncompressed_size,
#| ),
#| "zip: entry exceeds its decoded-output limit",
#| )
#| }
#| if compression is Store {
#| guard entry.compressed_size == entry.uncompressed_size else {
#| raise ZipError(SizeMismatch, "zip: stored entry sizes differ")
#| }
#| }
#| {
#| entry,
#| central_offset: self.central_offset,
#| max_output: limits.max_output_bytes,
#| crc: Crc32(),
#| inflater: if compression is Deflate {
#| Some(IncrementalDecoder())
#| } else {
#| None
#| },
#| phase: Initial,
#| payload_offset: 0UL,
#| descriptor_zip64: false,
#| requested: 0UL,
#| consumed: 0UL,
#| produced: 0UL,
#| step_produced: 0,
#| pending: b"",
#| pending_pos: 0,
#| }
#|}
#|pub fn EntryReader::step(
#| self : EntryReader,
#| input : BytesView,
#| output : MutArrayView[Byte],
#|) -> EntryStep raise ZipError {
#| self.step_produced = 0
#| if self.phase is Failed(kind, message) {
#| raise ZipError(kind, message)
#| }
#| self.step_active(input, output) catch {
#| ZipError(kind, message) => {
#| self.phase = Failed(kind, message)
#| self.step_produced = 0
#| self.pending = b""
#| self.inflater = None
#| raise ZipError(kind, message)
#| }
#| }
#|}
#|pub fn EntryReader::last_produced(self : EntryReader) -> Int32 {
#| self.step_produced
#|}
#|pub fn EntryReader::total_consumed(self : EntryReader) -> UInt64 {
#| self.consumed
#|}
#|pub fn EntryReader::total_produced(self : EntryReader) -> UInt64 {
#| self.produced
#|}
#|fn EntryReader::step_active(
#| self : EntryReader,
#| input : BytesView,
#| output : MutArrayView[Byte],
#|) -> EntryStep raise ZipError {
#| match self.phase {
#| Initial => {
#| expect_range_input(input, 0)
#| self.phase = Header
#| NeedRead({ offset: self.entry.local_offset, length: 30, })
#| }
#| Header => self.read_header(input)
#| Name(header) => {
#| expect_range_input(input, header.name_length)
#| self.read_name(header, input)
#| }
#| Extra(header) => {
#| expect_range_input(input, header.extra_length)
#| self.read_extra(header, input)
#| }
#| PayloadInput(length) => {
#| expect_range_input(input, length)
#| self.pending = copy_zip_input(input)
#| self.pending_pos = 0
#| self.requested += length.to_uint64()
#| self.phase = Payload
#| self.read_payload(output)
#| }
#| Payload => {
#| expect_range_input(input, 0)
#| self.read_payload(output)
#| }
#| Descriptor(length) => {
#| expect_range_input(input, length)
#| ignore(
#| checked_data_descriptor_size(
#| input,
#| self.entry.crc,
#| self.entry.compressed_size,
#| self.entry.uncompressed_size,
#| self.descriptor_zip64,
#| ),
#| )
#| self.phase = Complete
#| Done
#| }
#| Complete => {
#| expect_range_input(input, 0)
#| Done
#| }
#| Failed(kind, message) => raise ZipError(kind, message)
#| }
#|}
#|fn EntryReader::read_header(
#| self : EntryReader,
#| input : BytesView,
#|) -> EntryStep raise ZipError {
#| expect_range_input(input, 30)
#| guard input
#| is [
#| u32le(LOCAL_HEADER_SIG),
#| u16le(_),
#| u16le(flags),
#| u16le(method_id),
#| u16le(_),
#| u16le(_),
#| u32le(crc),
#| u32le(compressed_size),
#| u32le(uncompressed_size),
#| u16le(name_length),
#| u16le(extra_length),
#| ] else {
#| raise ZipError(InvalidSignature, "zip: invalid local header signature")
#| }
#| let flags = flags.reinterpret_as_int()
#| ensure_flags_supported(flags)
#| guard flags == self.entry.flags else {
#| raise ZipError(UnsupportedFeature, "zip: local and central flags differ")
#| }
#| guard method_id.reinterpret_as_int() == self.entry.method_id else {
#| raise ZipError(UnsupportedFeature, "zip: local and central methods differ")
#| }
#| let header = {
#| crc,
#| compressed_size,
#| uncompressed_size,
#| name_length: name_length.reinterpret_as_int(),
#| extra_length: extra_length.reinterpret_as_int(),
#| }
#| let header_size = 30UL +
#| header.name_length.to_uint64() +
#| header.extra_length.to_uint64()
#| guard header_size <= self.central_offset - self.entry.local_offset &&
#| self.entry.compressed_size <=
#| self.central_offset - self.entry.local_offset - header_size else {
#| raise ZipError(Truncated, "zip: local header or payload out of bounds")
#| }
#| self.payload_offset = self.entry.local_offset + header_size
#| guard header.name_length != 0 else { return self.read_name(header, b"") }
#| self.phase = Name(header)
#| NeedRead({
#| offset: self.entry.local_offset + 30UL,
#| length: header.name_length,
#| })
#|}
#|fn EntryReader::read_name(
#| self : EntryReader,
#| header : EntryLocalHeader,
#| input : BytesView,
#|) -> EntryStep raise ZipError {
#| guard decode_name(input) == self.entry.name else {
#| raise ZipError(UnsupportedFeature, "zip: local and central names differ")
#| }
#| guard header.extra_length != 0 else { return self.read_extra(header, b"") }
#| self.phase = Extra(header)
#| NeedRead({
#| offset: self.payload_offset - header.extra_length.to_uint64(),
#| length: header.extra_length,
#| })
#|}
#|fn EntryReader::read_extra(
#| self : EntryReader,
#| header : EntryLocalHeader,
#| input : BytesView,
#|) -> EntryStep raise ZipError {
#| let descriptor = (self.entry.flags & FLAG_DATA_DESCRIPTOR) != 0
#| let need_uncomp = header.uncompressed_size == MAX_U32
#| let need_comp = header.compressed_size == MAX_U32
#| let (zip_uncomp, zip_comp, _, _) = read_zip64_extra(
#| input, need_uncomp, need_comp, false,
#| )
#| guard header.crc == self.entry.crc || (descriptor && header.crc == 0U) else {
#| raise ZipError(UnsupportedFeature, "zip: local and central CRC-32 differ")
#| }
#| guard local_size_matches(
#| header.compressed_size,
#| zip_comp,
#| self.entry.compressed_size,
#| descriptor,
#| ) &&
#| local_size_matches(
#| header.uncompressed_size,
#| zip_uncomp,
#| self.entry.uncompressed_size,
#| descriptor,
#| ) else {
#| raise ZipError(SizeMismatch, "zip: local and central sizes differ")
#| }
#| self.descriptor_zip64 = need_uncomp ||
#| need_comp ||
#| self.entry.compressed_size >= 0xffff_ffffUL ||
#| self.entry.uncompressed_size >= 0xffff_ffffUL
#| guard self.entry.compressed_size != 0UL else {
#| guard self.entry.uncompressed_size == 0UL else {
#| raise ZipError(Truncated, "zip: empty DEFLATE payload")
#| }
#| return self.finish_payload()
#| }
#| self.request_payload()
#|}
#|fn EntryReader::request_payload(self : EntryReader) -> EntryStep {
#| let length = range_read_length(
#| self.entry.compressed_size - self.requested,
#| 65536,
#| )
#| self.pending = b""
#| self.pending_pos = 0
#| self.phase = PayloadInput(length)
#| NeedRead({ offset: self.payload_offset + self.requested, length, })
#|}
#|fn EntryReader::record_output(
#| self : EntryReader,
#| output : MutArrayView[Byte],
#| produced : Int32,
#| consumed : Int32,
#|) -> Unit raise ZipError {
#| guard produced.to_uint64() <= 0xffff_ffff_ffff_ffffUL - self.produced else {
#| raise ZipError(SizeMismatch, "zip: decoded size exceeds UInt64 range")
#| }
#| let actual = self.produced + produced.to_uint64()
#| guard actual <= self.max_output else {
#| raise ZipError(
#| LimitExceeded(
#| kind=EntryUncompressedBytes,
#| limit=self.max_output,
#| observed=actual,
#| ),
#| "zip: entry exceeds its decoded-output limit",
#| )
#| }
#| guard actual <= self.entry.uncompressed_size else {
#| raise ZipError(
#| SizeMismatch,
#| "zip: decoded size exceeds central directory size",
#| )
#| }
#| let start = output.start_offset()
#| self.crc.update(
#| borrowed_zip_buffer_bytes(zip_output_storage(output))[start:start + produced],
#| )
#| self.produced = actual
#| self.consumed += consumed.to_uint64()
#| self.pending_pos += consumed
#| self.step_produced = produced
#|}
#|fn EntryReader::read_payload(
#| self : EntryReader,
#| output : MutArrayView[Byte],
#|) -> EntryStep raise ZipError {
#| guard output.length() != 0 else { return NeedMoreOutput }
#| if self.inflater is Some(inflater) {
#| let status_result = inflater.step(self.pending[self.pending_pos:], output) catch {
#| error => raise wrap_inflate_error(error)
#| }
#| let status = status_result.status
#| if status is NeedMoreInput && self.requested == self.entry.compressed_size {
#| raise ZipError(Truncated, "zip: truncated DEFLATE payload")
#| }
#| self.record_output(output, status_result.produced, status_result.consumed)
#| match status {
#| Done => {
#| guard self.consumed == self.entry.compressed_size else {
#| raise ZipError(
#| InvalidDeflate,
#| "zip: trailing bytes in compressed payload",
#| )
#| }
#| self.finish_payload()
#| }
#| NeedMoreOutput => NeedMoreOutput
#| NeedMoreInput => self.request_payload()
#| }
#| } else {
#| let count = (self.pending.length() - self.pending_pos).min(output.length())
#| zip_output_storage(output).blit_from_bytesview(
#| output.start_offset(),
#| self.pending[self.pending_pos:self.pending_pos + count],
#| )
#| self.record_output(output, count, count)
#| guard self.pending_pos == self.pending.length() else {
#| return NeedMoreOutput
#| }
#| if self.requested == self.entry.compressed_size {
#| self.finish_payload()
#| } else {
#| self.request_payload()
#| }
#| }
#|}
#|fn EntryReader::finish_payload(self : EntryReader) -> EntryStep raise ZipError {
#| guard self.produced == self.entry.uncompressed_size else {
#| raise ZipError(
#| SizeMismatch,
#| "zip: decoded size differs from central directory size",
#| )
#| }
#| guard self.crc.finish() == self.entry.crc else {
#| raise ZipError(ChecksumMismatch, "zip: CRC-32 mismatch")
#| }
#| self.pending = b""
#| self.inflater = None
#| guard (self.entry.flags & FLAG_DATA_DESCRIPTOR) != 0 else {
#| self.phase = Complete
#| return Done
#| }
#| let offset = self.payload_offset + self.entry.compressed_size
#| let minimum = if self.descriptor_zip64 { 20 } else { 12 }
#| guard minimum.to_uint64() <= self.central_offset - offset else {
#| raise ZipError(Truncated, "zip: data descriptor out of bounds")
#| }
#| let length = range_read_length(self.central_offset - offset, minimum + 4)
#| self.phase = Descriptor(length)
#| NeedRead({ offset, length, })
#|}
),
"errors.mbt": (
#|#derive(Eq, @debug.Debug)
#|pub(all) enum LimitKind {
#| PackageBytes
#| Entries
#| EntryUncompressedBytes
#| TotalUncompressedBytes
#| DirectoryBytes
#| OutputBytes
#|}
#|#derive(Eq, @debug.Debug)
#|pub(all) enum ZipErrorKind {
#| Truncated
#| InvalidSignature
#| MissingEndOfCentral
#| UnsupportedCompression(Int32)
#| InvalidUtf8
#| UnsupportedFeature
#| InvalidDeflate
#| InvalidRead(expected~ : Int32, actual~ : Int32)
#| InvalidEntryIndex(Int32)
#| SizeMismatch
#| ChecksumMismatch
#| LimitExceeded(kind~ : LimitKind, limit~ : UInt64, observed~ : UInt64)
#| Cancelled
#| InvalidArgument
#| InvalidState
#|}
#|#derive(@debug.Debug)
#|pub suberror ZipError {
#| ZipError(ZipErrorKind, String)
#|}
#|pub impl Show for ZipError with fn output(self, logger) {
#| let ZipError(kind, message) = self
#| match kind {
#| LimitExceeded(kind~, limit~, observed~) =>
#| logger <+
#| $|LimitExceeded(kind=\{@debug.to_string(kind)}, limit=\{limit}, observed=\{observed})
#| _ => logger.write_string(@debug.to_string(kind))
#| }
#| logger <+ ": \{message}"
#|}
#|fn check_cancelled(cancelled : () -> Bool) -> Unit raise ZipError {
#| if cancelled() {
#| raise ZipError(Cancelled, "zip: cancelled")
#| }
#|}
#|fn wrap_inflate_error(err : @flate.InflateError) -> ZipError {
#| let InflateError(kind, message) = err
#| match kind {
#| Cancelled => ZipError(Cancelled, message)
#| Truncated => ZipError(Truncated, message)
#| _ => ZipError(InvalidDeflate, message)
#| }
#|}
),
"extends.mbt": (
#|pub extend LimitKind with Eq::{equal}
#|pub extend ZipErrorKind with Eq::{equal}
#|pub extend Compression with Eq::{equal}
#|pub extend ZipError with Show::{to_string}
),
"owned_buffer.mbt": (
#|fn owned_zip_buffer_to_bytes(buffer : FixedArray[Byte]) -> Bytes = "%identity"
#|fn borrowed_zip_buffer_bytes(buffer : FixedArray[Byte]) -> Bytes = "%identity"
#|fn zip_output_storage(output : MutArrayView[Byte]) -> FixedArray[Byte] = "%arrayview.buf"
),
"reader.mbt": (
#|priv struct MemoryEntryMeta {
#| name : String
#| name_len : Int32
#| flags : Int32
#| method_id : Int32
#| crc : UInt32
#| dos_date : Int32
#| dos_time : Int32
#| creator_system : Int32
#| external_attributes : UInt32
#| compressed_size : Int32
#| uncompressed_size : Int32
#| local_offset : Int32
#| central_record : BytesView
#| central_zip64_offset_position : Int32?
#| data_descriptor : Bool
#|}
#|priv struct LocalRecord {
#| compressed : BytesView
#| end : Int32
#|}
#|fn read_local_record(
#| bytes : BytesView,
#| meta : MemoryEntryMeta,
#| central_offset : Int32,
#|) -> LocalRecord raise ZipError {
#| guard meta.local_offset >= 0 && meta.local_offset < central_offset else {
#| raise ZipError(Truncated, "zip: local header offset out of bounds")
#| }
#| let header = bytes[meta.local_offset:]
#| guard header
#| is [
#| u32le(LOCAL_HEADER_SIG),
#| u16le(_),
#| u16le(flags),
#| u16le(method_id),
#| u16le(_),
#| u16le(_),
#| u32le(local_crc),
#| u32le(comp_size_raw),
#| u32le(uncomp_size_raw),
#| u16le(name_len),
#| u16le(extra_len),
#| ..,
#| ] else {
#| raise record_error(
#| header,
#| LOCAL_HEADER_SIG,
#| "zip: invalid local header signature",
#| )
#| }
#| let flags = flags.reinterpret_as_int()
#| ensure_flags_supported(flags)
#| guard flags == meta.flags else {
#| raise ZipError(UnsupportedFeature, "zip: local and central flags differ")
#| }
#| guard method_id.reinterpret_as_int() == meta.method_id else {
#| raise ZipError(UnsupportedFeature, "zip: local and central methods differ")
#| }
#| let reader = Cursor(bytes, meta.local_offset + 30)
#| let local_name = reader.read_bytes(name_len.reinterpret_as_int())
#| let local_extra = reader.read_bytes(extra_len.reinterpret_as_int())
#| let central_name = meta.central_record[46:46 + meta.name_len]
#| guard local_name == central_name || decode_name(local_name) == meta.name else {
#| raise ZipError(UnsupportedFeature, "zip: local and central names differ")
#| }
#| let has_descriptor = (flags & FLAG_DATA_DESCRIPTOR) != 0
#| let need_local_uncomp = uncomp_size_raw == MAX_U32
#| let need_local_comp = comp_size_raw == MAX_U32
#| let (local_zip_uncomp, local_zip_comp, _, _) = read_zip64_extra(
#| local_extra, need_local_uncomp, need_local_comp, false,
#| )
#| guard local_crc == meta.crc || (has_descriptor && local_crc == 0) else {
#| raise ZipError(UnsupportedFeature, "zip: local and central CRC-32 differ")
#| }
#| guard local_size_matches(
#| comp_size_raw,
#| local_zip_comp,
#| meta.compressed_size.to_uint64(),
#| has_descriptor,
#| ) &&
#| local_size_matches(
#| uncomp_size_raw,
#| local_zip_uncomp,
#| meta.uncompressed_size.to_uint64(),
#| has_descriptor,
#| ) else {
#| raise ZipError(SizeMismatch, "zip: local and central sizes differ")
#| }
#| guard reader.pos <= central_offset &&
#| meta.compressed_size <= central_offset - reader.pos else {
#| raise ZipError(Truncated, "zip: compressed payload out of bounds")
#| }
#| let compressed = reader.read_bytes(meta.compressed_size)
#| let end = if has_descriptor {
#| let zip64 = need_local_uncomp || need_local_comp
#| let end = reader.pos + data_descriptor_size(bytes, reader.pos, meta, zip64)
#| guard end <= central_offset else {
#| raise ZipError(Truncated, "zip: data descriptor out of bounds")
#| }
#| end
#| } else {
#| reader.pos
#| }
#| { compressed, end, }
#|}
#|fn check_sizes(
#| uncompressed_size : Int32,
#| entry_limit : Int32,
#| total_limit : Int32,
#| used_total : Int32,
#|) -> Unit raise ZipError {
#| guard uncompressed_size <= entry_limit else {
#| raise ZipError(
#| LimitExceeded(
#| kind=EntryUncompressedBytes,
#| limit=entry_limit.to_uint64(),
#| observed=uncompressed_size.to_uint64(),
#| ),
#| "zip: entry exceeds the per-entry uncompressed limit",
#| )
#| }
#| guard used_total >= 0 && uncompressed_size <= total_limit - used_total else {
#| raise ZipError(
#| LimitExceeded(
#| kind=TotalUncompressedBytes,
#| limit=total_limit.to_uint64(),
#| observed=used_total.to_uint64() + uncompressed_size.to_uint64(),
#| ),
#| "zip: entries exceed the total uncompressed limit",
#| )
#| }
#|}
#|fn inflate_declared_entry(
#| compressed : BytesView,
#| declared : Int32,
#| max_output : Int32,
#| limit_kind : LimitKind,
#| reported_limit : Int32,
#| cancelled : () -> Bool,
#|) -> Bytes raise ZipError {
#| guard declared <= max_output else {
#| return inflate_entry(
#| compressed, max_output, limit_kind, reported_limit, cancelled,
#| )
#| }
#| check_cancelled(cancelled)
#| let result = @flate.Decompressor().decompress(
#| compressed,
#| max_output_bytes=declared,
#| cancelled~,
#| ) catch {
#| InflateError(Cancelled, message) => raise ZipError(Cancelled, message)
#| _ =>
#| return inflate_entry(
#| compressed, max_output, limit_kind, reported_limit, cancelled,
#| )
#| }
#| guard result.data.length() == declared else {
#| return inflate_entry(
#| compressed, max_output, limit_kind, reported_limit, cancelled,
#| )
#| }
#| result.data
#|}
#|fn inflate_entry(
#| compressed : BytesView,
#| max_output : Int32,
#| limit_kind : LimitKind,
#| reported_limit : Int32,
#| cancelled : () -> Bool,
#|) -> Bytes raise ZipError {
#| check_cancelled(cancelled)
#| let inflater = @flate.IncrementalDecoder()
#| let scratch = FixedArray::make(8192, b'\x00')
#| let previous = (reported_limit - max_output).to_uint64()
#| let size = for position = 0, total = 0UL {
#| let result = inflater.step(compressed[position:], scratch.mut_view()) catch {
#| err => raise wrap_inflate_error(err)
#| }
#| guard !(result.status is NeedMoreInput) else {
#| raise ZipError(Truncated, "zip: truncated DEFLATE payload")
#| }
#| let position = position + result.consumed
#| let total = total + result.produced.to_uint64()
#| check_cancelled(cancelled)
#| guard total <= max_output.to_uint64() else {
#| raise ZipError(
#| LimitExceeded(
#| kind=limit_kind,
#| limit=reported_limit.to_uint64(),
#| observed=previous + total,
#| ),
#| "zip: decompressed size limit exceeded",
#| )
#| }
#| guard result.status is Done else { continue position, total }
#| guard position == compressed.length() else {
#| raise ZipError(
#| InvalidDeflate,
#| "zip: trailing bytes in compressed payload",
#| )
#| }
#| break total.to_int()
#| }
#| let output = FixedArray::make(size, b'\x00')
#| let result = @flate.Decompressor().decompress_into(
#| compressed,
#| output.mut_view(),
#| cancelled~,
#| ) catch {
#| err => raise wrap_inflate_error(err)
#| }
#| guard result.produced == size else {
#| raise ZipError(InvalidDeflate, "zip: preallocation size mismatch")
#| }
#| owned_zip_buffer_to_bytes(output)
#|}
#|priv struct EndOfCentral {
#| eocd_offset : Int32
#| entry_count : Int32
#| central_size : Int32
#| central_offset : Int32
#| central_boundary : Int32
#| zip64_eocd_offset : Int32?
#| comment : Bytes
#|}
#|fn zip64_central_bounds(
#| bytes : BytesView,
#| eocd_offset : Int32,
#| disk_entries : Int32,
#| total_entries : Int32,
#| central_size_raw : UInt32,
#| central_offset_raw : UInt32,
#|) -> (Int32, Int32, Int32, Int32) raise ZipError {
#| let (zip_entries, zip_size, zip_offset, zip64_offset) = read_zip64_eocd(
#| bytes, eocd_offset,
#| )
#| guard disk_entries == 0xFFFF || disk_entries.to_uint64() == zip_entries else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: classic and zip64 entry counts differ",
#| )
#| }
#| guard total_entries == 0xFFFF || total_entries.to_uint64() == zip_entries else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: classic and zip64 entry counts differ",
#| )
#| }
#| guard central_size_raw == MAX_U32 || central_size_raw.to_uint64() == zip_size else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: classic and zip64 central sizes differ",
#| )
#| }
#| guard central_offset_raw == MAX_U32 ||
#| central_offset_raw.to_uint64() == zip_offset else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: classic and zip64 central offsets differ",
#| )
#| }
#| let entry_count = (if total_entries == 0xFFFF {
#| zip_entries
#| } else {
#| total_entries.to_uint64()
#| }).to_int_checked()
#| let central_size = (if central_size_raw == MAX_U32 {
#| zip_size
#| } else {
#| central_size_raw.to_uint64()
#| }).to_int_checked()
#| let central_offset = (if central_offset_raw == MAX_U32 {
#| zip_offset
#| } else {
#| central_offset_raw.to_uint64()
#| }).to_int_checked()
#| (entry_count, central_size, central_offset, zip64_offset)
#|}
#|fn validate_central_bounds(
#| bytes : BytesView,
#| limits : ReadLimits,
#| eocd_offset : Int32,
#| entry_count : Int32,
#| central_size : Int32,
#| central_offset : Int32,
#| central_boundary : Int32,
#| zip64_eocd_offset : Int32?,
#| comment : Bytes,
#|) -> EndOfCentral raise ZipError {
#| guard entry_count <= limits.max_entries else {
#| raise ZipError(
#| LimitExceeded(
#| kind=Entries,
#| limit=limits.max_entries.to_uint64(),
#| observed=entry_count.to_uint64(),
#| ),
#| "zip: entry count exceeds its limit",
#| )
#| }
#| guard central_offset <= central_boundary &&
#| central_boundary <= bytes.length() &&
#| central_size == central_boundary - central_offset else {
#| raise ZipError(Truncated, "zip: central directory out of bounds")
#| }
#| {
#| eocd_offset,
#| entry_count,
#| central_size,
#| central_offset,
#| central_boundary,
#| zip64_eocd_offset,
#| comment,
#| }
#|}
#|fn parse_end_of_central(
#| bytes : BytesView,
#| limits : ReadLimits,
#| cancelled : () -> Bool,
#|) -> EndOfCentral raise ZipError {
#| check_cancelled(cancelled)
#| guard bytes.length() <= limits.max_package_bytes else {
#| raise ZipError(
#| LimitExceeded(
#| kind=PackageBytes,
#| limit=limits.max_package_bytes.to_uint64(),
#| observed=bytes.length().to_uint64(),
#| ),
#| "zip: package exceeds the input size limit",
#| )
#| }
#| guard find_end_of_central(bytes) is Some(record) else {
#| raise ZipError(
#| MissingEndOfCentral,
#| "zip: end of central directory not found",
#| )
#| }
#| guard record.disk == 0 && record.central_disk == 0 else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| let {
#| offset: eocd_offset,
#| disk_entries,
#| entries: total_entries,
#| central_size: central_size_raw,
#| central_offset: central_offset_raw,
#| ..
#| } = record
#| let comment = copy_cancellable(record.comment, cancelled)
#| guard !record.has_zip64(eocd_offset.to_uint64()) else {
#| let (entry_count, central_size, central_offset, zip64_offset) = zip64_central_bounds(
#| bytes, eocd_offset, disk_entries, total_entries, central_size_raw, central_offset_raw,
#| )
#| return validate_central_bounds(
#| bytes,
#| limits,
#| eocd_offset,
#| entry_count,
#| central_size,
#| central_offset,
#| zip64_offset,
#| Some(zip64_offset),
#| comment,
#| )
#| }
#| guard disk_entries == total_entries else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| validate_central_bounds(
#| bytes,
#| limits,
#| eocd_offset,
#| total_entries,
#| central_size_raw.to_int_checked(),
#| central_offset_raw.to_int_checked(),
#| eocd_offset,
#| None,
#| comment,
#| )
#|}
#|fn resolve_zip64_field(
#| need : Bool,
#| zip64_value : UInt64?,
#| classic_raw : UInt32,
#|) -> UInt64 {
#| guard need else { return classic_raw.to_uint64() }
#| zip64_value.unwrap()
#|}
#|fn parse_central_record(
#| central_bytes : BytesView,
#| central_reader : Cursor,
#|) -> MemoryEntryMeta raise ZipError {
#| let central_record_start = central_reader.pos
#| let parsed = parse_directory_record(central_reader)
#| let entry = parsed.entry
#| ensure_flags_supported(entry.flags)
#| {
#| name: entry.name,
#| name_len: parsed.name_len,
#| flags: entry.flags,
#| method_id: entry.method_id,
#| crc: entry.crc,
#| dos_date: entry.dos_date,
#| dos_time: entry.dos_time,
#| creator_system: entry.creator_system,
#| external_attributes: entry.external_attributes,
#| compressed_size: entry.compressed_size.to_int_checked(),
#| uncompressed_size: entry.uncompressed_size.to_int_checked(),
#| local_offset: entry.local_offset.to_int_checked(),
#| central_record: central_bytes[central_record_start:central_reader.pos],
#| central_zip64_offset_position: parsed.central_zip64_offset_position,
#| data_descriptor: (entry.flags & FLAG_DATA_DESCRIPTOR) != 0,
#| }
#|}
#|fn parse_central_directory(
#| bytes : BytesView,
#| eocd : EndOfCentral,
#| limits : ReadLimits,
#| cancelled : () -> Bool,
#|) -> Array[MemoryEntryMeta] raise ZipError {
#| let central_bytes = bytes[eocd.central_offset:eocd.central_boundary]
#| let central_reader = Cursor(central_bytes, 0)
#| let max_plausible = eocd.central_size / 46
#| let metas = Array(capacity=eocd.entry_count.min(max_plausible))
#| let mut used_total = 0
#| for entry_index in 0.. TrailerTemplate {
#| guard eocd.zip64_eocd_offset is Some(zip64_offset) else {
#| return {
#| classic_end_record: bytes[eocd.eocd_offset:],
#| zip64_end_record: None,
#| zip64_locator: None,
#| }
#| }
#| let locator_offset = eocd.eocd_offset - 20
#| {
#| classic_end_record: bytes[eocd.eocd_offset:],
#| zip64_end_record: Some(bytes[zip64_offset:locator_offset]),
#| zip64_locator: Some(bytes[locator_offset:eocd.eocd_offset]),
#| }
#|}
#|fn decode_entry(
#| bytes : BytesView,
#| meta : MemoryEntryMeta,
#| central_offset : Int32,
#| limits : ReadLimits,
#| actual_total : Int32,
#| cancelled : () -> Bool,
#|) -> Entry raise ZipError {
#| check_cancelled(cancelled)
#| let record = read_local_record(bytes, meta, central_offset)
#| let entry_limit = limits.max_entry_uncompressed_bytes
#| let total_limit = limits.max_total_uncompressed_bytes
#| let remaining = total_limit - actual_total
#| let (effective_cap, limit_kind, reported_limit) = if remaining < entry_limit {
#| (remaining, TotalUncompressedBytes, total_limit)
#| } else {
#| (entry_limit, EntryUncompressedBytes, entry_limit)
#| }
#| let compression = method_from_code(meta.method_id)
#| let data = {
#| guard !(record.compressed.length() == 0 && meta.uncompressed_size == 0) else {
#| b""
#| }
#| match compression {
#| Store => {
#| check_sizes(
#| record.compressed.length(),
#| entry_limit,
#| total_limit,
#| actual_total,
#| )
#| copy_cancellable(record.compressed, cancelled)
#| }
#| Deflate =>
#| inflate_declared_entry(
#| record.compressed,
#| meta.uncompressed_size,
#| effective_cap,
#| limit_kind,
#| reported_limit,
#| cancelled,
#| )
#| }
#| }
#| guard data.length() == meta.uncompressed_size else {
#| raise ZipError(
#| SizeMismatch,
#| "zip: decoded size does not match central directory",
#| )
#| }
#| guard data.length() <= total_limit - actual_total else {
#| raise ZipError(
#| LimitExceeded(
#| kind=TotalUncompressedBytes,
#| limit=total_limit.to_uint64(),
#| observed=actual_total.to_uint64() + data.length().to_uint64(),
#| ),
#| "zip: entries exceed the total uncompressed limit",
#| )
#| }
#| let crc = crc32_cancellable(data, cancelled~)
#| guard crc == meta.crc else {
#| raise ZipError(ChecksumMismatch, "zip: CRC-32 mismatch")
#| }
#| let source = Some({
#| local_record: bytes[meta.local_offset:record.end],
#| central_record: meta.central_record,
#| central_zip64_offset_position: meta.central_zip64_offset_position,
#| })
#| {
#| name: meta.name,
#| data,
#| compression,
#| crc32: meta.crc,
#| dos_date: meta.dos_date,
#| dos_time: meta.dos_time,
#| creator_system: meta.creator_system,
#| external_attributes: meta.external_attributes,
#| data_descriptor: meta.data_descriptor,
#| compressed_size: meta.compressed_size,
#| source,
#| origin_local_offset: Some(meta.local_offset),
#| }
#|}
#|fn read_impl(
#| bytes : BytesView,
#| limits : ReadLimits,
#| cancelled : () -> Bool,
#|) -> Archive raise ZipError {
#| let eocd = parse_end_of_central(bytes, limits, cancelled)
#| let original = copy_cancellable(bytes, cancelled)
#| let bytes = original[:]
#| let metas = parse_central_directory(bytes, eocd, limits, cancelled)
#| let archive = {
#| original: Some(original),
#| entries: [],
#| comment: eocd.comment,
#| trailer: Some(build_trailer(bytes, eocd)),
#| }
#| let mut actual_total = 0
#| for meta in metas {
#| let entry = decode_entry(
#| bytes,
#| meta,
#| eocd.central_offset,
#| limits,
#| actual_total,
#| cancelled,
#| )
#| actual_total = actual_total + entry.data().length()
#| archive.entries.push(entry)
#| }
#| check_cancelled(cancelled)
#| archive
#|}
#|pub fn read_all(
#| bytes : BytesView,
#| limits~ : ReadLimits = ReadLimits(),
#| cancelled~ : () -> Bool = () => false,
#|) -> Archive raise ZipError {
#| guard limits.max_package_bytes >= 0 &&
#| limits.max_entries >= 0 &&
#| limits.max_entry_uncompressed_bytes >= 0 &&
#| limits.max_total_uncompressed_bytes >= 0 else {
#| raise ZipError(InvalidArgument, "zip: read limits must be non-negative")
#| }
#| read_impl(bytes, limits, cancelled)
#|}
),
"reader_byte.mbt": (
#|const LOCAL_HEADER_SIG : UInt32 = 0x04034b50
#|const CENTRAL_HEADER_SIG : UInt32 = 0x02014b50
#|const DIRECTORY_SIGNATURE_SIG : UInt32 = 0x05054b50
#|const END_OF_CENTRAL_SIG : UInt32 = 0x06054b50
#|const DATA_DESCRIPTOR_SIG : UInt32 = 0x08074b50
#|const ZIP64_EOCD_SIG : UInt32 = 0x06064b50
#|const ZIP64_LOCATOR_SIG : UInt32 = 0x07064b50
#|const FLAG_DATA_DESCRIPTOR : Int32 = 0x0008
#|const FLAG_UTF8 : Int32 = 0x0800
#|const FLAG_ENCRYPTED : Int32 = 0x0001
#|const FLAG_DEFLATE_STRENGTH : Int32 = 0x0006
#|const ZIP64_EXTRA_ID : Int32 = 0x0001
#|const MAX_U32 : UInt32 = 0xFFFFFFFF
#|priv struct Cursor {
#| bytes : BytesView
#| mut pos : Int32
#|}
#|fn copy_zip_input(input : BytesView) -> Bytes {
#| let copy = FixedArray::make(input.length(), b'\x00')
#| copy.blit_from_bytesview(0, input)
#| owned_zip_buffer_to_bytes(copy)
#|}
#|fn Cursor::Cursor(bytes : BytesView, pos : Int32) -> Cursor {
#| { bytes, pos, }
#|}
#|fn Cursor::read_bytes(self : Cursor, len : Int32) -> BytesView raise ZipError {
#| if len < 0 || self.pos < 0 || len > self.bytes.length() - self.pos {
#| raise ZipError(Truncated, "zip: truncated field")
#| }
#| let start = self.pos
#| self.pos = self.pos + len
#| self.bytes[start:self.pos]
#|}
#|fn Cursor::skip(self : Cursor, len : Int32) -> Unit raise ZipError {
#| ignore(self.read_bytes(len))
#|}
#|priv struct EndRecord {
#| offset : Int32
#| disk : UInt32
#| central_disk : UInt32
#| disk_entries : Int32
#| entries : Int32
#| central_size : UInt32
#| central_offset : UInt32
#| comment : BytesView
#|}
#|fn find_end_of_central(bytes : BytesView) -> EndRecord? {
#| let len = bytes.length()
#| let min_offset = (len - (22 + 0xFFFF)).max(0)
#| for i in (len - 22)>=..min_offset {
#| if bytes[i:] is [u32le(END_OF_CENTRAL_SIG), ..] &&
#| end_record_at(bytes, i) is Some(record) {
#| return Some(record)
#| }
#| } nobreak {
#| None
#| }
#|}
#|fn end_record_at(bytes : BytesView, offset : Int32) -> EndRecord? {
#| guard bytes[offset:]
#| is [
#| u32le(END_OF_CENTRAL_SIG),
#| u16le(disk),
#| u16le(central_disk),
#| u16le(disk_entries),
#| u16le(entries),
#| u32le(central_size),
#| u32le(central_offset),
#| u16le(comment_len),
#| .. comment,
#| ] &&
#| comment.length() == comment_len.reinterpret_as_int() else {
#| return None
#| }
#| Some({
#| offset,
#| disk,
#| central_disk,
#| disk_entries: disk_entries.reinterpret_as_int(),
#| entries: entries.reinterpret_as_int(),
#| central_size,
#| central_offset,
#| comment,
#| })
#|}
#|fn EndRecord::has_zip64(self : EndRecord, absolute_offset : UInt64) -> Bool {
#| self.central_size == MAX_U32 ||
#| self.central_offset == MAX_U32 ||
#| self.central_offset.to_uint64() + self.central_size.to_uint64() !=
#| absolute_offset
#|}
#|fn record_error(
#| record : BytesView,
#| signature : UInt32,
#| message : String,
#|) -> ZipError {
#| if record is [u32le(found), ..] && found != signature {
#| ZipError(InvalidSignature, message)
#| } else {
#| ZipError(Truncated, "zip: truncated header")
#| }
#|}
#|fn decode_name(bytes : BytesView) -> String raise ZipError {
#| @encoding/utf8.decode(bytes) catch {
#| _ => raise ZipError(InvalidUtf8, "zip: entry name is not UTF-8")
#| }
#|}
#|fn ensure_flags_supported(flags : Int32) -> Unit raise ZipError {
#| if (flags & FLAG_ENCRYPTED) != 0 {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: encrypted entries are not supported",
#| )
#| }
#| let supported = FLAG_DATA_DESCRIPTOR | FLAG_UTF8 | FLAG_DEFLATE_STRENGTH
#| let unsupported = flags & supported.lnot()
#| if unsupported != 0 {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: unsupported entry flags: 0x\{unsupported.to_string(radix=16)}",
#| )
#| }
#|}
#|fn method_from_code(method_id : Int32) -> Compression raise ZipError {
#| match method_id {
#| 0 => Store
#| 8 => Deflate
#| _ =>
#| raise ZipError(
#| UnsupportedCompression(method_id),
#| "zip: unsupported compression method \{method_id}",
#| )
#| }
#|}
#|fn copy_cancellable(
#| bytes : BytesView,
#| cancelled : () -> Bool,
#|) -> Bytes raise ZipError {
#| check_cancelled(cancelled)
#| let output : FixedArray[Byte] = FixedArray::make(bytes.length(), b'\x00')
#| for offset = 0
#| offset < bytes.length()
#| offset = offset + (bytes.length() - offset).min(0x10000) {
#| check_cancelled(cancelled)
#| let end = offset + (bytes.length() - offset).min(0x10000)
#| output.blit_from_bytesview(offset, bytes[offset:end])
#| }
#| check_cancelled(cancelled)
#| owned_zip_buffer_to_bytes(output)
#|}
),
"reader_zip64.mbt": (
#|fn extra_field_at(
#| extra : BytesView,
#| pos : Int32,
#|) -> (Int32, Int32, Int32) raise ZipError {
#| guard pos >= 0 && extra[pos:] is [u16le(header_id), u16le(data_size), .. data] else {
#| raise ZipError(UnsupportedFeature, "zip: truncated extra field header")
#| }
#| let data_size = data_size.reinterpret_as_int()
#| if data_size > data.length() {
#| raise ZipError(Truncated, "zip: truncated extra field data")
#| }
#| (header_id.reinterpret_as_int(), data_size, pos + 4)
#|}
#|fn read_zip64_extra(
#| extra : BytesView,
#| need_uncomp : Bool,
#| need_comp : Bool,
#| need_offset : Bool,
#| need_disk~ : Bool = false,
#|) -> (UInt64?, UInt64?, UInt64?, Int32?) raise ZipError {
#| let mut pos = 0
#| while pos < extra.length() {
#| let (header_id, data_size, data_offset) = extra_field_at(extra, pos)
#| pos = data_offset + data_size
#| if header_id == ZIP64_EXTRA_ID {
#| let count = (if need_uncomp { 1 } else { 0 }) +
#| (if need_comp { 1 } else { 0 }) +
#| (if need_offset { 1 } else { 0 })
#| if data_size != 8 * count + (if need_disk { 4 } else { 0 }) {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: zip64 extra field size mismatch",
#| )
#| }
#| let fields = extra[data_offset:data_offset + data_size]
#| let (uncomp, fields) = take_zip64_field(fields, need_uncomp)
#| let (comp, fields) = take_zip64_field(fields, need_comp)
#| let (offset, fields) = take_zip64_field(fields, need_offset)
#| if need_disk {
#| guard fields is [u32le(0), ..] else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| }
#| let offset_position = if need_offset {
#| Some(
#| data_offset +
#| 8 *
#| ((if need_uncomp { 1 } else { 0 }) + (if need_comp { 1 } else { 0 })),
#| )
#| } else {
#| None
#| }
#| return (uncomp, comp, offset, offset_position)
#| }
#| }
#| if need_uncomp || need_comp || need_offset || need_disk {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: required zip64 extra field is missing",
#| )
#| }
#| (None, None, None, None)
#|}
#|fn take_zip64_field(fields : BytesView, need : Bool) -> (UInt64?, BytesView) {
#| guard need && fields is [u64le(value), .. rest] else { return (None, fields) }
#| (Some(value), rest)
#|}
#|fn read_zip64_eocd(
#| bytes : BytesView,
#| eocd_offset : Int32,
#|) -> (UInt64, UInt64, UInt64, Int32) raise ZipError {
#| let locator_offset = eocd_offset - 20
#| guard locator_offset >= 0 &&
#| bytes[locator_offset:] is [u32le(ZIP64_LOCATOR_SIG), .. locator] else {
#| raise ZipError(UnsupportedFeature, "zip: zip64 locator missing")
#| }
#| guard locator
#| is [u32le(locator_disk), u64le(zip64_offset_u64), u32le(locator_disks), ..] else {
#| raise ZipError(Truncated, "zip: truncated field")
#| }
#| if locator_disk != 0 || locator_disks != 1 {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| let zip64_offset = zip64_offset_u64.to_int_checked()
#| if zip64_offset > locator_offset || 12 > locator_offset - zip64_offset {
#| raise ZipError(Truncated, "zip: zip64 end record out of bounds")
#| }
#| guard bytes[zip64_offset:] is [u32le(sig), u64le(record_size), .. fields] else {
#| raise ZipError(Truncated, "zip: truncated header")
#| }
#| if sig != ZIP64_EOCD_SIG {
#| raise ZipError(InvalidSignature, "zip: invalid zip64 end record signature")
#| }
#| let expected = (locator_offset - zip64_offset - 12).to_uint64()
#| if record_size < 44 || record_size != expected {
#| raise ZipError(UnsupportedFeature, "zip: zip64 end record size mismatch")
#| }
#| guard fields
#| is [
#| u16le(_),
#| u16le(_),
#| u32le(disk_no),
#| u32le(disk_start),
#| u64le(entries_on_disk),
#| u64le(total_entries),
#| u64le(central_size),
#| u64le(central_offset),
#| ..,
#| ] else {
#| raise ZipError(Truncated, "zip: truncated header")
#| }
#| if disk_no != 0 || disk_start != 0 || entries_on_disk != total_entries {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: multi-disk archives are not supported",
#| )
#| }
#| (total_entries, central_size, central_offset, zip64_offset)
#|}
#|fn data_descriptor_matches(
#| bytes : BytesView,
#| crc : UInt32,
#| comp_size : UInt64,
#| uncomp_size : UInt64,
#| zip64 : Bool,
#| signed : Bool,
#|) -> Bool {
#| let fields = if signed {
#| guard bytes is [u32le(DATA_DESCRIPTOR_SIG), .. fields] else { return false }
#| fields
#| } else {
#| bytes
#| }
#| match fields {
#| [u32le(found_crc), u64le(comp), u64le(uncomp), ..] if zip64 =>
#| found_crc == crc && comp == comp_size && uncomp == uncomp_size
#| [u32le(found_crc), u32le(comp), u32le(uncomp), ..] if !zip64 =>
#| found_crc == crc &&
#| comp.to_uint64() == comp_size &&
#| uncomp.to_uint64() == uncomp_size
#| _ => false
#| }
#|}
#|fn data_descriptor_size(
#| bytes : BytesView,
#| offset : Int32,
#| meta : MemoryEntryMeta,
#| zip64 : Bool,
#|) -> Int32 raise ZipError {
#| checked_data_descriptor_size(
#| bytes[offset:],
#| meta.crc,
#| meta.compressed_size.to_uint64(),
#| meta.uncompressed_size.to_uint64(),
#| zip64,
#| )
#|}
#|fn checked_data_descriptor_size(
#| bytes : BytesView,
#| crc : UInt32,
#| comp_size : UInt64,
#| uncomp_size : UInt64,
#| zip64 : Bool,
#|) -> Int32 raise ZipError {
#| let signed = data_descriptor_matches(
#| bytes, crc, comp_size, uncomp_size, zip64, true,
#| )
#| let unsigned = data_descriptor_matches(
#| bytes, crc, comp_size, uncomp_size, zip64, false,
#| )
#| if signed && unsigned {
#| raise ZipError(UnsupportedFeature, "zip: ambiguous data descriptor")
#| }
#| if !signed && !unsigned {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: data descriptor does not match central directory",
#| )
#| }
#| (if signed { 4 } else { 0 }) + 4 + (if zip64 { 16 } else { 8 })
#|}
#|fn local_size_matches(
#| raw : UInt32,
#| zip64 : UInt64?,
#| expected : UInt64,
#| allow_placeholder : Bool,
#|) -> Bool {
#| let zip64_matches = zip64.map_or(true, value => {
#| value == expected || (allow_placeholder && value == 0)
#| })
#| if allow_placeholder && raw == 0 {
#| return zip64_matches
#| }
#| if raw == MAX_U32 {
#| return zip64 is Some(_) && zip64_matches
#| }
#| raw.to_uint64() == expected && zip64_matches
#|}
),
"stream_writer.mbt": (
#|priv enum WriterMode {
#| Ready
#| Writing(PendingEntry)
#| Emitting
#| Finished
#| Failed
#|}
#|priv struct PendingEntry {
#| name : Bytes
#| compression : Compression
#| offset : UInt64
#| expected_size : UInt64?
#| zip64 : Bool
#| crc : @checksum.Crc32
#| mut size : UInt64
#| mut compressed_size : UInt64
#|}
#|struct Writer {
#| sink : (BytesView) -> Unit raise
#| level : Int32
#| fast_store : Bool
#| mut mode : WriterMode
#| mut offset : UInt64
#| mut directory : Array[Bytes]
#| mut has_zip64 : Bool
#| mut compressor : @flate.Compressor?
#| mut deflater : @flate.IncrementalEncoder?
#| mut scratch : FixedArray[Byte]
#|}
#|pub fn Writer::Writer(
#| sink : (BytesView) -> Unit raise,
#| level~ : Int32 = 6,
#| fast_store~ : Bool = false,
#|) -> Writer {
#| {
#| sink,
#| level,
#| fast_store,
#| mode: Ready,
#| offset: 0UL,
#| directory: [],
#| has_zip64: false,
#| compressor: None,
#| deflater: None,
#| scratch: [],
#| }
#|}
#|pub fn Writer::position(self : Writer) -> UInt64 {
#| self.offset
#|}
#|pub fn Writer::is_finished(self : Writer) -> Bool {
#| self.mode is Finished
#|}
#|pub fn Writer::is_failed(self : Writer) -> Bool {
#| self.mode is Failed
#|}
#|fn Writer::require_ready(self : Writer) -> Unit raise ZipError {
#| match self.mode {
#| Ready => ()
#| Emitting => raise ZipError(InvalidState, "zip: reentrant sink write")
#| _ => raise ZipError(InvalidState, "zip: writer is not ready for an entry")
#| }
#|}
#|fn Writer::pending(self : Writer) -> PendingEntry raise ZipError {
#| guard self.mode is Writing(entry) else {
#| raise ZipError(InvalidState, "zip: no open entry")
#| }
#| entry
#|}
#|fn Writer::emit(self : Writer, bytes : BytesView) -> Unit raise {
#| guard bytes.length() > 0 else { return }
#| let size = bytes.length().to_uint64()
#| guard size <= 0xffff_ffff_ffff_ffffUL - self.offset else {
#| self.mode = Failed
#| self.release_storage()
#| raise ZipError(UnsupportedFeature, "zip: output exceeds UInt64 range")
#| }
#| let mode = self.mode
#| self.mode = Emitting
#| errdefer {
#| self.mode = Failed
#| self.release_storage()
#| }
#| (self.sink)(bytes)
#| self.offset += size
#| self.mode = mode
#|}
#|fn Writer::release_storage(self : Writer) -> Unit {
#| self.directory = []
#| self.compressor = None
#| self.deflater = None
#| self.scratch = []
#|}
#|fn stream_name(name : String) -> Bytes raise ZipError {
#| let bytes = @encoding/utf8.encode(name)
#| guard bytes.length() <= 0xffff else {
#| raise ZipError(UnsupportedFeature, "zip: entry name too long")
#| }
#| bytes
#|}
#|fn stream_local_header(
#| name : Bytes,
#| compression : Compression,
#| crc : UInt32,
#| size : UInt64,
#| compressed_size : UInt64,
#| zip64 : Bool,
#| descriptor : Bool,
#|) -> Bytes {
#| let out = @buffer.Buffer(
#| capacity=30 + name.length() + (if zip64 { 20 } else { 0 }),
#| )
#| out.write_uint_le(LOCAL_HEADER_SIG)
#| out.write_uint16_le(if zip64 { (45).to_uint16() } else { (20).to_uint16() })
#| out.write_uint16_le(
#| (FLAG_UTF8 | (if descriptor { FLAG_DATA_DESCRIPTOR } else { 0 })).to_uint16(),
#| )
#| out.write_uint16_le(compression.code().to_uint16())
#| out.write_uint_le(0U)
#| out.write_uint_le(crc)
#| out.write_uint_le(if zip64 { MAX_U32 } else { compressed_size.to_uint() })
#| out.write_uint_le(if zip64 { MAX_U32 } else { size.to_uint() })
#| out.write_uint16_le(name.length().to_uint16())
#| out.write_uint16_le(if zip64 { (20).to_uint16() } else { (0).to_uint16() })
#| out.write_bytesview(name)
#| if zip64 {
#| out.write_uint16_le(ZIP64_EXTRA_ID.to_uint16())
#| out.write_uint16_le((16).to_uint16())
#| out.write_uint64_le(size)
#| out.write_uint64_le(compressed_size)
#| }
#| out.to_bytes()
#|}
#|fn stream_central_record(
#| name : Bytes,
#| compression : Compression,
#| crc : UInt32,
#| size : UInt64,
#| compressed_size : UInt64,
#| offset : UInt64,
#| zip64 : Bool,
#| descriptor : Bool,
#|) -> Bytes {
#| let large_offset = offset >= 0xffff_ffffUL
#| let extra_payload = (if zip64 { 16 } else { 0 }) +
#| (if large_offset { 8 } else { 0 })
#| let extra_size = if extra_payload == 0 { 0 } else { 4 + extra_payload }
#| let out = @buffer.Buffer(capacity=46 + name.length() + extra_size)
#| let version = if zip64 || large_offset {
#| (45).to_uint16()
#| } else {
#| (20).to_uint16()
#| }
#| out.write_uint_le(CENTRAL_HEADER_SIG)
#| out.write_uint16_le(version)
#| out.write_uint16_le(version)
#| out.write_uint16_le(
#| (FLAG_UTF8 | (if descriptor { FLAG_DATA_DESCRIPTOR } else { 0 })).to_uint16(),
#| )
#| out.write_uint16_le(compression.code().to_uint16())
#| out.write_uint_le(0U)
#| out.write_uint_le(crc)
#| out.write_uint_le(if zip64 { MAX_U32 } else { compressed_size.to_uint() })
#| out.write_uint_le(if zip64 { MAX_U32 } else { size.to_uint() })
#| out.write_uint16_le(name.length().to_uint16())
#| out.write_uint16_le(extra_size.to_uint16())
#| out.write_uint16_le((0).to_uint16())
#| out.write_uint16_le((0).to_uint16())
#| out.write_uint16_le((0).to_uint16())
#| out.write_uint_le(0U)
#| out.write_uint_le(if large_offset { MAX_U32 } else { offset.to_uint() })
#| out.write_bytesview(name)
#| if extra_payload > 0 {
#| out.write_uint16_le(ZIP64_EXTRA_ID.to_uint16())
#| out.write_uint16_le(extra_payload.to_uint16())
#| if zip64 {
#| out.write_uint64_le(size)
#| out.write_uint64_le(compressed_size)
#| }
#| if large_offset {
#| out.write_uint64_le(offset)
#| }
#| }
#| out.to_bytes()
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn Writer::insert(
#| self : Writer,
#| name : String,
#| data : BytesView,
#| compression~ : Compression = Deflate,
#|) -> Unit raise {
#| self.require_ready()
#| let name = stream_name(name)
#| let crc = @checksum.crc32(data)
#| let compressed : BytesView = match compression {
#| Store => data
#| Deflate => {
#| let compressor = self.compressor.unwrap_or_else(() => {
#| let codec = @flate.Compressor(
#| strategy=Standard(level=self.level),
#| fast_store=self.fast_store,
#| )
#| self.compressor = Some(codec)
#| codec
#| })
#| compressor.compress(data)
#| }
#| }
#| let size = data.length().to_uint64()
#| let compressed_size = compressed.length().to_uint64()
#| let offset = self.offset
#| self.emit(
#| stream_local_header(
#| name, compression, crc, size, compressed_size, false, false,
#| ),
#| )
#| self.emit(compressed)
#| self.directory.push(
#| stream_central_record(
#| name, compression, crc, size, compressed_size, offset, false, false,
#| ),
#| )
#|}
#|pub fn Writer::begin_entry(
#| self : Writer,
#| name : String,
#| compression~ : Compression = Deflate,
#| size? : UInt64,
#|) -> Unit raise {
#| self.require_ready()
#| let name = stream_name(name)
#| let zip64 = size
#| .map(n => {
#| n >= 0xffff_ffffUL || n + 5UL * (n / 16384UL + 1UL) + 1UL >= 0xffff_ffffUL
#| })
#| .unwrap_or(true)
#| if compression is Deflate {
#| if self.deflater is Some(deflater) {
#| deflater.reset()
#| } else {
#| self.deflater = Some(
#| IncrementalEncoder(level=self.level, fast_store=self.fast_store),
#| )
#| self.scratch = FixedArray::make(65536, b'\x00')
#| }
#| }
#| let entry = {
#| name,
#| compression,
#| offset: self.offset,
#| expected_size: size,
#| zip64,
#| crc: Crc32(),
#| size: 0UL,
#| compressed_size: 0UL,
#| }
#| self.mode = Writing(entry)
#| self.emit(stream_local_header(name, compression, 0U, 0UL, 0UL, zip64, true))
#| self.has_zip64 = self.has_zip64 || zip64
#|}
#|fn Writer::pump(
#| self : Writer,
#| input : BytesView,
#| action : @flate.DeflateAction,
#|) -> Unit raise {
#| if self.deflater is Some(deflater) {
#| let mut consumed = 0
#| for ;; {
#| let status_result = deflater.step(
#| input[consumed:],
#| self.scratch.mut_view(),
#| action~,
#| )
#| let status = status_result.status
#| consumed += status_result.consumed
#| let produced = status_result.produced
#| if produced > 0 {
#| self.emit(Bytes::from_array(self.scratch[:produced]))
#| }
#| guard status is NeedMoreOutput else { break }
#| }
#| }
#|}
#|pub fn Writer::write(self : Writer, bytes : BytesView) -> Unit raise {
#| let entry = self.pending()
#| let length = bytes.length().to_uint64()
#| guard length <= 0xffff_ffff_ffff_ffffUL - entry.size else {
#| raise ZipError(UnsupportedFeature, "zip: entry exceeds UInt64 range")
#| }
#| if entry.expected_size is Some(size) {
#| guard length <= size - entry.size else {
#| raise ZipError(SizeMismatch, "zip: input exceeds declared size")
#| }
#| }
#| let start = self.offset
#| entry.crc.update(bytes)
#| match entry.compression {
#| Store => self.emit(bytes)
#| Deflate => self.pump(bytes, Continue)
#| }
#| entry.size += length
#| entry.compressed_size += self.offset - start
#|}
#|pub fn Writer::end_entry(self : Writer) -> Unit raise {
#| let entry = self.pending()
#| if entry.expected_size is Some(size) {
#| guard entry.size == size else {
#| raise ZipError(SizeMismatch, "zip: input is shorter than declared size")
#| }
#| }
#| let start = self.offset
#| if entry.compression is Deflate {
#| self.pump(b"", Finish)
#| }
#| entry.compressed_size += self.offset - start
#| let crc = entry.crc.finish()
#| let descriptor = @buffer.Buffer(capacity=if entry.zip64 { 24 } else { 16 })
#| descriptor.write_uint_le(DATA_DESCRIPTOR_SIG)
#| descriptor.write_uint_le(crc)
#| if entry.zip64 {
#| descriptor.write_uint64_le(entry.compressed_size)
#| descriptor.write_uint64_le(entry.size)
#| } else {
#| descriptor.write_uint_le(entry.compressed_size.to_uint())
#| descriptor.write_uint_le(entry.size.to_uint())
#| }
#| self.emit(descriptor.to_bytes())
#| self.directory.push(
#| stream_central_record(
#| entry.name,
#| entry.compression,
#| crc,
#| entry.size,
#| entry.compressed_size,
#| entry.offset,
#| entry.zip64,
#| true,
#| ),
#| )
#| self.mode = Ready
#|}
#|pub fn Writer::finish(self : Writer, comment~ : BytesView = b"") -> Unit raise {
#| self.require_ready()
#| guard comment.length() <= 0xffff else {
#| raise ZipError(UnsupportedFeature, "zip: archive comment too long")
#| }
#| let central_offset = self.offset
#| for record in self.directory {
#| self.emit(record)
#| }
#| let central_size = self.offset - central_offset
#| let count = self.directory.length()
#| let zip64 = self.has_zip64 ||
#| count >= 0xffff ||
#| central_size >= 0xffff_ffffUL ||
#| central_offset >= 0xffff_ffffUL
#| let out = @buffer.Buffer(capacity=98 + comment.length())
#| if zip64 {
#| out.write_uint_le(ZIP64_EOCD_SIG)
#| out.write_uint64_le(44UL)
#| out.write_uint16_le((45).to_uint16())
#| out.write_uint16_le((45).to_uint16())
#| out.write_uint_le(0U)
#| out.write_uint_le(0U)
#| out.write_uint64_le(count.to_uint64())
#| out.write_uint64_le(count.to_uint64())
#| out.write_uint64_le(central_size)
#| out.write_uint64_le(central_offset)
#| out.write_uint_le(ZIP64_LOCATOR_SIG)
#| out.write_uint_le(0U)
#| out.write_uint64_le(self.offset)
#| out.write_uint_le(1U)
#| }
#| out.write_uint_le(END_OF_CENTRAL_SIG)
#| out.write_uint_le(0U)
#| out.write_uint16_le(
#| if zip64 {
#| (0xffff).to_uint16()
#| } else {
#| count.to_uint16()
#| },
#| )
#| out.write_uint16_le(
#| if zip64 {
#| (0xffff).to_uint16()
#| } else {
#| count.to_uint16()
#| },
#| )
#| out.write_uint_le(if zip64 { MAX_U32 } else { central_size.to_uint() })
#| out.write_uint_le(if zip64 { MAX_U32 } else { central_offset.to_uint() })
#| out.write_uint16_le(comment.length().to_uint16())
#| out.write_bytesview(comment)
#| self.emit(out.to_bytes())
#| self.mode = Finished
#| self.release_storage()
#|}
),
"types.mbt": (
#|#derive(Eq, @debug.Debug)
#|pub(all) enum Compression {
#| Store
#| Deflate
#|}
#|fn Compression::code(compression : Compression) -> Int32 {
#| match compression {
#| Store => 0
#| Deflate => 8
#| }
#|}
#|#valtype
#|pub struct ReadLimits {
#| max_package_bytes : Int32
#| max_entries : Int32
#| max_entry_uncompressed_bytes : Int32
#| max_total_uncompressed_bytes : Int32
#|}
#|pub fn ReadLimits::ReadLimits(
#| max_package_bytes~ : Int32 = 0x7fff_ffff,
#| max_entries~ : Int32 = 0x7fff_ffff,
#| max_entry_uncompressed_bytes~ : Int32 = 0x7fff_ffff,
#| max_total_uncompressed_bytes~ : Int32 = 0x7fff_ffff,
#|) -> ReadLimits {
#| {
#| max_package_bytes,
#| max_entries,
#| max_entry_uncompressed_bytes,
#| max_total_uncompressed_bytes,
#| }
#|}
#|priv struct SourceRecord {
#| local_record : BytesView
#| central_record : BytesView
#| central_zip64_offset_position : Int32?
#|}
#|struct Entry {
#| name : String
#| data : Bytes
#| compression : Compression
#| crc32 : UInt32
#| dos_date : Int32
#| dos_time : Int32
#| creator_system : Int32
#| external_attributes : UInt32
#| data_descriptor : Bool
#| compressed_size : Int32
#| source : SourceRecord?
#| origin_local_offset : Int32?
#|}
#|pub fn Entry::name(self : Entry) -> String {
#| self.name
#|}
#|pub fn Entry::data(self : Entry) -> BytesView {
#| self.data
#|}
#|pub fn Entry::compression(self : Entry) -> Compression {
#| self.compression
#|}
#|pub fn Entry::crc32(self : Entry) -> UInt32 {
#| self.crc32
#|}
#|pub fn Entry::dos_date(self : Entry) -> Int32 {
#| self.dos_date
#|}
#|pub fn Entry::dos_time(self : Entry) -> Int32 {
#| self.dos_time
#|}
#|pub fn Entry::creator_system(self : Entry) -> Int32 {
#| self.creator_system
#|}
#|pub fn Entry::external_attributes(self : Entry) -> UInt32 {
#| self.external_attributes
#|}
#|priv struct TrailerTemplate {
#| classic_end_record : BytesView
#| zip64_end_record : BytesView?
#| zip64_locator : BytesView?
#|}
#|struct Archive {
#| entries : Array[Entry]
#| mut comment : Bytes
#| mut trailer : TrailerTemplate?
#| mut original : Bytes?
#|}
#|pub fn Archive::Archive() -> Archive {
#| { entries: [], comment: b"", trailer: None, original: None, }
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn Archive::insert(
#| self : Archive,
#| name : String,
#| data : BytesView,
#| compression~ : Compression = Deflate,
#|) -> Unit {
#| let data = data.to_owned()
#| self.original = None
#| self.entries.push({
#| name,
#| data,
#| compression,
#| data_descriptor: false,
#| crc32: @checksum.crc32(data),
#| dos_date: 0,
#| dos_time: 0,
#| creator_system: 0,
#| external_attributes: 0,
#| compressed_size: data.length(),
#| source: None,
#| origin_local_offset: None,
#| })
#|}
#|pub fn Archive::replace(
#| self : Archive,
#| name : StringView,
#| data : BytesView,
#|) -> Bool {
#| guard self.entries.search_by(entry => entry.name()[:] == name) is Some(index) else {
#| return false
#| }
#| self.original = None
#| let entry = self.entries[index]
#| let data = data.to_owned()
#| self.entries[index] = {
#| ..entry,
#| data,
#| crc32: @checksum.crc32(data),
#| compressed_size: data.length(),
#| source: None,
#| }
#| true
#|}
#|pub fn Archive::remove(self : Archive, name : StringView) -> Bool {
#| guard self.entries.search_by(entry => entry.name()[:] == name) is Some(index) else {
#| return false
#| }
#| self.original = None
#| ignore(self.entries.remove(index))
#| true
#|}
#|pub fn Archive::get(self : Archive, name : StringView) -> BytesView? {
#| guard self.entries.search_by(entry => entry.name()[:] == name) is Some(index) else {
#| return None
#| }
#| Some(self.entries[index].data())
#|}
#|pub fn Archive::entries(self : Archive) -> ArrayView[Entry] {
#| self.entries
#|}
#|pub fn Archive::comment(self : Archive) -> BytesView {
#| self.comment
#|}
#|pub fn Archive::set_comment(self : Archive, comment : BytesView) -> Unit {
#| self.original = None
#| self.comment = comment.to_owned()
#| self.trailer = None
#|}
#|#alias(clone, deprecated="Use `Archive::copy` instead")
#|pub fn Archive::copy(self : Archive) -> Archive {
#| {
#| entries: self.entries.copy(),
#| comment: self.comment,
#| trailer: self.trailer,
#| original: self.original,
#| }
#|}
),
"writer.mbt": (
#|priv struct Output {
#| limit : Int32?
#| mut buf : FixedArray[Byte]?
#| mut size : Int32
#|}
#|fn Output::counting(limit : Int32?) -> Output {
#| { limit, buf: None, size: 0, }
#|}
#|fn Output::writing(limit : Int32?, capacity~ : Int32 = 0) -> Output {
#| { limit, buf: Some(FixedArray::make(capacity, b'\x00')), size: 0, }
#|}
#|fn Output::is_counting(self : Output) -> Bool {
#| self.buf is None
#|}
#|fn Output::grow(self : Output, additional : Int32) -> Unit raise ZipError {
#| guard additional >= 0 else {
#| raise ZipError(UnsupportedFeature, "zip: negative output size")
#| }
#| if self.limit is Some(limit) {
#| guard additional <= limit - self.size else {
#| raise ZipError(
#| LimitExceeded(
#| kind=OutputBytes,
#| limit=limit.to_uint64(),
#| observed=self.size.to_uint64() + additional.to_uint64(),
#| ),
#| "zip: output exceeds its byte limit",
#| )
#| }
#| }
#| guard additional <= 0x7fff_ffff - self.size else {
#| raise ZipError(UnsupportedFeature, "zip: output size exceeds Int range")
#| }
#| let required = self.size + additional
#| if self.buf is Some(buf) && required > buf.length() {
#| let capacity = required.max(
#| buf.length() + buf.length().min(0x7fff_ffff - buf.length()),
#| )
#| let grown = FixedArray::make(capacity, b'\x00')
#| buf.blit_to(grown, len=self.size)
#| self.buf = Some(grown)
#| }
#| self.size = required
#|}
#|fn Output::write_bytes(self : Output, bytes : BytesView) -> Unit raise ZipError {
#| self.grow(bytes.length())
#| if self.buf is Some(buf) {
#| buf.blit_from_bytesview(self.size - bytes.length(), bytes)
#| }
#|}
#|fn Output::count(self : Output, n : Int32) -> Unit raise ZipError {
#| self.grow(n)
#|}
#|fn Output::write_u16(self : Output, value : Int32) -> Unit raise ZipError {
#| if value < 0 || value > 0xffff {
#| raise ZipError(UnsupportedFeature, "zip: u16 overflow")
#| }
#| self.grow(2)
#| if self.buf is Some(buf) {
#| buf.unsafe_write_uint16_le(self.size - 2, value.to_uint16())
#| }
#|}
#|fn Output::write_u32(self : Output, value : UInt32) -> Unit raise ZipError {
#| self.grow(4)
#| if self.buf is Some(buf) {
#| buf.unsafe_write_uint32_le(self.size - 4, value)
#| }
#|}
#|fn Output::write_u64(self : Output, value : UInt64) -> Unit raise ZipError {
#| self.grow(8)
#| if self.buf is Some(buf) {
#| buf.unsafe_write_uint64_le(self.size - 8, value)
#| }
#|}
#|fn Output::finish(self : Output) -> Bytes {
#| guard self.buf is Some(buf) else { return b"" }
#| self.buf = None
#| let size = self.size
#| self.size = 0
#| if size == buf.length() {
#| owned_zip_buffer_to_bytes(buf)
#| } else {
#| let exact = FixedArray::make(size, b'\x00')
#| buf.blit_to(exact, len=size)
#| owned_zip_buffer_to_bytes(exact)
#| }
#|}
#|fn write_u16_buf(buf : @buffer.Buffer, value : Int32) -> Unit raise ZipError {
#| let raw = value.to_uint_checked()
#| buf.write_uint16_le(raw.to_uint16())
#|}
#|fn build_zip64_extra(
#| uncomp~ : Int32?,
#| comp~ : Int32?,
#| offset~ : Int32?,
#|) -> Bytes raise ZipError {
#| let mut count = 0
#| if uncomp is Some(_) {
#| count = count + 1
#| }
#| if comp is Some(_) {
#| count = count + 1
#| }
#| if offset is Some(_) {
#| count = count + 1
#| }
#| if count == 0 {
#| return b""
#| }
#| let buf = @buffer.Buffer()
#| write_u16_buf(buf, ZIP64_EXTRA_ID)
#| write_u16_buf(buf, 8 * count)
#| if uncomp is Some(value) {
#| buf.write_uint64_le(value.to_uint64_checked())
#| }
#| if comp is Some(value) {
#| buf.write_uint64_le(value.to_uint64_checked())
#| }
#| if offset is Some(value) {
#| buf.write_uint64_le(value.to_uint64_checked())
#| }
#| buf.to_bytes()
#|}
#|priv struct PreparedPayload {
#| compressed_size : Int32
#| compressed : Bytes?
#|}
#|fn Entry::prepare_payload(
#| entry : Entry,
#| compressor : @flate.Compressor,
#|) -> PreparedPayload {
#| match entry.compression {
#| Store => { compressed_size: entry.data.length(), compressed: None, }
#| Deflate => {
#| let compressed = compressor.compress(entry.data)
#| { compressed_size: compressed.length(), compressed: Some(compressed), }
#| }
#| }
#|}
#|fn Entry::name_bytes(entry : Entry) -> Bytes raise ZipError {
#| let name = @encoding/utf8.encode(entry.name)
#| if name.length() > 0xffff {
#| raise ZipError(UnsupportedFeature, "zip: entry name too long")
#| }
#| name
#|}
#|fn Output::write_local_record(
#| output : Output,
#| entry : Entry,
#| name_bytes : Bytes,
#| payload : PreparedPayload,
#| use_descriptor : Bool,
#| force_zip64 : Bool,
#|) -> Unit raise ZipError {
#| let method_id = entry.compression.code()
#| let data_len = entry.data.length()
#| let compressed_size = payload.compressed_size
#| let crc = entry.crc32
#| let needs_zip64_sizes = force_zip64 ||
#| data_len.to_uint_checked() > MAX_U32 ||
#| compressed_size.to_uint_checked() > MAX_U32
#| if output.is_counting() {
#| let extra_size = if needs_zip64_sizes { 20 } else { 0 }
#| output.count(30 + name_bytes.length() + extra_size)
#| output.count(compressed_size)
#| if use_descriptor {
#| output.count(if needs_zip64_sizes { 24 } else { 16 })
#| }
#| return
#| }
#| let version = if needs_zip64_sizes { 45 } else { 20 }
#| let flags = FLAG_UTF8 |
#| (if use_descriptor { FLAG_DATA_DESCRIPTOR } else { 0 })
#| let local_extra = if needs_zip64_sizes {
#| build_zip64_extra(
#| uncomp=Some(data_len),
#| comp=Some(compressed_size),
#| offset=None,
#| )
#| } else {
#| b""
#| }
#| guard name_bytes.length() <= 0xffff else {
#| raise ZipError(UnsupportedFeature, "zip: u16 overflow")
#| }
#| let comp_field = if needs_zip64_sizes {
#| MAX_U32
#| } else if use_descriptor {
#| 0
#| } else {
#| compressed_size.to_uint_checked()
#| }
#| let uncomp_field = if needs_zip64_sizes {
#| MAX_U32
#| } else if use_descriptor {
#| 0
#| } else {
#| data_len.to_uint_checked()
#| }
#| let start = output.size
#| output.grow(30)
#| if output.buf is Some(buf) {
#| buf.unsafe_write_uint32_le(start, LOCAL_HEADER_SIG)
#| buf.unsafe_write_uint16_le(start + 4, version.to_uint16())
#| buf.unsafe_write_uint16_le(start + 6, flags.to_uint16())
#| buf.unsafe_write_uint16_le(start + 8, method_id.to_uint16())
#| buf.unsafe_write_uint16_le(start + 10, entry.dos_time.to_uint16())
#| buf.unsafe_write_uint16_le(start + 12, entry.dos_date.to_uint16())
#| buf.unsafe_write_uint32_le(start + 14, if use_descriptor { 0 } else { crc })
#| buf.unsafe_write_uint32_le(start + 18, comp_field)
#| buf.unsafe_write_uint32_le(start + 22, uncomp_field)
#| buf.unsafe_write_uint16_le(start + 26, name_bytes.length().to_uint16())
#| buf.unsafe_write_uint16_le(start + 28, local_extra.length().to_uint16())
#| }
#| output.write_bytes(name_bytes)
#| output.write_bytes(local_extra)
#| if payload.compressed is Some(compressed) {
#| output.write_bytes(compressed)
#| } else {
#| output.write_bytes(entry.data)
#| }
#| if use_descriptor {
#| output.write_u32(DATA_DESCRIPTOR_SIG)
#| output.write_u32(crc)
#| if needs_zip64_sizes {
#| output.write_u64(compressed_size.to_uint64_checked())
#| output.write_u64(data_len.to_uint64_checked())
#| } else {
#| output.write_u32(compressed_size.to_uint_checked())
#| output.write_u32(data_len.to_uint_checked())
#| }
#| }
#|}
#|fn Output::write_central_record(
#| output : Output,
#| entry : Entry,
#| name_bytes : Bytes,
#| local_offset : Int32,
#| compressed_size : Int32,
#| use_descriptor : Bool,
#| force_zip64 : Bool,
#|) -> Unit raise ZipError {
#| let method_id = entry.compression.code()
#| let data_len = entry.data.length()
#| let needs_zip64_sizes = force_zip64 ||
#| data_len.to_uint_checked() > MAX_U32 ||
#| compressed_size.to_uint_checked() > MAX_U32
#| let needs_zip64_offset = force_zip64 ||
#| local_offset.to_uint_checked() > MAX_U32
#| if output.is_counting() {
#| let extra_size = (if needs_zip64_sizes { 16 } else { 0 }) +
#| (if needs_zip64_offset { 8 } else { 0 })
#| output.count(
#| 46 +
#| name_bytes.length() +
#| (if extra_size > 0 { 4 + extra_size } else { 0 }),
#| )
#| return
#| }
#| let needs_zip64_entry = needs_zip64_sizes || needs_zip64_offset
#| let version = if needs_zip64_entry { 45 } else { 20 }
#| let version_made_by = (entry.creator_system << 8) | version
#| let flags = FLAG_UTF8 |
#| (if use_descriptor { FLAG_DATA_DESCRIPTOR } else { 0 })
#| let central_extra = build_zip64_extra(
#| uncomp=if needs_zip64_sizes { Some(data_len) } else { None },
#| comp=if needs_zip64_sizes { Some(compressed_size) } else { None },
#| offset=if needs_zip64_offset { Some(local_offset) } else { None },
#| )
#| guard name_bytes.length() <= 0xffff else {
#| raise ZipError(UnsupportedFeature, "zip: u16 overflow")
#| }
#| let comp_field = if needs_zip64_sizes {
#| MAX_U32
#| } else {
#| compressed_size.to_uint_checked()
#| }
#| let uncomp_field = if needs_zip64_sizes {
#| MAX_U32
#| } else {
#| data_len.to_uint_checked()
#| }
#| let offset_field = if needs_zip64_offset {
#| MAX_U32
#| } else {
#| local_offset.to_uint_checked()
#| }
#| let start = output.size
#| output.grow(46)
#| if output.buf is Some(buf) {
#| buf.unsafe_write_uint32_le(start, CENTRAL_HEADER_SIG)
#| buf.unsafe_write_uint16_le(start + 4, version_made_by.to_uint16())
#| buf.unsafe_write_uint16_le(start + 6, version.to_uint16())
#| buf.unsafe_write_uint16_le(start + 8, flags.to_uint16())
#| buf.unsafe_write_uint16_le(start + 10, method_id.to_uint16())
#| buf.unsafe_write_uint16_le(start + 12, entry.dos_time.to_uint16())
#| buf.unsafe_write_uint16_le(start + 14, entry.dos_date.to_uint16())
#| buf.unsafe_write_uint32_le(start + 16, entry.crc32)
#| buf.unsafe_write_uint32_le(start + 20, comp_field)
#| buf.unsafe_write_uint32_le(start + 24, uncomp_field)
#| buf.unsafe_write_uint16_le(start + 28, name_bytes.length().to_uint16())
#| buf.unsafe_write_uint16_le(start + 30, central_extra.length().to_uint16())
#| buf.unsafe_write_uint32_le(start + 32, 0)
#| buf.unsafe_write_uint16_le(start + 36, 0)
#| buf.unsafe_write_uint32_le(start + 38, entry.external_attributes)
#| buf.unsafe_write_uint32_le(start + 42, offset_field)
#| }
#| output.write_bytes(name_bytes)
#| output.write_bytes(central_extra)
#|}
#|fn Output::write_preserved_central_record(
#| output : Output,
#| source : SourceRecord,
#| local_offset : Int32,
#|) -> Unit raise ZipError {
#| let template = source.central_record
#| guard template.length() >= 46 && template is [u32le(CENTRAL_HEADER_SIG), ..] else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: preserved central record is malformed",
#| )
#| }
#| guard source.central_zip64_offset_position is Some(position) else {
#| output.write_bytes(template[0:42])
#| output.write_u32(local_offset.to_uint_checked())
#| output.write_bytes(template[46:])
#| return
#| }
#| output.write_bytes(template[0:position])
#| output.write_u64(local_offset.to_uint64_checked())
#| output.write_bytes(template[position + 8:])
#|}
#|fn Output::write_generated_classic_end_record(
#| output : Output,
#| zip64 : Bool,
#| total_entries : Int32,
#| central_size : Int32,
#| central_offset : Int32,
#| comment : Bytes,
#|) -> Unit raise ZipError {
#| output.write_u32(END_OF_CENTRAL_SIG)
#| output.write_u16(0)
#| output.write_u16(0)
#| if zip64 {
#| output.write_u16(0xffff)
#| output.write_u16(0xffff)
#| output.write_u32(MAX_U32)
#| output.write_u32(MAX_U32)
#| } else {
#| if total_entries > 0xffff {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: too many entries for a classic archive",
#| )
#| }
#| output.write_u16(total_entries)
#| output.write_u16(total_entries)
#| output.write_u32(central_size.to_uint_checked())
#| output.write_u32(central_offset.to_uint_checked())
#| }
#| if comment.length() > 0xffff {
#| raise ZipError(UnsupportedFeature, "zip: archive comment too long")
#| }
#| output.write_u16(comment.length())
#| output.write_bytes(comment)
#|}
#|fn Output::write_generated_zip64_trailer(
#| output : Output,
#| total_entries : Int32,
#| central_size : Int32,
#| central_offset : Int32,
#| comment : Bytes,
#|) -> Unit raise ZipError {
#| let zip64_offset = output.size
#| output.write_u32(ZIP64_EOCD_SIG)
#| output.write_u64((44).to_uint64_checked())
#| output.write_u16(45)
#| output.write_u16(45)
#| output.write_u32(0)
#| output.write_u32(0)
#| let entry_count = total_entries.to_uint64_checked()
#| output.write_u64(entry_count)
#| output.write_u64(entry_count)
#| output.write_u64(central_size.to_uint64_checked())
#| output.write_u64(central_offset.to_uint64_checked())
#| output.write_u32(ZIP64_LOCATOR_SIG)
#| output.write_u32(0)
#| output.write_u64(zip64_offset.to_uint64_checked())
#| output.write_u32(1)
#| output.write_generated_classic_end_record(
#| true, total_entries, central_size, central_offset, comment,
#| )
#|}
#|fn Output::write_preserved_trailer(
#| output : Output,
#| trailer : TrailerTemplate,
#| total_entries : Int32,
#| central_size : Int32,
#| central_offset : Int32,
#|) -> Unit raise ZipError {
#| let classic = trailer.classic_end_record
#| guard classic.length() >= 22 && classic is [u32le(END_OF_CENTRAL_SIG), ..] else {
#| raise ZipError(UnsupportedFeature, "zip: preserved end record is malformed")
#| }
#| guard trailer.zip64_end_record is Some(end_record) else {
#| output.write_preserved_classic_end_record(
#| classic,
#| total_entries,
#| central_size,
#| central_offset,
#| keep_zip64_sentinels=false,
#| )
#| return
#| }
#| guard end_record.length() >= 56 && end_record is [u32le(ZIP64_EOCD_SIG), ..] else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: preserved zip64 end record is malformed",
#| )
#| }
#| let zip64_offset = output.size
#| output.write_bytes(end_record[0:24])
#| output.write_u64(total_entries.to_uint64_checked())
#| output.write_u64(total_entries.to_uint64_checked())
#| output.write_u64(central_size.to_uint64_checked())
#| output.write_u64(central_offset.to_uint64_checked())
#| output.write_bytes(end_record[56:])
#| guard trailer.zip64_locator is Some(locator) else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: preserved zip64 locator is missing",
#| )
#| }
#| guard locator.length() == 20 && locator is [u32le(ZIP64_LOCATOR_SIG), ..] else {
#| raise ZipError(
#| UnsupportedFeature,
#| "zip: preserved zip64 locator is malformed",
#| )
#| }
#| output.write_bytes(locator[0:8])
#| output.write_u64(zip64_offset.to_uint64_checked())
#| output.write_bytes(locator[16:])
#| output.write_preserved_classic_end_record(
#| classic,
#| total_entries,
#| central_size,
#| central_offset,
#| keep_zip64_sentinels=true,
#| )
#|}
#|fn Output::write_preserved_classic_end_record(
#| output : Output,
#| classic : BytesView,
#| total_entries : Int32,
#| central_size : Int32,
#| central_offset : Int32,
#| keep_zip64_sentinels~ : Bool,
#|) -> Unit raise ZipError {
#| guard classic
#| is [
#| u32le(END_OF_CENTRAL_SIG),
#| u16le(_),
#| u16le(_),
#| u16le(entries_on_disk),
#| u16le(total_on_disk),
#| u32le(size_field),
#| u32le(offset_field),
#| u16le(_),
#| ..,
#| ] else {
#| raise ZipError(UnsupportedFeature, "zip: preserved end record is malformed")
#| }
#| let count = total_entries.min(0xffff)
#| let mut cursor = 0
#| if !keep_zip64_sentinels || entries_on_disk != 0xffff {
#| output.write_bytes(classic[cursor:8])
#| output.write_u16(count)
#| cursor = 10
#| }
#| if !keep_zip64_sentinels || total_on_disk != 0xffff {
#| output.write_bytes(classic[cursor:10])
#| output.write_u16(count)
#| cursor = 12
#| }
#| if !keep_zip64_sentinels || size_field != MAX_U32 {
#| output.write_bytes(classic[cursor:12])
#| output.write_u32(central_size.to_uint_checked())
#| cursor = 16
#| }
#| if !keep_zip64_sentinels || offset_field != MAX_U32 {
#| output.write_bytes(classic[cursor:16])
#| output.write_u32(central_offset.to_uint_checked())
#| cursor = 20
#| }
#| output.write_bytes(classic[cursor:])
#|}
#|priv struct EntryPlan {
#| local_offset : Int32
#| compressed_size : Int32
#| name_bytes : Bytes
#| mut payload : PreparedPayload
#|}
#|fn Archive::local_order(archive : Archive) -> FixedArray[Int32] {
#| let order = FixedArray::makei(archive.entries.length(), index => index)
#| let mut previous = -1
#| let mut fresh_seen = false
#| let mut ordered = true
#| for entry in archive.entries {
#| if entry.origin_local_offset is Some(offset) {
#| if fresh_seen || offset < previous {
#| ordered = false
#| break
#| }
#| previous = offset
#| } else {
#| fresh_seen = true
#| }
#| }
#| guard !ordered else { return order }
#| order.sort_by((left, right) => {
#| match
#| (
#| archive.entries[left].origin_local_offset,
#| archive.entries[right].origin_local_offset,
#| ) {
#| (Some(a), Some(b)) => a.compare(b)
#| (Some(_), None) => -1
#| (None, Some(_)) => 1
#| (None, None) => left.compare(right)
#| }
#| })
#| order
#|}
#|fn prepare_archive(
#| out : Output,
#| archive : Archive,
#| order : FixedArray[Int32],
#| preserve : Bool,
#| level : Int32,
#| fast_store : Bool,
#|) -> Array[EntryPlan] raise ZipError {
#| let plans : Array[EntryPlan] = []
#| let mut compressor : @flate.Compressor? = None
#| for index in order {
#| let entry = archive.entries[index]
#| let local_offset = out.size
#| if preserve && entry.source is Some(source) {
#| out.count(source.local_record.length())
#| plans.push({
#| local_offset,
#| compressed_size: entry.compressed_size,
#| name_bytes: b"",
#| payload: { compressed_size: entry.compressed_size, compressed: None, },
#| })
#| continue
#| }
#| let name_bytes = entry.name_bytes()
#| if out.limit is Some(limit) {
#| guard 30 + name_bytes.length() <= limit - out.size else {
#| raise ZipError(
#| LimitExceeded(
#| kind=OutputBytes,
#| limit=limit.to_uint64(),
#| observed=out.size.to_uint64() +
#| 30UL +
#| name_bytes.length().to_uint64(),
#| ),
#| "zip: output exceeds its byte limit",
#| )
#| }
#| }
#| let payload = match entry.compression {
#| Store => { compressed_size: entry.data.length(), compressed: None, }
#| Deflate => {
#| let codec = compressor.unwrap_or_else(() => {
#| let codec = @flate.Compressor(strategy=Standard(level~), fast_store~)
#| compressor = Some(codec)
#| codec
#| })
#| entry.prepare_payload(codec)
#| }
#| }
#| out.write_local_record(
#| entry,
#| name_bytes,
#| payload,
#| entry.data_descriptor,
#| false,
#| )
#| plans.push({
#| local_offset,
#| compressed_size: payload.compressed_size,
#| name_bytes,
#| payload,
#| })
#| }
#| plans
#|}
#|fn write_directory(
#| out : Output,
#| archive : Archive,
#| positions : FixedArray[Int32],
#| plans : Array[EntryPlan],
#| preserve : Bool,
#|) -> Unit raise ZipError {
#| for index in 0.. Unit raise ZipError {
#| let needs_zip64 = force_zip64 ||
#| total_entries >= 0xffff ||
#| central_size.to_uint_checked() > MAX_U32 ||
#| central_offset.to_uint_checked() > MAX_U32
#| match (preserve, archive.trailer) {
#| (true, Some(trailer)) if !needs_zip64 || trailer.zip64_end_record is Some(_) =>
#| out.write_preserved_trailer(
#| trailer, total_entries, central_size, central_offset,
#| )
#| _ =>
#| if needs_zip64 {
#| out.write_generated_zip64_trailer(
#| total_entries,
#| central_size,
#| central_offset,
#| archive.comment,
#| )
#| } else {
#| out.write_generated_classic_end_record(
#| false,
#| total_entries,
#| central_size,
#| central_offset,
#| archive.comment,
#| )
#| }
#| }
#|}
#|fn write_impl(
#| archive : Archive,
#| preserve : Bool,
#| level : Int32,
#| max_output : Int32?,
#| fast_store : Bool,
#|) -> Bytes raise ZipError {
#| if preserve && archive.original is Some(bytes) {
#| let counting = Output::counting(max_output)
#| counting.count(bytes.length())
#| let out = Output::writing(None, capacity=bytes.length())
#| out.write_bytes(bytes)
#| return out.finish()
#| }
#| let order = archive.local_order()
#| let positions = FixedArray::make(order.length(), 0)
#| for position in 0.. Bytes raise ZipError {
#| if max_output_bytes is Some(limit) {
#| guard limit >= 0 else {
#| raise ZipError(InvalidArgument, "zip: output limit must be non-negative")
#| }
#| }
#| write_impl(archive, preserve, level, max_output_bytes, fast_store)
#|}
),
},
)
///|
let moonbitlang_core_argparse_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/argparse",
deps={},
files={
"arg_action.mbt": (
#|fn arg_min_max_for_validate(arg : Arg) -> (Int32, Int32?) raise ArgBuildError {
#| if arg.info is PositionalInfo(num_args=Some(range), ..) {
#| if range.lower < 0 {
#| raise Unsupported("min values must be >= 0")
#| }
#| if range.upper is Some(max_value) {
#| if max_value < 0 {
#| raise Unsupported("max values must be >= 0")
#| }
#| if max_value < range.lower {
#| raise Unsupported("max values must be >= min values")
#| }
#| if range.lower == 0 && max_value == 0 {
#| raise Unsupported("empty value range (0..0) is unsupported")
#| }
#| }
#| (range.lower, range.upper)
#| } else {
#| (0, None)
#| }
#|}
#|fn arg_min_max(arg : Arg) -> (Int32, Int32?) {
#| match arg.info {
#| PositionalInfo(num_args=Some(range), ..) => (range.lower, range.upper)
#| _ => (0, None)
#| }
#|}
),
"arg_group.mbt": (
#|#derive(@debug.Debug)
#|struct ArgGroup {
#| name : String
#| required : Bool
#| multiple : Bool
#| args : Array[String]
#| requires : Array[String]
#| conflicts_with : Array[String]
#|}
#|#alias(new, deprecated="Use `ArgGroup()` instead")
#|pub fn ArgGroup::ArgGroup(
#| name : StringView,
#| required~ : Bool = false,
#| multiple~ : Bool = true,
#| args~ : ArrayView[String] = [],
#| requires~ : ArrayView[String] = [],
#| conflicts_with~ : ArrayView[String] = [],
#|) -> ArgGroup {
#| {
#| name: name.to_owned(),
#| required,
#| multiple,
#| args: args.to_owned(),
#| requires: requires.to_owned(),
#| conflicts_with: conflicts_with.to_owned(),
#| }
#|}
),
"arg_spec.mbt": (
#|#derive(Eq, @debug.Debug)
#|pub(all) enum FlagAction {
#| SetTrue
#| SetFalse
#| Count
#| Help
#| Version
#|}
#|#derive(Eq, @debug.Debug)
#|pub(all) enum OptionAction {
#| Set
#| Append
#|}
#|#derive(@debug.Debug)
#|priv struct Arg {
#| name : String
#| about : String?
#| env : String?
#| requires : Array[String]
#| conflicts_with : Array[String]
#| required : Bool
#| global : Bool
#| hidden : Bool
#| info : ArgInfo
#| multiple : Bool
#|}
#|#derive(@debug.Debug)
#|priv enum ArgInfo {
#| FlagInfo(
#| short~ : Char?,
#| long~ : String?,
#| action~ : FlagAction,
#| negatable~ : Bool,
#| )
#| OptionInfo(
#| short~ : Char?,
#| long~ : String?,
#| action~ : OptionAction,
#| default_values~ : Array[String]?,
#| allow_hyphen_values~ : Bool,
#| )
#| PositionalInfo(
#| num_args~ : ValueRange?,
#| default_values~ : Array[String]?,
#| allow_hyphen_values~ : Bool,
#| )
#|}
#|#derive(@debug.Debug)
#|struct FlagArg {
#| arg : Arg
#|}
#|#alias(new, deprecated="Use `FlagArg()` instead")
#|pub fn FlagArg::FlagArg(
#| name : StringView,
#| short? : Char,
#| long~ : StringView = name,
#| about? : StringView,
#| action~ : FlagAction = SetTrue,
#| env? : StringView,
#| requires~ : ArrayView[String] = [],
#| conflicts_with~ : ArrayView[String] = [],
#| required~ : Bool = false,
#| global~ : Bool = false,
#| negatable~ : Bool = false,
#| hidden~ : Bool = false,
#|) -> FlagArg {
#| let name = name.to_owned()
#| let long = if long == "" { None } else { Some(long.to_owned()) }
#| let about = about.map(v => v.to_owned())
#| let env = env.map(v => v.to_owned())
#| {
#| arg: {
#| name,
#| about,
#| env,
#| global,
#| hidden,
#| requires: requires.to_owned(),
#| conflicts_with: conflicts_with.to_owned(),
#| required,
#| info: FlagInfo(short~, long~, action~, negatable~),
#| multiple: false,
#| },
#| }
#|}
#|#derive(@debug.Debug)
#|struct OptionArg {
#| arg : Arg
#|}
#|#alias(new, deprecated="Use `OptionArg()` instead")
#|pub fn OptionArg::OptionArg(
#| name : StringView,
#| short? : Char,
#| long~ : StringView = name,
#| about? : StringView,
#| action~ : OptionAction = Set,
#| env? : StringView,
#| default_values? : ArrayView[String],
#| allow_hyphen_values~ : Bool = false,
#| requires~ : ArrayView[String] = [],
#| conflicts_with~ : ArrayView[String] = [],
#| required~ : Bool = false,
#| global~ : Bool = false,
#| hidden~ : Bool = false,
#|) -> OptionArg {
#| let name = name.to_owned()
#| let long = if long == "" { None } else { Some(long.to_owned()) }
#| let about = about.map(v => v.to_owned())
#| let env = env.map(v => v.to_owned())
#| {
#| arg: {
#| name,
#| about,
#| env,
#| requires: requires.to_owned(),
#| conflicts_with: conflicts_with.to_owned(),
#| required,
#| global,
#| hidden,
#| info: OptionInfo(
#| short~,
#| long~,
#| action~,
#| default_values=default_values.map(values => values.to_owned()),
#| allow_hyphen_values~,
#| ),
#| multiple: action is Append,
#| },
#| }
#|}
#|#derive(@debug.Debug)
#|struct PositionArg {
#| arg : Arg
#|}
#|#alias(new, deprecated="Use `PositionArg()` instead")
#|pub fn PositionArg::PositionArg(
#| name : StringView,
#| about? : StringView,
#| env? : StringView,
#| default_values? : ArrayView[String],
#| num_args? : ValueRange,
#| allow_hyphen_values~ : Bool = false,
#| requires~ : ArrayView[String] = [],
#| conflicts_with~ : ArrayView[String] = [],
#| global~ : Bool = false,
#| hidden~ : Bool = false,
#|) -> PositionArg {
#| let name = name.to_owned()
#| let about = about.map(v => v.to_owned())
#| let env = env.map(v => v.to_owned())
#| {
#| arg: {
#| name,
#| about,
#| env,
#| requires: requires.to_owned(),
#| conflicts_with: conflicts_with.to_owned(),
#| required: false,
#| global,
#| hidden,
#| info: PositionalInfo(
#| num_args~,
#| default_values=default_values.map(values => values.to_owned()),
#| allow_hyphen_values~,
#| ),
#| multiple: range_allows_multiple(num_args),
#| },
#| }
#|}
#|fn range_allows_multiple(range : ValueRange?) -> Bool {
#| range is Some(r) &&
#| (match r.upper {
#| Some(upper) => upper > 1
#| None => true
#| })
#|}
#|impl Show for FlagAction with fn to_string(self) {
#| match self {
#| SetTrue => "SetTrue"
#| SetFalse => "SetFalse"
#| Count => "Count"
#| Help => "Help"
#| Version => "Version"
#| }
#|}
#|impl Show for OptionAction with fn to_string(self) {
#| match self {
#| Set => "Set"
#| Append => "Append"
#| }
#|}
),
"command.mbt": (
#|#derive(@debug.Debug)
#|struct Command {
#| name : String
#| args : Array[Arg]
#| groups : Array[ArgGroup]
#| subcommands : Array[Command]
#| about : String?
#| version : String?
#| disable_help_flag : Bool
#| disable_version_flag : Bool
#| disable_help_subcommand : Bool
#| arg_required_else_help : Bool
#| subcommand_required : Bool
#| default_subcommand : String?
#| hidden : Bool
#| mut build_error : ArgBuildError?
#|}
#|#alias(new, deprecated="Use `Command()` instead")
#|pub fn Command::Command(
#| name : StringView,
#| flags~ : ArrayView[FlagArg] = [],
#| options~ : ArrayView[OptionArg] = [],
#| positionals~ : ArrayView[PositionArg] = [],
#| subcommands~ : ArrayView[Command] = [],
#| about? : StringView,
#| version? : StringView,
#| disable_help_flag~ : Bool = false,
#| disable_version_flag~ : Bool = false,
#| disable_help_subcommand~ : Bool = false,
#| arg_required_else_help~ : Bool = false,
#| subcommand_required~ : Bool = false,
#| hidden~ : Bool = false,
#| groups~ : ArrayView[ArgGroup] = [],
#| default_subcommand? : StringView,
#|) -> Command {
#| let (parsed_args, arg_error) = collect_args(flags, options, positionals)
#| let groups = groups.to_owned()
#| let cmd = Command::{
#| name: name.to_owned(),
#| args: parsed_args,
#| groups,
#| subcommands: subcommands.to_owned(),
#| about: about.map(v => v.to_owned()),
#| version: version.map(v => v.to_owned()),
#| disable_help_flag,
#| disable_version_flag,
#| disable_help_subcommand,
#| arg_required_else_help,
#| subcommand_required,
#| default_subcommand: default_subcommand.map(v => v.to_owned()),
#| hidden,
#| build_error: arg_error,
#| }
#| if cmd.build_error is None {
#| validate_command(cmd, parsed_args, groups, []) catch {
#| err => cmd.build_error = Some(err)
#| }
#| }
#| cmd
#|}
#|pub fn Command::render_help(self : Command) -> String {
#| render_help(self)
#|}
#|pub(all) enum ParseOutcome {
#| Parsed(Matches)
#| Help(String)
#| Version(String)
#| Failed(Error)
#|}
#|#as_free_fn
#|pub fn Command::parse_outcome(
#| self : Command,
#| argv~ : ArrayView[String],
#| env~ : Map[String, String] = Map([]),
#|) -> ParseOutcome {
#| try {
#| let raw = parse_command(self, argv, env, [], Map([]), Map([]), self.name)
#| Parsed(build_matches(self, raw, []))
#| } catch {
#| DisplayHelp::Message(text) => Help(trim_single_trailing_newline(text))
#| DisplayVersion::Message(text) => Version(trim_single_trailing_newline(text))
#| err => Failed(err)
#| }
#|}
#|#as_free_fn
#|pub fn Command::parse(
#| self : Command,
#| argv~ : ArrayView[String] = default_argv(),
#| env~ : Map[String, String] = Map([]),
#|) -> Matches raise {
#| try {
#| let raw = parse_command(self, argv, env, [], Map([]), Map([]), self.name)
#| build_matches(self, raw, [])
#| } catch {
#| DisplayHelp::Message(text) => print_and_exit_success(text)
#| DisplayVersion::Message(text) => print_and_exit_success(text)
#| ArgError::Message(_) as err => raise err
#| err => {
#| println(err.to_string())
#| panic()
#| }
#| }
#|}
#|fn build_matches(
#| cmd : Command,
#| raw : Matches,
#| inherited_globals : Array[Arg],
#|) -> Matches {
#| let flags = Map([])
#| let values = Map([])
#| let flag_counts = Map([])
#| let sources = Map([])
#| let specs = inherited_globals + cmd.args
#| for spec in specs {
#| let name = spec.name
#| if raw.values.get(name) is Some(vs) {
#| values[name] = vs.copy()
#| }
#| let count = raw.counts.get_or_default(name, 0)
#| if count > 0 {
#| flag_counts[name] = count
#| }
#| let source = match raw.flag_sources.get(name) {
#| Some(v) => Some(v)
#| None => raw.value_sources.get(name)
#| }
#| if source is Some(source) {
#| sources[name] = source
#| if spec.info is FlagInfo(action~, ..) {
#| if action is Count {
#| flags[name] = count > 0
#| } else {
#| flags[name] = raw.flags.get(name).unwrap_or(false)
#| }
#| }
#| }
#| }
#| let child_globals = merge_global_defs(
#| inherited_globals,
#| collect_globals(cmd.args),
#| )
#| let subcommand = match raw.parsed_subcommand {
#| Some((name, sub_raw)) =>
#| if cmd.subcommands.iter().find_first(sub => sub.name == name)
#| is Some(sub_spec) {
#| Some((name, build_matches(sub_spec, sub_raw, child_globals)))
#| } else {
#| Some((name, new_matches_parse_state()))
#| }
#| None => None
#| }
#| {
#| flags,
#| values,
#| flag_counts,
#| sources,
#| subcommand,
#| counts: Map([]),
#| flag_sources: Map([]),
#| value_sources: Map([]),
#| parsed_subcommand: None,
#| }
#|}
#|fn collect_args(
#| flags : ArrayView[FlagArg],
#| options : ArrayView[OptionArg],
#| positionals : ArrayView[PositionArg],
#|) -> (Array[Arg], ArgBuildError?) {
#| let args : Array[Arg] = []
#| for flag in flags {
#| args.push(flag.arg)
#| }
#| for option in options {
#| args.push(option.arg)
#| }
#| for positional in positionals {
#| args.push(positional.arg)
#| }
#| let ctx = ValidationCtx::new()
#| let first_error : ArgBuildError? = for
#| flag in flags
#| first_error = (None : ArgBuildError?) {
#| validate_flag_arg(flag.arg, ctx) catch {
#| err => if first_error is None { continue Some(err) }
#| }
#| continue first_error
#| } nobreak {
#| first_error
#| }
#| let first_error : ArgBuildError? = for
#| option in options
#| first_error = first_error {
#| validate_option_arg(option.arg, ctx) catch {
#| err => if first_error is None { continue Some(err) }
#| }
#| continue first_error
#| } nobreak {
#| first_error
#| }
#| let first_error : ArgBuildError? = for
#| positional in positionals
#| first_error = first_error {
#| validate_positional_arg(positional.arg, ctx) catch {
#| err => if first_error is None { continue Some(err) }
#| }
#| continue first_error
#| } nobreak {
#| first_error
#| }
#| let first_error = if first_error is None {
#| try {
#| ctx.finalize()
#| None
#| } catch {
#| err => Some(err)
#| }
#| } else {
#| first_error
#| }
#| (args, first_error)
#|}
),
"error.mbt": (
#|priv suberror ArgError {
#| Message(String)
#|}
#|impl Show for ArgError with fn output(self : ArgError, logger) {
#| match self {
#| Message(msg) => logger.write_string(msg)
#| }
#|}
#|impl @debug.Debug for ArgError with fn to_repr(self) {
#| match self {
#| Message(msg) => @debug.Repr::literal(msg)
#| }
#|}
#|test "ArgError debug keeps display text" {
#| let err : Error = ArgError::Message(
#| (
#| $|error: unexpected argument '--bad' found
#| $|
#| $|Usage: demo [options]
#| ),
#| )
#| inspect(
#| err.to_string(),
#| content=(
#| #|error: unexpected argument '--bad' found
#| #|
#| #|Usage: demo [options]
#| ),
#| )
#|}
#|priv suberror ArgParseError {
#| UnknownArgument(String, String?)
#| InvalidArgument(String)
#| MissingValue(String)
#| MissingRequired(String, String?)
#| TooFewValues(String, Int32, Int32)
#| TooManyValues(String, Int32, Int32)
#| TooManyPositionals(String, String?)
#| InvalidValue(String)
#| MissingGroup(String)
#| GroupConflict(String)
#|}
#|fn ArgParseError::arg_parse_error_message(self : ArgParseError) -> String {
#| match self {
#| UnknownArgument(arg, Some(hint)) =>
#| (
#| $|error: unexpected argument '\{arg}' found
#| $|
#| $| tip: a similar argument exists: '\{hint}'
#| )
#| UnknownArgument(arg, None) => "error: unexpected argument '\{arg}' found"
#| InvalidArgument(arg) =>
#| if arg.has_prefix("-") {
#| "error: unexpected argument '\{arg}' found"
#| } else {
#| "error: \{arg}"
#| }
#| MissingValue(arg) =>
#| "error: a value is required for '\{arg}' but none was supplied"
#| MissingRequired(name, by) =>
#| if by is Some(source) {
#| "error: the following required argument was not provided: '\{name}' (required by '\{source}')"
#| } else {
#| "error: the following required argument was not provided: '\{name}'"
#| }
#| TooFewValues(name, got, min) =>
#| "error: '\{name}' requires at least \{min} values but only \{got} were provided"
#| TooManyValues(name, got, max) =>
#| "error: '\{name}' allows at most \{max} values but \{got} were provided"
#| TooManyPositionals(value, Some(arg)) =>
#| "error: unexpected value '\{value}' for '\{arg}' found; no more were expected"
#| TooManyPositionals(value, None) =>
#| "error: unexpected value '\{value}' found; no more were expected"
#| InvalidValue(msg) => "error: \{msg}"
#| MissingGroup(name) =>
#| "error: the following required argument group was not provided: '\{name}'"
#| GroupConflict(name) => "error: group conflict \{name}"
#| }
#|}
#|#derive(@debug.Debug)
#|priv suberror ArgBuildError {
#| Unsupported(String)
#|}
#|priv suberror DisplayHelp {
#| Message(String)
#|}
#|priv suberror DisplayVersion {
#| Message(String)
#|}
),
"extends.mbt": (
#|pub extend FlagAction with Eq::{equal}
#|pub extend FlagAction with Show::{to_string}
#|pub extend OptionAction with Eq::{equal}
#|pub extend OptionAction with Show::{to_string}
#|pub extend ValueRange with Eq::{equal}
#|pub extend ValueRange with Show::{to_string}
#|pub extend ValueSource with Eq::{equal}
#|pub extend ValueSource with Show::{to_string}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ArgGroup with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Command with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend FlagAction with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend FlagAction with Eq::{not_equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend FlagArg with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Matches with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend OptionAction with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend OptionAction with Eq::{not_equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend OptionArg with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PositionArg with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ValueRange with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ValueRange with Eq::{not_equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ValueSource with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ValueSource with Eq::{not_equal}
),
"help_render.mbt": (
#|fn render_help(cmd : Command) -> String {
#| render_help_with_usage_override(cmd, None)
#|}
#|fn render_help_with_usage_override(
#| cmd : Command,
#| usage_line : String?,
#|) -> String {
#| let usage_line = usage_line.unwrap_or("Usage: \{cmd.name}\{usage_tail(cmd)}")
#| let about = cmd.about.unwrap_or("")
#| let about_section = if about == "" {
#| ""
#| } else {
#| (
#| $|
#| $|
#| $|\{about}
#| )
#| }
#| let commands_section = render_section("Commands:", subcommand_entries(cmd))
#| let arguments_section = render_section("Arguments:", positional_entries(cmd))
#| let options_section = render_section(
#| "Options:",
#| option_entries(cmd),
#| keep_empty=true,
#| )
#| let groups_section = render_section("Groups:", group_entries(cmd))
#| (
#| $|\{usage_line}\{about_section}\{commands_section}\{arguments_section}\{options_section}\{groups_section}
#| $|
#| )
#|}
#|fn usage_tail(cmd : Command) -> String {
#| let parts = [
#| required_option_usage(cmd),
#| if has_options(cmd) {
#| "[options]"
#| } else {
#| ""
#| },
#| positional_usage(cmd),
#| subcommand_usage(cmd),
#| ].filter(p => p != "")
#| if parts.is_empty() {
#| ""
#| } else {
#| " \{parts.join(" ")}"
#| }
#|}
#|fn subcommand_usage(cmd : Command) -> String {
#| if !has_subcommands_for_help(cmd) {
#| return ""
#| }
#| if cmd.subcommand_required {
#| ""
#| } else {
#| "[command]"
#| }
#|}
#|fn has_options(cmd : Command) -> Bool {
#| cmd.args.any(arg => {
#| !arg.hidden &&
#| arg.info is (OptionInfo(long~, short~, ..) | FlagInfo(long~, short~, ..)) &&
#| (long is Some(_) || short is Some(_)) &&
#| !is_required_arg(arg)
#| })
#|}
#|fn required_option_usage(cmd : Command) -> String {
#| [
#| for arg in cmd.args if !(arg.hidden ||
#| !is_required_arg(arg) ||
#| arg.info is PositionalInfo(_)) &&
#| required_option_token(arg) is Some(token) => token
#| ].join(" ")
#|}
#|fn required_option_token(arg : Arg) -> String? {
#| let prefix = match arg.info {
#| OptionInfo(long~, short~, ..) | FlagInfo(long~, short~, ..) =>
#| if long is Some(long) {
#| Some("--\{long}")
#| } else if short is Some(short) {
#| Some("-\{short}")
#| } else {
#| None
#| }
#| PositionalInfo(_) => None
#| }
#| match prefix {
#| Some(prefix) =>
#| if arg.info is OptionInfo(_) {
#| Some("\{prefix} <\{arg.name}>")
#| } else {
#| Some(prefix)
#| }
#| None => None
#| }
#|}
#|fn positional_usage(cmd : Command) -> String {
#| let parts = Array(capacity=cmd.args.length())
#| for arg in positional_args(cmd.args) {
#| if arg.hidden {
#| continue
#| }
#| let required = is_required_arg(arg)
#| if arg.multiple {
#| if required {
#| parts.push("<\{arg.name}...>")
#| } else {
#| parts.push("[\{arg.name}...]")
#| }
#| } else if required {
#| parts.push("<\{arg.name}>")
#| } else {
#| parts.push("[\{arg.name}]")
#| }
#| }
#| parts.join(" ")
#|}
#|fn option_entries(cmd : Command) -> Array[String] {
#| let args = cmd.args
#| let display = Array(capacity=args.length() + 2)
#| let builtin_help_short = help_flag_enabled(cmd) &&
#| !has_short_option(args, 'h')
#| let builtin_help_long = help_flag_enabled(cmd) &&
#| !has_long_option(args, "help")
#| let builtin_version_short = version_flag_enabled(cmd) &&
#| !has_short_option(args, 'V')
#| let builtin_version_long = version_flag_enabled(cmd) &&
#| !has_long_option(args, "version")
#| let builtin_help_label = builtin_option_label(
#| builtin_help_short, builtin_help_long, "-h", "--help",
#| )
#| if builtin_help_label is Some(label) {
#| display.push((label, "Show help information."))
#| }
#| let builtin_version_label = builtin_option_label(
#| builtin_version_short, builtin_version_long, "-V", "--version",
#| )
#| if builtin_version_label is Some(label) {
#| display.push((label, "Show version information."))
#| }
#| for arg in args {
#| guard arg.info
#| is (OptionInfo(long~, short~, ..) | FlagInfo(long~, short~, ..)) &&
#| (long is Some(_) || short is Some(_)) else {
#| continue
#| }
#| if arg.hidden {
#| continue
#| }
#| let name = if arg.info is OptionInfo(_) {
#| "\{arg_display(arg)} <\{arg.name}>"
#| } else {
#| arg_display(arg)
#| }
#| display.push((name, arg_doc(arg)))
#| }
#| format_entries(display)
#|}
#|fn has_long_option(args : Array[Arg], name : String) -> Bool {
#| args.any(arg => {
#| arg.info is (OptionInfo(long~, ..) | FlagInfo(long~, ..)) &&
#| long is Some(long) &&
#| long == name
#| })
#|}
#|fn has_short_option(args : Array[Arg], value : Char) -> Bool {
#| args.any(arg => {
#| arg.info is (OptionInfo(short~, ..) | FlagInfo(short~, ..)) &&
#| short is Some(short) &&
#| short == value
#| })
#|}
#|fn builtin_option_label(
#| has_short : Bool,
#| has_long : Bool,
#| short_label : String,
#| long_label : String,
#|) -> String? {
#| if has_short && has_long {
#| Some("\{short_label}, \{long_label}")
#| } else if has_short {
#| Some(short_label)
#| } else if has_long {
#| Some(long_label)
#| } else {
#| None
#| }
#|}
#|fn positional_entries(cmd : Command) -> Array[String] {
#| format_entries(
#| [
#| for arg in positional_args(cmd.args) if !arg.hidden => {
#| (positional_display(arg), arg_doc(arg))
#| }
#| ],
#| )
#|}
#|fn subcommand_entries(cmd : Command) -> Array[String] {
#| let display = [
#| for sub in cmd.subcommands if !sub.hidden => {
#| let doc = sub.about.unwrap_or("")
#| let doc = if cmd.default_subcommand is Some(name) && name == sub.name {
#| if doc == "" {
#| "(default)"
#| } else {
#| "\{doc} (default)"
#| }
#| } else {
#| doc
#| }
#| (sub.name, doc)
#| }
#| ]
#| if help_subcommand_enabled(cmd) {
#| display.push(("help", "Print help for the subcommand(s)."))
#| }
#| format_entries(display)
#|}
#|fn group_entries(cmd : Command) -> Array[String] {
#| let display = Array(capacity=cmd.groups.length())
#| for group in cmd.groups {
#| let members = group_members(cmd, group)
#| if members == "" {
#| continue
#| }
#| display.push((group_label(group), members))
#| }
#| format_entries(display)
#|}
#|fn render_section(
#| header : String,
#| lines : Array[String],
#| keep_empty~ : Bool = false,
#|) -> String {
#| if lines.is_empty() {
#| if keep_empty {
#| (
#| $|
#| $|
#| $|\{header}
#| )
#| } else {
#| ""
#| }
#| } else {
#| let body = lines.join("\n")
#| (
#| $|
#| $|
#| $|\{header}
#| $|\{body}
#| )
#| }
#|}
#|fn format_entries(display : Array[(String, String)]) -> Array[String] {
#| let max_len = display.fold(init=0, (acc, item) => acc.max(item.0.length()))
#| fn fmt(name : String, doc : String) -> String {
#| let padding = " ".repeat(max_len - name.length() + 2)
#| " \{name}\{padding}\{doc}"
#| }
#| [
#| for item in display => fmt(item.0, item.1)
#| ]
#|}
#|fn arg_display(arg : Arg) -> String {
#| let parts = Array(capacity=2)
#| let (short, long) = match arg.info {
#| OptionInfo(short~, long~, ..)
#| | FlagInfo(short~, long~, ..)
#| | (PositionalInfo(_) with short = None, long = None) => (short, long)
#| }
#| if short is Some(short) {
#| parts.push("-\{short}")
#| }
#| if long is Some(long) {
#| if arg.info is FlagInfo(negatable=true, ..) {
#| parts.push("--[no-]\{long}")
#| } else {
#| parts.push("--\{long}")
#| }
#| }
#| if parts.is_empty() {
#| arg.name
#| } else {
#| parts.join(", ")
#| }
#|}
#|fn positional_display(arg : Arg) -> String {
#| if arg.multiple {
#| "\{arg.name}..."
#| } else {
#| arg.name
#| }
#|}
#|fn arg_doc(arg : Arg) -> String {
#| let notes = []
#| if arg.env is Some(env_name) {
#| notes.push("[env: \{env_name}]")
#| }
#| if arg.info
#| is (OptionInfo(default_values~, ..) | PositionalInfo(default_values~, ..)) &&
#| default_values is Some(values) {
#| if !values.is_empty() {
#| let defaults = values.join(", ")
#| notes.push("[default: \{defaults}]")
#| }
#| }
#| let help = arg.about.unwrap_or("")
#| if help == "" {
#| notes.join(" ")
#| } else if !notes.is_empty() {
#| let notes_text = notes.join(" ")
#| "\{help} \{notes_text}"
#| } else {
#| help
#| }
#|}
#|fn has_subcommands_for_help(cmd : Command) -> Bool {
#| help_subcommand_enabled(cmd) || cmd.subcommands.any(sub => !sub.hidden)
#|}
#|fn is_required_arg(arg : Arg) -> Bool {
#| if arg.required {
#| true
#| } else {
#| let (min, _) = arg_min_max(arg)
#| min > 0
#| }
#|}
#|fn group_label(group : ArgGroup) -> String {
#| let tags = []
#| if group.required {
#| tags.push("[required]")
#| }
#| if !group.multiple {
#| tags.push("[exclusive]")
#| }
#| if tags.is_empty() {
#| group.name
#| } else {
#| let tags_text = tags.join(" ")
#| "\{group.name} \{tags_text}"
#| }
#|}
#|fn group_members(cmd : Command, group : ArgGroup) -> String {
#| [
#| for arg in cmd.args if !arg.hidden && arg_in_group(arg, group) => {
#| group_member_display(arg)
#| }
#| ].join(", ")
#|}
#|fn group_member_display(arg : Arg) -> String {
#| let base = arg_display(arg)
#| match arg.info {
#| FlagInfo(_) => base
#| OptionInfo(_) => "\{base} <\{arg.name}>"
#| PositionalInfo(_) => base
#| }
#|}
),
"matches.mbt": (
#|#derive(Eq, Show, @debug.Debug)
#|#warnings("-deprecated_syntax")
#|pub enum ValueSource {
#| Argv
#| Env
#| Default
#|}
#|#derive(@debug.Debug)
#|pub struct Matches {
#| flags : Map[String, Bool]
#| values : Map[String, Array[String]]
#| flag_counts : Map[String, Int32]
#| sources : Map[String, ValueSource]
#| subcommand : (String, Matches)?
#| priv counts : Map[String, Int32]
#| priv flag_sources : Map[String, ValueSource]
#| priv value_sources : Map[String, ValueSource]
#| priv mut parsed_subcommand : (String, Matches)?
#|}
#|fn new_matches_parse_state() -> Matches {
#| {
#| flags: Map([]),
#| values: Map([]),
#| flag_counts: Map([]),
#| sources: Map([]),
#| subcommand: None,
#| counts: Map([]),
#| flag_sources: Map([]),
#| value_sources: Map([]),
#| parsed_subcommand: None,
#| }
#|}
),
"parser.mbt": (
#|fn raise_help(text : String) -> Unit raise DisplayHelp {
#| raise Message(text)
#|}
#|fn raise_version(text : String) -> Unit raise DisplayVersion {
#| raise Message(text)
#|}
#|fn[T] raise_unknown_long(
#| name : StringView,
#| long_index : Map[String, Arg],
#|) -> T raise ArgParseError {
#| let hint = suggest_long(name, long_index)
#| raise UnknownArgument("--\{name}", hint)
#|}
#|fn[T] raise_unknown_short(
#| short : Char,
#| short_index : Map[Char, Arg],
#|) -> T raise ArgParseError {
#| let hint = suggest_short(short, short_index)
#| raise UnknownArgument("-\{short}", hint)
#|}
#|fn[T] raise_subcommand_conflict(name : String) -> T raise ArgParseError {
#| raise InvalidArgument(
#| "subcommand '\{name}' cannot be used with positional arguments",
#| )
#|}
#|fn help_context_command(
#| cmd : Command,
#| inherited_globals : Array[Arg],
#| command_path : String,
#|) -> Command {
#| let help_name = if command_path == "" { cmd.name } else { command_path }
#| { ..cmd, args: inherited_globals + cmd.args, name: help_name, }
#|}
#|fn render_help_for_context(
#| cmd : Command,
#| inherited_globals : Array[Arg],
#| command_path : String,
#|) -> String {
#| let help_cmd = help_context_command(cmd, inherited_globals, command_path)
#| render_help(help_cmd)
#|}
#|fn raise_context_help(
#| cmd : Command,
#| inherited_globals : Array[Arg],
#| command_path : String,
#|) -> Unit raise DisplayHelp {
#| raise_help(render_help_for_context(cmd, inherited_globals, command_path))
#|}
#|#cfg(target="js")
#|fn default_argv() -> ArrayView[String] {
#| match @env.args() {
#| [_, _, .. rest] => rest
#| _ => []
#| }
#|}
#|#cfg(not(target="js"))
#|fn default_argv() -> ArrayView[String] {
#| let args = @env.args()
#| match args {
#| [_, .. rest] => rest
#| [] => []
#| }
#|}
#|fn merge_global_defs(
#| inherited_globals : Array[Arg],
#| globals_here : Array[Arg],
#|) -> Array[Arg] {
#| let merged = inherited_globals.copy()
#| for global in globals_here {
#| match merged.search_by(arg => arg.name == global.name) {
#| Some(idx) => merged[idx] = global
#| None => merged.push(global)
#| }
#| }
#| merged
#|}
#|fn env_resolution_args(
#| inherited_globals : Array[Arg],
#| args : Array[Arg],
#|) -> Array[Arg] {
#| let merged_globals = merge_global_defs(
#| inherited_globals,
#| collect_globals(args),
#| )
#| let local_only = args.filter(arg => {
#| !(arg.global && arg.info is (FlagInfo(_) | OptionInfo(_)))
#| })
#| merged_globals + local_only
#|}
#|fn format_error_with_help(
#| msg : String,
#| cmd : Command,
#| inherited_globals : Array[Arg],
#| command_path : String,
#|) -> String {
#| (
#| $|\{msg}
#| $|
#| $|\{render_help_for_context(cmd, inherited_globals, command_path)}
#| )
#|}
#|fn arg_usage_token_for_group(arg : Arg) -> String {
#| let base = match arg.info {
#| OptionInfo(long~, short~, ..) | FlagInfo(long~, short~, ..) =>
#| if long is Some(long) {
#| "--\{long}"
#| } else if short is Some(short) {
#| "-\{short}"
#| } else {
#| arg.name
#| }
#| PositionalInfo(_) =>
#| if arg.multiple {
#| "<\{arg.name}...>"
#| } else {
#| "<\{arg.name}>"
#| }
#| }
#| if arg.info is OptionInfo(_) {
#| "\{base} <\{arg.name}>"
#| } else {
#| base
#| }
#|}
#|fn group_usage_expr(
#| groups : Array[ArgGroup],
#| args : Array[Arg],
#| name : String,
#|) -> String? {
#| for group in groups {
#| if group.name != name {
#| continue
#| }
#| let members = [
#| for member_name in group.args if args.search_by(arg => {
#| arg.name == member_name
#| })
#| is Some(idx) &&
#| !args[idx].hidden => arg_usage_token_for_group(args[idx])
#| ]
#| if members.is_empty() {
#| return None
#| }
#| let joined = members.join("|")
#| return Some("<\{joined}>")
#| }
#| None
#|}
#|fn missing_group_error_message(
#| cmd : Command,
#| inherited_globals : Array[Arg],
#| command_path : String,
#| group_name : String,
#|) -> String {
#| let help_cmd = help_context_command(cmd, inherited_globals, command_path)
#| if group_usage_expr(help_cmd.groups, help_cmd.args, group_name) is Some(expr) {
#| (
#| $|error: the following required arguments were not provided:
#| $| \{expr}
#| )
#| } else {
#| MissingGroup(group_name).arg_parse_error_message()
#| }
#|}
#|fn arg_error_for_parse_failure(
#| err : ArgParseError,
#| cmd : Command,
#| inherited_globals : Array[Arg],
#| command_path : String,
#|) -> ArgError {
#| let message = match err {
#| MissingGroup(name) =>
#| missing_group_error_message(cmd, inherited_globals, command_path, name)
#| TooManyPositionals(value, _) => {
#| let candidates = [ for sub in cmd.subcommands => sub.name ]
#| if help_subcommand_enabled(cmd) {
#| candidates.push("help")
#| }
#| if suggest_name(value, candidates) is Some(best) {
#| (
#| $|\{err.arg_parse_error_message()}
#| $|
#| $| tip: a similar subcommand exists: '\{best}'
#| )
#| } else {
#| err.arg_parse_error_message()
#| }
#| }
#| _ => err.arg_parse_error_message()
#| }
#| Message(format_error_with_help(message, cmd, inherited_globals, command_path))
#|}
#|fn parse_command(
#| cmd : Command,
#| argv : ArrayView[String],
#| env : Map[String, String],
#| inherited_globals : Array[Arg],
#| inherited_version_long : Map[String, String],
#| inherited_version_short : Map[Char, String],
#| command_path : String,
#| seed_matches~ : Matches = new_matches_parse_state(),
#|) -> Matches raise {
#| parse_command_impl(
#| cmd, argv, env, inherited_globals, inherited_version_long, inherited_version_short,
#| command_path, seed_matches,
#| ) catch {
#| UnknownArgument(arg, hint) =>
#| raise arg_error_for_parse_failure(
#| UnknownArgument(arg, hint),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| InvalidArgument(msg) =>
#| raise arg_error_for_parse_failure(
#| InvalidArgument(msg),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| MissingValue(name) =>
#| raise arg_error_for_parse_failure(
#| MissingValue(name),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| MissingRequired(name, by) =>
#| raise arg_error_for_parse_failure(
#| MissingRequired(name, by),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| TooFewValues(name, got, min) =>
#| raise arg_error_for_parse_failure(
#| TooFewValues(name, got, min),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| TooManyValues(name, got, max) =>
#| raise arg_error_for_parse_failure(
#| TooManyValues(name, got, max),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| TooManyPositionals(value, arg) =>
#| raise arg_error_for_parse_failure(
#| TooManyPositionals(value, arg),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| InvalidValue(msg) =>
#| raise arg_error_for_parse_failure(
#| InvalidValue(msg),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| MissingGroup(name) =>
#| raise arg_error_for_parse_failure(
#| MissingGroup(name),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| GroupConflict(name) =>
#| raise arg_error_for_parse_failure(
#| GroupConflict(name),
#| cmd,
#| inherited_globals,
#| command_path,
#| )
#| Unsupported(msg) =>
#| raise ArgError::Message(
#| "error: command definition validation failed: \{msg}",
#| )
#| err => raise err
#| }
#|}
#|fn parse_command_impl(
#| cmd : Command,
#| argv : ArrayView[String],
#| env : Map[String, String],
#| inherited_globals : Array[Arg],
#| inherited_version_long : Map[String, String],
#| inherited_version_short : Map[Char, String],
#| command_path : String,
#| seed_matches : Matches,
#|) -> Matches raise {
#| if cmd.build_error is Some(err) {
#| raise err
#| }
#| let args = cmd.args
#| let groups = cmd.groups
#| let subcommands = cmd.subcommands
#| if cmd.arg_required_else_help && argv.is_empty() {
#| raise_context_help(cmd, inherited_globals, command_path)
#| }
#| let matches = seed_matches
#| let globals_here = collect_globals(args)
#| let child_globals = merge_global_defs(inherited_globals, globals_here)
#| let child_version_long = inherited_version_long.copy()
#| let child_version_short = inherited_version_short.copy()
#| for global in globals_here {
#| if global.info is FlagInfo(long~, short~, action=Version, ..) {
#| if long is Some(name) {
#| child_version_long[name] = command_version(cmd)
#| }
#| if short is Some(short) {
#| child_version_short[short] = command_version(cmd)
#| }
#| }
#| }
#| let long_index = build_long_index(inherited_globals, args)
#| let short_index = build_short_index(inherited_globals, args)
#| let builtin_help_short = help_flag_enabled(cmd) &&
#| short_index.get('h') is None
#| let builtin_help_long = help_flag_enabled(cmd) &&
#| long_index.get("help") is None
#| let builtin_version_short = version_flag_enabled(cmd) &&
#| short_index.get('V') is None
#| let builtin_version_long = version_flag_enabled(cmd) &&
#| long_index.get("version") is None
#| let positionals = positional_args(args)
#| let positional_values = []
#| let mut i = 0
#| let mut positional_arg_found = false
#| let default_subcommand = match cmd.default_subcommand {
#| Some(default_name) =>
#| subcommands
#| .iter()
#| .find_first(sub => sub.name == default_name && !sub.hidden)
#| None => None
#| }
#| fn dispatch_subcommand(
#| sub : Command,
#| rest : ArrayView[String],
#| ) -> Matches raise _ {
#| let sub_path = if command_path == "" {
#| sub.name
#| } else {
#| "\{command_path} \{sub.name}"
#| }
#| let child_local_non_globals = collect_non_global_names(sub.args)
#| let child_seed = seed_child_globals_from_parent(
#| matches, child_globals, child_local_non_globals,
#| )
#| let sub_matches = parse_command(
#| sub,
#| rest,
#| env,
#| child_globals,
#| child_version_long,
#| child_version_short,
#| sub_path,
#| seed_matches=child_seed,
#| )
#| matches.parsed_subcommand = Some((sub.name, sub_matches))
#| merge_globals_from_child(
#| matches, sub_matches, child_globals, child_local_non_globals,
#| )
#| let env_args = env_resolution_args(inherited_globals, args)
#| let parent_matches = finalize_matches(
#| cmd, args, groups, matches, positionals, positional_values, env_args, env,
#| )
#| validate_relationships(parent_matches, args)
#| if parent_matches.parsed_subcommand is Some((sub_name, sub_m)) {
#| propagate_globals_to_child(
#| parent_matches, sub_m, child_globals, child_local_non_globals,
#| )
#| parent_matches.parsed_subcommand = Some((sub_name, sub_m))
#| }
#| parent_matches
#| }
#| while i < argv.length() {
#| let arg = argv[i]
#| if arg == "--" {
#| if default_subcommand is Some(sub) {
#| return dispatch_subcommand(sub, argv[i:])
#| }
#| if i + 1 < argv.length() {
#| positional_arg_found = true
#| }
#| positional_values.append(argv[i + 1:])
#| break
#| }
#| if builtin_help_short && arg == "-h" {
#| raise_context_help(cmd, inherited_globals, command_path)
#| }
#| if builtin_help_long && arg == "--help" {
#| raise_context_help(cmd, inherited_globals, command_path)
#| }
#| if builtin_version_short && arg == "-V" {
#| raise_version(command_version(cmd))
#| }
#| if builtin_version_long && arg == "--version" {
#| raise_version(command_version(cmd))
#| }
#| if should_parse_as_positional(
#| arg, positionals, positional_values, long_index, short_index,
#| ) {
#| positional_values.push(arg)
#| positional_arg_found = true
#| i += 1
#| continue
#| }
#| if arg.has_prefix("--") {
#| let (name, inline) = split_long(arg)
#| if builtin_help_long && name == "help" {
#| if inline is Some(_) {
#| raise ArgParseError::InvalidArgument(arg)
#| }
#| raise_context_help(cmd, inherited_globals, command_path)
#| }
#| if builtin_version_long && name == "version" {
#| if inline is Some(_) {
#| raise ArgParseError::InvalidArgument(arg)
#| }
#| raise_version(command_version(cmd))
#| }
#| match long_index.get_from_string(name) {
#| None =>
#| if lookup_negated_flag(long_index, name)
#| is Some({ info: FlagInfo(action~, ..), .. } as spec) {
#| if inline is Some(_) {
#| raise ArgParseError::InvalidArgument(arg)
#| }
#| let value = action is SetFalse
#| if action is Count {
#| matches.counts[spec.name] = 0
#| }
#| matches.flags[spec.name] = value
#| matches.flag_sources[spec.name] = Argv
#| } else if default_subcommand is Some(sub) {
#| return dispatch_subcommand(sub, argv[i:])
#| } else {
#| raise_unknown_long(name, long_index)
#| }
#| Some(spec) =>
#| if spec.info is (OptionInfo(_) | PositionalInfo(_)) {
#| check_duplicate_set_occurrence(matches, spec)
#| if inline is Some(v) {
#| assign_value(matches, spec, v, Argv)
#| } else {
#| let can_take_next = i + 1 < argv.length() &&
#| !should_stop_option_value(argv[i + 1], spec)
#| if can_take_next {
#| i += 1
#| assign_value(matches, spec, argv[i], Argv)
#| } else {
#| raise ArgParseError::MissingValue("--\{name}")
#| }
#| }
#| } else {
#| if inline is Some(_) {
#| raise ArgParseError::InvalidArgument(arg)
#| }
#| match spec.info {
#| FlagInfo(action=Help, ..) =>
#| raise_context_help(cmd, inherited_globals, command_path)
#| FlagInfo(action=Version, ..) =>
#| raise_version(
#| version_text_for_long_action(
#| cmd, name, inherited_version_long,
#| ),
#| )
#| _ => apply_flag(matches, spec, Argv)
#| }
#| }
#| }
#| i += 1
#| continue
#| }
#| if arg.has_prefix("-") && arg != "-" {
#| let chars = arg.iter()
#| ignore(chars.next())
#| let mut consumed_next = false
#| while chars.next() is Some(short) {
#| if short == 'h' && builtin_help_short {
#| raise_context_help(cmd, inherited_globals, command_path)
#| }
#| if short == 'V' && builtin_version_short {
#| raise_version(command_version(cmd))
#| }
#| let spec = match short_index.get(short) {
#| Some(v) => v
#| None =>
#| if default_subcommand is Some(sub) {
#| let remaining = Array(capacity=argv.length() - i)
#| remaining.push("-\{short}\{String::from_iter(chars)}")
#| remaining.append(argv[i + 1:])
#| return dispatch_subcommand(sub, remaining)
#| } else {
#| raise_unknown_short(short, short_index)
#| }
#| }
#| if spec.info is (OptionInfo(_) | PositionalInfo(_)) {
#| check_duplicate_set_occurrence(matches, spec)
#| let rest = String::from_iter(chars)
#| if rest != "" {
#| let inline = match rest.strip_prefix("=") {
#| Some(view) => view.to_owned()
#| None => rest
#| }
#| assign_value(matches, spec, inline, Argv)
#| } else {
#| let can_take_next = i + 1 < argv.length() &&
#| !should_stop_option_value(argv[i + 1], spec)
#| if can_take_next {
#| consumed_next = true
#| assign_value(matches, spec, argv[i + 1], Argv)
#| } else {
#| raise ArgParseError::MissingValue("-\{short}")
#| }
#| }
#| break
#| } else {
#| match spec.info {
#| FlagInfo(action=Help, ..) =>
#| raise_context_help(cmd, inherited_globals, command_path)
#| FlagInfo(action=Version, ..) =>
#| raise_version(
#| version_text_for_short_action(
#| cmd, short, inherited_version_short,
#| ),
#| )
#| _ => apply_flag(matches, spec, Argv)
#| }
#| }
#| }
#| i += 1 + (if consumed_next { 1 } else { 0 })
#| continue
#| }
#| if help_subcommand_enabled(cmd) && arg == "help" {
#| if positional_arg_found {
#| raise_subcommand_conflict("help")
#| }
#| let rest = argv[i + 1:]
#| let (target, target_globals, target_path) = resolve_help_target(
#| cmd, rest, builtin_help_short, builtin_help_long, inherited_globals, command_path,
#| )
#| let text = render_help_for_context(target, target_globals, target_path)
#| raise_help(text)
#| }
#| if subcommands.iter().find_first(sub => sub.name == arg) is Some(sub) {
#| if positional_arg_found {
#| raise_subcommand_conflict(sub.name)
#| }
#| return dispatch_subcommand(sub, argv[i + 1:])
#| }
#| if default_subcommand is Some(sub) {
#| return dispatch_subcommand(sub, argv[i:])
#| }
#| positional_values.push(arg)
#| positional_arg_found = true
#| i += 1
#| }
#| if default_subcommand is Some(sub) {
#| return dispatch_subcommand(sub, [])
#| }
#| let env_args = env_resolution_args(inherited_globals, args)
#| let final_matches = finalize_matches(
#| cmd, args, groups, matches, positionals, positional_values, env_args, env,
#| )
#| validate_relationships(final_matches, args)
#| final_matches
#|}
#|fn finalize_matches(
#| cmd : Command,
#| args : Array[Arg],
#| groups : Array[ArgGroup],
#| matches : Matches,
#| positionals : Array[Arg],
#| positional_values : Array[String],
#| env_args : Array[Arg],
#| env : Map[String, String],
#|) -> Matches raise ArgParseError {
#| assign_positionals(matches, positionals, positional_values)
#| apply_env(matches, env_args, env)
#| apply_defaults(matches, env_args)
#| validate_values(args, matches)
#| validate_groups(args, groups, matches)
#| validate_command_policies(cmd, matches)
#| matches
#|}
#|fn help_subcommand_enabled(cmd : Command) -> Bool {
#| !cmd.disable_help_subcommand && !cmd.subcommands.is_empty()
#|}
#|fn help_flag_enabled(cmd : Command) -> Bool {
#| !cmd.disable_help_flag
#|}
#|fn version_flag_enabled(cmd : Command) -> Bool {
#| !cmd.disable_version_flag && cmd.version is Some(_)
#|}
#|fn command_version(cmd : Command) -> String {
#| cmd.version.unwrap_or("")
#|}
#|fn version_text_for_long_action(
#| cmd : Command,
#| long : StringView,
#| inherited_version_long : Map[String, String],
#|) -> String {
#| for arg in cmd.args {
#| if arg.info is FlagInfo(long=Some(name), action=Version, ..) &&
#| name[:] == long {
#| return command_version(cmd)
#| }
#| }
#| inherited_version_long.get_from_string(long).unwrap_or(command_version(cmd))
#|}
#|fn version_text_for_short_action(
#| cmd : Command,
#| short : Char,
#| inherited_version_short : Map[Char, String],
#|) -> String {
#| for arg in cmd.args {
#| if arg.info is FlagInfo(short=Some(value), action=Version, ..) &&
#| value == short {
#| return command_version(cmd)
#| }
#| }
#| inherited_version_short.get(short).unwrap_or(command_version(cmd))
#|}
),
"parser_globals_merge.mbt": (
#|fn source_priority(source : ValueSource?) -> Int32 {
#| match source {
#| Some(Argv) => 3
#| Some(Env) => 2
#| Some(Default) => 1
#| None => 0
#| }
#|}
#|fn prefer_child_source(
#| parent_source : ValueSource?,
#| child_source : ValueSource?,
#|) -> Bool {
#| let parent_priority = source_priority(parent_source)
#| let child_priority = source_priority(child_source)
#| if child_priority > parent_priority {
#| true
#| } else if child_priority < parent_priority {
#| false
#| } else {
#| child_source is Some(Argv)
#| }
#|}
#|fn merge_global_value_from_child(
#| parent : Matches,
#| child : Matches,
#| arg : Arg,
#| name : String,
#|) -> Unit raise ArgParseError {
#| let parent_vals = parent.values.get(name)
#| let child_vals = child.values.get(name)
#| let parent_source = parent.value_sources.get(name)
#| let child_source = child.value_sources.get(name)
#| let has_parent = parent_vals is Some(pv) && !pv.is_empty()
#| let has_child = child_vals is Some(cv) && !cv.is_empty()
#| if !has_parent && !has_child {
#| return
#| }
#| if arg.multiple || arg.info is OptionInfo(action=Append, ..) {
#| let both_argv = parent_source is Some(Argv) && child_source is Some(Argv)
#| if both_argv {
#| let merged = parent_vals.unwrap_or([]) + child_vals.unwrap_or([])
#| if !merged.is_empty() {
#| parent.values[name] = merged
#| parent.value_sources[name] = Argv
#| }
#| return
#| }
#| } else if has_parent &&
#| has_child &&
#| parent_source is Some(Argv) &&
#| child_source is Some(Argv) &&
#| arg.info is OptionInfo(action=Set, ..) {
#| raise InvalidArgument(
#| "argument '\{option_conflict_label(arg)}' cannot be used multiple times",
#| )
#| }
#| let choose_child = has_child &&
#| (!has_parent || prefer_child_source(parent_source, child_source))
#| if choose_child {
#| if child_vals is Some(cv) && !cv.is_empty() {
#| parent.values[name] = cv.copy()
#| }
#| if child_source is Some(src) {
#| parent.value_sources[name] = src
#| }
#| } else if parent_vals is Some(pv) && !pv.is_empty() {
#| parent.values[name] = pv.copy()
#| if parent_source is Some(src) {
#| parent.value_sources[name] = src
#| }
#| }
#|}
#|fn merge_global_flag_from_child(
#| parent : Matches,
#| child : Matches,
#| arg : Arg,
#| name : String,
#|) -> Unit {
#| guard child.flags.get(name) is Some(v) else { return }
#| if arg.info is FlagInfo(action=Count, ..) {
#| let has_parent = parent.flags.get(name) is Some(_)
#| let parent_source = parent.flag_sources.get(name)
#| let child_source = child.flag_sources.get(name)
#| let choose_child = !has_parent ||
#| prefer_child_source(parent_source, child_source)
#| if choose_child {
#| let child_count = child.counts.get(name).unwrap_or(0)
#| parent.counts[name] = child_count
#| parent.flags[name] = child_count > 0
#| if child_source is Some(src) {
#| parent.flag_sources[name] = src
#| }
#| }
#| } else {
#| let has_parent = parent.flags.get(name) is Some(_)
#| let parent_source = parent.flag_sources.get(name)
#| let child_source = child.flag_sources.get(name)
#| let choose_child = !has_parent ||
#| prefer_child_source(parent_source, child_source)
#| if choose_child {
#| parent.flags[name] = v
#| if child_source is Some(src) {
#| parent.flag_sources[name] = src
#| }
#| }
#| }
#|}
#|fn seed_child_globals_from_parent(
#| parent : Matches,
#| globals : Array[Arg],
#| child_local_non_globals : @set.Set[String],
#|) -> Matches {
#| let seed = new_matches_parse_state()
#| let seen : @set.Set[String] = Set([])
#| for arg in globals {
#| let name = arg.name
#| if child_local_non_globals.contains(name) || !seen.insert_and_check(name) {
#| continue
#| }
#| if arg.info is (OptionInfo(_) | PositionalInfo(_)) {
#| if parent.values.get(name) is Some(_) &&
#| parent.value_sources.get(name) is Some(Argv) {
#| seed.values[name] = []
#| seed.value_sources[name] = Argv
#| }
#| continue
#| }
#| if parent.flags.get(name) is Some(v) &&
#| parent.flag_sources.get(name) is Some(Argv) {
#| seed.flags[name] = v
#| seed.flag_sources[name] = Argv
#| if arg.info is FlagInfo(action=Count, ..) {
#| seed.counts[name] = parent.counts.get(name).unwrap_or(0)
#| }
#| }
#| }
#| seed
#|}
#|fn merge_globals_from_child(
#| parent : Matches,
#| child : Matches,
#| globals : Array[Arg],
#| child_local_non_globals : @set.Set[String],
#|) -> Unit raise ArgParseError {
#| for arg in globals {
#| let name = arg.name
#| if child_local_non_globals.contains(name) {
#| continue
#| }
#| match arg.info {
#| OptionInfo(_) | PositionalInfo(_) =>
#| merge_global_value_from_child(parent, child, arg, name)
#| FlagInfo(_) => merge_global_flag_from_child(parent, child, arg, name)
#| }
#| }
#|}
#|fn propagate_globals_to_child(
#| parent : Matches,
#| child : Matches,
#| globals : Array[Arg],
#| child_local_non_globals : @set.Set[String],
#|) -> Unit {
#| for arg in globals {
#| let name = arg.name
#| if child_local_non_globals.contains(name) {
#| continue
#| }
#| if arg.info is (OptionInfo(_) | PositionalInfo(_)) {
#| if parent.values.get(name) is Some(values) {
#| child.values[name] = values.copy()
#| if parent.value_sources.get(name) is Some(src) {
#| child.value_sources[name] = src
#| }
#| }
#| } else if parent.flags.get(name) is Some(v) {
#| child.flags[name] = v
#| if parent.flag_sources.get(name) is Some(src) {
#| child.flag_sources[name] = src
#| }
#| if arg.info is FlagInfo(action=Count, ..) &&
#| parent.counts.get(name) is Some(c) {
#| child.counts[name] = c
#| }
#| }
#| }
#| if child.parsed_subcommand is Some((sub_name, sub_m)) {
#| propagate_globals_to_child(parent, sub_m, globals, Set([]))
#| child.parsed_subcommand = Some((sub_name, sub_m))
#| }
#|}
),
"parser_lookup.mbt": (
#|fn build_long_index(
#| globals : Array[Arg],
#| args : Array[Arg],
#|) -> Map[String, Arg] {
#| Map(
#| [
#| for arg in [..globals, ..args] if arg.info
#| is (FlagInfo(long~, ..) | OptionInfo(long~, ..)) &&
#| long is Some(name) => (name, arg)
#| ],
#| )
#|}
#|fn lookup_negated_flag(
#| long_index : Map[String, Arg],
#| name : StringView,
#|) -> Arg? {
#| guard name is [.. "no-", .. target] else { return None }
#| match long_index.get_from_string(target) {
#| Some({ info: FlagInfo(negatable=true, ..), .. } as spec) => Some(spec)
#| _ => None
#| }
#|}
#|fn build_short_index(globals : Array[Arg], args : Array[Arg]) -> Map[Char, Arg] {
#| Map(
#| [
#| for arg in [..globals, ..args] if arg.info
#| is (FlagInfo(short~, ..) | OptionInfo(short~, ..)) &&
#| short is Some(value) => (value, arg)
#| ],
#| )
#|}
#|fn collect_globals(args : Array[Arg]) -> Array[Arg] {
#| args.filter(arg => arg.global && arg.info is (FlagInfo(_) | OptionInfo(_)))
#|}
#|fn collect_non_global_names(args : Array[Arg]) -> @set.Set[String] {
#| Set([ for arg in args if !arg.global => arg.name ])
#|}
#|fn resolve_help_target(
#| cmd : Command,
#| argv : ArrayView[String],
#| builtin_help_short : Bool,
#| builtin_help_long : Bool,
#| inherited_globals : Array[Arg],
#| command_path : String,
#|) -> (Command, Array[Arg], String) raise ArgParseError {
#| let targets = match argv {
#| [.. pre, "-h"] if builtin_help_short => pre
#| [.. pre, "--help"] if builtin_help_long => pre
#| _ => argv
#| }
#| for
#| name in targets
#| current = cmd,
#| current_path = command_path,
#| current_globals = inherited_globals,
#| subs = cmd.subcommands {
#| if name.has_prefix("-") {
#| raise InvalidArgument("unexpected help argument: \{name}")
#| }
#| guard subs.iter().find_first(sub => sub.name == name) is Some(sub) else {
#| let message = if suggest_name(
#| name,
#| [
#| for sub in subs if !sub.hidden => sub.name
#| ],
#| )
#| is Some(best) {
#| (
#| $|unknown subcommand: \{name}
#| $|
#| $| tip: a similar subcommand exists: '\{best}'
#| )
#| } else {
#| "unknown subcommand: \{name}"
#| }
#| raise InvalidArgument(message)
#| }
#| let current_globals = merge_global_defs(
#| current_globals,
#| collect_globals(current.args),
#| )
#| let current_path = if current_path == "" {
#| sub.name
#| } else {
#| "\{current_path} \{sub.name}"
#| }
#| continue sub, current_path, current_globals, sub.subcommands
#| } nobreak {
#| (current, current_globals, current_path)
#| }
#|}
#|fn split_long(arg : String) -> (StringView, String?) {
#| let body = arg.strip_prefix("--").unwrap_or(arg)
#| match body.split_once("=") {
#| Some((name, value)) => (name, Some(value.to_owned()))
#| None => (body, None)
#| }
#|}
),
"parser_positionals.mbt": (
#|fn positional_args(args : Array[Arg]) -> Array[Arg] {
#| [
#| for arg in args if arg.info is PositionalInfo(_) => arg
#| ]
#|}
#|fn next_positional(positionals : Array[Arg], collected : Array[String]) -> Arg? {
#| let target = collected.length()
#| let total = target + 1
#| for idx, arg in positionals; cursor = 0 {
#| if cursor >= total {
#| break None
#| }
#| let remaining = total - cursor
#| let take = if arg.multiple {
#| let (min, max) = arg_min_max(arg)
#| let reserve = remaining_positional_min(positionals, idx + 1)
#| let mut take = remaining - reserve
#| if take < 0 {
#| take = 0
#| }
#| match max {
#| Some(max_count) if take > max_count => take = max_count
#| _ => ()
#| }
#| if take < min {
#| take = min
#| }
#| if take > remaining {
#| take = remaining
#| }
#| take
#| } else if remaining > 0 {
#| 1
#| } else {
#| 0
#| }
#| if take > 0 && target < cursor + take {
#| break Some(arg)
#| }
#| continue cursor + take
#| } nobreak {
#| None
#| }
#|}
#|fn should_parse_as_positional(
#| arg : String,
#| positionals : Array[Arg],
#| collected : Array[String],
#| long_index : Map[String, Arg],
#| short_index : Map[Char, Arg],
#|) -> Bool {
#| if !arg.has_prefix("-") || arg == "-" {
#| return false
#| }
#| guard next_positional(positionals, collected) is Some(next) else {
#| return false
#| }
#| let next_allow = match next.info {
#| FlagInfo(_) => false
#| OptionInfo(allow_hyphen_values~, ..)
#| | PositionalInfo(allow_hyphen_values~, ..) => allow_hyphen_values
#| }
#| let allow = next_allow || is_negative_number(arg)
#| if !allow {
#| return false
#| }
#| if arg.has_prefix("--") {
#| let (name, _) = split_long(arg)
#| return long_index.get_from_string(name) is None &&
#| lookup_negated_flag(long_index, name) is None
#| }
#| match arg {
#| ['-', second, ..] => short_index.get(second) is None
#| _ => true
#| }
#|}
#|fn is_negative_number(arg : String) -> Bool {
#| lexmatch arg {
#| re"^-[0-9]+$" => true
#| _ => false
#| }
#|}
),
"parser_suggest.mbt": (
#|fn suggest_long(name : StringView, long_index : Map[String, Arg]) -> String? {
#| let candidates = long_index.keys().collect()
#| if suggest_name(name, candidates) is Some(best) {
#| Some("--\{best}")
#| } else {
#| None
#| }
#|}
#|fn suggest_short(short : Char, short_index : Map[Char, Arg]) -> String? {
#| let candidates = short_index.keys().map(c => c.to_string()).collect()
#| let input = short.to_string()
#| if suggest_name(input, candidates) is Some(best) {
#| Some("-\{best}")
#| } else {
#| None
#| }
#|}
#|fn suggest_name(input : StringView, candidates : Array[String]) -> String? {
#| let max_dist = suggestion_threshold(input.length())
#| for cand in candidates; best = (None : String?), best_dist = 0 {
#| match
#| @edit_distance.edit_distance_str_within(
#| input,
#| cand,
#| max_distance=max_dist,
#| ) {
#| Some(dist) if best is None || dist < best_dist =>
#| continue Some(cand), dist
#| _ => continue best, best_dist
#| }
#| } nobreak {
#| best
#| }
#|}
#|fn suggestion_threshold(len : Int32) -> Int32 {
#| if len <= 4 {
#| 1
#| } else if len <= 8 {
#| 2
#| } else {
#| 3
#| }
#|}
),
"parser_validate.mbt": (
#|priv struct ValidationCtx {
#| inherited_global_names : @set.Set[String]
#| seen_names : @set.Set[String]
#| seen_long : @set.Set[String]
#| seen_short : @set.Set[Char]
#| args : Array[Arg]
#|}
#|fn ValidationCtx::new(
#| inherited_global_names~ : @set.Set[String] = Set([]),
#|) -> ValidationCtx {
#| {
#| inherited_global_names: inherited_global_names.copy(),
#| seen_names: Set([]),
#| seen_long: Set([]),
#| seen_short: Set([]),
#| args: [],
#| }
#|}
#|fn ValidationCtx::record_arg(
#| self : ValidationCtx,
#| arg : Arg,
#|) -> Unit raise ArgBuildError {
#| if !self.seen_names.insert_and_check(arg.name) {
#| raise Unsupported("duplicate arg name: \{arg.name}")
#| }
#| if !arg.global && self.inherited_global_names.contains(arg.name) {
#| raise Unsupported(
#| "arg '\{arg.name}' shadows an inherited global; rename the arg or mark it global",
#| )
#| }
#| fn check_long(name) raise _ {
#| if !self.seen_long.insert_and_check(name) {
#| raise ArgBuildError::Unsupported("duplicate long option: --\{name}")
#| }
#| }
#| fn check_short(short) raise _ {
#| if !self.seen_short.insert_and_check(short) {
#| raise ArgBuildError::Unsupported("duplicate short option: -\{short}")
#| }
#| }
#| match arg.info {
#| FlagInfo(long~, short~, negatable~, ..) => {
#| if long is Some(name) {
#| check_long(name)
#| if negatable {
#| check_long("no-\{name}")
#| }
#| }
#| if short is Some(short) {
#| check_short(short)
#| }
#| }
#| OptionInfo(long~, short~, ..) => {
#| if long is Some(name) {
#| check_long(name)
#| }
#| if short is Some(short) {
#| check_short(short)
#| }
#| }
#| PositionalInfo(_) => ()
#| }
#| self.args.push(arg)
#|}
#|fn ValidationCtx::finalize(self : ValidationCtx) -> Unit raise ArgBuildError {
#| validate_requires_conflicts_targets(self.args, self.seen_names)
#|}
#|fn validate_command(
#| cmd : Command,
#| args : Array[Arg],
#| groups : Array[ArgGroup],
#| inherited_globals : Array[Arg],
#|) -> Unit raise ArgBuildError {
#| if cmd.build_error is Some(err) {
#| raise err
#| }
#| validate_inherited_global_shadowing(args, inherited_globals)
#| validate_group_defs(args, groups)
#| validate_group_refs(args, groups)
#| validate_subcommand_defs(cmd.subcommands)
#| validate_subcommand_required_policy(cmd)
#| validate_default_subcommand_policy(cmd)
#| validate_help_subcommand(cmd)
#| validate_version_actions(cmd)
#| let child_inherited_globals = merge_global_defs(
#| inherited_globals,
#| collect_globals(args),
#| )
#| for sub in cmd.subcommands {
#| validate_command(sub, sub.args, sub.groups, child_inherited_globals)
#| }
#|}
#|fn validate_inherited_global_shadowing(
#| args : Array[Arg],
#| inherited_globals : Array[Arg],
#|) -> Unit raise ArgBuildError {
#| for arg in args {
#| if inherited_globals.iter().find_first(g => g.name == arg.name)
#| is Some(inherited_arg) {
#| if !arg.global {
#| raise Unsupported(
#| "arg '\{arg.name}' shadows an inherited global; rename the arg or mark it global",
#| )
#| }
#| if !global_override_compatible(inherited_arg, arg) {
#| raise Unsupported(
#| "global arg '\{arg.name}' is incompatible with inherited global definition",
#| )
#| }
#| }
#| match arg.info {
#| FlagInfo(long~, short~, negatable~, ..) => {
#| if long is Some(name) {
#| validate_inherited_global_long_collision(arg, name, inherited_globals)
#| if negatable {
#| validate_inherited_global_long_collision(
#| arg,
#| "no-\{name}",
#| inherited_globals,
#| )
#| }
#| }
#| if short is Some(value) {
#| validate_inherited_global_short_collision(
#| arg, value, inherited_globals,
#| )
#| }
#| }
#| OptionInfo(long~, short~, ..) => {
#| if long is Some(name) {
#| validate_inherited_global_long_collision(arg, name, inherited_globals)
#| }
#| if short is Some(value) {
#| validate_inherited_global_short_collision(
#| arg, value, inherited_globals,
#| )
#| }
#| }
#| PositionalInfo(_) => ()
#| }
#| }
#|}
#|fn global_override_compatible(inherited_arg : Arg, arg : Arg) -> Bool {
#| match inherited_arg.info {
#| FlagInfo(action=inherited_action, negatable=inherited_negatable, ..) =>
#| match arg.info {
#| FlagInfo(action~, negatable~, ..) =>
#| inherited_action == action && inherited_negatable == negatable
#| _ => false
#| }
#| OptionInfo(action=inherited_action, ..) =>
#| match arg.info {
#| OptionInfo(action~, ..) => inherited_action == action
#| _ => false
#| }
#| PositionalInfo(_) => false
#| }
#|}
#|fn validate_inherited_global_long_collision(
#| arg : Arg,
#| long : String,
#| inherited_globals : Array[Arg],
#|) -> Unit raise ArgBuildError {
#| if inherited_global_long_owner(arg.name, long, inherited_globals)
#| is Some(owner) {
#| raise Unsupported(
#| "arg '\{arg.name}' long option --\{long} conflicts with inherited global '\{owner}'",
#| )
#| }
#|}
#|fn validate_inherited_global_short_collision(
#| arg : Arg,
#| short : Char,
#| inherited_globals : Array[Arg],
#|) -> Unit raise ArgBuildError {
#| if inherited_global_short_owner(arg.name, short, inherited_globals)
#| is Some(owner) {
#| raise Unsupported(
#| "arg '\{arg.name}' short option -\{short} conflicts with inherited global '\{owner}'",
#| )
#| }
#|}
#|fn inherited_global_long_owner(
#| current_name : String,
#| long : String,
#| inherited_globals : Array[Arg],
#|) -> String? {
#| for inherited in inherited_globals {
#| if inherited.name == current_name {
#| continue
#| }
#| match inherited.info {
#| FlagInfo(long=inherited_long, negatable~, ..) =>
#| if inherited_long is Some(name) &&
#| (name == long || (negatable && "no-\{name}" == long)) {
#| return Some(inherited.name)
#| }
#| OptionInfo(long=inherited_long, ..) =>
#| if inherited_long is Some(name) && name == long {
#| return Some(inherited.name)
#| }
#| PositionalInfo(_) => ()
#| }
#| }
#| None
#|}
#|fn inherited_global_short_owner(
#| current_name : String,
#| short : Char,
#| inherited_globals : Array[Arg],
#|) -> String? {
#| for inherited in inherited_globals {
#| if inherited.name == current_name {
#| continue
#| }
#| match inherited.info {
#| FlagInfo(short=inherited_short, ..)
#| | OptionInfo(short=inherited_short, ..) =>
#| if inherited_short is Some(value) && value == short {
#| return Some(inherited.name)
#| }
#| PositionalInfo(_) => ()
#| }
#| }
#| None
#|}
#|fn validate_flag_arg(
#| arg : Arg,
#| ctx : ValidationCtx,
#|) -> Unit raise ArgBuildError {
#| validate_named_option_arg(arg)
#| guard! arg.info is FlagInfo(action~, negatable~, ..)
#| if action is (Help | Version) {
#| guard !negatable else {
#| raise Unsupported("help/version actions do not support negatable")
#| }
#| guard arg.env is None else {
#| raise Unsupported("help/version actions do not support env/defaults")
#| }
#| guard !arg.multiple else {
#| raise Unsupported("help/version actions do not support multiple values")
#| }
#| }
#| ctx.record_arg(arg)
#|}
#|fn validate_option_arg(
#| arg : Arg,
#| ctx : ValidationCtx,
#|) -> Unit raise ArgBuildError {
#| validate_named_option_arg(arg)
#| validate_default_values(arg)
#| ctx.record_arg(arg)
#|}
#|fn validate_positional_arg(
#| arg : Arg,
#| ctx : ValidationCtx,
#|) -> Unit raise ArgBuildError {
#| let (min, max) = arg_min_max_for_validate(arg)
#| if (min > 1 || (max is Some(m) && m > 1)) && !arg.multiple {
#| raise Unsupported(
#| "multiple values require action=Append or num_args allowing >1",
#| )
#| }
#| validate_default_values(arg)
#| ctx.record_arg(arg)
#|}
#|fn validate_named_option_arg(arg : Arg) -> Unit raise ArgBuildError {
#| guard! arg.info
#| is (FlagInfo(long~, short~, ..) | OptionInfo(long~, short~, ..))
#| guard long is Some(_) || short is Some(_) || arg.env is Some(_) else {
#| raise Unsupported("flag/option args require short/long/env")
#| }
#|}
#|fn validate_default_values(arg : Arg) -> Unit raise ArgBuildError {
#| if arg.info
#| is (OptionInfo(default_values~, ..) | PositionalInfo(default_values~, ..)) &&
#| default_values is Some(values) &&
#| values.length() > 1 &&
#| !arg.multiple &&
#| !(arg.info is OptionInfo(action=Append, ..)) {
#| raise Unsupported(
#| "default_values with multiple entries require action=Append",
#| )
#| }
#|}
#|fn validate_group_defs(
#| args : Array[Arg],
#| groups : Array[ArgGroup],
#|) -> Unit raise ArgBuildError {
#| let seen : @set.Set[String] = Set([])
#| let arg_seen : @set.Set[String] = Set([])
#| for arg in args {
#| arg_seen.insert(arg.name)
#| }
#| for group in groups {
#| if !seen.insert_and_check(group.name) {
#| raise Unsupported("duplicate group: \{group.name}")
#| }
#| }
#| for group in groups {
#| for required in group.requires {
#| if required == group.name {
#| raise Unsupported("group cannot require itself: \{group.name}")
#| }
#| if !seen.contains(required) && !arg_seen.contains(required) {
#| raise Unsupported(
#| "unknown group requires target: \{group.name} -> \{required}",
#| )
#| }
#| }
#| for conflict in group.conflicts_with {
#| if conflict == group.name {
#| raise Unsupported("group cannot conflict with itself: \{group.name}")
#| }
#| if !seen.contains(conflict) && !arg_seen.contains(conflict) {
#| raise Unsupported(
#| "unknown group conflicts_with target: \{group.name} -> \{conflict}",
#| )
#| }
#| }
#| }
#|}
#|fn validate_group_refs(
#| args : Array[Arg],
#| groups : Array[ArgGroup],
#|) -> Unit raise ArgBuildError {
#| if groups.is_empty() {
#| return
#| }
#| let arg_index : @set.Set[String] = Set([])
#| for arg in args {
#| arg_index.insert(arg.name)
#| }
#| for group in groups {
#| for name in group.args {
#| if !arg_index.contains(name) {
#| raise Unsupported("unknown group arg: \{group.name} -> \{name}")
#| }
#| }
#| }
#|}
#|fn validate_requires_conflicts_targets(
#| args : Array[Arg],
#| seen_names : @set.Set[String],
#|) -> Unit raise ArgBuildError {
#| for arg in args {
#| for required in arg.requires {
#| if required == arg.name {
#| raise Unsupported("arg cannot require itself: \{arg.name}")
#| }
#| if !seen_names.contains(required) {
#| raise Unsupported("unknown requires target: \{arg.name} -> \{required}")
#| }
#| }
#| for conflict in arg.conflicts_with {
#| if conflict == arg.name {
#| raise Unsupported("arg cannot conflict with itself: \{arg.name}")
#| }
#| if !seen_names.contains(conflict) {
#| raise Unsupported(
#| "unknown conflicts_with target: \{arg.name} -> \{conflict}",
#| )
#| }
#| }
#| }
#|}
#|fn validate_subcommand_defs(subs : Array[Command]) -> Unit raise ArgBuildError {
#| if subs.is_empty() {
#| return
#| }
#| let seen : @set.Set[String] = Set([])
#| for sub in subs {
#| if !seen.insert_and_check(sub.name) {
#| raise Unsupported("duplicate subcommand: \{sub.name}")
#| }
#| }
#|}
#|fn validate_subcommand_required_policy(
#| cmd : Command,
#|) -> Unit raise ArgBuildError {
#| if cmd.subcommand_required && cmd.subcommands.is_empty() {
#| raise Unsupported("subcommand_required requires at least one subcommand")
#| }
#|}
#|fn validate_default_subcommand_policy(
#| cmd : Command,
#|) -> Unit raise ArgBuildError {
#| guard cmd.default_subcommand is Some(name) else { return }
#| if cmd.subcommand_required {
#| raise Unsupported(
#| "default_subcommand cannot be used with subcommand_required",
#| )
#| }
#| if cmd.arg_required_else_help {
#| raise Unsupported(
#| "default_subcommand cannot be used with arg_required_else_help",
#| )
#| }
#| if cmd.subcommands.iter().find_first(sub => sub.name == name && !sub.hidden)
#| is None {
#| raise Unsupported(
#| "default_subcommand must name a visible subcommand: \{name}",
#| )
#| }
#| if !cmd.groups.is_empty() {
#| raise Unsupported("default_subcommand does not support root groups")
#| }
#| for arg in cmd.args {
#| if !(arg.global && arg.info is (FlagInfo(_) | OptionInfo(_))) {
#| raise Unsupported(
#| "default_subcommand only supports global root flags/options",
#| )
#| }
#| }
#|}
#|fn validate_help_subcommand(cmd : Command) -> Unit raise ArgBuildError {
#| if help_subcommand_enabled(cmd) &&
#| cmd.subcommands.any(cmd => cmd.name == "help") {
#| raise Unsupported(
#| "subcommand name reserved for built-in help: help (disable with disable_help_subcommand)",
#| )
#| }
#|}
#|fn validate_version_actions(cmd : Command) -> Unit raise ArgBuildError {
#| if cmd.version is None &&
#| cmd.args.any(arg => arg.info is FlagInfo(action=Version, ..)) {
#| raise Unsupported("version action requires command version text")
#| }
#|}
#|fn validate_command_policies(
#| cmd : Command,
#| matches : Matches,
#|) -> Unit raise ArgParseError {
#| if cmd.subcommand_required &&
#| !cmd.subcommands.is_empty() &&
#| matches.parsed_subcommand is None {
#| raise MissingRequired("subcommand", None)
#| }
#|}
#|fn validate_groups(
#| args : Array[Arg],
#| groups : Array[ArgGroup],
#| matches : Matches,
#|) -> Unit raise ArgParseError {
#| if groups.is_empty() {
#| return
#| }
#| let group_presence = Map([])
#| let group_seen : @set.Set[String] = Set([])
#| let arg_seen : @set.Set[String] = Set([])
#| for group in groups {
#| group_seen.insert(group.name)
#| }
#| for arg in args {
#| arg_seen.insert(arg.name)
#| }
#| for group in groups {
#| let count = args.count_if(arg => {
#| arg_in_group(arg, group) && matches_has_value_or_flag(matches, arg.name)
#| })
#| group_presence[group.name] = count
#| if group.required && count == 0 {
#| raise MissingGroup(group.name)
#| }
#| if !group.multiple && count > 1 {
#| raise GroupConflict(group.name)
#| }
#| }
#| for group in groups {
#| let count = group_presence[group.name]
#| if count == 0 {
#| continue
#| }
#| for required in group.requires {
#| if group_seen.contains(required) {
#| if group_presence.get(required).unwrap_or(0) == 0 {
#| raise MissingGroup(required)
#| }
#| } else if arg_seen.contains(required) {
#| if !matches_has_value_or_flag(matches, required) {
#| raise MissingRequired(required, None)
#| }
#| }
#| }
#| for conflict in group.conflicts_with {
#| if group_seen.contains(conflict) {
#| if group_presence.get(conflict).unwrap_or(0) > 0 {
#| raise GroupConflict("\{group.name} conflicts with \{conflict}")
#| }
#| } else if arg_seen.contains(conflict) {
#| if matches_has_value_or_flag(matches, conflict) {
#| raise GroupConflict("\{group.name} conflicts with \{conflict}")
#| }
#| }
#| }
#| }
#|}
#|fn arg_in_group(arg : Arg, group : ArgGroup) -> Bool {
#| group.args.contains(arg.name)
#|}
#|fn validate_values(
#| args : Array[Arg],
#| matches : Matches,
#|) -> Unit raise ArgParseError {
#| for arg in args {
#| let present = matches_has_value_or_flag(matches, arg.name)
#| if arg.required && !present {
#| raise MissingRequired(arg.name, None)
#| }
#| guard arg.info is (OptionInfo(_) | PositionalInfo(_)) else { continue }
#| if !present {
#| if arg.info is PositionalInfo(_) {
#| let (min, _) = arg_min_max(arg)
#| if min > 0 {
#| raise TooFewValues(arg.name, 0, min)
#| }
#| }
#| continue
#| }
#| let values = matches.values.get(arg.name).unwrap_or([])
#| let count = values.length()
#| let (min, max) = arg_min_max(arg)
#| if count < min {
#| raise TooFewValues(arg.name, count, min)
#| }
#| if !(arg.info is OptionInfo(action=Append, ..)) {
#| match max {
#| Some(max) if count > max => raise TooManyValues(arg.name, count, max)
#| _ => ()
#| }
#| }
#| }
#|}
#|fn validate_relationships(
#| matches : Matches,
#| args : Array[Arg],
#|) -> Unit raise ArgParseError {
#| for arg in args {
#| if !matches_has_value_or_flag(matches, arg.name) {
#| continue
#| }
#| for required in arg.requires {
#| if !matches_has_value_or_flag(matches, required) {
#| raise MissingRequired(required, Some(arg.name))
#| }
#| }
#| for conflict in arg.conflicts_with {
#| if matches_has_value_or_flag(matches, conflict) {
#| raise InvalidArgument(
#| "conflicting arguments: \{arg.name} and \{conflict}",
#| )
#| }
#| }
#| }
#|}
),
"parser_values.mbt": (
#|fn assign_positionals(
#| matches : Matches,
#| positionals : Array[Arg],
#| values : Array[String],
#|) -> Unit raise ArgParseError {
#| let cursor = for idx, arg in positionals; cursor = 0 {
#| let remaining = values.length() - cursor
#| if arg.multiple {
#| let (min, max) = arg_min_max(arg)
#| let reserve = remaining_positional_min(positionals, idx + 1)
#| let mut take = remaining - reserve
#| if take < 0 {
#| take = 0
#| }
#| match max {
#| Some(max_count) if take > max_count => take = max_count
#| _ => ()
#| }
#| if take < min {
#| take = min
#| }
#| if take > remaining {
#| take = remaining
#| }
#| for value in values[cursor:cursor + take] {
#| add_value(matches, arg.name, value, arg, Argv)
#| }
#| continue cursor + take
#| }
#| if remaining > 0 {
#| add_value(matches, arg.name, values[cursor], arg, Argv)
#| continue cursor + 1
#| } else {
#| continue cursor
#| }
#| } nobreak {
#| cursor
#| }
#| if cursor < values.length() {
#| let overflow_label = if positionals is [.., last] {
#| if last.multiple {
#| Some("<\{last.name}...>")
#| } else {
#| Some("<\{last.name}>")
#| }
#| } else {
#| None
#| }
#| raise TooManyPositionals(values[cursor], overflow_label)
#| }
#|}
#|fn positional_min_required(arg : Arg) -> Int32 {
#| let (min, _) = arg_min_max(arg)
#| if min > 0 {
#| min
#| } else {
#| 0
#| }
#|}
#|fn remaining_positional_min(positionals : Array[Arg], start : Int32) -> Int32 {
#| for idx = start, total = 0; idx < positionals.length(); {
#| continue idx + 1, total + positional_min_required(positionals[idx])
#| } nobreak {
#| total
#| }
#|}
#|fn add_value(
#| matches : Matches,
#| name : String,
#| value : String,
#| arg : Arg,
#| source : ValueSource,
#|) -> Unit {
#| if arg.multiple || arg.info is OptionInfo(action=Append, ..) {
#| let arr = matches.values.get(name).unwrap_or([])
#| arr.push(value)
#| matches.values[name] = arr
#| matches.value_sources[name] = source
#| } else {
#| matches.values[name] = [value]
#| matches.value_sources[name] = source
#| }
#|}
#|fn assign_value(
#| matches : Matches,
#| arg : Arg,
#| value : String,
#| source : ValueSource,
#|) -> Unit raise ArgParseError {
#| match arg.info {
#| OptionInfo(_) | PositionalInfo(_) =>
#| add_value(matches, arg.name, value, arg, source)
#| FlagInfo(action=SetTrue, ..) => {
#| let flag = parse_bool(value)
#| matches.flags[arg.name] = flag
#| matches.flag_sources[arg.name] = source
#| }
#| FlagInfo(action=SetFalse, ..) => {
#| let flag = parse_bool(value)
#| matches.flags[arg.name] = !flag
#| matches.flag_sources[arg.name] = source
#| }
#| FlagInfo(action=Count, ..) => {
#| let count = parse_count(value)
#| matches.counts[arg.name] = count
#| matches.flags[arg.name] = count > 0
#| matches.flag_sources[arg.name] = source
#| }
#| FlagInfo(action=Help, ..) =>
#| raise InvalidArgument("help action does not take values")
#| FlagInfo(action=Version, ..) =>
#| raise InvalidArgument("version action does not take values")
#| }
#|}
#|fn option_conflict_label(arg : Arg) -> String {
#| match arg.info {
#| FlagInfo(long=Some(name), ..) | OptionInfo(long=Some(name), ..) =>
#| "--\{name}"
#| FlagInfo(short=Some(short), ..) | OptionInfo(short=Some(short), ..) =>
#| "-\{short}"
#| _ => arg.name
#| }
#|}
#|fn check_duplicate_set_occurrence(
#| matches : Matches,
#| arg : Arg,
#|) -> Unit raise ArgParseError {
#| guard arg.info is (OptionInfo(action=Set, ..) | PositionalInfo(_)) else {
#| return
#| }
#| if matches.values.get(arg.name) is Some(values) && !values.is_empty() {
#| raise InvalidArgument(
#| "argument '\{option_conflict_label(arg)}' cannot be used multiple times",
#| )
#| }
#|}
#|fn should_stop_option_value(value : String, arg : Arg) -> Bool {
#| if !value.has_prefix("-") || value == "-" {
#| return false
#| }
#| if arg.info
#| is (OptionInfo(allow_hyphen_values~, ..)
#| | PositionalInfo(allow_hyphen_values~, ..)) &&
#| allow_hyphen_values {
#| return false
#| }
#| true
#|}
#|fn apply_env(
#| matches : Matches,
#| args : Array[Arg],
#| env : Map[String, String],
#|) -> Unit raise ArgParseError {
#| for arg in args {
#| let name = arg.name
#| if matches_has_value_or_flag(matches, name) {
#| continue
#| }
#| guard arg.env is Some(env_name) else { continue }
#| guard env.get(env_name) is Some(value) else { continue }
#| match arg.info {
#| FlagInfo(action=Help | Version, ..) => ()
#| _ => assign_value(matches, arg, value, Env)
#| }
#| }
#|}
#|fn apply_defaults(matches : Matches, args : Array[Arg]) -> Unit {
#| for arg in args {
#| let default_values = match arg.info {
#| FlagInfo(_) => continue
#| OptionInfo(default_values~, ..) | PositionalInfo(default_values~, ..) =>
#| default_values
#| }
#| if matches_has_value_or_flag(matches, arg.name) {
#| continue
#| }
#| match default_values {
#| Some(values) if !values.is_empty() =>
#| for value in values {
#| let _ = add_value(matches, arg.name, value, arg, Default)
#| }
#| _ => ()
#| }
#| }
#|}
#|fn matches_has_value_or_flag(matches : Matches, name : String) -> Bool {
#| matches.flags.contains(name) || matches.values.contains(name)
#|}
#|fn apply_flag(matches : Matches, arg : Arg, source : ValueSource) -> Unit {
#| match arg.info {
#| FlagInfo(action=SetTrue, ..) => matches.flags[arg.name] = true
#| FlagInfo(action=SetFalse, ..) => matches.flags[arg.name] = false
#| FlagInfo(action=Count, ..) => {
#| let current = matches.counts.get(arg.name).unwrap_or(0)
#| matches.counts[arg.name] = current + 1
#| matches.flags[arg.name] = true
#| }
#| FlagInfo(action=Help, ..) => ()
#| FlagInfo(action=Version, ..) => ()
#| _ => matches.flags[arg.name] = true
#| }
#| matches.flag_sources[arg.name] = source
#|}
#|fn parse_bool(value : String) -> Bool raise ArgParseError {
#| match value {
#| "1" | "true" | "yes" | "on" => true
#| "0" | "false" | "no" | "off" => false
#| _ =>
#| raise InvalidValue(
#| "invalid value '\{value}' for boolean flag; expected one of: 1, 0, true, false, yes, no, on, off",
#| )
#| }
#|}
#|fn parse_count(value : String) -> Int32 raise ArgParseError {
#| try @string.parse_int(value) catch {
#| _ =>
#| raise InvalidValue(
#| "invalid value '\{value}' for count; expected a non-negative integer",
#| )
#| } noraise {
#| _..<0 =>
#| raise InvalidValue(
#| "invalid value '\{value}' for count; expected a non-negative integer",
#| )
#| v => v
#| }
#|}
),
"runtime_exit.mbt": (
#|fn trim_single_trailing_newline(text : String) -> String {
#| match text.strip_suffix("\n") {
#| Some(view) => view.to_owned()
#| None => text
#| }
#|}
#|fn[T] print_and_exit_success(text : String) -> T {
#| println(trim_single_trailing_newline(text))
#| @env.exit(0)
#|}
),
"value_range.mbt": (
#|#derive(Eq, Show, @debug.Debug)
#|#warnings("-deprecated_syntax-deprecated")
#|struct ValueRange {
#| lower : Int32
#| upper : Int32?
#|}
#|#alias(new, deprecated="Use `ValueRange()` instead")
#|pub fn ValueRange::ValueRange(lower~ : Int32 = 0, upper? : Int32) -> ValueRange {
#| { lower, upper, }
#|}
#|pub fn ValueRange::single() -> ValueRange {
#| ValueRange(lower=1, upper=1)
#|}
),
},
)
///|
let moonbitlang_core_array_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/array",
deps={},
files={
"array.mbt": (
#|pub fn[A, B, C] zip_with(
#| l : Array[A],
#| r : Array[B],
#| merge : (A, B) -> C raise?,
#|) -> Array[C] raise? {
#| let length = if l.length() < r.length() { l.length() } else { r.length() }
#| Array::makei(length, i => merge(l[i], r[i]))
#|}
),
"view.mbt": (
#|#deprecated
#|pub type View[T] = ArrayView[T]
),
},
)
///|
let moonbitlang_core_async_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async",
deps={},
files={
"async.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|pub using @event_loop {platform}
#|pub using @coroutine {pause}
#|pub using @coroutine {is_being_cancelled}
#|pub using @coroutine {handle_cancellation}
#|#derive(@debug.Debug, @json.ToJson)
#|pub suberror TimeoutError
#|#callsite(autofill(loc))
#|pub async fn[X] with_timeout(
#| time : Int32,
#| f : async () -> X,
#| error~ : Error = TimeoutError,
#| loc~ : SourceLoc,
#|) -> X {
#| match with_timeout_opt(time, f, loc~) {
#| Some(result) => result
#| None => raise error
#| }
#|}
#|#callsite(autofill(loc))
#|pub async fn[X] with_timeout_opt(
#| time : Int32,
#| f : async () -> X,
#| loc~ : SourceLoc,
#|) -> X? {
#| let mut result = None
#| let status = @coroutine.handle_cancellation <| () => {
#| with_task_group(loc~) <| group => {
#| group.spawn_bg(no_wait=true) <| () => {
#| sleep(time)
#| group.return_immediately()
#| }
#| result = Some(f())
#| }
#| }
#| match result {
#| Some(_) => result
#| None if status is Some(_) => None
#| None => @coroutine.raise_cancellation_signal()
#| }
#|}
#|pub using @coroutine {protect_from_cancel}
#|pub using @event_loop {now}
#|#cfg(any(target="native", target="wasm"))
#|pub using @event_loop {set_deadlock_handler}
#|pub using @aqueue {type Queue}
#|pub using @semaphore {type Semaphore}
#|pub using @cond_var {type Cond}
#|pub(all) enum RetryMethod {
#| Immediate
#| FixedDelay(Int32)
#| ExponentialDelay(initial~ : Int32, factor~ : Double, maximum~ : Int32)
#|}
#|#alias(retry, visibility="pub")
#|async fn[X] moonbitlang_async_retry(
#| strategy : RetryMethod,
#| max_retry? : Int32,
#| fatal_error? : (Error) -> Bool,
#| f : async () -> X,
#|) -> X {
#| match strategy {
#| Immediate =>
#| for i = 0; ; i = i + 1 {
#| try f() catch {
#| err if fatal_error is Some(f) && f(err) => raise err
#| err if @coroutine.is_being_cancelled() => raise err
#| err if max_retry is Some(max) && i >= max => raise err
#| _ => ()
#| } noraise {
#| result => return result
#| }
#| }
#| FixedDelay(t) =>
#| for i = 0; ; i = i + 1 {
#| try f() catch {
#| err if fatal_error is Some(f) && f(err) => raise err
#| err if @coroutine.is_being_cancelled() => raise err
#| err if max_retry is Some(max) && i >= max => raise err
#| _ => sleep(t)
#| } noraise {
#| result => return result
#| }
#| }
#| ExponentialDelay(initial~, factor~, maximum~) =>
#| for i = 0, timeout = initial; ; i = i + 1 {
#| try f() catch {
#| err if fatal_error is Some(f) && f(err) => raise err
#| err if @coroutine.is_being_cancelled() => raise err
#| err if max_retry is Some(max) && i >= max => raise err
#| _ => {
#| sleep(timeout)
#| continue i + 1,
#| @cmp.minimum(maximum, (timeout.to_double() * factor).to_int())
#| }
#| } noraise {
#| result => return result
#| }
#| }
#| }
#|}
#|pub async fn[X] all(
#| tasks : ArrayView[async () -> X],
#| max_concurrency? : Int32,
#|) -> Array[X] {
#| let semaphore = if max_concurrency is Some(p) {
#| Some(Semaphore(p))
#| } else {
#| None
#| }
#| let results = Array::make(tasks.length(), None)
#| with_task_group() <| tg => {
#| for i, task in tasks {
#| if semaphore is Some(sem) {
#| sem.acquire()
#| }
#| tg.spawn_bg() <| () => {
#| defer (if semaphore is Some(sem) { sem.release() })
#| let result = task()
#| results[i] = Some(result)
#| }
#| }
#| }
#| results.map(r => r.unwrap())
#|}
#|#callsite(autofill(loc))
#|#warnings("-fragile_catch_all")
#|pub async fn[X] any(
#| tasks : ArrayView[async () -> X],
#| max_concurrency? : Int32,
#| allow_failure~ : Bool = false,
#| loc~ : SourceLoc,
#|) -> X {
#| let semaphore = if max_concurrency is Some(p) {
#| Some(Semaphore(p))
#| } else {
#| None
#| }
#| if tasks.is_empty() {
#| fail("no tasks provided to any()", loc~)
#| }
#| let mut result = None
#| let mut last_err = None
#| with_task_group() <| tg => {
#| for task in tasks {
#| if semaphore is Some(sem) {
#| sem.acquire()
#| }
#| tg.spawn_bg(allow_failure~) <| () => {
#| defer (if semaphore is Some(sem) { sem.release() })
#| result = Some(task()) catch {
#| e => {
#| last_err = Some(e)
#| raise e
#| }
#| }
#| tg.return_immediately()
#| }
#| }
#| }
#| if result is Some(result) {
#| result
#| } else {
#| raise last_err.unwrap()
#| }
#|}
),
"deprecated.mbt": (
#|#deprecated("`Show` implementation for this type is deprecated, use `Debug` related API, or use `@debug.to_string` instead.")
#|pub impl Show for TimeoutError
#|pub impl Show for TimeoutError with fn output(_self, logger) {
#| logger <+ "TimeoutError"
#|}
#|#deprecated("`Show` implementation for this type is deprecated, use `Debug` related API, or use `@debug.to_string` instead.")
#|pub impl Show for TimerCancelled
#|pub impl Show for TimerCancelled with fn output(_self, logger) {
#| logger <+ "TimerCancelled"
#|}
),
"integration.mbt": (
#|#coverage.skip
#|#cfg(target="native")
#|#doc(hidden)
#|pub fn run_async_main(main : async () -> Unit) -> Unit {
#| @event_loop.with_event_loop(() => main()) catch {
#| @event_loop.KilledBySignal(signal) =>
#| @event_loop.terminate_process_by_signal(signal)
#| err => {
#| eprintln(err.to_string())
#| exit(1)
#| }
#| }
#|}
#|#cfg(target="native")
#|pub using @event_loop {set_external_event_loop}
#|#cfg(target="native")
#|extern "C" fn exit(code : Int32) = "exit"
#|#coverage.skip
#|#cfg(target="js")
#|#doc(hidden)
#|pub fn run_async_main(main : async () -> Unit) -> Unit {
#| let _ = @coroutine.spawn <| () => {
#| main() catch {
#| err => {
#| eprintln(err.to_string())
#| panic()
#| }
#| }
#| }
#| @event_loop.reschedule()
#|}
),
"lazy_init.mbt": (
#|priv enum LazyValueState[X] {
#| Uninitialized
#| Running(@coroutine.Coroutine)
#| Done(X)
#| Fail(Error)
#|}
#|struct Lazy[X] {
#| worker : async () -> X
#| waiters : @set.Set[@coroutine.Coroutine]
#| mut state : LazyValueState[X]
#| loc : SourceLoc
#|}
#|pub async fn[X] Lazy::wait(self : Lazy[X]) -> X {
#| match self.state {
#| Done(result) => return result
#| Fail(err) => raise err
#| Running(_) => ()
#| Uninitialized => {
#| let coro = @coroutine.spawn(loc=self.loc, () => {
#| try @coroutine.handle_cancellation(self.worker) catch {
#| err => self.state = Fail(err)
#| } noraise {
#| None => self.state = Uninitialized
#| Some(ret) => self.state = Done(ret)
#| }
#| for waiter in self.waiters {
#| waiter.wake()
#| }
#| })
#| self.state = Running(coro)
#| }
#| }
#| let curr_coro = @coroutine.current_coroutine()
#| self.waiters.insert(curr_coro)
#| defer {
#| self.waiters.remove(curr_coro)
#| if self.waiters.is_empty() && self.state is Running(coro) {
#| coro.cancel()
#| }
#| }
#| @coroutine.suspend()
#| match self.state {
#| Done(result) => result
#| Fail(err) => raise err
#| Running(_) | Uninitialized => abort("@async.Lazy::wait")
#| }
#|}
#|#as_free_fn(lazy_init, deprecated="use `@async.Lazy(..)` instead")
#|#alias(new, deprecated)
#|#callsite(autofill(loc))
#|pub fn[X] Lazy::Lazy(f : async () -> X, loc~ : SourceLoc) -> Lazy[X] {
#| { worker: f, waiters: Set([]), state: Uninitialized, loc, }
#|}
),
"mutex.mbt": (
#|#valtype
#|struct Mutex {
#| semaphore : Semaphore
#|}
#|pub fn Mutex::Mutex() -> Mutex {
#| { semaphore: Semaphore(1), }
#|}
#|pub async fn Mutex::acquire(self : Mutex) -> Unit noraise {
#| self.semaphore.acquire()
#|}
#|pub fn Mutex::try_acquire(self : Mutex) -> Bool {
#| self.semaphore.try_acquire()
#|}
#|pub fn Mutex::release(self : Mutex) -> Unit {
#| self.semaphore.release()
#|}
),
"task.mbt": (
#|struct Task[X] {
#| value : @ref.Ref[X?]
#| coro : @coroutine.Coroutine
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|#alias(TaskCancelled, deprecated)
#|pub suberror WaitedTaskAlreadyCancelled
#|pub async fn[X] Task::wait(self : Task[X]) -> X {
#| match self.coro.wait() {
#| Continue => self.value.val.unwrap()
#| Cancelled => {
#| @coroutine.check_cancellation()
#| raise WaitedTaskAlreadyCancelled
#| }
#| }
#|}
#|pub fn[X] Task::try_wait(self : Task[X]) -> X? raise {
#| self.coro.check_error()
#| self.value.val
#|}
#|pub fn[X] Task::cancel(self : Task[X]) -> Unit {
#| self.coro.cancel()
#|}
),
"task_group.mbt": (
#|priv enum TaskGroupState {
#| Done
#| Fail(Error)
#| Cancelled
#| Running
#|}
#|priv struct GroupDeferBlock(async () -> Unit nocancel)
#|struct TaskGroup {
#| children : @set.Set[@coroutine.Coroutine]
#| parent : @coroutine.Coroutine
#| mut waiting : Int32
#| mut state : TaskGroupState
#| group_defer : Array[GroupDeferBlock]
#|}
#|fn TaskGroup::spawn_coroutine(
#| self : TaskGroup,
#| f : async () -> Unit,
#| no_wait~ : Bool,
#| allow_failure~ : Bool,
#| loc~ : SourceLoc,
#|) -> @coroutine.Coroutine {
#| guard self.state is Running || !self.children.is_empty() else {
#| abort("trying to spawn from a terminated task group")
#| }
#| if !no_wait {
#| self.waiting += 1
#| }
#| async fn worker() {
#| let coro = @coroutine.current_coroutine()
#| defer {
#| self.children.remove(coro)
#| if !no_wait {
#| self.waiting -= 1
#| if self.waiting == 0 && self.state is Running {
#| for child in self.children {
#| child.cancel()
#| }
#| self.state = Done
#| }
#| }
#| if self.children.is_empty() {
#| self.parent.wake()
#| }
#| }
#| f() catch {
#| err if allow_failure => raise err
#| err => {
#| if self.state is Running {
#| for child in self.children {
#| child.cancel()
#| }
#| self.state = Fail(err)
#| } else {
#| self.state = Fail(err)
#| }
#| raise err
#| }
#| }
#| }
#| let coro = @coroutine.spawn(worker, loc~)
#| self.children.insert(coro)
#| if !(self.state is Running) {
#| coro.cancel()
#| }
#| coro
#|}
#|#callsite(autofill(loc))
#|pub fn TaskGroup::spawn_bg(
#| self : TaskGroup,
#| f : async () -> Unit,
#| no_wait~ : Bool = false,
#| allow_failure~ : Bool = false,
#| loc~ : SourceLoc,
#|) -> Unit {
#| ignore(self.spawn_coroutine(f, no_wait~, allow_failure~, loc~))
#|}
#|#callsite(autofill(loc))
#|pub fn[X] TaskGroup::spawn(
#| self : TaskGroup,
#| f : async () -> X,
#| no_wait~ : Bool = false,
#| allow_failure~ : Bool = false,
#| loc~ : SourceLoc,
#|) -> Task[X] {
#| let value = @ref.Ref(None)
#| let coro = self.spawn_coroutine(
#| () => value.val = Some(f()),
#| no_wait~,
#| allow_failure~,
#| loc~,
#| )
#| { value, coro, }
#|}
#|pub fn TaskGroup::add_defer(
#| self : TaskGroup,
#| block : async () -> Unit nocancel,
#|) -> Unit {
#| guard self.state is Running else {
#| abort("trying to attach defer to a terminated task group")
#| }
#| self.group_defer.push(block)
#|}
#|#callsite(autofill(loc))
#|pub async fn with_task_group(
#| f : async (TaskGroup) -> Unit,
#| loc~ : SourceLoc,
#|) -> Unit {
#| let tg = {
#| children: Set([]),
#| parent: @coroutine.current_coroutine(),
#| waiting: 0,
#| state: Running,
#| group_defer: [],
#| }
#| tg.spawn_bg(loc~, () => f(tg))
#| match @coroutine.suspend_check_cancel() {
#| Continue => ()
#| Cancelled => {
#| if tg.state is Running {
#| tg.state = Cancelled
#| }
#| if !tg.children.is_empty() {
#| for child in tg.children {
#| child.cancel()
#| }
#| protect_from_cancel(() => @coroutine.suspend())
#| }
#| }
#| }
#| guard! tg.children.is_empty()
#| while tg.group_defer.pop() is Some(defer_block) {
#| defer_block() catch {
#| err => tg.state = Fail(err)
#| }
#| }
#| match tg.state {
#| Done => ()
#| Fail(err) => raise err
#| Cancelled => @coroutine.raise_cancellation_signal()
#| Running => panic()
#| }
#|}
#|pub async fn TaskGroup::return_immediately(self : TaskGroup) -> Unit noraise {
#| if self.state is Running {
#| self.state = Done
#| for child in self.children {
#| child.cancel()
#| }
#| }
#| @coroutine.check_cancellation()
#|}
#|#callsite(autofill(loc))
#|pub fn TaskGroup::spawn_loop(
#| self : TaskGroup,
#| f : async () -> Unit,
#| no_wait~ : Bool = false,
#| allow_failure~ : Bool = false,
#| retry? : RetryMethod,
#| max_retry? : Int32,
#| fatal_error? : (Error) -> Bool,
#| loc~ : SourceLoc,
#|) -> Unit {
#| self.spawn_bg(no_wait~, allow_failure~, loc~) <| () => {
#| try {
#| match retry {
#| None =>
#| for ;; {
#| f()
#| }
#| Some(strategy) => {
#| let fatal_error = (err : Error) => {
#| err is BreakFromSpawnLoop || (fatal_error is Some(f) && f(err))
#| }
#| for ;; {
#| moonbitlang_async_retry(strategy, f, max_retry?, fatal_error~)
#| }
#| }
#| }
#| } catch {
#| BreakFromSpawnLoop => ()
#| err => raise err
#| }
#| }
#|}
#|pub(all) suberror BreakFromSpawnLoop
),
"timer.mbt": (
#|pub using @event_loop {sleep}
#|priv enum TimerState {
#| Running(@event_loop.Timer)
#| Detached
#| Terminated
#| Cancelled(Error)
#|}
#|struct Timer {
#| duration : Int32
#| mut state : TimerState
#| mut expire_time : Int64
#| waiters : @set.Set[@coroutine.Coroutine]
#|}
#|#alias(new, deprecated)
#|pub fn Timer::Timer(duration : Int32) -> Timer {
#| let expire_time = now() + duration.to_int64()
#| { state: Detached, duration, expire_time, waiters: Set([]), }
#|}
#|fn Timer::wake(timer : Timer, target_state : TimerState) -> Unit {
#| for coro in timer.waiters {
#| coro.wake()
#| }
#| match timer.state {
#| Running(t) => t.cancel()
#| Detached | Terminated => ()
#| Cancelled(_) => return
#| }
#| timer.state = target_state
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|pub suberror TimerCancelled
#|pub fn Timer::cancel(timer : Timer, err~ : Error = TimerCancelled) -> Unit {
#| timer.wake(Cancelled(err))
#|}
#|pub async fn Timer::wait(timer : Timer) -> Unit {
#| match timer.state {
#| Running(_) => ()
#| Detached => {
#| let duration = timer.expire_time - now()
#| guard duration > 0 else { return }
#| timer.state = Running(
#| @event_loop.Timer::new(duration.to_int()) <| () => {
#| timer.wake(Terminated)
#| },
#| )
#| }
#| Terminated => return
#| Cancelled(err) => raise err
#| }
#| let coro = @coroutine.current_coroutine()
#| timer.waiters.insert(coro)
#| defer timer.waiters.remove(coro)
#| @coroutine.suspend()
#| if timer.state is Cancelled(err) {
#| raise err
#| }
#|}
#|pub fn Timer::refresh(timer : Timer) -> Unit {
#| match timer.state {
#| Running(t) => t.cancel()
#| Detached | Terminated => ()
#| Cancelled(_) => return
#| }
#| timer.expire_time = now() + timer.duration.to_int64()
#| if timer.waiters.is_empty() {
#| timer.state = Detached
#| } else {
#| timer.state = Running(
#| @event_loop.Timer::new(timer.duration, () => timer.wake(Terminated)),
#| )
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_aqueue_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/aqueue",
deps={},
files={
"aqueue.mbt": (
#|priv struct Waiter[X] {
#| mut value : X?
#| coro : @coroutine.Coroutine
#| mut index : Int32
#| mut next : Int32
#| mut prev : Int32
#|}
#|impl[X] @cancellable_queue.Element for Waiter[X] with fn get_index(self) {
#| self.index
#|}
#|impl[X] @cancellable_queue.Element for Waiter[X] with fn set_index(self, value) {
#| self.index = value
#|}
#|impl[X] @cancellable_queue.Element for Waiter[X] with fn get_next(self) {
#| self.next
#|}
#|impl[X] @cancellable_queue.Element for Waiter[X] with fn set_next(self, value) {
#| self.next = value
#|}
#|impl[X] @cancellable_queue.Element for Waiter[X] with fn get_prev(self) {
#| self.prev
#|}
#|impl[X] @cancellable_queue.Element for Waiter[X] with fn set_prev(self, value) {
#| self.prev = value
#|}
#|extend Waiter with @cancellable_queue.Element::{is_queued}
#|pub(all) enum Kind {
#| Unbounded
#| Blocking(Int32)
#| DiscardOldest(Int32)
#| DiscardLatest(Int32)
#|}
#|struct Queue[X] {
#| kind : Kind
#| readers : @cancellable_queue.Queue[Waiter[X]]
#| writers : @cancellable_queue.Queue[Waiter[X]]
#| buffer : @deque.Deque[X]
#| mut closed : Error?
#|}
#|#as_free_fn(new, deprecated)
#|#alias(new, deprecated)
#|pub fn[X] Queue::Queue(kind~ : Kind) -> Queue[X] {
#| match kind {
#| Unbounded => ()
#| Blocking(n) | DiscardOldest(n) | DiscardLatest(n) =>
#| if n < 0 {
#| abort("queue size must not be negative")
#| }
#| }
#| { kind, writers: Queue(), readers: Queue(), buffer: Deque([]), closed: None, }
#|}
#|pub async fn[X] Queue::put(self : Queue[X], data : X) -> Unit {
#| if self.closed is Some(err) {
#| raise err
#| }
#| if !self.try_put(data) {
#| match self.kind {
#| Unbounded => panic()
#| Blocking(_) => {
#| let writer = {
#| value: Some(data),
#| coro: @coroutine.current_coroutine(),
#| index: -1,
#| prev: -1,
#| next: -1,
#| }
#| self.writers.push(writer)
#| match @coroutine.suspend_check_cancel() {
#| Continue =>
#| if writer.value is Some(_) {
#| guard! self.closed is Some(err)
#| raise err
#| }
#| Cancelled if writer.value is None => ()
#| Cancelled => {
#| if writer.is_queued() {
#| self.writers.remove(writer)
#| }
#| @coroutine.raise_cancellation_signal()
#| }
#| }
#| if self.closed is Some(err) {
#| raise err
#| }
#| }
#| DiscardOldest(n) =>
#| if n > 0 {
#| let _ = self.buffer.pop_front()
#| self.buffer.push_back(data)
#| }
#| DiscardLatest(_) => ()
#| }
#| }
#|}
#|pub fn[X] Queue::try_put(self : Queue[X], data : X) -> Bool raise {
#| if self.closed is Some(err) {
#| raise err
#| }
#| if self.readers.pop() is Some(reader) {
#| reader.value = Some(data)
#| reader.coro.wake()
#| true
#| } else {
#| match self.kind {
#| Unbounded => {
#| self.buffer.push_back(data)
#| true
#| }
#| Blocking(n) | DiscardOldest(n) | DiscardLatest(n) =>
#| if self.buffer.length() < n {
#| self.buffer.push_back(data)
#| true
#| } else {
#| false
#| }
#| }
#| }
#|}
#|pub async fn[X] Queue::get(self : Queue[X]) -> X {
#| if self.try_get() is Some(data) {
#| return data
#| }
#| let reader = {
#| value: None,
#| coro: @coroutine.current_coroutine(),
#| index: -1,
#| next: -1,
#| prev: -1,
#| }
#| self.readers.push(reader)
#| match @coroutine.suspend_check_cancel() {
#| Continue =>
#| if reader.value is Some(value) {
#| value
#| } else {
#| guard! self.closed is Some(err)
#| raise err
#| }
#| Cancelled if reader.value is Some(value) =>
#| value
#| Cancelled => {
#| if reader.is_queued() {
#| self.readers.remove(reader)
#| }
#| @coroutine.raise_cancellation_signal()
#| }
#| }
#|}
#|pub fn[X] Queue::try_get(self : Queue[X]) -> X? raise {
#| let data = self.buffer.pop_front()
#| if self.writers.pop() is Some(writer) {
#| guard! writer.value is Some(value)
#| writer.value = None
#| writer.coro.wake()
#| if data is None {
#| return Some(value)
#| } else {
#| self.buffer.push_back(value)
#| }
#| }
#| if data is None && self.closed is Some(err) {
#| raise err
#| }
#| data
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|pub suberror QueueAlreadyClosed
#|pub fn[X] Queue::close(
#| self : Queue[X],
#| error~ : Error = QueueAlreadyClosed,
#| clear~ : Bool = false,
#|) -> Unit {
#| self.closed = Some(error)
#| while self.writers.pop() is Some(writer) {
#| writer.coro.wake()
#| }
#| while self.readers.pop() is Some(reader) {
#| reader.coro.wake()
#| }
#| if clear {
#| self.buffer.clear()
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_cond_var_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/cond_var",
deps={},
files={
"cond_var.mbt": (
#|priv struct Waiter {
#| coro : @coroutine.Coroutine
#| mut index : Int32
#| mut next : Int32
#| mut prev : Int32
#|}
#|impl @cancellable_queue.Element for Waiter with fn get_index(self) {
#| self.index
#|}
#|impl @cancellable_queue.Element for Waiter with fn set_index(self, value) {
#| self.index = value
#|}
#|impl @cancellable_queue.Element for Waiter with fn get_next(self) {
#| self.next
#|}
#|impl @cancellable_queue.Element for Waiter with fn set_next(self, value) {
#| self.next = value
#|}
#|impl @cancellable_queue.Element for Waiter with fn get_prev(self) {
#| self.prev
#|}
#|impl @cancellable_queue.Element for Waiter with fn set_prev(self, value) {
#| self.prev = value
#|}
#|extend Waiter with @cancellable_queue.Element::{is_queued}
#|#valtype
#|struct Cond {
#| waiters : @cancellable_queue.Queue[Waiter]
#|}
#|#alias(new, deprecated)
#|pub fn Cond::Cond() -> Cond {
#| { waiters: Queue(), }
#|}
#|pub async fn Cond::wait(cond : Cond) -> Unit noraise {
#| let coro = @coroutine.current_coroutine()
#| let waiter = { coro, index: -1, next: -1, prev: -1, }
#| cond.waiters.push(waiter)
#| match @coroutine.suspend_check_cancel() {
#| Continue => ()
#| Cancelled if !waiter.is_queued() =>
#| ()
#| Cancelled => {
#| cond.waiters.remove(waiter)
#| @coroutine.raise_cancellation_signal()
#| }
#| }
#|}
#|pub fn Cond::signal(cond : Cond) -> Unit {
#| if cond.waiters.pop() is Some(waiter) {
#| waiter.coro.wake()
#| }
#|}
#|pub fn Cond::broadcast(cond : Cond) -> Unit {
#| while cond.waiters.pop() is Some(waiter) {
#| waiter.coro.wake()
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_external_loop_integration_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/external_loop_integration",
deps={},
files={
"external_loop.mbt": (
#|pub(open) trait ExternalEventLoop {
#| fn poll(Self, timeout? : Int32) -> Unit raise
#| fn get_wakeup_callback_for_foreign_thread(Self) -> FuncRef[() -> Unit]
#| fn terminate(Self) -> Unit raise
#|}
),
},
)
///|
let moonbitlang_core_async_http_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/http",
deps={},
files={
"case_insensitive_string.mbt": (
#|#derive(@json.ToJson)
#|pub(all) struct CaseInsensitiveString(String)
#|pub impl Eq for CaseInsensitiveString with fn equal(x, y) {
#| Compare::compare(x, y) == 0
#|}
#|pub impl Compare for CaseInsensitiveString with fn compare(x, y) {
#| x.0.compare_ignore_ascii_case(y.0)
#|}
#|pub impl @debug.Debug for CaseInsensitiveString with fn to_repr(self) {
#| @debug.Debug::to_repr(self.0)
#|}
#|pub impl Show for CaseInsensitiveString with fn to_string(self) {
#| self.0
#|}
#|pub extend CaseInsensitiveString with Show::{to_string}
#|pub impl Hash for CaseInsensitiveString with fn hash_combine(self, hasher) {
#| for code in self.0.code_units() {
#| let code = match code {
#| 'A'..<='Z' => code + ('a' - 'A')
#| _ => code
#| }
#| hasher.combine_uint(code.to_uint())
#| }
#|}
),
"client.mbt": (
#|struct Client {
#| headers : Headers
#|}
#|pub fn Client::Client(headers~ : Headers = {}) -> Client {
#| { headers, }
#|}
#|pub fn Client::close(_ : Client) -> Unit {
#|}
#|pub async fn Client::request_stream(
#| self : Client,
#| meth : RequestMethod,
#| url : StringView,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> ClientConnection {
#| let (protocol, host, path) = resolve_url(url)
#| let conn = ClientConnection::connect(
#| host,
#| protocol~,
#| headers=self.headers,
#| proxy?,
#| )
#| errdefer conn.close()
#| conn.request(meth, path, extra_headers~)
#| conn
#|}
#|pub async fn Client::request_binary(
#| self : Client,
#| meth : RequestMethod,
#| url : StringView,
#| body : BytesView,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let conn = self.request_stream(meth, url, extra_headers~, proxy?)
#| defer conn.close()
#| conn.write(body)
#| let response = conn.end_request()
#| (response, conn.read_all())
#|}
#|pub async fn Client::request_text(
#| self : Client,
#| meth : RequestMethod,
#| url : StringView,
#| body : StringView,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let conn = self.request_stream(meth, url, extra_headers~, proxy?)
#| defer conn.close()
#| conn.text.write(body)
#| let response = conn.end_request()
#| (response, conn.read_all())
#|}
#|pub async fn Client::request_json(
#| self : Client,
#| meth : RequestMethod,
#| url : StringView,
#| body : @json.Json,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let conn = self.request_stream(meth, url, extra_headers~, proxy?)
#| defer conn.close()
#| conn.text.write(body.stringify())
#| let response = conn.end_request()
#| (response, conn.read_all())
#|}
#|pub async fn Client::get_stream(
#| self : Client,
#| url : StringView,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, ClientConnection) {
#| let conn = self.request_stream(Get, url, extra_headers~, proxy?)
#| errdefer conn.close()
#| let response = conn.end_request()
#| (response, conn)
#|}
#|pub async fn Client::get(
#| self : Client,
#| uri : StringView,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let (response, conn) = self.get_stream(uri, extra_headers~, proxy?)
#| defer conn.close()
#| (response, conn.read_all())
#|}
#|pub async fn Client::put_stream(
#| self : Client,
#| url : StringView,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> ClientConnection {
#| self.request_stream(Put, url, extra_headers~, proxy?)
#|}
#|pub async fn Client::put_binary(
#| self : Client,
#| uri : StringView,
#| content : BytesView,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| self.request_binary(Put, uri, content, extra_headers~, proxy?)
#|}
#|pub async fn Client::put_text(
#| self : Client,
#| uri : StringView,
#| content : StringView,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| self.request_text(Put, uri, content, extra_headers~, proxy?)
#|}
#|pub async fn Client::put_json(
#| self : Client,
#| uri : StringView,
#| content : @json.Json,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| self.request_json(Put, uri, content, extra_headers~, proxy?)
#|}
#|pub async fn Client::post_stream(
#| self : Client,
#| url : StringView,
#| extra_headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> ClientConnection {
#| self.request_stream(Post, url, extra_headers~, proxy?)
#|}
#|pub async fn Client::post_binary(
#| self : Client,
#| uri : StringView,
#| content : BytesView,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| self.request_binary(Post, uri, content, extra_headers~, proxy?)
#|}
#|pub async fn Client::post_text(
#| self : Client,
#| uri : StringView,
#| content : StringView,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| self.request_text(Post, uri, content, extra_headers~, proxy?)
#|}
#|pub async fn Client::post_json(
#| self : Client,
#| uri : StringView,
#| content : @json.Json,
#| extra_headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| self.request_json(Post, uri, content, extra_headers~, proxy?)
#|}
),
"client_connection.js.mbt": (
#|#external
#|priv type JsHeaders
#|extern "js" fn JsHeaders::new() -> JsHeaders =
#| #| () => new Headers()
#|extern "js" fn JsHeaders::append(
#| headers : JsHeaders,
#| name : String,
#| value : String,
#|) =
#| #| (headers, name, value) => headers.append(name, value)
#|extern "js" fn JsHeaders::to_array(
#| headers : JsHeaders,
#|) -> FixedArray[FixedArray[String]] =
#| #| (headers) => Array.from(headers.entries())
#|#external
#|priv type JsResponse
#|extern "js" fn JsResponse::status(response : JsResponse) -> Int32 =
#| #| (response) => response.status
#|extern "js" fn JsResponse::status_text(response : JsResponse) -> String =
#| #| (response) => response.statusText
#|extern "js" fn JsResponse::headers(response : JsResponse) -> JsHeaders =
#| #| (response) => response.headers
#|extern "js" fn JsResponse::body(
#| response : JsResponse,
#|) -> @js_async.JsReadableStream =
#| #| (response) => response.body ?? new ReadableStream({
#| #| start(controller) { controller.close() },
#| #| })
#|priv struct OngoingRequest {
#| meth : String
#| uri : String
#| body : @buffer.Buffer
#| ctrl : @io.BinaryWriterController
#| headers : JsHeaders
#|}
#|pub struct ClientConnection {
#| text : @io.Utf8TextView
#| priv host : String
#| priv protocol : Protocol
#| priv headers : Headers
#| priv mut request : OngoingRequest?
#| priv mut response_body : @js_async.ReadableStream?
#|}
#|pub fn ClientConnection::close(self : ClientConnection) -> Unit {
#| self.text.close()
#| self.request = None
#| if self.response_body is Some(response_body) {
#| response_body.close()
#| }
#|}
#|fn ClientConnection::connect(
#| host : String,
#| headers~ : Headers = Map([]),
#| protocol~ : Protocol = Https,
#| proxy? : Proxy,
#| trust~ : @tls.TrustedRoot = SystemRoot,
#|) -> ClientConnection {
#| if proxy is Some(proxy) {
#| ignore(proxy.setup)
#| }
#| ignore(trust)
#| let client = {
#| text: @io.Utf8TextView::new_uninitialized(),
#| host,
#| headers,
#| protocol,
#| request: None,
#| response_body: None,
#| }
#| client.text.set_inner(client)
#| client
#|}
#|#warnings("-unused_async")
#|#alias(new, deprecated)
#|#label_migration(trust, fill=false, msg="`trust` is unsupported on JS backend")
#|pub async fn ClientConnection::ClientConnection(
#| uri : String,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#| trust~ : @tls.TrustedRoot = SystemRoot,
#|) -> ClientConnection {
#| let (protocol, host, path) = resolve_url(uri)
#| guard path is "/" else { raise InvalidFormat }
#| ClientConnection::connect(host, protocol~, headers~, proxy?, trust~)
#|}
#|pub impl @io.BinaryWriter for ClientConnection
#|impl @io.BinaryWriterImpl for ClientConnection with fn raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len~,
#|) {
#| guard! self.request is Some(request)
#| request.body.write_bytesview(buf[offset:offset + len])
#| len
#|}
#|impl @io.BinaryWriterImpl for ClientConnection with fn name(_self) {
#| "@http.ClientConnection"
#|}
#|impl @io.BinaryWriterImpl for ClientConnection with fn get_controller(self) {
#| guard! self.request is Some(request)
#| request.ctrl
#|}
#|impl @io.BinaryWriterImpl for ClientConnection with fn flush_partial_data(self) {
#| ignore(self)
#|}
#|pub extend ClientConnection with @io.BinaryWriter::{
#| write,
#| write_reader,
#| flush_partial_data,
#|}
#|pub impl @io.BinaryReader for ClientConnection
#|impl @io.BinaryReaderImpl for ClientConnection with fn name(_self) {
#| "@http.ClientConnection"
#|}
#|impl @io.BinaryReaderImpl for ClientConnection with fn get_controller(self) {
#| guard! self.response_body is Some(stream)
#| @io.BinaryReaderImpl::get_controller(stream as &@io.BinaryReader)
#|}
#|impl @io.BinaryReaderImpl for ClientConnection with fn raw_read_impl(
#| self,
#| buf,
#| offset~,
#| max_len~,
#|) {
#| guard! self.response_body is Some(stream)
#| @io.BinaryReaderImpl::raw_read_impl(
#| stream as &@io.BinaryReader,
#| buf,
#| offset~,
#| max_len~,
#| )
#|}
#|pub extend ClientConnection with @io.BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
#|pub async fn ClientConnection::end_request(
#| self : ClientConnection,
#| on_informational_response? : async (ResponseHeader) -> Unit,
#|) -> ResponseHeader {
#| ignore(on_informational_response)
#| guard! self.request is Some(request)
#| self.request = None
#| let abort_controller = @js_async.AbortController()
#| let js_response = ClientConnection::request_ffi(
#| request.uri,
#| request.meth,
#| headers=request.headers,
#| body=request.body.contents(),
#| signal=abort_controller.signal(),
#| ).wait(abort_controller~)
#| let body_stream = @js_async.ReadableStream::from_js(js_response.body())
#| self.response_body = Some(body_stream)
#| let headers : Headers = Map([])
#| for entry in js_response.headers().to_array() {
#| headers[entry[0]] = entry[1]
#| }
#| {
#| code: js_response.status(),
#| reason: js_response.status_text(),
#| headers,
#| cookies: [],
#| }
#|}
#|extern "js" fn ClientConnection::request_ffi(
#| uri : String,
#| meth : String,
#| headers~ : JsHeaders,
#| body~ : Bytes,
#| signal~ : @js_async.AbortSignal,
#|) -> @js_async.Promise[JsResponse] =
#| #| (uri, method, headers, body, signal) => {
#| #| const fixed_body = (method === "GET" || method === "HEAD") ? null : body
#| #| return fetch(
#| #| uri,
#| #| {
#| #| body: fixed_body,
#| #| method: method,
#| #| headers: headers,
#| #| signal: signal,
#| #| },
#| #| )
#| #| }
#|#warnings("-unused_async")
#|pub async fn ClientConnection::request(
#| self : ClientConnection,
#| meth : RequestMethod,
#| path : StringView,
#| extra_headers~ : Headers = Map([]),
#|) -> Unit {
#| guard! self.request is None
#| let protocol = match self.protocol {
#| Http => "http://"
#| Https => "https://"
#| }
#| let path = if path is ['/', ..] { path } else { "/\{path}" }
#| let uri = "\{protocol}\{self.host}\{path}"
#| let meth = match meth {
#| Get => "GET"
#| Head => "HEAD"
#| Post => "POST"
#| Put => "PUT"
#| Delete => "DELETE"
#| Connect => "CONNECT"
#| Options => "OPTIONS"
#| Trace => "TRACE"
#| Patch => "PATCH"
#| Extension(ext) => {
#| guard ext.length() > 0 else { raise InvalidMethod(ext) }
#| for c in ext.code_units() {
#| guard is_token_char(c) else { raise InvalidMethod(ext) }
#| }
#| ext
#| }
#| }
#| let headers = JsHeaders::new()
#| for k, v in self.headers {
#| headers.append(k.0, v)
#| }
#| for k, v in extra_headers {
#| headers.append(k.0, v)
#| }
#| let request : OngoingRequest = {
#| meth,
#| uri,
#| headers,
#| body: Buffer(),
#| ctrl: BinaryWriterController(),
#| }
#| self.request = Some(request)
#|}
#|let _mute_unused_warning : Unit = {
#| ignore(IncorrectBodyLength)
#| ignore({ meth: Get, path: "", headers: {}, })
#|}
),
"cookie.mbt": (
#|#alias(new, deprecated)
#|pub fn Cookie::Cookie(
#| name : String,
#| value : String,
#| path? : String,
#| expires_raw? : String,
#| max_age? : Int64,
#| domain? : String,
#| secure~ : Bool = false,
#| http_only~ : Bool = false,
#| extensions~ : Array[String] = [],
#|) -> Cookie {
#| {
#| name,
#| value,
#| path,
#| expires_raw,
#| max_age,
#| domain,
#| secure,
#| http_only,
#| extensions,
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn Cookie::parse(line : StringView) -> Cookie? {
#| let (name_value, attrs) = match line.split_once(";") {
#| Some(pair) => pair
#| None => (line, "")
#| }
#| guard name_value.split_once("=") is Some((name, value)) else { return None }
#| let name = name.to_owned()
#| let value = value.to_owned()
#| if attrs is "" {
#| return Some(Cookie(name, value))
#| }
#| let mut path = None
#| let mut expires_raw = None
#| let mut max_age = None
#| let mut domain = None
#| let mut secure = false
#| let mut http_only = false
#| let extensions = []
#| for attr in attrs.split(";") {
#| let attr = attr.trim()
#| if attr.split_once("=") is Some((name, value)) {
#| match name.to_lower() {
#| "path" => path = Some(value.to_owned())
#| "expires" => expires_raw = Some(value.to_owned())
#| "max-age" =>
#| if parse_max_age(value) is Some(value) {
#| max_age = Some(value)
#| }
#| "domain" => domain = Some(value.to_owned())
#| _ => extensions.push(attr.to_owned())
#| }
#| } else {
#| match attr.to_lower() {
#| "secure" => secure = true
#| "httponly" => http_only = true
#| _ => extensions.push(attr.to_owned())
#| }
#| }
#| }
#| Some({
#| name,
#| value,
#| path,
#| expires_raw,
#| max_age,
#| domain,
#| secure,
#| http_only,
#| extensions,
#| })
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn parse_max_age(value : StringView) -> Int64? {
#| lexmatch value {
#| re"^-?[0-9]+$" =>
#| try @string.parse_int64(value) catch {
#| _ => None
#| } noraise {
#| value => Some(value)
#| }
#| _ => None
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|async fn[W : @io.BinaryWriter] Cookie::write_to(cookie : Cookie, w : W) -> Unit {
#| w
#| ..write(@utf8.encode(cookie.name))
#| ..write(b"=")
#| .write(@utf8.encode(cookie.value))
#| if cookie.path is Some(path) {
#| w..write(b"; Path=").write(@utf8.encode(path))
#| }
#| if cookie.expires_raw is Some(expires_raw) {
#| w..write(b"; Expires=").write(@utf8.encode(expires_raw))
#| }
#| if cookie.max_age is Some(max_age) {
#| w..write(b"; Max-Age=").write(@utf8.encode(max_age.to_string()))
#| }
#| if cookie.domain is Some(domain) {
#| w..write(b"; Domain=").write(@utf8.encode(domain))
#| }
#| if cookie.secure {
#| w.write(b"; Secure")
#| }
#| if cookie.http_only {
#| w.write(b"; HttpOnly")
#| }
#| for ext in cookie.extensions {
#| w..write(b"; ").write(@utf8.encode(ext))
#| }
#|}
),
"extends.mbt": (
#|pub extend CaseInsensitiveString with Eq::{equal}
#|pub extend CaseInsensitiveString with Compare::{compare}
#|pub extend CaseInsensitiveString with Hash::{hash}
#|#cfg(any(target="native", target="js", target="wasm"))
#|pub extend RequestMethod with Eq::{equal}
#|#cfg(any(target="native", target="js", target="wasm"))
#|pub extend RequestMethod with Compare::{compare}
#|#cfg(any(target="native", target="js", target="wasm"))
#|pub extend RequestMethod with Hash::{hash}
),
"proxy.mbt": (
#|struct Proxy {
#| setup : async (StringView, UInt16) -> ClientConnection
#|}
#|pub fn Proxy::custom_tunnel(
#| f : async (StringView, UInt16) -> ClientConnection,
#|) -> Proxy {
#| { setup: f, }
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub fn Proxy::Proxy(
#| url : StringView,
#| username? : StringView,
#| password? : StringView,
#|) -> Proxy {
#| let proxy_authorization = match (username, password) {
#| (None, None) => None
#| (username, password) => {
#| let username = username.unwrap_or("")
#| let password = password.unwrap_or("")
#| Some("Basic \{@base64.encode(@utf8.encode("\{username}:\{password}"))}")
#| }
#| }
#| async fn setup(target : StringView, port : UInt16) {
#| let conn = ClientConnection(url)
#| errdefer conn.close()
#| let extra_headers : Headers = match proxy_authorization {
#| Some(value) => { "Proxy-Authorization": value }
#| None => {}
#| }
#| let response = conn
#| ..request(Connect, "\{target}:\{port}", extra_headers~)
#| .end_request()
#| guard response.code is (200..<300) else { raise ProxyError(response) }
#| conn..skip_response_body()..enter_passthrough_mode()
#| }
#| { setup, }
#|}
#|#cfg(target="js")
#|#warnings("-unused_async")
#|pub fn Proxy::Proxy(
#| url : StringView,
#| username? : StringView,
#| password? : StringView,
#|) -> Proxy {
#| ignore(url)
#| ignore(username)
#| ignore(password)
#| async fn setup(_target : StringView, _port : UInt16) -> ClientConnection {
#| abort("HTTP proxies are unsupported on the JavaScript backend")
#| }
#| { setup, }
#|}
),
"request.mbt": (
#|priv enum Protocol {
#| Http
#| Https
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn Protocol::default_port(p : Protocol) -> UInt16 {
#| match p {
#| Http => 80
#| Https => 443
#| }
#|}
#|fn resolve_url(uri : StringView) -> (Protocol, String, StringView) raise {
#| guard uri.find("://") is Some(protocol_len) else { raise InvalidFormat }
#| let protocol = match uri[:protocol_len] {
#| "http" => Http
#| "https" => Https
#| protocol => raise UnsupportedProtocol(protocol.to_owned())
#| }
#| let uri = uri[protocol_len + 3:]
#| let (host, path) = if uri.find("/") is Some(i) {
#| uri.split_at(i)
#| } else {
#| (uri, "/")
#| }
#| (protocol, host.to_owned(), path)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn resolve_host(
#| host : StringView,
#| protocol~ : Protocol,
#|) -> (StringView, UInt16) raise {
#| if host =~ (
#| re"^" +
#| (re".*" as host) +
#| re":" +
#| (re"[0-9]+" as port_str) +
#| re"$"
#| ) {
#| let port = @string.parse_int(port_str) catch { _ => raise InvalidFormat }
#| guard port is (1..<65536) else { raise InvalidFormat }
#| (host, port.to_uint16())
#| } else {
#| (host, protocol.default_port())
#| }
#|}
#|pub async fn request_stream(
#| meth : RequestMethod,
#| uri : StringView,
#| headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> ClientConnection {
#| let (protocol, host, path) = resolve_url(uri)
#| let client = ClientConnection::connect(host, protocol~, proxy?)
#| errdefer client.close()
#| client.request(meth, path, extra_headers=headers)
#| client
#|}
#|pub async fn request_binary(
#| meth : RequestMethod,
#| uri : StringView,
#| body : BytesView,
#| headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let conn = request_stream(meth, uri, headers~, proxy?)
#| defer conn.close()
#| let response = conn..write(body).end_request()
#| (response, conn.read_all())
#|}
#|pub async fn request_text(
#| meth : RequestMethod,
#| uri : StringView,
#| body : StringView,
#| headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let conn = request_stream(meth, uri, headers~, proxy?)
#| defer conn.close()
#| conn.text.write(body)
#| let response = conn.end_request()
#| (response, conn.read_all())
#|}
#|pub async fn request_json(
#| meth : RequestMethod,
#| uri : StringView,
#| body : @json.Json,
#| headers~ : Headers = {},
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| let conn = request_stream(meth, uri, headers~, proxy?)
#| defer conn.close()
#| conn.text.write(body.stringify())
#| let response = conn.end_request()
#| (response, conn.read_all())
#|}
#|pub async fn get(
#| uri : StringView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_binary(Get, uri, b"", headers~, proxy?)
#|}
#|pub async fn put_binary(
#| uri : StringView,
#| content : BytesView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_binary(Put, uri, content, headers~, proxy?)
#|}
#|pub async fn put_text(
#| uri : StringView,
#| content : StringView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_text(Put, uri, content, headers~, proxy?)
#|}
#|pub async fn put_json(
#| uri : StringView,
#| content : @json.Json,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_json(Put, uri, content, headers~, proxy?)
#|}
#|pub async fn post_binary(
#| uri : StringView,
#| content : BytesView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_binary(Post, uri, content, headers~, proxy?)
#|}
#|pub async fn post_text(
#| uri : StringView,
#| content : StringView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_text(Post, uri, content, headers~, proxy?)
#|}
#|pub async fn post_json(
#| uri : StringView,
#| content : @json.Json,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, MessageBody) {
#| request_json(Post, uri, content, headers~, proxy?)
#|}
#|pub async fn get_stream(
#| uri : StringView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> (ResponseHeader, ClientConnection) {
#| let conn = request_stream(Get, uri, headers~, proxy?)
#| errdefer conn.close()
#| (conn.end_request(), conn)
#|}
#|pub async fn put_stream(
#| uri : StringView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> ClientConnection {
#| request_stream(Put, uri, headers~, proxy?)
#|}
#|pub async fn post_stream(
#| uri : StringView,
#| headers~ : Headers = Map([]),
#| proxy? : Proxy,
#|) -> ClientConnection {
#| request_stream(Post, uri, headers~, proxy?)
#|}
),
"types.mbt": (
#|#derive(@debug.Debug, Eq, Compare, Hash, @json.ToJson)
#|pub(all) enum RequestMethod {
#| Get
#| Head
#| Post
#| Put
#| Delete
#| Connect
#| Options
#| Trace
#| Patch
#| Extension(String)
#|}
#|pub impl Show for RequestMethod with fn to_string(self) {
#| match self {
#| Get => "GET"
#| Head => "HEAD"
#| Post => "POST"
#| Put => "PUT"
#| Delete => "DELETE"
#| Connect => "CONNECT"
#| Options => "OPTIONS"
#| Trace => "TRACE"
#| Patch => "PATCH"
#| Extension(meth) => meth
#| }
#|}
#|pub extend RequestMethod with Show::{to_string}
#|pub type Headers = Map[CaseInsensitiveString, String]
#|#derive(@debug.Debug, @json.ToJson)
#|pub struct RequestHeader {
#| meth : RequestMethod
#| path : String
#| headers : Headers
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|pub struct ResponseHeader {
#| code : Int32
#| reason : String
#| headers : Headers
#| cookies : Array[Cookie]
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|pub struct Cookie {
#| name : String
#| value : String
#| path : String?
#| expires_raw : String?
#| max_age : Int64?
#| domain : String?
#| secure : Bool
#| http_only : Bool
#| extensions : Array[String]
#|}
#|struct MessageBody(Bytes)
#|pub fn MessageBody::binary(self : MessageBody) -> Bytes {
#| self.0
#|}
#|pub fn MessageBody::text(self : MessageBody) -> String raise {
#| @utf8.decode(self.0)
#|}
#|pub fn MessageBody::json(self : MessageBody) -> @json.Json raise {
#| @json.parse(@utf8.decode(self.0))
#|}
#|#cfg(any(target="native", target="wasm"))
#|#derive(@debug.Debug, @json.ToJson)
#|#non_exhaustive
#|pub suberror ProtocolError {
#| BadMessage
#| HttpVersionNotSupported(String)
#| NotImplemented
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|suberror UriParseError {
#| InvalidFormat
#| UnsupportedProtocol(String)
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|suberror InvalidMessage {
#| InvalidMethod(String)
#| IncorrectBodyLength
#|}
),
"utils.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|fn encode_int(value : Int32, base~ : Int32) -> Bytes {
#| let len = for x = value, len = 1; x >= base; {
#| continue x / base, len + 1
#| } nobreak {
#| len
#| }
#| let out = FixedArray::make(len, b'0')
#| for x = value, i = len - 1; i >= 0; x = x / base, i = i - 1 {
#| let digit = x % base
#| let chr = if digit < 10 { digit + '0' } else { digit - 10 + 'a' }
#| out[i] = chr.to_byte()
#| }
#| out.unsafe_reinterpret_as_bytes()
#|}
#|fn is_token_char(c : UInt16) -> Bool {
#| match c {
#| 'A'..<='Z' => true
#| 'a'..<='z' => true
#| '0'..<='9' => true
#| '!'
#| | '#'
#| | '$'
#| | '%'
#| | '&'
#| | '\''
#| | '*'
#| | '+'
#| | '-'
#| | '.'
#| | '^'
#| | '_'
#| | '`'
#| | '|'
#| | '~' => true
#| _ => return false
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|const TOKEN = re"[!#$%&'*+\-.^_`|~0-9A-Za-z]+"
#|#cfg(any(target="native", target="wasm"))
#|const OWS = re"[ \t]*"
#|#cfg(any(target="native", target="wasm"))
#|const QUOTED_STRING = re"\"([\t !#-\[\]-~\u{80}-\u{ff}]|\\[\t -~\u{80}-\u{ff}])*\""
#|#cfg(any(target="native", target="wasm"))
#|fn is_valid_http_header_field_name(name : StringView) -> Bool {
#| lexmatch name {
#| re"^" + TOKEN + re"$" => true
#| _ => false
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn validate_chunk_extension(extension : StringView) -> Unit raise {
#| guard extension is [';', ..] else { raise BadMessage }
#| for rest = extension {
#| lexmatch rest with longest {
#| re"^$" => return
#| (re"^;" + OWS + TOKEN + OWS, after=rest) =>
#| lexmatch rest with longest {
#| (re"^=" + OWS + TOKEN + OWS, after=rest) => continue rest
#| (re"^=" + OWS + QUOTED_STRING + OWS, after=rest) => continue rest
#| _ => continue rest
#| }
#| _ => raise BadMessage
#| }
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_internal_bytes_util_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/bytes_util",
deps={},
files={
"util.mbt": (
#|pub fn ascii_to_string(ascii : BytesView) -> String {
#| let builder = StringBuilder()
#| for byte in ascii {
#| match byte {
#| b'\n' => builder.write_char('\n')
#| b'\r' => builder <+ "\\r"
#| b'\t' => builder <+ "\\t"
#| 32..<=126 => builder.write_char(byte.to_int().unsafe_to_char())
#| x => builder <+ "\\x\{x.to_int().to_string(radix=16)}"
#| }
#| }
#| builder.to_string()
#|}
),
},
)
///|
let moonbitlang_core_async_internal_cancellable_queue_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/cancellable_queue",
deps={},
files={
"cancellable_queue.mbt": (
#|pub(open) trait Element {
#| fn get_index(Self) -> Int32
#| fn set_index(Self, Int32) -> Unit
#| fn get_next(Self) -> Int32
#| fn set_next(Self, Int32) -> Unit
#| fn get_prev(Self) -> Int32
#| fn set_prev(Self, Int32) -> Unit
#| fn is_queued(Self) -> Bool = _
#|}
#|impl Element with fn is_queued(self) {
#| self.get_index() >= 0
#|}
#|struct Queue[X] {
#| mut buf : FixedArray[X?]
#| mut len : Int32
#| mut head : Int32
#| mut last : Int32
#|}
#|pub fn[X] Queue::Queue() -> Queue[X] {
#| { buf: [], len: 0, head: -1, last: -1, }
#|}
#|pub fn[X : Element] Queue::pop(self : Queue[X]) -> X? {
#| guard self.len > 0 else { None }
#| guard! self.buf.unsafe_get(self.head) is Some(head)
#| self.remove(head)
#| Some(head)
#|}
#|fn[X] Queue::enlarge_to(self : Queue[X], size : Int32) -> Unit {
#| let new_buf = FixedArray::make(size, None)
#| self.buf.blit_to(new_buf, src_offset=0, dst_offset=0, len=self.len)
#| self.buf = new_buf
#|}
#|pub fn[X : Element] Queue::push(self : Queue[X], elem : X) -> Unit {
#| let capacity = self.buf.length()
#| if self.len >= capacity {
#| self.enlarge_to(if capacity < 16 { 16 } else { capacity * 2 })
#| }
#| elem.set_index(self.len)
#| elem.set_next(-1)
#| elem.set_prev(self.last)
#| self.buf.unsafe_set(self.len, Some(elem))
#| if self.last >= 0 {
#| guard! self.buf.unsafe_get(self.last) is Some(last)
#| last.set_next(self.len)
#| }
#| self.last = self.len
#| if self.head < 0 {
#| self.head = self.len
#| }
#| self.len += 1
#|}
#|pub fn[X : Element] Queue::remove(self : Queue[X], elem : X) -> Unit {
#| let index = elem.get_index()
#| let prev = elem.get_prev()
#| let next = elem.get_next()
#| if prev >= 0 {
#| guard! self.buf.unsafe_get(prev) is Some(prev)
#| prev.set_next(next)
#| }
#| if next >= 0 {
#| guard! self.buf.unsafe_get(next) is Some(next)
#| next.set_prev(prev)
#| }
#| if index == self.head {
#| self.head = next
#| }
#| if index == self.last {
#| self.last = prev
#| }
#| self.len -= 1
#| if index < self.len {
#| guard! self.buf.unsafe_get(self.len) is Some(last)
#| self.buf.unsafe_set(index, Some(last))
#| last.set_index(index)
#| if self.head == self.len {
#| self.head = index
#| }
#| if self.last == self.len {
#| self.last = index
#| }
#| let prev = last.get_prev()
#| let next = last.get_next()
#| if prev >= 0 {
#| guard! self.buf.unsafe_get(prev) is Some(prev)
#| prev.set_next(index)
#| }
#| if next >= 0 {
#| guard! self.buf.unsafe_get(next) is Some(next)
#| next.set_prev(index)
#| }
#| }
#| self.buf.unsafe_set(self.len, None)
#| elem.set_index(-1)
#| elem.set_prev(-1)
#| elem.set_next(-1)
#|}
),
},
)
///|
let moonbitlang_core_async_internal_coroutine_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/coroutine",
deps={},
files={
"async_primitive.mbt": (
#|async fn[T] async_suspend(cb : ((T) -> Unit) -> Unit) -> T noraise + nocancel = "%async.suspend"
#|fn run_async(f : async () -> Unit noraise + nocancel) = "%async.run"
#|pub async fn[X] raise_cancellation_signal() -> X noraise = "%async.raise_cancellation_signal"
#|async fn[X] capture_cancellation(
#| f : async () -> X,
#| handler~ : async () -> X nocancel,
#|) -> X nocancel = "%async.capture_cancellation"
),
"coroutine.mbt": (
#|pub enum SuspendResult {
#| Continue
#| Cancelled
#|}
#|priv enum State {
#| Done
#| Fail(Error)
#| Cancelled
#| Running
#| Suspend((SuspendResult) -> Unit)
#|}
#|pub struct Coroutine {
#| priv coro_id : Int32
#| priv mut state : State
#| priv mut shielded : Bool
#| priv mut cancelled : Bool
#| priv mut ready : Bool
#| priv downstream : @set.Set[Coroutine]
#| loc : SourceLoc
#|}
#|pub impl Eq for Coroutine with fn equal(c1, c2) {
#| c1.coro_id == c2.coro_id
#|}
#|pub impl Hash for Coroutine with fn hash_combine(self, hasher) {
#| Hash::hash_combine(self.coro_id, hasher)
#|}
#|pub fn Coroutine::wake(self : Coroutine) -> Unit {
#| if !self.ready && self.state is Suspend(_) {
#| self.ready = true
#| scheduler.run_later.push_back(self)
#| }
#|}
#|pub fn is_being_cancelled() -> Bool {
#| let coro = current_coroutine()
#| coro.cancelled && !coro.shielded
#|}
#|pub async fn check_cancellation() -> Unit noraise {
#| if is_being_cancelled() {
#| raise_cancellation_signal()
#| }
#|}
#|pub fn Coroutine::cancel(self : Coroutine) -> Unit {
#| self.cancelled = true
#| if !self.shielded {
#| self.wake()
#| }
#|}
#|pub async fn pause() -> Unit noraise {
#| guard! scheduler.curr_coro is Some(coro)
#| if coro.cancelled && !coro.shielded {
#| raise_cancellation_signal()
#| }
#| let result = async_suspend() <| cont => {
#| guard! coro.state is Running
#| coro.state = Suspend(cont)
#| coro.ready = true
#| scheduler.run_later.push_back(coro)
#| }
#| match result {
#| Continue => ()
#| Cancelled => raise_cancellation_signal()
#| }
#|}
#|pub async fn pause_nocancel() -> Unit noraise + nocancel {
#| guard! scheduler.curr_coro is Some(coro)
#| let shielded = coro.shielded
#| coro.shielded = true
#| defer {
#| coro.shielded = shielded
#| }
#| let result = async_suspend() <| cont => {
#| guard! coro.state is Running
#| coro.state = Suspend(cont)
#| coro.ready = true
#| scheduler.run_later.push_back(coro)
#| }
#| guard! result is Continue
#|}
#|pub async fn suspend_check_cancel() -> SuspendResult noraise + nocancel {
#| guard! scheduler.curr_coro is Some(coro)
#| if coro.cancelled && !coro.shielded {
#| return Cancelled
#| }
#| async_suspend() <| cont => {
#| guard! coro.state is Running
#| coro.state = Suspend(cont)
#| }
#|}
#|pub async fn suspend() -> Unit noraise {
#| match suspend_check_cancel() {
#| Continue => ()
#| Cancelled => raise_cancellation_signal()
#| }
#|}
#|#callsite(autofill(loc))
#|pub fn spawn(f : async () -> Unit, loc~ : SourceLoc) -> Coroutine {
#| scheduler.coro_id += 1
#| let coro = {
#| state: Running,
#| ready: true,
#| shielded: false,
#| downstream: Set([]),
#| coro_id: scheduler.coro_id,
#| cancelled: false,
#| loc,
#| }
#| fn run(_) {
#| run_async() <| () => {
#| try capture_cancellation(f, handler=() => coro.state = Cancelled) catch {
#| err => coro.state = Fail(err)
#| } noraise {
#| _ if coro.state is Running => coro.state = Done
#| _ => ()
#| }
#| for coro in coro.downstream {
#| coro.wake()
#| }
#| scheduler.all_coros.remove(coro)
#| }
#| }
#| coro.state = Suspend(run)
#| scheduler.run_later.push_back(coro)
#| scheduler.all_coros.insert(coro)
#| coro
#|}
#|pub fn Coroutine::unwrap(self : Coroutine) -> SuspendResult raise {
#| match self.state {
#| Done => Continue
#| Cancelled => Cancelled
#| Fail(err) => raise err
#| Running | Suspend(_) => panic()
#| }
#|}
#|pub async fn Coroutine::wait(target : Coroutine) -> SuspendResult {
#| guard! scheduler.curr_coro is Some(coro)
#| guard! !physical_equal(coro, target)
#| match target.state {
#| Done => return Continue
#| Cancelled => return Cancelled
#| Fail(err) => raise err
#| Running | Suspend(_) => ()
#| }
#| target.downstream.insert(coro)
#| defer target.downstream.remove(coro)
#| suspend()
#| target.unwrap()
#|}
#|pub fn Coroutine::check_error(coro : Coroutine) -> Unit raise {
#| match coro.state {
#| Fail(err) => raise err
#| Done | Cancelled | Running | Suspend(_) => ()
#| }
#|}
#|pub async fn[X] protect_from_cancel(f : async () -> X) -> X nocancel {
#| guard! scheduler.curr_coro is Some(coro)
#| capture_cancellation(handler=() => panic()) <| () => {
#| if coro.shielded {
#| f()
#| } else {
#| coro.shielded = true
#| defer {
#| coro.shielded = false
#| }
#| f()
#| }
#| }
#|}
#|pub async fn[X] handle_cancellation(f : async () -> X) -> X? {
#| capture_cancellation(handler=() => None, () => Some(f()))
#|}
),
"scheduler.mbt": (
#|struct Scheduler {
#| mut coro_id : Int32
#| mut curr_coro : Coroutine?
#| run_later : @deque.Deque[Coroutine]
#| all_coros : @set.Set[Coroutine]
#|}
#|pub let scheduler : Scheduler = {
#| coro_id: 0,
#| curr_coro: None,
#| run_later: Deque([]),
#| all_coros: Set([]),
#|}
#|pub fn current_coroutine() -> Coroutine {
#| scheduler.curr_coro.unwrap()
#|}
#|pub fn has_immediately_ready_task() -> Bool {
#| !scheduler.run_later.is_empty()
#|}
#|pub fn reschedule() -> Unit {
#| let n = scheduler.run_later.length()
#| for _ in 0.. @set.Set[Coroutine] {
#| scheduler.all_coros
#|}
),
},
)
///|
let moonbitlang_core_async_internal_env_util_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/env_util",
deps={},
files={
"env_util.mbt": (
#|#cfg(target="native")
#|extern "C" fn get_pid() -> UInt32 = "moonbit_rt_getpid"
#|#cfg(target="wasm")
#|#unsafe_skip_stub_check
#|fn get_pid() -> UInt32 = "moonbitlang/async" "env_util/getpid"
#|#cfg(any(target="native", target="wasm"))
#|pub let current_process : UInt32 = get_pid()
#|pub using @types {type Platform}
#|#cfg(target="native")
#|extern "C" fn get_platform() -> Platform = "moonbit_rt_get_platform"
#|#cfg(target="wasm")
#|#unsafe_skip_stub_check
#|fn get_platform() -> Platform = "moonbitlang/async" "runtime/get_platform"
#|#cfg(any(target="native", target="wasm"))
#|pub let platform : Platform = get_platform()
),
},
)
///|
let moonbitlang_core_async_internal_errno_util_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/errno_util",
deps={},
files={
"errno_js.mbt": (
#|#external
#|priv type SystemErrorMap
#|#module("node:util")
#|extern "js" fn get_system_error_map() -> SystemErrorMap = "getSystemErrorMap"
#|extern "js" fn cached_system_error_map(
#| get_map : () -> SystemErrorMap,
#|) -> SystemErrorMap =
#| #| function cached(getMap) {
#| #| return cached.map ??= getMap();
#| #| }
#|extern "js" fn SystemErrorMap::name(
#| self : SystemErrorMap,
#| code : Int32,
#|) -> String =
#| #| (map, code) => map.get(code)?.[0] ?? ""
#|extern "js" fn SystemErrorMap::message(
#| self : SystemErrorMap,
#| code : Int32,
#|) -> String =
#| #| (map, code) => map.get(code)?.[1] ?? `Unknown system error ${code}`
#|#internal(internal, "for internal use only")
#|pub fn errno_to_name(errno : Int32) -> String {
#| cached_system_error_map(get_system_error_map).name(errno)
#|}
#|#internal(internal, "for internal use only")
#|pub fn errno_to_string(errno : Int32) -> String {
#| cached_system_error_map(get_system_error_map).message(errno)
#|}
),
},
)
///|
let moonbitlang_core_async_internal_event_loop_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/event_loop",
deps={},
files={
"event_loop.js.mbt": (
#|pub fn now() -> Int64 {
#| @env.now().reinterpret_as_int64()
#|}
#|pub fn reschedule() -> Unit {
#| guard !@coroutine.all_coroutines().is_empty() else { }
#| @coroutine.reschedule()
#| if @coroutine.has_immediately_ready_task() {
#| ignore(set_timeout(0, reschedule))
#| }
#|}
),
"timer.js.mbt": (
#|#external
#|type Timer
#|extern "js" fn set_timeout(duration : Int32, f : () -> Unit) -> Timer =
#| #| (duration, f) => setTimeout(f, duration)
#|pub fn Timer::new(duration : Int32, f : () -> Unit) -> Timer {
#| set_timeout(duration) <| () => {
#| f()
#| reschedule()
#| }
#|}
#|pub extern "js" fn Timer::cancel(self : Timer) =
#| #| (timer) => clearTimeout(timer)
#|pub async fn sleep(duration : Int32) -> Unit noraise {
#| let coro = @coroutine.current_coroutine()
#| let timer = Timer::new(duration, () => coro.wake())
#| defer timer.cancel()
#| @coroutine.suspend()
#|}
),
"types.mbt": "",
},
)
///|
let moonbitlang_core_async_internal_io_buffer_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/io_buffer",
deps={},
files={
"read_buffer.mbt": (
#|pub(all) struct Buffer {
#| mut buf : FixedArray[Byte]
#| mut start : Int32
#| mut len : Int32
#|}
#|#as_free_fn
#|pub fn Buffer::new(size : Int32) -> Buffer {
#| { buf: FixedArray::make(size, 0), start: 0, len: 0, }
#|}
#|pub fn Buffer::clear(buf : Buffer) -> Unit {
#| buf.buf = []
#| buf.start = 0
#| buf.len = 0
#|}
#|const SEGMENT_SIZE = 1024
#|pub fn Buffer::enlarge_to(self : Buffer, n : Int32) -> Unit {
#| let capacity = self.buf.length()
#| if self.start + n <= capacity {
#| return
#| }
#| if n <= capacity {
#| self.buf.blit_to(self.buf, src_offset=self.start, len=self.len)
#| self.start = 0
#| return
#| }
#| let padding = n % SEGMENT_SIZE
#| let target_len = if padding == 0 { n } else { n - padding + SEGMENT_SIZE }
#| let new_buf = FixedArray::make(target_len, b'0')
#| self.buf.blit_to(new_buf, src_offset=self.start, dst_offset=0, len=self.len)
#| self.buf = new_buf
#| self.start = 0
#|}
#|pub fn Buffer::drop(self : Buffer, n : Int32) -> Unit {
#| guard! n <= self.len
#| self.len -= n
#| if self.len == 0 {
#| self.start = 0
#| } else {
#| self.start += n
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_internal_test_util_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/internal/test_util",
deps={},
files={
"test_util.mbt": (
#|#cfg(target="native")
#|pub extern "C" fn increase_current_thread_priority() -> Unit = "increase_current_thread_priority"
#|#cfg(not(target="native"))
#|pub fn increase_current_thread_priority() -> Unit {
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub async fn build_test_program(name : String) -> String {
#| guard! @env.current_dir() is Some(cwd)
#| let bundle = @process.run("moon", stdout=Collect, stderr=DuplicateStdout, [
#| "bundle", "--target", "native",
#| ])
#| guard bundle.exit_code is 0 else {
#| fail("failed to bundle core for test program: \{bundle.stdout_text()}")
#| }
#| let proc = @process.run(
#| "moon",
#| [
#| "-C", "async/test_programs/", "build", name, "--target", "native", "--debug",
#| "--target-dir", "../../_build/test_programs",
#| ],
#| stdout=Collect,
#| stderr=DuplicateStdout,
#| extra_env={ "MOON_CORE_OVERRIDE": cwd },
#| )
#| guard proc.exit_code is 0 else {
#| fail("failed to build test program `\{name}`: \{proc.stdout_text()}")
#| }
#| "\{cwd}/_build/test_programs/native/debug/build/\{name}/\{name}.exe"
#|}
),
},
)
///|
let moonbitlang_core_async_io_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/io",
deps={},
files={
"binary_reader.mbt": (
#|pub(open) trait BinaryReaderImpl {
#| fn name(Self) -> String
#| fn get_controller(Self) -> BinaryReaderController
#| async fn raw_read_impl(
#| Self,
#| buf : FixedArray[Byte],
#| offset~ : Int32,
#| max_len~ : Int32,
#| ) -> Int32
#|}
#|pub(open) trait BinaryReader: BinaryReaderImpl {
#| async fn read_into(Self, MutArrayView[Byte]) -> Int32 raise ReaderError = _
#| async fn read(Self) -> Bytes? raise ReaderError = _
#| async fn read_at_least(Self, Int32) -> Bytes raise ReaderError = _
#| async fn peek(Self) -> BytesView? raise ReaderError = _
#| async fn peek_at_least(Self, Int32) -> BytesView raise ReaderError = _
#| async fn read_exactly(Self, len : Int32) -> Bytes raise ReaderError = _
#| async fn drop_exactly(Self, len : Int32) -> Unit raise ReaderError = _
#| async fn peek_exactly(Self, len : Int32) -> BytesView raise ReaderError = _
#| async fn read_all(Self) -> Bytes raise ReaderError = _
#| async fn drop_all(Self) -> Int64 raise ReaderError = _
#| async fn read_until(Self, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise ReaderError = _
#| async fn peek_until(Self, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise ReaderError = _
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|#non_exhaustive
#|pub(all) suberror ReaderError {
#| UnexpectedEof
#| LengthLimitExceeded
#| MalformedText
#| TransportError(Error, context~ : String)
#|}
#|fn MutArrayView::buf(self : MutArrayView[Byte]) -> FixedArray[Byte] = "%arrayview.buf"
#|impl BinaryReader with fn read_into(self, dst) {
#| let buf = dst.buf()
#| let offset = dst.start_offset()
#| let max_len = dst.length()
#| guard max_len > 0 else {
#| abort("`BinaryReader::read_into()`: `dst` must not be empty")
#| }
#| let controller = BinaryReaderImpl::get_controller(self)
#| if controller.buf_len > 0 {
#| controller.blit_to(buf, offset~, max_len~)
#| } else {
#| BinaryReaderImpl::raw_read_impl(self, buf, offset~, max_len~) catch {
#| err => raise TransportError(err, context=BinaryReaderImpl::name(self))
#| }
#| }
#|}
#|impl BinaryReader with fn read(self) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| if self.peek() is Some(data) {
#| let data = data.to_owned()
#| controller.drop(data.length())
#| Some(data)
#| } else {
#| None
#| }
#|}
#|impl BinaryReader with fn read_at_least(self, min_len : Int32) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| let data = self.peek_at_least(min_len)
#| let data = data.to_owned()
#| controller.drop(data.length())
#| data
#|}
#|impl BinaryReader with fn peek(self) {
#| try self.peek_at_least(1) catch {
#| UnexpectedEof => None
#| err => raise err
#| } noraise {
#| data => Some(data)
#| }
#|}
#|impl BinaryReader with fn peek_at_least(self, min_len : Int32) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| controller.enlarge_to(min_len)
#| while controller.buf_len < min_len {
#| let offset = controller.buf_start + controller.buf_len
#| let n = BinaryReaderImpl::raw_read_impl(
#| self,
#| controller.buf,
#| offset~,
#| max_len=controller.buf.length() - offset,
#| ) catch {
#| err => raise TransportError(err, context=BinaryReaderImpl::name(self))
#| }
#| if n is 0 {
#| raise UnexpectedEof
#| }
#| controller.buf_len += n
#| }
#| controller.buffered()
#|}
#|impl BinaryReader with fn read_exactly(self, len) {
#| let buf = FixedArray::make(len, b'\x00')
#| for received = 0; received < len; {
#| let new_received = self.read_into(buf.mut_view(start=received, end=len))
#| if new_received is 0 {
#| raise UnexpectedEof
#| }
#| continue received + new_received
#| }
#| buf.unsafe_reinterpret_as_bytes()
#|}
#|impl BinaryReader with fn drop_exactly(self, len) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| if controller.buf_len >= len {
#| controller.drop(len)
#| return
#| }
#| let buffered = controller.buf_len
#| controller.drop(buffered)
#| controller.enlarge_to(1)
#| for dropped = buffered {
#| let n = BinaryReaderImpl::raw_read_impl(
#| self,
#| controller.buf,
#| offset=controller.buf_start,
#| max_len=controller.buf.length() - controller.buf_start,
#| ) catch {
#| err => raise TransportError(err, context=BinaryReaderImpl::name(self))
#| }
#| if n is 0 {
#| raise UnexpectedEof
#| }
#| if dropped + n >= len {
#| let remaining = dropped + n - len
#| controller.buf_start += n - remaining
#| controller.buf_len = remaining
#| break
#| }
#| continue dropped + n
#| }
#|}
#|impl BinaryReader with fn peek_exactly(self, len) {
#| self.peek_at_least(len)[:len]
#|}
#|#internal(internal, "for internal use only")
#|#doc(hidden)
#|pub async fn[R : BinaryReader] read_all_binary(
#| reader : R,
#| prefix_list~ : FixedArray[BytesView] = [],
#| chunk_size~ : Int32 = 4096,
#|) -> Bytes raise ReaderError {
#| let buffer_list = []
#| let mut buffer = FixedArray::make(chunk_size, b'\x00')
#| let mut offset = 0
#| while reader.read_into(buffer.mut_view(start=offset)) is n && n > 0 {
#| offset += n
#| if offset == buffer.length() {
#| buffer_list.push(buffer)
#| buffer = FixedArray::make(chunk_size, b'0')
#| offset = 0
#| }
#| }
#| let prefix_len = for prefix in prefix_list; len = 0 {
#| continue len + prefix.length()
#| } nobreak {
#| len
#| }
#| if buffer_list.length() > (@int.MAX_VALUE - offset - prefix_len) / chunk_size {
#| raise LengthLimitExceeded
#| }
#| let total_size = prefix_len + buffer_list.length() * chunk_size + offset
#| let result = FixedArray::make(total_size, b'0')
#| for
#| prefix in prefix_list
#| dst_offset = 0
#| dst_offset = dst_offset + prefix.length() {
#| result.blit_from_bytesview(dst_offset, prefix)
#| }
#| for i, buf in buffer_list {
#| result.unsafe_blit(prefix_len + i * chunk_size, buf, 0, chunk_size)
#| }
#| result.unsafe_blit(
#| prefix_len + buffer_list.length() * chunk_size,
#| buffer,
#| 0,
#| offset,
#| )
#| result.unsafe_reinterpret_as_bytes()
#|}
#|impl BinaryReader with fn read_all(self) {
#| read_all_binary(self)
#|}
#|impl BinaryReader with fn drop_all(self) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| let buffered = controller.buf_len
#| if buffered > 0 {
#| controller.drop(buffered)
#| }
#| controller.enlarge_to(1)
#| for dropped = buffered.to_int64() {
#| let n = BinaryReaderImpl::raw_read_impl(
#| self,
#| controller.buf,
#| offset=controller.buf_start,
#| max_len=controller.buf.length() - controller.buf_start,
#| ) catch {
#| err => raise TransportError(err, context=BinaryReaderImpl::name(self))
#| }
#| if n is 0 {
#| break dropped
#| }
#| continue dropped + n.to_int64()
#| }
#|}
#|impl BinaryReader with fn read_until(
#| self,
#| pattern,
#| include_delimiter~ : Bool = false,
#|) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| if self.peek_until(pattern, include_delimiter~) is Some(result) {
#| let result = result.to_owned()
#| let consumed = if include_delimiter {
#| result.length()
#| } else {
#| @cmp.minimum(result.length() + pattern.length(), controller.buf_len)
#| }
#| controller.drop(consumed)
#| Some(result)
#| } else {
#| None
#| }
#|}
#|impl BinaryReader with fn peek_until(
#| self,
#| pattern,
#| include_delimiter~ : Bool = false,
#|) {
#| let controller = BinaryReaderImpl::get_controller(self)
#| let pattern_len = pattern.length()
#| guard pattern_len > 0 else {
#| abort("BinaryReader::peek_until() requires a non-empty pattern")
#| }
#| for search_start = 0 {
#| let search_region = controller.buffered()[search_start:]
#| if search_region.find(pattern) is Some(i) {
#| let pattern_start = search_start + i
#| let end_index = if include_delimiter {
#| pattern_start + pattern_len
#| } else {
#| pattern_start
#| }
#| return Some(controller.buffered()[:end_index])
#| }
#| let old_len = controller.buf_len
#| try self.peek_at_least(old_len + 1) catch {
#| UnexpectedEof if controller.buf_len == 0 => return None
#| UnexpectedEof => return Some(controller.buffered())
#| err => raise err
#| } noraise {
#| _ => ()
#| }
#| continue @cmp.maximum(0, old_len - pattern_len + 1)
#| }
#|}
),
"binary_reader_controller.mbt": (
#|struct BinaryReaderController {
#| mut buf : FixedArray[Byte]
#| mut buf_start : Int32
#| mut buf_len : Int32
#|}
#|const BUFFER_PREFIX_SIZE = 8
#|pub fn BinaryReaderController::BinaryReaderController() -> BinaryReaderController {
#| {
#| buf: FixedArray::make(BUFFER_PREFIX_SIZE, b'0'),
#| buf_start: BUFFER_PREFIX_SIZE,
#| buf_len: 0,
#| }
#|}
#|#doc(hidden)
#|#internal(internal, "for internal use only")
#|pub fn BinaryReaderController::buffered(
#| self : BinaryReaderController,
#|) -> BytesView {
#| let end = self.buf_start + self.buf_len
#| self.buf.unsafe_reinterpret_as_bytes()[self.buf_start:end]
#|}
#|#doc(hidden)
#|#internal(internal, "for internal use only")
#|pub fn BinaryReaderController::raw_buffer(
#| self : BinaryReaderController,
#|) -> FixedArray[Byte] {
#| self.buf
#|}
#|#doc(hidden)
#|#internal(internal, "for internal use only")
#|pub fn BinaryReaderController::clear(self : BinaryReaderController) -> Unit {
#| self.buf = FixedArray::make(BUFFER_PREFIX_SIZE, b'0')
#| self.buf_start = BUFFER_PREFIX_SIZE
#| self.buf_len = 0
#|}
#|const SEGMENT_SIZE = 1024
#|fn BinaryReaderController::enlarge_to(
#| self : BinaryReaderController,
#| n : Int32,
#|) -> Unit {
#| let capacity = self.buf.length()
#| if n <= capacity - self.buf_start {
#| return
#| }
#| if n <= capacity - BUFFER_PREFIX_SIZE {
#| self.buf.blit_to(
#| self.buf,
#| src_offset=self.buf_start,
#| dst_offset=BUFFER_PREFIX_SIZE,
#| len=self.buf_len,
#| )
#| self.buf_start = BUFFER_PREFIX_SIZE
#| return
#| }
#| let required_capacity = n + BUFFER_PREFIX_SIZE
#| let padding = required_capacity % SEGMENT_SIZE
#| let target_len = if padding == 0 {
#| required_capacity
#| } else {
#| required_capacity - padding + SEGMENT_SIZE
#| }
#| let new_buf = FixedArray::make(target_len, b'0')
#| self.buf.blit_to(
#| new_buf,
#| src_offset=self.buf_start,
#| dst_offset=BUFFER_PREFIX_SIZE,
#| len=self.buf_len,
#| )
#| self.buf = new_buf
#| self.buf_start = BUFFER_PREFIX_SIZE
#|}
#|#doc(hidden)
#|pub fn BinaryReaderController::drop(
#| self : BinaryReaderController,
#| n : Int32,
#|) -> Unit {
#| guard! n <= self.buf_len
#| self.buf_len -= n
#| if self.buf_len == 0 {
#| self.buf_start = BUFFER_PREFIX_SIZE
#| } else {
#| self.buf_start += n
#| }
#|}
#|fn BinaryReaderController::blit_to(
#| self : BinaryReaderController,
#| dst : FixedArray[Byte],
#| offset~ : Int32,
#| max_len~ : Int32,
#|) -> Int32 {
#| guard self.buf_len > 0 else { return 0 }
#| let n = @cmp.minimum(self.buf_len, max_len)
#| self.buf.blit_to(dst, src_offset=self.buf_start, dst_offset=offset, len=n)
#| self.drop(n)
#| n
#|}
),
"binary_writer.mbt": (
#|pub(open) trait BinaryWriterImpl {
#| fn name(Self) -> String
#| fn get_controller(Self) -> BinaryWriterController
#| async fn raw_write_impl(Self, Bytes, offset~ : Int32, len~ : Int32) -> Int32
#| async fn flush_partial_data(Self) -> Unit
#|}
#|pub(open) trait BinaryWriter: BinaryWriterImpl {
#| async fn write(Self, BytesView) -> Unit raise WriterError = _
#| async fn write_reader(Self, &BinaryReader) -> Unit = _
#| async fn flush_partial_data(Self) -> Unit = _
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|#non_exhaustive
#|pub(all) suberror WriterError {
#| TransportError(Error, context~ : String)
#|}
#|impl BinaryWriter with fn write(self, data) {
#| let buf = data.data()
#| let start = data.start_offset()
#| let len = data.length()
#| guard len > 0 else { return }
#| let end = start + len
#| for offset = start; offset < end; {
#| let progress = BinaryWriterImpl::raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len=end - offset,
#| ) catch {
#| err => raise TransportError(err, context=BinaryWriterImpl::name(self))
#| }
#| continue offset + progress
#| }
#|}
#|impl BinaryWriter with fn write_reader(self, reader) {
#| while reader.peek() is Some(chunk) {
#| self.write(chunk)
#| reader.drop_exactly(chunk.length())
#| }
#|}
#|impl BinaryWriter with fn flush_partial_data(self) {
#| BinaryWriterImpl::flush_partial_data(self)
#|}
),
"binary_writer_controller.mbt": (
#|struct BinaryWriterController {}
#|#warnings("-unnecessary_annotation")
#|pub fn BinaryWriterController::BinaryWriterController() -> BinaryWriterController {
#| BinaryWriterController::{ }
#|}
),
"buffered_writer.mbt": (
#|struct BufferedWriter[W] {
#| writer : W
#| buf : FixedArray[Byte]
#| mut len : Int32
#| ctrl : BinaryWriterController
#|}
#|#alias(new, deprecated="Use `BufferedWriter(writer, size=...)` instead")
#|pub fn[W] BufferedWriter::BufferedWriter(
#| writer : W,
#| size~ : Int32 = 1024,
#|) -> BufferedWriter[W] {
#| {
#| writer,
#| buf: FixedArray::make(size, 0),
#| len: 0,
#| ctrl: BinaryWriterController(),
#| }
#|}
#|pub fn[W] BufferedWriter::capacity(self : BufferedWriter[W]) -> Int32 {
#| self.buf.length()
#|}
#|pub async fn[W : BinaryWriter] BufferedWriter::flush(
#| self : BufferedWriter[W],
#|) -> Unit {
#| if self.len > 0 {
#| self.writer.write(self.buf.unsafe_reinterpret_as_bytes()[:self.len])
#| self.len = 0
#| }
#|}
#|pub impl[W : BinaryWriter] BinaryWriter for BufferedWriter[W]
#|impl[W : BinaryWriter] BinaryWriterImpl for BufferedWriter[W] with fn raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len~,
#|) {
#| if self.len >= self.buf.length() {
#| self.flush()
#| }
#| let len = @cmp.minimum(len, self.buf.length() - self.len)
#| self.buf.blit_from_bytes(self.len, buf, offset, len)
#| self.len += len
#| len
#|}
#|impl[W : BinaryWriter] BinaryWriterImpl for BufferedWriter[W] with fn name(
#| _self,
#|) {
#| "@io.BufferedWriter"
#|}
#|impl[W : BinaryWriter] BinaryWriterImpl for BufferedWriter[W] with fn get_controller(
#| self,
#|) {
#| self.ctrl
#|}
#|impl[W : BinaryWriter] BinaryWriterImpl for BufferedWriter[W] with fn flush_partial_data(
#| self,
#|) {
#| self.flush()
#| BinaryWriter::flush_partial_data(self.writer)
#|}
#|pub extend BufferedWriter with BinaryWriter::{
#| write,
#| write_reader,
#| flush_partial_data,
#|}
),
"memory_reader.mbt": (
#|struct MemoryReader {
#| reader : MemoryPipeRead
#| writer : @coroutine.Coroutine
#|}
#|#callsite(autofill(loc))
#|pub fn MemoryReader::MemoryReader(
#| f : async (&BinaryWriter) -> Unit,
#| loc~ : SourceLoc,
#|) -> MemoryReader {
#| let (r, w) = memory_pipe()
#| let writer = @coroutine.spawn(loc~) <| () => {
#| defer w.close()
#| f(w)
#| }
#| { reader: r, writer, }
#|}
#|pub fn MemoryReader::close(self : MemoryReader) -> Unit {
#| self.reader.close()
#| self.writer.cancel()
#|}
#|pub impl BinaryReader for MemoryReader
#|impl BinaryReaderImpl for MemoryReader with fn name(_self) {
#| "@io.MemoryReader"
#|}
#|impl BinaryReaderImpl for MemoryReader with fn get_controller(self) {
#| BinaryReaderImpl::get_controller(self.reader)
#|}
#|impl BinaryReaderImpl for MemoryReader with fn raw_read_impl(
#| self,
#| buf,
#| offset~,
#| max_len~,
#|) {
#| let n = BinaryReaderImpl::raw_read_impl(self.reader, buf, offset~, max_len~)
#| self.writer.check_error()
#| n
#|}
#|pub extend MemoryReader with BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
),
"pipe.mbt": (
#|priv enum PipeReader {
#| NoReader
#| ReadEndClosed
#| HasReader(
#| coro~ : @coroutine.Coroutine,
#| buf~ : FixedArray[Byte],
#| offset~ : Int32,
#| max_len~ : Int32,
#| mut result~ : Int32,
#| )
#|}
#|priv enum PipeWriter {
#| NoWriter
#| WriteEndClosed
#| HasWriter(
#| coro~ : @coroutine.Coroutine,
#| buf~ : Bytes,
#| offset~ : Int32,
#| len~ : Int32,
#| mut result~ : Int32,
#| )
#|}
#|priv struct Pipe {
#| mut reader : PipeReader
#| mut writer : PipeWriter
#|}
#|#valtype
#|struct MemoryPipeRead {
#| pipe : Pipe
#| ctrl : BinaryReaderController
#|}
#|#valtype
#|struct MemoryPipeWrite {
#| pipe : Pipe
#| ctrl : BinaryWriterController
#|}
#|pub fn memory_pipe() -> (MemoryPipeRead, MemoryPipeWrite) {
#| let pipe : Pipe = { reader: NoReader, writer: NoWriter, }
#| (
#| { pipe, ctrl: BinaryReaderController(), },
#| { pipe, ctrl: BinaryWriterController(), },
#| )
#|}
#|pub fn MemoryPipeRead::close(self : MemoryPipeRead) -> Unit {
#| if self.pipe.writer is (HasWriter(_) as writer) {
#| writer.result = 0
#| writer.coro.wake()
#| }
#| self.pipe.reader = ReadEndClosed
#|}
#|pub fn MemoryPipeWrite::close(self : MemoryPipeWrite) -> Unit {
#| if self.pipe.reader is (HasReader(_) as reader) {
#| reader.result = 0
#| reader.coro.wake()
#| }
#| self.pipe.writer = WriteEndClosed
#|}
#|pub impl BinaryReader for MemoryPipeRead
#|impl BinaryReaderImpl for MemoryPipeRead with fn raw_read_impl(
#| self,
#| dst,
#| offset~,
#| max_len~,
#|) {
#| guard! self.pipe.reader is NoReader
#| @coroutine.check_cancellation()
#| match self.pipe.writer {
#| NoWriter => {
#| let coro = @coroutine.current_coroutine()
#| let reader = HasReader(coro~, buf=dst, offset~, max_len~, result=0)
#| self.pipe.reader = reader
#| defer (if self.pipe.reader is HasReader(_) {
#| self.pipe.reader = NoReader
#| })
#| match @coroutine.suspend_check_cancel() {
#| Continue => {
#| guard! reader is HasReader(result~, ..)
#| result
#| }
#| Cancelled if reader is HasReader(result~, ..) && result > 0 =>
#| return result
#| Cancelled => @coroutine.raise_cancellation_signal()
#| }
#| }
#| WriteEndClosed => 0
#| HasWriter(_) as writer => {
#| let len = @cmp.minimum(max_len, writer.len)
#| dst.blit_from_bytes(offset, writer.buf, writer.offset, len)
#| writer.result = len
#| writer.coro.wake()
#| @coroutine.pause_nocancel()
#| len
#| }
#| }
#|}
#|impl BinaryReaderImpl for MemoryPipeRead with fn name(_self) {
#| "@io.MemoryPipeRead"
#|}
#|impl BinaryReaderImpl for MemoryPipeRead with fn get_controller(self) {
#| self.ctrl
#|}
#|pub extend MemoryPipeRead with BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
#|#derive(@debug.Debug, @json.ToJson)
#|pub suberror PipeClosed
#|pub impl BinaryWriter for MemoryPipeWrite
#|impl BinaryWriterImpl for MemoryPipeWrite with fn raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len~,
#|) {
#| guard! self.pipe.writer is NoWriter
#| @coroutine.check_cancellation()
#| match self.pipe.reader {
#| NoReader => {
#| let coro = @coroutine.current_coroutine()
#| let writer = HasWriter(coro~, buf~, offset~, len~, result=0)
#| self.pipe.writer = writer
#| defer (if self.pipe.writer is HasWriter(_) {
#| self.pipe.writer = NoWriter
#| })
#| match @coroutine.suspend_check_cancel() {
#| Continue => {
#| guard! writer is HasWriter(result~, ..)
#| guard result > 0 else { raise PipeClosed }
#| result
#| }
#| Cancelled if self.pipe.writer is HasWriter(result~, ..) && result > 0 =>
#| result
#| Cancelled => @coroutine.raise_cancellation_signal()
#| }
#| }
#| ReadEndClosed => raise PipeClosed
#| HasReader(_) as reader => {
#| let len = @cmp.minimum(len, reader.max_len)
#| reader.buf.blit_from_bytes(reader.offset, buf, offset, len)
#| reader.result = len
#| reader.coro.wake()
#| @coroutine.pause_nocancel()
#| len
#| }
#| }
#|}
#|impl BinaryWriterImpl for MemoryPipeWrite with fn name(_self) {
#| "@io.MemoryPipeWrite"
#|}
#|impl BinaryWriterImpl for MemoryPipeWrite with fn get_controller(self) {
#| self.ctrl
#|}
#|impl BinaryWriterImpl for MemoryPipeWrite with fn flush_partial_data(self) {
#| ignore(self)
#|}
#|pub extend MemoryPipeWrite with BinaryWriter::{write, write_reader}
),
"text_reader.mbt": (
#|pub(open) trait TextReader {
#| async fn read(Self) -> String? raise ReaderError
#| async fn read_all(Self) -> String raise ReaderError
#| async fn read_until(Self, StringView, include_delimiter? : Bool) -> String? raise ReaderError
#| async fn read_line(Self, include_newline? : Bool) -> String? raise ReaderError
#| async fn read_lines(Self, include_newline? : Bool) -> Array[String] raise ReaderError = _
#|}
#|struct Utf8Reader {
#| mut inner : &BinaryReader?
#|}
#|pub fn[R : BinaryReader] Utf8Reader::Utf8Reader(inner : R) -> Utf8Reader {
#| { inner: Some(inner), }
#|}
#|#doc(hidden)
#|pub fn Utf8Reader::new_uninitialized() -> Utf8Reader {
#| { inner: None, }
#|}
#|#doc(hidden)
#|pub fn[R : BinaryReader] Utf8Reader::set_inner(
#| self : Utf8Reader,
#| inner : R,
#|) -> Unit {
#| self.inner = Some(inner)
#|}
#|#doc(hidden)
#|pub fn Utf8Reader::close(self : Utf8Reader) -> Unit {
#| self.inner = None
#|}
#|pub impl TextReader for Utf8Reader with fn read(self) {
#| guard! self.inner is Some(inner)
#| let controller = BinaryReaderImpl::get_controller(inner)
#| guard inner.peek() is Some(binary) else { return None }
#| let (data, missing_bytes) = match binary {
#| [.. data, 0xC0..<0xE0] => (data, 1)
#| [.. data, 0xE0..<0xF0] => (data, 2)
#| [.. data, 0xE0..<0xF0, 0x80..<0xC0] => (data, 1)
#| [.. data, 0xF0..<=0xF4] => (data, 3)
#| [.. data, 0xF0..<=0xF4, 0x80..<0xC0] => (data, 2)
#| [.. data, 0xF0..<=0xF4, 0x80..<0XC0, 0x80..<0xC0] => (data, 1)
#| data => (data, 0)
#| }
#| if data.length() > 0 {
#| let text = @utf8.decode(data) catch { Malformed(_) => raise MalformedText }
#| inner.drop_exactly(data.length())
#| return Some(text)
#| }
#| try inner.peek_at_least(binary.length() + missing_bytes) catch {
#| UnexpectedEof => {
#| let data = controller.buffered()
#| let text = @utf8.decode(data) catch {
#| Malformed(_) => raise MalformedText
#| }
#| inner.drop_exactly(data.length())
#| return Some(text)
#| }
#| err => raise err
#| } noraise {
#| _ =>
#| self.read()
#| }
#|}
#|pub impl TextReader for Utf8Reader with fn read_all(self) {
#| guard! self.inner is Some(inner)
#| let data = inner.read_all()
#| @utf8.decode(data) catch {
#| Malformed(_) => raise MalformedText
#| }
#|}
#|pub impl TextReader for Utf8Reader with fn read_until(
#| self,
#| pattern,
#| include_delimiter~ : Bool = false,
#|) {
#| guard! self.inner is Some(inner)
#| let pattern_bytes = @utf8.encode(pattern)
#| match inner.read_until(pattern_bytes, include_delimiter~) {
#| None => None
#| Some(result) => {
#| let text = @utf8.decode(result) catch {
#| Malformed(_) => raise MalformedText
#| }
#| Some(text)
#| }
#| }
#|}
#|pub impl TextReader for Utf8Reader with fn read_line(
#| self,
#| include_newline~ : Bool = false,
#|) {
#| guard! self.inner is Some(inner)
#| guard inner.peek_until(b"\n", include_delimiter=true) is Some(whole_line) else {
#| return None
#| }
#| let line_bytes = match whole_line {
#| line if include_newline => line
#| [.. line, b'\r', b'\n'] => line
#| [.. line, b'\n'] => line
#| line => line
#| }
#| inner.drop_exactly(whole_line.length())
#| Some(@utf8.decode(line_bytes)) catch {
#| Malformed(_) => raise MalformedText
#| }
#|}
#|impl TextReader with fn read_lines(self, include_newline~ : Bool = false) {
#| let lines = []
#| while self.read_line(include_newline~) is Some(line) {
#| lines.push(line)
#| }
#| lines
#|}
#|pub extend Utf8Reader with TextReader::{
#| read,
#| read_all,
#| read_until,
#| read_line,
#| read_lines,
#|}
),
"text_view.mbt": (
#|struct Utf8TextView {
#| reader : Utf8Reader
#| writer : Utf8Writer
#|}
#|#doc(hidden)
#|pub fn Utf8TextView::new_uninitialized() -> Utf8TextView {
#| {
#| reader: Utf8Reader::new_uninitialized(),
#| writer: Utf8Writer::new_uninitialized(),
#| }
#|}
#|#doc(hidden)
#|pub fn[X : BinaryReader + BinaryWriter] Utf8TextView::set_inner(
#| self : Utf8TextView,
#| inner : X,
#|) -> Unit {
#| self.reader.set_inner(inner)
#| self.writer.set_inner(inner)
#|}
#|#doc(hidden)
#|pub fn Utf8TextView::close(self : Utf8TextView) -> Unit {
#| self.reader.close()
#| self.writer.close()
#|}
#|pub impl TextReader for Utf8TextView with fn read(self) {
#| self.reader.read()
#|}
#|pub impl TextReader for Utf8TextView with fn read_all(self) {
#| self.reader.read_all()
#|}
#|pub impl TextReader for Utf8TextView with fn read_until(
#| self,
#| pattern,
#| include_delimiter?,
#|) {
#| self.reader.read_until(pattern, include_delimiter?)
#|}
#|pub impl TextReader for Utf8TextView with fn read_line(
#| self,
#| include_newline~ : Bool = false,
#|) {
#| self.reader.read_line(include_newline~)
#|}
#|pub extend Utf8TextView with TextReader::{
#| read,
#| read_all,
#| read_until,
#| read_line,
#| read_lines,
#|}
#|pub impl TextWriter for Utf8TextView with fn write(self, data) {
#| self.writer.write(data)
#|}
#|pub impl TextWriter for Utf8TextView with fn write_json(self, data) {
#| self.writer.write_json(data)
#|}
#|pub extend Utf8TextView with TextWriter::{
#| write,
#| write_json,
#| write_string,
#| write_string_interpolation,
#|}
),
"text_writer.mbt": (
#|pub(open) trait TextWriter {
#| async fn write(Self, StringView) -> Unit raise WriterError
#| async fn write_json(Self, @json.Json) -> Unit raise WriterError
#| async fn write_string(Self, String) -> Unit raise WriterError = _
#| async fn write_string_interpolation(Self, &Show) -> Unit raise WriterError = _
#|}
#|impl TextWriter with fn write_string(self, str) {
#| self.write(str)
#|}
#|impl TextWriter with fn write_string_interpolation(self, obj) {
#| self.write(obj.to_string())
#|}
#|struct Utf8Writer {
#| mut inner : &BinaryWriter?
#|}
#|pub fn[W : BinaryWriter] Utf8Writer::Utf8Writer(inner : W) -> Utf8Writer {
#| { inner: Some(inner), }
#|}
#|#doc(hidden)
#|pub fn Utf8Writer::new_uninitialized() -> Utf8Writer {
#| { inner: None, }
#|}
#|#doc(hidden)
#|pub fn[R : BinaryWriter] Utf8Writer::set_inner(
#| self : Utf8Writer,
#| inner : R,
#|) -> Unit {
#| self.inner = Some(inner)
#|}
#|#doc(hidden)
#|pub fn Utf8Writer::close(self : Utf8Writer) -> Unit {
#| self.inner = None
#|}
#|pub impl TextWriter for Utf8Writer with fn write(self, data) {
#| guard! self.inner is Some(inner)
#| inner.write(@utf8.encode(data))
#|}
#|pub impl TextWriter for Utf8Writer with fn write_json(self, data) {
#| self.write(data.stringify())
#|}
#|pub extend Utf8Writer with TextWriter::{
#| write,
#| write_json,
#| write_string,
#| write_string_interpolation,
#|}
),
},
)
///|
let moonbitlang_core_async_js_async_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/js_async",
deps={},
files={
"js_async.mbt": (
#|#external
#|pub type Promise[X]
#|#external
#|priv type JsValue
#|fn[X] JsValue::make(v : X) -> JsValue = "%identity"
#|fn[X] JsValue::cast(v : JsValue) -> X = "%identity"
#|extern "js" fn JsValue::abort_error() -> JsValue =
#| #| () => {
#| #| const err = new Error()
#| #| err.name = 'AbortError'
#| #| return err
#| #| }
#|extern "js" fn JsValue::to_string(v : JsValue) -> String =
#| #| (v) => {
#| #| // A promise may reject with any value, including `null`, `undefined`
#| #| // and objects whose conversion throws, so never let printing throw.
#| #| try { return String(v) } catch (_) {}
#| #| try { return Object.prototype.toString.call(v) } catch (_) {}
#| #| return '[unprintable JavaScript value]'
#| #| }
#|suberror JsError {
#| JsError(JsValue)
#|}
#|pub impl Show for JsError with fn output(self, logger) {
#| let JsError(value) = self
#| logger <+ "\{value.to_string()}"
#|}
#|pub impl @debug.Debug for JsError with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "@js_async.JsError",
#| @debug.Repr::string(self.to_string()),
#| )
#|}
#|pub extend JsError with Show::{to_string}
#|#external
#|pub type AbortController
#|#external
#|pub type AbortSignal
#|#alias(new, deprecated="Use `AbortController()` instead")
#|pub extern "js" fn AbortController::AbortController() -> AbortController =
#| #| () => new AbortController()
#|pub extern "js" fn AbortController::signal(self : Self) -> AbortSignal =
#| #| (controller) => controller.signal
#|pub extern "js" fn AbortController::abort(self : Self) =
#| #| (controller) => controller.abort()
#|extern "js" fn AbortSignal::on_abort(
#| signal : AbortSignal,
#| f : () -> Unit,
#|) -> Unit =
#| #| (signal, f) => signal.addEventListener('abort', f, { once: true })
#|extern "js" fn AbortSignal::remove_abort_listener(
#| signal : AbortSignal,
#| f : () -> Unit,
#|) -> Unit =
#| #| (signal, f) => signal.removeEventListener('abort', f)
#|extern "js" fn AbortSignal::aborted(signal : AbortSignal) -> Bool =
#| #| (signal) => signal.aborted
#|extern "js" fn JsValue::then(
#| promise : JsValue,
#| resolve : (JsValue) -> Unit,
#| reject : (JsValue) -> Unit,
#|) =
#| #| (p, resolve, reject) => p.then(resolve, reject)
#|extern "js" fn JsValue::new_promise(
#| f : ((JsValue) -> Unit, (JsValue) -> Unit) -> Unit,
#|) -> JsValue =
#| #| (f) => new Promise(f)
#|priv struct PromiseWaiter[X] {
#| mut coro : @coroutine.Coroutine?
#| mut ret : X?
#| mut err : Error?
#|}
#|pub async fn[X] Promise::wait(
#| promise : Promise[X],
#| abort_controller? : AbortController,
#|) -> X {
#| let waiter : PromiseWaiter[X] = {
#| coro: Some(@coroutine.current_coroutine()),
#| ret: None,
#| err: None,
#| }
#| fn resolve(value : JsValue) {
#| waiter.ret = Some(value.cast())
#| if waiter.coro is Some(coro) {
#| coro.wake()
#| @event_loop.reschedule()
#| }
#| }
#| fn reject(err) {
#| waiter.err = Some(JsError(err))
#| if waiter.coro is Some(coro) {
#| coro.wake()
#| @event_loop.reschedule()
#| }
#| }
#| JsValue::make(promise).then(resolve, reject)
#| defer {
#| waiter.coro = None
#| }
#| if abort_controller is Some(controller) {
#| errdefer controller.abort()
#| @coroutine.suspend()
#| } else {
#| @coroutine.protect_from_cancel(() => @coroutine.suspend())
#| }
#| if waiter.err is Some(err) {
#| raise err
#| } else {
#| waiter.ret.unwrap()
#| }
#|}
#|pub async fn[X] run_promise(f : (AbortSignal) -> Promise[X]) -> X {
#| let controller = AbortController()
#| let promise = f(controller.signal())
#| promise.wait(abort_controller=controller)
#|}
#|pub fn[X] Promise::from_async(
#| f : async () -> X,
#| abort_signal? : AbortSignal,
#|) -> Promise[X] {
#| let promise = JsValue::new_promise((resolve, reject) => {
#| let _ = @coroutine.spawn(() => {
#| let coro = @coroutine.current_coroutine()
#| fn abort_handler() {
#| coro.cancel()
#| }
#| if abort_signal is Some(signal) {
#| signal.on_abort(abort_handler)
#| }
#| defer (if abort_signal is Some(signal) {
#| signal.remove_abort_listener(abort_handler)
#| })
#| if abort_signal is Some(signal) {
#| if signal.aborted() {
#| coro.cancel()
#| }
#| }
#| try @coroutine.handle_cancellation(f) catch {
#| err => reject(JsValue::make(err.to_string()))
#| } noraise {
#| None => reject(JsValue::abort_error())
#| Some(ret) => resolve(JsValue::make(ret))
#| }
#| })
#| })
#| @event_loop.reschedule()
#| promise.cast()
#|}
),
"readable_stream.mbt": (
#|#external
#|priv type StreamReader
#|priv struct ReadableStreamChunk {
#| value : Bytes
#| done : Bool
#|}
#|extern "js" fn StreamReader::read_ffi(
#| reader : Self,
#|) -> Promise[ReadableStreamChunk] =
#| #| (reader) => reader.read()
#|async fn StreamReader::read(reader : StreamReader) -> Bytes? {
#| match reader.read_ffi().wait() {
#| { done: true, .. } => None
#| { done: false, value, } => Some(value)
#| }
#|}
#|extern "js" fn StreamReader::release_lock(reader : Self) -> Unit =
#| #| (stream) => stream.releaseLock()
#|#external
#|priv type ReadableStreamController
#|extern "js" fn ReadableStreamController::close(controller : Self) =
#| #| (controller) => controller.close()
#|extern "js" fn ReadableStreamController::enqueue(
#| controller : Self,
#| chunk : Bytes,
#|) =
#| #| (controller, chunk) => controller.enqueue(chunk)
#|#external
#|type JsReadableStream
#|extern "js" fn JsReadableStream::new(
#| pull : (ReadableStreamController) -> Promise[Unit],
#| cancel : (JsValue) -> Unit,
#|) -> JsReadableStream =
#| #| (f, cancel) => new ReadableStream({ start: f, pull: f, cancel: cancel })
#|extern "js" fn JsReadableStream::cancel(stream : Self) -> Unit =
#| #| (stream) => {
#| #| stream.cancel().catch(() => {})
#| #| }
#|extern "js" fn JsReadableStream::get_reader(stream : Self) -> StreamReader =
#| #| (stream) => stream.getReader()
#|struct ReadableStream {
#| read_end : @io.MemoryPipeRead
#| worker : @coroutine.Coroutine
#| worker_error : @ref.Ref[Error?]
#| stream : JsReadableStream
#| reader : StreamReader
#|}
#|pub fn ReadableStream::from_js(stream : JsReadableStream) -> ReadableStream {
#| let (r, w) = @io.memory_pipe()
#| let reader = stream.get_reader()
#| let worker_error : @ref.Ref[Error?] = Ref(None)
#| let worker = @coroutine.spawn(() => {
#| defer w.close()
#| try {
#| while reader.read() is Some(chunk) {
#| w.write(chunk)
#| }
#| } catch {
#| err => worker_error.val = Some(err)
#| }
#| })
#| { read_end: r, worker, worker_error, stream, reader, }
#|}
#|pub fn ReadableStream::close(self : ReadableStream) -> Unit {
#| self.worker.cancel()
#| self.read_end.close()
#| self.reader.release_lock()
#| self.stream.cancel()
#|}
#|pub impl @io.BinaryReader for ReadableStream
#|impl @io.BinaryReaderImpl for ReadableStream with fn name(_self) {
#| "@js.ReadableStream"
#|}
#|impl @io.BinaryReaderImpl for ReadableStream with fn get_controller(self) {
#| @io.BinaryReaderImpl::get_controller(self.read_end as &@io.BinaryReader)
#|}
#|impl @io.BinaryReaderImpl for ReadableStream with fn raw_read_impl(
#| self,
#| buf,
#| offset~,
#| max_len~,
#|) {
#| let n = @io.BinaryReaderImpl::raw_read_impl(
#| self.read_end as &@io.BinaryReader,
#| buf,
#| offset~,
#| max_len~,
#| )
#| if n == 0 && self.worker_error.val is Some(err) {
#| raise err
#| }
#| n
#|}
#|pub extend ReadableStream with @io.BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
#|pub fn JsReadableStream::new_pipe() -> (JsReadableStream, @io.MemoryPipeWrite) {
#| let abort_controller = AbortController()
#| let abort_signal = abort_controller.signal()
#| let (pipe_r, pipe_w) = @io.memory_pipe()
#| fn pull(controller : ReadableStreamController) {
#| Promise::from_async(abort_signal~) <| () => {
#| guard pipe_r.read() is Some(chunk) else { controller.close() }
#| controller.enqueue(chunk)
#| }
#| }
#| fn cancel(_reason) {
#| pipe_r.close()
#| abort_controller.abort()
#| @coroutine.reschedule()
#| }
#| let stream = JsReadableStream::new(pull, cancel)
#| (stream, pipe_w)
#|}
),
},
)
///|
let moonbitlang_core_async_os_error_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/os_error",
deps={},
files={
"error.mbt": (
#|#derive(@json.ToJson)
#|pub suberror OsError {
#| OsError(Kind, raw_code~ : Int32, context~ : String)
#|}
#|#as_free_fn
#|pub fn OsError::from_errno(errno : Int32, context~ : String) -> OsError {
#| OsError(Kind(errno), raw_code=errno, context~)
#|}
#|pub fn OsError::wrap_context(err : OsError, f : (String) -> String) -> OsError {
#| let OsError(kind, raw_code~, context~) = err
#| OsError(kind, raw_code~, context=f(context))
#|}
#|pub impl Show for OsError with fn output(self, logger) -> Unit {
#| let OsError(_, raw_code~, context~) = self
#| logger <+ "\{context}: \{@errno.errno_to_string(raw_code)}"
#|}
#|pub impl @debug.Debug for OsError with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "@os_error.OsError",
#| @debug.Repr::string(self.to_string()),
#| )
#|}
#|pub extend OsError with Show::{to_string}
#|#cfg(any(target="native", target="wasm"))
#|pub fn check_errno(context : String) -> Unit raise OsError {
#| let err_code = @errno.get_errno()
#| if err_code != 0 {
#| raise OsError(Kind(err_code), raw_code=err_code, context~)
#| }
#|}
),
"error_kind.mbt": (
#|#derive(@debug.Debug, @json.ToJson, Eq)
#|#non_exhaustive
#|pub(all) enum Kind {
#| NotFound
#| AlreadyExists
#| AccessDenied
#| NotDirectory
#| ConnectionRefused
#| OperationNotSupported
#| NotEmpty
#| Unknown
#|}
#|#cfg(any(target="native", target="wasm"))
#|let errno_table : Map[Int32, Kind] = match @env_util.platform {
#| Linux =>
#| Map([
#| (@errno.linux_ENOENT, NotFound),
#| (@errno.linux_EEXIST, AlreadyExists),
#| (@errno.linux_EACCES, AccessDenied),
#| (@errno.linux_ENOTDIR, NotDirectory),
#| (@errno.linux_ECONNREFUSED, ConnectionRefused),
#| (@errno.linux_ENOTSUP, OperationNotSupported),
#| (@errno.linux_ENOTEMPTY, NotEmpty),
#| ])
#| MacOS =>
#| Map([
#| (@errno.macos_ENOENT, NotFound),
#| (@errno.macos_EEXIST, AlreadyExists),
#| (@errno.macos_EACCES, AccessDenied),
#| (@errno.macos_ENOTDIR, NotDirectory),
#| (@errno.macos_ECONNREFUSED, ConnectionRefused),
#| (@errno.macos_ENOTSUP, OperationNotSupported),
#| (@errno.macos_ENOTEMPTY, NotEmpty),
#| ])
#| Windows =>
#| Map([
#| (@errno.windows_ERROR_FILE_NOT_FOUND, NotFound),
#| (@errno.windows_ERROR_PATH_NOT_FOUND, NotFound),
#| (@errno.windows_ERROR_FILE_EXISTS, AlreadyExists),
#| (@errno.windows_ERROR_ALREADY_EXISTS, AlreadyExists),
#| (@errno.windows_ERROR_ACCESS_DENIED, AccessDenied),
#| (@errno.windows_ERROR_DIRECTORY, NotDirectory),
#| (@errno.windows_ERROR_CONNECTION_REFUSED, ConnectionRefused),
#| (@errno.windows_ERROR_NOT_SUPPORTED, OperationNotSupported),
#| (@errno.windows_ERROR_DIR_NOT_EMPTY, NotEmpty),
#| (@errno.windows_ERROR_NOT_EMPTY, NotEmpty),
#| ])
#| @env_util.Platform::.. => panic()
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn Kind::Kind(code : Int32) -> Kind {
#| match errno_table.get(code) {
#| None => Unknown
#| Some(kind) => kind
#| }
#|}
#|#cfg(target="js")
#|fn Kind::Kind(code : Int32) -> Kind {
#| kind_from_node_name(@errno.errno_to_name(code))
#|}
#|#cfg(target="js")
#|fn kind_from_node_name(name : String) -> Kind {
#| match name {
#| "ENOENT" => NotFound
#| "EEXIST" => AlreadyExists
#| "EACCES" => AccessDenied
#| "ENOTDIR" => NotDirectory
#| "ECONNREFUSED" => ConnectionRefused
#| "ENOTSUP" | "EOPNOTSUPP" => OperationNotSupported
#| "ENOTEMPTY" => NotEmpty
#| _ => Unknown
#| }
#|}
),
"extends.mbt": (
#|pub extend Kind with Eq::{equal}
),
},
)
///|
let moonbitlang_core_async_raw_fd_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/raw_fd",
deps={},
files={
"raw_fd.mbt": (
#|#valtype
#|struct RawFd {
#| io : @event_loop.IoHandle
#|}
#|#cfg(not(platform="windows"))
#|#alias(new, deprecated)
#|pub async fn RawFd::RawFd(fd : @fd_util.Fd) -> RawFd {
#| let context = "@raw_fd.RawFd::new()"
#| let kind = @event_loop.kind_of_fd(fd, context~)
#| let is_async = @fd_util.fd_is_nonblocking(fd, context~)
#| { io: @event_loop.IoHandle::from_fd(fd, kind~, is_async~), }
#|}
#|#cfg(platform="windows")
#|#alias(new, deprecated)
#|pub async fn RawFd::RawFd(fd : @fd_util.Fd) -> RawFd {
#| let context = "@raw_fd.RawFd::new()"
#| let kind = @event_loop.kind_of_fd(fd, context~)
#| { io: @event_loop.IoHandle::from_fd(fd, kind~, is_async=false), }
#|}
#|pub fn RawFd::fd(self : RawFd) -> @fd_util.Fd {
#| self.io.fd()
#|}
#|pub fn RawFd::close(self : RawFd) -> Unit {
#| self.io.close()
#|}
#|fn MutArrayView::buf(self : MutArrayView[Byte]) -> FixedArray[Byte] = "%arrayview.buf"
#|pub async fn RawFd::read_into_once(
#| self : RawFd,
#| dst : MutArrayView[Byte],
#|) -> Int32 {
#| self.io.read(
#| dst.buf(),
#| offset=dst.start_offset(),
#| len=dst.length(),
#| context="@raw_fd.RawFd::read_into_once()",
#| )
#|}
#|pub async fn RawFd::write_once(self : RawFd, data : BytesView) -> Int32 {
#| let buf = data.data()
#| let offset = data.start_offset()
#| let len = data.length()
#| self.io.write(buf, offset~, len~, context="@raw_fd.RawFd::write_once()")
#|}
#|struct RawFdStream {
#| io : @event_loop.IoHandle
#| reader_ctrl : @io.BinaryReaderController
#| writer_ctrl : @io.BinaryWriterController
#|}
#|#cfg(not(platform="windows"))
#|pub async fn RawFdStream::RawFdStream(fd : @fd_util.Fd) -> RawFdStream {
#| let context = "@raw_fd.RawFdStream()"
#| let kind = @event_loop.kind_of_fd(fd, context~)
#| let is_async = @fd_util.fd_is_nonblocking(fd, context~)
#| {
#| io: @event_loop.IoHandle::from_fd(fd, kind~, is_async~),
#| reader_ctrl: BinaryReaderController(),
#| writer_ctrl: BinaryWriterController(),
#| }
#|}
#|#cfg(platform="windows")
#|pub async fn RawFdStream::RawFdStream(fd : @fd_util.Fd) -> RawFdStream {
#| let context = "@raw_fd.RawFdStream()"
#| let kind = @event_loop.kind_of_fd(fd, context~)
#| {
#| io: @event_loop.IoHandle::from_fd(fd, kind~, is_async=false),
#| reader_ctrl: BinaryReaderController(),
#| writer_ctrl: BinaryWriterController(),
#| }
#|}
#|pub fn RawFdStream::fd(self : RawFdStream) -> @fd_util.Fd {
#| self.io.fd()
#|}
#|pub fn RawFdStream::close(self : RawFdStream) -> Unit {
#| self.io.close()
#|}
#|pub impl @io.BinaryReader for RawFdStream
#|impl @io.BinaryReaderImpl for RawFdStream with fn name(_self) {
#| "@raw_fd.RawFdStream"
#|}
#|impl @io.BinaryReaderImpl for RawFdStream with fn get_controller(self) {
#| self.reader_ctrl
#|}
#|impl @io.BinaryReaderImpl for RawFdStream with fn raw_read_impl(
#| self,
#| buf,
#| offset~,
#| max_len~,
#|) {
#| self.io.read(
#| buf,
#| offset~,
#| len=max_len,
#| context="impl @io.BinaryReader for @raw_fd.RawFdStream",
#| )
#|}
#|pub extend RawFdStream with @io.BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
#|pub impl @io.BinaryWriter for RawFdStream
#|impl @io.BinaryWriterImpl for RawFdStream with fn raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len~,
#|) {
#| self.io.write(
#| buf,
#| offset~,
#| len~,
#| context="impl @io.BinaryWriter for @raw_fd.RawFdStream",
#| )
#|}
#|impl @io.BinaryWriterImpl for RawFdStream with fn name(_self) {
#| "@raw_fd.RawFdStream"
#|}
#|impl @io.BinaryWriterImpl for RawFdStream with fn get_controller(self) {
#| self.writer_ctrl
#|}
#|impl @io.BinaryWriterImpl for RawFdStream with fn flush_partial_data(self) {
#| ignore(self)
#|}
#|pub extend RawFdStream with @io.BinaryWriter::{write, write_reader}
),
},
)
///|
let moonbitlang_core_async_raw_fd_internal_raw_fd_test_util_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/raw_fd/internal/raw_fd_test_util",
deps={},
files={
"raw_fd_test_util.mbt": (
#|pub extern "C" fn make_udp_socket() -> @fd_util.Fd = "raw_fd_test_make_udp_socket"
#|#borrow(sock)
#|extern "C" fn connect_ffi(
#| sock : @fd_util.Fd,
#| ip : UInt32,
#| port : Int32,
#|) -> Int32 = "raw_fd_test_connect"
#|pub fn connect(sock : @fd_util.Fd, addr : @socket.Addr) -> Int32 {
#| guard! addr.ip is Ipv4(ip)
#| connect_ffi(sock, ip, addr.port.to_int())
#|}
),
},
)
///|
let moonbitlang_core_async_runtime_internal_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/runtime_internal",
deps={},
files={
"runtime_internal.mbt": (
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#warnings("-unused_constructor")
#|struct Scheduler(@coroutine.Scheduler)
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|pub fn get_current_scheduler() -> Scheduler {
#| @coroutine.scheduler
#|}
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#warnings("-unused_constructor")
#|#cfg(any(target="native", target="wasm"))
#|struct CurrentEventLoop(@ref.Ref[@event_loop.EventLoop?])
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub fn get_current_event_loop() -> CurrentEventLoop {
#| @event_loop.get_current_event_loop()
#|}
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub using @event_loop {
#| with_event_loop,
#| register_hook,
#| set_deadlock_handler,
#| timer_id,
#|}
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#warnings("-unused_constructor")
#|#cfg(any(target="native", target="wasm"))
#|struct StdioChannel(Result[@event_loop.IoHandle, Error])
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub let stdin : StdioChannel = @event_loop.stdin
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub let stdout : StdioChannel = @event_loop.stdout
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub let stdout_lock : @async.Mutex = Mutex()
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub let stderr : StdioChannel = @event_loop.stderr
#|#doc(hidden)
#|#internal(internal, "This API is for compatibility with `moonbitlang/async`, do not use")
#|#cfg(any(target="native", target="wasm"))
#|pub let stderr_lock : @async.Mutex = Mutex()
),
},
)
///|
let moonbitlang_core_async_semaphore_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/semaphore",
deps={},
files={
"semaphore.mbt": (
#|priv struct Waiter {
#| coro : @coroutine.Coroutine
#| mut index : Int32
#| mut prev : Int32
#| mut next : Int32
#|}
#|impl @cancellable_queue.Element for Waiter with fn get_index(self) {
#| self.index
#|}
#|impl @cancellable_queue.Element for Waiter with fn set_index(self, value) {
#| self.index = value
#|}
#|impl @cancellable_queue.Element for Waiter with fn get_next(self) {
#| self.next
#|}
#|impl @cancellable_queue.Element for Waiter with fn set_next(self, value) {
#| self.next = value
#|}
#|impl @cancellable_queue.Element for Waiter with fn get_prev(self) {
#| self.prev
#|}
#|impl @cancellable_queue.Element for Waiter with fn set_prev(self, value) {
#| self.prev = value
#|}
#|extend Waiter with @cancellable_queue.Element::{is_queued}
#|struct Semaphore {
#| size : Int32
#| mut value : Int32
#| waiters : @cancellable_queue.Queue[Waiter]
#|}
#|#alias(new, deprecated)
#|pub fn Semaphore::Semaphore(
#| size : Int32,
#| initial_value~ : Int32 = size,
#|) -> Semaphore {
#| if size <= 0 {
#| abort("size of semaphore must be positive")
#| }
#| guard! initial_value >= 0 && initial_value <= size
#| { value: initial_value, size, waiters: Queue(), }
#|}
#|pub fn Semaphore::release(self : Semaphore) -> Unit {
#| if self.value >= self.size {
#| abort("semaphore: too many release")
#| }
#| if self.waiters.pop() is Some(waiter) {
#| waiter.coro.wake()
#| } else {
#| self.value += 1
#| }
#|}
#|pub async fn Semaphore::acquire(self : Semaphore) -> Unit noraise {
#| if self.value > 0 {
#| self.value -= 1
#| } else {
#| let waiter = {
#| coro: @coroutine.current_coroutine(),
#| index: -1,
#| next: -1,
#| prev: -1,
#| }
#| self.waiters.push(waiter)
#| match @coroutine.suspend_check_cancel() {
#| Continue => ()
#| Cancelled if !waiter.is_queued() =>
#| ()
#| Cancelled => {
#| self.waiters.remove(waiter)
#| @coroutine.raise_cancellation_signal()
#| }
#| }
#| }
#|}
#|pub fn Semaphore::try_acquire(self : Semaphore) -> Bool {
#| if self.value > 0 {
#| self.value -= 1
#| true
#| } else {
#| false
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_shell_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/shell",
deps={},
files={
"cmd.mbt": (
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|#derive(@debug.Debug)
#|pub(all) enum Stdin {
#| Text(String)
#| Binary(Bytes)
#| FromFile(String)
#| Inherit
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|#derive(@debug.Debug)
#|pub(all) enum Redirect {
#| Capture
#| Inherit
#| ToFile(String)
#| AppendToFile(String)
#| Discard
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|#derive(@debug.Debug)
#|struct Cmd {
#| program : String
#| arguments : Array[String]
#| cwd : String?
#| env : Map[String, String]
#| inherit_env : Bool
#| stdin : Stdin?
#| stdout : Redirect
#| stderr : Redirect
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Cmd::Cmd(
#| program : String,
#| arguments : ArrayView[String],
#| cwd? : String,
#| env~ : Map[String, String] = {},
#| inherit_env~ : Bool = true,
#| stdin? : Stdin,
#| stdout~ : Redirect = Capture,
#| stderr~ : Redirect = Capture,
#|) -> Cmd {
#| {
#| program,
#| arguments: arguments.to_owned(),
#| cwd,
#| env: env.copy(),
#| inherit_env,
#| stdin,
#| stdout,
#| stderr,
#| }
#|}
),
"execute.mbt": (
#|let default_max_output_bytes : Int32 = 8 * 1024 * 1024
#|priv enum Sink {
#| Buffer(Int32)
#| Lines(async (String) -> Unit, Int32)
#| Inherited
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Cmd::output(
#| self : Cmd,
#| timeout_ms? : Int32,
#| max_output_bytes~ : Int32 = default_max_output_bytes,
#|) -> Output {
#| validate_output_limit(max_output_bytes)
#| Pipeline::{ commands: [self], }.execute(Buffer(max_output_bytes), timeout_ms)
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Cmd::status(self : Cmd, timeout_ms? : Int32) -> Int32 {
#| Pipeline::{ commands: [self], }.execute(Inherited, timeout_ms).exit_code
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Cmd::run(self : Cmd, timeout_ms? : Int32) -> Unit {
#| Pipeline::{ commands: [self], }.execute(Inherited, timeout_ms).check()
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Cmd::each_line(
#| self : Cmd,
#| on_line : async (String) -> Unit,
#| timeout_ms? : Int32,
#| max_line_bytes~ : Int32 = default_max_output_bytes,
#|) -> Int32 {
#| validate_output_limit(max_line_bytes)
#| Pipeline::{ commands: [self], }.execute(
#| Lines(on_line, max_line_bytes),
#| timeout_ms,
#| ).exit_code
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Pipeline::output(
#| self : Pipeline,
#| timeout_ms? : Int32,
#| max_output_bytes~ : Int32 = default_max_output_bytes,
#|) -> Output {
#| validate_output_limit(max_output_bytes)
#| self.execute(Buffer(max_output_bytes), timeout_ms)
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Pipeline::status(self : Pipeline, timeout_ms? : Int32) -> Int32 {
#| self.execute(Inherited, timeout_ms).exit_code
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Pipeline::run(self : Pipeline, timeout_ms? : Int32) -> Unit {
#| self.execute(Inherited, timeout_ms).check()
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn Pipeline::each_line(
#| self : Pipeline,
#| on_line : async (String) -> Unit,
#| timeout_ms? : Int32,
#| max_line_bytes~ : Int32 = default_max_output_bytes,
#|) -> Int32 {
#| validate_output_limit(max_line_bytes)
#| self.execute(Lines(on_line, max_line_bytes), timeout_ms).exit_code
#|}
#|async fn Pipeline::execute(
#| self : Pipeline,
#| sink : Sink,
#| timeout_ms : Int32?,
#|) -> Output {
#| let count = self.commands.length()
#| if count == 0 {
#| raise ShellError::EmptyPipeline
#| }
#| for index, cmd in self.commands {
#| validate_cmd(cmd)
#| if index > 0 && cmd.stdin is Some(_) {
#| raise StdinOnNonFirstStage(index)
#| }
#| if index + 1 < count && !(cmd.stdout is Capture) {
#| raise RedirectOnNonFinalStage(index)
#| }
#| }
#| if sink is Lines(_, _) && !(self.commands[count - 1].stdout is Capture) {
#| raise StdoutNotCaptured
#| }
#| match timeout_ms {
#| None => run_pipeline(self, sink)
#| Some(milliseconds) =>
#| @async.with_timeout(milliseconds, () => run_pipeline(self, sink))
#| }
#|}
#|fn validate_cmd(cmd : Cmd) -> Unit raise ShellError {
#| if cmd.program.is_empty() {
#| raise EmptyProgram
#| }
#| if cmd.program.contains("\u{0000}") {
#| raise NulByte("program")
#| }
#| for index, argument in cmd.arguments {
#| if argument.contains("\u{0000}") {
#| raise NulByte("argument[\{index}]")
#| }
#| }
#| if cmd.cwd is Some(directory) && directory.contains("\u{0000}") {
#| raise NulByte("working directory")
#| }
#| if cmd.stdin is Some(FromFile(path)) && path.contains("\u{0000}") {
#| raise NulByte("standard input path")
#| }
#| validate_redirect(cmd.stdout, "standard output path")
#| validate_redirect(cmd.stderr, "standard error path")
#| for name, value in cmd.env {
#| if name.contains("\u{0000}") {
#| raise NulByte("environment name")
#| }
#| if name.is_empty() || name.contains("=") {
#| raise InvalidEnvironmentName(name)
#| }
#| if value.contains("\u{0000}") {
#| raise NulByte("environment value for \{name}")
#| }
#| }
#|}
#|fn validate_redirect(
#| redirect : Redirect,
#| location : String,
#|) -> Unit raise ShellError {
#| if redirect is (ToFile(path) | AppendToFile(path)) &&
#| path.contains("\u{0000}") {
#| raise NulByte(location)
#| }
#|}
#|fn validate_output_limit(limit : Int32) -> Unit raise ShellError {
#| if limit <= 0 {
#| raise InvalidOutputLimit(limit)
#| }
#|}
#|fn resolve_redirect(
#| redirect : Redirect,
#| capture : @process.ProcessOutput?,
#|) -> @process.ProcessOutput? {
#| match redirect {
#| Capture => capture
#| Inherit => None
#| ToFile(path) => Some(ToFile(path))
#| AppendToFile(path) => Some(AppendFile(path))
#| Discard => Some(Discard)
#| }
#|}
#|async fn spawn_stage(
#| group : @async.TaskGroup,
#| cmd : Cmd,
#| stdin : @process.ProcessInput,
#| stdout : @process.ProcessOutput?,
#| stderr : @process.ProcessOutput?,
#|) -> @process.Process {
#| @process.spawn(
#| group,
#| cmd.program,
#| cmd.arguments,
#| extra_env=cmd.env,
#| inherit_env=cmd.inherit_env,
#| stdin~,
#| stdout?,
#| stderr?,
#| cwd?=cmd.cwd.map(directory => directory),
#| cancel_handler=@process.hard_cancel(),
#| )
#|}
#|async fn write_standard_input(
#| writer : @process.WriteToProcess,
#| input : Stdin?,
#|) -> Unit {
#| defer writer.close()
#| match input {
#| Some(Text(text)) => writer.text.write(text)
#| Some(Binary(bytes)) => writer.write(bytes)
#| Some(FromFile(_) | Inherit) | None => ()
#| }
#|}
#|fn pipefail_exit_code(exit_codes : ArrayView[Int32]) -> Int32 {
#| let mut result = 0
#| for code in exit_codes {
#| if code != 0 {
#| result = code
#| }
#| }
#| result
#|}
#|async fn read_bounded(
#| reader : @process.ReadFromProcess,
#| captured : @ref.Ref[Int32],
#| limit : Int32,
#| stream : String,
#|) -> Bytes {
#| let buffer = @buffer.Buffer(capacity=4096)
#| for ;; {
#| match reader.read() {
#| None => break
#| Some(chunk) => {
#| let next_total = captured.val + chunk.length()
#| if next_total > limit {
#| raise OutputLimitExceeded(stream~, limit~)
#| }
#| captured.val = next_total
#| buffer.write_bytesview(chunk)
#| }
#| }
#| }
#| buffer.to_bytes()
#|}
#|async fn read_lines(
#| reader : @process.ReadFromProcess,
#| on_line : async (String) -> Unit,
#| limit : Int32,
#|) -> Unit {
#| let pending = @buffer.Buffer(capacity=4096)
#| while reader.read() is Some(chunk) {
#| let mut start = 0
#| for index in 0.. 0 {
#| on_line(take_line(pending, terminated=false, limit))
#| }
#|}
#|fn extend_line(
#| pending : @buffer.Buffer,
#| view : BytesView,
#| limit : Int32,
#|) -> Unit raise ShellError {
#| let projected = pending.length() + view.length()
#| if projected > limit && projected - limit > 1 {
#| raise OutputLimitExceeded(stream="stdout line", limit~)
#| }
#| pending.write_bytesview(view)
#|}
#|fn take_line(
#| pending : @buffer.Buffer,
#| terminated~ : Bool,
#| limit : Int32,
#|) -> String raise ShellError {
#| let raw = pending.to_bytes()
#| pending.reset()
#| let length = raw.length()
#| let bytes = if terminated && length > 0 && raw[length - 1] == b'\r' {
#| raw[:length - 1].to_owned()
#| } else {
#| raw
#| }
#| if bytes.length() > limit {
#| raise OutputLimitExceeded(stream="stdout line", limit~)
#| }
#| @utf8.decode_lossy(bytes)
#|}
#|async fn run_pipeline(pipeline : Pipeline, sink : Sink) -> Output {
#| let commands = pipeline.commands
#| let count = commands.length()
#| let collect_stderr = sink is Buffer(_)
#| let mut result = None
#| @async.with_task_group() <| group => {
#| let captured = @ref.Ref(0)
#| let limit = match sink {
#| Buffer(limit) => limit
#| _ => 0
#| }
#| let mut stdout_task : @async.Task[Bytes]? = None
#| let stderr_tasks : Array[@async.Task[Bytes]?] = []
#| let junction_inputs : Array[@process.SharedProcessInput] = []
#| let junction_outputs : Array[@process.SharedProcessOutput] = []
#| errdefer {
#| for input in junction_inputs {
#| input.close()
#| }
#| for output in junction_outputs {
#| output.close()
#| }
#| }
#| for _ in 1.. false
#| Some(Text(_) | Binary(_)) | None => true
#| }
#| let first_input : @process.ProcessInput = match first_stdin {
#| Some(FromFile(path)) => FromFile(path)
#| Some(Inherit) => Inherit
#| Some(Text(_) | Binary(_)) | None => FeedFromParent
#| }
#| let processes : Array[@process.Process] = []
#| for index, cmd in commands {
#| let stdin : @process.ProcessInput = if index == 0 {
#| first_input
#| } else {
#| Shared(junction_inputs[index - 1])
#| }
#| let captures_stdout = index + 1 == count &&
#| !(sink is Inherited) &&
#| cmd.stdout is Capture
#| let stdout : @process.ProcessOutput? = if index + 1 < count {
#| Some(Shared(junction_outputs[index]))
#| } else {
#| let final_capture : @process.ProcessOutput? = if captures_stdout {
#| Some(Collect)
#| } else {
#| None
#| }
#| resolve_redirect(cmd.stdout, final_capture)
#| }
#| let captures_stderr = collect_stderr && cmd.stderr is Capture
#| let stderr_capture : @process.ProcessOutput? = if captures_stderr {
#| Some(Collect)
#| } else {
#| None
#| }
#| let stderr = resolve_redirect(cmd.stderr, stderr_capture)
#| let child = spawn_stage(group, cmd, stdin, stdout, stderr)
#| if index > 0 {
#| junction_inputs[index - 1].close()
#| }
#| if index + 1 < count {
#| junction_outputs[index].close()
#| }
#| if index == 0 && writes_first_input {
#| group.spawn_bg(allow_failure=true, () => {
#| write_standard_input(child.stdin, first_stdin)
#| })
#| }
#| if captures_stdout {
#| match sink {
#| Buffer(_) =>
#| stdout_task = Some(
#| group.spawn(() => {
#| defer child.stdout.close()
#| read_bounded(child.stdout, captured, limit, "stdout")
#| }),
#| )
#| Lines(on_line, line_limit) =>
#| group.spawn_bg(() => {
#| defer child.stdout.close()
#| read_lines(child.stdout, on_line, line_limit)
#| })
#| Inherited => ()
#| }
#| }
#| if captures_stderr {
#| stderr_tasks.push(
#| Some(
#| group.spawn(() => {
#| defer child.stderr.close()
#| read_bounded(child.stderr, captured, limit, "stderr")
#| }),
#| ),
#| )
#| } else {
#| stderr_tasks.push(None)
#| }
#| processes.push(child)
#| }
#| let exit_codes = [ for child in processes => child.wait() ]
#| let stdout_bytes = match stdout_task {
#| Some(task) => task.wait()
#| None => b""
#| }
#| let stderr = StringBuilder()
#| for task in stderr_tasks {
#| let stage_bytes = match task {
#| Some(task) => task.wait()
#| None => b""
#| }
#| stderr.write_string(@utf8.decode_lossy(stage_bytes))
#| }
#| result = Some({
#| exit_code: pipefail_exit_code(exit_codes),
#| stdout: @utf8.decode_lossy(stdout_bytes),
#| stdout_bytes,
#| stderr: stderr.to_string(),
#| })
#| }
#| result.unwrap()
#|}
),
"glob.mbt": (
#|priv enum Item {
#| Literal(Char)
#| Any
#| Star
#| Class(Array[(Char, Char)], Bool)
#|}
#|priv enum Root {
#| Relative
#| Rooted(String)
#| Incomplete
#| DriveRelative(String)
#|}
#|let native_windows : Bool = @env_util.platform is Windows
#|fn parse_path(
#| text : String,
#| windows~ : Bool,
#| escapes~ : Bool,
#|) -> (Root, Array[String], Bool) {
#| let (root, rest) = if windows && starts_incomplete_root(text) {
#| (Incomplete, "")
#| } else if windows {
#| match windows_root(text) {
#| Some((root, rest)) =>
#| if root.has_suffix("\u{0000}") {
#| (DriveRelative(root[:root.length() - 1].to_owned()), rest)
#| } else {
#| (Rooted(root), rest)
#| }
#| None => (Relative, text)
#| }
#| } else {
#| (Relative, text)
#| }
#| let raw = split_path(rest, windows~, escapes~)
#| let root = match root {
#| Rooted(_) | DriveRelative(_) | Incomplete => root
#| Relative =>
#| if !text.is_empty() && raw is [first, ..] && first.is_empty() {
#| Rooted("/")
#| } else {
#| Relative
#| }
#| }
#| let only_separators = raw.all(segment => segment.is_empty())
#| let directories_only = !only_separators &&
#| raw is [_, .., last] &&
#| last.is_empty()
#| let segments = [ for segment in raw if !segment.is_empty() => segment ]
#| (root, segments, directories_only)
#|}
#|fn starts_incomplete_root(text : String) -> Bool {
#| let chars = collect_chars(text)
#| fn separator(index : Int32) -> Bool {
#| index < chars.length() && (chars[index] == '/' || chars[index] == '\\')
#| }
#| if !(separator(0) && separator(1)) {
#| return false
#| }
#| windows_root(text) is None
#|}
#|fn windows_root(text : String) -> (String, String)? {
#| let chars = collect_chars(text)
#| fn separator(index : Int32) -> Bool {
#| index < chars.length() && (chars[index] == '/' || chars[index] == '\\')
#| }
#| fn from(index : Int32) -> String {
#| let rest = StringBuilder()
#| for position in index..= 2 &&
#| chars[1] == ':' &&
#| (
#| (chars[0] >= 'A' && chars[0] <= 'Z') ||
#| (chars[0] >= 'a' && chars[0] <= 'z')
#| ) {
#| if separator(2) {
#| return Some(("\{chars[0]}:/", from(3)))
#| }
#| return Some(("\{chars[0]}:\u{0000}", from(2)))
#| }
#| if separator(0) {
#| return Some(("/", from(1)))
#| }
#| None
#|}
#|fn split_path(path : String, windows~ : Bool, escapes~ : Bool) -> Array[String] {
#| let chars = collect_chars(path)
#| let segments = []
#| let current = StringBuilder()
#| let mut index = 0
#| while index < chars.length() {
#| let char = chars[index]
#| if char == '\\' && windows {
#| segments.push(current.to_string())
#| current.reset()
#| index = index + 1
#| } else if char == '\\' && escapes && index + 1 < chars.length() {
#| if chars[index + 1] == '/' {
#| segments.push(current.to_string())
#| current.reset()
#| } else {
#| current.write_char('\\')
#| current.write_char(chars[index + 1])
#| }
#| index = index + 2
#| } else if char == '/' {
#| segments.push(current.to_string())
#| current.reset()
#| index = index + 1
#| } else {
#| current.write_char(char)
#| index = index + 1
#| }
#| }
#| segments.push(current.to_string())
#| segments
#|}
#|fn is_hidden(name : String) -> Bool {
#| name.has_prefix(".")
#|}
#|fn match_segment(pattern : String, name : String) -> Bool {
#| let items = parse_segment(pattern)
#| if is_hidden(name) && !(items is [Literal('.'), ..]) {
#| return false
#| }
#| match_items(items, collect_chars(name))
#|}
#|fn collect_chars(text : String) -> Array[Char] {
#| text.to_array()
#|}
#|fn parse_segment(pattern : String) -> Array[Item] {
#| let chars = collect_chars(pattern)
#| let items = []
#| let mut index = 0
#| while index < chars.length() {
#| match chars[index] {
#| '\\' =>
#| if index + 1 < chars.length() {
#| items.push(Literal(chars[index + 1]))
#| index = index + 2
#| } else {
#| items.push(Literal('\\'))
#| index = index + 1
#| }
#| '?' => {
#| items.push(Any)
#| index = index + 1
#| }
#| '*' => {
#| if !(items is [.., Star]) {
#| items.push(Star)
#| }
#| index = index + 1
#| }
#| '[' =>
#| match parse_class(chars, index + 1) {
#| Some((members, negated, next)) => {
#| items.push(Class(members, negated))
#| index = next
#| }
#| None => {
#| items.push(Literal('['))
#| index = index + 1
#| }
#| }
#| char => {
#| items.push(Literal(char))
#| index = index + 1
#| }
#| }
#| }
#| items
#|}
#|fn parse_class(
#| chars : Array[Char],
#| start : Int32,
#|) -> (Array[(Char, Char)], Bool, Int32)? {
#| let mut index = start
#| let negated = index < chars.length() &&
#| (chars[index] == '!' || chars[index] == '^')
#| if negated {
#| index = index + 1
#| }
#| let members = []
#| if index < chars.length() && chars[index] == ']' {
#| members.push((']', ']'))
#| index = index + 1
#| }
#| while index < chars.length() {
#| if chars[index] == ']' {
#| return Some((members, negated, index + 1))
#| }
#| let (low, after_low) = if chars[index] == '\\' && index + 1 < chars.length() {
#| (chars[index + 1], index + 2)
#| } else {
#| (chars[index], index + 1)
#| }
#| if after_low + 1 < chars.length() &&
#| chars[after_low] == '-' &&
#| chars[after_low + 1] != ']' {
#| let (high, after_high) = if chars[after_low + 1] == '\\' &&
#| after_low + 2 < chars.length() {
#| (chars[after_low + 2], after_low + 3)
#| } else {
#| (chars[after_low + 1], after_low + 2)
#| }
#| if low <= high {
#| members.push((low, high))
#| }
#| index = after_high
#| } else {
#| members.push((low, low))
#| index = after_low
#| }
#| }
#| None
#|}
#|fn match_items(items : Array[Item], name : Array[Char]) -> Bool {
#| let mut item_index = 0
#| let mut name_index = 0
#| let mut star = -1
#| let mut retry_at = 0
#| while name_index < name.length() {
#| if item_index < items.length() &&
#| item_matches(items[item_index], name[name_index]) {
#| item_index = item_index + 1
#| name_index = name_index + 1
#| } else if item_index < items.length() && items[item_index] is Star {
#| star = item_index
#| retry_at = name_index
#| item_index = item_index + 1
#| } else if star >= 0 {
#| retry_at = retry_at + 1
#| name_index = retry_at
#| item_index = star + 1
#| } else {
#| return false
#| }
#| }
#| while item_index < items.length() && items[item_index] is Star {
#| item_index = item_index + 1
#| }
#| item_index >= items.length()
#|}
#|fn item_matches(item : Item, candidate : Char) -> Bool {
#| match item {
#| Literal(char) => same_char(char, candidate)
#| Any => true
#| Star => false
#| Class(members, negated) => in_members(members, candidate) != negated
#| }
#|}
#|fn same_char(left : Char, right : Char) -> Bool {
#| left == right || (native_windows && flip_case(left) == right)
#|}
#|fn in_members(members : Array[(Char, Char)], candidate : Char) -> Bool {
#| if in_ranges(members, candidate) {
#| return true
#| }
#| if native_windows {
#| let flipped = flip_case(candidate)
#| if flipped != candidate && in_ranges(members, flipped) {
#| return true
#| }
#| }
#| false
#|}
#|fn in_ranges(members : Array[(Char, Char)], candidate : Char) -> Bool {
#| members.any(range => candidate >= range.0 && candidate <= range.1)
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub async fn glob(pattern : String, cwd~ : String = ".") -> Array[String] {
#| if pattern.contains("\u{0000}") {
#| raise ShellError::NulByte("glob pattern")
#| }
#| if cwd.contains("\u{0000}") {
#| raise ShellError::NulByte("glob working directory")
#| }
#| let (root, segments, directories_only) = parse_path(
#| pattern,
#| windows=native_windows,
#| escapes=!native_windows,
#| )
#| let (base, prefix, drive_relative) = match root {
#| Rooted(root) => (root, root, false)
#| DriveRelative(root) => (root, root, true)
#| Relative => (cwd, "", false)
#| Incomplete => return []
#| }
#| let found = []
#| let seen = Map([])
#| let visited = Map([])
#| expand(
#| base, prefix, segments, 0, directories_only, drive_relative, found, seen, visited,
#| )
#| checkpoint()
#| found.sort_by(compare_code_units)
#| found
#|}
#|fn compare_code_units(left : String, right : String) -> Int32 {
#| let shared = if left.length() < right.length() {
#| left.length()
#| } else {
#| right.length()
#| }
#| for index in 0.. Unit {
#| checkpoint()
#| let state = "\{index}\u{0000}\{base}"
#| if visited.contains(state) {
#| return
#| }
#| visited[state] = ()
#| if index >= segments.length() {
#| if !prefix.is_empty() &&
#| !seen.contains(prefix) &&
#| entry_exists(base) &&
#| (!directories_only || is_directory(base)) {
#| seen[prefix] = ()
#| found.push(if directories_only { prefix + "/" } else { prefix })
#| }
#| return
#| }
#| let segment = segments[index]
#| if segment == "**" {
#| expand(
#| base,
#| prefix,
#| segments,
#| index + 1,
#| directories_only,
#| attach_directly,
#| found,
#| seen,
#| visited,
#| )
#| for entry in read_directory(base) {
#| checkpoint()
#| if !is_hidden(entry) {
#| let child_base = attach(base, entry, attach_directly)
#| let child_prefix = attach(prefix, entry, attach_directly)
#| if is_real_directory(child_base) {
#| expand(
#| child_base, child_prefix, segments, index, directories_only, false, found,
#| seen, visited,
#| )
#| } else if index + 1 == segments.length() {
#| expand(
#| child_base,
#| child_prefix,
#| segments,
#| index + 1,
#| directories_only,
#| false,
#| found,
#| seen,
#| visited,
#| )
#| }
#| }
#| }
#| } else if is_literal_segment(segment) {
#| let literal = unescape(segment)
#| let child = attach(base, literal, attach_directly)
#| if index + 1 == segments.length() || is_directory(child) {
#| expand(
#| child,
#| attach(prefix, literal, attach_directly),
#| segments,
#| index + 1,
#| directories_only,
#| false,
#| found,
#| seen,
#| visited,
#| )
#| }
#| } else {
#| for entry in read_directory(base) {
#| checkpoint()
#| if match_segment(segment, entry) {
#| let child = attach(base, entry, attach_directly)
#| if index + 1 == segments.length() || is_directory(child) {
#| expand(
#| child,
#| attach(prefix, entry, attach_directly),
#| segments,
#| index + 1,
#| directories_only,
#| false,
#| found,
#| seen,
#| visited,
#| )
#| }
#| }
#| }
#| }
#|}
#|async fn checkpoint() -> Unit {
#| if @async.is_being_cancelled() {
#| @async.pause()
#| }
#|}
#|fn attach(prefix : String, entry : String, directly : Bool) -> String {
#| if directly {
#| prefix + entry
#| } else {
#| join(prefix, entry)
#| }
#|}
#|fn join(prefix : String, entry : String) -> String {
#| if prefix.is_empty() || prefix.has_suffix("/") {
#| prefix + entry
#| } else {
#| "\{prefix}/\{entry}"
#| }
#|}
#|fn is_literal_segment(segment : String) -> Bool {
#| parse_segment(segment).all(item => item is Literal(_))
#|}
#|fn unescape(segment : String) -> String {
#| let text = StringBuilder()
#| for item in parse_segment(segment) {
#| if item is Literal(char) {
#| text.write_char(char)
#| }
#| }
#| text.to_string()
#|}
#|async fn read_directory(path : String) -> Array[String] {
#| @fs.readdir(path, include_hidden=true) catch {
#| _ => []
#| }
#|}
#|async fn entry_exists(path : String) -> Bool {
#| try @fs.kind(path, follow_symlink=false) catch {
#| _ => false
#| } noraise {
#| _ => true
#| }
#|}
#|async fn is_real_directory(path : String) -> Bool {
#| (@fs.kind(path, follow_symlink=false) is Directory) catch {
#| _ => false
#| }
#|}
#|async fn is_directory(path : String) -> Bool {
#| (@fs.kind(path) is Directory) catch {
#| _ => false
#| }
#|}
#|fn flip_case(candidate : Char) -> Char {
#| if candidate >= 'a' && candidate <= 'z' {
#| (candidate.to_int() - 32).unsafe_to_char()
#| } else if candidate >= 'A' && candidate <= 'Z' {
#| (candidate.to_int() + 32).unsafe_to_char()
#| } else {
#| candidate
#| }
#|}
),
"output.mbt": (
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|#derive(@debug.Debug)
#|struct Output {
#| exit_code : Int32
#| stdout : String
#| stdout_bytes : Bytes
#| stderr : String
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Output::exit_code(self : Output) -> Int32 {
#| self.exit_code
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Output::stdout(self : Output) -> String {
#| self.stdout
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Output::stdout_bytes(self : Output) -> Bytes {
#| self.stdout_bytes
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Output::stderr(self : Output) -> String {
#| self.stderr
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Output::check(self : Output) -> Unit raise {
#| if self.exit_code != 0 {
#| raise CommandFailed(self)
#| }
#|}
#|#derive(@debug.Debug)
#|priv suberror ShellError {
#| EmptyProgram
#| EmptyPipeline
#| InvalidEnvironmentName(String)
#| InvalidOutputLimit(Int32)
#| OutputLimitExceeded(stream~ : String, limit~ : Int32)
#| NulByte(String)
#| StdinOnNonFirstStage(Int32)
#| RedirectOnNonFinalStage(Int32)
#| StdoutNotCaptured
#| CommandFailed(Output)
#|}
),
"pipeline.mbt": (
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|#derive(@debug.Debug)
#|struct Pipeline {
#| commands : Array[Cmd]
#|}
#|#deprecated("Use `@process.Cmd` instead", skip_current_package=true)
#|pub fn Pipeline::Pipeline(commands : ArrayView[Cmd]) -> Pipeline {
#| { commands: commands.to_owned(), }
#|}
),
},
)
///|
let moonbitlang_core_async_stdio_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/stdio",
deps={},
files={
"polyfill.mbt": (
#|pub struct Input {
#| text : @io.Utf8Reader
#| priv ctrl : @io.BinaryReaderController
#|}
#|pub fn Input::fd(_self : Input) -> @types.Fd raise {
#| raise Failure("`@stdio.stdin` is not supported on wasm-gc/js backend")
#|}
#|pub impl @io.BinaryReader for Input
#|impl @io.BinaryReaderImpl for Input with fn raw_read_impl(
#| _self,
#| _buf,
#| offset~,
#| max_len~,
#|) {
#| ignore(offset)
#| ignore(max_len)
#| raise Failure("`@stdio.stdin` is not supported on wasm-gc/js backend")
#|}
#|impl @io.BinaryReaderImpl for Input with fn name(_self) {
#| "@stdio.Input"
#|}
#|impl @io.BinaryReaderImpl for Input with fn get_controller(self) {
#| self.ctrl
#|}
#|pub extend Input with @io.BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
#|pub struct Output {
#| text : @io.Utf8Writer
#| priv ctrl : @io.BinaryWriterController
#|}
#|pub fn Output::fd(_self : Output) -> @types.Fd raise {
#| raise Failure(
#| "`@stdio.stdout` and `@stdio.stderr` are not supported on wasm-gc/js backend",
#| )
#|}
#|pub impl @io.BinaryWriter for Output
#|impl @io.BinaryWriterImpl for Output with fn raw_write_impl(
#| _self,
#| _buf,
#| offset~,
#| len~,
#|) {
#| ignore(offset)
#| ignore(len)
#| raise Failure(
#| "`@stdio.stdout` are `@stdio.stderr` are not supported on wasm-gc/js backend",
#| )
#|}
#|impl @io.BinaryWriterImpl for Output with fn name(_self) {
#| "@stdio.Output"
#|}
#|impl @io.BinaryWriterImpl for Output with fn get_controller(self) {
#| self.ctrl
#|}
#|impl @io.BinaryWriterImpl for Output with fn flush_partial_data(self) {
#| ignore(self)
#|}
#|pub extend Output with @io.BinaryWriter::{write, write_reader}
#|pub let stdin : Input = {
#| let dummy : Input = {
#| text: @io.Utf8Reader::new_uninitialized(),
#| ctrl: BinaryReaderController(),
#| }
#| { ..dummy, text: Utf8Reader(dummy), }
#|}
#|pub let stdout : Output = {
#| let dummy : Output = {
#| text: @io.Utf8Writer::new_uninitialized(),
#| ctrl: BinaryWriterController(),
#| }
#| { ..dummy, text: Utf8Writer(dummy), }
#|}
#|pub let stderr : Output = {
#| let dummy : Output = {
#| text: @io.Utf8Writer::new_uninitialized(),
#| ctrl: BinaryWriterController(),
#| }
#| { ..dummy, text: Utf8Writer(dummy), }
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_cat_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/cat",
deps={},
files={
"main.mbt": (
#|async fn main {
#| let mut out_channel = @stdio.stdout
#| let mut timeout = None
#| for args = @env.args()[1:] {
#| match args {
#| ["-stderr", .. rest] => {
#| out_channel = @stdio.stderr
#| continue rest
#| }
#| ["-stdout", .. rest] => {
#| out_channel = @stdio.stdout
#| continue rest
#| }
#| ["-timeout", .. rest] => {
#| guard rest is [t, .. rest] else { fail("invalid argument") }
#| timeout = Some(@string.parse_int(t))
#| continue rest
#| }
#| [] => break
#| _ => fail("invalid arguments")
#| }
#| }
#| async fn work() {
#| out_channel.write_reader(@stdio.stdin)
#| }
#| if timeout is Some(t) {
#| @async.with_timeout(t, work)
#| } else {
#| work()
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_concurrent_stdio_write_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/concurrent_stdio_write",
deps={},
files={
"main.mbt": (
#|async fn main {
#| @async.with_task_group <| group => {
#| group.spawn_bg() <| () => {
#| for _ in 0..<100 {
#| @stdio.stdout.write(b"a")
#| }
#| }
#| group.spawn_bg() <| () => {
#| for _ in 0..<100 {
#| @stdio.stdout.write(b"b")
#| }
#| }
#| group.spawn_bg() <| () => {
#| for _ in 0..<100 {
#| @stdio.stderr.write(b"a")
#| }
#| }
#| group.spawn_bg() <| () => {
#| for _ in 0..<100 {
#| @stdio.stderr.write(b"b")
#| }
#| }
#| }
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_dump_env_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/dump_env",
deps={},
files={
"main.mbt": (
#|extern "C" fn init_env() = "init_env"
#|#cfg(platform="windows")
#|extern "C" fn next_entry() -> String = "next_entry"
#|#cfg(platform="windows")
#|fn main {
#| init_env()
#| while next_entry() is entry && entry.length() > 0 {
#| println(entry)
#| }
#|}
#|#cfg(not(platform="windows"))
#|extern "C" fn next_entry() -> Bytes = "next_entry"
#|#cfg(not(platform="windows"))
#|fn main {
#| init_env()
#| while next_entry() is entry && entry.length() > 0 {
#| println(@utf8.decode_lossy(entry))
#| }
#|}
#|#cfg(platform="windows")
#|let _unused : Unit = ignore(@utf8.encode(""))
),
},
)
///|
let moonbitlang_core_async_test_programs_filter_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/filter",
deps={},
files={
"main.mbt": (
#|extern "C" fn exit(code : Int32) = "exit"
#|async fn main {
#| let mut upper = false
#| let mut keep = None
#| let mut head = None
#| let mut stderr_msg = None
#| let mut exit_code = 0
#| let mut print_cwd = false
#| for args = @env.args()[1:] {
#| match args {
#| [] => break
#| ["-upper", .. rest] => {
#| upper = true
#| continue rest
#| }
#| ["-keep", substr, .. rest] => {
#| keep = Some(substr)
#| continue rest
#| }
#| ["-head", n, .. rest] => {
#| head = Some(@string.parse_int(n))
#| continue rest
#| }
#| ["-stderr", msg, .. rest] => {
#| stderr_msg = Some(msg)
#| continue rest
#| }
#| ["-exit-code", n, .. rest] => {
#| exit_code = @string.parse_int(n)
#| continue rest
#| }
#| ["-print-cwd", .. rest] => {
#| print_cwd = true
#| continue rest
#| }
#| _ => fail("invalid arguments")
#| }
#| }
#| if print_cwd {
#| guard @env.current_dir() is Some(cwd) else { fail("no working directory") }
#| @stdio.stdout.text.write("\{cwd}\n")
#| } else {
#| let mut emitted = 0
#| while @stdio.stdin.text.read_until("\n") is Some(line) {
#| let line = line.trim_end(chars="\r").to_owned()
#| if keep is Some(substr) && !line.contains(substr) {
#| continue
#| }
#| let line = if upper { line.to_upper() } else { line }
#| @stdio.stdout.text.write("\{line}\n")
#| emitted += 1
#| if head is Some(limit) && emitted >= limit {
#| break
#| }
#| }
#| }
#| if stderr_msg is Some(msg) {
#| @stdio.stderr.text.write(msg)
#| }
#| exit(exit_code)
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_find_ipv6_test_interface_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/find_ipv6_test_interface",
deps={},
files={
"main.mbt": (
#|extern "C" fn find_ipv6_test_interface() -> Int32 = "find_ipv6_test_interface"
#|fn main {
#| println(find_ipv6_test_interface())
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_lock_file_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/lock_file",
deps={},
files={
"main.mbt": (
#|async fn main {
#| let mut duration = 500
#| let mut file = None
#| let mut kind = None
#| for args = @env.args()[1:] {
#| match args {
#| ["-duration", .. rest] => {
#| guard rest is [t, .. rest] else {
#| fail("expected duration value after `-duration`")
#| }
#| duration = @string.parse_int(t)
#| continue rest
#| }
#| ["-shared", .. rest] => {
#| kind = Some(@fs.Shared)
#| continue rest
#| }
#| ["-exclusive", .. rest] => {
#| kind = Some(Exclusive)
#| continue rest
#| }
#| [['-', ..] as opt, ..] => fail("unknown argument \{opt}")
#| [f, .. rest] => {
#| guard file is None else { fail("too many files specified") }
#| file = Some(f)
#| continue rest
#| }
#| [] => break
#| }
#| }
#| guard file is Some(file) else { fail("no file specified") }
#| guard kind is Some(kind) else { fail("no lock kind specified") }
#| let file = @fs.open(
#| file,
#| create_mode=OpenOrCreate,
#| mode=if kind is Shared { ReadOnly } else { ReadWrite },
#| )
#| defer file.close()
#| guard @stdio.stdin.read() is Some(_)
#| file.lock(kind)
#| defer {
#| file.unlock()
#| @stdio.stdout.text.write("released lock\n")
#| }
#| @stdio.stdout.text.write("acquired lock\n")
#| guard @stdio.stdin.read() is Some(_)
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_pty_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/pty",
deps={},
files={
"main.mbt": (
#|async fn main {
#| guard @env.args() is [_, path] else { fail("usage: pty ") }
#| let pty = spawn(path)
#| defer pty.close()
#| @stdio.stderr.write(b"ok\n")
#| @async.with_task_group(group => {
#| group.spawn_bg(no_wait=true, () => {
#| while pty.read_some() is Some(bytes) {
#| @stdio.stdout.write(bytes)
#| }
#| })
#| group.spawn_bg(no_wait=true, () => {
#| while @stdio.stdin.read_until(b"\n") is Some(line) {
#| run_command(pty, @utf8.decode(line))
#| }
#| pty.close()
#| })
#| let exit_code = pty.wait()
#| @stdio.stderr.write(@utf8.encode("exit \{exit_code}\n"))
#| })
#|}
#|async fn run_command(pty : Pty, command : String) -> Unit {
#| match command.split(" ").collect() {
#| ["input", bytes] => pty.write(@base64.decode(bytes))
#| ["resize", rows, cols] => {
#| pty.resize(rows=@string.parse_int(rows), cols=@string.parse_int(cols))
#| @stdio.stderr.write(b"ok\n")
#| }
#| _ => fail("unknown command: \{command}")
#| }
#|}
),
"pty.mbt": (
#|priv struct Pty {
#| input : @raw_fd.RawFdStream
#| output : @raw_fd.RawFdStream
#| handle : Handle
#| mut closed : Bool
#|}
#|async fn spawn(path : StringView) -> Pty {
#| let env = @env.get_env_vars()
#| .to_array()
#| .map(entry => @utf8.encode("\{entry.0}=\{entry.1}"))
#| let handle = Handle::spawn(
#| @utf8.encode(path),
#| FixedArray::from_array(env),
#| 24,
#| 80,
#| )
#| let error = handle.error()
#| if error != 0 {
#| handle.close()
#| raise @os_error.OsError::from_errno(error, context="spawn()")
#| }
#| let input = {
#| errdefer handle.close()
#| @raw_fd.RawFdStream(handle.take_input())
#| }
#| let output = {
#| errdefer {
#| input.close()
#| handle.close()
#| }
#| @raw_fd.RawFdStream(handle.take_output())
#| }
#| { input, output, handle, closed: false, }
#|}
#|async fn Pty::wait(self : Pty) -> Int32 {
#| @process.wait_pid(self.handle.pid())
#|}
#|fn Pty::resize(self : Pty, rows~ : Int32, cols~ : Int32) -> Unit raise {
#| let error = self.handle.resize(rows, cols)
#| if error != 0 {
#| raise @os_error.OsError::from_errno(error, context="Pty::resize()")
#| }
#|}
#|fn Pty::close(self : Pty) -> Unit {
#| guard !self.closed else { return }
#| self.closed = true
#| self.output.close()
#| self.input.close()
#| self.handle.close()
#|}
#|async fn Pty::read_some(self : Pty) -> Bytes? {
#| self.output.read()
#|}
#|async fn Pty::write(self : Pty, bytes : BytesView) -> Unit {
#| self.input.write(bytes)
#|}
#|#external
#|priv type Handle
#|#borrow(path, env)
#|extern "C" fn Handle::spawn(
#| path : Bytes,
#| env : FixedArray[Bytes],
#| rows : Int32,
#| cols : Int32,
#|) -> Handle = "moonbitlang_async_pty_spawn"
#|extern "C" fn Handle::error(self : Handle) -> Int32 = "moonbitlang_async_pty_error"
#|extern "C" fn Handle::pid(self : Handle) -> Int32 = "moonbitlang_async_pty_pid"
#|extern "C" fn Handle::take_input(self : Handle) -> @types.Fd = "moonbitlang_async_pty_take_input"
#|extern "C" fn Handle::take_output(self : Handle) -> @types.Fd = "moonbitlang_async_pty_take_output"
#|extern "C" fn Handle::resize(
#| self : Handle,
#| rows : Int32,
#| cols : Int32,
#|) -> Int32 = "moonbitlang_async_pty_resize"
#|extern "C" fn Handle::close(self : Handle) -> Unit = "moonbitlang_async_pty_close"
),
},
)
///|
let moonbitlang_core_async_test_programs_setup_sanitizer_check_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/setup_sanitizer_check",
deps={},
files={
"main.mbt": (
#|#cfg(platform="windows")
#|let sanitizer_flags = "/Z7 /fsanitize=address"
#|#cfg(not(platform="windows"))
#|let sanitizer_flags = "-fsanitize=address -g -fno-omit-frame-pointer"
#|let link_config : String =
#| $| link: {
#| $| "native": {
#| $| "cc-flags": "\{sanitizer_flags}",
#| $| "stub-cc-flags": "\{sanitizer_flags}",
#| $| },
#| $| },
#|async fn main {
#| fn exclude(path : String) {
#| match path {
#| "./target" | "./_build" | "./examples/target" | "./examples/_build" =>
#| true
#| "./test_programs/dump_env" =>
#| true
#| _ => false
#| }
#| }
#| @fs.walk(".", exclude~) <| ((path, files) => {
#| for file in files {
#| guard file is "moon.pkg" else { }
#| let full_path = "\{path}/\{file}"
#| let content = @fs.read_text(full_path).split("\n")
#| let transformed = StringBuilder()
#| let mut found = false
#| for line in content {
#| transformed <+ "\{line}\n"
#| if !found && line is "options(" {
#| found = true
#| transformed <+ "\{link_config}\n"
#| }
#| }
#| if !found {
#| transformed <+ "\noptions(\n\{link_config}\n)"
#| }
#| @fs.write_text(full_path, transformed.to_string())
#| }
#| })
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_sleep_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/sleep",
deps={},
files={
"main.mbt": (
#|extern "C" fn register_termination_handler() = "register_termination_handler"
#|extern "C" fn set_swallow_cancel_signal() = "set_swallow_cancel_signal"
#|extern "C" fn use_stderr_in_stub() = "set_use_stderr"
#|extern "C" fn exit(code : Int32) = "exit"
#|async fn main {
#| let mut time = 10_000
#| let mut exit_code = 0
#| let mut msg_after_sleep = None
#| let mut msg_finally = None
#| let mut msg_after_init = None
#| let mut use_default_handler = false
#| let mut wait_on_init = false
#| let mut use_stderr = false
#| for args = @env.args()[1:] {
#| match args {
#| [] => break
#| ["-exit-code", .. rest] => {
#| guard rest is [code, .. rest] else { fail("invalid arguments") }
#| exit_code = @string.parse_int(code)
#| continue rest
#| }
#| ["-swallow-cancel-signal", .. rest] => {
#| set_swallow_cancel_signal()
#| continue rest
#| }
#| ["-print-after-sleep", .. rest] => {
#| guard rest is [msg, .. rest] else { fail("invalid arguments") }
#| msg_after_sleep = Some(msg)
#| continue rest
#| }
#| ["-print-finally", .. rest] => {
#| guard rest is [msg, .. rest] else { fail("invalid arguments") }
#| msg_finally = Some(msg)
#| continue rest
#| }
#| ["-print-after-init", .. rest] => {
#| guard rest is [msg, .. rest] else { fail("invalid arguments") }
#| msg_after_init = Some(msg)
#| continue rest
#| }
#| ["-use-default-handler", .. rest] => {
#| use_default_handler = true
#| continue rest
#| }
#| ["-wait-on-init", .. rest] => {
#| wait_on_init = true
#| continue rest
#| }
#| ["-stderr", ..rest] => {
#| use_stderr = true
#| use_stderr_in_stub()
#| continue rest
#| }
#| [['-', ..] as flag, ..] => fail("unrecognized argument \{flag}")
#| [t, .. rest] => {
#| time = @string.parse_int(t)
#| continue rest
#| }
#| }
#| }
#| let output = if use_stderr { @stdio.stderr } else { @stdio.stdout }
#| defer (
#| if msg_finally is Some(msg) {
#| if use_stderr { eprintln(msg) } else { println(msg) }
#| }
#| )
#| if wait_on_init {
#| ignore(@stdio.stdin.read())
#| }
#| if !use_default_handler {
#| @signal.set_global_cancellation_signals([])
#| register_termination_handler()
#| }
#| if msg_after_init is Some(msg) {
#| output.text.write("\{msg}\n")
#| }
#| @async.sleep(time)
#| if msg_after_sleep is Some(msg) {
#| output.text.write("\{msg}\n")
#| }
#| exit(exit_code)
#|}
),
},
)
///|
let moonbitlang_core_async_test_programs_spawn_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/spawn",
deps={},
files={
"main.mbt": (
#|async fn main {
#| let args = match @env.args()[1:] {
#| ["-no-stdio", .. rest] => {
#| remove_standard_handles()
#| rest
#| }
#| args => args
#| }
#| let (is_orphan, args) = match args {
#| ["-orphan", .. rest] => (true, rest)
#| args => (false, args)
#| }
#| guard args is [cmd, .. args] else { fail("invalid argument") }
#| let exit_code = if is_orphan {
#| let proc = @process.spawn_orphan(cmd, args)
#| defer proc.close()
#| proc.wait()
#| } else {
#| @process.run(cmd, args).exit_code
#| }
#| println(exit_code)
#|}
#|extern "C" fn remove_standard_handles() = "remove_standard_handles"
),
},
)
///|
let moonbitlang_core_async_test_programs_ttyd_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/test_programs/ttyd",
deps={},
files={
"main.mbt": (
#|async fn main {
#| guard @env.get_env_var("TTYD_PORT") is Some(port) else {
#| fail("TTYD_PORT is not set")
#| }
#| let tty = @tty.open()
#| install_state_check()
#| let rpc = @socket.Tcp::connect(@socket.Addr::parse("127.0.0.1:\{port}"))
#| defer rpc.close()
#| while rpc.read_until(b"\n") is Some(line) {
#| let request = @json.parse(@utf8.decode_lossy(line))
#| if request is { "method": "fail", .. } {
#| fail("requested test failure")
#| }
#| rpc.write(@utf8.encode("\{respond(tty, request).stringify()}\n"))
#| }
#|}
#|priv suberror RpcError {
#| RpcError(code~ : Int32, message~ : String)
#|}
#|async fn respond(tty : @tty.Tty, request : Json) -> Json {
#| guard request
#| is {
#| "jsonrpc": "2.0",
#| "method": String(name),
#| "id"? : id,
#| "params"? : params,
#| ..
#| } else {
#| return {
#| "jsonrpc": "2.0",
#| "id": null,
#| "error": { "code": -32600, "message": "Invalid Request" },
#| }
#| }
#| let id = id.unwrap_or(null)
#| let params = params.unwrap_or(Json::empty_object())
#| try call(tty, name, params) catch {
#| RpcError(code~, message~) =>
#| {
#| "jsonrpc": "2.0",
#| "id": id,
#| "error": { "code": Json(code), "message": Json(message) },
#| }
#| err =>
#| {
#| "jsonrpc": "2.0",
#| "id": id,
#| "error": { "code": -32000, "message": Json(@debug.to_string(err)) },
#| }
#| } noraise {
#| result => { "jsonrpc": "2.0", "id": id, "result": result }
#| }
#|}
#|async fn call(tty : @tty.Tty, name : String, params : Json) -> Json {
#| match name {
#| "enter_raw_mode" => {
#| tty.enter_raw_mode()
#| null
#| }
#| "leave_raw_mode" => {
#| tty.leave_raw_mode()
#| null
#| }
#| "reopen" => {
#| ignore(@tty.open())
#| null
#| }
#| "size" => {
#| let (rows, cols) = tty.size()
#| [rows, cols]
#| }
#| "read" => {
#| let max_len = match params {
#| { "max_len": Number(max_len, ..), .. } => Some(max_len.to_int())
#| { "max_len": _, .. } =>
#| raise invalid_params("`max_len` must be a number")
#| _ => None
#| }
#| read_result(params, () => {
#| guard tty.wait_resize() is Input else { return { "type": "resize" } }
#| match max_len {
#| None => input_result(tty.read())
#| Some(max_len) => {
#| let buf = FixedArray::make(max_len, b'\x00')
#| match tty.read_into(buf.mut_view()) {
#| 0 => { "type": "eof" }
#| n => input_result(Some(Bytes::from_array(buf[:n])))
#| }
#| }
#| }
#| })
#| }
#| "read_exactly" => {
#| guard params is { "len": Number(len, ..), .. } else {
#| raise invalid_params("`len` must be a number")
#| }
#| read_result(params, () => {
#| input_result(Some(tty.read_exactly(len.to_int())))
#| })
#| }
#| "peek_exactly" => {
#| guard params is { "len": Number(len, ..), .. } else {
#| raise invalid_params("`len` must be a number")
#| }
#| input_result(Some(tty.peek_exactly(len.to_int()).to_owned()))
#| }
#| "read_until" =>
#| read_result(params, () => input_result(tty.read_until(b"\n")))
#| "expect_unexpected_eof" =>
#| try tty.read_exactly(2) catch {
#| @io.UnexpectedEof => { "type": "unexpected_eof" }
#| err => raise err
#| } noraise {
#| _ => fail("expected ReaderError::UnexpectedEof")
#| }
#| "drop_exactly" => {
#| guard params is { "len": Number(len, ..), .. } else {
#| raise invalid_params("`len` must be a number")
#| }
#| tty.drop_exactly(len.to_int())
#| { "type": "dropped" }
#| }
#| "parse_input" => {
#| guard params is { "len": Number(len, ..), .. } else {
#| raise invalid_params("`len` must be a number")
#| }
#| let len = len.to_int()
#| let lookahead = match params {
#| { "lookahead": Number(n, ..), .. } => n.to_int()
#| _ => 0
#| }
#| read_result(params, () => {
#| guard tty.wait_resize() is Input else { return { "type": "resize" } }
#| ignore(tty.peek_exactly(len + lookahead))
#| let bytes = tty.peek_exactly(len).to_owned()
#| tty.drop_exactly(len)
#| input_result(Some(bytes))
#| })
#| }
#| "wait_resize" =>
#| match tty.wait_resize() {
#| Resize => { "type": "resize" }
#| Input => { "type": "input" }
#| }
#| "write" => {
#| guard params is { "bytes": String(bytes), .. } else {
#| raise invalid_params("`bytes` must be a base64 string")
#| }
#| tty.write(@base64.decode(bytes))
#| null
#| }
#| _ => raise RpcError(code=-32601, message="Method not found: \{name}")
#| }
#|}
#|async fn read_result(params : Json, f : async () -> Json) -> Json {
#| try {
#| match params {
#| { "timeout": Number(timeout, ..), .. } =>
#| match @async.with_timeout_opt(timeout.to_int(), () => f()) {
#| None => return { "type": "timeout" }
#| Some(result) => result
#| }
#| { "timeout": _, .. } => raise invalid_params("`timeout` must be a number")
#| _ => f()
#| }
#| } catch {
#| @io.UnexpectedEof => { "type": "eof" }
#| err => raise err
#| }
#|}
#|fn input_result(result : Bytes?) -> Json {
#| match result {
#| Some(bytes) => { "type": "input", "bytes": @base64.encode(bytes) }
#| None => { "type": "eof" }
#| }
#|}
#|fn invalid_params(message : String) -> RpcError {
#| RpcError(code=-32602, message~)
#|}
),
"state_check.mbt": (
#|fn install_state_check() -> Unit raise {
#| guard watch_state() else { fail("could not snapshot terminal state") }
#|}
#|extern "C" fn watch_state() -> Bool = "ttyd_watch_state"
),
},
)
///|
let moonbitlang_core_async_tls_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/tls",
deps={},
files={
"types.mbt": (
#|pub(all) enum TrustedRoot {
#| NoVerification
#| SystemRoot
#| CustomPemFile(String)
#|}
),
},
)
///|
let moonbitlang_core_async_tty_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/tty",
deps={},
files={
"extends.mbt": (
#|pub extend Tty with @io.BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
),
"ffi.native.mbt": (
#|#cfg(not(platform="windows"))
#|extern "C" fn tty_name() -> @c_buffer.Buffer = "moonbit_rt_tty_name"
#|#cfg(not(platform="windows"))
#|async fn open_handles(
#| context~ : String,
#|) -> (@event_loop.IoHandle, @event_loop.IoHandle) {
#| let name = tty_name()
#| if name.is_null() {
#| @os_error.check_errno(context)
#| }
#| let io = open_device("/dev/\{name.decode_as_os_string()}", context~)
#| (io, io)
#|}
#|#cfg(platform="windows")
#|let _target_specific_imports : Unit = ignore(@c_buffer.Buffer::is_null)
#|#cfg(platform="windows")
#|async fn open_handles(
#| context~ : String,
#|) -> (@event_loop.IoHandle, @event_loop.IoHandle) {
#| let input = open_device("CONIN$", context~)
#| errdefer input.close()
#| (input, open_device("CONOUT$", context~))
#|}
#|async fn open_device(path : String, context~ : String) -> @event_loop.IoHandle {
#| let (io, _) = @event_loop.open(
#| path,
#| 2,
#| parent=@fd_util.invalid_fd,
#| create=0,
#| append=false,
#| sync=0,
#| mode=0,
#| context~,
#| )
#| io
#|}
#|#borrow(input, output, state)
#|extern "C" fn tty_get_state(
#| input : @fd_util.Fd,
#| output : @fd_util.Fd,
#| state : FixedArray[Byte],
#|) -> Int32 = "moonbit_rt_tty_get_state"
#|#borrow(input, output, state)
#|extern "C" fn tty_set_state(
#| input : @fd_util.Fd,
#| output : @fd_util.Fd,
#| state : FixedArray[Byte],
#|) -> Int32 = "moonbit_rt_tty_set_state"
#|#borrow(output)
#|extern "C" fn tty_size(output : @fd_util.Fd) -> Int64 = "moonbit_rt_tty_size"
),
"tty.mbt": (
#|struct Tty {
#| tty : @event_loop.Tty
#| output : @event_loop.IoHandle
#| read_ctrl : @io.BinaryReaderController
#| write_ctrl : @io.BinaryWriterController
#| original_state : State
#| mut waiting_for_event : Bool
#| mut pending_eof : Bool
#|}
#|let instance : @ref.Ref[Tty?] = {
#| let instance : @ref.Ref[Tty?] = Ref(None)
#| @event_loop.register_hook(exit=() => {
#| guard instance.val is Some(tty) else { return }
#| instance.val = None
#| defer tty.close()
#| tty.leave_raw_mode()
#| })
#| instance
#|}
#|pub async fn open() -> Tty {
#| @coroutine.check_cancellation()
#| if instance.val is Some(tty) {
#| return tty
#| }
#| let (input, output) = open_handles(context="@tty.open()")
#| if instance.val is Some(other) {
#| output.close()
#| input.close()
#| return other
#| }
#| let tty = Tty::new(input~, output~)
#| instance.val = Some(tty)
#| tty
#|}
#|fn Tty::new(
#| input~ : @event_loop.IoHandle,
#| output~ : @event_loop.IoHandle,
#|) -> Tty raise {
#| errdefer {
#| output.close()
#| input.close()
#| }
#| let original_state = State::read(input.fd(), output.fd())
#| {
#| tty: @event_loop.Tty::new(input~),
#| output,
#| read_ctrl: BinaryReaderController(),
#| write_ctrl: BinaryWriterController(),
#| original_state,
#| waiting_for_event: false,
#| pending_eof: false,
#| }
#|}
#|fn Tty::close(self : Tty) -> Unit {
#| self.output.close()
#| self.tty.close()
#|}
#|priv struct State(FixedArray[Byte])
#|fn state_size() -> Int32 {
#| match @event_loop.platform {
#| Linux => 44
#| MacOS => 72
#| Windows => 8
#| @env_util.Platform::.. => panic()
#| }
#|}
#|fn State::flag(self : State, index : Int32) -> UInt64 {
#| let width = if @event_loop.platform is MacOS { 8 } else { 4 }
#| let mut value = 0UL
#| for i = width - 1; i >= 0; i = i - 1 {
#| value = (value << 8) | self.0[index * width + i].to_uint64()
#| }
#| value
#|}
#|fn State::set_flag(self : State, index : Int32, value : UInt64) -> Unit {
#| let width = if @event_loop.platform is MacOS { 8 } else { 4 }
#| for i in 0..> (8 * i)).to_byte()
#| }
#|}
#|fn State::set_cc(self : State, index : Int32, value : Byte) -> Unit {
#| let offset = if @event_loop.platform is MacOS { 32 } else { 17 }
#| self.0[offset + index] = value
#|}
#|fn State::copy(self : State) -> State {
#| State(self.0.copy())
#|}
#|const IGNBRK : UInt64 = 0x1
#|const BRKINT : UInt64 = 0x2
#|const IGNPAR : UInt64 = 0x4
#|const PARMRK : UInt64 = 0x8
#|const INPCK : UInt64 = 0x10
#|const ISTRIP : UInt64 = 0x20
#|const INLCR : UInt64 = 0x40
#|const IGNCR : UInt64 = 0x80
#|const ICRNL : UInt64 = 0x100
#|let ixon : UInt64 = match @event_loop.platform {
#| Linux => 0x400
#| MacOS => 0x200
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let ixoff : UInt64 = match @event_loop.platform {
#| Linux => 0x1000
#| MacOS => 0x400
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|const IMAXBEL : UInt64 = 0x2000
#|const OPOST : UInt64 = 0x1
#|let csize : UInt64 = match @event_loop.platform {
#| Linux => 0x30
#| MacOS => 0x300
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let cs8 : UInt64 = match @event_loop.platform {
#| Linux => 0x30
#| MacOS => 0x300
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let cread : UInt64 = match @event_loop.platform {
#| Linux => 0x80
#| MacOS => 0x800
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let parenb : UInt64 = match @event_loop.platform {
#| Linux => 0x100
#| MacOS => 0x1000
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let isig : UInt64 = match @event_loop.platform {
#| Linux => 0x1
#| MacOS => 0x80
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let icanon : UInt64 = match @event_loop.platform {
#| Linux => 0x2
#| MacOS => 0x100
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|const ECHO : UInt64 = 0x8
#|let echoe : UInt64 = match @event_loop.platform {
#| Linux => 0x10
#| MacOS => 0x2
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let echok : UInt64 = match @event_loop.platform {
#| Linux => 0x20
#| MacOS => 0x4
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let echonl : UInt64 = match @event_loop.platform {
#| Linux => 0x40
#| MacOS => 0x10
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let noflsh : UInt64 = match @event_loop.platform {
#| Linux => 0x80
#| MacOS => 0x80000000
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let tostop : UInt64 = match @event_loop.platform {
#| Linux => 0x100
#| MacOS => 0x400000
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let pendin : UInt64 = match @event_loop.platform {
#| Linux => 0x4000
#| MacOS => 0x20000000
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|let iexten : UInt64 = match @event_loop.platform {
#| Linux => 0x8000
#| MacOS => 0x400
#| Windows => 0
#| @env_util.Platform::.. => panic()
#|}
#|const ENABLE_PROCESSED_INPUT : UInt64 = 0x1
#|const ENABLE_LINE_INPUT : UInt64 = 0x2
#|const ENABLE_ECHO_INPUT : UInt64 = 0x4
#|const ENABLE_WINDOW_INPUT : UInt64 = 0x8
#|const ENABLE_QUICK_EDIT_MODE : UInt64 = 0x40
#|const ENABLE_EXTENDED_FLAGS : UInt64 = 0x80
#|const ENABLE_VIRTUAL_TERMINAL_INPUT : UInt64 = 0x200
#|const ENABLE_PROCESSED_OUTPUT : UInt64 = 0x1
#|const ENABLE_VIRTUAL_TERMINAL_PROCESSING : UInt64 = 0x4
#|const DISABLE_NEWLINE_AUTO_RETURN : UInt64 = 0x8
#|fn State::make_raw(self : State) -> State {
#| let raw = self.copy()
#| match @event_loop.platform {
#| Linux => {
#| let iflag_clear = IGNBRK |
#| BRKINT |
#| PARMRK |
#| ISTRIP |
#| INLCR |
#| IGNCR |
#| ICRNL |
#| ixon
#| raw.set_flag(0, raw.flag(0) & iflag_clear.lnot())
#| raw.set_flag(1, raw.flag(1) & OPOST.lnot())
#| raw.set_flag(2, (raw.flag(2) & (csize | parenb).lnot()) | cs8)
#| raw.set_flag(
#| 3,
#| raw.flag(3) & (ECHO | echonl | icanon | isig | iexten).lnot(),
#| )
#| raw.set_cc(6, b'\x01') // VMIN
#| raw.set_cc(5, b'\x00') // VTIME
#| }
#| MacOS => {
#| let iflag_clear = IMAXBEL |
#| ixoff |
#| INPCK |
#| BRKINT |
#| PARMRK |
#| ISTRIP |
#| INLCR |
#| IGNCR |
#| ICRNL |
#| ixon |
#| IGNPAR
#| raw.set_flag(0, (raw.flag(0) & iflag_clear.lnot()) | IGNBRK)
#| raw.set_flag(1, raw.flag(1) & OPOST.lnot())
#| raw.set_flag(2, (raw.flag(2) & (csize | parenb).lnot()) | cs8 | cread)
#| let lflag_clear = ECHO |
#| echoe |
#| echok |
#| echonl |
#| icanon |
#| isig |
#| iexten |
#| noflsh |
#| tostop |
#| pendin
#| raw.set_flag(3, raw.flag(3) & lflag_clear.lnot())
#| raw.set_cc(16, b'\x01') // VMIN
#| raw.set_cc(17, b'\x00') // VTIME
#| }
#| Windows => {
#| let clear = ENABLE_PROCESSED_INPUT |
#| ENABLE_LINE_INPUT |
#| ENABLE_ECHO_INPUT |
#| ENABLE_QUICK_EDIT_MODE
#| let set = ENABLE_WINDOW_INPUT |
#| ENABLE_EXTENDED_FLAGS |
#| ENABLE_VIRTUAL_TERMINAL_INPUT
#| raw.set_flag(0, (raw.flag(0) & clear.lnot()) | set)
#| raw.set_flag(
#| 1,
#| raw.flag(1) |
#| ENABLE_PROCESSED_OUTPUT |
#| ENABLE_VIRTUAL_TERMINAL_PROCESSING |
#| DISABLE_NEWLINE_AUTO_RETURN,
#| )
#| }
#| @env_util.Platform::.. => panic()
#| }
#| raw
#|}
#|fn State::is_raw(self : State) -> Bool {
#| match @event_loop.platform {
#| Linux | MacOS => (self.flag(3) & icanon) == 0
#| Windows =>
#| (self.flag(0) & ENABLE_LINE_INPUT) == 0 &&
#| (self.flag(0) & ENABLE_VIRTUAL_TERMINAL_INPUT) != 0
#| @env_util.Platform::.. => panic()
#| }
#|}
#|fn State::read(input : @fd_util.Fd, output : @fd_util.Fd) -> State raise {
#| let state = State(FixedArray::make(state_size(), b'\x00'))
#| if tty_get_state(input, output, state.0) < 0 {
#| @os_error.check_errno("@tty: read terminal state")
#| }
#| state
#|}
#|fn Tty::set_state(self : Tty, state : State) -> Unit raise {
#| if tty_set_state(self.tty.input_fd(), self.output.fd(), state.0) < 0 {
#| @os_error.check_errno("@tty: set terminal state")
#| }
#|}
#|pub fn Tty::enter_raw_mode(self : Tty) -> Unit raise {
#| self.set_state(self.original_state.make_raw())
#|}
#|pub fn Tty::leave_raw_mode(self : Tty) -> Unit raise {
#| self.set_state(self.original_state)
#|}
#|pub fn Tty::size(self : Tty) -> (Int32, Int32) raise {
#| let packed = tty_size(self.output.fd())
#| if packed < 0 {
#| @os_error.check_errno("@tty.Tty::size()")
#| }
#| ((packed >> 32).to_int(), (packed & 0xFFFF).to_int())
#|}
#|#derive(@debug.Debug)
#|pub(all) enum Event {
#| Input
#| Resize
#|}
#|priv suberror Resized
#|pub async fn Tty::wait_resize(self : Tty) -> Event raise @io.ReaderError {
#| @coroutine.check_cancellation()
#| if self.tty.take_pending_resize() {
#| return Resize
#| }
#| if self.pending_eof {
#| return Input
#| }
#| self.waiting_for_event = true
#| defer {
#| self.waiting_for_event = false
#| }
#| let first = @io.BinaryReader::peek(self) catch {
#| TransportError(Resized::Resized, ..) => return Resize
#| err => raise err
#| }
#| self.pending_eof = first is None
#| @coroutine.check_cancellation()
#| Input
#|}
#|impl @io.BinaryReader for Tty
#|impl @io.BinaryReaderImpl for Tty with fn name(_self) {
#| "@tty.Tty"
#|}
#|impl @io.BinaryReaderImpl for Tty with fn get_controller(self) {
#| self.read_ctrl
#|}
#|impl @io.BinaryReaderImpl for Tty with fn raw_read_impl(
#| self,
#| buf,
#| offset~,
#| max_len~,
#|) {
#| @coroutine.check_cancellation()
#| if self.pending_eof {
#| self.pending_eof = false
#| return 0
#| }
#| let n = self.tty.read(
#| buf,
#| offset~,
#| len=max_len,
#| raw=@event_loop.platform is Windows &&
#| State::read(self.tty.input_fd(), self.output.fd()).is_raw(),
#| block_resize=!self.waiting_for_event,
#| context="@tty.Tty",
#| )
#| guard n >= 0 else { raise Resized::Resized }
#| n
#|}
#|pub impl @io.BinaryWriter for Tty
#|impl @io.BinaryWriterImpl for Tty with fn raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len~,
#|) {
#| self.output.write(
#| buf,
#| offset~,
#| len~,
#| context="impl @io.BinaryWriter for @tty.Tty",
#| )
#|}
#|impl @io.BinaryWriterImpl for Tty with fn name(_self) {
#| "@tty.Tty"
#|}
#|impl @io.BinaryWriterImpl for Tty with fn get_controller(self) {
#| self.write_ctrl
#|}
#|impl @io.BinaryWriterImpl for Tty with fn flush_partial_data(self) {
#| ignore(self)
#|}
#|pub extend Tty with @io.BinaryWriter::{write, write_reader}
),
},
)
///|
let moonbitlang_core_async_types_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/async/types",
deps={},
files={
"extends.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|pub extend Fd with Eq::{equal}
#|#cfg(any(target="native", target="wasm"))
#|pub extend Fd with Hash::{hash}
),
"types.mbt": (
#|#cfg(not(target="native"))
#|#warnings("-unused_constructor")
#|struct Fd(UInt64)
#|#cfg(all(target="native", not(platform="windows")))
#|#warnings("-unused_constructor")
#|struct Fd(Int32)
#|#cfg(platform="windows")
#|#external
#|type Fd
#|#cfg(not(platform="windows"))
#|pub impl Eq for Fd with fn equal(x, y) {
#| x.0 == y.0
#|}
#|#cfg(platform="windows")
#|pub impl Eq for Fd with fn equal(x, y) = "%refeq"
#|#cfg(platform="windows")
#|extern "C" fn Fd::to_uint64(fd : Fd) -> UInt64 = "moonbit_rt_fd_as_uint64"
#|#cfg(not(platform="windows"))
#|pub impl Hash for Fd with fn hash_combine(self, hasher) {
#| Hash::hash_combine(self.0, hasher)
#|}
#|#cfg(platform="windows")
#|pub impl Hash for Fd with fn hash_combine(self, hasher) {
#| hasher.combine_uint64(self.to_uint64())
#|}
#|#non_exhaustive
#|pub(all) enum Platform {
#| Linux = 0
#| MacOS = 1
#| Windows = 2
#|}
),
},
)
///|
let moonbitlang_core_bench_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bench",
deps={},
files={
"bench.mbt": (
#|fn iter_n_microseconds(inner : () -> Unit, k : Int32) -> Double {
#| let ts = monotonic_clock_start()
#| for _ in 0.. Unit, count : UInt32) -> Summary {
#| guard! count > 0 && count <= 0x7FFFFFFFU
#| let count = count.reinterpret_as_int()
#| let threshold = 100000.0 // 100 ms
#| let target_batch_size = for trial = 1 {
#| let single_us = iter_n_microseconds(inner, trial) / trial.to_double()
#| let target_batch_size = threshold /
#| (if single_us < 1.0 { 1.0 } else { single_us })
#| if trial.to_double() * single_us > threshold {
#| break target_batch_size
#| }
#| continue trial * 2
#| }
#| let batch_size = if target_batch_size < 1.0 {
#| 1
#| } else {
#| target_batch_size.ceil().to_int()
#| }
#| let samples = Array::makei(count, _ => {
#| iter_n_microseconds(inner, batch_size) / batch_size.to_double()
#| })
#| samples.sort()
#| winsorize(sorted_data=samples, 5.0)
#| Summary::new(name?, sorted_data=samples, batch_size)
#|}
#|pub fn Bench::bench(
#| self : Bench,
#| name? : String,
#| f : () -> Unit,
#| count~ : UInt32 = 10,
#|) -> Unit {
#| let summary = iter_count(name?, f, count)
#| if !self.buffer.is_empty() {
#| self.buffer.write_char(',')
#| }
#| self.buffer.write_string(summary.to_json().stringify(escape_slash=true))
#| self.summaries.push(summary)
#|}
#|pub fn single_bench(
#| name? : String,
#| f : () -> Unit,
#| count~ : UInt32 = 10,
#|) -> Summary {
#| iter_count(name?, f, count)
#|}
#|pub fn[Any] Bench::keep(self : Bench, value : Any) -> Unit {
#| let trait_object : &OpaqueValue = @ref.Ref(value)
#| self._storage = trait_object
#|}
#|#coverage.skip
#|pub fn Bench::dump_summaries(self : Bench) -> String {
#| "[\{self.buffer.to_string()}]"
#|}
#|#skip("slow benchmarking test")
#|test "bench - single_bench basic functionality" {
#| let bench_result = single_bench(
#| () => {
#| let x = 1 + 1
#| ignore(x)
#| },
#| count=2,
#| )
#| inspect(bench_result.runs, content="2")
#| inspect(bench_result.batch_size > 0, content="true")
#| inspect(bench_result.sum > 0.0, content="true")
#| inspect(bench_result.min <= bench_result.max, content="true")
#|}
#|#skip("slow benchmarking test")
#|test "bench - Summary struct properties" {
#| let bench_result = single_bench(
#| () => {
#| let x = 1
#| ignore(x)
#| },
#| count=5,
#| )
#| inspect(bench_result.mean >= 0, content="true")
#| inspect(bench_result.median >= 0, content="true")
#| inspect(bench_result.std_dev >= 0, content="true")
#| inspect(bench_result.std_dev_pct >= 0, content="true")
#| inspect(bench_result.median_abs_dev >= 0, content="true")
#| inspect(bench_result.median_abs_dev_pct >= 0, content="true")
#| let (q1, q2, q3) = bench_result.quartiles
#| inspect(q1 <= q2, content="true")
#| inspect(q2 <= q3, content="true")
#| inspect((q3 - q1 - bench_result.iqr).abs() < 0.001, content="true")
#|}
#|#skip("slow benchmarking test")
#|test "bench - T struct and bench method" {
#| let bench_obj = Bench()
#| inspect(bench_obj.buffer.is_empty(), content="true")
#| inspect(bench_obj.summaries.is_empty(), content="true")
#| bench_obj.bench(
#| () => {
#| let x = 20 * 20
#| ignore(x)
#| },
#| count=2,
#| )
#| inspect(bench_obj.buffer.is_empty(), content="false")
#| inspect(bench_obj.summaries.length(), content="1")
#| bench_obj.bench(
#| () => {
#| let y = 30 * 30
#| ignore(y)
#| },
#| count=2,
#| )
#| inspect(bench_obj.summaries.length(), content="2")
#|}
#|#skip("slow benchmarking test")
#|test "bench - monotonic clock functionality" {
#| let start = monotonic_clock_start()
#| let result = for _ in 0..<1000; result = 0 {
#| continue result + 1
#| } nobreak {
#| result
#| }
#| ignore(result)
#| let elapsed = monotonic_clock_end(start)
#| inspect(elapsed >= 0.0, content="true")
#|}
#|test "bench - keep stores value" {
#| let b = Bench()
#| @test.assert_eq(b.summaries.length(), 0)
#| b.keep(123)
#| @test.assert_eq(b.summaries.length(), 0)
#| b.keep(456)
#| @test.assert_eq(b.summaries.length(), 0)
#|}
),
"debug.mbt": (
#|pub impl @debug.Debug for Bench with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "Bench",
#| @debug.Repr::array(self.summaries.map(s => Repr(s))),
#| )
#|}
#|pub impl @debug.Debug for Timestamp with fn to_repr(_) {
#| @debug.Repr::opaque_("Timestamp", @debug.Repr::omitted())
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bench with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Summary with @debug.Debug::{to_repr}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Summary with @json.ToJson::{to_json}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Timestamp with @debug.Debug::{to_repr}
),
"monotonic_clock_js.mbt": (
#|type Timestamp
#|pub extern "js" fn monotonic_clock_start() -> Timestamp =
#| #| () => performance.now()
#|pub extern "js" fn monotonic_clock_end(ts : Timestamp) -> Double =
#| #| (ts) => (performance.now() - ts) * 1000.0
),
"stats.mbt": (
#|#derive(@json.ToJson, @debug.Debug)
#|struct Summary {
#| name : String?
#| sum : Double
#| min : Double
#| max : Double
#| mean : Double
#| median : Double
#| variance : Double
#| std_dev : Double
#| std_dev_pct : Double
#| median_abs_dev : Double
#| median_abs_dev_pct : Double
#| quartiles : (Double, Double, Double)
#| iqr : Double
#| batch_size : Int32
#| runs : Int32
#|}
#|fn Summary::new(
#| name? : String,
#| sorted_data~ : Array[Double],
#| batch_size : Int32,
#|) -> Summary {
#| let sum = sum(sorted_data)
#| let min = min(sorted_data~)
#| let max = max(sorted_data~)
#| let mean = mean(sorted_data, sum~)
#| let median = median(sorted_data~)
#| let variance = variance(sorted_data, mean~)
#| let std_dev = std_dev(variance~)
#| let std_dev_pct = std_dev_pct(mean~, std_dev~)
#| let median_abs_dev = median_abs_dev(sorted_data, median_=median)
#| let median_abs_dev_pct = median_abs_dev_pct(median~, median_abs_dev~)
#| let quartiles = quartiles(sorted_data~)
#| let iqr = iqr(quartiles~)
#| {
#| name,
#| sum,
#| min,
#| max,
#| mean,
#| median,
#| variance,
#| std_dev,
#| std_dev_pct,
#| median_abs_dev,
#| median_abs_dev_pct,
#| quartiles,
#| iqr,
#| batch_size,
#| runs: sorted_data.length(),
#| }
#|}
#|fn sum(data : Array[Double]) -> Double {
#| for i in data; sum = 0.0 {
#| continue sum + i
#| } nobreak {
#| sum
#| }
#|}
#|fn min(sorted_data~ : Array[Double]) -> Double {
#| sorted_data[0]
#|}
#|fn max(sorted_data~ : Array[Double]) -> Double {
#| sorted_data[sorted_data.length() - 1]
#|}
#|fn mean(data : Array[Double], sum~ : Double) -> Double {
#| let count = data.length()
#| sum / count.to_double()
#|}
#|fn median(sorted_data~ : Array[Double]) -> Double {
#| percentile(sorted_data~, pct=50.0)
#|}
#|fn variance(data : Array[Double], mean~ : Double) -> Double {
#| if data.length() < 2 {
#| return 0.0
#| }
#| for i in data; v = 0.0 {
#| let d = i - mean
#| continue v + d * d
#| } nobreak {
#| v / (data.length() - 1).to_double()
#| }
#|}
#|fn std_dev(variance~ : Double) -> Double {
#| variance.sqrt()
#|}
#|fn std_dev_pct(mean~ : Double, std_dev~ : Double) -> Double {
#| if mean == 0.0 {
#| return 0.0
#| }
#| std_dev / mean * 100.0
#|}
#|fn median_abs_dev(data : Array[Double], median_~ : Double) -> Double {
#| let abs_devs = data.map(x => (median_ - x).abs())
#| abs_devs.sort()
#| median(sorted_data=abs_devs) * 1.4826
#|}
#|fn median_abs_dev_pct(median~ : Double, median_abs_dev~ : Double) -> Double {
#| if median == 0.0 {
#| return 0.0
#| }
#| median_abs_dev / median * 100.0
#|}
#|fn quartiles(sorted_data~ : Array[Double]) -> (Double, Double, Double) {
#| let q1 = percentile(sorted_data~, pct=25.0)
#| let q2 = percentile(sorted_data~, pct=50.0)
#| let q3 = percentile(sorted_data~, pct=75.0)
#| (q1, q2, q3)
#|}
#|fn iqr(quartiles~ : (Double, Double, Double)) -> Double {
#| let (q1, _, q3) = quartiles
#| q3 - q1
#|}
#|fn percentile(sorted_data~ : Array[Double], pct~ : Double) -> Double {
#| guard! !sorted_data.is_empty()
#| guard! pct >= 0.0 && pct <= 100.0
#| if sorted_data.length() == 1 {
#| return sorted_data[0]
#| }
#| if pct == 100.0 {
#| return sorted_data[sorted_data.length() - 1]
#| }
#| let length = (sorted_data.length() - 1).to_double()
#| let rank = pct / 100 * length
#| let lrank = rank.floor()
#| let d = rank - lrank
#| let n = lrank.to_int()
#| let lo = sorted_data[n]
#| let hi = sorted_data[n + 1]
#| lo + (hi - lo) * d
#|}
#|fn winsorize(sorted_data~ : Array[Double], pct : Double) -> Unit {
#| let lo = percentile(sorted_data~, pct~)
#| let hi = percentile(sorted_data~, pct=100.0 - pct)
#| for i, samp in sorted_data {
#| if samp > hi {
#| sorted_data[i] = hi
#| } else if samp < lo {
#| sorted_data[i] = lo
#| }
#| }
#|}
#|test {
#| let data = [1.1, 21.4, 2.2, 3.3, 4.5, 12.5, 33.3, 14.4]
#| data.sort()
#| let summary = Summary::new(sorted_data=data, 3)
#| assert_true(summary.sum.is_close(92.7))
#| assert_true(summary.min.is_close(1.1))
#| assert_true(summary.max.is_close(33.3))
#| assert_true(summary.mean.is_close(11.5875))
#| assert_true(summary.median.is_close(8.5))
#| assert_true(summary.variance.is_close(127.64125))
#| assert_true(summary.std_dev.is_close(11.297842714430043))
#| assert_true(summary.std_dev_pct.is_close(97.50026075020534))
#| assert_true(summary.median_abs_dev.is_close(9.04386))
#| assert_true(summary.median_abs_dev_pct.is_close(106.39835294117646))
#| let (q1, q2, q3) = summary.quartiles
#| assert_true(q1.is_close(3.025))
#| assert_true(q2.is_close(8.5))
#| assert_true(q3.is_close(16.15))
#| assert_true(summary.iqr.is_close(13.125))
#| assert_true(summary.batch_size == 3)
#| assert_true(summary.runs == 8)
#| @json.json_inspect(summary, content={
#| "sum": 92.69999999999999,
#| "min": 1.1,
#| "max": 33.3,
#| "mean": 11.587499999999999,
#| "median": 8.5,
#| "variance": 127.64124999999999,
#| "std_dev": 11.297842714430042,
#| "std_dev_pct": 97.50026075020534,
#| "median_abs_dev": 9.043859999999999,
#| "median_abs_dev_pct": 106.39835294117646,
#| "quartiles": [3.025, 8.5, 16.15],
#| "iqr": 13.124999999999998,
#| "batch_size": 3,
#| "runs": 8,
#| })
#|}
#|test "std_dev_pct zero mean" {
#| @test.assert_eq(std_dev_pct(mean=0.0, std_dev=5.0), 0.0)
#|}
#|test "median_abs_dev_pct zero median" {
#| @test.assert_eq(median_abs_dev_pct(median=0.0, median_abs_dev=3.0), 0.0)
#|}
#|test "percentile edge cases" {
#| @test.assert_eq(percentile(sorted_data=[42.0], pct=50.0), 42.0)
#| @test.assert_eq(percentile(sorted_data=[1.0, 2.0, 3.0], pct=100.0), 3.0)
#|}
),
"types.mbt": (
#|priv trait OpaqueValue {}
#|impl[X] OpaqueValue for @ref.Ref[X]
#|#alias(T)
#|struct Bench {
#| buffer : StringBuilder
#| summaries : Array[Summary]
#| mut _storage : &OpaqueValue
#|}
#|#alias(new, deprecated="Use `Bench()` instead")
#|#as_free_fn
#|#as_free_fn(new, deprecated="Use `Bench()` instead")
#|pub fn Bench::Bench() -> Bench {
#| let buffer = StringBuilder()
#| let summaries = []
#| { buffer, summaries, _storage: @ref.Ref(()), }
#|}
),
},
)
///|
let moonbitlang_core_bigint_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bigint",
deps={},
files={
"bigint.mbt": (
#|pub impl @json.ToJson for BigInt with fn to_json(self : BigInt) -> @json.Json {
#| @json.Json::string(self.to_string())
#|}
#|pub impl Show for BigInt with fn to_string(self) {
#| BigInt::to_string(self)
#|}
#|pub impl @json.FromJson for BigInt with fn from_json(json, path) {
#| guard json is String(s) else {
#| raise JsonDecodeError(
#| (path, "BigInt::from_json: expected number in string representation"),
#| )
#| }
#| parse_bigint(s) catch {
#| _ =>
#| raise JsonDecodeError(
#| (path, "BigInt::from_json: invalid number in string representation"),
#| )
#| }
#|}
#|pub impl Default for BigInt with fn default() {
#| zero
#|}
#|pub fn BigInt::equal_int(self : BigInt, other : Int32) -> Bool {
#| can_convert_to_int(self) && self.to_int() == other
#|}
#|pub fn BigInt::equal_int64(self : BigInt, other : Int64) -> Bool {
#| can_convert_to_int64(self) && self.to_int64() == other
#|}
#|pub fn BigInt::equal_uint(self : BigInt, other : UInt32) -> Bool {
#| if is_neg(self) || self.bit_length() > 32 {
#| false
#| } else {
#| self.to_uint() == other
#| }
#|}
#|pub fn BigInt::equal_uint64(self : BigInt, other : UInt64) -> Bool {
#| if is_neg(self) || self.bit_length() > 64 {
#| false
#| } else {
#| self.to_uint64() == other
#| }
#|}
#|pub fn BigInt::compare_int(self : BigInt, other : Int32) -> Int32 {
#| guard can_convert_to_int(self) else {
#| return if is_neg(self) { -1 } else { 1 }
#| }
#| let self = self.to_int()
#| Int32::compare(self, other)
#|}
#|pub fn BigInt::compare_int64(self : BigInt, other : Int64) -> Int32 {
#| guard can_convert_to_int64(self) else {
#| return if is_neg(self) { -1 } else { 1 }
#| }
#| let self = self.to_int64()
#| Int64::compare(self, other)
#|}
#|pub fn BigInt::compare_uint(self : BigInt, other : UInt32) -> Int32 {
#| if is_neg(self) {
#| -1
#| } else if self.bit_length() > 32 {
#| 1
#| } else {
#| UInt32::compare(self.to_uint(), other)
#| }
#|}
#|pub fn BigInt::compare_uint64(self : BigInt, other : UInt64) -> Int32 {
#| if is_neg(self) {
#| -1
#| } else if self.bit_length() > 64 {
#| 1
#| } else {
#| UInt64::compare(self.to_uint64(), other)
#| }
#|}
#|test "can_convert_to_int" {
#| assert_true(can_convert_to_int(0N))
#| assert_true(can_convert_to_int(1N))
#| assert_true(can_convert_to_int(-1N))
#| assert_true(can_convert_to_int(2147483647N)) // Int.max_value
#| assert_true(can_convert_to_int(-2147483648N)) // Int.min_value
#| assert_false(can_convert_to_int(2147483648N)) // Int.max_value + 1
#| assert_false(can_convert_to_int(-2147483649N)) // Int.min_value - 1
#| assert_false(can_convert_to_int(4294967295N)) // 2^32 - 1
#| assert_false(can_convert_to_int(-4294967295N)) // -(2^32 - 1)
#| assert_false(can_convert_to_int(4294967296N)) // 2^32
#| assert_false(can_convert_to_int(-4294967296N)) // -2^32
#|}
#|test "can_convert_to_int64" {
#| assert_true(can_convert_to_int64(0N))
#| assert_true(can_convert_to_int64(1N))
#| assert_true(can_convert_to_int64(-1N))
#| assert_true(can_convert_to_int64(2147483647N)) // Int.max_value
#| assert_true(can_convert_to_int64(-2147483648N)) // Int.min_value
#| assert_true(can_convert_to_int64(2147483648N)) // Int.max_value + 1
#| assert_true(can_convert_to_int64(-2147483649N)) // Int.min_value - 1
#| assert_true(can_convert_to_int64(4294967295N)) // 2^32 - 1
#| assert_true(can_convert_to_int64(-4294967295N)) // -(2^32 - 1)
#| assert_true(can_convert_to_int64(4294967296N)) // 2^32
#| assert_true(can_convert_to_int64(-4294967296N)) // -2^32
#| assert_true(can_convert_to_int64(9223372036854775807N)) // Int64.max_value
#| assert_true(can_convert_to_int64(-9223372036854775808N)) // Int64.min_value
#| assert_false(can_convert_to_int64(9223372036854775808N)) // Int64.max_value + 1
#| assert_false(can_convert_to_int64(-9223372036854775809N)) // Int64.min_value - 1
#| assert_false(can_convert_to_int64(18446744073709551615N)) // 2^64 - 1
#| assert_false(can_convert_to_int64(-18446744073709551615N)) // -(2^64 - 1)
#| assert_false(can_convert_to_int64(18446744073709551616N)) // 2^64
#| assert_false(can_convert_to_int64(-18446744073709551616N)) // -2^64
#|}
#|pub impl Hash for BigInt with fn hash_combine(self, hasher) {
#| hasher.combine(self.signum())
#| for limb in self.limbs() {
#| hasher.combine(limb)
#| }
#|}
#|fn BigInt::signum(self : Self) -> Int32 {
#| if self.is_zero() {
#| 0
#| } else if is_neg(self) {
#| -1
#| } else {
#| 1
#| }
#|}
#|pub fn BigInt::to_uint16(self : BigInt) -> UInt16 {
#| self.to_int().to_uint16()
#|}
#|pub fn BigInt::to_int16(self : BigInt) -> @int16.Int16 {
#| @int16.Int16::from_int(self.to_int())
#|}
#|pub impl @debug.Debug for BigInt with fn to_repr(self) {
#| @debug.Repr::integer(self.to_string())
#|}
#|fn digit_from_char(x : UInt16) -> Int32? {
#| match x {
#| '0'..<='9' => Some(x.to_int() - '0')
#| 'A'..<='Z' => Some(x.to_int() + (10 - 'A'))
#| 'a'..<='z' => Some(x.to_int() + (10 - 'a'))
#| _ => None
#| }
#|}
#|#cfg(not(target="js"))
#|fn char_from_digit(d : Int32) -> Char {
#| if d < 10 {
#| (d + '0').unsafe_to_char()
#| } else {
#| (d - 10 + 'a').unsafe_to_char()
#| }
#|}
#|fn pow2_shift(radix : Int32) -> Int32? {
#| if radix >= 2 && (radix & (radix - 1)) == 0 {
#| Some(radix.ctz())
#| } else {
#| None
#| }
#|}
#|pub fn BigInt::abs(self : BigInt) -> BigInt {
#| if is_neg(self) {
#| -self
#| } else {
#| self
#| }
#|}
#|pub fn BigInt::to_double(self : BigInt) -> Double {
#| if self.is_zero() {
#| return 0.0
#| }
#| let negative = is_neg(self)
#| let mag = if negative { -self } else { self }
#| let bits = mag.bit_length()
#| let magnitude = if bits <= 53 {
#| mag.to_uint64().to_double()
#| } else {
#| let shift = bits - 54
#| let top = (mag >> shift).to_uint64()
#| let sticky = mag.ctz() < shift
#| let mut significand = top >> 1
#| let mut exponent = shift + 1
#| if (top & 1UL) != 0UL && (sticky || (significand & 1UL) != 0UL) {
#| significand += 1UL
#| if significand == 1UL << 53 {
#| significand = significand >> 1
#| exponent += 1
#| }
#| }
#| let unbiased = 52 + exponent
#| if unbiased > 1023 {
#| 0x7FF0000000000000UL.reinterpret_as_double()
#| } else {
#| (((unbiased + 1023).to_uint64() << 52) |
#| (significand & 0x000FFFFFFFFFFFFFUL)).reinterpret_as_double()
#| }
#| }
#| if negative {
#| -magnitude
#| } else {
#| magnitude
#| }
#|}
),
"bigint_js.mbt": (
#|type BigInt
#|let zero = 0N
#|#internal(internal, "use `@string.parse_bigint` instead")
#|#doc(hidden)
#|pub fn parse_bigint(str : StringView, base~ : Int32 = 10) -> BigInt raise {
#| if base < 2 || base > 36 {
#| base_err()
#| }
#| if str.is_empty() {
#| syntax_err()
#| }
#| match str.unsafe_get(str.length() - 1) {
#| '0'..<='9' | 'A'..<='Z' | 'a'..<='z' => ()
#| _ => syntax_err()
#| }
#| if base == 10 {
#| match str {
#| ['0', 'x' | 'X' | 'o' | 'O' | 'b' | 'B', ..]
#| | ['-', '0', 'x' | 'X' | 'o' | 'O' | 'b' | 'B', ..] => syntax_err()
#| ['0'..<='9', ..] | ['-', '0'..<='9', ..] => ()
#| _ => syntax_err()
#| }
#| }
#| match base {
#| 2 | 8 | 10 | 16 =>
#| match
#| BigInt::js_parse_string(
#| str.data(),
#| str.start_offset(),
#| str.start_offset() + str.length(),
#| base,
#| value => Some(value),
#| None,
#| ) {
#| Some(value) => value
#| None => syntax_err()
#| }
#| _ => BigInt::from_string_radix(str, base)
#| }
#|}
#|extern "js" fn BigInt::js_parse_string(
#| str : String,
#| start : Int32,
#| end : Int32,
#| base : Int32,
#| some : (BigInt) -> BigInt?,
#| none : BigInt?,
#|) -> BigInt? =
#| #|(str, start, end, base, some, none) => {
#| #| try {
#| #| if (base === 10) {
#| #| const input = start === 0 && end === str.length ? str : str.slice(start, end);
#| #| return some(BigInt(input));
#| #| }
#| #| const negative = str.charCodeAt(start) === 45;
#| #| const digits = str.slice(negative ? start + 1 : start, end);
#| #| const prefix = base === 2 ? "0b" : base === 8 ? "0o" : "0x";
#| #| const value = BigInt(prefix + digits);
#| #| return some(negative ? -value : value);
#| #| } catch (_) {
#| #| return none;
#| #| }
#| #|}
#|fn BigInt::from_string_radix(str : StringView, radix : Int32) -> BigInt raise {
#| let len = str.length()
#| let sign = if str.unsafe_get(0) == '-' { -1 } else { 1 }
#| let start = if sign == -1 { 1 } else { 0 }
#| if start == len {
#| syntax_err()
#| }
#| let mut acc = 0N
#| match pow2_shift(radix) {
#| Some(shift) =>
#| for i in start.. {
#| let base = BigInt::from_int(radix)
#| for i in start.. String {
#| if radix < 2 || radix > 36 {
#| abort("radix must be between 2 and 36")
#| }
#| BigInt::js_to_string_radix(self, radix)
#|}
#|extern "js" fn BigInt::js_to_string_radix(
#| self : BigInt,
#| radix : Int32,
#|) -> String =
#| #|(x, radix) => x.toString(radix)
#|extern "js" fn hex2(b : Byte) -> String =
#| #|(x) => x.toString(16).padStart(2, '0')
#|pub fn BigInt::from_octets(octets : BytesView, signum~ : Int32 = 1) -> BigInt {
#| if signum < 0 {
#| return -1N * BigInt::from_octets(octets, signum=1)
#| }
#| if signum == 0 {
#| return 0N
#| }
#| if octets.is_empty() {
#| return 0N
#| }
#| let str = StringBuilder()
#| for octet in octets {
#| str.write_string(hex2(octet))
#| }
#| parse_bigint(str.to_string(), base=16) catch {
#| Failure(msg) => abort(msg)
#| _ => abort("invalid syntax")
#| }
#|}
#|pub fn BigInt::to_octets(self : BigInt, length? : Int32) -> Bytes {
#| if self < 0 {
#| abort("negative BigInt")
#| }
#| if self == 0 {
#| return match length {
#| Some(len) =>
#| if len <= 0 {
#| abort("negative length")
#| } else {
#| Bytes::make(len, 0)
#| }
#| None => [0]
#| }
#| }
#| let buf = []
#| for v = self {
#| if v > 0 {
#| buf.push(v.to_byte())
#| continue v >> 8
#| } else {
#| break
#| }
#| }
#| let buf_len = buf.length()
#| match length {
#| Some(len) => {
#| if len <= 0 {
#| abort("negative length")
#| }
#| if len > buf_len {
#| Bytes::makei(len, i => {
#| let padding = len - buf_len
#| if i < padding {
#| 0
#| } else {
#| buf[buf_len - (i - padding) - 1]
#| }
#| })
#| } else {
#| Bytes::makei(buf_len, i => buf[buf_len - i - 1])
#| }
#| }
#| None => Bytes::makei(buf_len, i => buf[buf_len - i - 1])
#| }
#|}
#|extern "js" fn BigInt::compare_js(self : BigInt, other : BigInt) -> Int32 =
#| #|(x, y) => x < y ? -1 : x > y ? 1 : 0
#|pub impl Compare for BigInt with fn compare(self, other) {
#| self.compare_js(other)
#|}
#|extern "js" fn BigInt::equal_js(self : BigInt, other : BigInt) -> Bool =
#| #|(x, y) => x === y
#|pub impl Eq for BigInt with fn equal(self, other) {
#| self.equal_js(other)
#|}
#|pub extern "js" fn BigInt::from_int(x : Int32) -> BigInt =
#| #|(x) => BigInt(x)
#|pub extern "js" fn BigInt::from_uint(x : UInt32) -> BigInt =
#| #|(x) => BigInt(x >>> 0)
#|pub extern "js" fn BigInt::from_int64(x : Int64) -> BigInt =
#| #|(x) => BigInt.asIntN(64, x)
#|pub fn BigInt::from_uint64(x : UInt64) -> BigInt = "%identity"
#|pub extern "js" fn BigInt::is_zero(self : BigInt) -> Bool =
#| #|(x) => x === 0n
#|extern "js" fn BigInt::op_neg_ffi(self : BigInt) -> BigInt =
#| #|(x) => -x
#|pub impl Neg for BigInt with fn neg(self) {
#| self.op_neg_ffi()
#|}
#|extern "js" fn BigInt::op_add_ffi(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x + y
#|pub impl Add for BigInt with fn add(self, other) {
#| self.op_add_ffi(other)
#|}
#|extern "js" fn BigInt::op_sub_ffi(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x - y
#|pub impl Sub for BigInt with fn sub(self, other) {
#| self.op_sub_ffi(other)
#|}
#|extern "js" fn BigInt::op_mul_ffi(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x * y
#|pub impl Mul for BigInt with fn mul(self, other) {
#| self.op_mul_ffi(other)
#|}
#|extern "js" fn BigInt::op_div_ffi(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x / y
#|pub impl Div for BigInt with fn div(self, other) {
#| self.op_div_ffi(other)
#|}
#|extern "js" fn BigInt::op_mod_ffi(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x % y
#|pub impl Mod for BigInt with fn mod(self, other) {
#| self.op_mod_ffi(other)
#|}
#|extern "js" fn BigInt::modpow_ffi(
#| self : BigInt,
#| exponent : BigInt,
#| modulus : BigInt,
#|) -> BigInt =
#| #|(x, y, z) => {
#| #| if (z === 1n) return 0n;
#| #| let result = 1n;
#| #| x = ((x % z) + z) % z;
#| #| while (y > 0n) {
#| #| if (y & 1n) {
#| #| result = (result * x) % z;
#| #| }
#| #| y >>= 1n;
#| #| x = (x * x) % z;
#| #| }
#| #| return result;
#| #|}
#|extern "js" fn BigInt::pow_ffi(self : BigInt, exponent : BigInt) -> BigInt =
#| #|(x, y) => x ** y
#|pub fn BigInt::pow(
#| self : BigInt,
#| exponent : BigInt,
#| modulus? : BigInt,
#|) -> BigInt {
#| if exponent < 0 {
#| abort("negative exponent")
#| }
#| match modulus {
#| Some(modulus) =>
#| if modulus <= 0 {
#| abort("non-positive modulus")
#| } else {
#| self.modpow_ffi(exponent, modulus)
#| }
#| None => self.pow_ffi(exponent)
#| }
#|}
#|extern "js" fn BigInt::to_byte(self : BigInt) -> Byte =
#| #|(x) => Number(BigInt.asUintN(8, x)) | 0
#|pub impl Shl for BigInt with fn shl(self : BigInt, n : Int32) -> BigInt {
#| if n < 0 {
#| abort("negative shift count")
#| }
#| self.js_shl(n)
#|}
#|pub impl Shr for BigInt with fn shr(self : BigInt, n : Int32) -> BigInt {
#| if n < 0 {
#| abort("negative shift count")
#| }
#| self.js_shr(n)
#|}
#|extern "js" fn BigInt::js_shl(self : BigInt, other : Int32) -> BigInt =
#| #|(x, y) => x << BigInt(y)
#|extern "js" fn BigInt::js_shr(self : BigInt, other : Int32) -> BigInt =
#| #|(x, y) => x >> BigInt(y)
#|extern "js" fn BigInt::js_land(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x & y
#|pub impl BitAnd for BigInt with fn land(self, other) {
#| self.js_land(other)
#|}
#|extern "js" fn BigInt::js_lor(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x | y
#|pub impl BitOr for BigInt with fn lor(self, other) {
#| self.js_lor(other)
#|}
#|extern "js" fn BigInt::js_lxor(self : BigInt, other : BigInt) -> BigInt =
#| #|(x, y) => x ^ y
#|pub impl BitXOr for BigInt with fn lxor(self, other) {
#| self.js_lxor(other)
#|}
#|pub extern "js" fn BigInt::to_uint(self : BigInt) -> UInt32 =
#| #|(x) => Number(BigInt.asUintN(32, x)) | 0
#|pub extern "js" fn BigInt::to_int(self : BigInt) -> Int32 =
#| #|(x) => Number(BigInt.asIntN(32, x))
#|pub fn BigInt::to_uint64(self : BigInt) -> UInt64 {
#| let hi = (self >> 32).to_uint()
#| let lo = self.to_uint()
#| (hi.to_uint64() << 32) | lo.to_uint64()
#|}
#|pub fn BigInt::to_int64(self : BigInt) -> Int64 {
#| let hi = (self >> 32).to_uint()
#| let lo = self.to_uint()
#| (hi.to_int64() << 32) | lo.to_int64()
#|}
#|pub extern "js" fn BigInt::bit_length(self : BigInt) -> Int32 =
#| #|(n) => {
#| #| if (n >= 0) {
#| #| return n === 0n ? 0 : n.toString(2).length;
#| #| } else {
#| #| const absN = -n;
#| #| const absMinus1 = absN - 1n;
#| #| return absMinus1 === 0n ? 0 : absMinus1.toString(2).length;
#| #| }
#| #|}
#|pub extern "js" fn BigInt::ctz(self : BigInt) -> Int32 =
#| #|(n) => {
#| #| if (n === 0n) return 0;
#| #| let absN = n < 0n ? -n : n;
#| #| let count = 0;
#| #| while ((absN & 1n) === 0n) {
#| #| absN >>= 1n;
#| #| count++;
#| #| }
#| #| return count;
#| #|}
#|extern "js" fn can_convert_to_int(x : BigInt) -> Bool =
#| #|(x) => x >= -(2n ** 31n) && x < 2n ** 31n
#|extern "js" fn can_convert_to_int64(x : BigInt) -> Bool =
#| #|(x) => x >= -(2n ** 63n) && x < 2n ** 63n
#|extern "js" fn is_neg(x : BigInt) -> Bool =
#| #|(x) => x < 0n
#|fn BigInt::limbs(self : Self) -> Array[UInt32] {
#| guard !self.is_zero() else { [0] }
#| let result = []
#| for n = self * BigInt::from_int(self.signum()); n > 0; n = n >> 32 {
#| let limb = n.to_uint()
#| result.push(limb)
#| }
#| result
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `@string.parse_bigint` instead")
#|pub fn BigInt::from_string(input : String, radix~ : Int32 = 10) -> BigInt {
#| parse_bigint(input, base=radix) catch {
#| Failure(msg) => abort(msg)
#| _ => abort("invalid syntax")
#| }
#|}
),
"extends.mbt": (
#|pub extend BigInt with Add::{add}
#|pub extend BigInt with BitAnd::{land}
#|pub extend BigInt with BitOr::{lor}
#|pub extend BigInt with BitXOr::{lxor}
#|pub extend BigInt with Compare::{compare}
#|pub extend BigInt with Div::{div}
#|pub extend BigInt with Eq::{equal}
#|pub extend BigInt with Hash::{hash}
#|pub extend BigInt with Mod::{mod}
#|pub extend BigInt with Mul::{mul}
#|pub extend BigInt with Neg::{neg}
#|pub extend BigInt with Shl::{shl}
#|pub extend BigInt with Shr::{shr}
#|pub extend BigInt with Sub::{sub}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BigInt with @json.ToJson::{to_json}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BigInt with @debug.Debug::{to_repr}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BigInt with @json.FromJson::{from_json}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BigInt with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BigInt with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BigInt with Hash::{hash_combine}
),
"strconv_errors.mbt": (
#|let syntax_err_str = "invalid syntax"
#|let base_err_str = "invalid base"
#|fn[T] syntax_err() -> T raise {
#| raise Failure::Failure(syntax_err_str)
#|}
#|fn[T] base_err() -> T raise {
#| raise Failure::Failure(base_err_str)
#|}
),
},
)
///|
let moonbitlang_core_buffer_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/buffer",
deps={},
files={
"buffer.mbt": (
#|struct Buffer {
#| mut data : FixedArray[Byte]
#| mut len : Int32
#|}
#|fn buffer_growth_capacity(
#| current : Int32,
#| len : Int32,
#| required : Int32,
#|) -> Int32 {
#| if required < len {
#| abort("Buffer capacity overflow")
#| }
#| let start = current.max(8)
#| let enough_space = for space = start {
#| if space >= required {
#| break space
#| }
#| let next = space * 2
#| if next <= space {
#| break required
#| }
#| continue next
#| }
#| enough_space
#|}
#|#inline(never)
#|fn Buffer::grow(self : Buffer, required : Int32) -> Unit {
#| let new_capacity = buffer_growth_capacity(
#| self.data.length(),
#| self.len,
#| required,
#| )
#| let new_data = FixedArray::make_and_blit(
#| self.data,
#| allocate_len=new_capacity,
#| init=b'\x00',
#| len=self.len,
#| )
#| self.data = new_data
#|}
#|pub fn Buffer::length(self : Buffer) -> Int32 {
#| self.len
#|}
#|pub fn Buffer::is_empty(self : Buffer) -> Bool {
#| self.len == 0
#|}
#|#deprecated("use `Buffer()` instead (with `Buffer` in scope via the prelude)")
#|pub fn new(size_hint~ : Int32 = 0) -> Buffer {
#| Buffer(capacity=size_hint)
#|}
#|#label_migration(capacity, alias=size_hint, msg="Use capacity= instead")
#|pub fn Buffer::Buffer(capacity~ : Int32 = 0) -> Buffer {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| let data = FixedArray::make(capacity, b'\x00')
#| { data, len: 0, }
#|}
#|pub fn from_bytes(bytes : BytesView) -> Buffer {
#| let val_len = bytes.length()
#| let buf = Buffer(capacity=val_len)
#| buf.data.blit_from_bytes(0, bytes.data(), bytes.start_offset(), val_len)
#| buf.len = val_len
#| buf
#|}
#|pub fn from_array(arr : ArrayView[Byte]) -> Buffer {
#| let buf = Buffer(capacity=arr.length())
#| let data = buf.data
#| let mut len = buf.len
#| for byte in arr {
#| data.unsafe_set(len, byte)
#| len += 1
#| }
#| buf.len = len
#| buf
#|}
#|pub fn from_iter(iter : Iter[Byte]) -> Buffer {
#| let buf = Buffer()
#| for byte in iter; capacity = buf.data.length() {
#| let capacity = if buf.len == capacity {
#| buf.grow(capacity + 1)
#| buf.data.length()
#| } else {
#| capacity
#| }
#| buf.data.unsafe_set(buf.len, byte)
#| buf.len += 1
#| continue capacity
#| }
#| buf
#|}
#|pub fn Buffer::write_uint64_be(self : Buffer, value : UInt64) -> Unit {
#| if self.data.length() - self.len < 8 {
#| self.grow(self.len + 8)
#| }
#| let data = self.data
#| let offset = self.len
#| data.unsafe_set(offset, (value >> 56).to_byte())
#| data.unsafe_set(offset + 1, (value >> 48).to_byte())
#| data.unsafe_set(offset + 2, (value >> 40).to_byte())
#| data.unsafe_set(offset + 3, (value >> 32).to_byte())
#| data.unsafe_set(offset + 4, (value >> 24).to_byte())
#| data.unsafe_set(offset + 5, (value >> 16).to_byte())
#| data.unsafe_set(offset + 6, (value >> 8).to_byte())
#| data.unsafe_set(offset + 7, value.to_byte())
#| self.len += 8
#|}
#|pub fn Buffer::write_uint64_le(self : Buffer, value : UInt64) -> Unit {
#| if self.data.length() - self.len < 8 {
#| self.grow(self.len + 8)
#| }
#| let data = self.data
#| let offset = self.len
#| data.unsafe_set(offset, value.to_byte())
#| data.unsafe_set(offset + 1, (value >> 8).to_byte())
#| data.unsafe_set(offset + 2, (value >> 16).to_byte())
#| data.unsafe_set(offset + 3, (value >> 24).to_byte())
#| data.unsafe_set(offset + 4, (value >> 32).to_byte())
#| data.unsafe_set(offset + 5, (value >> 40).to_byte())
#| data.unsafe_set(offset + 6, (value >> 48).to_byte())
#| data.unsafe_set(offset + 7, (value >> 56).to_byte())
#| self.len += 8
#|}
#|pub fn Buffer::write_int64_be(self : Buffer, value : Int64) -> Unit {
#| self.write_uint64_be(value.reinterpret_as_uint64())
#|}
#|pub fn Buffer::write_int64_le(self : Buffer, value : Int64) -> Unit {
#| self.write_uint64_le(value.reinterpret_as_uint64())
#|}
#|pub fn Buffer::write_uint_be(self : Buffer, value : UInt32) -> Unit {
#| if self.data.length() - self.len < 4 {
#| self.grow(self.len + 4)
#| }
#| let data = self.data
#| let offset = self.len
#| data.unsafe_set(offset, (value >> 24).to_byte())
#| data.unsafe_set(offset + 1, (value >> 16).to_byte())
#| data.unsafe_set(offset + 2, (value >> 8).to_byte())
#| data.unsafe_set(offset + 3, value.to_byte())
#| self.len += 4
#|}
#|pub fn Buffer::write_uint_le(self : Buffer, value : UInt32) -> Unit {
#| if self.data.length() - self.len < 4 {
#| self.grow(self.len + 4)
#| }
#| let data = self.data
#| let offset = self.len
#| data.unsafe_set(offset, value.to_byte())
#| data.unsafe_set(offset + 1, (value >> 8).to_byte())
#| data.unsafe_set(offset + 2, (value >> 16).to_byte())
#| data.unsafe_set(offset + 3, (value >> 24).to_byte())
#| self.len += 4
#|}
#|pub fn Buffer::write_int_be(self : Buffer, value : Int32) -> Unit {
#| self.write_uint_be(value.reinterpret_as_uint())
#|}
#|pub fn Buffer::write_int_le(self : Buffer, value : Int32) -> Unit {
#| self.write_uint_le(value.reinterpret_as_uint())
#|}
#|pub fn Buffer::write_uint16_be(self : Buffer, value : UInt16) -> Unit {
#| if self.data.length() - self.len < 2 {
#| self.grow(self.len + 2)
#| }
#| let data = self.data
#| let offset = self.len
#| data.unsafe_set(offset, (value.to_int() >> 8).to_byte())
#| data.unsafe_set(offset + 1, value.to_byte())
#| self.len += 2
#|}
#|pub fn Buffer::write_uint16_le(self : Buffer, value : UInt16) -> Unit {
#| if self.data.length() - self.len < 2 {
#| self.grow(self.len + 2)
#| }
#| let data = self.data
#| let offset = self.len
#| data.unsafe_set(offset, value.to_byte())
#| data.unsafe_set(offset + 1, (value.to_int() >> 8).to_byte())
#| self.len += 2
#|}
#|pub fn Buffer::write_int16_be(self : Buffer, value : @int16.Int16) -> Unit {
#| self.write_uint16_be(value.reinterpret_as_uint16())
#|}
#|pub fn Buffer::write_int16_le(self : Buffer, value : @int16.Int16) -> Unit {
#| self.write_uint16_le(value.reinterpret_as_uint16())
#|}
#|pub fn Buffer::write_double_be(self : Buffer, value : Double) -> Unit {
#| self.write_uint64_be(value.reinterpret_as_uint64())
#|}
#|pub fn Buffer::write_double_le(self : Buffer, value : Double) -> Unit {
#| self.write_uint64_le(value.reinterpret_as_uint64())
#|}
#|pub fn Buffer::write_float_be(self : Buffer, value : @float.Float) -> Unit {
#| self.write_uint_be(value.reinterpret_as_uint())
#|}
#|pub fn Buffer::write_float_le(self : Buffer, value : @float.Float) -> Unit {
#| self.write_uint_le(value.reinterpret_as_uint())
#|}
#|#deprecated("Buffer::write_object writes UTF-16LE bytes; use write_utf8 or explicit Buffer encoders.", skip_current_package=true)
#|pub fn Buffer::write_object(self : Buffer, value : &Show) -> Unit {
#| self.write_string_utf16le(value.to_string())
#|}
#|#alias(write_bytes_interpolation)
#|pub fn[T : Show] Buffer::write_utf8(self : Buffer, value : T) -> Unit {
#| self.write_string_utf8(value.to_string())
#|}
#|#alias(write_bytes, deprecated="Use `write_bytesview` instead")
#|pub fn Buffer::write_bytesview(self : Buffer, value : BytesView) -> Unit {
#| let val_len = value.length()
#| let required = self.len + val_len
#| if required > self.data.length() || required < self.len {
#| self.grow(required)
#| }
#| self.data.blit_from_bytes(
#| self.len,
#| value.data(),
#| value.start_offset(),
#| value.length(),
#| )
#| self.len += val_len
#|}
#|pub fn Buffer::write_char_utf8(buf : Self, value : Char) -> Unit {
#| let code = value.to_uint()
#| match code {
#| _..<0x80 => {
#| if buf.len >= buf.data.length() {
#| buf.grow(buf.len + 1)
#| }
#| buf.data.unsafe_set(buf.len, ((code & 0x7F) | 0x00).to_byte())
#| buf.len += 1
#| }
#| _..<0x0800 => {
#| if buf.data.length() - buf.len < 2 {
#| buf.grow(buf.len + 2)
#| }
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (((code >> 6) & 0x1F) | 0xC0).to_byte())
#| data.unsafe_set(offset + 1, ((code & 0x3F) | 0x80).to_byte())
#| buf.len += 2
#| }
#| _..<0x010000 => {
#| if buf.data.length() - buf.len < 3 {
#| buf.grow(buf.len + 3)
#| }
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (((code >> 12) & 0x0F) | 0xE0).to_byte())
#| data.unsafe_set(offset + 1, (((code >> 6) & 0x3F) | 0x80).to_byte())
#| data.unsafe_set(offset + 2, ((code & 0x3F) | 0x80).to_byte())
#| buf.len += 3
#| }
#| _..<0x110000 => {
#| if buf.data.length() - buf.len < 4 {
#| buf.grow(buf.len + 4)
#| }
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (((code >> 18) & 0x07) | 0xF0).to_byte())
#| data.unsafe_set(offset + 1, (((code >> 12) & 0x3F) | 0x80).to_byte())
#| data.unsafe_set(offset + 2, (((code >> 6) & 0x3F) | 0x80).to_byte())
#| data.unsafe_set(offset + 3, ((code & 0x3F) | 0x80).to_byte())
#| buf.len += 4
#| }
#| _ => abort("Char out of range")
#| }
#|}
#|pub fn Buffer::write_char_utf16le(buf : Self, value : Char) -> Unit {
#| let code = value.to_uint()
#| if code < 0x10000 {
#| if buf.data.length() - buf.len < 2 {
#| buf.grow(buf.len + 2)
#| }
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (code & 0xFF).to_byte())
#| data.unsafe_set(offset + 1, (code >> 8).to_byte())
#| buf.len += 2
#| } else if code < 0x110000 {
#| let cp = code - 0x10000
#| let high = (cp >> 10) | 0xD800
#| let low = (cp & 0x3FF) | 0xDC00
#| if buf.data.length() - buf.len < 4 {
#| buf.grow(buf.len + 4)
#| }
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (high & 0xFF).to_byte())
#| data.unsafe_set(offset + 1, (high >> 8).to_byte())
#| data.unsafe_set(offset + 2, (low & 0xFF).to_byte())
#| data.unsafe_set(offset + 3, (low >> 8).to_byte())
#| buf.len += 4
#| } else {
#| abort("Char out of range")
#| }
#|}
#|pub fn Buffer::write_char_utf16be(buf : Self, value : Char) -> Unit {
#| let code = value.to_uint()
#| if code < 0x10000 {
#| if buf.data.length() - buf.len < 2 {
#| buf.grow(buf.len + 2)
#| }
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (code >> 8).to_byte())
#| data.unsafe_set(offset + 1, (code & 0xFF).to_byte())
#| buf.len += 2
#| } else if code < 0x110000 {
#| if buf.data.length() - buf.len < 4 {
#| buf.grow(buf.len + 4)
#| }
#| let cp = code - 0x10000
#| let high = (cp >> 10) | 0xD800
#| let low = (cp & 0x3FF) | 0xDC00
#| let data = buf.data
#| let offset = buf.len
#| data.unsafe_set(offset, (high >> 8).to_byte())
#| data.unsafe_set(offset + 1, (high & 0xFF).to_byte())
#| data.unsafe_set(offset + 2, (low >> 8).to_byte())
#| data.unsafe_set(offset + 3, (low & 0xFF).to_byte())
#| buf.len += 4
#| } else {
#| abort("Char out of range")
#| }
#|}
#|pub fn Buffer::write_string_utf8(buf : Self, string : StringView) -> Unit {
#| for ch in string {
#| buf.write_char_utf8(ch)
#| }
#|}
#|#alias(write_stringview, deprecated="use write_string_utf16le instead")
#|pub fn Buffer::write_string_utf16le(buf : Self, string : StringView) -> Unit {
#| let len = string.length()
#| let required = buf.len + len * 2
#| if required > buf.data.length() || required < buf.len {
#| buf.grow(required)
#| }
#| let data = buf.data
#| for code_unit in string.code_units(); j = buf.len {
#| let c = code_unit.to_int().reinterpret_as_uint()
#| data.unsafe_set(j, (c & 0xff).to_byte())
#| data.unsafe_set(j + 1, (c >> 8).to_byte())
#| continue j + 2
#| }
#| buf.len += len * 2
#|}
#|pub fn Buffer::write_string_utf16be(buf : Self, string : StringView) -> Unit {
#| let len = string.length()
#| let required = buf.len + len * 2
#| if required > buf.data.length() || required < buf.len {
#| buf.grow(required)
#| }
#| let data = buf.data
#| for code_unit in string.code_units(); j = buf.len {
#| let c = code_unit.to_int().reinterpret_as_uint()
#| data.unsafe_set(j + 1, (c & 0xff).to_byte())
#| data.unsafe_set(j, (c >> 8).to_byte())
#| continue j + 2
#| }
#| buf.len += len * 2
#|}
#|pub fn Buffer::write_byte(self : Buffer, value : Byte) -> Unit {
#| if self.len >= self.data.length() {
#| self.grow(self.len + 1)
#| }
#| self.data.unsafe_set(self.len, value)
#| self.len += 1
#|}
#|pub fn Buffer::write_iter(self : Buffer, iter : Iter[Byte]) -> Unit {
#| for byte in iter {
#| self.write_byte(byte)
#| }
#|}
#|pub fn Buffer::reset(self : Buffer) -> Unit {
#| self.len = 0
#|}
#|#alias(contents)
#|pub fn Buffer::to_bytes(self : Buffer) -> Bytes {
#| Bytes::from_array(self.data[0:self.len])
#|}
#|pub fn Buffer::view(self : Buffer) -> ArrayView[Byte] {
#| self.data[0:self.len]
#|}
#|pub impl Show for Buffer with fn output(self, logger) {
#| logger.write_string(self.contents().to_unchecked_string())
#|}
),
"deprecated.mbt": (
#|#deprecated("`Show` reads a `Buffer` as UTF-16LE. Decode its bytes with `@utf8.decode` or `@utf16.decode`, or inspect it with `@debug.Debug`")
#|pub impl Show for Buffer
),
"extends.mbt": (
#|#deprecated("`Show` reads a `Buffer` as UTF-16LE. Decode its bytes with `@utf8.decode` or `@utf16.decode`, or inspect it with `@debug.Debug`", skip_current_package=true)
#|#doc(hidden)
#|pub extend Buffer with Show::{to_string}
),
"sleb128.mbt": (
#|trait Leb128 {
#| fn output(Self, Buffer) -> Unit
#|}
#|pub impl Leb128 for Int32 with fn output(self, buffer) {
#| let required = buffer.len + 5
#| if required > buffer.data.length() || required < buffer.len {
#| buffer.grow(required)
#| }
#| let data = buffer.data
#| let mut len = buffer.len
#| for value = self {
#| let byte = value & 0x7f // Get the low 7 bits
#| let next_value = value >> 7 // Arithmetic right shift
#| let sign_bit_set = (byte & 0x40) != 0
#| let need_more = if value >= 0 {
#| next_value != 0 || sign_bit_set
#| } else {
#| next_value != -1 || !sign_bit_set
#| }
#| if need_more {
#| data.unsafe_set(len, (byte | 0x80).to_byte())
#| len += 1
#| continue next_value
#| } else {
#| data.unsafe_set(len, byte.to_byte())
#| len += 1
#| break
#| }
#| }
#| buffer.len = len
#|}
#|pub impl Leb128 for Int64 with fn output(self, buffer) {
#| let required = buffer.len + 10
#| if required > buffer.data.length() || required < buffer.len {
#| buffer.grow(required)
#| }
#| let data = buffer.data
#| let mut len = buffer.len
#| for value = self {
#| let byte = value & 0x7f // Get the low 7 bits
#| let next_value = value >> 7 // Arithmetic right shift
#| let sign_bit_set = (byte & 0x40) != 0
#| let need_more = if value >= 0 {
#| next_value != 0 || sign_bit_set
#| } else {
#| next_value != -1 || !sign_bit_set
#| }
#| if need_more {
#| data.unsafe_set(len, (byte | 0x80).to_byte())
#| len += 1
#| continue next_value
#| } else {
#| data.unsafe_set(len, byte.to_byte())
#| len += 1
#| break
#| }
#| }
#| buffer.len = len
#|}
#|pub fn[A : Leb128] Buffer::write_leb128(buffer : Buffer, value : A) -> Unit {
#| value.output(buffer)
#|}
),
"uleb128.mbt": (
#|pub impl Leb128 for UInt32 with fn output(self, buffer) {
#| let required = buffer.len + 5
#| if required > buffer.data.length() || required < buffer.len {
#| buffer.grow(required)
#| }
#| let data = buffer.data
#| let mut len = buffer.len
#| for value = self {
#| if value < 128U {
#| data.unsafe_set(len, value.to_byte())
#| len += 1
#| break
#| } else {
#| data.unsafe_set(len, ((value & 127U) | 128U).to_byte())
#| len += 1
#| continue value >> 7
#| }
#| }
#| buffer.len = len
#|}
#|pub impl Leb128 for UInt64 with fn output(self, buffer) {
#| let required = buffer.len + 10
#| if required > buffer.data.length() || required < buffer.len {
#| buffer.grow(required)
#| }
#| let data = buffer.data
#| let mut len = buffer.len
#| for value = self {
#| if value < 128UL {
#| data.unsafe_set(len, value.to_byte())
#| len += 1
#| break
#| } else {
#| data.unsafe_set(len, ((value & 127UL) | 128UL).to_byte())
#| len += 1
#| continue value >> 7
#| }
#| }
#| buffer.len = len
#|}
),
},
)
///|
let moonbitlang_core_builtin_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/builtin",
deps={},
files={
"array.mbt": (
#|struct Array[T] {
#| mut buf : UninitializedArray[T]
#| mut len : Int32
#|}
#|#doc(hidden)
#|pub fn[T] Array::unsafe_make_uninit(len : Int32) -> Array[T] {
#| { buf: UninitializedArray::make(len), len, }
#|}
#|#owned(src)
#|fn[T] Array::unsafe_make_and_blit(
#| src : UninitializedArray[T],
#| allocate_len~ : Int32,
#| len~ : Int32,
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> Array[T] {
#| {
#| buf: UninitializedArray::make_and_blit(
#| src,
#| allocate_len~,
#| src_offset~,
#| dst_offset~,
#| len~,
#| ),
#| len: allocate_len,
#| }
#|}
#|#owned(src)
#|fn[T] Array::unsafe_make_and_blit_from_fixed(
#| src : FixedArray[T],
#| allocate_len~ : Int32,
#| len~ : Int32,
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> Array[T] {
#| {
#| buf: UninitializedArray::unsafe_make_and_blit_from_fixed(
#| src, allocate_len, src_offset, dst_offset, len,
#| ),
#| len: allocate_len,
#| }
#|}
#|#owned(value)
#|fn[T] Array::unsafe_resize_with_default(
#| self : Array[T],
#| new_len : Int32,
#| value : T,
#|) -> Unit {
#| let len = self.length()
#| guard! new_len >= len
#| if new_len <= self.capacity() {
#| self.buf.unchecked_fill(len, value, new_len - len)
#| } else {
#| let new_buf = UninitializedArray::unsafe_make_and_blit_with_init(
#| self.buf,
#| new_len,
#| value,
#| 0,
#| 0,
#| len,
#| )
#| self.buf = new_buf
#| }
#| self.len = new_len
#|}
#|pub fn[T] Array::Array(capacity~ : Int32 = 0) -> Array[T] {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| if capacity == 0 {
#| []
#| } else {
#| { buf: UninitializedArray::make(capacity), len: 0, }
#| }
#|}
#|#intrinsic("%array.length")
#|pub fn[T] Array::length(self : Array[T]) -> Int32 {
#| self.len
#|}
#|fn[T] Array::unsafe_truncate_to_length(
#| self : Array[T],
#| new_len : Int32,
#|) -> Unit {
#| guard! new_len <= self.length()
#| self.len = new_len
#|}
#|test "unsafe_truncate_to_length" {
#| let arr = [1, 2, 3, 4, 5]
#| arr.unsafe_truncate_to_length(3)
#| assert_true(arr == [1, 2, 3])
#|}
#|fn[T] Array::buffer(self : Array[T]) -> UninitializedArray[T] {
#| self.buf
#|}
#|#inline
#|fn array_growth_capacity(
#| current : Int32,
#| len : Int32,
#| required : Int32,
#|) -> Int32 {
#| if required < len {
#| abort("Array capacity overflow")
#| }
#| let start = if current == 0 { 8 } else { current }
#| for space = start; space < required; {
#| let next = space * 2
#| if next <= space {
#| break required
#| }
#| continue next
#| } nobreak {
#| space
#| }
#|}
#|test "Array growth capacity doubles while representable" {
#| inspect(array_growth_capacity(8, 8, 9), content="16")
#| inspect(array_growth_capacity(8, 8, 33), content="64")
#|}
#|test "Array growth capacity falls back to the exact requirement" {
#| inspect(
#| array_growth_capacity(0x40000000, 0x40000000, 0x40000001),
#| content="1073741825",
#| )
#| inspect(
#| array_growth_capacity(0x20000000, 0x20000000, 0x40000001),
#| content="1073741825",
#| )
#|}
#|test "panic Array growth rejects a wrapped required size" {
#| ignore(array_growth_capacity(16, 16, -1))
#|}
#|fn[T] Array::resize_buffer(self : Array[T], new_capacity : Int32) -> Unit {
#| let old_buf = self.buf
#| self.buf = []
#| let len = self.len
#| let copy_len = if len < new_capacity { len } else { new_capacity }
#| let new_buf = UninitializedArray::make_and_blit(
#| old_buf,
#| allocate_len=new_capacity,
#| len=copy_len,
#| )
#| self.buf = new_buf
#|}
#|test "UninitializedArray::unsafe_blit_fixed" {
#| let src = FixedArray::make(5, 0)
#| let dst = UninitializedArray::make(5)
#| for i in 0..<5 {
#| src[i] = i + 1
#| }
#| UninitializedArray::unsafe_blit_fixed(dst, 0, src, 0, 5)
#| for i in 0..<5 {
#| assert_true(dst[i] == src[i])
#| }
#|}
#|test "Array::resize_buffer" {
#| let arr = Array::Array(capacity=2)
#| arr.push(1)
#| arr.push(2)
#| arr.resize_buffer(4)
#| assert_true(arr.buffer().0.length() >= 4)
#| arr.push(3)
#| arr.push(4)
#| assert_true(arr.length() == 4)
#| assert_true(arr[0] == 1)
#| assert_true(arr[1] == 2)
#| assert_true(arr[2] == 3)
#| assert_true(arr[3] == 4)
#|}
#|#inline(never)
#|fn[T] Array::realloc(self : Array[T], required : Int32) -> Unit {
#| let old_cap = self.capacity()
#| let new_cap = array_growth_capacity(old_cap, self.length(), required)
#| self.resize_buffer(new_cap)
#|}
#|pub fn[T] Array::reserve_capacity(self : Array[T], capacity : Int32) -> Unit {
#| if self.capacity() >= capacity {
#| return
#| }
#| self.resize_buffer(capacity)
#|}
#|pub fn[T] Array::shrink_to_fit(self : Array[T]) -> Unit {
#| if self.capacity() <= self.length() {
#| return
#| }
#| self.resize_buffer(self.length())
#|}
#|#owned(placeholder)
#|pub fn[T] Array::release_unused(self : Array[T], placeholder~ : T) -> Unit {
#| let len = self.len
#| self.buf.unchecked_fill(len, placeholder, self.capacity() - len)
#|}
#|#owned(value)
#|pub fn[T] Array::push(self : Array[T], value : T) -> Unit {
#| if self.length() == self.buffer().0.length() {
#| self.realloc(self.length() + 1)
#| }
#| let length = self.length()
#| self.unsafe_set(length, value)
#| self.len = length + 1
#|}
#|pub fn[T] Array::append(self : Array[T], other : ArrayView[T]) -> Unit {
#| let src = other.buf()
#| let src_offset = other.start()
#| let append_len = other.length()
#| let old_len = self.len
#| let new_len = old_len + append_len
#| guard! new_len >= old_len
#| if new_len > self.buf.0.length() {
#| self.realloc(new_len)
#| }
#| self.len = new_len
#| UninitializedArray::unsafe_blit(
#| self.buf,
#| old_len,
#| src,
#| src_offset,
#| append_len,
#| )
#|}
#|#label_migration(src_offset, fill=false, msg="Use ArrayView::blit_to instead")
#|#label_migration(len, fill=false, msg="Use ArrayView::blit_to instead")
#|pub fn[A] Array::blit_to(
#| self : Array[A],
#| dst : Array[A],
#| len~ : Int32 = self.length(),
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> Unit {
#| let old_len = dst.length()
#| guard! len >= 0 &&
#| dst_offset >= 0 &&
#| src_offset >= 0 &&
#| dst_offset <= old_len &&
#| len <= self.length() - src_offset
#| let new_len = dst_offset + len
#| guard! new_len >= 0
#| if new_len > dst.capacity() {
#| dst.realloc(new_len)
#| }
#| UninitializedArray::unsafe_blit(
#| dst.buffer(),
#| dst_offset,
#| self.buffer(),
#| src_offset,
#| len,
#| )
#| if new_len > old_len {
#| dst.len = new_len
#| }
#|}
#|pub fn[A] ArrayView::blit_to(
#| self : ArrayView[A],
#| dst : Array[A],
#| dst_offset~ : Int32 = 0,
#|) -> Unit {
#| let len = self.length()
#| let old_len = dst.length()
#| guard! dst_offset >= 0 && dst_offset <= old_len
#| let new_len = dst_offset + len
#| guard! new_len >= 0
#| if new_len > dst.capacity() {
#| dst.realloc(new_len)
#| }
#| UninitializedArray::unsafe_blit(
#| dst.buffer(),
#| dst_offset,
#| self.buf(),
#| self.start(),
#| len,
#| )
#| if new_len > old_len {
#| dst.len = new_len
#| }
#|}
#|pub fn[T] Array::pop(self : Array[T]) -> T? {
#| let len = self.length()
#| if len == 0 {
#| None
#| } else {
#| let index = len - 1
#| let v = self.unsafe_get(index)
#| self.len = index
#| Some(v)
#| }
#|}
#|#internal(unsafe, "Panic if the array is empty.")
#|#doc(hidden)
#|#alias(pop_exn, deprecated)
#|pub fn[T] Array::unsafe_pop(self : Array[T]) -> T {
#| let len = self.length()
#| guard! len != 0
#| let index = len - 1
#| let v = self.unsafe_get(index)
#| self.len = index
#| v
#|}
#|pub fn[T] Array::remove(self : Array[T], index : Int32) -> T {
#| guard index >= 0 && index < self.length() else {
#| abort(
#| "index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
#| )
#| }
#| let value = self.unsafe_get(index)
#| UninitializedArray::unsafe_blit(
#| self.buffer(),
#| index,
#| self.buffer(),
#| index + 1,
#| self.length() - index - 1,
#| )
#| self.unsafe_truncate_to_length(self.length() - 1)
#| value
#|}
#|pub fn[T] Array::drain(self : Array[T], begin : Int32, end : Int32) -> Array[T] {
#| guard! begin >= 0 && end <= self.length() && begin <= end
#| let num = end - begin
#| let v = {
#| buf: UninitializedArray::make_and_blit(
#| self.buffer(),
#| allocate_len=num,
#| src_offset=begin,
#| len=num,
#| ),
#| len: num,
#| }
#| UninitializedArray::unsafe_blit(
#| self.buffer(),
#| begin,
#| self.buffer(),
#| end,
#| self.length() - end,
#| )
#| self.unsafe_truncate_to_length(self.length() - num)
#| v
#|}
#|#owned(value)
#|pub fn[T] Array::insert(self : Array[T], index : Int32, value : T) -> Unit {
#| guard index >= 0 && index <= self.length() else {
#| abort(
#| "index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
#| )
#| }
#| if self.length() == self.buffer().0.length() {
#| self.realloc(self.length() + 1)
#| }
#| UninitializedArray::unsafe_blit(
#| self.buffer(),
#| index + 1,
#| self.buffer(),
#| index,
#| self.length() - index,
#| )
#| let length = self.length()
#| self.unsafe_set(index, value)
#| self.len = length + 1
#|}
#|#owned(value)
#|pub fn[A] Array::fill(
#| self : Array[A],
#| value : A,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Unit {
#| let array_length = self.length()
#| guard array_length > 0 else { return }
#| guard! start >= 0 && start < array_length
#| let length = match end {
#| None => array_length
#| Some(e) => {
#| guard! e >= start && e <= array_length
#| e
#| }
#| }
#| self.buf.unchecked_fill(start, value, length - start)
#|}
#|#alias(clone, deprecated)
#|pub fn[T] Array::copy(self : Array[T]) -> Array[T] {
#| let len = self.length()
#| if len == 0 {
#| return []
#| }
#| let arr = Array::unsafe_make_uninit(len)
#| Array::unsafe_blit(arr, 0, self, 0, len)
#| arr
#|}
#|#cfg(any(target="native", target="wasm"))
#|#owned(arr)
#|pub fn[T] Array::from_fixed_array(arr : FixedArray[T]) -> Array[T] {
#| let len = arr.length()
#| if unsafe_is_unique_ref(arr) {
#| { buf: arr[:].buf(), len, }
#| } else {
#| Array::unsafe_make_and_blit_from_fixed(arr, allocate_len=len, len~)
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#owned(arr)
#|pub fn[T] Array::from_fixed_array(arr : FixedArray[T]) -> Array[T] {
#| let len = arr.length()
#| Array::unsafe_make_and_blit_from_fixed(arr, allocate_len=len, len~)
#|}
#|#owned(elem)
#|pub fn[T] Array::make(len : Int32, elem : T) -> Array[T] {
#| let arr = Array::unsafe_make_uninit(len)
#| for i in 0.. T raise?,
#|) -> Array[T] raise? {
#| if length <= 0 {
#| []
#| } else {
#| let array = Array::unsafe_make_uninit(length)
#| for i in 0.. Int32 {
#| self.buffer().0.length()
#|}
#|#intrinsic("%array.unsafe_get")
#|pub fn[T] Array::unsafe_get(self : Array[T], idx : Int32) -> T {
#| self.buffer().unsafe_get(idx)
#|}
#|#intrinsic("%array.get")
#|#alias("_[_]")
#|pub fn[T] Array::at(self : Array[T], index : Int32) -> T {
#| let len = self.length()
#| guard! index >= 0 && index < len
#| self.buffer().unsafe_get(index)
#|}
#|#intrinsic("%array.get_opt")
#|pub fn[T] Array::get(self : Array[T], index : Int32) -> T? {
#| let len = self.length()
#| guard index >= 0 && index < len else { None }
#| Some(self.unsafe_get(index))
#|}
#|#intrinsic("%array.unsafe_set")
#|#owned(val)
#|pub fn[T] Array::unsafe_set(self : Array[T], idx : Int32, val : T) -> Unit {
#| self.buffer().unsafe_set(idx, val)
#|}
#|#intrinsic("%array.set")
#|#alias("_[_]=_")
#|#owned(value)
#|pub fn[T] Array::set(self : Array[T], index : Int32, value : T) -> Unit {
#| let len = self.length()
#| guard! index >= 0 && index < len
#| self.buffer().unsafe_set(index, value)
#|}
#|pub impl[T : Eq] Eq for Array[T] with fn equal(self, other) {
#| let self_len = self.length()
#| let other_len = other.length()
#| guard self_len == other_len else { return false }
#| for i in 0.. Unit raise?) -> Unit raise? {
#| for v in self {
#| f(v)
#| }
#|}
#|#locals(f)
#|pub fn[T] Array::rev_each(
#| self : Array[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| let len = self.length()
#| for i in 0.. Unit raise?,
#|) -> Unit raise? {
#| let len = self.length()
#| for i in 0.. Unit raise?,
#|) -> Unit raise? {
#| for i, v in self {
#| f(i, v)
#| }
#|}
#|#locals(f)
#|#alias(every)
#|pub fn[T] Array::all(self : Array[T], f : (T) -> Bool raise?) -> Bool raise? {
#| for v in self {
#| if !f(v) {
#| return false
#| }
#| }
#| true
#|}
#|#locals(f)
#|#alias(exists)
#|pub fn[T] Array::any(self : Array[T], f : (T) -> Bool raise?) -> Bool raise? {
#| for v in self {
#| if f(v) {
#| return true
#| }
#| }
#| false
#|}
#|pub fn[T] Array::clear(self : Array[T]) -> Unit {
#| self.unsafe_truncate_to_length(0)
#|}
#|#locals(f)
#|pub fn[T, U] Array::map(
#| self : Array[T],
#| f : (T) -> U raise?,
#|) -> Array[U] raise? {
#| let arr = Array::unsafe_make_uninit(self.length())
#| for i, v in self {
#| arr.unsafe_set(i, f(v))
#| }
#| arr
#|}
#|#locals(f)
#|#alias(map_inplace, deprecated)
#|pub fn[T] Array::map_in_place(
#| self : Array[T],
#| f : (T) -> T raise?,
#|) -> Unit raise? {
#| for i, v in self {
#| self[i] = f(v)
#| }
#|}
#|#locals(f)
#|pub fn[T, U] Array::mapi(
#| self : Array[T],
#| f : (Int32, T) -> U raise?,
#|) -> Array[U] raise? {
#| if self.is_empty() {
#| return []
#| }
#| let arr = Array::unsafe_make_uninit(self.length())
#| for i, v in self {
#| arr.unsafe_set(i, f(i, v))
#| }
#| arr
#|}
#|#locals(f)
#|#alias(mapi_inplace, deprecated)
#|pub fn[T] Array::mapi_in_place(
#| self : Array[T],
#| f : (Int32, T) -> T raise?,
#|) -> Unit raise? {
#| for i, v in self {
#| self[i] = f(i, v)
#| }
#|}
#|#locals(f)
#|pub fn[T] Array::filter(
#| self : Array[T],
#| f : (T) -> Bool raise?,
#|) -> Array[T] raise? {
#| [
#| for v in self if f(v) => v
#| ]
#|}
#|pub fn[T] Array::is_empty(self : Array[T]) -> Bool {
#| self.length() == 0
#|}
#|#alias(rev_inplace, deprecated)
#|pub fn[T] Array::rev_in_place(self : Array[T]) -> Unit {
#| let len = self.length()
#| for i in 0..<(len / 2) {
#| let temp = self.unsafe_get(i)
#| self.unsafe_set(i, self.unsafe_get(len - i - 1))
#| self.unsafe_set(len - i - 1, temp)
#| }
#|}
#|pub fn[T] Array::rev(self : Array[T]) -> Array[T] {
#| let len = self.length()
#| let arr = Array::unsafe_make_uninit(len)
#| for i in 0.. (Array[T], Array[T]) {
#| if index < 0 || index > self.length() {
#| let len = self.length()
#| abort(
#| "index out of bounds: the len is from 0 to \{len} but the index is \{index}",
#| )
#| }
#| let v1 = Array::unsafe_make_and_blit(
#| self.buffer(),
#| allocate_len=index,
#| len=index,
#| )
#| let v2 = if index != self.length() {
#| let len2 = self.length() - index
#| Array::unsafe_make_and_blit(
#| self.buffer(),
#| allocate_len=len2,
#| src_offset=index,
#| len=len2,
#| )
#| } else {
#| Array::unsafe_make_uninit(0)
#| }
#| (v1, v2)
#|}
#|pub fn[T : Eq] Array::contains(self : Array[T], value : T) -> Bool {
#| for v in self {
#| if v == value {
#| break true
#| }
#| } nobreak {
#| false
#| }
#|}
#|pub fn[T : Eq] Array::count(self : Array[T], value : T) -> Int32 {
#| self[:].count(value)
#|}
#|#locals(f)
#|pub fn[T] Array::count_if(
#| self : Array[T],
#| f : (T) -> Bool raise?,
#|) -> Int32 raise? {
#| for v in self; count = 0 {
#| if f(v) {
#| continue count + 1
#| }
#| continue count
#| } nobreak {
#| count
#| }
#|}
#|#alias(starts_with, deprecated="Use `has_prefix` instead")
#|pub fn[T : Eq] Array::has_prefix(
#| self : Array[T],
#| prefix : ArrayView[T],
#|) -> Bool {
#| self[:].has_prefix(prefix)
#|}
#|#alias(ends_with, deprecated="Use `has_suffix` instead")
#|pub fn[T : Eq] Array::has_suffix(
#| self : Array[T],
#| suffix : ArrayView[T],
#|) -> Bool {
#| self[:].has_suffix(suffix)
#|}
#|pub fn[T : Eq] Array::strip_prefix(
#| self : Array[T],
#| prefix : ArrayView[T],
#|) -> ArrayView[T]? {
#| self[:].strip_prefix(prefix)
#|}
#|pub fn[T : Eq] Array::strip_suffix(
#| self : Array[T],
#| suffix : ArrayView[T],
#|) -> ArrayView[T]? {
#| self[:].strip_suffix(suffix)
#|}
#|pub fn[T : Eq] Array::search(self : Array[T], value : T) -> Int32? {
#| self[:].search(value)
#|}
#|#locals(f)
#|#alias(find_index, deprecated)
#|pub fn[T] Array::search_by(
#| self : Array[T],
#| f : (T) -> Bool raise?,
#|) -> Int32? raise? {
#| for i, v in self {
#| if f(v) {
#| break Some(i)
#| }
#| } nobreak {
#| None
#| }
#|}
#|pub fn[T : Compare] Array::binary_search(
#| self : Array[T],
#| value : T,
#|) -> Result[Int32, Int32] {
#| self[:].binary_search(value)
#|}
#|pub fn[T] Array::binary_search_by(
#| self : Array[T],
#| cmp : (T) -> Int32 raise?,
#|) -> Result[Int32, Int32] raise? {
#| self[:].binary_search_by(cmp)
#|}
#|pub fn[T] Array::swap(self : Array[T], i : Int32, j : Int32) -> Unit {
#| if i >= self.length() || j >= self.length() || i < 0 || j < 0 {
#| index_out_of_bounds2(self.length(), i, j)
#| }
#| let temp = self.unsafe_get(i)
#| self.unsafe_set(i, self.unsafe_get(j))
#| self.unsafe_set(j, temp)
#|}
#|#locals(f)
#|pub fn[T] Array::retain(self : Array[T], f : (T) -> Bool raise?) -> Unit raise? {
#| let len = self.length()
#| let write = for read, v in self; write = 0 {
#| if f(v) {
#| if read != write {
#| self.unsafe_set(write, v)
#| }
#| continue write + 1
#| }
#| continue write
#| } nobreak {
#| write
#| }
#| if write != len {
#| self.unsafe_truncate_to_length(write)
#| }
#|}
#|pub fn[T] Array::resize(self : Array[T], new_len : Int32, f : T) -> Unit {
#| if new_len < 0 {
#| abort("negative new length")
#| }
#| let len = self.length()
#| if new_len < len {
#| self.unsafe_truncate_to_length(new_len)
#| } else if new_len > len {
#| self.unsafe_resize_with_default(new_len, f)
#| }
#|}
#|pub fn[T] Array::flatten(self : Array[Array[T]]) -> Array[T] {
#| let len = for x in self; len = 0 {
#| continue len + x.length()
#| } nobreak {
#| len
#| }
#| let res = Array::unsafe_make_uninit(len)
#| for xs in self; i = 0 {
#| res.unsafe_blit(i, xs, 0, xs.length())
#| continue i + xs.length()
#| }
#| res
#|}
#|pub fn[T] Array::repeat(self : Array[T], times : Int32) -> Array[T] {
#| if times < 0 {
#| abort("negative repeat count")
#| }
#| let len = self.length()
#| if times == 0 || len == 0 {
#| return []
#| }
#| let total = len * times
#| guard total / times == len else { abort("repeat result too large") }
#| let v = Array::Array(capacity=total)
#| for _ in 0.. B raise?,
#|) -> B raise? {
#| for item in self; acc = init {
#| continue f(acc, item)
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|#alias(fold_right, deprecated)
#|pub fn[A, B] Array::rev_fold(
#| self : Array[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| for i = self.length() - 1, acc = init; i >= 0; {
#| continue i - 1, f(acc, self[i])
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|#alias(fold_lefti, deprecated)
#|pub fn[A, B] Array::foldi(
#| self : Array[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| for i, x in self; acc = init {
#| continue f(i, acc, x)
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|#alias(fold_righti, deprecated)
#|pub fn[A, B] Array::rev_foldi(
#| self : Array[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| let len = self.length()
#| for i in len>..0; acc = init {
#| continue f(len - i - 1, acc, self[i])
#| } nobreak {
#| acc
#| }
#|}
#|pub fn[T : Eq] Array::dedup(self : Array[T]) -> Unit {
#| if self.is_empty() {
#| return
#| }
#| let w = for i in 1.. Bool raise?,
#|) -> Array[T] raise? {
#| let removed = []
#| for read in 0.. Array[ArrayView[T]] {
#| self[:].chunks(size)
#|}
#|#locals(pred)
#|pub fn[T] Array::chunk_by(
#| self : Array[T],
#| pred : (T, T) -> Bool raise?,
#|) -> Array[ArrayView[T]] raise? {
#| let chunks = []
#| if self.is_empty() {
#| return chunks
#| }
#| let start = for i in 1.. Array[ArrayView[T]] {
#| self[:].windows(size)
#|}
#|pub fn[T] Array::suffixes(
#| self : Array[T],
#| include_empty~ : Bool = false,
#|) -> Iter[ArrayView[T]] {
#| self[:].suffixes(include_empty~)
#|}
#|#alias(iterator, deprecated)
#|pub fn[T] Array::iter(self : Array[T]) -> Iter[T] {
#| self[:].iter()
#|}
#|#alias(rev_iterator, deprecated)
#|pub fn[T] Array::rev_iter(self : Array[T]) -> Iter[T] {
#| self[:].rev_iter()
#|}
#|#alias(iterator2, deprecated)
#|pub fn[A] Array::iter2(self : Array[A]) -> Iter2[Int32, A] {
#| self[:].iter2()
#|}
#|pub impl[T] Default for Array[T] with fn default() {
#| []
#|}
#|#internal(unsafe, "Panic if the array is empty.")
#|#doc(hidden)
#|pub fn[A] Array::unsafe_pop_back(self : Array[A]) -> Unit {
#| self.unsafe_pop() |> ignore
#|}
#|pub fn[A] Array::truncate(self : Array[A], len : Int32) -> Unit {
#| guard len >= 0 && len < self.length() else { return }
#| self.unsafe_truncate_to_length(len)
#|}
#|#locals(f)
#|pub fn[A] Array::retain_map(
#| self : Array[A],
#| f : (A) -> A? raise?,
#|) -> Unit raise? {
#| if self.is_empty() {
#| return
#| }
#| let buf = self.buffer()
#| let len = self.length()
#| for read_idx in 0.. {
#| buf[write_idx] = new_val
#| continue write_idx + 1
#| }
#| None => continue write_idx
#| }
#| } nobreak {
#| self.unsafe_truncate_to_length(write_idx)
#| }
#|}
#|#alias(from_iterator, deprecated)
#|pub fn[T] Array::from_iter(iter : Iter[T]) -> Array[T] {
#| iter.collect()
#|}
#|pub fn[T] Array::push_iter(self : Self[T], iter : Iter[T]) -> Unit {
#| if iter.size_hint() is Some(n) {
#| self.reserve_capacity(self.length() + n)
#| }
#| for x in iter {
#| self.push(x)
#| }
#|}
#|#locals(rand)
#|pub fn[T] Array::shuffle_in_place(
#| self : Array[T],
#| rand~ : (Int32) -> Int32,
#|) -> Unit {
#| let n = self.length()
#| for i in n>..1 {
#| let j = rand(i + 1) % (i + 1)
#| self.swap(i, j)
#| }
#|}
#|pub fn[T] Array::shuffle(self : Array[T], rand~ : (Int32) -> Int32) -> Array[T] {
#| let new_arr = self.copy()
#| Array::shuffle_in_place(new_arr, rand~)
#| new_arr
#|}
#|#locals(f)
#|pub fn[A, B] Array::filter_map(
#| self : Array[A],
#| f : (A) -> B? raise?,
#|) -> Array[B] raise? {
#| [
#| for x in self if f(x) is Some(y) => y
#| ]
#|}
#|pub fn[A] Array::last(self : Array[A]) -> A? {
#| match self {
#| [] => None
#| [.., last] => Some(last)
#| }
#|}
#|pub fn[A, B] Array::zip(self : Array[A], other : Array[B]) -> Array[(A, B)] {
#| let length = if self.length() < other.length() {
#| self.length()
#| } else {
#| other.length()
#| }
#| Array::makei(length, i => (self[i], other[i]))
#|}
#|pub fn[T1, T2] Array::unzip(self : Array[(T1, T2)]) -> (Array[T1], Array[T2]) {
#| let arr1 : Array[T1] = Array(capacity=self.length())
#| let arr2 : Array[T2] = Array(capacity=self.length())
#| for pair in self {
#| let (x, y) = pair
#| arr1.push(x)
#| arr2.push(y)
#| }
#| (arr1, arr2)
#|}
#|#alias(zip_to_iter2, deprecated)
#|pub fn[A, B] Array::zip_iter2(self : Array[A], other : Array[B]) -> Iter2[A, B] {
#| let length = if self.length() < other.length() {
#| self.length()
#| } else {
#| other.length()
#| }
#| let mut i = 0
#| Iter(
#| () => {
#| guard i < length else { None }
#| let elem = (self[i], other[i])
#| i += 1
#| Some(elem)
#| },
#| size_hint=length,
#| )
#|}
#|pub fn[A : ToStringView] Array::join(
#| self : Array[A],
#| separator : StringView,
#|) -> String {
#| self[:].join(separator)
#|}
#|pub fn[T : Compare] Array::lexical_compare(
#| self : Array[T],
#| other : Array[T],
#|) -> Int32 {
#| self[:].lexical_compare(other)
#|}
),
"array_block.mbt": (
#|#internal(unsafe, "Panic if the indices or length are out of bounds")
#|#doc(hidden)
#|pub fn[A] Array::unsafe_blit(
#| dst : Array[A],
#| dst_offset : Int32,
#| src : Array[A],
#| src_offset : Int32,
#| len : Int32,
#|) -> Unit {
#| FixedArray::unsafe_blit(
#| dst.buffer().0,
#| dst_offset,
#| src.buffer().0,
#| src_offset,
#| len,
#| )
#|}
#|pub fn[A] Array::unsafe_blit_fixed(
#| dst : Array[A],
#| dst_offset : Int32,
#| src : FixedArray[A],
#| src_offset : Int32,
#| len : Int32,
#|) -> Unit {
#| UninitializedArray::unsafe_blit_fixed(
#| dst.buffer(),
#| dst_offset,
#| src,
#| src_offset,
#| len,
#| )
#|}
#|test "Array::blit_to/basic" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| src[1:4].blit_to(dst, dst_offset=2)
#| assert_true(dst == [0, 0, 2, 3, 4])
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| src[0:3].blit_to(dst)
#| assert_true(dst == [1, 2, 3, 0, 0])
#|}
#|test "Array::blit_to/zero_length" {
#| let src = [1, 2, 3]
#| let dst = [4, 5, 6]
#| src[0:0].blit_to(dst)
#| assert_true(dst == [4, 5, 6])
#|}
#|test "Array::blit_to/grow_destination" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0]
#| src[0:3].blit_to(dst, dst_offset=1)
#| assert_true(dst == [0, 1, 2, 3])
#|}
#|#warnings("-deprecated")
#|test "Array::blit_to/grow_destination_directly" {
#| let src = [1, 2, 3, 4]
#| let dst = [0]
#| Array::blit_to(src, dst, len=2, src_offset=1, dst_offset=1)
#| assert_true(dst == [0, 2, 3])
#| let self = [1, 2, 3]
#| Array::blit_to(self, self, len=2, dst_offset=3)
#| assert_true(self == [1, 2, 3, 1, 2])
#|}
#|test "Array::blit_to/edge_cases" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| src[1:3].blit_to(dst, dst_offset=2)
#| assert_true(dst == [0, 0, 2, 3, 0])
#| src[0:2].blit_to(src, dst_offset=3)
#| assert_true(src == [1, 2, 3, 1, 2])
#| src[0:0].blit_to(dst, dst_offset=0)
#| assert_true(dst == [0, 0, 2, 3, 0])
#| src[0:5].blit_to(dst)
#| assert_true(dst == [1, 2, 3, 1, 2])
#|}
#|test "panic Array::blit_to/boundary_cases1" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| ignore(src.exact_view(start=3, end=1).blit_to(dst))
#|}
#|test "panic Array::blit_to/boundary_cases2" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| ignore(src.exact_view(start=-1, end=1).blit_to(dst))
#|}
#|test "panic Array::blit_to/boundary_cases3" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| ignore(src.exact_view(start=0, end=6).blit_to(dst))
#|}
#|test "panic Array::blit_to/boundary_cases4" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| ignore(src[0:5].blit_to(dst, dst_offset=6))
#|}
#|#warnings("-deprecated")
#|test "panic Array::blit_to/reject_overflowed_source_range" {
#| let src = [1]
#| let dst = [0]
#| Array::blit_to(src, dst, len=0x7fffffff, src_offset=1)
#|}
#|test "Array::blit_to - random cases" {
#| let src = [10, 20, 30, 40, 50]
#| let dst = [0, 0, 0, 0, 0]
#| src[2:4].blit_to(dst, dst_offset=1)
#| assert_true(dst == [0, 30, 40, 0, 0])
#| src[1:4].blit_to(dst, dst_offset=2)
#| assert_true(dst == [0, 30, 20, 30, 40])
#| src[4:5].blit_to(dst, dst_offset=4)
#| assert_true(dst == [0, 30, 20, 30, 50])
#|}
#|test "Array::blit_to - boundary cases" {
#| let src = [1, 2, 3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| src[4:5].blit_to(dst, dst_offset=0)
#| assert_true(dst == [5, 0, 0, 0, 0])
#| src[0:1].blit_to(dst, dst_offset=4)
#| assert_true(dst == [5, 0, 0, 0, 1])
#| src[3:5].blit_to(dst, dst_offset=3)
#| assert_true(dst == [5, 0, 0, 4, 5])
#|}
#|test "Array::unsafe_blit_fixed" {
#| let src = FixedArray::make(3, 1) // Create a FixedArray with 3 elements of value 1
#| let dst = Array::make(5, 0) // Create an Array with 5 elements of value 0
#| Array::unsafe_blit_fixed(dst, 1, src, 0, 2) // Copy 2 elements from src[0] to dst[1]
#| assert_true(dst == [0, 1, 1, 0, 0])
#|}
#|test "ArrayView::blit_to/basic" {
#| let src = [1, 2, 3, 4, 5]
#| let view = src[1:4] // view = [2, 3, 4]
#| let dst = [0, 0, 0, 0, 0]
#| view.blit_to(dst)
#| assert_true(dst == [2, 3, 4, 0, 0])
#|}
#|test "ArrayView::blit_to/with_offset" {
#| let src = [1, 2, 3, 4, 5]
#| let view = src[1:4] // view = [2, 3, 4]
#| let dst = [0, 0, 0, 0, 0]
#| view.blit_to(dst, dst_offset=2)
#| assert_true(dst == [0, 0, 2, 3, 4])
#|}
#|test "ArrayView::blit_to/grow_destination" {
#| let src = [1, 2, 3, 4, 5]
#| let view = src[1:4] // view = [2, 3, 4]
#| let dst = [0, 0]
#| view.blit_to(dst, dst_offset=1)
#| assert_true(dst == [0, 2, 3, 4])
#|}
#|test "ArrayView::blit_to/empty_view" {
#| let src = [1, 2, 3, 4, 5]
#| let view = src[2:2] // empty view
#| let dst = [0, 0, 0]
#| view.blit_to(dst)
#| assert_true(dst == [0, 0, 0])
#|}
#|test "ArrayView::blit_to/nested_view" {
#| let src = [1, 2, 3, 4, 5, 6, 7]
#| let view1 = src[1:6] // [2, 3, 4, 5, 6]
#| let view2 = view1[1:4] // [3, 4, 5]
#| let dst = [0, 0, 0, 0, 0]
#| view2.blit_to(dst, dst_offset=1)
#| assert_true(dst == [0, 3, 4, 5, 0])
#|}
#|test "panic ArrayView::blit_to/invalid_offset" {
#| let src = [1, 2, 3, 4, 5]
#| let view = src[1:4]
#| let dst = [0, 0, 0]
#| ignore(view.blit_to(dst, dst_offset=-1))
#|}
#|test "panic ArrayView::blit_to/offset_exceeds_length" {
#| let src = [1, 2, 3, 4, 5]
#| let view = src[1:4]
#| let dst = [0, 0, 0]
#| ignore(view.blit_to(dst, dst_offset=4))
#|}
),
"array_conversion.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|#owned(self)
#|fn[T] ArrayView::copy_or_reuse_buffer(
#| self : ArrayView[T],
#|) -> UninitializedArray[T] {
#| let len = self.length()
#| let start = self.start()
#| let buf = self.buf()
#| if start == 0 && len == buf.length() && unsafe_is_unique_ref(buf) {
#| return buf
#| }
#| UninitializedArray::unsafe_make_and_blit(buf, len, start, 0, len)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn[T] UninitializedArray::unsafe_to_fixed_array(
#| self : UninitializedArray[T],
#|) -> FixedArray[T] = "%identity"
),
"array_sort.mbt": (
#|pub fn[T] MutArrayView::sort_by(self : Self[T], cmp : (T, T) -> Int32) -> Unit {
#| fixed_quick_sort_by(self, cmp, None, fixed_get_limit(self.length()))
#|}
#|pub fn[T : Compare] MutArrayView::sort(self : MutArrayView[T]) -> Unit {
#| fixed_quick_sort(self, None, fixed_get_limit(self.length()))
#|}
#|pub fn[T : Compare] MutArrayView::stable_sort(self : MutArrayView[T]) -> Unit {
#| timsort(self)
#|}
#|pub fn[T] MutArrayView::stable_sort_by(
#| self : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| timsort_by(self, cmp)
#|}
#|pub fn[T, K : Compare] MutArrayView::sort_by_key(
#| self : Self[T],
#| map : (T) -> K,
#|) -> Unit {
#| fixed_quick_sort_by(
#| self,
#| (a, b) => map(a).compare(map(b)),
#| None,
#| fixed_get_limit(self.length()),
#| )
#|}
#|pub fn[T : Compare] ArrayView::is_sorted(self : ArrayView[T]) -> Bool {
#| for i in 1.. self[i] {
#| break false
#| }
#| } nobreak {
#| true
#| }
#|}
#|pub fn[T : Compare] Array::is_sorted(self : Array[T]) -> Bool {
#| self[:].is_sorted()
#|}
#|pub fn[T : Compare] Array::sort(self : Array[T]) -> Unit {
#| self.mut_view().sort()
#|}
#|pub fn[T, K : Compare] Array::sort_by_key(
#| self : Array[T],
#| map : (T) -> K,
#|) -> Unit {
#| self.mut_view().sort_by_key(map)
#|}
#|pub fn[T] Array::sort_by(self : Array[T], cmp : (T, T) -> Int32) -> Unit {
#| self.mut_view().sort_by(cmp)
#|}
#|pub fn[T : Compare] FixedArray::is_sorted(arr : FixedArray[T]) -> Bool {
#| arr[:].is_sorted()
#|}
#|pub fn[T : Compare] FixedArray::stable_sort(self : FixedArray[T]) -> Unit {
#| self.mut_view().stable_sort()
#|}
#|pub fn[T] FixedArray::stable_sort_by(
#| self : FixedArray[T],
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| self.mut_view().stable_sort_by(cmp)
#|}
#|pub fn[T, K : Compare] FixedArray::sort_by_key(
#| self : FixedArray[T],
#| map : (T) -> K,
#|) -> Unit {
#| self.mut_view().sort_by_key(map)
#|}
#|test "FixedArray::sort_by_key/basic" {
#| let arr : FixedArray[_] = [3, 1, 4, 1, 5]
#| arr.sort_by_key(x => x)
#| assert_true(arr == [1, 1, 3, 4, 5])
#| let arr2 : FixedArray[_] = [3, 1, 4, 1, 5]
#| arr2.sort_by_key(x => -x)
#| assert_true(arr2 == [5, 4, 3, 1, 1])
#|}
#|pub fn[T] FixedArray::sort_by(
#| self : FixedArray[T],
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| self.mut_view().sort_by(cmp)
#|}
#|test "FixedArray::sort_by: basic functionality" {
#| let arr : FixedArray[_] = [5, 3, 2, 4, 1]
#| arr.sort_by((a, b) => a - b)
#| assert_true(arr == [1, 2, 3, 4, 5])
#|}
#|test "FixedArray::sort_by: edge cases" {
#| let empty_arr : FixedArray[Int32] = []
#| empty_arr.sort_by((a, b) => a - b)
#| assert_true(empty_arr == [])
#| let single_element_arr : FixedArray[_] = [1]
#| single_element_arr.sort_by((a, b) => a - b)
#| assert_true(single_element_arr == [1])
#|}
#|test "FixedArray::sort_by: random cases" {
#| let random_arr1 : FixedArray[_] = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
#| random_arr1.sort_by((a, b) => a - b)
#| assert_true(random_arr1 == [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9])
#| let random_arr2 : FixedArray[_] = [7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9]
#| random_arr2.sort_by((a, b) => a - b)
#| assert_true(random_arr2 == [1, 1, 2, 2, 4, 5, 7, 8, 8, 8, 8, 9])
#| let random_arr3 : FixedArray[_] = [10, -1, 0, 10, -1, 0, 10, -1, 0]
#| random_arr3.sort_by((a, b) => a - b)
#| assert_true(random_arr3 == [-1, -1, -1, 0, 0, 0, 10, 10, 10])
#|}
#|test "FixedArray::sort_by: large array" {
#| let large_arr = FixedArray::makei(1000, i => 1000 - i)
#| large_arr.sort_by((a, b) => a - b)
#| let expected = FixedArray::makei(1000, i => i + 1)
#| assert_true(large_arr == expected)
#|}
#|test "FixedArray::sort_by: negative numbers" {
#| let negative_arr : FixedArray[_] = [-5, -3, -2, -4, -1]
#| negative_arr.sort_by((a, b) => a - b)
#| assert_true(negative_arr == [-5, -4, -3, -2, -1])
#|}
#|test "FixedArray::sort_by: mixed positive and negative numbers" {
#| let mixed_arr : FixedArray[_] = [-5, 3, -2, 4, -1]
#| mixed_arr.sort_by((a, b) => a - b)
#| assert_true(mixed_arr == [-5, -2, -1, 3, 4])
#|}
),
"array_sort_by_impl.mbt": (
#|fn[T] fixed_quick_sort_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#| pred : T?,
#| limit : Int32,
#|) -> Unit {
#| let bubble_sort_len = 16
#| for limit = limit, arr = arr, pred = pred, was_partitioned = true, balanced = true {
#| let len = arr.length()
#| if len <= bubble_sort_len {
#| if len >= 2 {
#| fixed_bubble_sort_by(arr, cmp)
#| }
#| return
#| }
#| if limit == 0 {
#| fixed_heap_sort_by(arr, cmp)
#| return
#| }
#| let (pivot_index, likely_sorted) = fixed_choose_pivot_by(arr, cmp)
#| if was_partitioned && balanced && likely_sorted {
#| if fixed_try_bubble_sort_by(arr, cmp) {
#| return
#| }
#| }
#| let (pivot, partitioned) = fixed_partition_by(arr, cmp, pivot_index)
#| let was_partitioned = partitioned
#| let balanced = minimum(pivot, len - pivot) >= len / 8
#| let limit = if !balanced { limit - 1 } else { limit }
#| if pred is Some(p) {
#| if cmp(p, arr.unsafe_get(pivot)) == 0 {
#| let i = for i = pivot; i < len && cmp(p, arr.unsafe_get(i)) == 0; {
#| continue i + 1
#| } nobreak {
#| i
#| }
#| continue limit, arr.slice(i, len), pred, was_partitioned, balanced
#| }
#| }
#| let left = arr.slice(0, pivot)
#| let right = arr.slice(pivot + 1, len)
#| if left.length() < right.length() {
#| fixed_quick_sort_by(left, cmp, pred, limit)
#| continue limit,
#| right,
#| Some(arr.unsafe_get(pivot)),
#| was_partitioned,
#| balanced
#| } else {
#| fixed_quick_sort_by(right, cmp, Some(arr.unsafe_get(pivot)), limit)
#| continue limit, left, pred, was_partitioned, balanced
#| }
#| }
#|}
#|fn[T] fixed_try_bubble_sort_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#|) -> Bool {
#| let max_tries = 8
#| for i in 1.. 0 &&
#| cmp(arr.unsafe_get(j - 1), arr.unsafe_get(j)) > 0; {
#| arr.swap(j, j - 1)
#| continue j - 1, false
#| } nobreak {
#| sorted
#| }
#| if !sorted {
#| let tries = tries + 1
#| if tries > max_tries {
#| break false
#| }
#| continue tries
#| } else {
#| continue tries
#| }
#| } nobreak {
#| true
#| }
#|}
#|fn[T] fixed_bubble_sort_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| for i in 1.. 0 && cmp(arr.unsafe_get(j - 1), arr.unsafe_get(j)) > 0
#| j = j - 1 {
#| arr.swap(j, j - 1)
#| }
#| }
#|}
#|test "try_bubble_sort" {
#| let arr : FixedArray[_] = [8, 7, 6, 5, 4, 3, 2, 1]
#| let sorted = fixed_try_bubble_sort_by(arr.mut_view(), (a, b) => a - b)
#| inspect(sorted, content="true")
#| assert_true(arr == [1, 2, 3, 4, 5, 6, 7, 8])
#|}
#|fn[T] fixed_partition_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#| pivot_index : Int32,
#|) -> (Int32, Bool) {
#| arr.swap(pivot_index, arr.length() - 1)
#| let pivot = arr.unsafe_get(arr.length() - 1)
#| let (i, partitioned) = for
#| j in 0..<(arr.length() - 1)
#| i = 0, partitioned = true {
#| if cmp(arr.unsafe_get(j), pivot) < 0 {
#| if i != j {
#| arr.swap(i, j)
#| continue i + 1, false
#| } else {
#| continue i + 1, partitioned
#| }
#| } else {
#| continue i, partitioned
#| }
#| } nobreak {
#| (i, partitioned)
#| }
#| arr.swap(i, arr.length() - 1)
#| (i, partitioned)
#|}
#|fn[T] fixed_choose_pivot_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#|) -> (Int32, Bool) {
#| let len = arr.length()
#| let use_median_of_medians = 50
#| let max_swaps = 4 * 3
#| let mut swaps = 0
#| let b = len / 4 * 2
#| if len >= 8 {
#| let a = len / 4 * 1
#| let c = len / 4 * 3
#| let sort_2 = (a : Int32, b : Int32) => {
#| if cmp(arr.unsafe_get(a), arr.unsafe_get(b)) > 0 {
#| arr.swap(a, b)
#| swaps += 1
#| }
#| }
#| let sort_3 = (a : Int32, b : Int32, c : Int32) => {
#| sort_2(a, b)
#| sort_2(b, c)
#| sort_2(a, b)
#| }
#| if len > use_median_of_medians {
#| sort_3(a - 1, a, a + 1)
#| sort_3(b - 1, b, b + 1)
#| sort_3(c - 1, c, c + 1)
#| }
#| sort_3(a, b, c)
#| }
#| if swaps == max_swaps {
#| arr.rev_in_place()
#| (len - b - 1, true)
#| } else {
#| (b, swaps == 0)
#| }
#|}
#|fn[T] fixed_heap_sort_by(arr : MutArrayView[T], cmp : (T, T) -> Int32) -> Unit {
#| let len = arr.length()
#| for i in (len / 2)>..0 {
#| fixed_sift_down_by(arr, i, cmp)
#| }
#| for i in len>..1 {
#| arr.swap(0, i)
#| fixed_sift_down_by(arr.slice(0, i), 0, cmp)
#| }
#|}
#|fn[T] fixed_sift_down_by(
#| arr : MutArrayView[T],
#| index : Int32,
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| let len = arr.length()
#| for index = index, child = index * 2 + 1; child < len; {
#| let child = if child + 1 < len &&
#| cmp(arr.unsafe_get(child), arr.unsafe_get(child + 1)) < 0 {
#| child + 1
#| } else {
#| child
#| }
#| if cmp(arr.unsafe_get(index), arr.unsafe_get(child)) >= 0 {
#| return
#| }
#| arr.swap(index, child)
#| continue child, child * 2 + 1
#| }
#|}
#|test "heap_sort" {
#| fixed_test_sort(arr => fixed_heap_sort_by(arr.mut_view(), (a, b) => a - b))
#|}
#|test "bubble_sort" {
#| fixed_test_sort(arr => fixed_bubble_sort_by(arr.mut_view(), (a, b) => a - b))
#|}
#|test "sort_by" {
#| let arr = [5, 1, 3, 4, 2]
#| arr.mut_view().sort_by_key(x => -x)
#| assert_true(arr == [5, 4, 3, 2, 1])
#|}
#|test "sort_by_fallback" {
#| let arr = FixedArray::makei(100, i => if i % 2 == 0 { 1 } else { 0 })
#| arr
#| .mut_view()
#| .sort_by(
#| (a, b) => if a < b { -1 } else { 1 },
#| )
#| assert_true(arr.is_sorted())
#|}
),
"array_sort_impl.mbt": (
#|priv struct TimSortRun {
#| len : Int32
#| start : Int32
#|}
#|fn[T : Compare] timsort(arr : MutArrayView[T]) -> Unit {
#| let max_insertion = 20
#| let len = arr.length()
#| if len <= max_insertion {
#| MutArrayView::insertion_sort(arr)
#| return
#| }
#| let runs : Array[TimSortRun] = []
#| for start = 0, end = 0; end < len; {
#| let (streak_end, was_reversed) = find_streak(arr.mut_view(start~))
#| let end = end + streak_end
#| if was_reversed {
#| arr.mut_view(start~, end~).rev_in_place()
#| }
#| let end = provide_sorted_batch(arr, start, end)
#| runs.push({ start, len: end - start, })
#| while true {
#| guard collapse(runs, len) is Some(r) else { break }
#| let left = runs[r]
#| let right = runs[r + 1]
#| merge(arr.slice(left.start, right.start + right.len), left.len)
#| runs[r + 1] = { start: left.start, len: left.len + right.len, }
#| runs.remove(r) |> ignore
#| }
#| continue end, end
#| }
#|}
#|fn[T : Compare] MutArrayView::insertion_sort(arr : MutArrayView[T]) -> Unit {
#| for i in 1.. 0 && arr.unsafe_get(j) < arr.unsafe_get(j - 1); j = j - 1 {
#| arr.swap(j, j - 1)
#| }
#| }
#|}
#|fn[T : Compare] merge(arr : MutArrayView[T], mid : Int32) -> Unit {
#| let buf_len = arr.length() - mid
#| let buf = UninitializedArray::make_and_blit(
#| arr.buf(),
#| allocate_len=buf_len,
#| src_offset=arr.start() + mid,
#| len=buf_len,
#| )
#| let buf_remaining = for p1 = mid - 1, p2 = buf_len - 1, p = mid + buf_len - 1; p1 >=
#| 0 &&
#| p2 >= 0; {
#| if arr.unsafe_get(p1) > buf[p2] {
#| arr.unsafe_set(p, arr.unsafe_get(p1))
#| continue p1 - 1, p2, p - 1
#| } else {
#| arr.unsafe_set(p, buf[p2])
#| continue p1, p2 - 1, p - 1
#| }
#| } nobreak {
#| p2
#| }
#| if buf_remaining >= 0 {
#| UninitializedArray::unsafe_blit(
#| arr.buf(),
#| arr.start(),
#| buf,
#| 0,
#| buf_remaining + 1,
#| )
#| }
#|}
#|fn[T : Compare] find_streak(arr : MutArrayView[T]) -> (Int32, Bool) {
#| let len = arr.length()
#| if len < 2 {
#| return (len, false)
#| }
#| let assume_reverse = arr.unsafe_get(1) < arr.unsafe_get(0)
#| if assume_reverse {
#| let end = for idx in 2..= arr.unsafe_get(idx - 1) {
#| continue
#| }
#| break idx
#| } nobreak {
#| len
#| }
#| (end, false)
#| }
#|}
#|fn[T : Compare] provide_sorted_batch(
#| arr : MutArrayView[T],
#| start : Int32,
#| end : Int32,
#|) -> Int32 {
#| let len = arr.length()
#| let min_insertion_run = 10
#| let start_end_diff = end - start
#| if start_end_diff < min_insertion_run && end < len {
#| let sort_end = minimum(len, start + min_insertion_run)
#| MutArrayView::insertion_sort(arr.slice(start, sort_end))
#| sort_end
#| } else {
#| end
#| }
#|}
#|fn collapse(runs : Array[TimSortRun], stop : Int32) -> Int32? {
#| let n : Int32 = runs.length()
#| if n >= 2 &&
#| (
#| runs[n - 1].start + runs[n - 1].len == stop ||
#| runs[n - 2].len <= runs[n - 1].len ||
#| (n >= 3 && runs[n - 3].len <= runs[n - 2].len + runs[n - 1].len) ||
#| (n >= 4 && runs[n - 4].len <= runs[n - 3].len + runs[n - 2].len)
#| ) {
#| if n >= 3 && runs[n - 3].len < runs[n - 1].len {
#| Some(n - 3)
#| } else {
#| Some(n - 2)
#| }
#| } else {
#| None
#| }
#|}
#|pub fn[T : Compare] FixedArray::sort(self : FixedArray[T]) -> Unit {
#| self.mut_view().sort()
#|}
#|fn[T] MutArrayView::slice(
#| arr : MutArrayView[T],
#| start : Int32,
#| end : Int32,
#|) -> MutArrayView[T] {
#| arr.mut_view(start~, end~)
#|}
#|fn[T] MutArrayView::swap(arr : MutArrayView[T], i : Int32, j : Int32) -> Unit {
#| let temp = arr.unsafe_get(i)
#| arr.unsafe_set(i, arr.unsafe_get(j))
#| arr.unsafe_set(j, temp)
#|}
#|fn[T] MutArrayView::rev_in_place(arr : MutArrayView[T]) -> Unit {
#| let len = arr.length()
#| let mid_len = len / 2
#| for i in 0.. Unit {
#| let bubble_sort_len = 16
#| for limit = limit, arr = arr, pred = pred, was_partitioned = true, balanced = true {
#| let len = arr.length()
#| if len <= bubble_sort_len {
#| if len >= 2 {
#| fixed_bubble_sort(arr)
#| }
#| return
#| }
#| if limit == 0 {
#| fixed_heap_sort(arr)
#| return
#| }
#| let (pivot_index, likely_sorted) = fixed_choose_pivot(arr)
#| if was_partitioned && balanced && likely_sorted {
#| if fixed_try_bubble_sort(arr) {
#| return
#| }
#| }
#| let (pivot, partitioned) = fixed_partition(arr, pivot_index)
#| let was_partitioned = partitioned
#| let balanced = minimum(pivot, len - pivot) >= len / 8
#| let limit = if !balanced { limit - 1 } else { limit }
#| if pred is Some(p) {
#| if p == arr.unsafe_get(pivot) {
#| let i = for i = pivot; i < len && p == arr.unsafe_get(i); {
#| continue i + 1
#| } nobreak {
#| i
#| }
#| continue limit, arr.slice(i, len), pred, was_partitioned, balanced
#| }
#| }
#| let left = arr.slice(0, pivot)
#| let right = arr.slice(pivot + 1, len)
#| if left.length() < right.length() {
#| fixed_quick_sort(left, pred, limit)
#| continue limit,
#| right,
#| Some(arr.unsafe_get(pivot)),
#| was_partitioned,
#| balanced
#| } else {
#| fixed_quick_sort(right, Some(arr.unsafe_get(pivot)), limit)
#| continue limit, left, pred, was_partitioned, balanced
#| }
#| }
#|}
#|fn fixed_get_limit(len : Int32) -> Int32 {
#| for len = len, limit = 0; len > 0; {
#| continue len / 2, limit + 1
#| } nobreak {
#| limit
#| }
#|}
#|fn[T : Compare] fixed_try_bubble_sort(arr : MutArrayView[T]) -> Bool {
#| let max_tries = 8
#| for i in 1.. 0 &&
#| arr.unsafe_get(j - 1) > arr.unsafe_get(j); {
#| arr.swap(j, j - 1)
#| continue j - 1, false
#| } nobreak {
#| sorted
#| }
#| if !sorted {
#| let tries = tries + 1
#| if tries > max_tries {
#| break false
#| }
#| continue tries
#| } else {
#| continue tries
#| }
#| } nobreak {
#| true
#| }
#|}
#|fn[T : Compare] fixed_bubble_sort(arr : MutArrayView[T]) -> Unit {
#| for i in 1.. 0 && arr.unsafe_get(j - 1) > arr.unsafe_get(j); j = j - 1 {
#| arr.swap(j, j - 1)
#| }
#| }
#|}
#|test "fixed_try_bubble_sort" {
#| let arr : FixedArray[_] = [8, 7, 6, 5, 4, 3, 2, 1]
#| let sorted = fixed_try_bubble_sort(arr.mut_view())
#| inspect(sorted, content="true")
#| assert_true(arr == [1, 2, 3, 4, 5, 6, 7, 8])
#|}
#|fn[T : Compare] fixed_partition(
#| arr : MutArrayView[T],
#| pivot_index : Int32,
#|) -> (Int32, Bool) {
#| arr.swap(pivot_index, arr.length() - 1)
#| let pivot = arr.unsafe_get(arr.length() - 1)
#| let (i, partitioned) = for
#| j in 0..<(arr.length() - 1)
#| i = 0, partitioned = true {
#| if arr.unsafe_get(j) < pivot {
#| if i != j {
#| arr.swap(i, j)
#| continue i + 1, false
#| } else {
#| continue i + 1, partitioned
#| }
#| } else {
#| continue i, partitioned
#| }
#| } nobreak {
#| (i, partitioned)
#| }
#| arr.swap(i, arr.length() - 1)
#| (i, partitioned)
#|}
#|fn[T : Compare] fixed_choose_pivot(arr : MutArrayView[T]) -> (Int32, Bool) {
#| let len = arr.length()
#| let use_median_of_medians = 50
#| let max_swaps = 4 * 3
#| let mut swaps = 0
#| let b = len / 4 * 2
#| if len >= 8 {
#| let a = len / 4 * 1
#| let c = len / 4 * 3
#| let sort_2 = (a : Int32, b : Int32) => {
#| if arr.unsafe_get(a) > arr.unsafe_get(b) {
#| arr.swap(a, b)
#| swaps += 1
#| }
#| }
#| let sort_3 = (a : Int32, b : Int32, c : Int32) => {
#| sort_2(a, b)
#| sort_2(b, c)
#| sort_2(a, b)
#| }
#| if len > use_median_of_medians {
#| sort_3(a - 1, a, a + 1)
#| sort_3(b - 1, b, b + 1)
#| sort_3(c - 1, c, c + 1)
#| }
#| sort_3(a, b, c)
#| }
#| if swaps == max_swaps {
#| arr.rev_in_place()
#| (len - b - 1, true)
#| } else {
#| (b, swaps == 0)
#| }
#|}
#|fn[T : Compare] fixed_heap_sort(arr : MutArrayView[T]) -> Unit {
#| let len = arr.length()
#| for i in (len / 2)>..0 {
#| fixed_sift_down(arr, i)
#| }
#| for i in len>..1 {
#| arr.swap(0, i)
#| fixed_sift_down(arr.slice(0, i), 0)
#| }
#|}
#|fn[T : Compare] fixed_sift_down(arr : MutArrayView[T], index : Int32) -> Unit {
#| let len = arr.length()
#| for index = index, child = index * 2 + 1; child < len; {
#| let child = if child + 1 < len &&
#| arr.unsafe_get(child) < arr.unsafe_get(child + 1) {
#| child + 1
#| } else {
#| child
#| }
#| if arr.unsafe_get(index) >= arr.unsafe_get(child) {
#| return
#| }
#| arr.swap(index, child)
#| continue child, child * 2 + 1
#| }
#|}
#|fn fixed_test_sort(f : (MutArrayView[Int32]) -> Unit) -> Unit raise {
#| let arr : FixedArray[_] = [5, 4, 3, 2, 1]
#| f(arr.mut_view())
#| assert_true(arr == [1, 2, 3, 4, 5])
#| let arr : FixedArray[_] = [5, 5, 5, 5, 1]
#| f(arr.mut_view())
#| assert_true(arr == [1, 5, 5, 5, 5])
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| f(arr.mut_view())
#| assert_true(arr == [1, 2, 3, 4, 5])
#| let arr = FixedArray::make(1000, 0)
#| for i in 0..<1000 {
#| arr[i] = 1000 - i - 1
#| }
#| for step in 1..<100 {
#| let i = step * 10
#| arr.swap(i, i - 1)
#| }
#| f(arr.mut_view())
#| let expected = FixedArray::make(1000, 0)
#| for i in 0..<1000 {
#| expected[i] = i
#| }
#| assert_true(arr == expected)
#|}
#|test "fixed_heap_sort" {
#| fixed_test_sort(arr => fixed_heap_sort(arr.mut_view()))
#|}
#|test "fixed_bubble_sort" {
#| fixed_test_sort(arr => fixed_bubble_sort(arr.mut_view()))
#|}
#|test "sort" {
#| fixed_test_sort(arr => arr.sort())
#|}
#|test "stable_sort" {
#| let arr : FixedArray[_] = [5, 1, 3, 4, 2]
#| arr.mut_view().stable_sort()
#| assert_true(arr == [1, 2, 3, 4, 5])
#| let arr = FixedArray::make(1000, 0)
#| for i in 0..<1000 {
#| arr[i] = 1000 - i - 1
#| }
#| for step in 1..<100 {
#| let i = step * 10
#| arr.swap(i, i - 1)
#| }
#| arr.mut_view().stable_sort()
#| let expected = FixedArray::make(1000, 0)
#| for i in 0..<1000 {
#| expected[i] = i
#| }
#| assert_true(arr == expected)
#|}
#|test "stable_sort_complex" {
#| let run_lens = [86, 64, 21, 20, 22]
#| let total_len = run_lens.fold(init=0, (acc, x) => acc + x)
#| let arr = FixedArray::make(total_len, 0)
#| for i in 0.. Int32 {
#| if x > y {
#| y
#| } else {
#| x
#| }
#|}
),
"array_stable_sort_by_impl.mbt": (
#|fn[T] timsort_by(arr : MutArrayView[T], cmp : (T, T) -> Int32) -> Unit {
#| let max_insertion = 20
#| let len = arr.length()
#| if len <= max_insertion {
#| MutArrayView::insertion_sort_by(arr, cmp)
#| return
#| }
#| let runs : Array[TimSortRun] = []
#| for start = 0, end = 0; end < len; {
#| let (streak_end, was_reversed) = find_streak_by(arr.mut_view(start~), cmp)
#| let end = end + streak_end
#| if was_reversed {
#| arr.mut_view(start~, end~).rev_in_place()
#| }
#| let end = provide_sorted_batch_by(arr, start, end, cmp)
#| runs.push({ start, len: end - start, })
#| while true {
#| guard collapse(runs, len) is Some(r) else { break }
#| let left = runs[r]
#| let right = runs[r + 1]
#| merge_by(arr.slice(left.start, right.start + right.len), left.len, cmp)
#| runs[r + 1] = { start: left.start, len: left.len + right.len, }
#| runs.remove(r) |> ignore
#| }
#| continue end, end
#| }
#|}
#|fn[T] MutArrayView::insertion_sort_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| for i in 1.. 0 && cmp(arr.unsafe_get(j), arr.unsafe_get(j - 1)) < 0
#| j = j - 1 {
#| arr.swap(j, j - 1)
#| }
#| }
#|}
#|fn[T] merge_by(
#| arr : MutArrayView[T],
#| mid : Int32,
#| cmp : (T, T) -> Int32,
#|) -> Unit {
#| let buf_len = arr.length() - mid
#| let buf = UninitializedArray::make_and_blit(
#| arr.buf(),
#| allocate_len=buf_len,
#| src_offset=arr.start() + mid,
#| len=buf_len,
#| )
#| let buf_remaining = for p1 = mid - 1, p2 = buf_len - 1, p = mid + buf_len - 1; p1 >=
#| 0 &&
#| p2 >= 0; {
#| if cmp(arr.unsafe_get(p1), buf[p2]) > 0 {
#| arr.unsafe_set(p, arr.unsafe_get(p1))
#| continue p1 - 1, p2, p - 1
#| } else {
#| arr.unsafe_set(p, buf[p2])
#| continue p1, p2 - 1, p - 1
#| }
#| } nobreak {
#| p2
#| }
#| if buf_remaining >= 0 {
#| UninitializedArray::unsafe_blit(
#| arr.buf(),
#| arr.start(),
#| buf,
#| 0,
#| buf_remaining + 1,
#| )
#| }
#|}
#|fn[T] find_streak_by(
#| arr : MutArrayView[T],
#| cmp : (T, T) -> Int32,
#|) -> (Int32, Bool) {
#| let len = arr.length()
#| if len < 2 {
#| return (len, false)
#| }
#| let assume_reverse = cmp(arr.unsafe_get(1), arr.unsafe_get(0)) < 0
#| if assume_reverse {
#| let end = for idx in 2..= 0 {
#| continue
#| }
#| break idx
#| } nobreak {
#| len
#| }
#| (end, false)
#| }
#|}
#|fn[T] provide_sorted_batch_by(
#| arr : MutArrayView[T],
#| start : Int32,
#| end : Int32,
#| cmp : (T, T) -> Int32,
#|) -> Int32 {
#| let len = arr.length()
#| let min_insertion_run = 10
#| if end - start < min_insertion_run && end < len {
#| let sort_end = minimum(len, start + min_insertion_run)
#| MutArrayView::insertion_sort_by(arr.slice(start, sort_end), cmp)
#| sort_end
#| } else {
#| end
#| }
#|}
),
"arrayview.mbt": (
#|#builtin.valtype
#|type ArrayView[T]
#|fn[T] ArrayView::buf(self : ArrayView[T]) -> UninitializedArray[T] = "%arrayview.buf"
#|fn[T] ArrayView::start(self : ArrayView[T]) -> Int32 = "%arrayview.start"
#|fn[T] ArrayView::end(self : ArrayView[T]) -> Int32 = "%arrayview.end"
#|#intrinsic("%arrayview.length")
#|pub fn[T] ArrayView::length(self : ArrayView[T]) -> Int32 {
#| self.end() - self.start()
#|}
#|pub fn[T] ArrayView::is_empty(self : ArrayView[T]) -> Bool {
#| self.length() == 0
#|}
#|pub fn[T] ArrayView::start_offset(self : Self[T]) -> Int32 {
#| self.start()
#|}
#|#intrinsic("%arrayview.get")
#|#alias("_[_]")
#|pub fn[T] ArrayView::at(self : ArrayView[T], index : Int32) -> T {
#| guard index >= 0 && index < self.length() else {
#| index_out_of_bounds(self.length(), index)
#| }
#| self.buf().unsafe_get(self.start() + index)
#|}
#|#intrinsic("%arrayview.get_opt")
#|pub fn[T] ArrayView::get(self : ArrayView[T], index : Int32) -> T? {
#| let len = self.length()
#| guard index >= 0 && index < len else { None }
#| Some(self.buf().unsafe_get(self.start() + index))
#|}
#|pub fn[T] ArrayView::last(self : ArrayView[T]) -> T? {
#| let len = self.length()
#| if len == 0 {
#| None
#| } else {
#| Some(self.unsafe_get(len - 1))
#| }
#|}
#|#intrinsic("%arrayview.unsafe_get")
#|#internal(unsafe, "Panic if index is out of bounds")
#|#doc(hidden)
#|pub fn[T] ArrayView::unsafe_get(self : ArrayView[T], index : Int32) -> T {
#| self.buf()[self.start() + index]
#|}
#|#intrinsic("%array.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn[T] Array::exact_view(
#| self : Array[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| self.buffer().unsafe_view(start~, end~)
#|}
#|#intrinsic("%array.view_opt")
#|pub fn[T] Array::get_view(
#| self : Array[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T]? {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else { None }
#| Some(self.buffer().unsafe_view(start~, end~))
#|}
#|#intrinsic("%arrayview.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn[T] ArrayView::exact_view(
#| self : ArrayView[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| self.buf().unsafe_view(start=self.start() + start, end=self.start() + end)
#|}
#|#intrinsic("%arrayview.view_opt")
#|pub fn[T] ArrayView::get_view(
#| self : ArrayView[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T]? {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else { None }
#| Some(
#| self.buf().unsafe_view(start=self.start() + start, end=self.start() + end),
#| )
#|}
#|#intrinsic("%array.clamped_view")
#|#alias("_[_:_]")
#|pub fn[T] Array::clamped_view(
#| self : Array[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| self.buffer().unsafe_view(start=lo, end=hi)
#|}
#|#intrinsic("%arrayview.clamped_view")
#|#alias("_[_:_]")
#|pub fn[T] ArrayView::clamped_view(
#| self : ArrayView[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| self.buf().unsafe_view(start=self.start() + lo, end=self.start() + hi)
#|}
#|fn[T] unsafe_cast_fixedarray_to_uninitializedarray(
#| arr : FixedArray[T],
#|) -> UninitializedArray[T] = "%identity"
#|#intrinsic("%fixedarray.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn[T] FixedArray::exact_view(
#| self : FixedArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| unsafe_cast_fixedarray_to_uninitializedarray(self).unsafe_view(start~, end~)
#|}
#|#intrinsic("%fixedarray.view_opt")
#|pub fn[T] FixedArray::get_view(
#| self : FixedArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T]? {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else { None }
#| Some(
#| unsafe_cast_fixedarray_to_uninitializedarray(self).unsafe_view(start~, end~),
#| )
#|}
#|#intrinsic("%fixedarray.clamped_view")
#|#alias("_[_:_]")
#|pub fn[T] FixedArray::clamped_view(
#| self : FixedArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| unsafe_cast_fixedarray_to_uninitializedarray(self).unsafe_view(
#| start=lo,
#| end=hi,
#| )
#|}
#|pub fn[T] ArrayView::suffixes(
#| self : Self[T],
#| include_empty~ : Bool = false,
#|) -> Iter[ArrayView[T]] {
#| let len = self.length()
#| let mut i = 0
#| Iter(
#| fn() -> ArrayView[T]? {
#| if i < len {
#| let suffix = self[i:]
#| i += 1
#| Some(suffix)
#| } else if i == len {
#| i += 1
#| if include_empty {
#| Some(self[len:])
#| } else {
#| None
#| }
#| } else {
#| None
#| }
#| },
#| size_hint=if include_empty { len + 1 } else { len },
#| )
#|}
#|pub fn[T] ArrayView::chunks(
#| self : ArrayView[T],
#| size : Int32,
#|) -> Array[ArrayView[T]] {
#| guard! size > 0
#| let len = self.length()
#| if len == 0 {
#| return []
#| }
#| let num_chunks = (len - 1) / size + 1
#| Array::makei(num_chunks, i => {
#| let start = i * size
#| let end = start + Int32::min(size, len - start)
#| self[start:end]
#| })
#|}
#|#locals(pred)
#|pub fn[T] ArrayView::chunk_by(
#| self : ArrayView[T],
#| pred : (T, T) -> Bool raise?,
#|) -> Array[ArrayView[T]] raise? {
#| let chunks = []
#| if self.is_empty() {
#| return chunks
#| }
#| let start = for i in 1.. Array[ArrayView[T]] {
#| guard! size > 0
#| let len = self.length() - size + 1
#| if len < 1 {
#| return []
#| }
#| Array::makei(len, i => self[i:i + size])
#|}
#|pub fn[A, B] ArrayView::zip(
#| self : ArrayView[A],
#| other : ArrayView[B],
#|) -> Array[(A, B)] {
#| let length = Int32::min(self.length(), other.length())
#| Array::makei(length, i => (self.unsafe_get(i), other.unsafe_get(i)))
#|}
#|pub fn[A, B] ArrayView::zip_iter2(
#| self : ArrayView[A],
#| other : ArrayView[B],
#|) -> Iter2[A, B] {
#| let length = Int32::min(self.length(), other.length())
#| let mut i = 0
#| Iter(
#| () => {
#| guard i < length else { None }
#| let elem = (self.unsafe_get(i), other.unsafe_get(i))
#| i += 1
#| Some(elem)
#| },
#| size_hint=length,
#| )
#|}
#|pub fn[T1, T2] ArrayView::unzip(
#| self : ArrayView[(T1, T2)],
#|) -> (Array[T1], Array[T2]) {
#| let arr1 : Array[T1] = Array(capacity=self.length())
#| let arr2 : Array[T2] = Array(capacity=self.length())
#| for pair in self {
#| let (x, y) = pair
#| arr1.push(x)
#| arr2.push(y)
#| }
#| (arr1, arr2)
#|}
#|#alias(iterator, deprecated)
#|pub fn[X] ArrayView::iter(self : ArrayView[X]) -> Iter[X] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let elem = self.unsafe_get(i)
#| i += 1
#| Some(elem)
#| },
#| size_hint=len,
#| )
#|}
#|#alias(rev_iterator, deprecated)
#|pub fn[X] ArrayView::rev_iter(self : ArrayView[X]) -> Iter[X] {
#| let len = self.length()
#| let mut i = len
#| Iter(
#| fn() {
#| guard i > 0 else { None }
#| i -= 1
#| Some(self.unsafe_get(i))
#| },
#| size_hint=len,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[X] ArrayView::iter2(self : ArrayView[X]) -> Iter2[Int32, X] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let result = Some((i, self.unsafe_get(i)))
#| i += 1
#| result
#| },
#| size_hint=len,
#| )
#|}
#|#locals(f)
#|pub fn[T] ArrayView::each(
#| self : ArrayView[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| for v in self {
#| f(v)
#| }
#|}
#|#locals(f)
#|pub fn[T] ArrayView::eachi(
#| self : ArrayView[T],
#| f : (Int32, T) -> Unit raise?,
#|) -> Unit raise? {
#| for i, v in self {
#| f(i, v)
#| }
#|}
#|#locals(f)
#|pub fn[T] ArrayView::rev_each(
#| self : ArrayView[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| let len = self.length()
#| for i in 0.. Unit raise?,
#|) -> Unit raise? {
#| let len = self.length()
#| for i in 0.. Bool raise?,
#|) -> Bool raise? {
#| for v in self {
#| if !f(v) {
#| return false
#| }
#| }
#| true
#|}
#|#locals(f)
#|#alias(exists)
#|pub fn[T] ArrayView::any(
#| self : ArrayView[T],
#| f : (T) -> Bool raise?,
#|) -> Bool raise? {
#| for v in self {
#| if f(v) {
#| return true
#| }
#| }
#| false
#|}
#|pub fn[T : Eq] ArrayView::contains(self : ArrayView[T], value : T) -> Bool {
#| for v in self {
#| if v == value {
#| break true
#| }
#| } nobreak {
#| false
#| }
#|}
#|pub fn[T : Eq] ArrayView::count(self : ArrayView[T], value : T) -> Int32 {
#| for v in self; count = 0 {
#| if v == value {
#| continue count + 1
#| }
#| continue count
#| } nobreak {
#| count
#| }
#|}
#|#locals(f)
#|pub fn[T] ArrayView::count_if(
#| self : ArrayView[T],
#| f : (T) -> Bool raise?,
#|) -> Int32 raise? {
#| for v in self; count = 0 {
#| if f(v) {
#| continue count + 1
#| }
#| continue count
#| } nobreak {
#| count
#| }
#|}
#|pub fn[T : Eq] ArrayView::search(self : ArrayView[T], value : T) -> Int32? {
#| for i, x in self {
#| if x == value {
#| break Some(i)
#| }
#| } nobreak {
#| None
#| }
#|}
#|#locals(f)
#|pub fn[T] ArrayView::search_by(
#| self : ArrayView[T],
#| f : (T) -> Bool raise?,
#|) -> Int32? raise? {
#| for i, v in self {
#| if f(v) {
#| break Some(i)
#| }
#| } nobreak {
#| None
#| }
#|}
#|#alias(starts_with, deprecated="Use `has_prefix` instead")
#|pub fn[T : Eq] ArrayView::has_prefix(
#| self : ArrayView[T],
#| prefix : ArrayView[T],
#|) -> Bool {
#| if prefix.length() > self.length() {
#| return false
#| }
#| for i in 0.. Bool {
#| let suffix_len = suffix.length()
#| let self_len = self.length()
#| if suffix_len > self_len {
#| return false
#| }
#| for i in 0.. ArrayView[T]? {
#| if self.has_prefix(prefix) {
#| Some(self[prefix.length():])
#| } else {
#| None
#| }
#|}
#|pub fn[T : Eq] ArrayView::strip_suffix(
#| self : ArrayView[T],
#| suffix : ArrayView[T],
#|) -> ArrayView[T]? {
#| if self.has_suffix(suffix) {
#| Some(self[:self.length() - suffix.length()])
#| } else {
#| None
#| }
#|}
#|pub fn[T : Compare] ArrayView::binary_search(
#| self : ArrayView[T],
#| value : T,
#|) -> Result[Int32, Int32] {
#| let len = self.length()
#| for i = 0, j = len; i < j; {
#| let h = i + (j - i) / 2
#| if self.unsafe_get(h) < value {
#| continue h + 1, j
#| } else {
#| continue i, h
#| }
#| } nobreak {
#| if i < len && self.unsafe_get(i) == value {
#| Ok(i)
#| } else {
#| Err(i)
#| }
#| }
#|}
#|#locals(cmp)
#|pub fn[T] ArrayView::binary_search_by(
#| self : ArrayView[T],
#| cmp : (T) -> Int32 raise?,
#|) -> Result[Int32, Int32] raise? {
#| let len = self.length()
#| for i = 0, j = len; i < j; {
#| let h = i + (j - i) / 2
#| if cmp(self.unsafe_get(h)) < 0 {
#| continue h + 1, j
#| } else {
#| continue i, h
#| }
#| } nobreak {
#| if i < len && cmp(self.unsafe_get(i)) == 0 {
#| Ok(i)
#| } else {
#| Err(i)
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, B] ArrayView::fold(
#| self : ArrayView[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| for x in self; acc = init {
#| continue f(acc, x)
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|pub fn[A, B] ArrayView::rev_fold(
#| self : ArrayView[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| for i = self.length() - 1, acc = init; i >= 0; {
#| continue i - 1, f(acc, self.unsafe_get(i))
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|pub fn[A, B] ArrayView::foldi(
#| self : ArrayView[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| for i, x in self; acc = init {
#| continue f(i, acc, x)
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|pub fn[A, B] ArrayView::rev_foldi(
#| self : ArrayView[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| let len = self.length()
#| for i in len>..0; acc = init {
#| continue f(len - i - 1, acc, self.unsafe_get(i))
#| } nobreak {
#| acc
#| }
#|}
#|#locals(f)
#|pub fn[T, U] ArrayView::map(
#| self : ArrayView[T],
#| f : (T) -> U raise?,
#|) -> Array[U] raise? {
#| let arr = Array::unsafe_make_uninit(self.length())
#| for i, x in self {
#| arr.unsafe_set(i, f(x))
#| }
#| arr
#|}
#|#locals(f)
#|pub fn[T, U] ArrayView::mapi(
#| self : ArrayView[T],
#| f : (Int32, T) -> U raise?,
#|) -> Array[U] raise? {
#| let arr = Array::unsafe_make_uninit(self.length())
#| for i, x in self {
#| arr.unsafe_set(i, f(i, x))
#| }
#| arr
#|}
#|#locals(f)
#|pub fn[T] ArrayView::filter(
#| self : ArrayView[T],
#| f : (T) -> Bool raise?,
#|) -> Array[T] raise? {
#| [
#| for v in self if f(v) => v
#| ]
#|}
#|#locals(f)
#|pub fn[A, B] ArrayView::filter_map(
#| self : ArrayView[A],
#| f : (A) -> B? raise?,
#|) -> Array[B] raise? {
#| [
#| for x in self if f(x) is Some(y) => y
#| ]
#|}
#|#cfg(any(target="native", target="wasm"))
#|#alias(to_array, deprecated)
#|#owned(self)
#|pub fn[T] ArrayView::to_owned(self : ArrayView[T]) -> Array[T] {
#| let len = self.length()
#| if len == 0 {
#| []
#| } else {
#| { buf: self.copy_or_reuse_buffer(), len, }
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#alias(to_array, deprecated)
#|pub fn[T] ArrayView::to_owned(self : ArrayView[T]) -> Array[T] {
#| let len = self.length()
#| if len == 0 {
#| []
#| } else {
#| Array::unsafe_make_and_blit(
#| self.buf(),
#| allocate_len=len,
#| src_offset=self.start(),
#| len~,
#| )
#| }
#|}
#|pub fn[A : ToStringView] ArrayView::join(
#| self : ArrayView[A],
#| separator : StringView,
#|) -> String {
#| join_string_views(self, separator)
#|}
#|pub fn[T : Compare] ArrayView::lexical_compare(
#| self : ArrayView[T],
#| other : ArrayView[T],
#|) -> Int32 {
#| let self_len = self.length()
#| let other_len = other.length()
#| let min_len = if self_len < other_len { self_len } else { other_len }
#| for i in 0.. Array[T] {
#| let len = self.length()
#| let arr = Array::unsafe_make_uninit(len)
#| for i in 0.. Bool {
#| if self.length() != other.length() {
#| return false
#| }
#| for i, x in self {
#| if !(x == other.unsafe_get(i)) {
#| return false
#| }
#| } nobreak {
#| true
#| }
#|}
#|pub impl[T : Compare] Compare for ArrayView[T] with fn compare(self, other) -> Int32 {
#| let len_self = self.length()
#| let len_other = other.length()
#| let cmp = len_self.compare(len_other)
#| guard cmp == 0 else { return cmp }
#| for i, x in self {
#| let cmp = x.compare(other.unsafe_get(i))
#| guard cmp == 0 else { break cmp }
#| } nobreak {
#| 0
#| }
#|}
#|pub impl[A : Hash] Hash for ArrayView[A] with fn hash_combine(self, hasher) {
#| for e in self {
#| hasher.combine(e)
#| }
#|}
#|pub impl[T] Add for ArrayView[T] with fn add(self, other) {
#| let len_self = self.length()
#| let len_other = other.length()
#| if len_self == 0 {
#| Array::unsafe_make_and_blit(
#| other.buf(),
#| allocate_len=len_other,
#| src_offset=other.start(),
#| len=len_other,
#| )
#| } else {
#| let result = Array::unsafe_make_and_blit(
#| self.buf(),
#| allocate_len=len_self + len_other,
#| src_offset=self.start(),
#| len=len_self,
#| )
#| UninitializedArray::unsafe_blit(
#| result.buffer(),
#| len_self,
#| other.buf(),
#| other.start(),
#| len_other,
#| )
#| result
#| }
#|}
),
"assert.mbt": (
#|#callsite(autofill(loc))
#|#coverage.skip
#|pub fn assert_true(x : Bool, msg? : StringView, loc~ : SourceLoc) -> Unit raise {
#| if !x {
#| let fail_msg : StringView = match msg {
#| Some(msg) | (None with msg = "`\{x}` is not true") => msg
#| }
#| fail(fail_msg, loc~)
#| }
#|}
#|#callsite(autofill(loc))
#|#coverage.skip
#|pub fn assert_false(
#| x : Bool,
#| msg? : StringView,
#| loc~ : SourceLoc,
#|) -> Unit raise {
#| if x {
#| let fail_msg : StringView = match msg {
#| Some(msg) | (None with msg = "`\{x}` is not false") => msg
#| }
#| fail(fail_msg, loc~)
#| }
#|}
),
"assert_release.mbt": (
#|pub fn debug_assert(x : () -> Bool) -> Unit = "%ignore"
),
"autoloc.mbt": (
#|pub(all) type SourceLoc
#|fn SourceLoc::repr(self : Self) -> String = "%loc_to_string"
#|pub impl Show for SourceLoc with fn output(self, logger) {
#| logger.write_string(self.repr())
#|}
#|pub impl Show for SourceLoc with fn to_string(self) {
#| self.repr()
#|}
#|priv struct SourceLocRepr {
#| filename : StringView
#| start_line : StringView
#| start_column : StringView
#| end_line : StringView
#| end_column : StringView
#|}
#|fn SourceLocRepr::parse(repr : String) -> SourceLocRepr {
#| lexmatch repr[:] with longest {
#| (re"^.+" as filename) +
#| re":" +
#| (re"[[:digit:]]+" as start_line) +
#| re":" +
#| (re"[[:digit:]]+" as start_column) +
#| re"-" +
#| (re"[[:digit:]]+" as end_line) +
#| re":" +
#| (re"[[:digit:]]+" as end_column) +
#| re"@" +
#| (re"[^:]*$" as _module) =>
#| { filename, start_line, start_column, end_line, end_column, }
#| _ => panic()
#| }
#|}
#|fn SourceLocRepr::to_json_string(self : SourceLocRepr) -> String {
#| (
#| $|{"filename":\{sb => self.filename.escape_to(sb)},"start_line":\{self.start_line},"start_column":\{self.start_column},"end_line":\{self.end_line},"end_column":\{self.end_column}}
#| )
#|}
#|pub fn SourceLoc::to_json_string(self : SourceLoc) -> String {
#| SourceLocRepr::parse(self.repr()).to_json_string()
#|}
#|pub(all) struct ArgsLoc(Array[SourceLoc?])
#|#warnings("-deprecated")
#|pub impl Show for ArgsLoc with fn output(self, logger) {
#| let ArgsLoc(arr) = self
#| Show::output(arr, logger)
#|}
#|pub fn ArgsLoc::to_json(self : ArgsLoc) -> String {
#| let buf = StringBuilder(capacity=10)
#| let ArgsLoc(self) = self
#| buf.write_char('[')
#| for i, item in self {
#| if i != 0 {
#| buf <+ ", "
#| }
#| match item {
#| None => buf <+ "null"
#| Some(loc) => buf.write_string(loc.to_json_string())
#| }
#| }
#| buf.write_char(']')
#| buf.to_string()
#|}
),
"bitstring.mbt": (
#|fn UInt32::extend_sign(self : UInt32, len : Int32) -> Int32 {
#| let b = 32 - len
#| self.reinterpret_as_int() << b >> b
#|}
#|fn UInt64::extend_sign(self : UInt64, len : Int32) -> Int64 {
#| let b = 64 - len
#| self.reinterpret_as_int64() << b >> b
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_bit(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| _len : Int32,
#|) -> UInt32 {
#| let byte_index = offset >> 3
#| let bit_shift = 7 - (offset & 7)
#| let byte_val = bs.unsafe_get(byte_index).to_uint()
#| (byte_val >> bit_shift) & 1U
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_bit_signed(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| _len : Int32,
#|) -> Int32 {
#| let byte_index = offset >> 3
#| let bit_shift = 7 - (offset & 7)
#| let byte_val = bs.unsafe_get(byte_index).to_int()
#| ((byte_val >> bit_shift) & 1) * -1
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_byte(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| let byte_index = offset >> 3
#| if (offset & 7) == 0 {
#| let byte = bs.unsafe_get(byte_index)
#| (byte >> (8 - len)).to_uint()
#| } else if (offset & 7) + len <= 8 {
#| let byte = bs.unsafe_get(byte_index).to_uint()
#| let shift = 8 - ((offset & 7) + len)
#| let mask = (1U << len) - 1
#| (byte >> shift) & mask
#| } else {
#| let b0 = bs.unsafe_get(byte_index).to_uint()
#| let b1 = bs.unsafe_get(byte_index + 1).to_uint()
#| let data = (b0 << 8) | b1
#| let bit_mask = (1U << (16 - (offset & 7))) - 1
#| let data = data & bit_mask
#| let shift = 16 - ((offset & 7) + len)
#| data >> shift
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint_le(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| if (offset & 7) == 0 {
#| if len == 32 {
#| return bs.unsafe_extract_uint_le_aligned(offset >> 3)
#| }
#| if len == 16 {
#| return bs.unsafe_extract_uint16_le_aligned(offset >> 3)
#| }
#| }
#| bs.unsafe_extract_uint_le_slow(offset, len)
#|}
#|#inline(never)
#|fn ArrayView::unsafe_extract_uint_le_slow(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8)
#| match bytes_needed {
#| 2 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, len - 8)
#| (b1 << 8) | b0
#| }
#| 3 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, len - 16)
#| (b2 << 16) | (b1 << 8) | b0
#| }
#| 4 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8)
#| let b3 = bs.unsafe_extract_byte(offset + 24, len - 24)
#| (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
#| }
#| _ => abort("Invalid byte count for int32 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint_be(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| if (offset & 7) == 0 {
#| if len == 32 {
#| return bs.unsafe_extract_uint_be_aligned(offset >> 3)
#| }
#| if len == 16 {
#| return bs.unsafe_extract_uint16_be_aligned(offset >> 3)
#| }
#| }
#| bs.unsafe_extract_uint_be_slow(offset, len)
#|}
#|#inline(never)
#|fn ArrayView::unsafe_extract_uint_be_slow(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8)
#| match bytes_needed {
#| 2 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, len - 8)
#| let shift = 16 - len
#| let data = (b0 << 8) | (b1 << shift)
#| data >> shift
#| }
#| 3 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, len - 16)
#| let shift = 24 - len
#| let data = (b0 << 16) | (b1 << 8) | (b2 << shift)
#| data >> shift
#| }
#| 4 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8)
#| let b3 = bs.unsafe_extract_byte(offset + 24, len - 24)
#| let shift = 32 - len
#| let data = (b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << shift)
#| data >> shift
#| }
#| _ => abort("Invalid byte count for int32 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint64_le(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| if len == 64 && (offset & 7) == 0 {
#| return bs.unsafe_extract_uint64_le_aligned(offset >> 3)
#| }
#| bs.unsafe_extract_uint64_le_slow(offset, len)
#|}
#|#inline(never)
#|fn ArrayView::unsafe_extract_uint64_le_slow(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8).to_uint64()
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8).to_uint64()
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8).to_uint64()
#| let b3 = bs.unsafe_extract_byte(offset + 24, 8).to_uint64()
#| match bytes_needed {
#| 5 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, len - 32).to_uint64()
#| (b4 << 32) | (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
#| }
#| 6 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, len - 40).to_uint64()
#| (b5 << 40) | (b4 << 32) | (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
#| }
#| 7 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, len - 48).to_uint64()
#| (b6 << 48) |
#| (b5 << 40) |
#| (b4 << 32) |
#| (b3 << 24) |
#| (b2 << 16) |
#| (b1 << 8) |
#| b0
#| }
#| 8 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, 8).to_uint64()
#| let b7 = bs.unsafe_extract_byte(offset + 56, len - 56).to_uint64()
#| (b7 << 56) |
#| (b6 << 48) |
#| (b5 << 40) |
#| (b4 << 32) |
#| (b3 << 24) |
#| (b2 << 16) |
#| (b1 << 8) |
#| b0
#| }
#| _ => abort("Invalid byte count for int64 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint64_be(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| if len == 64 && (offset & 7) == 0 {
#| return bs.unsafe_extract_uint64_be_aligned(offset >> 3)
#| }
#| bs.unsafe_extract_uint64_be_slow(offset, len)
#|}
#|#inline(never)
#|fn ArrayView::unsafe_extract_uint64_be_slow(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8).to_uint64()
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8).to_uint64()
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8).to_uint64()
#| let b3 = bs.unsafe_extract_byte(offset + 24, 8).to_uint64()
#| match bytes_needed {
#| 5 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, len - 32).to_uint64()
#| let shift = 40 - len
#| let data = (b0 << 32) |
#| (b1 << 24) |
#| (b2 << 16) |
#| (b3 << 8) |
#| (b4 << shift)
#| data >> shift
#| }
#| 6 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, len - 40).to_uint64()
#| let shift = 48 - len
#| let data = (b0 << 40) |
#| (b1 << 32) |
#| (b2 << 24) |
#| (b3 << 16) |
#| (b4 << 8) |
#| (b5 << shift)
#| data >> shift
#| }
#| 7 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, len - 48).to_uint64()
#| let shift = 56 - len
#| let data = (b0 << 48) |
#| (b1 << 40) |
#| (b2 << 32) |
#| (b3 << 24) |
#| (b4 << 16) |
#| (b5 << 8) |
#| (b6 << shift)
#| data >> shift
#| }
#| 8 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, 8).to_uint64()
#| let b7 = bs.unsafe_extract_byte(offset + 56, len - 56).to_uint64()
#| let shift = 64 - len
#| let data = (b0 << 56) |
#| (b1 << 48) |
#| (b2 << 40) |
#| (b3 << 32) |
#| (b4 << 24) |
#| (b5 << 16) |
#| (b6 << 8) |
#| (b7 << shift)
#| data >> shift
#| }
#| _ => abort("Invalid byte count for int64 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_bytesview(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> ArrayView[Byte] {
#| let start = offset >> 3
#| let end = start + (len >> 3)
#| bs[start:end]
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_byte_signed(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| let unsigned = bs.unsafe_extract_byte(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int_le(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| let unsigned = bs.unsafe_extract_uint_le(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int_be(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| let unsigned = bs.unsafe_extract_uint_be(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int64_le(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| let unsigned = bs.unsafe_extract_uint64_le(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int64_be(
#| bs : ArrayView[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| let unsigned = bs.unsafe_extract_uint64_be(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_bit(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_bit(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_bit_signed(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_bit_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_byte(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_byte(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_byte_signed(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_byte_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint_le(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint_be(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int_le(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int_be(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint64_le(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint64_be(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int64_le(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int64_be(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_bytesview(
#| bs : Array[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> ArrayView[Byte] {
#| bs[:].unsafe_extract_bytesview(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_bit(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_bit(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_bit_signed(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_bit_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_byte(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_byte(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_byte_signed(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_byte_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint_le(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint_be(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int_le(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int_be(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint64_le(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint64_be(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int64_le(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int64_be(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_bytesview(
#| bs : FixedArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> ArrayView[Byte] {
#| bs[:].unsafe_extract_bytesview(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_bit(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_bit(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_bit_signed(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_bit_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_byte(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_byte(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_byte_signed(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_byte_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint_le(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_uint_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint_be(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_uint_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int_le(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_int_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int_be(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_int_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint64_le(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_uint64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint64_be(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_uint64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int64_le(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_int64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int64_be(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_int64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_bytesview(
#| bs : ReadOnlyArray[Byte],
#| offset : Int32,
#| len : Int32,
#|) -> ArrayView[Byte] {
#| bs.unsafe_reinterpret_to_fixed_array().unsafe_extract_bytesview(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_bit(
#| bs : BytesView,
#| offset : Int32,
#| _len : Int32,
#|) -> UInt32 {
#| let byte_index = offset >> 3
#| let bit_shift = 7 - (offset & 7)
#| let byte_val = bs.unsafe_get(byte_index).to_uint()
#| (byte_val >> bit_shift) & 1U
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_bit_signed(
#| bs : BytesView,
#| offset : Int32,
#| _len : Int32,
#|) -> Int32 {
#| let byte_index = offset >> 3
#| let bit_shift = 7 - (offset & 7)
#| let byte_val = bs.unsafe_get(byte_index).to_int()
#| ((byte_val >> bit_shift) & 1) * -1
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_byte(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| let byte_index = offset >> 3
#| if (offset & 7) == 0 {
#| let byte = bs.unsafe_get(byte_index)
#| (byte >> (8 - len)).to_uint()
#| } else if (offset & 7) + len <= 8 {
#| let byte = bs.unsafe_get(byte_index).to_uint()
#| let shift = 8 - ((offset & 7) + len)
#| let mask = (1U << len) - 1
#| (byte >> shift) & mask
#| } else {
#| let b0 = bs.unsafe_get(byte_index).to_uint()
#| let b1 = bs.unsafe_get(byte_index + 1).to_uint()
#| let data = (b0 << 8) | b1
#| let bit_mask = (1U << (16 - (offset & 7))) - 1
#| let data = data & bit_mask
#| let shift = 16 - ((offset & 7) + len)
#| data >> shift
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint_le(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| if (offset & 7) == 0 {
#| if len == 32 {
#| return bs.unsafe_extract_uint_le_aligned(offset >> 3)
#| }
#| if len == 16 {
#| return bs.unsafe_extract_uint16_le_aligned(offset >> 3)
#| }
#| }
#| bs.unsafe_extract_uint_le_slow(offset, len)
#|}
#|#inline(never)
#|fn BytesView::unsafe_extract_uint_le_slow(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8)
#| match bytes_needed {
#| 2 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, len - 8)
#| (b1 << 8) | b0
#| }
#| 3 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, len - 16)
#| (b2 << 16) | (b1 << 8) | b0
#| }
#| 4 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8)
#| let b3 = bs.unsafe_extract_byte(offset + 24, len - 24)
#| (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
#| }
#| _ => abort("Invalid byte count for int32 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint_be(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| if (offset & 7) == 0 {
#| if len == 32 {
#| return bs.unsafe_extract_uint_be_aligned(offset >> 3)
#| }
#| if len == 16 {
#| return bs.unsafe_extract_uint16_be_aligned(offset >> 3)
#| }
#| }
#| bs.unsafe_extract_uint_be_slow(offset, len)
#|}
#|#inline(never)
#|fn BytesView::unsafe_extract_uint_be_slow(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8)
#| match bytes_needed {
#| 2 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, len - 8)
#| let shift = 16 - len
#| let data = (b0 << 8) | (b1 << shift)
#| data >> shift
#| }
#| 3 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, len - 16)
#| let shift = 24 - len
#| let data = (b0 << 16) | (b1 << 8) | (b2 << shift)
#| data >> shift
#| }
#| 4 => {
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8)
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8)
#| let b3 = bs.unsafe_extract_byte(offset + 24, len - 24)
#| let shift = 32 - len
#| let data = (b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << shift)
#| data >> shift
#| }
#| _ => abort("Invalid byte count for int32 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint64_le(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| if len == 64 && (offset & 7) == 0 {
#| return bs.unsafe_extract_uint64_le_aligned(offset >> 3)
#| }
#| bs.unsafe_extract_uint64_le_slow(offset, len)
#|}
#|#inline(never)
#|fn BytesView::unsafe_extract_uint64_le_slow(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8).to_uint64()
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8).to_uint64()
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8).to_uint64()
#| let b3 = bs.unsafe_extract_byte(offset + 24, 8).to_uint64()
#| match bytes_needed {
#| 5 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, len - 32).to_uint64()
#| (b4 << 32) | (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
#| }
#| 6 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, len - 40).to_uint64()
#| (b5 << 40) | (b4 << 32) | (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
#| }
#| 7 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, len - 48).to_uint64()
#| (b6 << 48) |
#| (b5 << 40) |
#| (b4 << 32) |
#| (b3 << 24) |
#| (b2 << 16) |
#| (b1 << 8) |
#| b0
#| }
#| 8 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, 8).to_uint64()
#| let b7 = bs.unsafe_extract_byte(offset + 56, len - 56).to_uint64()
#| (b7 << 56) |
#| (b6 << 48) |
#| (b5 << 40) |
#| (b4 << 32) |
#| (b3 << 24) |
#| (b2 << 16) |
#| (b1 << 8) |
#| b0
#| }
#| _ => abort("Invalid byte count for int64 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint64_be(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| if len == 64 && (offset & 7) == 0 {
#| return bs.unsafe_extract_uint64_be_aligned(offset >> 3)
#| }
#| bs.unsafe_extract_uint64_be_slow(offset, len)
#|}
#|#inline(never)
#|fn BytesView::unsafe_extract_uint64_be_slow(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| let bytes_needed = (len + 7) / 8
#| let b0 = bs.unsafe_extract_byte(offset, 8).to_uint64()
#| let b1 = bs.unsafe_extract_byte(offset + 8, 8).to_uint64()
#| let b2 = bs.unsafe_extract_byte(offset + 16, 8).to_uint64()
#| let b3 = bs.unsafe_extract_byte(offset + 24, 8).to_uint64()
#| match bytes_needed {
#| 5 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, len - 32).to_uint64()
#| let shift = 40 - len
#| let data = (b0 << 32) |
#| (b1 << 24) |
#| (b2 << 16) |
#| (b3 << 8) |
#| (b4 << shift)
#| data >> shift
#| }
#| 6 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, len - 40).to_uint64()
#| let shift = 48 - len
#| let data = (b0 << 40) |
#| (b1 << 32) |
#| (b2 << 24) |
#| (b3 << 16) |
#| (b4 << 8) |
#| (b5 << shift)
#| data >> shift
#| }
#| 7 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, len - 48).to_uint64()
#| let shift = 56 - len
#| let data = (b0 << 48) |
#| (b1 << 40) |
#| (b2 << 32) |
#| (b3 << 24) |
#| (b4 << 16) |
#| (b5 << 8) |
#| (b6 << shift)
#| data >> shift
#| }
#| 8 => {
#| let b4 = bs.unsafe_extract_byte(offset + 32, 8).to_uint64()
#| let b5 = bs.unsafe_extract_byte(offset + 40, 8).to_uint64()
#| let b6 = bs.unsafe_extract_byte(offset + 48, 8).to_uint64()
#| let b7 = bs.unsafe_extract_byte(offset + 56, len - 56).to_uint64()
#| let shift = 64 - len
#| let data = (b0 << 56) |
#| (b1 << 48) |
#| (b2 << 40) |
#| (b3 << 32) |
#| (b4 << 24) |
#| (b5 << 16) |
#| (b6 << 8) |
#| (b7 << shift)
#| data >> shift
#| }
#| _ => abort("Invalid byte count for int64 extraction")
#| }
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_bytesview(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> BytesView {
#| let start = offset >> 3
#| bs.unsafe_view(start~, end=start + (len >> 3))
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_byte_signed(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| let unsigned = bs.unsafe_extract_byte(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int_le(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| let unsigned = bs.unsafe_extract_uint_le(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int_be(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| let unsigned = bs.unsafe_extract_uint_be(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int64_le(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| let unsigned = bs.unsafe_extract_uint64_le(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int64_be(
#| bs : BytesView,
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| let unsigned = bs.unsafe_extract_uint64_be(offset, len)
#| unsigned.extend_sign(len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_bit(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_bit(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_bit_signed(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_bit_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_byte(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_byte(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_byte_signed(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_byte_signed(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint_le(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint_be(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int_le(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int_be(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint64_le(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint64_be(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int64_le(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_le(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int64_be(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_be(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_bytesview(
#| bs : Bytes,
#| offset : Int32,
#| len : Int32,
#|) -> BytesView {
#| bs[:].unsafe_extract_bytesview(offset, len)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_byte_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_get(byte_offset).to_uint()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_byte_signed_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_get(byte_offset).to_uint().extend_sign(8)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint16_le_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| fixedarray_read_uint16_le(
#| buffer_to_fixedarray(bs.buf()),
#| bs.start() + byte_offset,
#| ).to_uint()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint16_be_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| fixedarray_read_uint16_be(
#| buffer_to_fixedarray(bs.buf()),
#| bs.start() + byte_offset,
#| ).to_uint()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int16_le_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_extract_uint16_le_aligned(byte_offset).extend_sign(16)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int16_be_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_extract_uint16_be_aligned(byte_offset).extend_sign(16)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint_le_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| fixedarray_read_uint32_le(
#| buffer_to_fixedarray(bs.buf()),
#| bs.start() + byte_offset,
#| )
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint_be_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| fixedarray_read_uint32_be(
#| buffer_to_fixedarray(bs.buf()),
#| bs.start() + byte_offset,
#| )
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int_le_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_extract_uint_le_aligned(byte_offset).reinterpret_as_int()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int_be_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_extract_uint_be_aligned(byte_offset).reinterpret_as_int()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint64_le_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| fixedarray_read_uint64_le(
#| buffer_to_fixedarray(bs.buf()),
#| bs.start() + byte_offset,
#| )
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_uint64_be_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| fixedarray_read_uint64_be(
#| buffer_to_fixedarray(bs.buf()),
#| bs.start() + byte_offset,
#| )
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int64_le_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs.unsafe_extract_uint64_le_aligned(byte_offset).reinterpret_as_int64()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_int64_be_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs.unsafe_extract_uint64_be_aligned(byte_offset).reinterpret_as_int64()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_byte_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_get(byte_offset).to_uint()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_byte_signed_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_get(byte_offset).to_uint().extend_sign(8)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint16_le_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_read_uint16_le(byte_offset).to_uint()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint16_be_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_read_uint16_be(byte_offset).to_uint()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int16_le_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_extract_uint16_le_aligned(byte_offset).extend_sign(16)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int16_be_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_extract_uint16_be_aligned(byte_offset).extend_sign(16)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint_le_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_read_uint32_le(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint_be_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_read_uint32_be(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int_le_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_read_uint32_le(byte_offset).reinterpret_as_int()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int_be_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs.unsafe_read_uint32_be(byte_offset).reinterpret_as_int()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint64_le_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs.unsafe_read_uint64_le(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_uint64_be_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs.unsafe_read_uint64_be(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int64_le_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int64 {
#| bs.unsafe_read_uint64_le(byte_offset).reinterpret_as_int64()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_int64_be_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> Int64 {
#| bs.unsafe_read_uint64_be(byte_offset).reinterpret_as_int64()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_byte_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_byte_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_byte_signed_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_byte_signed_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint16_le_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint16_be_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int16_le_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int16_be_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint_le_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint_be_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int_le_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int_be_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint64_le_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_uint64_be_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int64_le_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_int64_be_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_byte_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_byte_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_byte_signed_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_byte_signed_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint16_le_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint16_be_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int16_le_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int16_be_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint_le_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint_be_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int_le_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int_be_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint64_le_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_uint64_be_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int64_le_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_int64_be_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_byte_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_byte_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_byte_signed_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_byte_signed_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint16_le_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_uint16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint16_be_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_uint16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int16_le_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_int16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int16_be_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_int16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint_le_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_uint_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint_be_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_uint_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int_le_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_int_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int_be_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int32 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_int_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint64_le_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_uint64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_uint64_be_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_uint64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int64_le_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_int64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_int64_be_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> Int64 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_int64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_byte_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_byte_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_byte_signed_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_byte_signed_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint16_le_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint16_be_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int16_le_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int16_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int16_be_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int16_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint_le_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs.unsafe_read_uint32_le(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint_be_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt32 {
#| bs[:].unsafe_extract_uint_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int_le_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int_be_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int32 {
#| bs[:].unsafe_extract_int_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint64_le_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs.unsafe_read_uint64_le(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_uint64_be_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> UInt64 {
#| bs[:].unsafe_extract_uint64_be_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int64_le_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_le_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_int64_be_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> Int64 {
#| bs[:].unsafe_extract_int64_be_aligned(byte_offset)
#|}
),
"bitstring_v128.mbt": (
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ArrayView::unsafe_extract_v128_aligned(
#| bs : ArrayView[Byte],
#| byte_offset : Int32,
#|) -> V128 {
#| v128_load(buffer_to_fixedarray(bs.buf()), bs.start() + byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn BytesView::unsafe_extract_v128_aligned(
#| bs : BytesView,
#| byte_offset : Int32,
#|) -> V128 {
#| v128_load(unsafe_from_bytes(bs.bytes()), bs.start() + byte_offset)
#|}
#|#borrow(bs)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Bytes::unsafe_extract_v128_aligned(
#| bs : Bytes,
#| byte_offset : Int32,
#|) -> V128 {
#| v128_load(unsafe_from_bytes(bs), byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn Array::unsafe_extract_v128_aligned(
#| bs : Array[Byte],
#| byte_offset : Int32,
#|) -> V128 {
#| bs[:].unsafe_extract_v128_aligned(byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_extract_v128_aligned(
#| bs : FixedArray[Byte],
#| byte_offset : Int32,
#|) -> V128 {
#| v128_load(bs, byte_offset)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn ReadOnlyArray::unsafe_extract_v128_aligned(
#| bs : ReadOnlyArray[Byte],
#| byte_offset : Int32,
#|) -> V128 {
#| bs
#| .unsafe_reinterpret_to_fixed_array()
#| .unsafe_extract_v128_aligned(byte_offset)
#|}
),
"bool.mbt": (
#|pub fn Bool::to_int(self : Bool) -> Int32 {
#| if self {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub fn Bool::to_int64(self : Bool) -> Int64 {
#| if self {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub fn Bool::to_uint(self : Bool) -> UInt32 {
#| if self {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub fn Bool::to_uint64(self : Bool) -> UInt64 {
#| if self {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub impl Hash for Bool with fn hash_combine(self, hasher) {
#| hasher.combine_bool(self)
#|}
#|pub impl Hash for Bool with fn hash(self : Bool) -> Int32 {
#| Hash::hash(if self { 1U } else { 0U })
#|}
#|pub fn Bool::to_uint16(self : Bool) -> UInt16 {
#| if self {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub fn Bool::to_byte(self : Bool) -> Byte {
#| if self {
#| b'\x01'
#| } else {
#| b'\x00'
#| }
#|}
),
"bounds.mbt": (
#|#inline(never)
#|fn[T] index_out_of_bounds(len : Int32, index : Int32) -> T {
#| abort(
#| "index out of bounds: the len is from 0 to \{len} but the index is \{index}",
#| )
#|}
#|#inline(never)
#|fn[T] index_out_of_bounds2(len : Int32, i : Int32, j : Int32) -> T {
#| abort(
#| "index out of bounds: the len is from 0 to \{len} but the index is (\{i}, \{j})",
#| )
#|}
#|fn clamp_offset(offset : Int32, len : Int32) -> Int32 {
#| if offset < 0 {
#| 0
#| } else if offset > len {
#| len
#| } else {
#| offset
#| }
#|}
),
"builtin_types.mbt": (
#|#builtin
#|pub type Unit
#|#builtin
#|pub type Bool
#|#builtin
#|pub type Byte
#|#builtin
#|pub type Char
#|#builtin
#|#alias(Int32)
#|pub type Int
#|#builtin
#|pub type Int64
#|#builtin
#|#alias(UInt32)
#|pub type UInt
#|#builtin
#|pub type UInt16
#|#builtin
#|pub type UInt64
#|#builtin
#|pub type Double
#|#builtin
#|pub type String
#|#builtin
#|pub type V128
#|#builtin
#|pub type Bytes
#|#builtin
#|#valtype
#|pub type StringView
#|#builtin
#|#valtype
#|pub type BytesView
#|#builtin
#|pub type FixedArray[T]
#|#builtin
#|pub type ReadOnlyArray[T]
#|#builtin
#|pub type Error
#|#builtin
#|pub type FuncRef[T]
#|#builtin
#|pub(all) enum Option[T] {
#| None
#| Some(T)
#|}
#|pub(all) enum Result[T, E] {
#| Err(E)
#| Ok(T)
#|}
),
"byte.mbt": (
#|pub fn Byte::Byte(self : Byte) -> Byte = "%identity"
#|pub impl Mul for Byte with fn mul(self : Byte, that : Byte) -> Byte {
#| (self.to_int() * that.to_int()).to_byte()
#|}
#|pub impl Div for Byte with fn div(self : Byte, that : Byte) -> Byte {
#| (self.to_int() / that.to_int()).to_byte()
#|}
#|pub impl Mod for Byte with fn mod(self : Byte, that : Byte) -> Byte {
#| (self.to_int() % that.to_int()).to_byte()
#|}
#|pub impl Eq for Byte with fn equal(self : Byte, that : Byte) -> Bool {
#| self.to_int() == that.to_int()
#|}
#|pub impl Eq for Byte with fn not_equal(self : Byte, that : Byte) -> Bool {
#| self.to_int() != that.to_int()
#|}
#|pub impl Add for Byte with fn add(self : Byte, that : Byte) -> Byte {
#| (self.to_int() + that.to_int()).to_byte()
#|}
#|pub impl Sub for Byte with fn sub(self : Byte, that : Byte) -> Byte {
#| (self.to_int() - that.to_int()).to_byte()
#|}
#|pub impl Compare for Byte with fn compare(self : Byte, that : Byte) -> Int32 {
#| self.to_int().compare(that.to_int())
#|}
#|pub impl Compare for Byte with fn lt(x, y) {
#| x.to_int() < y.to_int()
#|}
#|pub impl Compare for Byte with fn le(x, y) {
#| x.to_int() <= y.to_int()
#|}
#|pub impl Compare for Byte with fn gt(x, y) {
#| x.to_int() > y.to_int()
#|}
#|pub impl Compare for Byte with fn ge(x, y) {
#| x.to_int() >= y.to_int()
#|}
#|pub fn Byte::to_string(self : Byte, radix~ : Int32 = 10) -> String {
#| self.to_int().to_string(radix~)
#|}
#|pub impl Hash for Byte with fn hash_combine(self, hasher) {
#| hasher.combine_byte(self)
#|}
#|pub impl Default for Byte with fn default() {
#| b'\x00'
#|}
#|pub fn Byte::lnot(self : Byte) -> Byte {
#| self.to_int().lnot().to_byte()
#|}
#|pub impl BitAnd for Byte with fn land(self : Byte, that : Byte) -> Byte {
#| (self.to_int() & that.to_int()).to_byte()
#|}
#|pub impl BitOr for Byte with fn lor(self : Byte, that : Byte) -> Byte {
#| (self.to_int() | that.to_int()).to_byte()
#|}
#|pub impl BitXOr for Byte with fn lxor(self : Byte, that : Byte) -> Byte {
#| (self.to_int() ^ that.to_int()).to_byte()
#|}
#|pub fn Byte::to_uint(self : Byte) -> UInt32 {
#| self.to_int().reinterpret_as_uint()
#|}
#|pub fn Byte::to_uint64(self : Byte) -> UInt64 {
#| self.to_uint().to_uint64()
#|}
#|pub fn Byte::popcnt(self : Byte) -> Int32 {
#| self.to_uint().popcnt()
#|}
#|pub fn Byte::clz(self : Byte) -> Int32 {
#| self.to_uint().clz() - 24
#|}
#|pub fn Byte::ctz(self : Byte) -> Int32 {
#| (self.to_uint() | 0x100U).ctz()
#|}
#|pub fn Byte::min(self : Byte, other : Byte) -> Byte {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Byte::max(self : Byte, other : Byte) -> Byte {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Byte::clamp(self : Byte, min~ : Byte, max~ : Byte) -> Byte {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub impl Shl for Byte with fn shl(self : Byte, count : Int32) -> Byte {
#| (self.to_int() << count).to_byte()
#|}
#|pub impl Shr for Byte with fn shr(self : Byte, count : Int32) -> Byte {
#| (self.to_uint() >> count).reinterpret_as_int().to_byte()
#|}
),
"bytes.mbt": (
#|#internal(unsafe, "Creating mutable Bytes")
#|#doc(hidden)
#|pub fn FixedArray::unsafe_reinterpret_as_bytes(
#| self : FixedArray[Byte],
#|) -> Bytes = "%identity"
#|#locals(value)
#|pub fn Bytes::makei(
#| length : Int32,
#| value : (Int32) -> Byte raise?,
#|) -> Bytes raise? {
#| if length <= 0 {
#| return []
#| }
#| let arr = FixedArray::make(length, value(0))
#| for i in 1.. String = "$moonbit.unsafe_bytes_sub_string"
#|#label_migration(len, alias=length, msg="Use len= instead")
#|pub fn Bytes::to_unchecked_string(
#| self : Bytes,
#| offset~ : Int32 = 0,
#| len? : Int32,
#|) -> String {
#| let total = self.length()
#| let len = if len is Some(l) { l } else { total - offset }
#| guard! offset >= 0 && offset <= total && len >= 0 && len <= total - offset
#| unsafe_sub_string(self, offset, len)
#|}
#|pub fn FixedArray::blit_from_string(
#| self : FixedArray[Byte],
#| bytes_offset : Int32,
#| str : String,
#| str_offset : Int32,
#| length : Int32,
#|) -> Unit {
#| let len1 = self.length()
#| let len2 = str.length()
#| guard! length >= 0 &&
#| bytes_offset >= 0 &&
#| bytes_offset <= len1 &&
#| length <= (len1 - bytes_offset) / 2 &&
#| str_offset >= 0 &&
#| str_offset <= len2 &&
#| length <= len2 - str_offset
#| let end_str_offset = str_offset + length
#| for i = str_offset, j = bytes_offset; i < end_str_offset; i = i + 1, j = j + 2 {
#| let c = str.unsafe_get(i).to_int().reinterpret_as_uint()
#| self[j] = (c & 0xff).to_byte()
#| self[j + 1] = (c >> 8).to_byte()
#| }
#|}
#|fn unsafe_from_bytes(bytes : Bytes) -> FixedArray[Byte] = "%identity"
#|pub fn FixedArray::blit_from_bytes(
#| self : FixedArray[Byte],
#| bytes_offset : Int32,
#| src : Bytes,
#| src_offset : Int32,
#| length : Int32,
#|) -> Unit {
#| let len1 = self.length()
#| let len2 = src.length()
#| guard! length >= 0 &&
#| bytes_offset >= 0 &&
#| bytes_offset <= len1 &&
#| length <= len1 - bytes_offset &&
#| src_offset >= 0 &&
#| src_offset <= len2 &&
#| length <= len2 - src_offset
#| FixedArray::unsafe_blit(
#| self,
#| bytes_offset,
#| unsafe_from_bytes(src),
#| src_offset,
#| length,
#| )
#|}
#|pub fn FixedArray::blit_from_bytesview(
#| self : FixedArray[Byte],
#| bytes_offset : Int32,
#| src : BytesView,
#|) -> Unit {
#| FixedArray::blit_from_bytes(
#| self,
#| bytes_offset,
#| src.bytes(),
#| src.start(),
#| src.length(),
#| )
#|}
#|pub fn FixedArray::set_utf8_char(
#| self : FixedArray[Byte],
#| offset : Int32,
#| value : Char,
#|) -> Int32 {
#| let code = value.to_uint()
#| match code {
#| _..<0x80 => {
#| self[offset] = ((code & 0x7F) | 0x00).to_byte()
#| 1
#| }
#| _..<0x0800 => {
#| self[offset] = (((code >> 6) & 0x1F) | 0xC0).to_byte()
#| self[offset + 1] = ((code & 0x3F) | 0x80).to_byte()
#| 2
#| }
#| _..<0x010000 => {
#| self[offset] = (((code >> 12) & 0x0F) | 0xE0).to_byte()
#| self[offset + 1] = (((code >> 6) & 0x3F) | 0x80).to_byte()
#| self[offset + 2] = ((code & 0x3F) | 0x80).to_byte()
#| 3
#| }
#| _..<0x110000 => {
#| self[offset] = (((code >> 18) & 0x07) | 0xF0).to_byte()
#| self[offset + 1] = (((code >> 12) & 0x3F) | 0x80).to_byte()
#| self[offset + 2] = (((code >> 6) & 0x3F) | 0x80).to_byte()
#| self[offset + 3] = ((code & 0x3F) | 0x80).to_byte()
#| 4
#| }
#| _ => abort("Char out of range")
#| }
#|}
#|pub fn FixedArray::set_utf16le_char(
#| self : FixedArray[Byte],
#| offset : Int32,
#| value : Char,
#|) -> Int32 {
#| let code = value.to_uint()
#| if code < 0x10000 {
#| self[offset] = (code & 0xFF).to_byte()
#| self[offset + 1] = (code >> 8).to_byte()
#| 2
#| } else if code < 0x110000 {
#| let hi = code - 0x10000
#| let lo = (hi >> 10) | 0xD800
#| let hi = (hi & 0x3FF) | 0xDC00
#| self[offset] = (lo & 0xFF).to_byte()
#| self[offset + 1] = (lo >> 8).to_byte()
#| self[offset + 2] = (hi & 0xFF).to_byte()
#| self[offset + 3] = (hi >> 8).to_byte()
#| 4
#| } else {
#| abort("Char out of range")
#| }
#|}
#|pub fn FixedArray::set_utf16be_char(
#| self : FixedArray[Byte],
#| offset : Int32,
#| value : Char,
#|) -> Int32 {
#| let code = value.to_uint()
#| if code < 0x10000 {
#| self[offset] = (code >> 8).to_byte()
#| self[offset + 1] = (code & 0xFF).to_byte()
#| 2
#| } else if code < 0x110000 {
#| let hi = code - 0x10000
#| let lo = (hi >> 10) | 0xD800
#| let hi = (hi & 0x3FF) | 0xDC00
#| self[offset] = (lo >> 8).to_byte()
#| self[offset + 1] = (lo & 0xFF).to_byte()
#| self[offset + 2] = (hi >> 8).to_byte()
#| self[offset + 3] = (hi & 0xFF).to_byte()
#| 4
#| } else {
#| abort("Char out of range")
#| }
#|}
#|pub impl Eq for Bytes with fn equal(self : Bytes, other : Bytes) -> Bool = "%bytes.equal"
#|#borrow(self, other)
#|pub fn Bytes::range_equal(
#| self : Bytes,
#| start~ : Int32,
#| other : Bytes,
#| other_start~ : Int32,
#| len~ : Int32,
#|) -> Bool {
#| let self_len = self.length()
#| let other_len = other.length()
#| guard len >= 0 &&
#| start >= 0 &&
#| start <= self_len &&
#| other_start >= 0 &&
#| other_start <= other_len &&
#| len <= self_len - start &&
#| len <= other_len - other_start else {
#| abort("Bytes::range_equal: invalid range")
#| }
#| self.unsafe_range_equal(other, start~, other_start~, len~)
#|}
#|pub impl Compare for Bytes with fn compare(self, other) {
#| let self_len = self.length()
#| let other_len = other.length()
#| let cmp = self_len.compare(other_len)
#| if cmp != 0 {
#| return cmp
#| }
#| self.lexical_compare(other)
#|}
#|#cfg(any(target="native", target="wasm"))
#|#alias(of, deprecated)
#|#owned(arr)
#|pub fn Bytes::from_array(arr : ArrayView[Byte]) -> Bytes {
#| let len = arr.length()
#| if len == 0 {
#| return []
#| }
#| let start = arr.start()
#| let buf = arr.buf()
#| if start == 0 && len == buf.length() && unsafe_is_unique_ref(buf) {
#| return buffer_to_fixedarray(buf).unsafe_reinterpret_as_bytes()
#| }
#| let result = UninitializedArray::unsafe_make_and_blit(buf, len, start, 0, len)
#| buffer_to_fixedarray(result).unsafe_reinterpret_as_bytes()
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#alias(of, deprecated)
#|pub fn Bytes::from_array(arr : ArrayView[Byte]) -> Bytes {
#| let len = arr.length()
#| if len == 0 {
#| return []
#| }
#| let result = UninitializedArray::unsafe_make_and_blit(
#| arr.buf(),
#| len,
#| arr.start(),
#| 0,
#| len,
#| )
#| buffer_to_fixedarray(result).unsafe_reinterpret_as_bytes()
#|}
#|#deprecated("Use Bytes::from_array instead")
#|pub fn Bytes::from_fixedarray(arr : FixedArray[Byte], len? : Int32) -> Bytes {
#| let len = match len {
#| None => arr.length()
#| Some(x) => {
#| guard! 0 <= x && x <= arr.length()
#| x
#| }
#| }
#| let result = FixedArray::make_and_blit(arr, allocate_len=len, init=0, len~)
#| result.unsafe_reinterpret_as_bytes()
#|}
#|#label_migration(len, fill=false)
#|pub fn Bytes::to_fixedarray(self : Bytes, len? : Int32) -> FixedArray[Byte] {
#| let len = match len {
#| None => self.length()
#| Some(x) => {
#| guard! 0 <= x && x <= self.length()
#| x
#| }
#| }
#| FixedArray::make_and_blit(
#| unsafe_from_bytes(self),
#| allocate_len=len,
#| init=0,
#| len~,
#| )
#|}
#|pub fn BytesView::to_fixedarray(self : BytesView) -> FixedArray[Byte] {
#| let len = self.length()
#| FixedArray::make_and_blit(
#| unsafe_from_bytes(self.data()),
#| allocate_len=len,
#| init=0,
#| src_offset=self.start_offset(),
#| len~,
#| )
#|}
#|#alias(from_iterator, deprecated)
#|pub fn Bytes::from_iter(iter : Iter[Byte]) -> Bytes {
#| Bytes::from_array(iter.collect())
#|}
#|pub fn Bytes::to_array(self : Bytes) -> Array[Byte] {
#| let len = self.length()
#| Array::unsafe_make_and_blit_from_fixed(
#| unsafe_from_bytes(self),
#| allocate_len=len,
#| len~,
#| )
#|}
#|pub fn BytesView::to_array(self : BytesView) -> Array[Byte] {
#| let len = self.length()
#| Array::unsafe_make_and_blit_from_fixed(
#| unsafe_from_bytes(self.data()),
#| allocate_len=len,
#| src_offset=self.start_offset(),
#| len~,
#| )
#|}
#|#alias(iterator, deprecated)
#|pub fn Bytes::iter(self : Bytes) -> Iter[Byte] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let c = self.unsafe_get(i)
#| i += 1
#| Some(c)
#| },
#| size_hint=len,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn Bytes::iter2(self : Bytes) -> Iter2[Int32, Byte] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let result = (i, self.unsafe_get(i))
#| i += 1
#| Some(result)
#| },
#| size_hint=len,
#| )
#|}
#|pub impl Default for Bytes with fn default() {
#| b""
#|}
#|pub fn Bytes::is_empty(self : Bytes) -> Bool {
#| self.length() == 0
#|}
#|#intrinsic("%bytes.get_opt")
#|pub fn Bytes::get(self : Bytes, index : Int32) -> Byte? {
#| guard index >= 0 && index < self.length() else { None }
#| Some(self[index])
#|}
#|pub impl Add for Bytes with fn add(self : Bytes, other : Bytes) -> Bytes {
#| self.bytes_add_impl(other)
#|}
#|#cfg(not(target="js"))
#|fn Bytes::bytes_add_impl(self : Bytes, other : Bytes) -> Bytes {
#| let len_self = self.length()
#| let len_other = other.length()
#| let rv = UninitializedArray::unsafe_make_and_blit_from_fixed(
#| unsafe_from_bytes(self),
#| len_self + len_other,
#| 0,
#| 0,
#| len_self,
#| )
#| UninitializedArray::unsafe_blit_fixed(
#| rv,
#| len_self,
#| unsafe_from_bytes(other),
#| 0,
#| len_other,
#| )
#| unsafe_to_bytes(buffer_to_fixedarray(rv))
#|}
#|#cfg(target="js")
#|fn Bytes::bytes_add_impl(self : Bytes, other : Bytes) -> Bytes {
#| let len_self = self.length()
#| let len_other = other.length()
#| let rv : FixedArray[Byte] = FixedArray::make(len_self + len_other, 0)
#| rv.blit_from_bytes(0, self, 0, len_self)
#| rv.blit_from_bytes(len_self, other, 0, len_other)
#| unsafe_to_bytes(rv)
#|}
#|pub impl Hash for Bytes with fn hash_combine(self, hasher) {
#| hasher.combine(self[:])
#|}
#|#cfg(not(target="js"))
#|pub impl Hash for Bytes with fn hash(self : Bytes) -> Int32 {
#| let mut acc = seed.reinterpret_as_uint() + GPRIME5
#| let (cur, remain) = for cur = 0, remain = self.length(); remain >= 4; {
#| acc += 4U
#| acc = consume4_acc(acc, self.unsafe_read_uint32_le(cur))
#| continue cur + 4, remain - 4
#| } nobreak {
#| (cur, remain)
#| }
#| for cur = cur, remain = remain; remain >= 1; {
#| acc = rotl(acc + self.unsafe_get(cur).to_uint() * GPRIME5, 11) * GPRIME1
#| continue cur + 1, remain - 1
#| }
#| finalize_acc(acc)
#|}
#|pub fn Bytes::repeat(self : Self, count : Int32) -> Bytes {
#| if count < 0 {
#| abort("negative repeat count")
#| }
#| if count == 0 || self.is_empty() {
#| return []
#| }
#| if count == 1 {
#| return self
#| }
#| let len = self.length()
#| let total = len * count
#| guard total / count == len else { abort("repeat result too large") }
#| let arr = FixedArray::make(total, (0 : Byte))
#| arr.blit_from_bytes(0, self, 0, len)
#| for filled = len; filled < total; {
#| let remaining = total - filled
#| let copy_len = if filled < remaining { filled } else { remaining }
#| let src = unsafe_to_bytes(arr)
#| arr.blit_from_bytes(filled, src, 0, copy_len)
#| continue filled + copy_len
#| }
#| unsafe_to_bytes(arr)
#|}
#|pub fn Bytes::lexical_compare(self : Bytes, other : Bytes) -> Int32 {
#| self[:].lexical_compare(other)
#|}
),
"bytes_find.mbt": (
#|let find_max_short_pattern : Int32 = 32
#|let find_max_bruteforce : Int32 = 64
#|let find_prime_rk : UInt32 = 16777619U
#|pub fn BytesView::find(target : BytesView, pattern : BytesView) -> Int32? {
#| find_index(target, pattern)
#|}
#|pub fn Bytes::find(target : Bytes, pattern : BytesView) -> Int32? {
#| target[:].find(pattern)
#|}
#|fn find_index(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| if pattern_len == 0 {
#| return Some(0)
#| }
#| if pattern_len > target_len {
#| return None
#| }
#| if pattern_len == 1 {
#| let found = find_byte_from_view(
#| target,
#| 0,
#| target_len,
#| pattern.unsafe_get(0),
#| )
#| if found < 0 {
#| None
#| } else {
#| Some(found)
#| }
#| } else if pattern_len == target_len {
#| if target == pattern {
#| Some(0)
#| } else {
#| None
#| }
#| } else if pattern_len <= find_max_short_pattern {
#| find_short(target, pattern)
#| } else {
#| find_long_by_byte_scanner(target, pattern)
#| }
#|}
#|#inline
#|fn find_cutover(i : Int32) -> Int32 {
#| 4 + i / 16
#|}
#|declare fn find_byte_from_view(
#| target : BytesView,
#| start : Int32,
#| end : Int32,
#| byte : Byte,
#|) -> Int32
#|#cfg(any(target="js", target="wasm-gc"))
#|fn find_byte_from_view(
#| target : BytesView,
#| start : Int32,
#| end : Int32,
#| byte : Byte,
#|) -> Int32 {
#| for pos in start.. Int32 {
#| let target_start = target.start_offset()
#| let found = find_byte_from_bytes(
#| target.data(),
#| target_start + start,
#| target_start + end,
#| byte,
#| )
#| if found < 0 {
#| -1
#| } else {
#| found - target_start
#| }
#|}
#|declare fn rev_find_byte_from_view(
#| target : BytesView,
#| start : Int32,
#| end : Int32,
#| byte : Byte,
#|) -> Int32
#|#cfg(any(target="js", target="wasm-gc"))
#|fn rev_find_byte_from_view(
#| target : BytesView,
#| start : Int32,
#| end : Int32,
#| byte : Byte,
#|) -> Int32 {
#| for pos in end>..start {
#| if target.unsafe_get(pos) == byte {
#| break pos
#| }
#| } nobreak {
#| -1
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn rev_find_byte_from_view(
#| target : BytesView,
#| start : Int32,
#| end : Int32,
#| byte : Byte,
#|) -> Int32 {
#| let target_start = target.start_offset()
#| let found = rev_find_byte_from_bytes(
#| target.data(),
#| target_start + start,
#| target_start + end,
#| byte,
#| )
#| if found < 0 {
#| -1
#| } else {
#| found - target_start
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn Bytes::v128_load(self : Bytes, offset : Int32) -> V128 {
#| v128_load(unsafe_from_bytes(self), offset)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_byte_from_bytes(
#| data : Bytes,
#| start : Int32,
#| end : Int32,
#| byte : Byte,
#|) -> Int32 {
#| let byte_v = i8x16_splat(byte)
#| let pos = for pos = start; end - pos >= 32; {
#| let lo = i8x16_eq(data.v128_load(pos), byte_v)
#| let hi = i8x16_eq(data.v128_load(pos + 16), byte_v)
#| if v128_any_true(v128_or(lo, hi)) {
#| let lo_mask = i8x16_bitmask(lo)
#| if lo_mask != 0 {
#| return pos + lo_mask.ctz()
#| }
#| return pos + 16 + i8x16_bitmask(hi).ctz()
#| }
#| continue pos + 32
#| } nobreak {
#| pos
#| }
#| let tail_start = if end - pos >= 16 {
#| let mask = i8x16_bitmask(i8x16_eq(data.v128_load(pos), byte_v))
#| if mask != 0 {
#| return pos + mask.ctz()
#| }
#| pos + 16
#| } else {
#| pos
#| }
#| for pos in tail_start.. Int32 {
#| let byte_v = i8x16_splat(byte)
#| let pos = for pos = end; pos - 32 >= start; {
#| let hi = i8x16_eq(data.v128_load(pos - 16), byte_v)
#| let lo = i8x16_eq(data.v128_load(pos - 32), byte_v)
#| if v128_any_true(v128_or(hi, lo)) {
#| let hi_mask = i8x16_bitmask(hi)
#| if hi_mask != 0 {
#| return pos - 16 + 31 - hi_mask.clz()
#| }
#| return pos - 32 + 31 - i8x16_bitmask(lo).clz()
#| }
#| continue pos - 32
#| } nobreak {
#| pos
#| }
#| let head_end = if pos - 16 >= start {
#| let mask = i8x16_bitmask(i8x16_eq(data.v128_load(pos - 16), byte_v))
#| if mask != 0 {
#| return pos - 16 + 31 - mask.clz()
#| }
#| pos - 16
#| } else {
#| pos
#| }
#| for pos in head_end>..start {
#| if data.unsafe_get(pos) == byte {
#| break pos
#| }
#| } nobreak {
#| -1
#| }
#|}
#|declare fn find_short(target : BytesView, pattern : BytesView) -> Int32?
#|#cfg(any(target="js", target="wasm-gc"))
#|fn find_short(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let last_offset = pattern_len - 1
#| let last_byte = pattern.unsafe_get(last_offset)
#| for anchor_pos = last_offset; anchor_pos < target_len; {
#| let found = find_byte_from_view(target, anchor_pos, target_len, last_byte)
#| if found < 0 {
#| break None
#| }
#| let candidate = found - last_offset
#| if target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=target_start + candidate,
#| other_start=pattern_start,
#| len=last_offset,
#| ) {
#| break Some(candidate)
#| }
#| continue found + 1
#| } nobreak {
#| None
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_short(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let last = target_len - pattern_len
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let candidate_end = target_start + last + 1
#| let first = pattern.unsafe_get(0)
#| let last_offset = pattern_len - 1
#| let last_byte = pattern.unsafe_get(last_offset)
#| let middle_len = last_offset - 1
#| for pos = 0; pos <= last; {
#| let found = find_short_candidate_from_bytes(
#| target.data(),
#| target_start + pos,
#| candidate_end,
#| first,
#| last_offset,
#| last_byte,
#| )
#| if found < 0 {
#| break None
#| }
#| let candidate = found - target_start
#| if target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=found + 1,
#| other_start=pattern_start + 1,
#| len=middle_len,
#| ) {
#| break Some(candidate)
#| }
#| continue candidate + 1
#| } nobreak {
#| None
#| }
#|}
#|#cfg(any(target="js", target="wasm-gc"))
#|fn rev_find_short_candidate_from_view(
#| target : BytesView,
#| start : Int32,
#| candidate_end : Int32,
#| first : Byte,
#| last_offset : Int32,
#| last : Byte,
#|) -> Int32 {
#| for pos in candidate_end>..start {
#| if target.unsafe_get(pos) == first &&
#| target.unsafe_get(pos + last_offset) == last {
#| break pos
#| }
#| } nobreak {
#| -1
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_short_candidate_from_bytes(
#| data : Bytes,
#| start : Int32,
#| candidate_end : Int32,
#| first : Byte,
#| last_offset : Int32,
#| last : Byte,
#|) -> Int32 {
#| let first_v = i8x16_splat(first)
#| let last_v = i8x16_splat(last)
#| let pos = for pos = start; candidate_end - pos >= 32; {
#| let lo = v128_and(
#| i8x16_eq(data.v128_load(pos), first_v),
#| i8x16_eq(data.v128_load(pos + last_offset), last_v),
#| )
#| let hi = v128_and(
#| i8x16_eq(data.v128_load(pos + 16), first_v),
#| i8x16_eq(data.v128_load(pos + 16 + last_offset), last_v),
#| )
#| if v128_any_true(v128_or(lo, hi)) {
#| let lo_bits = i8x16_bitmask(lo)
#| if lo_bits != 0 {
#| return pos + lo_bits.ctz()
#| }
#| return pos + 16 + i8x16_bitmask(hi).ctz()
#| }
#| continue pos + 32
#| } nobreak {
#| pos
#| }
#| let tail_start = if candidate_end - pos >= 16 {
#| let mask = v128_and(
#| i8x16_eq(data.v128_load(pos), first_v),
#| i8x16_eq(data.v128_load(pos + last_offset), last_v),
#| )
#| let bits = i8x16_bitmask(mask)
#| if bits != 0 {
#| return pos + bits.ctz()
#| }
#| pos + 16
#| } else {
#| pos
#| }
#| for pos in tail_start.. Int32 {
#| let first_v = i8x16_splat(first)
#| let last_v = i8x16_splat(last)
#| let pos = for pos = candidate_end; pos - 32 >= start; {
#| let hi = v128_and(
#| i8x16_eq(data.v128_load(pos - 16), first_v),
#| i8x16_eq(data.v128_load(pos - 16 + last_offset), last_v),
#| )
#| let lo = v128_and(
#| i8x16_eq(data.v128_load(pos - 32), first_v),
#| i8x16_eq(data.v128_load(pos - 32 + last_offset), last_v),
#| )
#| if v128_any_true(v128_or(hi, lo)) {
#| let hi_bits = i8x16_bitmask(hi)
#| if hi_bits != 0 {
#| return pos - 16 + 31 - hi_bits.clz()
#| }
#| return pos - 32 + 31 - i8x16_bitmask(lo).clz()
#| }
#| continue pos - 32
#| } nobreak {
#| pos
#| }
#| let head_end = if pos - 16 >= start {
#| let chunk_start = pos - 16
#| let mask = v128_and(
#| i8x16_eq(data.v128_load(chunk_start), first_v),
#| i8x16_eq(data.v128_load(chunk_start + last_offset), last_v),
#| )
#| let bits = i8x16_bitmask(mask)
#| if bits != 0 {
#| return chunk_start + 31 - bits.clz()
#| }
#| chunk_start
#| } else {
#| pos
#| }
#| for pos in head_end>..start {
#| if data.unsafe_get(pos) == first &&
#| data.unsafe_get(pos + last_offset) == last {
#| break pos
#| }
#| } nobreak {
#| -1
#| }
#|}
#|declare fn rev_find_by_byte_scanner(
#| target : BytesView,
#| pattern : BytesView,
#|) -> Int32?
#|#cfg(any(target="js", target="wasm-gc"))
#|fn rev_find_by_byte_scanner(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let last = target_len - pattern_len
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let first = pattern.unsafe_get(0)
#| let last_offset = pattern_len - 1
#| let last_byte = pattern.unsafe_get(last_offset)
#| let middle_len = last_offset - 1
#| for candidate_end = last + 1; candidate_end > 0; {
#| let found = rev_find_short_candidate_from_view(
#| target, 0, candidate_end, first, last_offset, last_byte,
#| )
#| if found < 0 {
#| break None
#| }
#| if target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=target_start + found + 1,
#| other_start=pattern_start + 1,
#| len=middle_len,
#| ) {
#| break Some(found)
#| }
#| continue found
#| } nobreak {
#| None
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn rev_find_by_byte_scanner(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let last = target_len - pattern_len
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let first = pattern.unsafe_get(0)
#| let last_offset = pattern_len - 1
#| let last_byte = pattern.unsafe_get(last_offset)
#| let middle_len = last_offset - 1
#| for candidate_end = last + 1; candidate_end > 0; {
#| let found = rev_find_short_candidate_from_bytes(
#| target.data(),
#| target_start,
#| target_start + candidate_end,
#| first,
#| last_offset,
#| last_byte,
#| )
#| if found < 0 {
#| break None
#| }
#| let candidate = found - target_start
#| if target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=found + 1,
#| other_start=pattern_start + 1,
#| len=middle_len,
#| ) {
#| break Some(candidate)
#| }
#| continue candidate
#| } nobreak {
#| None
#| }
#|}
#|fn find_rabin_karp_from(
#| target : BytesView,
#| pattern : BytesView,
#| start : Int32,
#|) -> Int32? {
#| fn hash(bytes : BytesView, start : Int32, length : Int32) -> UInt32 {
#| for i in 0.. UInt32 {
#| for pow = 1U, square = find_prime_rk, exp = length; exp > 0; {
#| let next_pow = if exp % 2 == 1 { pow * square } else { pow }
#| continue next_pow, square * square, exp / 2
#| } nobreak {
#| pow
#| }
#| }
#| let pattern_len = pattern.length()
#| let target_len = target.length()
#| if start + pattern_len > target_len {
#| return None
#| }
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let pattern_hash = hash(pattern, 0, pattern_len)
#| let pow = prime_pow(pattern_len)
#| let hash = hash(target, start, pattern_len)
#| if hash == pattern_hash &&
#| target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=target_start + start,
#| other_start=pattern_start,
#| len=pattern_len,
#| ) {
#| return Some(start)
#| }
#| for index = start + pattern_len, hash = hash; index < target_len; {
#| let hash = hash * find_prime_rk + target.unsafe_get(index).to_uint()
#| let hash = hash - pow * target.unsafe_get(index - pattern_len).to_uint()
#| let candidate = index - pattern_len + 1
#| if hash == pattern_hash &&
#| target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=target_start + candidate,
#| other_start=pattern_start,
#| len=pattern_len,
#| ) {
#| break Some(candidate)
#| }
#| continue index + 1, hash
#| } nobreak {
#| None
#| }
#|}
#|fn find_long_by_byte_scanner(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let last = target_len - pattern_len
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let first_byte = pattern.unsafe_get(0)
#| for pos = 0, failures = 0; pos <= last; {
#| let found = find_byte_from_view(target, pos, target_len, first_byte)
#| if found < 0 || found > last {
#| break None
#| }
#| if target
#| .data()
#| .unsafe_range_equal(
#| pattern.data(),
#| start=target_start + found + 1,
#| other_start=pattern_start + 1,
#| len=pattern_len - 1,
#| ) {
#| break Some(found)
#| }
#| let failures = failures + 1
#| if failures > find_max_bruteforce || failures > find_cutover(found) {
#| break find_rabin_karp_from(target, pattern, found + 1)
#| }
#| continue found + 1, failures
#| } nobreak {
#| None
#| }
#|}
#|pub fn BytesView::rev_find(target : BytesView, pattern : BytesView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| if pattern_len == 0 {
#| return Some(target_len)
#| }
#| if pattern_len > target_len {
#| return None
#| }
#| if pattern_len == 1 {
#| let found = rev_find_byte_from_view(
#| target,
#| 0,
#| target_len,
#| pattern.unsafe_get(0),
#| )
#| if found < 0 {
#| None
#| } else {
#| Some(found)
#| }
#| } else if pattern_len == target_len {
#| if target == pattern {
#| Some(0)
#| } else {
#| None
#| }
#| } else {
#| rev_find_by_byte_scanner(target, pattern)
#| }
#|}
#|pub fn Bytes::rev_find(target : Bytes, pattern : BytesView) -> Int32? {
#| target[:].rev_find(pattern)
#|}
#|pub fn BytesView::has_prefix(self : BytesView, prefix : BytesView) -> Bool {
#| let prefix_len = prefix.length()
#| self.length() >= prefix_len && self[:prefix_len] == prefix
#|}
#|pub fn Bytes::has_prefix(self : Bytes, prefix : BytesView) -> Bool {
#| self[:].has_prefix(prefix)
#|}
#|pub fn BytesView::has_suffix(self : BytesView, suffix : BytesView) -> Bool {
#| let self_len = self.length()
#| let suffix_len = suffix.length()
#| self_len >= suffix_len && self[self_len - suffix_len:] == suffix
#|}
#|pub fn Bytes::has_suffix(self : Bytes, suffix : BytesView) -> Bool {
#| self[:].has_suffix(suffix)
#|}
#|#alias(chop_prefix, deprecated="Use `strip_prefix` instead")
#|pub fn BytesView::strip_prefix(
#| self : BytesView,
#| prefix : BytesView,
#|) -> BytesView? {
#| let prefix_len = prefix.length()
#| if self.length() >= prefix_len && self[:prefix_len] == prefix {
#| Some(self[prefix_len:])
#| } else {
#| None
#| }
#|}
#|#alias(chop_prefix, deprecated="Use `strip_prefix` instead")
#|pub fn Bytes::strip_prefix(self : Bytes, prefix : BytesView) -> BytesView? {
#| self[:].strip_prefix(prefix)
#|}
#|#alias(chop_suffix, deprecated="Use `strip_suffix` instead")
#|pub fn BytesView::strip_suffix(
#| self : BytesView,
#| suffix : BytesView,
#|) -> BytesView? {
#| let self_len = self.length()
#| let suffix_len = suffix.length()
#| if self_len >= suffix_len && self[self_len - suffix_len:] == suffix {
#| Some(self[:self_len - suffix_len])
#| } else {
#| None
#| }
#|}
#|#alias(chop_suffix, deprecated="Use `strip_suffix` instead")
#|pub fn Bytes::strip_suffix(self : Bytes, suffix : BytesView) -> BytesView? {
#| self[:].strip_suffix(suffix)
#|}
),
"bytes_unsafe.mbt": (
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_write_uint64_le")
#|pub fn FixedArray::unsafe_write_uint64_le(
#| bytes : FixedArray[Byte],
#| index : Int32,
#| value : UInt64,
#|) -> Unit {
#| bytes.unsafe_set(index, value.to_byte())
#| bytes.unsafe_set(index + 1, (value >> 8).to_byte())
#| bytes.unsafe_set(index + 2, (value >> 16).to_byte())
#| bytes.unsafe_set(index + 3, (value >> 24).to_byte())
#| bytes.unsafe_set(index + 4, (value >> 32).to_byte())
#| bytes.unsafe_set(index + 5, (value >> 40).to_byte())
#| bytes.unsafe_set(index + 6, (value >> 48).to_byte())
#| bytes.unsafe_set(index + 7, (value >> 56).to_byte())
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_write_uint64_be")
#|pub fn FixedArray::unsafe_write_uint64_be(
#| bytes : FixedArray[Byte],
#| index : Int32,
#| value : UInt64,
#|) -> Unit {
#| bytes.unsafe_set(index, (value >> 56).to_byte())
#| bytes.unsafe_set(index + 1, (value >> 48).to_byte())
#| bytes.unsafe_set(index + 2, (value >> 40).to_byte())
#| bytes.unsafe_set(index + 3, (value >> 32).to_byte())
#| bytes.unsafe_set(index + 4, (value >> 24).to_byte())
#| bytes.unsafe_set(index + 5, (value >> 16).to_byte())
#| bytes.unsafe_set(index + 6, (value >> 8).to_byte())
#| bytes.unsafe_set(index + 7, value.to_byte())
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_write_uint32_le")
#|pub fn FixedArray::unsafe_write_uint32_le(
#| bytes : FixedArray[Byte],
#| index : Int32,
#| value : UInt32,
#|) -> Unit {
#| bytes.unsafe_set(index, value.to_byte())
#| bytes.unsafe_set(index + 1, (value >> 8).to_byte())
#| bytes.unsafe_set(index + 2, (value >> 16).to_byte())
#| bytes.unsafe_set(index + 3, (value >> 24).to_byte())
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_write_uint32_be")
#|pub fn FixedArray::unsafe_write_uint32_be(
#| bytes : FixedArray[Byte],
#| index : Int32,
#| value : UInt32,
#|) -> Unit {
#| bytes.unsafe_set(index, (value >> 24).to_byte())
#| bytes.unsafe_set(index + 1, (value >> 16).to_byte())
#| bytes.unsafe_set(index + 2, (value >> 8).to_byte())
#| bytes.unsafe_set(index + 3, value.to_byte())
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_write_uint16_le")
#|pub fn FixedArray::unsafe_write_uint16_le(
#| bytes : FixedArray[Byte],
#| index : Int32,
#| value : UInt16,
#|) -> Unit {
#| bytes.unsafe_set(index, value.to_byte())
#| bytes.unsafe_set(index + 1, (value >> 8).to_byte())
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_write_uint16_be")
#|pub fn FixedArray::unsafe_write_uint16_be(
#| bytes : FixedArray[Byte],
#| index : Int32,
#| value : UInt16,
#|) -> Unit {
#| bytes.unsafe_set(index, (value >> 8).to_byte())
#| bytes.unsafe_set(index + 1, value.to_byte())
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint64_le")
#|#doc(hidden)
#|pub fn Bytes::unsafe_read_uint64_le(bytes : Bytes, index : Int32) -> UInt64 {
#| bytes.unsafe_get(index).to_uint64() |
#| (bytes.unsafe_get(index + 1).to_uint64() << 8) |
#| (bytes.unsafe_get(index + 2).to_uint64() << 16) |
#| (bytes.unsafe_get(index + 3).to_uint64() << 24) |
#| (bytes.unsafe_get(index + 4).to_uint64() << 32) |
#| (bytes.unsafe_get(index + 5).to_uint64() << 40) |
#| (bytes.unsafe_get(index + 6).to_uint64() << 48) |
#| (bytes.unsafe_get(index + 7).to_uint64() << 56)
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint64_be")
#|#doc(hidden)
#|pub fn Bytes::unsafe_read_uint64_be(bytes : Bytes, index : Int32) -> UInt64 {
#| (bytes.unsafe_get(index).to_uint64() << 56) |
#| (bytes.unsafe_get(index + 1).to_uint64() << 48) |
#| (bytes.unsafe_get(index + 2).to_uint64() << 40) |
#| (bytes.unsafe_get(index + 3).to_uint64() << 32) |
#| (bytes.unsafe_get(index + 4).to_uint64() << 24) |
#| (bytes.unsafe_get(index + 5).to_uint64() << 16) |
#| (bytes.unsafe_get(index + 6).to_uint64() << 8) |
#| bytes.unsafe_get(index + 7).to_uint64()
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint32_le")
#|#doc(hidden)
#|pub fn Bytes::unsafe_read_uint32_le(bytes : Bytes, index : Int32) -> UInt32 {
#| bytes.unsafe_get(index).to_uint() |
#| (bytes.unsafe_get(index + 1).to_uint() << 8) |
#| (bytes.unsafe_get(index + 2).to_uint() << 16) |
#| (bytes.unsafe_get(index + 3).to_uint() << 24)
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint32_be")
#|#doc(hidden)
#|pub fn Bytes::unsafe_read_uint32_be(bytes : Bytes, index : Int32) -> UInt32 {
#| (bytes.unsafe_get(index).to_uint() << 24) |
#| (bytes.unsafe_get(index + 1).to_uint() << 16) |
#| (bytes.unsafe_get(index + 2).to_uint() << 8) |
#| bytes.unsafe_get(index + 3).to_uint()
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint16_le")
#|#doc(hidden)
#|pub fn Bytes::unsafe_read_uint16_le(bytes : Bytes, index : Int32) -> UInt16 {
#| bytes.unsafe_get(index).to_uint16() |
#| (bytes.unsafe_get(index + 1).to_uint16() << 8)
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint16_be")
#|#doc(hidden)
#|pub fn Bytes::unsafe_read_uint16_be(bytes : Bytes, index : Int32) -> UInt16 {
#| (bytes.unsafe_get(index).to_uint16() << 8) |
#| bytes.unsafe_get(index + 1).to_uint16()
#|}
#|#doc(hidden)
#|pub fn BytesView::unsafe_read_uint64_le(
#| bytes : BytesView,
#| index : Int32,
#|) -> UInt64 {
#| bytes.bytes().unsafe_read_uint64_le(bytes.start() + index)
#|}
#|#doc(hidden)
#|pub fn BytesView::unsafe_read_uint64_be(
#| bytes : BytesView,
#| index : Int32,
#|) -> UInt64 {
#| bytes.bytes().unsafe_read_uint64_be(bytes.start() + index)
#|}
#|#doc(hidden)
#|pub fn BytesView::unsafe_read_uint32_le(
#| bytes : BytesView,
#| index : Int32,
#|) -> UInt32 {
#| bytes.bytes().unsafe_read_uint32_le(bytes.start() + index)
#|}
#|#doc(hidden)
#|pub fn BytesView::unsafe_read_uint32_be(
#| bytes : BytesView,
#| index : Int32,
#|) -> UInt32 {
#| bytes.bytes().unsafe_read_uint32_be(bytes.start() + index)
#|}
#|#doc(hidden)
#|pub fn BytesView::unsafe_read_uint16_le(
#| bytes : BytesView,
#| index : Int32,
#|) -> UInt16 {
#| bytes.bytes().unsafe_read_uint16_le(bytes.start() + index)
#|}
#|#doc(hidden)
#|pub fn BytesView::unsafe_read_uint16_be(
#| bytes : BytesView,
#| index : Int32,
#|) -> UInt16 {
#| bytes.bytes().unsafe_read_uint16_be(bytes.start() + index)
#|}
#|#borrow(self, other)
#|#cfg(target="wasm")
#|#intrinsic("%bytes.unsafe_range_equal")
#|pub fn Bytes::unsafe_range_equal(
#| self : Bytes,
#| start~ : Int32,
#| other : Bytes,
#| other_start~ : Int32,
#| len~ : Int32,
#|) -> Bool {
#| if len >= 16 {
#| return bytes_range_equal_simd(self, start, other, other_start, len)
#| }
#| for i in 0.. Bool {
#| for i in 0.. Bool {
#| let left_data = unsafe_from_bytes(left)
#| let right_data = unsafe_from_bytes(right)
#| for i = 0; i + 16 <= len; i = i + 16 {
#| let a = v128_load(left_data, left_off + i)
#| let b = v128_load(right_data, right_off + i)
#| guard i8x16_all_true(i8x16_eq(a, b)) else { return false }
#| }
#| if (len & 15) != 0 {
#| let a = v128_load(left_data, left_off + len - 16)
#| let b = v128_load(right_data, right_off + len - 16)
#| return i8x16_all_true(i8x16_eq(a, b))
#| }
#| true
#|}
#|fn buffer_to_fixedarray(buf : UninitializedArray[Byte]) -> FixedArray[Byte] = "%identity"
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint16_le")
#|fn fixedarray_read_uint16_le(bytes : FixedArray[Byte], index : Int32) -> UInt16 {
#| bytes.unsafe_get(index).to_uint16() |
#| (bytes.unsafe_get(index + 1).to_uint16() << 8)
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint16_be")
#|fn fixedarray_read_uint16_be(bytes : FixedArray[Byte], index : Int32) -> UInt16 {
#| (bytes.unsafe_get(index).to_uint16() << 8) |
#| bytes.unsafe_get(index + 1).to_uint16()
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint32_le")
#|fn fixedarray_read_uint32_le(bytes : FixedArray[Byte], index : Int32) -> UInt32 {
#| bytes.unsafe_get(index).to_uint() |
#| (bytes.unsafe_get(index + 1).to_uint() << 8) |
#| (bytes.unsafe_get(index + 2).to_uint() << 16) |
#| (bytes.unsafe_get(index + 3).to_uint() << 24)
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint32_be")
#|fn fixedarray_read_uint32_be(bytes : FixedArray[Byte], index : Int32) -> UInt32 {
#| (bytes.unsafe_get(index).to_uint() << 24) |
#| (bytes.unsafe_get(index + 1).to_uint() << 16) |
#| (bytes.unsafe_get(index + 2).to_uint() << 8) |
#| bytes.unsafe_get(index + 3).to_uint()
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint64_le")
#|fn fixedarray_read_uint64_le(bytes : FixedArray[Byte], index : Int32) -> UInt64 {
#| bytes.unsafe_get(index).to_uint64() |
#| (bytes.unsafe_get(index + 1).to_uint64() << 8) |
#| (bytes.unsafe_get(index + 2).to_uint64() << 16) |
#| (bytes.unsafe_get(index + 3).to_uint64() << 24) |
#| (bytes.unsafe_get(index + 4).to_uint64() << 32) |
#| (bytes.unsafe_get(index + 5).to_uint64() << 40) |
#| (bytes.unsafe_get(index + 6).to_uint64() << 48) |
#| (bytes.unsafe_get(index + 7).to_uint64() << 56)
#|}
#|#borrow(bytes)
#|#intrinsic("%bytes.unsafe_read_uint64_be")
#|fn fixedarray_read_uint64_be(bytes : FixedArray[Byte], index : Int32) -> UInt64 {
#| (bytes.unsafe_get(index).to_uint64() << 56) |
#| (bytes.unsafe_get(index + 1).to_uint64() << 48) |
#| (bytes.unsafe_get(index + 2).to_uint64() << 40) |
#| (bytes.unsafe_get(index + 3).to_uint64() << 32) |
#| (bytes.unsafe_get(index + 4).to_uint64() << 24) |
#| (bytes.unsafe_get(index + 5).to_uint64() << 16) |
#| (bytes.unsafe_get(index + 6).to_uint64() << 8) |
#| bytes.unsafe_get(index + 7).to_uint64()
#|}
),
"bytesview.mbt": (
#|fn BytesView::bytes(self : BytesView) -> Bytes = "%bytesview.bytes"
#|fn BytesView::start(self : BytesView) -> Int32 = "%bytesview.start"
#|fn BytesView::end(self : BytesView) -> Int32 = "%bytesview.end"
#|#intrinsic("%bytesview.length")
#|pub fn BytesView::length(self : BytesView) -> Int32 {
#| self.end() - self.start()
#|}
#|pub fn BytesView::is_empty(self : BytesView) -> Bool {
#| self.length() == 0
#|}
#|#intrinsic("%bytesview.get")
#|#alias("_[_]")
#|pub fn BytesView::at(self : BytesView, index : Int32) -> Byte {
#| guard index >= 0 && index < self.length() else {
#| index_out_of_bounds(self.length(), index)
#| }
#| self.bytes().unsafe_get(self.start() + index)
#|}
#|#intrinsic("%bytesview.get_opt")
#|pub fn BytesView::get(self : BytesView, index : Int32) -> Byte? {
#| guard index >= 0 && index < self.length() else { None }
#| Some(self.bytes().unsafe_get(self.start() + index))
#|}
#|#intrinsic("%bytesview.unsafe_get")
#|#internal(unsafe, "Panic if index is out of bounds")
#|#doc(hidden)
#|pub fn BytesView::unsafe_get(self : BytesView, index : Int32) -> Byte {
#| self.bytes().unsafe_get(self.start() + index)
#|}
#|#intrinsic("%bytes.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn Bytes::exact_view(
#| self : Bytes,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> BytesView {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("Invalid index for View")
#| }
#| self.unsafe_view(start~, end~)
#|}
#|#intrinsic("%bytes.view_opt")
#|pub fn Bytes::get_view(
#| self : Bytes,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> BytesView? {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else { None }
#| Some(self.unsafe_view(start~, end~))
#|}
#|#intrinsic("%bytesview.view_opt")
#|pub fn BytesView::get_view(
#| self : BytesView,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> BytesView? {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else { None }
#| Some(self.unsafe_view(start~, end~))
#|}
#|#intrinsic("%bytesview.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn BytesView::exact_view(
#| self : BytesView,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> BytesView {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("Invalid index for View")
#| }
#| self.unsafe_view(start~, end~)
#|}
#|#intrinsic("%bytes.clamped_view")
#|#alias("_[_:_]")
#|pub fn Bytes::clamped_view(
#| self : Bytes,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> BytesView {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| self.unsafe_view(start=lo, end=hi)
#|}
#|#intrinsic("%bytesview.clamped_view")
#|#alias("_[_:_]")
#|pub fn BytesView::clamped_view(
#| self : BytesView,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> BytesView {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| self.unsafe_view(start=lo, end=hi)
#|}
#|#alias(iterator, deprecated)
#|pub fn BytesView::iter(self : BytesView) -> Iter[Byte] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let result = self.unsafe_get(i)
#| i += 1
#| Some(result)
#| },
#| size_hint=len,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn BytesView::iter2(self : BytesView) -> Iter2[Int32, Byte] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let result = (i, self.unsafe_get(i))
#| i += 1
#| Some(result)
#| },
#| size_hint=len,
#| )
#|}
#|fn[T : Logger] write_byte_hex(logger : T, byte : Byte) -> Unit {
#| fn hex_digit(value : Int32) -> Char {
#| if value < 10 {
#| (value + '0'.to_int()).unsafe_to_char()
#| } else {
#| (value - 10 + 'a'.to_int()).unsafe_to_char()
#| }
#| }
#| let value = byte.to_int()
#| logger.write_char(hex_digit(value >> 4))
#| logger.write_char(hex_digit(value & 0x0f))
#|}
#|fn[T : Logger] BytesView::escape_to(self : BytesView, logger : T) -> Unit {
#| for byte in self {
#| if byte is (b' '..<=b'~') && byte != b'"' && byte != b'\\' {
#| logger.write_char(byte.to_char())
#| } else {
#| logger <+ "\\x\{cb => write_byte_hex(cb, byte)}"
#| }
#| }
#|}
#|pub impl Show for BytesView with fn output(self, logger) {
#| logger <+ "b\"\{cb => self.escape_to(cb)}\""
#|}
#|pub impl Show for Bytes with fn output(self, logger) {
#| Show::output(self[:], logger)
#|}
#|pub impl Eq for BytesView with fn equal(self, other) -> Bool {
#| let len = self.length()
#| guard len == other.length() else { return false }
#| self
#| .bytes()
#| .unsafe_range_equal(
#| other.bytes(),
#| start=self.start(),
#| other_start=other.start(),
#| len~,
#| )
#|}
#|pub fn BytesView::equal_to_bytes(self : BytesView, other : Bytes) -> Bool {
#| let len = self.length()
#| guard len == other.length() else { return false }
#| self
#| .bytes()
#| .unsafe_range_equal(other, start=self.start(), other_start=0, len~)
#|}
#|pub impl Compare for BytesView with fn compare(self, other) -> Int32 {
#| let self_len = self.length()
#| let other_len = other.length()
#| let cmp = self_len.compare(other_len)
#| guard cmp == 0 else { return cmp }
#| self.lexical_compare(other)
#|}
#|pub fn BytesView::lexical_compare(self : BytesView, other : BytesView) -> Int32 {
#| self.bytesview_lexical_compare_impl(other)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn BytesView::bytesview_lexical_compare_impl(
#| self : BytesView,
#| other : BytesView,
#|) -> Int32 {
#| let self_len = self.length()
#| let other_len = other.length()
#| let min_len = if self_len < other_len { self_len } else { other_len }
#| if min_len >= 16 {
#| let x = self.unsafe_get(0)
#| let y = other.unsafe_get(0)
#| if x != y {
#| return if x < y { -1 } else { 1 }
#| }
#| let x = self.unsafe_get(1)
#| let y = other.unsafe_get(1)
#| if x != y {
#| return if x < y { -1 } else { 1 }
#| }
#| }
#| let self_bytes = unsafe_from_bytes(self.bytes())
#| let other_bytes = unsafe_from_bytes(other.bytes())
#| let self_start = self.start()
#| let other_start = other.start()
#| let mut i = 0
#| let block_limit = min_len - min_len % 16
#| while i < block_limit {
#| let a = v128_load(self_bytes, self_start + i)
#| let b = v128_load(other_bytes, other_start + i)
#| let diff_bits = i8x16_bitmask(i8x16_eq(a, b)) ^ 0xFFFF
#| if diff_bits != 0 {
#| let lane = diff_bits.ctz()
#| return self_bytes
#| .unsafe_get(self_start + i + lane)
#| .compare(other_bytes.unsafe_get(other_start + i + lane))
#| }
#| i += 16
#| }
#| while i < min_len {
#| let x = self.unsafe_get(i)
#| let y = other.unsafe_get(i)
#| if x != y {
#| return if x < y { -1 } else { 1 }
#| }
#| i += 1
#| }
#| self_len.compare(other_len)
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn BytesView::bytesview_lexical_compare_impl(
#| self : BytesView,
#| other : BytesView,
#|) -> Int32 {
#| let self_len = self.length()
#| let other_len = other.length()
#| let min_len = if self_len < other_len { self_len } else { other_len }
#| for i in 0.. Bytes {
#| self.bytes()
#|}
#|pub fn BytesView::start_offset(self : BytesView) -> Int32 {
#| self.start()
#|}
#|#alias(to_bytes, deprecated="Use `to_owned` to allocate an owned `Bytes` from a `BytesView`")
#|pub fn BytesView::to_owned(self : BytesView) -> Bytes {
#| if self.length() == self.bytes().length() {
#| return self.bytes()
#| }
#| let bytes = FixedArray::make(self.length(), (0 : Byte))
#| bytes.blit_from_bytes(0, self.bytes(), self.start_offset(), self.length())
#| unsafe_to_bytes(bytes)
#|}
),
"char.mbt": (
#|fn Char::to_hex(char : Char) -> String {
#| let code = char.to_int()
#| match code {
#| 0..<=0xFF => code.to_byte().to_hex()
#| _..<=0xFFFF => (code >> 8).to_byte().to_hex() + code.to_byte().to_hex()
#| _ =>
#| (code >> 16).to_byte().to_hex() +
#| (code >> 8).to_byte().to_hex() +
#| code.to_byte().to_hex()
#| }
#|}
#|test "Char::to_hex" {
#| inspect(Char::to_hex('A'), content="41")
#| inspect(Char::to_hex('a'), content="61")
#| inspect(Char::to_hex('0'), content="30")
#| inspect(Char::to_hex('\n'), content="0a")
#| inspect(Char::to_hex('\u{0}'), content="00")
#| inspect(Char::to_hex('\u{FF}'), content="ff")
#| inspect(Char::to_hex('\u{100}'), content="0100")
#| inspect(Char::to_hex('\u{1000}'), content="1000")
#| inspect(Char::to_hex('😀'), content="01f600")
#|}
#|pub impl Hash for Char with fn hash_combine(self, hasher) -> Unit {
#| hasher.combine_char(self)
#|}
#|pub impl Hash for Char with fn hash(self : Char) -> Int32 {
#| Hash::hash(self.to_uint())
#|}
#|pub fn Char::is_ascii(self : Self) -> Bool {
#| self is ('\u{00}'..<='\u{7F}')
#|}
#|pub fn Char::is_ascii_alphabetic(self : Self) -> Bool {
#| self is ('A'..<='Z' | 'a'..<='z')
#|}
#|pub fn Char::is_ascii_control(self : Self) -> Bool {
#| self is ('\u{00}'..<='\u{1F}' | '\u{7F}')
#|}
#|pub fn Char::is_ascii_digit(self : Self) -> Bool {
#| self is ('0'..<='9')
#|}
#|pub fn Char::is_ascii_graphic(self : Self) -> Bool {
#| self is ('\u{21}'..<='\u{7E}')
#|}
#|pub fn Char::is_ascii_hexdigit(self : Self) -> Bool {
#| self is ('0'..<='9' | 'A'..<='F' | 'a'..<='f')
#|}
#|pub fn Char::is_ascii_lowercase(self : Self) -> Bool {
#| self is ('a'..<='z')
#|}
#|pub fn Char::is_ascii_octdigit(self : Self) -> Bool {
#| self is ('0'..<='7')
#|}
#|pub fn Char::is_ascii_punctuation(self : Self) -> Bool {
#| self
#| is ('\u{21}'..<='\u{2F}'
#| | '\u{3A}'..<='\u{40}'
#| | '\u{5B}'..<='\u{60}'
#| | '\u{7B}'..<='\u{7E}')
#|}
#|pub fn Char::is_ascii_uppercase(self : Self) -> Bool {
#| self is ('A'..<='Z')
#|}
#|pub fn Char::is_ascii_whitespace(self : Self) -> Bool {
#| self is ('\u{09}'..<='\u{0D}' | ' ')
#|}
#|pub fn Char::is_control(self : Self) -> Bool {
#| self is ('\u0000'..<='\u001F' | '\u007F'..<='\u009F')
#|}
#|pub fn Char::is_digit(self : Self, radix : UInt32) -> Bool {
#| let v = self.to_uint()
#| match radix {
#| 2..<=10 => v >= 48 && v <= radix + 47
#| 11..<=36 =>
#| (v >= 48 && v <= 57) ||
#| (v >= 65 && v <= radix + 54) ||
#| (v >= 97 && v <= radix + 86)
#| _ => panic()
#| }
#|}
#|pub fn Char::is_whitespace(self : Self) -> Bool {
#| if self <= '\u{20}' {
#| return self.is_ascii_whitespace()
#| }
#| self
#| is ('\u0085'
#| | '\u00A0'
#| | '\u1680'
#| | '\u2000'..<='\u200A'
#| | '\u2028'
#| | '\u2029'
#| | '\u202F'
#| | '\u205F'
#| | '\u3000')
#|}
#|pub fn Char::is_numeric(self : Self) -> Bool {
#| if self is ('0'..<='9') {
#| return true
#| }
#| if self < '\u{B2}' {
#| return false
#| }
#| self
#| is ('\u00B2'
#| | '\u00B3'
#| | '\u00B9'
#| | '\u00BC'
#| | '\u00BD'
#| | '\u00BE'
#| | '\u0660'..<='\u0669'
#| | '\u06F0'..<='\u06F9'
#| | '\u07C0'..<='\u07C9'
#| | '\u0966'..<='\u096F'
#| | '\u09E6'..<='\u09EF'
#| | '\u09F4'..<='\u09F9'
#| | '\u0A66'..<='\u0A6F'
#| | '\u0AE6'..<='\u0AEF'
#| | '\u0B66'..<='\u0B6F'
#| | '\u0B72'..<='\u0B77'
#| | '\u0BE6'..<='\u0BEF'
#| | '\u0BF0'..<='\u0BF2'
#| | '\u0C66'..<='\u0C6F'
#| | '\u0C78'..<='\u0C7E'
#| | '\u0CE6'..<='\u0CEF'
#| | '\u0D58'..<='\u0D5E'
#| | '\u0D66'..<='\u0D6F'
#| | '\u0D70'..<='\u0D78'
#| | '\u0DE6'..<='\u0DEF'
#| | '\u0E50'..<='\u0E59'
#| | '\u0ED0'..<='\u0ED9'
#| | '\u0F20'..<='\u0F33'
#| | '\u1040'..<='\u1049'
#| | '\u1090'..<='\u1099'
#| | '\u1369'..<='\u137C'
#| | '\u16EE'..<='\u16F0'
#| | '\u17E0'..<='\u17E9'
#| | '\u17F0'..<='\u17F9'
#| | '\u1810'..<='\u1819'
#| | '\u1946'..<='\u194F'
#| | '\u19D0'..<='\u19DA'
#| | '\u1A80'..<='\u1A89'
#| | '\u1A90'..<='\u1A99'
#| | '\u1B50'..<='\u1B59'
#| | '\u1BB0'..<='\u1BB9'
#| | '\u1C40'..<='\u1C49'
#| | '\u1C50'..<='\u1C59'
#| | '\u2070'
#| | '\u2074'..<='\u2079'
#| | '\u2080'..<='\u2089'
#| | '\u2150'..<='\u2182'
#| | '\u2185'..<='\u2189'
#| | '\u2460'..<='\u249B'
#| | '\u24EA'..<='\u24FF'
#| | '\u2776'..<='\u2793'
#| | '\u2CFD'
#| | '\u3007'
#| | '\u3021'..<='\u3029'
#| | '\u3038'..<='\u303A'
#| | '\u3192'..<='\u3195'
#| | '\u3220'..<='\u3229'
#| | '\u3248'..<='\u324F'
#| | '\u3251'..<='\u325F'
#| | '\u3280'..<='\u3289'
#| | '\u32B1'..<='\u32BF'
#| | '\uA620'..<='\uA629'
#| | '\uA6E6'..<='\uA6EF'
#| | '\uA830'..<='\uA835'
#| | '\uA8D0'..<='\uA8D9'
#| | '\uA900'..<='\uA909'
#| | '\uA9D0'..<='\uA9D9'
#| | '\uA9F0'..<='\uA9F9'
#| | '\uAA50'..<='\uAA59'
#| | '\uABF0'..<='\uABF9'
#| | '\uFF10'..<='\uFF19'
#| | '\u{10107}'..<='\u{10133}'
#| | '\u{10140}'..<='\u{10178}'
#| | '\u{1018A}'..<='\u{1018B}'
#| | '\u{102E1}'..<='\u{102FB}'
#| | '\u{10320}'..<='\u{10323}'
#| | '\u{10341}'
#| | '\u{1034A}'
#| | '\u{103D1}'..<='\u{103D5}'
#| | '\u{104A0}'..<='\u{104A9}'
#| | '\u{10858}'..<='\u{1085F}'
#| | '\u{10879}'..<='\u{1087F}'
#| | '\u{108A7}'..<='\u{108AF}'
#| | '\u{108FB}'..<='\u{108FF}'
#| | '\u{10916}'..<='\u{1091B}'
#| | '\u{109BC}'..<='\u{109BD}'
#| | '\u{109C0}'..<='\u{109CF}'
#| | '\u{109D2}'..<='\u{109FF}'
#| | '\u{10A40}'..<='\u{10A48}'
#| | '\u{10A7D}'..<='\u{10A7E}'
#| | '\u{10A9D}'..<='\u{10A9F}'
#| | '\u{10AEB}'..<='\u{10AEF}'
#| | '\u{10B58}'..<='\u{10B5F}'
#| | '\u{10B78}'..<='\u{10B7F}'
#| | '\u{10BA9}'..<='\u{10BAF}'
#| | '\u{10CFA}'..<='\u{10CFF}'
#| | '\u{10D30}'..<='\u{10D39}'
#| | '\u{10D40}'..<='\u{10D49}'
#| | '\u{10E60}'..<='\u{10E7E}'
#| | '\u{10F1D}'..<='\u{10F26}'
#| | '\u{10F51}'..<='\u{10F54}'
#| | '\u{10FC5}'..<='\u{10FCB}'
#| | '\u{11052}'..<='\u{1106F}'
#| | '\u{110F0}'..<='\u{110F9}'
#| | '\u{11136}'..<='\u{1113F}'
#| | '\u{111D0}'..<='\u{111D9}'
#| | '\u{111E1}'..<='\u{111F4}'
#| | '\u{112F0}'..<='\u{112F9}'
#| | '\u{11450}'..<='\u{11459}'
#| | '\u{114D0}'..<='\u{114D9}'
#| | '\u{11650}'..<='\u{11659}'
#| | '\u{116C0}'..<='\u{116C9}'
#| | '\u{116D0}'..<='\u{116E3}'
#| | '\u{11730}'..<='\u{1173B}'
#| | '\u{118E0}'..<='\u{118F2}'
#| | '\u{11950}'..<='\u{11959}'
#| | '\u{11BF0}'..<='\u{11BF9}'
#| | '\u{11C50}'..<='\u{11C6C}'
#| | '\u{11D50}'..<='\u{11D59}'
#| | '\u{11DA0}'..<='\u{11DA9}'
#| | '\u{11F50}'..<='\u{11F59}'
#| | '\u{11FC0}'..<='\u{11FD4}'
#| | '\u{12400}'..<='\u{1246E}'
#| | '\u{16130}'..<='\u{16139}'
#| | '\u{16A60}'..<='\u{16A69}'
#| | '\u{16AC0}'..<='\u{16AC9}'
#| | '\u{16B50}'..<='\u{16B59}'
#| | '\u{16B5B}'..<='\u{16B61}'
#| | '\u{16D70}'..<='\u{16D79}'
#| | '\u{16E80}'..<='\u{16E96}'
#| | '\u{1CCF0}'..<='\u{1CCF9}'
#| | '\u{1D2C0}'..<='\u{1D2D3}'
#| | '\u{1D2E0}'..<='\u{1D2F3}'
#| | '\u{1D360}'..<='\u{1D378}'
#| | '\u{1D7CE}'..<='\u{1D7FF}'
#| | '\u{1E140}'..<='\u{1E149}'
#| | '\u{1E2F0}'..<='\u{1E2F9}'
#| | '\u{1E4F0}'..<='\u{1E4F9}'
#| | '\u{1E5F1}'..<='\u{1E5FA}'
#| | '\u{1E8C7}'..<='\u{1E8CF}'
#| | '\u{1E950}'..<='\u{1E959}'
#| | '\u{1EC71}'..<='\u{1ECAB}'
#| | '\u{1ECAD}'..<='\u{1ECAF}'
#| | '\u{1ECB1}'..<='\u{1ECB4}'
#| | '\u{1ED01}'..<='\u{1ED2D}'
#| | '\u{1ED2F}'..<='\u{1ED3D}'
#| | '\u{1F100}'..<='\u{1F10C}'
#| | '\u{1FBF0}'..<='\u{1FBF9}')
#|}
#|pub fn Char::is_printable(self : Self) -> Bool {
#| if self.is_control() {
#| return false
#| }
#| let self = self.to_int()
#| if self is (0xE000..<=0xF8FF | 0xF0000..<=0xFFFFD | 0x100000..<=0x10FFFD) {
#| return false
#| }
#| if self
#| is ('\u{00AD}'
#| | '\u{0600}'..<='\u{0605}'
#| | '\u{061C}'
#| | '\u{06DD}'
#| | '\u{070F}'
#| | '\u{0890}'..<='\u{0891}'
#| | '\u{08E2}'
#| | '\u{180E}'
#| | '\u{200B}'..<='\u{200F}'
#| | '\u{202A}'..<='\u{202E}'
#| | '\u{2060}'..<='\u{2064}'
#| | '\u{2066}'..<='\u{206F}'
#| | '\u{feff}'
#| | '\u{FFF9}'..<='\u{FFFB}'
#| | '\u{110BD}'
#| | '\u{110CD}'
#| | '\u{13430}'..<='\u{1343F}'
#| | '\u{1BCA0}'..<='\u{1BCA3}'
#| | '\u{1D173}'..<='\u{1D17A}'
#| | '\u{E0001}'
#| | '\u{E0020}'..<='\u{E007F}') {
#| return false
#| }
#| if self.is_surrogate() {
#| return false
#| }
#| if self == '\u{2028}' || self == '\u{2029}' {
#| return false
#| }
#| if self
#| is (0xFDD0..<=0xFDEF
#| | 0xFFFE..<=0xFFFF
#| | 0x1FFFE..<=0x1FFFF
#| | 0x2FFFE..<=0x2FFFF
#| | 0x3FFFE..<=0x3FFFF
#| | 0x4FFFE..<=0x4FFFF
#| | 0x5FFFE..<=0x5FFFF
#| | 0x6FFFE..<=0x6FFFF
#| | 0x7FFFE..<=0x7FFFF
#| | 0x8FFFE..<=0x8FFFF
#| | 0x9FFFE..<=0x9FFFF
#| | 0xAFFFE..<=0xAFFFF
#| | 0xBFFFE..<=0xBFFFF
#| | 0xCFFFE..<=0xCFFFF
#| | 0xDFFFE..<=0xDFFFF
#| | 0xEFFFE..<=0xEFFFF
#| | 0xFFFFE..<=0xFFFFF
#| | 0x10FFFE..<=0x10FFFF) {
#| return false
#| }
#| true
#|}
#|pub fn Char::to_ascii_lowercase(self : Self) -> Char {
#| if self.is_ascii_uppercase() {
#| return (self.to_int() + 32).unsafe_to_char()
#| }
#| self
#|}
#|pub fn Char::to_ascii_uppercase(self : Self) -> Char {
#| if self.is_ascii_lowercase() {
#| return (self.to_int() - 32).unsafe_to_char()
#| }
#| self
#|}
#|pub impl Show for Char with fn to_string(self : Char) -> String {
#| char_to_string(self)
#|}
#|#intrinsic("%char.to_string")
#|fn char_to_string(char : Char) -> String {
#| [char]
#|}
#|pub fn Char::escape(self : Char, quote~ : Bool = true) -> String {
#| let buf = StringBuilder()
#| self.escape_to(buf, quote~)
#| buf.to_string()
#|}
#|fn Char::escape_to(self : Char, logger : &Logger, quote~ : Bool = true) -> Unit {
#| if quote {
#| logger.write_char('\'')
#| }
#| match self {
#| '\'' | '\\' => {
#| logger.write_char('\\')
#| logger.write_char(self)
#| }
#| '\n' => logger <+ "\\n"
#| '\r' => logger <+ "\\r"
#| '\b' => logger <+ "\\b"
#| '\t' => logger <+ "\\t"
#| ' '..<='~' => logger.write_char(self)
#| _ =>
#| if !self.is_printable() {
#| logger <+ "\\u{\{self.to_hex()}}"
#| } else {
#| logger.write_char(self)
#| }
#| }
#| if quote {
#| logger.write_char('\'')
#| }
#|}
#|#alias(length, deprecated="Use `utf16_len` instead")
#|pub fn Char::utf16_len(self : Self) -> Int32 {
#| let code = self.to_int()
#| if code <= 0xFFFF {
#| 1
#| } else {
#| 2
#| }
#|}
#|pub fn Char::is_bmp(self : Self) -> Bool {
#| self.to_int() <= 0xFFFF
#|}
),
"compare_fn.mbt": (
#|pub fn[A : Compare] compare(x : A, y : A) -> Int32 {
#| Compare::compare(x, y)
#|}
),
"console.mbt": (
#|fn println_mono(s : String) -> Unit = "%println"
#|pub fn[T : Show] println(input : T) -> Unit {
#| println_mono(input.to_string())
#|}
#|fn eprintln_mono(s : String) -> Unit = "%eprintln"
#|pub fn[T : Show] eprintln(input : T) -> Unit {
#| eprintln_mono(input.to_string())
#|}
#|pub(all) suberror InspectError {
#| InspectError(String)
#|}
#|fn base64_encode(data : FixedArray[Byte]) -> String {
#| let base64 = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#| let buf = StringBuilder()
#| let len = data.length()
#| let rem = len % 3
#| for i = 0; i < len - rem; i = i + 3 {
#| let b0 = data[i].to_int()
#| let b1 = data[i + 1].to_int()
#| let b2 = data[i + 2].to_int()
#| let x0 = base64[(b0 & 0xFC) >> 2]
#| let x1 = base64[((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4)]
#| let x2 = base64[((b1 & 0x0F) << 2) | ((b2 & 0xC0) >> 6)]
#| let x3 = base64[b2 & 0x3F]
#| buf.write_char(x0.to_char())
#| buf.write_char(x1.to_char())
#| buf.write_char(x2.to_char())
#| buf.write_char(x3.to_char())
#| }
#| if rem == 1 {
#| let b0 = data[len - 1].to_int()
#| let x0 = base64[(b0 & 0xFC) >> 2]
#| let x1 = base64[(b0 & 0x03) << 4]
#| buf.write_char(x0.to_char())
#| buf.write_char(x1.to_char())
#| buf <+ "=="
#| } else if rem == 2 {
#| let b0 = data[len - 2].to_int()
#| let b1 = data[len - 1].to_int()
#| let x0 = base64[(b0 & 0xFC) >> 2]
#| let x1 = base64[((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4)]
#| let x2 = base64[(b1 & 0x0F) << 2]
#| buf.write_char(x0.to_char())
#| buf.write_char(x1.to_char())
#| buf.write_char(x2.to_char())
#| buf.write_char('=')
#| }
#| buf.to_string()
#|}
#|fn base64_encode_string_codepoint(s : String) -> String {
#| let codepoint_length = s.char_length()
#| let data : FixedArray[Byte] = FixedArray::make(codepoint_length * 4, 0)
#| for i = 0, utf16_index = 0
#| i < codepoint_length
#| i = i + 1, utf16_index = utf16_index + 1 {
#| let c = s.unsafe_char_at(utf16_index).to_int()
#| if c > 0xFFFF {
#| data[i * 4] = (c & 0xFF).to_byte()
#| data[i * 4 + 1] = ((c >> 8) & 0xFF).to_byte()
#| data[i * 4 + 2] = ((c >> 16) & 0xFF).to_byte()
#| data[i * 4 + 3] = ((c >> 24) & 0xFF).to_byte()
#| continue i + 1, utf16_index + 2
#| } else {
#| data[i * 4] = (c & 0xFF).to_byte()
#| data[i * 4 + 1] = ((c >> 8) & 0xFF).to_byte()
#| data[i * 4 + 2] = 0
#| data[i * 4 + 3] = 0
#| }
#| }
#| base64_encode(data)
#|}
#|test {
#| inspect(base64_encode_string_codepoint(""))
#| inspect(base64_encode_string_codepoint("a"), content="YQAAAA==")
#| inspect(base64_encode_string_codepoint("ab"), content="YQAAAGIAAAA=")
#| inspect(base64_encode_string_codepoint("abc"), content="YQAAAGIAAABjAAAA")
#| inspect(
#| base64_encode_string_codepoint("abcd"),
#| content="YQAAAGIAAABjAAAAZAAAAA==",
#| )
#| inspect(
#| base64_encode_string_codepoint("abcde"),
#| content="YQAAAGIAAABjAAAAZAAAAGUAAAA=",
#| )
#| inspect(base64_encode_string_codepoint("a中"), content="YQAAAC1OAAA=")
#| inspect(
#| base64_encode_string_codepoint("a中🤣"),
#| content="YQAAAC1OAAAj+QEA",
#| )
#| inspect(
#| base64_encode_string_codepoint("a中🤣a"),
#| content="YQAAAC1OAAAj+QEAYQAAAA==",
#| )
#| inspect(
#| base64_encode_string_codepoint("a中🤣中"),
#| content="YQAAAC1OAAAj+QEALU4AAA==",
#| )
#|}
#|#callsite(autofill(args_loc, loc))
#|pub fn inspect(
#| obj : &Show,
#| content~ : String = "",
#| loc~ : SourceLoc,
#| args_loc~ : ArgsLoc,
#|) -> Unit raise InspectError {
#| let actual = obj.to_string()
#| if actual != content {
#| let loc = loc.to_json_string()
#| let args_loc = args_loc.to_json()
#| let expect_escaped = content.escape()
#| let actual_escaped = actual.escape()
#| let expect_base64 = "\"\{base64_encode_string_codepoint(content)}\""
#| let actual_base64 = "\"\{base64_encode_string_codepoint(actual)}\""
#| raise InspectError(
#| (
#| $|@EXPECT_FAILED {"loc": \{loc}, "args_loc": \{args_loc}, "expect": \{expect_escaped}, "actual": \{actual_escaped}, "expect_base64": \{expect_base64}, "actual_base64": \{actual_base64}}
#| ),
#| )
#| }
#|}
#|pub(all) suberror SnapshotError {
#| SnapshotError(String)
#|}
#|#deprecated
#|pub(all) suberror BenchError {
#| BenchError(String)
#|}
#|test "panic error case of inspect" {
#| let x : Int32 = 42
#| inspect(x, content="100")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `clamped_view(start~, end~)` or `exact_view(start~, end~)` instead")
#|pub fn StringView::view(
#| self : StringView,
#| start_offset~ : Int32 = 0,
#| end_offset? : Int32,
#|) -> StringView {
#| let end_offset = if end_offset is Some(o) { o } else { self.length() }
#| guard start_offset >= 0 &&
#| start_offset <= end_offset &&
#| end_offset <= self.length() else {
#| abort("Invalid index for View")
#| }
#| self
#| .str()
#| .unsafe_view(start=self.start() + start_offset, end=self.start() + end_offset)
#|}
#|#deprecated("Use `clamped_view(start~, end~)` or `exact_view(start~, end~)` instead")
#|pub fn String::view(
#| self : String,
#| start_offset~ : Int32 = 0,
#| end_offset? : Int32,
#|) -> StringView {
#| let end_offset = if end_offset is Some(o) { o } else { self.length() }
#| guard start_offset >= 0 &&
#| start_offset <= end_offset &&
#| end_offset <= self.length() else {
#| abort("Invalid index for View")
#| }
#| self.unsafe_view(start=start_offset, end=end_offset)
#|}
#|#deprecated("Array::split is deprecated; splitting semantics are being reconsidered")
#|#locals(pred)
#|pub fn[T] Array::split(
#| self : Array[T],
#| pred : (T) -> Bool raise?,
#|) -> Array[Array[T]] raise? {
#| let chunks = []
#| for i = 0; i < self.length(); {
#| let chunk = []
#| let i = for i = i; i < self.length() && !pred(self[i]); {
#| chunk.push(self[i])
#| continue i + 1
#| } nobreak {
#| i
#| }
#| chunks.push(chunk)
#| continue i + 1
#| }
#| chunks
#|}
#|#deprecated("check `@encoding/utf8.encode`")
#|#coverage.skip
#|pub fn Bytes::of_string(str : String) -> Bytes {
#| let arr = FixedArray::make(str.length() * 2, b'\x00')
#| arr.blit_from_string(0, str, 0, str.length())
#| arr.unsafe_reinterpret_as_bytes()
#|}
#|#deprecated("Bytes are immutable. Use `FixedArray::blit_from_bytes` if it's really necessary.")
#|#alias(clone, deprecated)
#|pub fn Bytes::copy(self : Bytes) -> Bytes {
#| Bytes::makei(self.length(), i => self[i])
#|}
#|#deprecated("Use `Map([], capacity=...)` instead")
#|pub fn[K, V] Map::new(capacity~ : Int32 = default_init_capacity) -> Map[K, V] {
#| new_map(capacity)
#|}
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn Byte::lsl(self : Byte, count : Int32) -> Byte {
#| (self.to_int() << count).to_byte()
#|}
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn Byte::lsr(self : Byte, count : Int32) -> Byte {
#| (self.to_uint() >> count).reinterpret_as_int().to_byte()
#|}
#|#deprecated("Use `s.get_char(i).unwrap()` instead", skip_current_package=true)
#|pub fn String::unsafe_char_at(self : String, index : Int32) -> Char {
#| let c1 = self.unsafe_get(index)
#| if c1.is_leading_surrogate() {
#| let c2 = self.unsafe_get(index + 1)
#| code_point_of_surrogate_pair(c1.to_int(), c2.to_int())
#| } else {
#| c1.unsafe_to_char()
#| }
#|}
#|#deprecated("Use `Array(capacity=...)` instead")
#|pub fn[T] Array::new(capacity~ : Int32 = 0) -> Array[T] {
#| Array(capacity~)
#|}
#|#deprecated("Use `@range.iter(start=start, end=end, step=..., inclusive=...)` instead; `Double::until` accumulates rounding error")
#|#coverage.skip
#|pub fn Double::until(
#| self : Double,
#| end : Double,
#| step~ : Double = 1.0,
#| inclusive~ : Bool = false,
#|) -> Iter[Double] {
#| until_impl(self, end, step, inclusive)
#|}
#|#deprecated("Use `Double::to_uint` instead; `trunc_double` is undefined for NaN and out-of-range inputs", skip_current_package=true)
#|pub fn UInt32::trunc_double(val : Double) -> UInt32 = "%f64.to_u32"
#|#deprecated("Use `Double::to_uint64` instead; `trunc_double` is undefined for NaN and out-of-range inputs", skip_current_package=true)
#|pub fn UInt64::trunc_double(val : Double) -> UInt64 = "%f64.to_u64"
#|#deprecated("Use `Bytes::make(len, b'\\x00')` instead")
#|pub fn Bytes::new(len : Int32) -> Bytes {
#| Bytes::make(len, b'\x00')
#|}
#|#deprecated("The bucket count is an implementation detail; the constructor's `capacity` hint is the only sizing control")
#|pub fn[K, V] Map::capacity(self : Map[K, V]) -> Int32 {
#| self.capacity
#|}
#|#deprecated("Use `x.to_double()` (`Int::to_double`) instead")
#|pub fn Double::from_int(i : Int32) -> Double = "%i32_to_f64"
#|#deprecated("Use `x.to_int64()` (`Int::to_int64`) instead")
#|pub fn Int64::from_int(i : Int32) -> Int64 = "%i32_to_i64"
#|#deprecated("Use `x.to_double()` (`UInt::to_double`) instead")
#|pub fn Double::convert_uint(val : UInt32) -> Double = "%u32.to_f64"
#|#deprecated("Use `x.to_double()` (`UInt64::to_double`) instead")
#|pub fn Double::convert_uint64(val : UInt64) -> Double = "%u64.to_f64"
#|#deprecated("Use `x.to_uint64()` (`UInt::to_uint64`) instead")
#|pub fn UInt64::extend_uint(val : UInt32) -> UInt64 = "%u32.to_u64"
#|#deprecated("Use `Iter2(Iter(f))` or `Iter2(Iter(f, size_hint=n))` instead")
#|#owned(f)
#|pub fn[X, Y] Iter2::new(f : () -> (X, Y)?, size_hint? : Int32) -> Iter2[X, Y] {
#| let size_hint = match size_hint {
#| Some(n) if n > 0 => Some(n)
#| Some(_) => Some(0)
#| None => None
#| }
#| Iter2({ f, size_hint, })
#|}
),
"double.mbt": (
#|pub fn Double::Double(self : Double) -> Double = "%identity"
#|pub fn Double::abs(self : Double) -> Double = "%f64.abs"
#|pub fn Double::min(self : Double, other : Double) -> Double {
#| if self.is_nan() {
#| other
#| } else if other.is_nan() {
#| self
#| } else if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Double::max(self : Double, other : Double) -> Double {
#| if self.is_nan() {
#| other
#| } else if other.is_nan() {
#| self
#| } else if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Double::clamp(self : Double, min~ : Double, max~ : Double) -> Double {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub fn Double::lerp(self : Double, target~ : Double, t~ : Double) -> Double {
#| self + (target - self) * t
#|}
#|pub fn Double::signum(self : Double) -> Double {
#| if self < 0.0 {
#| -1.0
#| } else if self > 0.0 {
#| 1.0
#| } else {
#| self // handles 0.0, -0.0, NaN
#| }
#|}
#|pub fn Double::is_nan(self : Double) -> Bool {
#| self != self
#|}
#|pub fn Double::is_inf(self : Double) -> Bool {
#| self > double_max_value || self < double_min_value
#|}
#|pub fn Double::is_pos_inf(self : Double) -> Bool {
#| self > double_max_value
#|}
#|pub fn Double::is_neg_inf(self : Double) -> Bool {
#| self < double_min_value
#|}
#|pub fn Double::is_finite(self : Double) -> Bool {
#| self >= double_min_value && self <= double_max_value
#|}
#|pub impl Hash for Double with fn hash_combine(self, hasher) {
#| hasher.combine_double(self)
#|}
#|#intrinsic("%f64.to_string")
#|pub fn Double::to_string(self : Double) -> String {
#| ryu_to_string(self)
#|}
#|pub impl Show for Double with fn to_string(self) {
#| Double::to_string(self)
#|}
#|pub fn Double::is_close(
#| self : Self,
#| other : Self,
#| relative_tolerance~ : Self = 1.0e-09,
#| absolute_tolerance~ : Self = 0.0,
#|) -> Bool {
#| if relative_tolerance < 0.0 || absolute_tolerance < 0.0 {
#| abort("Tolerances must be non-negative")
#| }
#| if self == other {
#| return true
#| }
#| if self.is_inf() || other.is_inf() {
#| return false
#| }
#| let diff = (other - self).abs()
#| return (
#| diff <= (relative_tolerance * other).abs() ||
#| diff <= (relative_tolerance * self).abs()
#| ) ||
#| diff <= absolute_tolerance
#|}
#|#deprecated
#|#doc(hidden)
#|pub fn Double::to_be_bytes(self : Double) -> Bytes {
#| self.reinterpret_as_uint64().to_be_bytes()
#|}
#|#deprecated
#|#doc(hidden)
#|pub fn Double::to_le_bytes(self : Double) -> Bytes {
#| self.reinterpret_as_uint64().to_le_bytes()
#|}
),
"double_limits.mbt": (
#|#cfg(not(target="js"))
#|let not_a_number : Double = 0x7FF8000000000001L.reinterpret_as_double()
#|let double_max_value : Double = 0x7FEFFFFFFFFFFFFFL.reinterpret_as_double()
#|let double_min_value : Double = 0xFFEFFFFFFFFFFFFFL.reinterpret_as_double()
),
"double_mod_js.mbt": (
#|extern "js" fn Double::mod_ffi(self : Double, other : Double) -> Double =
#| #| (a, b) => (a % b)
#|pub impl Mod for Double with fn mod(self, other) {
#| self.mod_ffi(other)
#|}
),
"double_pow_js.mbt": (
#|#deprecated("Use `@math.pow` instead")
#|pub fn Double::pow(self : Double, other : Double) -> Double = "Math" "pow"
#|let _j : (FixedArray[String], StringView) -> String = FixedArray::join
),
"double_round_js.mbt": (
#|pub fn Double::trunc(self : Double) -> Double = "Math" "trunc"
#|pub fn Double::ceil(self : Double) -> Double = "Math" "ceil"
#|pub fn Double::floor(self : Double) -> Double = "Math" "floor"
#|pub fn Double::round(self : Double) -> Double = "Math" "round"
),
"double_ryu_js.mbt": (
#|extern "js" fn ryu_to_string(val : Double) -> String =
#| #|(number) => number.toString()
),
"double_to_int.mbt": (
#|pub fn Double::to_int(self : Double) -> Int32 {
#| if self != self {
#| 0
#| } else if self >= 2147483647 {
#| 2147483647
#| } else if self <= -2147483648 {
#| -2147483648
#| } else {
#| self.to_unchecked_int()
#| }
#|}
#|fn Double::to_unchecked_int(self : Double) -> Int32 = "%f64_to_i32"
),
"double_to_int64_js_wasm.mbt": (
#|pub fn Double::to_int64(self : Double) -> Int64 = "%f64_to_i64_saturate"
#|pub fn Double::to_uint64(self : Double) -> UInt64 = "%f64.to_u64_saturate"
),
"double_to_uint.mbt": (
#|pub fn Double::to_uint(self : Double) -> UInt32 {
#| if self != self {
#| 0
#| } else if self >= 4294967295.0 {
#| 4294967295U
#| } else if self <= 0 {
#| 0
#| } else {
#| UInt32::trunc_double(self)
#| }
#|}
),
"double_total_compare.mbt": (
#|pub fn Double::total_compare(self : Double, other : Double) -> Int32 {
#| (self > other).to_int() -
#| (self < other).to_int() +
#| (other.is_nan().to_int() - self.is_nan().to_int())
#|}
),
"error.mbt": (
#|pub fn Error::to_string(self : Error) -> String = "%error.to_string"
#|pub impl Show for Error with fn to_string(self) {
#| Error::to_string(self)
#|}
),
"extends.mbt": (
#|type Tuple2[A, B] = (A, B)
#|type Tuple3[A, B, C] = (A, B, C)
#|type Tuple4[A, B, C, D] = (A, B, C, D)
#|type Tuple5[A, B, C, D, E] = (A, B, C, D, E)
#|type Tuple6[A, B, C, D, E, F] = (A, B, C, D, E, F)
#|type Tuple7[A, B, C, D, E, F, G] = (A, B, C, D, E, F, G)
#|type Tuple8[A, B, C, D, E, F, G, H] = (A, B, C, D, E, F, G, H)
#|type Tuple9[A, B, C, D, E, F, G, H, I] = (A, B, C, D, E, F, G, H, I)
#|type Tuple10[A, B, C, D, E, F, G, H, I, J] = (A, B, C, D, E, F, G, H, I, J)
#|type Tuple11[A, B, C, D, E, F, G, H, I, J, K] = (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#| H,
#| I,
#| J,
#| K,
#|)
#|type Tuple12[A, B, C, D, E, F, G, H, I, J, K, L] = (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#| H,
#| I,
#| J,
#| K,
#| L,
#|)
#|type Tuple13[A, B, C, D, E, F, G, H, I, J, K, L, M] = (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#| H,
#| I,
#| J,
#| K,
#| L,
#| M,
#|)
#|type Tuple14[A, B, C, D, E, F, G, H, I, J, K, L, M, N] = (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#| H,
#| I,
#| J,
#| K,
#| L,
#| M,
#| N,
#|)
#|type Tuple15[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O] = (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#| H,
#| I,
#| J,
#| K,
#| L,
#| M,
#| N,
#| O,
#|)
#|type Tuple16[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P] = (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#| H,
#| I,
#| J,
#| K,
#| L,
#| M,
#| N,
#| O,
#| P,
#|)
#|pub extend ArgsLoc with Show::{to_string}
#|pub extend Array with Add::{add}
#|pub extend Array with Compare::{compare}
#|pub extend Array with Eq::{equal}
#|pub extend Array with Hash::{hash}
#|pub extend ArrayView with Add::{add}
#|pub extend ArrayView with Compare::{compare}
#|pub extend ArrayView with Eq::{equal}
#|pub extend ArrayView with Hash::{hash}
#|pub extend Bool with Compare::{compare}
#|pub extend Bool with Eq::{equal}
#|pub extend Bool with Hash::{hash}
#|pub extend Bool with Show::{to_string}
#|pub extend Byte with Add::{add}
#|pub extend Byte with BitAnd::{land}
#|pub extend Byte with BitOr::{lor}
#|pub extend Byte with BitXOr::{lxor}
#|pub extend Byte with Compare::{compare}
#|pub extend Byte with Default::{default}
#|pub extend Byte with Div::{div}
#|pub extend Byte with Eq::{equal}
#|pub extend Byte with Hash::{hash}
#|pub extend Byte with Mod::{mod}
#|pub extend Byte with Mul::{mul}
#|pub extend Byte with Shl::{shl}
#|pub extend Byte with Shr::{shr}
#|pub extend Byte with Sub::{sub}
#|pub extend Bytes with Add::{add}
#|pub extend Bytes with Compare::{compare}
#|pub extend Bytes with Eq::{equal}
#|pub extend Bytes with Hash::{hash}
#|pub extend Bytes with Show::{to_string}
#|pub extend BytesView with Compare::{compare}
#|pub extend BytesView with Eq::{equal}
#|pub extend BytesView with Hash::{hash}
#|pub extend BytesView with Show::{to_string}
#|pub extend Char with Compare::{compare}
#|pub extend Char with Eq::{equal}
#|pub extend Char with Hash::{hash}
#|pub extend Char with Show::{to_string}
#|pub extend Double with Add::{add}
#|pub extend Double with Compare::{compare}
#|pub extend Double with Div::{div}
#|pub extend Double with Eq::{equal}
#|pub extend Double with Hash::{hash}
#|pub extend Double with Mod::{mod}
#|pub extend Double with Mul::{mul}
#|pub extend Double with Neg::{neg}
#|pub extend Double with Sub::{sub}
#|pub extend Failure with Show::{to_string}
#|pub extend FixedArray with Add::{add}
#|pub extend FixedArray with Compare::{compare}
#|pub extend FixedArray with Eq::{equal}
#|pub extend FixedArray with Hash::{hash}
#|pub extend Int32 with Add::{add}
#|pub extend Int32 with BitAnd::{land}
#|pub extend Int32 with BitOr::{lor}
#|pub extend Int32 with BitXOr::{lxor}
#|pub extend Int32 with Compare::{compare}
#|pub extend Int32 with Div::{div}
#|pub extend Int32 with Eq::{equal}
#|pub extend Int32 with Hash::{hash}
#|pub extend Int32 with Mod::{mod}
#|pub extend Int32 with Mul::{mul}
#|pub extend Int32 with Neg::{neg}
#|pub extend Int32 with Sub::{sub}
#|pub extend Int64 with Add::{add}
#|pub extend Int64 with BitAnd::{land}
#|pub extend Int64 with BitOr::{lor}
#|pub extend Int64 with BitXOr::{lxor}
#|pub extend Int64 with Compare::{compare}
#|pub extend Int64 with Div::{div}
#|pub extend Int64 with Eq::{equal}
#|pub extend Int64 with Hash::{hash}
#|pub extend Int64 with Mod::{mod}
#|pub extend Int64 with Mul::{mul}
#|pub extend Int64 with Neg::{neg}
#|pub extend Int64 with Sub::{sub}
#|pub extend Iter with Add::{add}
#|pub extend Map with Eq::{equal}
#|pub extend MutArrayView with Compare::{compare}
#|pub extend MutArrayView with Eq::{equal}
#|pub extend MutArrayView with Hash::{hash}
#|pub extend Option with Compare::{compare}
#|pub extend Option with Eq::{equal}
#|pub extend Option with Hash::{hash}
#|pub extend ReadOnlyArray with Compare::{compare}
#|pub extend ReadOnlyArray with Eq::{equal}
#|pub extend ReadOnlyArray with Hash::{hash}
#|pub extend Result with Compare::{compare}
#|pub extend Result with Eq::{equal}
#|pub extend Result with Hash::{hash}
#|pub extend SourceLoc with Show::{to_string}
#|pub extend String with Add::{add}
#|pub extend String with Compare::{compare}
#|pub extend String with Eq::{equal}
#|pub extend String with Hash::{hash}
#|pub extend String with Show::{to_string}
#|pub extend String with ToStringView::{to_string_view}
#|pub extend StringBuilder with Logger::{
#| write_char,
#| write_string,
#| write_substring,
#| write_view,
#|}
#|pub extend StringView with Add::{add}
#|pub extend StringView with Compare::{compare}
#|pub extend StringView with Eq::{equal}
#|pub extend StringView with Hash::{hash}
#|pub extend StringView with ToStringView::{to_string_view}
#|#doc(hidden)
#|pub extend Tuple10 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple10 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple10 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple11 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple11 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple11 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple12 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple12 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple12 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple13 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple13 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple13 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple14 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple14 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple14 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple15 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple15 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple15 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple16 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple16 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple16 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple2 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple2 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple2 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple3 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple3 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple3 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple4 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple4 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple4 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple5 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple5 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple5 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple6 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple6 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple6 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple7 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple7 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple7 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple8 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple8 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple8 with Hash::{hash}
#|#doc(hidden)
#|pub extend Tuple9 with Compare::{compare}
#|#doc(hidden)
#|pub extend Tuple9 with Eq::{equal}
#|#doc(hidden)
#|pub extend Tuple9 with Hash::{hash}
#|pub extend UInt32 with Add::{add}
#|pub extend UInt32 with BitAnd::{land}
#|pub extend UInt32 with BitOr::{lor}
#|pub extend UInt32 with BitXOr::{lxor}
#|pub extend UInt32 with Compare::{compare}
#|pub extend UInt32 with Div::{div}
#|pub extend UInt32 with Eq::{equal}
#|pub extend UInt32 with Hash::{hash}
#|pub extend UInt32 with Mod::{mod}
#|pub extend UInt32 with Mul::{mul}
#|pub extend UInt32 with Sub::{sub}
#|pub extend UInt16 with Add::{add}
#|pub extend UInt16 with BitAnd::{land}
#|pub extend UInt16 with BitOr::{lor}
#|pub extend UInt16 with BitXOr::{lxor}
#|pub extend UInt16 with Compare::{compare}
#|pub extend UInt16 with Div::{div}
#|pub extend UInt16 with Eq::{equal}
#|pub extend UInt16 with Hash::{hash}
#|pub extend UInt16 with Mod::{mod}
#|pub extend UInt16 with Mul::{mul}
#|pub extend UInt16 with Shl::{shl}
#|pub extend UInt16 with Shr::{shr}
#|pub extend UInt16 with Sub::{sub}
#|pub extend UInt64 with Add::{add}
#|pub extend UInt64 with BitAnd::{land}
#|pub extend UInt64 with BitOr::{lor}
#|pub extend UInt64 with BitXOr::{lxor}
#|pub extend UInt64 with Compare::{compare}
#|pub extend UInt64 with Div::{div}
#|pub extend UInt64 with Eq::{equal}
#|pub extend UInt64 with Hash::{hash}
#|pub extend UInt64 with Mod::{mod}
#|pub extend UInt64 with Mul::{mul}
#|pub extend UInt64 with Sub::{sub}
#|pub extend Unit with Compare::{compare}
#|pub extend Unit with Eq::{equal}
#|pub extend Unit with Hash::{hash}
#|pub extend Unit with Show::{to_string}
#|pub extend V128 with Eq::{equal}
#|pub extend V128 with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Array with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Array with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Array with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Array with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ArrayView with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ArrayView with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend ArrayView with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bool with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bool with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bool with Hash::{hash_combine}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Byte with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Byte with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bytes with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bytes with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bytes with Hash::{hash_combine}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BytesView with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BytesView with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Char with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Char with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Char with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Double with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Double with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Double with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend FixedArray with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend FixedArray with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend FixedArray with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend FixedArray with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int32 with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int32 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int32 with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int64 with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int64 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int64 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Iter with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Map with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Map with Eq::{not_equal}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Map with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend MutArrayView with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend MutArrayView with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend MutArrayView with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Option with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Option with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Option with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ReadOnlyArray with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ReadOnlyArray with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ReadOnlyArray with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend ReadOnlyArray with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Result with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Result with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Result with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend String with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend String with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend String with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend StringView with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend StringView with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend StringView with Hash::{hash_combine}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple10 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple10 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple10 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple11 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple11 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple11 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple12 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple12 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple12 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple13 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple13 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple13 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple14 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple14 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple14 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple15 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple15 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple15 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple16 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple16 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple16 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple2 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple2 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple2 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple3 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple3 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple3 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple4 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple4 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple4 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple5 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple5 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple5 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple6 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple6 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple6 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple7 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple7 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple7 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple8 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple8 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple8 with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple9 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple9 with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Tuple9 with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt32 with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt32 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt32 with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt16 with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt16 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt16 with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt64 with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt64 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend UInt64 with Hash::{hash_combine}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Unit with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Unit with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Unit with Hash::{hash_combine}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend V128 with Eq::{not_equal}
),
"failure.mbt": (
#|pub(all) suberror Failure {
#| Failure(String)
#|}
#|#callsite(autofill(loc))
#|pub fn[T] fail(msg : StringView, loc~ : SourceLoc) -> T raise Failure {
#| raise Failure("\{loc} FAILED: \{msg}")
#|}
#|pub impl Show for Failure with fn output(self, logger) {
#| let Failure(msg) = self
#| logger..write_string("Failure: ").write_string(msg)
#|}
),
"fixedarray.mbt": (
#|#intrinsic("%fixedarray.get_opt")
#|pub fn[T] FixedArray::get(self : FixedArray[T], idx : Int32) -> T? {
#| let len = self.length()
#| guard idx >= 0 && idx < len else { None }
#| Some(self.unsafe_get(idx))
#|}
#|pub impl[X] Default for FixedArray[X] with fn default() {
#| []
#|}
#|#owned(value)
#|pub fn[T] FixedArray::fill(
#| self : FixedArray[T],
#| value : T,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Unit {
#| let array_length = self.length()
#| guard array_length > 0 else { return }
#| guard! start >= 0 && start < array_length
#| let length = match end {
#| None => array_length - start
#| Some(e) => {
#| guard! e >= start && e <= array_length
#| e - start
#| }
#| }
#| self.unchecked_fill(start, value, length)
#|}
#|#coverage.skip
#|#intrinsic("%fixedarray.fill")
#|#owned(value)
#|fn[T] FixedArray::unchecked_fill(
#| self : FixedArray[T],
#| start : Int32,
#| value : T,
#| len : Int32,
#|) -> Unit {
#| for i in start..<(start + len) {
#| self[i] = value
#| }
#|}
#|pub fn[T] FixedArray::is_empty(self : FixedArray[T]) -> Bool {
#| self.length() == 0
#|}
#|pub fn[T : Compare] FixedArray::binary_search(
#| self : FixedArray[T],
#| value : T,
#|) -> Result[Int32, Int32] {
#| self[:].binary_search(value)
#|}
#|pub fn[T] FixedArray::binary_search_by(
#| self : FixedArray[T],
#| cmp : (T) -> Int32 raise?,
#|) -> Result[Int32, Int32] raise? {
#| self[:].binary_search_by(cmp)
#|}
#|#locals(f)
#|pub fn[T] FixedArray::each(
#| self : FixedArray[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| for v in self {
#| f(v)
#| }
#|}
#|test "each" {
#| let mut i = 0
#| let mut failed = false
#| let f = elem => {
#| if elem != i + 1 {
#| failed = true
#| }
#| i += 1
#| }
#| {
#| i = 0
#| ([] : FixedArray[_]).each(f)
#| assert_false(failed)
#| inspect(i, content="0")
#| }
#| {
#| i = 0
#| ([1] : FixedArray[_]).each(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| }
#| i = 0
#| ([1, 2, 3, 4, 5] : FixedArray[_]).each(f)
#| assert_false(failed)
#| inspect(i, content="5")
#|}
#|#locals(f)
#|pub fn[T] FixedArray::eachi(
#| self : FixedArray[T],
#| f : (Int32, T) -> Unit raise?,
#|) -> Unit raise? {
#| for i, v in self {
#| f(i, v)
#| }
#|}
#|test "eachi" {
#| let mut i = 0
#| let mut failed = false
#| let f = (index, elem) => {
#| if index != i || elem != i + 1 {
#| failed = true
#| }
#| i += 1
#| }
#| {
#| i = 0
#| ([] : FixedArray[_]).eachi(f)
#| assert_false(failed)
#| inspect(i, content="0")
#| }
#| {
#| i = 0
#| ([1] : FixedArray[_]).eachi(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| }
#| i = 0
#| ([1, 2, 3, 4, 5] : FixedArray[_]).eachi(f)
#| assert_false(failed)
#| inspect(i, content="5")
#|}
#|#locals(f)
#|pub fn[T] FixedArray::rev_each(
#| self : FixedArray[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| for i in self.length()>..0 {
#| f(self.unsafe_get(i))
#| }
#|}
#|test "rev_each" {
#| let mut i = 6
#| let mut failed = false
#| let f = elem => {
#| if elem != i - 1 {
#| failed = true
#| }
#| i -= 1
#| }
#| {
#| i = 1
#| ([] : FixedArray[_]).rev_each(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| }
#| {
#| i = 2
#| ([1] : FixedArray[_]).rev_each(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| }
#| i = 6
#| ([1, 2, 3, 4, 5] : FixedArray[_]).rev_each(f)
#| assert_false(failed)
#| inspect(i, content="1")
#|}
#|#locals(f)
#|pub fn[T] FixedArray::rev_eachi(
#| self : FixedArray[T],
#| f : (Int32, T) -> Unit raise?,
#|) -> Unit raise? {
#| let len = self.length()
#| for i in 0.. {
#| if index != j || elem != i - 1 {
#| failed = true
#| }
#| i -= 1
#| j += 1
#| }
#| {
#| i = 1
#| j = 0
#| ([] : FixedArray[_]).rev_eachi(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| inspect(j, content="0")
#| }
#| {
#| i = 2
#| j = 0
#| ([1] : FixedArray[_]).rev_eachi(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| inspect(j, content="1")
#| }
#| i = 6
#| j = 0
#| ([1, 2, 3, 4, 5] : FixedArray[_]).rev_eachi(f)
#| assert_false(failed)
#| inspect(i, content="1")
#| inspect(j, content="5")
#|}
#|#locals(f)
#|pub fn[T, U] FixedArray::map(
#| self : FixedArray[T],
#| f : (T) -> U raise?,
#|) -> FixedArray[U] raise? {
#| if self.is_empty() {
#| return []
#| }
#| let len = self.length()
#| let res = FixedArray::make(len, f(self.unsafe_get(0)))
#| for i in 1.. x)
#| assert_true(empty == [])
#| let simple_arr : FixedArray[_] = [6]
#| let simple_doubled = simple_arr.map(x => x * 2)
#| assert_true(simple_doubled == [12])
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| let doubled = arr.map(x => x * 2)
#| assert_true(doubled == [2, 4, 6, 8, 10])
#|}
#|#locals(f)
#|pub fn[T, U] FixedArray::mapi(
#| self : FixedArray[T],
#| f : (Int32, T) -> U raise?,
#|) -> FixedArray[U] raise? {
#| if self.is_empty() {
#| return []
#| }
#| let len = self.length()
#| let res = FixedArray::make(len, f(0, self.unsafe_get(0)))
#| for i in 1.. x + i)
#| assert_true(empty == [])
#| let simple_arr : FixedArray[_] = [6]
#| let simple_doubled = simple_arr.mapi((i, x) => x * 2 + i)
#| assert_true(simple_doubled == [12])
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| let doubled = arr.mapi((i, x) => x * 2 + i)
#| assert_true(doubled == [2, 5, 8, 11, 14])
#|}
#|#locals(value)
#|pub fn[T] FixedArray::makei(
#| length : Int32,
#| value : (Int32) -> T raise?,
#|) -> FixedArray[T] raise? {
#| if length <= 0 {
#| []
#| } else {
#| let array = FixedArray::make(length, value(0))
#| for i in 1.. i)
#| inspect(empty.length(), content="0")
#| let simple_arr = FixedArray::makei(1, i => i)
#| inspect(simple_arr.length(), content="1")
#| inspect(simple_arr[0], content="0")
#| let arr = FixedArray::makei(2, i => i)
#| inspect(arr.length(), content="2")
#| inspect(arr[0], content="0")
#| inspect(arr[1], content="1")
#|}
#|#cfg(any(target="native", target="wasm"))
#|#owned(array)
#|pub fn[T] FixedArray::from_array(array : ArrayView[T]) -> FixedArray[T] {
#| array.copy_or_reuse_buffer().unsafe_to_fixed_array()
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn[T] FixedArray::from_array(array : ArrayView[T]) -> FixedArray[T] {
#| FixedArray::makei(array.length(), i => array.unsafe_get(i))
#|}
#|test "from_array" {
#| let array = FixedArray::from_array([1, 2, 3, 4, 5])
#| assert_true(array == [1, 2, 3, 4, 5])
#|}
#|#locals(f)
#|pub fn[A, B] FixedArray::fold(
#| self : FixedArray[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| for x in self; acc = init {
#| continue f(acc, x)
#| } nobreak {
#| acc
#| }
#|}
#|test "fold" {
#| let sum = ([] : FixedArray[_]).fold(init=1, (sum, elem) => sum + elem)
#| inspect(sum, content="1")
#| let sum = ([1] : FixedArray[_]).fold(init=2, (sum, elem) => sum + elem)
#| inspect(sum, content="3")
#| let sum = ([1, 2, 3, 4, 5] : FixedArray[_]).fold(init=0, (sum, elem) => {
#| sum + elem
#| })
#| inspect(sum, content="15")
#|}
#|#locals(f)
#|pub fn[A, B] FixedArray::rev_fold(
#| self : FixedArray[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| for i = self.length() - 1, acc = init; i >= 0; {
#| continue i - 1, f(acc, self.unsafe_get(i))
#| } nobreak {
#| acc
#| }
#|}
#|test "rev_fold" {
#| let sum = ([] : FixedArray[_]).rev_fold(init=1, (sum, elem) => sum + elem)
#| inspect(sum, content="1")
#| let sum = ([1] : FixedArray[_]).rev_fold(init=2, (sum, elem) => sum + elem)
#| inspect(sum, content="3")
#| let sum = ([1, 2, 3, 4, 5] : FixedArray[_]).rev_fold(init=0, (sum, elem) => {
#| sum + elem
#| })
#| inspect(sum, content="15")
#|}
#|#locals(f)
#|pub fn[A, B] FixedArray::foldi(
#| self : FixedArray[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| for i, x in self; acc = init {
#| continue f(i, acc, x)
#| } nobreak {
#| acc
#| }
#|}
#|test "fold_lefti" {
#| let f = (index, sum, elem) => index + sum + elem
#| {
#| let sum = ([] : FixedArray[_]).foldi(init=1, f)
#| inspect(sum, content="1")
#| }
#| {
#| let sum = ([1] : FixedArray[_]).foldi(init=2, f)
#| inspect(sum, content="3")
#| }
#| let sum = ([1, 2, 3, 4, 5] : FixedArray[_]).foldi(init=0, f)
#| inspect(sum, content="25")
#|}
#|#locals(f)
#|pub fn[A, B] FixedArray::rev_foldi(
#| self : FixedArray[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| for i in self.length()>..0; index = 0, acc = init {
#| continue index + 1, f(index, acc, self.unsafe_get(i))
#| } nobreak {
#| acc
#| }
#|}
#|test "rev_foldi" {
#| let f = (index, sum, elem) => index + sum + elem
#| {
#| let sum = ([] : FixedArray[_]).rev_foldi(init=1, f)
#| inspect(sum, content="1")
#| }
#| {
#| let sum = ([1] : FixedArray[_]).rev_foldi(init=2, f)
#| inspect(sum, content="3")
#| }
#| let sum = ([1, 2, 3, 4, 5] : FixedArray[_]).rev_foldi(init=0, f)
#| inspect(sum, content="25")
#|}
#|test "rev_foldi preserves reversed index element pairs" {
#| let trace = ([10, 20, 30] : FixedArray[_]).rev_foldi(init=0, (
#| index,
#| acc,
#| elem,
#| ) => acc * 1000 + index * 100 + elem)
#| inspect(trace, content="30120210")
#|}
#|#alias(rev_inplace, deprecated)
#|pub fn[T] FixedArray::rev_in_place(self : FixedArray[T]) -> Unit {
#| let mid_len = self.length() / 2
#| for i in 0.. FixedArray[T] {
#| match self {
#| [] => []
#| [.., first] => {
#| let res = FixedArray::make(self.length(), first)
#| let len = self.length()
#| for i in 1.. Unit {
#| let temp = self[i]
#| self[i] = self[j]
#| self[j] = temp
#|}
#|test "swap" {
#| {
#| let arr : FixedArray[Int32] = [1]
#| arr.swap(0, 0)
#| assert_true(arr == [1])
#| }
#| {
#| let arr : FixedArray[_] = [1, 2]
#| arr.swap(0, 0)
#| assert_true(arr == [1, 2])
#| arr.swap(0, 1)
#| assert_true(arr == [2, 1])
#| }
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| arr.swap(3, 3)
#| assert_true(arr == [1, 2, 3, 4, 5])
#| arr.swap(1, 3)
#| assert_true(arr == [1, 4, 3, 2, 5])
#|}
#|#alias(every)
#|pub fn[T] FixedArray::all(
#| self : FixedArray[T],
#| f : (T) -> Bool raise?,
#|) -> Bool raise? {
#| self[:].all(f)
#|}
#|test "all" {
#| {
#| let arr : FixedArray[Int32] = []
#| assert_true(arr.all(ele => ele < 6))
#| assert_true(arr.all(ele => ele < 5))
#| }
#| {
#| let arr : FixedArray[_] = [5]
#| assert_true(arr.all(ele => ele < 6))
#| assert_false(arr.all(ele => ele < 5))
#| }
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| assert_true(arr.all(ele => ele < 6))
#| assert_false(arr.all(ele => ele < 5))
#|}
#|#alias(exists)
#|pub fn[T] FixedArray::any(
#| self : FixedArray[T],
#| f : (T) -> Bool raise?,
#|) -> Bool raise? {
#| self[:].any(f)
#|}
#|test "any" {
#| {
#| let arr : FixedArray[Int32] = []
#| assert_false(arr.any(ele => ele < 6))
#| assert_false(arr.any(ele => ele < 5))
#| }
#| {
#| let arr : FixedArray[_] = [5]
#| assert_true(arr.any(ele => ele < 6))
#| assert_false(arr.any(ele => ele < 5))
#| }
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| assert_true(arr.any(ele => ele < 6))
#| assert_true(arr.any(ele => ele < 5))
#|}
#|test "fill" {
#| {
#| let arr : FixedArray[Int32] = []
#| arr.fill(3)
#| assert_true(arr == [])
#| }
#| {
#| let arr : FixedArray[_] = [6]
#| arr.fill(5)
#| assert_true(arr == [5])
#| }
#| let arr : FixedArray[_] = [0, 0, 0, 0, 0]
#| arr.fill(3)
#| assert_true(arr == [3, 3, 3, 3, 3])
#|}
#|pub fn[T : Eq] FixedArray::search(self : FixedArray[T], value : T) -> Int32? {
#| self[:].search(value)
#|}
#|test "search" {
#| {
#| let arr : FixedArray[Int32] = []
#| assert_true(arr.search(3) == None)
#| assert_true(arr.search(-1) == None)
#| }
#| {
#| let arr : FixedArray[_] = [3]
#| assert_true(arr.search(3) == Some(0))
#| assert_true(arr.search(-1) == None)
#| }
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| assert_true(arr.search(1) == Some(0))
#| assert_true(arr.search(5) == Some(4))
#| assert_true(arr.search(3) == Some(2))
#| assert_true(arr.search(-1) == None)
#|}
#|pub fn[T : Eq] FixedArray::contains(self : FixedArray[T], value : T) -> Bool {
#| for x in self {
#| if x == value {
#| return true
#| }
#| }
#| false
#|}
#|test "contains" {
#| {
#| let arr : FixedArray[Int32] = []
#| assert_false(arr.contains(3))
#| assert_false(arr.contains(-1))
#| }
#| {
#| let arr : FixedArray[_] = [3]
#| assert_true(arr.contains(3))
#| assert_false(arr.contains(-1))
#| }
#| let arr : FixedArray[_] = [3, 4, 5]
#| assert_true(arr.contains(3))
#| assert_true(arr.contains(4))
#| assert_true(arr.contains(5))
#| assert_false(arr.contains(6))
#|}
#|#alias(starts_with, deprecated="Use `has_prefix` instead")
#|pub fn[T : Eq] FixedArray::has_prefix(
#| self : FixedArray[T],
#| prefix : ArrayView[T],
#|) -> Bool {
#| self[:].has_prefix(prefix)
#|}
#|test "has_prefix" {
#| {
#| let arr : FixedArray[Int32] = []
#| assert_true(arr.has_prefix([]))
#| assert_false(arr.has_prefix([1]))
#| }
#| {
#| let arr : FixedArray[_] = [3]
#| assert_true(arr.has_prefix([]))
#| assert_true(arr.has_prefix([3]))
#| assert_false(arr.has_prefix([2]))
#| assert_false(arr.has_prefix([3, 1]))
#| }
#| let arr : FixedArray[_] = [3, 4, 5]
#| assert_true(arr.has_prefix([]))
#| assert_true(arr.has_prefix([3]))
#| assert_false(arr.has_prefix([2]))
#| assert_true(arr.has_prefix([3, 4]))
#| assert_false(arr.has_prefix([3, 2]))
#| assert_true(arr.has_prefix([3, 4, 5]))
#| assert_false(arr.has_prefix([3, 4, 2]))
#| assert_false(arr.has_prefix([3, 4, 5, 6]))
#|}
#|#alias(ends_with, deprecated="Use `has_suffix` instead")
#|pub fn[T : Eq] FixedArray::has_suffix(
#| self : FixedArray[T],
#| suffix : ArrayView[T],
#|) -> Bool {
#| self[:].has_suffix(suffix)
#|}
#|test "has_suffix" {
#| {
#| let arr : FixedArray[Int32] = []
#| assert_true(arr.has_suffix([]))
#| assert_false(arr.has_suffix([1]))
#| }
#| {
#| let arr : FixedArray[_] = [3]
#| assert_true(arr.has_suffix([]))
#| assert_true(arr.has_suffix([3]))
#| assert_false(arr.has_suffix([2]))
#| assert_false(arr.has_suffix([3, 1]))
#| }
#| let arr : FixedArray[_] = [3, 4, 5]
#| assert_true(arr.has_suffix([]))
#| assert_true(arr.has_suffix([5]))
#| assert_false(arr.has_suffix([2]))
#| assert_true(arr.has_suffix([4, 5]))
#| assert_false(arr.has_suffix([4, 2]))
#| assert_false(arr.has_suffix([2, 5]))
#| assert_true(arr.has_suffix([3, 4, 5]))
#| assert_false(arr.has_suffix([3, 4, 2]))
#| assert_false(arr.has_suffix([3, 2, 5]))
#| assert_false(arr.has_suffix([2, 4, 5]))
#| assert_false(arr.has_suffix([3, 4, 5, 6]))
#| assert_false(arr.has_suffix([2, 3, 4, 5]))
#|}
#|pub impl[T : Eq] Eq for FixedArray[T] with fn equal(
#| self : FixedArray[T],
#| that : FixedArray[T],
#|) -> Bool {
#| if self.length() != that.length() {
#| return false
#| }
#| for i, x in self {
#| if x != that[i] {
#| return false
#| }
#| }
#| true
#|}
#|pub impl[T : Hash] Hash for FixedArray[T] with fn hash_combine(self, hasher) {
#| for v in self {
#| v.hash_combine(hasher)
#| }
#|}
#|test "equal" {
#| {
#| inspect(([] : FixedArray[Int32]) == [], content="true")
#| inspect(([] : FixedArray[_]) == [1], content="false")
#| inspect(([1, 2] : FixedArray[_]) == [], content="false")
#| }
#| {
#| inspect(([1] : FixedArray[_]) == [1], content="true")
#| inspect(([1] : FixedArray[_]) == [2], content="false")
#| inspect(([1, 2] : FixedArray[_]) == [1], content="false")
#| inspect(([1] : FixedArray[_]) == [1, 2], content="false")
#| }
#| inspect(([1, 2, 3, 4, 5] : FixedArray[_]) == [1, 2, 3, 4, 5], content="true")
#| inspect(([1, 2, 3, 4, 5] : FixedArray[_]) == [1, 2, 3, 4], content="false")
#| inspect(([1, 2, 3, 4] : FixedArray[_]) == [1, 2, 3, 4, 5], content="false")
#| inspect(([1, 2, 3, 4, 5] : FixedArray[_]) == [6, 2, 3, 4, 5], content="false")
#| inspect(([1, 2, 3, 4, 5] : FixedArray[_]) == [1, 2, 6, 4, 5], content="false")
#| inspect(([1, 2, 3, 4, 5] : FixedArray[_]) == [1, 2, 3, 4, 6], content="false")
#|}
#|pub impl[T : Compare] Compare for FixedArray[T] with fn compare(self, other) {
#| let len_self = self.length()
#| let len_other = other.length()
#| let cmp = len_self.compare(len_other)
#| guard cmp == 0 else { return cmp }
#| for i in 0.. {
#| if i < slen {
#| self[i]
#| } else {
#| other[i - slen]
#| }
#| })
#|}
#|test "add" {
#| {
#| assert_true(([] : FixedArray[Int32]) + [] == [])
#| assert_true(([] : FixedArray[_]) + [1, 2, 3, 4, 5] == [1, 2, 3, 4, 5])
#| assert_true(([1, 2, 3, 4, 5] : FixedArray[_]) + [] == [1, 2, 3, 4, 5])
#| }
#| {
#| assert_true(([1] : FixedArray[_]) + [2] == [1, 2])
#| assert_true(([1] : FixedArray[_]) + [1, 2, 3, 4, 5] == [1, 1, 2, 3, 4, 5])
#| assert_true(([1, 2, 3, 4, 5] : FixedArray[_]) + [1] == [1, 2, 3, 4, 5, 1])
#| }
#| assert_true(
#| ([1, 2, 3, 4, 5] : FixedArray[_]) + [6, 7, 8, 9, 10] ==
#| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
#| )
#|}
#|test "iter" {
#| let arr : FixedArray[_] = [1, 2, 3, 4, 5]
#| let iter = arr.iter()
#| let exb = StringBuilder()
#| let mut i = 0
#| iter.each(x => {
#| exb <+ "\{x}\n"
#| i += 1
#| })
#| assert_true(i == arr.length())
#| inspect(
#| exb,
#| content=(
#| #|1
#| #|2
#| #|3
#| #|4
#| #|5
#| #|
#| ),
#| )
#|}
#|#alias(from_iterator, deprecated)
#|pub fn[T] FixedArray::from_iter(iter : Iter[T]) -> FixedArray[T] {
#| FixedArray::from_array(iter.collect())
#|}
#|pub fn[A] FixedArray::last(self : FixedArray[A]) -> A? {
#| match self {
#| [] => None
#| [.., last] => Some(last)
#| }
#|}
#|pub fn[A : ToStringView] FixedArray::join(
#| self : FixedArray[A],
#| separator : StringView,
#|) -> String {
#| self[:].join(separator)
#|}
#|test "FixedArray::last/empty" {
#| let empty : FixedArray[Int32] = []
#| assert_true(empty.last() == None)
#|}
#|test "FixedArray::last/non_empty" {
#| let array : FixedArray[_] = [1, 2, 3]
#| assert_true(array.last() == Some(3))
#| let single : FixedArray[_] = [42]
#| assert_true(single.last() == Some(42))
#|}
#|test "FixedArray::last/empty_array" {
#| let empty_array : FixedArray[Int32] = []
#| assert_true(FixedArray::last(empty_array) == None)
#|}
#|test "FixedArray::last/single_element" {
#| let single_element_array : FixedArray[Int32] = [42]
#| assert_true(FixedArray::last(single_element_array) == Some(42))
#|}
#|test "FixedArray::last/multiple_elements" {
#| let multiple_elements_array : FixedArray[Int32] = [1, 2, 3, 4, 5]
#| assert_true(FixedArray::last(multiple_elements_array) == Some(5))
#|}
#|test "FixedArray::join" {
#| let fixed_array : FixedArray[String] = ["1", "2", "3"]
#| inspect(fixed_array.join(","), content="1,2,3")
#| inspect(fixed_array.join(""), content="123")
#| inspect(fixed_array.join(" "), content="1 2 3")
#| let fixed_array_empty : FixedArray[String] = []
#| inspect(fixed_array_empty.join(","), content="")
#| let view_array : FixedArray[StringView] = ["a", "b", "c"]
#| inspect(view_array.join("-"), content="a-b-c")
#|}
#|#alias(iterator, deprecated)
#|pub fn[X] FixedArray::iter(self : FixedArray[X]) -> Iter[X] {
#| self[:].iter()
#|}
#|#alias(iterator2, deprecated)
#|pub fn[X] FixedArray::iter2(self : FixedArray[X]) -> Iter2[Int32, X] {
#| self[:].iter2()
#|}
#|#alias(clone, deprecated)
#|pub fn[T] FixedArray::copy(self : FixedArray[T]) -> FixedArray[T] {
#| let len = self.length()
#| if len == 0 {
#| []
#| } else {
#| FixedArray::make_and_blit(self, allocate_len=len, init=self[0], len~)
#| }
#|}
#|pub fn[T : Compare] FixedArray::lexical_compare(
#| self : FixedArray[T],
#| other : FixedArray[T],
#|) -> Int32 {
#| self[:].lexical_compare(other)
#|}
),
"fixedarray_block.mbt": (
#|#owned(dst, src)
#|#intrinsic("%fixedarray.copy")
#|#coverage.skip
#|pub fn[A] FixedArray::unsafe_blit(
#| dst : FixedArray[A],
#| dst_offset : Int32,
#| src : FixedArray[A],
#| src_offset : Int32,
#| len : Int32,
#|) -> Unit {
#| if physical_equal(dst, src) && dst_offset < src_offset {
#| for i in 0....0 {
#| dst[dst_offset + i] = src[src_offset + i]
#| }
#| }
#|}
#|#owned(dst, src)
#|#intrinsic("%fixedarray.copy")
#|#coverage.skip
#|fn[T] UninitializedArray::unsafe_blit_fixed(
#| dst : UninitializedArray[T],
#| dst_offset : Int32,
#| src : FixedArray[T],
#| src_offset : Int32,
#| len : Int32,
#|) -> Unit {
#| for i in len>..0 {
#| dst[dst_offset + i] = src[src_offset + i]
#| }
#|}
#|#intrinsic("%fixedarray.make_with_init_blit")
#|#owned(src, init)
#|fn[T] FixedArray::unsafe_make_and_blit(
#| src : FixedArray[T],
#| allocate_len : Int32,
#| init : T,
#| src_offset : Int32,
#| dst_offset : Int32,
#| blit_len : Int32,
#|) -> FixedArray[T] {
#| let dst = FixedArray::make(allocate_len, init)
#| FixedArray::unsafe_blit(dst, dst_offset, src, src_offset, blit_len)
#| dst
#|}
#|#inline
#|fn range_fits(offset : Int32, len : Int32, length : Int32) -> Bool {
#| offset >= 0 && offset <= length - len
#|}
#|#owned(src, init)
#|pub fn[T] FixedArray::make_and_blit(
#| src : FixedArray[T],
#| allocate_len~ : Int32,
#| init~ : T,
#| len~ : Int32,
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> FixedArray[T] {
#| guard allocate_len >= 0 &&
#| len >= 0 &&
#| range_fits(src_offset, len, src.length()) &&
#| range_fits(dst_offset, len, allocate_len) else {
#| abort(
#| "bounds check failed: allocate_len = \{allocate_len}, src_offset = \{src_offset}, dst_offset = \{dst_offset}, len = \{len}, src.length = \{src.length()}",
#| )
#| }
#| FixedArray::unsafe_make_and_blit(
#| src, allocate_len, init, src_offset, dst_offset, len,
#| )
#|}
#|pub fn[A] FixedArray::blit_to(
#| self : FixedArray[A],
#| dst : FixedArray[A],
#| len~ : Int32,
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> Unit {
#| guard len >= 0 &&
#| range_fits(src_offset, len, self.length()) &&
#| range_fits(dst_offset, len, dst.length()) else {
#| abort(
#| "bounds check failed: dst_offset = \{dst_offset}, src_offset = \{src_offset}, len = \{len}, dst.length = \{dst.length()}, self.length = \{self.length()}",
#| )
#| }
#| FixedArray::unsafe_blit(dst, dst_offset, self, src_offset, len)
#|}
),
"hash_fn.mbt": (
#|pub fn[T : Hash] hash(value : T) -> Int32 {
#| Hash::hash(value)
#|}
),
"hasher.mbt": (
#|const GPRIME1 : UInt32 = 0x9E3779B1
#|const GPRIMES2 : UInt32 = 0x85EBCA77
#|const GPRIME3 : UInt32 = 0xC2B2AE3D
#|const GPRIME4 : UInt32 = 0x27D4EB2F
#|const GPRIME5 : UInt32 = 0x165667B1
#|struct Hasher {
#| mut acc : UInt32
#|}
#|#alias(new, deprecated="Use `Hasher()` instead")
#|pub fn Hasher::Hasher(seed~ : Int32 = seed) -> Hasher {
#| { acc: seed.reinterpret_as_uint() + GPRIME5, }
#|}
#|#cfg(target="native")
#|let seed : Int32 = {
#| let bytes : FixedArray[Byte] = FixedArray::make(4, b'\x00')
#| if moonbit_rt_get_random_for_hash_seed(bytes) == 0 {
#| fixedarray_read_uint32_le(bytes, 0).reinterpret_as_int()
#| } else {
#| 0
#| }
#|}
#|#cfg(any(target="wasm", target="wasm-gc"))
#|let seed : Int32 = 0
#|#cfg(target="js")
#|let seed : Int32 = random_seed()
#|#cfg(target="js")
#|extern "js" fn random_seed() -> Int32 =
#| #|() => {
#| #| const cryptoObj = globalThis.crypto;
#| #| if (cryptoObj != null && typeof cryptoObj.getRandomValues === "function") {
#| #| try {
#| #| const array = new Uint32Array(1);
#| #| cryptoObj.getRandomValues(array);
#| #| return array[0] | 0; // Convert to signed 32
#| #| } catch (_) {
#| #| // The host refuses Web Crypto here, e.g. Workers at module load
#| #| }
#| #| }
#| #| return Math.floor(Math.random() * 0x100000000) | 0; // Fallback to Math.random
#| #|}
#|#cfg(target="native")
#|#borrow(bytes)
#|extern "c" fn moonbit_rt_get_random_for_hash_seed(
#| bytes : FixedArray[Byte],
#|) -> Int32 = "moonbit_rt_get_random"
#|pub fn[T : Hash] Hasher::combine(self : Hasher, value : T) -> Unit {
#| value.hash_combine(self)
#|}
#|pub fn Hasher::combine_unit(self : Hasher) -> Unit {
#| self.combine_uint(0)
#|}
#|pub fn Hasher::combine_bool(self : Hasher, value : Bool) -> Unit {
#| self.combine_uint(if value { 1 } else { 0 })
#|}
#|pub fn Hasher::combine_int(self : Hasher, value : Int32) -> Unit {
#| self.combine_uint(value.reinterpret_as_uint())
#|}
#|pub fn Hasher::combine_int64(self : Hasher, value : Int64) -> Unit {
#| self.acc += 8
#| self.consume4(value.reinterpret_as_uint64().to_uint())
#| self.consume4((value.reinterpret_as_uint64() >> 32).to_uint())
#|}
#|pub fn Hasher::combine_uint(self : Hasher, value : UInt32) -> Unit {
#| self.acc += 4
#| self.consume4(value)
#|}
#|pub fn Hasher::combine_uint64(self : Hasher, value : UInt64) -> Unit {
#| self.combine_int64(value.reinterpret_as_int64())
#|}
#|pub fn Hasher::combine_double(self : Hasher, value : Double) -> Unit {
#| let bits = value.reinterpret_as_int64()
#| self.combine_int64(if bits == -9223372036854775808L { 0L } else { bits })
#|}
#|pub fn Hasher::combine_byte(self : Hasher, value : Byte) -> Unit {
#| self.consume1(value)
#|}
#|pub fn Hasher::combine_bytes(self : Hasher, value : Bytes) -> Unit {
#| for data = value.exact_view() {
#| if data is [u32le(x), .. rest] {
#| self.consume4(x)
#| continue rest
#| } else {
#| if data is [b0, .. data] {
#| self.consume1(b0)
#| if data is [b1, .. data] {
#| self.consume1(b1)
#| if data is [b2, ..] {
#| self.consume1(b2)
#| }
#| }
#| }
#| break
#| }
#| }
#|}
#|pub fn Hasher::combine_string(self : Hasher, value : String) -> Unit {
#| for i in 0.. Unit {
#| self.combine_uint(value.to_uint())
#|}
#|pub fn Hasher::finalize(self : Hasher) -> Int32 {
#| finalize_acc(self.acc)
#|}
#|fn finalize_acc(acc : UInt32) -> Int32 {
#| avalanche_acc(acc).reinterpret_as_int()
#|}
#|fn avalanche_acc(acc : UInt32) -> UInt32 {
#| let mut acc = acc
#| acc = acc ^ (acc >> 15)
#| acc *= GPRIMES2
#| acc = acc ^ (acc >> 13)
#| acc *= GPRIME3
#| acc = acc ^ (acc >> 16)
#| acc
#|}
#|fn Hasher::consume4(self : Hasher, input : UInt32) -> Unit {
#| self.acc = consume4_acc(self.acc, input)
#|}
#|fn consume4_acc(acc : UInt32, input : UInt32) -> UInt32 {
#| rotl(acc + input * GPRIME3, 17) * GPRIME4
#|}
#|fn Hasher::consume1(self : Hasher, input : Byte) -> Unit {
#| self.acc = rotl(self.acc + input.to_uint() * GPRIME5, 11) * GPRIME1
#|}
#|fn rotl(x : UInt32, r : Int32) -> UInt32 {
#| (x << r) | (x >> (32 - r))
#|}
#|pub impl Hash for String with fn hash_combine(self, hasher) {
#| hasher.combine_string(self)
#|}
#|pub impl Hash for String with fn hash(self : String) -> Int32 {
#| let mut acc = seed.reinterpret_as_uint() + GPRIME5
#| for i in 0.. Unit {
#| let str = self.str()
#| for i in self.start().. Int32 {
#| let str = self.str()
#| let mut acc = seed.reinterpret_as_uint() + GPRIME5
#| for i in self.start().. Int32 {
#| let acc = seed.reinterpret_as_uint() + GPRIME5 + 4U
#| finalize_acc(consume4_acc(acc, self.reinterpret_as_uint()))
#|}
#|pub impl Hash for UInt32 with fn hash_combine(self, hasher) {
#| hasher.combine_uint(self)
#|}
#|pub impl Hash for UInt32 with fn hash(self : UInt32) -> Int32 {
#| let acc = seed.reinterpret_as_uint() + GPRIME5 + 4U
#| finalize_acc(consume4_acc(acc, self))
#|}
#|pub impl Hash for UInt64 with fn hash_combine(self, hasher) {
#| hasher.combine_uint64(self)
#|}
#|pub impl Hash for UInt64 with fn hash(self : UInt64) -> Int32 {
#| let acc = seed.reinterpret_as_uint() + GPRIME5 + 8U
#| let low = (self & 0xFFFF_FFFFUL).to_uint()
#| let high = ((self >> 32) & 0xFFFF_FFFFUL).to_uint()
#| let acc = consume4_acc(acc, low)
#| let acc = consume4_acc(acc, high)
#| finalize_acc(acc)
#|}
#|pub impl[X : Hash] Hash for X? with fn hash_combine(self, hasher) {
#| match self {
#| None => hasher.combine_int(0)
#| Some(x) => {
#| hasher.combine_int(1)
#| hasher.combine(x)
#| }
#| }
#|}
#|pub impl[T : Hash, E : Hash] Hash for Result[T, E] with fn hash_combine(
#| self,
#| hasher,
#|) {
#| match self {
#| Ok(x) => {
#| hasher.combine_int(0)
#| hasher.combine(x)
#| }
#| Err(x) => {
#| hasher.combine_int(1)
#| hasher.combine(x)
#| }
#| }
#|}
#|pub impl Hash for BytesView with fn hash_combine(
#| self : BytesView,
#| hasher : Hasher,
#|) {
#| for data = self {
#| if data is [u32le(x), .. rest] {
#| hasher.combine_uint(x)
#| continue rest
#| } else {
#| if data is [b0, .. data] {
#| hasher.combine_byte(b0)
#| if data is [b1, .. data] {
#| hasher.combine_byte(b1)
#| if data is [b2, ..] {
#| hasher.combine_byte(b2)
#| }
#| }
#| }
#| break
#| }
#| }
#|}
),
"int.mbt": (
#|pub fn Int32::Int(self : Int32) -> Int32 = "%identity"
#|pub fn Int32::next_power_of_two(self : Int32) -> Int32 {
#| guard! self >= 0
#| if self <= 1 {
#| return 1
#| }
#| let max_power_of_two = 1073741824 // 2^30
#| if self > max_power_of_two {
#| return max_power_of_two
#| }
#| (2147483647 >> ((self - 1).clz() - 1)) + 1
#|}
#|pub fn Int32::min(self : Int32, other : Int32) -> Int32 {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Int32::max(self : Int32, other : Int32) -> Int32 {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Int32::clamp(self : Int32, min~ : Int32, max~ : Int32) -> Int32 {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub fn Int32::is_leading_surrogate(self : Int32) -> Bool {
#| 0xD800 <= self && self <= 0xDBFF
#|}
#|pub fn Int32::is_trailing_surrogate(self : Int32) -> Bool {
#| 0xDC00 <= self && self <= 0xDFFF
#|}
#|pub fn Int32::is_surrogate(self : Int32) -> Bool {
#| 0xD800 <= self && self <= 0xDFFF
#|}
#|pub fn Int32::abs(self : Int32) -> Int32 {
#| if self < 0 {
#| -self
#| } else {
#| self
#| }
#|}
),
"int64.mbt": (
#|pub fn Int64::Int64(self : Int64) -> Int64 = "%identity"
#|pub fn Int64::abs(self : Int64) -> Int64 {
#| if self < 0L {
#| -self
#| } else {
#| self
#| }
#|}
#|pub fn Int64::min(self : Int64, other : Int64) -> Int64 {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Int64::max(self : Int64, other : Int64) -> Int64 {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Int64::clamp(self : Int64, min~ : Int64, max~ : Int64) -> Int64 {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub impl Hash for Int64 with fn hash_combine(self, hasher) {
#| hasher.combine_int64(self)
#|}
#|pub impl Hash for Int64 with fn hash(self : Int64) -> Int32 {
#| Hash::hash(self.reinterpret_as_uint64())
#|}
#|pub impl Neg for Int64 with fn neg(self : Int64) -> Int64 = "%i64_neg"
#|pub impl Add for Int64 with fn add(self, other) = "%i64_add"
#|pub impl Sub for Int64 with fn sub(self, other) = "%i64_sub"
#|pub impl Mul for Int64 with fn mul(self, other) = "%i64_mul"
#|pub impl Div for Int64 with fn div(self, other) = "%i64_div"
#|pub impl Mod for Int64 with fn mod(self, other) = "%i64_mod"
#|pub fn Int64::lnot(self : Int64) -> Int64 = "%i64_lnot"
#|pub impl BitAnd for Int64 with fn land(self, other) = "%i64_land"
#|pub impl BitOr for Int64 with fn lor(self, other) = "%i64_lor"
#|pub impl BitXOr for Int64 with fn lxor(self, other) = "%i64_lxor"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn Int64::lsl(self : Int64, other : Int32) -> Int64 = "%i64_shl"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn Int64::shl(self : Int64, other : Int32) -> Int64 = "%i64_shl"
#|#deprecated("Use UInt64 type and infix operator `>>` instead")
#|#coverage.skip
#|pub fn Int64::lsr(self : Int64, other : Int32) -> Int64 = "%u64.shr"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn Int64::asr(self : Int64, other : Int32) -> Int64 = "%i64_shr"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn Int64::shr(self : Int64, other : Int32) -> Int64 = "%i64_shr"
#|pub impl Shl for Int64 with fn shl(self, other) = "%i64_shl"
#|pub impl Shr for Int64 with fn shr(self, other) = "%i64_shr"
#|pub fn Int64::ctz(self : Int64) -> Int32 = "%i64_ctz"
#|pub fn Int64::clz(self : Int64) -> Int32 = "%i64_clz"
#|pub fn Int64::popcnt(self : Int64) -> Int32 = "%i64_popcnt"
#|pub impl Eq for Int64 with fn equal(self : Int64, other : Int64) -> Bool = "%i64_eq"
#|pub impl Eq for Int64 with fn not_equal(self : Int64, other : Int64) -> Bool = "%i64_ne"
#|pub impl Compare for Int64 with fn compare(self, other) = "%i64_compare"
#|pub impl Compare for Int64 with fn lt(x, y) = "%i64.lt"
#|pub impl Compare for Int64 with fn le(x, y) = "%i64.le"
#|pub impl Compare for Int64 with fn gt(x, y) = "%i64.gt"
#|pub impl Compare for Int64 with fn ge(x, y) = "%i64.ge"
#|pub impl Default for Int64 with fn default() = "%i64_default"
#|pub fn Int64::to_int(self : Int64) -> Int32 = "%i64_to_i32"
#|pub fn Int64::to_uint(self : Int64) -> UInt32 {
#| self.to_int().reinterpret_as_uint()
#|}
#|pub fn Int64::to_double(self : Int64) -> Double = "%i64_to_f64"
#|pub fn Int64::reinterpret_as_double(self : Int64) -> Double = "%i64_to_f64_reinterpret"
#|pub fn UInt64::reinterpret_as_double(self : UInt64) -> Double = "%i64_to_f64_reinterpret"
#|pub fn Int64::to_byte(self : Int64) -> Byte = "%i64_to_byte"
#|pub fn Int64::to_uint16(self : Int64) -> UInt16 = "%i64_to_u16"
#|pub fn Int32::to_int64(self : Int32) -> Int64 = "%i32_to_i64"
#|pub fn UInt16::to_int64(self : UInt16) -> Int64 = "%u16_to_i64"
#|#deprecated("Use `reinterpret_as_int64` instead")
#|#coverage.skip
#|pub fn Double::reinterpret_as_i64(self : Double) -> Int64 = "%f64_to_i64_reinterpret"
#|pub fn Double::reinterpret_as_int64(self : Double) -> Int64 = "%f64_to_i64_reinterpret"
#|#deprecated("Use `reinterpret_as_uint64` instead")
#|#coverage.skip
#|pub fn Double::reinterpret_as_u64(self : Double) -> UInt64 = "%f64_to_i64_reinterpret"
#|pub fn Double::reinterpret_as_uint64(self : Double) -> UInt64 = "%f64_to_i64_reinterpret"
#|#deprecated("Use `reinterpret_as_uint64` instead")
#|#coverage.skip
#|pub fn Int64::to_uint64(self : Int64) -> UInt64 = "%i64.to_u64_reinterpret"
#|pub fn Int64::reinterpret_as_uint64(self : Int64) -> UInt64 = "%i64.to_u64_reinterpret"
#|#deprecated("Use `reinterpret_as_int64` instead")
#|#coverage.skip
#|pub fn UInt64::to_int64(self : UInt64) -> Int64 = "%u64.to_i64_reinterpret"
#|pub fn UInt64::reinterpret_as_int64(self : UInt64) -> Int64 = "%u64.to_i64_reinterpret"
#|pub fn UInt64::to_uint16(self : UInt64) -> UInt16 {
#| self.reinterpret_as_int64().to_uint16()
#|}
#|pub fn UInt64::to_uint(self : UInt64) -> UInt32 = "%u64.to_u32"
#|pub fn UInt64::to_int(self : UInt64) -> Int32 = "%u64.to_i32"
#|pub fn UInt64::to_double(self : UInt64) -> Double = "%u64.to_f64"
#|pub impl Add for UInt64 with fn add(self, other) = "%u64.add"
#|pub impl Sub for UInt64 with fn sub(self, other) = "%u64.sub"
#|pub impl Mul for UInt64 with fn mul(self, other) = "%u64.mul"
#|pub impl Div for UInt64 with fn div(self, other) = "%u64.div"
#|pub impl Mod for UInt64 with fn mod(self, other) = "%u64.mod"
#|pub impl Compare for UInt64 with fn compare(self, other) = "%u64.compare"
#|pub impl Compare for UInt64 with fn lt(x, y) = "%u64.lt"
#|pub impl Compare for UInt64 with fn le(x, y) = "%u64.le"
#|pub impl Compare for UInt64 with fn gt(x, y) = "%u64.gt"
#|pub impl Compare for UInt64 with fn ge(x, y) = "%u64.ge"
#|pub impl Eq for UInt64 with fn equal(self : UInt64, other : UInt64) -> Bool = "%u64.eq"
#|pub impl Eq for UInt64 with fn not_equal(self : UInt64, other : UInt64) -> Bool = "%u64.ne"
#|pub impl BitAnd for UInt64 with fn land(self, other) = "%u64.bitand"
#|pub impl BitOr for UInt64 with fn lor(self, other) = "%u64.bitor"
#|pub impl BitXOr for UInt64 with fn lxor(self, other) = "%u64.bitxor"
#|pub fn UInt64::lnot(self : UInt64) -> UInt64 = "%u64.bitnot"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn UInt64::lsl(self : UInt64, shift : Int32) -> UInt64 = "%u64.shl"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn UInt64::shl(self : UInt64, shift : Int32) -> UInt64 = "%u64.shl"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn UInt64::shr(self : UInt64, shift : Int32) -> UInt64 = "%u64.shr"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn UInt64::lsr(self : UInt64, shift : Int32) -> UInt64 = "%u64.shr"
#|pub impl Shl for UInt64 with fn shl(self, shift) = "%u64.shl"
#|pub impl Shr for UInt64 with fn shr(self, shift) = "%u64.shr"
#|pub fn UInt64::clz(self : UInt64) -> Int32 = "%u64.clz"
#|pub fn UInt64::ctz(self : UInt64) -> Int32 = "%u64.ctz"
#|pub fn UInt64::popcnt(self : UInt64) -> Int32 = "%u64.popcnt"
),
"intrinsics.mbt": (
#|pub fn[T] ignore(t : T) -> Unit = "%ignore"
#|pub fn[T] physical_equal(a : T, b : T) -> Bool = "%refeq"
#|#cfg(any(target="native", target="wasm"))
#|fn[T] unsafe_is_unique_ref(value : T) -> Bool = "%unsafe_is_unique_ref"
#|pub fn[T] abort(msg : String) -> T = "%abort"
#|pub fn[T] panic() -> T = "%panic"
#|#deprecated
#|pub fn not(x : Bool) -> Bool = "%bool_not"
#|pub impl Eq for Bool with fn equal(self : Bool, other : Bool) -> Bool = "%bool_eq"
#|pub impl Compare for Bool with fn compare(self, other) = "%bool_compare"
#|pub impl Default for Bool with fn default() = "%bool_default"
#|pub impl Neg for Int32 with fn neg(self) = "%i32_neg"
#|pub impl Add for Int32 with fn add(self, other) = "%i32_add"
#|pub impl Sub for Int32 with fn sub(self, other) = "%i32_sub"
#|pub impl Mul for Int32 with fn mul(self, other) = "%i32_mul"
#|pub impl Div for Int32 with fn div(self, other) = "%i32_div"
#|pub impl Mod for Int32 with fn mod(self, other) = "%i32_mod"
#|pub fn Int32::lnot(self : Int32) -> Int32 = "%i32_lnot"
#|pub impl BitAnd for Int32 with fn land(self : Int32, other : Int32) -> Int32 = "%i32_land"
#|pub impl BitOr for Int32 with fn lor(self : Int32, other : Int32) -> Int32 = "%i32_lor"
#|pub impl BitXOr for Int32 with fn lxor(self : Int32, other : Int32) -> Int32 = "%i32_lxor"
#|pub impl Shl for Int32 with fn shl(self, other) = "%i32_shl"
#|pub impl Shr for Int32 with fn shr(self, other) = "%i32_shr"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn Int32::lsl(self : Int32, other : Int32) -> Int32 = "%i32_shl"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn Int32::shl(self : Int32, other : Int32) -> Int32 = "%i32_shl"
#|#deprecated("Use UInt type and infix operator `>>` instead")
#|#coverage.skip
#|pub fn Int32::lsr(self : Int32, other : Int32) -> Int32 {
#| (self.reinterpret_as_uint() >> other).reinterpret_as_int()
#|}
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn Int32::asr(self : Int32, other : Int32) -> Int32 = "%i32_shr"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn Int32::shr(self : Int32, other : Int32) -> Int32 = "%i32_shr"
#|pub fn Int32::ctz(self : Int32) -> Int32 = "%i32_ctz"
#|pub fn Int32::clz(self : Int32) -> Int32 = "%i32_clz"
#|pub fn Int32::popcnt(self : Int32) -> Int32 = "%i32_popcnt"
#|pub impl Eq for Int32 with fn equal(self : Int32, other : Int32) -> Bool = "%i32_eq"
#|pub impl Eq for Int32 with fn not_equal(self : Int32, other : Int32) -> Bool = "%i32_ne"
#|pub impl Compare for Int32 with fn compare(self, other) = "%i32_compare"
#|pub impl Compare for Int32 with fn lt(x, y) = "%i32.lt"
#|pub impl Compare for Int32 with fn le(x, y) = "%i32.le"
#|pub impl Compare for Int32 with fn gt(x, y) = "%i32.gt"
#|pub impl Compare for Int32 with fn ge(x, y) = "%i32.ge"
#|pub fn Int32::is_pos(self : Int32) -> Bool = "%i32_is_pos"
#|pub fn Int32::is_neg(self : Int32) -> Bool = "%i32_is_neg"
#|pub fn Int32::is_non_pos(self : Int32) -> Bool = "%i32_is_non_pos"
#|pub fn Int32::is_non_neg(self : Int32) -> Bool = "%i32_is_non_neg"
#|pub impl Default for Int32 with fn default() = "%i32_default"
#|pub fn Int32::to_double(self : Int32) -> Double = "%i32_to_f64"
#|pub fn Int32::to_int(self : Int32) -> Int32 = "%identity"
#|pub fn Int32::reinterpret_as_uint(self : Int32) -> UInt32 = "%i32.to_u32_reinterpret"
#|#deprecated("Use `reinterpret_as_uint` instead")
#|#coverage.skip
#|pub fn Int32::to_uint(self : Int32) -> UInt32 = "%i32.to_u32_reinterpret"
#|pub fn Int32::to_uint64(self : Int32) -> UInt64 {
#| self.to_int64().reinterpret_as_uint64()
#|}
#|pub impl Neg for Double with fn neg(self) = "%f64_neg"
#|pub impl Add for Double with fn add(self, other) = "%f64_add"
#|pub impl Sub for Double with fn sub(self, other) = "%f64_sub"
#|pub impl Mul for Double with fn mul(self, other) = "%f64_mul"
#|pub impl Div for Double with fn div(self, other) = "%f64_div"
#|pub fn Double::sqrt(self : Double) -> Double = "%f64_sqrt"
#|pub impl Eq for Double with fn equal(self : Double, other : Double) -> Bool = "%f64_eq"
#|pub impl Eq for Double with fn not_equal(self : Double, other : Double) -> Bool = "%f64_ne"
#|pub impl Compare for Double with fn compare(self, other) = "%f64_compare"
#|pub impl Compare for Double with fn lt(x, y) = "%f64.lt"
#|pub impl Compare for Double with fn le(x, y) = "%f64.le"
#|pub impl Compare for Double with fn gt(x, y) = "%f64.gt"
#|pub impl Compare for Double with fn ge(x, y) = "%f64.ge"
#|pub impl Default for Double with fn default() = "%f64_default"
#|pub fn Char::to_int(self : Char) -> Int32 = "%char_to_int"
#|pub fn Char::to_uint(self : Char) -> UInt32 {
#| self.to_int().reinterpret_as_uint()
#|}
#|#deprecated("Use `Int::unsafe_to_char` instead, and use `Int::to_char` for safe conversion")
#|pub fn Char::from_int(val : Int32) -> Char = "%char_from_int"
#|pub impl Eq for Char with fn equal(self : Char, other : Char) -> Bool = "%char_eq"
#|pub impl Eq for Char with fn not_equal(self : Char, other : Char) -> Bool = "%i32_ne"
#|pub impl Compare for Char with fn compare(self, other) = "%char_compare"
#|pub impl Compare for Char with fn lt(x, y) = "%i32.lt"
#|pub impl Compare for Char with fn le(x, y) = "%i32.le"
#|pub impl Compare for Char with fn gt(x, y) = "%i32.gt"
#|pub impl Compare for Char with fn ge(x, y) = "%i32.ge"
#|pub impl Default for Char with fn default() = "%char_default"
#|#alias("_[_]")
#|pub fn Bytes::at(self : Bytes, idx : Int32) -> Byte = "%bytes_get"
#|#internal(unsafe, "Panic if index is out of bounds")
#|#doc(hidden)
#|pub fn Bytes::unsafe_get(self : Bytes, idx : Int32) -> Byte = "%bytes.unsafe_get"
#|pub fn Bytes::length(self : Bytes) -> Int32 = "%bytes_length"
#|pub fn Bytes::make(len : Int32, init : Byte) -> Bytes {
#| if len < 0 {
#| return []
#| }
#| Bytes::unsafe_make(len, init)
#|}
#|fn Bytes::unsafe_make(len : Int32, init : Byte) -> Bytes = "%bytes_make"
#|pub fn Int32::to_byte(self : Int32) -> Byte = "%i32_to_byte"
#|#doc(hidden)
#|pub fn Int32::unsafe_to_char(self : Int32) -> Char = "%char_from_int"
#|pub fn Int32::to_char(self : Int32) -> Char? {
#| if self is (0..<=0xD7FF) || self is (0xE000..<=0x10FFFF) {
#| Some(self.unsafe_to_char())
#| } else {
#| None
#| }
#|}
#|pub fn UInt64::to_byte(self : UInt64) -> Byte {
#| self.to_int().to_byte()
#|}
#|#alias("_[_]")
#|pub fn[T] FixedArray::at(self : FixedArray[T], idx : Int32) -> T = "%fixedarray.get"
#|#internal(unsafe, "Panic if index is out of bounds")
#|#doc(hidden)
#|pub fn[T] FixedArray::unsafe_get(self : FixedArray[T], idx : Int32) -> T = "%fixedarray.unsafe_get"
#|#internal(unsafe, "Panic if index is out of bounds")
#|#doc(hidden)
#|pub fn[T] FixedArray::unsafe_set(
#| self : FixedArray[T],
#| idx : Int32,
#| val : T,
#|) -> Unit = "%fixedarray.unsafe_set"
#|#alias("_[_]=_")
#|pub fn[T] FixedArray::set(self : FixedArray[T], idx : Int32, val : T) -> Unit = "%fixedarray.set"
#|pub fn[T] FixedArray::length(self : FixedArray[T]) -> Int32 = "%fixedarray.length"
#|pub fn[T] FixedArray::make(len : Int32, init : T) -> FixedArray[T] = "%fixedarray.make"
#|#alias(charcode_length, deprecated)
#|pub fn String::length(self : String) -> Int32 = "%string_length"
#|#alias("_[_]")
#|#alias(code_unit_at)
#|pub fn String::at(self : String, idx : Int32) -> UInt16 = "%string_get"
#|#internal(unsafe, "Undefined behavior if index is out of bounds.")
#|#doc(hidden)
#|pub fn String::unsafe_get(self : String, idx : Int32) -> UInt16 = "%string.unsafe_get"
#|#internal(unsafe, "Panic if index is out of bounds.")
#|#doc(hidden)
#|#deprecated("Use `String::unsafe_get` instead")
#|pub fn String::unsafe_charcode_at(self : String, idx : Int32) -> Int32 = "%string.unsafe_get"
#|pub impl Add for String with fn add(self, other) = "%string_add"
#|pub impl Eq for String with fn equal(self : String, other : String) -> Bool = "%string_eq"
#|priv type UnsafeMaybeUninit[_]
#|pub fn Byte::to_int(self : Byte) -> Int32 = "%byte_to_int"
#|pub fn Byte::to_char(self : Byte) -> Char {
#| self.to_int().unsafe_to_char()
#|}
#|pub fn Byte::to_int64(self : Byte) -> Int64 {
#| self.to_int().to_int64()
#|}
#|pub fn UInt32::reinterpret_as_int(self : UInt32) -> Int32 = "%u32.to_i32_reinterpret"
#|#deprecated("Use `reinterpret_as_int` instead")
#|#coverage.skip
#|pub fn UInt32::to_int(self : UInt32) -> Int32 = "%u32.to_i32_reinterpret"
#|pub impl Add for UInt32 with fn add(self, other) = "%u32.add"
#|pub impl Sub for UInt32 with fn sub(self, other) = "%u32.sub"
#|pub impl Mul for UInt32 with fn mul(self, other) = "%u32.mul"
#|pub impl Div for UInt32 with fn div(self, other) = "%u32.div"
#|pub impl Mod for UInt32 with fn mod(self, other) = "%u32.mod"
#|pub impl Eq for UInt32 with fn equal(self : UInt32, other : UInt32) -> Bool = "%u32.eq"
#|pub impl Eq for UInt32 with fn not_equal(self : UInt32, other : UInt32) -> Bool = "%u32.ne"
#|pub impl Compare for UInt32 with fn compare(self, other) = "%u32.compare"
#|pub impl Compare for UInt32 with fn lt(x, y) = "%u32.lt"
#|pub impl Compare for UInt32 with fn le(x, y) = "%u32.le"
#|pub impl Compare for UInt32 with fn gt(x, y) = "%u32.gt"
#|pub impl Compare for UInt32 with fn ge(x, y) = "%u32.ge"
#|pub impl BitAnd for UInt32 with fn land(self : UInt32, other : UInt32) -> UInt32 = "%u32.bitand"
#|pub impl BitOr for UInt32 with fn lor(self : UInt32, other : UInt32) -> UInt32 = "%u32.bitor"
#|pub impl BitXOr for UInt32 with fn lxor(self : UInt32, other : UInt32) -> UInt32 = "%u32.bitxor"
#|pub fn UInt32::lnot(self : UInt32) -> UInt32 = "%u32.bitnot"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn UInt32::lsl(self : UInt32, shift : Int32) -> UInt32 = "%u32.shl"
#|#deprecated("Use infix operator `<<` instead")
#|#coverage.skip
#|pub fn UInt32::shl(self : UInt32, shift : Int32) -> UInt32 = "%u32.shl"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn UInt32::lsr(self : UInt32, shift : Int32) -> UInt32 = "%u32.shr"
#|#deprecated("Use infix operator `>>` instead")
#|#coverage.skip
#|pub fn UInt32::shr(self : UInt32, shift : Int32) -> UInt32 = "%u32.shr"
#|pub impl Shl for UInt32 with fn shl(self, shift) = "%u32.shl"
#|pub impl Shr for UInt32 with fn shr(self, shift) = "%u32.shr"
#|pub fn UInt32::clz(self : UInt32) -> Int32 = "%u32.clz"
#|pub fn UInt32::ctz(self : UInt32) -> Int32 = "%u32.ctz"
#|pub fn UInt32::popcnt(self : UInt32) -> Int32 = "%u32.popcnt"
#|pub fn UInt32::to_uint64(self : UInt32) -> UInt64 = "%u32.to_u64"
#|pub fn UInt32::to_byte(self : UInt32) -> Byte {
#| self.reinterpret_as_int().to_byte()
#|}
#|pub fn UInt32::to_double(self : UInt32) -> Double = "%u32.to_f64"
#|pub fn Byte::to_double(self : Byte) -> Double {
#| self.to_int().to_double()
#|}
#|pub fn UInt16::to_int(self : UInt16) -> Int32 = "%u16_to_i32"
#|pub fn UInt16::to_byte(self : UInt16) -> Byte = "%u16_to_byte"
#|pub fn UInt32::to_uint16(self : UInt32) -> UInt16 {
#| self.reinterpret_as_int().to_uint16()
#|}
#|pub fn Int32::to_uint16(self : Int32) -> UInt16 = "%i32_to_u16"
#|pub fn Byte::to_uint16(self : Byte) -> UInt16 = "%byte_to_u16"
),
"iter.mbt": (
#|pub fn Int32::until(
#| self : Int32,
#| end : Int32,
#| step~ : Int32 = 1,
#| inclusive~ : Bool = false,
#|) -> Iter[Int32] {
#| until_impl(self, end, step, inclusive)
#|}
#|pub fn Int64::until(
#| self : Int64,
#| end : Int64,
#| step~ : Int64 = 1L,
#| inclusive~ : Bool = false,
#|) -> Iter[Int64] {
#| until_impl(self, end, step, inclusive)
#|}
),
"iterator.mbt": (
#|#alias(Iterator, deprecated="The name `Iterator` is deprecated, use `Iter` instead. Note that if you have defined `iterator()` method to support `for .. in` loop, you should also rename `iterator()` to `iter()`. See https://github.com/moonbitlang/core/pull/3127 for more details.")
#|struct Iter[X] {
#| f : () -> X?
#| mut size_hint : Int32?
#|}
#|priv enum IntersperseState[X] {
#| IntersperseInit
#| IntersperseElem(X)
#| IntersperseSep
#|}
#|#alias(peek, deprecated)
#|#alias(head)
#|pub fn[X] Iter::next(self : Iter[X]) -> X? {
#| let result = (self.f)()
#| match (result, self.size_hint) {
#| (Some(_), Some(n)) =>
#| self.size_hint = if n > 0 { Some(n - 1) } else { Some(0) }
#| (None, _) => self.size_hint = Some(0)
#| _ => ()
#| }
#| result
#|}
#|pub fn[X] Iter::size_hint(self : Iter[X]) -> Int32? {
#| self.size_hint
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[X : Show] Show for Iter[X]
#|pub impl[X : Show] Show for Iter[X] with fn output(self, logger) {
#| logger <+ "["
#| if self.next() is Some(x) {
#| logger.write_object(x)
#| while self.next() is Some(x) {
#| logger <+ ", \{cb => cb.write_object(x)}"
#| }
#| }
#| logger <+ "]"
#|}
#|#locals(f)
#|pub fn[X] Iter::each(self : Iter[X], f : (X) -> Unit raise?) -> Unit raise? {
#| for x in self {
#| f(x)
#| }
#|}
#|#locals(f)
#|pub fn[X] Iter::any(self : Iter[X], f : (X) -> Bool raise?) -> Bool raise? {
#| for x in self {
#| if f(x) {
#| break true
#| }
#| } nobreak {
#| false
#| }
#|}
#|#locals(f)
#|pub fn[X] Iter::all(self : Iter[X], f : (X) -> Bool raise?) -> Bool raise? {
#| for x in self {
#| guard f(x) else { break false }
#| } nobreak {
#| true
#| }
#|}
#|#locals(f)
#|pub fn[X] Iter::eachi(
#| self : Iter[X],
#| f : (Int32, X) -> Unit raise?,
#|) -> Unit raise? {
#| for i, x in self {
#| f(i, x)
#| }
#|}
#|#locals(f)
#|pub fn[X, R] Iter::fold(
#| self : Iter[X],
#| init~ : R,
#| f : (R, X) -> R raise?,
#|) -> R raise? {
#| let mut acc = init
#| while self.next() is Some(x) {
#| acc = f(acc, x)
#| }
#| acc
#|}
#|#alias(length, deprecated="Use `count` instead")
#|pub fn[X] Iter::count(self : Iter[X]) -> Int32 {
#| for _ in self; count = 0 {
#| continue count + 1
#| } nobreak {
#| count
#| }
#|}
#|#locals(f)
#|pub fn[X] Iter::count_if(
#| self : Iter[X],
#| f : (X) -> Bool raise?,
#|) -> Int32 raise? {
#| for x in self; count = 0 {
#| if f(x) {
#| continue count + 1
#| }
#| continue count
#| } nobreak {
#| count
#| }
#|}
#|#alias(new, deprecated="Use `Iter(f, size_hint=...)` instead")
#|#owned(f)
#|pub fn[X] Iter::Iter(f : () -> X?, size_hint? : Int32) -> Iter[X] {
#| let size_hint = match size_hint {
#| Some(n) if n > 0 => Some(n)
#| Some(_) => Some(0)
#| None => None
#| }
#| { f, size_hint, }
#|}
#|pub fn[X] Iter::empty() -> Iter[X] {
#| Iter(() => None, size_hint=0)
#|}
#|pub fn[X] Iter::singleton(elem : X) -> Iter[X] {
#| let mut consumed = false
#| Iter(
#| fn() {
#| if consumed {
#| None
#| } else {
#| consumed = true
#| Some(elem)
#| }
#| },
#| size_hint=1,
#| )
#|}
#|pub fn[X] Iter::repeat(x : X) -> Iter[X] {
#| Iter(() => Some(x))
#|}
#|pub fn[X] Iter::filter(self : Iter[X], f : (X) -> Bool) -> Iter[X] {
#| Iter(fn() {
#| for x in self {
#| if f(x) {
#| break Some(x)
#| }
#| } nobreak {
#| None
#| }
#| })
#|}
#|pub fn[X, Y] Iter::map(self : Iter[X], f : (X) -> Y) -> Iter[Y] {
#| {
#| f: fn() {
#| guard self.next() is Some(x) else { None }
#| Some(f(x))
#| },
#| size_hint: self.size_hint,
#| }
#|}
#|pub fn[X, Y] Iter::mapi(self : Iter[X], f : (Int32, X) -> Y) -> Iter[Y] {
#| let mut i = 0
#| {
#| f: fn() {
#| guard self.next() is Some(x) else { None }
#| let result = f(i, x)
#| i += 1
#| Some(result)
#| },
#| size_hint: self.size_hint,
#| }
#|}
#|pub fn[X, Y] Iter::filter_map(self : Iter[X], f : (X) -> Y?) -> Iter[Y] {
#| Iter(fn() {
#| while self.next() is Some(x) {
#| guard f(x) is (Some(_) as y) else { () }
#| break y
#| } nobreak {
#| None
#| }
#| })
#|}
#|pub fn[X, Y] Iter::flat_map(self : Iter[X], f : (X) -> Iter[Y]) -> Iter[Y] {
#| let mut current_iter = Some([||])
#| Iter(fn() {
#| guard current_iter is Some(iter) else { None }
#| for x = iter.next() {
#| match x {
#| Some(_) as elem => break elem
#| None => {
#| guard self.next() is Some(x) else { break None }
#| let iter = f(x)
#| current_iter = Some(iter)
#| continue iter.next()
#| }
#| }
#| }
#| })
#|}
#|pub fn[X] Iter::flatten(self : Iter[Iter[X]]) -> Iter[X] {
#| self.flat_map(it => it)
#|}
#|#cfg(not(target="js"))
#|pub fn[A : ToStringView] Iter::join(self : Iter[A], sep : StringView) -> String {
#| let parts = self.to_array()
#| join_string_views(parts, sep)
#|}
#|#cfg(target="js")
#|pub fn[A : ToStringView] Iter::join(self : Iter[A], sep : StringView) -> String {
#| let mut result = ""
#| if self.next() is Some(first) {
#| result = first.to_string_view().to_owned()
#| let separator = sep.to_owned()
#| while self.next() is Some(part) {
#| result += separator
#| result += part.to_string_view().to_owned()
#| }
#| }
#| result
#|}
#|pub fn[X] Iter::tap(self : Iter[X], f : (X) -> Unit) -> Iter[X] {
#| {
#| f: fn() {
#| let result = self.next()
#| if result is Some(x) {
#| f(x)
#| }
#| result
#| },
#| size_hint: self.size_hint,
#| }
#|}
#|pub fn[X] Iter::take(self : Iter[X], n : Int32) -> Iter[X] {
#| let mut remaining = n
#| let size_hint = match self.size_hint {
#| Some(_) if n <= 0 => Some(0)
#| Some(len) if n < len => Some(n)
#| Some(len) => Some(len)
#| None if n <= 0 => Some(0)
#| None => None
#| }
#| {
#| f: fn() {
#| guard remaining > 0 else { None }
#| let result = self.next()
#| if result is Some(_) {
#| remaining -= 1
#| }
#| result
#| },
#| size_hint,
#| }
#|}
#|pub fn[X] Iter::take_while(self : Iter[X], f : (X) -> Bool) -> Iter[X] {
#| let mut still_running = true
#| Iter(fn() {
#| guard still_running else { None }
#| let result = self.next()
#| if result is Some(x) && !f(x) {
#| still_running = false
#| None
#| } else {
#| result
#| }
#| })
#|}
#|pub fn[X, Y] Iter::map_while(self : Iter[X], f : (X) -> Y?) -> Iter[Y] {
#| let mut still_running = true
#| Iter(fn() {
#| guard still_running else { None }
#| let src = self.next()
#| guard src is Some(x) else { None }
#| let result = f(x)
#| if result is None {
#| still_running = false
#| }
#| result
#| })
#|}
#|pub fn[X] Iter::drop(self : Iter[X], n : Int32) -> Iter[X] {
#| let mut remaining = n
#| let size_hint = match self.size_hint {
#| Some(len) if n <= 0 => Some(len)
#| Some(len) if n < len => Some(len - n)
#| Some(_) => Some(0)
#| None => None
#| }
#| {
#| f: fn() {
#| while remaining > 0 {
#| guard self.next() is Some(_) else { break None }
#| remaining -= 1
#| } nobreak {
#| self.next()
#| }
#| },
#| size_hint,
#| }
#|}
#|pub fn[X] Iter::drop_while(self : Iter[X], f : (X) -> Bool) -> Iter[X] {
#| let mut dropped = false
#| Iter(fn() {
#| if !dropped {
#| dropped = true
#| for x = self.next() {
#| match x {
#| Some(x) if f(x) => continue self.next()
#| result => break result
#| }
#| }
#| } else {
#| self.next()
#| }
#| })
#|}
#|pub fn[X] Iter::find_first(self : Iter[X], f : (X) -> Bool raise?) -> X? raise? {
#| for x in self {
#| if f(x) {
#| break Some(x)
#| }
#| } nobreak {
#| None
#| }
#|}
#|pub fn[X] Iter::concat(self : Iter[X], other : Iter[X]) -> Iter[X] {
#| let mut in_first = true
#| let size_hint = match (self.size_hint, other.size_hint) {
#| (Some(n), Some(m)) => Some(n + m)
#| _ => None
#| }
#| {
#| f: fn() {
#| if in_first {
#| let result = self.next()
#| if result is None {
#| in_first = false
#| other.next()
#| } else {
#| result
#| }
#| } else {
#| other.next()
#| }
#| },
#| size_hint,
#| }
#|}
#|#alias(combine)
#|pub fn[X, Y] Iter::zip(self : Iter[X], other : Iter[Y]) -> Iter[(X, Y)] {
#| let size_hint = match (self.size_hint, other.size_hint) {
#| (Some(n), Some(m)) if n < m => Some(n)
#| (Some(_), Some(m)) | ((Some(0), _) | (_, Some(0)) with m = 0) => Some(m)
#| _ => None
#| }
#| {
#| f: fn() {
#| guard self.next() is Some(x) else { None }
#| guard other.next() is Some(y) else { None }
#| Some((x, y))
#| },
#| size_hint,
#| }
#|}
#|pub impl[T] Add for Iter[T] with fn add(self, other) {
#| self.concat(other)
#|}
#|#alias(collect)
#|pub fn[X] Iter::to_array(self : Iter[X]) -> Array[X] {
#| let result = match self.size_hint {
#| Some(n) => Array::Array(capacity=n)
#| None => []
#| }
#| while self.next() is Some(x) {
#| result.push(x)
#| }
#| result
#|}
#|#alias(iterator, deprecated)
#|pub fn[X] Iter::iter(self : Iter[X]) -> Iter[X] {
#| self
#|}
#|#alias(iterator2, deprecated)
#|pub fn[X] Iter::iter2(self : Iter[X]) -> Iter2[Int32, X] {
#| let mut i = 0
#| Iter2({
#| f: () => {
#| guard self.next() is Some(elem) else { None }
#| let result = Some((i, elem))
#| i += 1
#| result
#| },
#| size_hint: self.size_hint,
#| })
#|}
#|pub fn[X] Iter::last(self : Iter[X]) -> X? {
#| for x = (None : X?), y = self.next() {
#| match (x, y) {
#| (last, None) => break last
#| (_, Some(_) as x) => continue x, self.next()
#| }
#| }
#|}
#|pub fn[X] Iter::intersperse(self : Iter[X], sep : X) -> Iter[X] {
#| let mut state : IntersperseState[X] = IntersperseInit
#| let size_hint = match self.size_hint {
#| Some(0) => Some(0)
#| Some(n) => Some(n * 2 - 1)
#| None => None
#| }
#| {
#| f: fn() {
#| match state {
#| IntersperseInit => {
#| let result = self.next()
#| state = IntersperseSep
#| result
#| }
#| IntersperseElem(x) => {
#| state = IntersperseSep
#| Some(x)
#| }
#| IntersperseSep =>
#| match self.next() {
#| Some(x) => {
#| state = IntersperseElem(x)
#| Some(sep)
#| }
#| None => None
#| }
#| }
#| },
#| size_hint,
#| }
#|}
#|#alias("_[_:_]")
#|#alias(view, deprecated="Use `clamped_view` instead")
#|#alias(sub, deprecated="Use `clamped_view` instead")
#|pub fn[X] Iter::clamped_view(
#| self : Iter[X],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Iter[X] {
#| match (start, end) {
#| (_..<=0, None) => self
#| (_..<=0, Some(end)) => self.take(end)
#| (start, None) => self.drop(start)
#| (start, Some(end)) => {
#| let mut index = 0
#| let size_hint = match self.size_hint {
#| Some(_) if end <= start => Some(0)
#| Some(len) if start >= len => Some(0)
#| Some(len) if end < len => Some(end - start)
#| Some(len) => Some(len - start)
#| None if end <= start => Some(0)
#| None => None
#| }
#| {
#| f: fn() {
#| if index >= end {
#| return None
#| }
#| while index < start {
#| guard self.next() is Some(_) else { return None }
#| index += 1
#| }
#| if index >= end {
#| return None
#| }
#| let result = self.next()
#| if result is Some(_) {
#| index += 1
#| }
#| result
#| },
#| size_hint,
#| }
#| }
#| }
#|}
#|pub fn[X : Eq] Iter::contains(self : Iter[X], value : X) -> Bool {
#| for x in self {
#| if x == value {
#| break true
#| }
#| } nobreak {
#| false
#| }
#|}
#|pub fn[X] Iter::nth(self : Iter[X], n : Int32) -> X? {
#| guard n >= 0 else { None }
#| for _ in 0.. X? {
#| guard self.next() is Some(x) else { return None }
#| let mut res = x
#| for x in self {
#| if x > res {
#| res = x
#| }
#| }
#| Some(res)
#|}
#|pub fn[X : Compare] Iter::minimum(self : Iter[X]) -> X? {
#| guard self.next() is Some(x) else { return None }
#| let mut res = x
#| for x in self {
#| if x < res {
#| res = x
#| }
#| }
#| Some(res)
#|}
#|#alias(Iterator2, deprecated="The name `Iterator2` is deprecated, use `Iter2` instead. Note that if you have defined `iterator2()` method to support `for .. in` loop, you should also rename `iterator2()` to `iter2()`. See https://github.com/moonbitlang/core/pull/3127 for more details.")
#|pub(all) struct Iter2[X, Y](Iter[(X, Y)])
#|#alias(iterator, deprecated)
#|pub fn[X, Y] Iter2::iter(self : Iter2[X, Y]) -> Iter[(X, Y)] {
#| self.0
#|}
#|#alias(iterator2, deprecated)
#|pub fn[X, Y] Iter2::iter2(self : Iter2[X, Y]) -> Iter2[X, Y] {
#| self
#|}
#|pub fn[X, Y] Iter2::next(self : Iter2[X, Y]) -> (X, Y)? {
#| self.0.next()
#|}
#|pub fn[X, Y] Iter2::each(
#| self : Iter2[X, Y],
#| f : (X, Y) -> Unit raise?,
#|) -> Unit raise? {
#| self.0.each(pair => f(pair.0, pair.1))
#|}
#|pub fn[X, Y] Iter2::concat(
#| self : Iter2[X, Y],
#| other : Iter2[X, Y],
#|) -> Iter2[X, Y] {
#| Iter2(self.0.concat(other.0))
#|}
#|pub fn[X, Y] Iter2::to_array(self : Iter2[X, Y]) -> Array[(X, Y)] {
#| self.0.to_array()
#|}
),
"linked_hash_map.mbt": (
#|#unsafe_cycle_free
#|priv struct Entry[K, V] {
#| mut prev : Int32
#| mut next : Entry[K, V]?
#| mut psl : Int32
#| hash : Int32
#| key : K
#| mut value : V
#|}
#|struct Map[K, V] {
#| mut entries : FixedArray[Entry[K, V]?]
#| mut size : Int32 // active key-value pairs count
#| mut capacity : Int32 // current capacity
#| mut capacity_mask : Int32 // capacity_mask = capacity - 1, used to find idx
#| mut grow_at : Int32 // threshold that triggers grow
#| mut head : Entry[K, V]? // head of linked list
#| mut tail : Int32 // tail of linked list
#|}
#|let default_init_capacity = 8
#|fn[K, V] new_map(capacity : Int32) -> Map[K, V] {
#| let capacity = capacity.next_power_of_two()
#| {
#| size: 0,
#| capacity,
#| capacity_mask: capacity - 1,
#| grow_at: calc_grow_threshold(capacity),
#| entries: FixedArray::make(capacity, None),
#| head: None,
#| tail: -1,
#| }
#|}
#|fn capacity_for_length(length : Int32) -> Int32 {
#| guard length <= calc_grow_threshold(0x40000000) else {
#| abort("Map capacity overflow")
#| }
#| let mut capacity = length.next_power_of_two()
#| if length > calc_grow_threshold(capacity) {
#| capacity *= 2
#| }
#| capacity
#|}
#|#alias(from_array)
#|pub fn[K : Hash + Eq, V] Map::Map(
#| arr : ArrayView[(K, V)],
#| capacity? : Int32,
#|) -> Map[K, V] {
#| let length = arr.length()
#| let capacity = match capacity {
#| Some(capacity) => {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| capacity_for_length(capacity.max(length))
#| }
#| None =>
#| if length == 0 {
#| default_init_capacity
#| } else {
#| capacity_for_length(length)
#| }
#| }
#| let m = new_map(capacity)
#| for e in arr {
#| m.set(e.0, e.1)
#| }
#| m
#|}
#|#alias("_[_]=_")
#|#owned(value)
#|pub fn[K : Hash + Eq, V] Map::set(self : Map[K, V], key : K, value : V) -> Unit {
#| self.set_with_hash(key, value, Hash::hash(key))
#|}
#|#owned(value)
#|fn[K : Eq, V] Map::set_with_hash(
#| self : Map[K, V],
#| key : K,
#| value : V,
#| hash : Int32,
#|) -> Unit {
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| if self.size >= self.grow_at {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| let entry = { prev: self.tail, next: None, psl, key, value, hash, }
#| self.add_entry_to_tail(idx, entry)
#| return
#| }
#| Some(curr_entry) => {
#| if curr_entry.hash == hash && curr_entry.key == key {
#| curr_entry.value = value
#| return
#| }
#| if psl > curr_entry.psl {
#| if self.size >= self.grow_at {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| self.push_away(idx, curr_entry)
#| let entry = { prev: self.tail, next: None, psl, key, value, hash, }
#| self.add_entry_to_tail(idx, entry)
#| return
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|#owned(entry)
#|fn[K, V] Map::push_away(
#| self : Map[K, V],
#| idx : Int32,
#| entry : Entry[K, V],
#|) -> Unit {
#| for psl = entry.psl + 1, idx = (idx + 1) & self.capacity_mask, entry = entry {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| break
#| }
#| Some(curr_entry) =>
#| if psl > curr_entry.psl {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| continue curr_entry.psl + 1,
#| (idx + 1) & self.capacity_mask,
#| curr_entry
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask, entry
#| }
#| }
#| }
#|}
#|#owned(entry)
#|fn[K, V] Map::set_entry(
#| self : Map[K, V],
#| entry : Entry[K, V],
#| new_idx : Int32,
#|) -> Unit {
#| match entry.next {
#| None => self.tail = new_idx
#| Some(next) => next.prev = new_idx
#| }
#| self.entries.unsafe_set(new_idx, Some(entry))
#|}
#|pub fn[K : Hash + Eq, V] Map::get(self : Map[K, V], key : K) -> V? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && entry.key == key {
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|#alias("_[_]")
#|pub fn[K : Hash + Eq, V] Map::at(self : Map[K, V], key : K) -> V {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard! self.entries.unsafe_get(idx) is Some(entry)
#| if entry.hash == hash && entry.key == key {
#| return entry.value
#| }
#| guard! i <= entry.psl
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V] Map::get_or_default(
#| self : Map[K, V],
#| key : K,
#| default : V,
#|) -> V {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| Some(entry) => {
#| if entry.hash == hash && entry.key == key {
#| break entry.value
#| }
#| if i > entry.psl {
#| break default
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#| None => break default
#| }
#| }
#|}
#|pub fn[K : Hash + Eq, V] Map::get_or_init(
#| self : Map[K, V],
#| key : K,
#| default : () -> V raise?,
#|) -> V raise? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| return entry.value
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#| let new_value = default()
#| self.set_with_hash(key, new_value, hash)
#| new_value
#|}
#|pub fn[K : Hash + Eq, V] Map::update_or_default(
#| self : Map[K, V],
#| key : K,
#| default : V,
#| f : (V) -> V raise?,
#|) -> Unit raise? {
#| let hash = Hash::hash(key)
#| let (idx, psl, push_away) = for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| Some(entry) => {
#| if entry.hash == hash && entry.key == key {
#| let new_value = f(entry.value)
#| if self.entries.unsafe_get(idx & self.capacity_mask) is Some(current) &&
#| physical_equal(current, entry) {
#| entry.value = new_value
#| } else {
#| self.set_with_hash(key, new_value, hash)
#| }
#| return
#| }
#| if psl > entry.psl {
#| break (idx, psl, Some(entry))
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| None => break (idx, psl, None)
#| }
#| }
#| if self.size >= self.grow_at {
#| self.grow()
#| self.set_with_hash(key, default, hash)
#| } else {
#| if push_away is Some(entry) {
#| self.push_away(idx, entry)
#| }
#| let entry = { prev: self.tail, next: None, psl, hash, key, value: default, }
#| self.add_entry_to_tail(idx, entry)
#| }
#|}
#|pub fn[K : Hash + Eq, V] Map::contains(self : Map[K, V], key : K) -> Bool {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break false }
#| if entry.hash == hash && entry.key == key {
#| break true
#| }
#| if i > entry.psl {
#| break false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V : Eq] Map::contains_kv(
#| self : Map[K, V],
#| key : K,
#| value : V,
#|) -> Bool {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break false }
#| if entry.hash == hash && entry.key == key && entry.value == value {
#| break true
#| }
#| if i > entry.psl {
#| break false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V] Map::remove(self : Map[K, V], key : K) -> Unit {
#| self.remove_with_hash(key, Hash::hash(key))
#|}
#|fn[K : Eq, V] Map::remove_with_hash(
#| self : Map[K, V],
#| key : K,
#| hash : Int32,
#|) -> Unit {
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| break
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V] Map::remove_and_get(self : Map[K, V], key : K) -> V? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && entry.key == key {
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|#owned(entry)
#|fn[K, V] Map::add_entry_to_tail(
#| self : Map[K, V],
#| idx : Int32,
#| entry : Entry[K, V],
#|) -> Unit {
#| match self.tail {
#| -1 => self.head = Some(entry)
#| tail => self.entries[tail].unwrap().next = Some(entry)
#| }
#| self.tail = idx
#| self.entries.unsafe_set(idx, Some(entry))
#| self.size += 1
#|}
#|fn[K, V] Map::remove_entry(self : Map[K, V], entry : Entry[K, V]) -> Unit {
#| match entry.prev {
#| -1 => self.head = entry.next
#| idx => self.entries[idx].unwrap().next = entry.next
#| }
#| match entry.next {
#| None => self.tail = entry.prev
#| Some(next) => next.prev = entry.prev
#| }
#|}
#|fn[K, V] Map::shift_back(self : Map[K, V], idx : Int32) -> Unit {
#| for cur = idx {
#| let next = (cur + 1) & self.capacity_mask
#| match self.entries.unsafe_get(next) {
#| None | Some({ psl: 0, .. }) => {
#| self.entries.unsafe_set(cur, None)
#| break
#| }
#| Some(entry) => {
#| entry.psl -= 1
#| self.set_entry(entry, cur)
#| continue next
#| }
#| }
#| }
#|}
#|fn[K, V] Map::grow(self : Map[K, V]) -> Unit {
#| let old_head = self.head
#| let new_capacity = self.capacity << 1
#| self.entries = FixedArray::make(new_capacity, None)
#| self.capacity = new_capacity
#| self.capacity_mask = new_capacity - 1
#| self.grow_at = calc_grow_threshold(self.capacity)
#| self.size = 0
#| self.head = None
#| self.tail = -1
#| for x = old_head {
#| match x {
#| None => break
#| Some(e) => {
#| let next_in_chain = e.next
#| e.next = None
#| self.rehash_place_entry(e)
#| continue next_in_chain
#| }
#| }
#| }
#|}
#|#owned(outer)
#|fn[K, V] Map::rehash_place_entry(self : Map[K, V], outer : Entry[K, V]) -> Unit {
#| let hash = outer.hash
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| outer.psl = psl
#| outer.prev = self.tail
#| self.add_entry_to_tail(idx, outer)
#| return
#| }
#| Some(curr) =>
#| if psl > curr.psl {
#| self.push_away(idx, curr)
#| outer.psl = psl
#| outer.prev = self.tail
#| self.add_entry_to_tail(idx, outer)
#| return
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|fn calc_grow_threshold(capacity : Int32) -> Int32 {
#| capacity / 16 * 13 + capacity % 16 * 13 / 16
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[K : Show, V : Show] Show for Map[K, V]
#|pub impl[K : Show, V : Show] Show for Map[K, V] with fn output(self, logger) {
#| logger <+ "{"
#| for x = 0, y = self.head {
#| match (x, y) {
#| (_, None) => break logger <+ "}"
#| (i, Some({ key, value, next, .. })) => {
#| if i > 0 {
#| logger <+ ", "
#| }
#| logger <+
#| "\{cb => cb.write_object(key)}: \{cb => cb.write_object(value)}"
#| continue i + 1, next
#| }
#| }
#| }
#|}
#|#alias(size, deprecated)
#|pub fn[K, V] Map::length(self : Map[K, V]) -> Int32 {
#| self.size
#|}
#|pub fn[K, V] Map::is_empty(self : Map[K, V]) -> Bool {
#| self.size == 0
#|}
#|#locals(f)
#|pub fn[K, V] Map::each(
#| self : Map[K, V],
#| f : (K, V) -> Unit raise?,
#|) -> Unit raise? {
#| for x = self.head {
#| match x {
#| Some({ key, value, next, .. }) => {
#| f(key, value)
#| continue next
#| }
#| None => break
#| }
#| }
#|}
#|#locals(f)
#|pub fn[K, V] Map::eachi(
#| self : Map[K, V],
#| f : (Int32, K, V) -> Unit raise?,
#|) -> Unit raise? {
#| for x = 0, y = self.head {
#| match (x, y) {
#| (i, Some({ key, value, next, .. })) => {
#| f(i, key, value)
#| continue i + 1, next
#| }
#| (_, None) => break
#| }
#| }
#|}
#|pub fn[K, V] Map::clear(self : Map[K, V]) -> Unit {
#| self.entries.fill(None)
#| self.size = 0
#| self.head = None
#| self.tail = -1
#|}
#|#alias(iterator, deprecated)
#|pub fn[K, V] Map::iter(self : Map[K, V]) -> Iter[(K, V)] {
#| let mut curr_entry = self.head
#| let len = self.size
#| let mut remaining = len
#| Iter(
#| fn() {
#| guard remaining > 0 && curr_entry is Some({ key, value, next, .. }) else {
#| None
#| }
#| curr_entry = next
#| remaining -= 1
#| Some((key, value))
#| },
#| size_hint=len,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[K, V] Map::iter2(self : Map[K, V]) -> Iter2[K, V] {
#| self.iter()
#|}
#|pub fn[K, V] Map::keys(self : Map[K, V]) -> Iter[K] {
#| let mut curr_entry = self.head
#| let len = self.size
#| let mut remaining = len
#| Iter(
#| fn() {
#| guard remaining > 0 && curr_entry is Some({ key, next, .. }) else { None }
#| curr_entry = next
#| remaining -= 1
#| Some(key)
#| },
#| size_hint=len,
#| )
#|}
#|pub fn[K, V] Map::values(self : Map[K, V]) -> Iter[V] {
#| let mut curr_entry = self.head
#| let len = self.size
#| let mut remaining = len
#| Iter(
#| fn() {
#| guard remaining > 0 && curr_entry is Some({ value, next, .. }) else {
#| None
#| }
#| curr_entry = next
#| remaining -= 1
#| Some(value)
#| },
#| size_hint=len,
#| )
#|}
#|pub fn[K, V] Map::to_array(self : Map[K, V]) -> Array[(K, V)] {
#| let arr = Array(capacity=self.size)
#| for x = self.head {
#| match x {
#| Some({ key, value, next, .. }) => {
#| arr.push((key, value))
#| continue next
#| }
#| None => break
#| }
#| }
#| arr
#|}
#|pub impl[K : Hash + Eq, V : Eq] Eq for Map[K, V] with fn equal(
#| self : Map[K, V],
#| that : Map[K, V],
#|) -> Bool {
#| guard self.size == that.size else { return false }
#| for k, v in self {
#| guard that.contains_kv(k, v) else { return false }
#| } nobreak {
#| true
#| }
#|}
#|#deprecated("Use `Map([(k, v), ...])` or `Map::from_array` instead")
#|pub fn[K : Hash + Eq, V] Map::of(arr : FixedArray[(K, V)]) -> Map[K, V] {
#| let length = arr.length()
#| let m = new_map(capacity_for_length(length))
#| for e in arr {
#| m.set(e.0, e.1)
#| }
#| m
#|}
#|#alias(from_iterator, deprecated)
#|pub fn[K : Hash + Eq, V] Map::from_iter(iter : Iter[(K, V)]) -> Map[K, V] {
#| let m = Map([])
#| while iter.next() is Some((k, v)) {
#| m.set(k, v)
#| }
#| m
#|}
#|pub impl[K, V] Default for Map[K, V] with fn default() {
#| new_map(default_init_capacity)
#|}
#|pub fn[K, V, V2] Map::map(
#| self : Map[K, V],
#| f : (K, V) -> V2 raise?,
#|) -> Map[K, V2] raise? {
#| let { size, capacity, capacity_mask, grow_at, .. } = self
#| let other = {
#| capacity,
#| entries: FixedArray::make(capacity, None),
#| size: 0,
#| capacity_mask,
#| grow_at,
#| head: None,
#| tail: -1,
#| }
#| let mut tail = -1
#| let mut written = 0
#| for x = self.head, remaining = size, previous = (None : Entry[K, V2]?) {
#| guard remaining > 0 && x is Some({ next, hash, key, value, .. }) else {
#| break
#| }
#| let idx = match next {
#| Some(next) => next.prev
#| None => self.tail
#| }
#| let new_value = f(key, value)
#| guard idx >= 0 && idx < capacity && other.entries.unsafe_get(idx) is None else {
#| continue next, remaining - 1, previous
#| }
#| let new_entry = {
#| prev: tail,
#| next: None,
#| psl: (idx - hash) & capacity_mask,
#| hash,
#| key,
#| value: new_value,
#| }
#| other.entries.unsafe_set(idx, Some(new_entry))
#| match previous {
#| Some(previous) => previous.next = Some(new_entry)
#| None => other.head = Some(new_entry)
#| }
#| tail = idx
#| written += 1
#| continue next, remaining - 1, Some(new_entry)
#| }
#| other.tail = tail
#| other.size = written
#| other
#|}
#|#alias(clone, deprecated)
#|pub fn[K, V] Map::copy(self : Map[K, V]) -> Map[K, V] {
#| let other = {
#| capacity: self.capacity,
#| entries: FixedArray::make(self.capacity, None),
#| size: self.size,
#| capacity_mask: self.capacity_mask,
#| grow_at: self.grow_at,
#| head: None,
#| tail: self.tail,
#| }
#| if self.size == 0 {
#| return other
#| }
#| guard! self.entries[self.tail] is Some(last)
#| for entry = last, idx = self.tail, next = (None : Entry[K, V]?) {
#| let { prev, psl, hash, key, value, .. } = entry
#| let new_entry = { prev, next, psl, hash, key, value, }
#| other.entries[idx] = Some(new_entry)
#| if prev != -1 {
#| continue self.entries[prev].unwrap(), prev, Some(new_entry)
#| } else {
#| other.head = Some(new_entry)
#| break
#| }
#| }
#| other
#|}
#|pub fn[K : Eq, V] Map::merge(self : Map[K, V], other : Map[K, V]) -> Map[K, V] {
#| let result = self.copy()
#| result.merge_in_place(other)
#| result
#|}
#|pub fn[K : Eq, V] Map::merge_in_place(
#| self : Map[K, V],
#| other : Map[K, V],
#|) -> Unit {
#| if physical_equal(self, other) {
#| return
#| }
#| for x = other.head {
#| match x {
#| Some({ key, value, next, hash, .. }) => {
#| self.set_with_hash(key, value, hash)
#| continue next
#| }
#| None => break
#| }
#| }
#|}
#|#locals(f)
#|pub fn[K, V] Map::retain(self : Map[K, V], f : (K, V) -> Bool) -> Unit {
#| for x = self.head, y = false {
#| match (x, y) {
#| (Some({ key, value, next, prev: idx, .. }), remove_prev) => {
#| if remove_prev {
#| guard! self.entries[idx] is Some(entry)
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| }
#| continue next, !f(key, value)
#| }
#| (None, remove_prev) => {
#| if remove_prev {
#| let idx = self.tail
#| guard! self.entries[idx] is Some(entry)
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| }
#| break
#| }
#| }
#| }
#|}
#|pub fn[K, V] Map::filter(
#| self : Map[K, V],
#| f : (K, V) -> Bool raise?,
#|) -> Map[K, V] raise? {
#| let kept : Array[Entry[K, V]] = []
#| for x = self.head {
#| match x {
#| Some({ key, value, next, .. } as entry) => {
#| if f(key, value) {
#| kept.push(entry)
#| }
#| continue next
#| }
#| None => break
#| }
#| }
#| let other = new_map(
#| if kept.is_empty() {
#| default_init_capacity
#| } else {
#| capacity_for_length(kept.length())
#| },
#| )
#| for entry in kept {
#| other.add_unique_with_hash(entry.key, entry.value, entry.hash)
#| }
#| other
#|}
#|#owned(value)
#|fn[K, V] Map::add_unique_with_hash(
#| self : Map[K, V],
#| key : K,
#| value : V,
#| hash : Int32,
#|) -> Unit {
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| let entry = { prev: self.tail, next: None, psl, key, value, hash, }
#| self.add_entry_to_tail(idx, entry)
#| return
#| }
#| Some(curr_entry) => {
#| if psl > curr_entry.psl {
#| self.push_away(idx, curr_entry)
#| let entry = { prev: self.tail, next: None, psl, key, value, hash, }
#| self.add_entry_to_tail(idx, entry)
#| return
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|pub fn[K : Hash + Eq, V] Map::update(
#| self : Map[K, V],
#| key : K,
#| f : (V?) -> V? raise?,
#|) -> Unit raise? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| let result = f(Some(entry.value))
#| let idx = idx & self.capacity_mask
#| if self.entries.unsafe_get(idx) is Some(current) &&
#| physical_equal(current, entry) {
#| if result is Some(new_value) {
#| entry.value = new_value
#| } else {
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| }
#| } else if result is Some(new_value) {
#| self.set_with_hash(key, new_value, hash)
#| } else {
#| self.remove_with_hash(key, hash)
#| }
#| return
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#| guard f(None) is Some(new_value) else { return }
#| self.set_with_hash(key, new_value, hash)
#|}
#|pub fn[V] Map::get_from_bytes(map : Self[Bytes, V], key : BytesView) -> V? {
#| let hash = key.hash()
#| for i = 0, idx = hash & map.capacity_mask {
#| guard map.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && key.equal_to_bytes(entry.key) {
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & map.capacity_mask
#| }
#|}
#|pub fn[V] Map::get_from_string(map : Self[String, V], key : StringView) -> V? {
#| let hash = key.hash()
#| for i = 0, idx = hash & map.capacity_mask {
#| guard map.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && key.equal_to_string(entry.key) {
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & map.capacity_mask
#| }
#|}
),
"mutarrayview.mbt": (
#|#builtin.valtype
#|type MutArrayView[T]
#|fn[T] MutArrayView::buf(self : MutArrayView[T]) -> UninitializedArray[T] = "%arrayview.buf"
#|fn[T] MutArrayView::start(self : MutArrayView[T]) -> Int32 = "%arrayview.start"
#|fn[T] MutArrayView::end(self : MutArrayView[T]) -> Int32 = "%arrayview.end"
#|fn[T] MutArrayView::make_view(
#| buf : UninitializedArray[T],
#| start : Int32,
#| end : Int32,
#|) -> MutArrayView[T] = "%arrayview.make_view"
#|#intrinsic("%mutarrayview.length")
#|pub fn[T] MutArrayView::length(self : MutArrayView[T]) -> Int32 {
#| self.end() - self.start()
#|}
#|pub fn[T] MutArrayView::is_empty(self : MutArrayView[T]) -> Bool {
#| self.length() == 0
#|}
#|pub fn[T] MutArrayView::start_offset(self : MutArrayView[T]) -> Int32 {
#| self.start()
#|}
#|#intrinsic("%mutarrayview.get")
#|#alias("_[_]")
#|pub fn[T] MutArrayView::at(self : MutArrayView[T], index : Int32) -> T {
#| guard index >= 0 && index < self.length() else {
#| index_out_of_bounds(self.length(), index)
#| }
#| self.buf().unsafe_get(self.start() + index)
#|}
#|#intrinsic("%mutarrayview.unsafe_get")
#|#internal(unsafe, "Panic if index is out of bounds")
#|pub fn[T] MutArrayView::unsafe_get(self : MutArrayView[T], index : Int32) -> T {
#| self.buf()[self.start() + index]
#|}
#|#intrinsic("%mutarrayview.set")
#|#alias("_[_]=_")
#|#owned(value)
#|pub fn[T] MutArrayView::set(
#| self : MutArrayView[T],
#| index : Int32,
#| value : T,
#|) -> Unit {
#| guard index >= 0 && index < self.length() else {
#| index_out_of_bounds(self.length(), index)
#| }
#| self.buf().unsafe_set(self.start() + index, value)
#|}
#|#intrinsic("%mutarrayview.unsafe_set")
#|#internal(unsafe, "Panic if index is out of bounds")
#|#owned(value)
#|pub fn[T] MutArrayView::unsafe_set(
#| self : MutArrayView[T],
#| index : Int32,
#| value : T,
#|) -> Unit {
#| self.buf()[self.start() + index] = value
#|}
#|#intrinsic("%array.mut_view")
#|pub fn[T] Array::mut_view(
#| self : Array[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> MutArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| MutArrayView::make_view(self.buffer(), start, end)
#|}
#|#intrinsic("%mutarrayview.mut_view")
#|pub fn[T] MutArrayView::mut_view(
#| self : MutArrayView[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> MutArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| MutArrayView::make_view(self.buf(), self.start() + start, self.start() + end)
#|}
#|#intrinsic("%fixedarray.mut_view")
#|pub fn[T] FixedArray::mut_view(
#| self : FixedArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> MutArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| MutArrayView::make_view(
#| unsafe_cast_fixedarray_to_uninitializedarray(self),
#| start,
#| end,
#| )
#|}
#|#intrinsic("%mutarrayview.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn[T] MutArrayView::exact_view(
#| self : MutArrayView[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| self.buf().unsafe_view(start=self.start() + start, end=self.start() + end)
#|}
#|#intrinsic("%mutarrayview.clamped_view")
#|#alias("_[_:_]")
#|pub fn[T] MutArrayView::clamped_view(
#| self : MutArrayView[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| self.buf().unsafe_view(start=self.start() + lo, end=self.start() + hi)
#|}
#|#alias(to_array, deprecated)
#|pub fn[T] MutArrayView::to_owned(self : MutArrayView[T]) -> Array[T] {
#| self[:].to_owned()
#|}
#|#alias(iterator, deprecated)
#|pub fn[X] MutArrayView::iter(self : MutArrayView[X]) -> Iter[X] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let elem = self.unsafe_get(i)
#| i += 1
#| Some(elem)
#| },
#| size_hint=len,
#| )
#|}
#|#alias(rev_iterator, deprecated)
#|pub fn[X] MutArrayView::rev_iter(self : MutArrayView[X]) -> Iter[X] {
#| let len = self.length()
#| let mut i = len
#| Iter(
#| fn() {
#| guard i > 0 else { None }
#| i -= 1
#| Some(self.unsafe_get(i))
#| },
#| size_hint=len,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[X] MutArrayView::iter2(self : MutArrayView[X]) -> Iter2[Int32, X] {
#| let mut i = 0
#| let len = self.length()
#| Iter(
#| fn() {
#| guard i < len else { None }
#| let result = Some((i, self.unsafe_get(i)))
#| i += 1
#| result
#| },
#| size_hint=len,
#| )
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[X : Show] Show for MutArrayView[X]
#|#warnings("-deprecated")
#|pub impl[X : Show] Show for MutArrayView[X] with fn output(self, logger) {
#| Show::output(self[:], logger)
#|}
#|pub impl[T : Eq] Eq for MutArrayView[T] with fn equal(self, other) -> Bool {
#| self[:] == other
#|}
#|pub impl[T : Compare] Compare for MutArrayView[T] with fn compare(self, other) -> Int32 {
#| self[:].compare(other)
#|}
#|pub impl[A : Hash] Hash for MutArrayView[A] with fn hash_combine(self, hasher) {
#| self[:].hash_combine(hasher)
#|}
#|#inline
#|pub fn[T] MutArrayView::blit_from_array(
#| self : MutArrayView[T],
#| source : ArrayView[T],
#| dst_offset~ : Int32 = 0,
#|) -> Unit {
#| guard! dst_offset >= 0 &&
#| dst_offset <= self.length() &&
#| source.length() <= self.length() - dst_offset
#| UninitializedArray::unsafe_blit(
#| self.buf(),
#| self.start() + dst_offset,
#| source.buf(),
#| source.start(),
#| source.length(),
#| )
#|}
),
"op.mbt": "",
"operators.mbt": (
#|pub(open) trait Add {
#| fn add(Self, Self) -> Self
#|}
#|pub(open) trait Sub {
#| fn sub(Self, Self) -> Self
#|}
#|pub(open) trait Mul {
#| fn mul(Self, Self) -> Self
#|}
#|pub(open) trait Div {
#| fn div(Self, Self) -> Self
#|}
#|pub(open) trait Neg {
#| fn neg(Self) -> Self
#|}
#|pub(open) trait Mod {
#| fn mod(Self, Self) -> Self
#|}
#|pub(open) trait BitAnd {
#| fn land(Self, Self) -> Self
#|}
#|pub(open) trait BitOr {
#| fn lor(Self, Self) -> Self
#|}
#|pub(open) trait BitXOr {
#| fn lxor(Self, Self) -> Self
#|}
#|pub(open) trait Shl {
#| fn shl(Self, Int32) -> Self
#|}
#|pub(open) trait Shr {
#| fn shr(Self, Int32) -> Self
#|}
),
"option.mbt": (
#|pub impl[X : Eq] Eq for X? with fn equal(self, other) {
#| match (self, other) {
#| (None, None) => true
#| (Some(x), Some(y)) => x == y
#| _ => false
#| }
#|}
#|#deprecated("Option does not have a meaningful string representation")
#|pub fn[X : Show] Option::to_string(self : X?) -> String {
#| match self {
#| None => "None"
#| Some(x) => "Some(\{x})"
#| }
#|}
#|pub fn[X] Option::unwrap(self : X?) -> X {
#| match self {
#| None => panic()
#| Some(x) => x
#| }
#|}
#|#alias(or, deprecated)
#|pub fn[T] Option::unwrap_or(self : T?, default : T) -> T {
#| match self {
#| Some(t) | (None with t = default) => t
#| }
#|}
#|#alias(or_else, deprecated)
#|pub fn[T] Option::unwrap_or_else(
#| self : T?,
#| default : () -> T raise?,
#|) -> T raise? {
#| match self {
#| None => default()
#| Some(t) => t
#| }
#|}
#|#alias(or_default, deprecated)
#|pub fn[T : Default] Option::unwrap_or_default(self : T?) -> T {
#| match self {
#| Some(t) | (None with t = T::default()) => t
#| }
#|}
#|pub impl[X : Compare] Compare for X? with fn compare(self, other) {
#| match (self, other) {
#| (Some(x), Some(y)) => x.compare(y)
#| (Some(_), None) => 1
#| (None, Some(_)) => -1
#| (None, None) => 0
#| }
#|}
#|#alias(or_error, deprecated)
#|pub fn[T, Err : Error] Option::unwrap_or_error(
#| self : T?,
#| err : Err,
#|) -> T raise Err {
#| guard self is Some(v) else { raise err }
#| v
#|}
#|pub impl[X] Default for X? with fn default() {
#| None
#|}
#|#alias(iterator, deprecated)
#|pub fn[T] Option::iter(self : T?) -> Iter[T] {
#| match self {
#| Some(v) => [|v|]
#| None => [||]
#| }
#|}
#|pub fn[T, U] Option::map(self : T?, f : (T) -> U raise?) -> U? raise? {
#| match self {
#| Some(t) => Some(f(t))
#| None => None
#| }
#|}
#|pub fn[T, U] Option::map_or(
#| self : T?,
#| default : U,
#| f : (T) -> U raise?,
#|) -> U raise? {
#| match self {
#| None => default
#| Some(x) => f(x)
#| }
#|}
#|pub fn[T, U] Option::map_or_else(
#| self : T?,
#| default : () -> U raise?,
#| f : (T) -> U raise?,
#|) -> U raise? {
#| match self {
#| None => default()
#| Some(x) => f(x)
#| }
#|}
#|pub fn[T, U] Option::bind(self : T?, f : (T) -> U? raise?) -> U? raise? {
#| match self {
#| Some(t) => f(t)
#| None => None
#| }
#|}
#|#deprecated("use `option.bind(x => x)` instead")
#|pub fn[T] Option::flatten(self : T??) -> T? {
#| match self {
#| Some(inner) => inner
#| None => None
#| }
#|}
#|#deprecated("use `x is None` instead")
#|pub fn[T] Option::is_empty(self : T?) -> Bool {
#| self is None
#|}
#|pub fn[T] Option::filter(self : T?, f : (T) -> Bool raise?) -> T? raise? {
#| match self {
#| Some(t) => if f(t) { self } else { None }
#| None => None
#| }
#|}
#|#deprecated("use `x is Some(_)` instead")
#|pub fn[T] Option::is_some(self : T?) -> Bool {
#| self is Some(_)
#|}
#|#deprecated("use `x is None` instead")
#|pub fn[T] Option::is_none(self : T?) -> Bool {
#| self is None
#|}
),
"range.mbt": (
#|fn[T : Add + Compare + Default] until_impl(
#| start : T,
#| end : T,
#| step : T,
#| inclusive : Bool,
#|) -> Iter[T] {
#| let zero = Default::default()
#| if step == zero {
#| return [||]
#| }
#| let mut i = start
#| let mut done = false
#| Iter(() => {
#| guard !done else { None }
#| guard (step > zero && i < end) ||
#| (step < zero && i > end) ||
#| (inclusive && i == end) else {
#| None
#| }
#| let value = i
#| let next = i + step
#| if (step > zero && next >= i) || (step < zero && next <= i) {
#| i = next
#| } else {
#| done = true
#| }
#| Some(value)
#| })
#|}
),
"readonlyarray.mbt": (
#|fn[T] ReadOnlyArray::unsafe_reinterpret_to_fixed_array(
#| self : ReadOnlyArray[T],
#|) -> FixedArray[T] = "%identity"
#|fn[T] unsafe_reinterpret_from_fixed_array(
#| arr : FixedArray[T],
#|) -> ReadOnlyArray[T] = "%identity"
#|#alias("_[_]")
#|pub fn[T] ReadOnlyArray::at(self : ReadOnlyArray[T], index : Int32) -> T {
#| self.unsafe_reinterpret_to_fixed_array()[index]
#|}
#|#owned(array)
#|pub fn[T] ReadOnlyArray::from_array(array : ArrayView[T]) -> ReadOnlyArray[T] {
#| unsafe_reinterpret_from_fixed_array(FixedArray::from_array(array))
#|}
#|#alias(from_iterator, deprecated)
#|pub fn[T] ReadOnlyArray::from_iter(iter : Iter[T]) -> ReadOnlyArray[T] {
#| unsafe_reinterpret_from_fixed_array(FixedArray::from_iter(iter))
#|}
#|pub fn[T] ReadOnlyArray::makei(
#| length : Int32,
#| value : (Int32) -> T raise?,
#|) -> ReadOnlyArray[T] raise? {
#| unsafe_reinterpret_from_fixed_array(FixedArray::makei(length, value))
#|}
#|#intrinsic("%readonlyarray.get_opt")
#|pub fn[T] ReadOnlyArray::get(self : ReadOnlyArray[T], index : Int32) -> T? {
#| self.unsafe_reinterpret_to_fixed_array().get(index)
#|}
#|#internal(unsafe, "Undefined behavior if index is out of bounds")
#|#doc(hidden)
#|pub fn[T] ReadOnlyArray::unsafe_get(
#| self : ReadOnlyArray[T],
#| index : Int32,
#|) -> T {
#| self.unsafe_reinterpret_to_fixed_array().unsafe_get(index)
#|}
#|pub fn[T] ReadOnlyArray::length(self : ReadOnlyArray[T]) -> Int32 {
#| self.unsafe_reinterpret_to_fixed_array().length()
#|}
#|pub fn[T] ReadOnlyArray::is_empty(self : ReadOnlyArray[T]) -> Bool {
#| self.unsafe_reinterpret_to_fixed_array().is_empty()
#|}
#|pub fn[T] ReadOnlyArray::last(self : ReadOnlyArray[T]) -> T? {
#| self.unsafe_reinterpret_to_fixed_array().last()
#|}
#|#alias(iterator, deprecated)
#|pub fn[T] ReadOnlyArray::iter(self : ReadOnlyArray[T]) -> Iter[T] {
#| self.unsafe_reinterpret_to_fixed_array().iter()
#|}
#|pub fn[T] ReadOnlyArray::iter2(self : ReadOnlyArray[T]) -> Iter2[Int32, T] {
#| self.unsafe_reinterpret_to_fixed_array().iter2()
#|}
#|pub fn[T] ReadOnlyArray::rev_iter(self : ReadOnlyArray[T]) -> Iter[T] {
#| self[:].rev_iter()
#|}
#|pub fn[T] ReadOnlyArray::each(
#| self : ReadOnlyArray[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| self.unsafe_reinterpret_to_fixed_array().each(f)
#|}
#|pub fn[T] ReadOnlyArray::eachi(
#| self : ReadOnlyArray[T],
#| f : (Int32, T) -> Unit raise?,
#|) -> Unit raise? {
#| self.unsafe_reinterpret_to_fixed_array().eachi(f)
#|}
#|pub fn[T] ReadOnlyArray::rev_each(
#| self : ReadOnlyArray[T],
#| f : (T) -> Unit raise?,
#|) -> Unit raise? {
#| self.unsafe_reinterpret_to_fixed_array().rev_each(f)
#|}
#|pub fn[T] ReadOnlyArray::rev_eachi(
#| self : ReadOnlyArray[T],
#| f : (Int32, T) -> Unit raise?,
#|) -> Unit raise? {
#| self.unsafe_reinterpret_to_fixed_array().rev_eachi(f)
#|}
#|pub fn[T, U] ReadOnlyArray::map(
#| self : ReadOnlyArray[T],
#| f : (T) -> U raise?,
#|) -> ReadOnlyArray[U] raise? {
#| unsafe_reinterpret_from_fixed_array(
#| self.unsafe_reinterpret_to_fixed_array().map(f),
#| )
#|}
#|pub fn[T, U] ReadOnlyArray::mapi(
#| self : ReadOnlyArray[T],
#| f : (Int32, T) -> U raise?,
#|) -> ReadOnlyArray[U] raise? {
#| unsafe_reinterpret_from_fixed_array(
#| self.unsafe_reinterpret_to_fixed_array().mapi(f),
#| )
#|}
#|pub fn[T] ReadOnlyArray::filter(
#| self : ReadOnlyArray[T],
#| f : (T) -> Bool raise?,
#|) -> ReadOnlyArray[T] raise? {
#| ReadOnlyArray::from_array(self[:].filter(f))
#|}
#|pub fn[A, B] ReadOnlyArray::filter_map(
#| self : ReadOnlyArray[A],
#| f : (A) -> B? raise?,
#|) -> ReadOnlyArray[B] raise? {
#| ReadOnlyArray::from_array(self[:].filter_map(f))
#|}
#|pub fn[A, B] ReadOnlyArray::fold(
#| self : ReadOnlyArray[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| self.unsafe_reinterpret_to_fixed_array().fold(init~, f)
#|}
#|pub fn[A, B] ReadOnlyArray::rev_fold(
#| self : ReadOnlyArray[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| self.unsafe_reinterpret_to_fixed_array().rev_fold(init~, f)
#|}
#|pub fn[A, B] ReadOnlyArray::foldi(
#| self : ReadOnlyArray[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| self.unsafe_reinterpret_to_fixed_array().foldi(init~, f)
#|}
#|pub fn[A, B] ReadOnlyArray::rev_foldi(
#| self : ReadOnlyArray[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| self.unsafe_reinterpret_to_fixed_array().rev_foldi(init~, f)
#|}
#|pub fn[T] ReadOnlyArray::rev(self : ReadOnlyArray[T]) -> ReadOnlyArray[T] {
#| unsafe_reinterpret_from_fixed_array(
#| self.unsafe_reinterpret_to_fixed_array().rev(),
#| )
#|}
#|pub fn[T : Eq] ReadOnlyArray::search(
#| self : ReadOnlyArray[T],
#| value : T,
#|) -> Int32? {
#| self.unsafe_reinterpret_to_fixed_array().search(value)
#|}
#|pub fn[T] ReadOnlyArray::search_by(
#| self : ReadOnlyArray[T],
#| f : (T) -> Bool raise?,
#|) -> Int32? raise? {
#| self[:].search_by(f)
#|}
#|pub fn[T : Eq] ReadOnlyArray::contains(
#| self : ReadOnlyArray[T],
#| value : T,
#|) -> Bool {
#| self.unsafe_reinterpret_to_fixed_array().contains(value)
#|}
#|pub fn[T : Compare] ReadOnlyArray::is_sorted(self : ReadOnlyArray[T]) -> Bool {
#| self.unsafe_reinterpret_to_fixed_array().is_sorted()
#|}
#|#alias(starts_with, deprecated="Use `has_prefix` instead")
#|pub fn[T : Eq] ReadOnlyArray::has_prefix(
#| self : ReadOnlyArray[T],
#| prefix : ArrayView[T],
#|) -> Bool {
#| self[:].has_prefix(prefix)
#|}
#|#alias(ends_with, deprecated="Use `has_suffix` instead")
#|pub fn[T : Eq] ReadOnlyArray::has_suffix(
#| self : ReadOnlyArray[T],
#| suffix : ArrayView[T],
#|) -> Bool {
#| self[:].has_suffix(suffix)
#|}
#|pub fn[T : Eq] ReadOnlyArray::strip_prefix(
#| self : ReadOnlyArray[T],
#| prefix : ArrayView[T],
#|) -> ArrayView[T]? {
#| self[:].strip_prefix(prefix)
#|}
#|pub fn[T : Eq] ReadOnlyArray::strip_suffix(
#| self : ReadOnlyArray[T],
#| suffix : ArrayView[T],
#|) -> ArrayView[T]? {
#| self[:].strip_suffix(suffix)
#|}
#|#alias(every)
#|pub fn[T] ReadOnlyArray::all(
#| self : ReadOnlyArray[T],
#| f : (T) -> Bool raise?,
#|) -> Bool raise? {
#| self.unsafe_reinterpret_to_fixed_array().all(f)
#|}
#|#alias(exists)
#|pub fn[T] ReadOnlyArray::any(
#| self : ReadOnlyArray[T],
#| f : (T) -> Bool raise?,
#|) -> Bool raise? {
#| self.unsafe_reinterpret_to_fixed_array().any(f)
#|}
#|pub fn[T : Compare] ReadOnlyArray::binary_search(
#| self : ReadOnlyArray[T],
#| value : T,
#|) -> Result[Int32, Int32] {
#| self.unsafe_reinterpret_to_fixed_array().binary_search(value)
#|}
#|pub fn[T] ReadOnlyArray::binary_search_by(
#| self : ReadOnlyArray[T],
#| cmp : (T) -> Int32 raise?,
#|) -> Result[Int32, Int32] raise? {
#| self.unsafe_reinterpret_to_fixed_array().binary_search_by(cmp)
#|}
#|pub fn[T] ReadOnlyArray::chunks(
#| self : ReadOnlyArray[T],
#| size : Int32,
#|) -> Array[ArrayView[T]] {
#| self[:].chunks(size)
#|}
#|pub fn[T] ReadOnlyArray::windows(
#| self : ReadOnlyArray[T],
#| size : Int32,
#|) -> Array[ArrayView[T]] {
#| self[:].windows(size)
#|}
#|pub fn[T] ReadOnlyArray::chunk_by(
#| self : ReadOnlyArray[T],
#| pred : (T, T) -> Bool raise?,
#|) -> Array[ArrayView[T]] raise? {
#| self[:].chunk_by(pred)
#|}
#|pub fn[T] ReadOnlyArray::suffixes(
#| self : ReadOnlyArray[T],
#| include_empty~ : Bool = false,
#|) -> Iter[ArrayView[T]] {
#| self[:].suffixes(include_empty~)
#|}
#|pub fn[T : Compare] ReadOnlyArray::lexical_compare(
#| self : ReadOnlyArray[T],
#| other : ReadOnlyArray[T],
#|) -> Int32 {
#| self[:].lexical_compare(other)
#|}
#|#intrinsic("%readonlyarray.view")
#|#alias(view, deprecated="Use `exact_view` instead")
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn[T] ReadOnlyArray::exact_view(
#| self : ReadOnlyArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let fixed = self.unsafe_reinterpret_to_fixed_array()
#| match end {
#| None => fixed.exact_view(start~)
#| Some(end) => fixed.exact_view(start~, end~)
#| }
#|}
#|#intrinsic("%readonlyarray.view_opt")
#|pub fn[T] ReadOnlyArray::get_view(
#| self : ReadOnlyArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T]? {
#| let fixed = self.unsafe_reinterpret_to_fixed_array()
#| match end {
#| None => fixed.get_view(start~)
#| Some(end) => fixed.get_view(start~, end~)
#| }
#|}
#|#intrinsic("%readonlyarray.clamped_view")
#|#alias("_[_:_]")
#|pub fn[T] ReadOnlyArray::clamped_view(
#| self : ReadOnlyArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let fixed = self.unsafe_reinterpret_to_fixed_array()
#| match end {
#| None => fixed.clamped_view(start~)
#| Some(end) => fixed.clamped_view(start~, end~)
#| }
#|}
#|pub fn[A : ToStringView] ReadOnlyArray::join(
#| self : ReadOnlyArray[A],
#| separator : StringView,
#|) -> String {
#| self[:].join(separator)
#|}
#|pub impl[T] Default for ReadOnlyArray[T] with fn default() {
#| unsafe_reinterpret_from_fixed_array(([] : FixedArray[_]))
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[T : Show] Show for ReadOnlyArray[T]
#|#warnings("-deprecated")
#|pub impl[T : Show] Show for ReadOnlyArray[T] with fn output(self, logger) {
#| Show::output(self.unsafe_reinterpret_to_fixed_array(), logger)
#|}
#|pub impl[T : Eq] Eq for ReadOnlyArray[T] with fn equal(self, other) {
#| self
#| .unsafe_reinterpret_to_fixed_array()
#| .equal(other.unsafe_reinterpret_to_fixed_array())
#|}
#|pub impl[T : Hash] Hash for ReadOnlyArray[T] with fn hash_combine(self, hasher) {
#| self.unsafe_reinterpret_to_fixed_array().hash_combine(hasher)
#|}
#|pub impl[T : Compare] Compare for ReadOnlyArray[T] with fn compare(self, other) {
#| self
#| .unsafe_reinterpret_to_fixed_array()
#| .compare(other.unsafe_reinterpret_to_fixed_array())
#|}
),
"result.mbt": (
#|pub fn[T, E, U] Result::map(
#| self : Result[T, E],
#| f : (T) -> U raise?,
#|) -> Result[U, E] raise? {
#| match self {
#| Ok(value) => Ok(f(value))
#| Err(err) => Err(err)
#| }
#|}
#|test "map" {
#| let x : Result[Int32, Unit] = Ok(6)
#| let y = x.map((v : Int32) => v * 7)
#| let z : Result[Int32, Int32] = Err(3)
#| let w = z.map((v : Int32) => v * 7)
#| assert_true(y == Ok(42))
#| assert_true(w == Err(3))
#|}
#|pub fn[T, E, F] Result::map_err(
#| self : Result[T, E],
#| f : (E) -> F raise?,
#|) -> Result[T, F] raise? {
#| match self {
#| Ok(value) => Ok(value)
#| Err(err) => Err(f(err))
#| }
#|}
#|test "map_err" {
#| let x : Result[Int32, String] = Err("error")
#| let y = x.map_err((v : String) => v + "!")
#| let z : Result[Int32, Int32] = Ok(6)
#| let w = z.map_err((v : Int32) => v + 6)
#| assert_true(y == Err("error!"))
#| assert_true(w == Ok(6))
#|}
#|#alias(or, deprecated)
#|pub fn[T, E] Result::unwrap_or(self : Result[T, E], default : T) -> T {
#| match self {
#| Ok(value) | (Err(_) with value = default) => value
#| }
#|}
#|test "unwrap_or" {
#| let x : Result[Int32, String] = Ok(3)
#| let y : Result[Int32, String] = Err("error")
#| assert_true(x.unwrap_or(5) == 3)
#| assert_true(y.unwrap_or(5) == 5)
#|}
#|#alias(or_else, deprecated)
#|pub fn[T, E] Result::unwrap_or_else(
#| self : Result[T, E],
#| default : () -> T raise?,
#|) -> T raise? {
#| match self {
#| Ok(value) => value
#| Err(_) => default()
#| }
#|}
#|test "unwrap_or_else" {
#| let x : Result[Int32, String] = Ok(3)
#| let y : Result[Int32, String] = Err("error")
#| assert_true(x.unwrap_or_else(() => 5) == 3)
#| assert_true(y.unwrap_or_else(() => 5) == 5)
#|}
#|pub fn[T, E] Result::flatten(self : Result[Result[T, E], E]) -> Result[T, E] {
#| match self {
#| Ok(value) => value
#| Err(err) => Err(err)
#| }
#|}
#|test "flatten" {
#| let x : Result[Result[Int32, String], String] = Ok(Ok(6))
#| let y = x.flatten()
#| let z : Result[Result[Int32, String], String] = Err("error")
#| let w = z.flatten()
#| assert_true(y == Ok(6))
#| assert_true(w == Err("error"))
#|}
#|pub fn[T, E, U] Result::bind(
#| self : Result[T, E],
#| g : (T) -> Result[U, E] raise?,
#|) -> Result[U, E] raise? {
#| match self {
#| Ok(value) => g(value)
#| Err(err) => Err(err)
#| }
#|}
#|test "bind" {
#| let x : Result[Int32, String] = Ok(6)
#| let y = x.bind((v : Int32) => Ok(v * 7))
#| assert_true(y == Ok(42))
#|}
#|pub fn[T, E] Result::to_option(self : Result[T, E]) -> T? {
#| match self {
#| Ok(value) => Some(value)
#| Err(_) => None
#| }
#|}
#|test "to_option" {
#| let x : Result[Int32, String] = Ok(6)
#| let y : Result[Int32, String] = Err("error")
#| let z = x.to_option()
#| let w = y.to_option()
#| assert_true(z == Some(6))
#| assert_true(w == None)
#|}
#|pub impl[T : Compare, E : Compare] Compare for Result[T, E] with fn compare(
#| self : Result[T, E],
#| other : Result[T, E],
#|) -> Int32 {
#| match (self, other) {
#| (Ok(x), Ok(y)) => x.compare(y)
#| (Ok(_), Err(_)) => -1
#| (Err(_), Ok(_)) => 1
#| (Err(x), Err(y)) => x.compare(y)
#| }
#|}
#|test "compare" {
#| let ok1 = Result::Ok(1)
#| let ok2 = Result::Ok(2)
#| let err1 = Result::Err(1)
#| let err2 = Result::Err(2)
#| assert_true(0 == ok1.compare(ok1))
#| assert_true(0 == err2.compare(Err(2)))
#| assert_true(-1 == ok1.compare(ok2))
#| assert_true(1 == ok2.compare(ok1))
#| assert_true(-1 == err1.compare(err2))
#| assert_true(1 == err2.compare(err1))
#| assert_true(-1 == ok2.compare(err1))
#| assert_true(1 == err1.compare(ok2))
#|}
#|pub fn[T, E] Result::unwrap(self : Result[T, E]) -> T {
#| match self {
#| Ok(x) => x
#| Err(_) => abort("called `Result::unwrap()` on an `Err` value")
#| }
#|}
#|pub fn[T, E] Result::unwrap_err(self : Result[T, E]) -> E {
#| match self {
#| Ok(_) => abort("called `Result::unwrap_err()` on an `Ok` value")
#| Err(e) => e
#| }
#|}
#|#warnings("-deprecated")
#|test "show" {
#| let ok : Result[_, String] = Ok("hello")
#| inspect(
#| ok,
#| content=(
#| #|Ok(hello)
#| ),
#| )
#| let err : Result[String, _] = Err("world")
#| inspect(
#| err,
#| content=(
#| #|Err(world)
#| ),
#| )
#|}
#|pub fn[T : Default, E] Result::unwrap_or_default(self : Result[T, E]) -> T {
#| match self {
#| Ok(value) | (Err(_) with value = T::default()) => value
#| }
#|}
#|test "unwrap_or_default" {
#| let x : Result[Int32, String] = Ok(3)
#| let y : Result[Int32, String] = Err("error")
#| assert_true(x.unwrap_or_default() == 3)
#| assert_true(y.unwrap_or_default() == 0)
#|}
#|pub fn[T, E : Error] Result::unwrap_or_error(self : Result[T, E]) -> T raise E {
#| match self {
#| Ok(x) => x
#| Err(e) => raise e
#| }
#|}
#|test "unwrap exn" {
#| let result = try
#| (Err(Failure("This is serious")) : Result[Unit, Failure]).unwrap_or_error()
#| |> Ok
#| catch {
#| Failure(msg) => Err(msg)
#| }
#| assert_true(result == Err("This is serious"))
#|}
#|pub impl[T : Eq, E : Eq] Eq for Result[T, E] with fn equal(self, other) {
#| match (self, other) {
#| (Ok(x), Ok(y)) => x == y
#| (Err(x), Err(y)) => x == y
#| _ => false
#| }
#|}
#|test "map/map_err/bind propagate callback errors" {
#| let ok : Result[Int32, String] = Ok(1)
#| let err : Result[Int32, String] = Err("e")
#| let r1 : Result[Result[Int32, String], Error] = Ok(
#| ok.map(x => if x == 1 { fail("map") } else { x }),
#| ) catch {
#| e => Err(e)
#| }
#| assert_true(r1 is Err(Failure(msg)) && msg.contains("map"))
#| let r2 : Result[Result[Int32, String], Error] = Ok(
#| err.map_err(_ => fail("map_err")),
#| ) catch {
#| e => Err(e)
#| }
#| assert_true(r2 is Err(Failure(msg)) && msg.contains("map_err"))
#| let r3 : Result[Result[Int32, String], Error] = Ok(ok.bind(_ => fail("bind"))) catch {
#| e => Err(e)
#| }
#| assert_true(r3 is Err(Failure(msg)) && msg.contains("bind"))
#| assert_true(err.map(_ => (fail("unreachable") : Int32)) == Err("e"))
#| assert_true(ok.map_err(_ => (fail("unreachable") : String)) == Ok(1))
#| assert_true(
#| err.bind(_ => (fail("unreachable") : Result[Int32, String])) == Err("e"),
#| )
#|}
),
"show.mbt": (
#|pub impl Show for Unit with fn output(_self, logger) {
#| logger <+ "()"
#|}
#|pub impl Show for Unit with fn to_string(_self) {
#| "()"
#|}
#|pub impl Show for Bool with fn to_string(self) {
#| if self {
#| "true"
#| } else {
#| "false"
#| }
#|}
#|pub impl Show for Int32 with fn to_string(self) {
#| Int32::to_string(self)
#|}
#|pub impl Show for Int64 with fn to_string(self) {
#| Int64::to_string(self)
#|}
#|pub impl Show for UInt32 with fn to_string(self) {
#| UInt32::to_string(self)
#|}
#|pub impl Show for UInt64 with fn to_string(self) {
#| UInt64::to_string(self)
#|}
#|pub impl Show for Byte with fn to_string(self) {
#| Byte::to_string(self)
#|}
#|pub impl Show for UInt16 with fn to_string(self) {
#| UInt16::to_string(self)
#|}
#|pub fn Byte::to_hex(b : Byte) -> String {
#| fn to_hex_digit(i : Byte) -> Char {
#| if i < 10 {
#| (i + b'0').to_char()
#| } else {
#| (i + b'a' - 10).to_char()
#| }
#| }
#| [to_hex_digit(b / 16), to_hex_digit(b % 16)]
#|}
#|test "to_hex_digit" {
#| inspect(Byte::to_hex(b'\xee'), content="ee")
#| inspect(Byte::to_hex(b'\xf3'), content="f3")
#|}
#|pub fn String::escape(self : String, quote~ : Bool = true) -> String {
#| let buf = StringBuilder()
#| self[:].escape_to(buf, quote~)
#| buf.to_string()
#|}
#|pub fn StringView::escape(
#| self : StringView,
#| quote~ : Bool = true,
#|) -> StringView {
#| let buf = StringBuilder()
#| self.escape_to(buf, quote~)
#| buf.to_string()
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn StringView::escape_to(
#| self : StringView,
#| logger : &Logger,
#| quote~ : Bool = true,
#|) -> Unit {
#| if quote {
#| logger.write_char('"')
#| }
#| let len = self.length()
#| fn flush_segment(seg : Int32, i : Int32) {
#| if i > seg {
#| logger.write_view(self[seg:i])
#| }
#| }
#| for i = 0, seg = 0 {
#| if i >= len {
#| flush_segment(seg, i)
#| break
#| }
#| let code = self.unsafe_get(i)
#| match code {
#| '"' | '\\' as c => {
#| flush_segment(seg, i)
#| logger.write_char('\\')
#| logger.write_char(c.unsafe_to_char())
#| continue i + 1, i + 1
#| }
#| '\n' => {
#| flush_segment(seg, i)
#| logger <+ "\\n"
#| continue i + 1, i + 1
#| }
#| '\r' => {
#| flush_segment(seg, i)
#| logger <+ "\\r"
#| continue i + 1, i + 1
#| }
#| '\b' => {
#| flush_segment(seg, i)
#| logger <+ "\\b"
#| continue i + 1, i + 1
#| }
#| '\t' => {
#| flush_segment(seg, i)
#| logger <+ "\\t"
#| continue i + 1, i + 1
#| }
#| code =>
#| if code < ' ' {
#| flush_segment(seg, i)
#| logger <+ "\\u{\{code.to_byte().to_hex()}}"
#| continue i + 1, i + 1
#| } else {
#| continue i + 1, seg
#| }
#| }
#| }
#| if quote {
#| logger.write_char('"')
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn StringView::escape_to(
#| self : StringView,
#| logger : &Logger,
#| quote~ : Bool = true,
#|) -> Unit {
#| if quote {
#| logger.write_char('"')
#| }
#| let data = self.str()
#| let end = self.start() + self.length()
#| for i = self.start(), seg = self.start() {
#| let i = find_escape_code_unit(data, i, end)
#| if i > seg {
#| logger.write_view(data.unsafe_view(start=seg, end=i))
#| }
#| guard i < end else { break }
#| let code = data.unsafe_get(i)
#| match code {
#| '"' | '\\' as c => {
#| logger.write_char('\\')
#| logger.write_char(c.unsafe_to_char())
#| }
#| '\n' => logger <+ "\\n"
#| '\r' => logger <+ "\\r"
#| '\b' => logger <+ "\\b"
#| '\t' => logger <+ "\\t"
#| code => logger <+ "\\u{\{code.to_byte().to_hex()}}"
#| }
#| continue i + 1, i + 1
#| }
#| if quote {
#| logger.write_char('"')
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn find_escape_code_unit(data : String, pos : Int32, end : Int32) -> Int32 {
#| if end - pos < 16 {
#| find_escape_code_unit_scalar(data, pos, end)
#| } else if code_unit_needs_escape(data.unsafe_get(pos)) {
#| pos
#| } else {
#| find_escape_code_unit_simd(data, pos + 1, end)
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn code_unit_needs_escape(code : UInt16) -> Bool {
#| code == '"' || code == '\\' || code < ' '
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_escape_code_unit_scalar(
#| data : String,
#| pos : Int32,
#| end : Int32,
#|) -> Int32 {
#| for i in pos.. Int32 {
#| let quote = i16x8_splat('"')
#| let backslash = i16x8_splat('\\')
#| let control_max = i16x8_splat(0x1F)
#| let tail_start = for p = pos; end - p >= 8; {
#| let block = v128_load_i16x8(data, p)
#| let escaped = v128_or(
#| i16x8_le_u(block, control_max),
#| v128_or(i16x8_eq(block, quote), i16x8_eq(block, backslash)),
#| )
#| if v128_any_true(escaped) {
#| return p + i16x8_bitmask(escaped).ctz()
#| }
#| continue p + 8
#| } nobreak {
#| p
#| }
#| find_escape_code_unit_scalar(data, tail_start, end)
#|}
#|pub impl Show for String with fn to_string(self) {
#| self
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[X : Show] Show for X?
#|pub impl[X : Show] Show for X? with fn output(self, logger) {
#| match self {
#| None => logger <+ "None"
#| Some(arg) =>
#| logger <+
#| $|Some(\{arg})
#| }
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[T : Show, E : Show] Show for Result[T, E]
#|pub impl[T : Show, E : Show] Show for Result[T, E] with fn output(self, logger) {
#| match self {
#| Ok(x) =>
#| logger <+
#| $|Ok(\{x})
#| Err(e) =>
#| logger <+
#| $|Err(\{e})
#| }
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[X : Show] Show for FixedArray[X]
#|pub impl[X : Show] Show for FixedArray[X] with fn output(self, logger) {
#| logger.write_iter(self.iter())
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[X : Show] Show for Array[X]
#|pub impl[X : Show] Show for Array[X] with fn output(self, logger) {
#| logger.write_iter(self.iter())
#|}
),
"simd.mbt": (
#|fn v128_make(lo : UInt64, hi : UInt64) -> V128 = "%v128.make"
#|fn v128_lo(value : V128) -> UInt64 = "%v128.lo"
#|fn v128_hi(value : V128) -> UInt64 = "%v128.hi"
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i8x16_splat")
#|fn i8x16_splat(value : Byte) -> V128 {
#| let lane = value.to_uint64() * (0x0101010101010101 : UInt64)
#| v128_make(lane, lane)
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i16x8_splat")
#|fn i16x8_splat(value : UInt16) -> V128 {
#| let lane = value.to_uint64() * (0x0001000100010001 : UInt64)
#| v128_make(lane, lane)
#|}
#|#borrow(bytes)
#|#intrinsic("%v128.v128_load")
#|fn v128_load(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| v128_make(
#| fixedarray_read_uint64_le(bytes, offset),
#| fixedarray_read_uint64_le(bytes, offset + 8),
#| )
#|}
#|#borrow(str)
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.v128_load_i16x8")
#|fn v128_load_i16x8(str : String, offset : Int32) -> V128 {
#| let lo = for i in 0..<4; word = (0 : UInt64) {
#| continue word | (str.unsafe_get(offset + i).to_uint64() << (16 * i))
#| } nobreak {
#| word
#| }
#| let hi = for i in 0..<4; word = (0 : UInt64) {
#| continue word | (str.unsafe_get(offset + 4 + i).to_uint64() << (16 * i))
#| } nobreak {
#| word
#| }
#| v128_make(lo, hi)
#|}
#|#borrow(data)
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.v128_load_i16x8")
#|fn v128_load_fixedarray_i16x8(
#| data : FixedArray[UInt16],
#| offset : Int32,
#|) -> V128 {
#| let lo = for i in 0..<4; word = (0 : UInt64) {
#| continue word | (data.unsafe_get(offset + i).to_uint64() << (16 * i))
#| } nobreak {
#| word
#| }
#| let hi = for i in 0..<4; word = (0 : UInt64) {
#| continue word | (data.unsafe_get(offset + 4 + i).to_uint64() << (16 * i))
#| } nobreak {
#| word
#| }
#| v128_make(lo, hi)
#|}
#|#borrow(data)
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.v128_store_i16x8")
#|fn v128_store_i16x8(
#| data : FixedArray[UInt16],
#| offset : Int32,
#| value : V128,
#|) -> Unit {
#| for i in 0..<4 {
#| data.unsafe_set(offset + i, (v128_lo(value) >> (16 * i)).to_uint16())
#| }
#| for i in 0..<4 {
#| data.unsafe_set(offset + 4 + i, (v128_hi(value) >> (16 * i)).to_uint16())
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.v128_and")
#|fn v128_and(a : V128, b : V128) -> V128 {
#| v128_make(v128_lo(a) & v128_lo(b), v128_hi(a) & v128_hi(b))
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.v128_or")
#|fn v128_or(a : V128, b : V128) -> V128 {
#| v128_make(v128_lo(a) | v128_lo(b), v128_hi(a) | v128_hi(b))
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.v128_any_true")
#|fn v128_any_true(value : V128) -> Bool {
#| v128_lo(value) != 0 || v128_hi(value) != 0
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i16x8_add")
#|fn i16x8_add(a : V128, b : V128) -> V128 {
#| v128_make(
#| u64_u16x4_add(v128_lo(a), v128_lo(b)),
#| u64_u16x4_add(v128_hi(a), v128_hi(b)),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i16x8_sub")
#|fn i16x8_sub(a : V128, b : V128) -> V128 {
#| v128_make(
#| u64_u16x4_sub(v128_lo(a), v128_lo(b)),
#| u64_u16x4_sub(v128_hi(a), v128_hi(b)),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i16x8_le_u")
#|fn i16x8_le_u(a : V128, b : V128) -> V128 {
#| v128_make(
#| u64_u16x4_le_mask(v128_lo(a), v128_lo(b)),
#| u64_u16x4_le_mask(v128_hi(a), v128_hi(b)),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i8x16_eq")
#|fn i8x16_eq(a : V128, b : V128) -> V128 {
#| v128_make(
#| u64_byte_eq_mask(v128_lo(a), v128_lo(b)),
#| u64_byte_eq_mask(v128_hi(a), v128_hi(b)),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i16x8_eq")
#|fn i16x8_eq(a : V128, b : V128) -> V128 {
#| v128_make(
#| u64_u16_eq_mask(v128_lo(a), v128_lo(b)),
#| u64_u16_eq_mask(v128_hi(a), v128_hi(b)),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i8x16_bitmask")
#|fn i8x16_bitmask(value : V128) -> Int32 {
#| u64_high_bits(v128_lo(value)) | (u64_high_bits(v128_hi(value)) << 8)
#|}
#|#cfg(any(target="native", target="wasm"))
#|#intrinsic("%v128.i16x8_bitmask")
#|fn i16x8_bitmask(value : V128) -> Int32 {
#| u64_u16_high_bits(v128_lo(value)) | (u64_u16_high_bits(v128_hi(value)) << 4)
#|}
#|#cfg(target="wasm")
#|#intrinsic("%v128.i8x16_all_true")
#|fn i8x16_all_true(value : V128) -> Bool {
#| u64_byte_eq_mask(v128_lo(value), 0) == 0 &&
#| u64_byte_eq_mask(v128_hi(value), 0) == 0
#|}
#|#cfg(target="wasm")
#|#intrinsic("%v128.i16x8_all_true")
#|fn i16x8_all_true(value : V128) -> Bool {
#| u64_u16_eq_mask(v128_lo(value), 0) == 0 &&
#| u64_u16_eq_mask(v128_hi(value), 0) == 0
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_byte_eq_mask(a : UInt64, b : UInt64) -> UInt64 {
#| for i in 0..<8; result = (0 : UInt64) {
#| let shift = 8 * i
#| let mask = if ((a >> shift) & 0xFF) == ((b >> shift) & 0xFF) {
#| (0xFF : UInt64)
#| } else {
#| 0
#| }
#| continue result | (mask << shift)
#| } nobreak {
#| result
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_u16_eq_mask(a : UInt64, b : UInt64) -> UInt64 {
#| for i in 0..<4; result = (0 : UInt64) {
#| let shift = 16 * i
#| let mask = if ((a >> shift) & 0xFFFF) == ((b >> shift) & 0xFFFF) {
#| (0xFFFF : UInt64)
#| } else {
#| 0
#| }
#| continue result | (mask << shift)
#| } nobreak {
#| result
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_u16x4_add(a : UInt64, b : UInt64) -> UInt64 {
#| for i in 0..<4; result = (0 : UInt64) {
#| let shift = 16 * i
#| let lane = (a >> shift).to_uint16() + (b >> shift).to_uint16()
#| continue result | (lane.to_uint64() << shift)
#| } nobreak {
#| result
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_u16x4_sub(a : UInt64, b : UInt64) -> UInt64 {
#| for i in 0..<4; result = (0 : UInt64) {
#| let shift = 16 * i
#| let lane = (a >> shift).to_uint16() - (b >> shift).to_uint16()
#| continue result | (lane.to_uint64() << shift)
#| } nobreak {
#| result
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_u16x4_le_mask(a : UInt64, b : UInt64) -> UInt64 {
#| for i in 0..<4; result = (0 : UInt64) {
#| let shift = 16 * i
#| let mask = if (a >> shift).to_uint16() <= (b >> shift).to_uint16() {
#| (0xFFFF : UInt64)
#| } else {
#| 0
#| }
#| continue result | (mask << shift)
#| } nobreak {
#| result
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_high_bits(value : UInt64) -> Int32 {
#| for i in 0..<8; result = 0 {
#| let bit = ((value >> (8 * i + 7)) & 1).to_int()
#| continue result | (bit << i)
#| } nobreak {
#| result
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn u64_u16_high_bits(value : UInt64) -> Int32 {
#| for i in 0..<4; result = 0 {
#| let bit = ((value >> (16 * i + 15)) & 1).to_int()
#| continue result | (bit << i)
#| } nobreak {
#| result
#| }
#|}
),
"string.mbt": (
#|fn unsafe_make_string(length : Int32, value : Char) -> String = "$moonbit.unsafe_make_string"
#|pub fn String::make(length : Int32, value : Char) -> String {
#| guard length >= 0 else { abort("invalid length") }
#| if value.to_int() <= 0xFFFF {
#| unsafe_make_string(length, value)
#| } else {
#| let buf = StringBuilder(capacity=2 * length)
#| for _ in 0.. Char {
#| ((leading - 0xD800) * 0x400 + trailing - 0xDC00 + 0x10000).unsafe_to_char()
#|}
#|#alias(codepoint_length, deprecated)
#|#cfg(not(any(target="native", target="wasm")))
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::char_length(
#| self : String,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Int32 {
#| let end = if end is Some(o) { o } else { self.length() }
#| guard start >= 0 && start <= end && end <= self.length() else {
#| abort("invalid start or end index for String::codepoint_length")
#| }
#| for utf16_index = start, char_count = 0
#| utf16_index < end
#| utf16_index = utf16_index + 1, char_count = char_count + 1 {
#| let c1 = self.unsafe_get(utf16_index)
#| if c1.is_leading_surrogate() && utf16_index + 1 < end {
#| let c2 = self.unsafe_get(utf16_index + 1)
#| if c2.is_trailing_surrogate() {
#| continue utf16_index + 2, char_count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| char_count
#| }
#|}
#|#alias(codepoint_length, deprecated)
#|#cfg(any(target="native", target="wasm"))
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::char_length(
#| self : String,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Int32 {
#| let end = if end is Some(o) { o } else { self.length() }
#| guard start >= 0 && start <= end && end <= self.length() else {
#| abort("invalid start or end index for String::codepoint_length")
#| }
#| if end - start >= CHAR_LENGTH_SIMD_THRESHOLD {
#| return string_char_length_simd(self, start, end)
#| }
#| for utf16_index = start, char_count = 0
#| utf16_index < end
#| utf16_index = utf16_index + 1, char_count = char_count + 1 {
#| let c1 = self.unsafe_get(utf16_index)
#| if c1.is_leading_surrogate() && utf16_index + 1 < end {
#| let c2 = self.unsafe_get(utf16_index + 1)
#| if c2.is_trailing_surrogate() {
#| continue utf16_index + 2, char_count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| char_count
#| }
#|}
#|#intrinsic("%string.substring")
#|pub fn String::unsafe_substring(
#| str : String,
#| start~ : Int32,
#| end~ : Int32,
#|) -> String {
#| if start == 0 && end == str.length() {
#| return str
#| }
#| let len = end - start
#| let bytes = FixedArray::make(len * 2, b'\x00')
#| bytes.blit_from_string(0, str, start, len)
#| bytes.unsafe_reinterpret_as_bytes().to_unchecked_string()
#|}
#|pub fn String::range_equal(
#| self : String,
#| start~ : Int32,
#| other : String,
#| other_start~ : Int32,
#| len~ : Int32,
#|) -> Bool {
#| let self_len = self.length()
#| let other_len = other.length()
#| guard len >= 0 &&
#| start >= 0 &&
#| start <= self_len &&
#| other_start >= 0 &&
#| other_start <= other_len &&
#| len <= self_len - start &&
#| len <= other_len - other_start else {
#| abort("String::range_equal: invalid range")
#| }
#| self.unsafe_range_equal(other, start~, other_start~, len~)
#|}
#|#cfg(target="wasm")
#|#intrinsic("%string.unsafe_range_equal")
#|pub fn String::unsafe_range_equal(
#| self : String,
#| start~ : Int32,
#| other : String,
#| other_start~ : Int32,
#| len~ : Int32,
#|) -> Bool {
#| if len >= 8 {
#| return string_range_equal_simd(self, start, other, other_start, len)
#| }
#| for i in 0.. Bool {
#| for i in 0.. Bool {
#| for i = 0; i + 8 <= len; i = i + 8 {
#| let a = v128_load_i16x8(left, left_off + i)
#| let b = v128_load_i16x8(right, right_off + i)
#| guard i16x8_all_true(i16x8_eq(a, b)) else { return false }
#| }
#| if (len & 7) != 0 {
#| let a = v128_load_i16x8(left, left_off + len - 8)
#| let b = v128_load_i16x8(right, right_off + len - 8)
#| return i16x8_all_true(i16x8_eq(a, b))
#| }
#| true
#|}
#|#deprecated("Use `str[:]` or `str[:].to_string()` instead", skip_current_package=true)
#|pub fn String::substring(
#| self : String,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> String {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard! start >= 0 && start <= end && end <= len
#| self.unsafe_substring(start~, end~)
#|}
#|pub fn String::suffixes(
#| self : String,
#| include_empty~ : Bool = false,
#|) -> Iter[StringView] {
#| self[:].suffixes(include_empty~)
#|}
#|test "substring/empty" {
#| let s = "test"
#| inspect(s.substring(start=2, end=2), content="")
#| inspect(s.substring(start=4, end=4), content="")
#| inspect("".substring(), content="")
#|}
#|test "panic substring/invalid_range" {
#| let s = "test"
#| ignore(s.substring(start=-1))
#| ignore(s.substring(end=5))
#| ignore(s.substring(start=3, end=2))
#|}
#|test "substring/basic" {
#| inspect("Hello world".substring(start=0, end=5), content="Hello")
#| inspect("Hello world".substring(start=6, end=11), content="world")
#| inspect("Hello world".substring(start=0), content="Hello world")
#| inspect("Hello world".substring(start=6), content="world")
#|}
#|test "substring/boundary" {
#| inspect("".substring(start=0, end=0), content="")
#| inspect("a".substring(start=0, end=1), content="a")
#| inspect("abc".substring(start=0), content="abc")
#| inspect("abc".substring(start=1), content="bc")
#| inspect("abc".substring(start=0, end=3), content="abc")
#|}
#|test "panic substring/out_of_bounds" {
#| ignore("hello".substring(start=-1, end=4))
#| ignore("hello".substring(start=6, end=4))
#| ignore("hello".substring(start=0, end=6))
#|}
#|pub impl Compare for String with fn compare(self, other) {
#| let len = self.length()
#| match len.compare(other.length()) {
#| 0 => {
#| for i in 0.. order
#| }
#|}
#|pub impl Default for String with fn default() {
#| ""
#|}
#|#deprecated("Check `@encoding/utf8.encode`")
#|pub fn String::to_bytes(self : String) -> Bytes {
#| let array = FixedArray::make(self.length() * 2, b'\x00')
#| array.blit_from_string(0, self, 0, self.length())
#| array |> unsafe_to_bytes
#|}
#|fn unsafe_to_bytes(array : FixedArray[Byte]) -> Bytes = "%identity"
#|pub fn String::to_array(self : String) -> Array[Char] {
#| let chars : Array[Char] = Array(capacity=self.length())
#| for c in self {
#| chars.push(c)
#| }
#| chars
#|}
#|#intrinsic("%string.code_units")
#|pub fn String::code_units(self : String) -> ArrayView[UInt16] {
#| FixedArray::makei(self.length(), i => self.unsafe_get(i))
#|}
#|#alias(iterator, deprecated)
#|pub fn String::iter(self : String) -> Iter[Char] {
#| let len = self.length()
#| let mut index = 0
#| Iter(fn() {
#| guard index < len else { None }
#| let c1 = self.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < len {
#| let c2 = self.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| let c = code_point_of_surrogate_pair(c1.to_int(), c2.to_int())
#| index += 2
#| return Some(c)
#| }
#| }
#| index += 1
#| Some(c1.unsafe_to_char())
#| })
#|}
#|#alias(iterator2, deprecated)
#|pub fn String::iter2(self : String) -> Iter2[Int32, Char] {
#| self.iter().iter2()
#|}
#|#locals(f)
#|#alias(every)
#|pub fn String::all(self : String, f : (Char) -> Bool raise?) -> Bool raise? {
#| for c in self {
#| if !f(c) {
#| return false
#| }
#| }
#| true
#|}
#|#locals(f)
#|#alias(exists)
#|pub fn String::any(self : String, f : (Char) -> Bool raise?) -> Bool raise? {
#| for c in self {
#| if f(c) {
#| return true
#| }
#| }
#| false
#|}
#|#alias(rev_iterator, deprecated)
#|pub fn String::rev_iter(self : String) -> Iter[Char] {
#| self[:].rev_iter()
#|}
#|fn String::offset_of_nth_char_forward(
#| self : String,
#| n : Int32,
#| start_offset~ : Int32,
#| end_offset~ : Int32,
#|) -> Int32? {
#| guard start_offset >= 0 && start_offset <= end_offset else {
#| abort("Invalid start index")
#| }
#| let (utf16_offset, char_count) = for utf16_offset = start_offset, char_count = 0; utf16_offset <
#| end_offset &&
#| char_count < n; {
#| let c = self.unsafe_get(utf16_offset)
#| if c.is_leading_surrogate() {
#| continue utf16_offset + 2, char_count + 1
#| } else {
#| continue utf16_offset + 1, char_count + 1
#| }
#| } nobreak {
#| (utf16_offset, char_count)
#| }
#| if char_count < n || utf16_offset >= end_offset {
#| None
#| } else {
#| Some(utf16_offset)
#| }
#|}
#|fn String::offset_of_nth_char_backward(
#| self : String,
#| n : Int32,
#| start_offset~ : Int32,
#| end_offset~ : Int32,
#|) -> Int32? {
#| let (utf16_offset, char_count) = for utf16_offset = end_offset, char_count = 0; utf16_offset -
#| 1 >=
#| start_offset &&
#| char_count < n; {
#| let c = self.unsafe_get(utf16_offset - 1)
#| if c.is_trailing_surrogate() {
#| continue utf16_offset - 2, char_count + 1
#| } else {
#| continue utf16_offset - 1, char_count + 1
#| }
#| } nobreak {
#| (utf16_offset, char_count)
#| }
#| if char_count < n || utf16_offset < start_offset {
#| None
#| } else {
#| Some(utf16_offset)
#| }
#|}
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::offset_of_nth_char(
#| self : String,
#| i : Int32,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Int32? {
#| let end = if end is Some(o) { o } else { self.length() }
#| if i >= 0 {
#| self.offset_of_nth_char_forward(i, start_offset=start, end_offset=end)
#| } else {
#| self.offset_of_nth_char_backward(-i, start_offset=start, end_offset=end)
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::char_length_eq(
#| self : String,
#| len : Int32,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Bool {
#| let end = if end is Some(o) { o } else { self.length() }
#| for index = start, count = 0
#| index < end && count < len
#| index = index + 1, count = count + 1 {
#| let c1 = self.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < end {
#| let c2 = self.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| continue index + 2, count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| count == len && index == end
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::char_length_eq(
#| self : String,
#| len : Int32,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Bool {
#| let end = if end is Some(o) { o } else { self.length() }
#| if len >= CHAR_LENGTH_SIMD_THRESHOLD &&
#| end - start >= CHAR_LENGTH_SIMD_THRESHOLD &&
#| start >= 0 &&
#| start <= end &&
#| end <= self.length() {
#| return string_char_length_cmp_simd(self, len, start, end, exact=true)
#| }
#| for index = start, count = 0
#| index < end && count < len
#| index = index + 1, count = count + 1 {
#| let c1 = self.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < end {
#| let c2 = self.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| continue index + 2, count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| count == len && index == end
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::char_length_ge(
#| self : String,
#| len : Int32,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Bool {
#| let end = if end is Some(o) { o } else { self.length() }
#| for index = start, count = 0
#| index < end && count < len
#| index = index + 1, count = count + 1 {
#| let c1 = self.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < end {
#| let c2 = self.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| continue index + 2, count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| count >= len
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#label_migration(start, alias=start_offset, msg="Use start= instead")
#|#label_migration(end, alias=end_offset, msg="Use end= instead")
#|pub fn String::char_length_ge(
#| self : String,
#| len : Int32,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> Bool {
#| let end = if end is Some(o) { o } else { self.length() }
#| if len >= CHAR_LENGTH_SIMD_THRESHOLD &&
#| end - start >= CHAR_LENGTH_SIMD_THRESHOLD &&
#| start >= 0 &&
#| start <= end &&
#| end <= self.length() {
#| return string_char_length_cmp_simd(self, len, start, end, exact=false)
#| }
#| for index = start, count = 0
#| index < end && count < len
#| index = index + 1, count = count + 1 {
#| let c1 = self.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < end {
#| let c2 = self.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| continue index + 2, count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| count >= len
#| }
#|}
#|pub fn String::lexical_compare(self : String, other : String) -> Int32 {
#| self[:].lexical_compare(other)
#|}
#|pub fn String::compare_ignore_ascii_case(
#| self : String,
#| other : String,
#|) -> Int32 {
#| self[:].compare_ignore_ascii_case(other)
#|}
#|pub fn String::equal_ignore_ascii_case(self : String, other : String) -> Bool {
#| self[:].equal_ignore_ascii_case(other)
#|}
#|pub fn String::from_array(chars : ArrayView[Char]) -> String {
#| let buf = StringBuilder(capacity=chars.length() * 4)
#| for c in chars {
#| buf.write_char(c)
#| }
#| buf.to_string()
#|}
#|#alias(from_iterator, deprecated)
#|pub fn String::from_iter(iter : Iter[Char]) -> String {
#| let buf = StringBuilder()
#| for c in iter {
#| buf.write_char(c)
#| }
#| buf.to_string()
#|}
),
"string_char_length.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|const CHAR_LENGTH_SIMD_THRESHOLD = 16
#|#cfg(any(target="native", target="wasm"))
#|fn string_char_length_scalar(
#| str : String,
#| index : Int32,
#| end : Int32,
#| count : Int32,
#|) -> Int32 {
#| for index = index, count = count
#| index < end
#| index = index + 1, count = count + 1 {
#| let c1 = str.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < end {
#| let c2 = str.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| continue index + 2, count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| count
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_char_length_cmp_scalar(
#| str : String,
#| len : Int32,
#| index : Int32,
#| end : Int32,
#| count : Int32,
#| exact~ : Bool,
#|) -> Bool {
#| for index = index, count = count
#| index < end && count < len
#| index = index + 1, count = count + 1 {
#| let c1 = str.unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < end {
#| let c2 = str.unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| continue index + 2, count + 1
#| } else {
#| abort("invalid surrogate pair")
#| }
#| }
#| } nobreak {
#| if exact {
#| count == len && index == end
#| } else {
#| count >= len
#| }
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_char_length_simd(str : String, start : Int32, end : Int32) -> Int32 {
#| let leading_base = i16x8_splat(0xD800)
#| let trailing_base = i16x8_splat(0xDC00)
#| let surrogate_span = i16x8_splat(0x3FF)
#| let trailing_limit = i16x8_splat(0x400)
#| for index = start, pairs = 0; end - index >= 8; {
#| let leading = i16x8_le_u(
#| i16x8_sub(v128_load_i16x8(str, index), leading_base),
#| surrogate_span,
#| )
#| guard v128_any_true(leading) else { continue index + 8, pairs }
#| guard end - index > 8 else {
#| break string_char_length_scalar(str, index, end, index - start - pairs)
#| }
#| let not_trailing = i16x8_le_u(
#| trailing_limit,
#| i16x8_sub(v128_load_i16x8(str, index + 1), trailing_base),
#| )
#| if v128_any_true(v128_and(leading, not_trailing)) {
#| abort("invalid surrogate pair")
#| }
#| continue index + 8, pairs + i16x8_bitmask(leading).popcnt()
#| } nobreak {
#| string_char_length_scalar(str, index, end, index - start - pairs)
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_char_length_cmp_simd(
#| str : String,
#| len : Int32,
#| start : Int32,
#| end : Int32,
#| exact~ : Bool,
#|) -> Bool {
#| let leading_base = i16x8_splat(0xD800)
#| let trailing_base = i16x8_splat(0xDC00)
#| let surrogate_span = i16x8_splat(0x3FF)
#| let trailing_limit = i16x8_splat(0x400)
#| for index = start, count = 0; end - index >= 8 && len - count >= 8; {
#| let leading = i16x8_le_u(
#| i16x8_sub(v128_load_i16x8(str, index), leading_base),
#| surrogate_span,
#| )
#| guard v128_any_true(leading) else { continue index + 8, count + 8 }
#| guard end - index > 8 else {
#| break string_char_length_cmp_scalar(str, len, index, end, count, exact~)
#| }
#| let not_trailing = i16x8_le_u(
#| trailing_limit,
#| i16x8_sub(v128_load_i16x8(str, index + 1), trailing_base),
#| )
#| if v128_any_true(v128_and(leading, not_trailing)) {
#| abort("invalid surrogate pair")
#| }
#| continue index + 8, count + 8 - i16x8_bitmask(leading).popcnt()
#| } nobreak {
#| string_char_length_cmp_scalar(str, len, index, end, count, exact~)
#| }
#|}
),
"string_find_code_unit.mbt": (
#|fn find_code_unit_from_view(
#| target : StringView,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| let target_start = target.start_offset()
#| let found = find_code_unit_from_string(
#| target.data(),
#| target_start + start,
#| target_start + end,
#| code,
#| )
#| if found < 0 {
#| -1
#| } else {
#| found - target_start
#| }
#|}
#|fn rev_find_code_unit_from_view(
#| target : StringView,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| let target_start = target.start_offset()
#| let found = rev_find_code_unit_from_string(
#| target.data(),
#| target_start + start,
#| target_start + end,
#| code,
#| )
#| if found < 0 {
#| -1
#| } else {
#| found - target_start
#| }
#|}
#|#inline
#|fn find_code_unit_scalar(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| for pos in start.. Int32 {
#| for pos in end>..start {
#| if data.unsafe_get(pos) == code {
#| break pos
#| }
#| } nobreak {
#| -1
#| }
#|}
#|#cfg(not(any(target="native", target="wasm", target="js")))
#|fn find_code_unit_from_string(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| find_code_unit_scalar(data, start, end, code)
#|}
#|#cfg(not(any(target="native", target="wasm", target="js")))
#|fn rev_find_code_unit_from_string(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| rev_find_code_unit_scalar(data, start, end, code)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_code_unit_from_string(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| guard start < end else { return -1 }
#| if data.unsafe_get(start) == code {
#| return start
#| }
#| guard end - start >= 8 else {
#| return find_code_unit_scalar(data, start + 1, end, code)
#| }
#| let code_v = i16x8_splat(code)
#| let pos = for pos = start; end - pos >= 16; {
#| let lo = i16x8_eq(v128_load_i16x8(data, pos), code_v)
#| let hi = i16x8_eq(v128_load_i16x8(data, pos + 8), code_v)
#| if v128_any_true(v128_or(lo, hi)) {
#| let lo_mask = i16x8_bitmask(lo)
#| if lo_mask != 0 {
#| return pos + lo_mask.ctz()
#| }
#| return pos + 8 + i16x8_bitmask(hi).ctz()
#| }
#| continue pos + 16
#| } nobreak {
#| pos
#| }
#| let pos = if end - pos >= 8 {
#| let mask = i16x8_bitmask(i16x8_eq(v128_load_i16x8(data, pos), code_v))
#| if mask != 0 {
#| return pos + mask.ctz()
#| }
#| pos + 8
#| } else {
#| pos
#| }
#| guard pos < end else { return -1 }
#| let tail = end - 8
#| let mask = i16x8_bitmask(i16x8_eq(v128_load_i16x8(data, tail), code_v))
#| if mask != 0 {
#| tail + mask.ctz()
#| } else {
#| -1
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn rev_find_code_unit_from_string(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| guard start < end else { return -1 }
#| if data.unsafe_get(end - 1) == code {
#| return end - 1
#| }
#| guard end - start >= 8 else {
#| return rev_find_code_unit_scalar(data, start, end - 1, code)
#| }
#| let code_v = i16x8_splat(code)
#| let pos = for pos = end; pos - 16 >= start; {
#| let hi = i16x8_eq(v128_load_i16x8(data, pos - 8), code_v)
#| let lo = i16x8_eq(v128_load_i16x8(data, pos - 16), code_v)
#| if v128_any_true(v128_or(hi, lo)) {
#| let hi_mask = i16x8_bitmask(hi)
#| if hi_mask != 0 {
#| return pos - 8 + 31 - hi_mask.clz()
#| }
#| return pos - 16 + 31 - i16x8_bitmask(lo).clz()
#| }
#| continue pos - 16
#| } nobreak {
#| pos
#| }
#| let pos = if pos - 8 >= start {
#| let mask = i16x8_bitmask(i16x8_eq(v128_load_i16x8(data, pos - 8), code_v))
#| if mask != 0 {
#| return pos - 8 + 31 - mask.clz()
#| }
#| pos - 8
#| } else {
#| pos
#| }
#| guard pos > start else { return -1 }
#| let mask = i16x8_bitmask(i16x8_eq(v128_load_i16x8(data, start), code_v))
#| if mask != 0 {
#| start + 31 - mask.clz()
#| } else {
#| -1
#| }
#|}
#|#cfg(not(target="js"))
#|#inline
#|fn find_pattern(target : StringView, pattern : StringView) -> Int32? {
#| find_by_two_anchors(target, pattern)
#|}
#|fn find_by_two_anchors(target : StringView, pattern : StringView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let last_offset = pattern_len - 1
#| let candidate_end = target_start + target_len - pattern_len + 1
#| let first = pattern.unsafe_get(0)
#| let last = pattern.unsafe_get(last_offset)
#| let middle_len = last_offset - 1
#| for pos = target_start, failures = 0; pos < candidate_end; {
#| let found = find_two_anchor_candidate_from_string(
#| target.data(),
#| pos,
#| candidate_end,
#| first,
#| last_offset,
#| last,
#| )
#| if found < 0 {
#| break None
#| }
#| if string_ranges_equal(
#| target.data(),
#| found + 1,
#| pattern.data(),
#| pattern_start + 1,
#| middle_len,
#| ) {
#| break Some(found - target_start)
#| }
#| let failures = failures + 1
#| let scanned = found - target_start
#| if two_anchor_should_fallback(failures, scanned) {
#| break find_pattern_kmp_from(target, pattern, scanned + 1)
#| }
#| continue found + 1, failures
#| } nobreak {
#| None
#| }
#|}
#|fn rev_find_by_two_anchors(target : StringView, pattern : StringView) -> Int32? {
#| let target_len = target.length()
#| let pattern_len = pattern.length()
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let last_offset = pattern_len - 1
#| let first = pattern.unsafe_get(0)
#| let last = pattern.unsafe_get(last_offset)
#| let middle_len = last_offset - 1
#| let last_candidate = target_len - pattern_len
#| for candidate_end = target_start + last_candidate + 1, failures = 0; candidate_end >
#| target_start; {
#| let found = rev_find_two_anchor_candidate_from_string(
#| target.data(),
#| target_start,
#| candidate_end,
#| first,
#| last_offset,
#| last,
#| )
#| if found < 0 {
#| break None
#| }
#| if string_ranges_equal(
#| target.data(),
#| found + 1,
#| pattern.data(),
#| pattern_start + 1,
#| middle_len,
#| ) {
#| break Some(found - target_start)
#| }
#| let candidate = found - target_start
#| let failures = failures + 1
#| let scanned = last_candidate - candidate
#| if two_anchor_should_fallback(failures, scanned) {
#| break rev_find_pattern_kmp_before(target, pattern, candidate)
#| }
#| continue found, failures
#| } nobreak {
#| None
#| }
#|}
#|#inline
#|fn two_anchor_should_fallback(failures : Int32, scanned : Int32) -> Bool {
#| failures > 64 || failures > 4 + scanned / 8
#|}
#|fn kmp_failure_table(pattern : StringView) -> FixedArray[Int32] {
#| let m = pattern.length()
#| let table = FixedArray::make(m, 0)
#| let mut k = 0
#| for i in 1.. 0 && c != pattern.unsafe_get(k) {
#| k = table[k - 1]
#| }
#| if c == pattern.unsafe_get(k) {
#| k += 1
#| }
#| table[i] = k
#| }
#| table
#|}
#|fn find_pattern_kmp_from(
#| target : StringView,
#| pattern : StringView,
#| start : Int32,
#|) -> Int32? {
#| let n = target.length()
#| let m = pattern.length()
#| let table = kmp_failure_table(pattern)
#| let mut k = 0
#| for i in start.. 0 && c != pattern.unsafe_get(k) {
#| k = table[k - 1]
#| }
#| if c == pattern.unsafe_get(k) {
#| k += 1
#| }
#| if k == m {
#| return Some(i - m + 1)
#| }
#| }
#| None
#|}
#|fn rev_find_pattern_kmp_before(
#| target : StringView,
#| pattern : StringView,
#| candidate_end : Int32,
#|) -> Int32? {
#| guard candidate_end > 0 else { return None }
#| let m = pattern.length()
#| let table = kmp_failure_table(pattern)
#| let scan_end = candidate_end + m - 1
#| let mut k = 0
#| let mut best = -1
#| for i in 0.. 0 && c != pattern.unsafe_get(k) {
#| k = table[k - 1]
#| }
#| if c == pattern.unsafe_get(k) {
#| k += 1
#| }
#| if k == m {
#| best = i - m + 1
#| k = table[k - 1]
#| }
#| }
#| if best >= 0 {
#| Some(best)
#| } else {
#| None
#| }
#|}
#|test "kmp fallbacks handle periodic and overlapping patterns" {
#| let t : StringView = "aaaaaa"
#| assert_true(find_pattern_kmp_from(t, "aaa", 0) is Some(0))
#| assert_true(find_pattern_kmp_from(t, "aaa", 2) is Some(2))
#| assert_true(find_pattern_kmp_from(t, "aaa", 4) is None)
#| assert_true(rev_find_pattern_kmp_before(t, "aaa", 4) is Some(3))
#| assert_true(rev_find_pattern_kmp_before(t, "aaa", 1) is Some(0))
#| let u : StringView = "ababcababab"
#| assert_true(find_pattern_kmp_from(u, "abab", 0) is Some(0))
#| assert_true(find_pattern_kmp_from(u, "abab", 1) is Some(5))
#| assert_true(rev_find_pattern_kmp_before(u, "abab", 8) is Some(7))
#| assert_true(rev_find_pattern_kmp_before(u, "ababc", 7) is Some(0))
#| assert_true(find_pattern_kmp_from(u, "abcabc", 0) is None)
#| assert_true(rev_find_pattern_kmp_before(u, "abab", 0) is None)
#|}
#|#inline
#|fn string_ranges_equal(
#| left : String,
#| left_start : Int32,
#| right : String,
#| right_start : Int32,
#| length : Int32,
#|) -> Bool {
#| for i in 0.. Int32 {
#| for pos in start.. Int32 {
#| for pos in candidate_end>..start {
#| if data.unsafe_get(pos) == first &&
#| data.unsafe_get(pos + last_offset) == last {
#| break pos
#| }
#| } nobreak {
#| -1
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn find_two_anchor_candidate_from_string(
#| data : String,
#| start : Int32,
#| candidate_end : Int32,
#| first : UInt16,
#| last_offset : Int32,
#| last : UInt16,
#|) -> Int32 {
#| find_two_anchor_candidate_scalar(
#| data, start, candidate_end, first, last_offset, last,
#| )
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn rev_find_two_anchor_candidate_from_string(
#| data : String,
#| start : Int32,
#| candidate_end : Int32,
#| first : UInt16,
#| last_offset : Int32,
#| last : UInt16,
#|) -> Int32 {
#| rev_find_two_anchor_candidate_scalar(
#| data, start, candidate_end, first, last_offset, last,
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_two_anchor_candidate_from_string(
#| data : String,
#| start : Int32,
#| candidate_end : Int32,
#| first : UInt16,
#| last_offset : Int32,
#| last : UInt16,
#|) -> Int32 {
#| guard start < candidate_end else { return -1 }
#| if data.unsafe_get(start) == first &&
#| data.unsafe_get(start + last_offset) == last {
#| return start
#| }
#| let first_v = i16x8_splat(first)
#| let last_v = i16x8_splat(last)
#| let pos = for pos = start; candidate_end - pos >= 16; {
#| let lo = v128_and(
#| i16x8_eq(v128_load_i16x8(data, pos), first_v),
#| i16x8_eq(v128_load_i16x8(data, pos + last_offset), last_v),
#| )
#| let hi = v128_and(
#| i16x8_eq(v128_load_i16x8(data, pos + 8), first_v),
#| i16x8_eq(v128_load_i16x8(data, pos + 8 + last_offset), last_v),
#| )
#| if v128_any_true(v128_or(lo, hi)) {
#| let lo_bits = i16x8_bitmask(lo)
#| if lo_bits != 0 {
#| return pos + lo_bits.ctz()
#| }
#| return pos + 8 + i16x8_bitmask(hi).ctz()
#| }
#| continue pos + 16
#| } nobreak {
#| pos
#| }
#| let tail_start = if candidate_end - pos >= 8 {
#| let mask = v128_and(
#| i16x8_eq(v128_load_i16x8(data, pos), first_v),
#| i16x8_eq(v128_load_i16x8(data, pos + last_offset), last_v),
#| )
#| let bits = i16x8_bitmask(mask)
#| if bits != 0 {
#| return pos + bits.ctz()
#| }
#| pos + 8
#| } else {
#| pos
#| }
#| find_two_anchor_candidate_scalar(
#| data, tail_start, candidate_end, first, last_offset, last,
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn rev_find_two_anchor_candidate_from_string(
#| data : String,
#| start : Int32,
#| candidate_end : Int32,
#| first : UInt16,
#| last_offset : Int32,
#| last : UInt16,
#|) -> Int32 {
#| guard start < candidate_end else { return -1 }
#| let final_candidate = candidate_end - 1
#| if data.unsafe_get(final_candidate) == first &&
#| data.unsafe_get(final_candidate + last_offset) == last {
#| return final_candidate
#| }
#| let first_v = i16x8_splat(first)
#| let last_v = i16x8_splat(last)
#| let pos = for pos = candidate_end; pos - 16 >= start; {
#| let hi = v128_and(
#| i16x8_eq(v128_load_i16x8(data, pos - 8), first_v),
#| i16x8_eq(v128_load_i16x8(data, pos - 8 + last_offset), last_v),
#| )
#| let lo = v128_and(
#| i16x8_eq(v128_load_i16x8(data, pos - 16), first_v),
#| i16x8_eq(v128_load_i16x8(data, pos - 16 + last_offset), last_v),
#| )
#| if v128_any_true(v128_or(hi, lo)) {
#| let hi_bits = i16x8_bitmask(hi)
#| if hi_bits != 0 {
#| return pos - 8 + 31 - hi_bits.clz()
#| }
#| return pos - 16 + 31 - i16x8_bitmask(lo).clz()
#| }
#| continue pos - 16
#| } nobreak {
#| pos
#| }
#| let head_end = if pos - 8 >= start {
#| let chunk_start = pos - 8
#| let mask = v128_and(
#| i16x8_eq(v128_load_i16x8(data, chunk_start), first_v),
#| i16x8_eq(v128_load_i16x8(data, chunk_start + last_offset), last_v),
#| )
#| let bits = i16x8_bitmask(mask)
#| if bits != 0 {
#| return chunk_start + 31 - bits.clz()
#| }
#| chunk_start
#| } else {
#| pos
#| }
#| rev_find_two_anchor_candidate_scalar(
#| data, start, head_end, first, last_offset, last,
#| )
#|}
),
"string_find_js.mbt": (
#|extern "js" fn js_index_of(
#| data : String,
#| start : Int32,
#| end : Int32,
#| pattern : String,
#|) -> Int32 =
#| #|(s, start, end, p) => {
#| #| if (end - start < p.length) return -1;
#| #| if (end === s.length) return s.indexOf(p, start);
#| #| const i = s.substring(start, end).indexOf(p);
#| #| return i < 0 ? i : start + i;
#| #|}
#|extern "js" fn js_last_index_of(
#| data : String,
#| start : Int32,
#| end : Int32,
#| pattern : String,
#|) -> Int32 =
#| #|(s, start, end, p) => {
#| #| if (end - start < p.length) return -1;
#| #| if (start === 0) return s.lastIndexOf(p, end - p.length);
#| #| const i = s.substring(start, end).lastIndexOf(p);
#| #| return i < 0 ? i : start + i;
#| #|}
#|extern "js" fn js_code_unit_string(code : UInt16) -> String =
#| #|(c) => String.fromCharCode(c)
#|extern "js" fn js_substring(
#| data : String,
#| start : Int32,
#| end : Int32,
#|) -> String =
#| #|(s, start, end) => start === 0 && end === s.length ? s : s.substring(start, end)
#|const JS_SCALAR_SCAN_MAX = 8
#|fn find_code_unit_from_string(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| if end - start <= JS_SCALAR_SCAN_MAX {
#| find_code_unit_scalar(data, start, end, code)
#| } else {
#| js_index_of(data, start, end, js_code_unit_string(code))
#| }
#|}
#|fn rev_find_code_unit_from_string(
#| data : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Int32 {
#| if end - start <= JS_SCALAR_SCAN_MAX {
#| rev_find_code_unit_scalar(data, start, end, code)
#| } else {
#| js_last_index_of(data, start, end, js_code_unit_string(code))
#| }
#|}
#|const JS_INDEX_OF_MAX_PATTERN = 250
#|fn find_pattern(target : StringView, pattern : StringView) -> Int32? {
#| guard pattern.length() <= JS_INDEX_OF_MAX_PATTERN else {
#| return find_by_two_anchors(target, pattern)
#| }
#| let target_start = target.start_offset()
#| let pattern_start = pattern.start_offset()
#| let found = js_index_of(
#| target.data(),
#| target_start,
#| target_start + target.length(),
#| js_substring(
#| pattern.data(),
#| pattern_start,
#| pattern_start + pattern.length(),
#| ),
#| )
#| if found < 0 {
#| None
#| } else {
#| Some(found - target_start)
#| }
#|}
),
"string_join.mbt": (
#|#cfg(not(target="js"))
#|fn join_length_add(length : Int32, extra : Int32) -> Int32 {
#| guard extra <= 0x7FFFFFFF - length else {
#| abort("String join length overflow")
#| }
#| length + extra
#|}
#|#cfg(not(target="js"))
#|fn[A : ToStringView] join_string_views(
#| parts : ArrayView[A],
#| separator : StringView,
#|) -> String {
#| match parts {
#| [] => ""
#| [one] => one.to_string_view().to_owned()
#| [first, .. rest] => {
#| let first = first.to_string_view()
#| let length = for part in rest; length = first.length() {
#| continue join_length_add(
#| join_length_add(length, separator.length()),
#| part.to_string_view().length(),
#| )
#| } nobreak {
#| length
#| }
#| if length == 0 {
#| return ""
#| }
#| let output : UninitializedArray[UInt16] = UninitializedArray::make(length)
#| output.unsafe_blit_from_string_uninit(
#| 0,
#| first.data(),
#| first.start_offset(),
#| first.length(),
#| )
#| for part in rest; offset = first.length() {
#| let part = part.to_string_view()
#| output.unsafe_blit_from_string_uninit(
#| offset,
#| separator.data(),
#| separator.start_offset(),
#| separator.length(),
#| )
#| let offset = offset + separator.length()
#| output.unsafe_blit_from_string_uninit(
#| offset,
#| part.data(),
#| part.start_offset(),
#| part.length(),
#| )
#| continue offset + part.length()
#| }
#| unsafe_fixedarray_uint16_to_string(
#| unsafe_uint16_buffer_as_fixedarray(output),
#| )
#| }
#| }
#|}
#|#cfg(target="js")
#|fn[A : ToStringView] join_string_views(
#| parts : ArrayView[A],
#| separator : StringView,
#|) -> String {
#| match parts {
#| [] => ""
#| [one] => one.to_string_view().to_owned()
#| [first, .. rest] => {
#| let separator = separator.to_owned()
#| let mut result = first.to_string_view().to_owned()
#| for part in rest {
#| result += separator
#| result += part.to_string_view().to_owned()
#| }
#| result
#| }
#| }
#|}
),
"string_like.mbt": (
#|pub trait ToStringView {
#| fn to_string_view(Self) -> StringView
#|}
#|pub impl ToStringView for String with fn to_string_view(self) -> StringView {
#| self
#|}
#|pub impl ToStringView for StringView with fn to_string_view(self) -> StringView {
#| self
#|}
),
"string_methods.mbt": (
#|pub fn StringView::find(self : StringView, str : StringView) -> Int32? {
#| let pattern_len = str.length()
#| match pattern_len {
#| 0 => Some(0)
#| 1 => self.find_code_unit(str.unsafe_get(0))
#| _ =>
#| if pattern_len > self.length() {
#| None
#| } else {
#| find_pattern(self, str)
#| }
#| }
#|}
#|pub fn String::find(self : String, str : StringView) -> Int32? {
#| self[:].find(str)
#|}
#|pub fn StringView::find_code_unit(self : StringView, code : UInt16) -> Int32? {
#| let found = find_code_unit_from_view(self, 0, self.length(), code)
#| if found < 0 {
#| None
#| } else {
#| Some(found)
#| }
#|}
#|pub fn String::find_code_unit(self : String, code : UInt16) -> Int32? {
#| self[:].find_code_unit(code)
#|}
#|test "find" {
#| assert_true("hello".find("o") == Some(4))
#| assert_true("hello".find("l") == Some(2))
#| assert_true("hello".find("hello") == Some(0))
#| assert_true("hello".find("h") == Some(0))
#| assert_true("hello".find("") == Some(0))
#| assert_true("hello".find("world") == None)
#| assert_true("".find("") == Some(0))
#| assert_true("".find("a") == None)
#| assert_true("hello hello".find("hello") == Some(0))
#| assert_true("aaa".find("aa") == Some(0))
#| assert_true("😀😀".find("😀") == Some(0))
#| assert_true(
#| ("😀😀aa".repeat(20) + "😀😀😀😀").find("😀😀😀😀") ==
#| Some(120),
#| )
#| assert_true(
#| ("😀😀😀😀" + "😀😀aa".repeat(20)).find("😀😀😀😀") ==
#| Some(0),
#| )
#|}
#|#locals(pred)
#|pub fn StringView::find_by(
#| self : StringView,
#| pred : (Char) -> Bool raise?,
#|) -> Int32? raise? {
#| for c in self; offset = 0 {
#| if pred(c) {
#| break Some(offset)
#| }
#| continue offset + c.utf16_len()
#| } nobreak {
#| None
#| }
#|}
#|pub fn String::find_by(
#| self : String,
#| pred : (Char) -> Bool raise?,
#|) -> Int32? raise? {
#| self[:].find_by(pred)
#|}
#|test "find_by" {
#| assert_true("hello".find_by(c => c == 'o') == Some(4))
#| assert_true("hello".find_by(c => c == 'l') == Some(2))
#| assert_true("hello".find_by(c => c == 'z') == None)
#| assert_true("".find_by(c => c == 'a') == None)
#| assert_true("hello".find_by(c => c is ('0'..<='9')) == None)
#| assert_true("hello123".find_by(c => c is ('0'..<='9')) == Some(5))
#| assert_true("hello".find_by(c => c is ('A'..<='Z')) == None)
#| assert_true("Hello".find_by(c => c is ('A'..<='Z')) == Some(0))
#| assert_true("αβγ".find_by(c => c == 'β') == Some(1))
#|}
#|test "find_by returns UTF-16 code-unit offsets" {
#| assert_true("😀X".find_by(c => c == 'X') == Some(2))
#| assert_true("😀😁😂".find_by(c => c == '😂') == Some(4))
#| let view = "a😀Xz"[1:4]
#| assert_true(view.find_by(c => c == 'X') == Some(2))
#|}
#|pub fn StringView::rev_find(self : StringView, str : StringView) -> Int32? {
#| let pattern_len = str.length()
#| match pattern_len {
#| 0 => Some(self.length())
#| 1 => self.rev_find_code_unit(str.unsafe_get(0))
#| _ =>
#| if pattern_len > self.length() {
#| None
#| } else {
#| rev_find_by_two_anchors(self, str)
#| }
#| }
#|}
#|pub fn String::rev_find(self : String, str : StringView) -> Int32? {
#| self[:].rev_find(str)
#|}
#|pub fn StringView::rev_find_code_unit(
#| self : StringView,
#| code : UInt16,
#|) -> Int32? {
#| let found = rev_find_code_unit_from_view(self, 0, self.length(), code)
#| if found < 0 {
#| None
#| } else {
#| Some(found)
#| }
#|}
#|pub fn String::rev_find_code_unit(self : String, code : UInt16) -> Int32? {
#| self[:].rev_find_code_unit(code)
#|}
#|test "rev_find" {
#| assert_true("hello".rev_find("o") == Some(4))
#| assert_true("hello".rev_find("l") == Some(3))
#| assert_true("hello".rev_find("hello") == Some(0))
#| assert_true("hello".rev_find("h") == Some(0))
#| assert_true("hello".rev_find("") == Some(5))
#| assert_true("hello".rev_find("world") == None)
#| assert_true("".rev_find("") == Some(0))
#| assert_true("".rev_find("a") == None)
#| assert_true("hello hello".rev_find("hello") == Some(6))
#| assert_true("aaa".rev_find("aa") == Some(1))
#| assert_true("😀😀".rev_find("😀") == Some(2))
#| assert_true(
#| ("😀😀aa".repeat(20) + "😀😀😀😀").rev_find("😀😀😀😀") ==
#| Some(120),
#| )
#| assert_true(
#| ("😀😀😀😀" + "😀😀aa".repeat(20)).rev_find("😀😀😀😀") ==
#| Some(4),
#| )
#|}
#|#alias(ends_with, deprecated)
#|pub fn StringView::has_suffix(self : StringView, str : StringView) -> Bool {
#| let self_len = self.length()
#| let str_len = str.length()
#| guard str_len <= self_len else { return false }
#| let start = self_len - str_len
#| guard str_len == 0 || self.unsafe_get(start) == str.unsafe_get(0) else {
#| return false
#| }
#| self
#| .str()
#| .unsafe_range_equal(
#| str.str(),
#| start=self.start() + start,
#| other_start=str.start(),
#| len=str_len,
#| )
#|}
#|#alias(ends_with, deprecated)
#|pub fn String::has_suffix(self : String, str : StringView) -> Bool {
#| self[:].has_suffix(str)
#|}
#|test "has_suffix" {
#| inspect("hello".has_suffix("lo"), content="true")
#| inspect("hello".has_suffix("hello"), content="true")
#| inspect("hello".has_suffix(""), content="true")
#| inspect("hello".has_suffix("world"), content="false")
#| inspect("hello".has_suffix("hel"), content="false")
#| inspect("".has_suffix(""), content="true")
#| inspect("".has_suffix("a"), content="false")
#| inspect("hello world".has_suffix("world"), content="true")
#| inspect("😀😀".has_suffix("😀"), content="true")
#| inspect("😀😀".has_suffix("😀😀"), content="true")
#|}
#|#alias(starts_with, deprecated)
#|pub fn StringView::has_prefix(self : StringView, str : StringView) -> Bool {
#| let str_len = str.length()
#| guard str_len <= self.length() else { return false }
#| guard str_len == 0 || self.unsafe_get(0) == str.unsafe_get(0) else {
#| return false
#| }
#| self
#| .str()
#| .unsafe_range_equal(
#| str.str(),
#| start=self.start(),
#| other_start=str.start(),
#| len=str_len,
#| )
#|}
#|#alias(starts_with, deprecated)
#|pub fn String::has_prefix(self : String, str : StringView) -> Bool {
#| self[:].has_prefix(str)
#|}
#|test "has_prefix" {
#| inspect("hello".has_prefix("h"), content="true")
#| inspect("hello".has_prefix("he"), content="true")
#| inspect("hello".has_prefix(""), content="true")
#| inspect("hello".has_prefix("world"), content="false")
#| inspect("hello".has_prefix("lo"), content="false")
#| inspect("".has_prefix(""), content="true")
#| inspect("".has_prefix("a"), content="false")
#| inspect("😀hello".has_prefix("😀"), content="true")
#| inspect("😀😃hello".has_prefix("😀😃"), content="true")
#| inspect("😀hello".has_prefix("😃"), content="false")
#| inspect("hello😀".has_prefix("😀"), content="false")
#|}
#|test "StringView has_prefix and has_suffix" {
#| let view = "x😀hello worldy"[1:14]
#| assert_true(view.has_prefix("😀hello"))
#| assert_false(view.has_prefix("world"))
#| assert_false(view.has_prefix("😀hello world!"))
#| assert_true(view.has_suffix("world"))
#| assert_false(view.has_suffix("😀hello"))
#| assert_false(view.has_suffix("😀hello world!"))
#|}
#|#alias(chop_suffix, deprecated="Use `strip_suffix` instead")
#|pub fn String::strip_suffix(self : String, suffix : StringView) -> StringView? {
#| self[:].strip_suffix(suffix)
#|}
#|test "strip_prefix" {
#| assert_true("hello world".strip_prefix("hello ") == Some("world"))
#| assert_true("hello world".strip_prefix("hi ") == None)
#| assert_true("hello".strip_prefix("hello") == Some(""))
#| assert_true("".strip_prefix("") == Some(""))
#| assert_true("".strip_prefix("a") == None)
#| assert_true("abc".strip_prefix("") == Some("abc"))
#| assert_true("😀hello".strip_prefix("😀") == Some("hello"))
#| assert_true("😀😃hello".strip_prefix("😀😃") == Some("hello"))
#|}
#|test "strip_suffix" {
#| assert_true("hello world".strip_suffix(" world") == Some("hello"))
#| assert_true("hello world".strip_suffix(" moon") == None)
#| assert_true("hello".strip_suffix("hello") == Some(""))
#| assert_true("".strip_suffix("") == Some(""))
#| assert_true("".strip_suffix("a") == None)
#| assert_true("abc".strip_suffix("") == Some("abc"))
#| assert_true("hello😀".strip_suffix("😀") == Some("hello"))
#| assert_true("hello😀😃".strip_suffix("😀😃") == Some("hello"))
#|}
#|#alias(chop_prefix, deprecated="Use `strip_prefix` instead")
#|pub fn String::strip_prefix(self : String, prefix : StringView) -> StringView? {
#| self[:].strip_prefix(prefix)
#|}
#|#alias(chop_prefix, deprecated="Use `strip_prefix` instead")
#|pub fn StringView::strip_prefix(
#| self : StringView,
#| prefix : StringView,
#|) -> StringView? {
#| let prefix_len = prefix.length()
#| if self.length() >= prefix_len &&
#| self.unsafe_view(start=0, end=prefix_len) == prefix {
#| Some(self.unsafe_view(start=prefix_len, end=self.length()))
#| } else {
#| None
#| }
#|}
#|#alias(chop_suffix, deprecated="Use `strip_suffix` instead")
#|pub fn StringView::strip_suffix(
#| self : StringView,
#| suffix : StringView,
#|) -> StringView? {
#| let self_len = self.length()
#| let suffix_len = suffix.length()
#| if self_len >= suffix_len &&
#| self.unsafe_view(start=self_len - suffix_len, end=self.length()) == suffix {
#| Some(self.unsafe_view(start=0, end=self_len - suffix_len))
#| } else {
#| None
#| }
#|}
#|pub fn StringView::to_array(self : StringView) -> Array[Char] {
#| let chars : Array[Char] = Array(capacity=self.length())
#| for c in self {
#| chars.push(c)
#| }
#| chars
#|}
#|#deprecated("Check `@encoding/utf8.encode`")
#|#coverage.skip
#|pub fn StringView::to_bytes(self : StringView) -> Bytes {
#| let array = FixedArray::make(self.length() * 2, b'\x00')
#| array.blit_from_string(0, self.data(), self.start_offset(), self.length())
#| array |> unsafe_to_bytes
#|}
#|test "View::strip_prefix" {
#| let view = "hello world"[:]
#| assert_true(view.strip_prefix("hello ") == Some("world"))
#| assert_true(view.strip_prefix("hi ") == None)
#| assert_true(view.strip_prefix("hello world") == Some(""))
#| assert_true(view.strip_prefix("") == Some("hello world"))
#| let empty_view = ""[:]
#| assert_true(empty_view.strip_prefix("") == Some(""))
#| assert_true(empty_view.strip_prefix("a") == None)
#| let unicode_view = "😀hello😃"[:]
#| assert_true(unicode_view.strip_prefix("😀") == Some("hello😃"))
#| assert_true(unicode_view.strip_prefix("😃") == None)
#|}
#|test "View::strip_suffix" {
#| let view = "hello world"[:]
#| assert_true(view.strip_suffix(" world") == Some("hello"))
#| assert_true(view.strip_suffix(" moon") == None)
#| assert_true(view.strip_suffix("hello world") == Some(""))
#| assert_true(view.strip_suffix("") == Some("hello world"))
#| let empty_view = ""[:]
#| assert_true(empty_view.strip_suffix("") == Some(""))
#| assert_true(empty_view.strip_suffix("a") == None)
#| let unicode_view = "😀hello😃"[:]
#| assert_true(unicode_view.strip_suffix("😃") == Some("😀hello"))
#| assert_true(unicode_view.strip_suffix("😀") == None)
#|}
#|test "View::to_array" {
#| let view = "Hello🤣"[:]
#| let chars = view.to_array()
#| assert_true(chars == ['H', 'e', 'l', 'l', 'o', '🤣'])
#| let empty_view = ""[:]
#| let empty_chars = empty_view.to_array()
#| assert_true(empty_chars == [])
#| let sub_view = "Hello World"[6:11] // "World"
#| let sub_chars = sub_view.to_array()
#| assert_true(sub_chars == ['W', 'o', 'r', 'l', 'd'])
#|}
#|pub fn StringView::contains_code_unit(self : StringView, code : UInt16) -> Bool {
#| string_contains_code_unit(self.str(), self.start(), self.end(), code)
#|}
#|#inline
#|fn string_contains_code_unit_scalar(
#| str : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Bool {
#| for i in start.. Bool {
#| string_contains_code_unit_scalar(str, start, end, code)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_contains_code_unit(
#| str : String,
#| start : Int32,
#| end : Int32,
#| code : UInt16,
#|) -> Bool {
#| guard start + 8 <= end else {
#| return string_contains_code_unit_scalar(str, start, end, code)
#| }
#| let needle = i16x8_splat(code)
#| let pos = for pos = start; end - pos >= 16; {
#| let lo = i16x8_eq(v128_load_i16x8(str, pos), needle)
#| let hi = i16x8_eq(v128_load_i16x8(str, pos + 8), needle)
#| if v128_any_true(v128_or(lo, hi)) {
#| return true
#| }
#| continue pos + 16
#| } nobreak {
#| pos
#| }
#| let tail_start = if end - pos >= 8 {
#| if v128_any_true(i16x8_eq(v128_load_i16x8(str, pos), needle)) {
#| return true
#| }
#| pos + 8
#| } else {
#| pos
#| }
#| string_contains_code_unit_scalar(str, tail_start, end, code)
#|}
#|pub fn StringView::contains(self : StringView, str : StringView) -> Bool {
#| match str.length() {
#| 0 => true
#| 1 => self.contains_code_unit(str.unsafe_get(0))
#| _ => self.find(str) is Some(_)
#| }
#|}
#|pub fn String::contains(self : String, str : StringView) -> Bool {
#| self[:].contains(str)
#|}
#|pub fn String::contains_code_unit(self : String, code : UInt16) -> Bool {
#| string_contains_code_unit(self, 0, self.length(), code)
#|}
#|const ASCII_CHAR_SET_LIMIT : UInt32 = 128U
#|const ASCII_CHAR_SET_WORD_MASK : UInt32 = 31U
#|const ASCII_CHAR_SET_WORD_SHIFT = 5
#|#cfg(any(target="native", target="wasm"))
#|const ASCII_CHAR_SET_SIMD_MAX_CHARS = 8
#|#inline
#|fn build_ascii_char_set(
#| chars : StringView,
#|) -> (UInt32, UInt32, UInt32, UInt32)? {
#| let mut bits0 = 0U
#| let mut bits1 = 0U
#| let mut bits2 = 0U
#| let mut bits3 = 0U
#| for c in chars {
#| let code = c.to_uint()
#| guard code < ASCII_CHAR_SET_LIMIT else { return None }
#| let bit = 1U << (code & ASCII_CHAR_SET_WORD_MASK).reinterpret_as_int()
#| match code >> ASCII_CHAR_SET_WORD_SHIFT {
#| 0 => bits0 = bits0 | bit
#| 1 => bits1 = bits1 | bit
#| 2 => bits2 = bits2 | bit
#| _ => bits3 = bits3 | bit
#| }
#| }
#| Some((bits0, bits1, bits2, bits3))
#|}
#|#inline
#|fn ascii_char_set_contains(
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#| code : UInt32,
#|) -> Bool {
#| guard code < ASCII_CHAR_SET_LIMIT else { return false }
#| let bit = 1U << (code & ASCII_CHAR_SET_WORD_MASK).reinterpret_as_int()
#| match code >> ASCII_CHAR_SET_WORD_SHIFT {
#| 0 => (bits0 & bit) != 0U
#| 1 => (bits1 & bit) != 0U
#| 2 => (bits2 & bit) != 0U
#| _ => (bits3 & bit) != 0U
#| }
#|}
#|#cfg(not(target="js"))
#|#inline
#|fn string_contains_any_ascii_scalar(
#| str : String,
#| start : Int32,
#| end : Int32,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Bool {
#| for i in start.. Int32 {
#| for pos = start {
#| if pos < end &&
#| ascii_char_set_contains(
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| str.unsafe_get(pos).to_uint(),
#| ) {
#| continue pos + 1
#| } else {
#| break pos
#| }
#| }
#|}
#|#inline
#|fn string_trim_end_ascii_scalar(
#| str : String,
#| start : Int32,
#| end : Int32,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Int32 {
#| for pos = end {
#| if pos > start &&
#| ascii_char_set_contains(
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| str.unsafe_get(pos - 1).to_uint(),
#| ) {
#| continue pos - 1
#| } else {
#| break pos
#| }
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn ascii_char_set_splat(
#| chars : StringView,
#| count : Int32,
#| index : Int32,
#|) -> V128 {
#| i16x8_splat(chars.unsafe_get(if index < count { index } else { 0 }))
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn ascii_char_set_block_mask(
#| block : V128,
#| s0 : V128,
#| s1 : V128,
#| s2 : V128,
#| s3 : V128,
#| s4 : V128,
#| s5 : V128,
#| s6 : V128,
#| s7 : V128,
#|) -> V128 {
#| v128_or(
#| v128_or(
#| v128_or(i16x8_eq(block, s0), i16x8_eq(block, s1)),
#| v128_or(i16x8_eq(block, s2), i16x8_eq(block, s3)),
#| ),
#| v128_or(
#| v128_or(i16x8_eq(block, s4), i16x8_eq(block, s5)),
#| v128_or(i16x8_eq(block, s6), i16x8_eq(block, s7)),
#| ),
#| )
#|}
#|#cfg(target="js")
#|fn string_contains_any_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| _chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Bool {
#| str
#| .unsafe_view(start~, end~)
#| .contains_any_ascii_chars(bits0, bits1, bits2, bits3)
#|}
#|#cfg(target="js")
#|fn StringView::contains_any_ascii_chars(
#| self : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Bool {
#| for c in self {
#| if ascii_char_set_contains(bits0, bits1, bits2, bits3, c.to_uint()) {
#| return true
#| }
#| }
#| false
#|}
#|#cfg(not(any(target="native", target="wasm", target="js")))
#|fn string_contains_any_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| _chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Bool {
#| string_contains_any_ascii_scalar(str, start, end, bits0, bits1, bits2, bits3)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_contains_any_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Bool {
#| let count = chars.length()
#| guard count >= 1 && count <= ASCII_CHAR_SET_SIMD_MAX_CHARS && start + 8 <= end else {
#| return string_contains_any_ascii_scalar(
#| str, start, end, bits0, bits1, bits2, bits3,
#| )
#| }
#| let s0 = ascii_char_set_splat(chars, count, 0)
#| let s1 = ascii_char_set_splat(chars, count, 1)
#| let s2 = ascii_char_set_splat(chars, count, 2)
#| let s3 = ascii_char_set_splat(chars, count, 3)
#| let s4 = ascii_char_set_splat(chars, count, 4)
#| let s5 = ascii_char_set_splat(chars, count, 5)
#| let s6 = ascii_char_set_splat(chars, count, 6)
#| let s7 = ascii_char_set_splat(chars, count, 7)
#| let tail_start = for pos = start; pos + 8 <= end; {
#| let block = v128_load_i16x8(str, pos)
#| if v128_any_true(
#| ascii_char_set_block_mask(block, s0, s1, s2, s3, s4, s5, s6, s7),
#| ) {
#| return true
#| }
#| continue pos + 8
#| } nobreak {
#| pos
#| }
#| string_contains_any_ascii_scalar(
#| str, tail_start, end, bits0, bits1, bits2, bits3,
#| )
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn string_trim_start_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| _chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Int32 {
#| string_trim_start_ascii_scalar(str, start, end, bits0, bits1, bits2, bits3)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_trim_start_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Int32 {
#| let count = chars.length()
#| guard count >= 1 && count <= ASCII_CHAR_SET_SIMD_MAX_CHARS && start + 8 <= end else {
#| return string_trim_start_ascii_scalar(
#| str, start, end, bits0, bits1, bits2, bits3,
#| )
#| }
#| let s0 = ascii_char_set_splat(chars, count, 0)
#| let s1 = ascii_char_set_splat(chars, count, 1)
#| let s2 = ascii_char_set_splat(chars, count, 2)
#| let s3 = ascii_char_set_splat(chars, count, 3)
#| let s4 = ascii_char_set_splat(chars, count, 4)
#| let s5 = ascii_char_set_splat(chars, count, 5)
#| let s6 = ascii_char_set_splat(chars, count, 6)
#| let s7 = ascii_char_set_splat(chars, count, 7)
#| let boundary = for pos = start; pos + 8 <= end; {
#| let block = v128_load_i16x8(str, pos)
#| let mask = ascii_char_set_block_mask(block, s0, s1, s2, s3, s4, s5, s6, s7)
#| if i16x8_bitmask(mask) != 0xFF {
#| break pos
#| }
#| continue pos + 8
#| } nobreak {
#| pos
#| }
#| string_trim_start_ascii_scalar(str, boundary, end, bits0, bits1, bits2, bits3)
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn string_trim_end_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| _chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Int32 {
#| string_trim_end_ascii_scalar(str, start, end, bits0, bits1, bits2, bits3)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn string_trim_end_ascii(
#| str : String,
#| start : Int32,
#| end : Int32,
#| chars : StringView,
#| bits0 : UInt32,
#| bits1 : UInt32,
#| bits2 : UInt32,
#| bits3 : UInt32,
#|) -> Int32 {
#| let count = chars.length()
#| guard count >= 1 && count <= ASCII_CHAR_SET_SIMD_MAX_CHARS && start + 8 <= end else {
#| return string_trim_end_ascii_scalar(
#| str, start, end, bits0, bits1, bits2, bits3,
#| )
#| }
#| let s0 = ascii_char_set_splat(chars, count, 0)
#| let s1 = ascii_char_set_splat(chars, count, 1)
#| let s2 = ascii_char_set_splat(chars, count, 2)
#| let s3 = ascii_char_set_splat(chars, count, 3)
#| let s4 = ascii_char_set_splat(chars, count, 4)
#| let s5 = ascii_char_set_splat(chars, count, 5)
#| let s6 = ascii_char_set_splat(chars, count, 6)
#| let s7 = ascii_char_set_splat(chars, count, 7)
#| let boundary = for pos = end; pos - 8 >= start; {
#| let block = v128_load_i16x8(str, pos - 8)
#| let mask = ascii_char_set_block_mask(block, s0, s1, s2, s3, s4, s5, s6, s7)
#| if i16x8_bitmask(mask) != 0xFF {
#| break pos
#| }
#| continue pos - 8
#| } nobreak {
#| pos
#| }
#| string_trim_end_ascii_scalar(str, start, boundary, bits0, bits1, bits2, bits3)
#|}
#|fn StringView::trim_start_with_chars(
#| self : StringView,
#| chars : StringView,
#|) -> StringView {
#| for x = self {
#| match x {
#| [] as v => break v
#| [c, .. rest] as v =>
#| if chars.contains_char(c) {
#| continue rest
#| } else {
#| break v
#| }
#| }
#| }
#|}
#|fn StringView::trim_end_with_chars(
#| self : StringView,
#| chars : StringView,
#|) -> StringView {
#| for x = self {
#| match x {
#| [] as v => break v
#| [.. rest, c] as v =>
#| if chars.contains_char(c) {
#| continue rest
#| } else {
#| break v
#| }
#| }
#| }
#|}
#|pub fn StringView::contains_any(self : StringView, chars~ : StringView) -> Bool {
#| match chars {
#| [] => false
#| [c] => self.contains_char(c) // specialize for single character
#| _ =>
#| match build_ascii_char_set(chars) {
#| Some((bits0, bits1, bits2, bits3)) =>
#| string_contains_any_ascii(
#| self.str(),
#| self.start(),
#| self.end(),
#| chars,
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| )
#| None =>
#| for c in self {
#| if chars.contains_char(c) {
#| break true
#| }
#| } nobreak {
#| false
#| }
#| }
#| }
#|}
#|pub fn String::contains_any(self : String, chars~ : StringView) -> Bool {
#| self[:].contains_any(chars~)
#|}
#|test "contains" {
#| inspect("hello".contains("o"), content="true")
#| inspect("hello".contains("l"), content="true")
#| inspect("hello".contains("hello"), content="true")
#| inspect("hello".contains("h"), content="true")
#| inspect("hello".contains(""), content="true")
#| inspect("hello".contains("world"), content="false")
#| inspect("".contains(""), content="true")
#| inspect("".contains("a"), content="false")
#| inspect("hello hello".contains("hello"), content="true")
#| inspect("aaa".contains("aa"), content="true")
#| inspect("😀😀".contains("😀"), content="true")
#| let leading_surrogate = String::from_array([(0xD800).unsafe_to_char()])
#| inspect(leading_surrogate.contains(leading_surrogate), content="true")
#|}
#|test "contains_code_unit" {
#| inspect("hello".contains_code_unit(('h' : UInt16)), content="true")
#| inspect("hello".contains_code_unit(('x' : UInt16)), content="false")
#| inspect("xhello"[1:].contains_code_unit(('h' : UInt16)), content="true")
#| inspect("xhello"[1:].contains_code_unit(('x' : UInt16)), content="false")
#| inspect("😀".contains_code_unit(0xD83D), content="true")
#| inspect("😀".contains_code_unit(0xDE00), content="true")
#| let leading_surrogate = String::from_array([(0xD800).unsafe_to_char()])
#| inspect(leading_surrogate.contains_code_unit(0xD800), content="true")
#|}
#|test "contains_code_unit SIMD blocks and view bounds" {
#| let str = "01234567abcdefghZ"
#| inspect(str.contains_code_unit(('0' : UInt16)), content="true")
#| inspect(str.contains_code_unit(('7' : UInt16)), content="true")
#| inspect(str.contains_code_unit(('a' : UInt16)), content="true")
#| inspect(str.contains_code_unit(('h' : UInt16)), content="true")
#| inspect(str.contains_code_unit(('Z' : UInt16)), content="true")
#| inspect(str.contains_code_unit(('x' : UInt16)), content="false")
#| let view = ("x" + str + "y")[1:18]
#| inspect(view.contains_code_unit(('x' : UInt16)), content="false")
#| inspect(view.contains_code_unit(('a' : UInt16)), content="true")
#| inspect(view.contains_code_unit(('Z' : UInt16)), content="true")
#| inspect(view.contains_code_unit(('y' : UInt16)), content="false")
#| let wide = "01234567abcd😀efgh"
#| inspect(wide.contains_code_unit(0xD83D), content="true")
#| inspect(wide.contains_code_unit(0xDE00), content="true")
#|}
#|test "find_code_unit" {
#| assert_true("hello".find_code_unit(('l' : UInt16)) == Some(2))
#| assert_true("hello".find_code_unit(('x' : UInt16)) == None)
#| assert_true("".find_code_unit(('x' : UInt16)) == None)
#| assert_true("xhellox"[1:6].find_code_unit(('l' : UInt16)) == Some(2))
#| assert_true("xhellox"[1:6].find_code_unit(('x' : UInt16)) == None)
#| assert_true("a😀b😀".find_code_unit(0xD83D) == Some(1))
#| assert_true("a😀b😀".find_code_unit(0xDE00) == Some(2))
#| let leading_surrogate = String::from_array([(0xD800).unsafe_to_char()])
#| assert_true(leading_surrogate.find_code_unit(0xD800) == Some(0))
#|}
#|test "rev_find_code_unit" {
#| assert_true("hello".rev_find_code_unit(('l' : UInt16)) == Some(3))
#| assert_true("hello".rev_find_code_unit(('x' : UInt16)) == None)
#| assert_true("".rev_find_code_unit(('x' : UInt16)) == None)
#| assert_true("xhellox"[1:6].rev_find_code_unit(('l' : UInt16)) == Some(3))
#| assert_true("xhellox"[1:6].rev_find_code_unit(('x' : UInt16)) == None)
#| assert_true("a😀b😀".rev_find_code_unit(0xD83D) == Some(4))
#| assert_true("a😀b😀".rev_find_code_unit(0xDE00) == Some(5))
#|}
#|test "find_code_unit SIMD blocks and view bounds" {
#| let str = "01234567abcdefghZ01234567abcdefghZ"
#| for i in 0..<17 {
#| let code = str.unsafe_get(i)
#| assert_true(str.find_code_unit(code) == Some(i))
#| assert_true(str.rev_find_code_unit(code) == Some(i + 17))
#| assert_true(str.find_code_unit(code) == str.find(str[i:i + 1]))
#| assert_true(str.rev_find_code_unit(code) == str.rev_find(str[i:i + 1]))
#| }
#| assert_true(str.find_code_unit(('x' : UInt16)) == None)
#| let view = ("x" + str + "y")[1:35]
#| assert_true(view.find_code_unit(('x' : UInt16)) == None)
#| assert_true(view.rev_find_code_unit(('y' : UInt16)) == None)
#| assert_true(view.find_code_unit(('Z' : UInt16)) == Some(16))
#| assert_true(view.rev_find_code_unit(('0' : UInt16)) == Some(17))
#| let wide = "01234567abcd😀efgh01234567abcd😀efgh"
#| assert_true(wide.find_code_unit(0xD83D) == Some(12))
#| assert_true(wide.rev_find_code_unit(0xDE00) == Some(31))
#|}
#|test "contains_any" {
#| inspect("hello".contains_any(chars="h"), content="true")
#| inspect("hello".contains_any(chars="xyz"), content="false")
#| inspect("hello".contains_any(chars=""), content="false")
#| inspect("".contains_any(chars="abc"), content="false")
#| inspect("😀😃".contains_any(chars="😄😀"), content="true")
#| inspect("hello"[:].contains_any(chars="eo"), content="true")
#|}
#|test "contains_any and trim ASCII character sets" {
#| assert_true("😀a".contains_any(chars="az"))
#| assert_false("😀".contains_any(chars="az"))
#| assert_true("😀".contains_any(chars="a😀"))
#| let view = "x hello \ty"[1:10]
#| assert_true(view.trim(chars=" \t") == "hello")
#|}
#|test "build ASCII character set" {
#| match build_ascii_char_set("a z") {
#| Some((bits0, bits1, bits2, bits3)) => {
#| assert_true(
#| ascii_char_set_contains(bits0, bits1, bits2, bits3, 'a'.to_uint()),
#| )
#| assert_true(
#| ascii_char_set_contains(bits0, bits1, bits2, bits3, 'z'.to_uint()),
#| )
#| assert_true(
#| ascii_char_set_contains(bits0, bits1, bits2, bits3, ' '.to_uint()),
#| )
#| assert_false(
#| ascii_char_set_contains(bits0, bits1, bits2, bits3, 'b'.to_uint()),
#| )
#| }
#| None => assert_false(true)
#| }
#| assert_true(build_ascii_char_set("a😀") is None)
#|}
#|pub fn StringView::contains_char(self : StringView, c : Char) -> Bool {
#| let len = self.length()
#| guard len > 0 else { return false }
#| let c = c.to_int()
#| if c >= 0 && c <= 0xFFFF {
#| return self.contains_code_unit(c.to_uint16())
#| } else if c < 0 {
#| return false
#| } else {
#| guard len >= 2 else { return false }
#| let adj = c - 0x10000
#| let high = 0xD800 + (adj >> 10)
#| guard high <= 0xFFFF else { return false }
#| let high = high.to_uint16()
#| let low = (0xDC00 + (adj & 0x3FF)).to_uint16()
#| for i = 0; i < len - 1; {
#| if self.unsafe_get(i) == high {
#| if self.unsafe_get(i + 1) == low {
#| return true
#| }
#| continue i + 2
#| }
#| continue i + 1
#| }
#| }
#| false
#|}
#|pub fn String::contains_char(self : String, c : Char) -> Bool {
#| self[:].contains_char(c)
#|}
#|test "contains_char" {
#| inspect("hello".contains_char('h'), content="true")
#| inspect("hello".contains_char('e'), content="true")
#| inspect("hello".contains_char('l'), content="true")
#| inspect("hello".contains_char('o'), content="true")
#| inspect("hello".contains_char('x'), content="false")
#| inspect("".contains_char('a'), content="false")
#| inspect("hello world".contains_char(' '), content="true")
#| inspect("hello world".contains_char('w'), content="true")
#| inspect("😀😀".contains_char('😀'), content="true")
#| inspect("😀😀".contains_char('😃'), content="false")
#| inspect("hello".contains_char('h'), content="true")
#| let leading_surrogate = String::from_array([(0xD800).unsafe_to_char()])
#| inspect(
#| leading_surrogate.contains_char((0xD800).unsafe_to_char()),
#| content="true",
#| )
#| let max_code_unit = String::from_array([(0xFFFF).unsafe_to_char()])
#| inspect(max_code_unit.contains_char((-1).unsafe_to_char()), content="false")
#|}
#|#label_migration(chars, alias=char_set)
#|pub fn StringView::trim_start(
#| self : StringView,
#| chars~ : StringView = "\t\n\r ",
#|) -> StringView {
#| match build_ascii_char_set(chars) {
#| Some((bits0, bits1, bits2, bits3)) => {
#| let start = string_trim_start_ascii(
#| self.str(),
#| self.start(),
#| self.end(),
#| chars,
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| )
#| self.str().unsafe_view(start~, end=self.end())
#| }
#| None => self.trim_start_with_chars(chars)
#| }
#|}
#|#label_migration(chars, alias=char_set)
#|pub fn String::trim_start(
#| self : String,
#| chars~ : StringView = "\t\n\r ",
#|) -> StringView {
#| self[:].trim_start(chars~)
#|}
#|test "trim_start" {
#| inspect("hello".trim_start(chars="h"), content="ello")
#| inspect("hello".trim_start(chars="he"), content="llo")
#| inspect("hello".trim_start(chars="eh"), content="llo")
#| inspect("hello".trim_start(chars="x"), content="hello")
#| inspect("hello".trim_start(chars=""), content="hello")
#| inspect("".trim_start(chars="a"), content="")
#| inspect(" hello".trim_start(chars=" "), content="hello")
#| inspect("hello world".trim_start(chars="helo"), content=" world")
#| inspect("😀😀hello".trim_start(chars="😀"), content="hello")
#| inspect("😀😃hello".trim_start(chars="😀😃"), content="hello")
#| inspect("aaaabc".trim_start(chars="a"), content="bc")
#| inspect("aaaa".trim_start(chars="a"), content="")
#|}
#|#label_migration(chars, alias=char_set)
#|pub fn StringView::trim_end(
#| self : StringView,
#| chars~ : StringView = "\t\n\r ",
#|) -> StringView {
#| match build_ascii_char_set(chars) {
#| Some((bits0, bits1, bits2, bits3)) => {
#| let end = string_trim_end_ascii(
#| self.str(),
#| self.start(),
#| self.end(),
#| chars,
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| )
#| self.str().unsafe_view(start=self.start(), end~)
#| }
#| None => self.trim_end_with_chars(chars)
#| }
#|}
#|#label_migration(chars, alias=char_set)
#|pub fn String::trim_end(
#| self : String,
#| chars~ : StringView = "\t\n\r ",
#|) -> StringView {
#| self[:].trim_end(chars~)
#|}
#|test "trim_end" {
#| inspect("hello".trim_end(chars="o"), content="hell")
#| inspect("hello".trim_end(chars="lo"), content="he")
#| inspect("hello".trim_end(chars="x"), content="hello")
#| inspect("hello".trim_end(chars=""), content="hello")
#| inspect("".trim_end(chars="a"), content="")
#| inspect("hello ".trim_end(chars=" "), content="hello")
#| inspect("hello world".trim_end(chars="dlrow "), content="he")
#| inspect("hello😀😀".trim_end(chars="😀"), content="hello")
#| inspect("hello😀😃".trim_end(chars="😀😃"), content="hello")
#| inspect("abcccc".trim_end(chars="c"), content="ab")
#| inspect("cccc".trim_end(chars="c"), content="")
#|}
#|#label_migration(chars, alias=char_set)
#|pub fn StringView::trim(
#| self : StringView,
#| chars~ : StringView = "\t\n\r ",
#|) -> StringView {
#| match build_ascii_char_set(chars) {
#| Some((bits0, bits1, bits2, bits3)) => {
#| let start = string_trim_start_ascii(
#| self.str(),
#| self.start(),
#| self.end(),
#| chars,
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| )
#| let end = string_trim_end_ascii(
#| self.str(),
#| start,
#| self.end(),
#| chars,
#| bits0,
#| bits1,
#| bits2,
#| bits3,
#| )
#| self.str().unsafe_view(start~, end~)
#| }
#| None => self.trim_start_with_chars(chars).trim_end_with_chars(chars)
#| }
#|}
#|#label_migration(chars, alias=char_set)
#|pub fn String::trim(
#| self : String,
#| chars~ : StringView = "\t\n\r ",
#|) -> StringView {
#| self[:].trim(chars~)
#|}
#|test "trim" {
#| inspect("hello".trim(chars="h"), content="ello")
#| inspect("hello".trim(chars="o"), content="hell")
#| inspect("hello".trim(chars="ho"), content="ell")
#| inspect("hello".trim(chars="oh"), content="ell")
#| inspect("hello".trim(chars="x"), content="hello")
#| inspect("hello".trim(chars=""), content="hello")
#| inspect("".trim(chars="a"), content="")
#| inspect(" hello ".trim(chars=" "), content="hello")
#| inspect("hello world".trim(chars="hd"), content="ello worl")
#| inspect("😀hello😀".trim(chars="😀"), content="hello")
#| inspect("😀😃hello😀😃".trim(chars="😀😃"), content="hello")
#| inspect("aaaabcaaa".trim(chars="a"), content="bc")
#| inspect("aaaa".trim(chars="a"), content="")
#| inspect(" hello world ".trim(chars=" "), content="hello world")
#| inspect("abcabc".trim(chars="abc"), content="")
#|}
#|#deprecated("Use `trim` with default whitespace characters instead")
#|pub fn StringView::trim_space(self : StringView) -> StringView {
#| self.trim()
#|}
#|#deprecated("Use `trim` with default whitespace characters instead")
#|pub fn String::trim_space(self : String) -> StringView {
#| self.trim()
#|}
#|test "trim whitespace for string" {
#| inspect("hello".trim(), content="hello")
#| inspect(" hello ".trim(), content="hello")
#| inspect("hello ".trim(), content="hello")
#| inspect(" hello".trim(), content="hello")
#| inspect("\t\nhello\r\n".trim(), content="hello")
#| inspect(" hello world ".trim(), content="hello world")
#| inspect(" ".trim(), content="")
#| inspect("\n\r\t".trim(), content="")
#| inspect("".trim(), content="")
#| inspect(" hello\nworld\t".trim(), content="hello\nworld")
#|}
#|pub fn StringView::is_empty(self : StringView) -> Bool {
#| self.length() == 0
#|}
#|pub fn String::is_empty(self : String) -> Bool {
#| self == ""
#|}
#|test "is_empty" {
#| inspect("".is_empty(), content="true")
#| inspect("hello".is_empty(), content="false")
#| inspect(" ".is_empty(), content="false")
#| inspect("\n".is_empty(), content="false")
#| inspect("\t".is_empty(), content="false")
#| inspect(" ".is_empty(), content="false")
#| let s = "hello"
#| let empty_view = s[0:0]
#| let non_empty_view = s[0:3]
#| inspect(empty_view.is_empty(), content="true")
#| inspect(non_empty_view.is_empty(), content="false")
#|}
#|pub fn StringView::is_blank(self : StringView) -> Bool {
#| self.trim().is_empty()
#|}
#|pub fn String::is_blank(self : String) -> Bool {
#| self[:].is_blank()
#|}
#|test "is_blank" {
#| inspect("".is_blank(), content="true")
#| inspect("hello".is_blank(), content="false")
#| inspect(" ".is_blank(), content="true")
#| inspect("\n".is_blank(), content="true")
#| inspect("\t".is_blank(), content="true")
#| inspect(" ".is_blank(), content="true")
#| inspect(" \n\t\r ".is_blank(), content="true")
#| inspect("hello world".is_blank(), content="false")
#| inspect(" hello ".is_blank(), content="false")
#| let s = " hello "
#| let blank_view = s[0:3] // " "
#| let non_blank_view = s[3:8] // "hello"
#| inspect(blank_view.is_blank(), content="true")
#| inspect(non_blank_view.is_blank(), content="false")
#|}
#|pub fn StringView::pad_start(
#| self : StringView,
#| total_width : Int32,
#| padding_char : Char,
#|) -> String {
#| let len = self.length()
#| guard len < total_width else { return self.to_owned() }
#| let deficit = total_width - len
#| let count = if padding_char.is_bmp() {
#| deficit
#| } else {
#| deficit - deficit / 2
#| }
#| let padding = String::make(count, padding_char)
#| [..padding, ..self]
#|}
#|pub fn String::pad_start(
#| self : String,
#| total_width : Int32,
#| padding_char : Char,
#|) -> String {
#| let len = self.length()
#| guard len < total_width else { return self }
#| let deficit = total_width - len
#| let count = if padding_char.is_bmp() {
#| deficit
#| } else {
#| deficit - deficit / 2
#| }
#| let padding = String::make(count, padding_char)
#| [..padding, ..self]
#|}
#|test "pad_start" {
#| inspect("2".pad_start(3, '0'), content="002")
#| inspect("abc".pad_start(5, 'x'), content="xxabc")
#| inspect("hello".pad_start(4, ' '), content="hello") // No padding needed
#| inspect("".pad_start(3, '-'), content="---")
#| inspect("test".pad_start(8, '*'), content="****test")
#| inspect("123".pad_start(6, '0'), content="000123")
#| let s = "hello"
#| let view = s[2:5] // "llo"
#| inspect(view.pad_start(5, 'x'), content="xxllo")
#| inspect("🌟".pad_start(3, '✨'), content="✨🌟")
#| inspect("".pad_start(2, '😀'), content="😀")
#| inspect("".pad_start(4, '😀').length(), content="4")
#| inspect("a".pad_start(3, '😀'), content="😀a")
#| inspect(view.pad_start(7, '😀'), content="😀😀llo")
#| inspect("".pad_start(3, '😀'), content="😀😀")
#| inspect("ab".pad_start(3, '😀').length(), content="4")
#| inspect("abc".pad_start(0, 'x'), content="abc") // width less than string length
#| inspect("abc".pad_start(3, 'x'), content="abc") // width equal to string length
#|}
#|pub fn StringView::pad_end(
#| self : StringView,
#| total_width : Int32,
#| padding_char : Char,
#|) -> String {
#| let len = self.length()
#| guard len < total_width else { return self.to_owned() }
#| let deficit = total_width - len
#| let count = if padding_char.is_bmp() {
#| deficit
#| } else {
#| deficit - deficit / 2
#| }
#| let padding = String::make(count, padding_char)
#| [..self, ..padding]
#|}
#|pub fn String::pad_end(
#| self : String,
#| total_width : Int32,
#| padding_char : Char,
#|) -> String {
#| let len = self.length()
#| guard len < total_width else { return self }
#| let deficit = total_width - len
#| let count = if padding_char.is_bmp() {
#| deficit
#| } else {
#| deficit - deficit / 2
#| }
#| let padding = String::make(count, padding_char)
#| [..self, ..padding]
#|}
#|test "pad_end" {
#| inspect("2".pad_end(3, '0'), content="200")
#| inspect("abc".pad_end(5, 'x'), content="abcxx")
#| inspect("hello".pad_end(4, ' '), content="hello") // No padding needed
#| inspect("".pad_end(3, '-'), content="---")
#| inspect("test".pad_end(8, '*'), content="test****")
#| inspect("123".pad_end(6, '0'), content="123000")
#| let s = "hello"
#| let view = s[2:5] // "llo"
#| inspect(view.pad_end(5, 'x'), content="lloxx")
#| inspect("🌟".pad_end(3, '✨'), content="🌟✨")
#| inspect("".pad_end(2, '😀'), content="😀")
#| inspect("".pad_end(4, '😀').length(), content="4")
#| inspect("a".pad_end(3, '😀'), content="a😀")
#| inspect(view.pad_end(7, '😀'), content="llo😀😀")
#| inspect("".pad_end(3, '😀'), content="😀😀")
#| inspect("ab".pad_end(3, '😀').length(), content="4")
#| inspect("abc".pad_end(0, 'x'), content="abc") // width less than string length
#| inspect("abc".pad_end(3, 'x'), content="abc") // width equal to string length
#|}
#|pub fn StringView::repeat(self : StringView, n : Int32) -> StringView {
#| match n {
#| _..<0 => abort("negative repeat count")
#| 0 => ""
#| 1 => self
#| _ => {
#| let len = self.length()
#| let total = len * n
#| guard len == 0 || total / n == len else {
#| abort("repeat result too large")
#| }
#| let buf = StringBuilder(capacity=total)
#| let str = self.to_owned()
#| for _ in 0.. String {
#| match n {
#| _..<0 => abort("negative repeat count")
#| 0 => ""
#| 1 => self
#| _ => {
#| let len = self.length()
#| let total = len * n
#| guard len == 0 || total / n == len else {
#| abort("repeat result too large")
#| }
#| let buf = StringBuilder(capacity=total)
#| let str = self.to_string()
#| for _ in 0.. String {
#| let buf = StringBuilder(capacity=self.length())
#| for c in self.rev_iter() {
#| buf.write_char(c)
#| }
#| buf.to_string()
#|}
#|pub fn String::rev(self : String) -> String {
#| self[:].rev()
#|}
#|test "rev" {
#| inspect("hello".rev(), content="olleh")
#| inspect("".rev(), content="")
#| inspect("abc".rev(), content="cba")
#| inspect("😀😃".rev(), content="😃😀")
#|}
#|pub fn StringView::split(
#| self : StringView,
#| sep : StringView,
#|) -> Iter[StringView] {
#| let sep_len = sep.length()
#| if sep_len == 0 {
#| return self.iter().map(c => c.to_string())
#| }
#| let mut remaining = Some(self)
#| Iter(() => {
#| guard remaining is Some(view) else { None }
#| guard view.find(sep) is Some(end) else {
#| remaining = None
#| Some(view)
#| }
#| remaining = Some(view.unsafe_view(start=end + sep_len, end=view.length()))
#| Some(view.unsafe_view(start=0, end~))
#| })
#|}
#|pub fn String::split(self : String, sep : StringView) -> Iter[StringView] {
#| self[:].split(sep)
#|}
#|pub fn StringView::split_once(
#| self : StringView,
#| needle : StringView,
#|) -> (StringView, StringView)? {
#| match self.find(needle) {
#| Some(index) =>
#| Some(
#| (
#| self.unsafe_view(start=0, end=index),
#| self.unsafe_view(start=index + needle.length(), end=self.length()),
#| ),
#| )
#| None => None
#| }
#|}
#|pub fn String::split_once(
#| self : String,
#| needle : StringView,
#|) -> (StringView, StringView)? {
#| self[:].split_once(needle)
#|}
#|pub fn StringView::before(
#| self : StringView,
#| needle : StringView,
#|) -> StringView? {
#| match self.find(needle) {
#| Some(index) => Some(self.unsafe_view(start=0, end=index))
#| None => None
#| }
#|}
#|pub fn String::before(self : String, needle : StringView) -> StringView? {
#| self[:].before(needle)
#|}
#|pub fn StringView::after(self : StringView, needle : StringView) -> StringView? {
#| match self.find(needle) {
#| Some(index) =>
#| Some(self.unsafe_view(start=index + needle.length(), end=self.length()))
#| None => None
#| }
#|}
#|pub fn String::after(self : String, needle : StringView) -> StringView? {
#| self[:].after(needle)
#|}
#|pub fn StringView::rev_split_once(
#| self : StringView,
#| needle : StringView,
#|) -> (StringView, StringView)? {
#| match self.rev_find(needle) {
#| Some(index) =>
#| Some(
#| (
#| self.unsafe_view(start=0, end=index),
#| self.unsafe_view(start=index + needle.length(), end=self.length()),
#| ),
#| )
#| None => None
#| }
#|}
#|pub fn String::rev_split_once(
#| self : String,
#| needle : StringView,
#|) -> (StringView, StringView)? {
#| self[:].rev_split_once(needle)
#|}
#|pub fn StringView::rev_before(
#| self : StringView,
#| needle : StringView,
#|) -> StringView? {
#| match self.rev_find(needle) {
#| Some(index) => Some(self.unsafe_view(start=0, end=index))
#| None => None
#| }
#|}
#|pub fn String::rev_before(self : String, needle : StringView) -> StringView? {
#| self[:].rev_before(needle)
#|}
#|pub fn StringView::rev_after(
#| self : StringView,
#| needle : StringView,
#|) -> StringView? {
#| match self.rev_find(needle) {
#| Some(index) =>
#| Some(self.unsafe_view(start=index + needle.length(), end=self.length()))
#| None => None
#| }
#|}
#|pub fn String::rev_after(self : String, needle : StringView) -> StringView? {
#| self[:].rev_after(needle)
#|}
#|test "split" {
#| assert_true(
#| "a,b,c".split(",").map(x => x.to_owned()).collect() == ["a", "b", "c"],
#| )
#| assert_true(
#| "a,b,c".split("").map(x => x.to_owned()).collect() ==
#| ["a", ",", "b", ",", "c"],
#| )
#| assert_true(
#| "apple::orange::banana".split("::").map(x => x.to_owned()).collect() ==
#| ["apple", "orange", "banana"],
#| )
#| assert_true(
#| "abc".split("").map(x => x.to_owned()).collect() == ["a", "b", "c"],
#| )
#| assert_true("hello".split(",").map(x => x.to_owned()).collect() == ["hello"])
#| assert_true(
#| ",a,b,c".split(",").map(x => x.to_owned()).collect() == ["", "a", "b", "c"],
#| )
#| assert_true(
#| "a,b,c,".split(",").map(x => x.to_owned()).collect() == ["a", "b", "c", ""],
#| )
#| assert_true(
#| "a,b,c".split("").map(x => x.to_owned()).collect() ==
#| ["a", ",", "b", ",", "c"],
#| )
#| assert_true("".split("").map(x => x.to_owned()).collect() == [])
#| assert_true("".split(",").map(x => x.to_owned()).collect() == [""])
#| assert_true(
#| "😀,😃,😄".split(",").map(x => x.to_owned()).collect() ==
#| ["😀", "😃", "😄"],
#| )
#| assert_true(
#| "a😀b😀c".split("😀").map(x => x.to_owned()).collect() ==
#| ["a", "b", "c"],
#| )
#|}
#|test "split_once before after" {
#| assert_true("a=b=c".split_once("=") == Some(("a", "b=c")))
#| assert_true("a=b=c".before("=") == Some("a"))
#| assert_true("a=b=c".after("=") == Some("b=c"))
#| assert_true("nope".split_once("=") == None)
#| assert_true("nope".before("=") == None)
#| assert_true("nope".after("=") == None)
#| assert_true("".split_once("") == Some(("", "")))
#| assert_true("".before("") == Some(""))
#| assert_true("".after("") == Some(""))
#| assert_true("hello".split_once("") == Some(("", "hello")))
#| assert_true("hello".before("") == Some(""))
#| assert_true("hello".after("") == Some("hello"))
#| assert_true("😊::🚀".split_once("::") == Some(("😊", "🚀")))
#|}
#|test "rev_split_once rev_before rev_after" {
#| assert_true("a=b=c".rev_split_once("=") == Some(("a=b", "c")))
#| assert_true("a=b=c".rev_before("=") == Some("a=b"))
#| assert_true("a=b=c".rev_after("=") == Some("c"))
#| assert_true("nope".rev_split_once("=") == None)
#| assert_true("nope".rev_before("=") == None)
#| assert_true("nope".rev_after("=") == None)
#| assert_true("a/b/c.txt".rev_before("/") == Some("a/b"))
#| assert_true("a/b/c.txt".rev_after("/") == Some("c.txt"))
#| assert_true("one=two".rev_split_once("=") == Some(("one", "two")))
#| assert_true(
#| "😊::🚀::⭐".rev_split_once("::") == Some(("😊::🚀", "⭐")),
#| )
#|}
#|pub fn StringView::replace(
#| self : StringView,
#| old~ : StringView,
#| new~ : StringView,
#|) -> StringView {
#| match self.find(old) {
#| Some(end) =>
#| [
#| ..self.unsafe_view(start=0, end~),
#| ..new,
#| ..self.unsafe_view(start=end + old.length(), end=self.length()),
#| ]
#| None => self
#| }
#|}
#|pub fn String::replace(
#| self : String,
#| old~ : StringView,
#| new~ : StringView,
#|) -> String {
#| match self.find(old) {
#| Some(end) =>
#| [
#| ..self.unsafe_view(start=0, end~),
#| ..new,
#| ..self.unsafe_view(start=end + old.length(), end=self.length()),
#| ]
#| None => self
#| }
#|}
#|test "replace" {
#| inspect("hello".replace(old="o", new="a"), content="hella")
#| inspect("hello".replace(old="l", new="a"), content="healo")
#| inspect("hello".replace(old="hello", new="a"), content="a")
#| inspect("hello".replace(old="h", new="a"), content="aello")
#| inspect("hello".replace(old="", new="a"), content="ahello")
#| inspect("hello".replace(old="world", new="a"), content="hello")
#| inspect("".replace(old="", new="a"), content="a")
#|}
#|pub fn StringView::replace_all(
#| self : StringView,
#| old~ : StringView,
#| new~ : StringView,
#|) -> StringView {
#| let len = self.length()
#| let buf = StringBuilder(capacity=len)
#| let old_len = old.length()
#| let new = new.to_owned()
#| if old_len == 0 {
#| buf.write_string(new)
#| for c in self {
#| buf.write_char(c)
#| buf.write_string(new)
#| }
#| buf.to_string()
#| } else {
#| let first_end = self.find(old)
#| if first_end is Some(end) {
#| for view = self, end = end {
#| let seg = view.unsafe_view(start=0, end~)
#| buf.write_substring(seg.data(), seg.start_offset(), seg.length())
#| buf.write_string(new)
#| guard end + old_len <= len else { break }
#| let next_view = view.unsafe_view(start=end + old_len, end=view.length())
#| guard next_view.find(old) is Some(next_end) else {
#| buf.write_substring(
#| next_view.data(),
#| next_view.start_offset(),
#| next_view.length(),
#| )
#| break
#| }
#| continue next_view, next_end
#| }
#| buf.to_string()
#| } else {
#| self
#| }
#| }
#|}
#|pub fn String::replace_all(
#| self : String,
#| old~ : StringView,
#| new~ : StringView,
#|) -> String {
#| let len = self.length()
#| let buf = StringBuilder(capacity=len)
#| let old_len = old.length()
#| let new = new.to_owned()
#| if old_len == 0 {
#| buf.write_string(new)
#| for c in self {
#| buf.write_char(c)
#| buf.write_string(new)
#| }
#| buf.to_string()
#| } else {
#| let first_end = self.find(old)
#| if first_end is Some(end) {
#| for view = self[:], end = end {
#| let seg = view.unsafe_view(start=0, end~)
#| buf.write_substring(seg.data(), seg.start_offset(), seg.length())
#| buf.write_string(new)
#| guard end + old_len <= len else { break }
#| let next_view = view.unsafe_view(start=end + old_len, end=view.length())
#| guard next_view.find(old) is Some(next_end) else {
#| buf.write_substring(
#| next_view.data(),
#| next_view.start_offset(),
#| next_view.length(),
#| )
#| break
#| }
#| continue next_view, next_end
#| }
#| buf.to_string()
#| } else {
#| self
#| }
#| }
#|}
#|test "replace_all" {
#| assert_true("hello".replace_all(old="o", new="a") == "hella")
#| assert_true("hello".replace_all(old="l", new="a") == "heaao")
#| assert_true("hello".replace_all(old="ll", new="rr") == "herro")
#| assert_true("hello".replace_all(old="hello", new="world") == "world")
#| assert_true(
#| "hello hello hello".replace_all(old="hello", new="hi") == "hi hi hi",
#| )
#| assert_true(
#| "hello hello helloi".replace_all(old="hello", new="hi") == "hi hi hii",
#| )
#| assert_true(
#| "hi hi hii".replace_all(old="hi", new="hello") == "hello hello helloi",
#| )
#| assert_true("hello".replace_all(old="", new="a") == "ahaealalaoa")
#| assert_true("hello".replace_all(old="world", new="a") == "hello")
#| assert_true("".replace_all(old="", new="a") == "a")
#| assert_true("aaa".replace_all(old="a", new="b") == "bbb")
#| assert_true("aaa".replace_all(old="a", new="bb") == "bbbbbb")
#| assert_true("aaa".replace_all(old="aa", new="b") == "ba")
#| assert_true(
#| "🤣🤣🤣".replace_all(old="🤣", new="😊") == "😊😊😊",
#| )
#| assert_true("abc123abc".replace_all(old="abc", new="xyz") == "xyz123xyz")
#| assert_true("abcabcabc".replace_all(old="abc", new="") == "")
#| assert_true("abc".replace_all(old="abc", new="") == "")
#| assert_true("abc".replace_all(old="", new="x") == "xaxbxcx")
#|}
#|test "String::replace_all boundary cases" {
#| assert_true("helloworld".replace_all(old="world", new="X") == "helloX")
#| assert_true("abcdef".replace_all(old="def", new="XYZ") == "abcXYZ")
#| assert_true("abcabc".replace_all(old="abc", new="X") == "XX")
#| assert_true("test".replace_all(old="test", new="done") == "done")
#| assert_true("remove_me".replace_all(old="_me", new="") == "remove")
#|}
#|test "View::replace_all" {
#| assert_true("hello"[:].replace_all(old="o", new="a") == "hella")
#| assert_true("hello"[:].replace_all(old="l", new="a") == "heaao")
#| assert_true("hello"[:].replace_all(old="ll", new="rr") == "herro")
#| assert_true("hello"[:].replace_all(old="hello", new="world") == "world")
#| assert_true(
#| "hello hello hello"[:].replace_all(old="hello", new="hi") == "hi hi hi",
#| )
#| assert_true(
#| "hello hello helloi"[:].replace_all(old="hello", new="hi") == "hi hi hii",
#| )
#| assert_true(
#| "hi hi hii"[:].replace_all(old="hi", new="hello") == "hello hello helloi",
#| )
#| assert_true("hello"[:].replace_all(old="", new="a") == "ahaealalaoa")
#| assert_true("hello"[:].replace_all(old="world", new="a") == "hello")
#| assert_true(""[:].replace_all(old="", new="a") == "a")
#| assert_true("aaa"[:].replace_all(old="a", new="b") == "bbb")
#| assert_true("aaa"[:].replace_all(old="a", new="bb") == "bbbbbb")
#| assert_true("aaa"[:].replace_all(old="aa", new="b") == "ba")
#| assert_true(
#| "🤣🤣🤣"[:].replace_all(old="🤣", new="😊") == "😊😊😊",
#| )
#| assert_true("abc123abc"[:].replace_all(old="abc", new="xyz") == "xyz123xyz")
#| assert_true("abcabcabc"[:].replace_all(old="abc", new="") == "")
#| assert_true("abc"[:].replace_all(old="abc", new="") == "")
#| assert_true("abc"[:].replace_all(old="", new="x") == "xaxbxcx")
#|}
#|test "View::replace_all boundary cases" {
#| assert_true("abcabc"[:].replace_all(old="abc", new="X") == "XX")
#| assert_true("aaaa"[:].replace_all(old="aa", new="b") == "bb")
#| assert_true("hello"[:].replace_all(old="lo", new="X") == "helX")
#| assert_true("a"[:].replace_all(old="a", new="") == "")
#| inspect("Testing boundary condition", content="Testing boundary condition")
#|}
#|#cfg(not(any(target="js", target="native", target="wasm")))
#|fn find_ascii_uppercase_code_unit(
#| data : String,
#| start : Int32,
#| len : Int32,
#|) -> Int32? {
#| let end = start + len
#| for i in start.. Int32? {
#| let end = start + len
#| for i in start.. Int32? {
#| let end = start + len
#| let code_unit = data.unsafe_get(start)
#| if code_unit >= first && code_unit <= last {
#| return Some(0)
#| }
#| let range_start = i16x8_splat(first)
#| let range_width = i16x8_splat(last - first)
#| let pos = for pos = start; end - pos >= 16; {
#| let lo = i16x8_le_u(
#| i16x8_sub(v128_load_i16x8(data, pos), range_start),
#| range_width,
#| )
#| let hi = i16x8_le_u(
#| i16x8_sub(v128_load_i16x8(data, pos + 8), range_start),
#| range_width,
#| )
#| if v128_any_true(v128_or(lo, hi)) {
#| for i in pos..<(pos + 16) {
#| let code_unit = data.unsafe_get(i)
#| if code_unit >= first && code_unit <= last {
#| return Some(i - start)
#| }
#| }
#| }
#| continue pos + 16
#| } nobreak {
#| pos
#| }
#| let tail_start = if end - pos >= 8 {
#| let code_units = v128_load_i16x8(data, pos)
#| let in_range = i16x8_le_u(i16x8_sub(code_units, range_start), range_width)
#| if v128_any_true(in_range) {
#| for i in pos..<(pos + 8) {
#| let code_unit = data.unsafe_get(i)
#| if code_unit >= first && code_unit <= last {
#| return Some(i - start)
#| }
#| }
#| }
#| pos + 8
#| } else {
#| pos
#| }
#| for i in tail_start..= first && code_unit <= last {
#| return Some(i - start)
#| }
#| }
#| None
#|}
#|#inline
#|#cfg(any(target="native", target="wasm"))
#|fn find_ascii_uppercase_code_unit(
#| data : String,
#| start : Int32,
#| len : Int32,
#|) -> Int32? {
#| if len < 24 {
#| let end = start + len
#| for i in start.. Int32? {
#| if len < 24 {
#| let end = start + len
#| for i in start.. String {
#| let len = view.length()
#| let output : FixedArray[UInt16] = FixedArray::make(len, 0)
#| output.unsafe_blit_from_string(0, view.data(), view.start_offset(), len)
#| let range_start = i16x8_splat(first)
#| let range_width = i16x8_splat(last - first)
#| let lane_delta = i16x8_splat(delta.to_uint16())
#| let tail_start = for pos = first_match; len - pos >= 8; {
#| let code_units = v128_load_fixedarray_i16x8(output, pos)
#| let in_range = i16x8_le_u(i16x8_sub(code_units, range_start), range_width)
#| let converted = i16x8_add(code_units, v128_and(in_range, lane_delta))
#| v128_store_i16x8(output, pos, converted)
#| continue pos + 8
#| } nobreak {
#| pos
#| }
#| for i in tail_start..= first && code_unit <= last {
#| output.unsafe_set(i, (code_unit.to_int() + delta).to_uint16())
#| }
#| }
#| unsafe_fixedarray_uint16_to_string(output)
#|}
#|#cfg(not(any(target="js", target="native", target="wasm")))
#|fn ascii_case_copy(
#| view : StringView,
#| first_match : Int32,
#| first : UInt16,
#| last : UInt16,
#| delta : Int32,
#|) -> String {
#| let len = view.length()
#| let output : FixedArray[UInt16] = FixedArray::make(len, 0)
#| output.unsafe_blit_from_string(0, view.data(), view.start_offset(), len)
#| let tail = view.unsafe_view(start=first_match, end=view.length())
#| for i, u in tail.code_units() {
#| if u >= first && u <= last {
#| output.unsafe_set(first_match + i, (u.to_int() + delta).to_uint16())
#| }
#| }
#| unsafe_fixedarray_uint16_to_string(output)
#|}
#|#cfg(not(target="js"))
#|pub fn StringView::to_lower(self : StringView) -> StringView {
#| let data = self.data()
#| let start = self.start_offset()
#| let len = self.length()
#| guard find_ascii_uppercase_code_unit(data, start, len) is Some(idx) else {
#| return self
#| }
#| ascii_case_copy(self, idx, 'A', 'Z', 32)
#|}
#|#cfg(not(target="js"))
#|pub fn String::to_lower(self : String) -> String {
#| let len = self.length()
#| guard find_ascii_uppercase_code_unit(self, 0, len) is Some(idx) else {
#| return self
#| }
#| ascii_case_copy(self, idx, 'A', 'Z', 32)
#|}
#|#cfg(target="js")
#|pub fn StringView::to_lower(self : StringView) -> StringView {
#| guard self.find_by(c => c.is_ascii_uppercase()) is Some(idx) else {
#| return self
#| }
#| let buf = StringBuilder(capacity=self.length())
#| let head = self.unsafe_view(start=0, end=idx)
#| buf.write_substring(head.data(), head.start_offset(), head.length())
#| for c in self.unsafe_view(start=idx, end=self.length()) {
#| if c.is_ascii_uppercase() {
#| buf.write_char((c.to_int() + 32).unsafe_to_char())
#| } else {
#| buf.write_char(c)
#| }
#| }
#| buf.to_string()
#|}
#|#cfg(target="js")
#|pub fn String::to_lower(self : String) -> String {
#| guard self.find_by(c => c.is_ascii_uppercase()) is Some(idx) else {
#| return self
#| }
#| let buf = StringBuilder(capacity=self.length())
#| let head = self.unsafe_view(start=0, end=idx)
#| buf.write_substring(head.data(), head.start_offset(), head.length())
#| for c in self.unsafe_view(start=idx, end=self.length()) {
#| if c.is_ascii_uppercase() {
#| buf.write_char((c.to_int() + 32).unsafe_to_char())
#| } else {
#| buf.write_char(c)
#| }
#| }
#| buf.to_string()
#|}
#|test "to_lower" {
#| assert_true("Hello".to_lower() == "hello")
#| assert_true("HELLO".to_lower() == "hello")
#| assert_true("Hello, World!".to_lower() == "hello, world!")
#| assert_true("xxA@Z[az09!".to_lower() == "xxa@z[az09!")
#| assert_true("🤣A".to_lower() == "🤣a")
#| assert_true("A🤣B".to_lower() == "a🤣b")
#| let unpaired = String::from_array([(0xD800).unsafe_to_char(), 'A'])
#| let lowered_unpaired = String::from_array([(0xD800).unsafe_to_char(), 'a'])
#| assert_true(unpaired.to_lower() == lowered_unpaired)
#|}
#|test "to_lower after a non-BMP character" {
#| inspect("😀X".to_lower(), content="😀x")
#| inspect("a😀Xz"[1:4].to_lower(), content="😀x")
#|}
#|test "to_lower SIMD search boundaries" {
#| for uppercase_at in 0..<40 {
#| let prefix = "x".repeat(uppercase_at)
#| let suffix = "x".repeat(40 - uppercase_at)
#| assert_true((prefix + "A" + suffix).to_lower() == prefix + "a" + suffix)
#| }
#| let source = "A" + "x".repeat(40) + "Z"
#| assert_true(source[1:41].to_lower() == "x".repeat(40))
#|}
#|test "View::to_lower" {
#| assert_true("Hello"[:].to_lower() == "hello")
#| assert_true("HELLO"[:].to_lower() == "hello")
#| assert_true("Hello, World!"[:].to_lower() == "hello, world!")
#| assert_true("x🤣ABy"[1:5].to_lower() == "🤣ab")
#| let unpaired = String::from_array([(0xD800).unsafe_to_char(), 'A'])
#| let lowered_unpaired = String::from_array([(0xD800).unsafe_to_char(), 'a'])
#| assert_true(unpaired[:].to_lower() == lowered_unpaired)
#|}
#|#cfg(not(target="js"))
#|pub fn StringView::to_upper(self : StringView) -> StringView {
#| let data = self.data()
#| let start = self.start_offset()
#| let len = self.length()
#| guard find_ascii_lowercase_code_unit(data, start, len) is Some(idx) else {
#| return self
#| }
#| ascii_case_copy(self, idx, 'a', 'z', -32)
#|}
#|#cfg(not(target="js"))
#|pub fn String::to_upper(self : String) -> String {
#| let len = self.length()
#| guard find_ascii_lowercase_code_unit(self, 0, len) is Some(idx) else {
#| return self
#| }
#| ascii_case_copy(self, idx, 'a', 'z', -32)
#|}
#|#cfg(target="js")
#|pub fn StringView::to_upper(self : StringView) -> StringView {
#| guard self.find_by(c => c.is_ascii_lowercase()) is Some(idx) else {
#| return self
#| }
#| let buf = StringBuilder(capacity=self.length())
#| let head = self.unsafe_view(start=0, end=idx)
#| buf.write_substring(head.data(), head.start_offset(), head.length())
#| for c in self.unsafe_view(start=idx, end=self.length()) {
#| if c.is_ascii_lowercase() {
#| buf.write_char((c.to_int() - 32).unsafe_to_char())
#| } else {
#| buf.write_char(c)
#| }
#| }
#| buf.to_string()
#|}
#|#cfg(target="js")
#|pub fn String::to_upper(self : String) -> String {
#| guard self.find_by(c => c.is_ascii_lowercase()) is Some(idx) else {
#| return self
#| }
#| let buf = StringBuilder(capacity=self.length())
#| let head = self.unsafe_view(start=0, end=idx)
#| buf.write_substring(head.data(), head.start_offset(), head.length())
#| for c in self.unsafe_view(start=idx, end=self.length()) {
#| if c.is_ascii_lowercase() {
#| buf.write_char((c.to_int() - 32).unsafe_to_char())
#| } else {
#| buf.write_char(c)
#| }
#| }
#| buf.to_string()
#|}
#|test "to_upper" {
#| assert_true("hello".to_upper() == "HELLO")
#| assert_true("HELLO".to_upper() == "HELLO")
#| assert_true("Hello, World!".to_upper() == "HELLO, WORLD!")
#| assert_true("XXa`z{AZ09!".to_upper() == "XXA`Z{AZ09!")
#| assert_true("🤣a".to_upper() == "🤣A")
#| assert_true("a🤣b".to_upper() == "A🤣B")
#| let unpaired = String::from_array([(0xD800).unsafe_to_char(), 'a'])
#| let uppered_unpaired = String::from_array([(0xD800).unsafe_to_char(), 'A'])
#| assert_true(unpaired.to_upper() == uppered_unpaired)
#|}
#|test "to_upper after a non-BMP character" {
#| inspect("😀x".to_upper(), content="😀X")
#| inspect("a😀xz"[1:4].to_upper(), content="😀X")
#|}
#|test "to_upper SIMD search boundaries" {
#| for lowercase_at in 0..<40 {
#| let prefix = "X".repeat(lowercase_at)
#| let suffix = "X".repeat(40 - lowercase_at)
#| assert_true((prefix + "a" + suffix).to_upper() == prefix + "A" + suffix)
#| }
#| let source = "a" + "X".repeat(40) + "z"
#| assert_true(source[1:41].to_upper() == "X".repeat(40))
#|}
#|test "View::to_upper" {
#| assert_true("hello"[:].to_upper() == "HELLO")
#| assert_true("HELLO"[:].to_upper() == "HELLO")
#| assert_true("Hello, World!"[:].to_upper() == "HELLO, WORLD!")
#| assert_true("X🤣abY"[1:5].to_upper() == "🤣AB")
#| let unpaired = String::from_array([(0xD800).unsafe_to_char(), 'a'])
#| let uppered_unpaired = String::from_array([(0xD800).unsafe_to_char(), 'A'])
#| assert_true(unpaired[:].to_upper() == uppered_unpaired)
#|}
#|#locals(f)
#|pub fn[A] StringView::fold(
#| self : StringView,
#| init~ : A,
#| f : (A, Char) -> A raise?,
#|) -> A raise? {
#| for c in self; rv = (init : A) {
#| continue f(rv, c)
#| } nobreak {
#| rv
#| }
#|}
#|pub fn[A] String::fold(
#| self : String,
#| init~ : A,
#| f : (A, Char) -> A raise?,
#|) -> A raise? {
#| self[:].fold(init~, f)
#|}
#|test "fold" {
#| assert_true(
#| "hello".fold(init=[], (acc, c) => {
#| acc.push(c)
#| acc
#| }) ==
#| ['h', 'e', 'l', 'l', 'o'],
#| )
#| assert_true(
#| "hello".fold(init=0, (acc, c) => acc + c.to_int()) ==
#| 104 + 101 + 108 + 108 + 111,
#| )
#|}
#|test "fold with raise" {
#| try
#| ignore(
#| "hello".fold(init=0, (acc, c) => {
#| if c == 'l' {
#| raise Failure("found l")
#| }
#| acc + 1
#| }),
#| )
#| catch {
#| e => assert_true(e is Failure("found l"))
#| } noraise {
#| _ => fail("expected fold to raise")
#| }
#|}
#|#locals(f)
#|pub fn[A] StringView::rev_fold(
#| self : StringView,
#| init~ : A,
#| f : (A, Char) -> A raise?,
#|) -> A raise? {
#| for c in self.rev_iter(); rv = (init : A) {
#| continue f(rv, c)
#| } nobreak {
#| rv
#| }
#|}
#|pub fn[A] String::rev_fold(
#| self : String,
#| init~ : A,
#| f : (A, Char) -> A raise?,
#|) -> A raise? {
#| self[:].rev_fold(init~, f)
#|}
#|test "rev_fold" {
#| assert_true(
#| "hello".rev_fold(init=[], (acc, c) => {
#| acc.push(c)
#| acc
#| }) ==
#| ['o', 'l', 'l', 'e', 'h'],
#| )
#| assert_true(
#| "hello".rev_fold(init=0, (acc, c) => acc + c.to_int()) ==
#| 111 + 108 + 108 + 101 + 104,
#| )
#|}
#|test "rev_fold with raise" {
#| try
#| ignore(
#| "hello".rev_fold(init=0, (acc, c) => {
#| if c == 'l' {
#| raise Failure("found l")
#| }
#| acc + 1
#| }),
#| )
#| catch {
#| e => assert_true(e is Failure("found l"))
#| } noraise {
#| _ => fail("expected rev_fold to raise")
#| }
#|}
#|#intrinsic("%string.get_opt")
#|pub fn String::get(self : String, idx : Int32) -> UInt16? {
#| guard idx >= 0 && idx < self.length() else { return None }
#| Some(self.unsafe_get(idx))
#|}
#|#intrinsic("%stringview.get_opt")
#|pub fn StringView::get(self : StringView, idx : Int32) -> UInt16? {
#| guard idx >= 0 && idx < self.length() else { return None }
#| Some(self.unsafe_get(idx))
#|}
#|test "String::get supports emoji (surrogate pair)" {
#| let s = "hello"
#| assert_true(s.get(0) is Some(104))
#| assert_true(s.get(4) is Some(111))
#| assert_true(s.get(5) is None)
#| assert_true(s.get(-1) is None)
#| let s = "a🤣b"
#| assert_true(s.get(0) is Some(97))
#| assert_true(s.get(1) is Some(55358))
#| assert_true(s.get(2) is Some(56611))
#| assert_true(s.get(3) is Some(98))
#| assert_true(s.get(4) is None)
#|}
#|test "View::get basic cases" {
#| let v = "hello"[1:4]
#| assert_true(v.get(0) is Some(101))
#| assert_true(v.get(2) is Some(108))
#| assert_true(v.get(3) is None)
#| assert_true(v.get(-1) is None)
#| let v = "ab🤣cd"[1:5]
#| assert_true(v.get(0) is Some(98))
#| assert_true(v.get(1) is Some(55358))
#| assert_true(v.get(2) is Some(56611))
#|}
#|pub fn String::get_char(self : String, idx : Int32) -> Char? {
#| guard idx >= 0 && idx < self.length() else { return None }
#| let c = self.unsafe_get(idx)
#| if c.is_leading_surrogate() {
#| guard idx + 1 < self.length() else { return None }
#| let next = self.unsafe_get(idx + 1)
#| if next.is_trailing_surrogate() {
#| Some(code_point_of_surrogate_pair(c.to_int(), next.to_int()))
#| } else {
#| None
#| }
#| } else if c.is_trailing_surrogate() {
#| None
#| } else {
#| Some(c.unsafe_to_char())
#| }
#|}
#|pub fn StringView::get_char(self : StringView, idx : Int32) -> Char? {
#| guard idx >= 0 && idx < self.length() else { return None }
#| let c = self.unsafe_get(idx)
#| if c.is_leading_surrogate() {
#| guard idx + 1 < self.length() else { return None }
#| let next = self.unsafe_get(idx + 1)
#| if next.is_trailing_surrogate() {
#| Some(code_point_of_surrogate_pair(c.to_int(), next.to_int()))
#| } else {
#| None
#| }
#| } else if c.is_trailing_surrogate() {
#| None
#| } else {
#| Some(c.unsafe_to_char())
#| }
#|}
#|test "String::get_char basic cases" {
#| let s = "hello"
#| assert_true(s.get_char(0) == Some('h'))
#| assert_true(s.get_char(1) == Some('e'))
#| assert_true(s.get_char(4) == Some('o'))
#| assert_true(s.get_char(5) == None)
#| assert_true(s.get_char(-1) == None)
#| let s = "a🤣b"
#| assert_true(s.get_char(0) == Some('a'))
#| assert_true(s.get_char(1) == Some('🤣'))
#| assert_true(s.get_char(2) == None) // Second half of surrogate pair is not a valid char
#| assert_true(s.get_char(3) == Some('b'))
#| assert_true(s.get_char(4) == None)
#|}
#|test "View::get_char basic cases" {
#| let s = "a🤣b"
#| let v = s[0:3]
#| assert_true(v.get_char(0) == Some('a'))
#| assert_true(v.get_char(1) == Some('🤣'))
#| assert_true(v.get_char(2) == None)
#| assert_true(v.get_char(3) == None)
#| assert_true(v.get_char(4) == None)
#| let v2 = s[1:3] // Only contains the emoji surrogate pair
#| assert_true(v2.get_char(0) == Some('🤣'))
#| assert_true(v2.get_char(1) == None)
#| assert_true(v2.get_char(2) == None)
#|}
),
"stringbuilder.mbt": (
#|#alias(write)
#|#alias(write_string_interpolation)
#|pub fn[T : Show] StringBuilder::write_object(
#| self : StringBuilder,
#| obj : T,
#|) -> Unit {
#| obj.output(self)
#|}
#|pub fn StringBuilder::write_iter(
#| self : StringBuilder,
#| iter : Iter[Char],
#|) -> Unit {
#| for ch in iter {
#| self.write_char(ch)
#| }
#|}
#|pub fn StringBuilder::write_stringview(
#| self : StringBuilder,
#| view : StringView,
#|) -> Unit {
#| self.write_view(view)
#|}
),
"stringbuilder_concat.mbt": (
#|struct StringBuilder {
#| mut val : String
#|}
#|#alias(new, deprecated="Use `StringBuilder()` instead")
#|#label_migration(capacity, alias=size_hint, msg="Use capacity= instead")
#|pub fn StringBuilder::StringBuilder(capacity~ : Int32 = 8) -> StringBuilder {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| ignore(capacity)
#| { val: "", }
#|}
#|pub fn StringBuilder::is_empty(self : StringBuilder) -> Bool {
#| self.val == ""
#|}
#|pub impl Logger for StringBuilder with fn write_string(self, str) {
#| self.val += str
#|}
#|pub impl Logger for StringBuilder with fn write_char(self, ch) {
#| self.val += char_to_string(ch)
#|}
#|pub impl Logger for StringBuilder with fn write_view(
#| self : StringBuilder,
#| str : StringView,
#|) -> Unit {
#| self.val += str.to_owned()
#|}
#|pub impl Show for StringBuilder with fn output(self, logger) {
#| logger.write_string(self.val)
#|}
#|pub fn StringBuilder::to_string(self : StringBuilder) -> String {
#| self.val
#|}
#|pub fn StringBuilder::reset(
#| self : StringBuilder,
#| keep_capacity~ : Bool = true,
#|) -> Unit {
#| ignore(keep_capacity)
#| self.val = ""
#|}
),
"stringview.mbt": (
#|fn StringView::str(self : StringView) -> String = "%stringview.str"
#|fn StringView::start(self : StringView) -> Int32 = "%stringview.start"
#|fn StringView::end(self : StringView) -> Int32 = "%stringview.end"
#|#intrinsic("%stringview.get")
#|#alias("_[_]")
#|#alias(code_unit_at)
#|pub fn StringView::at(self : StringView, index : Int32) -> UInt16 {
#| guard index >= 0 && index < self.length() else {
#| index_out_of_bounds(self.length(), index)
#| }
#| self.unsafe_get(index)
#|}
#|#intrinsic("%stringview.length")
#|pub fn StringView::length(self : StringView) -> Int32 {
#| self.end() - self.start()
#|}
#|pub fn StringView::suffixes(
#| self : StringView,
#| include_empty~ : Bool = false,
#|) -> Iter[StringView] {
#| let str = self.str()
#| let end = self.end()
#| let mut next_start = self.start()
#| let mut finished = false
#| Iter(fn() -> StringView? {
#| if finished {
#| None
#| } else if next_start == end {
#| finished = true
#| if include_empty {
#| Some(str.unsafe_view(start=next_start, end~))
#| } else {
#| None
#| }
#| } else {
#| let suffix = str.unsafe_view(start=next_start, end~)
#| let code = str.unsafe_get(next_start)
#| if code.is_leading_surrogate() &&
#| next_start + 1 < end &&
#| str.unsafe_get(next_start + 1).is_trailing_surrogate() {
#| next_start += 2
#| } else {
#| next_start += 1
#| }
#| Some(suffix)
#| }
#| })
#|}
#|pub fn StringView::data(self : StringView) -> String {
#| self.str()
#|}
#|pub fn StringView::start_offset(self : StringView) -> Int32 {
#| self.start()
#|}
#|#intrinsic("%stringview.unsafe_get")
#|#internal(unsafe, "Undefined behavior if index is out of bounds.")
#|pub fn StringView::unsafe_get(self : StringView, index : Int32) -> UInt16 {
#| self.str().unsafe_get(self.start() + index)
#|}
#|#intrinsic("%stringview.code_units")
#|pub fn StringView::code_units(self : StringView) -> ArrayView[UInt16] {
#| FixedArray::makei(self.str().length(), i => self.str().unsafe_get(i))[self.start():self.end()]
#|}
#|#deprecated("Use `StringView::unsafe_get` instead")
#|pub fn StringView::unsafe_charcode_at(
#| self : StringView,
#| index : Int32,
#|) -> Int32 {
#| self.str().unsafe_get(self.start() + index).to_int()
#|}
#|pub fn StringView::char_length(self : StringView) -> Int32 {
#| self.str().char_length(start=self.start(), end=self.end())
#|}
#|#alias(to_string, deprecated="Use `to_owned` to allocate an owned String from a StringView; use `Show::to_string` or format strings for display")
#|pub fn StringView::to_owned(self : StringView) -> String {
#| self.str().unsafe_substring(start=self.start(), end=self.end())
#|}
#|pub impl Show for StringView with fn output(self, logger) {
#| logger.write_view(self)
#|}
#|pub impl Show for StringView with fn to_string(self) {
#| self.to_owned()
#|}
#|#alias(iterator, deprecated)
#|pub fn StringView::iter(self : StringView) -> Iter[Char] {
#| let start = self.start()
#| let end = self.end()
#| let mut index = start
#| Iter(fn() {
#| guard index < end else { None }
#| let c1 = self.str().unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < self.end() {
#| let c2 = self.str().unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| index += 2
#| return Some(code_point_of_surrogate_pair(c1.to_int(), c2.to_int()))
#| }
#| }
#| index += 1
#| Some(c1.unsafe_to_char())
#| })
#|}
#|#alias(iterator2, deprecated)
#|pub fn StringView::iter2(self : StringView) -> Iter2[Int32, Char] {
#| let start = self.start()
#| let end = self.end()
#| let mut index = start
#| let mut char_index = 0
#| Iter(fn() {
#| guard index < end else { None }
#| let c1 = self.str().unsafe_get(index)
#| if c1.is_leading_surrogate() && index + 1 < self.end() {
#| let c2 = self.str().unsafe_get(index + 1)
#| if c2.is_trailing_surrogate() {
#| let result = (
#| char_index,
#| code_point_of_surrogate_pair(c1.to_int(), c2.to_int()),
#| )
#| index += 2
#| char_index += 1
#| return Some(result)
#| }
#| }
#| let result = (char_index, c1.unsafe_to_char())
#| index += 1
#| char_index += 1
#| Some(result)
#| })
#|}
#|#alias(rev_iterator, deprecated)
#|pub fn StringView::rev_iter(self : StringView) -> Iter[Char] {
#| let start = self.start()
#| let end = self.end()
#| let mut index = end
#| Iter(fn() {
#| guard index > start else { None }
#| index -= 1
#| let c1 = self.str().unsafe_get(index)
#| if c1.is_trailing_surrogate() && index - 1 >= 0 {
#| let c2 = self.str().unsafe_get(index - 1)
#| if c2.is_leading_surrogate() {
#| index -= 1
#| return Some(code_point_of_surrogate_pair(c2.to_int(), c1.to_int()))
#| }
#| }
#| Some(c1.unsafe_to_char())
#| })
#|}
#|#locals(f)
#|#alias(every)
#|pub fn StringView::all(
#| self : StringView,
#| f : (Char) -> Bool raise?,
#|) -> Bool raise? {
#| for c in self {
#| if !f(c) {
#| return false
#| }
#| }
#| true
#|}
#|#locals(f)
#|#alias(exists)
#|pub fn StringView::any(
#| self : StringView,
#| f : (Char) -> Bool raise?,
#|) -> Bool raise? {
#| for c in self {
#| if f(c) {
#| return true
#| }
#| }
#| false
#|}
#|pub fn StringView::range_equal(
#| self : StringView,
#| start~ : Int32,
#| other : StringView,
#| other_start~ : Int32,
#| len~ : Int32,
#|) -> Bool {
#| let self_len = self.length()
#| let other_len = other.length()
#| guard len >= 0 &&
#| start >= 0 &&
#| start <= self_len &&
#| other_start >= 0 &&
#| other_start <= other_len &&
#| len <= self_len - start &&
#| len <= other_len - other_start else {
#| abort("StringView::range_equal: invalid range")
#| }
#| self
#| .str()
#| .unsafe_range_equal(
#| other.str(),
#| start=self.start() + start,
#| other_start=other.start() + other_start,
#| len~,
#| )
#|}
#|pub impl Eq for StringView with fn equal(self, other) {
#| let len = self.length()
#| guard len == other.length() else { return false }
#| if physical_equal(self.str(), other.str()) && self.start() == other.start() {
#| return true
#| }
#| self
#| .str()
#| .unsafe_range_equal(
#| other.str(),
#| start=self.start(),
#| other_start=other.start(),
#| len~,
#| )
#|}
#|pub fn StringView::equal_to_string(self : StringView, other : String) -> Bool {
#| let len = self.length()
#| guard len == other.length() else { return false }
#| if physical_equal(self.str(), other) && self.start() == 0 {
#| return true
#| }
#| self.str().unsafe_range_equal(other, start=self.start(), other_start=0, len~)
#|}
#|pub impl Compare for StringView with fn compare(self, other) {
#| let self_len = self.length()
#| let other_len = other.length()
#| let cmp = self_len.compare(other_len)
#| guard cmp == 0 else { return cmp }
#| if physical_equal(self.str(), other.str()) && self.start() == other.start() {
#| return 0
#| }
#| for i in 0.. Int32 {
#| let self_len = self.length()
#| let other_len = other.length()
#| let min_len = if self_len < other_len { self_len } else { other_len }
#| for i in 0.. UInt16 {
#| if c - 'A' < 26 {
#| c | 0x20
#| } else {
#| c
#| }
#|}
#|#inline
#|fn ascii_case_pair_u16(a : UInt16, b : UInt16) -> Bool {
#| (a ^ b) == 0x20 && (a | 0x20) - 'a' < 26
#|}
#|pub fn StringView::compare_ignore_ascii_case(
#| self : StringView,
#| other : StringView,
#|) -> Int32 {
#| let self_len = self.length()
#| let other_len = other.length()
#| let min_len = if self_len < other_len { self_len } else { other_len }
#| for i in 0.. Bool {
#| let len = self.length()
#| if len != other.length() {
#| return false
#| }
#| for i in 0.. StringView {
#| String::from_array(chars)
#|}
#|#alias(from_iterator, deprecated)
#|pub fn StringView::from_iter(iter : Iter[Char]) -> StringView {
#| String::from_iter(iter)
#|}
#|#intrinsic("%string.view_opt")
#|pub fn String::get_view(
#| self : String,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> StringView? {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else { None }
#| if start < len && self.unsafe_get(start).is_trailing_surrogate() {
#| return None
#| }
#| if end < len && self.unsafe_get(end).is_trailing_surrogate() {
#| return None
#| }
#| Some(self.unsafe_view(start~, end~))
#|}
#|#intrinsic("%stringview.view_opt")
#|pub fn StringView::get_view(
#| self : StringView,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> StringView? {
#| let str_len = self.str().length()
#| let abs_end = match end {
#| None => self.end()
#| Some(end) => self.start() + end
#| }
#| let abs_start = self.start() + start
#| guard abs_start >= self.start() &&
#| abs_start <= abs_end &&
#| abs_end <= self.end() else {
#| None
#| }
#| if abs_start < str_len &&
#| self.str().unsafe_get(abs_start).is_trailing_surrogate() {
#| return None
#| }
#| if abs_end < str_len && self.str().unsafe_get(abs_end).is_trailing_surrogate() {
#| return None
#| }
#| Some(self.str().unsafe_view(start=abs_start, end=abs_end))
#|}
#|#alias("_[_:_]")
#|pub fn String::clamped_view(
#| self : String,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> StringView {
#| let len = self.length()
#| let lo = if start <= 0 {
#| 0
#| } else if start >= len {
#| len
#| } else if self.unsafe_get(start).is_trailing_surrogate() &&
#| self.unsafe_get(start - 1).is_leading_surrogate() {
#| start + 1
#| } else {
#| start
#| }
#| let hi = match end {
#| None => len
#| Some(e) =>
#| if e <= 0 {
#| 0
#| } else if e >= len {
#| len
#| } else if self.unsafe_get(e).is_trailing_surrogate() &&
#| self.unsafe_get(e - 1).is_leading_surrogate() {
#| e - 1
#| } else {
#| e
#| }
#| }
#| if lo >= hi {
#| self.unsafe_view(start=lo, end=lo)
#| } else {
#| self.unsafe_view(start=lo, end=hi)
#| }
#|}
#|#alias("_[_:_]")
#|pub fn StringView::clamped_view(
#| self : StringView,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> StringView {
#| let len = self.length()
#| let str = self.str()
#| let base = self.start()
#| let lo = if start <= 0 {
#| 0
#| } else if start >= len {
#| len
#| } else if str.unsafe_get(base + start).is_trailing_surrogate() &&
#| str.unsafe_get(base + start - 1).is_leading_surrogate() {
#| start + 1
#| } else {
#| start
#| }
#| let hi = match end {
#| None => len
#| Some(e) =>
#| if e <= 0 {
#| 0
#| } else if e >= len {
#| len
#| } else if str.unsafe_get(base + e).is_trailing_surrogate() &&
#| str.unsafe_get(base + e - 1).is_leading_surrogate() {
#| e - 1
#| } else {
#| e
#| }
#| }
#| if lo >= hi {
#| str.unsafe_view(start=base + lo, end=base + lo)
#| } else {
#| str.unsafe_view(start=base + lo, end=base + hi)
#| }
#|}
#|pub fn String::split_at(self : String, at : Int32) -> (StringView, StringView) {
#| let len = self.length()
#| let mut cut = if at < 0 { 0 } else if at > len { len } else { at }
#| if cut > 0 &&
#| cut < len &&
#| self.unsafe_get(cut).is_trailing_surrogate() &&
#| self.unsafe_get(cut - 1).is_leading_surrogate() {
#| cut -= 1
#| }
#| (self.unsafe_view(start=0, end=cut), self.unsafe_view(start=cut, end=len))
#|}
#|pub fn StringView::split_at(
#| self : StringView,
#| at : Int32,
#|) -> (StringView, StringView) {
#| let len = self.length()
#| let mut cut = if at < 0 { 0 } else if at > len { len } else { at }
#| let str = self.str()
#| let base = self.start()
#| if cut > 0 &&
#| cut < len &&
#| str.unsafe_get(base + cut).is_trailing_surrogate() &&
#| str.unsafe_get(base + cut - 1).is_leading_surrogate() {
#| cut -= 1
#| }
#| (
#| str.unsafe_view(start=base, end=base + cut),
#| str.unsafe_view(start=base + cut, end=self.end()),
#| )
#|}
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn String::exact_view(
#| self : String,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> StringView {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard! start >= 0 && start <= end && end <= len
#| if start < len {
#| guard! !self.unsafe_get(start).is_trailing_surrogate()
#| }
#| if end < len {
#| guard! !self.unsafe_get(end).is_trailing_surrogate()
#| }
#| self.unsafe_view(start~, end~)
#|}
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn StringView::exact_view(
#| self : StringView,
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> StringView {
#| let str_len = self.str().length()
#| let abs_end = match end {
#| None => self.end()
#| Some(end) => self.start() + end
#| }
#| let abs_start = self.start() + start
#| guard! abs_start >= self.start() &&
#| abs_start <= abs_end &&
#| abs_end <= self.end()
#| if abs_start < str_len {
#| guard! !self.str().unsafe_get(abs_start).is_trailing_surrogate()
#| }
#| if abs_end < str_len {
#| guard! !self.str().unsafe_get(abs_end).is_trailing_surrogate()
#| }
#| self.str().unsafe_view(start=abs_start, end=abs_end)
#|}
#|pub fn StringView::char_length_eq(self : StringView, len : Int32) -> Bool {
#| self.str().char_length_eq(len, start=self.start(), end=self.end())
#|}
#|pub fn StringView::char_length_ge(self : StringView, len : Int32) -> Bool {
#| self.str().char_length_ge(len, start=self.start(), end=self.end())
#|}
#|pub fn StringView::offset_of_nth_char(self : StringView, i : Int32) -> Int32? {
#| if self.str().offset_of_nth_char(i, start=self.start(), end=self.end())
#| is Some(index) {
#| Some(index - self.start())
#| } else {
#| None
#| }
#|}
#|pub impl Default for StringView with fn default() {
#| ""
#|}
#|pub fn StringView::make(length : Int32, value : Char) -> StringView {
#| String::make(length, value)
#|}
#|pub impl Add for StringView with fn add(self, other) {
#| [..self, ..other]
#|}
),
"to_string.mbt": (
#|#cfg(not(target="js"))
#|const ALPHABET : String = "0123456789abcdefghijklmnopqrstuvwxyz"
#|#cfg(not(target="js"))
#|fn unsafe_fixedarray_uint16_to_string(buffer : FixedArray[UInt16]) -> String = "%string.unsafe_from_uint16_fixedarray"
#|#cfg(not(target="js"))
#|fn int_to_string_hex(
#| buffer : FixedArray[UInt16],
#| num : UInt32,
#| digit_start : Int32,
#| total_len : Int32,
#|) -> Unit {
#| let (offset, n) = for offset = total_len - digit_start, n = num; offset >= 2; {
#| let byte_val = n.land(0xFFU).reinterpret_as_int()
#| let hi = byte_val / 16
#| let lo = byte_val % 16
#| buffer.unsafe_set(digit_start + offset - 2, ALPHABET.unsafe_get(hi))
#| buffer.unsafe_set(digit_start + offset - 1, ALPHABET.unsafe_get(lo))
#| continue offset - 2, n >> 8
#| } nobreak {
#| (offset, n)
#| }
#| if offset == 1 {
#| let nibble = n.land(0xFU).reinterpret_as_int()
#| buffer.unsafe_set(digit_start, ALPHABET.unsafe_get(nibble))
#| }
#|}
#|#cfg(not(target="js"))
#|fn int_to_string_generic(
#| buffer : FixedArray[UInt16],
#| num : UInt32,
#| digit_start : Int32,
#| total_len : Int32,
#| radix : Int32,
#|) -> Unit {
#| let base = radix.reinterpret_as_uint()
#| if (radix & (radix - 1)) == 0 {
#| let shift = radix.ctz()
#| let mask = base - 1U
#| for offset = total_len - digit_start, n = num; n > 0U; {
#| let digit = n.land(mask).reinterpret_as_int()
#| buffer.unsafe_set(digit_start + offset - 1, ALPHABET.unsafe_get(digit))
#| continue offset - 1, n >> shift
#| }
#| } else {
#| for offset = total_len - digit_start, n = num; n > 0U; {
#| let q = n / base
#| let digit = (n - q * base).reinterpret_as_int()
#| buffer.unsafe_set(digit_start + offset - 1, ALPHABET.unsafe_get(digit))
#| continue offset - 1, q
#| }
#| }
#|}
#|#cfg(not(target="js"))
#|fn int_to_string_dec(
#| buffer : FixedArray[UInt16],
#| num : UInt32,
#| digit_start : Int32,
#| total_len : Int32,
#|) -> Unit {
#| let (num, offset) = for num = num, offset = total_len - digit_start; num >=
#| 10000U; {
#| let t = num / 10000U
#| let r = (num % 10000U).reinterpret_as_int()
#| let d1 = r / 100
#| let d2 = r % 100
#| let d1_hi = (0x30 + d1 / 10).to_uint16()
#| let d1_lo = (0x30 + d1 % 10).to_uint16()
#| let d2_hi = (0x30 + d2 / 10).to_uint16()
#| let d2_lo = (0x30 + d2 % 10).to_uint16()
#| buffer.unsafe_set(digit_start + offset - 4, d1_hi)
#| buffer.unsafe_set(digit_start + offset - 3, d1_lo)
#| buffer.unsafe_set(digit_start + offset - 2, d2_hi)
#| buffer.unsafe_set(digit_start + offset - 1, d2_lo)
#| continue t, offset - 4
#| } nobreak {
#| (num, offset)
#| }
#| let (remaining, offset) = for remaining = num.reinterpret_as_int(), offset = offset; remaining >=
#| 100; {
#| let t = remaining / 100
#| let d = remaining % 100
#| let d_hi = (0x30 + d / 10).to_uint16()
#| let d_lo = (0x30 + d % 10).to_uint16()
#| buffer.unsafe_set(digit_start + offset - 2, d_hi)
#| buffer.unsafe_set(digit_start + offset - 1, d_lo)
#| continue t, offset - 2
#| } nobreak {
#| (remaining, offset)
#| }
#| if remaining >= 10 {
#| let d_hi = (0x30 + remaining / 10).to_uint16()
#| let d_lo = (0x30 + remaining % 10).to_uint16()
#| buffer.unsafe_set(digit_start + offset - 2, d_hi)
#| buffer.unsafe_set(digit_start + offset - 1, d_lo)
#| } else {
#| buffer.unsafe_set(digit_start + offset - 1, (0x30 + remaining).to_uint16())
#| }
#|}
#|#cfg(not(target="js"))
#|fn dec_count32(value : UInt32) -> Int32 {
#| if value >= 100000U { // >= 10^5 means 6+ digits
#| if value >= 10000000U { // >= 10^7 means 8+ digits
#| if value >= 1000000000U { // >= 10^9 means 10 digits
#| 10
#| } else if value >= 100000000U { // >= 10^8 means 9 digits
#| 9
#| } else {
#| 8
#| }
#| } else if value >= 1000000U { // >= 10^6 means 7 digits
#| 7
#| } else {
#| 6
#| }
#| } else if value >= 1000U { // >= 10^3 means 4+ digits
#| if value >= 10000U { // >= 10^4 means 5 digits
#| 5
#| } else {
#| 4
#| }
#| } else if value >= 100U { // >= 10^2 means 3 digits
#| 3
#| } else if value >= 10U { // >= 10^1 means 2 digits
#| 2
#| } else {
#| 1
#| }
#|}
#|#cfg(not(target="js"))
#|fn hex_count32(value : UInt32) -> Int32 {
#| if value == 0U {
#| 1
#| } else {
#| let leading_zeros = value.clz()
#| (31 - leading_zeros) / 4 + 1
#| }
#|}
#|#cfg(not(target="js"))
#|fn radix_count32(value : UInt32, radix : Int32) -> Int32 {
#| if value == 0U {
#| return 1
#| }
#| let base = radix.reinterpret_as_uint()
#| for num = value, count = 0; num > 0U; {
#| continue num / base, count + 1
#| } nobreak {
#| count
#| }
#|}
#|#cfg(not(target="js"))
#|pub fn Int32::to_string(self : Int32, radix~ : Int32 = 10) -> String {
#| if radix < 2 || radix > 36 {
#| abort("radix must be between 2 and 36")
#| }
#| if self == 0 {
#| return "0"
#| }
#| let is_negative = self < 0
#| let num : UInt32 = if is_negative {
#| (-self).reinterpret_as_uint()
#| } else {
#| self.reinterpret_as_uint()
#| }
#| let digit_start = if is_negative { 1 } else { 0 }
#| let buffer = match radix {
#| 10 => {
#| let digit_len = dec_count32(num)
#| let total_len = digit_len + digit_start
#| let buffer : FixedArray[UInt16] = FixedArray::make(total_len, 0)
#| int_to_string_dec(buffer, num, digit_start, total_len)
#| buffer
#| }
#| 16 => {
#| let digit_len = hex_count32(num)
#| let total_len = digit_len + digit_start
#| let buffer : FixedArray[UInt16] = FixedArray::make(total_len, 0)
#| int_to_string_hex(buffer, num, digit_start, total_len)
#| buffer
#| }
#| _ => {
#| let digit_len = radix_count32(num, radix)
#| let total_len = digit_len + digit_start
#| let buffer : FixedArray[UInt16] = FixedArray::make(total_len, 0)
#| int_to_string_generic(buffer, num, digit_start, total_len, radix)
#| buffer
#| }
#| }
#| if is_negative {
#| buffer.unsafe_set(0, 0x002D)
#| }
#| unsafe_fixedarray_uint16_to_string(buffer)
#|}
#|#cfg(not(target="js"))
#|pub fn UInt32::to_string(self : UInt32, radix~ : Int32 = 10) -> String {
#| if radix < 2 || radix > 36 {
#| abort("radix must be between 2 and 36")
#| }
#| if self == 0U {
#| return "0"
#| }
#| let buffer = match radix {
#| 10 => {
#| let len = dec_count32(self)
#| let buffer : FixedArray[UInt16] = FixedArray::make(len, 0)
#| int_to_string_dec(buffer, self, 0, len)
#| buffer
#| }
#| 16 => {
#| let len = hex_count32(self)
#| let buffer : FixedArray[UInt16] = FixedArray::make(len, 0)
#| int_to_string_hex(buffer, self, 0, len)
#| buffer
#| }
#| _ => {
#| let len = radix_count32(self, radix)
#| let buffer : FixedArray[UInt16] = FixedArray::make(len, 0)
#| int_to_string_generic(buffer, self, 0, len, radix)
#| buffer
#| }
#| }
#| unsafe_fixedarray_uint16_to_string(buffer)
#|}
#|#cfg(target="js")
#|pub fn Int32::to_string(self : Int32, radix~ : Int32 = 10) -> String {
#| int_to_string_js(self, radix)
#|}
#|#cfg(target="js")
#|extern "js" fn int_to_string_js(i : Int32, radix : Int32) -> String =
#| #|(x, radix) => {
#| #| return x.toString(radix);
#| #|}
#|#cfg(target="js")
#|pub fn UInt32::to_string(self : UInt32, radix~ : Int32 = 10) -> String {
#| uint_to_string_js(self, radix)
#|}
#|#cfg(target="js")
#|extern "js" fn uint_to_string_js(i : UInt32, radix : Int32) -> String =
#| #|(x, radix) => {
#| #| return (x >>> 0).toString(radix);
#| #|}
#|#cfg(not(target="js"))
#|fn dec_count64(value : UInt64) -> Int32 {
#| if value >= 10000000000UL { // >= 10^10 means 11+ digits
#| if value >= 100000000000000UL { // >= 10^14 means 15+ digits
#| if value >= 10000000000000000UL { // >= 10^16 means 17+ digits
#| if value >= 1000000000000000000UL { // >= 10^18 means 19+ digits
#| if value >= 10000000000000000000UL { // >= 10^19 means 20 digits
#| 20
#| } else {
#| 19
#| }
#| } else if value >= 100000000000000000UL { // >= 10^17 means 18 digits
#| 18
#| } else {
#| 17
#| }
#| } else if value >= 1000000000000000UL { // >= 10^15 means 16 digits
#| 16
#| } else {
#| 15
#| }
#| } else if value >= 1000000000000UL { // >= 10^12 means 13+ digits
#| if value >= 10000000000000UL { // >= 10^13 means 14 digits
#| 14
#| } else {
#| 13
#| }
#| } else if value >= 100000000000UL { // >= 10^11 means 12 digits
#| 12
#| } else {
#| 11
#| }
#| } else if value >= 100000UL { // >= 10^5 means 6+ digits
#| if value >= 10000000UL { // >= 10^7 means 8+ digits
#| if value >= 1000000000UL { // >= 10^9 means 10 digits
#| 10
#| } else if value >= 100000000UL { // >= 10^8 means 9 digits
#| 9
#| } else {
#| 8
#| }
#| } else if value >= 1000000UL { // >= 10^6 means 7 digits
#| 7
#| } else {
#| 6
#| }
#| } else if value >= 1000UL { // >= 10^3 means 4+ digits
#| if value >= 10000UL { // >= 10^4 means 5 digits
#| 5
#| } else {
#| 4
#| }
#| } else if value >= 100UL { // >= 10^2 means 3 digits
#| 3
#| } else if value >= 10UL { // >= 10^1 means 2 digits
#| 2
#| } else {
#| 1
#| }
#|}
#|#cfg(not(target="js"))
#|fn hex_count64(value : UInt64) -> Int32 {
#| if value == 0UL {
#| 1
#| } else {
#| let leading_zeros = value.clz()
#| (63 - leading_zeros) / 4 + 1
#| }
#|}
#|#cfg(not(target="js"))
#|fn radix_count64(value : UInt64, radix : Int32) -> Int32 {
#| if value == 0UL {
#| return 1
#| }
#| let base = radix.to_uint64()
#| for num = value, count = 0; num > 0UL; {
#| continue num / base, count + 1
#| } nobreak {
#| count
#| }
#|}
#|#cfg(not(target="js"))
#|fn int64_to_string_hex(
#| buffer : FixedArray[UInt16],
#| num : UInt64,
#| digit_start : Int32,
#| total_len : Int32,
#|) -> Unit {
#| let (offset, n) = for offset = total_len - digit_start, n = num; offset >= 2; {
#| let byte_val = n.land(0xFFUL).to_int()
#| let hi = byte_val / 16
#| let lo = byte_val % 16
#| buffer.unsafe_set(digit_start + offset - 2, ALPHABET.unsafe_get(hi))
#| buffer.unsafe_set(digit_start + offset - 1, ALPHABET.unsafe_get(lo))
#| continue offset - 2, n >> 8
#| } nobreak {
#| (offset, n)
#| }
#| if offset == 1 {
#| let nibble = n.land(0xFUL).to_int()
#| buffer.unsafe_set(digit_start, ALPHABET.unsafe_get(nibble))
#| }
#|}
#|#cfg(not(target="js"))
#|fn int64_to_string_generic(
#| buffer : FixedArray[UInt16],
#| num : UInt64,
#| digit_start : Int32,
#| total_len : Int32,
#| radix : Int32,
#|) -> Unit {
#| let base = radix.to_uint64()
#| if (radix & (radix - 1)) == 0 {
#| let shift = radix.ctz()
#| let mask = base - 1UL
#| for offset = total_len - digit_start, n = num; n > 0UL; {
#| let digit = n.land(mask).to_int()
#| buffer.unsafe_set(digit_start + offset - 1, ALPHABET.unsafe_get(digit))
#| continue offset - 1, n >> shift
#| }
#| } else {
#| for offset = total_len - digit_start, n = num; n > 0UL; {
#| let q = n / base
#| let digit = (n - q * base).to_int()
#| buffer.unsafe_set(digit_start + offset - 1, ALPHABET.unsafe_get(digit))
#| continue offset - 1, q
#| }
#| }
#|}
#|#cfg(not(target="js"))
#|fn int64_to_string_dec(
#| buffer : FixedArray[UInt16],
#| num : UInt64,
#| digit_start : Int32,
#| total_len : Int32,
#|) -> Unit {
#| let (num, offset) = for num = num, offset = total_len - digit_start; num >=
#| 10000UL; {
#| let t = num / 10000UL
#| let r = (num % 10000UL).to_int()
#| let d1 = r / 100
#| let d2 = r % 100
#| let d1_hi = (0x30 + d1 / 10).to_uint16()
#| let d1_lo = (0x30 + d1 % 10).to_uint16()
#| let d2_hi = (0x30 + d2 / 10).to_uint16()
#| let d2_lo = (0x30 + d2 % 10).to_uint16()
#| buffer.unsafe_set(digit_start + offset - 4, d1_hi)
#| buffer.unsafe_set(digit_start + offset - 3, d1_lo)
#| buffer.unsafe_set(digit_start + offset - 2, d2_hi)
#| buffer.unsafe_set(digit_start + offset - 1, d2_lo)
#| continue t, offset - 4
#| } nobreak {
#| (num, offset)
#| }
#| let (remaining, offset) = for remaining = num.to_int(), offset = offset; remaining >=
#| 100; {
#| let t = remaining / 100
#| let d = remaining % 100
#| let d_hi = (0x30 + d / 10).to_uint16()
#| let d_lo = (0x30 + d % 10).to_uint16()
#| buffer.unsafe_set(digit_start + offset - 2, d_hi)
#| buffer.unsafe_set(digit_start + offset - 1, d_lo)
#| continue t, offset - 2
#| } nobreak {
#| (remaining, offset)
#| }
#| if remaining >= 10 {
#| let d_hi = (0x30 + remaining / 10).to_uint16()
#| let d_lo = (0x30 + remaining % 10).to_uint16()
#| buffer.unsafe_set(digit_start + offset - 2, d_hi)
#| buffer.unsafe_set(digit_start + offset - 1, d_lo)
#| } else {
#| buffer.unsafe_set(digit_start + offset - 1, (0x30 + remaining).to_uint16())
#| }
#|}
#|#cfg(not(target="js"))
#|pub fn Int64::to_string(self : Int64, radix~ : Int32 = 10) -> String {
#| if radix < 2 || radix > 36 {
#| abort("radix must be between 2 and 36")
#| }
#| if self == 0L {
#| return "0"
#| }
#| let is_negative = self < 0L
#| let num : UInt64 = if is_negative {
#| (-self).reinterpret_as_uint64()
#| } else {
#| self.reinterpret_as_uint64()
#| }
#| let digit_start = if is_negative { 1 } else { 0 }
#| let buffer = match radix {
#| 10 => {
#| let digit_len = dec_count64(num)
#| let total_len = digit_len + digit_start
#| let buffer : FixedArray[UInt16] = FixedArray::make(total_len, 0)
#| int64_to_string_dec(buffer, num, digit_start, total_len)
#| buffer
#| }
#| 16 => {
#| let digit_len = hex_count64(num)
#| let total_len = digit_len + digit_start
#| let buffer : FixedArray[UInt16] = FixedArray::make(total_len, 0)
#| int64_to_string_hex(buffer, num, digit_start, total_len)
#| buffer
#| }
#| _ => {
#| let digit_len = radix_count64(num, radix)
#| let total_len = digit_len + digit_start
#| let buffer : FixedArray[UInt16] = FixedArray::make(total_len, 0)
#| int64_to_string_generic(buffer, num, digit_start, total_len, radix)
#| buffer
#| }
#| }
#| if is_negative {
#| buffer.unsafe_set(0, 0x002D)
#| }
#| unsafe_fixedarray_uint16_to_string(buffer)
#|}
#|#cfg(not(target="js"))
#|pub fn UInt64::to_string(self : UInt64, radix~ : Int32 = 10) -> String {
#| if radix < 2 || radix > 36 {
#| abort("radix must be between 2 and 36")
#| }
#| if self == 0UL {
#| return "0"
#| }
#| let buffer = match radix {
#| 10 => {
#| let len = dec_count64(self)
#| let buffer : FixedArray[UInt16] = FixedArray::make(len, 0)
#| int64_to_string_dec(buffer, self, 0, len)
#| buffer
#| }
#| 16 => {
#| let len = hex_count64(self)
#| let buffer : FixedArray[UInt16] = FixedArray::make(len, 0)
#| int64_to_string_hex(buffer, self, 0, len)
#| buffer
#| }
#| _ => {
#| let len = radix_count64(self, radix)
#| let buffer : FixedArray[UInt16] = FixedArray::make(len, 0)
#| int64_to_string_generic(buffer, self, 0, len, radix)
#| buffer
#| }
#| }
#| unsafe_fixedarray_uint16_to_string(buffer)
#|}
#|#cfg(target="js")
#|pub fn Int64::to_string(self : Int64, radix~ : Int32 = 10) -> String {
#| int64_to_string_js(self, radix)
#|}
#|#cfg(target="js")
#|extern "js" fn int64_to_string_js(num : Int64, radix : Int32) -> String =
#| #|(num, radix) => BigInt.asIntN(64, num).toString(radix)
#|#cfg(target="js")
#|pub fn UInt64::to_string(self : UInt64, radix~ : Int32 = 10) -> String {
#| uint64_to_string_js(self, radix)
#|}
#|#cfg(target="js")
#|extern "js" fn uint64_to_string_js(num : UInt64, radix : Int32) -> String =
#| #|(num, radix) => num.toString(radix)
#|pub fn UInt16::to_string(self : UInt16, radix~ : Int32 = 10) -> String {
#| self.to_int().to_string(radix~)
#|}
#|test "UInt::to_string" {
#| inspect(0U, content="0")
#| inspect(17U, content="17")
#| inspect(4294967295U, content="4294967295")
#|}
#|test "to_string" {
#| assert_true((0x100).to_string() == "256")
#| assert_true("\{0x100}" == "256")
#| assert_true(0x200U.to_string() == "512")
#| assert_true("\{0x200U}" == "512")
#| assert_true(0x300L.to_string() == "768")
#| assert_true("\{0x300L}" == "768")
#| assert_true(0x400UL.to_string() == "1024")
#| assert_true("\{0x400UL}" == "1024")
#|}
#|test "panic to_string_by_radix/illegal_radix" {
#| ignore((1).to_string(radix=1))
#| ignore((1).to_string(radix=37))
#| ignore(1L.to_string(radix=0))
#| ignore(1L.to_string(radix=42))
#| ignore(1U.to_string(radix=-1))
#| ignore(1U.to_string(radix=73))
#| ignore(1UL.to_string(radix=-100))
#| ignore(1UL.to_string(radix=100))
#|}
),
"traits.mbt": (
#|pub(open) trait Eq {
#| fn equal(Self, Self) -> Bool
#| fn not_equal(Self, Self) -> Bool = _
#|}
#|impl Eq with fn not_equal(x, y) {
#| !(x == y)
#|}
#|pub(open) trait Compare: Eq {
#| fn compare(Self, Self) -> Int32
#| fn lt(Self, Self) -> Bool = _
#| #pending_deprecated("Use Compare::lt instead")
#| fn op_lt(Self, Self) -> Bool = _
#| fn gt(Self, Self) -> Bool = _
#| #pending_deprecated("Use Compare::gt instead")
#| fn op_gt(Self, Self) -> Bool = _
#| fn le(Self, Self) -> Bool = _
#| #pending_deprecated("Use Compare::le instead")
#| fn op_le(Self, Self) -> Bool = _
#| fn ge(Self, Self) -> Bool = _
#| #pending_deprecated("Use Compare::ge instead")
#| fn op_ge(Self, Self) -> Bool = _
#|}
#|impl Compare with fn lt(x, y) {
#| x.compare(y).is_neg()
#|}
#|impl Compare with fn op_lt(x, y) {
#| Compare::lt(x, y)
#|}
#|impl Compare with fn gt(x, y) {
#| x.compare(y).is_pos()
#|}
#|impl Compare with fn op_gt(x, y) {
#| Compare::gt(x, y)
#|}
#|impl Compare with fn le(x, y) {
#| x.compare(y).is_non_pos()
#|}
#|impl Compare with fn op_le(x, y) {
#| Compare::le(x, y)
#|}
#|impl Compare with fn ge(x, y) {
#| x.compare(y).is_non_neg()
#|}
#|impl Compare with fn op_ge(x, y) {
#| Compare::ge(x, y)
#|}
#|pub(open) trait Hash {
#| fn hash_combine(Self, Hasher) -> Unit
#| fn hash(Self) -> Int32 = _
#|}
#|impl Hash with fn hash(self) {
#| let h = Hasher()
#| h.combine(self)
#| h.finalize()
#|}
#|pub(open) trait Default {
#| fn default() -> Self
#|}
#|#must_implement_one(write_view, write_substring)
#|pub(open) trait Logger {
#| fn write_string(Self, String) -> Unit = _
#| #deprecated("use `write_view` instead", skip_current_package=true)
#| fn write_substring(Self, String, Int32, Int32) -> Unit = _
#| fn write_view(Self, StringView) -> Unit = _
#| fn write_char(Self, Char) -> Unit = _
#| fn write_string_interpolation(Self, &Show) -> Unit = _
#| fn write(Self, &Show) -> Unit = _
#|}
#|impl Logger with fn write_string_interpolation(self, show) {
#| show.output(self)
#|}
#|impl Logger with fn write(self, show) {
#| show.output(self)
#|}
#|impl Logger with fn write_substring(self, value, start, len) {
#| self.write_view(value[start:start + len])
#|}
#|impl Logger with fn write_string(self, value) {
#| self.write_view(value)
#|}
#|#deprecated("replace `impl write_substring` with `impl write_view`")
#|impl Logger with fn write_view(self, value) {
#| self.write_substring(value.data(), value.start_offset(), value.length())
#|}
#|impl Logger with fn write_char(self, value) {
#| self.write_string([value])
#|}
#|#must_implement_one(output, to_string)
#|pub(open) trait Show {
#| fn output(Self, &Logger) -> Unit = _
#| fn to_string(Self) -> String = _
#|}
#|impl Show with fn output(self, logger) {
#| logger.write_string(self.to_string())
#|}
#|impl Show with fn to_string(self) {
#| let logger = StringBuilder()
#| self.output(logger)
#| logger.to_string()
#|}
#|pub fn[Obj : Show] &Logger::write_object(self : &Logger, obj : Obj) -> Unit {
#| obj.output(self)
#|}
#|pub fn[T : Show] &Logger::write_iter(
#| self : &Logger,
#| iter : Iter[T],
#| prefix~ : String = "[",
#| suffix~ : String = "]",
#| sep~ : String = ", ",
#| trailing~ : Bool = false,
#|) -> Unit {
#| self.write_string(prefix)
#| if trailing {
#| for x in iter {
#| self.write_object(x)
#| self.write_string(sep)
#| }
#| } else if iter.next() is Some(x) {
#| self.write_object(x)
#| for x in iter {
#| self.write_string(sep)
#| self.write_object(x)
#| }
#| }
#| self.write_string(suffix)
#|}
),
"tuple_compare.mbt": (
#|pub impl[T0 : Compare, T1 : Compare] Compare for (T0, T1) with fn compare(
#| self : (T0, T1),
#| other : (T0, T1),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| self.1.compare(other.1)
#|}
#|pub impl[T0 : Compare, T1 : Compare, T2 : Compare] Compare for (T0, T1, T2) with fn compare(
#| self : (T0, T1, T2),
#| other : (T0, T1, T2),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| self.2.compare(other.2)
#|}
#|pub impl[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare] Compare for (
#| T0,
#| T1,
#| T2,
#| T3,
#|) with fn compare(self : (T0, T1, T2, T3), other : (T0, T1, T2, T3)) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| self.3.compare(other.3)
#|}
#|pub impl[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare] Compare for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#|) with fn compare(self : (T0, T1, T2, T3, T4), other : (T0, T1, T2, T3, T4)) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| self.4.compare(other.4)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5),
#| other : (T0, T1, T2, T3, T4, T5),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| self.5.compare(other.5)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6),
#| other : (T0, T1, T2, T3, T4, T5, T6),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| self.6.compare(other.6)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| self.7.compare(other.7)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| self.8.compare(other.8)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| self.9.compare(other.9)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#| T10 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| let t9 = self.9.compare(other.9)
#| guard t9 == 0 else { return t9 }
#| self.10.compare(other.10)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#| T10 : Compare,
#| T11 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| let t9 = self.9.compare(other.9)
#| guard t9 == 0 else { return t9 }
#| let t10 = self.10.compare(other.10)
#| guard t10 == 0 else { return t10 }
#| self.11.compare(other.11)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#| T10 : Compare,
#| T11 : Compare,
#| T12 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| let t9 = self.9.compare(other.9)
#| guard t9 == 0 else { return t9 }
#| let t10 = self.10.compare(other.10)
#| guard t10 == 0 else { return t10 }
#| let t11 = self.11.compare(other.11)
#| guard t11 == 0 else { return t11 }
#| self.12.compare(other.12)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#| T10 : Compare,
#| T11 : Compare,
#| T12 : Compare,
#| T13 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| let t9 = self.9.compare(other.9)
#| guard t9 == 0 else { return t9 }
#| let t10 = self.10.compare(other.10)
#| guard t10 == 0 else { return t10 }
#| let t11 = self.11.compare(other.11)
#| guard t11 == 0 else { return t11 }
#| let t12 = self.12.compare(other.12)
#| guard t12 == 0 else { return t12 }
#| self.13.compare(other.13)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#| T10 : Compare,
#| T11 : Compare,
#| T12 : Compare,
#| T13 : Compare,
#| T14 : Compare,
#|] Compare for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| let t9 = self.9.compare(other.9)
#| guard t9 == 0 else { return t9 }
#| let t10 = self.10.compare(other.10)
#| guard t10 == 0 else { return t10 }
#| let t11 = self.11.compare(other.11)
#| guard t11 == 0 else { return t11 }
#| let t12 = self.12.compare(other.12)
#| guard t12 == 0 else { return t12 }
#| let t13 = self.13.compare(other.13)
#| guard t13 == 0 else { return t13 }
#| self.14.compare(other.14)
#|}
#|pub impl[
#| T0 : Compare,
#| T1 : Compare,
#| T2 : Compare,
#| T3 : Compare,
#| T4 : Compare,
#| T5 : Compare,
#| T6 : Compare,
#| T7 : Compare,
#| T8 : Compare,
#| T9 : Compare,
#| T10 : Compare,
#| T11 : Compare,
#| T12 : Compare,
#| T13 : Compare,
#| T14 : Compare,
#| T15 : Compare,
#|] Compare for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#| T6,
#| T7,
#| T8,
#| T9,
#| T10,
#| T11,
#| T12,
#| T13,
#| T14,
#| T15,
#|) with fn compare(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15),
#|) -> Int32 {
#| let t0 = self.0.compare(other.0)
#| guard t0 == 0 else { return t0 }
#| let t1 = self.1.compare(other.1)
#| guard t1 == 0 else { return t1 }
#| let t2 = self.2.compare(other.2)
#| guard t2 == 0 else { return t2 }
#| let t3 = self.3.compare(other.3)
#| guard t3 == 0 else { return t3 }
#| let t4 = self.4.compare(other.4)
#| guard t4 == 0 else { return t4 }
#| let t5 = self.5.compare(other.5)
#| guard t5 == 0 else { return t5 }
#| let t6 = self.6.compare(other.6)
#| guard t6 == 0 else { return t6 }
#| let t7 = self.7.compare(other.7)
#| guard t7 == 0 else { return t7 }
#| let t8 = self.8.compare(other.8)
#| guard t8 == 0 else { return t8 }
#| let t9 = self.9.compare(other.9)
#| guard t9 == 0 else { return t9 }
#| let t10 = self.10.compare(other.10)
#| guard t10 == 0 else { return t10 }
#| let t11 = self.11.compare(other.11)
#| guard t11 == 0 else { return t11 }
#| let t12 = self.12.compare(other.12)
#| guard t12 == 0 else { return t12 }
#| let t13 = self.13.compare(other.13)
#| guard t13 == 0 else { return t13 }
#| let t14 = self.14.compare(other.14)
#| guard t14 == 0 else { return t14 }
#| self.15.compare(other.15)
#|}
),
"tuple_eq.mbt": (
#|pub impl[T0 : Eq, T1 : Eq] Eq for (T0, T1) with fn equal(
#| self : (T0, T1),
#| other : (T0, T1),
#|) -> Bool {
#| self.0 == other.0 && self.1 == other.1
#|}
#|pub impl[T0 : Eq, T1 : Eq, T2 : Eq] Eq for (T0, T1, T2) with fn equal(
#| self : (T0, T1, T2),
#| other : (T0, T1, T2),
#|) -> Bool {
#| self.0 == other.0 && self.1 == other.1 && self.2 == other.2
#|}
#|pub impl[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq] Eq for (T0, T1, T2, T3) with fn equal(
#| self : (T0, T1, T2, T3),
#| other : (T0, T1, T2, T3),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3
#|}
#|pub impl[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq] Eq for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#|) with fn equal(self : (T0, T1, T2, T3, T4), other : (T0, T1, T2, T3, T4)) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4
#|}
#|pub impl[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq] Eq for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#|) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5),
#| other : (T0, T1, T2, T3, T4, T5),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5
#|}
#|pub impl[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq] Eq for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#| T6,
#|) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6),
#| other : (T0, T1, T2, T3, T4, T5, T6),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6
#|}
#|pub impl[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq] Eq for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#| T6,
#| T7,
#|) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#| T10 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9 &&
#| self.10 == other.10
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#| T10 : Eq,
#| T11 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9 &&
#| self.10 == other.10 &&
#| self.11 == other.11
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#| T10 : Eq,
#| T11 : Eq,
#| T12 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9 &&
#| self.10 == other.10 &&
#| self.11 == other.11 &&
#| self.12 == other.12
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#| T10 : Eq,
#| T11 : Eq,
#| T12 : Eq,
#| T13 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9 &&
#| self.10 == other.10 &&
#| self.11 == other.11 &&
#| self.12 == other.12 &&
#| self.13 == other.13
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#| T10 : Eq,
#| T11 : Eq,
#| T12 : Eq,
#| T13 : Eq,
#| T14 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9 &&
#| self.10 == other.10 &&
#| self.11 == other.11 &&
#| self.12 == other.12 &&
#| self.13 == other.13 &&
#| self.14 == other.14
#|}
#|pub impl[
#| T0 : Eq,
#| T1 : Eq,
#| T2 : Eq,
#| T3 : Eq,
#| T4 : Eq,
#| T5 : Eq,
#| T6 : Eq,
#| T7 : Eq,
#| T8 : Eq,
#| T9 : Eq,
#| T10 : Eq,
#| T11 : Eq,
#| T12 : Eq,
#| T13 : Eq,
#| T14 : Eq,
#| T15 : Eq,
#|] Eq for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) with fn equal(
#| self : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15),
#| other : (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15),
#|) -> Bool {
#| self.0 == other.0 &&
#| self.1 == other.1 &&
#| self.2 == other.2 &&
#| self.3 == other.3 &&
#| self.4 == other.4 &&
#| self.5 == other.5 &&
#| self.6 == other.6 &&
#| self.7 == other.7 &&
#| self.8 == other.8 &&
#| self.9 == other.9 &&
#| self.10 == other.10 &&
#| self.11 == other.11 &&
#| self.12 == other.12 &&
#| self.13 == other.13 &&
#| self.14 == other.14 &&
#| self.15 == other.15
#|}
),
"tuple_hash.mbt": (
#|pub impl[A : Hash, B : Hash] Hash for (A, B) with fn hash_combine(self, hasher) {
#| let (a, b) = self
#| hasher.combine(a)
#| hasher.combine(b)
#|}
#|pub impl[A : Hash, B : Hash, C : Hash] Hash for (A, B, C) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#|}
#|pub impl[A : Hash, B : Hash, C : Hash, D : Hash] Hash for (A, B, C, D) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c, d) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#|}
#|pub impl[A : Hash, B : Hash, C : Hash, D : Hash, E : Hash] Hash for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#|}
#|pub impl[A : Hash, B : Hash, C : Hash, D : Hash, E : Hash, F : Hash] Hash for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#|) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e, f) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#|}
#|pub impl[A : Hash, B : Hash, C : Hash, D : Hash, E : Hash, F : Hash, G : Hash] Hash for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#|) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e, f, g) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#|] Hash for (A, B, C, D, E, F, G, H) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e, f, g, h) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e, f, g, h, i) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e, f, g, h, i, j) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#| K : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J, K) with fn hash_combine(self, hasher) {
#| let (a, b, c, d, e, f, g, h, i, j, k) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#| hasher.combine(k)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#| K : Hash,
#| L : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J, K, L) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#| hasher.combine(k)
#| hasher.combine(l)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#| K : Hash,
#| L : Hash,
#| M : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J, K, L, M) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#| hasher.combine(k)
#| hasher.combine(l)
#| hasher.combine(m)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#| K : Hash,
#| L : Hash,
#| M : Hash,
#| N : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J, K, L, M, N) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#| hasher.combine(k)
#| hasher.combine(l)
#| hasher.combine(m)
#| hasher.combine(n)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#| K : Hash,
#| L : Hash,
#| M : Hash,
#| N : Hash,
#| O : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#| hasher.combine(k)
#| hasher.combine(l)
#| hasher.combine(m)
#| hasher.combine(n)
#| hasher.combine(o)
#|}
#|pub impl[
#| A : Hash,
#| B : Hash,
#| C : Hash,
#| D : Hash,
#| E : Hash,
#| F : Hash,
#| G : Hash,
#| H : Hash,
#| I : Hash,
#| J : Hash,
#| K : Hash,
#| L : Hash,
#| M : Hash,
#| N : Hash,
#| O : Hash,
#| P : Hash,
#|] Hash for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) with fn hash_combine(
#| self,
#| hasher,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = self
#| hasher.combine(a)
#| hasher.combine(b)
#| hasher.combine(c)
#| hasher.combine(d)
#| hasher.combine(e)
#| hasher.combine(f)
#| hasher.combine(g)
#| hasher.combine(h)
#| hasher.combine(i)
#| hasher.combine(j)
#| hasher.combine(k)
#| hasher.combine(l)
#| hasher.combine(m)
#| hasher.combine(n)
#| hasher.combine(o)
#| hasher.combine(p)
#|}
),
"tuple_show.mbt": (
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show, B : Show] Show for (A, B)
#|pub impl[A : Show, B : Show] Show for (A, B) with fn output(self, logger) {
#| let (a, b) = self
#| logger <+ "(\{a}, \{b})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show, B : Show, C : Show] Show for (A, B, C)
#|pub impl[A : Show, B : Show, C : Show] Show for (A, B, C) with fn output(
#| self,
#| logger,
#|) {
#| let (a, b, c) = self
#| logger <+ "(\{a}, \{b}, \{c})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show, B : Show, C : Show, D : Show] Show for (A, B, C, D)
#|pub impl[A : Show, B : Show, C : Show, D : Show] Show for (A, B, C, D) with fn output(
#| self,
#| logger,
#|) {
#| let (a, b, c, d) = self
#| logger <+ "(\{a}, \{b}, \{c}, \{d})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show, B : Show, C : Show, D : Show, E : Show] Show for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|)
#|pub impl[A : Show, B : Show, C : Show, D : Show, E : Show] Show for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|) with fn output(self, logger) {
#| let (a, b, c, d, e) = self
#| logger <+ "(\{a}, \{b}, \{c}, \{d}, \{e})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show, B : Show, C : Show, D : Show, E : Show, F : Show] Show for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#|)
#|pub impl[A : Show, B : Show, C : Show, D : Show, E : Show, F : Show] Show for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#|) with fn output(self, logger) {
#| let (a, b, c, d, e, f) = self
#| logger <+ "(\{a}, \{b}, \{c}, \{d}, \{e}, \{f})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show, B : Show, C : Show, D : Show, E : Show, F : Show, G : Show] Show for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#|)
#|pub impl[A : Show, B : Show, C : Show, D : Show, E : Show, F : Show, G : Show] Show for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#| G,
#|) with fn output(self, logger) {
#| let (a, b, c, d, e, f, g) = self
#| logger <+ "(\{a}, \{b}, \{c}, \{d}, \{e}, \{f}, \{g})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7) with fn output(self, logger) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7) = self
#| logger <+ "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8) with fn output(self, logger) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8) = self
#| logger <+ "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) with fn output(self, logger) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) with fn output(
#| self,
#| logger,
#|) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9}, \{x10})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) with fn output(
#| self,
#| logger,
#|) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9}, \{x10}, \{x11})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) with fn output(
#| self,
#| logger,
#|) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9}, \{x10}, \{x11}, \{x12})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#| T13 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#| T13 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) with fn output(
#| self,
#| logger,
#|) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9}, \{x10}, \{x11}, \{x12}, \{x13})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#| T13 : Show,
#| T14 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#| T13 : Show,
#| T14 : Show,
#|] Show for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) with fn output(
#| self,
#| logger,
#|) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9}, \{x10}, \{x11}, \{x12}, \{x13}, \{x14})"
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#| T13 : Show,
#| T14 : Show,
#| T15 : Show,
#|] Show for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#| T6,
#| T7,
#| T8,
#| T9,
#| T10,
#| T11,
#| T12,
#| T13,
#| T14,
#| T15,
#|)
#|pub impl[
#| T0 : Show,
#| T1 : Show,
#| T2 : Show,
#| T3 : Show,
#| T4 : Show,
#| T5 : Show,
#| T6 : Show,
#| T7 : Show,
#| T8 : Show,
#| T9 : Show,
#| T10 : Show,
#| T11 : Show,
#| T12 : Show,
#| T13 : Show,
#| T14 : Show,
#| T15 : Show,
#|] Show for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#| T6,
#| T7,
#| T8,
#| T9,
#| T10,
#| T11,
#| T12,
#| T13,
#| T14,
#| T15,
#|) with fn output(self, logger) {
#| let (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) = self
#| logger <+
#| "(\{x0}, \{x1}, \{x2}, \{x3}, \{x4}, \{x5}, \{x6}, \{x7}, \{x8}, \{x9}, \{x10}, \{x11}, \{x12}, \{x13}, \{x14}, \{x15})"
#|}
),
"uint.mbt": (
#|pub fn UInt32::UInt(self : UInt32) -> UInt32 = "%identity"
#|pub impl Default for UInt32 with fn default() {
#| 0
#|}
#|pub fn UInt32::to_int64(self : UInt32) -> Int64 {
#| self.to_uint64().reinterpret_as_int64()
#|}
#|pub fn UInt32::min(self : UInt32, other : UInt32) -> UInt32 {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn UInt32::max(self : UInt32, other : UInt32) -> UInt32 {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn UInt32::clamp(self : UInt32, min~ : UInt32, max~ : UInt32) -> UInt32 {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
),
"uint16_char.mbt": (
#|pub fn UInt16::is_leading_surrogate(self : Self) -> Bool {
#| self >= 0xD800 && self <= 0xDBFF
#|}
#|pub fn UInt16::UInt16(self : UInt16) -> UInt16 = "%identity"
#|pub fn UInt16::is_trailing_surrogate(self : Self) -> Bool {
#| self >= 0xDC00 && self <= 0xDFFF
#|}
#|pub fn UInt16::is_surrogate(self : Self) -> Bool {
#| self >= 0xD800 && self <= 0xDFFF
#|}
#|#doc(hidden)
#|pub fn UInt16::unsafe_to_char(self : UInt16) -> Char {
#| self.to_int().unsafe_to_char()
#|}
#|pub fn UInt16::to_char(self : UInt16) -> Char? {
#| if self is (0..<=0xD7FF) || self is (0xE000..<_) {
#| Some(self.unsafe_to_char())
#| } else {
#| None
#| }
#|}
#|pub impl Add for UInt16 with fn add(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() + that.to_int()).to_uint16()
#|}
#|pub impl Sub for UInt16 with fn sub(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() - that.to_int()).to_uint16()
#|}
#|pub impl Mul for UInt16 with fn mul(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() * that.to_int()).to_uint16()
#|}
#|pub impl Div for UInt16 with fn div(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() / that.to_int()).to_uint16()
#|}
#|pub impl Mod for UInt16 with fn mod(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() % that.to_int()).to_uint16()
#|}
#|pub impl Eq for UInt16 with fn equal(self, that) {
#| self.to_int() == that.to_int()
#|}
#|pub impl Eq for UInt16 with fn not_equal(self, that) {
#| self.to_int() != that.to_int()
#|}
#|pub impl Compare for UInt16 with fn compare(self, that) {
#| self.to_int().compare(that.to_int())
#|}
#|pub impl Compare for UInt16 with fn lt(x, y) = "%i32.lt"
#|pub impl Compare for UInt16 with fn le(x, y) = "%i32.le"
#|pub impl Compare for UInt16 with fn gt(x, y) = "%i32.gt"
#|pub impl Compare for UInt16 with fn ge(x, y) = "%i32.ge"
#|pub impl Hash for UInt16 with fn hash_combine(self, hasher) {
#| hasher.combine_int(self.to_int())
#|}
#|pub impl Shl for UInt16 with fn shl(self : UInt16, that : Int32) -> UInt16 {
#| (self.to_int() << that).to_uint16()
#|}
#|pub impl Shr for UInt16 with fn shr(self : UInt16, that : Int32) -> UInt16 {
#| (self.to_int() >> that).to_uint16()
#|}
#|pub impl BitOr for UInt16 with fn lor(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() | that.to_int()).to_uint16()
#|}
#|pub impl BitAnd for UInt16 with fn land(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() & that.to_int()).to_uint16()
#|}
#|pub impl BitXOr for UInt16 with fn lxor(self : UInt16, that : UInt16) -> UInt16 {
#| (self.to_int() ^ that.to_int()).to_uint16()
#|}
#|pub fn UInt16::lnot(self : UInt16) -> UInt16 {
#| self.to_int().lnot().to_uint16()
#|}
#|pub impl Default for UInt16 with fn default() {
#| 0
#|}
#|pub fn UInt16::to_uint(self : UInt16) -> UInt32 {
#| self.to_int().reinterpret_as_uint()
#|}
#|pub fn UInt16::to_uint64(self : UInt16) -> UInt64 {
#| self.to_int().to_uint64()
#|}
#|pub fn UInt16::to_double(self : UInt16) -> Double {
#| self.to_int().to_double()
#|}
#|pub fn UInt16::min(self : UInt16, other : UInt16) -> UInt16 {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn UInt16::max(self : UInt16, other : UInt16) -> UInt16 {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn UInt16::clamp(self : UInt16, min~ : UInt16, max~ : UInt16) -> UInt16 {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub fn UInt16::clz(self : UInt16) -> Int32 {
#| self.to_uint().clz() - 16
#|}
#|pub fn UInt16::ctz(self : UInt16) -> Int32 {
#| (self.to_uint() | 0x10000U).ctz()
#|}
#|pub fn UInt16::popcnt(self : UInt16) -> Int32 {
#| self.to_uint().popcnt()
#|}
),
"uint64.mbt": (
#|pub fn UInt64::UInt64(self : UInt64) -> UInt64 = "%identity"
#|pub impl Default for UInt64 with fn default() {
#| 0
#|}
#|test {
#| inspect(0x7000_0001_1F00_100FUL.popcnt(), content="14")
#|}
#|pub fn UInt64::to_be_bytes(self : UInt64) -> Bytes {
#| [
#| (self >> 56).to_byte(),
#| (self >> 48).to_byte(),
#| (self >> 40).to_byte(),
#| (self >> 32).to_byte(),
#| (self >> 24).to_byte(),
#| (self >> 16).to_byte(),
#| (self >> 8).to_byte(),
#| self.to_byte(),
#| ]
#|}
#|pub fn UInt64::to_le_bytes(self : UInt64) -> Bytes {
#| [
#| self.to_byte(),
#| (self >> 8).to_byte(),
#| (self >> 16).to_byte(),
#| (self >> 24).to_byte(),
#| (self >> 32).to_byte(),
#| (self >> 40).to_byte(),
#| (self >> 48).to_byte(),
#| (self >> 56).to_byte(),
#| ]
#|}
#|pub fn UInt64::min(self : UInt64, other : UInt64) -> UInt64 {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn UInt64::max(self : UInt64, other : UInt64) -> UInt64 {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn UInt64::clamp(self : UInt64, min~ : UInt64, max~ : UInt64) -> UInt64 {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
),
"uninitialized_array.mbt": (
#|struct UninitializedArray[T](FixedArray[UnsafeMaybeUninit[T]])
#|pub fn[T] UninitializedArray::make(size : Int32) -> UninitializedArray[T] = "%fixedarray.make_uninit"
#|#alias("_[_]")
#|pub fn[T] UninitializedArray::at(
#| self : UninitializedArray[T],
#| index : Int32,
#|) -> T = "%fixedarray.get"
#|#internal(unsafe, "For internal use only.")
#|fn[T] UninitializedArray::unsafe_get(
#| self : UninitializedArray[T],
#| index : Int32,
#|) -> T = "%fixedarray.unsafe_get"
#|#alias("_[_]=_")
#|pub fn[T] UninitializedArray::set(
#| self : UninitializedArray[T],
#| index : Int32,
#| value : T,
#|) = "%fixedarray.set"
#|#internal(unsafe, "For internal use only.")
#|fn[T] UninitializedArray::unsafe_set(
#| self : UninitializedArray[T],
#| index : Int32,
#| value : T,
#|) -> Unit = "%fixedarray.unsafe_set"
#|#alias(sub, deprecated="Use `exact_view` instead")
#|pub fn[T] UninitializedArray::exact_view(
#| self : UninitializedArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let end = match end {
#| Some(end) | (None with end = len) => end
#| }
#| guard start >= 0 && start <= end && end <= len else {
#| abort("View index out of bounds")
#| }
#| self.unsafe_view(start~, end~)
#|}
#|#alias("_[_:_]")
#|pub fn[T] UninitializedArray::clamped_view(
#| self : UninitializedArray[T],
#| start~ : Int32 = 0,
#| end? : Int32,
#|) -> ArrayView[T] {
#| let len = self.length()
#| let lo = clamp_offset(start, len)
#| let hi = match end {
#| None => len
#| Some(end) => clamp_offset(end, len)
#| }
#| let hi = if hi > lo { hi } else { lo }
#| self.unsafe_view(start=lo, end=hi)
#|}
#|pub fn[A] UninitializedArray::length(self : UninitializedArray[A]) -> Int32 {
#| self.0.length()
#|}
#|#internal(unsafe, "For internal use only.")
#|#doc(hidden)
#|pub fn[T] UninitializedArray::unsafe_blit(
#| dst : UninitializedArray[T],
#| dst_offset : Int32,
#| src : UninitializedArray[T],
#| src_offset : Int32,
#| len : Int32,
#|) -> Unit {
#| FixedArray::unsafe_blit(dst.0, dst_offset, src.0, src_offset, len)
#|}
#|#intrinsic("%fixedarray.make_with_blit")
#|#owned(src)
#|fn[T] UninitializedArray::unsafe_make_and_blit(
#| src : UninitializedArray[T],
#| allocate_len : Int32,
#| src_offset : Int32,
#| dst_offset : Int32,
#| blit_len : Int32,
#|) -> UninitializedArray[T] {
#| let dst = UninitializedArray::make(allocate_len)
#| UninitializedArray::unsafe_blit(dst, dst_offset, src, src_offset, blit_len)
#| dst
#|}
#|#intrinsic("%fixedarray.make_with_init_blit")
#|#owned(src, init)
#|fn[T] UninitializedArray::unsafe_make_and_blit_with_init(
#| src : UninitializedArray[T],
#| allocate_len : Int32,
#| init : T,
#| src_offset : Int32,
#| dst_offset : Int32,
#| blit_len : Int32,
#|) -> UninitializedArray[T] {
#| let dst = UninitializedArray::make(allocate_len)
#| dst.unchecked_fill(0, init, allocate_len)
#| UninitializedArray::unsafe_blit(dst, dst_offset, src, src_offset, blit_len)
#| dst
#|}
#|#owned(src)
#|pub fn[T] UninitializedArray::make_and_blit(
#| src : UninitializedArray[T],
#| allocate_len~ : Int32,
#| len~ : Int32,
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> UninitializedArray[T] {
#| guard allocate_len >= 0 &&
#| len >= 0 &&
#| range_fits(src_offset, len, src.length()) &&
#| range_fits(dst_offset, len, allocate_len) else {
#| abort(
#| "bounds check failed: allocate_len = \{allocate_len}, src_offset = \{src_offset}, dst_offset = \{dst_offset}, len = \{len}, src.length = \{src.length()}",
#| )
#| }
#| UninitializedArray::unsafe_make_and_blit(
#| src, allocate_len, src_offset, dst_offset, len,
#| )
#|}
#|#intrinsic("%fixedarray.make_with_blit")
#|#owned(src)
#|fn[T] UninitializedArray::unsafe_make_and_blit_from_fixed(
#| src : FixedArray[T],
#| allocate_len : Int32,
#| src_offset : Int32,
#| dst_offset : Int32,
#| blit_len : Int32,
#|) -> UninitializedArray[T] {
#| let dst = UninitializedArray::make(allocate_len)
#| UninitializedArray::unsafe_blit_fixed(
#| dst, dst_offset, src, src_offset, blit_len,
#| )
#| dst
#|}
#|test "as_view with valid_range" {
#| let arr : UninitializedArray[Int32] = UninitializedArray::make(5)
#| let view = arr[1:4]
#| inspect(view.start(), content="1")
#| inspect(view.length(), content="3")
#|}
#|test "panic as_view with invalid_start" {
#| let arr : UninitializedArray[Int32] = UninitializedArray::make(5)
#| ignore(arr.exact_view(start=-1))
#|}
#|test "panic as_view with invalid_end" {
#| let arr : UninitializedArray[Int32] = UninitializedArray::make(5)
#| ignore(arr.exact_view(start=2, end=10))
#|}
#|#coverage.skip
#|#intrinsic("%fixedarray.fill")
#|#owned(value)
#|fn[T] UninitializedArray::unchecked_fill(
#| self : UninitializedArray[T],
#| start : Int32,
#| value : T,
#| len : Int32,
#|) -> Unit {
#| for i in start..<(start + len) {
#| self[i] = value
#| }
#|}
),
"unit.mbt": (
#|pub impl Eq for Unit with fn equal(_ : Unit, _ : Unit) -> Bool {
#| true
#|}
#|pub impl Compare for Unit with fn compare(_, _) {
#| 0
#|}
#|pub impl Default for Unit with fn default() -> Unit {
#| ()
#|}
#|pub impl Hash for Unit with fn hash_combine(_, hasher) -> Unit {
#| hasher.combine_unit()
#|}
),
"unsafe_view.mbt": (
#|#intrinsic("%array.unsafe_view")
#|pub fn[T] Array::unsafe_view(
#| self : Array[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> ArrayView[T] {
#| self.buffer().unsafe_view(start~, end~)
#|}
#|#intrinsic("%arrayview.unsafe_view")
#|pub fn[T] ArrayView::unsafe_view(
#| self : ArrayView[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> ArrayView[T] {
#| self.buf().unsafe_view(start=self.start() + start, end=self.start() + end)
#|}
#|pub fn[T] FixedArray::unsafe_view(
#| self : FixedArray[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> ArrayView[T] = "%arrayview.make_view"
#|pub fn[T] ReadOnlyArray::unsafe_view(
#| self : ReadOnlyArray[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> ArrayView[T] = "%arrayview.make_view"
#|#intrinsic("%mutarrayview.unsafe_view")
#|pub fn[T] MutArrayView::unsafe_view(
#| self : MutArrayView[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> ArrayView[T] {
#| self.buf().unsafe_view(start=self.start() + start, end=self.start() + end)
#|}
#|#intrinsic("%array.unsafe_mut_view")
#|pub fn[T] Array::unsafe_mut_view(
#| self : Array[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> MutArrayView[T] {
#| MutArrayView::make_view(self.buffer(), start, end)
#|}
#|pub fn[T] FixedArray::unsafe_mut_view(
#| self : FixedArray[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> MutArrayView[T] = "%arrayview.make_view"
#|#intrinsic("%mutarrayview.unsafe_mut_view")
#|pub fn[T] MutArrayView::unsafe_mut_view(
#| self : MutArrayView[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> MutArrayView[T] {
#| MutArrayView::make_view(self.buf(), self.start() + start, self.start() + end)
#|}
#|pub fn[T] UninitializedArray::unsafe_view(
#| self : UninitializedArray[T],
#| start~ : Int32,
#| end~ : Int32,
#|) -> ArrayView[T] = "%arrayview.make_view"
#|pub fn Bytes::unsafe_view(
#| self : Bytes,
#| start~ : Int32,
#| end~ : Int32,
#|) -> BytesView = "%bytesview.make_view"
#|#intrinsic("%bytesview.unsafe_view")
#|pub fn BytesView::unsafe_view(
#| self : BytesView,
#| start~ : Int32,
#| end~ : Int32,
#|) -> BytesView {
#| self.bytes().unsafe_view(start=self.start() + start, end=self.start() + end)
#|}
#|pub fn String::unsafe_view(
#| self : String,
#| start~ : Int32,
#| end~ : Int32,
#|) -> StringView = "%stringview.make"
#|pub fn StringView::unsafe_view(
#| self : StringView,
#| start~ : Int32,
#| end~ : Int32,
#|) -> StringView {
#| self.str().unsafe_view(start=self.start() + start, end=self.start() + end)
#|}
),
"v128.mbt": (
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub impl Eq for V128 with fn equal(self : V128, other : V128) -> Bool {
#| v128_lo(self) == v128_lo(other) && v128_hi(self) == v128_hi(other)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub impl Eq for V128 with fn not_equal(self : V128, other : V128) -> Bool {
#| v128_lo(self) != v128_lo(other) || v128_hi(self) != v128_hi(other)
#|}
#|fn u64_hex_to(logger : &Logger, value : UInt64) -> Unit {
#| let digits = value.to_string(radix=16)
#| logger <+ "0x"
#| for _ in digits.length()..<16 {
#| logger.write_char('0')
#| }
#| logger.write_string(digits)
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub impl Show for V128 with fn output(self : V128, logger) {
#| logger <+
#| "V128(\{cb => u64_hex_to(cb, v128_lo(self))}, \{cb => u64_hex_to(cb, v128_hi(self))})"
#|}
),
},
)
///|
let moonbitlang_core_byte_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/byte",
deps={},
files={
"byte.mbt": (
#|pub const MAX_VALUE : Byte = b'\xFF'
#|#deprecated("Use `MAX_VALUE` instead")
#|pub let max_value : Byte = b'\xFF'
#|pub const MIN_VALUE : Byte = b'\x00'
#|#deprecated("Use `MIN_VALUE` instead")
#|pub let min_value : Byte = b'\x00'
),
},
)
///|
let moonbitlang_core_bytes_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes",
deps={},
files={
"alias.mbt": (
#|#deprecated("Use `Bytes::default` instead")
#|#doc(hidden)
#|pub fn default() -> Bytes {
#| b""
#|}
#|#deprecated("Use `Bytes::from_array` instead")
#|#doc(hidden)
#|pub fn from_fixedarray(arr : FixedArray[Byte], len? : Int32) -> Bytes {
#| Bytes::from_array(arr.exact_view(end=len.unwrap_or(arr.length())))
#|}
#|#deprecated("Use `Bytes::from_iter` instead")
#|#alias(from_iterator, deprecated="Use `Bytes::from_iter` instead")
#|#doc(hidden)
#|pub fn from_iter(iter : Iter[Byte]) -> Bytes {
#| Bytes::from_iter(iter)
#|}
#|#deprecated("Use `Bytes::from_array` instead")
#|#doc(hidden)
#|pub fn of(arr : ArrayView[Byte]) -> Bytes {
#| Bytes::from_array(arr)
#|}
#|#deprecated("Use `Bytes::from_array` instead")
#|#doc(hidden)
#|pub fn from_array(arr : ArrayView[Byte]) -> Bytes {
#| Bytes::from_array(arr)
#|}
#|#deprecated("@bytes.View is deprecated, use BytesView instead.")
#|pub type View = BytesView
),
"extends.mbt": (
#|pub extend BytesRegex with Add::{add}
#|pub extend BytesRegex with BitOr::{lor}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend MatchResult with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend BytesRegex with @debug.Debug::{to_repr}
),
"regex.mbt": (
#|struct BytesRegex {
#| pat : @re.Pattern
#| mut re : @re.Regex?
#|}
#|pub fn BytesRegex::BytesRegex(pattern : StringView) -> BytesRegex raise {
#| let pat = @regex_parser.parse(pattern)
#| { pat, re: None, }
#|}
#|pub impl @debug.Debug for BytesRegex with fn to_repr(self) {
#| @debug.Repr::opaque_("BytesRegex", Repr(self.pat))
#|}
#|#derive(@debug.Debug)
#|struct MatchResult {
#| input : BytesView
#| group_names : ReadOnlyArray[String?]
#| result : @re.MatchResult
#|}
#|pub fn MatchResult::before(self : MatchResult) -> BytesView {
#| self.input[:self.result.start(0)]
#|}
#|pub fn MatchResult::after(self : MatchResult) -> BytesView {
#| self.input[self.result.end(0):]
#|}
#|pub fn MatchResult::content(self : MatchResult) -> BytesView {
#| self.input[self.result.start(0):self.result.end(0)]
#|}
#|pub fn MatchResult::group(
#| self : MatchResult,
#| group_index : Int32,
#|) -> BytesView? {
#| let start = self.result.start(group_index)
#| let end = self.result.end(group_index)
#| guard start >= 0 && end >= 0 else { None }
#| Some(self.input[start:end])
#|}
#|pub fn MatchResult::named_group(
#| self : MatchResult,
#| name : String,
#|) -> BytesView? {
#| match self.group_names.search(Some(name)) {
#| None => None
#| Some(index) => self.group(index)
#| }
#|}
#|#internal(internal, "not intended for public use")
#|#doc(hidden)
#|pub fn BytesRegex::internal_from_string(pat : String) -> BytesRegex {
#| try! BytesRegex(pat)
#|}
#|#intrinsic("%bytes_regex.seq")
#|#warnings("-unknown_intrinsic")
#|pub impl Add for BytesRegex with fn add(self, other) -> BytesRegex {
#| { pat: @re.seq([self.pat, other.pat]), re: None, }
#|}
#|#intrinsic("%bytes_regex.alt")
#|#warnings("-unknown_intrinsic")
#|pub impl BitOr for BytesRegex with fn lor(self, other) -> BytesRegex {
#| { pat: @re.alt([self.pat, other.pat]), re: None, }
#|}
#|fn BytesRegex::re(self : BytesRegex) -> @re.Regex {
#| match self.re {
#| Some(re) => re
#| None => {
#| let re = @re.compile(self.pat)
#| self.re = Some(re)
#| re
#| }
#| }
#|}
#|pub fn BytesRegex::execute(
#| self : BytesRegex,
#| input : BytesView,
#| last_index~ : Int32 = 0,
#|) -> MatchResult? {
#| let re = self.re()
#| match re.execute(input, last_index) {
#| None => None
#| Some(result) => Some({ input, group_names: re.group_names(), result, })
#| }
#|}
#|#doc(hidden)
#|pub fn BytesRegex::internal_search(
#| self : BytesRegex,
#| input : BytesView,
#|) -> FixedArray[Int32]? {
#| self.re().search(input, 0)
#|}
),
},
)
///|
let moonbitlang_core_bytes_internal_regex_engine_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes/internal/regex_engine",
deps={},
files={
"dfa.mbt": (
#|fn Regex::width(self : Regex) -> Int32 {
#| self.nsym + 1
#|}
#|fn terminal(state : Int32) -> Int32 {
#| DEAD - 1 - 2 * state
#|}
#|fn terminal_state(entry : Int32) -> Int32 {
#| (DEAD - 1 - entry) / 2
#|}
#|fn moving(index : Int32) -> Int32 {
#| DEAD - 2 - 2 * index
#|}
#|fn is_moving(entry : Int32) -> Bool {
#| entry < DEAD && ((DEAD - 1 - entry) & 1) == 1
#|}
#|fn Regex::move_registers(self : Regex, pairs : FixedArray[Int32]) -> Unit {
#| let count = pairs.length() / 2
#| if self.move_values.length() < count {
#| self.move_values = FixedArray::make(count, 0)
#| }
#| let values = self.move_values
#| let regs = self.regs
#| for i in 0.. Int32 {
#| let taken = self.moves[(DEAD - 2 - entry) / 2]
#| self.move_registers(taken.pairs)
#| taken.target
#|}
#|fn Regex::flush(self : Regex) -> Unit {
#| let used = self.kernels.length() * self.width()
#| self.state_ids.clear()
#| self.kernels.clear()
#| self.trans.fill(UNKNOWN, end=used)
#| self.slots.fill(0, end=used)
#| self.start_bases.fill(UNKNOWN)
#| self.cache_words = 0
#| self.shapes = None
#| self.moves.clear()
#| self.move_words = 0
#|}
#|fn Regex::cost(self : Regex, key : FixedArray[Int32]) -> Int32 {
#| key.length() + 2 * self.width()
#|}
#|fn Regex::intern(
#| self : Regex,
#| key : FixedArray[Int32],
#| threads : Int32,
#|) -> Int32 {
#| self.state_ids.get_or_init(key, () => self.allocate_state(key, threads))
#|}
#|fn Regex::insert(
#| self : Regex,
#| key : FixedArray[Int32],
#| threads : Int32,
#|) -> Int32 {
#| let state = self.allocate_state(key, threads)
#| self.state_ids.set(key, state)
#| state
#|}
#|fn Regex::allocate_state(
#| self : Regex,
#| key : FixedArray[Int32],
#| threads : Int32,
#|) -> Int32 {
#| let width = self.width()
#| let state = self.kernels.length()
#| let needed = (state + 1) * width
#| if needed > self.trans.length() {
#| let capacity = Int32::max(needed, self.trans.length() * 2)
#| self.trans = FixedArray::make_and_blit(
#| self.trans,
#| allocate_len=capacity,
#| init=UNKNOWN,
#| len=self.trans.length(),
#| )
#| self.slots = FixedArray::make_and_blit(
#| self.slots,
#| allocate_len=capacity,
#| init=0,
#| len=self.slots.length(),
#| )
#| }
#| self.kernels.push({ key, threads, })
#| self.cache_words += self.cost(key)
#| if self.shapes is Some(shapes) {
#| add_shape(
#| shapes,
#| shape_hash(key, threads, Ranks::new(self.regs.length())),
#| state,
#| )
#| }
#| state
#|}
#|priv struct Ranks {
#| rank : FixedArray[Int32]
#| stamp : FixedArray[Int32]
#| mut epoch : Int32
#|}
#|fn Ranks::new(registers : Int32) -> Ranks {
#| {
#| rank: FixedArray::make(registers, 0),
#| stamp: FixedArray::make(registers, 0),
#| epoch: 0,
#| }
#|}
#|fn shape_hash(key : FixedArray[Int32], threads : Int32, ranks : Ranks) -> Int32 {
#| ranks.epoch += 1
#| let epoch = ranks.epoch
#| let hasher = Hasher()
#| let rows = 2 + threads
#| for i in 0.. Unit {
#| match shapes.get(hash) {
#| Some(states) => states.push(state)
#| None => shapes.set(hash, [state])
#| }
#|}
#|fn Regex::renaming(
#| self : Regex,
#| key : FixedArray[Int32],
#| threads : Int32,
#| state : Int32,
#|) -> FixedArray[Int32]? {
#| let other = self.kernels[state]
#| let theirs = other.key
#| guard other.threads == threads && theirs.length() == key.length() else {
#| None
#| }
#| let rows = 2 + threads
#| for i in 0.. (Int32, FixedArray[Int32])? {
#| let shapes = match self.shapes {
#| Some(shapes) => shapes
#| None => {
#| let shapes = @hashmap.HashMap([])
#| let ranks = Ranks::new(self.regs.length())
#| for state, kernel in self.kernels {
#| add_shape(shapes, shape_hash(kernel.key, kernel.threads, ranks), state)
#| }
#| self.shapes = Some(shapes)
#| shapes
#| }
#| }
#| let hash = shape_hash(key, threads, Ranks::new(self.regs.length()))
#| guard shapes.get(hash) is Some(candidates) else { None }
#| for state in candidates {
#| guard self.renaming(key, threads, state) is Some(forward) else { continue }
#| let pairs : Array[Int32] = []
#| for register, target in forward {
#| if target >= 0 && target != register {
#| pairs.push(register)
#| pairs.push(target)
#| }
#| }
#| return Some((state, FixedArray::from_array(pairs)))
#| }
#| None
#|}
#|fn Regex::start_base(self : Regex, ctx : Int32) -> Int32 {
#| let base = self.start_bases[ctx]
#| if base != UNKNOWN {
#| return base
#| }
#| let key : FixedArray[Int32] = [ctx, 0, self.entry, 0]
#| let state = self.intern(key, 1)
#| let base = state * self.width()
#| self.start_bases[ctx] = base
#| base
#|}
#|const EPOCH_LIMIT : Int32 = 0x7fffffff
#|fn Regex::next_epoch(self : Regex) -> Int32 {
#| if self.epoch >= EPOCH_LIMIT {
#| self.visited.fill(0)
#| self.targeted.fill(0)
#| self.epoch = 0
#| }
#| self.epoch += 1
#| self.epoch
#|}
#|fn Regex::build(
#| self : Regex,
#| base : Int32,
#| column : Int32,
#| pos : Int32,
#|) -> Int32 {
#| let nodes = self.nodes
#| let width = self.width()
#| let kernel = self.kernels[base / width]
#| let cat = self.col_cat[column]
#| let at_end = column == self.nsym
#| let epoch = self.next_epoch()
#| let visited = self.visited
#| let targeted = self.targeted
#| let out_nodes : Array[Int32] = []
#| let out_tags : Array[FixedArray[Int32]] = []
#| let mut accept : FixedArray[Int32]? = None
#| let stack : Array[(Int32, FixedArray[Int32])] = []
#| let threads = kernel.threads
#| let ctx = kernel.ctx()
#| let mut offset = 2 + threads
#| for i in 0.. continue next, set_current(tags, tag)
#| After(bit, next) => if (ctx & (1 << bit)) != 0 { continue next, tags }
#| Before(expected, next) =>
#| if cat.intersects(expected) {
#| continue next, tags
#| }
#| Union(alts) => {
#| for j in (alts.length() - 1)>=..1 {
#| stack.push((alts[j], tags))
#| }
#| if alts is [first, ..] {
#| continue first, tags
#| }
#| }
#| Char(set, next) =>
#| if !at_end && targeted[next] != epoch && set.contains(column) {
#| targeted[next] = epoch
#| out_nodes.push(next)
#| out_tags.push(tags)
#| }
#| Accept => {
#| accept = Some(tags)
#| break
#| }
#| }
#| }
#| match stack.pop() {
#| Some((next, tags)) => continue next, tags
#| None => break
#| }
#| }
#| if accept is Some(_) {
#| break
#| }
#| }
#| if accept is None && kernel.accepted() {
#| accept = Some(kernel.row(offset))
#| }
#| if accept is Some(tags) {
#| out_tags.push(tags)
#| }
#| let rows = out_tags.length()
#| let mut fresh = false
#| let used : Array[Bool] = [true]
#| let mut size = 2 + out_nodes.length()
#| for tags in out_tags {
#| size += 1 + tags.length()
#| for i = 1; i < tags.length(); i = i + 2 {
#| let slot = tags[i]
#| if slot == CURRENT {
#| fresh = true
#| } else {
#| while used.length() <= slot {
#| used.push(false)
#| }
#| used[slot] = true
#| }
#| }
#| }
#| let slot = if fresh { used.search(false).unwrap_or(used.length()) } else { 0 }
#| if slot >= self.regs.length() {
#| self.regs = FixedArray::make_and_blit(
#| self.regs,
#| allocate_len=Int32::max(slot + 1, self.regs.length() * 2),
#| init=-1,
#| len=self.regs.length(),
#| )
#| }
#| self.built_slot = slot
#| guard rows > 0 else {
#| self.trans[base + column] = DEAD
#| self.slots[base + column] = 0
#| DEAD
#| }
#| let count = out_nodes.length()
#| let key = FixedArray::make(size, 0)
#| key[0] = if at_end { 0 } else { self.sym_ctx[column] }
#| key[1] = if accept is Some(_) { 1 } else { 0 }
#| for i, node in out_nodes {
#| key[2 + i] = node
#| }
#| let mut at = 2 + count
#| for tags in out_tags {
#| key[at] = tags.length() / 2
#| for i = 0; i < tags.length(); i = i + 2 {
#| key[at + 1 + i] = tags[i]
#| key[at + 2 + i] = if tags[i + 1] == CURRENT { slot } else { tags[i + 1] }
#| }
#| at += 1 + tags.length()
#| }
#| let encode = (state : Int32) => {
#| if count == 0 {
#| terminal(state)
#| } else {
#| state * width
#| }
#| }
#| if self.cache_words + self.cost(key) <= self.max_cache_words {
#| let entry = encode(self.intern(key, count))
#| self.trans[base + column] = entry
#| self.slots[base + column] = slot
#| return entry
#| }
#| match self.state_ids.get(key) {
#| Some(state) => {
#| let entry = encode(state)
#| self.trans[base + column] = entry
#| self.slots[base + column] = slot
#| entry
#| }
#| None =>
#| if self.reuse(key, count) is Some((state, pairs)) {
#| let entry = encode(state)
#| self.regs[slot] = pos
#| self.move_registers(pairs)
#| self.built_slot = 0
#| let words = pairs.length() + 2
#| if self.move_words + words <= self.max_cache_words {
#| self.move_words += words
#| self.trans[base + column] = moving(self.moves.length())
#| self.slots[base + column] = slot
#| self.moves.push({ pairs, target: entry, })
#| }
#| entry
#| } else {
#| self.flush()
#| encode(self.insert(key, count))
#| }
#| }
#|}
),
"execute.mbt": (
#|#derive(@debug.Debug)
#|struct MatchResult(FixedArray[Int32])
#|pub fn MatchResult::group(self : MatchResult, index : Int32) -> (Int32, Int32)? {
#| let start = self.start(index)
#| let end = self.end(index)
#| guard start >= 0 && end >= 0 else { None }
#| Some((start, end))
#|}
#|pub fn MatchResult::start(self : MatchResult, index : Int32) -> Int32 {
#| guard index >= 0 && index < self.0.length() / 2 else { -1 }
#| self.0[index * 2]
#|}
#|pub fn MatchResult::end(self : MatchResult, index : Int32) -> Int32 {
#| guard index >= 0 && index < self.0.length() / 2 else { -1 }
#| self.0[index * 2 + 1]
#|}
#|pub fn Regex::execute(
#| self : Regex,
#| input : BytesView,
#| last_index : Int32,
#|) -> MatchResult? {
#| match self.search(input, last_index) {
#| None => None
#| Some(offsets) => Some(MatchResult(offsets.copy()))
#| }
#|}
#|pub fn Regex::search(
#| self : Regex,
#| input : BytesView,
#| last_index : Int32,
#|) -> FixedArray[Int32]? {
#| guard last_index >= 0 && last_index <= input.length() else { panic() }
#| let table = self.table
#| let data = input.data()
#| let shift = input.start_offset()
#| let end = shift + input.length()
#| let ctx = if last_index == 0 {
#| self.ctx_start
#| } else {
#| self.sym_ctx[table.map(input.unsafe_get(last_index - 1))]
#| }
#| let mut base = self.start_base(ctx)
#| let mut trans = self.trans
#| let mut slots = self.slots
#| let mut regs = self.regs
#| let mut pos = shift + last_index
#| let mut stop = 0
#| while pos < end {
#| let index = base + table.map(data.unsafe_get(pos))
#| let mut next = trans.unsafe_get(index)
#| if next < 0 {
#| if next == UNKNOWN {
#| next = self.build(base, index - base, pos)
#| trans = self.trans
#| slots = self.slots
#| regs = self.regs
#| regs.unsafe_set(self.built_slot, pos)
#| if next >= 0 {
#| base = next
#| pos += 1
#| continue
#| }
#| } else {
#| regs.unsafe_set(slots.unsafe_get(index), pos)
#| if is_moving(next) {
#| next = self.follow(next)
#| if next >= 0 {
#| base = next
#| pos += 1
#| continue
#| }
#| }
#| }
#| stop = next
#| break
#| }
#| regs.unsafe_set(slots.unsafe_get(index), pos)
#| base = next
#| pos += 1
#| }
#| if stop == 0 {
#| let index = base + self.nsym
#| stop = trans.unsafe_get(index)
#| if stop == UNKNOWN {
#| stop = self.build(base, self.nsym, pos)
#| regs = self.regs
#| regs.unsafe_set(self.built_slot, pos)
#| } else {
#| regs.unsafe_set(slots.unsafe_get(index), pos)
#| if is_moving(stop) {
#| stop = self.follow(stop)
#| }
#| }
#| }
#| guard stop != DEAD else { None }
#| let key = self.kernels[terminal_state(stop)].key
#| let offsets = self.offsets
#| for i in 0.. Int32 {
#| self.nodes.push(node)
#| self.nodes.length() - 1
#|}
#|fn Builder::after_bit(self : Builder, cat : Category) -> Int32 {
#| match self.after_cats.search(cat) {
#| Some(i) => i
#| None => {
#| self.after_cats.push(cat)
#| self.after_cats.length() - 1
#| }
#| }
#|}
#|fn ir_after(cat : Category) -> Ir {
#| IAfter(cat)
#|}
#|fn ir_before(cat : Category) -> Ir {
#| IBefore(cat)
#|}
#|fn lower_assertion(asrt : Assertion) -> Ir {
#| match asrt {
#| StartOfInput => ir_after(Category::inexistant())
#| EndOfInput => ir_before(Category::inexistant())
#| StartOfLine => ir_after(Category::inexistant_or_newline())
#| EndOfLine => ir_before(Category::inexistant_or_newline())
#| StartOfWord =>
#| ISeq([
#| ir_after(Category::inexistant_or_non_word_end()),
#| ir_before(Category::word()),
#| ])
#| EndOfWord =>
#| ISeq([
#| ir_after(Category::word()),
#| ir_before(Category::inexistant_or_non_word_start()),
#| ])
#| NotWordBoundary =>
#| IAlt([
#| ISeq([ir_after(Category::word()), ir_before(Category::word())]),
#| ISeq([
#| ir_after(Category::inexistant_or_non_word_end()),
#| ir_before(Category::inexistant_or_non_word_start()),
#| ]),
#| ])
#| }
#|}
#|fn Builder::lower(self : Builder, pat : Pattern) -> Ir {
#| match pat.desc {
#| Char(set) => IChar(self.table.map_set(set))
#| Sequence(pats) => ISeq([ for p in pats => self.lower(p) ])
#| Alternation(_) => {
#| let flat : Array[Pattern] = []
#| let stack : Array[Pattern] = [pat]
#| while stack.pop() is Some(p) {
#| match p.desc {
#| Alternation(ps) =>
#| for i in (ps.length() - 1)>=..0 {
#| stack.push(ps[i])
#| }
#| _ => flat.push(p)
#| }
#| }
#| IAlt(flat.map(p => self.lower(p)))
#| }
#| Quantifier(q, inner) => {
#| let greedy = q.mode is Greedy
#| IRepeat(
#| self.lower(inner),
#| min=q.min,
#| max=q.max,
#| greedy~,
#| nullable=inner.nullable,
#| )
#| }
#| Capture(name~, inner) => {
#| let index = self.groups.length()
#| self.groups.push(name)
#| IGroup(index, self.lower(inner))
#| }
#| Assertion(asrt) => lower_assertion(asrt)
#| }
#|}
#|fn Builder::choice(
#| self : Builder,
#| greedy : Bool,
#| first : Int32,
#| second : Int32,
#|) -> Int32 {
#| self.insert(Union(if greedy { [first, second] } else { [second, first] }))
#|}
#|fn Builder::set_choice(
#| self : Builder,
#| at : Int32,
#| greedy : Bool,
#| first : Int32,
#| second : Int32,
#|) -> Unit {
#| self.nodes[at] = Union(if greedy { [first, second] } else { [second, first] })
#|}
#|fn Builder::emit(self : Builder, ir : Ir, next : Int32) -> Int32 {
#| match ir {
#| IChar(set) => self.insert(Char(set, next))
#| ISeq(irs) =>
#| for i = irs.length() - 1, next = next; i >= 0; {
#| continue i - 1, self.emit(irs[i], next)
#| } nobreak {
#| next
#| }
#| IAlt(irs) =>
#| match irs {
#| [] => self.insert(Char(ByteSet::empty(), next))
#| [ir] => self.emit(ir, next)
#| _ =>
#| self.insert(
#| Union(FixedArray::makei(irs.length(), i => self.emit(irs[i], next))),
#| )
#| }
#| IGroup(index, inner) => {
#| let close = self.insert(Tag(index * 2 + 1, next))
#| let body = self.emit(inner, close)
#| self.insert(Tag(index * 2, body))
#| }
#| IAfter(cat) => self.insert(After(self.after_bit(cat), next))
#| IBefore(cat) => self.insert(Before(cat, next))
#| IRepeat(inner, min~, max~, greedy~, nullable~) => {
#| let rest = match max {
#| None =>
#| if min == 0 {
#| if nullable {
#| let plus = self.insert(Accept)
#| let body = self.emit(inner, plus)
#| self.set_choice(plus, greedy, body, next)
#| self.choice(greedy, body, next)
#| } else {
#| let star = self.insert(Accept)
#| let body = self.emit(inner, star)
#| self.set_choice(star, greedy, body, next)
#| star
#| }
#| } else {
#| let plus = self.insert(Accept)
#| let body = self.emit(inner, plus)
#| self.set_choice(plus, greedy, body, next)
#| body
#| }
#| Some(max) =>
#| for i = max - min, rest = next; i > 0; {
#| let body = self.emit(inner, rest)
#| continue i - 1, self.choice(greedy, body, next)
#| } nobreak {
#| rest
#| }
#| }
#| let mandatory = if max is None && min > 0 { min - 1 } else { min }
#| for i = mandatory, rest = rest; i > 0; {
#| continue i - 1, self.emit(inner, rest)
#| } nobreak {
#| rest
#| }
#| }
#| }
#|}
),
"profile.mbt": (
#|pub let word_set : ByteSet = ByteSet::char_range('a', 'z') +
#| ByteSet::char_range('A', 'Z') +
#| ByteSet::char_range('0', '9') +
#| ByteSet::char('_')
#|fn category(c : Int32) -> Category {
#| match c {
#| 'a'..<='z' | 'A'..<='Z' | '0'..<='9' | '_' => Category::word()
#| '\n' => Category::newline() + Category::not_word()
#| _ => Category::not_word()
#| }
#|}
),
"regex.mbt": (
#|const UNKNOWN : Int32 = -1
#|const DEAD : Int32 = -2
#|const CURRENT : Int32 = -1
#|const MAX_CACHE_WORDS : Int32 = 1 << 24
#|priv struct Kernel {
#| key : FixedArray[Int32]
#| threads : Int32
#|}
#|fn Kernel::ctx(self : Kernel) -> Int32 {
#| self.key[0]
#|}
#|fn Kernel::accepted(self : Kernel) -> Bool {
#| self.key[1] != 0
#|}
#|fn Kernel::node(self : Kernel, thread : Int32) -> Int32 {
#| self.key[2 + thread]
#|}
#|fn Kernel::row(self : Kernel, offset : Int32) -> FixedArray[Int32] {
#| let len = 2 * self.key[offset]
#| let row = FixedArray::make(len, 0)
#| self.key.blit_to(row, len~, src_offset=offset + 1)
#| row
#|}
#|fn set_current(row : FixedArray[Int32], tag : Int32) -> FixedArray[Int32] {
#| let len = row.length()
#| let mut i = 0
#| while i < len && row[i] < tag {
#| i += 2
#| }
#| if i < len && row[i] == tag {
#| if row[i + 1] == CURRENT {
#| return row
#| }
#| let copy = row.copy()
#| copy[i + 1] = CURRENT
#| return copy
#| }
#| let out = FixedArray::make(len + 2, CURRENT)
#| row.blit_to(out, len=i)
#| out[i] = tag
#| row.blit_to(out, len=len - i, src_offset=i, dst_offset=i + 2)
#| out
#|}
#|priv struct Move {
#| pairs : FixedArray[Int32]
#| target : Int32
#|}
#|struct Regex {
#| groups : ReadOnlyArray[String?]
#| table : @symbol_map.Table
#| nodes : FixedArray[Node]
#| entry : Int32
#| ntags : Int32
#| nsym : Int32
#| sym_ctx : FixedArray[Int32]
#| col_cat : FixedArray[Category]
#| ctx_start : Int32
#| state_ids : @hashmap.HashMap[FixedArray[Int32], Int32]
#| kernels : Array[Kernel]
#| mut trans : FixedArray[Int32]
#| mut slots : FixedArray[Int32]
#| start_bases : FixedArray[Int32]
#| mut built_slot : Int32
#| mut cache_words : Int32
#| max_cache_words : Int32
#| mut shapes : @hashmap.HashMap[Int32, Array[Int32]]?
#| moves : Array[Move]
#| mut move_words : Int32
#| mut move_values : FixedArray[Int32]
#| mut regs : FixedArray[Int32]
#| offsets : FixedArray[Int32]
#| visited : FixedArray[Int32]
#| targeted : FixedArray[Int32]
#| mut epoch : Int32
#|}
#|pub fn Regex::group_names(self : Regex) -> ReadOnlyArray[String?] {
#| self.groups
#|}
#|pub fn compile(
#| ast : Pattern,
#| max_cache_words~ : Int32 = MAX_CACHE_WORDS,
#|) -> Regex {
#| let symbol_map = @symbol_map.new()
#| symbolize(symbol_map~, ast)
#| let (table, symbol_repr) = symbol_map.finalize()
#| let nsym = symbol_repr.length()
#| let builder : Builder = { nodes: [], table, groups: [None], after_cats: [], }
#| let ir = IGroup(0, builder.lower(ast))
#| let accept = builder.insert(Accept)
#| let body = builder.emit(ir, accept)
#| let entry = if ast.is_anchored() {
#| body
#| } else {
#| let any = ByteSet::char_range(0, nsym - 1)
#| let search = builder.insert(Accept)
#| let skip = builder.insert(Char(any, search))
#| builder.set_choice(search, true, body, skip)
#| search
#| }
#| let after_cats = builder.after_cats
#| fn ctx_of(cat : Category) -> Int32 {
#| for i = 0, ctx = 0; i < after_cats.length(); {
#| continue i + 1,
#| if cat.intersects(after_cats[i]) {
#| ctx | (1 << i)
#| } else {
#| ctx
#| }
#| } nobreak {
#| ctx
#| }
#| }
#| let sym_ctx = FixedArray::makei(nsym, i => ctx_of(category(symbol_repr[i])))
#| let col_cat = FixedArray::makei(nsym + 1, i => {
#| if i == nsym {
#| Category::inexistant()
#| } else {
#| category(symbol_repr[i])
#| }
#| })
#| let nnodes = builder.nodes.length()
#| let ntags = builder.groups.length() * 2
#| {
#| groups: ReadOnlyArray::from_array(builder.groups),
#| table,
#| nodes: FixedArray::from_array(builder.nodes),
#| entry,
#| ntags,
#| nsym,
#| sym_ctx,
#| col_cat,
#| ctx_start: ctx_of(Category::inexistant()),
#| state_ids: HashMap([]),
#| kernels: [],
#| trans: [],
#| slots: [],
#| start_bases: FixedArray::make(1 << after_cats.length(), UNKNOWN),
#| built_slot: 0,
#| cache_words: 0,
#| max_cache_words,
#| shapes: None,
#| moves: [],
#| move_words: 0,
#| move_values: [],
#| regs: FixedArray::make(8, -1),
#| offsets: FixedArray::make(ntags, -1),
#| visited: FixedArray::make(nnodes, 0),
#| targeted: FixedArray::make(nnodes, 0),
#| epoch: 0,
#| }
#|}
),
"symbolize.mbt": (
#|fn symbolize(symbol_map~ : @symbol_map.SymbolMap, expr : Pattern) -> Unit {
#| fn aux(expr : Pattern) {
#| match expr.desc {
#| Char(s) => symbol_map.split(s)
#| Sequence(l) => l.each(aux)
#| Alternation(l) => l.each(aux)
#| Quantifier(_, r) => aux(r)
#| Assertion(StartOfLine) | Assertion(EndOfLine) =>
#| symbol_map.split(ByteSet::char('\n'))
#| Assertion(StartOfWord)
#| | Assertion(EndOfWord)
#| | Assertion(NotWordBoundary) => symbol_map.split(word_set)
#| Assertion(StartOfInput) | Assertion(EndOfInput) => ()
#| Capture(r, ..) => aux(r)
#| }
#| }
#| aux(expr)
#|}
),
"using.mbt": (
#|pub using @shared_types {type ByteSet, type Category, type QuantifierMode}
#|pub using @ast {
#| type Pattern,
#| type Assertion,
#| type Quantifier,
#| char,
#| epsilon,
#| seq,
#| empty,
#| alt,
#| quantifier,
#| capture,
#| assertion,
#| start_of_input,
#| end_of_input,
#| start_of_line,
#| end_of_line,
#| start_of_word,
#| end_of_word,
#| not_word_boundary,
#|}
),
},
)
///|
let moonbitlang_core_bytes_internal_regex_engine_ast_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes/internal/regex_engine/ast",
deps={},
files={
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Assertion with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Pattern with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PatternDesc with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Quantifier with @debug.Debug::{to_repr}
),
"pattern.mbt": (
#|#derive(@debug.Debug)
#|pub(all) struct Quantifier {
#| min : Int32
#| max : Int32?
#| mode : @shared_types.QuantifierMode
#|}
#|#derive(@debug.Debug)
#|pub enum Assertion {
#| StartOfInput
#| EndOfInput
#| StartOfLine
#| EndOfLine
#| StartOfWord
#| EndOfWord
#| NotWordBoundary
#|}
#|pub struct Pattern {
#| desc : PatternDesc
#| nullable : Bool
#|}
#|pub impl @debug.Debug for Pattern with fn to_repr(self) {
#| Repr(self.desc)
#|}
#|#derive(@debug.Debug)
#|pub enum PatternDesc {
#| Char(@shared_types.ByteSet)
#| Sequence(ReadOnlyArray[Pattern])
#| Alternation(ReadOnlyArray[Pattern])
#| Quantifier(Quantifier, Pattern)
#| Capture(name~ : String?, Pattern)
#| Assertion(Assertion)
#|}
#|pub fn Pattern::is_anchored(self : Pattern) -> Bool {
#| match self.desc {
#| Char(_) => false
#| Sequence(exprs) => exprs.any(e => e.is_anchored())
#| Alternation(exprs) => exprs.all(e => e.is_anchored())
#| Quantifier(q, expr) => q.min > 0 && expr.is_anchored()
#| Capture(expr, ..) => expr.is_anchored()
#| Assertion(StartOfInput) => true
#| Assertion(_) => false
#| }
#|}
#|pub fn char(cs : @shared_types.ByteSet) -> Pattern {
#| if cs.is_empty() {
#| empty
#| } else {
#| { desc: Char(cs), nullable: false, }
#| }
#|}
#|pub let epsilon : Pattern = { desc: Sequence([]), nullable: true, }
#|pub fn seq(exprs : ReadOnlyArray[Pattern]) -> Pattern {
#| let flatten = []
#| for expr in exprs {
#| if expr.desc is Sequence(nested) {
#| for e in nested {
#| flatten.push(e)
#| }
#| } else {
#| flatten.push(expr)
#| }
#| }
#| match flatten {
#| [expr] | ([] with expr = epsilon) => expr
#| exprs =>
#| {
#| desc: Sequence(ReadOnlyArray::from_array(exprs)),
#| nullable: exprs.all(e => e.nullable),
#| }
#| }
#|}
#|pub let empty : Pattern = { desc: Alternation([]), nullable: false, }
#|pub fn alt(exprs : ReadOnlyArray[Pattern]) -> Pattern {
#| match exprs {
#| [expr] | ([] with expr = empty) => expr
#| exprs => { desc: Alternation(exprs), nullable: exprs.any(e => e.nullable), }
#| }
#|}
#|pub fn quantifier(expr : Pattern, q : Quantifier) -> Pattern {
#| guard q.min >= 0 else { panic() }
#| guard q.max is None || (q.max is Some(max) && max >= q.min) else { panic() }
#| { desc: Quantifier(q, expr), nullable: expr.nullable || q.min == 0, }
#|}
#|pub fn capture(name? : String, expr : Pattern) -> Pattern {
#| { desc: Capture(name~, expr), nullable: expr.nullable, }
#|}
#|pub fn assertion(a : Assertion) -> Pattern {
#| { desc: Assertion(a), nullable: true, }
#|}
#|pub let start_of_input : Pattern = assertion(StartOfInput)
#|pub let end_of_input : Pattern = assertion(EndOfInput)
#|pub let start_of_line : Pattern = assertion(StartOfLine)
#|pub let end_of_line : Pattern = assertion(EndOfLine)
#|pub let start_of_word : Pattern = assertion(StartOfWord)
#|pub let end_of_word : Pattern = assertion(EndOfWord)
#|pub let not_word_boundary : Pattern = assertion(NotWordBoundary)
),
"using.mbt": "",
},
)
///|
let moonbitlang_core_bytes_internal_regex_engine_shared_types_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes/internal/regex_engine/shared_types",
deps={},
files={
"byte_set.mbt": (
#|pub using @byte_set {type ByteSet}
),
"category.mbt": (
#|#derive(Eq, Hash)
#|pub struct Category(Int32)
#|pub fn Category::dummy() -> Category {
#| Category(-1)
#|}
#|pub fn Category::inexistant() -> Category {
#| Category(1)
#|}
#|pub fn Category::word() -> Category {
#| Category(2)
#|}
#|pub fn Category::not_word_start() -> Category {
#| Category(4)
#|}
#|pub fn Category::not_word_end() -> Category {
#| Category(8)
#|}
#|pub fn Category::not_word() -> Category {
#| Category(4 | 8)
#|}
#|pub fn Category::newline() -> Category {
#| Category(16)
#|}
#|pub fn Category::inexistant_or_newline() -> Category {
#| Category(1 | 16)
#|}
#|pub fn Category::inexistant_or_non_word_start() -> Category {
#| Category(1 | 4)
#|}
#|pub fn Category::inexistant_or_non_word_end() -> Category {
#| Category(1 | 8)
#|}
#|pub impl Add for Category with fn add(cat1, cat2) {
#| Category(cat1.0 | cat2.0)
#|}
#|pub fn Category::intersects(a : Category, b : Category) -> Bool {
#| (a.0 & b.0) != 0
#|}
),
"extends.mbt": (
#|pub extend Category with Add::{add}
#|pub extend Category with Eq::{equal}
#|pub extend Category with Hash::{hash}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Category with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Category with Hash::{hash_combine}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend QuantifierMode with @debug.Debug::{to_repr}
),
"quantifier_mode.mbt": (
#|#derive(@debug.Debug)
#|pub(all) enum QuantifierMode {
#| Greedy
#| NonGreedy
#|}
),
"using.mbt": "",
},
)
///|
let moonbitlang_core_bytes_internal_regex_engine_shared_types_byte_set_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes/internal/regex_engine/shared_types/byte_set",
deps={},
files={
"byte_set.mbt": (
#|struct ByteSet {
#| w0 : UInt32
#| w1 : UInt32
#| w2 : UInt32
#| w3 : UInt32
#| w4 : UInt32
#| w5 : UInt32
#| w6 : UInt32
#| w7 : UInt32
#|}
#|pub fn ByteSet::empty() -> ByteSet {
#| { w0: 0, w1: 0, w2: 0, w3: 0, w4: 0, w5: 0, w6: 0, w7: 0, }
#|}
#|pub fn ByteSet::full() -> ByteSet {
#| ByteSet::char_range(0, 255)
#|}
#|fn range_mask(lo : Int32, hi : Int32, base : Int32) -> UInt32 {
#| let lo = Int32::max(lo, base) - base
#| let hi = Int32::min(hi, base + 31) - base
#| if lo > hi {
#| 0
#| } else {
#| (0xFFFF_FFFFU >> (31 - hi)) & (0xFFFF_FFFFU << lo)
#| }
#|}
#|pub fn ByteSet::char_range(lo : Int32, hi : Int32) -> ByteSet {
#| let lo = Int32::max(lo, 0)
#| let hi = Int32::min(hi, 255)
#| guard lo <= hi else { ByteSet::empty() }
#| {
#| w0: range_mask(lo, hi, 0),
#| w1: range_mask(lo, hi, 32),
#| w2: range_mask(lo, hi, 64),
#| w3: range_mask(lo, hi, 96),
#| w4: range_mask(lo, hi, 128),
#| w5: range_mask(lo, hi, 160),
#| w6: range_mask(lo, hi, 192),
#| w7: range_mask(lo, hi, 224),
#| }
#|}
#|pub fn ByteSet::char(c : Int32) -> ByteSet {
#| ByteSet::char_range(c, c)
#|}
#|fn ByteSet::word(self : ByteSet, i : Int32) -> UInt32 {
#| match i {
#| 0 => self.w0
#| 1 => self.w1
#| 2 => self.w2
#| 3 => self.w3
#| 4 => self.w4
#| 5 => self.w5
#| 6 => self.w6
#| _ => self.w7
#| }
#|}
#|pub fn ByteSet::contains(self : ByteSet, c : Int32) -> Bool {
#| guard c >= 0 && c < 256 else { false }
#| ((self.word(c >> 5) >> (c & 31)) & 1) != 0
#|}
#|pub fn ByteSet::is_empty(self : ByteSet) -> Bool {
#| (
#| self.w0 |
#| self.w1 |
#| self.w2 |
#| self.w3 |
#| self.w4 |
#| self.w5 |
#| self.w6 |
#| self.w7
#| ) ==
#| 0
#|}
#|fn ByteSet::zip(
#| self : ByteSet,
#| other : ByteSet,
#| f : (UInt32, UInt32) -> UInt32,
#|) -> ByteSet {
#| {
#| w0: f(self.w0, other.w0),
#| w1: f(self.w1, other.w1),
#| w2: f(self.w2, other.w2),
#| w3: f(self.w3, other.w3),
#| w4: f(self.w4, other.w4),
#| w5: f(self.w5, other.w5),
#| w6: f(self.w6, other.w6),
#| w7: f(self.w7, other.w7),
#| }
#|}
#|pub fn ByteSet::union(self : ByteSet, other : ByteSet) -> ByteSet {
#| self.zip(other, (a, b) => a | b)
#|}
#|pub fn ByteSet::intersection(self : ByteSet, other : ByteSet) -> ByteSet {
#| self.zip(other, (a, b) => a & b)
#|}
#|pub fn ByteSet::difference(self : ByteSet, other : ByteSet) -> ByteSet {
#| self.zip(other, (a, b) => a & b.lnot())
#|}
#|pub impl Add for ByteSet with fn add(self, other) {
#| self.union(other)
#|}
#|pub impl BitAnd for ByteSet with fn land(self, other) {
#| self.intersection(other)
#|}
#|pub impl Sub for ByteSet with fn sub(self, other) {
#| self.difference(other)
#|}
#|pub fn ByteSet::intervals(self : ByteSet) -> Iter[(Int32, Int32)] {
#| let runs = []
#| let mut c = 0
#| while c < 256 {
#| if (c & 31) == 0 && self.word(c >> 5) == 0 {
#| c += 32
#| } else if self.contains(c) {
#| let lo = c
#| while c < 256 && self.contains(c) {
#| c += 1
#| }
#| runs.push((lo, c - 1))
#| } else {
#| c += 1
#| }
#| }
#| runs.iter()
#|}
#|pub fn ByteSet::offset_by(self : ByteSet, f : (Int32) -> Int32) -> ByteSet {
#| let mut out = ByteSet::empty()
#| for c in 0..<256 {
#| if self.contains(c) {
#| out = out + ByteSet::char(f(c))
#| }
#| }
#| out
#|}
#|pub impl @debug.Debug for ByteSet with fn to_repr(self) {
#| Repr(self.intervals().to_array())
#|}
),
"extends.mbt": (
#|pub extend ByteSet with Add::{add}
#|pub extend ByteSet with BitAnd::{land}
#|pub extend ByteSet with Sub::{sub}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ByteSet with @debug.Debug::{to_repr}
),
"using.mbt": "",
},
)
///|
let moonbitlang_core_bytes_internal_regex_engine_symbol_map_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes/internal/regex_engine/symbol_map",
deps={},
files={
"symbol_map.mbt": (
#|struct SymbolMap(FixedArray[Bool])
#|pub fn new() -> SymbolMap {
#| SymbolMap(FixedArray::make(256, false))
#|}
#|pub fn SymbolMap::split(self : SymbolMap, set : @shared_types.ByteSet) -> Unit {
#| for interval in set.intervals() {
#| let (start, stop) = interval
#| self.0[start] = true
#| if stop + 1 < 256 {
#| self.0[stop + 1] = true
#| }
#| }
#|}
#|pub fn SymbolMap::finalize(self : SymbolMap) -> (Table, ReadOnlyArray[Int32]) {
#| let repr = [0]
#| let symbols = FixedArray::make(256, b'\x00')
#| for c in 1..<256 {
#| if self.0[c] {
#| repr.push(c)
#| }
#| symbols[c] = (repr.length() - 1).to_byte()
#| }
#| (Table(Bytes::from_array(symbols)), ReadOnlyArray::from_array(repr))
#|}
),
"table.mbt": (
#|struct Table(Bytes)
#|pub fn Table::map(self : Table, b : Byte) -> Int32 {
#| self.0.unsafe_get(b.to_int()).to_int()
#|}
#|pub fn Table::map_set(
#| self : Table,
#| set : @shared_types.ByteSet,
#|) -> @shared_types.ByteSet {
#| let mut out = @shared_types.ByteSet::empty()
#| for interval in set.intervals() {
#| let (lo, hi) = interval
#| out = out +
#| @shared_types.ByteSet::char_range(
#| self.0[lo].to_int(),
#| self.0[hi].to_int(),
#| )
#| }
#| out
#|}
),
"using.mbt": "",
},
)
///|
let moonbitlang_core_bytes_internal_regex_parser_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/bytes/internal/regex_parser",
deps={},
files={
"extends.mbt": (
#|pub extend ParserError with Show::{to_string}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ParserError with @debug.Debug::{to_repr}
),
"parser.mbt": (
#|priv type Parser
#|priv enum CharOrClass {
#| Char(Int32)
#| Class(@re.ByteSet)
#|}
#|const MAX_QUANT = 256
#|const MAX_GROUP_DEPTH = 64
#|fn Parser::class_atom(
#| rest : StringView,
#| ctx~ : ParserContext,
#| class_start~ : StringView,
#|) -> (StringView, CharOrClass) raise ParserError {
#| lexmatch rest with longest {
#| (re"^\\[dDsSwW]", after=_) =>
#| raise ctx.error_at(rest, HINT_USE_POSIX_CHARACTER_CLASS)
#| (re"^\\b", after=rest) => (rest, Char('\b'))
#| (re"^\\f", after=rest) => (rest, Char('\f'))
#| (re"^\\n", after=rest) => (rest, Char('\n'))
#| (re"^\\r", after=rest) => (rest, Char('\r'))
#| (re"^\\t", after=rest) => (rest, Char('\t'))
#| (re"^\\v", after=rest) => (rest, Char('\u{0b}'))
#| (re"^\\x" + (re"[0-9A-Fa-f]{2}" as hex), after=rest) =>
#| (rest, Char(hex2i(hex)))
#| (re"^\\u", after=_) =>
#| raise ctx.error_at(rest, HINT_UNICODE_ESCAPE_NOT_ALLOWED)
#| (re"^\\" + (re"[\^$\\.*+?()\[\]{}|/\"\-:&]" as c), after=rest) =>
#| (rest, Char(c.to_int()))
#| (re"^\\", after=_) => raise ctx.error_at(rest, HINT_INVALID_ESCAPE)
#| (re"^\[:ascii:\]", after=rest) => (rest, Class(posix_cset_ascii))
#| (re"^\[:upper:\]", after=rest) => (rest, Class(posix_cset_upper))
#| (re"^\[:lower:\]", after=rest) => (rest, Class(posix_cset_lower))
#| (re"^\[:alpha:\]", after=rest) => (rest, Class(posix_cset_alpha))
#| (re"^\[:alnum:\]", after=rest) => (rest, Class(posix_cset_alnum))
#| (re"^\[:digit:\]", after=rest) => (rest, Class(posix_cset_digit))
#| (re"^\[:xdigit:\]", after=rest) => (rest, Class(posix_cset_xdigit))
#| (re"^\[:blank:\]", after=rest) => (rest, Class(posix_cset_blank))
#| (re"^\[:space:\]", after=rest) => (rest, Class(posix_cset_space))
#| (re"^\[:word:\]", after=rest) => (rest, Class(@re.word_set))
#| (re"^\[:[A-Za-z0-9_\-]+:\]", after=_) =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_POSIX_CLASS)
#| (re"^\[:", after=_) => raise ctx.error_at(rest, HINT_INVALID_POSIX_CLASS)
#| (re"^." as c, after=rest2) => {
#| guard c.to_int() < 0x80 else {
#| raise ctx.error_at(rest, HINT_NON_ASCII_CHAR)
#| }
#| (rest2, Char(c.to_int()))
#| }
#| _ => raise ctx.error_at(class_start, HINT_UNCLOSED_CHAR_CLASS)
#| }
#|}
#|fn Parser::class_contents(
#| rest : StringView,
#| ctx~ : ParserContext,
#| class_start~ : StringView,
#|) -> (StringView, @re.ByteSet) raise ParserError {
#| let colon_start = rest is [':', ..]
#| let content_length = rest.length()
#| outer~: for rest = rest, char_set = @re.ByteSet::empty() {
#| match rest {
#| [] => raise ctx.error_at(class_start, HINT_UNCLOSED_CHAR_CLASS)
#| [']', ..] => break outer~ (rest, char_set)
#| [':', ']', ..] if colon_start && content_length - rest.length() >= 2 =>
#| raise ctx.error_at(class_start, HINT_BARE_POSIX_CLASS)
#| _ => {
#| let atom_start = rest
#| let (rest, left) = match rest {
#| ['[', ':', ..] =>
#| Parser::class_atom(rest, ctx~, class_start~)
#| ['[', ..] =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_NESTED_CHAR_CLASS)
#| ['-', ..] => raise ctx.error_at(rest, HINT_DASH_AFTER_RANGE)
#| _ => Parser::class_atom(rest, ctx~, class_start~)
#| }
#| let (rest, left_set) = match left {
#| Char(c) =>
#| match rest {
#| ['-', '-', ..] =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_CLASS_SET_EXPRESSION)
#| ['-', ']', ..] => raise ctx.error_at(rest, HINT_DASH_AT_BOUNDARY)
#| ['-', .. rest] => {
#| let (rest2, right) = Parser::class_atom(
#| rest,
#| ctx~,
#| class_start~,
#| )
#| match right {
#| Char(c2) => {
#| guard c <= c2 else {
#| raise ctx.error_at(atom_start, HINT_INVALID_CHAR_RANGE)
#| }
#| (rest2, @re.ByteSet::char_range(c, c2))
#| }
#| Class(_) =>
#| raise ctx.error_at(
#| rest,
#| offset=1,
#| HINT_CHAR_RANGE_OVER_SETS,
#| )
#| }
#| }
#| _ => (rest, @re.ByteSet::char(c))
#| }
#| Class(cs) => (rest, cs)
#| }
#| let char_set = char_set + left_set
#| match rest {
#| [.. "--", ..] =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_CLASS_SET_EXPRESSION)
#| [.. "&&", ..] =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_CLASS_SET_EXPRESSION)
#| _ => continue outer~ rest, char_set
#| }
#| }
#| }
#| }
#|}
#|fn Parser::character_class(
#| rest : StringView,
#| ctx~ : ParserContext,
#|) -> (StringView, @re.ByteSet) raise ParserError {
#| let class_start = rest
#| let (rest, neg) = match rest {
#| ['[', '^', .. rest] => (rest, true)
#| ['[', .. rest] => (rest, false)
#| _ => raise ctx.error_at(rest, HINT_UNEXPECTED_CHAR)
#| }
#| match rest {
#| ['-', ..] => raise ctx.error_at(rest, HINT_DASH_AT_BOUNDARY)
#| _ => ()
#| }
#| let (rest, char_set) = Parser::class_contents(rest, ctx~, class_start~)
#| match rest {
#| [']', .. rest] =>
#| (
#| rest,
#| if neg {
#| let char_set = if ctx.ignore_case {
#| ignore_case(char_set)
#| } else {
#| char_set
#| }
#| cset_any - char_set
#| } else {
#| char_set
#| },
#| )
#| [] => raise ctx.error_at(class_start, HINT_UNCLOSED_CHAR_CLASS)
#| _ => panic()
#| }
#|}
#|fn Parser::quantifier_opt(
#| rest : StringView,
#| ctx~ : ParserContext,
#|) -> (StringView, @re.Quantifier?) raise ParserError {
#| let quantifier_start = rest
#| let (rest, range) = lexmatch rest with longest {
#| (re"^\*", after=rest) => (rest, Some((0, None)))
#| (re"^\+", after=rest) => (rest, Some((1, None)))
#| (re"^\?", after=rest) => (rest, Some((0, Some(1))))
#| (re"^[{]" + (re"[0-9]+" as min) + re"[}]", after=rest) => {
#| let min = Parser::quant_bound(min, ctx~, start=quantifier_start)
#| (rest, Some((min, Some(min))))
#| }
#| (re"^[{]" + (re"[0-9]+" as min) + re",[}]", after=rest) => {
#| let min = Parser::quant_bound(min, ctx~, start=quantifier_start)
#| (rest, Some((min, None)))
#| }
#| (
#| re"^[{]" +
#| (re"[0-9]+" as min) +
#| re"," +
#| (re"[0-9]+" as max) +
#| re"[}]",
#| after=rest,
#| ) => {
#| let min = Parser::quant_bound(min, ctx~, start=quantifier_start)
#| let max = Parser::quant_bound(max, ctx~, start=quantifier_start)
#| guard min <= max else {
#| raise ctx.error_at(quantifier_start, HINT_INVALID_QUANTIFIER)
#| }
#| (rest, Some((min, Some(max))))
#| }
#| (re"^[{]", after=_) => raise ctx.error_at(rest, HINT_INVALID_QUANTIFIER)
#| _ => (rest, None)
#| }
#| match range {
#| Some((min, max)) => {
#| let rest0 = rest
#| let (rest, mode) = match rest {
#| ['?', .. rest] => (rest, @re.QuantifierMode::NonGreedy)
#| ['+', ..] =>
#| raise ctx.error_at(rest0, HINT_UNSUPPORTED_POSSESSIVE_QUANTIFIER)
#| _ => (rest, Greedy)
#| }
#| (rest, Some({ min, max, mode, }))
#| }
#| None => (rest, None)
#| }
#|}
#|fn Parser::quant_bound(
#| digits : StringView,
#| ctx~ : ParserContext,
#| start~ : StringView,
#|) -> Int32 raise ParserError {
#| for c in digits; value = 0 {
#| let digit = c.to_int() - '0'
#| let next = value * 10 + digit
#| guard next <= MAX_QUANT else {
#| raise ctx.error_at(start, HINT_QUANTIFIER_TOO_LARGE)
#| }
#| continue next
#| } nobreak {
#| value
#| }
#|}
#|fn Parser::term(
#| rest : StringView,
#| ctx~ : ParserContext,
#|) -> (StringView, @re.Pattern) raise ParserError {
#| let term_start = rest
#| let (rest, pat, is_assertion) = lexmatch rest with longest {
#| (re"^\^", after=rest) => (rest, @re.start_of_input, true)
#| (re"^\$", after=rest) => (rest, @re.end_of_input, true)
#| (re"^\\b", after=rest) =>
#| (rest, @re.alt([@re.start_of_word, @re.end_of_word]), true)
#| (re"^\\B", after=rest) => (rest, @re.not_word_boundary, true)
#| (re"^\\[dDsSwW]", after=_) =>
#| raise ctx.error_at(rest, HINT_USE_POSIX_CHARACTER_CLASS)
#| (re"^\\f", after=rest) => (rest, @re.char(@re.ByteSet::char('\f')), false)
#| (re"^\\n", after=rest) => (rest, @re.char(@re.ByteSet::char('\n')), false)
#| (re"^\\r", after=rest) => (rest, @re.char(@re.ByteSet::char('\r')), false)
#| (re"^\\t", after=rest) => (rest, @re.char(@re.ByteSet::char('\t')), false)
#| (re"^\\v", after=rest) =>
#| (rest, @re.char(@re.ByteSet::char('\u{0b}')), false)
#| (re"^\\x" + (re"[0-9A-Fa-f]{2}" as hex), after=rest) =>
#| (rest, @re.char(@re.ByteSet::char(hex2i(hex))), false)
#| (re"^\\u", after=_) =>
#| raise ctx.error_at(rest, HINT_UNICODE_ESCAPE_NOT_ALLOWED)
#| (re"^\\" + (re"[\^$\\.*+?()\[\]{}|/\"]" as c), after=rest) =>
#| (rest, @re.char(@re.ByteSet::char(c.to_int())), false)
#| (re"^\\.", after=_) => raise ctx.error_at(rest, HINT_INVALID_ESCAPE)
#| (re"^\.", after=rest) => (rest, @re.char(cset_any), false)
#| (re"^\(\?=", after=_) =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_LOOKAHEAD)
#| (re"^\(\?!", after=_) =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_NEGATIVE_LOOKAHEAD)
#| (re"^\(\?<=", after=_) =>
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_LOOKBEHIND)
#| (re"^\(\?
#| raise ctx.error_at(rest, HINT_UNSUPPORTED_NEGATIVE_LOOKBEHIND)
#| (re"^\(\?<" + (re"[A-Za-z_][A-Za-z0-9_]*" as name) + re">", after=rest2) => {
#| let name = name.to_owned()
#| if ctx.group_names.contains(name) {
#| raise ctx.error_at(rest, offset=3, HINT_DUPLICATE_GROUP_NAME)
#| }
#| ctx.group_names.insert(name)
#| let (rest, pat) = Parser::parse_group_continue(
#| rest2,
#| ctx~,
#| group_start=term_start,
#| )
#| (rest, @re.capture(name~, pat), false)
#| }
#| (re"^\(\?:", after=rest) => {
#| let (rest, pat) = Parser::parse_group_continue(
#| rest,
#| ctx~,
#| group_start=term_start,
#| )
#| (rest, pat, false)
#| }
#| (re"^\(\?<", after=_) =>
#| raise ctx.error_at(term_start, offset=1, HINT_INVALID_GROUP)
#| (re"^\(\?" + (re"[A-Za-z]+" as modifier) + re":", after=rest) =>
#| match modifier {
#| "i" => {
#| let need_process = !ctx.ignore_case
#| let (rest, pat) = Parser::parse_group_continue(
#| rest,
#| group_start=term_start,
#| ctx={ ..ctx, ignore_case: true, },
#| )
#| (
#| rest,
#| if need_process {
#| pattern_map_char(pat, ignore_case)
#| } else {
#| pat
#| },
#| false,
#| )
#| }
#| _ => raise ctx.error_at(term_start, offset=1, HINT_INVALID_GROUP)
#| }
#| (re"^\(", after=rest) => {
#| let (rest, pat) = Parser::parse_group_continue(
#| rest,
#| ctx~,
#| group_start=term_start,
#| )
#| (rest, @re.capture(pat), false)
#| }
#| (re"^\[", after=_) => {
#| let (rest, char_set) = Parser::character_class(rest, ctx~)
#| (rest, @re.char(char_set), false)
#| }
#| (re"^[\^$\\.*+?()\[\]{}|]", after=_) =>
#| raise ctx.error_at(rest, HINT_UNEXPECTED_CHAR)
#| (re"^." as c, after=rest2) => {
#| guard c.to_int() < 0x80 else {
#| raise ctx.error_at(rest, HINT_NON_ASCII_CHAR)
#| }
#| (rest2, @re.char(@re.ByteSet::char(c.to_int())), false)
#| }
#| _ => raise ctx.error_at(rest, HINT_UNEXPECTED_CHAR)
#| }
#| if !is_assertion {
#| let (rest, quantifier) = Parser::quantifier_opt(rest, ctx~)
#| match quantifier {
#| Some(q) => (rest, @re.quantifier(pat, q))
#| None => (rest, pat)
#| }
#| } else {
#| (rest, pat)
#| }
#|}
#|fn Parser::alternative(
#| rest : StringView,
#| ctx~ : ParserContext,
#|) -> (StringView, @re.Pattern) raise ParserError {
#| let seq = []
#| let rest = for rest = rest {
#| match rest {
#| [] | ['|' | ')', ..] => break rest
#| _ => {
#| let (rest, pat) = Parser::term(rest, ctx~)
#| seq.push(pat)
#| continue rest
#| }
#| }
#| }
#| (rest, @re.seq(ReadOnlyArray::from_array(seq)))
#|}
#|fn Parser::parse_group_continue(
#| rest : StringView,
#| ctx~ : ParserContext,
#| group_start~ : StringView,
#|) -> (StringView, @re.Pattern) raise ParserError {
#| guard ctx.depth < MAX_GROUP_DEPTH else {
#| raise ctx.error_at(group_start, HINT_NESTING_TOO_DEEP)
#| }
#| let ctx = { ..ctx, depth: ctx.depth + 1, }
#| let (rest, pat) = Parser::disjunction(rest, ctx~)
#| match rest {
#| [')', .. rest] => (rest, pat)
#| [] => raise ctx.error_at(group_start, HINT_UNCLOSED_GROUP)
#| _ => raise ctx.error_at(rest, HINT_UNEXPECTED_CHAR)
#| }
#|}
#|fn Parser::disjunction(
#| rest : StringView,
#| ctx~ : ParserContext,
#|) -> (StringView, @re.Pattern) raise ParserError {
#| let alts = []
#| let (rest, alt) = Parser::alternative(rest, ctx~)
#| alts.push(alt)
#| let rest = for rest = rest {
#| match rest {
#| ['|', .. rest] => {
#| let (rest, pat) = Parser::alternative(rest, ctx~)
#| alts.push(pat)
#| continue rest
#| }
#| _ => break rest
#| }
#| }
#| (rest, @re.alt(ReadOnlyArray::from_array(alts)))
#|}
#|pub fn parse(pattern : StringView) -> @re.Pattern raise ParserError {
#| let ctx = ParserContext::new(base=pattern.start_offset())
#| let (rest, pat) = Parser::disjunction(pattern, ctx~)
#| if !rest.is_empty() {
#| raise ctx.error_at(rest, HINT_UNEXPECTED_CHAR)
#| }
#| pat
#|}
),
"parser_context.mbt": (
#|priv struct ParserContext {
#| base : Int32
#| ignore_case : Bool
#| depth : Int32
#| group_names : @set.Set[String]
#|}
#|fn ParserContext::new(base~ : Int32) -> ParserContext {
#| { base, ignore_case: false, depth: 0, group_names: Set([]), }
#|}
#|fn ParserContext::error_at(
#| self : ParserContext,
#| view : StringView,
#| hint : String,
#| offset~ : Int32 = 0,
#|) -> ParserError {
#| ParserError(at=view.start_offset() - self.base + offset, hint~)
#|}
),
"parser_error.mbt": (
#|#derive(@debug.Debug)
#|suberror ParserError {
#| ParserError(at~ : Int32, hint~ : String)
#|}
#|pub impl Show for ParserError with fn output(self, logger) {
#| match self {
#| ParserError(at~, hint~) =>
#| logger <+
#| $|ParserError at position \{at}: \{hint}
#| }
#|}
#|const HINT_UNCLOSED_CHAR_CLASS = "Unclosed character class"
#|const HINT_UNCLOSED_GROUP = "Unclosed group"
#|const HINT_NESTING_TOO_DEEP = "Groups nested too deeply"
#|const HINT_UNEXPECTED_CHAR = "Unexpected character"
#|const HINT_QUANTIFIER_TOO_LARGE = "Quantifier too large"
#|const HINT_INVALID_QUANTIFIER = "Invalid quantifier"
#|const HINT_INVALID_ESCAPE = "Invalid escape sequence"
#|const HINT_UNICODE_ESCAPE_NOT_ALLOWED = "Unicode escape sequences are not allowed in a bytes pattern, use \\xHH for a byte"
#|const HINT_NON_ASCII_CHAR = "Non-ASCII characters are not allowed in a bytes pattern, use \\xHH for a byte"
#|const HINT_INVALID_GROUP = "Invalid group"
#|const HINT_DUPLICATE_GROUP_NAME = "Duplicate capture group name"
#|const HINT_UNSUPPORTED_LOOKAHEAD = "Unsupported lookahead assertion"
#|const HINT_UNSUPPORTED_NEGATIVE_LOOKAHEAD = "Unsupported negative lookahead assertion"
#|const HINT_UNSUPPORTED_LOOKBEHIND = "Unsupported lookbehind assertion"
#|const HINT_UNSUPPORTED_NEGATIVE_LOOKBEHIND = "Unsupported negative lookbehind assertion"
#|const HINT_INVALID_POSIX_CLASS = "Invalid POSIX character class"
#|const HINT_BARE_POSIX_CLASS = "A class that starts and ends with ':' looks like a POSIX class without its outer brackets, write [[:digit:]] for the POSIX class or list ':' once (e.g. [a-z:]) for a literal colon"
#|const HINT_UNSUPPORTED_POSIX_CLASS = "Unsupported POSIX character class"
#|const HINT_CHAR_RANGE_OVER_SETS = "Character class range over sets"
#|const HINT_INVALID_CHAR_RANGE = "Invalid character class range"
#|const HINT_UNSUPPORTED_CLASS_SET_EXPRESSION = "Unsupported character class set expression"
#|const HINT_UNSUPPORTED_POSSESSIVE_QUANTIFIER = "Unsupported possessive quantifier"
#|const HINT_UNSUPPORTED_NESTED_CHAR_CLASS = "Unsupported nested character class"
#|const HINT_USE_POSIX_CHARACTER_CLASS = "Use POSIX character class instead of \\w, \\W, \\d, \\D, \\s, \\S"
#|const HINT_DASH_AT_BOUNDARY = "Dash at the start or end of character class is not supported, use \\- to match a literal dash"
#|const HINT_DASH_AFTER_RANGE = "Dash after a range or a POSIX class is ambiguous, use \\- to match a literal dash"
),
"parser_util.mbt": (
#|fn hex2i(hex : StringView) -> Int32 {
#| for c in hex; value = 0 {
#| let value = value * 16
#| match c {
#| '0'..<='9' => continue value + (c.to_int() - '0')
#| 'a'..<='f' => continue value + (c.to_int() - 'a' + 10)
#| 'A'..<='F' => continue value + (c.to_int() - 'A' + 10)
#| _ => panic()
#| }
#| } nobreak {
#| value
#| }
#|}
#|fn pattern_map_char(
#| pat : @re.Pattern,
#| f : (@re.ByteSet) -> @re.ByteSet,
#|) -> @re.Pattern {
#| match pat.desc {
#| Capture(name~, pat) => @re.capture(name?, pattern_map_char(pat, f))
#| Quantifier(quant, pat) => @re.quantifier(pattern_map_char(pat, f), quant)
#| Alternation(pats) => @re.alt(pats.map(p => pattern_map_char(p, f)))
#| Sequence(pats) => @re.seq(pats.map(p => pattern_map_char(p, f)))
#| Char(c) => @re.char(f(c))
#| Assertion(a) => @re.assertion(a)
#| }
#|}
#|fn ignore_case(cs : @re.ByteSet) -> @re.ByteSet {
#| cs +
#| (cs & @re.ByteSet::char_range('A', 'Z')).offset_by(it => it + 32) +
#| (cs & @re.ByteSet::char_range('a', 'z')).offset_by(it => it - 32)
#|}
#|let cset_any : @re.ByteSet = @re.ByteSet::full()
#|let posix_cset_ascii : @re.ByteSet = @re.ByteSet::char_range(0, 0x7f)
#|let posix_cset_upper : @re.ByteSet = @re.ByteSet::char_range('A', 'Z')
#|let posix_cset_lower : @re.ByteSet = @re.ByteSet::char_range('a', 'z')
#|let posix_cset_alpha : @re.ByteSet = posix_cset_upper + posix_cset_lower
#|let posix_cset_digit : @re.ByteSet = @re.ByteSet::char_range('0', '9')
#|let posix_cset_xdigit : @re.ByteSet = posix_cset_digit +
#| @re.ByteSet::char_range('a', 'f') +
#| @re.ByteSet::char_range('A', 'F')
#|let posix_cset_alnum : @re.ByteSet = posix_cset_alpha + posix_cset_digit
#|let posix_cset_space : @re.ByteSet = @re.ByteSet::char(' ') +
#| @re.ByteSet::char('\t') +
#| @re.ByteSet::char('\n') +
#| @re.ByteSet::char('\r') +
#| @re.ByteSet::char('\u{000B}') +
#| @re.ByteSet::char('\f')
#|let posix_cset_blank : @re.ByteSet = @re.ByteSet::char(' ') +
#| @re.ByteSet::char('\t')
),
"using.mbt": "",
},
)
///|
let moonbitlang_core_char_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/char",
deps={},
files={ "char_util.mbt": "" },
)
///|
let moonbitlang_core_cmp_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/cmp",
deps={},
files={
"bound.mbt": (
#|#derive(Eq, Hash, @debug.Debug)
#|pub(all) enum Bound[T] {
#| Included(T)
#| Excluded(T)
#| Unbounded
#|}
#|test "Bound equality and hashing" {
#| let a : Bound[Int32] = Included(1)
#| assert_true(a == Included(1))
#| assert_true(a != Excluded(1))
#| assert_true(Unbounded == (Unbounded : Bound[Int32]))
#| assert_true(a.hash() == Bound::Included(1).hash())
#|}
),
"cmp.mbt": (
#|#derive(Eq, Hash, @debug.Debug)
#|pub(all) struct Reverse[T](T)
#|#deprecated("Use `@debug.Debug` instead of `Show`", skip_current_package=true)
#|pub impl[T : Show] Show for Reverse[T]
#|pub impl[T : Show] Show for Reverse[T] with fn output(self, logger) {
#| logger <+
#| $|Reverse(\{self.0})|
#|}
#|pub impl[T : Compare] Compare for Reverse[T] with fn compare(a, b) {
#| b.0.compare(a.0)
#|}
#|pub fn[T, K : Compare] maximum_by_key(x : T, y : T, f : (T) -> K) -> T {
#| if f(x) > f(y) {
#| x
#| } else {
#| y
#| }
#|}
#|pub fn[T, K : Compare] minimum_by_key(x : T, y : T, f : (T) -> K) -> T {
#| if f(x) > f(y) {
#| y
#| } else {
#| x
#| }
#|}
#|pub fn[T : Compare] maximum(x : T, y : T) -> T {
#| if x > y {
#| x
#| } else {
#| y
#| }
#|}
#|pub fn[T : Compare] minimum(x : T, y : T) -> T {
#| if x > y {
#| y
#| } else {
#| x
#| }
#|}
#|pub fn[T : Compare] minmax(x : T, y : T) -> (T, T) {
#| if x > y {
#| (y, x)
#| } else {
#| (x, y)
#| }
#|}
#|pub fn[T, K : Compare] minmax_by_key(x : T, y : T, f : (T) -> K) -> (T, T) {
#| if f(x) > f(y) {
#| (y, x)
#| } else {
#| (x, y)
#| }
#|}
),
"extends.mbt": (
#|pub extend Reverse with Compare::{compare}
#|pub extend Reverse with Eq::{equal}
#|pub extend Reverse with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Reverse with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Reverse with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Reverse with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show`", skip_current_package=true)
#|#doc(hidden)
#|pub extend Reverse with Show::{to_string}
#|pub extend Bound with Eq::{equal}
#|pub extend Bound with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bound with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bound with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bound with Hash::{hash_combine}
),
},
)
///|
let moonbitlang_core_compress_flate_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/compress/flate",
deps={},
files={
"bitwriter.mbt": (
#|priv struct BitWriter {
#| out : Array[Byte]
#| mut bitbuf : Int32
#| mut bit_count : Int32 // streaming Tokens may extend bitbuf with token_high
#| mut direct_output : FixedArray[Byte]?
#| mut direct_start : Int32
#| spill_on_full : Bool
#| mut direct_capacity : Int32
#| growable : Bool
#| mut direct_pos : Int32
#| mut overflowed : Bool
#|}
#|fn BitWriter::BitWriter(spill_on_full~ : Bool = false) -> BitWriter {
#| {
#| spill_on_full,
#| out: [],
#| bitbuf: 0,
#| bit_count: 0,
#| direct_output: None,
#| direct_start: 0,
#| direct_capacity: 0,
#| direct_pos: 0,
#| growable: false,
#| overflowed: false,
#| }
#|}
#|fn BitWriter::into(output : MutArrayView[Byte]) -> BitWriter {
#| {
#| out: [],
#| bitbuf: 0,
#| bit_count: 0,
#| spill_on_full: false,
#| direct_output: Some(output_storage(output)),
#| direct_start: output.start_offset(),
#| direct_capacity: output.length(),
#| direct_pos: 0,
#| growable: false,
#| overflowed: false,
#| }
#|}
#|fn BitWriter::growing(output : FixedArray[Byte]) -> BitWriter {
#| { ..BitWriter::into(output.mut_view()), growable: true, }
#|}
#|fn bitwriter_growth_capacity(current : Int32, required : Int32) -> Int32 {
#| let doubled = if current <= 0x3fff_ffff { current * 2 } else { required }
#| required.max(doubled).max(64)
#|}
#|fn bitwriter_output_length(written : Int32, additional : Int32) -> Int32 {
#| guard additional <= 0x7fff_ffff - written else {
#| abort("DEFLATE output capacity overflow")
#| }
#| written + additional
#|}
#|fn BitWriter::grow(
#| self : BitWriter,
#| output : FixedArray[Byte],
#| required : Int32,
#|) -> FixedArray[Byte] {
#| let grown = FixedArray::make_and_blit(
#| output,
#| allocate_len=bitwriter_growth_capacity(output.length(), required),
#| init=b'\x00',
#| len=self.direct_pos,
#| )
#| self.direct_output = Some(grown)
#| self.direct_capacity = grown.length()
#| grown
#|}
#|#inline
#|fn BitWriter::write_byte(self : BitWriter, byte : Byte) -> Unit {
#| if self.direct_output is Some(output) {
#| if self.direct_pos < self.direct_capacity {
#| output[self.direct_start + self.direct_pos] = byte
#| self.direct_pos += 1
#| } else if self.growable {
#| let required = bitwriter_output_length(self.direct_pos, 1)
#| let grown = self.grow(output, required)
#| grown[self.direct_pos] = byte
#| self.direct_pos = required
#| } else if self.spill_on_full {
#| self.out.push(byte)
#| } else {
#| self.overflowed = true
#| }
#| } else {
#| self.out.push(byte)
#| }
#|}
#|fn BitWriter::write_bytes(
#| self : BitWriter,
#| input : BytesView,
#| offset : Int32,
#| length : Int32,
#|) -> Unit {
#| if self.direct_output is Some(output) {
#| let output = if self.growable &&
#| length > self.direct_capacity - self.direct_pos {
#| self.grow(output, bitwriter_output_length(self.direct_pos, length))
#| } else {
#| output
#| }
#| let count = length.min((self.direct_capacity - self.direct_pos).max(0))
#| if count > 0 {
#| output.blit_from_bytes(
#| self.direct_start + self.direct_pos,
#| input.data(),
#| input.start_offset() + offset,
#| count,
#| )
#| }
#| self.direct_pos += count
#| if count < length {
#| self.overflowed = true
#| }
#| } else {
#| append_payload(self.out, input[offset:offset + length])
#| }
#|}
#|#cfg(not(target="js"))
#|fn append_payload(out : Array[Byte], payload : BytesView) -> Unit {
#| let staged = FixedArray::make(payload.length(), b'\x00')
#| staged.blit_from_bytesview(0, payload)
#| out.append(staged)
#|}
#|#cfg(target="js")
#|fn append_payload(out : Array[Byte], payload : BytesView) -> Unit {
#| out.push_iter(payload.iter())
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn BitWriter::write_array(
#| self : BitWriter,
#| bytes : FixedArray[Byte],
#| length : Int32,
#|) -> Unit {
#| if self.direct_output is Some(output) {
#| let output = if self.growable &&
#| length > self.direct_capacity - self.direct_pos {
#| self.grow(output, bitwriter_output_length(self.direct_pos, length))
#| } else {
#| output
#| }
#| let count = length.min((self.direct_capacity - self.direct_pos).max(0))
#| if count > 0 {
#| bytes.blit_to(
#| output,
#| len=count,
#| dst_offset=self.direct_start + self.direct_pos,
#| )
#| }
#| self.direct_pos += count
#| if count < length {
#| self.overflowed = true
#| }
#| } else {
#| self.out.append(bytes[:length])
#| }
#|}
#|fn BitWriter::write_bit(self : BitWriter, bit : Int32) -> Unit {
#| self.bitbuf = self.bitbuf | (bit << self.bit_count)
#| self.bit_count = self.bit_count + 1
#| if self.bit_count == 8 {
#| self.write_byte((self.bitbuf & 0xFF).to_byte())
#| self.bitbuf = 0
#| self.bit_count = 0
#| }
#|}
#|fn BitWriter::write_bits(self : BitWriter, value : Int32, n : Int32) -> Unit {
#| self.bitbuf = self.bitbuf | ((value & ((1 << n) - 1)) << self.bit_count)
#| self.bit_count = self.bit_count + n
#| while self.bit_count >= 8 {
#| self.write_byte((self.bitbuf & 0xFF).to_byte())
#| self.bitbuf = self.bitbuf >> 8
#| self.bit_count = self.bit_count - 8
#| }
#|}
#|fn BitWriter::write_rcode(self : BitWriter, code : UInt32, n : Int32) -> Unit {
#| self.write_bits(code.reinterpret_as_int(), n)
#|}
#|fn BitWriter::flush(self : BitWriter) -> Unit {
#| if self.bit_count > 0 {
#| self.write_byte((self.bitbuf & 0xFF).to_byte())
#| self.bitbuf = 0
#| self.bit_count = 0
#| }
#|}
#|fn output_storage(output : MutArrayView[Byte]) -> FixedArray[Byte] = "%arrayview.buf"
),
"block_planner.mbt": (
#|const MIN_LOOKAHEAD : Int32 = 258
#|fn IncrementalEncoder::pending_bytes(deflater : IncrementalEncoder) -> Int32 {
#| deflater.pending_end - deflater.parse_pos
#|}
#|fn IncrementalEncoder::push_pending(
#| deflater : IncrementalEncoder,
#| input : BytesView,
#|) -> Unit {
#| let mut rest = input
#| while rest.length() > 0 {
#| let offset = deflater.pending_end & STREAM_RING_MASK
#| let count = rest.length().min(STREAM_RING_SIZE - offset)
#| let chunk = rest[:count]
#| deflater.pending.blit_from_bytesview(offset, chunk)
#| deflater.pending.blit_from_bytesview(offset + STREAM_RING_SIZE, chunk)
#| deflater.pending_end = deflater.pending_end + count
#| rest = rest[count:]
#| }
#|}
#|fn IncrementalEncoder::chain_ready_history(
#| deflater : IncrementalEncoder,
#|) -> Unit {
#| let ready_end = deflater.parse_pos.min((deflater.pending_end - 3).max(0))
#| while deflater.hashed_through < ready_end {
#| deflater.finder.insert_window_pos(
#| deflater.pending,
#| deflater.hashed_through,
#| deflater.pending_end,
#| )
#| deflater.hashed_through += 1
#| }
#|}
#|fn IncrementalEncoder::record_chained_positions(
#| deflater : IncrementalEncoder,
#| processed : Int32,
#|) -> Unit {
#| let chained_end = processed.min((deflater.pending_end - 3).max(0))
#| if chained_end > deflater.hashed_through {
#| deflater.hashed_through = chained_end
#| }
#|}
#|fn IncrementalEncoder::try_fast_store(
#| deflater : IncrementalEncoder,
#| end : Int32,
#| is_final~ : Bool,
#|) -> Bool {
#| guard deflater.fast_store &&
#| should_fast_store_window(deflater.pending, deflater.parse_pos, end) else {
#| return false
#| }
#| deflater.chain_ready_history()
#| deflater.start_stored(deflater.parse_pos, end, is_final~)
#| deflater.finder.insert_window_positions(
#| deflater.pending,
#| deflater.parse_pos,
#| end,
#| deflater.pending_end,
#| )
#| deflater.parse_pos = end
#| deflater.record_chained_positions(end)
#| deflater.maybe_rebase()
#| true
#|}
#|fn IncrementalEncoder::plan_block(
#| self : IncrementalEncoder,
#| finish~ : Bool,
#|) -> Unit {
#| let start = self.parse_pos
#| if self.level == 0 {
#| self.start_stored(start, self.pending_end, is_final=finish)
#| self.parse_pos = 0
#| self.pending_end = 0
#| return
#| }
#| let target = start + self.pending_bytes().min(DEFLATE_BLOCK_SIZE)
#| let is_final = finish && target == self.pending_end
#| if self.try_fast_store(target, is_final~) {
#| return
#| }
#| self.tokens.clear()
#| self.ll_freq.fill(0)
#| self.d_freq.fill(0)
#| self.chain_ready_history()
#| let end = tokenize_window(
#| self.pending,
#| self.finder,
#| start,
#| target,
#| self.pending_end,
#| self.cfg,
#| self.tokens,
#| self.ll_freq,
#| self.d_freq,
#| )
#| match choose_block_encoding(end - start, self.ll_freq, self.d_freq) {
#| Stored => self.start_stored(start, end, is_final~)
#| Fixed => self.phase = FixedHeader
#| Dynamic(codes) => self.phase = DynamicHeader(codes)
#| }
#| self.emitted_final = is_final
#| self.parse_pos = end
#| self.record_chained_positions(end)
#| self.maybe_rebase()
#|}
#|fn IncrementalEncoder::maybe_rebase(deflater : IncrementalEncoder) -> Unit {
#| let rebase_at = 1 << 30
#| guard deflater.parse_pos >= rebase_at else { return }
#| let offset = (deflater.parse_pos / STREAM_RING_SIZE - 1) * STREAM_RING_SIZE
#| deflater.finder.rebase(offset)
#| deflater.parse_pos = deflater.parse_pos - offset
#| deflater.pending_end = deflater.pending_end - offset
#| deflater.hashed_through = deflater.hashed_through - offset
#|}
),
"block_writer.mbt": (
#|#valtype
#|priv struct CodeLenSym {
#| sym : Int32
#| extra : Int32
#| nextra : Int32
#|}
#|fn stored_block_count(byte_len : Int32) -> Int32 {
#| if byte_len == 0 {
#| 1
#| } else {
#| (byte_len - 1) / 65535 + 1
#| }
#|}
#|fn build_codelen_syms(
#| ll_len : FixedArray[Int32],
#| hlit : Int32,
#| dd_len : FixedArray[Int32],
#| hdist : Int32,
#|) -> Array[CodeLenSym] {
#| let comb = FixedArray::make(hlit + hdist, 0)
#| ll_len.blit_to(comb, len=hlit)
#| dd_len.blit_to(comb, len=hdist, dst_offset=hlit)
#| let out : Array[CodeLenSym] = []
#| let m = comb.length()
#| let mut i = 0
#| while i < m {
#| let cur = comb[i]
#| let mut run = 1
#| while i + run < m && comb[i + run] == cur {
#| run = run + 1
#| }
#| i = i + run
#| if cur == 0 {
#| while run >= 11 {
#| let r = run.min(138)
#| out.push({ sym: 18, extra: r - 11, nextra: 7, })
#| run = run - r
#| }
#| while run >= 3 {
#| let r = run.min(10)
#| out.push({ sym: 17, extra: r - 3, nextra: 3, })
#| run = run - r
#| }
#| while run > 0 {
#| out.push({ sym: 0, extra: 0, nextra: 0, })
#| run = run - 1
#| }
#| } else {
#| out.push({ sym: cur, extra: 0, nextra: 0, })
#| run = run - 1
#| while run >= 3 {
#| let r = run.min(6)
#| out.push({ sym: 16, extra: r - 3, nextra: 2, })
#| run = run - r
#| }
#| while run > 0 {
#| out.push({ sym: cur, extra: 0, nextra: 0, })
#| run = run - 1
#| }
#| }
#| }
#| out
#|}
#|#valtype
#|priv struct BlockCost {
#| stored : Int32
#| fixed : Int32
#| dynamic : Int32
#|}
#|priv struct DynamicCodeLengths {
#| dll_len : FixedArray[Int32]
#| dd_len : FixedArray[Int32]
#| hlit : Int32
#| hdist : Int32
#| codelen_syms : Array[CodeLenSym]
#| codelen_code_len : FixedArray[Int32]
#| hclen : Int32
#|}
#|priv struct DynamicCodes {
#| lengths : DynamicCodeLengths
#| dll_val : FixedArray[UInt32]
#| dd_val : FixedArray[UInt32]
#| codelen_code_val : FixedArray[UInt32]
#|}
#|priv enum BlockEncoding {
#| Stored
#| Fixed
#| Dynamic(DynamicCodes)
#|}
#|fn build_dynamic_code_lengths(
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> DynamicCodeLengths {
#| let dll_len = FixedArray::make(286, 0)
#| gen_huffman(ll_freq, 286, 15, dll_len)
#| let dd_len = FixedArray::make(30, 0)
#| gen_huffman(d_freq, 30, 15, dd_len)
#| if !dd_len.any(length => length > 0) {
#| dd_len[0] = 1 // DEFLATE requires at least one distance code
#| }
#| let mut hlit = 286
#| while hlit > 257 && dll_len[hlit - 1] == 0 {
#| hlit = hlit - 1
#| }
#| let mut hdist = 30
#| while hdist > 1 && dd_len[hdist - 1] == 0 {
#| hdist = hdist - 1
#| }
#| let codelen_syms = build_codelen_syms(dll_len, hlit, dd_len, hdist)
#| let codelen_freq = FixedArray::make(19, 0)
#| for it in codelen_syms {
#| codelen_freq[it.sym] += 1
#| }
#| let codelen_code_len = FixedArray::make(19, 0)
#| gen_huffman(codelen_freq, 19, 7, codelen_code_len)
#| let mut hclen = 19
#| while hclen > 4 && codelen_code_len[code_order[hclen - 1]] == 0 {
#| hclen = hclen - 1
#| }
#| { dll_len, dd_len, hlit, hdist, codelen_syms, codelen_code_len, hclen, }
#|}
#|fn canonical_code_values(
#| code_len : FixedArray[Int32],
#| max_bits : Int32,
#|) -> FixedArray[UInt32] {
#| let counts = FixedArray::make(max_bits + 1, 0)
#| for length in code_len {
#| if length > 0 {
#| counts[length] += 1
#| }
#| }
#| let next_code = FixedArray::make(max_bits + 1, 0)
#| for length in 1..<=max_bits {
#| next_code[length] = (next_code[length - 1] + counts[length - 1]) << 1
#| }
#| let values = FixedArray::make(code_len.length(), 0U)
#| for symbol in 0.. 0 {
#| values[symbol] = reverse_bits(
#| next_code[length].reinterpret_as_uint(),
#| length,
#| )
#| next_code[length] += 1
#| }
#| }
#| values
#|}
#|fn materialize_dynamic_codes(lengths : DynamicCodeLengths) -> DynamicCodes {
#| {
#| lengths,
#| dll_val: canonical_code_values(lengths.dll_len, 15),
#| dd_val: canonical_code_values(lengths.dd_len, 15),
#| codelen_code_val: canonical_code_values(lengths.codelen_code_len, 7),
#| }
#|}
#|fn estimate_block_cost(
#| codes : DynamicCodeLengths,
#| byte_len : Int32,
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> BlockCost {
#| let mut dyn_sym_bits = 0
#| let mut fixed_sym_bits = 0
#| for s in 0..<286 {
#| if ll_freq[s] != 0 {
#| dyn_sym_bits = dyn_sym_bits + ll_freq[s] * codes.dll_len[s]
#| fixed_sym_bits = fixed_sym_bits + ll_freq[s] * fixed_ll_len[s]
#| }
#| }
#| for d in 0..<30 {
#| if d_freq[d] != 0 {
#| dyn_sym_bits = dyn_sym_bits + d_freq[d] * codes.dd_len[d]
#| fixed_sym_bits = fixed_sym_bits + d_freq[d] * 5
#| }
#| }
#| let mut extra_bits = 0
#| for i in 0..<29 {
#| extra_bits = extra_bits + ll_freq[257 + i] * len_extra[i]
#| }
#| for d in 0..<30 {
#| extra_bits = extra_bits + d_freq[d] * dist_extra[d]
#| }
#| let mut dyn_hdr_bits = 14 + 3 * codes.hclen
#| for it in codes.codelen_syms {
#| dyn_hdr_bits = dyn_hdr_bits + codes.codelen_code_len[it.sym] + it.nextra
#| }
#| let stored_bits = 8 * (byte_len + 5 * stored_block_count(byte_len))
#| {
#| stored: stored_bits,
#| fixed: 3 + fixed_sym_bits + extra_bits,
#| dynamic: 3 + dyn_hdr_bits + dyn_sym_bits + extra_bits,
#| }
#|}
#|fn choose_block_encoding(
#| byte_len : Int32,
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> BlockEncoding {
#| ll_freq[256] += 1 // end-of-block
#| let lengths = build_dynamic_code_lengths(ll_freq, d_freq)
#| let cost = estimate_block_cost(lengths, byte_len, ll_freq, d_freq)
#| if cost.stored < cost.fixed && cost.stored < cost.dynamic {
#| Stored
#| } else if cost.dynamic < cost.fixed {
#| Dynamic(materialize_dynamic_codes(lengths))
#| } else {
#| Fixed
#| }
#|}
#|fn BitWriter::write_huffman_block(
#| writer : BitWriter,
#| dynamic_codes : DynamicCodes?,
#| tokens : ArrayView[Int32],
#| is_final : Bool,
#|) -> Unit {
#| writer.write_bit(if is_final { 1 } else { 0 }) // BFINAL
#| guard dynamic_codes is Some(codes) else {
#| writer.write_fixed_block(tokens)
#| return
#| }
#| writer.write_dynamic_block(codes, tokens)
#|}
#|fn BitWriter::write_tokens_bytewise(
#| writer : BitWriter,
#| tokens : ArrayView[Int32],
#| ll_val : FixedArray[UInt32],
#| ll_len : FixedArray[Int32],
#| dd_val : FixedArray[UInt32],
#| dd_len : FixedArray[Int32],
#|) -> Unit {
#| let mut bitbuf = writer.bitbuf
#| let mut bit_count = writer.bit_count
#| for tok in tokens {
#| if tok < 256 {
#| bitbuf = bitbuf | (ll_val[tok].reinterpret_as_int() << bit_count)
#| bit_count = bit_count + ll_len[tok]
#| } else {
#| let length = tok >> 16
#| let dist = tok & 0xFFFF
#| let lidx = len_to_idx[length]
#| let lsym = 257 + lidx
#| bitbuf = bitbuf | (ll_val[lsym].reinterpret_as_int() << bit_count)
#| bit_count = bit_count + ll_len[lsym]
#| bitbuf = bitbuf | ((length - len_base[lidx]) << bit_count)
#| bit_count = bit_count + len_extra[lidx]
#| while bit_count >= 8 {
#| writer.write_byte((bitbuf & 0xFF).to_byte())
#| bitbuf = bitbuf >> 8
#| bit_count = bit_count - 8
#| }
#| let didx = dist_index(dist)
#| bitbuf = bitbuf | (dd_val[didx].reinterpret_as_int() << bit_count)
#| bit_count = bit_count + dd_len[didx]
#| while bit_count >= 8 {
#| writer.write_byte((bitbuf & 0xFF).to_byte())
#| bitbuf = bitbuf >> 8
#| bit_count = bit_count - 8
#| }
#| bitbuf = bitbuf | ((dist - dist_base[didx]) << bit_count)
#| bit_count = bit_count + dist_extra[didx]
#| }
#| while bit_count >= 8 {
#| writer.write_byte((bitbuf & 0xFF).to_byte())
#| bitbuf = bitbuf >> 8
#| bit_count = bit_count - 8
#| }
#| }
#| writer.bitbuf = bitbuf
#| writer.bit_count = bit_count
#|}
#|#cfg(any(target="native", target="wasm"))
#|const TOKEN_CHUNK_TOKENS : Int32 = 1024
#|#cfg(any(target="native", target="wasm"))
#|const TOKEN_CHUNK_BYTES : Int32 = 6 * TOKEN_CHUNK_TOKENS + 8
#|#cfg(any(target="native", target="wasm"))
#|fn BitWriter::write_tokens_wide(
#| writer : BitWriter,
#| tokens : ArrayView[Int32],
#| ll_val : FixedArray[UInt32],
#| ll_len : FixedArray[Int32],
#| dd_val : FixedArray[UInt32],
#| dd_len : FixedArray[Int32],
#|) -> Unit {
#| let chunk = FixedArray::make(TOKEN_CHUNK_BYTES, b'\x00')
#| let mut start = 0
#| while start < tokens.length() {
#| let end = start + (tokens.length() - start).min(TOKEN_CHUNK_TOKENS)
#| let count = writer.write_wide_run(
#| tokens[start:end],
#| ll_val,
#| ll_len,
#| dd_val,
#| dd_len,
#| chunk,
#| 0,
#| )
#| writer.write_array(chunk, count)
#| start = end
#| }
#|}
#|fn BitWriter::write_wide_run(
#| writer : BitWriter,
#| tokens : ArrayView[Int32],
#| ll_val : FixedArray[UInt32],
#| ll_len : FixedArray[Int32],
#| dd_val : FixedArray[UInt32],
#| dd_len : FixedArray[Int32],
#| dst : FixedArray[Byte],
#| offset : Int32,
#|) -> Int32 {
#| let mut bitbuf = writer.bitbuf.reinterpret_as_uint().to_uint64()
#| let mut bit_count = writer.bit_count
#| let mut pos = offset
#| for tok in tokens {
#| if tok < 256 {
#| bitbuf = bitbuf | (ll_val[tok].to_uint64() << bit_count)
#| bit_count = bit_count + ll_len[tok]
#| } else {
#| let length = tok >> 16
#| let dist = tok & 0xFFFF
#| let lidx = len_to_idx[length]
#| let lsym = 257 + lidx
#| bitbuf = bitbuf | (ll_val[lsym].to_uint64() << bit_count)
#| bit_count = bit_count + ll_len[lsym]
#| bitbuf = bitbuf |
#| (
#| (length - len_base[lidx]).reinterpret_as_uint().to_uint64() <<
#| bit_count
#| )
#| bit_count = bit_count + len_extra[lidx]
#| let didx = dist_index(dist)
#| bitbuf = bitbuf | (dd_val[didx].to_uint64() << bit_count)
#| bit_count = bit_count + dd_len[didx]
#| bitbuf = bitbuf |
#| (
#| (dist - dist_base[didx]).reinterpret_as_uint().to_uint64() <<
#| bit_count
#| )
#| bit_count = bit_count + dist_extra[didx]
#| }
#| dst.unsafe_write_uint64_le(pos, bitbuf)
#| pos = pos + (bit_count >> 3)
#| bitbuf = bitbuf >> (bit_count & 0x38)
#| bit_count = bit_count & 7
#| }
#| writer.bitbuf = bitbuf.to_int()
#| writer.bit_count = bit_count
#| pos - offset
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn BitWriter::write_tokens(
#| writer : BitWriter,
#| tokens : ArrayView[Int32],
#| ll_val : FixedArray[UInt32],
#| ll_len : FixedArray[Int32],
#| dd_val : FixedArray[UInt32],
#| dd_len : FixedArray[Int32],
#|) -> Unit {
#| if tokens.length() < 64 {
#| writer.write_tokens_bytewise(tokens, ll_val, ll_len, dd_val, dd_len)
#| } else {
#| writer.write_tokens_wide(tokens, ll_val, ll_len, dd_val, dd_len)
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn BitWriter::write_tokens(
#| writer : BitWriter,
#| tokens : ArrayView[Int32],
#| ll_val : FixedArray[UInt32],
#| ll_len : FixedArray[Int32],
#| dd_val : FixedArray[UInt32],
#| dd_len : FixedArray[Int32],
#|) -> Unit {
#| writer.write_tokens_bytewise(tokens, ll_val, ll_len, dd_val, dd_len)
#|}
#|fn BitWriter::write_dynamic_block(
#| writer : BitWriter,
#| codes : DynamicCodes,
#| tokens : ArrayView[Int32],
#|) -> Unit {
#| writer.write_bits(2, 2) // BTYPE = 10 (dynamic)
#| writer.write_bits(codes.lengths.hlit - 257, 5)
#| writer.write_bits(codes.lengths.hdist - 1, 5)
#| writer.write_bits(codes.lengths.hclen - 4, 4)
#| for k in 0.. 0 {
#| writer.write_bits(it.extra, it.nextra)
#| }
#| }
#| writer.write_tokens(
#| tokens,
#| codes.dll_val,
#| codes.lengths.dll_len,
#| codes.dd_val,
#| codes.lengths.dd_len,
#| )
#| writer.write_rcode(codes.dll_val[256], codes.lengths.dll_len[256]) // EOB
#|}
#|fn BitWriter::write_fixed_block(
#| writer : BitWriter,
#| tokens : ArrayView[Int32],
#|) -> Unit {
#| writer.write_bits(1, 2) // BTYPE = 01 (fixed)
#| writer.write_tokens(
#| tokens, fixed_ll_val, fixed_ll_len, fixed_dist_val, fixed_dist_len,
#| )
#| writer.write_rcode(fixed_ll_val[256], fixed_ll_len[256]) // EOB
#|}
#|fn BitWriter::emit_empty_block(writer : BitWriter, input : BytesView) -> Unit {
#| let ll_freq = FixedArray::make(MAX_NUM_LIT, 0)
#| let d_freq = FixedArray::make(MAX_NUM_DIST, 0)
#| let none : FixedArray[Int32] = []
#| writer.emit_block(input, 0, 0, none, ll_freq, d_freq, is_final=true)
#|}
#|fn BitWriter::emit_block(
#| writer : BitWriter,
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#| tokens : ArrayView[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#| is_final~ : Bool,
#|) -> Unit {
#| let encoding = choose_block_encoding(end - start, ll_freq, d_freq)
#| match encoding {
#| Stored => writer.write_stored(data, start, end, is_final~)
#| Fixed => writer.write_huffman_block(None, tokens, is_final)
#| Dynamic(codes) => writer.write_huffman_block(Some(codes), tokens, is_final)
#| }
#|}
#|fn BitWriter::write_stored(
#| writer : BitWriter,
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#| is_final~ : Bool,
#|) -> Unit {
#| let mut p = start
#| for ;; {
#| let n = end - p
#| let chunk = n.min(65535)
#| let last = p + chunk == end
#| if is_final && last {
#| writer.write_bit(1) // BFINAL
#| } else {
#| writer.write_bit(0)
#| }
#| writer.write_bits(0, 2) // BTYPE = 00 (stored)
#| writer.flush() // stored blocks are byte-aligned
#| let nlen = chunk ^ 0xFFFF
#| writer.write_byte((chunk & 0xFF).to_byte())
#| writer.write_byte(((chunk >> 8) & 0xFF).to_byte())
#| writer.write_byte((nlen & 0xFF).to_byte())
#| writer.write_byte(((nlen >> 8) & 0xFF).to_byte())
#| writer.write_bytes(data, p, chunk)
#| p = p + chunk
#| if last {
#| break
#| }
#| }
#|}
),
"decode_rules.mbt": (
#|#inline
#|fn validate_stored_length(len : Int32, nlen : Int32) -> Unit raise InflateError {
#| guard len == (nlen ^ 0xFFFF) else {
#| raise InflateError(Corrupt, "stored block length mismatch")
#| }
#|}
#|#inline
#|fn dynamic_literal_count(bits : Int32) -> Int32 raise InflateError {
#| let count = bits + 257
#| guard count <= MAX_NUM_LIT else {
#| raise InflateError(Corrupt, "corrupt: too many literal codes")
#| }
#| count
#|}
#|#inline
#|fn dynamic_distance_count(bits : Int32) -> Int32 raise InflateError {
#| let count = bits + 1
#| guard count <= MAX_NUM_DIST else {
#| raise InflateError(Corrupt, "corrupt: too many distance codes")
#| }
#| count
#|}
#|#inline
#|fn back_reference_distance_is_valid(
#| distance : Int32,
#| available_history : Int32,
#|) -> Bool {
#| distance.reinterpret_as_uint() - 1U < available_history.reinterpret_as_uint()
#|}
#|#valtype
#|priv struct CodeLengthRepeat {
#| base : Int32
#| extra_bits : Int32
#| value : Int32
#|}
#|fn interpret_code_length_repeat(
#| symbol : Int32,
#| previous : Int32?,
#|) -> CodeLengthRepeat raise InflateError {
#| match symbol {
#| 16 => {
#| guard previous is Some(value) else {
#| raise InflateError(Corrupt, "corrupt: repeat with no previous length")
#| }
#| { base: 3, extra_bits: 2, value, }
#| }
#| 17 => { base: 3, extra_bits: 3, value: 0, }
#| 18 => { base: 11, extra_bits: 7, value: 0, }
#| _ => raise InflateError(Corrupt, "corrupt: bad code-length symbol")
#| }
#|}
#|#inline
#|fn fixed_distance_symbol(bits5 : Int32) -> Int32 {
#| reverse8(((bits5 << 3) & 0xFF).to_byte()).to_int()
#|}
#|fn decode_dynamic_header(
#| read_bits : (Int32) -> Int32 raise InflateError,
#| huff_sym : () -> Int32 raise InflateError,
#| dyn_litlen : HuffmanDecoder,
#| codebits : FixedArray[Int32],
#| clbits : FixedArray[Int32],
#|) -> (Int32, Int32) raise InflateError {
#| let nlit = dynamic_literal_count(read_bits(5))
#| let ndist = dynamic_distance_count(read_bits(5))
#| let nclen = read_bits(4) + 4
#| for i in 0..= 16 else {
#| clbits[i] = x
#| i = i + 1
#| continue
#| }
#| let previous = if i == 0 { None } else { Some(clbits[i - 1]) }
#| let repeat = interpret_code_length_repeat(x, previous)
#| let rep = repeat.base + read_bits(repeat.extra_bits)
#| guard i + rep <= n else {
#| raise InflateError(Corrupt, "corrupt: code-length repeat overflow")
#| }
#| clbits.fill(repeat.value, start=i, end=i + rep)
#| i += rep
#| }
#| (nlit, ndist)
#|}
),
"deflate.mbt": (
#|const DEFLATE_BLOCK_SIZE : Int32 = 16384
#|fn clamp_level(level : Int32) -> Int32 {
#| level.clamp(min=0, max=9)
#|}
#|struct IncrementalEncoder {
#| pending : FixedArray[Byte] // mirrored 64 KiB input ring
#| mut parse_pos : Int32 // first un-emitted absolute byte position
#| mut pending_end : Int32 // one past the last accepted absolute position
#| mut hashed_through : Int32 // first historical position not yet chained
#| finder : MatchFinder // persistent 32 KiB hash-chain matchfinder
#| w : BitWriter // persistent bit sink spanning all blocks
#| mut phase : EmissionPhase
#| mut cursor : Int32 // next payload byte, header symbol, or token in `phase`
#| mut token_high : UInt32 // upper word of the suspended Huffman bit accumulator
#| mut out_pos : Int32 // next byte of the bounded spill
#| mut flushing : Bool // an accepted flush has started emitting its blocks
#| mut emitted_final : Bool // the current or just-completed block has BFINAL
#| mut done : Bool // the final block has been fully drained
#| mut flush_done : Bool // the current buffered content has been sync-flushed
#| mut flush_requested : Bool // sticky until the requested flush is emitted
#| mut end_requested : Bool // sticky until reset; no later input is accepted
#| fast_store : Bool
#| level : Int32 // compression level 0-9 (0 = stored only)
#| cfg : LevelConfig // match-search tuning for `level`
#| tokens : Array[Int32]
#| ll_freq : FixedArray[Int32]
#| d_freq : FixedArray[Int32]
#|}
#|pub fn IncrementalEncoder::IncrementalEncoder(
#| level~ : Int32 = 6,
#| dictionary? : BytesView,
#| fast_store~ : Bool = false,
#|) -> IncrementalEncoder {
#| let lv = clamp_level(level)
#| let deflater = {
#| pending: FixedArray::make(STREAM_RING_STORAGE_SIZE, b'\x00'),
#| parse_pos: 0,
#| pending_end: 0,
#| hashed_through: 0,
#| finder: MatchFinder(),
#| w: BitWriter(spill_on_full=true),
#| phase: Idle,
#| cursor: 0,
#| token_high: 0U,
#| flushing: false,
#| out_pos: 0,
#| emitted_final: false,
#| done: false,
#| flush_done: false,
#| flush_requested: false,
#| end_requested: false,
#| level: lv,
#| fast_store,
#| cfg: level_configs[lv],
#| tokens: [],
#| ll_freq: FixedArray::make(MAX_NUM_LIT, 0),
#| d_freq: FixedArray::make(MAX_NUM_DIST, 0),
#| }
#| if dictionary is Some(dict) {
#| deflater.preload_dictionary(dict)
#| }
#| deflater
#|}
#|fn IncrementalEncoder::preload_dictionary(
#| self : IncrementalEncoder,
#| dictionary : BytesView,
#|) -> Unit {
#| let start = (dictionary.length() - WINDOW_SIZE).max(0)
#| self.push_pending(dictionary[start:])
#| for i in 0.. Unit {
#| self.finder.reset_stream(self.pending_end)
#| self.parse_pos = 0
#| self.pending_end = 0
#| self.hashed_through = 0
#| self.phase = Idle
#| self.flushing = false
#| self.cursor = 0
#| self.token_high = 0U
#| self.w.out.clear()
#| self.w.bitbuf = 0
#| self.w.bit_count = 0
#| self.out_pos = 0
#| self.emitted_final = false
#| self.done = false
#| self.flush_done = false
#| self.flush_requested = false
#| self.end_requested = false
#| if dictionary is Some(dict) {
#| self.preload_dictionary(dict)
#| }
#|}
#|pub fn IncrementalEncoder::is_finished(self : IncrementalEncoder) -> Bool {
#| self.done
#|}
#|fn IncrementalEncoder::latch_action(
#| self : IncrementalEncoder,
#| action : DeflateAction,
#|) -> Unit {
#| match action {
#| Finish => {
#| self.end_requested = true
#| self.flush_requested = false
#| }
#| SyncFlush if !self.flush_done => self.flush_requested = true
#| Continue | SyncFlush => ()
#| }
#|}
#|fn IncrementalEncoder::accept_input(
#| self : IncrementalEncoder,
#| input : BytesView,
#| consumed : Int32,
#| input_end : Int32,
#| capacity : Int32,
#| action : DeflateAction,
#|) -> Int32 {
#| let take = (input_end - consumed).min(capacity)
#| self.push_pending(input[consumed:consumed + take])
#| let consumed = consumed + take
#| if take > 0 {
#| self.flush_done = false
#| }
#| if consumed == input_end {
#| self.latch_action(action)
#| }
#| consumed
#|}
#|pub fn IncrementalEncoder::step(
#| self : IncrementalEncoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| action~ : DeflateAction = Continue,
#|) -> StepResult {
#| guard !self.done else { return { status: Done, consumed: 0, produced: 0, } }
#| if input.length() == 0 && !self.end_requested && !self.emitted_final {
#| self.latch_action(action)
#| }
#| if self.phase is StoredPayload(end) &&
#| self.w.out.is_empty() &&
#| output.length() < end - self.cursor {
#| if output.length() == 1 {
#| output.unsafe_set(0, self.pending.unsafe_get(self.cursor))
#| self.cursor += 1
#| } else {
#| self.copy_stored(output, 0, output.length())
#| }
#| return { status: NeedMoreOutput, consumed: 0, produced: output.length(), }
#| }
#| self.step_emission(input, output, action)
#|}
#|fn IncrementalEncoder::step_emission(
#| self : IncrementalEncoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| action : DeflateAction,
#|) -> StepResult {
#| let mut pos = 0
#| if self.phase is Tokens(codes) && self.w.out.is_empty() {
#| if output.length() == 1 && self.w.bit_count >= 16 {
#| output.unsafe_set(0, self.w.bitbuf.to_byte())
#| self.w.bitbuf = ((self.w.bitbuf.reinterpret_as_uint() >> 8) |
#| (self.token_high << 24)).reinterpret_as_int()
#| self.token_high = self.token_high >> 8
#| self.w.bit_count -= 8
#| return { status: NeedMoreOutput, consumed: 0, produced: 1, }
#| }
#| pos = self.resume_token_prefix(codes, output, 0)
#| if self.phase is Tokens(_) {
#| return { status: NeedMoreOutput, consumed: 0, produced: pos, }
#| }
#| }
#| self.w.direct_output = Some(output_storage(output))
#| self.w.direct_start = output.start_offset()
#| self.w.direct_capacity = output.length()
#| self.w.direct_pos = pos
#| let result = self.step_blocks(input, output, action)
#| self.w.direct_output = None // do not retain caller-owned output between steps
#| result
#|}
#|fn IncrementalEncoder::step_blocks(
#| self : IncrementalEncoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| action : DeflateAction,
#|) -> StepResult {
#| let input_end = input.length()
#| let input_limit = DEFLATE_BLOCK_SIZE + MIN_LOOKAHEAD
#| let mut consumed = 0
#| for ;; {
#| guard self.resume_block(output) else {
#| return { status: NeedMoreOutput, consumed, produced: self.w.direct_pos, }
#| }
#| guard !self.emitted_final else {
#| self.done = true
#| return { status: Done, consumed, produced: self.w.direct_pos, }
#| }
#| if self.flushing || self.flush_requested {
#| self.flush_requested = false
#| if self.pending_bytes() > 0 {
#| self.flushing = true
#| self.plan_block(finish=false)
#| } else {
#| self.start_stored(0, 0, is_final=false)
#| self.flushing = false
#| self.flush_done = true
#| }
#| continue
#| }
#| if self.end_requested {
#| self.plan_block(finish=true)
#| continue
#| }
#| let buffered = self.pending_bytes()
#| guard buffered < input_limit else {
#| self.plan_block(finish=false)
#| continue
#| }
#| guard consumed < input_end else {
#| return { status: NeedMoreInput, consumed, produced: self.w.direct_pos, }
#| }
#| consumed = self.accept_input(
#| input,
#| consumed,
#| input_end,
#| input_limit - buffered,
#| action,
#| )
#| }
#|}
),
"deflate_all.mbt": (
#|#derive(Eq, @debug.Debug)
#|pub(all) enum Strategy {
#| Standard(level~ : Int32)
#| Split(level~ : Int32)
#| Optimal(iterations~ : Int32)
#|}
#|pub fn Strategy::split(level~ : Int32 = 6) -> Strategy {
#| Split(level~)
#|}
#|pub fn Strategy::optimal(iterations~ : Int32 = 15) -> Strategy {
#| Optimal(iterations~)
#|}
#|struct Compressor {
#| strategy : Strategy
#| fast_store : Bool
#| dictionary : Bytes
#| mut prefix : FixedArray[Byte]
#| finder : MatchFinder
#| tokens : Array[Int32]
#| mut output : FixedArray[Byte]
#| ll_freq : FixedArray[Int32]
#| d_freq : FixedArray[Int32]
#|}
#|pub fn Compressor::Compressor(
#| strategy~ : Strategy = Standard(level=6),
#| dictionary? : BytesView,
#| fast_store~ : Bool = false,
#|) -> Compressor {
#| let strategy = match strategy {
#| Standard(level~) => Standard(level=clamp_level(level))
#| Split(level~) => Split(level=clamp_level(level))
#| Optimal(iterations~) => Optimal(iterations=iterations.max(1))
#| }
#| {
#| strategy,
#| fast_store,
#| dictionary: dictionary.map_or(b"", dict => {
#| dict[(dict.length() - WINDOW_SIZE).max(0):].to_owned()
#| }),
#| prefix: [],
#| finder: MatchFinder(input_size=0),
#| tokens: [],
#| output: [],
#| ll_freq: FixedArray::make(MAX_NUM_LIT, 0),
#| d_freq: FixedArray::make(MAX_NUM_DIST, 0),
#| }
#|}
#|pub fn deflate_bound(
#| input_size : Int32,
#| strategy~ : Strategy = Standard(level=6),
#|) -> Int32 {
#| guard input_size >= 0 else { abort("negative DEFLATE input size") }
#| let size = input_size.to_int64()
#| if strategy is Standard(level=0) || strategy is Split(level=0) {
#| let bound = size + 5L * ((size + 65534L) / 65535L).max(1L)
#| guard bound <= 0x7fff_ffffL else {
#| abort("DEFLATE output capacity overflow")
#| }
#| return bound.to_int()
#| }
#| let max_blocks = match strategy {
#| Standard(_) => (size + 65534L) / 65535L
#| Split(_) => (size + 65534L) / 65535L * 7L
#| Optimal(_) =>
#| (size + OPTIMAL_MASTER_BLOCK.to_int64() - 1L) /
#| OPTIMAL_MASTER_BLOCK.to_int64() *
#| OPTIMAL_MAX_BLOCKS.to_int64()
#| }
#| let blocks = max_blocks.min(size).max(1L)
#| let bound = size + 5L * (size / 65535L + blocks) + blocks + 1L
#| guard bound <= 0x7fff_ffffL else { abort("DEFLATE output capacity overflow") }
#| bound.to_int()
#|}
#|pub fn Compressor::compress_into(
#| self : Compressor,
#| input : BytesView,
#| output : MutArrayView[Byte],
#|) -> Int32? {
#| self.encode(input, BitWriter::into(output))
#|}
#|pub fn Compressor::compress(self : Compressor, input : BytesView) -> Bytes {
#| let writer = BitWriter::growing(self.output)
#| ignore(self.encode(input, writer))
#| if writer.direct_output is Some(output) {
#| self.output = output
#| }
#| Bytes::from_array(self.output[:writer.direct_pos])
#|}
#|fn Compressor::encode(
#| self : Compressor,
#| input : BytesView,
#| w : BitWriter,
#|) -> Int32? {
#| let stored = self.strategy is Standard(level=0) ||
#| self.strategy is Split(level=0) ||
#| (self.fast_store && should_fast_store(input))
#| if stored {
#| w.write_stored(input, 0, input.length(), is_final=true)
#| } else {
#| let block_size = match self.strategy {
#| Optimal(_) => OPTIMAL_MASTER_BLOCK
#| _ => 65535
#| }
#| let mut pos = 0
#| if self.dictionary.length() > 0 && input.length() > 0 {
#| let data = self.prepare_prefix(input, block_size)
#| let start = self.dictionary.length()
#| self.finder.reset_for_input(data)
#| for i in 0.. BytesView {
#| let count = input.length().min(block_size + 257)
#| let start = self.dictionary.length()
#| let length = start + count
#| if self.prefix.length() < length {
#| self.prefix = FixedArray::make(length, b'\x00')
#| }
#| self.prefix.blit_from_bytesview(0, self.dictionary)
#| self.prefix.blit_from_bytesview(start, input[:count])
#| borrowed_encode_buffer_bytes(self.prefix)[:length]
#|}
#|fn borrowed_encode_buffer_bytes(buffer : FixedArray[Byte]) -> Bytes = "%identity"
#|fn Compressor::encode_chunk(
#| self : Compressor,
#| input : BytesView,
#| pos : Int32,
#| target : Int32,
#| w : BitWriter,
#| is_final~ : Bool,
#|) -> Int32 {
#| match self.strategy {
#| Standard(level~) => {
#| let processed = self.tokenize(input, pos, target, level_configs[level])
#| w.emit_block(
#| input,
#| pos,
#| processed,
#| self.tokens,
#| self.ll_freq,
#| self.d_freq,
#| is_final=is_final && processed == input.length(),
#| )
#| processed
#| }
#| Split(level~) =>
#| self.encode_split(input, pos, target, w, level_configs[level], is_final~)
#| Optimal(iterations~) =>
#| self.encode_optimal(input, pos, target, w, iterations, is_final~)
#| }
#|}
#|pub fn deflate_all(
#| input : BytesView,
#| strategy~ : Strategy = Standard(level=6),
#| dictionary? : BytesView,
#| fast_store~ : Bool = false,
#|) -> Bytes {
#| Compressor(strategy~, dictionary?, fast_store~).compress(input)
#|}
#|#inline
#|fn next_block_target(pos : Int32, end : Int32, block_size : Int32) -> Int32 {
#| pos + (end - pos).min(block_size)
#|}
#|fn Compressor::tokenize(
#| self : Compressor,
#| input : BytesView,
#| pos : Int32,
#| target : Int32,
#| cfg : LevelConfig,
#|) -> Int32 {
#| self.tokens.clear()
#| self.ll_freq.fill(0)
#| self.d_freq.fill(0)
#| tokenize(
#| input,
#| self.finder,
#| pos,
#| target,
#| cfg,
#| self.tokens,
#| self.ll_freq,
#| self.d_freq,
#| )
#|}
),
"extends.mbt": (
#|pub extend InputMode with Eq::{equal}
#|pub extend InflateErrorKind with Eq::{equal}
#|pub extend InflateError with Show::{to_string}
#|pub extend Status with Eq::{equal}
#|pub extend DeflateAction with Eq::{equal}
#|pub extend Strategy with Eq::{equal}
#|pub extend StepResult with Eq::{equal}
#|pub extend DecompressResult with Eq::{equal}
#|pub extend Decompressed with Eq::{equal}
),
"fast_store.mbt": (
#|fn fast_store_sample(length : Int32, byte_at : (Int32) -> Byte) -> Bool {
#| let frequencies = FixedArray::make(256, 0)
#| if length < 8192 {
#| let mut distinct = 0
#| let mut largest = 0
#| for i in 0.. 240 && largest <= length / 32 else { return false }
#| for distance in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512] {
#| let matches = for i = 0, matches = 0 {
#| guard i < 64 else { break matches }
#| continue i + 1,
#| matches + (if byte_at(i) == byte_at(i + distance) { 1 } else { 0 })
#| }
#| guard matches <= 32 else { return false }
#| }
#| return true
#| }
#| let quarter = length / 4
#| for region in 0..<4 {
#| frequencies.fill(0)
#| let start = region * quarter
#| let end = if region == 3 { length } else { start + quarter }
#| let stride = (end - start) / 512
#| let mut distinct = 0
#| let mut largest = 0
#| let mut local_matches = 0
#| for sample in 0..<512 {
#| let position = start + sample * stride
#| let byte = byte_at(position)
#| let value = byte.to_int()
#| if frequencies[value] == 0 {
#| distinct += 1
#| }
#| frequencies[value] += 1
#| largest = largest.max(frequencies[value])
#| if position >= start + 3 &&
#| (
#| byte == byte_at(position - 1) ||
#| (
#| byte == byte_at(position - 2) &&
#| byte_at(position - 1) == byte_at(position - 3)
#| )
#| ) {
#| local_matches += 1
#| }
#| }
#| guard distinct > 200 && largest <= 16 && local_matches * 10 < 512 else {
#| return false
#| }
#| }
#| true
#|}
#|fn should_fast_store(input : BytesView) -> Bool {
#| guard input.length() >= 1024 else { return false }
#| fast_store_sample(input.length(), i => input[i])
#|}
#|fn should_fast_store_window(
#| data : FixedArray[Byte],
#| start : Int32,
#| end : Int32,
#|) -> Bool {
#| guard end - start >= 1024 else { return false }
#| fast_store_sample(end - start, i => data[(start + i) & STREAM_RING_MASK])
#|}
),
"huffman_build.mbt": (
#|#valtype
#|priv struct Node {
#| lit : Int32
#| freq : Int32
#|}
#|priv struct LevelInfo {
#| mut level : Int32
#| mut last_freq : Int32
#| mut next_char_freq : Int32
#| mut next_pair_freq : Int32
#| mut needed : Int32
#|}
#|fn LevelInfo::LevelInfo() -> LevelInfo {
#| { level: 0, last_freq: 0, next_char_freq: 0, next_pair_freq: 0, needed: 0, }
#|}
#|fn reverse_bits(number : UInt32, bit_length : Int32) -> UInt32 {
#| reverse16(number << (16 - bit_length))
#|}
#|fn bounded_bit_counts(
#| nodes : Array[Node],
#| n : Int32,
#| max_bits_in : Int32,
#|) -> FixedArray[Int32] {
#| let maxi = 2147483647
#| let max_bits = max_bits_in.min(n - 1)
#| let levels = FixedArray::makei(17, _ => LevelInfo())
#| let leaf_counts = FixedArray::make(17 * 17, 0)
#| for level in 1..<=max_bits {
#| levels[level].level = level
#| levels[level].last_freq = nodes[1].freq
#| levels[level].next_char_freq = nodes[2].freq
#| levels[level].next_pair_freq = nodes[0].freq + nodes[1].freq
#| levels[level].needed = 0
#| leaf_counts[level * 17 + level] = 2
#| if level == 1 {
#| levels[level].next_pair_freq = maxi
#| }
#| }
#| levels[max_bits].needed = 2 * n - 4
#| let mut level = max_bits
#| for ;; {
#| let lv = levels[level]
#| if lv.next_pair_freq == maxi && lv.next_char_freq == maxi {
#| lv.needed = 0
#| levels[level + 1].next_pair_freq = maxi
#| level = level + 1
#| continue
#| }
#| let prev_freq = lv.last_freq
#| if lv.next_char_freq < lv.next_pair_freq {
#| let nn = leaf_counts[level * 17 + level] + 1
#| lv.last_freq = lv.next_char_freq
#| leaf_counts[level * 17 + level] = nn
#| lv.next_char_freq = if nn < n { nodes[nn].freq } else { maxi }
#| } else {
#| lv.last_freq = lv.next_pair_freq
#| leaf_counts.blit_to(
#| leaf_counts,
#| len=level,
#| src_offset=(level - 1) * 17,
#| dst_offset=level * 17,
#| )
#| levels[lv.level - 1].needed = 2
#| }
#| lv.needed = lv.needed - 1
#| if lv.needed == 0 {
#| if lv.level == max_bits {
#| break
#| }
#| levels[lv.level + 1].next_pair_freq = prev_freq + lv.last_freq
#| level = level + 1
#| } else {
#| while levels[level - 1].needed > 0 {
#| level = level - 1
#| }
#| }
#| }
#| let bit_count = FixedArray::make(max_bits + 1, 0)
#| let counts = leaf_counts[max_bits * 17:(max_bits + 1) * 17]
#| let mut bits = 1
#| for lvl = max_bits; lvl > 0; lvl = lvl - 1 {
#| bit_count[bits] = counts[lvl] - counts[lvl - 1]
#| bits = bits + 1
#| }
#| bit_count
#|}
#|fn assign_codes(
#| bit_count : FixedArray[Int32],
#| nodes : Array[Node],
#| code_len : FixedArray[Int32],
#| code_val : FixedArray[UInt32]?,
#|) -> Unit {
#| let mut rem = nodes.length()
#| for nlen in 1.. 0 {
#| values[symbol] = reverse_bits(next_code[nlen].reinterpret_as_uint(), nlen)
#| next_code[nlen] += 1
#| }
#| }
#|}
#|fn gen_huffman(
#| freq : FixedArray[Int32],
#| size : Int32,
#| max_bits : Int32,
#| code_len : FixedArray[Int32],
#| code_val? : FixedArray[UInt32],
#|) -> Unit {
#| let nodes : Array[Node] = []
#| for i in 0.. 2 else {
#| for k in 0.. {
#| if a.freq != b.freq {
#| a.freq - b.freq
#| } else {
#| a.lit - b.lit
#| }
#| })
#| let bit_count = bit_counts(nodes, count, max_bits)
#| assign_codes(bit_count, nodes, code_len, code_val)
#|}
#|fn bit_counts(
#| nodes : Array[Node],
#| n : Int32,
#| max_bits : Int32,
#|) -> FixedArray[Int32] {
#| let parents = FixedArray::make(2 * n - 1, 0)
#| let weights = FixedArray::make(n - 1, 0)
#| let mut leaf = 0
#| let mut branch = 0
#| for built in 0..<(n - 1) {
#| let mut sum = 0
#| for _ in 0..<2 {
#| if leaf < n && (branch == built || nodes[leaf].freq < weights[branch]) {
#| sum += nodes[leaf].freq
#| parents[leaf] = n + built
#| leaf += 1
#| } else {
#| sum += weights[branch]
#| parents[n + branch] = n + built
#| branch += 1
#| }
#| }
#| weights[built] = sum
#| }
#| let counts = FixedArray::make(max_bits + 1, 0)
#| for i = 2 * n - 3; i >= 0; i = i - 1 {
#| let depth = parents[parents[i]] + 1
#| guard depth <= max_bits else {
#| return bounded_bit_counts(nodes, n, max_bits)
#| }
#| parents[i] = depth
#| if i < n {
#| counts[depth] += 1
#| }
#| }
#| counts
#|}
),
"huffman_table.mbt": (
#|const HUFFMAN_CHUNK_BITS : Int32 = 9
#|const HUFFMAN_VALUE_SHIFT : Int32 = 4
#|const MAX_CODE_LEN : Int32 = 16
#|priv struct HuffmanDecoder {
#| mut min : Int32
#| chunks : FixedArray[UInt32]
#| mut links : FixedArray[UInt32]
#| mut link_mask : UInt32
#|}
#|fn HuffmanDecoder::HuffmanDecoder() -> HuffmanDecoder {
#| { min: 0, chunks: FixedArray::make(512, 0U), links: [], link_mask: 0, }
#|}
#|fn HuffmanDecoder::initialize(
#| self : HuffmanDecoder,
#| lengths : ArrayView[Int32],
#| metadata~ : FixedArray[UInt32]? = None,
#| length_totals~ : Bool = false,
#|) -> Bool {
#| if self.min != 0 {
#| self.min = 0
#| self.links = []
#| self.link_mask = 0
#| }
#| let count = FixedArray::make(MAX_CODE_LEN, 0)
#| let mut min = 0
#| let mut max = 0
#| for n in lengths {
#| if n == 0 {
#| continue
#| }
#| if min == 0 || n < min {
#| min = n
#| }
#| if n > max {
#| max = n
#| }
#| count[n] = count[n] + 1
#| }
#| guard max != 0 else {
#| self.chunks.fill(0U)
#| return true
#| }
#| let mut code = 0
#| let nextcode = FixedArray::make(MAX_CODE_LEN, 0)
#| for i in min..<=max {
#| code = code << 1
#| nextcode[i] = code
#| code = code + count[i]
#| }
#| let complete = code == 1 << max
#| if !complete {
#| self.chunks.fill(0U)
#| }
#| guard complete || (code == 1 && max == 1) else { return false }
#| self.min = min
#| if max > HUFFMAN_CHUNK_BITS {
#| let num_links = 1 << (max - HUFFMAN_CHUNK_BITS)
#| self.link_mask = (num_links - 1).reinterpret_as_uint()
#| let link = nextcode[HUFFMAN_CHUNK_BITS + 1] >> 1
#| self.links = FixedArray::make((512 - link) * num_links, 0U)
#| for j in link..<512 {
#| let mut reverse = reverse16((j & 0xffff).reinterpret_as_uint()).reinterpret_as_int()
#| reverse = reverse >> (16 - HUFFMAN_CHUNK_BITS)
#| let off = (j - link) * num_links
#| self.chunks[reverse] = (off.reinterpret_as_uint() << HUFFMAN_VALUE_SHIFT) |
#| (HUFFMAN_CHUNK_BITS + 1).reinterpret_as_uint()
#| }
#| }
#| for idx, n in lengths {
#| if n == 0 {
#| continue
#| }
#| let code = nextcode[n]
#| nextcode[n] = nextcode[n] + 1
#| let extra = if metadata is Some(values) { values[idx] } else { 0U }
#| let extra = if length_totals && extra != 0U {
#| extra | ((n.reinterpret_as_uint() + ((extra >> 13) & 7U)) << 25)
#| } else {
#| extra
#| }
#| let chunk = (idx << HUFFMAN_VALUE_SHIFT).reinterpret_as_uint() |
#| n.reinterpret_as_uint() |
#| extra
#| let mut reverse = reverse16((code & 0xffff).reinterpret_as_uint()).reinterpret_as_int()
#| reverse = reverse >> (16 - n)
#| if n <= HUFFMAN_CHUNK_BITS {
#| for off = reverse; off < self.chunks.length(); off = off + (1 << n) {
#| self.chunks[off] = chunk
#| }
#| } else {
#| let j = reverse & 0x1FF
#| let value = (self.chunks[j] >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int()
#| let num_links = 1 << (max - HUFFMAN_CHUNK_BITS)
#| reverse = reverse >> HUFFMAN_CHUNK_BITS
#| for off = reverse
#| off < num_links
#| off = off + (1 << (n - HUFFMAN_CHUNK_BITS)) {
#| self.links[value + off] = chunk
#| }
#| }
#| }
#| true
#|}
#|fn make_fixed_huffman_decoder() -> HuffmanDecoder {
#| let lengths = FixedArray::make(288, 0)
#| lengths.fill(8, start=0, end=144)
#| lengths.fill(9, start=144, end=256)
#| lengths.fill(7, start=256, end=280)
#| lengths.fill(8, start=280, end=288)
#| let h = HuffmanDecoder()
#| let _ = h.initialize(lengths)
#| h
#|}
#|let fixed_huffman_decoder : HuffmanDecoder = make_fixed_huffman_decoder()
#|#cfg(target="native")
#|priv enum InflateAlphabet {
#| LiteralLength
#| Distance
#|}
#|#cfg(target="native")
#|let native_length_metadata : FixedArray[UInt32] = FixedArray::makei(288, symbol => {
#| if symbol >= 257 && symbol < 286 {
#| length_code_info_table[symbol - 257].reinterpret_as_uint() << 13
#| } else {
#| 0U
#| }
#|})
#|#cfg(target="native")
#|let native_distance_metadata : FixedArray[UInt32] = FixedArray::makei(32, symbol => {
#| if symbol < MAX_NUM_DIST {
#| distance_code_info_table[symbol].reinterpret_as_uint() << 13
#| } else {
#| 0U
#| }
#|})
#|#cfg(target="native")
#|fn HuffmanDecoder::initialize_data(
#| self : HuffmanDecoder,
#| lengths : ArrayView[Int32],
#| alphabet : InflateAlphabet,
#|) -> Bool {
#| let metadata = match alphabet {
#| LiteralLength => native_length_metadata
#| Distance => native_distance_metadata
#| }
#| self.initialize(
#| lengths,
#| metadata=Some(metadata),
#| length_totals=alphabet is LiteralLength,
#| )
#|}
#|#cfg(target="native")
#|fn HuffmanDecoder::initialize_litlen(
#| self : HuffmanDecoder,
#| lengths : ArrayView[Int32],
#|) -> Bool {
#| self.initialize_data(lengths, LiteralLength)
#|}
#|#cfg(target="native")
#|fn HuffmanDecoder::initialize_distance(
#| self : HuffmanDecoder,
#| lengths : ArrayView[Int32],
#|) -> Bool {
#| self.initialize_data(lengths, Distance)
#|}
#|#cfg(not(target="native"))
#|fn HuffmanDecoder::initialize_litlen(
#| self : HuffmanDecoder,
#| lengths : ArrayView[Int32],
#|) -> Bool {
#| self.initialize(lengths)
#|}
#|#cfg(not(target="native"))
#|fn HuffmanDecoder::initialize_distance(
#| self : HuffmanDecoder,
#| lengths : ArrayView[Int32],
#|) -> Bool {
#| self.initialize(lengths)
#|}
),
"inflate.mbt": (
#|#derive(Eq, @debug.Debug)
#|#non_exhaustive
#|pub(all) enum InflateErrorKind {
#| Truncated
#| Corrupt
#| TrailingData
#| OutputLimitExceeded(limit~ : UInt64, observed~ : UInt64)
#| Cancelled
#| InvalidArgument
#|}
#|#derive(@debug.Debug)
#|pub suberror InflateError {
#| InflateError(InflateErrorKind, String)
#|}
#|pub impl Show for InflateError with fn output(self, logger) {
#| let InflateError(kind, message) = self
#| match kind {
#| OutputLimitExceeded(limit~, observed~) =>
#| logger <+ "OutputLimitExceeded(limit=\{limit}, observed=\{observed})"
#| _ => logger.write_string(@debug.to_string(kind))
#| }
#| logger <+ ": \{message}"
#|}
#|priv suberror NeedInput
#|priv enum DecoderErrorClass {
#| DecoderInputExhausted
#| DecoderFailure(InflateErrorKind, String)
#|}
#|fn classify_decoder_error(err : Error) -> DecoderErrorClass {
#| match err {
#| NeedInput => DecoderInputExhausted
#| InflateError(kind, message) => DecoderFailure(kind, message)
#| _ => DecoderFailure(Corrupt, "corrupt: unexpected decoder error")
#| }
#|}
#|priv enum Mode {
#| AtBlock // about to read the next block header
#| InStored // copying a stored block (stored_remaining bytes left)
#| InHuffman // decoding literal/match units with hl/hd
#| Done
#|}
#|struct IncrementalDecoder {
#| mut mode : Mode
#| mut bitbuf : Int32 // LSB-first bit accumulator
#| mut bit_count : Int32
#| mut final_block : Bool
#| mut stored_remaining : Int32
#| mut hl : HuffmanDecoder? // literal/length table (None until a block sets it)
#| mut hd : HuffmanDecoder? // distance table (None => fixed 5-bit distances)
#| mut hd_fixed : Bool
#| window : FixedArray[Byte] // 32 KB circular history
#| mut wpos : Int32 // total bytes ever written; window index = wpos & 32767
#| mut filled : Bool // window has held >= 32 KB, so any distance <= 32768 is valid
#| dyn_litlen : HuffmanDecoder // dynamic literal/length (and code-length) table
#| dyn_dist : HuffmanDecoder // dynamic distance table
#| clbits : FixedArray[Int32]
#| codebits : FixedArray[Int32]
#| mut copy_remaining : Int32
#| mut copy_dist : Int32
#| mut staged_input : Array[Byte]
#| mut staged_pos : Int32
#| mut failure_kind : InflateErrorKind? // stable terminal error
#| mut failure_message : String
#| mut view : BytesView
#| mut in_pos : Int32
#| mut out_pos : Int32
#| mut out : MutArrayView[Byte]
#| mut out_end : Int32 // one past the last writable index in `out`
#| empty_out : FixedArray[Byte]
#|}
#|pub fn IncrementalDecoder::IncrementalDecoder(
#| dictionary? : BytesView,
#|) -> IncrementalDecoder {
#| let empty_out = FixedArray::make(0, b'\x00')
#| let inflater : IncrementalDecoder = {
#| mode: AtBlock,
#| bitbuf: 0,
#| bit_count: 0,
#| final_block: false,
#| stored_remaining: 0,
#| hl: None,
#| hd: None,
#| hd_fixed: false,
#| window: FixedArray::make(WINDOW_SIZE, b'\x00'),
#| wpos: 0,
#| filled: false,
#| dyn_litlen: HuffmanDecoder(),
#| dyn_dist: HuffmanDecoder(),
#| clbits: FixedArray::make(MAX_NUM_LIT + MAX_NUM_DIST, 0),
#| codebits: FixedArray::make(NUM_CODES, 0),
#| copy_remaining: 0,
#| copy_dist: 0,
#| staged_input: [],
#| staged_pos: 0,
#| failure_kind: None,
#| failure_message: "",
#| view: b"",
#| in_pos: 0,
#| out_pos: 0,
#| out: empty_out.mut_view(),
#| out_end: 0,
#| empty_out,
#| }
#| if dictionary is Some(dict) {
#| inflater.preload_dictionary(dict)
#| }
#| inflater
#|}
#|pub fn IncrementalDecoder::is_finished(self : IncrementalDecoder) -> Bool {
#| self.mode is Done
#|}
#|fn IncrementalDecoder::preload_dictionary(
#| self : IncrementalDecoder,
#| dict : BytesView,
#|) -> Unit {
#| let start = (dict.length() - WINDOW_SIZE).max(0)
#| self.window.blit_from_bytesview(0, dict[start:])
#| self.wpos = dict.length() - start
#| self.filled = self.wpos == WINDOW_SIZE
#|}
#|pub fn IncrementalDecoder::reset(
#| self : IncrementalDecoder,
#| dictionary? : BytesView,
#|) -> Unit {
#| self.mode = AtBlock
#| self.bitbuf = 0
#| self.bit_count = 0
#| self.final_block = false
#| self.stored_remaining = 0
#| self.hl = None
#| self.hd = None
#| self.hd_fixed = false
#| self.wpos = 0
#| self.filled = false
#| self.copy_remaining = 0
#| self.copy_dist = 0
#| self.staged_input.clear()
#| self.staged_pos = 0
#| self.failure_kind = None
#| self.failure_message = ""
#| self.view = b""
#| self.in_pos = 0
#| self.out_pos = 0
#| self.out = self.empty_out.mut_view()
#| self.out_end = 0
#| if dictionary is Some(dict) {
#| self.preload_dictionary(dict)
#| }
#|}
#|fn IncrementalDecoder::next_fill_byte(self : IncrementalDecoder) -> Byte? {
#| if self.staged_pos < self.staged_input.length() {
#| let byte = self.staged_input[self.staged_pos]
#| self.staged_pos = self.staged_pos + 1
#| Some(byte)
#| } else if self.in_pos < self.view.length() {
#| let byte = self.view[self.in_pos]
#| self.in_pos = self.in_pos + 1
#| Some(byte)
#| } else {
#| None
#| }
#|}
#|fn IncrementalDecoder::fill(
#| self : IncrementalDecoder,
#| n : Int32,
#|) -> Unit raise NeedInput {
#| while self.bit_count < n {
#| guard self.next_fill_byte() is Some(b) else { raise NeedInput }
#| self.bitbuf = self.bitbuf | (b.to_int() << self.bit_count)
#| self.bit_count = self.bit_count + 8
#| }
#|}
#|fn IncrementalDecoder::read_bits(
#| self : IncrementalDecoder,
#| n : Int32,
#|) -> Int32 raise NeedInput {
#| self.fill(n)
#| let v = self.bitbuf & ((1 << n) - 1)
#| self.bitbuf = self.bitbuf >> n
#| self.bit_count = self.bit_count - n
#| v
#|}
#|fn IncrementalDecoder::huff_sym(
#| self : IncrementalDecoder,
#| h : HuffmanDecoder,
#|) -> Int32 raise {
#| let mut n = h.min
#| for ;; {
#| self.fill(n)
#| let mut chunk = h.chunks[self.bitbuf & 0x1FF]
#| n = (chunk & 0xF).reinterpret_as_int()
#| if n > HUFFMAN_CHUNK_BITS {
#| let link = ((self.bitbuf >> HUFFMAN_CHUNK_BITS).reinterpret_as_uint() &
#| h.link_mask).reinterpret_as_int()
#| chunk = h.links[(chunk >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int() + link]
#| n = (chunk & 0xF).reinterpret_as_int()
#| }
#| if n <= self.bit_count {
#| if n == 0 {
#| raise InflateError(Corrupt, "corrupt: bad Huffman code")
#| }
#| self.bitbuf = self.bitbuf >> n
#| self.bit_count = self.bit_count - n
#| return (chunk >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int()
#| }
#| }
#|}
#|fn IncrementalDecoder::emit(self : IncrementalDecoder, b : Byte) -> Unit {
#| self.out.unsafe_set(self.out_pos, b)
#| self.out_pos = self.out_pos + 1
#| self.window[self.wpos & (WINDOW_SIZE - 1)] = b
#| self.wpos = self.wpos + 1
#| if !self.filled && self.wpos >= WINDOW_SIZE {
#| self.filled = true
#| }
#|}
#|fn streaming_header_error(err : Error) -> InflateError {
#| match err {
#| NeedInput => InflateError(Truncated, "unexpected end of input")
#| InflateError(kind, message) => InflateError(kind, message)
#| _ => InflateError(Corrupt, "corrupt: unexpected decoder error")
#| }
#|}
#|fn IncrementalDecoder::read_dynamic(self : IncrementalDecoder) -> Unit raise {
#| let (nlit, ndist) = decode_dynamic_header(
#| n => {
#| self.read_bits(n) catch {
#| NeedInput => raise InflateError(Truncated, "unexpected end of input")
#| }
#| },
#| () => {
#| self.huff_sym(self.dyn_litlen) catch {
#| err => raise streaming_header_error(err)
#| }
#| },
#| self.dyn_litlen,
#| self.codebits,
#| self.clbits,
#| ) catch {
#| InflateError(Truncated, _) => raise NeedInput
#| InflateError(kind, message) => raise InflateError(kind, message)
#| }
#| guard self.dyn_litlen.initialize(self.clbits[0:nlit]) else {
#| raise InflateError(Corrupt, "corrupt: bad literal/length tree")
#| }
#| guard self.dyn_dist.initialize(self.clbits[nlit:nlit + ndist]) else {
#| raise InflateError(Corrupt, "corrupt: bad distance tree")
#| }
#|}
#|fn IncrementalDecoder::enter_huffman_mode(
#| self : IncrementalDecoder,
#| hl : HuffmanDecoder,
#| hd : HuffmanDecoder?,
#| fixed : Bool,
#|) -> Unit {
#| self.hl = Some(hl)
#| self.hd = hd
#| self.hd_fixed = fixed
#| self.mode = InHuffman
#|}
#|fn IncrementalDecoder::parse_block_header(
#| self : IncrementalDecoder,
#|) -> Unit raise {
#| self.fill(3)
#| self.final_block = (self.bitbuf & 1) == 1
#| let btype = (self.bitbuf >> 1) & 3
#| self.bitbuf = self.bitbuf >> 3
#| self.bit_count = self.bit_count - 3
#| match btype {
#| 0 => {
#| let drop = self.bit_count & 7
#| self.bitbuf = self.bitbuf >> drop
#| self.bit_count = self.bit_count - drop
#| let len = self.read_bits(16)
#| let nlen = self.read_bits(16)
#| validate_stored_length(len, nlen)
#| self.stored_remaining = len
#| self.mode = InStored
#| }
#| 1 => self.enter_huffman_mode(fixed_huffman_decoder, None, true)
#| 2 => {
#| self.read_dynamic()
#| self.enter_huffman_mode(self.dyn_litlen, Some(self.dyn_dist), false)
#| }
#| _ => raise InflateError(Corrupt, "corrupt: reserved block type")
#| }
#|}
#|fn IncrementalDecoder::next_stored_byte(self : IncrementalDecoder) -> Byte? {
#| if self.bit_count >= 8 {
#| let b = (self.bitbuf & 0xFF).to_byte()
#| self.bitbuf = self.bitbuf >> 8
#| self.bit_count = self.bit_count - 8
#| Some(b)
#| } else {
#| self.next_fill_byte()
#| }
#|}
#|fn IncrementalDecoder::stage_incomplete_input(
#| self : IncrementalDecoder,
#|) -> Unit {
#| let kept = self.staged_input[self.staged_pos:].to_owned()
#| kept.push_iter(self.view[self.in_pos:].iter())
#| self.staged_input = kept
#| self.staged_pos = 0
#|}
#|fn IncrementalDecoder::compact_staged_input(self : IncrementalDecoder) -> Unit {
#| guard self.staged_pos != 0 else { return }
#| guard self.staged_pos < self.staged_input.length() else {
#| self.staged_input.clear()
#| self.staged_pos = 0
#| return
#| }
#| self.staged_input = self.staged_input[self.staged_pos:].to_owned()
#| self.staged_pos = 0
#|}
#|fn IncrementalDecoder::release_step_views(self : IncrementalDecoder) -> Unit {
#| self.view = b""
#| self.in_pos = 0
#| self.out_pos = 0
#| self.out = self.empty_out.mut_view()
#| self.out_end = 0
#|}
#|#inline
#|fn IncrementalDecoder::advance_window(
#| self : IncrementalDecoder,
#| count : Int32,
#|) -> Unit {
#| self.wpos = self.wpos + count
#| if !self.filled && self.wpos >= WINDOW_SIZE {
#| self.filled = true
#| }
#|}
#|#inline
#|fn IncrementalDecoder::write_window_to_output(
#| self : IncrementalDecoder,
#| start : Int32,
#| length : Int32,
#|) -> Unit {
#| self.copy_window_to_view(start, length)
#| self.out_pos = self.out_pos + length
#|}
#|#cfg(not(target="js"))
#|#inline
#|fn IncrementalDecoder::copy_window_to_view(
#| self : IncrementalDecoder,
#| start : Int32,
#| length : Int32,
#|) -> Unit {
#| self.out.blit_from_array(
#| self.window[start:start + length],
#| dst_offset=self.out_pos,
#| )
#|}
#|#cfg(target="js")
#|#inline
#|fn IncrementalDecoder::copy_window_to_view(
#| self : IncrementalDecoder,
#| start : Int32,
#| length : Int32,
#|) -> Unit {
#| let window = self.window
#| let output = self.out
#| let out_pos = self.out_pos
#| for i in 0.. Unit {
#| let mut source = source
#| let mut remaining = length
#| while remaining > 0 {
#| let src = source & (WINDOW_SIZE - 1)
#| let dst = self.wpos & (WINDOW_SIZE - 1)
#| let chunk = remaining.min(WINDOW_SIZE - src).min(WINDOW_SIZE - dst)
#| FixedArray::unsafe_blit(self.window, dst, self.window, src, chunk)
#| self.write_window_to_output(dst, chunk)
#| self.advance_window(chunk)
#| source = source + chunk
#| remaining = remaining - chunk
#| }
#|}
#|const SHORT_MATCH_COPY : Int32 = 48
#|#cfg(not(target="js"))
#|#inline
#|fn use_bulk_match(distance : Int32) -> Bool {
#| distance == 1
#|}
#|#cfg(target="js")
#|#inline
#|fn use_bulk_match(_distance : Int32) -> Bool {
#| false
#|}
#|fn IncrementalDecoder::drain_match(self : IncrementalDecoder) -> Unit {
#| let total = self.copy_remaining.min(self.out_end - self.out_pos)
#| guard total > 0 else { return }
#| let source = (self.wpos - self.copy_dist) & (WINDOW_SIZE - 1)
#| let target = self.wpos & (WINDOW_SIZE - 1)
#| if (total <= SHORT_MATCH_COPY || !use_bulk_match(self.copy_dist)) &&
#| source <= WINDOW_SIZE - total &&
#| target <= WINDOW_SIZE - total {
#| let window = self.window
#| let out_pos = self.out_pos
#| let output = self.out
#| for i in 0.. 0 {
#| let dst = self.wpos & (WINDOW_SIZE - 1)
#| let chunk = remaining.min(WINDOW_SIZE - dst)
#| self.window.fill(repeated, start=dst, end=dst + chunk)
#| self.write_window_to_output(dst, chunk)
#| self.advance_window(chunk)
#| remaining = remaining - chunk
#| }
#| } else {
#| let mut remaining = total
#| while remaining > 0 {
#| let chunk = remaining.min(self.copy_dist)
#| self.copy_window_segment(self.wpos - self.copy_dist, chunk)
#| remaining = remaining - chunk
#| }
#| }
#| self.copy_remaining = self.copy_remaining - total
#|}
#|fn IncrementalDecoder::copy_stored_view(
#| self : IncrementalDecoder,
#| source : Int32,
#| length : Int32,
#|) -> Unit {
#| let mut source = source
#| let mut remaining = length
#| while remaining > 0 {
#| let dst = self.wpos & (WINDOW_SIZE - 1)
#| let chunk = remaining.min(WINDOW_SIZE - dst)
#| self.window.blit_from_bytesview(dst, self.view[source:source + chunk])
#| self.write_window_to_output(dst, chunk)
#| self.advance_window(chunk)
#| source = source + chunk
#| remaining = remaining - chunk
#| }
#|}
#|fn IncrementalDecoder::decode_unit(self : IncrementalDecoder) -> Bool raise {
#| let v = self.huff_sym(self.hl.unwrap())
#| guard v >= 256 else {
#| self.emit(v.to_byte())
#| return false
#| }
#| guard v != 256 else { return true }
#| guard v < 286 else {
#| raise InflateError(Corrupt, "corrupt: invalid literal/length code")
#| }
#| let length_info = length_code_info_table[v - 257]
#| let length = (length_info >> 3) + self.read_bits(length_info & 0x7)
#| let dsym = if self.hd_fixed {
#| fixed_distance_symbol(self.read_bits(5))
#| } else {
#| self.huff_sym(self.hd.unwrap())
#| }
#| guard dsym.reinterpret_as_uint() < MAX_NUM_DIST.reinterpret_as_uint() else {
#| raise InflateError(Corrupt, "corrupt: invalid distance code")
#| }
#| let distance_info = distance_code_info_table[dsym]
#| let dist = (distance_info >> 4) + self.read_bits(distance_info & 0xF)
#| guard self.filled || back_reference_distance_is_valid(dist, self.wpos) else {
#| raise InflateError(Corrupt, "corrupt: distance too far back")
#| }
#| self.copy_remaining = length
#| self.copy_dist = dist
#| self.drain_match()
#| false
#|}
#|#cfg(target="native")
#|fn IncrementalDecoder::decode_fast(self : IncrementalDecoder) -> Unit {
#| guard self.hl is Some(hl) else { return }
#| let input = self.view
#| let input_length = input.length()
#| let output = self.out
#| let out_end = self.out_end
#| let window = self.window
#| let fixed_distance = self.hd_fixed
#| let hd = match self.hd {
#| Some(tree) => tree
#| None => hl // never consulted: fixed blocks read five distance bits
#| }
#| guard fixed_distance || self.hd is Some(_) else { return }
#| let mut bits = self.bitbuf.reinterpret_as_uint().to_uint64()
#| let mut count = self.bit_count
#| let mut pos = self.in_pos
#| let first_out_pos = self.out_pos
#| let mut out_pos = first_out_pos
#| let mut wpos = self.wpos
#| for ;; {
#| guard pos >= 0 &&
#| pos <= input_length - 8 &&
#| out_pos >= 0 &&
#| out_pos <= out_end - 258 else {
#| break
#| }
#| if count < 48 {
#| let loaded = input.unsafe_read_uint64_le(pos)
#| let bytes = (63 - count) >> 3
#| bits = bits | (loaded << count)
#| count = count + (bytes << 3)
#| pos = pos + bytes
#| }
#| let entry = batch_huffman_entry(hl, bits)
#| let used = (entry & 15).reinterpret_as_int()
#| guard used != 0 else { break }
#| let symbol = (entry >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int()
#| if symbol < 256 {
#| bits = bits >> used
#| count = count - used
#| output.unsafe_set(out_pos, symbol.to_byte())
#| window.unsafe_set(wpos & (WINDOW_SIZE - 1), symbol.to_byte())
#| out_pos = out_pos + 1
#| wpos = wpos + 1
#| continue
#| }
#| if symbol == 256 {
#| bits = bits >> used
#| count = count - used
#| self.mode = if self.final_block { Done } else { AtBlock }
#| break
#| }
#| guard symbol < 286 else { break }
#| let length_info = length_code_info_table[symbol - 257]
#| let length_extra = length_info & 7
#| let length = (length_info >> 3) +
#| ((bits >> used).to_int() & ((1 << length_extra) - 1))
#| let mut word = bits >> (used + length_extra)
#| let mut remaining = count - used - length_extra
#| let distance_symbol = if fixed_distance {
#| let code = fixed_distance_symbol(word.to_int() & 31)
#| word = word >> 5
#| remaining = remaining - 5
#| code
#| } else {
#| let distance_entry = batch_huffman_entry(hd, word)
#| let distance_used = (distance_entry & 15).reinterpret_as_int()
#| guard distance_used != 0 else { break }
#| word = word >> distance_used
#| remaining = remaining - distance_used
#| (distance_entry >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int()
#| }
#| guard distance_symbol.reinterpret_as_uint() <
#| MAX_NUM_DIST.reinterpret_as_uint() else {
#| break
#| }
#| let distance_info = distance_code_info_table[distance_symbol]
#| let distance_extra = distance_info & 15
#| let distance = (distance_info >> 4) +
#| (word.to_int() & ((1 << distance_extra) - 1))
#| guard self.filled ||
#| wpos >= WINDOW_SIZE ||
#| back_reference_distance_is_valid(distance, wpos) else {
#| break
#| }
#| bits = word >> distance_extra
#| count = remaining - distance_extra
#| self.out_pos = out_pos
#| self.wpos = wpos
#| self.copy_remaining = length
#| self.copy_dist = distance
#| self.drain_match()
#| out_pos = self.out_pos
#| wpos = self.wpos
#| }
#| self.in_pos = pos - (count >> 3)
#| self.bit_count = count & 7
#| self.bitbuf = (bits.to_uint() & ((1U << (count & 7)) - 1)).reinterpret_as_int()
#| self.out_pos = out_pos
#| self.wpos = wpos
#| if !self.filled &&
#| (wpos >= WINDOW_SIZE || out_pos - first_out_pos >= WINDOW_SIZE) {
#| self.filled = true
#| }
#|}
#|#cfg(not(target="native"))
#|#inline
#|fn IncrementalDecoder::decode_fast(_self : IncrementalDecoder) -> Unit {
#|}
#|#valtype
#|priv struct CursorMark {
#| bitbuf : Int32
#| bit_count : Int32
#| staged_pos : Int32
#| in_pos : Int32
#|}
#|#inline
#|fn IncrementalDecoder::mark_cursor(self : IncrementalDecoder) -> CursorMark {
#| {
#| bitbuf: self.bitbuf,
#| bit_count: self.bit_count,
#| staged_pos: self.staged_pos,
#| in_pos: self.in_pos,
#| }
#|}
#|#inline
#|fn IncrementalDecoder::rewind_cursor(
#| self : IncrementalDecoder,
#| mark : CursorMark,
#|) -> Unit {
#| self.bitbuf = mark.bitbuf
#| self.bit_count = mark.bit_count
#| self.staged_pos = mark.staged_pos
#| self.in_pos = mark.in_pos
#|}
#|fn IncrementalDecoder::retry_or_raise(
#| self : IncrementalDecoder,
#| mark : CursorMark,
#| err : Error,
#|) -> Status raise InflateError {
#| match classify_decoder_error(err) {
#| DecoderInputExhausted => {
#| self.rewind_cursor(mark)
#| NeedMoreInput
#| }
#| DecoderFailure(kind, message) => raise InflateError(kind, message)
#| }
#|}
#|fn IncrementalDecoder::run_step(
#| self : IncrementalDecoder,
#|) -> Status raise InflateError {
#| for ;; {
#| match self.mode {
#| Done => return Done
#| AtBlock => {
#| let mark = self.mark_cursor()
#| self.parse_block_header() catch {
#| err => return self.retry_or_raise(mark, err)
#| }
#| }
#| InStored => {
#| while self.stored_remaining > 0 {
#| guard self.out_pos < self.out_end else { return NeedMoreOutput }
#| if self.bit_count == 0 &&
#| self.staged_pos == self.staged_input.length() &&
#| self.in_pos < self.view.length() {
#| let output_room = self.out_end - self.out_pos
#| let input_room = self.view.length() - self.in_pos
#| let chunk = self.stored_remaining.min(output_room).min(input_room)
#| self.copy_stored_view(self.in_pos, chunk)
#| self.in_pos = self.in_pos + chunk
#| self.stored_remaining = self.stored_remaining - chunk
#| } else {
#| guard self.next_stored_byte() is Some(b) else {
#| return NeedMoreInput
#| }
#| self.emit(b)
#| self.stored_remaining = self.stored_remaining - 1
#| }
#| }
#| self.mode = if self.final_block { Done } else { AtBlock }
#| }
#| InHuffman => {
#| if self.copy_remaining > 0 {
#| self.drain_match()
#| guard self.copy_remaining == 0 else { return NeedMoreOutput }
#| }
#| guard self.out_pos < self.out_end else { return NeedMoreOutput }
#| if self.bit_count < 8 &&
#| self.staged_pos == self.staged_input.length() &&
#| self.view.length() - self.in_pos >= 8 &&
#| self.out_end - self.out_pos >= 258 {
#| self.decode_fast()
#| guard self.mode is InHuffman else { continue }
#| guard self.out_pos < self.out_end else { return NeedMoreOutput }
#| }
#| let mark = self.mark_cursor()
#| let eob = self.decode_unit() catch {
#| err => return self.retry_or_raise(mark, err)
#| }
#| if eob {
#| self.mode = if self.final_block { Done } else { AtBlock }
#| }
#| }
#| }
#| }
#|}
#|pub fn IncrementalDecoder::step(
#| self : IncrementalDecoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#|) -> StepResult raise InflateError {
#| if self.failure_kind is Some(kind) {
#| raise InflateError(kind, self.failure_message)
#| }
#| self.view = input
#| self.in_pos = 0
#| self.out_pos = 0
#| self.out = output
#| self.out_end = output.length()
#| defer self.release_step_views()
#| let status = self.run_step() catch {
#| InflateError(kind, message) => {
#| self.failure_kind = Some(kind)
#| self.failure_message = message
#| self.staged_input.clear()
#| self.staged_pos = 0
#| raise InflateError(kind, message)
#| }
#| }
#| let decoded_input = self.in_pos
#| let produced = self.out_pos
#| let consumed = if status is NeedMoreInput {
#| self.stage_incomplete_input()
#| input.length()
#| } else {
#| self.compact_staged_input()
#| decoded_input
#| }
#| { status, consumed, produced, }
#|}
),
"inflate_all.mbt": (
#|priv struct DecodeOutput {
#| mut storage : FixedArray[Byte]
#| mut start : Int32
#| mut capacity : Int32
#| mut length : Int32
#| limit : Int32
#|}
#|fn DecodeOutput::DecodeOutput(
#| size_hint : Int32,
#| limit~ : Int32 = MAX_DECODE_OUTPUT_SIZE,
#|) -> DecodeOutput {
#| {
#| storage: FixedArray::make(size_hint.max(1).min(limit), b'\x00'),
#| start: 0,
#| capacity: size_hint.max(1).min(limit),
#| length: 0,
#| limit,
#| }
#|}
#|fn DecodeOutput::into(output : MutArrayView[Byte]) -> DecodeOutput {
#| {
#| storage: output_storage(output),
#| start: output.start_offset(),
#| capacity: output.length(),
#| length: 0,
#| limit: output.length(),
#| }
#|}
#|const MAX_DECODE_OUTPUT_SIZE : Int32 = 0x7FFFFFFF
#|fn checked_decode_output_length(
#| length : Int32,
#| additional : Int32,
#| limit~ : Int32 = MAX_DECODE_OUTPUT_SIZE,
#|) -> Int32 raise InflateError {
#| guard additional <= limit - length else {
#| raise InflateError(
#| OutputLimitExceeded(
#| limit=limit.to_uint64(),
#| observed=length.to_uint64() + additional.to_uint64(),
#| ),
#| "decoded output exceeds its byte limit",
#| )
#| }
#| length + additional
#|}
#|fn grown_decode_output_capacity(current : Int32, required : Int32) -> Int32 {
#| if current <= MAX_DECODE_OUTPUT_SIZE / 2 {
#| (current * 2).max(required)
#| } else {
#| let step = (current / 2).min(MAX_DECODE_OUTPUT_SIZE - current)
#| (current + step).max(required)
#| }
#|}
#|fn DecodeOutput::ensure_capacity(
#| self : DecodeOutput,
#| additional : Int32,
#|) -> Unit raise InflateError {
#| let required = checked_decode_output_length(
#| self.length,
#| additional,
#| limit=self.limit,
#| )
#| guard required > self.capacity else { return }
#| let capacity = grown_decode_output_capacity(self.capacity, required).min(
#| self.limit,
#| )
#| self.storage = FixedArray::make_and_blit(
#| self.storage,
#| allocate_len=capacity,
#| init=b'\x00',
#| len=self.length,
#| )
#| self.capacity = self.storage.length()
#|}
#|fn DecodeOutput::append_byte(
#| self : DecodeOutput,
#| byte : Byte,
#|) -> Unit raise InflateError {
#| self.ensure_capacity(1)
#| self.storage[self.start + self.length] = byte
#| self.length = self.length + 1
#|}
#|fn DecodeOutput::append_bytes(
#| self : DecodeOutput,
#| source : BytesView,
#| source_offset : Int32,
#| length : Int32,
#|) -> Unit raise InflateError {
#| guard length > 0 else { return }
#| self.ensure_capacity(length)
#| self.storage.blit_from_bytes(
#| self.start + self.length,
#| source.data(),
#| source.start_offset() + source_offset,
#| length,
#| )
#| self.length = self.length + length
#|}
#|fn DecodeOutput::append_repeated_suffix(
#| self : DecodeOutput,
#| distance : Int32,
#| length : Int32,
#|) -> Unit raise InflateError {
#| guard length > 0 else { return }
#| self.ensure_capacity(length)
#| let start = self.start + self.length
#| let storage = self.storage
#| if length <= 8 {
#| for i in 0.. Unit {
#| if distance >= 16 && length >= 16 {
#| for offset = 0; offset + 16 <= length; offset = offset + 16 {
#| @simd.v128_store(
#| storage,
#| start + offset,
#| @simd.v128_load(storage, start + offset - distance),
#| )
#| }
#| if (length & 15) != 0 {
#| let offset = length - 16
#| @simd.v128_store(
#| storage,
#| start + offset,
#| @simd.v128_load(storage, start + offset - distance),
#| )
#| }
#| } else {
#| copy_match_bytes_scalar(storage, start, distance, length)
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn copy_match_bytes(
#| storage : FixedArray[Byte],
#| start : Int32,
#| distance : Int32,
#| length : Int32,
#|) -> Unit {
#| copy_match_bytes_scalar(storage, start, distance, length)
#|}
#|fn copy_match_bytes_scalar(
#| storage : FixedArray[Byte],
#| start : Int32,
#| distance : Int32,
#| length : Int32,
#|) -> Unit {
#| let first = distance.min(length)
#| FixedArray::unsafe_blit(storage, start, storage, start - distance, first)
#| let mut copied = first
#| while copied < length {
#| let chunk = copied.min(length - copied)
#| FixedArray::unsafe_blit(storage, start + copied, storage, start, chunk)
#| copied = copied + chunk
#| }
#|}
#|#cfg(target="native")
#|#inline
#|fn reserves_token(capacity : Int32, length : Int32) -> Bool {
#| capacity - length < 258 && capacity <= MAX_DECODE_OUTPUT_SIZE / 2
#|}
#|#cfg(target="native")
#|fn DecodeOutput::reserve_token(self : DecodeOutput) -> Unit {
#| guard self.capacity < self.limit && reserves_token(self.capacity, self.length) else {
#| return
#| }
#| self.storage = FixedArray::make_and_blit(
#| self.storage,
#| allocate_len=grown_decode_output_capacity(self.capacity, self.length + 258).min(
#| self.limit,
#| ),
#| init=b'\x00',
#| len=self.length,
#| )
#| self.capacity = self.storage.length()
#|}
#|fn DecodeOutput::to_bytes(self : DecodeOutput) -> Bytes {
#| Bytes::from_array(self.storage[self.start:self.start + self.length])
#|}
#|fn DecodeOutput::into_owned_bytes(self : DecodeOutput) -> Bytes {
#| if self.start == 0 && self.length == self.storage.length() {
#| owned_decode_buffer_to_bytes(self.storage)
#| } else {
#| self.to_bytes()
#| }
#|}
#|priv struct MemDecoder {
#| mut input : BytesView
#| mut pos : Int32 // next input byte
#| mut bitbuf : UInt32 // LSB-first bit accumulator
#| mut bit_count : Int32 // valid bits in `bitbuf`
#| mut out : DecodeOutput
#| dictionary : Bytes
#| mut cancelled : () -> Bool // polled every `CANCEL_GRANULARITY` produced bytes
#| mut poll_cancel : Bool
#| mut next_cancel : Int32 // poll `cancelled` once `out` reaches this length
#| dyn_litlen : HuffmanDecoder // literal/length (also reused for the code-length tree)
#| dyn_dist : HuffmanDecoder // distance
#| clbits : FixedArray[Int32] // decoded literal+distance code lengths
#| codebits : FixedArray[Int32] // code-length code lengths
#|}
#|const CANCEL_GRANULARITY : Int32 = 4096
#|fn no_cancellation() -> Bool {
#| false
#|}
#|const HUFFMAN_FAST_BITS : Int32 = 15
#|const HUFFMAN_FAST_REFILL_BITS : Int32 = 24
#|const INFLATE_SCRATCH_SIZE : Int32 = 8192
#|fn MemDecoder::MemDecoder(
#| input : BytesView,
#| cancelled : () -> Bool,
#| dictionary? : BytesView,
#|) -> MemDecoder {
#| {
#| input,
#| pos: 0,
#| bitbuf: 0,
#| bit_count: 0,
#| out: DecodeOutput(input.length()),
#| dictionary: dictionary.map_or(b"", dict => {
#| dict[(dict.length() - WINDOW_SIZE).max(0):].to_owned()
#| }),
#| cancelled,
#| poll_cancel: true,
#| next_cancel: 0,
#| dyn_litlen: HuffmanDecoder(),
#| dyn_dist: HuffmanDecoder(),
#| clbits: FixedArray::make(MAX_NUM_LIT + MAX_NUM_DIST, 0),
#| codebits: FixedArray::make(NUM_CODES, 0),
#| }
#|}
#|fn MemDecoder::reset_into(
#| self : MemDecoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| cancelled : (() -> Bool)?,
#|) -> Unit {
#| self.input = input
#| self.pos = 0
#| self.bitbuf = 0
#| self.bit_count = 0
#| self.out = DecodeOutput::into(output)
#| self.set_cancellation(cancelled)
#|}
#|fn MemDecoder::clear_call_references(self : MemDecoder) -> Unit {
#| self.input = b""
#| self.out.storage = []
#| self.out.start = 0
#| self.out.capacity = 0
#| self.out.length = 0
#| self.cancelled = no_cancellation
#|}
#|fn MemDecoder::set_cancellation(
#| self : MemDecoder,
#| cancelled : (() -> Bool)?,
#|) -> Unit {
#| self.cancelled = cancelled.unwrap_or(() => false)
#| self.poll_cancel = cancelled is Some(_)
#| self.next_cancel = if self.poll_cancel { 0 } else { 0x7FFF_FFFF }
#|}
#|fn MemDecoder::check_cancel(self : MemDecoder) -> Unit raise InflateError {
#| guard self.poll_cancel && self.out.length >= self.next_cancel else { return }
#| if (self.cancelled)() {
#| raise InflateError(Cancelled, "cancelled by caller")
#| }
#| self.next_cancel = self.out.length + CANCEL_GRANULARITY
#|}
#|#inline
#|fn MemDecoder::push(self : MemDecoder, b : Byte) -> Unit raise InflateError {
#| let out = self.out
#| let start = out.length
#| if start < out.capacity && start + 1 < self.next_cancel {
#| out.storage[out.start + start] = b
#| out.length = start + 1
#| } else {
#| out.append_byte(b)
#| self.check_cancel()
#| }
#|}
#|fn MemDecoder::pull_byte(self : MemDecoder) -> Bool {
#| if self.pos >= self.input.length() {
#| return false
#| }
#| self.bitbuf = self.bitbuf | (self.input[self.pos].to_uint() << self.bit_count)
#| self.bit_count = self.bit_count + 8
#| self.pos = self.pos + 1
#| true
#|}
#|fn MemDecoder::need(self : MemDecoder, n : Int32) -> Unit raise InflateError {
#| while self.bit_count < n {
#| if !self.pull_byte() {
#| raise InflateError(Truncated, "unexpected end of input")
#| }
#| }
#|}
#|fn MemDecoder::read_bits(
#| self : MemDecoder,
#| n : Int32,
#|) -> Int32 raise InflateError {
#| self.need(n)
#| let v = (self.bitbuf & ((1U << n) - 1)).reinterpret_as_int()
#| self.bitbuf = self.bitbuf >> n
#| self.bit_count = self.bit_count - n
#| v
#|}
#|fn MemDecoder::huff_sym(
#| self : MemDecoder,
#| h : HuffmanDecoder,
#|) -> Int32 raise InflateError {
#| let mut n = h.min
#| for ;; {
#| while self.bit_count < n {
#| if !self.pull_byte() {
#| raise InflateError(Truncated, "unexpected end of input")
#| }
#| }
#| let mut chunk = h.chunks[(self.bitbuf & 0x1FF).reinterpret_as_int()]
#| n = (chunk & 0xF).reinterpret_as_int()
#| if n > HUFFMAN_CHUNK_BITS {
#| let link = ((self.bitbuf >> HUFFMAN_CHUNK_BITS) & h.link_mask).reinterpret_as_int()
#| chunk = h.links[(chunk >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int() + link]
#| n = (chunk & 0xF).reinterpret_as_int()
#| }
#| if n <= self.bit_count {
#| if n == 0 {
#| raise InflateError(Corrupt, "corrupt: bad Huffman code")
#| }
#| self.bitbuf = self.bitbuf >> n
#| self.bit_count = self.bit_count - n
#| return ((chunk >> HUFFMAN_VALUE_SHIFT) & 0x1FF).reinterpret_as_int()
#| }
#| }
#|}
#|#inline
#|fn MemDecoder::refill_huffman_fast(self : MemDecoder) -> Unit {
#| while self.bit_count < HUFFMAN_FAST_REFILL_BITS &&
#| self.pos < self.input.length() {
#| if self.bit_count <= HUFFMAN_FAST_BITS && self.pos + 1 < self.input.length() {
#| let pair = self.input[self.pos].to_uint() |
#| (self.input[self.pos + 1].to_uint() << 8)
#| self.bitbuf = self.bitbuf | (pair << self.bit_count)
#| self.bit_count = self.bit_count + 16
#| self.pos = self.pos + 2
#| } else {
#| self.bitbuf = self.bitbuf |
#| (self.input[self.pos].to_uint() << self.bit_count)
#| self.bit_count = self.bit_count + 8
#| self.pos = self.pos + 1
#| }
#| }
#|}
#|#inline
#|fn MemDecoder::huff_sym_data(
#| self : MemDecoder,
#| h : HuffmanDecoder,
#|) -> Int32 raise InflateError {
#| if self.bit_count < HUFFMAN_FAST_BITS {
#| self.refill_huffman_fast()
#| }
#| if self.bit_count < HUFFMAN_FAST_BITS {
#| return self.huff_sym(h)
#| }
#| let mut chunk = h.chunks[(self.bitbuf & 0x1FF).reinterpret_as_int()]
#| let mut n = (chunk & 0xF).reinterpret_as_int()
#| if n > HUFFMAN_CHUNK_BITS {
#| let link = ((self.bitbuf >> HUFFMAN_CHUNK_BITS) & h.link_mask).reinterpret_as_int()
#| chunk = h.links[(chunk >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int() + link]
#| n = (chunk & 0xF).reinterpret_as_int()
#| }
#| guard n != 0 else { raise InflateError(Corrupt, "corrupt: bad Huffman code") }
#| self.bitbuf = self.bitbuf >> n
#| self.bit_count = self.bit_count - n
#| ((chunk >> HUFFMAN_VALUE_SHIFT) & 0x1FF).reinterpret_as_int()
#|}
#|#inline
#|fn MemDecoder::decode_match(
#| self : MemDecoder,
#| code : Int32,
#| hd : HuffmanDecoder?,
#|) -> Unit raise InflateError {
#| guard code < 286 else {
#| raise InflateError(Corrupt, "corrupt: invalid literal/length code")
#| }
#| let length_info = length_code_info_table[code - 257]
#| let length = (length_info >> 3) + self.read_bits(length_info & 0x7)
#| let dsym = if hd is Some(h) {
#| self.huff_sym_data(h)
#| } else {
#| fixed_distance_symbol(self.read_bits(5))
#| }
#| guard dsym.reinterpret_as_uint() < MAX_NUM_DIST.reinterpret_as_uint() else {
#| raise InflateError(Corrupt, "corrupt: invalid distance code")
#| }
#| let distance_info = distance_code_info_table[dsym]
#| let dist = (distance_info >> 4) + self.read_bits(distance_info & 0xF)
#| let available_history = self.out.length
#| guard available_history >= WINDOW_SIZE ||
#| back_reference_distance_is_valid(dist, available_history) ||
#| (
#| dist > available_history &&
#| dist - available_history <= self.dictionary.length()
#| ) else {
#| raise InflateError(Corrupt, "corrupt: distance too far back")
#| }
#| let out = self.out
#| let start = out.length
#| if dist <= start &&
#| length <= out.capacity - start &&
#| length < self.next_cancel - start {
#| let storage = out.storage
#| let at = out.start + start
#| if length <= 16 {
#| storage[at] = storage[at - dist]
#| storage[at + 1] = storage[at + 1 - dist]
#| storage[at + 2] = storage[at + 2 - dist]
#| for i in 3.. UInt32 {
#| let chunk = h.chunks.unsafe_get((bits & 0x1FFUL).to_int())
#| if (chunk & 15) > HUFFMAN_CHUNK_BITS.reinterpret_as_uint() {
#| let link = ((bits >> HUFFMAN_CHUNK_BITS).to_uint() & h.link_mask).reinterpret_as_int()
#| h.links[(chunk >> HUFFMAN_VALUE_SHIFT).reinterpret_as_int() + link]
#| } else {
#| chunk
#| }
#|}
#|#cfg(target="native")
#|let short_copy_shuffle : FixedArray[Byte] = FixedArray::makei(1024, index => {
#| let lane = index & 15
#| let length = index >> 7
#| let distance = ((index >> 4) & 7).max(1)
#| if lane < length {
#| (lane % distance).to_byte()
#| } else {
#| (8 + (lane & 7)).to_byte()
#| }
#|})
#|#cfg(target="native")
#|fn MemDecoder::decode_batch(
#| self : MemDecoder,
#| hl : HuffmanDecoder,
#| hd : HuffmanDecoder?,
#|) -> Bool {
#| guard hd is Some(distance_tree) else { return false }
#| let input = self.input
#| let out = self.out
#| guard input.length() - self.pos >= 8 && self.next_cancel > 0 else {
#| return false
#| }
#| out.reserve_token()
#| let storage = out.storage
#| let output_end = out.start + out.capacity.min(self.next_cancel - 1)
#| guard output_end - out.start - out.length >= 258 else { return false }
#| let mut bits = self.bitbuf.to_uint64()
#| let mut count = self.bit_count
#| let mut pos = self.pos
#| let mut written = out.start + out.length
#| let mut next_primary = 0U
#| let ended = for ;; {
#| let input_length = input.length()
#| let output_length = out.start + out.capacity
#| let can_decode_token = input_length >= 8 &&
#| pos >= 0 &&
#| pos <= input_length - 8 &&
#| output_length >= 258 &&
#| written >= 0 &&
#| written <= output_length - 258 &&
#| output_end - written >= 258
#| guard can_decode_token else { break false }
#| let mut word = bits
#| let mut remaining = count
#| let mut cursor = pos
#| if remaining < 48 {
#| let loaded = input.unsafe_read_uint64_le(cursor)
#| let bytes = (63 - remaining) >> 3
#| word = word | (loaded << remaining)
#| remaining += bytes << 3
#| cursor += bytes
#| }
#| let mut entry = if next_primary != 0U {
#| next_primary
#| } else {
#| hl.chunks.unsafe_get((word & 511UL).to_int())
#| }
#| if (entry & 15U) > 9U {
#| entry = batch_huffman_entry(hl, word)
#| }
#| let mut used = (entry & 15).reinterpret_as_int()
#| guard used != 0 else { break false }
#| let mut symbol = ((entry >> HUFFMAN_VALUE_SHIFT) & 0x1FF).reinterpret_as_int()
#| if symbol < 256 {
#| word = word >> used
#| remaining -= used
#| next_primary = hl.chunks.unsafe_get((word & 511UL).to_int())
#| storage[written] = symbol.to_byte()
#| written += 1
#| bits = word
#| count = remaining
#| pos = cursor
#| guard remaining >= 48 && output_end - written >= 258 else { continue }
#| entry = next_primary
#| if (entry & 15U) > 9U {
#| entry = batch_huffman_entry(hl, word)
#| }
#| used = (entry & 15).reinterpret_as_int()
#| guard used != 0 else { break false }
#| symbol = ((entry >> HUFFMAN_VALUE_SHIFT) & 0x1FF).reinterpret_as_int()
#| }
#| if symbol < 256 {
#| word = word >> used
#| remaining -= used
#| next_primary = hl.chunks.unsafe_get((word & 511UL).to_int())
#| storage[written] = symbol.to_byte()
#| written += 1
#| } else if symbol == 256 {
#| bits = word >> used
#| count = remaining - used
#| pos = cursor
#| break true
#| } else {
#| guard symbol < 286 else { break false }
#| let info = ((entry >> 13) & 0xFFF).reinterpret_as_int()
#| let extra = info & 7
#| let length = (info >> 3) +
#| ((word >> used) & ((1UL << extra) - 1)).to_int()
#| let total = (entry >> 25).reinterpret_as_int()
#| word = word >> total
#| remaining -= total
#| let distance_entry = batch_huffman_entry(distance_tree, word)
#| let distance_used = (distance_entry & 15).reinterpret_as_int()
#| let distance_info = (distance_entry >> 13).reinterpret_as_int()
#| guard distance_used != 0 && distance_info != 0 else { break false }
#| word = word >> distance_used
#| remaining -= distance_used
#| let distance_extra = distance_info & 15
#| let distance = (distance_info >> 4) +
#| (word & ((1UL << distance_extra) - 1)).to_int()
#| guard distance > 0 && distance <= written - out.start else { break false }
#| word = word >> distance_extra
#| remaining -= distance_extra
#| next_primary = if remaining >= 9 {
#| hl.chunks.unsafe_get((word & 511UL).to_int())
#| } else {
#| 0U
#| }
#| if length <= 7 {
#| let history = @simd.v128_load64_zero(storage, written - distance)
#| let combined = @simd.v128_load64_lane(storage, written, history, 1)
#| let control = @simd.v128_load(
#| short_copy_shuffle,
#| (length * 8 + distance.min(7)) * 16,
#| )
#| @simd.v128_store64_lane(
#| storage,
#| written,
#| @simd.i8x16_swizzle(combined, control),
#| 0,
#| )
#| } else if length <= 16 && distance >= 8 {
#| @simd.v128_store64_lane(
#| storage,
#| written,
#| @simd.v128_load64_zero(storage, written - distance),
#| 0,
#| )
#| if length > 8 {
#| let tail = written + length - 8
#| @simd.v128_store64_lane(
#| storage,
#| tail,
#| @simd.v128_load64_zero(storage, tail - distance),
#| 0,
#| )
#| }
#| } else if length <= 16 {
#| storage[written] = storage[written - distance]
#| storage[written + 1] = storage[written + 1 - distance]
#| storage[written + 2] = storage[written + 2 - distance]
#| if length > 3 {
#| storage[written + 3] = storage[written + 3 - distance]
#| }
#| if length > 4 {
#| storage[written + 4] = storage[written + 4 - distance]
#| }
#| if length > 5 {
#| storage[written + 5] = storage[written + 5 - distance]
#| }
#| if length > 6 {
#| storage[written + 6] = storage[written + 6 - distance]
#| }
#| if length > 7 {
#| storage[written + 7] = storage[written + 7 - distance]
#| }
#| if length > 8 {
#| storage[written + 8] = storage[written + 8 - distance]
#| }
#| if length > 9 {
#| storage[written + 9] = storage[written + 9 - distance]
#| }
#| if length > 10 {
#| storage[written + 10] = storage[written + 10 - distance]
#| }
#| if length > 11 {
#| storage[written + 11] = storage[written + 11 - distance]
#| }
#| if length > 12 {
#| storage[written + 12] = storage[written + 12 - distance]
#| }
#| if length > 13 {
#| storage[written + 13] = storage[written + 13 - distance]
#| }
#| if length > 14 {
#| storage[written + 14] = storage[written + 14 - distance]
#| }
#| if length > 15 {
#| storage[written + 15] = storage[written + 15 - distance]
#| }
#| } else if length <= 48 && distance >= 16 {
#| @simd.v128_store(
#| storage,
#| written,
#| @simd.v128_load(storage, written - distance),
#| )
#| if length > 32 {
#| @simd.v128_store(
#| storage,
#| written + 16,
#| @simd.v128_load(storage, written + 16 - distance),
#| )
#| }
#| let tail = written + length - 16
#| @simd.v128_store(
#| storage,
#| tail,
#| @simd.v128_load(storage, tail - distance),
#| )
#| } else if distance == 1 {
#| storage.fill(storage[written - 1], start=written, end=written + length)
#| } else {
#| copy_match_bytes(storage, written, distance, length)
#| }
#| written += length
#| }
#| bits = word
#| count = remaining
#| pos = cursor
#| }
#| self.pos = pos - (count >> 3)
#| self.bit_count = count & 7
#| self.bitbuf = bits.to_uint() & ((1U << self.bit_count) - 1)
#| out.length = written - out.start
#| ended
#|}
#|#cfg(not(target="native"))
#|#inline
#|fn MemDecoder::decode_batch(
#| _self : MemDecoder,
#| _hl : HuffmanDecoder,
#| _hd : HuffmanDecoder?,
#|) -> Bool {
#| false
#|}
#|fn MemDecoder::decode_block(
#| self : MemDecoder,
#| hl : HuffmanDecoder,
#| hd : HuffmanDecoder?,
#|) -> Unit raise InflateError {
#| for ;; {
#| if self.decode_batch(hl, hd) {
#| return
#| }
#| let v = self.huff_sym_data(hl)
#| guard v >= 256 else {
#| self.push(v.to_byte())
#| let next = self.huff_sym_data(hl)
#| guard next >= 256 else {
#| self.push(next.to_byte())
#| continue
#| }
#| guard next != 256 else { return }
#| self.decode_match(next, hd)
#| continue
#| }
#| guard v != 256 else { return }
#| self.decode_match(v, hd)
#| }
#|}
#|#cfg(target="native")
#|let fixed_data_decoders : (HuffmanDecoder, HuffmanDecoder) = {
#| let litlen = HuffmanDecoder()
#| let distance = HuffmanDecoder()
#| ignore(litlen.initialize_litlen(fixed_ll_len))
#| ignore(distance.initialize_distance(FixedArray::make(32, 5)))
#| (litlen, distance)
#|}
#|#cfg(target="native")
#|fn MemDecoder::decode_fixed_block(self : MemDecoder) -> Unit raise InflateError {
#| let (litlen, distance) = fixed_data_decoders
#| self.decode_block(litlen, Some(distance))
#|}
#|#cfg(not(target="native"))
#|fn MemDecoder::decode_fixed_block(self : MemDecoder) -> Unit raise InflateError {
#| for ;; {
#| let symbol = self.huff_sym_data(fixed_huffman_decoder)
#| guard symbol >= 256 else {
#| self.push(symbol.to_byte())
#| let next = self.huff_sym_data(fixed_huffman_decoder)
#| guard next >= 256 else {
#| self.push(next.to_byte())
#| continue
#| }
#| guard next != 256 else { return }
#| self.decode_match(next, None)
#| continue
#| }
#| guard symbol != 256 else { return }
#| self.decode_match(symbol, None)
#| }
#|}
#|fn MemDecoder::copy_match(
#| self : MemDecoder,
#| distance : Int32,
#| length : Int32,
#|) -> Unit raise InflateError {
#| let mut remaining = length
#| while remaining > 0 {
#| let until_cancel = if self.poll_cancel {
#| self.next_cancel - self.out.length
#| } else {
#| remaining
#| }
#| let chunk = remaining.min(until_cancel)
#| let copied = if distance > self.out.length {
#| let history = distance - self.out.length
#| let copied = chunk.min(history)
#| self.out.append_bytes(
#| self.dictionary,
#| self.dictionary.length() - history,
#| copied,
#| )
#| copied
#| } else {
#| self.out.append_repeated_suffix(distance, chunk)
#| chunk
#| }
#| remaining -= copied
#| self.check_cancel()
#| }
#|}
#|fn MemDecoder::copy_stored(
#| self : MemDecoder,
#| length : Int32,
#|) -> Unit raise InflateError {
#| let mut remaining = length
#| while remaining > 0 {
#| let until_cancel = if self.poll_cancel {
#| self.next_cancel - self.out.length
#| } else {
#| remaining
#| }
#| let chunk = remaining.min(until_cancel)
#| self.out.append_bytes(self.input, self.pos, chunk)
#| self.pos = self.pos + chunk
#| remaining = remaining - chunk
#| self.check_cancel()
#| }
#|}
#|fn MemDecoder::stored_block(self : MemDecoder) -> Unit raise InflateError {
#| self.pos = self.pos - (self.bit_count >> 3)
#| self.bitbuf = 0
#| self.bit_count = 0
#| if !has_bytes_from(self.pos, 4, self.input.length()) {
#| raise InflateError(Truncated, "unexpected end of input")
#| }
#| let len = self.input[self.pos].to_int() |
#| (self.input[self.pos + 1].to_int() << 8)
#| let nlen = self.input[self.pos + 2].to_int() |
#| (self.input[self.pos + 3].to_int() << 8)
#| self.pos = self.pos + 4
#| validate_stored_length(len, nlen)
#| if !has_bytes_from(self.pos, len, self.input.length()) {
#| raise InflateError(Truncated, "unexpected end of input")
#| }
#| self.copy_stored(len)
#|}
#|fn MemDecoder::read_dynamic(self : MemDecoder) -> Unit raise InflateError {
#| let (nlit, ndist) = decode_dynamic_header(
#| n => self.read_bits(n),
#| () => self.huff_sym(self.dyn_litlen),
#| self.dyn_litlen,
#| self.codebits,
#| self.clbits,
#| )
#| guard self.dyn_litlen.initialize_litlen(self.clbits[0:nlit]) else {
#| raise InflateError(Corrupt, "corrupt: bad literal/length tree")
#| }
#| guard self.dyn_dist.initialize_distance(self.clbits[nlit:nlit + ndist]) else {
#| raise InflateError(Corrupt, "corrupt: bad distance tree")
#| }
#|}
#|fn MemDecoder::run(self : MemDecoder) -> Unit raise InflateError {
#| self.check_cancel()
#| for ;; {
#| self.need(3)
#| let bfinal = (self.bitbuf & 1) == 1
#| let btype = ((self.bitbuf >> 1) & 3).reinterpret_as_int()
#| self.bitbuf = self.bitbuf >> 3
#| self.bit_count = self.bit_count - 3
#| match btype {
#| 0 => self.stored_block()
#| 1 => self.decode_fixed_block()
#| 2 => {
#| self.read_dynamic()
#| self.decode_block(self.dyn_litlen, Some(self.dyn_dist))
#| }
#| _ => raise InflateError(Corrupt, "corrupt: reserved block type")
#| }
#| if bfinal {
#| break
#| }
#| }
#|}
#|fn MemDecoder::consumed(self : MemDecoder) -> Int32 {
#| self.pos - (self.bit_count >> 3)
#|}
#|struct Decompressor {
#| decoder : MemDecoder
#|}
#|pub fn Decompressor::Decompressor(dictionary? : BytesView) -> Decompressor {
#| { decoder: MemDecoder(b"", no_cancellation, dictionary?), }
#|}
#|fn check_input_mode(
#| mode : InputMode,
#| consumed : Int32,
#| input_length : Int32,
#|) -> Unit raise InflateError {
#| if mode is Exact && consumed != input_length {
#| raise InflateError(TrailingData, "trailing data after DEFLATE stream")
#| }
#|}
#|pub fn Decompressor::decompress_into(
#| self : Decompressor,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| mode~ : InputMode = Exact,
#| cancelled? : () -> Bool,
#|) -> DecompressResult raise InflateError {
#| self.decoder.reset_into(input, output, cancelled)
#| defer self.decoder.clear_call_references()
#| self.decoder.run()
#| let consumed = self.decoder.consumed()
#| check_input_mode(mode, consumed, input.length())
#| { consumed, produced: self.decoder.out.length, }
#|}
#|const PREFIX_OUTPUT_CAPACITY : Int32 = 16384
#|#label_migration(max_output_bytes, alias=max_output, msg="Use max_output_bytes= instead")
#|pub fn Decompressor::decompress(
#| self : Decompressor,
#| input : BytesView,
#| mode~ : InputMode = Exact,
#| max_output_bytes? : Int32,
#| cancelled? : () -> Bool,
#|) -> Decompressed raise InflateError {
#| let limit = max_output_bytes.unwrap_or(MAX_DECODE_OUTPUT_SIZE)
#| guard limit >= 0 else {
#| raise InflateError(InvalidArgument, "max_output_bytes must be non-negative")
#| }
#| let decoder = self.decoder
#| decoder.reset_into(input, [].mut_view(), cancelled)
#| decoder.out = DecodeOutput(input.length().min(PREFIX_OUTPUT_CAPACITY), limit~)
#| defer decoder.clear_call_references()
#| decoder.run()
#| let consumed = decoder.consumed()
#| check_input_mode(mode, consumed, input.length())
#| { data: decoder.out.into_owned_bytes(), consumed, }
#|}
#|#label_migration(max_output_bytes, alias=max_output, msg="Use max_output_bytes= instead")
#|pub fn inflate_exact(
#| input : BytesView,
#| dictionary? : BytesView,
#| max_output_bytes? : Int32,
#| cancelled? : () -> Bool,
#| two_pass~ : Bool = false,
#|) -> Bytes raise InflateError {
#| inflate_allocated(
#| input,
#| Exact,
#| dictionary,
#| max_output_bytes,
#| cancelled,
#| two_pass,
#| ).data
#|}
#|#label_migration(max_output_bytes, alias=max_output, msg="Use max_output_bytes= instead")
#|pub fn inflate_prefix(
#| input : BytesView,
#| dictionary? : BytesView,
#| max_output_bytes? : Int32,
#| cancelled? : () -> Bool,
#| two_pass~ : Bool = false,
#|) -> Decompressed raise InflateError {
#| inflate_allocated(
#| input,
#| Prefix,
#| dictionary,
#| max_output_bytes,
#| cancelled,
#| two_pass,
#| )
#|}
#|fn inflate_allocated(
#| input : BytesView,
#| mode : InputMode,
#| dictionary : BytesView?,
#| max_output_bytes : Int32?,
#| cancelled : (() -> Bool)?,
#| two_pass : Bool,
#|) -> Decompressed raise InflateError {
#| if max_output_bytes is Some(limit) {
#| guard limit >= 0 else {
#| raise InflateError(
#| InvalidArgument,
#| "max_output_bytes must be non-negative",
#| )
#| }
#| }
#| if two_pass {
#| inflate_preallocated(
#| input,
#| max_output_bytes,
#| cancelled.unwrap_or(no_cancellation),
#| mode,
#| dictionary,
#| )
#| } else {
#| Decompressor(dictionary?).decompress(
#| input,
#| mode~,
#| max_output_bytes?,
#| cancelled?,
#| )
#| }
#|}
#|fn inflate_drive(
#| inflater : IncrementalDecoder,
#| input : BytesView,
#| max_output_bytes : Int32?,
#| mode : InputMode,
#| cancelled : () -> Bool,
#| emit : (FixedArray[Byte], Int32) -> Unit,
#|) -> DecompressResult raise InflateError {
#| let scratch = FixedArray::make(INFLATE_SCRATCH_SIZE, b'\x00')
#| let mut total = 0
#| let mut input_pos = 0
#| for ;; {
#| let status_result = inflater.step(input[input_pos:], scratch.mut_view())
#| let status = status_result.status
#| guard !(status is NeedMoreInput) else {
#| raise InflateError(Truncated, "unexpected end of input")
#| }
#| let consumed = status_result.consumed
#| let produced = status_result.produced
#| input_pos = input_pos + consumed
#| guard !cancelled() else {
#| raise InflateError(Cancelled, "cancelled by caller")
#| }
#| total = checked_decode_output_length(
#| total,
#| produced,
#| limit=max_output_bytes.unwrap_or(MAX_DECODE_OUTPUT_SIZE),
#| )
#| emit(scratch, produced)
#| guard status is Done else { continue }
#| check_input_mode(mode, input_pos, input.length())
#| return { consumed: input_pos, produced: total, }
#| }
#|}
#|fn validate_preallocation_size(
#| size : Int32,
#| filled : Int32,
#|) -> Unit raise InflateError {
#| guard filled == size else {
#| raise InflateError(Corrupt, "corrupt: preallocation size mismatch")
#| }
#|}
#|fn inflate_preallocated(
#| input : BytesView,
#| max_output_bytes : Int32?,
#| cancelled : () -> Bool,
#| mode : InputMode,
#| dictionary : BytesView?,
#|) -> Decompressed raise InflateError {
#| guard !cancelled() else {
#| raise InflateError(Cancelled, "cancelled by caller")
#| }
#| let inflater = IncrementalDecoder(dictionary?)
#| let counted = inflate_drive(
#| inflater,
#| input,
#| max_output_bytes,
#| mode,
#| cancelled,
#| (_scratch, _produced) => (),
#| )
#| inflater.reset(dictionary?)
#| let buf = FixedArray::make(counted.produced, b'\x00')
#| let mut filled = 0
#| let replayed = inflate_drive(
#| inflater,
#| input[:counted.consumed],
#| Some(counted.produced),
#| Exact,
#| cancelled,
#| (scratch, produced) => {
#| scratch.blit_to(buf, len=produced, dst_offset=filled)
#| filled += produced
#| },
#| )
#| validate_preallocation_size(counted.produced, replayed.produced)
#| { data: owned_decode_buffer_to_bytes(buf), consumed: replayed.consumed, }
#|}
#|fn owned_decode_buffer_to_bytes(buffer : FixedArray[Byte]) -> Bytes = "%identity"
),
"lz77.mbt": (
#|const HASH_BITS : Int32 = 15
#|const WINDOW_MASK : Int32 = WINDOW_SIZE - 1
#|const STREAM_RING_SIZE : Int32 = 65536
#|const STREAM_RING_MASK : Int32 = STREAM_RING_SIZE - 1
#|const STREAM_RING_STORAGE_SIZE : Int32 = STREAM_RING_SIZE * 2
#|#derive(Eq)
#|#valtype
#|priv struct LevelConfig {
#| good_length : Int32 // shrink the chain budget once a match this long is held
#| max_lazy : Int32 // 0 = greedy (commit immediately); else defer while best < this
#| nice_length : Int32 // stop searching outright at this match length
#| max_chain : Int32 // candidate positions examined per search
#|}
#|let level_configs : FixedArray[LevelConfig] = [
#| { good_length: 0, max_lazy: 0, nice_length: 0, max_chain: 0, }, // 0: stored
#| { good_length: 4, max_lazy: 0, nice_length: 8, max_chain: 4, },
#| { good_length: 4, max_lazy: 0, nice_length: 16, max_chain: 8, },
#| { good_length: 4, max_lazy: 0, nice_length: 32, max_chain: 32, },
#| { good_length: 4, max_lazy: 4, nice_length: 16, max_chain: 16, },
#| { good_length: 8, max_lazy: 16, nice_length: 32, max_chain: 32, },
#| { good_length: 8, max_lazy: 16, nice_length: 128, max_chain: 128, }, // 6: default
#| { good_length: 8, max_lazy: 32, nice_length: 128, max_chain: 256, },
#| { good_length: 32, max_lazy: 128, nice_length: 258, max_chain: 1024, },
#| { good_length: 32, max_lazy: 258, nice_length: 258, max_chain: 4096, }, // 9
#|]
#|fn hash3(data : BytesView, i : Int32) -> Int32 {
#| let a = data[i].to_int()
#| let b = data[i + 1].to_int()
#| let c = data[i + 2].to_int()
#| (a ^ (b << 5) ^ (c << 10)) & ((1 << HASH_BITS) - 1)
#|}
#|fn hash3_window(
#| data : FixedArray[Byte],
#| i : Int32,
#| shift~ : Int32 = 9,
#|) -> Int32 {
#| let offset = i & STREAM_RING_MASK
#| let triple = (data[offset].to_int() |
#| (data[offset + 1].to_int() << 8) |
#| (data[offset + 2].to_int() << 16)).reinterpret_as_uint()
#| short_hash_product(triple * 0x1E35A7BDU, shift~)
#|}
#|#inline
#|fn short_hash_product(product : UInt32, shift~ : Int32 = 9) -> Int32 {
#| ((product & 0xFFFFFF) >> shift).reinterpret_as_int()
#|}
#|fn short_hash3(data : BytesView, position : Int32, shift~ : Int32 = 9) -> Int32 {
#| let triple = (data[position].to_int() |
#| (data[position + 1].to_int() << 8) |
#| (data[position + 2].to_int() << 16)).reinterpret_as_uint()
#| short_hash_product(triple * 0x1E35A7BDU, shift~)
#|}
#|const NARROW_LONG_HASH_BITS : Int32 = 16
#|const MID_LONG_HASH_BITS : Int32 = 17
#|const LONG_HASH_BITS : Int32 = 18
#|fn long_hash_bits_for_input(data : BytesView) -> Int32 {
#| let n = data.length()
#| guard n >= 32768 else { return NARROW_LONG_HASH_BITS }
#| let sample = n.min(1024)
#| let span = n - sample
#| let used = FixedArray::make(256, 0)
#| let local_used = FixedArray::make(256, 0)
#| let mut distinct = 0
#| let mut high_regions = 0
#| for region in 0..<4 {
#| let start = match region {
#| 0 => 0
#| 1 => span / 3
#| 2 => span / 3 * 2
#| _ => span
#| }
#| local_used.fill(0)
#| let mut local_distinct = 0
#| for i in start..<(start + sample) {
#| let byte = data[i].to_int()
#| if used[byte] == 0 {
#| used[byte] = 1
#| distinct += 1
#| }
#| if local_used[byte] == 0 {
#| local_used[byte] = 1
#| local_distinct += 1
#| }
#| }
#| if local_distinct >= 128 {
#| high_regions += 1
#| }
#| }
#| if high_regions == 4 {
#| LONG_HASH_BITS
#| } else if distinct >= 64 {
#| MID_LONG_HASH_BITS
#| } else {
#| NARROW_LONG_HASH_BITS
#| }
#|}
#|#inline
#|fn long_hash_product(product : UInt32, shift~ : Int32 = 16) -> Int32 {
#| (product >> shift).reinterpret_as_int()
#|}
#|#inline
#|fn read_match_word(data : BytesView, position : Int32) -> UInt32 {
#| data.data().unsafe_read_uint32_le(data.start_offset() + position)
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#inline
#|fn read_window_match_word(data : FixedArray[Byte], position : Int32) -> UInt32 {
#| let offset = position & STREAM_RING_MASK
#| (data[offset].to_int() |
#| (data[offset + 1].to_int() << 8) |
#| (data[offset + 2].to_int() << 16) |
#| (data[offset + 3].to_int() << 24)).reinterpret_as_uint()
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn match_window_as_bytes(data : FixedArray[Byte]) -> Bytes = "%identity"
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn read_window_match_word(data : FixedArray[Byte], position : Int32) -> UInt32 {
#| match_window_as_bytes(data).unsafe_read_uint32_le(position & STREAM_RING_MASK)
#|}
#|priv struct MatchFinder {
#| mut min_match_length : Int32
#| mut position_base : Int32
#| mut next_base : Int32
#| mut short_shift : Int32
#| mut long_shift : Int32
#| mut short_head : FixedArray[Int32]
#| mut head : FixedArray[Int32]
#| prev : FixedArray[Byte]
#|}
#|fn MatchFinder::MatchFinder(
#| input_size~ : Int32 = WINDOW_SIZE,
#| long_hash_bits~ : Int32 = NARROW_LONG_HASH_BITS,
#|) -> MatchFinder {
#| let short_size = match_table_capacity(
#| input_size.min(16384) * 2,
#| 1 << HASH_BITS,
#| )
#| let long_size = long_match_table_capacity(input_size, long_hash_bits)
#| {
#| min_match_length: 0,
#| position_base: 0,
#| next_base: 0,
#| short_shift: 24 - short_size.ctz(),
#| long_shift: 32 - long_size.ctz(),
#| short_head: FixedArray::make(short_size, -1),
#| head: FixedArray::make(long_size, -1),
#| prev: FixedArray::make(WINDOW_SIZE * 2 + 2, b'\xFF'),
#| }
#|}
#|fn match_table_capacity(requested : Int32, maximum : Int32) -> Int32 {
#| for capacity = 256 {
#| guard capacity < requested && capacity < maximum else { break capacity }
#| continue capacity * 2
#| }
#|}
#|fn long_match_table_capacity(
#| input_size : Int32,
#| long_hash_bits : Int32,
#|) -> Int32 {
#| guard input_size < 32768 else { return 1 << long_hash_bits }
#| match_table_capacity(input_size.min(16384) * 4, 1 << long_hash_bits)
#|}
#|fn MatchFinder::size_for_input(
#| self : MatchFinder,
#| input_size : Int32,
#| long_hash_bits : Int32,
#|) -> Unit {
#| let short_size = match_table_capacity(
#| input_size.min(16384) * 2,
#| 1 << HASH_BITS,
#| )
#| let long_size = long_match_table_capacity(input_size, long_hash_bits)
#| if self.short_head.length() < short_size {
#| self.short_head = FixedArray::make(short_size, -1)
#| }
#| if self.head.length() < long_size {
#| self.head = FixedArray::make(long_size, -1)
#| }
#| self.short_shift = 24 - short_size.ctz()
#| self.long_shift = 32 - long_size.ctz()
#|}
#|fn MatchFinder::reset(self : MatchFinder) -> Unit {
#| self.min_match_length = 0
#| self.position_base = 0
#| self.next_base = 0
#| self.short_head.fill(-1)
#| self.head.fill(-1)
#| self.prev.fill(b'\xFF')
#|}
#|fn MatchFinder::reset_stream(self : MatchFinder, previous_size : Int32) -> Unit {
#| self.min_match_length = 0
#| let next = self.position_base + previous_size
#| guard next < 1 << 29 else {
#| self.reset()
#| return
#| }
#| self.position_base = next
#| self.next_base = next + (1 << 30) + STREAM_RING_SIZE
#|}
#|fn MatchFinder::reset_for_input(
#| self : MatchFinder,
#| input : BytesView,
#| long_hash_bits~ : Int32 = -1,
#|) -> Unit {
#| self.min_match_length = 0
#| let bits = if long_hash_bits >= 0 {
#| long_hash_bits
#| } else {
#| long_hash_bits_for_input(input)
#| }
#| self.size_for_input(input.length(), bits)
#| if input.length() > 2147483647 - self.next_base {
#| self.short_head.fill(-1)
#| self.head.fill(-1)
#| self.next_base = 0
#| }
#| self.position_base = self.next_base
#| self.next_base += input.length()
#|}
#|fn MatchFinder::rebase(self : MatchFinder, offset : Int32) -> Unit {
#| for table in [self.short_head, self.head] {
#| for i in 0..= 0 {
#| table[i] = table[i] - offset
#| }
#| }
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn bytes_as_fixedarray(data : Bytes) -> FixedArray[Byte] = "%identity"
#|#cfg(any(target="native", target="wasm"))
#|fn match_len_up_to(
#| data : BytesView,
#| src : Int32,
#| pos : Int32,
#| limit : Int32,
#|) -> Int32 {
#| let bytes = bytes_as_fixedarray(data.data())
#| let start = data.start_offset()
#| let mut k = 0
#| while k + 16 <= limit {
#| let equal_lanes = @simd.i8x16_bitmask(
#| @simd.i8x16_eq(
#| @simd.v128_load(bytes, start + src + k),
#| @simd.v128_load(bytes, start + pos + k),
#| ),
#| )
#| if equal_lanes != 0xFFFF {
#| return k + (equal_lanes ^ 0xFFFF).ctz()
#| }
#| k = k + 16
#| }
#| while k < limit && data[src + k] == data[pos + k] {
#| k = k + 1
#| }
#| k
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn match_len_in_window(
#| data : FixedArray[Byte],
#| src : Int32,
#| pos : Int32,
#| limit : Int32,
#|) -> Int32 {
#| let src = src & STREAM_RING_MASK
#| let pos = pos & STREAM_RING_MASK
#| let mut k = 0
#| while k + 16 <= limit {
#| let equal_lanes = @simd.i8x16_bitmask(
#| @simd.i8x16_eq(
#| @simd.v128_load(data, src + k),
#| @simd.v128_load(data, pos + k),
#| ),
#| )
#| if equal_lanes != 0xFFFF {
#| return k + (equal_lanes ^ 0xFFFF).ctz()
#| }
#| k = k + 16
#| }
#| while k < limit && data[src + k] == data[pos + k] {
#| k = k + 1
#| }
#| k
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn match_len_up_to(
#| data : BytesView,
#| src : Int32,
#| pos : Int32,
#| limit : Int32,
#|) -> Int32 {
#| let mut k = 0
#| while k < limit && data[src + k] == data[pos + k] {
#| k = k + 1
#| }
#| k
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn match_len_in_window(
#| data : FixedArray[Byte],
#| src : Int32,
#| pos : Int32,
#| limit : Int32,
#|) -> Int32 {
#| let mut k = 0
#| while k < limit &&
#| data[(src + k) & STREAM_RING_MASK] == data[(pos + k) & STREAM_RING_MASK] {
#| k = k + 1
#| }
#| k
#|}
#|#inline
#|fn match_candidate_can_beat(
#| data : BytesView,
#| candidate : Int32,
#| position : Int32,
#| best_len : Int32,
#| limit : Int32,
#|) -> Bool {
#| guard best_len < limit else { return false }
#| if best_len < 3 {
#| data[candidate] == data[position] &&
#| data[candidate + 1] == data[position + 1] &&
#| data[candidate + 2] == data[position + 2]
#| } else {
#| data[candidate] == data[position] &&
#| data[candidate + 1] == data[position + 1] &&
#| data[candidate + best_len - 1] == data[position + best_len - 1] &&
#| data[candidate + best_len] == data[position + best_len]
#| }
#|}
#|#inline
#|fn has_bytes_from(position : Int32, count : Int32, end : Int32) -> Bool {
#| end - position >= count
#|}
#|fn MatchFinder::find_match_hashed(
#| finder : MatchFinder,
#| data : BytesView,
#| i : Int32,
#| n : Int32,
#| base : Int32,
#| cfg : LevelConfig,
#| prefix : UInt32,
#| product : UInt32,
#|) -> Int32 {
#| let limit = (n - i).min(258)
#| guard base < limit else { return base << 16 }
#| let mut budget = if base >= cfg.good_length {
#| cfg.max_chain >> 2
#| } else {
#| cfg.max_chain
#| }
#| guard budget > 0 else { return base << 16 }
#| let cutoff = (i - WINDOW_SIZE).max(0)
#| let mut best_len = base
#| let mut best_dist = 0
#| if base < 3 {
#| let short_hash = short_hash_product(product, shift=finder.short_shift)
#| let short = finder.short_head.unsafe_get(short_hash) - finder.position_base
#| guard short >= cutoff else { return base << 16 }
#| if (read_match_word(data, short) & 0xFFFFFF) == (prefix & 0xFFFFFF) {
#| best_len = 3
#| best_dist = i - short
#| }
#| }
#| let mut cand = finder.head.unsafe_get(
#| long_hash_product(product, shift=finder.long_shift),
#| ) -
#| finder.position_base
#| guard cand >= cutoff else { return (best_len << 16) | best_dist }
#| let prev = finder.prev
#| let mut probe = best_len.max(3)
#| let mut suffix = read_match_word(data, i + probe - 3)
#| let nice_length = cfg.nice_length.min(limit)
#| for ;; {
#| for ;; {
#| let candidate_suffix = read_match_word(data, cand + probe - 3)
#| if candidate_suffix == suffix && read_match_word(data, cand) == prefix {
#| break
#| }
#| budget -= 1
#| cand = chain_previous(prev, cand)
#| guard budget > 0 && cand >= cutoff else {
#| return (best_len << 16) | best_dist
#| }
#| }
#| let length = match_len_up_to(data, cand, i, limit)
#| if length > best_len {
#| best_len = length
#| best_dist = i - cand
#| if length >= nice_length {
#| break
#| }
#| probe = length
#| suffix = read_match_word(data, i + probe - 3)
#| }
#| budget -= 1
#| cand = chain_previous(prev, cand)
#| guard budget > 0 && cand >= cutoff else { break }
#| }
#| (best_len << 16) | best_dist
#|}
#|fn MatchFinder::find_match(
#| finder : MatchFinder,
#| data : BytesView,
#| i : Int32,
#| n : Int32,
#| base : Int32,
#| cfg : LevelConfig,
#|) -> Int32 {
#| let limit = (n - i).min(258)
#| if limit < 4 {
#| let budget = if base >= cfg.good_length {
#| cfg.max_chain >> 2
#| } else {
#| cfg.max_chain
#| }
#| guard base < limit && budget > 0 else { return base << 16 }
#| let cutoff = (i - WINDOW_SIZE).max(0)
#| let short = finder.short_head.unsafe_get(
#| short_hash3(data, i, shift=finder.short_shift),
#| ) -
#| finder.position_base
#| if base < 3 && short >= cutoff {
#| if data[short] == data[i] &&
#| data[short + 1] == data[i + 1] &&
#| data[short + 2] == data[i + 2] {
#| return (3 << 16) | (i - short)
#| }
#| }
#| return base << 16
#| }
#| let prefix = read_match_word(data, i)
#| let product = prefix * 0x1E35A7BDU
#| finder.find_match_hashed(data, i, n, base, cfg, prefix, product)
#|}
#|fn MatchFinder::find_window_match_hashed(
#| finder : MatchFinder,
#| data : FixedArray[Byte],
#| i : Int32,
#| end : Int32,
#| base : Int32,
#| cfg : LevelConfig,
#| prefix : UInt32,
#| product : UInt32,
#|) -> Int32 {
#| let limit = (end - i).min(258)
#| guard base < limit else { return base << 16 }
#| let mut budget = if base >= cfg.good_length {
#| cfg.max_chain >> 2
#| } else {
#| cfg.max_chain
#| }
#| guard budget > 0 else { return base << 16 }
#| let cutoff = (i - WINDOW_SIZE).max(0)
#| let mut best_len = base
#| let mut best_dist = 0
#| if base < 3 {
#| let short_hash = short_hash_product(product, shift=finder.short_shift)
#| let short = finder.short_head.unsafe_get(short_hash) - finder.position_base
#| guard short >= cutoff else { return base << 16 }
#| if (read_window_match_word(data, short) & 0xFFFFFF) == (prefix & 0xFFFFFF) {
#| best_len = 3
#| best_dist = i - short
#| }
#| }
#| let mut cand = finder.head.unsafe_get(
#| long_hash_product(product, shift=finder.long_shift),
#| ) -
#| finder.position_base
#| guard cand >= cutoff else { return (best_len << 16) | best_dist }
#| let prev = finder.prev
#| let mut probe = best_len.max(3)
#| let mut suffix = read_window_match_word(data, i + probe - 3)
#| let nice_length = cfg.nice_length.min(limit)
#| for ;; {
#| for ;; {
#| let candidate_suffix = read_window_match_word(data, cand + probe - 3)
#| if candidate_suffix == suffix &&
#| read_window_match_word(data, cand) == prefix {
#| break
#| }
#| budget -= 1
#| cand = chain_previous(prev, cand)
#| guard budget > 0 && cand >= cutoff else {
#| return (best_len << 16) | best_dist
#| }
#| }
#| let length = match_len_in_window(data, cand, i, limit)
#| if length > best_len {
#| best_len = length
#| best_dist = i - cand
#| if length >= nice_length {
#| break
#| }
#| probe = length
#| suffix = read_window_match_word(data, i + probe - 3)
#| }
#| budget -= 1
#| cand = chain_previous(prev, cand)
#| guard budget > 0 && cand >= cutoff else { break }
#| }
#| (best_len << 16) | best_dist
#|}
#|fn MatchFinder::find_window_match(
#| finder : MatchFinder,
#| data : FixedArray[Byte],
#| i : Int32,
#| end : Int32,
#| base : Int32,
#| cfg : LevelConfig,
#|) -> Int32 {
#| let limit = (end - i).min(258)
#| if limit < 4 {
#| let budget = if base >= cfg.good_length {
#| cfg.max_chain >> 2
#| } else {
#| cfg.max_chain
#| }
#| guard base < limit && budget > 0 else { return base << 16 }
#| let cutoff = (i - WINDOW_SIZE).max(0)
#| let short = finder.short_head.unsafe_get(
#| hash3_window(data, i, shift=finder.short_shift),
#| ) -
#| finder.position_base
#| if base < 3 && short >= cutoff {
#| let candidate = short & STREAM_RING_MASK
#| let position = i & STREAM_RING_MASK
#| if data[candidate] == data[position] &&
#| data[candidate + 1] == data[position + 1] &&
#| data[candidate + 2] == data[position + 2] {
#| return (3 << 16) | (i - short)
#| }
#| }
#| return base << 16
#| }
#| let prefix = read_window_match_word(data, i)
#| let product = prefix * 0x1E35A7BDU
#| finder.find_window_match_hashed(data, i, end, base, cfg, prefix, product)
#|}
#|fn MatchFinder::insert_pos_hashed(
#| finder : MatchFinder,
#| i : Int32,
#| product : UInt32,
#|) -> Unit {
#| let h = long_hash_product(product, shift=finder.long_shift)
#| let stamped = i + finder.position_base
#| finder.short_head.unsafe_set(
#| short_hash_product(product, shift=finder.short_shift),
#| stamped,
#| )
#| set_chain_previous(
#| finder.prev,
#| i,
#| finder.head.unsafe_get(h) - finder.position_base,
#| )
#| finder.head.unsafe_set(h, stamped)
#|}
#|fn MatchFinder::insert_pos(
#| finder : MatchFinder,
#| data : BytesView,
#| i : Int32,
#| n : Int32,
#|) -> Unit {
#| if n - i >= 4 {
#| finder.insert_pos_hashed(i, read_match_word(data, i) * 0x1E35A7BDU)
#| } else if n - i >= 3 {
#| finder.short_head.unsafe_set(
#| short_hash3(data, i, shift=finder.short_shift),
#| i + finder.position_base,
#| )
#| }
#|}
#|fn MatchFinder::insert_positions(
#| finder : MatchFinder,
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#| available_end : Int32,
#|) -> Unit {
#| let end = end.min(available_end - 2)
#| guard start < end else { return }
#| let long_end = end.min(available_end - 3)
#| let head = finder.head
#| let short_head = finder.short_head
#| let prev = finder.prev
#| let base = finder.position_base
#| let short_shift = finder.short_shift
#| let long_shift = finder.long_shift
#| for position in start.. Unit {
#| let h = long_hash_product(product, shift=finder.long_shift)
#| let stamped = i + finder.position_base
#| finder.short_head.unsafe_set(
#| short_hash_product(product, shift=finder.short_shift),
#| stamped,
#| )
#| set_chain_previous(
#| finder.prev,
#| i,
#| finder.head.unsafe_get(h) - finder.position_base,
#| )
#| finder.head.unsafe_set(h, stamped)
#|}
#|fn MatchFinder::insert_window_pos(
#| finder : MatchFinder,
#| data : FixedArray[Byte],
#| i : Int32,
#| n : Int32,
#|) -> Unit {
#| if n - i >= 4 {
#| finder.insert_window_pos_hashed(
#| i,
#| read_window_match_word(data, i) * 0x1E35A7BDU,
#| )
#| } else if n - i >= 3 {
#| finder.short_head.unsafe_set(
#| hash3_window(data, i, shift=finder.short_shift),
#| i + finder.position_base,
#| )
#| }
#|}
#|fn MatchFinder::insert_window_positions(
#| finder : MatchFinder,
#| data : FixedArray[Byte],
#| start : Int32,
#| end : Int32,
#| available_end : Int32,
#|) -> Unit {
#| let end = end.min(available_end - 2)
#| guard start < end else { return }
#| let long_end = end.min(available_end - 3)
#| let head = finder.head
#| let short_head = finder.short_head
#| let prev = finder.prev
#| let base = finder.position_base
#| let short_shift = finder.short_shift
#| let long_shift = finder.long_shift
#| for position in start.. Int32 {
#| (length << 16) | distance
#|}
#|#inline
#|fn commit_match(
#| data : BytesView,
#| finder : MatchFinder,
#| n : Int32,
#| start : Int32,
#| length : Int32,
#| dist : Int32,
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Int32 {
#| tokens.push(pack_match_token(length, dist))
#| ll_freq[257 + len_to_idx[length]] += 1
#| d_freq[dist_index(dist)] += 1
#| let match_end = start + length
#| if length >= 32 {
#| finder.insert_matched_positions(data, start + 1, match_end, n)
#| } else {
#| finder.insert_positions(data, start + 1, match_end, n)
#| }
#| match_end
#|}
#|#inline
#|fn commit_window_match(
#| data : FixedArray[Byte],
#| finder : MatchFinder,
#| end : Int32,
#| start : Int32,
#| length : Int32,
#| dist : Int32,
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Int32 {
#| tokens.push(pack_match_token(length, dist))
#| ll_freq[257 + len_to_idx[length]] += 1
#| d_freq[dist_index(dist)] += 1
#| let match_end = start + length
#| if length >= 32 {
#| finder.insert_matched_window_positions(data, start + 1, match_end, end)
#| } else {
#| finder.insert_window_positions(data, start + 1, match_end, end)
#| }
#| match_end
#|}
#|#inline
#|fn emit_literal(
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| b : Int32,
#|) -> Unit {
#| tokens.push(b)
#| ll_freq[b] += 1
#|}
#|fn tokenize(
#| data : BytesView,
#| finder : MatchFinder,
#| start : Int32,
#| end : Int32,
#| cfg : LevelConfig,
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Int32 {
#| let n = data.length()
#| let cost_model = cfg == level_configs[6] &&
#| (finder.min_match_length > 0 || n - start >= 1024)
#| if cost_model && finder.min_match_length == 0 && n - start >= 512 {
#| let used = FixedArray::make(256, 0)
#| for j in start..<(start + 4096).min(n) {
#| used[data[j].to_int()] = 1
#| }
#| let mut distinct = 0
#| for j in 0..<256 {
#| distinct += used[j]
#| }
#| finder.min_match_length = minimum_match_length(distinct)
#| }
#| let min_length = if cost_model { finder.min_match_length.max(3) } else { 3 }
#| let max_lazy = if cost_model { 65 } else { cfg.max_lazy }
#| let chain_budget = if min_length >= 5 {
#| 17
#| } else if min_length >= 4 {
#| 24
#| } else {
#| 8
#| }
#| let chain_budget_after_prev = if min_length >= 5 {
#| 8
#| } else if min_length >= 4 {
#| 12
#| } else {
#| 4
#| }
#| let cost_cfg = {
#| good_length: 259,
#| max_lazy: 65,
#| nice_length: 65,
#| max_chain: chain_budget,
#| }
#| let cost_cfg_after_prev = {
#| good_length: 259,
#| max_lazy: 65,
#| nice_length: 65,
#| max_chain: chain_budget_after_prev,
#| }
#| let mut i = start
#| let mut prev_len = 0
#| let mut prev_dist = 0
#| let mut have_prev = false // a match found at i-1 is pending a decision
#| while i < end {
#| let has_word = has_bytes_from(i, 4, n)
#| let mut prefix = 0U
#| let mut product = 0U
#| if has_word {
#| prefix = read_match_word(data, i)
#| product = prefix * 0x1E35A7BDU
#| }
#| let searching = has_bytes_from(i, 3, n) &&
#| !(have_prev && prev_len >= max_lazy)
#| let base = if have_prev {
#| prev_len - (if cost_model { 1 } else { 0 })
#| } else {
#| min_length - 1
#| }
#| let search_cfg = if cost_model {
#| if have_prev {
#| cost_cfg_after_prev
#| } else {
#| cost_cfg
#| }
#| } else {
#| cfg
#| }
#| let match_info = if searching {
#| if has_word {
#| finder.find_match_hashed(data, i, n, base, search_cfg, prefix, product)
#| } else {
#| finder.find_match(data, i, n, base, search_cfg)
#| }
#| } else {
#| 0
#| }
#| let cur_len = match_info >> 16
#| let cur_dist = match_info & 0xFFFF
#| let prefer_current = cur_dist > 0 &&
#| (
#| !cost_model ||
#| (
#| cur_len >= prev_len &&
#| 4 * (cur_len - prev_len) + cur_dist.clz() - prev_dist.clz() > 2
#| )
#| )
#| guard !have_prev || prefer_current else {
#| i = commit_match(
#| data,
#| finder,
#| n,
#| i - 1,
#| prev_len,
#| prev_dist,
#| tokens,
#| ll_freq,
#| d_freq,
#| )
#| have_prev = false
#| continue
#| }
#| if has_word {
#| finder.insert_pos_hashed(i, product)
#| } else {
#| finder.insert_pos(data, i, n)
#| }
#| guard !have_prev else {
#| emit_literal(tokens, ll_freq, data[i - 1].to_int())
#| prev_len = cur_len
#| prev_dist = cur_dist
#| i = i + 1
#| continue
#| }
#| guard cur_dist > 0 &&
#| cur_len >= min_length &&
#| !(cost_model && cur_len == 3 && cur_dist > 8192) else {
#| emit_literal(tokens, ll_freq, data[i].to_int())
#| i = i + 1
#| continue
#| }
#| guard cfg.max_lazy > 0 else {
#| i = commit_match(
#| data, finder, n, i, cur_len, cur_dist, tokens, ll_freq, d_freq,
#| )
#| continue
#| }
#| prev_len = cur_len
#| prev_dist = cur_dist
#| have_prev = true
#| i = i + 1
#| }
#| if have_prev {
#| i = commit_match(
#| data,
#| finder,
#| n,
#| i - 1,
#| prev_len,
#| prev_dist,
#| tokens,
#| ll_freq,
#| d_freq,
#| )
#| }
#| i
#|}
#|fn tokenize_window(
#| data : FixedArray[Byte],
#| finder : MatchFinder,
#| start : Int32,
#| token_end : Int32,
#| available_end : Int32,
#| cfg : LevelConfig,
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Int32 {
#| let cost_model = cfg == level_configs[6] &&
#| (finder.min_match_length > 0 || available_end - start >= 1024)
#| if cost_model && finder.min_match_length == 0 && available_end - start >= 512 {
#| let used = FixedArray::make(256, 0)
#| for j in start..<(start + 4096).min(available_end) {
#| used[data[j & STREAM_RING_MASK].to_int()] = 1
#| }
#| let mut distinct = 0
#| for j in 0..<256 {
#| distinct += used[j]
#| }
#| finder.min_match_length = minimum_match_length(distinct)
#| }
#| let min_length = if cost_model { finder.min_match_length.max(3) } else { 3 }
#| let max_lazy = if cost_model { 65 } else { cfg.max_lazy }
#| let chain_budget = if min_length >= 5 {
#| 17
#| } else if min_length >= 4 {
#| 24
#| } else {
#| 8
#| }
#| let chain_budget_after_prev = if min_length >= 5 {
#| 8
#| } else if min_length >= 4 {
#| 12
#| } else {
#| 4
#| }
#| let cost_cfg = {
#| good_length: 259,
#| max_lazy: 65,
#| nice_length: 65,
#| max_chain: chain_budget,
#| }
#| let cost_cfg_after_prev = {
#| good_length: 259,
#| max_lazy: 65,
#| nice_length: 65,
#| max_chain: chain_budget_after_prev,
#| }
#| let mut i = start
#| let mut prev_len = 0
#| let mut prev_dist = 0
#| let mut have_prev = false
#| while i < token_end {
#| let has_word = i + 4 <= available_end
#| let mut prefix = 0U
#| let mut product = 0U
#| if has_word {
#| prefix = read_window_match_word(data, i)
#| product = prefix * 0x1E35A7BDU
#| }
#| let searching = i + 3 <= available_end &&
#| !(have_prev && prev_len >= max_lazy)
#| let base = if have_prev {
#| prev_len - (if cost_model { 1 } else { 0 })
#| } else {
#| min_length - 1
#| }
#| let search_cfg = if cost_model {
#| if have_prev {
#| cost_cfg_after_prev
#| } else {
#| cost_cfg
#| }
#| } else {
#| cfg
#| }
#| let match_info = if searching {
#| if has_word {
#| finder.find_window_match_hashed(
#| data, i, available_end, base, search_cfg, prefix, product,
#| )
#| } else {
#| finder.find_window_match(data, i, available_end, base, search_cfg)
#| }
#| } else {
#| 0
#| }
#| let cur_len = match_info >> 16
#| let cur_dist = match_info & 0xFFFF
#| let prefer_current = cur_dist > 0 &&
#| (
#| !cost_model ||
#| (
#| cur_len >= prev_len &&
#| 4 * (cur_len - prev_len) + cur_dist.clz() - prev_dist.clz() > 2
#| )
#| )
#| guard !have_prev || prefer_current else {
#| i = commit_window_match(
#| data,
#| finder,
#| available_end,
#| i - 1,
#| prev_len,
#| prev_dist,
#| tokens,
#| ll_freq,
#| d_freq,
#| )
#| have_prev = false
#| continue
#| }
#| if has_word {
#| finder.insert_window_pos_hashed(i, product)
#| } else {
#| finder.insert_window_pos(data, i, available_end)
#| }
#| guard !have_prev else {
#| emit_literal(tokens, ll_freq, data[(i - 1) & STREAM_RING_MASK].to_int())
#| prev_len = cur_len
#| prev_dist = cur_dist
#| i = i + 1
#| continue
#| }
#| guard cur_dist > 0 &&
#| cur_len >= min_length &&
#| !(cost_model && cur_len == 3 && cur_dist > 8192) else {
#| emit_literal(tokens, ll_freq, data[i & STREAM_RING_MASK].to_int())
#| i = i + 1
#| continue
#| }
#| guard cfg.max_lazy > 0 else {
#| i = commit_window_match(
#| data, finder, available_end, i, cur_len, cur_dist, tokens, ll_freq, d_freq,
#| )
#| continue
#| }
#| prev_len = cur_len
#| prev_dist = cur_dist
#| have_prev = true
#| i = i + 1
#| }
#| if have_prev {
#| i = commit_window_match(
#| data,
#| finder,
#| available_end,
#| i - 1,
#| prev_len,
#| prev_dist,
#| tokens,
#| ll_freq,
#| d_freq,
#| )
#| }
#| i
#|}
#|#inline
#|fn set_chain_previous(
#| chain : FixedArray[Byte],
#| position : Int32,
#| previous : Int32,
#|) -> Unit {
#| let delta = if previous < position - WINDOW_SIZE {
#| WINDOW_SIZE + 1
#| } else {
#| position - previous
#| }
#| let index = (position & WINDOW_MASK) * 2
#| chain.unsafe_set(index, delta.to_byte())
#| chain.unsafe_set(index + 1, (delta >> 8).to_byte())
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn chain_previous(chain : FixedArray[Byte], position : Int32) -> Int32 {
#| let index = (position & WINDOW_MASK) * 2
#| let delta = match_window_as_bytes(chain).unsafe_read_uint32_le(index) & 0xFFFF
#| position - delta.reinterpret_as_int()
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#inline
#|fn chain_previous(chain : FixedArray[Byte], position : Int32) -> Int32 {
#| let index = (position & WINDOW_MASK) * 2
#| let delta = chain.unsafe_get(index).to_int() |
#| (chain.unsafe_get(index + 1).to_int() << 8)
#| position - delta
#|}
#|fn MatchFinder::insert_matched_positions(
#| finder : MatchFinder,
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#| available_end : Int32,
#|) -> Unit {
#| let end = end.min(available_end - 2)
#| let long_end = (end - 3).min(available_end - 3)
#| let head = finder.head
#| let short_head = finder.short_head
#| let prev = finder.prev
#| let base = finder.position_base
#| let short_shift = finder.short_shift
#| let long_shift = finder.long_shift
#| let start = finder.insert_matched_quads(data, start, long_end)
#| for position in start..> 8).to_byte())
#| head.unsafe_set(h, stamped)
#| }
#| finder.insert_positions(data, long_end, end, available_end)
#|}
#|fn MatchFinder::insert_matched_window_positions(
#| finder : MatchFinder,
#| data : FixedArray[Byte],
#| start : Int32,
#| end : Int32,
#| available_end : Int32,
#|) -> Unit {
#| let end = end.min(available_end - 2)
#| let long_end = (end - 3).min(available_end - 3)
#| let head = finder.head
#| let short_head = finder.short_head
#| let prev = finder.prev
#| let base = finder.position_base
#| let short_shift = finder.short_shift
#| let long_shift = finder.long_shift
#| let start = finder.insert_matched_window_quads(data, start, long_end)
#| for position in start..> 8).to_byte())
#| head.unsafe_set(h, stamped)
#| }
#| finder.insert_window_positions(data, long_end, end, available_end)
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn matched_quad_hashes(bytes : V128, shift : Int32, shorts : Bool) -> V128 {
#| let words = @simd.i8x16_shuffle(
#| bytes, bytes, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6,
#| )
#| let products = @simd.i32x4_mul(words, @simd.i32x4_splat(0x1E35A7BDU))
#| if shorts {
#| @simd.i32x4_shr_u(
#| @simd.v128_and_(products, @simd.i32x4_splat(0xFFFFFFU)),
#| shift,
#| )
#| } else {
#| @simd.i32x4_shr_u(products, shift)
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn link_matched_position(
#| short_head : FixedArray[Int32],
#| head : FixedArray[Int32],
#| prev : FixedArray[Byte],
#| base : Int32,
#| position : Int32,
#| long_hash : UInt32,
#| short_hash : UInt32,
#|) -> Unit {
#| let h = long_hash.reinterpret_as_int()
#| let stamped = position + base
#| short_head.unsafe_set(short_hash.reinterpret_as_int(), stamped)
#| let delta = stamped - head.unsafe_get(h)
#| let index = (position & WINDOW_MASK) * 2
#| prev.unsafe_set(index, delta.to_byte())
#| prev.unsafe_set(index + 1, (delta >> 8).to_byte())
#| head.unsafe_set(h, stamped)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn MatchFinder::insert_matched_quads(
#| finder : MatchFinder,
#| data : BytesView,
#| start : Int32,
#| long_end : Int32,
#|) -> Int32 {
#| let bytes = bytes_as_fixedarray(data.data())
#| let offset = data.start_offset()
#| let length = data.length()
#| let head = finder.head
#| let short_head = finder.short_head
#| let prev = finder.prev
#| let base = finder.position_base
#| let short_shift = finder.short_shift
#| let long_shift = finder.long_shift
#| let mut position = start
#| while position <= long_end - 4 && position <= length - 16 {
#| let block = @simd.v128_load(bytes, offset + position)
#| let longs = matched_quad_hashes(block, long_shift, false)
#| let shorts = matched_quad_hashes(block, short_shift, true)
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position,
#| @simd.i32x4_extract_lane(longs, 0),
#| @simd.i32x4_extract_lane(shorts, 0),
#| )
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position + 1,
#| @simd.i32x4_extract_lane(longs, 1),
#| @simd.i32x4_extract_lane(shorts, 1),
#| )
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position + 2,
#| @simd.i32x4_extract_lane(longs, 2),
#| @simd.i32x4_extract_lane(shorts, 2),
#| )
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position + 3,
#| @simd.i32x4_extract_lane(longs, 3),
#| @simd.i32x4_extract_lane(shorts, 3),
#| )
#| position = position + 4
#| }
#| position
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn MatchFinder::insert_matched_window_quads(
#| finder : MatchFinder,
#| data : FixedArray[Byte],
#| start : Int32,
#| long_end : Int32,
#|) -> Int32 {
#| let head = finder.head
#| let short_head = finder.short_head
#| let prev = finder.prev
#| let base = finder.position_base
#| let short_shift = finder.short_shift
#| let long_shift = finder.long_shift
#| let mut position = start
#| while position <= long_end - 4 {
#| let block = @simd.v128_load(data, position & STREAM_RING_MASK)
#| let longs = matched_quad_hashes(block, long_shift, false)
#| let shorts = matched_quad_hashes(block, short_shift, true)
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position,
#| @simd.i32x4_extract_lane(longs, 0),
#| @simd.i32x4_extract_lane(shorts, 0),
#| )
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position + 1,
#| @simd.i32x4_extract_lane(longs, 1),
#| @simd.i32x4_extract_lane(shorts, 1),
#| )
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position + 2,
#| @simd.i32x4_extract_lane(longs, 2),
#| @simd.i32x4_extract_lane(shorts, 2),
#| )
#| link_matched_position(
#| short_head,
#| head,
#| prev,
#| base,
#| position + 3,
#| @simd.i32x4_extract_lane(longs, 3),
#| @simd.i32x4_extract_lane(shorts, 3),
#| )
#| position = position + 4
#| }
#| position
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#inline
#|fn MatchFinder::insert_matched_quads(
#| _finder : MatchFinder,
#| _data : BytesView,
#| start : Int32,
#| _long_end : Int32,
#|) -> Int32 {
#| start
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#inline
#|fn MatchFinder::insert_matched_window_quads(
#| _finder : MatchFinder,
#| _data : FixedArray[Byte],
#| start : Int32,
#| _long_end : Int32,
#|) -> Int32 {
#| start
#|}
#|fn minimum_match_length(distinct : Int32) -> Int32 {
#| match () {
#| _ if distinct >= 80 => 3
#| _ if distinct >= 45 => 4
#| _ if distinct >= 16 => 5
#| _ if distinct >= 10 => 6
#| _ if distinct >= 8 => 7
#| _ if distinct >= 6 => 8
#| _ => 9
#| }
#|}
),
"optimal_parse.mbt": (
#|#valtype
#|priv struct MatchRange {
#| len : Int32
#| dist : Int32
#|}
#|fn insert_optimal_pos(
#| data : BytesView,
#| head : FixedArray[Int32],
#| prev : FixedArray[Int32],
#| i : Int32,
#| n : Int32,
#| seed_from : Int32,
#|) -> Unit {
#| if has_bytes_from(i, 3, n) {
#| let h = hash3(data, i)
#| prev[i - seed_from] = head[h]
#| head[h] = i
#| }
#|}
#|const OPTIMAL_MAX_CHAIN : Int32 = 8192
#|fn collect_matches(
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#|) -> FixedArray[Array[MatchRange]] {
#| let seed_from = (start - WINDOW_SIZE).max(0)
#| let head = FixedArray::make(1 << HASH_BITS, -1)
#| let prev = FixedArray::make((end - seed_from).max(1), -1)
#| let n = data.length()
#| for j in seed_from..= 3 && has_bytes_from(i, 3, n) {
#| let h = hash3(data, i)
#| let mut cand = head[h]
#| let mut best = 2
#| let mut steps = 0
#| while cand >= 0 && steps < OPTIMAL_MAX_CHAIN && i - cand <= 32768 {
#| let l = if match_candidate_can_beat(data, cand, i, best, limit) {
#| match_len_up_to(data, cand, i, limit)
#| } else {
#| best
#| }
#| if l > best {
#| frontier.push({ len: l, dist: i - cand, })
#| best = l
#| if l >= limit {
#| break
#| }
#| }
#| cand = prev[cand - seed_from]
#| steps = steps + 1
#| }
#| }
#| insert_optimal_pos(data, head, prev, i, n, seed_from)
#| out[i - start] = frontier
#| }
#| out
#|}
#|fn replay_tokens(
#| data : BytesView,
#| start : Int32,
#| m : Int32,
#| len_choice : FixedArray[Int32],
#| dist_choice : FixedArray[Int32],
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Unit {
#| let steps : Array[Int32] = []
#| let mut o = m
#| while o > 0 {
#| steps.push(o)
#| o = o - len_choice[o]
#| }
#| let mut pos = start
#| for at in steps.rev_iter() {
#| let l = len_choice[at]
#| if l == 1 && dist_choice[at] == 0 {
#| let b = data[pos].to_int()
#| tokens.push(b)
#| ll_freq[b] += 1
#| } else {
#| tokens.push(pack_match_token(l, dist_choice[at]))
#| ll_freq[257 + len_to_idx[l]] += 1
#| d_freq[dist_index(dist_choice[at])] += 1
#| }
#| pos = pos + l
#| }
#|}
#|fn squeeze_parse(
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#| matches : FixedArray[Array[MatchRange]],
#| ll_cost : FixedArray[Int32],
#| d_cost : FixedArray[Int32],
#| tokens : Array[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Unit {
#| let m = end - start
#| let inf = 0x3FFFFFFF
#| let costs = FixedArray::make(m + 1, inf)
#| let len_choice = FixedArray::make(m + 1, 0)
#| let dist_choice = FixedArray::make(m + 1, 0)
#| costs[0] = 0
#| for o in 0.. Unit {
#| let ll_freq_eob = FixedArray::make(286, 0)
#| ll_freq.blit_to(ll_freq_eob, len=286)
#| ll_freq_eob[256] += 1
#| let ll_len = FixedArray::make(286, 0)
#| gen_huffman(ll_freq_eob, 286, 15, ll_len)
#| let dd_len = FixedArray::make(30, 0)
#| gen_huffman(d_freq, 30, 15, dd_len)
#| for s in 0..<286 {
#| ll_cost[s] = if ll_len[s] == 0 { 15 } else { ll_len[s] }
#| }
#| for d in 0..<30 {
#| d_cost[d] = if dd_len[d] == 0 { 15 } else { dd_len[d] }
#| }
#|}
#|fn tokenize_optimal(
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#| iterations : Int32,
#|) -> Array[Int32] {
#| let matches = collect_matches(data, start, end)
#| let mut ll_cost = FixedArray::makei(286, s => fixed_litlen_info[s] & 0xF)
#| let mut d_cost = FixedArray::make(30, 5)
#| let mut best_bits = 0x3FFFFFFF
#| let mut best_tokens : Array[Int32]? = None
#| for _it in 0.. Int32 {
#| let ll_freq = FixedArray::make(286, 0)
#| ll_freq_in.blit_to(ll_freq, len=286)
#| ll_freq[256] += 1
#| let lengths = build_dynamic_code_lengths(ll_freq, d_freq)
#| let cost = estimate_block_cost(lengths, byte_len, ll_freq, d_freq)
#| cost.stored.min(cost.fixed).min(cost.dynamic)
#|}
#|fn range_freqs(
#| tokens : Array[Int32],
#| lo : Int32,
#| hi : Int32,
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Unit {
#| ll_freq.fill(0, end=286)
#| d_freq.fill(0, end=30)
#| for k in lo..> 16
#| let distance = tok & 0xFFFF
#| ll_freq[257 + len_to_idx[length]] += 1
#| d_freq[dist_index(distance)] += 1
#| }
#| }
#|}
#|fn split_range_cost(
#| tokens : Array[Int32],
#| lo : Int32,
#| hi : Int32,
#| byte_len : Int32,
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Int32 {
#| range_freqs(tokens, lo, hi, ll_freq, d_freq)
#| block_cost_bits(byte_len, ll_freq, d_freq)
#|}
#|fn split_candidate_cost(
#| tokens : Array[Int32],
#| tok_pos : FixedArray[Int32],
#| lo : Int32,
#| hi : Int32,
#| k : Int32,
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Int32 {
#| split_range_cost(tokens, lo, k, tok_pos[k] - tok_pos[lo], ll_freq, d_freq) +
#| split_range_cost(tokens, k, hi, tok_pos[hi] - tok_pos[k], ll_freq, d_freq)
#|}
#|fn find_best_split(
#| tokens : Array[Int32],
#| tok_pos : FixedArray[Int32],
#| lo : Int32,
#| hi : Int32,
#| result : FixedArray[Int32],
#| ll_freq : FixedArray[Int32],
#| d_freq : FixedArray[Int32],
#|) -> Unit {
#| let sample = 9
#| let mut plo = lo + 1
#| let mut phi = hi - 1
#| let mut best_k = -1
#| let mut best_cost = 0x3FFFFFFF
#| while phi - plo > sample {
#| let step = (phi - plo) / (sample + 1)
#| let mut win_i = 0
#| let mut win_k = plo
#| let mut win_c = 0x3FFFFFFF
#| for i in 0.. Array[Int32] {
#| let bounds : Array[Int32] = [0, tokens.length()]
#| let split_result = FixedArray::make(2, 0)
#| let ll_freq = FixedArray::make(286, 0)
#| let d_freq = FixedArray::make(30, 0)
#| while bounds.length() - 1 < OPTIMAL_MAX_BLOCKS {
#| let mut best_gain = 0
#| let mut best_seg = -1
#| let mut best_k = -1
#| for s in 0..<(bounds.length() - 1) {
#| let lo = bounds[s]
#| let hi = bounds[s + 1]
#| if hi - lo < 16 {
#| continue
#| }
#| let whole = split_range_cost(
#| tokens,
#| lo,
#| hi,
#| tok_pos[hi] - tok_pos[lo],
#| ll_freq,
#| d_freq,
#| )
#| find_best_split(tokens, tok_pos, lo, hi, split_result, ll_freq, d_freq)
#| let k = split_result[0]
#| let cost = split_result[1]
#| if k >= 0 && whole - cost > best_gain {
#| best_gain = whole - cost
#| best_seg = s
#| best_k = k
#| }
#| }
#| if best_seg < 0 {
#| break
#| }
#| bounds.insert(best_seg + 1, best_k)
#| }
#| bounds
#|}
#|fn grouping_cost(
#| tokens : Array[Int32],
#| tok_pos : FixedArray[Int32],
#| bounds : Array[Int32],
#|) -> Int32 {
#| let mut total = 0
#| let ll_freq = FixedArray::make(286, 0)
#| let d_freq = FixedArray::make(30, 0)
#| for s in 0..<(bounds.length() - 1) {
#| let lo = bounds[s]
#| let hi = bounds[s + 1]
#| total = total +
#| split_range_cost(
#| tokens,
#| lo,
#| hi,
#| tok_pos[hi] - tok_pos[lo],
#| ll_freq,
#| d_freq,
#| )
#| }
#| total
#|}
#|fn token_positions(
#| tokens : Array[Int32],
#| start : Int32,
#| end : Int32,
#|) -> FixedArray[Int32] {
#| let positions = FixedArray::make(tokens.length() + 1, 0)
#| let mut p = start
#| for k in 0..> 16 })
#| }
#| positions[tokens.length()] = end
#| positions
#|}
#|priv struct OptimalChunk {
#| tokens : Array[Int32]
#| joins : Array[Int32]
#| chunk_end : Int32
#|}
#|fn Compressor::optimal_parse_chunk(
#| self : Compressor,
#| input : BytesView,
#| pos : Int32,
#| target : Int32,
#| iterations : Int32,
#|) -> OptimalChunk {
#| let processed = self.tokenize(input, pos, target, level_configs[9])
#| let gtoks = self.tokens
#| let chunk_end = processed // a boundary match may overhang `target`
#| let gtok_pos = token_positions(gtoks, pos, chunk_end)
#| let bounds1 = plan_split_points(gtoks, gtok_pos)
#| let tokens : Array[Int32] = []
#| let joins : Array[Int32] = [0] // phase-1 boundaries, as optimal-token indices
#| for s in 0..<(bounds1.length() - 1) {
#| let a = gtok_pos[bounds1[s]]
#| let b = gtok_pos[bounds1[s + 1]]
#| let toks = tokenize_optimal(input, a, b, iterations)
#| tokens.append(toks)
#| joins.push(tokens.length())
#| }
#| { tokens, joins, chunk_end, }
#|}
#|fn emit_optimal_chunk(
#| w : BitWriter,
#| input : BytesView,
#| chunk : OptimalChunk,
#| pos : Int32,
#| is_final~ : Bool,
#|) -> Unit {
#| let all = chunk.tokens
#| let all_pos = token_positions(all, pos, chunk.chunk_end)
#| let bounds2 = plan_split_points(all, all_pos)
#| let bounds = if grouping_cost(all, all_pos, bounds2) <
#| grouping_cost(all, all_pos, chunk.joins) {
#| bounds2
#| } else {
#| chunk.joins
#| }
#| for s in 0..<(bounds.length() - 1) {
#| let lo = bounds[s]
#| let hi = bounds[s + 1]
#| let ll_freq = FixedArray::make(286, 0)
#| let d_freq = FixedArray::make(30, 0)
#| range_freqs(all, lo, hi, ll_freq, d_freq)
#| w.emit_block(
#| input,
#| all_pos[lo],
#| all_pos[hi],
#| all[lo:hi],
#| ll_freq,
#| d_freq,
#| is_final=is_final && hi == all.length(),
#| )
#| }
#|}
#|fn Compressor::encode_optimal(
#| self : Compressor,
#| input : BytesView,
#| pos : Int32,
#| target : Int32,
#| w : BitWriter,
#| iterations : Int32,
#| is_final~ : Bool,
#|) -> Int32 {
#| guard pos < input.length() else {
#| w.emit_empty_block(input)
#| return pos
#| }
#| let chunk = self.optimal_parse_chunk(input, pos, target, iterations)
#| emit_optimal_chunk(
#| w,
#| input,
#| chunk,
#| pos,
#| is_final=is_final && chunk.chunk_end == input.length(),
#| )
#| chunk.chunk_end
#|}
),
"split_plan.mbt": (
#|const SPLIT_NUM_LITERAL_CLASSES : Int32 = 8
#|const SPLIT_NUM_CLASSES : Int32 = 10
#|const SPLIT_CHECK_TOKENS : Int32 = 512
#|const SPLIT_MIN_BLOCK_BYTES : Int32 = 10000
#|const SPLIT_SOFT_MAX_BLOCK_BYTES : Int32 = 300000
#|const SPLIT_CUTOFF_NUM : Int32 = 200
#|const SPLIT_CUTOFF_DEN : Int32 = 512
#|const SPLIT_BIAS_BYTES : Int32 = 4096
#|#inline
#|fn split_token_class(tok : Int32) -> Int32 {
#| let lit = ((tok >> 5) & 6) | (tok & 1)
#| let len = tok >> 16
#| let is_lit = (tok - 256) >> 31
#| let match_class = SPLIT_NUM_LITERAL_CLASSES + (1 - (((len - 9) >> 31) & 1))
#| (lit & is_lit) | (match_class & (is_lit ^ -1))
#|}
#|#inline
#|fn split_token_bytes(tok : Int32) -> Int32 {
#| let is_lit = (tok - 256) >> 31
#| 1 + ((is_lit ^ -1) & ((tok >> 16) - 1))
#|}
#|fn split_end_block_check(
#| old : FixedArray[Int32],
#| old_tot : Int32,
#| new : FixedArray[Int32],
#| new_tot : Int32,
#| block_bytes : Int32,
#|) -> Bool {
#| let mut delta = 0
#| for i in 0..= cutoff
#|}
#|fn choose_splits(tokens : Array[Int32]) -> Array[Int32] {
#| let total_bytes = tokens.fold(init=0, (total, tok) => {
#| total + split_token_bytes(tok)
#| })
#| let old = FixedArray::make(SPLIT_NUM_CLASSES, 0)
#| let mut old_tot = 0
#| let new = FixedArray::make(SPLIT_NUM_CLASSES, 0)
#| let mut new_tot = 0
#| let mut block_bytes = 0
#| let mut done_bytes = 0
#| let cuts : Array[Int32] = []
#| for i, tok in tokens {
#| let c = split_token_class(tok)
#| new[c] = new[c] + 1
#| new_tot = new_tot + 1
#| let tb = split_token_bytes(tok)
#| block_bytes = block_bytes + tb
#| done_bytes = done_bytes + tb
#| guard block_bytes >= SPLIT_MIN_BLOCK_BYTES &&
#| total_bytes - done_bytes >= SPLIT_MIN_BLOCK_BYTES else {
#| continue
#| }
#| let cut = block_bytes >= SPLIT_SOFT_MAX_BLOCK_BYTES ||
#| (
#| new_tot >= SPLIT_CHECK_TOKENS &&
#| old_tot > 0 &&
#| split_end_block_check(old, old_tot, new, new_tot, block_bytes)
#| )
#| guard !cut else {
#| cuts.push(i + 1)
#| old.fill(0)
#| new.fill(0)
#| old_tot = 0
#| new_tot = 0
#| block_bytes = 0
#| continue
#| }
#| if new_tot >= SPLIT_CHECK_TOKENS {
#| for j in 0.. Array[Int32] {
#| let m = tokens.length()
#| let cuts : Array[Int32] = []
#| let end = all_pos[m]
#| let mut target = next_block_target(pos, end, block_size)
#| let mut k = 1
#| while k < m {
#| if all_pos[k] >= target {
#| cuts.push(k)
#| target = next_block_target(target, end, block_size)
#| }
#| k = k + 1
#| }
#| cuts
#|}
#|fn partition_cost(
#| tokens : Array[Int32],
#| all_pos : FixedArray[Int32],
#| cuts : Array[Int32],
#|) -> Int32 {
#| let m = tokens.length()
#| let mut total = 0
#| let mut lo = 0
#| for cut in cuts {
#| let ll_freq = FixedArray::make(MAX_NUM_LIT, 0)
#| let d_freq = FixedArray::make(MAX_NUM_DIST, 0)
#| range_freqs(tokens, lo, cut, ll_freq, d_freq)
#| total = total + block_cost_bits(all_pos[cut] - all_pos[lo], ll_freq, d_freq)
#| lo = cut
#| }
#| if lo < m {
#| let ll_freq = FixedArray::make(MAX_NUM_LIT, 0)
#| let d_freq = FixedArray::make(MAX_NUM_DIST, 0)
#| range_freqs(tokens, lo, m, ll_freq, d_freq)
#| total = total + block_cost_bits(all_pos[m] - all_pos[lo], ll_freq, d_freq)
#| }
#| total
#|}
#|fn emit_split_blocks(
#| w : BitWriter,
#| data : BytesView,
#| tokens : Array[Int32],
#| all_pos : FixedArray[Int32],
#| cuts : Array[Int32],
#| is_final~ : Bool,
#|) -> Unit {
#| let m = tokens.length()
#| let mut lo = 0
#| for cut in cuts {
#| let ll_freq = FixedArray::make(MAX_NUM_LIT, 0)
#| let d_freq = FixedArray::make(MAX_NUM_DIST, 0)
#| range_freqs(tokens, lo, cut, ll_freq, d_freq)
#| w.emit_block(
#| data,
#| all_pos[lo],
#| all_pos[cut],
#| tokens[lo:cut],
#| ll_freq,
#| d_freq,
#| is_final=is_final && cut == m,
#| )
#| lo = cut
#| }
#| if lo < m {
#| let ll_freq = FixedArray::make(MAX_NUM_LIT, 0)
#| let d_freq = FixedArray::make(MAX_NUM_DIST, 0)
#| range_freqs(tokens, lo, m, ll_freq, d_freq)
#| w.emit_block(
#| data,
#| all_pos[lo],
#| all_pos[m],
#| tokens[lo:m],
#| ll_freq,
#| d_freq,
#| is_final~,
#| )
#| }
#|}
#|fn Compressor::encode_split(
#| self : Compressor,
#| input : BytesView,
#| pos : Int32,
#| target : Int32,
#| w : BitWriter,
#| cfg : LevelConfig,
#| is_final~ : Bool,
#|) -> Int32 {
#| guard pos < input.length() else {
#| w.emit_empty_block(input)
#| return pos
#| }
#| let processed = self.tokenize(input, pos, target, cfg)
#| let tokens = self.tokens
#| let all_pos = token_positions(tokens, pos, processed)
#| let split_cuts = choose_splits(tokens)
#| let fixed_cuts = fixed_cadence_cuts(tokens, all_pos, pos, 65535)
#| let cuts = if partition_cost(tokens, all_pos, split_cuts) <
#| partition_cost(tokens, all_pos, fixed_cuts) {
#| split_cuts
#| } else {
#| fixed_cuts
#| }
#| emit_split_blocks(
#| w,
#| input,
#| tokens,
#| all_pos,
#| cuts,
#| is_final=is_final && processed == input.length(),
#| )
#| processed
#|}
),
"status.mbt": (
#|#derive(Eq, @debug.Debug)
#|pub(all) enum Status {
#| Done
#| NeedMoreInput
#| NeedMoreOutput
#|}
#|#derive(Eq, @debug.Debug)
#|pub(all) enum DeflateAction {
#| Continue
#| SyncFlush
#| Finish
#|}
#|#derive(Eq, @debug.Debug)
#|#valtype
#|pub(all) struct StepResult {
#| status : Status
#| consumed : Int32
#| produced : Int32
#|}
#|#derive(Eq, @debug.Debug)
#|pub(all) enum InputMode {
#| Exact
#| Prefix
#|}
#|#derive(Eq, @debug.Debug)
#|#valtype
#|pub struct DecompressResult {
#| consumed : Int32
#| produced : Int32
#|}
#|#derive(Eq, @debug.Debug)
#|pub struct Decompressed {
#| data : Bytes
#| consumed : Int32
#|}
),
"stream_emission.mbt": (
#|priv enum EmissionPhase {
#| Idle
#| StoredHeader(Int32)
#| StoredPayload(Int32)
#| FixedHeader
#| DynamicHeader(DynamicCodes)
#| CodeLengths(DynamicCodes)
#| CodeSymbols(DynamicCodes)
#| Tokens(DynamicCodes?)
#| EndBlock(DynamicCodes?)
#|}
#|fn IncrementalEncoder::start_stored(
#| self : IncrementalEncoder,
#| start : Int32,
#| end : Int32,
#| is_final~ : Bool,
#|) -> Unit {
#| self.cursor = start & STREAM_RING_MASK
#| self.phase = StoredHeader(self.cursor + (end - start))
#| self.emitted_final = is_final
#|}
#|fn IncrementalEncoder::copy_stored(
#| self : IncrementalEncoder,
#| output : MutArrayView[Byte],
#| pos : Int32,
#| count : Int32,
#|) -> Unit {
#| if count == 1 {
#| output[pos] = self.pending[self.cursor]
#| } else if count > 1 {
#| self.pending.blit_to(
#| output_storage(output),
#| src_offset=self.cursor,
#| dst_offset=output.start_offset() + pos,
#| len=count,
#| )
#| }
#| self.cursor += count
#|}
#|#cfg(any(target="native", target="wasm"))
#|const STREAM_WIDE_TOKENS : Bool = true
#|#cfg(not(any(target="native", target="wasm")))
#|const STREAM_WIDE_TOKENS : Bool = false
#|fn IncrementalEncoder::resume_token_prefix(
#| self : IncrementalEncoder,
#| codes : DynamicCodes?,
#| output : MutArrayView[Byte],
#| pos : Int32,
#|) -> Int32 {
#| let pos = if codes is Some(c) {
#| self.write_token_prefix(
#| output,
#| pos,
#| c.dll_val,
#| c.lengths.dll_len,
#| c.dd_val,
#| c.lengths.dd_len,
#| )
#| } else {
#| self.write_token_prefix(
#| output, pos, fixed_ll_val, fixed_ll_len, fixed_dist_val, fixed_dist_len,
#| )
#| }
#| if self.cursor == self.tokens.length() && self.w.bit_count < 8 {
#| self.phase = EndBlock(codes)
#| }
#| pos
#|}
#|fn IncrementalEncoder::write_token_prefix(
#| self : IncrementalEncoder,
#| output : MutArrayView[Byte],
#| start : Int32,
#| ll_val : FixedArray[UInt32],
#| ll_len : FixedArray[Int32],
#| dd_val : FixedArray[UInt32],
#| dd_len : FixedArray[Int32],
#|) -> Int32 {
#| let mut pos = start
#| let mut cursor = self.cursor
#| let end = self.tokens.length()
#| let mut lo = self.w.bitbuf.reinterpret_as_uint()
#| let mut hi = self.token_high
#| let mut bits = self.w.bit_count
#| for ;; {
#| while bits >= 8 && pos < output.length() {
#| output.unsafe_set(pos, lo.to_byte())
#| pos += 1
#| lo = (lo >> 8) | (hi << 24)
#| hi = hi >> 8
#| bits -= 8
#| }
#| let room = output.length() - pos
#| if bits >= 8 || cursor == end || (room == 0 && end - cursor >= 8) {
#| break
#| }
#| if STREAM_WIDE_TOKENS && room >= 8 && end - cursor > 64 {
#| let count = (end - cursor - 64).min(1 + (room - 8) / 6)
#| self.w.bitbuf = lo.reinterpret_as_int()
#| self.w.bit_count = bits
#| pos += self.w.write_wide_run(
#| self.tokens[cursor:cursor + count],
#| ll_val,
#| ll_len,
#| dd_val,
#| dd_len,
#| output_storage(output),
#| output.start_offset() + pos,
#| )
#| lo = self.w.bitbuf.reinterpret_as_uint()
#| bits = self.w.bit_count
#| cursor += count
#| continue
#| }
#| let goal = if room == 0 { 8 } else { 48 }
#| while cursor < end && bits < goal {
#| let tok = self.tokens.unsafe_get(cursor)
#| let (low, high, width) = if tok < 256 {
#| (ll_val[tok], 0U, ll_len[tok])
#| } else {
#| let length = tok >> 16
#| let dist = tok & 0xFFFF
#| let lidx = len_to_idx[length]
#| let lsym = 257 + lidx
#| let didx = dist_index(dist)
#| let lbits = ll_len[lsym] + len_extra[lidx]
#| let l = ll_val[lsym] |
#| ((length - len_base[lidx]).reinterpret_as_uint() << ll_len[lsym])
#| let d = dd_val[didx] |
#| ((dist - dist_base[didx]).reinterpret_as_uint() << dd_len[didx])
#| (
#| l | (d << lbits),
#| d >> (32 - lbits),
#| lbits + dd_len[didx] + dist_extra[didx],
#| )
#| }
#| if bits + width > 63 {
#| break
#| }
#| if bits >= 32 {
#| hi = hi | (low << (bits - 32))
#| } else {
#| hi = if bits == 0 {
#| high
#| } else {
#| (high << bits) | (low >> (32 - bits))
#| }
#| lo = lo | (low << bits)
#| }
#| bits += width
#| cursor += 1
#| }
#| }
#| self.cursor = cursor
#| self.w.bitbuf = lo.reinterpret_as_int()
#| self.token_high = hi
#| self.w.bit_count = bits
#| pos
#|}
#|fn IncrementalEncoder::resume_block(
#| self : IncrementalEncoder,
#| output : MutArrayView[Byte],
#|) -> Bool {
#| let w = self.w
#| for ;; {
#| if w.out.length() > 0 {
#| let remaining = w.out.length() - self.out_pos
#| let count = remaining.min(output.length() - w.direct_pos)
#| for i in 0.. return true
#| StoredHeader(end) => {
#| let n = end - self.cursor
#| w.write_bits(self.emitted_final.to_int(), 3)
#| w.flush()
#| w.write_byte(n.to_byte())
#| w.write_byte((n >> 8).to_byte())
#| w.write_byte((n ^ 0xFFFF).to_byte())
#| w.write_byte(((n ^ 0xFFFF) >> 8).to_byte())
#| self.phase = StoredPayload(end)
#| }
#| StoredPayload(end) => {
#| let count = (end - self.cursor).min(output.length() - w.direct_pos)
#| self.copy_stored(output, w.direct_pos, count)
#| w.direct_pos += count
#| if self.cursor < end {
#| return false
#| }
#| self.phase = Idle
#| }
#| FixedHeader => {
#| w.write_bits(2 | self.emitted_final.to_int(), 3)
#| self.cursor = 0
#| self.phase = Tokens(None)
#| }
#| DynamicHeader(codes) => {
#| w.write_bits(4 | self.emitted_final.to_int(), 3)
#| w.write_bits(codes.lengths.hlit - 257, 5)
#| w.write_bits(codes.lengths.hdist - 1, 5)
#| w.write_bits(codes.lengths.hclen - 4, 4)
#| self.cursor = 0
#| self.phase = CodeLengths(codes)
#| }
#| CodeLengths(codes) => {
#| w.write_bits(codes.lengths.codelen_code_len[code_order[self.cursor]], 3)
#| self.cursor += 1
#| if self.cursor == codes.lengths.hclen {
#| self.cursor = 0
#| self.phase = CodeSymbols(codes)
#| }
#| }
#| CodeSymbols(codes) => {
#| let it = codes.lengths.codelen_syms[self.cursor]
#| w.write_rcode(
#| codes.codelen_code_val[it.sym],
#| codes.lengths.codelen_code_len[it.sym],
#| )
#| w.write_bits(it.extra, it.nextra)
#| self.cursor += 1
#| if self.cursor == codes.lengths.codelen_syms.length() {
#| self.cursor = 0
#| self.phase = Tokens(Some(codes))
#| }
#| }
#| Tokens(codes) => {
#| w.direct_pos = self.resume_token_prefix(codes, output, w.direct_pos)
#| if self.phase is Tokens(_) {
#| return false
#| }
#| }
#| EndBlock(codes) => {
#| if codes is Some(c) {
#| w.write_rcode(c.dll_val[256], c.lengths.dll_len[256])
#| } else {
#| w.write_rcode(fixed_ll_val[256], fixed_ll_len[256])
#| }
#| if self.emitted_final {
#| w.flush()
#| }
#| self.phase = Idle
#| }
#| }
#| }
#|}
),
"tables.mbt": (
#|const WINDOW_SIZE : Int32 = 32768
#|const MAX_NUM_LIT : Int32 = 286
#|const MAX_NUM_DIST : Int32 = 30
#|const NUM_CODES : Int32 = 19
#|let code_order : FixedArray[Int32] = [
#| 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
#|]
#|let len_base : FixedArray[Int32] = [
#| 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83,
#| 99, 115, 131, 163, 195, 227, 258,
#|]
#|let len_extra : FixedArray[Int32] = [
#| 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
#| 5, 0,
#|]
#|fn build_length_code_info_table() -> FixedArray[Int32] {
#| FixedArray::makei(29, index => (len_base[index] << 3) | len_extra[index])
#|}
#|let length_code_info_table : FixedArray[Int32] = build_length_code_info_table()
#|fn build_len_to_idx() -> FixedArray[Int32] {
#| let m = FixedArray::make(259, 0)
#| for idx in 0..<29 {
#| let lo = len_base[idx]
#| let hi = if idx + 1 < 29 { len_base[idx + 1] } else { 259 }
#| m.fill(idx, start=lo, end=hi)
#| }
#| m
#|}
#|let len_to_idx : FixedArray[Int32] = build_len_to_idx()
#|let dist_base : FixedArray[Int32] = [
#| 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
#| 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577,
#|]
#|let dist_extra : FixedArray[Int32] = [
#| 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
#| 12, 12, 13, 13,
#|]
#|fn build_distance_code_info_table() -> FixedArray[Int32] {
#| FixedArray::makei(MAX_NUM_DIST, symbol => {
#| (dist_base[symbol] << 4) | dist_extra[symbol]
#| })
#|}
#|let distance_code_info_table : FixedArray[Int32] = build_distance_code_info_table()
#|fn build_dist_to_idx() -> FixedArray[Byte] {
#| let slots = FixedArray::make(256, b'\x00')
#| for symbol in 0..<16 {
#| slots.fill(
#| symbol.to_byte(),
#| start=dist_base[symbol] - 1,
#| end=dist_base[symbol + 1] - 1,
#| )
#| }
#| slots
#|}
#|let dist_to_idx : FixedArray[Byte] = build_dist_to_idx()
#|#inline
#|fn dist_index(distance : Int32) -> Int32 {
#| let shift = ((256U - distance.reinterpret_as_uint()) >> 29).reinterpret_as_int()
#| dist_to_idx[(distance - 1) >> shift].to_int() + (shift << 1)
#|}
#|fn build_fixed_litlen_info() -> FixedArray[Int32] {
#| FixedArray::makei(288, symbol => {
#| match symbol {
#| _ if symbol < 144 => ((0x30 + symbol) << 4) | 8
#| _ if symbol < 256 => ((0x190 + (symbol - 144)) << 4) | 9
#| _ if symbol < 280 => ((symbol - 256) << 4) | 7
#| _ => ((0xC0 + (symbol - 280)) << 4) | 8
#| }
#| })
#|}
#|let fixed_litlen_info : FixedArray[Int32] = build_fixed_litlen_info()
#|fn build_fixed_ll_val() -> FixedArray[UInt32] {
#| fixed_litlen_info.map(info => {
#| reverse_bits((info >> 4).reinterpret_as_uint(), info & 0xF)
#| })
#|}
#|let fixed_ll_val : FixedArray[UInt32] = build_fixed_ll_val()
#|fn build_fixed_ll_len() -> FixedArray[Int32] {
#| fixed_litlen_info.map(info => info & 0xF)
#|}
#|let fixed_ll_len : FixedArray[Int32] = build_fixed_ll_len()
#|fn build_fixed_dist_val() -> FixedArray[UInt32] {
#| FixedArray::makei(MAX_NUM_DIST, d => reverse_bits(d.reinterpret_as_uint(), 5))
#|}
#|let fixed_dist_val : FixedArray[UInt32] = build_fixed_dist_val()
#|let fixed_dist_len : FixedArray[Int32] = FixedArray::make(MAX_NUM_DIST, 5)
#|fn reverse8(x : Byte) -> Byte {
#| rev8tab[x.to_int()].to_byte()
#|}
#|fn reverse16(x : UInt32) -> UInt32 {
#| rev8tab[((x & 0xff00) >> 8).reinterpret_as_int()] |
#| (rev8tab[(x & 0xff).reinterpret_as_int()] << 8)
#|}
#|let rev8tab : FixedArray[UInt32] = [
#| 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136,
#| 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196,
#| 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172,
#| 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226,
#| 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154,
#| 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214,
#| 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190,
#| 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
#| 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133,
#| 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205,
#| 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163,
#| 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235,
#| 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151,
#| 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223,
#| 63, 191, 127, 255,
#|]
),
},
)
///|
let moonbitlang_core_compress_gzip_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/compress/gzip",
deps={},
files={
"decoder.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|const CHECKSUM_BUFFER_SIZE : Int32 = 4096
#|#cfg(not(any(target="native", target="wasm")))
#|const CHECKSUM_BUFFER_SIZE : Int32 = 0
#|priv enum DecMode {
#| GzHeader // the fixed 10-byte header (magic, method, FLG, MTIME, XFL, OS)
#| GzExtraLen // the 2-byte XLEN of FEXTRA
#| GzExtra(Int32) // discarding the FEXTRA payload; bytes still to skip
#| GzName // discarding the NUL-terminated FNAME
#| GzComment // discarding the NUL-terminated FCOMMENT
#| GzHcrc // verify the low sixteen bits of the header CRC-32
#| GzBody // inflating the DEFLATE stream
#| GzTrailer // reading + verifying the CRC-32 / ISIZE trailer
#| GzFinished
#|}
#|struct Decoder {
#| inf : @flate.IncrementalDecoder
#| mut crc : @checksum.Crc32 // of the current member's decompressed output
#| checksum_buffer : FixedArray[Byte] // owned batch storage; empty on direct-view backends
#| mut checksum_pending : Int32
#| mut header_crc : @checksum.Crc32
#| mut osize : UInt32 // current member's decompressed length, mod 2^32
#| mut pending : BytesView // incomplete fixed-size field, at most 10 bytes
#| mut borrowed_input : Bool // only borrowed during the active step
#| mut flg : Int32 // header FLG byte, gates the optional-field states
#| mut ending : Bool // Final input stays latched across output suspension
#| mut mode : DecMode
#| mut failure_kind : GzipErrorKind? // stable first terminal error
#| mut failure_message : String
#|}
#|pub fn Decoder::Decoder() -> Decoder {
#| {
#| inf: IncrementalDecoder(),
#| crc: Crc32(),
#| checksum_buffer: FixedArray::make(CHECKSUM_BUFFER_SIZE, b'\x00'),
#| checksum_pending: 0,
#| header_crc: Crc32(),
#| osize: 0,
#| pending: b"",
#| borrowed_input: false,
#| flg: 0,
#| ending: false,
#| mode: GzHeader,
#| failure_kind: None,
#| failure_message: "",
#| }
#|}
#|pub fn Decoder::reset(self : Decoder) -> Unit {
#| self.inf.reset()
#| self.crc = Crc32()
#| self.checksum_pending = 0
#| self.header_crc = Crc32()
#| self.osize = 0
#| self.pending = b""
#| self.borrowed_input = false
#| self.flg = 0
#| self.ending = false
#| self.mode = GzHeader
#| self.failure_kind = None
#| self.failure_message = ""
#|}
#|pub fn Decoder::is_finished(self : Decoder) -> Bool {
#| self.mode is GzFinished
#|}
#|fn Decoder::available(self : Decoder) -> Int32 {
#| self.pending.length()
#|}
#|fn Decoder::finish_stream(self : Decoder) -> Unit {
#| guard! self.available() == 0
#| self.pending = b""
#| self.borrowed_input = false
#| self.mode = GzFinished
#|}
#|fn Decoder::pending_at(self : Decoder, off : Int32) -> Byte {
#| self.pending[off]
#|}
#|fn Decoder::buffer_input(self : Decoder, input : BytesView) -> Unit {
#| guard input.length() != 0 else { return }
#| if self.available() == 0 {
#| self.pending = input
#| self.borrowed_input = true
#| } else {
#| self.pending = Bytes::from_iter(self.pending.iter().concat(input.iter()))
#| self.borrowed_input = false
#| }
#|}
#|fn Decoder::fill_pending(
#| self : Decoder,
#| input : BytesView,
#| start : Int32,
#| end : Int32,
#| required : Int32,
#|) -> Int32 {
#| let take = (required - self.available()).min(end - start)
#| guard take > 0 else { return 0 }
#| self.buffer_input(input[start:start + take])
#| take
#|}
#|fn Decoder::detach_input(self : Decoder) -> Unit {
#| guard self.borrowed_input else { return }
#| self.pending = copy_input(self.pending)
#| self.borrowed_input = false
#|}
#|fn Decoder::advance(self : Decoder, k : Int32) -> Unit {
#| guard k > 0 else { return }
#| guard! k == self.pending.length()
#| self.pending = b""
#| self.borrowed_input = false
#|}
#|fn Decoder::next_header_mode(self : Decoder, done : Int32) -> DecMode {
#| if done < 1 && (self.flg & 4) != 0 {
#| GzExtraLen
#| } else if done < 2 && (self.flg & 8) != 0 {
#| GzName
#| } else if done < 3 && (self.flg & 16) != 0 {
#| GzComment
#| } else if done < 4 && (self.flg & 2) != 0 {
#| GzHcrc
#| } else {
#| GzBody
#| }
#|}
#|fn Decoder::finish_gzheader(self : Decoder) -> Unit raise GzipError {
#| guard! self.available() >= 10
#| guard self.pending_at(0).to_int() == 0x1F &&
#| self.pending_at(1).to_int() == 0x8B else {
#| raise GzipError(InvalidHeader, "gzip: bad magic")
#| }
#| guard self.pending_at(2).to_int() == 0x08 else {
#| raise GzipError(InvalidHeader, "gzip: unsupported compression method")
#| }
#| self.flg = self.pending_at(3).to_int()
#| guard (self.flg & 0xE0) == 0 else {
#| raise GzipError(InvalidHeader, "gzip: reserved flags are set")
#| }
#| self.advance_header(10)
#| self.mode = self.next_header_mode(0)
#|}
#|fn Decoder::after_body_step(
#| self : Decoder,
#| st : @flate.Status,
#|) -> @flate.Status? raise GzipError {
#| match st {
#| Done => {
#| self.mode = GzTrailer
#| None
#| }
#| NeedMoreInput => {
#| guard !self.ending else { raise GzipError(Truncated, "gzip: truncated") }
#| Some(NeedMoreInput)
#| }
#| NeedMoreOutput => Some(NeedMoreOutput)
#| }
#|}
#|fn Decoder::finish_member_trailer(self : Decoder) -> Unit raise GzipError {
#| guard! self.available() >= 8
#| self.flush_output_checksum()
#| guard self.crc.finish() == self.u32_le(0) else {
#| raise GzipError(ChecksumMismatch, "gzip: CRC-32 mismatch")
#| }
#| guard self.osize == self.u32_le(4) else {
#| raise GzipError(LengthMismatch, "gzip: length mismatch")
#| }
#| self.advance(8)
#| self.finish_stream()
#|}
#|fn Decoder::u32_le(self : Decoder, off : Int32) -> UInt32 {
#| self.pending_at(off).to_uint() |
#| (self.pending_at(off + 1).to_uint() << 8) |
#| (self.pending_at(off + 2).to_uint() << 16) |
#| (self.pending_at(off + 3).to_uint() << 24)
#|}
#|fn Decoder::update_output_checksum(
#| decoder : Decoder,
#| data : ArrayView[Byte],
#|) -> Unit {
#| let capacity = decoder.checksum_buffer.length()
#| guard capacity > 0 else {
#| decoder.crc.update_array(data)
#| return
#| }
#| let mut position = 0
#| while position < data.length() {
#| let count = (data.length() - position).min(
#| capacity - decoder.checksum_pending,
#| )
#| decoder.checksum_buffer
#| .mut_view()
#| .blit_from_array(
#| data[position:position + count],
#| dst_offset=decoder.checksum_pending,
#| )
#| position += count
#| decoder.checksum_pending += count
#| if decoder.checksum_pending == capacity {
#| decoder.flush_output_checksum()
#| }
#| }
#|}
#|fn Decoder::flush_output_checksum(decoder : Decoder) -> Unit {
#| guard decoder.checksum_pending > 0 else { return }
#| decoder.crc.update(
#| decoder.checksum_buffer.unsafe_reinterpret_as_bytes()[:decoder.checksum_pending],
#| )
#| decoder.checksum_pending = 0
#|}
#|pub fn Decoder::step(
#| self : Decoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#|) -> @flate.StepResult raise GzipError {
#| self.step_impl(input, output, end_of_input=false)
#|}
#|pub fn Decoder::check_end(self : Decoder) -> @flate.Status raise GzipError {
#| self.step_impl(b"", [].mut_view(), end_of_input=true).status
#|}
#|fn Decoder::step_impl(
#| self : Decoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| end_of_input~ : Bool,
#|) -> @flate.StepResult raise GzipError {
#| if self.failure_kind is Some(kind) {
#| raise GzipError(kind, self.failure_message)
#| }
#| defer self.detach_input()
#| self.step_active(input, output, end_of_input~) catch {
#| GzipError(kind, message) => {
#| self.failure_kind = Some(kind)
#| self.failure_message = message
#| raise GzipError(kind, message)
#| }
#| }
#|}
#|fn Decoder::step_active(
#| self : Decoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| end_of_input~ : Bool,
#|) -> @flate.StepResult raise GzipError {
#| if self.mode is GzFinished {
#| return { status: Done, consumed: 0, produced: 0, }
#| }
#| if end_of_input {
#| self.ending = true
#| }
#| let input_end = if self.ending { 0 } else { input.length() }
#| let mut consumed = 0
#| let mut op = 0
#| for ;; {
#| match self.mode {
#| GzHeader => {
#| consumed += self.fill_pending(input, consumed, input_end, 10)
#| guard self.available() >= 10 else {
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| self.finish_gzheader()
#| }
#| GzExtraLen => {
#| consumed += self.fill_pending(input, consumed, input_end, 2)
#| guard self.available() >= 2 else {
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| let xlen = self.pending_at(0).to_int() |
#| (self.pending_at(1).to_int() << 8)
#| self.advance_header(2)
#| self.mode = GzExtra(xlen)
#| }
#| GzExtra(remaining) => {
#| let take = remaining.min(input_end - consumed)
#| if take > 0 {
#| if (self.flg & 2) != 0 {
#| self.header_crc.update(input[consumed:consumed + take])
#| }
#| consumed += take
#| }
#| guard take == remaining else {
#| self.mode = GzExtra(remaining - take)
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| self.mode = self.next_header_mode(1)
#| }
#| GzName => {
#| let remaining = input[consumed:input_end]
#| match remaining.find(b"\x00") {
#| Some(nul) => {
#| let take = nul + 1
#| if (self.flg & 2) != 0 {
#| self.header_crc.update(remaining[:take])
#| }
#| consumed += take
#| self.mode = self.next_header_mode(2)
#| }
#| None => {
#| if remaining.length() > 0 && (self.flg & 2) != 0 {
#| self.header_crc.update(remaining)
#| }
#| consumed = input_end
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| }
#| }
#| GzComment => {
#| let remaining = input[consumed:input_end]
#| match remaining.find(b"\x00") {
#| Some(nul) => {
#| let take = nul + 1
#| if (self.flg & 2) != 0 {
#| self.header_crc.update(remaining[:take])
#| }
#| consumed += take
#| self.mode = self.next_header_mode(3)
#| }
#| None => {
#| if remaining.length() > 0 && (self.flg & 2) != 0 {
#| self.header_crc.update(remaining)
#| }
#| consumed = input_end
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| }
#| }
#| GzHcrc => {
#| consumed += self.fill_pending(input, consumed, input_end, 2)
#| guard self.available() >= 2 else {
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| let expected = self.pending_at(0).to_uint() |
#| (self.pending_at(1).to_uint() << 8)
#| guard (self.header_crc.finish() & 0xffff) == expected else {
#| raise GzipError(ChecksumMismatch, "gzip: header CRC-16 mismatch")
#| }
#| self.advance(2)
#| self.mode = GzBody
#| }
#| GzBody => {
#| let op_before = op
#| let st_result = self.inf.step(
#| input[consumed:input_end],
#| output.mut_view(start=op),
#| ) catch {
#| err => raise wrap_inflate_error(err)
#| }
#| let st = st_result.status
#| let used = st_result.consumed
#| let produced = st_result.produced
#| if produced > 0 {
#| self.update_output_checksum(output[op_before:op_before + produced])
#| self.osize = self.osize + produced.reinterpret_as_uint()
#| }
#| op = op + produced
#| consumed += used
#| guard self.after_body_step(st) is Some(status) else { continue }
#| return { status, consumed, produced: op, }
#| }
#| GzTrailer => {
#| consumed += self.fill_pending(input, consumed, input_end, 8)
#| guard self.available() >= 8 else {
#| guard !self.ending else {
#| raise GzipError(Truncated, "gzip: truncated")
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| self.finish_member_trailer()
#| }
#| GzFinished => return { status: Done, consumed, produced: op, }
#| }
#| }
#|}
#|fn Decoder::advance_header(self : Decoder, count : Int32) -> Unit {
#| if (self.flg & 2) != 0 {
#| for i in 0.. Bytes {
#| guard input.length() != 0 else { return b"" }
#| let copy = FixedArray::make(input.length(), b'\x00')
#| copy.blit_from_bytes(0, input.data(), input.start_offset(), input.length())
#| copy.unsafe_reinterpret_as_bytes()
#|}
),
"encoder.mbt": (
#|let gzip_header_bytes : FixedArray[Byte] = [
#| 0x1F, 0x8B, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
#|]
#|priv enum EncMode {
#| Header // draining the gzip header
#| Body // draining DEFLATE blocks from the inner IncrementalEncoder
#| Trailer // draining the CRC-32 + ISIZE trailer
#| Finished
#|}
#|struct Encoder {
#| def : @flate.IncrementalEncoder
#| mut crc : @checksum.Crc32
#| mut isize : UInt32 // total input length, mod 2^32 (gzip ISIZE)
#| mut mode : EncMode
#| mut stage : FixedArray[Byte] // header or trailer bytes being drained
#| mut stage_pos : Int32
#| mut pending_action : @flate.DeflateAction
#|}
#|pub fn Encoder::Encoder(
#| level~ : Int32 = 6,
#| fast_store~ : Bool = false,
#|) -> Encoder {
#| {
#| def: IncrementalEncoder(level~, fast_store~),
#| crc: Crc32(),
#| isize: 0,
#| mode: Header,
#| stage: gzip_header_bytes,
#| stage_pos: 0,
#| pending_action: Continue,
#| }
#|}
#|pub fn Encoder::reset(self : Encoder) -> Unit {
#| self.def.reset()
#| self.crc = Crc32()
#| self.isize = 0
#| self.mode = Header
#| self.stage = gzip_header_bytes
#| self.stage_pos = 0
#| self.pending_action = Continue
#|}
#|pub fn Encoder::is_finished(self : Encoder) -> Bool {
#| self.mode is Finished
#|}
#|fn Encoder::build_trailer(self : Encoder) -> FixedArray[Byte] {
#| let crc = self.crc.finish()
#| let isize = self.isize
#| let t = FixedArray::make(8, b'\x00')
#| t.unsafe_write_uint32_le(0, crc)
#| t.unsafe_write_uint32_le(4, isize)
#| t
#|}
#|pub fn Encoder::step(
#| self : Encoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| action~ : @flate.DeflateAction = Continue,
#|) -> @flate.StepResult {
#| if self.mode is Finished {
#| return { status: Done, consumed: 0, produced: 0, }
#| }
#| let mut consumed = 0
#| if !(self.pending_action is Finish) {
#| let scratch = FixedArray::make(0, b'\x00').mut_view()
#| let accepted = self.def.step(input, scratch, action~).consumed
#| consumed = accepted
#| if accepted > 0 {
#| self.crc.update(input[:accepted])
#| self.isize = self.isize + accepted.reinterpret_as_uint()
#| }
#| if accepted == input.length() {
#| guard action is Continue else { self.pending_action = action }
#| }
#| }
#| let out_end = output.length()
#| let mut op = 0
#| for ;; {
#| match self.mode {
#| Header | Trailer => {
#| while self.stage_pos < self.stage.length() && op < out_end {
#| output.set(op, self.stage[self.stage_pos])
#| op = op + 1
#| self.stage_pos = self.stage_pos + 1
#| }
#| if self.stage_pos < self.stage.length() {
#| return { status: NeedMoreOutput, consumed, produced: op, }
#| }
#| self.mode = if self.mode is Header { Body } else { Finished }
#| }
#| Body => {
#| let st_result = self.def.step(
#| b"",
#| output.mut_view(start=op),
#| action=self.pending_action,
#| )
#| let st = st_result.status
#| let p = st_result.produced
#| op = op + p
#| if st is NeedMoreInput {
#| if self.pending_action is SyncFlush {
#| self.pending_action = Continue
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| if st is NeedMoreOutput {
#| return { status: NeedMoreOutput, consumed, produced: op, }
#| }
#| self.stage = self.build_trailer()
#| self.stage_pos = 0
#| self.mode = Trailer
#| }
#| Finished => return { status: Done, consumed, produced: op, }
#| }
#| }
#|}
),
"extends.mbt": (
#|pub extend GzipErrorKind with Eq::{equal}
#|pub extend GzipError with Show::{to_string}
),
"gzip.mbt": (
#|pub using @flate {
#| type Strategy,
#| type Status,
#| type StepResult,
#| type DeflateAction,
#|}
#|#derive(Eq, @debug.Debug)
#|#non_exhaustive
#|pub(all) enum GzipErrorKind {
#| Truncated
#| InvalidHeader
#| InvalidDeflate
#| ChecksumMismatch
#| LengthMismatch
#| OutputLimitExceeded(limit~ : UInt64, observed~ : UInt64)
#| Cancelled
#| InvalidArgument
#|}
#|#derive(@debug.Debug)
#|pub suberror GzipError {
#| GzipError(GzipErrorKind, String)
#|}
#|pub impl Show for GzipError with fn output(self, logger) {
#| let GzipError(kind, message) = self
#| match kind {
#| OutputLimitExceeded(limit~, observed~) =>
#| logger <+ "OutputLimitExceeded(limit=\{limit}, observed=\{observed})"
#| _ => logger.write_string(@debug.to_string(kind))
#| }
#| logger <+ ": \{message}"
#|}
#|fn wrap_inflate_error(err : @flate.InflateError) -> GzipError {
#| let InflateError(kind, message) = err
#| match kind {
#| OutputLimitExceeded(limit~, observed~) =>
#| GzipError(OutputLimitExceeded(limit~, observed~), message)
#| Cancelled => GzipError(Cancelled, message)
#| InvalidArgument => GzipError(InvalidArgument, message)
#| _ => GzipError(InvalidDeflate, message)
#| }
#|}
#|fn check_cancelled(cancelled : (() -> Bool)?) -> Unit raise GzipError {
#| if cancelled is Some(callback) && callback() {
#| raise GzipError(Cancelled, "gzip: cancelled by caller")
#| }
#|}
#|fn crc32_cancellable(
#| input : BytesView,
#| cancelled : (() -> Bool)?,
#|) -> UInt32 raise GzipError {
#| guard cancelled is Some(_) else { return @checksum.crc32(input) }
#| let crc = @checksum.Crc32()
#| let mut start = 0
#| while start < input.length() {
#| check_cancelled(cancelled)
#| let end = start + (input.length() - start).min(4096)
#| crc.update(input[start:end])
#| start = end
#| }
#| check_cancelled(cancelled)
#| crc.finish()
#|}
#|fn u32_le(data : BytesView, off : Int32) -> UInt32 {
#| data[off].to_uint() |
#| (data[off + 1].to_uint() << 8) |
#| (data[off + 2].to_uint() << 16) |
#| (data[off + 3].to_uint() << 24)
#|}
#|let gzip_header : Bytes = b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff"
#|const IN_PLACE_MAX_INPUT : Int32 = 0x7000_0000
#|fn emits_stored(strategy : @flate.Strategy) -> Bool {
#| match strategy {
#| Standard(level~) | Split(level~) => level <= 0
#| Optimal(_) => false
#| }
#|}
#|fn frame_length(deflated : Int32, overhead : Int32) -> Int32 {
#| guard deflated <= 0x7FFF_FFFF - overhead else {
#| abort("gzip: member exceeds the Int range")
#| }
#| deflated + overhead
#|}
#|fn gzip_frame(
#| input : BytesView,
#| strategy : @flate.Strategy,
#| fast_store : Bool,
#|) -> Bytes {
#| let header_size = gzip_header.length()
#| let length = input.length()
#| if emits_stored(strategy) && length <= IN_PLACE_MAX_INPUT {
#| let stored = @flate.deflate_bound(length, strategy=Standard(level=0))
#| let out = FixedArray::make(header_size + stored + 8, b'\x00')
#| let body = out.mut_view(start=header_size, end=header_size + stored)
#| let compressor = @flate.Compressor(strategy~, fast_store~)
#| if compressor.compress_into(input, body) is Some(deflated) &&
#| deflated == stored {
#| out.blit_from_bytes(0, gzip_header, 0, header_size)
#| put_trailer(out, header_size + stored, input)
#| return out.unsafe_reinterpret_as_bytes()
#| }
#| }
#| let deflated = @flate.deflate_all(input, strategy~, fast_store~)
#| let out = FixedArray::make(
#| frame_length(deflated.length(), header_size + 8),
#| b'\x00',
#| )
#| out.blit_from_bytes(0, gzip_header, 0, header_size)
#| out.blit_from_bytes(header_size, deflated, 0, deflated.length())
#| put_trailer(out, header_size + deflated.length(), input)
#| out.unsafe_reinterpret_as_bytes()
#|}
#|fn put_trailer(
#| out : FixedArray[Byte],
#| offset : Int32,
#| input : BytesView,
#|) -> Unit {
#| let crc = @checksum.crc32(input)
#| let isize = input.length().reinterpret_as_uint()
#| for i in 0..<4 {
#| out[offset + i] = (crc >> (8 * i)).to_byte()
#| out[offset + 4 + i] = (isize >> (8 * i)).to_byte()
#| }
#|}
#|pub fn compress(
#| input : BytesView,
#| strategy~ : @flate.Strategy = Standard(level=6),
#| fast_store~ : Bool = false,
#|) -> Bytes {
#| gzip_frame(input, strategy, fast_store)
#|}
#|fn inflate_member(
#| input : BytesView,
#| q : Int32,
#| decoder : @flate.Decompressor,
#| max_output_bytes : Int32,
#| cancelled : (() -> Bool)?,
#|) -> @flate.Decompressed raise GzipError {
#| decoder.decompress(input[q:], mode=Prefix, max_output_bytes~, cancelled?) catch {
#| InflateError(Truncated, _) => raise GzipError(Truncated, "gzip: truncated")
#| err => raise wrap_inflate_error(err)
#| }
#|}
#|fn gunzip_member_magic(input : BytesView, p : Int32, n : Int32) -> Bool {
#| input[p].to_int() == 0x1F && (p + 1 >= n || input[p + 1].to_int() == 0x8B)
#|}
#|fn gunzip_skip_nul_terminated(
#| input : BytesView,
#| q : Int32,
#| cancelled : (() -> Bool)?,
#|) -> Int32 raise GzipError {
#| let mut q = q
#| while q < input.length() && input[q].to_int() != 0 {
#| if (q & 4095) == 0 {
#| check_cancelled(cancelled)
#| }
#| q = q + 1
#| }
#| q + 1
#|}
#|fn gunzip_optional_fields_end(
#| input : BytesView,
#| q : Int32,
#| flg : Int32,
#| cancelled : (() -> Bool)?,
#|) -> Int32 raise GzipError {
#| let header_start = q - 10
#| let mut q = q
#| if (flg & 4) != 0 { // FEXTRA
#| let xlen = input[q].to_int() | (input[q + 1].to_int() << 8)
#| q = q + 2 + xlen
#| }
#| if (flg & 8) != 0 { // FNAME (NUL-terminated)
#| q = gunzip_skip_nul_terminated(input, q, cancelled)
#| }
#| if (flg & 16) != 0 { // FCOMMENT (NUL-terminated)
#| q = gunzip_skip_nul_terminated(input, q, cancelled)
#| }
#| if (flg & 2) != 0 { // FHCRC
#| guard q <= input.length() - 2 else {
#| raise GzipError(Truncated, "gzip: truncated header CRC")
#| }
#| let expected = input[q].to_uint() | (input[q + 1].to_uint() << 8)
#| guard (crc32_cancellable(input[header_start:q], cancelled) & 0xffff) ==
#| expected else {
#| raise GzipError(ChecksumMismatch, "gzip: header CRC-16 mismatch")
#| }
#| q = q + 2
#| }
#| q
#|}
#|fn gunzip_decode_member(
#| input : BytesView,
#| p : Int32,
#| n : Int32,
#| decoder : @flate.Decompressor,
#| max_output_bytes : Int32,
#| cancelled : (() -> Bool)?,
#|) -> (Bytes, Int32) raise GzipError {
#| guard n - p >= 18 else { raise GzipError(Truncated, "gzip: truncated") }
#| guard input[p + 2].to_int() == 0x08 else {
#| raise GzipError(InvalidHeader, "gzip: unsupported compression method")
#| }
#| let flg = input[p + 3].to_int()
#| guard (flg & 0xE0) == 0 else {
#| raise GzipError(InvalidHeader, "gzip: reserved flags are set")
#| }
#| let q = gunzip_optional_fields_end(input, p + 10, flg, cancelled)
#| guard q <= n - 8 else { raise GzipError(Truncated, "gzip: truncated") }
#| let result = inflate_member(input, q, decoder, max_output_bytes, cancelled)
#| let payload = result.data
#| let trailer = q + result.consumed
#| guard trailer <= n - 8 else { raise GzipError(Truncated, "gzip: truncated") }
#| guard crc32_cancellable(payload, cancelled) == u32_le(input, trailer) else {
#| raise GzipError(ChecksumMismatch, "gzip: CRC-32 mismatch")
#| }
#| guard payload.length().reinterpret_as_uint() == u32_le(input, trailer + 4) else {
#| raise GzipError(LengthMismatch, "gzip: length mismatch")
#| }
#| (payload, trailer + 8)
#|}
#|#label_migration(max_output_bytes, alias=max_output, msg="Use max_output_bytes= instead")
#|pub fn decompress(
#| input : BytesView,
#| max_output_bytes? : Int32,
#| cancelled? : () -> Bool,
#|) -> Bytes raise GzipError {
#| let limit = max_output_bytes.unwrap_or(0x7fff_ffff)
#| guard limit >= 0 else {
#| raise GzipError(InvalidArgument, "max_output_bytes must be non-negative")
#| }
#| check_cancelled(cancelled)
#| let n = input.length()
#| guard n >= 18 else { raise GzipError(Truncated, "gzip: input too short") }
#| let out = @buffer.Buffer()
#| let decoder = @flate.Decompressor()
#| let mut p = 0
#| let mut total = 0
#| for ;; {
#| guard p < n else { break }
#| guard gunzip_member_magic(input, p, n) else {
#| guard p == 0 else { break }
#| raise GzipError(InvalidHeader, "gzip: bad magic")
#| }
#| check_cancelled(cancelled)
#| let (payload, next_p) = gunzip_decode_member(
#| input,
#| p,
#| n,
#| decoder,
#| limit - total,
#| cancelled,
#| ) catch {
#| GzipError(OutputLimitExceeded(limit=_, observed~), message) =>
#| raise GzipError(
#| OutputLimitExceeded(
#| limit=limit.to_uint64(),
#| observed=total.to_uint64() + observed,
#| ),
#| message,
#| )
#| error => raise error
#| }
#| total += payload.length()
#| if p == 0 && next_p == n {
#| return payload
#| }
#| if cancelled is Some(_) {
#| let mut start = 0
#| while start < payload.length() {
#| check_cancelled(cancelled)
#| let end = start + (payload.length() - start).min(4096)
#| out.write_bytesview(payload[start:end])
#| start = end
#| }
#| } else {
#| out.write_bytesview(payload)
#| }
#| p = next_p
#| }
#| check_cancelled(cancelled)
#| out.to_bytes()
#|}
),
"stream_decoder.mbt": (
#|struct StreamDecoder[R] {
#| inner : R
#| core : Decoder
#| multistream : Bool
#| ctrl : @io.BinaryReaderController
#|}
#|pub fn[R] StreamDecoder::StreamDecoder(
#| inner : R,
#| multistream~ : Bool = false,
#|) -> StreamDecoder[R] {
#| { inner, core: Decoder(), multistream, ctrl: BinaryReaderController(), }
#|}
#|async fn[R : @io.BinaryReader] StreamDecoder::start_next_member(
#| self : StreamDecoder[R],
#|) -> Bool {
#| guard self.multistream else { return false }
#| match self.inner.peek() {
#| None => false
#| Some(prefix) => {
#| guard prefix.length() > 0 && prefix[0].to_int() == 0x1F else {
#| return false
#| }
#| let prefix = if prefix.length() >= 2 {
#| prefix
#| } else {
#| self.inner.peek_at_least(2) catch {
#| UnexpectedEof => return false
#| err => raise err
#| }
#| }
#| guard prefix[1].to_int() == 0x8B else { return false }
#| self.core.reset()
#| true
#| }
#| }
#|}
#|pub impl[R : @io.BinaryReader] @io.BinaryReader for StreamDecoder[R]
#|impl[R : @io.BinaryReader] @io.BinaryReaderImpl for StreamDecoder[R] with fn name(
#| _self,
#|) {
#| "@gzip.StreamDecoder"
#|}
#|impl[R : @io.BinaryReader] @io.BinaryReaderImpl for StreamDecoder[R] with fn get_controller(
#| self,
#|) {
#| self.ctrl
#|}
#|impl[R : @io.BinaryReader] @io.BinaryReaderImpl for StreamDecoder[R] with fn raw_read_impl(
#| self,
#| dst,
#| offset~,
#| max_len~,
#|) {
#| let output = dst.mut_view(start=offset, end=offset + max_len)
#| for ;; {
#| let drained = self.core.step(b"", output)
#| if drained.produced > 0 {
#| return drained.produced
#| }
#| match drained.status {
#| Done => {
#| if self.start_next_member() {
#| continue
#| }
#| return 0
#| }
#| NeedMoreOutput => panic()
#| NeedMoreInput => ()
#| }
#| match self.inner.peek() {
#| None =>
#| match self.core.check_end() {
#| Done => return 0
#| NeedMoreOutput => continue
#| NeedMoreInput => panic()
#| }
#| Some(input) => {
#| let result = self.core.step(input, output)
#| self.inner.drop_exactly(result.consumed)
#| if result.produced > 0 {
#| return result.produced
#| }
#| match result.status {
#| Done => {
#| if self.start_next_member() {
#| continue
#| }
#| return 0
#| }
#| NeedMoreInput => continue
#| NeedMoreOutput => panic()
#| }
#| }
#| }
#| }
#|}
#|pub extend StreamDecoder with @io.BinaryReader::{
#| read_into,
#| read,
#| read_at_least,
#| peek,
#| peek_at_least,
#| read_exactly,
#| drop_exactly,
#| peek_exactly,
#| read_all,
#| drop_all,
#| read_until,
#| peek_until,
#|}
),
"stream_encoder.mbt": (
#|const STREAM_ENCODER_BUFFER_SIZE : Int32 = 4096
#|struct StreamEncoder[W] {
#| inner : W
#| buf : FixedArray[Byte]
#| mut buf_len : Int32
#| core : Encoder
#| ctrl : @io.BinaryWriterController
#|}
#|pub fn[W] StreamEncoder::StreamEncoder(
#| inner : W,
#| level~ : Int32 = 6,
#| fast_store~ : Bool = false,
#|) -> StreamEncoder[W] {
#| {
#| inner,
#| buf: FixedArray::make(STREAM_ENCODER_BUFFER_SIZE, b'\x00'),
#| buf_len: 0,
#| core: Encoder(level~, fast_store~),
#| ctrl: BinaryWriterController(),
#| }
#|}
#|async fn[W : @io.BinaryWriter] StreamEncoder::flush_output(
#| self : StreamEncoder[W],
#|) -> Unit {
#| guard self.buf_len > 0 else { return }
#| self.inner.write(self.buf.unsafe_reinterpret_as_bytes()[:self.buf_len])
#| self.buf_len = 0
#|}
#|async fn[W : @io.BinaryWriter] StreamEncoder::sync_flush_core(
#| self : StreamEncoder[W],
#|) -> Unit {
#| guard !self.core.is_finished() else {
#| self.flush_output()
#| return
#| }
#| for ;; {
#| let result = self.core.step(
#| b"",
#| self.buf.mut_view(start=self.buf_len),
#| action=SyncFlush,
#| )
#| self.buf_len += result.produced
#| match result.status {
#| NeedMoreOutput => self.flush_output()
#| NeedMoreInput => {
#| self.flush_output()
#| return
#| }
#| Done => {
#| self.flush_output()
#| return
#| }
#| }
#| }
#|}
#|pub async fn[W : @io.BinaryWriter] StreamEncoder::end(
#| self : StreamEncoder[W],
#|) -> Unit {
#| guard !self.core.is_finished() else {
#| self.flush_output()
#| return
#| }
#| for ;; {
#| let result = self.core.step(
#| b"",
#| self.buf.mut_view(start=self.buf_len),
#| action=Finish,
#| )
#| self.buf_len += result.produced
#| match result.status {
#| NeedMoreOutput => self.flush_output()
#| NeedMoreInput => ()
#| Done => {
#| self.flush_output()
#| return
#| }
#| }
#| }
#|}
#|pub impl[W : @io.BinaryWriter] @io.BinaryWriter for StreamEncoder[W]
#|impl[W : @io.BinaryWriter] @io.BinaryWriterImpl for StreamEncoder[W] with fn raw_write_impl(
#| self,
#| buf,
#| offset~,
#| len~,
#|) {
#| guard! !self.core.is_finished()
#| let input = buf[offset:offset + len]
#| for ;; {
#| if self.buf_len == self.buf.length() {
#| self.flush_output()
#| }
#| let result = self.core.step(
#| input,
#| self.buf.mut_view(start=self.buf_len),
#| action=Continue,
#| )
#| self.buf_len += result.produced
#| if result.consumed > 0 {
#| return result.consumed
#| }
#| match result.status {
#| NeedMoreOutput => self.flush_output()
#| NeedMoreInput if result.produced > 0 => continue
#| NeedMoreInput => panic()
#| Done => panic()
#| }
#| }
#|}
#|impl[W : @io.BinaryWriter] @io.BinaryWriterImpl for StreamEncoder[W] with fn name(
#| _self,
#|) {
#| "@gzip.StreamEncoder"
#|}
#|impl[W : @io.BinaryWriter] @io.BinaryWriterImpl for StreamEncoder[W] with fn get_controller(
#| self,
#|) {
#| self.ctrl
#|}
#|impl[W : @io.BinaryWriter] @io.BinaryWriterImpl for StreamEncoder[W] with fn flush_partial_data(
#| self,
#|) {
#| self.sync_flush_core()
#| @io.BinaryWriter::flush_partial_data(self.inner)
#|}
#|pub extend StreamEncoder with @io.BinaryWriter::{
#| write,
#| write_reader,
#| flush_partial_data,
#|}
),
},
)
///|
let moonbitlang_core_compress_zlib_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/compress/zlib",
deps={},
files={
"decoder.mbt": (
#|const DECODER_INPUT_LIMIT : Int32 = 65536
#|#cfg(any(target="native", target="wasm"))
#|const CHECKSUM_BUFFER_SIZE : Int32 = 4096
#|#cfg(not(any(target="native", target="wasm")))
#|const CHECKSUM_BUFFER_SIZE : Int32 = 0
#|priv enum DecMode {
#| ZHeader
#| ZBody
#| ZTrailer
#| ZFinished
#|}
#|struct Decoder {
#| inf : @flate.IncrementalDecoder
#| adler : @checksum.Adler32 // of the decompressed output
#| checksum_buffer : FixedArray[Byte] // owned batch storage; empty on direct-view backends
#| mut checksum_pending : Int32
#| mut pending : BytesView
#| mut pending_pos : Int32
#| mut borrowed_input : Bool
#| mut unused : Bytes // cached read-ahead suffix after successful completion
#| mut ending : Bool // Final input stays latched across output suspension
#| mut mode : DecMode
#| dict : BytesView? // preset dictionary, if the caller has one
#| mut failure_kind : ZlibErrorKind? // stable first terminal error
#| mut failure_message : String
#|}
#|pub fn Decoder::Decoder(dictionary? : BytesView) -> Decoder {
#| {
#| inf: IncrementalDecoder(),
#| adler: Adler32(),
#| checksum_buffer: FixedArray::make(CHECKSUM_BUFFER_SIZE, b'\x00'),
#| checksum_pending: 0,
#| pending: b"",
#| pending_pos: 0,
#| borrowed_input: false,
#| unused: b"",
#| ending: false,
#| mode: ZHeader,
#| dict: dictionary,
#| failure_kind: None,
#| failure_message: "",
#| }
#|}
#|pub fn Decoder::is_finished(self : Decoder) -> Bool {
#| self.mode is ZFinished
#|}
#|pub fn Decoder::unused_input(self : Decoder) -> Bytes {
#| guard self.mode is ZFinished else { return b"" }
#| self.unused
#|}
#|fn Decoder::pending_length(self : Decoder) -> Int32 {
#| self.pending.length() - self.pending_pos
#|}
#|fn Decoder::finish_stream(self : Decoder) -> Unit {
#| self.unused = copy_input(self.pending[self.pending_pos:])
#| self.pending = b""
#| self.pending_pos = 0
#| self.borrowed_input = false
#| self.mode = ZFinished
#|}
#|fn Decoder::buffer_input(self : Decoder, input : BytesView) -> Unit {
#| guard input.length() != 0 else { return }
#| if self.pending_length() == 0 {
#| self.pending = input
#| self.pending_pos = 0
#| self.borrowed_input = true
#| } else {
#| self.pending = Bytes::from_iter(self.pending.iter().concat(input.iter()))
#| self.borrowed_input = false
#| }
#|}
#|fn Decoder::detach_input(self : Decoder) -> Unit {
#| guard self.borrowed_input else { return }
#| self.pending = copy_input(self.pending[self.pending_pos:])
#| self.pending_pos = 0
#| self.borrowed_input = false
#|}
#|fn Decoder::compact_for_input(self : Decoder) -> Unit {
#| guard self.pending_pos != 0 else { return }
#| self.pending = self.pending[self.pending_pos:]
#| self.pending_pos = 0
#|}
#|fn Decoder::advance(self : Decoder, k : Int32) -> Unit {
#| guard k > 0 else { return }
#| self.pending_pos = self.pending_pos + k
#| guard self.pending_pos < self.pending.length() else {
#| self.pending = b""
#| self.pending_pos = 0
#| self.borrowed_input = false
#| return
#| }
#| let worth_compacting = self.pending_pos >= 4096 &&
#| self.pending_pos >= self.pending_length()
#| guard worth_compacting else { return }
#| self.compact_for_input()
#|}
#|fn Decoder::pending_view(self : Decoder) -> BytesView {
#| self.pending[self.pending_pos:]
#|}
#|fn Decoder::u32_be(self : Decoder, off : Int32) -> UInt32 {
#| let pos = self.pending_pos + off
#| (self.pending[pos].to_uint() << 24) |
#| (self.pending[pos + 1].to_uint() << 16) |
#| (self.pending[pos + 2].to_uint() << 8) |
#| self.pending[pos + 3].to_uint()
#|}
#|fn Decoder::read_zheader(self : Decoder) -> Bool raise ZlibError {
#| guard self.pending_length() >= 2 else {
#| guard !self.ending else {
#| raise ZlibError(Truncated, "zlib: truncated header")
#| }
#| return true
#| }
#| let cmf = self.pending[self.pending_pos].to_int()
#| let flg = self.pending[self.pending_pos + 1].to_int()
#| guard (cmf & 0x0F) == 8 else {
#| raise ZlibError(InvalidHeader, "zlib: unsupported compression method")
#| }
#| guard cmf >> 4 <= 7 else {
#| raise ZlibError(InvalidHeader, "zlib: invalid window size")
#| }
#| guard (cmf * 256 + flg) % 31 == 0 else {
#| raise ZlibError(InvalidHeader, "zlib: bad header check")
#| }
#| guard (flg & 0x20) == 0 else {
#| guard self.pending_length() >= 6 else {
#| guard !self.ending else {
#| raise ZlibError(Truncated, "zlib: truncated dictionary id")
#| }
#| return true
#| }
#| let dictid = self.u32_be(2)
#| guard self.dict is Some(dict) else {
#| raise ZlibError(DictionaryRequired, "zlib: preset dictionary required")
#| }
#| guard @checksum.adler32(dict) == dictid else {
#| raise ZlibError(DictionaryMismatch, "zlib: dictionary id mismatch")
#| }
#| self.inf.reset(dictionary=dict[:])
#| self.advance(6)
#| self.mode = ZBody
#| return false
#| }
#| self.advance(2)
#| self.mode = ZBody
#| false
#|}
#|fn Decoder::after_inflate_step(
#| self : Decoder,
#| st : @flate.Status,
#|) -> @flate.Status? raise ZlibError {
#| match st {
#| Done => {
#| self.mode = ZTrailer
#| None
#| }
#| NeedMoreInput => {
#| guard !self.ending else {
#| raise ZlibError(Truncated, "zlib: truncated DEFLATE stream")
#| }
#| Some(NeedMoreInput)
#| }
#| NeedMoreOutput => Some(NeedMoreOutput)
#| }
#|}
#|fn Decoder::read_ztrailer(self : Decoder) -> Bool raise ZlibError {
#| guard self.pending_length() >= 4 else {
#| guard !self.ending else {
#| raise ZlibError(Truncated, "zlib: truncated trailer")
#| }
#| return true
#| }
#| self.flush_output_checksum()
#| guard self.adler.finish() == self.u32_be(0) else {
#| raise ZlibError(ChecksumMismatch, "zlib: Adler-32 mismatch")
#| }
#| self.advance(4)
#| self.finish_stream()
#| false
#|}
#|fn Decoder::update_output_checksum(
#| decoder : Decoder,
#| data : ArrayView[Byte],
#|) -> Unit {
#| let capacity = decoder.checksum_buffer.length()
#| guard capacity > 0 else {
#| decoder.adler.update_array(data)
#| return
#| }
#| let mut position = 0
#| while position < data.length() {
#| let count = (data.length() - position).min(
#| capacity - decoder.checksum_pending,
#| )
#| decoder.checksum_buffer
#| .mut_view()
#| .blit_from_array(
#| data[position:position + count],
#| dst_offset=decoder.checksum_pending,
#| )
#| position += count
#| decoder.checksum_pending += count
#| if decoder.checksum_pending == capacity {
#| decoder.flush_output_checksum()
#| }
#| }
#|}
#|fn Decoder::flush_output_checksum(decoder : Decoder) -> Unit {
#| guard decoder.checksum_pending > 0 else { return }
#| decoder.adler.update(
#| decoder.checksum_buffer.unsafe_reinterpret_as_bytes()[:decoder.checksum_pending],
#| )
#| decoder.checksum_pending = 0
#|}
#|pub fn Decoder::step(
#| self : Decoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#|) -> @flate.StepResult raise ZlibError {
#| self.step_impl(input, output, end_of_input=false)
#|}
#|pub fn Decoder::check_end(self : Decoder) -> @flate.Status raise ZlibError {
#| self.step_impl(b"", [].mut_view(), end_of_input=true).status
#|}
#|fn Decoder::step_impl(
#| self : Decoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| end_of_input~ : Bool,
#|) -> @flate.StepResult raise ZlibError {
#| if self.failure_kind is Some(kind) {
#| raise ZlibError(kind, self.failure_message)
#| }
#| defer self.detach_input()
#| self.step_active(input, output, end_of_input~) catch {
#| ZlibError(kind, message) => {
#| self.failure_kind = Some(kind)
#| self.failure_message = message
#| raise ZlibError(kind, message)
#| }
#| }
#|}
#|fn Decoder::step_active(
#| self : Decoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| end_of_input~ : Bool,
#|) -> @flate.StepResult raise ZlibError {
#| if self.mode is ZFinished {
#| return { status: Done, consumed: 0, produced: 0, }
#| }
#| let mut consumed = 0
#| let mut admitted = false
#| let mut op = 0
#| for ;; {
#| if !admitted &&
#| (
#| !(self.mode is ZBody) ||
#| self.pending_length() == 0 ||
#| input.length() == 0
#| ) {
#| if !self.ending && self.pending.length() >= DECODER_INPUT_LIMIT {
#| self.compact_for_input()
#| }
#| let capacity = DECODER_INPUT_LIMIT - self.pending.length()
#| consumed = if self.ending || capacity <= 0 {
#| 0
#| } else {
#| input.length().min(capacity)
#| }
#| if consumed > 0 {
#| self.buffer_input(input[:consumed])
#| }
#| if end_of_input && consumed == input.length() {
#| self.ending = true
#| }
#| admitted = true
#| }
#| match self.mode {
#| ZHeader => {
#| guard !self.read_zheader() else {
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| }
#| ZBody => {
#| let op_before = op
#| let st_result = self.inf.step(
#| self.pending_view(),
#| output.mut_view(start=op),
#| ) catch {
#| InflateError(_, message) =>
#| raise ZlibError(
#| InvalidDeflate,
#| "zlib: invalid DEFLATE stream: \{message}",
#| )
#| }
#| let st = st_result.status
#| let used = st_result.consumed
#| let produced = st_result.produced
#| if produced > 0 {
#| self.update_output_checksum(output[op_before:op_before + produced])
#| }
#| op = op + produced
#| self.advance(used)
#| if st is NeedMoreInput && !admitted {
#| continue
#| }
#| guard self.after_inflate_step(st) is Some(status) else { continue }
#| return { status, consumed, produced: op, }
#| }
#| ZTrailer => {
#| guard !self.read_ztrailer() else {
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| }
#| ZFinished => return { status: Done, consumed, produced: op, }
#| }
#| }
#|}
#|fn copy_input(input : BytesView) -> Bytes {
#| guard input.length() != 0 else { return b"" }
#| let copy = FixedArray::make(input.length(), b'\x00')
#| copy.blit_from_bytes(0, input.data(), input.start_offset(), input.length())
#| copy.unsafe_reinterpret_as_bytes()
#|}
),
"encoder.mbt": (
#|let zlib_header_bytes : FixedArray[Byte] = [0x78, 0x01]
#|priv enum EncMode {
#| Header
#| Body
#| Trailer
#| Finished
#|}
#|struct Encoder {
#| def : @flate.IncrementalEncoder
#| mut adler : @checksum.Adler32
#| mut mode : EncMode
#| mut stage : FixedArray[Byte]
#| mut stage_pos : Int32
#| mut pending_action : @flate.DeflateAction
#|}
#|pub fn Encoder::Encoder(
#| level~ : Int32 = 6,
#| dictionary? : BytesView,
#| fast_store~ : Bool = false,
#|) -> Encoder {
#| let def = if dictionary is Some(dict) {
#| @flate.IncrementalEncoder(level~, dictionary=dict, fast_store~)
#| } else {
#| IncrementalEncoder(level~, fast_store~)
#| }
#| {
#| def,
#| adler: Adler32(),
#| mode: Header,
#| stage: zlib_header(dictionary),
#| stage_pos: 0,
#| pending_action: Continue,
#| }
#|}
#|fn zlib_header(dictionary : BytesView?) -> FixedArray[Byte] {
#| guard dictionary is Some(dict) else { return zlib_header_bytes }
#| let id = @checksum.adler32(dict)
#| let h = FixedArray::make(6, b'\x00')
#| h[0] = b'\x78'
#| h[1] = b'\x20'
#| h[2] = ((id >> 24) & 0xFF).reinterpret_as_int().to_byte()
#| h[3] = ((id >> 16) & 0xFF).reinterpret_as_int().to_byte()
#| h[4] = ((id >> 8) & 0xFF).reinterpret_as_int().to_byte()
#| h[5] = (id & 0xFF).reinterpret_as_int().to_byte()
#| h
#|}
#|pub fn Encoder::reset(self : Encoder, dictionary? : BytesView) -> Unit {
#| match dictionary {
#| Some(dict) => self.def.reset(dictionary=dict)
#| None => self.def.reset()
#| }
#| self.adler = Adler32()
#| self.mode = Header
#| self.stage = zlib_header(dictionary)
#| self.stage_pos = 0
#| self.pending_action = Continue
#|}
#|pub fn Encoder::is_finished(self : Encoder) -> Bool {
#| self.mode is Finished
#|}
#|fn Encoder::build_trailer(self : Encoder) -> FixedArray[Byte] {
#| let adler = self.adler.finish()
#| let t = FixedArray::make(4, b'\x00')
#| t.unsafe_write_uint32_be(0, adler)
#| t
#|}
#|pub fn Encoder::step(
#| self : Encoder,
#| input : BytesView,
#| output : MutArrayView[Byte],
#| action~ : @flate.DeflateAction = Continue,
#|) -> @flate.StepResult {
#| if self.mode is Finished {
#| return { status: Done, consumed: 0, produced: 0, }
#| }
#| let mut consumed = 0
#| if !(self.pending_action is Finish) {
#| let scratch = FixedArray::make(0, b'\x00').mut_view()
#| let accepted = self.def.step(input, scratch, action~).consumed
#| consumed = accepted
#| if accepted > 0 {
#| self.adler.update(input[:accepted])
#| }
#| if accepted == input.length() {
#| guard action is Continue else { self.pending_action = action }
#| }
#| }
#| let out_end = output.length()
#| let mut op = 0
#| for ;; {
#| match self.mode {
#| Header | Trailer => {
#| while self.stage_pos < self.stage.length() && op < out_end {
#| output.set(op, self.stage[self.stage_pos])
#| op = op + 1
#| self.stage_pos = self.stage_pos + 1
#| }
#| if self.stage_pos < self.stage.length() {
#| return { status: NeedMoreOutput, consumed, produced: op, }
#| }
#| self.mode = if self.mode is Header { Body } else { Finished }
#| }
#| Body => {
#| let st_result = self.def.step(
#| b"",
#| output.mut_view(start=op),
#| action=self.pending_action,
#| )
#| let st = st_result.status
#| let p = st_result.produced
#| op = op + p
#| if st is NeedMoreInput {
#| if self.pending_action is SyncFlush {
#| self.pending_action = Continue
#| }
#| return { status: NeedMoreInput, consumed, produced: op, }
#| }
#| if st is NeedMoreOutput {
#| return { status: NeedMoreOutput, consumed, produced: op, }
#| }
#| self.stage = self.build_trailer()
#| self.stage_pos = 0
#| self.mode = Trailer
#| }
#| Finished => return { status: Done, consumed, produced: op, }
#| }
#| }
#|}
),
"extends.mbt": (
#|pub extend ZlibErrorKind with Eq::{equal}
#|pub extend ZlibError with Show::{to_string}
),
"zlib.mbt": (
#|pub using @flate {
#| type Strategy,
#| type Status,
#| type StepResult,
#| type DeflateAction,
#|}
#|#derive(Eq, @debug.Debug)
#|#non_exhaustive
#|pub(all) enum ZlibErrorKind {
#| Truncated
#| InvalidHeader
#| DictionaryRequired
#| DictionaryMismatch
#| InvalidDeflate
#| ChecksumMismatch
#| TrailingData
#| OutputLimitExceeded(limit~ : UInt64, observed~ : UInt64)
#| Cancelled
#| InvalidArgument
#|}
#|#derive(@debug.Debug)
#|pub suberror ZlibError {
#| ZlibError(ZlibErrorKind, String)
#|}
#|pub impl Show for ZlibError with fn output(self, logger) {
#| let ZlibError(kind, message) = self
#| match kind {
#| OutputLimitExceeded(limit~, observed~) =>
#| logger <+ "OutputLimitExceeded(limit=\{limit}, observed=\{observed})"
#| _ => logger.write_string(@debug.to_string(kind))
#| }
#| logger <+ ": \{message}"
#|}
#|fn u32_be(data : BytesView, off : Int32) -> UInt32 {
#| (data[off].to_uint() << 24) |
#| (data[off + 1].to_uint() << 16) |
#| (data[off + 2].to_uint() << 8) |
#| data[off + 3].to_uint()
#|}
#|fn check_cancelled(cancelled : (() -> Bool)?) -> Unit raise ZlibError {
#| if cancelled is Some(callback) && callback() {
#| raise ZlibError(Cancelled, "zlib: cancelled by caller")
#| }
#|}
#|fn adler32_cancellable(
#| input : BytesView,
#| cancelled : (() -> Bool)?,
#|) -> UInt32 raise ZlibError {
#| guard cancelled is Some(_) else { return @checksum.adler32(input) }
#| let adler = @checksum.Adler32()
#| let mut start = 0
#| while start < input.length() {
#| check_cancelled(cancelled)
#| let end = start + (input.length() - start).min(4096)
#| adler.update(input[start:end])
#| start = end
#| }
#| check_cancelled(cancelled)
#| adler.finish()
#|}
#|const IN_PLACE_MAX_INPUT : Int32 = 0x7000_0000
#|fn emits_stored(strategy : @flate.Strategy) -> Bool {
#| match strategy {
#| Standard(level~) | Split(level~) => level <= 0
#| Optimal(_) => false
#| }
#|}
#|fn frame_length(deflated : Int32, overhead : Int32) -> Int32 {
#| guard deflated <= 0x7FFF_FFFF - overhead else {
#| abort("zlib: stream exceeds the Int range")
#| }
#| deflated + overhead
#|}
#|fn zlib_frame(
#| input : BytesView,
#| strategy : @flate.Strategy,
#| dictionary : BytesView?,
#| fast_store : Bool,
#|) -> Bytes {
#| let header = zlib_header(dictionary)
#| let header_size = header.length()
#| let length = input.length()
#| if emits_stored(strategy) && length <= IN_PLACE_MAX_INPUT {
#| let stored = @flate.deflate_bound(length, strategy=Standard(level=0))
#| let out = FixedArray::make(header_size + stored + 4, b'\x00')
#| let body = out.mut_view(start=header_size, end=header_size + stored)
#| let compressor = @flate.Compressor(strategy~, dictionary?, fast_store~)
#| if compressor.compress_into(input, body) is Some(deflated) &&
#| deflated == stored {
#| header.blit_to(out, len=header_size)
#| put_trailer(out, header_size + stored, input)
#| return out.unsafe_reinterpret_as_bytes()
#| }
#| }
#| let deflated = @flate.deflate_all(input, strategy~, dictionary?, fast_store~)
#| let out = FixedArray::make(
#| frame_length(deflated.length(), header_size + 4),
#| b'\x00',
#| )
#| header.blit_to(out, len=header_size)
#| out.blit_from_bytes(header_size, deflated, 0, deflated.length())
#| put_trailer(out, header_size + deflated.length(), input)
#| out.unsafe_reinterpret_as_bytes()
#|}
#|fn put_trailer(
#| out : FixedArray[Byte],
#| offset : Int32,
#| input : BytesView,
#|) -> Unit {
#| let adler = @checksum.adler32(input)
#| for i in 0..<4 {
#| out[offset + i] = (adler >> (24 - 8 * i)).to_byte()
#| }
#|}
#|pub fn compress(
#| input : BytesView,
#| strategy~ : @flate.Strategy = Standard(level=6),
#| dictionary? : BytesView,
#| fast_store~ : Bool = false,
#|) -> Bytes {
#| zlib_frame(input, strategy, dictionary, fast_store)
#|}
#|#label_migration(max_output_bytes, alias=max_output, msg="Use max_output_bytes= instead")
#|pub fn decompress(
#| input : BytesView,
#| dictionary? : BytesView,
#| max_output_bytes? : Int32,
#| cancelled? : () -> Bool,
#|) -> Bytes raise ZlibError {
#| if max_output_bytes is Some(limit) && limit < 0 {
#| raise ZlibError(InvalidArgument, "max_output_bytes must be non-negative")
#| }
#| check_cancelled(cancelled)
#| let n = input.length()
#| guard n >= 6 else { raise ZlibError(Truncated, "zlib: input too short") }
#| let cmf = input[0].to_int()
#| let flg = input[1].to_int()
#| guard (cmf & 0x0F) == 8 else {
#| raise ZlibError(InvalidHeader, "zlib: unsupported compression method")
#| }
#| guard cmf >> 4 <= 7 else {
#| raise ZlibError(InvalidHeader, "zlib: invalid window size")
#| }
#| guard (cmf * 256 + flg) % 31 == 0 else {
#| raise ZlibError(InvalidHeader, "zlib: bad header check")
#| }
#| let dict = if (flg & 0x20) != 0 {
#| guard dictionary is Some(dict) else {
#| raise ZlibError(DictionaryRequired, "zlib: preset dictionary required")
#| }
#| guard n >= 10 else {
#| raise ZlibError(Truncated, "zlib: truncated dictionary ID")
#| }
#| guard adler32_cancellable(dict, cancelled) == u32_be(input, 2) else {
#| raise ZlibError(DictionaryMismatch, "zlib: dictionary ID mismatch")
#| }
#| Some(dict)
#| } else {
#| None
#| }
#| let payload_start = if dict is Some(_) { 6 } else { 2 }
#| let out = @flate.inflate_exact(
#| input[payload_start:n - 4],
#| dictionary?=dict,
#| max_output_bytes?,
#| cancelled?,
#| ) catch {
#| error => raise wrap_inflate_error(error)
#| }
#| guard adler32_cancellable(out, cancelled) == u32_be(input, n - 4) else {
#| raise ZlibError(ChecksumMismatch, "zlib: Adler-32 mismatch")
#| }
#| out
#|}
#|fn wrap_inflate_error(error : @flate.InflateError) -> ZlibError {
#| let InflateError(kind, message) = error
#| match kind {
#| Truncated => ZlibError(Truncated, "zlib: truncated DEFLATE stream")
#| TrailingData =>
#| ZlibError(TrailingData, "zlib: data between DEFLATE stream and trailer")
#| OutputLimitExceeded(limit~, observed~) =>
#| ZlibError(OutputLimitExceeded(limit~, observed~), message)
#| Cancelled => ZlibError(Cancelled, message)
#| InvalidArgument => ZlibError(InvalidArgument, message)
#| _ => ZlibError(InvalidDeflate, message)
#| }
#|}
),
},
)
///|
let moonbitlang_core_debug_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/debug",
deps={},
files={
"buffer.mbt": (
#|pub impl Debug for @buffer.Buffer with fn to_repr(self) {
#| Repr::opaque_("Buffer", Repr(self.to_bytes().to_array()))
#|}
),
"debug.mbt": (
#|pub(open) trait Debug {
#| #as_free_fn(deprecated="Use `Repr(x)` instead")
#| fn to_repr(Self) -> Repr
#|}
#|pub impl Debug for Int32 with fn to_repr(self) {
#| Repr::integer(self.to_string())
#|}
#|pub impl Debug for @int16.Int16 with fn to_repr(self) {
#| Repr::integer(self.to_string())
#|}
#|pub impl Debug for Int64 with fn to_repr(self) {
#| Repr::integer(self.to_string())
#|}
#|pub impl Debug for UInt16 with fn to_repr(self) {
#| Repr::integer(self.to_string())
#|}
#|pub impl Debug for UInt32 with fn to_repr(self) {
#| Repr::integer(self.to_string())
#|}
#|pub impl Debug for UInt64 with fn to_repr(self) {
#| Repr::integer(self.to_string())
#|}
#|pub impl Debug for Double with fn to_repr(self) {
#| Repr::double(self)
#|}
#|pub impl Debug for @float.Float with fn to_repr(self) {
#| Repr::float(self)
#|}
#|pub impl Debug for Bool with fn to_repr(self) {
#| Repr::bool(self)
#|}
#|pub impl Debug for Byte with fn to_repr(self) {
#| Repr::literal("0x" + self.to_hex())
#|}
#|pub impl Debug for Char with fn to_repr(self) {
#| Repr::char(self)
#|}
#|pub impl Debug for String with fn to_repr(self) {
#| Repr::string(self)
#|}
#|pub impl Debug for StringView with fn to_repr(self) {
#| Repr::opaque_("StringView", Repr::string(self.to_owned()))
#|}
#|pub impl Debug for Bytes with fn to_repr(self) {
#| Repr::opaque_("Bytes", Repr::array(self.to_array().map(x => Repr(x))))
#|}
#|pub impl Debug for BytesView with fn to_repr(self) {
#| Repr::opaque_("BytesView", Repr::array(self.to_array().map(x => Repr(x))))
#|}
#|pub impl Debug for Unit with fn to_repr(_) {
#| Repr::unit()
#|}
#|pub impl[T : Debug] Debug for Array[T] with fn to_repr(self) {
#| Repr::array(self.map(x => Repr(x)))
#|}
#|pub impl[T : Debug] Debug for ArrayView[T] with fn to_repr(self) {
#| Repr::opaque_("ArrayView", Repr::array(self.map(x => Repr(x))))
#|}
#|pub impl[T : Debug] Debug for FixedArray[T] with fn to_repr(self) {
#| let view : ArrayView[T] = self
#| Repr::opaque_("FixedArray", Repr::array(view.map(x => Repr(x))))
#|}
#|pub impl[T : Debug] Debug for ReadOnlyArray[T] with fn to_repr(self) {
#| let view : ArrayView[T] = self
#| Repr::opaque_("ReadOnlyArray", Repr::array(view.map(x => Repr(x))))
#|}
#|pub impl[T : Debug] Debug for T? with fn to_repr(self) {
#| match self {
#| None => Repr::ctor("None", [])
#| Some(x) => Repr::ctor("Some", [(None, Repr(x))])
#| }
#|}
#|pub impl[T : Debug, E : Debug] Debug for Result[T, E] with fn to_repr(self) {
#| match self {
#| Ok(x) => Repr::ctor("Ok", [(None, Repr(x))])
#| Err(e) => Repr::ctor("Err", [(None, Repr(e))])
#| }
#|}
#|pub impl[A] Debug for Iter[A] with fn to_repr(_) {
#| Repr::opaque_("Iter", Omitted)
#|}
#|pub impl[A, B] Debug for Iter2[A, B] with fn to_repr(_) {
#| Repr::opaque_("Iter2", Omitted)
#|}
#|pub impl[T : Debug] Debug for MutArrayView[T] with fn to_repr(self) {
#| Repr::opaque_("MutArrayView", Repr::array(self[:].map(x => Repr(x))))
#|}
#|pub impl Debug for StringBuilder with fn to_repr(self) {
#| Repr::string(self.to_string())
#|}
#|pub impl[K : Debug, V : Debug] Debug for Map[K, V] with fn to_repr(self) {
#| Repr::map([ for k, v in self => (Repr(k), Repr(v)) ])
#|}
#|pub impl Debug for SourceLoc with fn to_repr(self) {
#| Repr::opaque_("SourceLoc", Repr::string(self.to_string()))
#|}
#|#callsite(autofill(args_loc, loc))
#|#alias(inspect, deprecated="use `debug_inspect` without package name instead")
#|pub fn debug_inspect(
#| obj : &Debug,
#| content? : String,
#| loc~ : SourceLoc,
#| args_loc~ : ArgsLoc,
#|) -> Unit raise InspectError {
#| let actual = render_pruned(obj.to_repr(), None)
#| let want = content.unwrap_or("")
#| if actual != want {
#| let loc = loc.to_json_string()
#| let args_loc = args_loc.to_json()
#| raise InspectError(
#| (
#| $|@EXPECT_FAILED {"loc": \{loc}, "args_loc": \{args_loc}, "expect": \{want.escape()}, "actual": \{actual.escape()} }
#| ),
#| )
#| }
#|}
#|#callsite(autofill(loc))
#|pub fn[T : Eq + Debug] assert_eq(
#| a : T,
#| b : T,
#| msg? : String,
#| loc~ : SourceLoc,
#|) -> Unit raise {
#| if a != b {
#| let fail_msg = match msg {
#| Some(msg) => msg
#| None => {
#| let repr_a = Repr(a)
#| let repr_b = Repr(b)
#| let diff = pretty_print_delta(diff_repr(repr_a, repr_b), use_ansi=false)
#| (
#| $|`\{render_pruned(repr_a, None)} != \{render_pruned(repr_b, None)}`
#| $|diff:
#| $|\{diff}
#| )
#| }
#| }
#| fail(fail_msg, loc~)
#| }
#|}
#|test "core builtin Debug implementations" {
#| debug_inspect(@int16.Int16(1), content="1")
#| debug_inspect(UInt16(1), content="1")
#| debug_inspect(UInt64(1), content="1")
#| debug_inspect(@float.Float(1.0), content="1")
#| let b : Bytes = b"ab"
#| debug_inspect(b, content="")
#| let bv : BytesView = b
#| debug_inspect(bv, content="")
#| let sv : StringView = "abc"
#| debug_inspect(
#| sv,
#| content=(
#| #|
#| ),
#| )
#| let av : ArrayView[Int32] = [1, 2, 3][1:3]
#| debug_inspect(av, content="")
#| let fa : FixedArray[Int32] = [1, 2, 3]
#| debug_inspect(fa, content="")
#| let ro : ReadOnlyArray[Int32] = [1, 2, 3]
#| debug_inspect(ro, content="")
#| let mv : MutArrayView[Int32] = [1, 2, 3].mut_view(start=1, end=3)
#| debug_inspect(mv, content="")
#| debug_inspect((Ok(1) : Result[Int32, String]), content="Ok(1)")
#| debug_inspect((Err("e") : Result[Int32, String]), content="Err(\"e\")")
#|}
#|#callsite(autofill(loc))
#|#deprecated("This function is for debugging only and should not be used in production", skip_current_package=true)
#|pub fn[T : Debug] dump(t : T, name? : String, loc~ : SourceLoc) -> T {
#| let name = name.unwrap_or("")
#| println("dump(\{name}@\{loc}) = \{to_string(t)}")
#| t
#|}
#|test "dump" {
#| let x = 42
#| if false {
#| dump(()) // never reached here
#| assert_eq(dump(x) + x, 84)
#| }
#|}
#|pub impl Debug for InspectError with fn to_repr(self) {
#| match self {
#| InspectError(msg) => Repr::ctor("InspectError", [(None, Repr::string(msg))])
#| }
#|}
#|pub impl Debug for Failure with fn to_repr(self) {
#| match self {
#| Failure(msg) => Repr::ctor("Failure", [(None, Repr::string(msg))])
#| }
#|}
#|pub impl Debug for SnapshotError with fn to_repr(self) {
#| match self {
#| SnapshotError(msg) =>
#| Repr::ctor("SnapshotError", [(None, Repr::string(msg))])
#| }
#|}
#|#warnings("-deprecated")
#|pub impl Debug for BenchError with fn to_repr(self) {
#| match self {
#| BenchError(msg) => Repr::ctor("BenchError", [(None, Repr::string(msg))])
#| }
#|}
#|pub impl Debug for ArgsLoc with fn to_repr(self) {
#| let ArgsLoc(arr) = self
#| Repr::opaque_("ArgsLoc", Repr(arr))
#|}
#|pub impl Debug for Hasher with fn to_repr(_) {
#| Repr::opaque_("Hasher", Repr::omitted())
#|}
#|pub impl[T] Debug for UninitializedArray[T] with fn to_repr(_) {
#| Repr::opaque_("UninitializedArray", Repr::omitted())
#|}
#|test "Debug for builtin errors" {
#| debug_inspect(
#| InspectError::InspectError("inspect msg"),
#| content="InspectError(\"inspect msg\")",
#| )
#| debug_inspect(
#| Failure::Failure("failure msg"),
#| content="Failure(\"failure msg\")",
#| )
#| debug_inspect(
#| SnapshotError::SnapshotError("snapshot msg"),
#| content="SnapshotError(\"snapshot msg\")",
#| )
#|}
),
"delta.mbt": (
#|priv enum ReprDelta {
#| Same(Repr, Array[ReprDelta])
#| Different(Repr, Repr)
#| Extra1(Repr)
#| Extra2(Repr)
#|}
#|const DEFAULT_MAX_RELATIVE_ERROR : Double = 0.000000000001
#|fn relative_error(x : Double, y : Double) -> Double {
#| if x == y {
#| 0.0
#| } else {
#| let denom = Double::max(x.abs(), y.abs())
#| if denom == 0.0 {
#| (x - y).abs()
#| } else {
#| (x - y).abs() / denom
#| }
#| }
#|}
#|fn info_approx_eq(max_relative_error : Double, x : Repr, y : Repr) -> Bool {
#| match (x, y) {
#| (UnitLit, UnitLit) => true
#| (Integer(x1), Integer(y1)) => x1 == y1
#| (DoubleLit(x1), DoubleLit(y1)) =>
#| relative_error(x1, y1) <= max_relative_error
#| (FloatLit(x1), FloatLit(y1)) => x1 == y1
#| (BoolLit(x1), BoolLit(y1)) => x1 == y1
#| (CharLit(x1), CharLit(y1)) => x1 == y1
#| (StringLit(x1), StringLit(y1)) => x1 == y1
#| (Tuple(_), Tuple(_)) => true
#| (Array(_), Array(_)) => true
#| (Record(_), Record(_)) => true
#| (Enum(n1, _), Enum(n2, _)) => n1 == n2
#| (Map(_), Map(_)) => true
#| (RecordField(n1, _), RecordField(n2, _)) => n1 == n2
#| (EnumLabeledArg(n1, _), EnumLabeledArg(n2, _)) => n1 == n2
#| (Opaque(n1, _), Opaque(n2, _)) => n1 == n2
#| (Literal(x1), Literal(y1)) => x1 == y1
#| (MapEntry(_, _), MapEntry(_, _)) => true
#| (Omitted, Omitted) => true
#| _ => false
#| }
#|}
#|fn info_is_unimportant(info : Repr) -> Bool {
#| info is (MapEntry(_, _) | Tuple(_) | Array(_) | Record(_))
#|}
#|fn diff_info_with(
#| left : Repr,
#| right : Repr,
#| max_relative_error~ : Double,
#|) -> ReprDelta {
#| fn go(left_node : Repr, right_node : Repr) -> ReprDelta {
#| if info_approx_eq(max_relative_error, left_node, right_node) {
#| let xs = left_node.children()
#| let ys = right_node.children()
#| let xlen = xs.length()
#| let ylen = ys.length()
#| let min_len = Int32::min(xlen, ylen)
#| let children : Array[ReprDelta] = []
#| for i in 0.. !(d is Same(_, _))) {
#| Different(left_node, right_node)
#| } else {
#| Same(left_node.shallow(), children)
#| }
#| } else {
#| Different(left_node, right_node)
#| }
#| }
#| go(left, right)
#|}
#|fn diff_repr(
#| x : Repr,
#| y : Repr,
#| max_relative_error~ : Double = DEFAULT_MAX_RELATIVE_ERROR,
#|) -> ReprDelta {
#| diff_info_with(x, y, max_relative_error~)
#|}
#|fn pretty_print_delta(
#| d : ReprDelta,
#| compact_threshold~ : Int32 = default_threshold,
#| use_ansi~ : Bool = true,
#|) -> String {
#| print_content(render_delta(compact_threshold, use_ansi, d).no_wrap())
#|}
#|const ANSI_RESET : String = "\u001b[0m"
#|const ANSI_RED : String = "\u001b[31m"
#|const ANSI_GREEN : String = "\u001b[32m"
#|fn mark_removed(use_ansi : Bool, x : ContentParens) -> ContentParens {
#| if use_ansi {
#| surround(ANSI_RED + "-", ANSI_RESET, x)
#| } else {
#| surround("-", "", x)
#| }
#|}
#|fn mark_added(use_ansi : Bool, x : ContentParens) -> ContentParens {
#| if use_ansi {
#| surround(ANSI_GREEN + "+", ANSI_RESET, x)
#| } else {
#| surround("+", "", x)
#| }
#|}
#|fn render_delta(
#| threshold : Int32,
#| use_ansi : Bool,
#| delta : ReprDelta,
#|) -> Content {
#| match delta {
#| Same(label, children_delta) => {
#| let children = children_delta.map(child => {
#| render_delta(threshold, use_ansi, child)
#| })
#| with_resizing(threshold, label.pretty_print(children))
#| }
#| Different(left, right) =>
#| with_resizing(
#| threshold,
#| no_parens(
#| mark_removed(use_ansi, left.render_repr(threshold).no_wrap()) +
#| mark_added(use_ansi, right.render_repr(threshold).no_wrap()),
#| ),
#| )
#| Extra1(x) =>
#| with_resizing(
#| threshold,
#| no_parens(mark_removed(use_ansi, x.render_repr(threshold).no_wrap())),
#| )
#| Extra2(x) =>
#| with_resizing(
#| threshold,
#| no_parens(mark_added(use_ansi, x.render_repr(threshold).no_wrap())),
#| )
#| }
#|}
),
"error.mbt": (
#|fn error_to_repr(e : Error) -> Repr = "%error.to_repr"
#|pub impl Debug for Error with fn to_repr(self) {
#| error_to_repr(self)
#|}
#|test "Debug for Error" {
#| fn fail_with_error() -> Unit raise {
#| raise InspectError::InspectError("test error")
#| }
#| try fail_with_error() catch {
#| e => debug_inspect(e, content="InspectError(\"test error\")")
#| } noraise {
#| _ => fail("expected error")
#| }
#|}
),
"extends.mbt": (
#|pub extend Repr with Show::{to_string}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Repr with Debug::{to_repr}
),
"json.mbt": (
#|pub impl Debug for @json.Json with fn to_repr(self) {
#| match self {
#| Null => Repr::ctor("Null", [])
#| True => Repr::ctor("True", [])
#| False => Repr::ctor("False", [])
#| Number(number, repr~) =>
#| match repr {
#| None => Repr::ctor("Number", [(None, Repr(number))])
#| Some(_) =>
#| Repr::ctor("Number", [
#| (None, Repr(number)),
#| (Some("repr"), Repr(repr)),
#| ])
#| }
#| String(string) => Repr::ctor("String", [(None, Repr(string))])
#| Array(array) => Repr::ctor("Array", [(None, Repr(array))])
#| Object(object) => Repr::ctor("Object", [(None, Repr(object))])
#| }
#|}
#|pub impl Debug for @json.Position with fn to_repr(self) {
#| Repr::record({ "line": Repr(self.line), "column": Repr(self.column) })
#|}
#|pub impl Debug for @json.ParseError with fn to_repr(self) {
#| match self {
#| InvalidChar(position, char) =>
#| Repr::ctor("InvalidChar", [(None, Repr(position)), (None, Repr(char))])
#| InvalidEof => Repr::ctor("InvalidEof", [])
#| InvalidNumber(position, number) =>
#| Repr::ctor("InvalidNumber", [(None, Repr(position)), (None, Repr(number))])
#| InvalidIdentEscape(position) =>
#| Repr::ctor("InvalidIdentEscape", [(None, Repr(position))])
#| DuplicateKey(position, key) =>
#| Repr::ctor("DuplicateKey", [(None, Repr(position)), (None, Repr(key))])
#| DepthLimitExceeded => Repr::ctor("DepthLimitExceeded", [])
#| }
#|}
#|pub impl Debug for @json.DecodeContext with fn to_repr(self) {
#| Repr::opaque_("JsonPath", Repr(self.to_string()))
#|}
#|pub impl Debug for @json.JsonDecodeError with fn to_repr(self) {
#| let JsonDecodeError(contents) = self
#| Repr::ctor("JsonDecodeError", [(None, Repr(contents))])
#|}
#|pub impl Debug for @json.Replacer with fn to_repr(_self) {
#| Repr::record({ "f": Repr::opaque_("function", Repr::omitted()) })
#|}
),
"pretty_print.mbt": (
#|let default_max_depth : Int32 = 16
#|let default_threshold : Int32 = 70
#|fn Repr::prune_info(
#| self : Repr,
#| replacement~ : Repr = Repr::omitted(),
#| max_depth~ : Int32?,
#|) -> Repr {
#| match max_depth {
#| None => self
#| Some(depth) => {
#| fn go(d : Int32, node : Repr) -> Repr {
#| let children = node.children()
#| if d <= 0 {
#| if children.is_empty() {
#| node
#| } else if !node.info_adds_depth() {
#| node.with_children(children.map(child => go(d, child)))
#| } else {
#| replacement
#| }
#| } else if children.is_empty() {
#| node
#| } else {
#| let next_depth = if node.info_adds_depth() { d - 1 } else { d }
#| node.with_children(children.map(child => go(next_depth, child)))
#| }
#| }
#| go(Int32::max(1, depth), self)
#| }
#| }
#|}
#|fn Repr::info_adds_depth(info : Repr) -> Bool {
#| !(info is (RecordField(_, _) | EnumLabeledArg(_, _) | MapEntry(_, _)))
#|}
#|fn is_unquoted_key(key : String) -> Bool {
#| lexmatch key {
#| re"^[a-z_][a-zA-Z0-9_]*$" => true
#| _ => false
#| }
#|}
#|fn pretty_print_label(name : String) -> String {
#| if is_unquoted_key(name) {
#| name
#| } else {
#| name.escape()
#| }
#|}
#|fn Repr::pretty_print(self : Repr, children : Array[Content]) -> Content {
#| match self {
#| UnitLit => comma_seq("(", ")", [])
#| Integer(s) =>
#| no_parens(content_parens(if s is ['-', ..] { 1 } else { 0 }, [s]))
#| DoubleLit(x) => {
#| let needs_parens = 1.0 / x < 0.0
#| leaf(x.to_string(), needs_parens~)
#| }
#| FloatLit(x) => {
#| let needs_parens : Bool = @float.Float(1.0) / x < @float.Float(0.0)
#| leaf(x.to_string(), needs_parens~)
#| }
#| BoolLit(x) => leaf(x.to_string())
#| CharLit(x) => leaf(x.escape())
#| StringLit(x) => no_parens(verbatim(x.escape()))
#| Tuple(_) => comma_seq("(", ")", children.map(x => x.no_wrap()))
#| Enum(name, _) =>
#| match children {
#| [] => no_parens(verbatim(name))
#| _ =>
#| if name == "Tuple" {
#| comma_seq("(", ")", children.map(x => x.no_wrap()))
#| } else {
#| comma_seq(name + "(", ")", children.map(x => x.no_wrap()))
#| }
#| }
#| Array(_) => comma_seq("[", "]", children.map(x => x.no_wrap()))
#| Record(_) => comma_seq("{", "}", children.map(x => x.no_wrap()))
#| Opaque(name, _) =>
#| if children.is_empty() {
#| no_parens(surround("<", ">", verbatim(name)))
#| } else {
#| let body = verbatim(name + ":") +
#| indent(
#| " ",
#| comma_seq("", "", children.map(x => x.no_wrap())).no_wrap(),
#| )
#| no_parens(surround("<", ">", body))
#| }
#| Literal(str) => no_parens(verbatim(str))
#| Map(_) => comma_seq("{", "}", children.map(x => x.no_wrap()))
#| MapEntry(_, _) =>
#| match children {
#| [key, val] => {
#| let k = key.no_wrap()
#| let v = val.no_wrap()
#| match v.lines {
#| [] => empty_content()
#| [first, .. rest] => {
#| let head = print_content(surround("", ": ", k)) + first
#| no_parens(content_parens(1 + k.size + v.size, [head, ..rest]))
#| }
#| }
#| }
#| _ => empty_content()
#| }
#| EnumLabeledArg(name, _) =>
#| match children {
#| [val] =>
#| match val.lines {
#| [] => empty_content()
#| [first, .. rest] =>
#| no_parens(
#| content_parens(1 + val.size, [name + "=" + first, ..rest]),
#| )
#| }
#| _ => empty_content()
#| }
#| RecordField(name, _) =>
#| match children {
#| [val] => {
#| let label = pretty_print_label(name)
#| let v = val.no_wrap()
#| match v.lines {
#| [] => empty_content()
#| [first, .. rest] =>
#| no_parens(
#| content_parens(1 + v.size, [label + ": " + first, ..rest]),
#| )
#| }
#| }
#| _ => empty_content()
#| }
#| Omitted => parens(verbatim("..."))
#| }
#|}
#|fn Repr::render_repr(self : Repr, threshold : Int32) -> Content {
#| let label = self.shallow()
#| let children = self.children().map(child => child.render_repr(threshold))
#| with_resizing(threshold, label.pretty_print(children))
#|}
#|pub fn render(r : Repr, max_depth? : Int32) -> String {
#| render_pruned(r, Some(max_depth.unwrap_or(default_max_depth)))
#|}
#|fn render_pruned(r : Repr, max_depth : Int32?) -> String {
#| let info = r.prune_info(max_depth~)
#| print_content(info.render_repr(default_threshold).no_wrap())
#|}
#|pub impl Show for Repr with fn output(self, logger) {
#| logger.write_string(render(self))
#|}
#|pub impl Debug for Repr with fn to_repr(self) {
#| self
#|}
#|pub fn[T : Debug] debug(x : T) -> Unit {
#| println(render(x.to_repr()))
#|}
#|#alias(repr)
#|pub fn[T : Debug] to_string(x : T) -> String {
#| render(x.to_repr())
#|}
),
"printer.mbt": (
#|priv struct Content {
#| size : Int32
#| lines : Array[String]
#| needs_parens : Bool
#|}
#|priv struct ContentParens {
#| size : Int32
#| lines : Array[String]
#|}
#|impl Add for ContentParens with fn add(self, other) {
#| { size: self.size + other.size, lines: self.lines + other.lines, }
#|}
#|fn empty_content() -> Content {
#| { size: 0, lines: [], needs_parens: false, }
#|}
#|fn verbatim(x : String) -> ContentParens {
#| { size: 1, lines: [x], }
#|}
#|fn content_parens(size : Int32, lines : Array[String]) -> ContentParens {
#| { size, lines, }
#|}
#|fn leaf(x : String, needs_parens~ : Bool = false) -> Content {
#| { size: 1, lines: [x], needs_parens, }
#|}
#|fn with_lines(
#| r : ContentParens,
#| f : (Array[String]) -> Array[String],
#|) -> ContentParens {
#| { size: r.size, lines: f(r.lines), }
#|}
#|fn Content::with_lines_content(
#| r : Content,
#| f : (Array[String]) -> Array[String],
#|) -> Content {
#| { size: r.size, lines: f(r.lines), needs_parens: r.needs_parens, }
#|}
#|fn surround_lines(
#| start : String,
#| finish : String,
#| lines : Array[String],
#|) -> Array[String] {
#| match lines {
#| [] => [start + finish]
#| [item] => [start + item + finish]
#| [first, .. middle, last] => [start + first, ..middle, last + finish]
#| }
#|}
#|fn surround(
#| start : String,
#| finish : String,
#| r : ContentParens,
#|) -> ContentParens {
#| with_lines(r, fn(lines) { surround_lines(start, finish, lines) })
#|}
#|fn Content::no_wrap(c : Content) -> ContentParens {
#| { size: c.size, lines: c.lines, }
#|}
#|fn parens(r : ContentParens) -> Content {
#| { size: r.size, lines: r.lines, needs_parens: true, }
#|}
#|fn no_parens(r : ContentParens) -> Content {
#| { size: r.size, lines: r.lines, needs_parens: false, }
#|}
#|fn compact_middle(middle : ArrayView[String]) -> StringView {
#| let parts : Array[StringView] = []
#| for m in middle {
#| let t = m.trim()
#| if t != "" {
#| parts.push(t)
#| }
#| }
#| let joined0 = parts.join(" ")
#| joined0.strip_suffix(",").unwrap_or(joined0)
#|}
#|fn compact_lines(lines : Array[String]) -> Array[String] {
#| match lines {
#| [] => []
#| [x] => [x]
#| [first, .. middle, last] =>
#| if (first == "[" && last == "]") || (first == "{" && last == "}") {
#| let joined = compact_middle(middle)
#| if first == "{" {
#| let s1 = joined.strip_prefix(" ").unwrap_or(joined)
#| let inner = s1.strip_suffix(" ").unwrap_or(s1)
#| if inner == "" {
#| ["{}"]
#| } else {
#| ["{ \{inner} }"]
#| }
#| } else {
#| ["[\{joined}]"]
#| }
#| } else if first.has_suffix("(") && last == ")" {
#| let joined = compact_middle(middle)
#| ["\{first}\{joined}\{last}"]
#| } else {
#| let parts : Array[StringView] = [first]
#| for m in middle {
#| parts.push(m.trim())
#| }
#| parts.push(last.trim())
#| [parts.join(" ")]
#| }
#| }
#|}
#|fn Content::compact(r : Content) -> Content {
#| r.with_lines_content(compact_lines)
#|}
#|fn indent(prefix : String, r : ContentParens) -> ContentParens {
#| with_lines(r, lines => lines.map(line => prefix + line))
#|}
#|fn indent_spaces(n : Int32, r : ContentParens) -> ContentParens {
#| indent(" ".repeat(n), r)
#|}
#|fn push_indented_items(
#| out : Array[String],
#| indent_by : Int32,
#| contents : Array[Array[String]],
#|) -> Unit {
#| for item in contents {
#| let item_lines = item.filter(line => line != "")
#| match item_lines {
#| [] => ()
#| [x] => out.push(" ".repeat(indent_by) + x + ",")
#| [first, .. middle, last] => {
#| out.push(" ".repeat(indent_by) + first)
#| for mid in middle {
#| out.push(" ".repeat(indent_by) + mid)
#| }
#| out.push(" ".repeat(indent_by) + last + ",")
#| }
#| }
#| }
#|}
#|fn bracket_seq_lines(
#| open : String,
#| close : String,
#| indent_by : Int32,
#| contents : Array[Array[String]],
#|) -> Array[String] {
#| match contents {
#| [] => [open + close]
#| [item] =>
#| if item.length() > 1 {
#| let lines = indent_spaces(indent_by, { size: 0, lines: item, }).lines
#| if !lines.is_empty() {
#| let last_i = lines.length() - 1
#| lines[last_i] += ","
#| }
#| [open, ..lines, close]
#| } else {
#| let inner = compact_lines(item)
#| match inner {
#| [] => [open + close]
#| [x] =>
#| if open == "{" && close == "}" {
#| let inner = x.trim()
#| if inner == "" {
#| ["{}"]
#| } else {
#| ["{ \{inner} }"]
#| }
#| } else {
#| [open + x + close]
#| }
#| _ =>
#| [
#| open,
#| ..indent_spaces(indent_by, { size: 0, lines: item, }).lines,
#| close,
#| ]
#| }
#| }
#| _ => {
#| let out : Array[String] = [open]
#| push_indented_items(out, indent_by, contents)
#| out.push(close)
#| out
#| }
#| }
#|}
#|fn comma_seq_lines(
#| begin : String,
#| end : String,
#| contents : Array[Array[String]],
#|) -> Array[String] {
#| if begin == "[" && end == "]" {
#| bracket_seq_lines("[", "]", 2, contents)
#| } else if begin == "{" && end == "}" {
#| bracket_seq_lines("{", "}", 2, contents)
#| } else if begin == "" && end == "" {
#| let lines = match contents {
#| [] => [begin + end]
#| [item] => surround_lines(begin, end, item)
#| [first, .. middle, last] => {
#| let space = " ".repeat(begin.length())
#| let middle_lines = []
#| for item in middle {
#| middle_lines.append(surround_lines(space, ",", item))
#| }
#| [
#| ..surround_lines(begin, ",", first),
#| ..middle_lines,
#| ..surround_lines(space, end, last),
#| ]
#| }
#| }
#| lines.filter(line => line != "")
#| } else {
#| let indent_by = 2
#| match contents {
#| [] => [begin + end]
#| [item] => {
#| let item_lines = item.filter(line => line != "")
#| match item_lines {
#| [] => [begin + end]
#| [x] => [begin + x + end]
#| [.. pre_lines, last_line] =>
#| [
#| begin,
#| ..pre_lines.map(line => " ".repeat(indent_by) + line),
#| " ".repeat(indent_by) + last_line + ",",
#| end,
#| ]
#| }
#| }
#| _ => {
#| let out : Array[String] = [begin]
#| push_indented_items(out, indent_by, contents)
#| out.push(end)
#| out
#| }
#| }
#| }
#|}
#|fn comma_seq(
#| begin : String,
#| end : String,
#| contents : Array[ContentParens],
#|) -> Content {
#| for c in contents; size = 0 {
#| continue size + c.size
#| } nobreak {
#| {
#| size,
#| lines: comma_seq_lines(begin, end, contents.map(c => c.lines)),
#| needs_parens: false,
#| }
#| }
#|}
#|fn print_content(r : ContentParens) -> String {
#| r.lines.join("\n")
#|}
#|fn with_resizing(threshold : Int32, rendered_children : Content) -> Content {
#| if threshold <= 0 {
#| rendered_children
#| } else {
#| let compacted = rendered_children.compact()
#| if compacted.lines.all(line => line.length() <= threshold) {
#| compacted
#| } else {
#| rendered_children
#| }
#| }
#|}
),
"repr.mbt": (
#|enum Repr {
#| UnitLit
#| Integer(String)
#| DoubleLit(Double)
#| FloatLit(@float.Float)
#| BoolLit(Bool)
#| CharLit(Char)
#| StringLit(String)
#| Tuple(Array[Repr])
#| Array(Array[Repr])
#| Record(Array[Repr])
#| Enum(String, Array[Repr])
#| Map(Array[Repr])
#| RecordField(String, Repr)
#| EnumLabeledArg(String, Repr)
#| Opaque(String, Repr)
#| Literal(String)
#| MapEntry(Repr, Repr)
#| Omitted
#|}
#|fn Repr::children(self : Repr) -> Array[Repr] {
#| match self {
#| UnitLit
#| | Integer(_)
#| | DoubleLit(_)
#| | FloatLit(_)
#| | BoolLit(_)
#| | CharLit(_)
#| | StringLit(_)
#| | Literal(_)
#| | Omitted => []
#| Tuple(xs) | Array(xs) | Record(xs) | Enum(_, xs) | Map(xs) => xs
#| Opaque(_, value) | RecordField(_, value) | EnumLabeledArg(_, value) =>
#| [value]
#| MapEntry(key, value) => [key, value]
#| }
#|}
#|fn Repr::with_children(self : Repr, children : Array[Repr]) -> Repr {
#| match self {
#| UnitLit
#| | Integer(_)
#| | DoubleLit(_)
#| | FloatLit(_)
#| | BoolLit(_)
#| | CharLit(_)
#| | StringLit(_)
#| | Literal(_)
#| | Omitted => self
#| Tuple(_) => Tuple(children)
#| Array(_) => Array(children)
#| Record(_) => Record(children)
#| Enum(name, _) => Enum(name, children)
#| Map(_) => Map(children)
#| RecordField(name, _) =>
#| match children {
#| [value] => RecordField(name, value)
#| _ => RecordField(name, Omitted)
#| }
#| EnumLabeledArg(label, _) =>
#| match children {
#| [value] => EnumLabeledArg(label, value)
#| _ => EnumLabeledArg(label, Omitted)
#| }
#| MapEntry(_, _) =>
#| match children {
#| [key, value] => MapEntry(key, value)
#| _ => MapEntry(Omitted, Omitted)
#| }
#| Opaque(name, _) =>
#| match children {
#| [value] => Opaque(name, value)
#| _ => Opaque(name, Omitted)
#| }
#| }
#|}
#|#doc(hidden)
#|pub fn Repr::traverse(self : Repr, f : (Repr) -> Repr) -> Repr {
#| fn go(node : Repr) -> Repr {
#| let children = node.children()
#| let next_children = children.map(child => go(child))
#| f(node.with_children(next_children))
#| }
#| go(self)
#|}
#|pub fn[T : Debug] Repr::Repr(value : T) -> Repr {
#| Debug::to_repr(value)
#|}
#|#doc(hidden)
#|pub fn Repr::integer(x : String) -> Repr {
#| Integer(x)
#|}
#|#doc(hidden)
#|pub fn Repr::double(x : Double) -> Repr {
#| DoubleLit(x)
#|}
#|#doc(hidden)
#|pub fn Repr::float(x : @float.Float) -> Repr {
#| FloatLit(x)
#|}
#|#doc(hidden)
#|pub fn Repr::bool(x : Bool) -> Repr {
#| BoolLit(x)
#|}
#|#doc(hidden)
#|pub fn Repr::char(x : Char) -> Repr {
#| CharLit(x)
#|}
#|#doc(hidden)
#|pub fn Repr::string(x : String) -> Repr {
#| StringLit(x)
#|}
#|#doc(hidden)
#|pub fn Repr::tuple(children : Array[Repr]) -> Repr {
#| Tuple(children)
#|}
#|#doc(hidden)
#|pub fn Repr::array(children : Array[Repr]) -> Repr {
#| Array(children)
#|}
#|#doc(hidden)
#|pub fn Repr::record(fields : Map[String, Repr]) -> Repr {
#| Record([ for name, value in fields => RecordField(name, value) ])
#|}
#|#doc(hidden)
#|pub fn Repr::opaque_(name : String, children : Repr) -> Repr {
#| Opaque(name, children)
#|}
#|#doc(hidden)
#|pub fn Repr::literal(value : String) -> Repr {
#| Literal(value)
#|}
#|#doc(hidden)
#|pub fn Repr::map(contents : Array[(Repr, Repr)]) -> Repr {
#| Map(
#| contents.map(pair => {
#| let (k, v) = pair
#| MapEntry(k, v)
#| }),
#| )
#|}
#|#doc(hidden)
#|pub fn Repr::omitted() -> Repr {
#| Omitted
#|}
#|#doc(hidden)
#|pub fn Repr::unit() -> Repr {
#| UnitLit
#|}
#|#doc(hidden)
#|pub fn Repr::ctor(name : String, args : Array[(String?, Repr)]) -> Repr {
#| Enum(
#| name,
#| args.map(arg => {
#| let (label, value) = arg
#| match label {
#| None => value
#| Some(label) => EnumLabeledArg(label, value)
#| }
#| }),
#| )
#|}
#|fn Repr::shallow(self : Repr) -> Repr {
#| self.with_children([])
#|}
),
"tuple_debug.mbt": (
#|pub impl[A : Debug, B : Debug] Debug for (A, B) with fn to_repr(self) {
#| let (a, b) = self
#| Repr::tuple([Repr(a), Repr(b)])
#|}
#|pub impl[A : Debug, B : Debug, C : Debug] Debug for (A, B, C) with fn to_repr(
#| self,
#|) {
#| let (a, b, c) = self
#| Repr::tuple([Repr(a), Repr(b), Repr(c)])
#|}
#|pub impl[A : Debug, B : Debug, C : Debug, D : Debug] Debug for (A, B, C, D) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d) = self
#| Repr::tuple([Repr(a), Repr(b), Repr(c), Repr(d)])
#|}
#|pub impl[A : Debug, B : Debug, C : Debug, D : Debug, E : Debug] Debug for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|) with fn to_repr(self) {
#| let (a, b, c, d, e) = self
#| Repr::tuple([Repr(a), Repr(b), Repr(c), Repr(d), Repr(e)])
#|}
#|pub impl[A : Debug, B : Debug, C : Debug, D : Debug, E : Debug, F : Debug] Debug for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#|) with fn to_repr(self) {
#| let (a, b, c, d, e, f) = self
#| Repr::tuple([Repr(a), Repr(b), Repr(c), Repr(d), Repr(e), Repr(f)])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#|] Debug for (A, B, C, D, E, F, G) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g) = self
#| Repr::tuple([Repr(a), Repr(b), Repr(c), Repr(d), Repr(e), Repr(f), Repr(g)])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#|] Debug for (A, B, C, D, E, F, G, H) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i, j) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i, j, k) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) with fn to_repr(self) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#| Q : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| Repr(q),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#| Q : Debug,
#| R : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| Repr(q),
#| Repr(r),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#| Q : Debug,
#| R : Debug,
#| S : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| Repr(q),
#| Repr(r),
#| Repr(s),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#| Q : Debug,
#| R : Debug,
#| S : Debug,
#| T : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| Repr(q),
#| Repr(r),
#| Repr(s),
#| Repr(t),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#| Q : Debug,
#| R : Debug,
#| S : Debug,
#| T : Debug,
#| U : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| Repr(q),
#| Repr(r),
#| Repr(s),
#| Repr(t),
#| Repr(u),
#| ])
#|}
#|pub impl[
#| A : Debug,
#| B : Debug,
#| C : Debug,
#| D : Debug,
#| E : Debug,
#| F : Debug,
#| G : Debug,
#| H : Debug,
#| I : Debug,
#| J : Debug,
#| K : Debug,
#| L : Debug,
#| M : Debug,
#| N : Debug,
#| O : Debug,
#| P : Debug,
#| Q : Debug,
#| R : Debug,
#| S : Debug,
#| T : Debug,
#| U : Debug,
#| V : Debug,
#|] Debug for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V) with fn to_repr(
#| self,
#|) {
#| let (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v) = self
#| Repr::tuple([
#| Repr(a),
#| Repr(b),
#| Repr(c),
#| Repr(d),
#| Repr(e),
#| Repr(f),
#| Repr(g),
#| Repr(h),
#| Repr(i),
#| Repr(j),
#| Repr(k),
#| Repr(l),
#| Repr(m),
#| Repr(n),
#| Repr(o),
#| Repr(p),
#| Repr(q),
#| Repr(r),
#| Repr(s),
#| Repr(t),
#| Repr(u),
#| Repr(v),
#| ])
#|}
),
"v128.mbt": (
#|fn v128_lo(value : V128) -> UInt64 = "%v128.lo"
#|fn v128_hi(value : V128) -> UInt64 = "%v128.hi"
#|fn u64_hex_to(logger : &Logger, value : UInt64) -> Unit {
#| let digits = value.to_string(radix=16)
#| logger <+ "0x"
#| for _ in digits.length()..<16 {
#| logger.write_char('0')
#| }
#| logger.write_string(digits)
#|}
#|fn u64_hex(value : UInt64) -> String {
#| let buf = StringBuilder()
#| u64_hex_to(buf, value)
#| buf.to_string()
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub impl Debug for V128 with fn to_repr(self) {
#| Repr::opaque_(
#| "V128",
#| Repr::tuple([
#| Repr::literal(u64_hex(v128_lo(self))),
#| Repr::literal(u64_hex(v128_hi(self))),
#| ]),
#| )
#|}
),
},
)
///|
let moonbitlang_core_deque_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/deque",
deps={},
files={
"bounds.mbt": (
#|#inline(never)
#|fn[T] index_out_of_bounds(len : Int32, index : Int32) -> T {
#| abort(
#| "index out of bounds: the len is from 0 to \{len} but the index is \{index}",
#| )
#|}
),
"debug.mbt": (
#|pub impl[A : @debug.Debug] @debug.Debug for Deque[A] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "Deque",
#| @debug.Repr::array(self.to_array().map(x => Repr(x))),
#| )
#|}
#|test "Debug for Deque" {
#| let dq : Deque[Int32] = from_array([1, 2, 3])
#| @debug.debug_inspect(dq, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `Deque([], capacity=...)` instead")
#|#as_free_fn(deprecated="Use `Deque([], capacity=...)` instead")
#|pub fn[A] Deque::new(capacity~ : Int32 = 0) -> Deque[A] {
#| new_deque(capacity)
#|}
),
"deque.mbt": (
#|fn[A] fill_slots(
#| buf : UninitializedArray[A],
#| start : Int32,
#| end : Int32,
#| value : A,
#|) -> Unit {
#| for i in start.. Deque[A] {
#| { buf: UninitializedArray::make(capacity), len: 0, head: 0, }
#|}
#|fn[A] Deque::tail_index(self : Deque[A]) -> Int32 {
#| wrap_index(self.head + self.len - 1, self.buf.length())
#|}
#|#inline
#|fn wrap_index(i : Int32, cap : Int32) -> Int32 {
#| if i >= cap {
#| i - cap
#| } else {
#| i
#| }
#|}
#|#inline
#|fn[T] UninitializedArray::unsafe_get(
#| self : UninitializedArray[T],
#| index : Int32,
#|) -> T = "%fixedarray.unsafe_get"
#|#inline
#|fn[T] UninitializedArray::unsafe_set(
#| self : UninitializedArray[T],
#| index : Int32,
#| value : T,
#|) -> Unit = "%fixedarray.unsafe_set"
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show] Show for Deque[A]
#|pub impl[A : Show] Show for Deque[A] with fn output(self, logger) {
#| logger.write_iter(self.iter(), prefix="@deque.from_array([", suffix="])")
#|}
#|pub impl[A : Hash] Hash for Deque[A] with fn hash_combine(self, hasher) {
#| for v in self {
#| v.hash_combine(hasher)
#| }
#|}
#|pub impl[A] Add for Deque[A] with fn add(self, other) {
#| let len = self.len + other.len
#| let buf = self.unsafe_make_and_blit_to(len, 0)
#| other.unsafe_blit_to(buf, self.len)
#| { buf, len, head: 0, }
#|}
#|test "add_empty" {
#| let empty1 : Deque[Int32] = new_deque(0)
#| let empty2 : Deque[Int32] = new_deque(0)
#| let result = empty1 + empty2
#| inspect(result.len, content="0")
#| inspect(result.is_empty(), content="true")
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[A] Deque::Deque(arr : ArrayView[A], capacity? : Int32) -> Deque[A] {
#| let len = arr.length()
#| let capacity = match capacity {
#| Some(capacity) => {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| capacity.max(len)
#| }
#| None => len
#| }
#| let buf = UninitializedArray::make(capacity)
#| for i, x in arr {
#| buf[i] = x
#| }
#| { buf, len, head: 0, }
#|}
#|test "from_array_empty" {
#| let dq : Deque[Int32] = from_array([])
#| inspect(dq.len, content="0")
#| inspect(dq.is_empty(), content="true")
#|}
#|#alias(clone, deprecated)
#|pub fn[A] Deque::copy(self : Deque[A]) -> Deque[A] {
#| let len = self.len
#| let buf = self.unsafe_make_and_blit_to(len, 0)
#| { buf, len, head: 0, }
#|}
#|pub fn[A] Deque::blit_to(
#| self : Deque[A],
#| dst : Deque[A],
#| len~ : Int32,
#| src_offset~ : Int32 = 0,
#| dst_offset~ : Int32 = 0,
#|) -> Unit {
#| guard! len >= 0 &&
#| dst_offset >= 0 &&
#| src_offset >= 0 &&
#| dst_offset <= dst.length() &&
#| src_offset <= self.length() &&
#| len <= self.length() - src_offset &&
#| len <= 0x7fffffff - dst_offset
#| if dst_offset + len > dst.buf.length() {
#| dst.reserve_capacity(dst_offset + len)
#| dst.head = 0
#| }
#| let dst_len = dst.len
#| let needs_reverse = physical_equal(self, dst) &&
#| src_offset < dst_offset &&
#| src_offset + len > dst_offset
#| if needs_reverse {
#| let new_len = if dst_offset + len > dst_len {
#| dst_offset + len
#| } else {
#| dst_len
#| }
#| if new_len > dst_len {
#| dst.len = new_len
#| }
#| for i in len>..0 {
#| let dst_idx = (dst.head + dst_offset + i) % dst.buf.length()
#| let src_idx = (self.head + src_offset + i) % self.buf.length()
#| dst.buf[dst_idx] = self.buf[src_idx]
#| }
#| } else {
#| for i in 0..= dst_len {
#| dst.len += 1
#| }
#| }
#| }
#|}
#|pub fn[A] Deque::append(self : Deque[A], other : Deque[A]) -> Unit {
#| let other_len = other.len
#| let other_head = other.head
#| let other_buf = other.buf
#| let other_buf_len = other_buf.length()
#| guard other_len != 0 else { return }
#| let space = self.buf.length() - self.len
#| if space < other_len {
#| let new_cap = if self.len + other_len > self.buf.length() * 2 {
#| self.len + other_len
#| } else {
#| self.buf.length() * 2
#| }
#| let new_buf = UninitializedArray::make(new_cap)
#| for i, x in self {
#| new_buf[i] = x
#| }
#| self.buf = new_buf
#| self.head = 0
#| }
#| let cap = self.buf.length()
#| for i in 0.. Unit {
#| guard index >= 0 && index <= self.length() else {
#| abort(
#| "index out of bounds: the len is from 0 to \{self.length()} but the index is \{index}",
#| )
#| }
#| if self.buf.length() - self.len == 0 {
#| self.realloc()
#| }
#| let cap = self.buf.length()
#| if index < self.len / 2 {
#| let new_head = (self.head - 1 + cap) % cap
#| for i in 0.. index; i = i - 1 {
#| let from = (self.head + i - 1) % cap
#| let to = (self.head + i) % cap
#| self.buf[to] = self.buf[from]
#| }
#| }
#| self.buf[(self.head + index) % cap] = value
#| self.len += 1
#|}
#|pub fn[A] Deque::remove(self : Deque[A], index : Int32) -> A {
#| guard index >= 0 && index < self.length() else {
#| abort(
#| "index out of bounds: the len is \{self.length()} but the index is \{index}",
#| )
#| }
#| let res = self[index]
#| let cap = self.buf.length()
#| if index < self.len / 2 {
#| let new_head = (self.head + 1) % cap
#| for i in index>..0 {
#| let to = (self.head + i + 1) % cap
#| let from = (self.head + i) % cap
#| self.buf[to] = self.buf[from]
#| }
#| self.head = new_head
#| } else {
#| for i in (index + 1).. Int32 {
#| self.len
#|}
#|pub fn[A] Deque::capacity(self : Deque[A]) -> Int32 {
#| self.buf.length()
#|}
#|#inline(never)
#|fn[A] Deque::realloc(self : Deque[A]) -> Unit {
#| let old_cap = self.buf.length()
#| let new_cap = if old_cap == 0 { 8 } else { old_cap * 2 }
#| let new_buf = self.unsafe_make_and_blit_to(new_cap, 0)
#| self.head = 0
#| self.buf = new_buf
#|}
#|pub fn[A] Deque::front(self : Deque[A]) -> A? {
#| if self.len == 0 {
#| None
#| } else {
#| Some(self.buf[self.head])
#| }
#|}
#|pub fn[A] Deque::back(self : Deque[A]) -> A? {
#| if self.len == 0 {
#| None
#| } else {
#| Some(self.buf[self.tail_index()])
#| }
#|}
#|#owned(value)
#|pub fn[A] Deque::push_front(self : Deque[A], value : A) -> Unit {
#| if self.len == self.buf.length() {
#| self.realloc()
#| }
#| let cap = self.buf.length()
#| self.head = if self.head == 0 { cap - 1 } else { self.head - 1 }
#| self.buf[self.head] = value
#| self.len += 1
#|}
#|#owned(value)
#|pub fn[A] Deque::push_back(self : Deque[A], value : A) -> Unit {
#| if self.len == self.buf.length() {
#| self.realloc()
#| }
#| let cap = self.buf.length()
#| let write_idx = wrap_index(self.head + self.len, cap)
#| self.buf[write_idx] = value
#| self.len += 1
#|}
#|#internal(unsafe, "Panic if the deque is empty.")
#|#doc(hidden)
#|#alias(pop_front_exn, deprecated)
#|pub fn[A] Deque::unsafe_pop_front(self : Deque[A]) -> Unit {
#| guard self.len > 0 else { abort("The deque is empty!") }
#| self.head = wrap_index(self.head + 1, self.buf.length())
#| self.len -= 1
#|}
#|test "unsafe_pop_front after many push_front" {
#| let dq = new_deque(0)
#| for i in 0..<10 {
#| dq.push_front(i)
#| }
#| for _ in 0..<10 {
#| dq.unsafe_pop_front()
#| }
#| @test.assert_eq(dq.len, 0)
#|}
#|#internal(unsafe, "Panic if the deque is empty.")
#|#doc(hidden)
#|#alias(pop_back_exn, deprecated)
#|pub fn[A] Deque::unsafe_pop_back(self : Deque[A]) -> Unit {
#| guard self.len > 0 else { abort("The deque is empty!") }
#| self.len -= 1
#|}
#|pub fn[A] Deque::pop_front(self : Deque[A]) -> A? {
#| guard self.len > 0 else { return None }
#| let value = self.buf[self.head]
#| self.head = wrap_index(self.head + 1, self.buf.length())
#| self.len -= 1
#| Some(value)
#|}
#|pub fn[A] Deque::pop_back(self : Deque[A]) -> A? {
#| guard self.len > 0 else { return None }
#| let tail_idx = self.tail_index()
#| let value = self.buf[tail_idx]
#| self.len -= 1
#| Some(value)
#|}
#|#alias("_[_]")
#|pub fn[A] Deque::at(self : Deque[A], index : Int32) -> A {
#| if index < 0 || index >= self.len {
#| index_out_of_bounds(self.len, index)
#| }
#| if self.head + index < self.buf.length() {
#| self.buf[self.head + index]
#| } else {
#| self.buf[self.head + index - self.buf.length()]
#| }
#|}
#|#alias("_[_]=_")
#|#owned(value)
#|pub fn[A] Deque::set(self : Deque[A], index : Int32, value : A) -> Unit {
#| if index < 0 || index >= self.len {
#| index_out_of_bounds(self.len, index)
#| }
#| if self.head + index < self.buf.length() {
#| self.buf[self.head + index] = value
#| } else {
#| self.buf[self.head + index - self.buf.length()] = value
#| }
#|}
#|pub fn[A] Deque::as_views(self : Deque[A]) -> (ArrayView[A], ArrayView[A]) {
#| guard self.len != 0 else { ([], []) }
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| let head_len = cap - head
#| if head_len >= len {
#| (buf[head:head + len], [])
#| } else {
#| (buf[head:cap], buf[:len - head_len])
#| }
#|}
#|fn[A] Deque::unsafe_blit_to(
#| self : Deque[A],
#| dst : UninitializedArray[A],
#| dst_offset : Int32,
#|) -> Unit {
#| guard self.len != 0 else { return }
#| let (front, back) = self.as_views()
#| dst.unsafe_blit(dst_offset, self.buf, front.start_offset(), front.length())
#| dst.unsafe_blit(
#| dst_offset + front.length(),
#| self.buf,
#| back.start_offset(),
#| back.length(),
#| )
#|}
#|fn[A] Deque::unsafe_make_and_blit_to(
#| self : Deque[A],
#| allocate_len : Int32,
#| dst_offset : Int32,
#|) -> UninitializedArray[A] {
#| guard self.len != 0 else { return UninitializedArray::make(allocate_len) }
#| let (front, back) = self.as_views()
#| let dst = UninitializedArray::make_and_blit(
#| self.buf,
#| allocate_len~,
#| src_offset=front.start_offset(),
#| dst_offset~,
#| len=front.length(),
#| )
#| dst.unsafe_blit(
#| dst_offset + front.length(),
#| self.buf,
#| back.start_offset(),
#| back.length(),
#| )
#| dst
#|}
#|pub impl[A : Eq] Eq for Deque[A] with fn equal(self, other) {
#| if self.len != other.len {
#| return false
#| }
#| for i in 0.. Unit raise?) -> Unit raise? {
#| for v in self {
#| f(v)
#| }
#|}
#|#locals(f)
#|pub fn[A] Deque::eachi(
#| self : Deque[A],
#| f : (Int32, A) -> Unit raise?,
#|) -> Unit raise? {
#| for i, v in self {
#| f(i, v)
#| }
#|}
#|#locals(f)
#|pub fn[A] Deque::rev_each(
#| self : Deque[A],
#| f : (A) -> Unit raise?,
#|) -> Unit raise? {
#| for v in self.rev_iter() {
#| f(v)
#| }
#|}
#|#locals(f)
#|pub fn[A] Deque::rev_eachi(
#| self : Deque[A],
#| f : (Int32, A) -> Unit raise?,
#|) -> Unit raise? {
#| for i, v in self.rev_iter2() {
#| f(i, v)
#| }
#|}
#|pub fn[A] Deque::clear(self : Deque[A]) -> Unit {
#| self.len = 0
#| self.head = 0
#|}
#|#locals(f)
#|pub fn[A, U] Deque::map(
#| self : Deque[A],
#| f : (A) -> U raise?,
#|) -> Deque[U] raise? {
#| let { buf: src, head, len, } = self
#| let cap = src.length()
#| if len == 0 {
#| new_deque(0)
#| } else {
#| let buf : UninitializedArray[U] = UninitializedArray::make(len)
#| for i in 0.. U raise?,
#|) -> Deque[U] raise? {
#| let { buf: src, head, len, } = self
#| let cap = src.length()
#| if len == 0 {
#| new_deque(0)
#| } else {
#| let buf : UninitializedArray[U] = UninitializedArray::make(len)
#| for i in 0.. Bool {
#| self.len == 0
#|}
#|pub fn[A : Eq] Deque::search(self : Deque[A], value : A) -> Int32? {
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| for i in 0.. Bool {
#| self.iter().contains(value)
#|}
#|#locals(f)
#|pub fn[A] Deque::extract_if(self : Deque[A], f : (A) -> Bool) -> Deque[A] {
#| guard !self.is_empty() else { from_array([]) }
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| let removed = from_array([])
#| let kept_len = for read in 0.. Bool raise?,
#|) -> Deque[A] raise? {
#| let dq = from_array([])
#| for v in self {
#| if f(v) {
#| dq.push_back(v)
#| }
#| }
#| dq
#|}
#|pub fn[A] Deque::reserve_capacity(self : Deque[A], capacity : Int32) -> Unit {
#| if self.capacity() >= capacity {
#| return
#| }
#| let new_buf = self.unsafe_make_and_blit_to(capacity, 0)
#| self.buf = new_buf
#| self.head = 0
#|}
#|pub fn[A] Deque::shrink_to_fit(self : Deque[A]) -> Unit {
#| if self.capacity() <= self.length() {
#| return
#| }
#| let new_buf = self.unsafe_make_and_blit_to(self.len, 0)
#| self.buf = new_buf
#| self.head = 0
#|}
#|pub fn[A] Deque::release_unused(self : Deque[A], placeholder~ : A) -> Unit {
#| let cap = self.buf.length()
#| let head_len = cap - self.head
#| if self.len <= head_len {
#| fill_slots(self.buf, 0, self.head, placeholder)
#| fill_slots(self.buf, self.head + self.len, cap, placeholder)
#| } else {
#| fill_slots(self.buf, self.len - head_len, self.head, placeholder)
#| }
#|}
#|pub fn[A] Deque::truncate(self : Deque[A], len : Int32) -> Unit {
#| guard len >= 0 && len < self.len else { return }
#| self.len = len
#| if len == 0 {
#| self.head = 0
#| }
#|}
#|test "truncate zero" {
#| let dq = from_array([1, 2, 3])
#| dq.truncate(0)
#| inspect(dq.len, content="0")
#|}
#|#locals(f)
#|#alias(filter_map_inplace, deprecated)
#|pub fn[A] Deque::retain_map(self : Deque[A], f : (A) -> A?) -> Unit {
#| guard !self.is_empty() else { return }
#| let { head, buf, .. } = self
#| let cap = buf.length()
#| let (front, back) = self.as_views()
#| let (idx, kept_len) = for cur in front; idx = head, kept_len = 0 {
#| if f(cur) is Some(v) {
#| buf[idx] = v
#| continue idx + 1, kept_len + 1
#| }
#| continue idx, kept_len
#| } nobreak {
#| (idx, kept_len)
#| }
#| if back.is_empty() {
#| self.truncate(kept_len)
#| return
#| }
#| let kept_len = for cur in back; idx = idx, kept_len = kept_len {
#| let idx = if idx == cap { 0 } else { idx }
#| if f(cur) is Some(v) {
#| buf[idx] = v
#| continue idx + 1, kept_len + 1
#| }
#| continue idx, kept_len
#| } nobreak {
#| kept_len
#| }
#| self.truncate(kept_len)
#|}
#|#locals(f)
#|pub fn[A] Deque::retain(self : Deque[A], f : (A) -> Bool) -> Unit {
#| guard !self.is_empty() else { return }
#| let { head, buf, .. } = self
#| let cap = buf.length()
#| let (front, back) = self.as_views()
#| let (idx, kept_len) = for cur in front; idx = head, kept_len = 0 {
#| if f(cur) {
#| buf[idx] = cur
#| continue idx + 1, kept_len + 1
#| }
#| continue idx, kept_len
#| } nobreak {
#| (idx, kept_len)
#| }
#| if back.is_empty() {
#| self.truncate(kept_len)
#| return
#| }
#| let kept_len = for cur in back; idx = idx, kept_len = kept_len {
#| let idx = if idx == cap { 0 } else { idx }
#| if f(cur) {
#| buf[idx] = cur
#| continue idx + 1, kept_len + 1
#| }
#| continue idx, kept_len
#| } nobreak {
#| kept_len
#| }
#| self.truncate(kept_len)
#|}
#|#alias(iterator, deprecated)
#|pub fn[A] Deque::iter(self : Deque[A]) -> Iter[A] {
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| let mut index = 0
#| Iter(
#| fn() {
#| guard index < len else { None }
#| let elem = buf.unsafe_get(wrap_index(head + index, cap))
#| index += 1
#| Some(elem)
#| },
#| size_hint=len,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[A] Deque::iter2(self : Deque[A]) -> Iter2[Int32, A] {
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| let mut index = 0
#| Iter(
#| fn() {
#| guard index < len else { None }
#| let result = (index, buf.unsafe_get(wrap_index(head + index, cap)))
#| index += 1
#| Some(result)
#| },
#| size_hint=len,
#| )
#|}
#|#alias(rev_iterator, deprecated)
#|pub fn[A] Deque::rev_iter(self : Deque[A]) -> Iter[A] {
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| let mut index = len
#| Iter(
#| fn() {
#| guard index > 0 else { None }
#| index -= 1
#| Some(buf.unsafe_get(wrap_index(head + index, cap)))
#| },
#| size_hint=len,
#| )
#|}
#|#alias(rev_iterator2, deprecated)
#|pub fn[A] Deque::rev_iter2(self : Deque[A]) -> Iter2[Int32, A] {
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| let mut rev_index = len
#| Iter(
#| fn() {
#| guard rev_index > 0 else { None }
#| let index = len - rev_index
#| rev_index -= 1
#| Some((index, buf.unsafe_get(wrap_index(head + rev_index, cap))))
#| },
#| size_hint=len,
#| )
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A] Deque::from_iter(iter : Iter[A]) -> Deque[A] {
#| let dq = new_deque(0)
#| while iter.next() is Some(e) {
#| dq.push_back(e)
#| }
#| dq
#|}
#|pub fn[A] Deque::to_array(self : Deque[A]) -> Array[A] {
#| let len = self.length()
#| if len == 0 {
#| return []
#| }
#| let xs = Array::unsafe_make_uninit(len)
#| for i in 0.. String {
#| self.iter().join(separator)
#|}
#|pub impl[A : @json.ToJson] @json.ToJson for Deque[A] with fn to_json(
#| self : Deque[A],
#|) -> @json.Json {
#| [
#| for x in self => x
#| ]
#|}
#|pub impl[A : @json.FromJson] @json.FromJson for Deque[A] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(arr) else {
#| raise path.error("Deque::from_json: expected array")
#| }
#| let len = arr.length()
#| let buf = UninitializedArray::make(len)
#| for i, x in arr {
#| buf[i] = @json.FromJson::from_json(x, path.add_index(i))
#| }
#| { len, buf, head: 0, }
#|}
#|pub fn[A] Deque::chunks(self : Deque[A], size : Int32) -> Deque[Deque[A]] {
#| guard! size > 0
#| let chunks = from_array([])
#| for i = 0; i < self.length(); {
#| let chunk = Deque([], capacity=size)
#| let i = for _ in 0..= self.length() {
#| break i
#| }
#| chunk.push_back(self[i])
#| continue i + 1
#| } nobreak {
#| i
#| }
#| chunks.push_back(chunk)
#| continue i
#| }
#| chunks
#|}
#|#locals(pred)
#|pub fn[A] Deque::chunk_by(
#| self : Deque[A],
#| pred : (A, A) -> Bool raise?,
#|) -> Deque[Deque[A]] raise? {
#| let chunks = from_array([])
#| for i = 0; i < self.length(); {
#| let chunk = from_array([])
#| chunk.push_back(self[i])
#| let i = for i = i + 1; i < self.length() && pred(self[i - 1], self[i]); {
#| chunk.push_back(self[i])
#| continue i + 1
#| } nobreak {
#| i
#| }
#| chunks.push_back(chunk)
#| continue i
#| }
#| chunks
#|}
#|pub fn[A] Deque::flatten(self : Deque[Deque[A]]) -> Deque[A] {
#| let len = for deque in self; len = 0 {
#| continue len + deque.length()
#| } nobreak {
#| len
#| }
#| let target = Deque::{ buf: UninitializedArray::make(len), len, head: 0, }
#| for deque in self; i = 0 {
#| let (front, back) = deque.as_views()
#| target.buf.unsafe_blit(i, deque.buf, front.start_offset(), front.length())
#| let i = i + front.length()
#| target.buf.unsafe_blit(i, deque.buf, back.start_offset(), back.length())
#| continue i + back.length()
#| }
#| target
#|}
#|pub fn[A] Deque::drain(
#| self : Deque[A],
#| start~ : Int32,
#| len? : Int32,
#|) -> Deque[A] {
#| guard start >= 0 && start <= self.len else {
#| abort(
#| "Deque::drain: start index out of bounds (start=\{start}, deque.length()=\{self.len})",
#| )
#| }
#| let len = match len {
#| Some(l) => {
#| guard l >= 0 && l <= self.len - start else {
#| abort(
#| "Deque::drain: len out of bounds (start=\{start}, len=\{l}, deque.length()=\{self.len})",
#| )
#| }
#| l
#| }
#| None => self.len - start
#| }
#| if len == 0 {
#| return new_deque(0)
#| }
#| let deque = Deque::{ buf: UninitializedArray::make(len), len, head: 0, }
#| let (front, back) = self.as_views()
#| if start < front.length() {
#| let front_max_drain = front.length() - start
#| if len <= front_max_drain {
#| deque.buf.unsafe_blit(0, self.buf, front.start_offset() + start, len)
#| if start == 0 && len == front_max_drain {
#| self.head = 0
#| } else if len < front_max_drain || back.length() != 0 {
#| let new_head = front.start_offset() + len
#| self.buf.unsafe_blit(new_head, self.buf, front.start_offset(), start)
#| self.head = new_head
#| }
#| } else {
#| deque.buf.unsafe_blit(
#| 0,
#| self.buf,
#| front.start_offset() + start,
#| front_max_drain,
#| )
#| deque.buf.unsafe_blit(front_max_drain, self.buf, 0, len - front_max_drain)
#| if start > 0 {
#| let back_remaining = len - front_max_drain
#| let back_len = back.length() - back_remaining
#| if back_len != 0 {
#| let new_head = front.start_offset() + front_max_drain
#| self.buf.unsafe_blit(new_head, self.buf, front.start_offset(), start)
#| self.head = new_head
#| self.buf.unsafe_blit(0, self.buf, back_remaining, back_len)
#| }
#| } else {
#| self.head = len - front_max_drain
#| }
#| }
#| } else {
#| let back_start = start - front.length()
#| deque.buf.unsafe_blit(0, self.buf, back_start, len)
#| self.buf.unsafe_blit(
#| back_start,
#| self.buf,
#| back_start + len,
#| back.length() - back_start - len,
#| )
#| }
#| self.len -= len
#| if self.len == 0 {
#| self.head = 0
#| }
#| deque
#|}
#|test "weird behavior after drain" {
#| let deque = new_deque(16)
#| deque.push_back(1)
#| deque.push_back(2)
#| deque.push_back(3)
#| deque.push_back(4)
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| deque.push_back(5)
#| ignore(deque.pop_front())
#| deque.push_back(6)
#| ignore(deque.pop_front())
#| deque.push_back(7)
#| ignore(deque.pop_front())
#| deque.push_back(8)
#| ignore(deque.pop_front())
#| deque.push_back(9)
#| deque.push_back(10)
#| deque.push_back(11)
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| deque.push_back(12)
#| deque.push_back(13)
#| ignore(deque.pop_front())
#| deque.push_back(14)
#| ignore(deque.pop_front())
#| ignore(deque.drain(start=0, len=2))
#| deque.push_back(15)
#| ignore(deque.pop_front())
#| deque.push_back(16)
#| ignore(deque.pop_front())
#| deque.push_back(17)
#| ignore(deque.pop_front())
#| deque.push_back(18)
#| ignore(deque.pop_front())
#| deque.push_back(19)
#| ignore(deque.pop_front())
#| deque.push_back(20)
#| deque.push_back(21)
#| deque.push_back(22)
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| deque.push_back(23)
#| deque.push_back(24)
#| deque.push_back(25)
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| deque.push_back(26)
#| deque.push_back(27)
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| ignore(deque.pop_front())
#| deque.push_back(28)
#| ignore(deque.pop_front())
#| deque.push_back(29)
#| ignore(deque.pop_front())
#| deque.push_back(30)
#| @debug.debug_inspect(
#| deque.as_views(),
#| content=(
#| #|(, )
#| ),
#| )
#| ignore(deque.drain(start=0, len=2))
#| @debug.debug_inspect(
#| deque.as_views(),
#| content=(
#| #|(, )
#| ),
#| )
#| deque.push_back(31)
#| deque.push_back(32)
#| @debug.debug_inspect(
#| deque.as_views(),
#| content=(
#| #|(, )
#| ),
#| )
#| ignore(deque.pop_front())
#|}
#|#locals(cmp)
#|pub fn[A] Deque::binary_search_by(
#| self : Deque[A],
#| cmp : (A) -> Int32 raise?,
#|) -> Result[Int32, Int32] raise? {
#| let len = self.len
#| for i = 0, j = len; i < j; {
#| let h = i + (j - i) / 2
#| let ord = cmp(self[h])
#| if ord < 0 {
#| continue h + 1, j
#| } else {
#| continue i, h
#| }
#| } nobreak {
#| if i < len && cmp(self[i]) == 0 {
#| Ok(i)
#| } else {
#| Err(i)
#| }
#| }
#|}
#|pub fn[A] Deque::get(self : Deque[A], index : Int32) -> A? {
#| if index >= 0 && index < self.len {
#| let physical_index = wrap_index(self.head + index, self.buf.length())
#| Some(self.buf[physical_index])
#| } else {
#| None
#| }
#|}
#|pub fn[A : Compare] Deque::binary_search(
#| self : Deque[A],
#| value : A,
#|) -> Result[Int32, Int32] {
#| self.binary_search_by(x => x.compare(value))
#|}
#|pub impl[A : Compare] Compare for Deque[A] with fn compare(self, other) {
#| let len_self = self.length()
#| let len_other = other.length()
#| let cmp = len_self.compare(len_other)
#| guard cmp is 0 else { return cmp }
#| for i in 0.. Unit {
#| guard self.len > 0 else { return }
#| let cap = self.buf.length()
#| for _ in 0..<(self.len / 2); left = self.head, right = self.tail_index() {
#| let temp = self.buf[left]
#| self.buf[left] = self.buf[right]
#| self.buf[right] = temp
#| continue (left + 1) % cap, (right - 1 + cap) % cap
#| }
#|}
#|pub fn[A] Deque::rev(self : Deque[A]) -> Deque[A] {
#| let len = self.len
#| let new_buf = UninitializedArray::make(len)
#| for i in 0.. Int32,
#|) -> Unit {
#| let { buf, head, len, } = self
#| let cap = buf.length()
#| for i in len>..1 {
#| let j = rand(i + 1)
#| guard j >= 0 && j <= i else { index_out_of_bounds(i + 1, j) }
#| let i_pos = wrap_index(head + i, cap)
#| let j_pos = wrap_index(head + j, cap)
#| let tmp = buf[i_pos]
#| buf[i_pos] = buf[j_pos]
#| buf[j_pos] = tmp
#| }
#|}
#|pub fn[A] Deque::shuffle(self : Deque[A], rand~ : (Int32) -> Int32) -> Deque[A] {
#| let new_deque = self.copy()
#| new_deque.shuffle_in_place(rand~)
#| new_deque
#|}
#|pub impl[A] Default for Deque[A] with fn default() {
#| Deque([])
#|}
),
"extends.mbt": (
#|pub extend Deque with Add::{add}
#|pub extend Deque with Compare::{compare}
#|pub extend Deque with Eq::{equal}
#|pub extend Deque with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with Default::{default}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with @json.FromJson::{from_json}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Deque with @json.ToJson::{to_json}
),
"types.mbt": (
#|struct Deque[A] {
#| mut buf : UninitializedArray[A]
#| mut len : Int32
#| mut head : Int32
#|}
),
},
)
///|
let moonbitlang_core_diff_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/diff",
deps={},
files={
"backpointer.mbt": (
#|priv struct BackPointer {
#| value : (Int32, Int32)
#| prev : BackPointer?
#|}
#|fn BackPointer::to_array(self : BackPointer) -> Array[(Int32, Int32)] {
#| let result = []
#| let mut self = self
#| while self.prev is Some(prev) {
#| result.push(self.value)
#| self = prev
#| }
#| result.push(self.value)
#| return result.rev()
#|}
),
"compact.mbt": (
#|priv struct CompactedElements[T]((ArrayView[T], FixedArray[Int32]))
#|#alias("_[_]")
#|fn[T] CompactedElements::at(self : CompactedElements[T], index : Int32) -> T {
#| let (source, indices) = self.0
#| source[indices[index]]
#|}
#|fn[T] CompactedElements::get_index(
#| self : CompactedElements[T],
#| index : Int32,
#|) -> Int32 {
#| let (_, indices) = self.0
#| indices[index]
#|}
#|fn[T] CompactedElements::indices_length(self : CompactedElements[T]) -> Int32 {
#| self.0.1.length()
#|}
#|fn[T] CompactedElements::source_length(self : CompactedElements[T]) -> Int32 {
#| self.0.0.length()
#|}
),
"diff.mbt": (
#|#valtype
#|priv struct Partition {
#| lhs_midpoint : Int32 // position of midpoint in sequence lhs
#| rhs_midpoint : Int32 // position of midpoint in sequence rhs
#| lo_minimal : Bool // whether left half is optimal
#| hi_minimal : Bool // whether right half is optimal
#|}
#|fn[T : Eq] diag(
#| forward_search_diagonal~ : FixedArray[Int32], // forward search diagonal array - stores farthest reachable point on each diagonal
#| backward_search_diagonal~ : FixedArray[Int32], // backward search diagonal array - stores nearest reachable point on each diagonal
#| diagonal_shift~ : Int32, // diagonal index shift: store diagonal k at [sh + k], including negative k and k±1/k±2 neighbors
#| lhs~ : CompactedElements[T], // compacted elements for sequence lhs
#| rhs~ : CompactedElements[T], // compacted elements for sequence rhs
#| lhs_offset~ : Int32, // starting position of sequence lhs - defines left boundary of search window
#| lhs_limit~ : Int32, // ending position of sequence lhs - defines right boundary of search window
#| rhs_offset~ : Int32, // starting position of sequence rhs - defines upper boundary of search window
#| rhs_limit~ : Int32, // ending position of sequence rhs - defines lower boundary of search window
#| too_expensive~ : Int32, // computation cost threshold - use heuristic algorithm when exceeded
#| find_minimal~ : Bool, // optimality flag - whether optimal solution must be found
#|) -> Partition {
#| let dmin = lhs_offset - rhs_limit // minimum valid diagonal - corresponds to maximum insertions
#| let dmax = lhs_limit - rhs_offset // maximum valid diagonal - corresponds to maximum deletions
#| let fmid = lhs_offset - rhs_offset // center diagonal for forward search - diagonal corresponding to starting point
#| let bmid = lhs_limit - rhs_limit // center diagonal for backward search - diagonal corresponding to end point
#| let odd = ((fmid - bmid) & 1) != 0
#| forward_search_diagonal[diagonal_shift + fmid] = lhs_offset // forward search starts from (lhs_offset, rhs_offset)
#| backward_search_diagonal[diagonal_shift + bmid] = lhs_limit // backward search starts from (lhs_limit, rhs_limit)
#| for c = 1, fmin = fmid, fmax = fmid, bmin = bmid, bmax = bmid {
#| let fmin = if fmin > dmin {
#| forward_search_diagonal[diagonal_shift + fmin - 2] = -1 // mark unused diagonal
#| fmin - 1 // extend to next diagonal
#| } else {
#| fmin + 1 // boundary constraint
#| }
#| let fmax = if fmax < dmax {
#| forward_search_diagonal[diagonal_shift + fmax + 2] = -1 // mark unused diagonal
#| fmax + 1 // extend to previous diagonal
#| } else {
#| fmax - 1 // boundary constraint
#| }
#| let mut forward_result : Partition? = None
#| let mut d = fmax
#| while d >= fmin {
#| let tlo = forward_search_diagonal[diagonal_shift + d - 1]
#| let thi = forward_search_diagonal[diagonal_shift + d + 1]
#| let x = if tlo >= thi { tlo + 1 } else { thi }
#| let (x_cur, y_cur) = for x_cur = x, y_cur = x - d {
#| if x_cur < lhs_limit && y_cur < rhs_limit && lhs[x_cur] == rhs[y_cur] {
#| continue x_cur + 1, y_cur + 1
#| } else {
#| break (x_cur, y_cur)
#| }
#| }
#| forward_search_diagonal[diagonal_shift + d] = x_cur
#| if odd &&
#| bmin <= d &&
#| d <= bmax &&
#| backward_search_diagonal[diagonal_shift + d] <=
#| forward_search_diagonal[diagonal_shift + d] {
#| forward_result = Some({
#| lhs_midpoint: x_cur,
#| rhs_midpoint: y_cur,
#| lo_minimal: true, // forward path is optimal
#| hi_minimal: true, // backward path is also optimal
#| })
#| break // found meeting point, exit early
#| }
#| d -= 2 // Myers algorithm characteristic: only check diagonals with same parity
#| }
#| match forward_result {
#| Some(result) => break result
#| None => ()
#| }
#| let bmin = if bmin > dmin {
#| backward_search_diagonal[diagonal_shift + bmin - 2] = @int.MAX_VALUE // mark unused diagonal as max value
#| bmin - 1
#| } else {
#| bmin + 1
#| }
#| let bmax = if bmax < dmax {
#| backward_search_diagonal[diagonal_shift + bmax + 2] = @int.MAX_VALUE // mark unused diagonal as max value
#| bmax + 1
#| } else {
#| bmax - 1
#| }
#| let mut backward_result : Partition? = None
#| let mut d = bmax
#| while d >= bmin {
#| let tlo = backward_search_diagonal[diagonal_shift + d - 1]
#| let thi = backward_search_diagonal[diagonal_shift + d + 1]
#| let x = if tlo < thi { tlo } else { thi - 1 }
#| let (x_cur, y_cur) = for x_cur = x, y_cur = x - d {
#| if x_cur > lhs_offset &&
#| y_cur > rhs_offset &&
#| lhs[x_cur - 1] == rhs[y_cur - 1] {
#| continue x_cur - 1, y_cur - 1
#| } else {
#| break (x_cur, y_cur)
#| }
#| }
#| backward_search_diagonal[diagonal_shift + d] = x_cur
#| if !odd &&
#| fmin <= d &&
#| d <= fmax &&
#| backward_search_diagonal[diagonal_shift + d] <=
#| forward_search_diagonal[diagonal_shift + d] {
#| backward_result = Some({
#| lhs_midpoint: x_cur,
#| rhs_midpoint: y_cur,
#| lo_minimal: true,
#| hi_minimal: true,
#| })
#| break
#| }
#| d -= 2
#| }
#| match backward_result {
#| Some(result) => break result
#| None => ()
#| }
#| if !find_minimal && c >= too_expensive {
#| let mut fxybest = -1
#| let mut fxbest = fmax
#| let mut d = fmax
#| while d >= fmin {
#| let x = lhs_limit.min(forward_search_diagonal[diagonal_shift + d]) // limit to valid range
#| let y = x - d
#| let (x, y) = if rhs_limit < y {
#| (rhs_limit + d, rhs_limit)
#| } else {
#| (x, y)
#| }
#| if fxybest < x + y {
#| fxybest = x + y
#| fxbest = x
#| }
#| d -= 2
#| }
#| let mut bxybest = @int.MAX_VALUE
#| let mut bxbest = bmax
#| let mut d = bmax
#| while d >= bmin {
#| let x = lhs_offset.max(backward_search_diagonal[diagonal_shift + d]) // limit to valid range
#| let y = x - d
#| let (x, y) = if y < rhs_offset {
#| (rhs_offset + d, rhs_offset)
#| } else {
#| (x, y)
#| }
#| if x + y < bxybest {
#| bxybest = x + y
#| bxbest = x
#| }
#| d -= 2
#| }
#| if lhs_limit + rhs_limit - bxybest < fxybest - (lhs_offset + rhs_offset) {
#| break {
#| lhs_midpoint: fxbest,
#| rhs_midpoint: fxybest - fxbest,
#| lo_minimal: true,
#| hi_minimal: false, // right half is not optimal
#| }
#| } else {
#| break {
#| lhs_midpoint: bxbest,
#| rhs_midpoint: bxybest - bxbest,
#| lo_minimal: false, // left half is not optimal
#| hi_minimal: true,
#| }
#| }
#| } else {
#| continue c + 1, fmin, fmax, bmin, bmax
#| }
#| }
#|}
#|fn[T : Eq] diff_loop(
#| cutoff : Int32?, // computation cost threshold
#| lhs : CompactedElements[T],
#| rhs : CompactedElements[T],
#|) -> (FixedArray[Bool], FixedArray[Bool]) {
#| let forward_search_diagonal = FixedArray::make(
#| lhs.indices_length() + rhs.indices_length() + 3,
#| 0,
#| ) // forward search array
#| let backward_search_diagonal = FixedArray::make(
#| lhs.indices_length() + rhs.indices_length() + 3,
#| 0,
#| ) // backward search array
#| let diagonal_shift = rhs.indices_length() + 1
#| let too_expensive = match cutoff {
#| Some(c) => c
#| None => {
#| let diags = lhs.indices_length() + rhs.indices_length() + 3
#| for acc = 1, diags_cur = diags {
#| if diags_cur != 0 {
#| continue acc << 1, diags_cur >> 2
#| } else {
#| break acc.max(4096) // at least 4096, ensuring reasonable performance
#| }
#| }
#| }
#| }
#| let lhs_change_markers = FixedArray::make(lhs.source_length(), true)
#| let rhs_change_markers = FixedArray::make(rhs.source_length(), true)
#| for i in 0.. Unit {
#| let (lhs_offset, rhs_offset) = for lhs_offset = lhs_offset, rhs_offset = rhs_offset {
#| if lhs_offset < lhs_limit &&
#| rhs_offset < rhs_limit &&
#| lhs[lhs_offset] == rhs[rhs_offset] {
#| continue lhs_offset + 1, rhs_offset + 1
#| } else {
#| break (lhs_offset, rhs_offset)
#| }
#| }
#| let (lhs_limit, rhs_limit) = for lhs_limit = lhs_limit, rhs_limit = rhs_limit {
#| if lhs_limit > lhs_offset &&
#| rhs_limit > rhs_offset &&
#| lhs[lhs_limit - 1] == rhs[rhs_limit - 1] {
#| continue lhs_limit - 1, rhs_limit - 1
#| } else {
#| break (lhs_limit, rhs_limit)
#| }
#| }
#| if lhs_offset == lhs_limit {
#| for y in rhs_offset.. FixedArray[Int32] {
#| let n = a.length()
#| let present : Map[T, Bool] = Map([], capacity=b.length())
#| for elem in b {
#| present[elem] = true
#| }
#| let ai = FixedArray::make(n, 0) // temporary storage for valid indices
#| let mut k = 0 // valid element count
#| for i, elem in a {
#| guard present.get(elem) is Some(_) else { () } // doesn't exist in b, skip
#| ai[k] = i // record original index of this element
#| k += 1
#| }
#| FixedArray::makei(k, i => ai[i])
#|}
#|fn[T : Eq + Hash] diff_(
#| cutoff : Int32?,
#| lhs : ArrayView[T],
#| rhs : ArrayView[T],
#|) -> (FixedArray[Bool], FixedArray[Bool]) {
#| let lhs_indices = make_indexer(lhs, rhs) // indices of lhs elements that exist in rhs
#| let rhs_indices = make_indexer(rhs, lhs) // indices of rhs elements that exist in lhs
#| let lhs = CompactedElements((lhs, lhs_indices))
#| let rhs = CompactedElements((rhs, rhs_indices))
#| diff_loop(cutoff, lhs, rhs)
#|}
#|fn[T : Eq + Hash] append_myers_matches(
#| builder : EditBuilder,
#| cutoff : Int32?,
#| old : ArrayView[T],
#| new : ArrayView[T],
#| old_offset : Int32,
#| new_offset : Int32,
#|) -> Unit {
#| if old.is_empty() || new.is_empty() {
#| return
#| }
#| let (old_changed, new_changed) = diff_(cutoff, old, new)
#| builder.append_unchanged_runs(
#| old_changed, new_changed, old_offset, new_offset,
#| )
#|}
#|fn EditBuilder::append_unchanged_runs(
#| self : EditBuilder,
#| old_changed : FixedArray[Bool],
#| new_changed : FixedArray[Bool],
#| old_offset : Int32,
#| new_offset : Int32,
#|) -> Unit {
#| let mut i = 0
#| let mut j = 0
#| while i < old_changed.length() && j < new_changed.length() {
#| if old_changed[i] {
#| i += 1
#| } else if new_changed[j] {
#| j += 1
#| } else {
#| let start = i
#| let new_start = j
#| i += 1
#| j += 1
#| while i < old_changed.length() &&
#| j < new_changed.length() &&
#| !old_changed[i] &&
#| !new_changed[j] {
#| i += 1
#| j += 1
#| }
#| self.push_run(old_offset + start, new_offset + new_start, i - start)
#| }
#| }
#|}
#|fn[T : Eq + Hash] append_patience_matches(
#| builder : EditBuilder,
#| cutoff : Int32?,
#| old : ArrayView[T],
#| new : ArrayView[T],
#| old_offset : Int32,
#| new_offset : Int32,
#|) -> Unit {
#| let anchors = unique_lcs(old~, new~)
#| if anchors.is_empty() {
#| append_myers_matches(builder, cutoff, old, new, old_offset, new_offset)
#| return
#| }
#| let mut prev_old_idx = 0
#| let mut prev_new_idx = 0
#| for (old_idx, new_idx) in anchors {
#| append_myers_matches(
#| builder,
#| cutoff,
#| old.exact_view(start=prev_old_idx, end=old_idx),
#| new.exact_view(start=prev_new_idx, end=new_idx),
#| old_offset + prev_old_idx,
#| new_offset + prev_new_idx,
#| )
#| builder.push_run(old_offset + old_idx, new_offset + new_idx, 1)
#| prev_old_idx = old_idx + 1
#| prev_new_idx = new_idx + 1
#| }
#| append_myers_matches(
#| builder,
#| cutoff,
#| old.exact_view(start=prev_old_idx, end=old.length()),
#| new.exact_view(start=prev_new_idx, end=new.length()),
#| old_offset + prev_old_idx,
#| new_offset + prev_new_idx,
#| )
#|}
#|#derive(Eq, @debug.Debug)
#|pub(all) enum DiffAlgorithm {
#| Myers
#| Patience
#|}
#|struct Diff[T] {
#| old : ArrayView[T]
#| new : ArrayView[T]
#| edits : Array[Edit]
#|}
#|pub fn[T] Diff::edits(self : Diff[T]) -> ArrayView[Edit] {
#| self.edits
#|}
#|pub fn[T] Diff::old_view(self : Diff[T]) -> ArrayView[T] {
#| self.old
#|}
#|pub fn[T] Diff::new_view(self : Diff[T]) -> ArrayView[T] {
#| self.new
#|}
#|pub fn[T : Hash + Eq] Diff::Diff(
#| old~ : ArrayView[T],
#| new~ : ArrayView[T],
#| cutoff? : Int32,
#| algorithm~ : DiffAlgorithm = Myers,
#|) -> Diff[T] {
#| let builder : EditBuilder = {
#| edits: Array(capacity=old.length() + new.length()),
#| old_pos: 0,
#| new_pos: 0,
#| equal_len: 0,
#| }
#| match algorithm {
#| Patience => append_patience_matches(builder, cutoff, old, new, 0, 0)
#| Myers => append_myers_matches(builder, cutoff, old, new, 0, 0)
#| }
#| builder.flush(old.length(), new.length())
#| { old, new, edits: builder.edits, }
#|}
#|priv struct EditBuilder {
#| edits : Array[Edit]
#| mut old_pos : Int32
#| mut new_pos : Int32
#| mut equal_len : Int32
#|}
#|fn EditBuilder::push_run(
#| self : EditBuilder,
#| old_idx : Int32,
#| new_idx : Int32,
#| len : Int32,
#|) -> Unit {
#| if self.old_pos != old_idx || self.new_pos != new_idx {
#| self.flush(old_idx, new_idx)
#| }
#| self.old_pos = old_idx + len
#| self.new_pos = new_idx + len
#| self.equal_len += len
#|}
#|fn EditBuilder::flush(
#| self : EditBuilder,
#| old_end : Int32,
#| new_end : Int32,
#|) -> Unit {
#| if self.equal_len > 0 {
#| self.edits.push(
#| Equal(
#| old_index=self.old_pos - self.equal_len,
#| new_index=self.new_pos - self.equal_len,
#| len=self.equal_len,
#| ),
#| )
#| self.equal_len = 0
#| }
#| if self.old_pos < old_end {
#| self.edits.push(
#| Delete(
#| old_index=self.old_pos,
#| old_len=old_end - self.old_pos,
#| new_index=self.new_pos,
#| ),
#| )
#| }
#| if self.new_pos < new_end {
#| self.edits.push(
#| Insert(
#| old_index=old_end,
#| new_index=self.new_pos,
#| new_len=new_end - self.new_pos,
#| ),
#| )
#| }
#|}
#|pub fn[T] Diff::group(self : Diff[T], context~ : Int32 = 3) -> Array[Hunk[T]] {
#| group_edits(self.edits, context~, old=self.old, new=self.new)
#|}
),
"edit.mbt": (
#|#derive(Eq, @debug.Debug)
#|pub enum Edit {
#| Delete(old_index~ : Int32, old_len~ : Int32, new_index~ : Int32)
#| Insert(old_index~ : Int32, new_index~ : Int32, new_len~ : Int32)
#| Equal(old_index~ : Int32, new_index~ : Int32, len~ : Int32)
#|}
#|fn Edit::old_range(self : Edit) -> (Int32, Int32) {
#| match self {
#| Insert(old_index~, ..) => (old_index, old_index)
#| Delete(old_index~, old_len~, ..) => (old_index, old_index + old_len)
#| Equal(old_index~, len~, ..) => (old_index, old_index + len)
#| }
#|}
#|fn Edit::new_range(self : Edit) -> (Int32, Int32) {
#| match self {
#| Insert(new_index~, new_len~, ..) => (new_index, new_index + new_len)
#| Delete(new_index~, ..) => (new_index, new_index)
#| Equal(new_index~, len~, ..) => (new_index, new_index + len)
#| }
#|}
#|fn[T] Edit::view_from(
#| self : Edit,
#| old~ : ArrayView[T],
#| new~ : ArrayView[T],
#|) -> ArrayView[T] {
#| match self {
#| Insert(new_index~, new_len~, ..) =>
#| new.exact_view(start=new_index, end=new_index + new_len)
#| Delete(old_index~, old_len~, ..) =>
#| old.exact_view(start=old_index, end=old_index + old_len)
#| Equal(old_index~, len~, ..) =>
#| old.exact_view(start=old_index, end=old_index + len)
#| }
#|}
#|fn[T] group_edits(
#| edits : Array[Edit],
#| context~ : Int32 = 3,
#| old~ : ArrayView[T],
#| new~ : ArrayView[T],
#|) -> Array[Hunk[T]] {
#| guard context >= 0 else { abort("context must be non-negative") }
#| if edits.is_empty() {
#| return []
#| }
#| let n = edits.length()
#| let mut pending : Array[Edit] = Array()
#| let result : Array[Hunk[T]] = Array()
#| for i, edit in edits {
#| match edit {
#| Equal(old_index~, new_index~, len~) => {
#| let (old_index, new_index, len) = if i == 0 {
#| let offset = len.saturating_sub(context)
#| (old_index + offset, new_index + offset, len - offset)
#| } else {
#| (old_index, new_index, len)
#| }
#| let len = if i == n - 1 {
#| len - len.saturating_sub(context)
#| } else {
#| len
#| }
#| if len - context > context {
#| if context > 0 {
#| pending.push(Equal(old_index~, new_index~, len=context))
#| }
#| result.push({ edits: pending, old, new, })
#| let offset = len.saturating_sub(context)
#| pending = if context > 0 {
#| [
#| Equal(
#| old_index=old_index + offset,
#| new_index=new_index + offset,
#| len=len - offset,
#| ),
#| ]
#| } else {
#| []
#| }
#| } else if len > 0 {
#| pending.push(Equal(old_index~, new_index~, len~))
#| }
#| }
#| _ => pending.push(edit)
#| }
#| }
#| if !(pending is [] || pending is [Equal(_)]) {
#| result.push({ edits: pending, old, new, })
#| }
#| result
#|}
#|fn Int32::saturating_sub(self : Int32, subtrahend : Int32) -> Int32 {
#| if self < subtrahend {
#| 0
#| } else {
#| self - subtrahend
#| }
#|}
),
"extends.mbt": (
#|pub extend DiffAlgorithm with Eq::{equal}
#|pub extend Edit with Eq::{equal}
#|pub extend LevenshteinEdit with Eq::{equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend DiffAlgorithm with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend DiffAlgorithm with Eq::{not_equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Edit with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Edit with Eq::{not_equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend LevenshteinEdit with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend LevenshteinEdit with Eq::{not_equal}
),
"hunk.mbt": (
#|priv struct Range(Int32, Int32) // zero-based start and exclusive end
#|impl Show for Range with fn output(self, logger) {
#| let mut beginning = self.0 + 1 // from array index to line number
#| let len = self.1 - self.0
#| if len == 1 {
#| logger.write_object(beginning)
#| } else {
#| if len == 0 {
#| beginning -= 1
#| }
#| logger <+ "\{beginning},\{len}"
#| }
#|}
#|priv struct HunkHeader(Range, Range)
#|fn HunkHeader::new(edits : ArrayView[Edit]) -> Self {
#| let (first_old_start, first_old_end) = edits[0].old_range()
#| let (first_new_start, first_new_end) = edits[0].new_range()
#| let mut old_end = first_old_end
#| let mut new_end = first_new_end
#| let mut found_old_end = false
#| let mut found_new_end = false
#| for i in edits.length()>..0 {
#| let edit = edits[i]
#| let (edit_old_start, edit_old_end) = edit.old_range()
#| let (edit_new_start, edit_new_end) = edit.new_range()
#| if !found_old_end && edit_old_start != edit_old_end {
#| old_end = edit_old_end
#| found_old_end = true
#| }
#| if !found_new_end && edit_new_start != edit_new_end {
#| new_end = edit_new_end
#| found_new_end = true
#| }
#| if found_old_end && found_new_end {
#| break
#| }
#| }
#| HunkHeader(Range(first_old_start, old_end), Range(first_new_start, new_end))
#|}
#|impl Show for HunkHeader with fn output(self, logger) {
#| logger <+ "@@ -\{self.0} +\{self.1} @@"
#|}
#|struct Hunk[T] {
#| edits : ArrayView[Edit]
#| old : ArrayView[T]
#| new : ArrayView[T]
#|}
#|pub fn[T] Hunk::header(self : Hunk[T]) -> String {
#| Show::to_string(HunkHeader::new(self.edits))
#|}
#|pub fn[T] Hunk::edits(self : Hunk[T]) -> ArrayView[Edit] {
#| self.edits
#|}
#|pub fn[T] Hunk::old_view(self : Hunk[T]) -> ArrayView[T] {
#| self.old
#|}
#|pub fn[T] Hunk::new_view(self : Hunk[T]) -> ArrayView[T] {
#| self.new
#|}
#|pub fn[T] Hunk::render(self : Hunk[T], show~ : (T) -> String) -> String {
#| let buf = StringBuilder()
#| buf <+ "\{self.header()}\n"
#| for edit in self.edits {
#| let (prefix, slice) = match edit {
#| Insert(..) => ("+", edit.view_from(old=self.old, new=self.new))
#| Delete(..) => ("-", edit.view_from(old=self.old, new=self.new))
#| Equal(..) => (" ", edit.view_from(old=self.old, new=self.new))
#| }
#| for s in slice {
#| buf <+ "\{prefix}\{show(s)}\n"
#| }
#| }
#| buf.to_string()
#|}
),
"levenshtein.mbt": (
#|pub using @edit_distance {
#| edit_distance,
#| edit_distance_within,
#| edit_distance_str,
#| edit_distance_str_within,
#|}
#|#derive(Eq, @debug.Debug)
#|pub enum LevenshteinEdit {
#| Delete(old_index~ : Int32, old_len~ : Int32, new_index~ : Int32)
#| Insert(old_index~ : Int32, new_index~ : Int32, new_len~ : Int32)
#| Equal(old_index~ : Int32, new_index~ : Int32, len~ : Int32)
#| Replace(old_index~ : Int32, new_index~ : Int32, len~ : Int32)
#|}
#|fn[T : Eq] common_prefix_len(old : ArrayView[T], new : ArrayView[T]) -> Int32 {
#| let max = old.length().min(new.length())
#| let mut i = 0
#| while i < max && old[i] == new[i] {
#| i += 1
#| }
#| i
#|}
#|pub fn[T : Eq] levenshtein_edits(
#| old~ : ArrayView[T],
#| new~ : ArrayView[T],
#|) -> Array[LevenshteinEdit] {
#| let prefix = common_prefix_len(old, new)
#| let a = old.exact_view(start=prefix)
#| let b = new.exact_view(start=prefix)
#| let m = a.length()
#| let n = b.length()
#| guard (m.to_int64() + 1L) * (n.to_int64() + 1L) <= @int.MAX_VALUE.to_int64() else {
#| abort("levenshtein_edits: input too large")
#| }
#| let width = n + 1
#| let bp : FixedArray[Byte] = FixedArray::make((m + 1) * width, 0)
#| let mut next_row = FixedArray::makei(width, j => n - j)
#| for j in 0.. 0 {
#| edits.push(Equal(old_index=0, new_index=0, len=prefix))
#| }
#| let mut i = 0
#| let mut j = 0
#| while i < m || j < n {
#| match bp[i * width + j] {
#| 0 => {
#| let eq = a[i] == b[j]
#| let start_i = i
#| let start_j = j
#| let mut len = 0
#| while i < m && j < n && bp[i * width + j] == 0 && (a[i] == b[j]) == eq {
#| i += 1
#| j += 1
#| len += 1
#| }
#| if eq {
#| edits.push(
#| Equal(old_index=prefix + start_i, new_index=prefix + start_j, len~),
#| )
#| } else {
#| edits.push(
#| Replace(
#| old_index=prefix + start_i,
#| new_index=prefix + start_j,
#| len~,
#| ),
#| )
#| }
#| }
#| 1 => {
#| let start_i = i
#| let mut len = 0
#| while i < m && bp[i * width + j] == 1 {
#| i += 1
#| len += 1
#| }
#| edits.push(
#| Delete(old_index=prefix + start_i, old_len=len, new_index=prefix + j),
#| )
#| }
#| _ => {
#| let start_j = j
#| let mut len = 0
#| while j < n && bp[i * width + j] == 2 {
#| j += 1
#| len += 1
#| }
#| edits.push(
#| Insert(old_index=prefix + i, new_index=prefix + start_j, new_len=len),
#| )
#| }
#| }
#| }
#| edits
#|}
),
"pile.mbt": (
#|priv struct Pile(Array[BackPointer])
#|fn Pile::new(top : BackPointer) -> Pile {
#| Pile([top])
#|}
#|fn Pile::push(self : Pile, t : BackPointer) -> Unit {
#| self.0.push(t)
#|}
#|fn Pile::top(self : Pile) -> BackPointer {
#| self.0[self.0.length() - 1]
#|}
),
"piles.mbt": (
#|priv struct Piles(Array[Pile])
#|fn Piles::is_empty(self : Piles) -> Bool {
#| self.0.is_empty()
#|}
#|fn Piles::put_back(self : Piles, singleton : BackPointer) -> Unit {
#| self.0.push(Pile::new(singleton))
#|}
#|fn Piles::last(self : Piles) -> Pile? {
#| self.0.last()
#|}
#|#alias("_[_]")
#|fn Piles::at(self : Piles, i : Int32) -> Pile {
#| self.0[i]
#|}
#|fn Piles::put_by_binary_search(
#| self : Piles,
#| new_idx~ : Int32,
#| place~ : Int32,
#|) -> Unit {
#| let mut lo = -1
#| let mut hi = self.0.length()
#| while lo + 1 < hi {
#| let mid = (lo + hi) / 2
#| if self[mid].top().value.0 < new_idx {
#| lo = mid
#| } else {
#| hi = mid
#| }
#| }
#| if lo >= 0 {
#| let prev = Some(self[lo].top())
#| if lo + 1 < self.0.length() {
#| self[lo + 1].push({ value: (new_idx, place), prev, })
#| } else {
#| self.put_back({ value: (new_idx, place), prev, })
#| }
#| } else {
#| self[0].push({ value: (new_idx, place), prev: None, })
#| }
#|}
),
"unique.mbt": (
#|priv struct CountRecord {
#| old_idx : Int32
#| mut old_count : Int32
#| mut new_idx : Int32
#| mut new_count : Int32
#|}
#|fn[T : Eq + Hash] find_unique(
#| old~ : ArrayView[T],
#| new~ : ArrayView[T],
#|) -> Array[(Int32, Int32)] {
#| let match_lines : @hashmap.HashMap[T, CountRecord] = HashMap(
#| [],
#| capacity=old.length().max(new.length()),
#| )
#| for i, value in old {
#| if match_lines.get(value) is Some(count_record) {
#| count_record.old_count += 1
#| } else {
#| match_lines[value] = {
#| old_idx: i,
#| old_count: 1,
#| new_idx: -1,
#| new_count: 0,
#| }
#| }
#| }
#| for i, value in new {
#| if match_lines.get(value) is Some(count_record) {
#| count_record.new_count += 1
#| if count_record.new_idx == -1 {
#| count_record.new_idx = i
#| }
#| }
#| }
#| let unique_match_lines : Array[(Int32, Int32)] = [
#| for (_, record) in match_lines if record.new_count == 1 &&
#| record.old_count == 1 => (record.old_idx, record.new_idx)
#| ]
#| unique_match_lines.sort_by_key(pair => pair.0)
#| return unique_match_lines
#|}
),
"unique_lcs.mbt": (
#|fn[T : Eq + Hash] unique_lcs(
#| old~ : ArrayView[T],
#| new~ : ArrayView[T],
#|) -> ArrayView[(Int32, Int32)] {
#| let matches = find_unique(old~, new~)
#| let piles = Piles(Array(capacity=matches.length()))
#| for place, (_, new_idx) in matches {
#| if piles.is_empty() {
#| piles.put_back({ value: (new_idx, place), prev: None, })
#| } else {
#| piles.put_by_binary_search(new_idx~, place~)
#| }
#| }
#| guard piles.last() is Some(head) else { return [] }
#| return [ for (_, place) in head.top().to_array() => matches[place] ]
#|}
),
},
)
///|
let moonbitlang_core_double_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/double",
deps={},
files={
"alias.mbt": (
#|pub fn round(d : Double) -> Double {
#| d.round()
#|}
#|pub fn ceil(d : Double) -> Double {
#| d.ceil()
#|}
#|pub fn floor(d : Double) -> Double {
#| d.floor()
#|}
#|pub fn trunc(d : Double) -> Double {
#| d.trunc()
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `@math.pow` instead")
#|#warnings("-deprecated")
#|pub fn pow(m : Double, n : Double) -> Double {
#| m.pow(n)
#|}
#|#deprecated("Use `Double::is_close` instead")
#|pub fn is_close(
#| d : Double,
#| other : Double,
#| relative_tolerance~ : Double = 1.0e-09,
#| absolute_tolerance~ : Double = 0.0,
#|) -> Bool {
#| d.is_close(other, relative_tolerance~, absolute_tolerance~)
#|}
#|#deprecated("Use `x.to_double()` (`Int::to_double`) instead")
#|pub fn from_int(i : Int32) -> Double {
#| i.to_double()
#|}
#|#deprecated("Use `x.abs()` instead")
#|pub fn abs(x : Double) -> Double {
#| x.abs()
#|}
),
"limits.mbt": (
#|pub let not_a_number : Double = 0x7FF8000000000001L.reinterpret_as_double()
#|pub let infinity : Double = 0x7FF0000000000000L.reinterpret_as_double()
#|pub let neg_infinity : Double = 0xFFF0000000000000L.reinterpret_as_double()
#|pub let max_value : Double = 0x7FEFFFFFFFFFFFFFL.reinterpret_as_double()
#|pub let min_value : Double = 0xFFEFFFFFFFFFFFFFL.reinterpret_as_double()
#|pub let min_positive : Double = 0x0010000000000000L.reinterpret_as_double()
),
},
)
///|
let moonbitlang_core_encoding_ascii_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/encoding/ascii",
deps={},
files={
"decode.mbt": (
#|#derive(@debug.Debug)
#|pub suberror Malformed {
#| Malformed(BytesView)
#|}
#|fn unsafe_fixedarray_uint16_to_string(buffer : FixedArray[UInt16]) -> String = "%string.unsafe_from_uint16_fixedarray"
#|#cfg(target="js")
#|#warnings("-unused_value")
#|fn suppress_unused_v128_import_on_js() -> Unit {
#| ignore(@simd.i8x16_splat(0))
#|}
#|fn finish_string(buffer : FixedArray[UInt16], len : Int32) -> String {
#| if len == buffer.length() {
#| unsafe_fixedarray_uint16_to_string(buffer)
#| } else {
#| let data = FixedArray::make(len, (Default::default() : UInt16))
#| data.unsafe_blit(0, buffer, 0, len)
#| unsafe_fixedarray_uint16_to_string(data)
#| }
#|}
#|#inline
#|fn decode_scalar(bytes : BytesView) -> String raise Malformed {
#| let t : FixedArray[UInt16] = FixedArray::make(bytes.length(), 0)
#| let tlen = for tlen = 0, bs = bytes {
#| match (tlen, bs) {
#| (tlen, []) => break tlen
#| (tlen, [0..<=0x7F as b, .. rest]) => {
#| t.unsafe_set(tlen, b.to_uint16())
#| continue tlen + 1, rest
#| }
#| (_, _ as bytes) => raise Malformed(bytes)
#| }
#| }
#| finish_string(t, tlen)
#|}
#|#cfg(not(target="js"))
#|fn decode_v128(bytes : BytesView) -> String raise Malformed {
#| let length = bytes.length()
#| let result = FixedArray::make(length * 2, b'\x00')
#| for rest = bytes, result_offset = 0 {
#| match rest {
#| [v128le(block), .. tail] => {
#| let non_ascii = @simd.i8x16_bitmask(block)
#| if non_ascii != 0 {
#| raise Malformed(bytes[result_offset / 2 + non_ascii.ctz():])
#| }
#| @simd.v128_store(
#| result,
#| result_offset,
#| @simd.i16x8_extend_low_i8x16_u(block),
#| )
#| @simd.v128_store(
#| result,
#| result_offset + 16,
#| @simd.i16x8_extend_high_i8x16_u(block),
#| )
#| continue tail, result_offset + 32
#| }
#| [0..<=0x7F as byte, .. tail] => {
#| result.unsafe_set(result_offset, byte)
#| result.unsafe_set(result_offset + 1, b'\x00')
#| continue tail, result_offset + 2
#| }
#| [] => break
#| _ => raise Malformed(bytes[result_offset / 2:])
#| }
#| }
#| result.unsafe_reinterpret_as_bytes().to_unchecked_string()
#|}
#|#cfg(not(target="js"))
#|#inline
#|pub fn decode(bytes : BytesView) -> String raise Malformed {
#| if bytes.length() >= 64 {
#| decode_v128(bytes)
#| } else {
#| decode_scalar(bytes)
#| }
#|}
#|#cfg(target="js")
#|pub fn decode(bytes : BytesView) -> String raise Malformed {
#| decode_scalar(bytes)
#|}
#|pub fn decode_lossy(bytes : BytesView) -> String {
#| let t : FixedArray[UInt16] = FixedArray::make(bytes.length(), 0)
#| let tlen = for tlen = 0, bs = bytes {
#| match (tlen, bs) {
#| (tlen, []) => break tlen
#| (tlen, [0..<=0x7F as b, .. rest]) => {
#| t.unsafe_set(tlen, b.to_uint16())
#| continue tlen + 1, rest
#| }
#| (tlen, [_, .. rest]) => {
#| t.unsafe_set(tlen, 0xFFFD)
#| continue tlen + 1, rest
#| }
#| }
#| }
#| finish_string(t, tlen)
#|}
),
"encode.mbt": (
#|pub fn encode(str : StringView) -> Bytes {
#| let length = str.length()
#| let arr = FixedArray::make(length, b'\x00')
#| for i, code_unit in str.code_units() {
#| if code_unit > 0x7F {
#| panic()
#| }
#| arr[i] = code_unit.to_byte()
#| }
#| arr.unsafe_reinterpret_as_bytes()
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Malformed with @debug.Debug::{to_repr}
),
},
)
///|
let moonbitlang_core_encoding_base64_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/encoding/base64",
deps={},
files={
"decode.mbt": (
#|#derive(@debug.Debug)
#|pub suberror Malformed {
#| Malformed(StringView)
#|}
#|const WRITESPACE = -3
#|const PADDING = -2
#|const INVALID_CHAR = -1
#|fn base64_value(code_unit : Int32, url_safe : Bool) -> Int32 {
#| match code_unit {
#| 'A'..<='Z' => code_unit - 'A'
#| 'a'..<='z' => code_unit - 'a' + 26
#| '0'..<='9' => code_unit - '0' + 52
#| '+' if !url_safe => 62
#| '/' if !url_safe => 63
#| '-' if url_safe => 62
#| '_' if url_safe => 63
#| ' ' | '\n' | '\r' | '\t' => WRITESPACE
#| '=' => PADDING
#| _ => INVALID_CHAR
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn decode(
#| text : StringView,
#| ignore_whitespace~ : Bool = false,
#| url_safe~ : Bool = false,
#|) -> Bytes raise Malformed {
#| decode_scalar(text, ignore_whitespace, url_safe)
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub fn decode(
#| text : StringView,
#| ignore_whitespace~ : Bool = false,
#| url_safe~ : Bool = false,
#|) -> Bytes raise Malformed {
#| if !ignore_whitespace && decode_v128(text, url_safe) is Some(bytes) {
#| return bytes
#| }
#| decode_scalar(text, ignore_whitespace, url_safe)
#|}
#|fn decode_scalar(
#| text : StringView,
#| ignore_whitespace : Bool,
#| url_safe : Bool,
#|) -> Bytes raise Malformed {
#| let buffer = @buffer.Buffer(capacity=text.length() / 4 * 3)
#| let quartet = FixedArray::make(4, 0)
#| let mut count = 0
#| for i, code_unit in text.code_units() {
#| match base64_value(code_unit.to_int(), url_safe) {
#| WRITESPACE =>
#| if ignore_whitespace {
#| continue
#| } else {
#| raise Malformed(text)
#| }
#| PADDING =>
#| if count < 2 {
#| raise Malformed(text)
#| } else {
#| quartet[count] = PADDING
#| count += 1
#| if count == 4 {
#| count = 0
#| if quartet[2] == PADDING {
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| buffer.write_byte(b0)
#| guard (quartet[1] & 0x0F) == 0 else { raise Malformed(text) }
#| } else {
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| let b1 = (((quartet[1] & 0x0F) << 4) | (quartet[2] >> 2)).to_byte()
#| guard (quartet[2] & 0x03) == 0 else { raise Malformed(text) }
#| buffer.write_byte(b0)
#| buffer.write_byte(b1)
#| }
#| if ignore_whitespace {
#| for code_unit in text.code_units()[i + 1:] {
#| guard code_unit is (' ' | '\n' | '\r' | '\t') else {
#| raise Malformed(text)
#| }
#| }
#| } else {
#| guard i + 1 == text.length() else { raise Malformed(text) }
#| }
#| break
#| }
#| }
#| INVALID_CHAR => raise Malformed(text)
#| value => {
#| quartet[count] = value
#| count += 1
#| if count == 4 {
#| count = 0
#| guard quartet[2] != PADDING else { raise Malformed(text) }
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| let b1 = (((quartet[1] & 0x0F) << 4) | (quartet[2] >> 2)).to_byte()
#| let b2 = (((quartet[2] & 0x03) << 6) | quartet[3]).to_byte()
#| buffer.write_byte(b0)
#| buffer.write_byte(b1)
#| buffer.write_byte(b2)
#| }
#| }
#| }
#| }
#| match count {
#| 1 => raise Malformed(text)
#| 2 => {
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| guard (quartet[1] & 0x0F) == 0 else { raise Malformed(text) }
#| buffer.write_byte(b0)
#| }
#| 3 => {
#| guard quartet[2] != PADDING else { raise Malformed(text) }
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| let b1 = (((quartet[1] & 0x0F) << 4) | (quartet[2] >> 2)).to_byte()
#| guard (quartet[2] & 0x03) == 0 else { raise Malformed(text) }
#| buffer.write_byte(b0)
#| buffer.write_byte(b1)
#| }
#| _ => ()
#| }
#| buffer.to_bytes()
#|}
#|pub fn decode_lossy(
#| text : StringView,
#| ignore_whitespace~ : Bool = true,
#| url_safe~ : Bool = false,
#|) -> Bytes {
#| let buffer = @buffer.Buffer(capacity=text.length() / 4 * 3)
#| let quartet = FixedArray::make(4, 0)
#| let mut count = 0
#| for code_unit in text.code_units() {
#| match base64_value(code_unit.to_int(), url_safe) {
#| WRITESPACE => if !ignore_whitespace { break }
#| PADDING => break
#| INVALID_CHAR => ()
#| value => {
#| quartet[count] = value
#| count += 1
#| if count == 4 {
#| count = 0
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| let b1 = (((quartet[1] & 0x0F) << 4) | (quartet[2] >> 2)).to_byte()
#| let b2 = (((quartet[2] & 0x03) << 6) | quartet[3]).to_byte()
#| buffer.write_byte(b0)
#| buffer.write_byte(b1)
#| buffer.write_byte(b2)
#| }
#| }
#| }
#| }
#| if count == 2 {
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| buffer.write_byte(b0)
#| } else if count == 3 {
#| let b0 = ((quartet[0] << 2) | (quartet[1] >> 4)).to_byte()
#| let b1 = (((quartet[1] & 0x0F) << 4) | (quartet[2] >> 2)).to_byte()
#| buffer.write_byte(b0)
#| buffer.write_byte(b1)
#| }
#| buffer.to_bytes()
#|}
),
"decode_v128.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|fn decode_offsets_v128() -> V128 {
#| @simd.i8x16_const(0, 16, 19, 4, 191, 191, 185, 185, 0, 0, 0, 0, 0, 0, 0, 0)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn decode_valid_v128(ascii : V128, url_safe : Bool) -> Bool {
#| let upper = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'A')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'Z')),
#| )
#| let lower = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'a')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'z')),
#| )
#| let digit = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'0')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'9')),
#| )
#| let plus = @simd.i8x16_eq(
#| ascii,
#| @simd.i8x16_splat(if url_safe { b'-' } else { b'+' }),
#| )
#| let slash = @simd.i8x16_eq(
#| ascii,
#| @simd.i8x16_splat(if url_safe { b'_' } else { b'/' }),
#| )
#| @simd.i8x16_all_true(
#| @simd.v128_or_(
#| @simd.v128_or_(upper, lower),
#| @simd.v128_or_(digit, @simd.v128_or_(plus, slash)),
#| ),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn url_to_standard_v128(ascii : V128) -> V128 {
#| @simd.i8x16_sub(
#| ascii,
#| @simd.v128_or_(
#| @simd.v128_and_(
#| @simd.i8x16_eq(ascii, @simd.i8x16_splat(b'-')),
#| @simd.i8x16_splat(2),
#| ),
#| @simd.v128_and_(
#| @simd.i8x16_eq(ascii, @simd.i8x16_splat(b'_')),
#| @simd.i8x16_splat(48),
#| ),
#| ),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn decode_block_v128(ascii : V128) -> V128 {
#| let hash = @simd.i8x16_add(
#| @simd.i8x16_shr_u(ascii, 4),
#| @simd.i8x16_eq(ascii, @simd.i8x16_splat(b'/')),
#| )
#| let sextets = @simd.i8x16_add(
#| ascii,
#| @simd.i8x16_swizzle(decode_offsets_v128(), hash),
#| )
#| let groups = @simd.v128_or_(
#| @simd.v128_or_(
#| @simd.i32x4_shl(
#| @simd.v128_and_(sextets, @simd.i32x4_splat(0x0000003F)),
#| 18,
#| ),
#| @simd.i32x4_shl(
#| @simd.v128_and_(sextets, @simd.i32x4_splat(0x00003F00)),
#| 4,
#| ),
#| ),
#| @simd.v128_or_(
#| @simd.i32x4_shr_u(
#| @simd.v128_and_(sextets, @simd.i32x4_splat(0x003F0000)),
#| 10,
#| ),
#| @simd.i32x4_shr_u(
#| @simd.v128_and_(sextets, @simd.i32x4_splat(0x3F000000)),
#| 24,
#| ),
#| ),
#| )
#| @simd.i8x16_shuffle(
#| groups, groups, 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 0, 0, 0, 0,
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn decode_v128(text : StringView, url_safe : Bool) -> Bytes? {
#| let length = text.length()
#| guard length >= 32 && length % 4 == 0 else { return None }
#| let padding = if text.unsafe_get(length - 1) != b'='.to_uint16() {
#| 0
#| } else if text.unsafe_get(length - 2) != b'='.to_uint16() {
#| 1
#| } else {
#| 2
#| }
#| let out = FixedArray::make(length / 4 * 3 - padding, b'\x00')
#| let src = text.data()
#| let body = length - 4
#| let base = text.start_offset()
#| let mut index = 0
#| let mut written = 0
#| while body - index >= 16 && out.length() - written >= 16 {
#| let offset = base + index
#| let ascii = @simd.i8x16_narrow_i16x8_u(
#| @simd.v128_load_i16x8(src, offset),
#| @simd.v128_load_i16x8(src, offset + 8),
#| )
#| guard decode_valid_v128(ascii, url_safe) else { return None }
#| let ascii = if url_safe { url_to_standard_v128(ascii) } else { ascii }
#| @simd.v128_store(out, written, decode_block_v128(ascii))
#| index += 16
#| written += 12
#| }
#| while index < body {
#| let mut group = 0
#| for lane in 0..<4 {
#| let value = base64_value(text.unsafe_get(index + lane).to_int(), url_safe)
#| guard value >= 0 else { return None }
#| group = (group << 6) | value
#| }
#| out[written] = (group >> 16).to_byte()
#| out[written + 1] = (group >> 8).to_byte()
#| out[written + 2] = group.to_byte()
#| index += 4
#| written += 3
#| }
#| let tail_bytes = decode_scalar(text[body:], false, url_safe) catch {
#| _ => return None
#| }
#| guard tail_bytes.length() == out.length() - written else { return None }
#| for i, byte in tail_bytes {
#| out[written + i] = byte
#| }
#| Some(out.unsafe_reinterpret_as_bytes())
#|}
),
"encode.mbt": (
#|const BASE64_STD : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#|const BASE64_URL : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn encode(
#| bytes : BytesView,
#| padding~ : Bool = true,
#| url_safe~ : Bool = false,
#|) -> String {
#| encode_scalar(bytes, padding~, url_safe~)
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub fn encode(
#| bytes : BytesView,
#| padding~ : Bool = true,
#| url_safe~ : Bool = false,
#|) -> String {
#| encode_v128(bytes, padding, url_safe)
#|}
#|fn encode_scalar(
#| bytes : BytesView,
#| padding~ : Bool = true,
#| url_safe~ : Bool = false,
#|) -> String {
#| let alphabet = if url_safe { BASE64_URL } else { BASE64_STD }
#| let full_groups = bytes.length() / 3
#| let remainder = bytes.length() % 3
#| let mut capacity = full_groups * 4
#| if remainder != 0 {
#| capacity += if padding is true { 4 } else { remainder + 1 }
#| }
#| let builder = StringBuilder(capacity~)
#| for remaining = bytes {
#| match remaining {
#| [b0, b1, b2, .. rest] => {
#| let n = (b0.to_int() << 16) | (b1.to_int() << 8) | b2.to_int()
#| builder.write_char(alphabet[(n >> 18) & 0x3F].to_char())
#| builder.write_char(alphabet[(n >> 12) & 0x3F].to_char())
#| builder.write_char(alphabet[(n >> 6) & 0x3F].to_char())
#| builder.write_char(alphabet[n & 0x3F].to_char())
#| continue rest
#| }
#| [b0, b1] => {
#| let n = (b0.to_int() << 16) | (b1.to_int() << 8)
#| builder.write_char(alphabet[(n >> 18) & 0x3F].to_char())
#| builder.write_char(alphabet[(n >> 12) & 0x3F].to_char())
#| builder.write_char(alphabet[(n >> 6) & 0x3F].to_char())
#| if padding is true {
#| builder.write_char('=')
#| }
#| break
#| }
#| [b0] => {
#| let n = b0.to_int() << 16
#| builder.write_char(alphabet[(n >> 18) & 0x3F].to_char())
#| builder.write_char(alphabet[(n >> 12) & 0x3F].to_char())
#| if padding is true {
#| builder <+ "=="
#| }
#| break
#| }
#| [] => break
#| }
#| }
#| builder.to_string()
#|}
),
"encode_v128.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|fn encode_offsets_v128(url_safe : Bool) -> V128 {
#| if url_safe {
#| @simd.i8x16_const(
#| 71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 239, 32, 65, 0, 0,
#| )
#| } else {
#| @simd.i8x16_const(
#| 71, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 237, 240, 65, 0, 0,
#| )
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn encode_block_v128(data : V128, offsets : V128) -> V128 {
#| let zero = @simd.i8x16_splat(0)
#| let groups = @simd.i8x16_shuffle(
#| data, zero, 2, 1, 0, 16, 5, 4, 3, 16, 8, 7, 6, 16, 11, 10, 9, 16,
#| )
#| let sextets = @simd.v128_or_(
#| @simd.v128_or_(
#| @simd.v128_and_(
#| @simd.i32x4_shr_u(groups, 18),
#| @simd.i32x4_splat(0x0000003F),
#| ),
#| @simd.v128_and_(
#| @simd.i32x4_shr_u(groups, 4),
#| @simd.i32x4_splat(0x00003F00),
#| ),
#| ),
#| @simd.v128_or_(
#| @simd.v128_and_(
#| @simd.i32x4_shl(groups, 10),
#| @simd.i32x4_splat(0x003F0000),
#| ),
#| @simd.v128_and_(
#| @simd.i32x4_shl(groups, 24),
#| @simd.i32x4_splat(0x3F000000),
#| ),
#| ),
#| )
#| let class = @simd.v128_or_(
#| @simd.i8x16_sub_sat_u(sextets, @simd.i8x16_splat(51)),
#| @simd.v128_and_(
#| @simd.i8x16_lt_u(sextets, @simd.i8x16_splat(26)),
#| @simd.i8x16_splat(13),
#| ),
#| )
#| @simd.i8x16_add(sextets, @simd.i8x16_swizzle(offsets, class))
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn write_code_unit(out : FixedArray[Byte], offset : Int32, code : Byte) -> Unit {
#| out[offset] = code
#| out[offset + 1] = 0
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn encode_v128(bytes : BytesView, padding : Bool, url_safe : Bool) -> String {
#| let length = bytes.length()
#| guard length <= 0x2000_0000 else {
#| return encode_scalar(bytes, padding~, url_safe~)
#| }
#| let alphabet = if url_safe { BASE64_URL } else { BASE64_STD }
#| let offsets = encode_offsets_v128(url_safe)
#| let full_groups = length / 3
#| let remainder = length % 3
#| let mut char_count = full_groups * 4
#| if remainder != 0 {
#| char_count += if padding { 4 } else { remainder + 1 }
#| }
#| let out = FixedArray::make(char_count * 2, b'\x00')
#| for rest = bytes, written = 0 {
#| match rest {
#| [v128le(block), ..] as current => {
#| let chars = encode_block_v128(block, offsets)
#| @simd.v128_store(out, written, @simd.i16x8_extend_low_i8x16_u(chars))
#| @simd.v128_store(
#| out,
#| written + 16,
#| @simd.i16x8_extend_high_i8x16_u(chars),
#| )
#| continue current[12:], written + 32
#| }
#| [b0, b1, b2, .. tail] => {
#| let n = (b0.to_int() << 16) | (b1.to_int() << 8) | b2.to_int()
#| write_code_unit(out, written, alphabet[(n >> 18) & 0x3F])
#| write_code_unit(out, written + 2, alphabet[(n >> 12) & 0x3F])
#| write_code_unit(out, written + 4, alphabet[(n >> 6) & 0x3F])
#| write_code_unit(out, written + 6, alphabet[n & 0x3F])
#| continue tail, written + 8
#| }
#| [b0, b1] => {
#| let n = (b0.to_int() << 16) | (b1.to_int() << 8)
#| write_code_unit(out, written, alphabet[(n >> 18) & 0x3F])
#| write_code_unit(out, written + 2, alphabet[(n >> 12) & 0x3F])
#| write_code_unit(out, written + 4, alphabet[(n >> 6) & 0x3F])
#| if padding {
#| write_code_unit(out, written + 6, b'=')
#| }
#| break
#| }
#| [b0] => {
#| let n = b0.to_int() << 16
#| write_code_unit(out, written, alphabet[(n >> 18) & 0x3F])
#| write_code_unit(out, written + 2, alphabet[(n >> 12) & 0x3F])
#| if padding {
#| write_code_unit(out, written + 4, b'=')
#| write_code_unit(out, written + 6, b'=')
#| }
#| break
#| }
#| [] => break
#| }
#| }
#| out.unsafe_reinterpret_as_bytes().to_unchecked_string()
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Malformed with @debug.Debug::{to_repr}
),
},
)
///|
let moonbitlang_core_encoding_hex_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/encoding/hex",
deps={},
files={
"decode.mbt": (
#|#derive(@debug.Debug)
#|pub suberror Malformed {
#| Malformed(StringView)
#|}
#|fn hex_value(code_unit : Int32) -> Int32 {
#| match code_unit {
#| '0'..<='9' => code_unit - '0'
#| 'a'..<='f' => code_unit - 'a' + 10
#| 'A'..<='F' => code_unit - 'A' + 10
#| _ => -1
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn decode(text : StringView) -> Bytes raise Malformed {
#| decode_scalar(text)
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub fn decode(text : StringView) -> Bytes raise Malformed {
#| match decode_v128(text) {
#| Some(bytes) => bytes
#| None => decode_scalar(text)
#| }
#|}
#|fn decode_scalar(text : StringView) -> Bytes raise Malformed {
#| if text.length() % 2 != 0 {
#| raise Malformed(text)
#| }
#| let buffer = @buffer.Buffer(capacity=text.length() / 2)
#| for i in 0..<(text.length() / 2) {
#| let hi = hex_value(text.code_unit_at(i * 2).to_int())
#| let lo = hex_value(text.code_unit_at(i * 2 + 1).to_int())
#| guard hi >= 0 && lo >= 0 else { raise Malformed(text) }
#| buffer.write_byte(((hi << 4) | lo).to_byte())
#| }
#| buffer.to_bytes()
#|}
),
"decode_v128.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|fn hex_valid_v128(ascii : V128) -> Bool {
#| let digit = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'0')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'9')),
#| )
#| let lower = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'a')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'f')),
#| )
#| let upper = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'A')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'F')),
#| )
#| @simd.i8x16_all_true(@simd.v128_or_(digit, @simd.v128_or_(lower, upper)))
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn hex_nibbles_v128(ascii : V128) -> V128 {
#| let lower = @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'a')),
#| @simd.i8x16_splat(217), // -39 mod 256
#| )
#| let upper = @simd.v128_and_(
#| @simd.v128_and_(
#| @simd.i8x16_ge_u(ascii, @simd.i8x16_splat(b'A')),
#| @simd.i8x16_le_u(ascii, @simd.i8x16_splat(b'F')),
#| ),
#| @simd.i8x16_splat(249), // -7 mod 256
#| )
#| @simd.i8x16_add(
#| @simd.i8x16_add(ascii, @simd.i8x16_splat(208)), // -48 mod 256
#| @simd.v128_or_(lower, upper),
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn decode_v128(text : StringView) -> Bytes? {
#| let length = text.length()
#| guard length >= 32 && length % 2 == 0 else { return None }
#| let out = FixedArray::make(length / 2, b'\x00')
#| let src = text.data()
#| let base = text.start_offset()
#| let mut index = 0
#| let mut written = 0
#| while length - index >= 32 {
#| let offset = base + index
#| let ascii0 = @simd.i8x16_narrow_i16x8_u(
#| @simd.v128_load_i16x8(src, offset),
#| @simd.v128_load_i16x8(src, offset + 8),
#| )
#| let ascii1 = @simd.i8x16_narrow_i16x8_u(
#| @simd.v128_load_i16x8(src, offset + 16),
#| @simd.v128_load_i16x8(src, offset + 24),
#| )
#| guard hex_valid_v128(ascii0) && hex_valid_v128(ascii1) else { return None }
#| let n0 = hex_nibbles_v128(ascii0)
#| let n1 = hex_nibbles_v128(ascii1)
#| let hi = @simd.i8x16_shuffle(
#| n0, n1, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
#| )
#| let lo = @simd.i8x16_shuffle(
#| n0, n1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
#| )
#| @simd.v128_store(out, written, @simd.v128_or_(@simd.i8x16_shl(hi, 4), lo))
#| index += 32
#| written += 16
#| }
#| while index < length {
#| let hi = hex_value(text.unsafe_get(index).to_int())
#| let lo = hex_value(text.unsafe_get(index + 1).to_int())
#| guard hi >= 0 && lo >= 0 else { return None }
#| out[written] = ((hi << 4) | lo).to_byte()
#| index += 2
#| written += 1
#| }
#| Some(out.unsafe_reinterpret_as_bytes())
#|}
),
"encode.mbt": (
#|const HEX_DIGITS : Bytes = b"0123456789abcdef"
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn encode(bytes : BytesView) -> String {
#| encode_scalar(bytes)
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub fn encode(bytes : BytesView) -> String {
#| encode_v128(bytes)
#|}
#|fn encode_scalar(bytes : BytesView) -> String {
#| let builder = StringBuilder(capacity=2 * bytes.length())
#| for byte in bytes {
#| let n = byte.to_int()
#| builder.write_char(HEX_DIGITS[(n >> 4) & 0x0F].to_char())
#| builder.write_char(HEX_DIGITS[n & 0x0F].to_char())
#| }
#| builder.to_string()
#|}
),
"encode_v128.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|fn hex_digits_v128() -> V128 {
#| @simd.i8x16_const(
#| 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102,
#| )
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn write_code_unit(out : FixedArray[Byte], offset : Int32, code : Byte) -> Unit {
#| out[offset] = code
#| out[offset + 1] = 0
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn encode_v128(bytes : BytesView) -> String {
#| let length = bytes.length()
#| guard length <= 0x1FFF_FFFF else { return encode_scalar(bytes) } // Int::MAX / 4
#| let out = FixedArray::make(length * 4, b'\x00')
#| let table = hex_digits_v128()
#| for rest = bytes, written = 0 {
#| match rest {
#| [v128le(data), .. tail] => {
#| let hi = @simd.i8x16_shr_u(data, 4)
#| let lo = @simd.v128_and_(data, @simd.i8x16_splat(0x0F))
#| let first = @simd.i8x16_shuffle(
#| hi, lo, 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23,
#| )
#| let second = @simd.i8x16_shuffle(
#| hi, lo, 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31,
#| )
#| let d0 = @simd.i8x16_swizzle(table, first)
#| let d1 = @simd.i8x16_swizzle(table, second)
#| @simd.v128_store(out, written, @simd.i16x8_extend_low_i8x16_u(d0))
#| @simd.v128_store(out, written + 16, @simd.i16x8_extend_high_i8x16_u(d0))
#| @simd.v128_store(out, written + 32, @simd.i16x8_extend_low_i8x16_u(d1))
#| @simd.v128_store(out, written + 48, @simd.i16x8_extend_high_i8x16_u(d1))
#| continue tail, written + 64
#| }
#| [byte, .. tail] => {
#| let n = byte.to_int()
#| write_code_unit(out, written, HEX_DIGITS[(n >> 4) & 0x0F])
#| write_code_unit(out, written + 2, HEX_DIGITS[n & 0x0F])
#| continue tail, written + 4
#| }
#| [] => break
#| }
#| }
#| out.unsafe_reinterpret_as_bytes().to_unchecked_string()
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Malformed with @debug.Debug::{to_repr}
),
},
)
///|
let moonbitlang_core_encoding_percent_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/encoding/percent",
deps={},
files={
"decode.mbt": (
#|#derive(@debug.Debug)
#|pub suberror Malformed {
#| Malformed(StringView)
#|}
#|fn hex_value(code_unit : Int32) -> Int32 {
#| match code_unit {
#| '0'..<='9' => code_unit - '0'
#| 'a'..<='f' => code_unit - 'a' + 10
#| 'A'..<='F' => code_unit - 'A' + 10
#| _ => -1
#| }
#|}
#|pub fn decode(text : StringView) -> String raise Malformed {
#| match decode_impl(text, lossy=false) {
#| Some(decoded) => decoded
#| None => raise Malformed(text)
#| }
#|}
#|pub fn decode_lossy(text : StringView) -> String {
#| decode_impl(text, lossy=true).unwrap()
#|}
#|fn decode_impl(text : StringView, lossy~ : Bool) -> String? {
#| let length = text.length()
#| let builder = StringBuilder(capacity=length)
#| let bytes = @buffer.Buffer()
#| let mut i = 0
#| let mut literal_start = 0
#| while i < length {
#| if text.unsafe_get(i) != '%' {
#| i += 1
#| continue
#| }
#| builder.write_view(text.unsafe_view(start=literal_start, end=i))
#| bytes.reset()
#| let mut resume_after_percent = false
#| while i < length && text.unsafe_get(i) == '%' {
#| if i + 2 < length {
#| let hi = hex_value(text.unsafe_get(i + 1).to_int())
#| let lo = hex_value(text.unsafe_get(i + 2).to_int())
#| if hi >= 0 && lo >= 0 {
#| bytes.write_byte(((hi << 4) | lo).to_byte())
#| i += 3
#| continue
#| }
#| }
#| guard lossy else { return None }
#| resume_after_percent = true
#| break
#| }
#| if lossy {
#| builder.write_string(@utf8.decode_lossy(bytes.contents()))
#| } else {
#| let decoded = @utf8.decode(bytes.contents()) catch { _ => return None }
#| builder.write_string(decoded)
#| }
#| if resume_after_percent {
#| builder.write_char('%')
#| i += 1
#| }
#| literal_start = i
#| }
#| builder.write_view(text.unsafe_view(start=literal_start, end=length))
#| Some(builder.to_string())
#|}
#|pub fn decode_to_bytes(text : StringView) -> Bytes raise Malformed {
#| let length = text.length()
#| let buffer = @buffer.Buffer(capacity=length)
#| let mut i = 0
#| let mut literal_start = 0
#| while i < length {
#| if text.unsafe_get(i) != '%' {
#| i += 1
#| continue
#| }
#| write_literal_utf8(buffer, text.unsafe_view(start=literal_start, end=i))
#| if length - i >= 3 {
#| let hi = hex_value(text.unsafe_get(i + 1).to_int())
#| let lo = hex_value(text.unsafe_get(i + 2).to_int())
#| if hi >= 0 && lo >= 0 {
#| buffer.write_byte(((hi << 4) | lo).to_byte())
#| i += 3
#| literal_start = i
#| continue
#| }
#| }
#| raise Malformed(text)
#| }
#| write_literal_utf8(buffer, text.unsafe_view(start=literal_start, end=length))
#| buffer.to_bytes()
#|}
#|fn write_literal_utf8(buffer : @buffer.Buffer, text : StringView) -> Unit {
#| let length = text.length()
#| let mut i = 0
#| while i < length {
#| let unit = text.unsafe_get(i).to_int()
#| if unit < 0x80 {
#| buffer.write_byte(unit.to_byte())
#| i += 1
#| } else if unit < 0x800 {
#| buffer.write_byte((0xC0 | (unit >> 6)).to_byte())
#| buffer.write_byte((0x80 | (unit & 0x3F)).to_byte())
#| i += 1
#| } else if unit.is_leading_surrogate() &&
#| i + 1 < length &&
#| text.unsafe_get(i + 1).is_trailing_surrogate() {
#| let low = text.unsafe_get(i + 1).to_int()
#| let code_point = 0x10000 + ((unit - 0xD800) << 10) + (low - 0xDC00)
#| buffer.write_byte((0xF0 | (code_point >> 18)).to_byte())
#| buffer.write_byte((0x80 | ((code_point >> 12) & 0x3F)).to_byte())
#| buffer.write_byte((0x80 | ((code_point >> 6) & 0x3F)).to_byte())
#| buffer.write_byte((0x80 | (code_point & 0x3F)).to_byte())
#| i += 2
#| } else {
#| let scalar = if unit.is_leading_surrogate() ||
#| unit.is_trailing_surrogate() {
#| 0xFFFD
#| } else {
#| unit
#| }
#| buffer.write_byte((0xE0 | (scalar >> 12)).to_byte())
#| buffer.write_byte((0x80 | ((scalar >> 6) & 0x3F)).to_byte())
#| buffer.write_byte((0x80 | (scalar & 0x3F)).to_byte())
#| i += 1
#| }
#| }
#|}
),
"encode.mbt": (
#|const HEX_UPPER : Bytes = b"0123456789ABCDEF"
#|fn is_unreserved(code_unit : Int32) -> Bool {
#| match code_unit {
#| 'A'..<='Z' | 'a'..<='z' | '0'..<='9' | '-' | '.' | '_' | '~' => true
#| _ => false
#| }
#|}
#|fn write_escape(builder : StringBuilder, byte : Int32) -> Unit {
#| builder.write_char('%')
#| builder.write_char(HEX_UPPER[(byte >> 4) & 0x0F].to_char())
#| builder.write_char(HEX_UPPER[byte & 0x0F].to_char())
#|}
#|pub fn encode(text : StringView) -> String {
#| let builder = StringBuilder(capacity=text.length())
#| let length = text.length()
#| let mut i = 0
#| let mut literal_start = 0
#| while i < length {
#| let unit = text.unsafe_get(i).to_int()
#| if unit < 0x80 {
#| if is_unreserved(unit) {
#| i += 1
#| continue
#| }
#| builder.write_view(text.unsafe_view(start=literal_start, end=i))
#| write_escape(builder, unit)
#| i += 1
#| } else {
#| builder.write_view(text.unsafe_view(start=literal_start, end=i))
#| if unit < 0x800 {
#| write_escape(builder, 0xC0 | (unit >> 6))
#| write_escape(builder, 0x80 | (unit & 0x3F))
#| i += 1
#| } else if unit.is_leading_surrogate() &&
#| i + 1 < length &&
#| text.unsafe_get(i + 1).is_trailing_surrogate() {
#| let low = text.unsafe_get(i + 1).to_int()
#| let code_point = 0x10000 + ((unit - 0xD800) << 10) + (low - 0xDC00)
#| write_escape(builder, 0xF0 | (code_point >> 18))
#| write_escape(builder, 0x80 | ((code_point >> 12) & 0x3F))
#| write_escape(builder, 0x80 | ((code_point >> 6) & 0x3F))
#| write_escape(builder, 0x80 | (code_point & 0x3F))
#| i += 2
#| } else {
#| let scalar = if unit.is_leading_surrogate() ||
#| unit.is_trailing_surrogate() {
#| 0xFFFD
#| } else {
#| unit
#| }
#| write_escape(builder, 0xE0 | (scalar >> 12))
#| write_escape(builder, 0x80 | ((scalar >> 6) & 0x3F))
#| write_escape(builder, 0x80 | (scalar & 0x3F))
#| i += 1
#| }
#| }
#| literal_start = i
#| }
#| builder.write_view(text.unsafe_view(start=literal_start, end=length))
#| builder.to_string()
#|}
#|pub fn encode_bytes(bytes : BytesView) -> String {
#| let builder = StringBuilder(capacity=bytes.length())
#| for byte in bytes {
#| if is_unreserved(byte.to_int()) {
#| builder.write_char(byte.to_char())
#| } else {
#| write_escape(builder, byte.to_int())
#| }
#| }
#| builder.to_string()
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Malformed with @debug.Debug::{to_repr}
),
},
)
///|
let moonbitlang_core_encoding_utf16_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/encoding/utf16",
deps={},
files={
"decode.mbt": (
#|#derive(@debug.Debug)
#|pub suberror Malformed {
#| Malformed(BytesView)
#|}
#|const U_REP = '\u{FFFD}'
#|#cfg(target="js")
#|#warnings("-unused_value")
#|fn suppress_unused_v128_import_on_js() -> Unit {
#| ignore(@simd.i8x16_splat(0))
#|}
#|#cfg(not(target="js"))
#|fn utf16_swap_u16x8(value : V128) -> V128 {
#| @simd.i8x16_shuffle(
#| value, value, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14,
#| )
#|}
#|#cfg(not(target="js"))
#|fn utf16_v128_has_surrogate(value : V128) -> Bool {
#| let lower = @simd.i16x8_splat(0xD800)
#| let upper = @simd.i16x8_splat(0xDFFF)
#| @simd.i16x8_bitmask(
#| @simd.v128_and_(
#| @simd.i16x8_ge_u(value, lower),
#| @simd.i16x8_le_u(value, upper),
#| ),
#| ) !=
#| 0
#|}
#|#cfg(not(target="js"))
#|fn utf16_needs_scalar_le_v128(bytes : BytesView) -> Bool {
#| if bytes.length() % 2 != 0 {
#| return true
#| }
#| for rest = bytes {
#| match rest {
#| [v128le(block), .. tail] => {
#| if utf16_v128_has_surrogate(block) {
#| return true
#| }
#| continue tail
#| }
#| [u16le(0xD800..<=0xDFFF), ..] => return true
#| [u16le(_), .. tail] => continue tail
#| [] => break false
#| _ => break true
#| }
#| }
#|}
#|#cfg(not(target="js"))
#|fn utf16_needs_scalar_be_v128(bytes : BytesView) -> Bool {
#| if bytes.length() % 2 != 0 {
#| return true
#| }
#| for rest = bytes {
#| match rest {
#| [v128le(block), .. tail] => {
#| if utf16_v128_has_surrogate(utf16_swap_u16x8(block)) {
#| return true
#| }
#| continue tail
#| }
#| [u16be(0xD800..<=0xDFFF), ..] => return true
#| [u16be(_), .. tail] => continue tail
#| [] => break false
#| _ => break true
#| }
#| }
#|}
#|#cfg(not(target="js"))
#|fn utf16_decode_be_no_surrogate_v128(bytes : BytesView) -> String {
#| let string_bytes = FixedArray::make(bytes.length(), b'\x00')
#| for rest = bytes, written = 0 {
#| match rest {
#| [v128le(block), .. tail] => {
#| @simd.v128_store(string_bytes, written, utf16_swap_u16x8(block))
#| continue tail, written + 16
#| }
#| [u16be(code_unit), .. tail] => {
#| string_bytes.unsafe_set(written, (code_unit & 0xFF).to_byte())
#| string_bytes.unsafe_set(written + 1, (code_unit >> 8).to_byte())
#| continue tail, written + 2
#| }
#| [] => break
#| _ => break
#| }
#| }
#| string_bytes.unsafe_reinterpret_as_bytes().to_unchecked_string()
#|}
#|#inline
#|fn drop_utf16_bom(
#| bytes : BytesView,
#| ignore_bom : Bool,
#| endianness : Endian,
#|) -> BytesView {
#| if ignore_bom {
#| if endianness is Little && bytes is [.. b"\xff\xfe", .. rest] {
#| rest
#| } else if endianness is Big && bytes is [.. b"\xfe\xff", .. rest] {
#| rest
#| } else {
#| bytes
#| }
#| } else {
#| bytes
#| }
#|}
#|fn decode_scalar(
#| bytes : BytesView,
#| endianness : Endian,
#|) -> String raise Malformed {
#| if endianness is Little {
#| for x = bytes {
#| match x {
#| [] => break
#| [
#| u16le(0xD800..<=0xDBFF as higher),
#| u16le(0xDC00..<=0xDFFF as lower),
#| .. rest,
#| ] as bytes => {
#| if ((higher.reinterpret_as_int() - 0xD800) << 10) +
#| (lower.reinterpret_as_int() - 0xDC00) +
#| 0x10000 >
#| 0x10FFFF {
#| raise Malformed(bytes)
#| }
#| continue rest
#| }
#| [u16le(0xD800..<=0xDFFF), ..] as bytes => raise Malformed(bytes)
#| [u16le(_), .. rest] => continue rest
#| _ as bytes => raise Malformed(bytes)
#| }
#| }
#| bytes
#| .data()
#| .to_unchecked_string(offset=bytes.start_offset(), len=bytes.length())
#| } else {
#| let string_bytes = FixedArray::make(bytes.length(), b'\x00')
#| let mut i = 0
#| for x = bytes {
#| match x {
#| [] => break
#| [
#| u16be(0xD800..<=0xDBFF as higher),
#| u16be(0xDC00..<=0xDFFF as lower),
#| .. rest,
#| ] as bytes => {
#| if ((higher.reinterpret_as_int() - 0xD800) << 10) +
#| (lower.reinterpret_as_int() - 0xDC00) +
#| 0x10000 >
#| 0x10FFFF {
#| raise Malformed(bytes)
#| }
#| string_bytes[i] = (higher & 0xFF).to_byte()
#| string_bytes[i + 1] = (higher >> 8).to_byte()
#| string_bytes[i + 2] = (lower & 0xFF).to_byte()
#| string_bytes[i + 3] = (lower >> 8).to_byte()
#| i += 4
#| continue rest
#| }
#| [u16be(0xD800..<=0xDFFF), ..] as bytes => raise Malformed(bytes)
#| [u16be(code_unit), .. rest] => {
#| string_bytes[i] = (code_unit & 0xFF).to_byte()
#| string_bytes[i + 1] = (code_unit >> 8).to_byte()
#| i += 2
#| continue rest
#| }
#| _ as bytes => raise Malformed(bytes)
#| }
#| }
#| string_bytes.unsafe_reinterpret_as_bytes().to_unchecked_string()
#| }
#|}
#|#cfg(target="js")
#|pub fn decode(
#| bytes : BytesView,
#| ignore_bom~ : Bool = false,
#| endianness~ : Endian = Little,
#|) -> String raise Malformed {
#| decode_scalar(drop_utf16_bom(bytes, ignore_bom, endianness), endianness)
#|}
#|#cfg(not(target="js"))
#|pub fn decode(
#| bytes : BytesView,
#| ignore_bom~ : Bool = false,
#| endianness~ : Endian = Little,
#|) -> String raise Malformed {
#| let bytes = drop_utf16_bom(bytes, ignore_bom, endianness)
#| if endianness is Little {
#| if !utf16_needs_scalar_le_v128(bytes) {
#| return bytes
#| .data()
#| .to_unchecked_string(offset=bytes.start_offset(), len=bytes.length())
#| }
#| } else if !utf16_needs_scalar_be_v128(bytes) {
#| return utf16_decode_be_no_surrogate_v128(bytes)
#| }
#| decode_scalar(bytes, endianness)
#|}
#|pub fn decode_lossy(
#| bytes : BytesView,
#| ignore_bom~ : Bool = false,
#| endianness~ : Endian = Little,
#|) -> String {
#| let bytes = drop_utf16_bom(bytes, ignore_bom, endianness)
#| let builder = StringBuilder(capacity=bytes.length() / 2 + bytes.length() % 2)
#| if endianness is Little {
#| for x = bytes {
#| match x {
#| [] => break
#| [
#| u16le(0xD800..<=0xDBFF as higher),
#| u16le(0xDC00..<=0xDFFF as lower),
#| .. rest,
#| ] => {
#| let ch = ((higher.reinterpret_as_int() - 0xD800) << 10) +
#| (lower.reinterpret_as_int() - 0xDC00) +
#| 0x10000
#| if ch > 0x10FFFF {
#| builder.write_char(U_REP)
#| } else {
#| builder.write_char(ch.unsafe_to_char())
#| }
#| continue rest
#| }
#| [u16le(0xD800..<=0xDFFF), .. rest] => {
#| builder.write_char(U_REP)
#| continue rest
#| }
#| [u16le(ch), .. rest] => {
#| builder.write_char(ch.reinterpret_as_int().unsafe_to_char())
#| continue rest
#| }
#| _ => {
#| builder.write_char(U_REP)
#| break
#| }
#| }
#| }
#| } else {
#| for x = bytes {
#| match x {
#| [] => break
#| [
#| u16be(0xD800..<=0xDBFF as higher),
#| u16be(0xDC00..<=0xDFFF as lower),
#| .. rest,
#| ] => {
#| let ch = ((higher.reinterpret_as_int() - 0xD800) << 10) +
#| (lower.reinterpret_as_int() - 0xDC00) +
#| 0x10000
#| if ch > 0x10FFFF {
#| builder.write_char(U_REP)
#| } else {
#| builder.write_char(ch.unsafe_to_char())
#| }
#| continue rest
#| }
#| [u16be(0xD800..<=0xDFFF), .. rest] => {
#| builder.write_char(U_REP)
#| continue rest
#| }
#| [u16be(ch), .. rest] => {
#| builder.write_char(ch.reinterpret_as_int().unsafe_to_char())
#| continue rest
#| }
#| _ => {
#| builder.write_char(U_REP)
#| break
#| }
#| }
#| }
#| }
#| builder.to_string()
#|}
),
"encode.mbt": (
#|pub fn encode(
#| str : StringView,
#| bom~ : Bool = false,
#| endianness~ : Endian = Little,
#|) -> Bytes {
#| let bom_length = if bom { 2 } else { 0 }
#| let arr = FixedArray::make(str.length() * 2 + bom_length, b'\x00')
#| if endianness is Little {
#| if bom {
#| arr[0] = 0xFF
#| arr[1] = 0xFE
#| }
#| arr.blit_from_string(
#| bom_length,
#| str.data(),
#| str.start_offset(),
#| str.length(),
#| )
#| } else {
#| if bom {
#| arr[0] = 0xFE
#| arr[1] = 0xFF
#| }
#| encode_be_into(str, arr, bom_length)
#| }
#| arr.unsafe_reinterpret_as_bytes()
#|}
#|#cfg(not(target="wasm"))
#|#inline
#|fn encode_be_into(
#| str : StringView,
#| out : FixedArray[Byte],
#| offset : Int32,
#|) -> Unit {
#| for code_unit in str.code_units(); i = offset {
#| out[i] = (code_unit >> 8).to_byte()
#| out[i + 1] = (code_unit & 0xFF).to_byte()
#| continue i + 2
#| }
#|}
#|#cfg(target="wasm")
#|fn encode_be_into(
#| str : StringView,
#| out : FixedArray[Byte],
#| offset : Int32,
#|) -> Unit {
#| let length = str.length()
#| let source = str.data()
#| let base = str.start_offset()
#| let mut index = 0
#| while length - index >= 8 {
#| let block = @simd.v128_load_i16x8(source, base + index)
#| @simd.v128_store(out, offset + index * 2, utf16_swap_u16x8(block))
#| index += 8
#| }
#| while index < length {
#| let unit = str.unsafe_get(index)
#| out[offset + index * 2] = (unit >> 8).to_byte()
#| out[offset + index * 2 + 1] = (unit & 0xFF).to_byte()
#| index += 1
#| }
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Endian with @debug.Debug::{to_repr}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Malformed with @debug.Debug::{to_repr}
),
"types.mbt": (
#|#derive(@debug.Debug)
#|pub(all) enum Endian {
#| Little
#| Big
#|}
),
},
)
///|
let moonbitlang_core_encoding_utf8_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/encoding/utf8",
deps={},
files={
"decode.mbt": (
#|#derive(@debug.Debug)
#|pub suberror Malformed {
#| Malformed(BytesView)
#|}
#|fn drop_utf8_bom(bytes : BytesView, ignore_bom : Bool) -> BytesView {
#| if ignore_bom && bytes is [.. b"\xef\xbb\xbf", .. rest] {
#| rest
#| } else {
#| bytes
#| }
#|}
),
"decode_js.mbt": (
#|extern "js" fn decode_utf8_js(
#| bytes : Bytes,
#| start : Int32,
#| len : Int32,
#| preserve_bom : Bool,
#|) -> FixedArray[String] =
#| #| ((preserveBOMDecoder, dropBOMDecoder) => function(bytes, start, len, preserveBOM) {
#| #| try {
#| #| const end = start + len;
#| #| const slice = bytes.subarray(start, end);
#| #| const decoder = preserveBOM ? preserveBOMDecoder : dropBOMDecoder;
#| #| return [decoder.decode(slice)];
#| #| } catch (_) {
#| #| return [];
#| #| }
#| #| })(
#| #| new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }),
#| #| new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }),
#| #| )
#|extern "js" fn decode_utf8_lossy_js(
#| bytes : Bytes,
#| start : Int32,
#| len : Int32,
#| preserve_bom : Bool,
#|) -> String =
#| #| ((preserveBOMDecoder, dropBOMDecoder) => function(bytes, start, len, preserveBOM) {
#| #| const end = start + len;
#| #| const slice = bytes.subarray(start, end);
#| #| const decoder = preserveBOM ? preserveBOMDecoder : dropBOMDecoder;
#| #| return decoder.decode(slice);
#| #| })(
#| #| new TextDecoder("utf-8", { ignoreBOM: true }),
#| #| new TextDecoder("utf-8", { ignoreBOM: false }),
#| #| )
#|fn utf8_find_malformed(
#| src : Bytes,
#| src_offset : Int32,
#| src_length : Int32,
#|) -> Int32 {
#| let view = src[src_offset:src_offset + src_length]
#| for bytes = view {
#| match bytes {
#| [] => break -1
#| [
#| _..<=0x7F,
#| _..<=0x7F,
#| _..<=0x7F,
#| _..<=0x7F,
#| _..<=0x7F,
#| _..<=0x7F,
#| _..<=0x7F,
#| _..<=0x7F,
#| .. rest,
#| ] => continue rest
#| [0..<=0x7F, .. rest] => continue rest
#| [0xC2..<=0xDF, 0x80..<=0xBF, .. rest] => continue rest
#| [0xE0, 0xA0..<=0xBF, 0x80..<=0xBF, .. rest]
#| | [0xE1..<=0xEC, 0x80..<=0xBF, 0x80..<=0xBF, .. rest]
#| | [0xED, 0x80..<=0x9F, 0x80..<=0xBF, .. rest]
#| | [0xEE..<=0xEF, 0x80..<=0xBF, 0x80..<=0xBF, .. rest] => continue rest
#| [0xF0, 0x90..<=0xBF, 0x80..<=0xBF, 0x80..<=0xBF, .. rest]
#| | [0xF1..<=0xF3, 0x80..<=0xBF, 0x80..<=0xBF, 0x80..<=0xBF, .. rest]
#| | [0xF4, 0x80..<=0x8F, 0x80..<=0xBF, 0x80..<=0xBF, .. rest] =>
#| continue rest
#| malformed => break malformed.start_offset() - src_offset
#| }
#| }
#|}
#|fn strict_malformed_suffix(bytes : BytesView) -> BytesView {
#| let input = bytes.data()
#| let src_offset = bytes.start_offset()
#| let src_length = bytes.length()
#| let malformed_offset = utf8_find_malformed(input, src_offset, src_length)
#| bytes[malformed_offset:]
#|}
#|pub fn decode(
#| bytes : BytesView,
#| ignore_bom~ : Bool = false,
#|) -> String raise Malformed {
#| let result = decode_utf8_js(
#| bytes.data(),
#| bytes.start_offset(),
#| bytes.length(),
#| !ignore_bom,
#| )
#| if result.length() == 1 {
#| result[0]
#| } else {
#| let bytes = drop_utf8_bom(bytes, ignore_bom)
#| raise Malformed(strict_malformed_suffix(bytes))
#| }
#|}
#|pub fn decode_lossy(bytes : BytesView, ignore_bom~ : Bool = false) -> String {
#| decode_utf8_lossy_js(
#| bytes.data(),
#| bytes.start_offset(),
#| bytes.length(),
#| !ignore_bom,
#| )
#|}
),
"encode_js.mbt": (
#|extern "js" fn encode_utf8_js(
#| src : String,
#| start : Int32,
#| len : Int32,
#| bom : Bool,
#|) -> Bytes =
#| #| (() => {
#| #| const encoder = new TextEncoder();
#| #| return function(src, start, len, bom) {
#| #| const end = start + len;
#| #| const encoded = encoder.encode(src.slice(start, end));
#| #| if (!bom) {
#| #| return encoded;
#| #| }
#| #| const result = new Uint8Array(encoded.length + 3);
#| #| result[0] = 0xEF;
#| #| result[1] = 0xBB;
#| #| result[2] = 0xBF;
#| #| result.set(encoded, 3);
#| #| return result;
#| #| };
#| #| })()
#|pub fn encode(str : StringView, bom~ : Bool = false) -> Bytes {
#| encode_utf8_js(str.data(), str.start_offset(), str.length(), bom)
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Malformed with @debug.Debug::{to_repr}
),
},
)
///|
let moonbitlang_core_env_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/env",
deps={},
files={
"env.mbt": (
#|pub fn args() -> Array[String] {
#| get_cli_args_internal()
#|}
#|pub fn[T] exit(code : Int32) -> T {
#| exit_internal(code)
#| panic()
#|}
#|pub fn now() -> UInt64 {
#| now_internal()
#|}
#|pub fn current_dir() -> String? {
#| current_dir_internal()
#|}
#|pub fn get_env_var(key : String) -> String? {
#| get_env_var_internal(key)
#|}
#|pub fn get_env_vars() -> Map[String, String] {
#| get_env_vars_internal()
#|}
#|pub fn set_env_var(key : String, value : String) -> Unit {
#| set_env_var_internal(key, value)
#|}
#|pub fn unset_env_var(key : String) -> Unit {
#| unset_env_var_internal(key)
#|}
#|pub fn rand(n : Int32) -> Bytes? {
#| if n <= 0 {
#| let probe : FixedArray[Byte] = FixedArray::make(1, b'\x00')
#| return if rand_internal(probe) { Some(b"") } else { None }
#| }
#| let buf : FixedArray[Byte] = FixedArray::make(n, b'\x00')
#| if rand_internal(buf) {
#| Some(Bytes::from_array(buf))
#| } else {
#| None
#| }
#|}
#|#cfg(not(target="native"))
#|#warnings("-unused_value")
#|fn unused_function() -> Unit {
#| ignore(@ref.Ref(42))
#| let _ : (@os_string.OsString) -> Unit = ignore
#|}
),
"env_js.mbt": (
#|fn get_cli_args_internal() -> Array[String] {
#| Array::from_fixed_array(get_cli_args_ffi())
#|}
#|extern "js" fn get_cli_args_ffi() -> FixedArray[String] =
#| #| function() {
#| #| if (typeof process !== "undefined" && typeof process.argv !== "undefined") {
#| #| return process.argv;
#| #| } else {
#| #| return [];
#| #| }
#| #| }
#|extern "js" fn exit_internal(code : Int32) -> Unit =
#| #| function(code) {
#| #| if (typeof process !== "undefined" && typeof process.exit === "function") {
#| #| process.exit(code);
#| #| }
#| #| }
#|extern "js" fn now_internal() -> UInt64 =
#| #| function() {
#| #| const nowMs = Date.now();
#| #| return BigInt.asUintN(64, BigInt(nowMs));
#| #| }
#|extern "js" fn current_dir_ffi() -> String =
#| #| function() {
#| #| try {
#| #| if (typeof process !== "undefined" && typeof process.cwd === "function") {
#| #| return process.cwd();
#| #| }
#| #| } catch (_) {
#| #| // For example, the current directory may have been removed.
#| #| }
#| #| return "";
#| #| }
#|fn current_dir_internal() -> String? {
#| match current_dir_ffi() {
#| "" => None
#| cwd => Some(cwd)
#| }
#|}
#|fn get_env_var_internal(key : String) -> String? {
#| if is_env_var_exists_internal(key) {
#| Some(get_env_var_value_internal(key))
#| } else {
#| None
#| }
#|}
#|extern "js" fn is_env_var_exists_internal(key : String) -> Bool =
#| #| function(key) {
#| #| if (typeof process === "undefined" || typeof process.env === "undefined") {
#| #| return false;
#| #| }
#| #| return Object.prototype.hasOwnProperty.call(process.env, key);
#| #| }
#|extern "js" fn get_env_var_value_internal(key : String) -> String =
#| #| function(key) {
#| #| if (typeof process === "undefined" || typeof process.env === "undefined") {
#| #| return "";
#| #| }
#| #| const value = process.env[key];
#| #| return value === undefined ? "" : String(value);
#| #| }
#|fn get_env_vars_internal() -> Map[String, String] {
#| let tmp = get_env_vars_array_internal()
#| let res = Map([])
#| for i = 0; i < tmp.length(); i = i + 2 {
#| res[tmp[i]] = tmp[i + 1]
#| }
#| res
#|}
#|extern "js" fn get_env_vars_array_internal() -> FixedArray[String] =
#| #| function() {
#| #| if (typeof process === "undefined" || typeof process.env === "undefined") {
#| #| return [];
#| #| }
#| #| const result = [];
#| #| for (const key in process.env) {
#| #| const value = process.env[key];
#| #| if (value !== undefined) {
#| #| result.push(key);
#| #| result.push(String(value));
#| #| }
#| #| }
#| #| return result;
#| #| }
#|fn set_env_var_internal(key : String, value : String) -> Unit {
#| set_env_var_ffi(key, value)
#|}
#|extern "js" fn set_env_var_ffi(key : String, value : String) -> Unit =
#| #| function(key, value) {
#| #| if (typeof process !== "undefined" && typeof process.env !== "undefined") {
#| #| process.env[key] = value;
#| #| }
#| #| }
#|fn unset_env_var_internal(key : String) -> Unit {
#| unset_env_var_ffi(key)
#|}
#|extern "js" fn unset_env_var_ffi(key : String) -> Unit =
#| #| function(key) {
#| #| if (typeof process !== "undefined" && typeof process.env !== "undefined") {
#| #| delete process.env[key];
#| #| }
#| #| }
#|fn rand_internal(to_be_filled : FixedArray[Byte]) -> Bool {
#| if to_be_filled.length() == 0 {
#| return true
#| }
#| get_random_values_js(to_be_filled)
#|}
#|extern "js" fn get_random_values_js(to_be_filled : FixedArray[Byte]) -> Bool =
#| #| function(toBeFilled) {
#| #| const cryptoObj = globalThis.crypto;
#| #| if (cryptoObj == null || typeof cryptoObj.getRandomValues !== "function") {
#| #| return false;
#| #| }
#| #| try {
#| #| for (let offset = 0; offset < toBeFilled.length; offset += 65536) {
#| #| cryptoObj.getRandomValues(toBeFilled.subarray(offset, offset + 65536));
#| #| }
#| #| return true;
#| #| } catch (_) {
#| #| return false;
#| #| }
#| #| }
),
},
)
///|
let moonbitlang_core_float_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/float",
deps={},
files={
"builtin_types.mbt": (
#|#builtin
#|pub type Float
),
"deprecated.mbt": (
#|#deprecated("Use `@range.iter(start=start, end=end, step=..., inclusive=...)` instead; `Float::until` accumulates rounding error")
#|#coverage.skip
#|pub fn Float::until(
#| self : Float,
#| end : Float,
#| step~ : Float = 1.0,
#| inclusive~ : Bool = false,
#|) -> Iter[Float] {
#| if step == 0.0 {
#| return [||]
#| }
#| let mut curr_value = Some(self)
#| Iter(() => {
#| guard curr_value is Some(i) else { None }
#| guard (step > 0.0 && i < end) ||
#| (step < 0.0 && i > end) ||
#| (inclusive && i == end) else {
#| None
#| }
#| let next = i + step
#| if (step > 0.0 && next >= i) || (step < 0.0 && next <= i) {
#| curr_value = Some(next)
#| } else {
#| curr_value = None
#| }
#| Some(i)
#| })
#|}
),
"extends.mbt": (
#|pub extend Float with Add::{add}
#|pub extend Float with Compare::{compare}
#|pub extend Float with Div::{div}
#|pub extend Float with Eq::{equal}
#|pub extend Float with Hash::{hash}
#|pub extend Float with Mod::{mod}
#|pub extend Float with Mul::{mul}
#|pub extend Float with Neg::{neg}
#|pub extend Float with Show::{to_string}
#|pub extend Float with Sub::{sub}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Float with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Float with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Float with Hash::{hash_combine}
),
"float.mbt": (
#|pub let not_a_number : Float = Float::reinterpret_from_int(0x7FC00000)
#|pub let infinity : Float = Float::reinterpret_from_int(0x7F800000)
#|pub let neg_infinity : Float = Float::reinterpret_from_int(0xFF800000)
#|pub let max_value : Float = Float::reinterpret_from_int(0x7F7FFFFF)
#|pub let min_value : Float = Float::reinterpret_from_int(0xFF7FFFFF)
#|pub let min_positive : Float = Float::reinterpret_from_int(0x00800000)
#|pub fn Float::Float(self : Float) -> Float = "%identity"
#|#as_free_fn(deprecated)
#|pub fn Float::abs(self : Float) -> Float = "%f32.abs"
#|#deprecated
#|pub fn default() -> Float {
#| 0.0
#|}
),
"methods.mbt": (
#|pub impl Show for Float with fn to_string(self) {
#| let bits = self.reinterpret_as_uint()
#| let ieee_mantissa = bits & 0x7FFFFFU
#| let ieee_exponent = (bits >> 23) & 0xFFU
#| if ieee_exponent == 0xFFU || (ieee_exponent == 0U && ieee_mantissa == 0U) {
#| return self.to_double().to_string()
#| }
#| let decimal = f2d(ieee_mantissa, ieee_exponent)
#| let mut mantissa = decimal.mantissa
#| let mut exponent = decimal.exponent
#| while mantissa % 10U == 0U {
#| mantissa = mantissa / 10U
#| exponent += 1
#| }
#| let digits = mantissa.to_string()
#| let length = digits.length()
#| let exp = exponent + length - 1
#| let builder = StringBuilder(capacity=16)
#| if bits >> 31 != 0U {
#| builder.write_char('-')
#| }
#| if !(exp >= -6 && exp < 21) {
#| builder.write_view(digits[:1])
#| if length > 1 {
#| builder <+ ".\{digits[1:]}"
#| }
#| builder <+
#| "e\{if exp < 0 { '-' } else { '+' }}\{if exp < 0 { -exp } else { exp }}"
#| } else if exp < 0 {
#| builder <+ "0."
#| for _ in 0..<(-exp - 1) {
#| builder.write_char('0')
#| }
#| builder.write_string(digits)
#| } else if exp + 1 >= length {
#| builder.write_string(digits)
#| for _ in 0..<(exp + 1 - length) {
#| builder.write_char('0')
#| }
#| } else {
#| builder <+ "\{digits[:exp + 1]}.\{digits[exp + 1:]}"
#| }
#| builder.to_string()
#|}
#|pub impl Default for Float with fn default() {
#| 0
#|}
#|pub impl Hash for Float with fn hash_combine(self, hasher) {
#| let bits = self.reinterpret_as_uint()
#| hasher.combine_uint(if bits == 0x8000_0000U { 0U } else { bits })
#|}
#|pub fn Float::to_be_bytes(self : Float) -> Bytes {
#| let uint = self.reinterpret_as_uint()
#| [
#| (uint >> 24).to_byte(),
#| (uint >> 16).to_byte(),
#| (uint >> 8).to_byte(),
#| uint.to_byte(),
#| ]
#|}
#|pub fn Float::to_le_bytes(self : Float) -> Bytes {
#| let uint = self.reinterpret_as_uint()
#| [
#| uint.to_byte(),
#| (uint >> 8).to_byte(),
#| (uint >> 16).to_byte(),
#| (uint >> 24).to_byte(),
#| ]
#|}
#|pub fn Float::is_inf(self : Float) -> Bool {
#| self.is_pos_inf() || self.is_neg_inf()
#|}
#|pub fn Float::is_pos_inf(self : Float) -> Bool {
#| self > max_value
#|}
#|pub fn Float::is_neg_inf(self : Float) -> Bool {
#| self < min_value
#|}
#|pub fn Float::is_nan(self : Float) -> Bool {
#| self != self
#|}
#|pub fn Float::is_finite(self : Float) -> Bool {
#| self >= min_value && self <= max_value
#|}
#|pub fn Float::min(self : Float, other : Float) -> Float {
#| if self.is_nan() {
#| other
#| } else if other.is_nan() {
#| self
#| } else if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Float::max(self : Float, other : Float) -> Float {
#| if self.is_nan() {
#| other
#| } else if other.is_nan() {
#| self
#| } else if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Float::clamp(self : Float, min~ : Float, max~ : Float) -> Float {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub fn Float::lerp(self : Float, target~ : Float, t~ : Float) -> Float {
#| self + (target - self) * t
#|}
#|pub fn Float::signum(self : Float) -> Float {
#| if self < 0.0 {
#| -1.0
#| } else if self > 0.0 {
#| 1.0
#| } else {
#| self
#| }
#|}
#|pub fn Float::is_close(
#| self : Self,
#| other : Self,
#| relative_tolerance~ : Self = 1.0e-09,
#| absolute_tolerance~ : Self = 0.0,
#|) -> Bool {
#| if relative_tolerance < 0.0 || absolute_tolerance < 0.0 {
#| abort("Tolerances must be non-negative")
#| }
#| if self == other {
#| return true
#| }
#| if self.is_inf() || other.is_inf() {
#| return false
#| }
#| let diff = (other - self).abs()
#| return (
#| diff <= (relative_tolerance * other).abs() ||
#| diff <= (relative_tolerance * self).abs()
#| ) ||
#| diff <= absolute_tolerance
#|}
#|pub impl Mod for Float with fn mod(self : Float, other : Float) -> Float {
#| Float::from_double(self.to_double() % other.to_double())
#|}
#|pub fn Float::to_double(self : Float) -> Double = "%f32.to_f64"
#|pub fn Float::sqrt(self : Float) -> Float = "%f32.sqrt"
#|pub fn Float::reinterpret_as_int(self : Float) -> Int32 = "%f32.to_i32_reinterpret"
#|pub impl Neg for Float with fn neg(self) = "%f32.neg"
#|pub impl Add for Float with fn add(self, other) = "%f32.add"
#|pub impl Sub for Float with fn sub(self, other) = "%f32.sub"
#|pub impl Mul for Float with fn mul(self, other) = "%f32.mul"
#|pub impl Div for Float with fn div(self, other) = "%f32.div"
#|pub impl Eq for Float with fn equal(self : Float, other : Float) -> Bool = "%f32.eq"
#|pub impl Eq for Float with fn not_equal(self : Float, other : Float) -> Bool = "%f32.ne"
#|pub impl Compare for Float with fn compare(self, other) = "%f32.compare"
#|pub impl Compare for Float with fn lt(x, y) = "%f32.lt"
#|pub impl Compare for Float with fn le(x, y) = "%f32.le"
#|pub impl Compare for Float with fn gt(x, y) = "%f32.gt"
#|pub impl Compare for Float with fn ge(x, y) = "%f32.ge"
#|pub fn Float::reinterpret_as_uint(self : Float) -> UInt32 = "%f32.to_i32_reinterpret"
#|pub fn Float::reinterpret_from_int(self : Int32) -> Float = "%i32.to_f32_reinterpret"
#|pub fn Float::reinterpret_from_uint(self : UInt32) -> Float = "%i32.to_f32_reinterpret"
#|test "Float::reinterpret" {
#| inspect(Float::reinterpret_from_int(1065353216), content="1")
#| inspect(Float::reinterpret_from_uint(0x3F800000), content="1")
#|}
#|pub fn Float::from_int(self : Int32) -> Float = "%i32.to_f32"
#|pub fn Float::from_byte(self : Byte) -> Float = "%byte.to_f32"
#|pub fn Float::from_double(self : Double) -> Float = "%f64.to_f32"
#|pub fn Float::from_uint(self : UInt32) -> Float = "%u32.to_f32"
#|#cfg(not(target="js"))
#|pub fn Float::from_uint64(self : UInt64) -> Float = "%u64.to_f32"
#|#cfg(target="js")
#|pub fn Float::from_uint64(self : UInt64) -> Float {
#| Float::from_double(uint64_to_double_round_to_odd(self))
#|}
#|#cfg(target="js")
#|fn uint64_to_double_round_to_odd(x : UInt64) -> Double {
#| let n = 64 - x.clz()
#| if n <= 53 {
#| return x.to_double()
#| }
#| let k = n - 53
#| let sticky = if (x & ((1UL << k) - 1UL)) != 0UL { 1UL } else { 0UL }
#| ((x >> k) | sticky).to_double() * (1UL << k).to_double()
#|}
#|test "Float::from_uint64" {
#| let n = 42UL
#| inspect(Float::from_uint64(n).to_double(), content="42")
#| let big = 18446744073709551615UL // UInt64::max_value
#| inspect(Float::from_uint64(big).to_double(), content="18446744073709552000")
#|}
#|#cfg(not(target="js"))
#|pub fn Float::from_int64(self : Int64) -> Float = "%i64.to_f32"
#|#cfg(target="js")
#|pub fn Float::from_int64(self : Int64) -> Float {
#| if self < 0L {
#| -Float::from_uint64((-self).reinterpret_as_uint64())
#| } else {
#| Float::from_uint64(self.reinterpret_as_uint64())
#| }
#|}
#|test "Float::from_int64" {
#| let n = 42L
#| let f = Float::from_int64(n)
#| inspect(f.to_double(), content="42")
#|}
#|pub fn Float::to_int64(self : Float) -> Int64 {
#| self.to_double().to_int64()
#|}
#|pub fn Float::to_uint(self : Float) -> UInt32 {
#| self.to_double().to_uint()
#|}
#|pub fn Float::to_uint64(self : Float) -> UInt64 {
#| self.to_double().to_uint64()
#|}
),
"pow.mbt": (
#|#deprecated("Use `@math.powf` instead")
#|#as_free_fn(deprecated="Use `@math.powf` instead")
#|#warnings("-deprecated")
#|pub fn Float::pow(self : Float, other : Float) -> Float {
#| Float::from_double(self.to_double().pow(other.to_double()))
#|}
),
"round_js.mbt": (
#|#as_free_fn
#|pub fn Float::trunc(self : Float) -> Float = "Math" "trunc"
#|#as_free_fn
#|pub fn Float::ceil(self : Float) -> Float = "Math" "ceil"
#|#as_free_fn
#|pub fn Float::floor(self : Float) -> Float = "Math" "floor"
#|#as_free_fn
#|pub fn Float::round(self : Float) -> Float = "Math" "round"
),
"ryu.mbt": (
#|#valtype
#|priv struct FloatingDecimal32 {
#| mantissa : UInt32
#| exponent : Int32
#|}
#|const FLOAT_POW5_BITCOUNT : Int32 = 61
#|fn f2s_pow5bits(e : Int32) -> Int32 {
#| ((e * 1217359) >> 19) + 1
#|}
#|fn f2s_log10_pow2(e : Int32) -> Int32 {
#| (e * 78913) >> 18
#|}
#|fn f2s_log10_pow5(e : Int32) -> Int32 {
#| (e * 732923) >> 20
#|}
#|fn f2s_multiple_of_power_of_5_32(value : UInt32, p : Int32) -> Bool {
#| let mut value = value
#| let mut count = 0
#| while value % 5U == 0U {
#| value = value / 5U
#| count += 1
#| }
#| count >= p
#|}
#|fn f2s_multiple_of_power_of_2_32(value : UInt32, p : Int32) -> Bool {
#| (value & ((1U << p) - 1U)) == 0U
#|}
#|fn f2s_mul_shift_32(m : UInt32, factor : UInt64, shift : Int32) -> UInt32 {
#| let factor_lo = factor & 0xFFFFFFFFUL
#| let factor_hi = factor >> 32
#| let m = m.to_uint64()
#| let bits0 = m * factor_lo
#| let bits1 = m * factor_hi
#| let sum = (bits0 >> 32) + bits1
#| (sum >> (shift - 32)).to_uint()
#|}
#|fn f2s_mul_pow5_inv_div_pow2(m : UInt32, q : Int32, j : Int32) -> UInt32 {
#| f2s_mul_shift_32(m, f2s_pow5_inv_split_hi[q] + 1UL, j)
#|}
#|fn f2s_mul_pow5_div_pow2(m : UInt32, i : Int32, j : Int32) -> UInt32 {
#| f2s_mul_shift_32(m, f2s_pow5_split_hi[i], j)
#|}
#|fn f2d(ieee_mantissa : UInt32, ieee_exponent : UInt32) -> FloatingDecimal32 {
#| let e2 = if ieee_exponent == 0U {
#| 1 - 127 - 23 - 2
#| } else {
#| ieee_exponent.reinterpret_as_int() - 127 - 23 - 2
#| }
#| let m2 = if ieee_exponent == 0U {
#| ieee_mantissa
#| } else {
#| (1U << 23) | ieee_mantissa
#| }
#| let even = (m2 & 1U) == 0U
#| let accept_bounds = even
#| let mv = 4U * m2
#| let mp = 4U * m2 + 2U
#| let mm_shift : UInt32 = if ieee_mantissa != 0U || ieee_exponent <= 1U {
#| 1U
#| } else {
#| 0U
#| }
#| let mm = 4U * m2 - 1U - mm_shift
#| let mut vr = 0U
#| let mut vp = 0U
#| let mut vm = 0U
#| let mut e10 = 0
#| let mut vm_is_trailing_zeros = false
#| let mut vr_is_trailing_zeros = false
#| let mut last_removed_digit = 0U
#| if e2 >= 0 {
#| let q = f2s_log10_pow2(e2)
#| e10 = q
#| let k = FLOAT_POW5_BITCOUNT + f2s_pow5bits(q) - 1
#| let i = -e2 + q + k
#| vr = f2s_mul_pow5_inv_div_pow2(mv, q, i)
#| vp = f2s_mul_pow5_inv_div_pow2(mp, q, i)
#| vm = f2s_mul_pow5_inv_div_pow2(mm, q, i)
#| if q != 0 && (vp - 1U) / 10U <= vm / 10U {
#| let l = FLOAT_POW5_BITCOUNT + f2s_pow5bits(q - 1) - 1
#| last_removed_digit = f2s_mul_pow5_inv_div_pow2(mv, q - 1, -e2 + q - 1 + l) %
#| 10U
#| }
#| if q <= 9 {
#| if mv % 5U == 0U {
#| vr_is_trailing_zeros = f2s_multiple_of_power_of_5_32(mv, q)
#| } else if accept_bounds {
#| vm_is_trailing_zeros = f2s_multiple_of_power_of_5_32(mm, q)
#| } else if f2s_multiple_of_power_of_5_32(mp, q) {
#| vp = vp - 1U
#| }
#| }
#| } else {
#| let q = f2s_log10_pow5(-e2)
#| e10 = q + e2
#| let i = -e2 - q
#| let k = f2s_pow5bits(i) - FLOAT_POW5_BITCOUNT
#| let mut j = q - k
#| vr = f2s_mul_pow5_div_pow2(mv, i, j)
#| vp = f2s_mul_pow5_div_pow2(mp, i, j)
#| vm = f2s_mul_pow5_div_pow2(mm, i, j)
#| if q != 0 && (vp - 1U) / 10U <= vm / 10U {
#| j = q - 1 - (f2s_pow5bits(i + 1) - FLOAT_POW5_BITCOUNT)
#| last_removed_digit = f2s_mul_pow5_div_pow2(mv, i + 1, j) % 10U
#| }
#| if q <= 1 {
#| vr_is_trailing_zeros = true
#| if accept_bounds {
#| vm_is_trailing_zeros = mm_shift == 1U
#| } else {
#| vp = vp - 1U
#| }
#| } else if q < 31 {
#| vr_is_trailing_zeros = f2s_multiple_of_power_of_2_32(mv, q - 1)
#| }
#| }
#| let mut removed = 0
#| let output = if vm_is_trailing_zeros || vr_is_trailing_zeros {
#| while vp / 10U > vm / 10U {
#| vm_is_trailing_zeros = vm_is_trailing_zeros && vm - vm / 10U * 10U == 0U
#| vr_is_trailing_zeros = vr_is_trailing_zeros && last_removed_digit == 0U
#| last_removed_digit = vr % 10U
#| vr = vr / 10U
#| vp = vp / 10U
#| vm = vm / 10U
#| removed += 1
#| }
#| if vm_is_trailing_zeros {
#| while vm % 10U == 0U {
#| vr_is_trailing_zeros = vr_is_trailing_zeros && last_removed_digit == 0U
#| last_removed_digit = vr % 10U
#| vr = vr / 10U
#| vm = vm / 10U
#| removed += 1
#| }
#| }
#| if vr_is_trailing_zeros && last_removed_digit == 5U && vr % 2U == 0U {
#| last_removed_digit = 4U
#| }
#| if (vr == vm && (!accept_bounds || !vm_is_trailing_zeros)) ||
#| last_removed_digit >= 5U {
#| vr + 1U
#| } else {
#| vr
#| }
#| } else {
#| while vp / 10U > vm / 10U {
#| last_removed_digit = vr % 10U
#| vr = vr / 10U
#| vp = vp / 10U
#| vm = vm / 10U
#| removed += 1
#| }
#| if vr == vm || last_removed_digit >= 5U {
#| vr + 1U
#| } else {
#| vr
#| }
#| }
#| { mantissa: output, exponent: e10 + removed, }
#|}
#|let f2s_pow5_inv_split_hi : ReadOnlyArray[UInt64] = [
#| 0x2000000000000000UL, 0x1999999999999999UL, 0x147AE147AE147AE1UL, 0x10624DD2F1A9FBE7UL,
#| 0x1A36E2EB1C432CA5UL, 0x14F8B588E368F084UL, 0x10C6F7A0B5ED8D36UL, 0x1AD7F29ABCAF4857UL,
#| 0x15798EE2308C39DFUL, 0x112E0BE826D694B2UL, 0x1B7CDFD9D7BDBAB7UL, 0x15FD7FE17964955FUL,
#| 0x119799812DEA1119UL, 0x1C25C268497681C2UL, 0x16849B86A12B9B01UL, 0x1203AF9EE756159BUL,
#| 0x1CD2B297D889BC2BUL, 0x170EF54646D49689UL, 0x12725DD1D243ABA0UL, 0x1D83C94FB6D2AC34UL,
#| 0x179CA10C9242235DUL, 0x12E3B40A0E9B4F7DUL, 0x1E392010175EE596UL, 0x182DB34012B25144UL,
#| 0x1357C299A88EA76AUL, 0x1EF2D0F5DA7DD8AAUL, 0x18C240C4AECB13BBUL, 0x13CE9A36F23C0FC9UL,
#| 0x1FB0F6BE50601941UL, 0x195A5EFEA6B34767UL, 0x14484BFEEBC29F86UL,
#|]
#|let f2s_pow5_split_hi : ReadOnlyArray[UInt64] = [
#| 0x1000000000000000UL, 0x1400000000000000UL, 0x1900000000000000UL, 0x1F40000000000000UL,
#| 0x1388000000000000UL, 0x186A000000000000UL, 0x1E84800000000000UL, 0x1312D00000000000UL,
#| 0x17D7840000000000UL, 0x1DCD650000000000UL, 0x12A05F2000000000UL, 0x174876E800000000UL,
#| 0x1D1A94A200000000UL, 0x12309CE540000000UL, 0x16BCC41E90000000UL, 0x1C6BF52634000000UL,
#| 0x11C37937E0800000UL, 0x16345785D8A00000UL, 0x1BC16D674EC80000UL, 0x1158E460913D0000UL,
#| 0x15AF1D78B58C4000UL, 0x1B1AE4D6E2EF5000UL, 0x10F0CF064DD59200UL, 0x152D02C7E14AF680UL,
#| 0x1A784379D99DB420UL, 0x108B2A2C28029094UL, 0x14ADF4B7320334B9UL, 0x19D971E4FE8401E7UL,
#| 0x1027E72F1F128130UL, 0x1431E0FAE6D7217CUL, 0x193E5939A08CE9DBUL, 0x1F8DEF8808B02452UL,
#| 0x13B8B5B5056E16B3UL, 0x18A6E32246C99C60UL, 0x1ED09BEAD87C0378UL, 0x13426172C74D822BUL,
#| 0x1812F9CF7920E2B6UL, 0x1E17B84357691B64UL, 0x12CED32A16A1B11EUL, 0x178287F49C4A1D66UL,
#| 0x1D6329F1C35CA4BFUL, 0x125DFA371A19E6F7UL, 0x16F578C4E0A060B5UL, 0x1CB2D6F618C878E3UL,
#| 0x11EFC659CF7D4B8DUL, 0x166BB7F0435C9E71UL, 0x1C06A5EC5433C60DUL, 0x118427B3B4A05BC8UL,
#|]
),
"to_int.mbt": (
#|fn Float::to_unchecked_int(self : Float) -> Int32 = "%f32.to_i32"
#|pub fn Float::to_int(self : Float) -> Int32 {
#| if self != self {
#| 0
#| } else if self >= 2147483647 {
#| 2147483647
#| } else if self <= -2147483648 {
#| -2147483648
#| } else {
#| self.to_unchecked_int()
#| }
#|}
),
"total_compare.mbt": (
#|pub fn Float::total_compare(self : Float, other : Float) -> Int32 {
#| (self > other).to_int() -
#| (self < other).to_int() +
#| (other.is_nan().to_int() - self.is_nan().to_int())
#|}
),
},
)
///|
let moonbitlang_core_hashmap_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/hashmap",
deps={},
files={
"debug.mbt": (
#|pub impl[K : @debug.Debug, V : @debug.Debug] @debug.Debug for HashMap[K, V] with fn to_repr(
#| self,
#|) {
#| @debug.Repr::opaque_(
#| "HashMap",
#| @debug.Repr::map([ for k, v in self => (Repr(k), Repr(v)) ]),
#| )
#|}
#|test "Debug for HashMap" {
#| let hm : HashMap[Int32, String] = from_array([(2, "b")])
#| @debug.debug_inspect(
#| hm,
#| content=(
#| #|
#| ),
#| )
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `HashMap([], capacity=...)` instead")
#|#as_free_fn(deprecated="Use `HashMap([], capacity=...)` instead")
#|pub fn[K, V] HashMap::new(
#| capacity~ : Int32 = default_init_capacity,
#|) -> HashMap[K, V] {
#| new_hashmap(capacity)
#|}
#|#deprecated("The bucket count is an implementation detail; the constructor's `capacity` hint is the only sizing control")
#|pub fn[K, V] HashMap::capacity(self : HashMap[K, V]) -> Int32 {
#| self.capacity
#|}
),
"extends.mbt": (
#|pub extend HashMap with Eq::{equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with @debug.Debug::{to_repr}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with @json.FromJson::{from_json}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with Eq::{not_equal}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with @json.ToJson::{to_json}
),
"hashmap.mbt": (
#|let default_init_capacity = 8
#|fn[K, V] new_hashmap(capacity : Int32) -> HashMap[K, V] {
#| let capacity = capacity.next_power_of_two()
#| {
#| size: 0,
#| capacity,
#| entries: FixedArray::make(capacity, None),
#| capacity_mask: capacity - 1,
#| }
#|}
#|fn capacity_for_length(length : Int32) -> Int32 {
#| guard length <= 0x40000000 / 2 else { abort("HashMap capacity overflow") }
#| (length * 2).next_power_of_two()
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[K : Hash + Eq, V] HashMap::HashMap(
#| arr : ArrayView[(K, V)],
#| capacity? : Int32,
#|) -> HashMap[K, V] {
#| let length = arr.length()
#| let capacity = match capacity {
#| Some(capacity) => {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| capacity_for_length(capacity.max(length))
#| }
#| None =>
#| if length == 0 {
#| default_init_capacity
#| } else {
#| capacity_for_length(length)
#| }
#| }
#| let m = new_hashmap(capacity)
#| arr.each(e => m.set(e.0, e.1))
#| m
#|}
#|#alias("_[_]=_")
#|#owned(value)
#|pub fn[K : Hash + Eq, V] HashMap::set(
#| self : HashMap[K, V],
#| key : K,
#| value : V,
#|) -> Unit {
#| self.set_with_hash(key, value, Hash::hash(key))
#|}
#|#owned(value)
#|fn[K : Eq, V] HashMap::set_with_hash(
#| self : HashMap[K, V],
#| key : K,
#| value : V,
#| hash : Int32,
#|) -> Unit {
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| if self.size >= self.capacity / 2 {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| let entry = { psl, key, value, hash, }
#| self.entries.unsafe_set(idx, Some(entry))
#| self.size += 1
#| return
#| }
#| Some(curr_entry) => {
#| if curr_entry.hash == hash && curr_entry.key == key {
#| curr_entry.value = value
#| return
#| }
#| if psl > curr_entry.psl {
#| if self.size >= self.capacity / 2 {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| self.push_away(idx, curr_entry)
#| let entry = { psl, key, value, hash, }
#| self.entries.unsafe_set(idx, Some(entry))
#| self.size += 1
#| return
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|#owned(entry)
#|fn[K, V] HashMap::push_away(
#| self : HashMap[K, V],
#| idx : Int32,
#| entry : Entry[K, V],
#|) -> Unit {
#| for psl = entry.psl + 1, idx = (idx + 1) & self.capacity_mask, entry = entry {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| entry.psl = psl
#| self.entries.unsafe_set(idx, Some(entry))
#| break
#| }
#| Some(curr_entry) =>
#| if psl > curr_entry.psl {
#| entry.psl = psl
#| self.entries.unsafe_set(idx, Some(entry))
#| continue curr_entry.psl + 1,
#| (idx + 1) & self.capacity_mask,
#| curr_entry
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask, entry
#| }
#| }
#| }
#|}
#|pub fn[K : Hash + Eq, V] HashMap::get(self : HashMap[K, V], key : K) -> V? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && entry.key == key {
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[V] HashMap::get_from_bytes(
#| self : HashMap[Bytes, V],
#| key : BytesView,
#|) -> V? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && key.equal_to_bytes(entry.key) {
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[V] HashMap::get_from_string(
#| self : HashMap[String, V],
#| key : StringView,
#|) -> V? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && key.equal_to_string(entry.key) {
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|#alias("_[_]")
#|pub fn[K : Hash + Eq, V] HashMap::at(self : HashMap[K, V], key : K) -> V {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard! self.entries.unsafe_get(idx) is Some(entry)
#| if entry.hash == hash && entry.key == key {
#| break entry.value
#| }
#| guard! i <= entry.psl
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V] HashMap::get_or_init(
#| self : HashMap[K, V],
#| key : K,
#| init : () -> V raise?,
#|) -> V raise? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| return entry.value
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#| let new_value = init()
#| self.set_with_hash(key, new_value, hash)
#| new_value
#|}
#|pub fn[K : Hash + Eq, V] HashMap::update(
#| self : HashMap[K, V],
#| key : K,
#| f : (V?) -> V? raise?,
#|) -> Unit raise? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| let result = f(Some(entry.value))
#| let idx = idx & self.capacity_mask
#| if self.entries.unsafe_get(idx) is Some(current) &&
#| physical_equal(current, entry) {
#| if result is Some(new_value) {
#| entry.value = new_value
#| } else {
#| self.shift_back(idx)
#| self.size -= 1
#| }
#| } else if result is Some(new_value) {
#| self.set_with_hash(key, new_value, hash)
#| } else {
#| self.remove_with_hash(key, hash)
#| }
#| return
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#| guard f(None) is Some(new_value) else { return }
#| self.set_with_hash(key, new_value, hash)
#|}
#|pub fn[K : Hash + Eq, V] HashMap::update_or_default(
#| self : HashMap[K, V],
#| key : K,
#| default : V,
#| f : (V) -> V raise?,
#|) -> Unit raise? {
#| let hash = Hash::hash(key)
#| let (idx, psl, push_away) = for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| Some(entry) => {
#| if entry.hash == hash && entry.key == key {
#| let new_value = f(entry.value)
#| if self.entries.unsafe_get(idx & self.capacity_mask) is Some(current) &&
#| physical_equal(current, entry) {
#| entry.value = new_value
#| } else {
#| self.set_with_hash(key, new_value, hash)
#| }
#| return
#| }
#| if psl > entry.psl {
#| break (idx, psl, Some(entry))
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| None => break (idx, psl, None)
#| }
#| }
#| if self.size >= self.capacity / 2 {
#| self.grow()
#| self.set_with_hash(key, default, hash)
#| } else {
#| if push_away is Some(entry) {
#| self.push_away(idx, entry)
#| }
#| let entry = { psl, hash, key, value: default, }
#| self.entries.unsafe_set(idx, Some(entry))
#| self.size += 1
#| }
#|}
#|pub fn[K : Hash + Eq, V] HashMap::get_or_default(
#| self : HashMap[K, V],
#| key : K,
#| default : V,
#|) -> V {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break default }
#| if entry.hash == hash && entry.key == key {
#| break entry.value
#| }
#| if i > entry.psl {
#| break default
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V] HashMap::contains(
#| self : HashMap[K, V],
#| key : K,
#|) -> Bool {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { return false }
#| if entry.hash == hash && entry.key == key {
#| return true
#| }
#| if i > entry.psl {
#| return false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V : Eq] HashMap::contains_kv(
#| self : HashMap[K, V],
#| key : K,
#| value : V,
#|) -> Bool {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { return false }
#| if entry.hash == hash && entry.key == key && entry.value == value {
#| return true
#| }
#| if i > entry.psl {
#| return false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq, V] HashMap::remove(self : HashMap[K, V], key : K) -> Unit {
#| self.remove_with_hash(key, Hash::hash(key))
#|}
#|fn[K : Eq, V] HashMap::remove_with_hash(
#| self : HashMap[K, V],
#| key : K,
#| hash : Int32,
#|) -> Unit {
#| for i = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| Some(entry) => {
#| if entry.hash == hash && entry.key == key {
#| self.shift_back(idx)
#| self.size -= 1
#| break
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#| None => break
#| }
#| }
#|}
#|pub fn[K : Hash + Eq, V] HashMap::remove_and_get(
#| self : HashMap[K, V],
#| key : K,
#|) -> V? {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break None }
#| if entry.hash == hash && entry.key == key {
#| self.shift_back(idx)
#| self.size -= 1
#| break Some(entry.value)
#| }
#| if i > entry.psl {
#| break None
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|fn[K, V] HashMap::shift_back(self : HashMap[K, V], idx : Int32) -> Unit {
#| for cur = idx {
#| let next = (cur + 1) & self.capacity_mask
#| match self.entries.unsafe_get(next) {
#| None | Some({ psl: 0, .. }) => {
#| self.entries.unsafe_set(cur, None)
#| break
#| }
#| Some(entry) => {
#| entry.psl -= 1
#| self.entries.unsafe_set(cur, Some(entry))
#| continue next
#| }
#| }
#| }
#|}
#|fn[K, V] HashMap::grow(self : HashMap[K, V]) -> Unit {
#| let old_entries = self.entries
#| let new_capacity = self.capacity << 1
#| self.entries = FixedArray::make(new_capacity, None)
#| self.capacity = new_capacity
#| self.capacity_mask = new_capacity - 1
#| for entry in old_entries {
#| if entry is Some(entry) {
#| self.rehash_place_entry(entry)
#| }
#| }
#|}
#|#owned(entry)
#|fn[K, V] HashMap::rehash_place_entry(
#| self : HashMap[K, V],
#| entry : Entry[K, V],
#|) -> Unit {
#| let hash = entry.hash
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| entry.psl = psl
#| self.entries.unsafe_set(idx, Some(entry))
#| return
#| }
#| Some(curr) =>
#| if psl > curr.psl {
#| self.push_away(idx, curr)
#| entry.psl = psl
#| self.entries.unsafe_set(idx, Some(entry))
#| return
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|test "of" {
#| let m = from_array([(1, 2), (3, 4)])
#| @debug.debug_inspect(m.get(1), content="Some(2)")
#| @debug.debug_inspect(m.get(3), content="Some(4)")
#|}
#|pub impl[K, V] Default for HashMap[K, V] with fn default() {
#| new_hashmap(default_init_capacity)
#|}
#|pub fn[K, V, V2] HashMap::map(
#| self : HashMap[K, V],
#| f : (K, V) -> V2 raise?,
#|) -> HashMap[K, V2] raise? {
#| let { entries, capacity, capacity_mask, .. } = self
#| let other_entries = FixedArray::make(capacity, None)
#| let mut size = 0
#| for i in 0.. HashMap[K, V] {
#| let other = {
#| capacity: self.capacity,
#| entries: FixedArray::make(self.capacity, None),
#| size: self.size,
#| capacity_mask: self.capacity_mask,
#| }
#| if self.size == 0 {
#| return other
#| }
#| for i in 0.. HashMap[K, V] {
#| let result = self.copy()
#| result.merge_in_place(other)
#| result
#|}
#|pub fn[K : Eq, V] HashMap::merge_in_place(
#| self : HashMap[K, V],
#| other : HashMap[K, V],
#|) -> Unit {
#| if physical_equal(self, other) {
#| return
#| }
#| for entry in other.entries {
#| if entry is Some({ key, value, hash, .. }) {
#| self.set_with_hash(key, value, hash)
#| }
#| }
#|}
#|#derive(Eq, @debug.Debug)
#|priv struct MyString(String)
#|#coverage.skip
#|impl Hash for MyString with fn hash(self) {
#| let MyString(self) = self
#| self.length()
#|}
#|#coverage.skip
#|impl Hash for MyString with fn hash_combine(self, hasher) {
#| hasher.combine_string(self.0)
#|}
#|test "set" {
#| let m : HashMap[MyString, Int32] = new_hashmap(default_init_capacity)
#| m.set("a", 1)
#| m.set("b", 1)
#| m.set("bc", 2)
#| m.set("abc", 3)
#| m.set("cd", 2)
#| m.set("c", 1)
#| m.set("d", 1)
#| inspect(m.length(), content="7")
#|}
#|test "remove" {
#| let m : HashMap[MyString, Int32] = new_hashmap(default_init_capacity)
#| m.set("a", 1)
#| m.set("ab", 2)
#| m.set("bc", 2)
#| m.set("cd", 2)
#| m.set("abc", 3)
#| m.set("abcdef", 6)
#| m.remove("ab")
#| inspect(m.length(), content="5")
#|}
#|test "remove_unexist_key" {
#| let m : HashMap[MyString, Int32] = new_hashmap(default_init_capacity)
#| m.set("a", 1)
#| m.set("ab", 2)
#| m.set("abc", 3)
#| m.remove("d")
#| inspect(m.length(), content="3")
#|}
#|test "clear" {
#| let m : HashMap[MyString, Int32] = from_array([("a", 1), ("b", 2), ("c", 3)])
#| m.clear()
#| inspect(m.length(), content="0")
#| inspect(m.capacity, content="8")
#| for entry in m.entries {
#| @test.assert_same_object(entry, None)
#| }
#|}
#|test "grow" {
#| let m : HashMap[MyString, Int32] = new_hashmap(default_init_capacity)
#| m.set("C", 1)
#| m.set("Go", 2)
#| m.set("C++", 3)
#| m.set("Java", 4)
#| m.set("Scala", 5)
#| m.set("Julia", 5)
#| inspect(m.length(), content="6")
#| inspect(m.capacity, content="16")
#| m.set("Cobol", 5)
#| inspect(m.length(), content="7")
#| inspect(m.capacity, content="16")
#| m.set("Python", 6)
#| m.set("Haskell", 7)
#| m.set("Rescript", 8)
#| inspect(m.length(), content="10")
#| inspect(m.capacity, content="32")
#| @debug.debug_inspect(m.get("C"), content="Some(1)")
#| @debug.debug_inspect(m.get("Go"), content="Some(2)")
#| @debug.debug_inspect(m.get("C++"), content="Some(3)")
#| @debug.debug_inspect(m.get("Java"), content="Some(4)")
#| @debug.debug_inspect(m.get("Scala"), content="Some(5)")
#| @debug.debug_inspect(m.get("Julia"), content="Some(5)")
#| @debug.debug_inspect(m.get("Cobol"), content="Some(5)")
#| @debug.debug_inspect(m.get("Python"), content="Some(6)")
#| @debug.debug_inspect(m.get("Haskell"), content="Some(7)")
#| @debug.debug_inspect(m.get("Rescript"), content="Some(8)")
#|}
#|test "_[_]" {
#| let m : HashMap[MyString, Int32] = new_hashmap(default_init_capacity)
#| m.set("a", 1)
#| m.set("b", 1)
#| m.set("cc", 2)
#| inspect(m["a"], content="1")
#| inspect(m["b"], content="1")
#| inspect(m["cc"], content="2")
#|}
#|test "get_or_init" {
#| let m : HashMap[MyString, Int32] = new_hashmap(default_init_capacity)
#| inspect(m.get_or_init("a", () => 1), content="1")
#| @debug.debug_inspect(m.get("a"), content="Some(1)")
#| inspect(m.get_or_init("a", () => 2), content="1")
#| @debug.debug_inspect(m.get("a"), content="Some(1)")
#|}
),
"json.mbt": (
#|pub impl[K : Show, V : @json.ToJson] @json.ToJson for HashMap[K, V] with fn to_json(
#| self,
#|) {
#| @json.Json::object(
#| Map(
#| capacity=self.length(),
#| [
#| for k, v in self => (k.to_string(), v.to_json())
#| ],
#| ),
#| )
#|}
#|pub impl[V : @json.FromJson] @json.FromJson for HashMap[String, V] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Object(obj) else {
#| raise path.error("@hashmap.from_json: expected object")
#| }
#| let res : HashMap[String, V] = HashMap([], capacity=obj.length())
#| for k, v in obj {
#| res[k] = V::from_json(v, path.add_key(k))
#| }
#| res
#|}
),
"types.mbt": (
#|#derive(@debug.Debug)
#|priv struct Entry[K, V] {
#| mut psl : Int32
#| hash : Int32
#| key : K
#| mut value : V
#|}
#|struct HashMap[K, V] {
#| mut entries : FixedArray[Entry[K, V]?]
#| mut capacity : Int32
#| mut capacity_mask : Int32 // capacity_mask = capacity - 1, used to find idx
#| mut size : Int32 // active key-value pairs count
#|}
#|pub impl[K : Hash + Eq, V : Eq] Eq for HashMap[K, V] with fn equal(
#| self : HashMap[K, V],
#| that : HashMap[K, V],
#|) -> Bool {
#| guard self.size == that.size else { return false }
#| for k, v in self {
#| guard that.contains_kv(k, v) else { return false }
#| } nobreak {
#| true
#| }
#|}
),
"utils.mbt": (
#|pub fn[K, V] HashMap::clear(self : HashMap[K, V]) -> Unit {
#| self.entries.fill(None)
#| self.size = 0
#|}
#|#alias(iterator, deprecated)
#|pub fn[K, V] HashMap::iter(self : HashMap[K, V]) -> Iter[(K, V)] {
#| let mut i = 0
#| let len = self.entries.length()
#| Iter(
#| fn() {
#| while i < len {
#| let entry = self.entries.unsafe_get(i)
#| i += 1
#| if entry is Some({ key, value, .. }) {
#| return Some((key, value))
#| }
#| } nobreak {
#| None
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[K, V] HashMap::iter2(self : HashMap[K, V]) -> Iter2[K, V] {
#| self.iter()
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Hash + Eq, V] HashMap::from_iter(
#| iter : Iter[(K, V)],
#|) -> HashMap[K, V] {
#| let m = new_hashmap(default_init_capacity)
#| while iter.next() is Some((k, v)) {
#| m[k] = v
#| }
#| m
#|}
#|pub fn[K, V] HashMap::to_array(self : HashMap[K, V]) -> Array[(K, V)] {
#| let res = Array(capacity=self.size)
#| for i in 0.. Int32 {
#| self.size
#|}
#|pub fn[K, V] HashMap::is_empty(self : HashMap[K, V]) -> Bool {
#| self.size == 0
#|}
#|#locals(f)
#|pub fn[K, V] HashMap::each(
#| self : HashMap[K, V],
#| f : (K, V) -> Unit raise?,
#|) -> Unit raise? {
#| for i in 0.. Unit raise?,
#|) -> Unit raise? {
#| for i = 0, idx = 0; i < self.capacity; {
#| match self.entries[i] {
#| Some({ key, value, .. }) => {
#| f(idx, key, value)
#| continue i + 1, idx + 1
#| }
#| None => continue i + 1, idx
#| }
#| }
#|}
#|pub fn[K, V] HashMap::keys(self : HashMap[K, V]) -> Iter[K] {
#| let mut i = 0
#| Iter(
#| () => {
#| while i < self.entries.length() {
#| let entry = self.entries[i]
#| i += 1
#| if entry is Some({ key, .. }) {
#| break Some(key)
#| }
#| } nobreak {
#| None
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|pub fn[K, V] HashMap::values(self : HashMap[K, V]) -> Iter[V] {
#| let mut i = 0
#| Iter(
#| () => {
#| while i < self.entries.length() {
#| let entry = self.entries[i]
#| i += 1
#| if entry is Some({ value, .. }) {
#| break Some(value)
#| }
#| } nobreak {
#| None
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|#locals(f)
#|pub fn[K, V] HashMap::retain(self : HashMap[K, V], f : (K, V) -> Bool) -> Unit {
#| guard self.size > 0 else { return }
#| let mask = self.capacity_mask
#| let start = for i = 0 {
#| if self.entries[i] is None {
#| break i
#| }
#| continue i + 1
#| }
#| for k in 1..<=mask {
#| let i = (start + k) & mask
#| while self.entries[i] is Some(entry) {
#| if f(entry.key, entry.value) {
#| break
#| } else {
#| self.shift_back(i)
#| self.size -= 1
#| }
#| }
#| }
#|}
#|pub fn[K, V] HashMap::filter(
#| self : HashMap[K, V],
#| f : (K, V) -> Bool raise?,
#|) -> HashMap[K, V] raise? {
#| let kept : Array[Entry[K, V]] = []
#| for entry in self.entries {
#| if entry is Some({ key, value, hash, .. }) && f(key, value) {
#| kept.push({ psl: 0, key, value, hash, })
#| }
#| }
#| let other = new_hashmap(
#| if kept.is_empty() {
#| default_init_capacity
#| } else {
#| capacity_for_length(kept.length())
#| },
#| )
#| for entry in kept {
#| other.rehash_place_entry(entry)
#| }
#| other.size = kept.length()
#| other
#|}
#|test "retain" {
#| let hashmap : HashMap[String, Int32] = {
#| capacity: 8,
#| capacity_mask: 7,
#| size: 4,
#| entries: [
#| Some({ psl: 2, hash: 448974246, key: "c", value: 3, }),
#| Some({ psl: 2, hash: -136509641, key: "d", value: 4, }),
#| None,
#| None,
#| None,
#| None,
#| Some({ psl: 0, hash: 1614946358, key: "a", value: 1, }),
#| Some({ psl: 1, hash: -765931946, key: "b", value: 2, }),
#| ],
#| }
#| hashmap.retain((_k, v) => v % 2 == 0)
#| @debug.debug_inspect(
#| hashmap.entries,
#| content=(
#| #|
#| ),
#| )
#|}
),
},
)
///|
let moonbitlang_core_hashset_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/hashset",
deps={},
files={
"debug.mbt": (
#|pub impl[K : @debug.Debug] @debug.Debug for HashSet[K] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "HashSet",
#| @debug.Repr::array([ for x in self => Repr(x) ]),
#| )
#|}
#|test "Debug for HashSet" {
#| let hs : HashSet[Int32] = from_array([1])
#| @debug.debug_inspect(hs, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `HashSet([], capacity=...)` instead")
#|#as_free_fn(deprecated="Use `HashSet([], capacity=...)` instead")
#|pub fn[K] HashSet::new(capacity~ : Int32 = default_init_capacity) -> HashSet[K] {
#| new_hashset(capacity)
#|}
#|#deprecated("The bucket count is an implementation detail; the constructor's `capacity` hint is the only sizing control")
#|pub fn[K] HashSet::capacity(self : HashSet[K]) -> Int32 {
#| self.capacity
#|}
),
"extends.mbt": (
#|pub extend HashSet with BitAnd::{land}
#|pub extend HashSet with BitOr::{lor}
#|pub extend HashSet with BitXOr::{lxor}
#|pub extend HashSet with Eq::{equal}
#|pub extend HashSet with Sub::{sub}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with Default::{default}
#|#deprecated("Use `@debug.Debug` instead of `Show` for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with Eq::{not_equal}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with @json.FromJson::{from_json}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with @json.ToJson::{to_json}
),
"hashset.mbt": (
#|let default_init_capacity = 8
#|fn[K] new_hashset(capacity : Int32) -> HashSet[K] {
#| let capacity = capacity.next_power_of_two()
#| {
#| size: 0,
#| capacity,
#| capacity_mask: capacity - 1,
#| grow_at: calc_grow_threshold(capacity),
#| entries: FixedArray::make(capacity, None),
#| }
#|}
#|fn capacity_for_length(length : Int32) -> Int32 {
#| guard length <= calc_grow_threshold(0x40000000) else {
#| abort("HashSet capacity overflow")
#| }
#| let mut capacity = length.next_power_of_two()
#| if length > calc_grow_threshold(capacity) {
#| capacity *= 2
#| }
#| capacity
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[K : Hash + Eq] HashSet::HashSet(
#| arr : ArrayView[K],
#| capacity? : Int32,
#|) -> HashSet[K] {
#| let length = arr.length()
#| let capacity = match capacity {
#| Some(capacity) => {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| capacity_for_length(capacity.max(length))
#| }
#| None => default_init_capacity.max(capacity_for_length(length))
#| }
#| let m = new_hashset(capacity)
#| arr.each(e => m.insert(e))
#| m
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn[K : Hash + Eq] HashSet::insert(self : HashSet[K], key : K) -> Unit {
#| self.add_with_hash(key, Hash::hash(key))
#|}
#|fn[K : Eq] HashSet::add_with_hash(
#| self : HashSet[K],
#| key : K,
#| hash : Int32,
#|) -> Unit {
#| let (idx, psl) = for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| if self.size >= self.grow_at {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| break (idx, psl)
#| }
#| Some(curr_entry) => {
#| if curr_entry.hash == hash && curr_entry.key == key {
#| return
#| }
#| if psl > curr_entry.psl {
#| if self.size >= self.grow_at {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| self.push_away(idx, curr_entry)
#| break (idx, psl)
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#| let entry = { psl, key, hash, }
#| self.set_entry(entry, idx)
#| self.size += 1
#|}
#|#owned(entry)
#|fn[K] HashSet::push_away(
#| self : HashSet[K],
#| idx : Int32,
#| entry : Entry[K],
#|) -> Unit {
#| for psl = entry.psl + 1, idx = (idx + 1) & self.capacity_mask, entry = entry {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| break
#| }
#| Some(curr_entry) =>
#| if psl > curr_entry.psl {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| continue curr_entry.psl + 1,
#| (idx + 1) & self.capacity_mask,
#| curr_entry
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask, entry
#| }
#| }
#| }
#|}
#|#inline
#|#owned(entry)
#|fn[K] HashSet::set_entry(
#| self : HashSet[K],
#| entry : Entry[K],
#| new_idx : Int32,
#|) -> Unit {
#| self.entries.unsafe_set(new_idx, Some(entry))
#|}
#|pub fn[K : Hash + Eq] HashSet::contains(self : HashSet[K], key : K) -> Bool {
#| self.contains_with_hash(key, Hash::hash(key))
#|}
#|#inline
#|fn[K : Eq] HashSet::contains_with_hash(
#| self : HashSet[K],
#| key : K,
#| hash : Int32,
#|) -> Bool {
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break false }
#| if entry.hash == hash && entry.key == key {
#| break true
#| }
#| if i > entry.psl {
#| break false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|#inline
#|fn[K : Eq] HashSet::find_entry_with_hash(
#| self : HashSet[K],
#| key : K,
#| hash : Int32,
#|) -> Entry[K]? {
#| for distance = 0, index = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(index) is Some(entry) else { break None }
#| if entry.hash == hash && entry.key == key {
#| break Some(entry)
#| }
#| if distance > entry.psl {
#| break None
#| }
#| continue distance + 1, (index + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq] HashSet::remove(self : HashSet[K], key : K) -> Unit {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| self.shift_back(idx)
#| self.size -= 1
#| break
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|fn[K] HashSet::shift_back(self : HashSet[K], idx : Int32) -> Unit {
#| for cur = idx {
#| let next = (cur + 1) & self.capacity_mask
#| match self.entries.unsafe_get(next) {
#| None | Some({ psl: 0, .. }) => {
#| self.entries.unsafe_set(cur, None)
#| break
#| }
#| Some(entry) => {
#| entry.psl -= 1
#| self.set_entry(entry, cur)
#| continue next
#| }
#| }
#| }
#|}
#|fn[K] HashSet::grow(self : HashSet[K]) -> Unit {
#| if self.capacity == 0 {
#| self.capacity = default_init_capacity
#| self.capacity_mask = self.capacity - 1
#| self.grow_at = calc_grow_threshold(self.capacity)
#| self.size = 0
#| self.entries = FixedArray::make(self.capacity, None)
#| return
#| }
#| let old_entries = self.entries
#| let new_capacity = self.capacity * 2
#| self.entries = FixedArray::make(new_capacity, None)
#| self.capacity = new_capacity
#| self.capacity_mask = new_capacity - 1
#| self.grow_at = calc_grow_threshold(self.capacity)
#| for entry in old_entries {
#| if entry is Some(entry) {
#| self.rehash_place_entry(entry)
#| }
#| }
#|}
#|#owned(entry)
#|fn[K] HashSet::rehash_place_entry(self : HashSet[K], entry : Entry[K]) -> Unit {
#| let hash = entry.hash
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| return
#| }
#| Some(curr) =>
#| if psl > curr.psl {
#| self.push_away(idx, curr)
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| return
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[K : Show] Show for HashSet[K]
#|pub impl[K : Show] Show for HashSet[K] with fn output(self, logger) {
#| logger.write_iter(self.iter(), prefix="@hashset.from_array([", suffix="])")
#|}
#|#alias(size, deprecated)
#|pub fn[K] HashSet::length(self : HashSet[K]) -> Int32 {
#| self.size
#|}
#|pub fn[K] HashSet::is_empty(self : HashSet[K]) -> Bool {
#| self.size == 0
#|}
#|#locals(f)
#|pub fn[K] HashSet::each(
#| self : HashSet[K],
#| f : (K) -> Unit raise?,
#|) -> Unit raise? {
#| for entry in self.entries {
#| if entry is Some({ key, .. }) {
#| f(key)
#| }
#| }
#|}
#|#locals(f)
#|pub fn[K] HashSet::eachi(
#| self : HashSet[K],
#| f : (Int32, K) -> Unit raise?,
#|) -> Unit raise? {
#| for i in 0.. Unit {
#| self.entries.fill(None)
#| self.size = 0
#|}
#|#alias(iterator, deprecated)
#|pub fn[K] HashSet::iter(self : HashSet[K]) -> Iter[K] {
#| let mut i = 0
#| let len = self.entries.length()
#| Iter(
#| fn() {
#| while i < len {
#| let entry = self.entries.unsafe_get(i)
#| i += 1
#| if entry is Some({ key, .. }) {
#| return Some(key)
#| }
#| } nobreak {
#| None
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|pub fn[K] HashSet::to_array(self : HashSet[K]) -> Array[K] {
#| [
#| for entry in self.entries if entry is Some({ key, .. }) => key
#| ]
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Hash + Eq] HashSet::from_iter(iter : Iter[K]) -> HashSet[K] {
#| let s = new_hashset(default_init_capacity)
#| for e in iter {
#| s.insert(e)
#| }
#| s
#|}
#|pub fn[K : Eq] HashSet::union(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| let m = self.copy()
#| m.union_in_place(other)
#| m
#|}
#|pub fn[K : Eq] HashSet::union_in_place(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> Unit {
#| if physical_equal(self, other) {
#| return
#| }
#| for entry in other.entries {
#| if entry is Some({ key, hash, .. }) {
#| self.add_with_hash(key, hash)
#| }
#| }
#|}
#|pub fn[K : Eq] HashSet::intersection(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| let m = new_hashset(default_init_capacity)
#| if self.size == 0 || other.size == 0 {
#| return m
#| }
#| if self.capacity < other.capacity ||
#| (self.capacity == other.capacity && self.size <= other.size) {
#| for entry in self.entries {
#| if entry is Some({ key, hash, .. }) && other.contains_with_hash(key, hash) {
#| m.add_with_hash(key, hash)
#| }
#| }
#| } else {
#| for candidate in other.entries {
#| if candidate is Some({ key, hash, .. }) &&
#| self.find_entry_with_hash(key, hash) is Some(original) {
#| m.add_with_hash(original.key, original.hash)
#| }
#| }
#| }
#| m
#|}
#|pub fn[K : Eq] HashSet::difference(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| let m = new_hashset(default_init_capacity)
#| for entry in self.entries {
#| if entry is Some({ key, hash, .. }) && !other.contains_with_hash(key, hash) {
#| m.add_with_hash(key, hash)
#| }
#| }
#| m
#|}
#|pub fn[K : Eq] HashSet::symmetric_difference(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| let m = self.difference(other)
#| for entry in other.entries {
#| if entry is Some({ key, hash, .. }) && !self.contains_with_hash(key, hash) {
#| m.add_with_hash(key, hash)
#| }
#| }
#| m
#|}
#|pub fn[K : Eq] HashSet::is_disjoint(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> Bool {
#| let (small, large) = if self.length() <= other.length() {
#| (self, other)
#| } else {
#| (other, self)
#| }
#| for entry in small.entries {
#| if entry is Some({ key, hash, .. }) && large.contains_with_hash(key, hash) {
#| return false
#| }
#| }
#| true
#|}
#|pub fn[K : Eq] HashSet::is_subset(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> Bool {
#| guard self.length() <= other.length() else { return false }
#| for entry in self.entries {
#| if entry is Some({ key, hash, .. }) && !other.contains_with_hash(key, hash) {
#| return false
#| }
#| }
#| true
#|}
#|pub fn[K : Eq] HashSet::is_superset(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> Bool {
#| other.is_subset(self)
#|}
#|pub impl[K : Eq] BitAnd for HashSet[K] with fn land(self, other) {
#| self.intersection(other)
#|}
#|pub impl[K : Eq] BitOr for HashSet[K] with fn lor(self, other) {
#| self.union(other)
#|}
#|pub impl[K : Eq] BitXOr for HashSet[K] with fn lxor(self, other) {
#| self.symmetric_difference(other)
#|}
#|pub impl[K : Eq] Sub for HashSet[K] with fn sub(self, other) {
#| self.difference(other)
#|}
#|#locals(f)
#|pub fn[K] HashSet::retain(self : HashSet[K], f : (K) -> Bool) -> Unit {
#| guard self.size > 0 else { return }
#| let mask = self.capacity_mask
#| let start = for i = 0 {
#| if self.entries[i] is None {
#| break i
#| }
#| continue i + 1
#| }
#| for k in 1..<=mask {
#| let i = (start + k) & mask
#| while self.entries[i] is Some(entry) {
#| if f(entry.key) {
#| break
#| } else {
#| self.shift_back(i)
#| self.size -= 1
#| }
#| }
#| }
#|}
#|pub fn[K] HashSet::filter(
#| self : HashSet[K],
#| f : (K) -> Bool raise?,
#|) -> HashSet[K] raise? {
#| let kept : Array[Entry[K]] = []
#| for entry in self.entries {
#| if entry is Some({ key, hash, .. }) && f(key) {
#| kept.push({ psl: 0, key, hash, })
#| }
#| }
#| let other = new_hashset(
#| default_init_capacity.max(capacity_for_length(kept.length())),
#| )
#| for entry in kept {
#| other.rehash_place_entry(entry)
#| }
#| other.size = kept.length()
#| other
#|}
#|fn calc_grow_threshold(capacity : Int32) -> Int32 {
#| capacity / 16 * 13 + capacity % 16 * 13 / 16
#|}
#|#derive(Eq, @debug.Debug)
#|priv struct MyString(String)
#|impl Hash for MyString with fn hash(self) {
#| let MyString(self) = self
#| self.length()
#|}
#|impl Hash for MyString with fn hash_combine(self, hasher) {
#| let MyString(self) = self
#| hasher.combine_string(self)
#|}
#|test "set" {
#| let m : HashSet[MyString] = new_hashset(default_init_capacity)
#| m.insert("a")
#| m.insert("b")
#| m.insert("bc")
#| m.insert("abc")
#| m.insert("cd")
#| m.insert("c")
#| m.insert("d")
#| inspect(m.size, content="7")
#|}
#|test "remove" {
#| let m : HashSet[MyString] = new_hashset(default_init_capacity)
#| fn i(s) {
#| MyString::MyString(s)
#| }
#| m.insert("a" |> i)
#| m.insert("ab" |> i)
#| m.insert("bc" |> i)
#| m.insert("cd" |> i)
#| m.insert("abc" |> i)
#| m.insert("abcdef" |> i)
#| m.remove("ab" |> i)
#| inspect(m.length(), content="5")
#|}
#|test "remove_unexist_key" {
#| let m : HashSet[MyString] = new_hashset(default_init_capacity)
#| fn i(s) {
#| MyString::MyString(s)
#| }
#| m.insert("a" |> i)
#| m.insert("ab" |> i)
#| m.insert("abc" |> i)
#| m.remove("d" |> i)
#| inspect(m.length(), content="3")
#|}
#|test "grow" {
#| let m : HashSet[MyString] = new_hashset(default_init_capacity)
#| fn i(s) {
#| MyString::MyString(s)
#| }
#| m.insert("C" |> i)
#| m.insert("Go" |> i)
#| m.insert("C++" |> i)
#| m.insert("Java" |> i)
#| m.insert("Scala" |> i)
#| m.insert("Julia" |> i)
#| inspect(m.size, content="6")
#| inspect(m.capacity, content="8")
#| m.insert("Cobol" |> i)
#| inspect(m.size, content="7")
#| inspect(m.capacity, content="16")
#| m.insert("Python" |> i)
#| m.insert("Haskell" |> i)
#| m.insert("Rescript" |> i)
#| inspect(m.size, content="10")
#| inspect(m.capacity, content="16")
#|}
#|test "clear" {
#| let m : HashSet[MyString] = new_hashset(default_init_capacity)
#| m.clear()
#| inspect(m.length(), content="0")
#| inspect(m.capacity, content="8")
#| for entry in m.entries {
#| @test.assert_same_object(entry, None)
#| }
#|}
#|#alias(add_and_check, deprecated="Use `insert_and_check` instead")
#|pub fn[K : Hash + Eq] HashSet::insert_and_check(
#| self : HashSet[K],
#| key : K,
#|) -> Bool {
#| let old_size = self.length()
#| self.insert(key)
#| self.length() > old_size
#|}
#|pub fn[K : Hash + Eq] HashSet::remove_and_check(
#| self : HashSet[K],
#| key : K,
#|) -> Bool {
#| let old_size = self.size
#| self.remove(key)
#| self.size < old_size
#|}
#|#alias(clone, deprecated)
#|pub fn[K] HashSet::copy(self : HashSet[K]) -> HashSet[K] {
#| let other = {
#| capacity: self.capacity,
#| entries: FixedArray::make(self.capacity, None),
#| size: self.size,
#| capacity_mask: self.capacity_mask,
#| grow_at: self.grow_at,
#| }
#| for i in 0.. key
#| ]
#|}
#|pub impl[X : @json.FromJson + Hash + Eq] @json.FromJson for HashSet[X] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(arr) else {
#| raise path.error("@hashset.from_json: expected array")
#| }
#| let res : HashSet[X] = new_hashset(
#| default_init_capacity.max(capacity_for_length(arr.length())),
#| )
#| for i, x in arr {
#| res.insert(@json.FromJson::from_json(x, path.add_index(i)))
#| }
#| res
#|}
#|pub impl[K] Default for HashSet[K] with fn default() {
#| new_hashset(default_init_capacity)
#|}
),
"types.mbt": (
#|#derive(@debug.Debug)
#|priv struct Entry[K] {
#| mut psl : Int32
#| hash : Int32
#| key : K
#|}
#|struct HashSet[K] {
#| mut entries : FixedArray[Entry[K]?]
#| mut size : Int32 // active key count
#| mut capacity : Int32 // current capacity
#| mut capacity_mask : Int32 // capacity_mask = capacity - 1, used to find idx
#| mut grow_at : Int32 // threshold that triggers grow
#|}
#|pub impl[K : Eq] Eq for HashSet[K] with fn equal(self, other) {
#| guard self.size == other.size else { return false }
#| for entry in self.entries {
#| if entry is Some({ key, hash, .. }) && !other.contains_with_hash(key, hash) {
#| return false
#| }
#| }
#| true
#|}
),
},
)
///|
let moonbitlang_core_immut_hashmap_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/hashmap",
deps={},
files={
"HAMT.mbt": (
#|priv enum CurrNode[K, V] {
#| Tree(Node[K, V])
#| Bucket(@list.List[(K, V)])
#|}
#|priv struct BuildEntry[K, V] {
#| key : K
#| value : V
#| path : @path.Path // Full path; bulk construction tracks consumed segments by depth.
#|}
#|let bulk_build_threshold = 64
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[K, V] HashMap::empty() -> HashMap[K, V] {
#| { data: None, }
#|}
#|#as_free_fn
#|#owned(key, value)
#|pub fn[K : Hash, V] HashMap::singleton(key : K, value : V) -> HashMap[K, V] {
#| { data: Some(Flat(key, value, @path.of(key))), }
#|}
#|pub fn[K : Eq + Hash, V] HashMap::contains(
#| self : HashMap[K, V],
#| key : K,
#|) -> Bool {
#| self.get(key) is Some(_)
#|}
#|#alias(find, deprecated)
#|pub fn[K : Eq + Hash, V] HashMap::get(self : HashMap[K, V], key : K) -> V? {
#| match self.data {
#| None => None
#| Some(node) => node.get_with_path(key, @path.of(key))
#| }
#|}
#|#alias("_[_]")
#|pub fn[K : Eq + Hash, V] HashMap::at(self : HashMap[K, V], key : K) -> V {
#| guard! self.data is Some(node)
#| node.get_with_path(key, @path.of(key)).unwrap()
#|}
#|fn[K : Eq, V] Node::get_with_path(
#| self : Node[K, V],
#| key : K,
#| path : @path.Path,
#|) -> V? {
#| for node = self, path = path {
#| match (node, path) {
#| (Leaf(key1, value1, bucket), _) =>
#| break if key == key1 { Some(value1) } else { bucket.lookup(key) }
#| (Flat(key1, value1, path1), path) =>
#| break if path == path1 && key == key1 { Some(value1) } else { None }
#| (Branch(children), path) => {
#| let idx = path.idx()
#| if children.get(idx) is Some(child) {
#| continue child, path.next()
#| }
#| break None
#| }
#| }
#| }
#|}
#|#owned(key1, value1, key2, value2)
#|fn[K, V] join_2(
#| key1 : K,
#| value1 : V,
#| path1 : @path.Path,
#| key2 : K,
#| value2 : V,
#| path2 : @path.Path,
#|) -> Node[K, V] {
#| if path1 == @path.exhausted {
#| return Leaf(key2, value2, @list.singleton((key1, value1)))
#| }
#| let idx1 = path1.idx()
#| let idx2 = path2.idx()
#| if idx1 == idx2 {
#| let node = join_2(key1, value1, path1.next(), key2, value2, path2.next())
#| Branch(@sparse_array.singleton(idx1, node))
#| } else {
#| let node1 = Flat(key1, value1, path1.next())
#| let node2 = Flat(key2, value2, path2.next())
#| Branch(@sparse_array.doubleton(idx1, node1, idx2, node2))
#| }
#|}
#|#owned(value)
#|fn[K : Eq, V] Node::add_with_path(
#| self : Node[K, V],
#| key : K,
#| value : V,
#| path : @path.Path,
#|) -> Node[K, V] {
#| match self {
#| Leaf(key1, value1, bucket) =>
#| if key == key1 {
#| Leaf(key1, value, bucket)
#| } else if bucket.find_index(kv => kv.0 == key) is Some(index) {
#| Leaf(
#| key1,
#| value1,
#| bucket.mapi((i, kv) => if i == index { (kv.0, value) } else { kv }),
#| )
#| } else {
#| Leaf(key, value, bucket.prepend((key1, value1)))
#| }
#| Flat(key1, value1, path1) =>
#| if path == path1 && key == key1 {
#| Flat(key1, value, path1)
#| } else {
#| join_2(key1, value1, path1, key, value, path)
#| }
#| Branch(children) => {
#| let idx = path.idx()
#| match children.get(idx) {
#| Some(child) => {
#| let child = child.add_with_path(key, value, path.next())
#| Branch(children.replace(idx, child))
#| }
#| None => {
#| let child = Flat(key, value, path.next())
#| Branch(children.insert(idx, child))
#| }
#| }
#| }
#| }
#|}
#|fn[K : Eq, V] BuildEntry::add_to_node(
#| self : BuildEntry[K, V],
#| node : Node[K, V]?,
#| depth : Int32,
#|) -> Node[K, V] {
#| let path = self.path.advance(depth)
#| match node {
#| None => Flat(self.key, self.value, path)
#| Some(node) => node.add_with_path(self.key, self.value, path)
#| }
#|}
#|fn[K : Eq, V] build_hashmap_node_by_add(
#| entries : Array[BuildEntry[K, V]],
#| start : Int32,
#| end : Int32,
#| depth : Int32,
#|) -> Node[K, V] {
#| for i = start, node = (None : Node[K, V]?) {
#| if i == end {
#| break node.unwrap()
#| }
#| let node = entries[i].add_to_node(node, depth)
#| continue i + 1, Some(node)
#| }
#|}
#|fn[K : Eq, V] build_hashmap_node_range(
#| entries : Array[BuildEntry[K, V]],
#| start : Int32,
#| end : Int32,
#| depth : Int32,
#|) -> Node[K, V] {
#| if end - start == 1 {
#| let entry = entries[start]
#| return Flat(entry.key, entry.value, entry.path.advance(depth))
#| }
#| if depth == 5 {
#| return build_hashmap_node_by_add(entries, start, end, depth)
#| }
#| let starts = FixedArray::make(32, 0)
#| for i in start.. Flat(key, value, path.push(first_idx))
#| _ => Branch(@sparse_array.singleton(first_idx, first_child))
#| }
#| }
#| let indices = FixedArray::make(child_count, first_idx)
#| let children = FixedArray::make(child_count, first_child)
#| for idx = first_idx + 1, out = 1; idx < 32; {
#| if nexts[idx] == starts[idx] {
#| continue idx + 1, out
#| }
#| indices[out] = idx
#| children[out] = build_hashmap_node_range(
#| partitioned,
#| starts[idx],
#| nexts[idx],
#| depth + 1,
#| )
#| continue idx + 1, out + 1
#| }
#| Branch(@sparse_array.from_sorted_fixed_array(indices, children))
#|}
#|fn[K : Eq + Hash, V] hash_map_from_iter_by_add(
#| iter : Iter[(K, V)],
#|) -> HashMap[K, V] {
#| iter.fold(init=empty(), (m, e) => m.insert(e.0, e.1))
#|}
#|fn[K : Eq + Hash, V] hash_map_from_array_by_add(
#| arr : ArrayView[(K, V)],
#|) -> HashMap[K, V] {
#| for (k, v) in arr; map = empty() {
#| continue map.insert(k, v)
#| } nobreak {
#| map
#| }
#|}
#|fn[K : Eq + Hash, V] hash_map_from_array(
#| arr : ArrayView[(K, V)],
#|) -> HashMap[K, V] {
#| guard arr.length() > 0 else { return empty() }
#| if arr.length() <= bulk_build_threshold {
#| return hash_map_from_array_by_add(arr)
#| }
#| let entries = Array::makei(arr.length(), i => {
#| let kv = arr[i]
#| let (k, v) = kv
#| { key: k, value: v, path: @path.of(k), }
#| })
#| { data: Some(build_hashmap_node_range(entries, 0, entries.length(), 0)), }
#|}
#|#alias(filter_with_key, deprecated)
#|pub fn[K, V] HashMap::filter(
#| self : HashMap[K, V],
#| pred : (K, V) -> Bool raise?,
#|) -> HashMap[K, V] raise? {
#| fn go(node : Node[K, V]) raise? {
#| match node {
#| Leaf(key1, value1, bucket) => {
#| let new_bucket = bucket.filter(kv => pred(kv.0, kv.1))
#| if pred(key1, value1) {
#| if new_bucket.length() == bucket.length() {
#| return Some(node)
#| }
#| match new_bucket {
#| Empty => Some(Flat(key1, value1, @path.exhausted))
#| _ => Some(Leaf(key1, value1, new_bucket))
#| }
#| } else {
#| match new_bucket {
#| Empty => None
#| More((k1, v1), tail=Empty) => Some(Flat(k1, v1, @path.exhausted))
#| More((k1, v1), tail~) => Some(Leaf(k1, v1, tail))
#| }
#| }
#| }
#| Flat(key1, value1, _) =>
#| if pred(key1, value1) {
#| Some(node)
#| } else {
#| None
#| }
#| Branch(children) =>
#| match children.filter_map(go) {
#| None => None
#| Some(new_children) if physical_equal(new_children, children) =>
#| Some(node)
#| Some(new_children) => Some(collapse_branch(new_children))
#| }
#| }
#| }
#| match self.data {
#| None => self
#| Some(node) =>
#| match go(node) {
#| Some(new_node) if physical_equal(new_node, node) => self
#| new_data => { data: new_data, }
#| }
#| }
#|}
#|#alias(fold_with_key, deprecated)
#|pub fn[K, V, A] HashMap::fold(
#| self : HashMap[K, V],
#| init~ : A,
#| f : (A, K, V) -> A raise?,
#|) -> A raise? {
#| fn go(acc, node : Node[K, V]) raise? {
#| match node {
#| Leaf(k, v, bucket) =>
#| bucket.fold(init=f(acc, k, v), (acc, kv) => f(acc, kv.0, kv.1))
#| Flat(k, v, _) => f(acc, k, v)
#| Branch(children) => children.data.fold(init=acc, go)
#| }
#| }
#| match self.data {
#| None => init
#| Some(node) => go(init, node)
#| }
#|}
#|#alias(map_with_key, deprecated)
#|pub fn[K, V, A] HashMap::map(
#| self : HashMap[K, V],
#| f : (K, V) -> A raise?,
#|) -> HashMap[K, A] raise? {
#| fn go(m : Node[K, V]) -> Node[K, A] raise? {
#| match m {
#| Leaf(k, v, bucket) =>
#| Leaf(k, f(k, v), bucket.map(kv => (kv.0, f(kv.0, kv.1))))
#| Flat(k, v, path) => Flat(k, f(k, v), path)
#| Branch(children) => Branch(children.map(go))
#| }
#| }
#| {
#| data: match self.data {
#| None => None
#| Some(node) => Some(go(node))
#| },
#| }
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|#owned(value)
#|pub fn[K : Eq + Hash, V] HashMap::insert(
#| self : HashMap[K, V],
#| key : K,
#| value : V,
#|) -> HashMap[K, V] {
#| {
#| data: match self.data {
#| None => Some(Flat(key, value, @path.of(key)))
#| Some(node) => Some(node.add_with_path(key, value, @path.of(key)))
#| },
#| }
#|}
#|fn[K, V] collapse_branch(
#| children : @sparse_array.SparseArray[Node[K, V]],
#|) -> Node[K, V] {
#| match children.data {
#| [Flat(key, value, path)] =>
#| Flat(key, value, path.push(children.elem_info.first_idx()))
#| _ => Branch(children)
#| }
#|}
#|pub fn[K : Eq + Hash, V] HashMap::remove(
#| self : HashMap[K, V],
#| key : K,
#|) -> HashMap[K, V] {
#| match self.data {
#| None => self
#| Some(node) =>
#| match node.remove_with_path(key, @path.of(key)) {
#| Some(new_node) if physical_equal(new_node, node) => self
#| new_data => { data: new_data, }
#| }
#| }
#|}
#|pub fn[K : Eq + Hash, V] HashMap::update(
#| self : HashMap[K, V],
#| key : K,
#| f : (V?) -> V? raise?,
#|) -> HashMap[K, V] raise? {
#| match f(self.get(key)) {
#| Some(value) => self.insert(key, value)
#| None => self.remove(key)
#| }
#|}
#|fn[K : Eq, V] Node::remove_with_path(
#| self : Node[K, V],
#| key : K,
#| path : @path.Path,
#|) -> Node[K, V]? {
#| match self {
#| Leaf(key1, value1, bucket) =>
#| if key1 == key {
#| match bucket {
#| Empty => None
#| More((key2, value2), tail=Empty) => Some(Flat(key2, value2, path))
#| More((key2, value2), tail~) => Some(Leaf(key2, value2, tail))
#| }
#| } else if bucket.find_index(kv => kv.0 == key) is Some(index) {
#| match bucket.remove_at(index) {
#| Empty => Some(Flat(key1, value1, path))
#| new_bucket => Some(Leaf(key1, value1, new_bucket))
#| }
#| } else {
#| Some(self)
#| }
#| Flat(key1, _, path1) =>
#| if path == path1 && key == key1 {
#| None
#| } else {
#| Some(self)
#| }
#| Branch(children) => {
#| let idx = path.idx()
#| match children.get(idx) {
#| None => Some(self)
#| Some(child) => {
#| let new_child = child.remove_with_path(key, path.next())
#| if new_child is Some(nc) && physical_equal(nc, child) {
#| return Some(self)
#| }
#| let new_children = match (children.length(), new_child) {
#| (1, None) => return None
#| (_, None) => children.remove(idx)
#| (_, Some(new_child)) => children.replace(idx, new_child)
#| }
#| Some(collapse_branch(new_children))
#| }
#| }
#| }
#| }
#|}
#|pub fn[K, V] HashMap::is_empty(self : HashMap[K, V]) -> Bool {
#| self.data is None
#|}
#|#alias(size, deprecated)
#|pub fn[K, V] HashMap::length(self : HashMap[K, V]) -> Int32 {
#| fn node_size(node : Node[K, V]) {
#| match node {
#| Leaf(_, _, bucket) => 1 + bucket.length()
#| Flat(_) => 1
#| Branch(children) =>
#| for child in children.data; total_size = 0 {
#| continue total_size + node_size(child)
#| } nobreak {
#| total_size
#| }
#| }
#| }
#| match self.data {
#| None => 0
#| Some(node) => node_size(node)
#| }
#|}
#|#alias(union, deprecated="Use merge instead")
#|pub fn[K : Eq, V] HashMap::merge(
#| self : HashMap[K, V],
#| other : HashMap[K, V],
#|) -> HashMap[K, V] {
#| fn go(node1 : Node[_], node2) {
#| if physical_equal(node1, node2) {
#| return node2
#| }
#| match (node1, node2) {
#| (_, Flat(key2, value2, path2)) => node1.add_with_path(key2, value2, path2)
#| (Flat(key1, value1, path1), _) =>
#| match node2.get_with_path(key1, path1) {
#| Some(_) => node2
#| None => node2.add_with_path(key1, value1, path1)
#| }
#| (Branch(children1), Branch(children2)) =>
#| Branch(children1.union(children2, go))
#| (Leaf(key1, value1, bucket1), Leaf(key2, value2, bucket2)) => {
#| let kvs1 = bucket1.prepend((key1, value1))
#| let kvs2 = bucket2.prepend((key2, value2))
#| match kvs1.filter(kv => kvs2.lookup(kv.0) is None) {
#| Empty => node2
#| More(head, tail~) => Leaf(key2, value2, bucket2 + tail.prepend(head))
#| }
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| {
#| data: match (self.data, other.data) {
#| (None, x) | (x, None) => x
#| (Some(a), Some(b)) => Some(go(a, b))
#| },
#| }
#|}
#|#alias(union_with, deprecated="Use merge_with instead")
#|pub fn[K : Eq, V] HashMap::merge_with(
#| self : HashMap[K, V],
#| other : HashMap[K, V],
#| f : (K, V, V) -> V raise?,
#|) -> HashMap[K, V] raise? {
#| fn go(node1 : Node[_], node2 : Node[_]) raise? {
#| match (node1, node2) {
#| (_, Flat(key2, value2, path2)) => {
#| let new_value = match node1.get_with_path(key2, path2) {
#| Some(value1) => f(key2, value1, value2)
#| None => value2
#| }
#| node1.add_with_path(key2, new_value, path2)
#| }
#| (Flat(key1, value1, path1), _) => {
#| let new_value = match node2.get_with_path(key1, path1) {
#| Some(value2) => f(key1, value1, value2)
#| None => value1
#| }
#| node2.add_with_path(key1, new_value, path1)
#| }
#| (Branch(children1), Branch(children2)) =>
#| Branch(children1.union(children2, go))
#| (Leaf(key1, value1, bucket1), Leaf(key2, value2, bucket2)) => {
#| let kvs1 = bucket1.prepend((key1, value1))
#| let kvs2 = bucket2.prepend((key2, value2))
#| kvs1.union_with(kvs2, f)
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| {
#| data: match (self.data, other.data) {
#| (None, x) | (x, None) => x
#| (Some(a), Some(b)) => Some(go(a, b))
#| },
#| }
#|}
#|fn[K : Eq, V] @list.List::union_with(
#| self : Self[(K, V)],
#| other : Self[(K, V)],
#| f : (K, V, V) -> V raise?,
#|) -> Node[K, V] raise? {
#| let res = self.to_array()
#| for kv2 in other {
#| for i, kv1 in res {
#| if kv1.0 == kv2.0 {
#| res[i] = (kv1.0, f(kv1.0, kv1.1, kv2.1))
#| break
#| }
#| } nobreak {
#| res.push(kv2)
#| }
#| }
#| guard! @list.List(res) is More((k, v), tail~)
#| Leaf(k, v, tail)
#|}
#|pub fn[K : Eq, V] HashMap::intersection(
#| self : HashMap[K, V],
#| other : HashMap[K, V],
#|) -> HashMap[K, V] {
#| fn go(node1 : Node[_], node2) {
#| if physical_equal(node1, node2) {
#| return Some(node2)
#| }
#| match (node1, node2) {
#| (_, Flat(key2, _, path2)) =>
#| match node1.get_with_path(key2, path2) {
#| Some(_) => Some(node2)
#| None => None
#| }
#| (Flat(key1, _, path1), _) =>
#| match node2.get_with_path(key1, path1) {
#| Some(value2) => Some(Flat(key1, value2, path1))
#| None => None
#| }
#| (Branch(children1), Branch(children2)) =>
#| match children1.intersection(children2, go) {
#| None => None
#| Some(children) => Some(collapse_branch(children))
#| }
#| (Leaf(key1, value1, bucket1), Leaf(key2, value2, bucket2)) => {
#| let kvs1 = bucket1.prepend((key1, value1))
#| let kvs2 = bucket2.prepend((key2, value2))
#| match kvs2.filter(kv => kvs1.lookup(kv.0) is Some(_)) {
#| Empty => None
#| More(head, tail=Empty) => Some(Flat(head.0, head.1, @path.exhausted))
#| More(head, tail~) => Some(Leaf(head.0, head.1, tail))
#| }
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| {
#| data: match (self.data, other.data) {
#| (None, _) | (_, None) => None
#| (Some(a), Some(b)) => go(a, b)
#| },
#| }
#|}
#|pub fn[K : Eq, V] HashMap::intersection_with(
#| self : HashMap[K, V],
#| other : HashMap[K, V],
#| f : (K, V, V) -> V raise?,
#|) -> HashMap[K, V] raise? {
#| fn go(node1 : Node[_], node2 : Node[_]) raise? {
#| match (node1, node2) {
#| (_, Flat(key2, value2, path2)) =>
#| match node1.get_with_path(key2, path2) {
#| Some(value1) => Some(Flat(key2, f(key2, value1, value2), path2))
#| None => None
#| }
#| (Flat(key1, value1, path1), _) =>
#| match node2.get_with_path(key1, path1) {
#| Some(value2) => Some(Flat(key1, f(key1, value1, value2), path1))
#| None => None
#| }
#| (Branch(children1), Branch(children2)) =>
#| match children1.intersection(children2, go) {
#| None => None
#| Some(children) => Some(collapse_branch(children))
#| }
#| (Leaf(key1, value1, bucket1), Leaf(key2, value2, bucket2)) => {
#| let kvs1 = bucket1.prepend((key1, value1))
#| let kvs2 = bucket2.prepend((key2, value2))
#| kvs1.intersection_with(kvs2, f)
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| {
#| data: match (self.data, other.data) {
#| (None, _) | (_, None) => None
#| (Some(a), Some(b)) => go(a, b)
#| },
#| }
#|}
#|fn[K : Eq, V] @list.List::intersection_with(
#| self : Self[(K, V)],
#| other : Self[(K, V)],
#| f : (K, V, V) -> V raise?,
#|) -> Node[K, V]? raise? {
#| let res = []
#| for kv1 in self {
#| for kv2 in other {
#| if kv1.0 == kv2.0 {
#| res.push((kv1.0, f(kv1.0, kv1.1, kv2.1)))
#| break
#| }
#| }
#| }
#| match @list.List(res) {
#| Empty => None
#| More((k, v), tail=Empty) => Some(Flat(k, v, @path.exhausted))
#| More((k, v), tail~) => Some(Leaf(k, v, tail))
#| }
#|}
#|pub fn[K : Eq, V] HashMap::difference(
#| self : HashMap[K, V],
#| other : HashMap[K, V],
#|) -> HashMap[K, V] {
#| fn go(node1 : Node[_], node2) {
#| if physical_equal(node1, node2) {
#| return None
#| }
#| match (node1, node2) {
#| (node, Flat(k, _, path)) => node.remove_with_path(k, path)
#| (Flat(key, _, path), _) =>
#| match node2.get_with_path(key, path) {
#| Some(_) => None
#| None => Some(node1)
#| }
#| (Branch(children1), Branch(children2)) =>
#| match children1.difference(children2, go) {
#| None => None
#| Some(children) => Some(collapse_branch(children))
#| }
#| (Leaf(key1, value1, bucket1), Leaf(key2, value2, bucket2)) => {
#| let kvs1 = bucket1.prepend((key1, value1))
#| let kvs2 = bucket2.prepend((key2, value2))
#| match kvs1.filter(kv => !(kvs2.lookup(kv.0) is Some(_))) {
#| Empty => None
#| More(head, tail=Empty) => Some(Flat(head.0, head.1, @path.exhausted))
#| More(head, tail~) => Some(Leaf(head.0, head.1, tail))
#| }
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| match (self.data, other.data) {
#| (None, _) => { data: None, }
#| (_, None) => self
#| (Some(a), Some(b)) => { data: go(a, b), }
#| }
#|}
#|pub fn[K, V] HashMap::each(
#| self : HashMap[K, V],
#| f : (K, V) -> Unit raise?,
#|) -> Unit raise? {
#| fn go(node : Node[K, V]) raise? {
#| match node {
#| Leaf(k, v, bucket) => {
#| f(k, v)
#| bucket.each(kv => f(kv.0, kv.1))
#| }
#| Flat(k, v, _) => f(k, v)
#| Branch(children) => children.each(go)
#| }
#| }
#| if self.data is Some(node) {
#| go(node)
#| }
#|}
#|pub fn[K, V] HashMap::keys(self : HashMap[K, V]) -> Iter[K] {
#| self.iter().map(p => p.0)
#|}
#|#alias(elems, deprecated="Use `values` instead")
#|pub fn[K, V] HashMap::values(self : HashMap[K, V]) -> Iter[V] {
#| self.iter().map(p => p.1)
#|}
#|#alias(iterator, deprecated)
#|pub fn[K, V] HashMap::iter(self : HashMap[K, V]) -> Iter[(K, V)] {
#| let empty = Bucket(List([]))
#| let mut curr_node = match self.data {
#| Some(tree) => Tree(tree)
#| None => empty
#| }
#| let mut curr_index = 0
#| let parents = []
#| Iter(fn() {
#| for cn = curr_node {
#| match cn {
#| Tree(Flat(k, v, _)) => {
#| curr_node = empty
#| break Some((k, v))
#| }
#| Tree(Leaf(k, v, bucket)) => {
#| curr_node = Bucket(bucket)
#| break Some((k, v))
#| }
#| Bucket(More(pair, tail~)) => {
#| curr_node = Bucket(tail)
#| break Some(pair)
#| }
#| Tree(Branch(children)) as n if curr_index < children.length() => {
#| let child = children.data[curr_index]
#| parents.push((n, curr_index + 1))
#| curr_index = 0
#| continue Tree(child)
#| }
#| Bucket(Empty) | Tree(Branch(_)) if parents.pop()
#| is Some((parent, parent_index)) => {
#| curr_node = parent
#| curr_index = parent_index
#| continue parent
#| }
#| Bucket(Empty) | Tree(Branch(_)) => break None
#| }
#| }
#| })
#|}
#|#alias(iterator2, deprecated)
#|pub fn[K, V] HashMap::iter2(self : HashMap[K, V]) -> Iter2[K, V] {
#| self.iter()
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Eq + Hash, V] HashMap::from_iter(
#| iter : Iter[(K, V)],
#|) -> HashMap[K, V] {
#| if iter.size_hint() is Some(len) && len <= bulk_build_threshold {
#| return hash_map_from_iter_by_add(iter)
#| }
#| let entries = match iter.size_hint() {
#| Some(len) => Array(capacity=len)
#| None => []
#| }
#| iter.each(e => entries.push({ key: e.0, value: e.1, path: @path.of(e.0), }))
#| if entries.is_empty() {
#| empty()
#| } else {
#| { data: Some(build_hashmap_node_range(entries, 0, entries.length(), 0)), }
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[K : Show, V : Show] Show for HashMap[K, V]
#|#warnings("-deprecated")
#|pub impl[K : Show, V : Show] Show for HashMap[K, V] with fn output(self, logger) {
#| logger.write_iter(
#| self.iter(),
#| prefix="@immut/hashmap.from_array([",
#| suffix="])",
#| )
#|}
#|#as_free_fn(deprecated="Use @immut/hashmap.HashMap([...]) instead")
#|#alias(of, deprecated="Use @immut/hashmap.HashMap([...]) instead")
#|#as_free_fn(of, deprecated="Use @immut/hashmap.HashMap([...]) instead")
#|#deprecated("Use @immut/hashmap.HashMap([...]) instead")
#|pub fn[K : Eq + Hash, V] HashMap::from_array(
#| arr : ArrayView[(K, V)],
#|) -> HashMap[K, V] {
#| hash_map_from_array(arr)
#|}
#|pub fn[K : Eq + Hash, V] HashMap::HashMap(
#| arr : ArrayView[(K, V)],
#|) -> HashMap[K, V] {
#| hash_map_from_array(arr)
#|}
#|pub fn[K, V] HashMap::to_array(self : HashMap[K, V]) -> Array[(K, V)] {
#| [
#| for k, v in self => (k, v)
#| ]
#|}
#|impl[K : Eq, V : Eq] Eq for Node[K, V] with fn equal(self, other) {
#| if physical_equal(self, other) {
#| return true
#| }
#| match (self, other) {
#| (Flat(key1, value1, path1), Flat(key2, value2, path2)) =>
#| path1 == path2 && key1 == key2 && value1 == value2
#| (Branch(children1), Branch(children2)) => children1 == children2
#| (Leaf(key1, value1, bucket1), Leaf(key2, value2, bucket2)) => {
#| guard bucket1.length() == bucket2.length() else { return false }
#| let kvs1 = bucket1.prepend((key1, value1))
#| let kvs2 = bucket2.prepend((key2, value2))
#| kvs1.all(kv => kvs2.lookup(kv.0) is Some(v) && kv.1 == v)
#| }
#| _ => false
#| }
#|}
#|pub impl[K : Hash, V : Hash] Hash for HashMap[K, V] with fn hash_combine(
#| self,
#| hasher,
#|) {
#| hasher.combine(
#| self.fold(init=0, (acc, k, v) => {
#| let h = Hasher()
#| h.combine((k, v))
#| acc ^ h.finalize()
#| }),
#| )
#|}
),
"debug.mbt": (
#|pub impl[K : @debug.Debug, V : @debug.Debug] @debug.Debug for HashMap[K, V] with fn to_repr(
#| self,
#|) {
#| @debug.Repr::opaque_(
#| "HashMap",
#| @debug.Repr::map([ for k, v in self => (Repr(k), Repr(v)) ]),
#| )
#|}
#|test "Debug for HashMap" {
#| let hm : HashMap[Int32, String] = HashMap([(2, "b")])
#| @debug.debug_inspect(
#| hm,
#| content=(
#| #|
#| ),
#| )
#|}
),
"deprecated.mbt": "",
"extends.mbt": (
#|pub extend HashMap with Eq::{equal}
#|pub extend HashMap with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashMap with Show::{to_string}
),
"types.mbt": (
#|#unsafe_cycle_free
#|priv enum Node[K, V] {
#| Flat(K, V, @path.Path)
#| Leaf(K, V, @list.List[(K, V)])
#| Branch(@sparse_array.SparseArray[Node[K, V]])
#|}
#|#derive(Eq)
#|#valtype
#|struct HashMap[K, V] {
#| data : Node[K, V]?
#|}
),
},
)
///|
let moonbitlang_core_immut_hashset_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/hashset",
deps={},
files={
"HAMT.mbt": (
#|priv struct BuildEntry[A] {
#| value : A
#| path : @path.Path // Full path; bulk construction tracks consumed segments by depth.
#|}
#|let bulk_build_threshold = 64
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[A] HashSet::empty() -> HashSet[A] {
#| { data: None, }
#|}
#|pub fn[A : Eq + Hash] HashSet::contains(self : HashSet[A], key : A) -> Bool {
#| self.data is Some(node) && node.contains(key, @path.of(key))
#|}
#|fn[A : Eq] Node::contains(self : Node[A], key : A, path : @path.Path) -> Bool {
#| for s = self, p = path {
#| match (s, p) {
#| (Leaf(key1, bucket), _) => break key == key1 || bucket.contains(key)
#| (Flat(key1, path1), path) => break path == path1 && key == key1
#| (Branch(children), path) => {
#| let idx = path.idx()
#| if children.get(idx) is Some(child) {
#| continue child, path.next()
#| }
#| break false
#| }
#| }
#| }
#|}
#|#owned(key1, key2)
#|fn[A] join_2(
#| key1 : A,
#| path1 : @path.Path,
#| key2 : A,
#| path2 : @path.Path,
#|) -> Node[A] {
#| if path1 == @path.exhausted {
#| return Leaf(key2, @list.singleton(key1))
#| }
#| let idx1 = path1.idx()
#| let idx2 = path2.idx()
#| if idx1 == idx2 {
#| let node = join_2(key1, path1.next(), key2, path2.next())
#| Branch(@sparse_array.singleton(idx1, node))
#| } else {
#| let node1 = Flat(key1, path1.next())
#| let node2 = Flat(key2, path2.next())
#| Branch(@sparse_array.doubleton(idx1, node1, idx2, node2))
#| }
#|}
#|fn[A : Eq] Node::add_with_path(
#| self : Node[A],
#| key : A,
#| path : @path.Path,
#|) -> Node[A] {
#| match self {
#| Leaf(key1, bucket) =>
#| if key == key1 || bucket.contains(key) {
#| self
#| } else {
#| Leaf(key, bucket.prepend(key1))
#| }
#| Flat(key1, path1) =>
#| if path == path1 && key == key1 {
#| self
#| } else {
#| join_2(key1, path1, key, path)
#| }
#| Branch(children) => {
#| let idx = path.idx()
#| match children.get(idx) {
#| Some(child) => {
#| let new_child = child.add_with_path(key, path.next())
#| if physical_equal(new_child, child) {
#| self
#| } else {
#| Branch(children.replace(idx, new_child))
#| }
#| }
#| None => {
#| let child = Flat(key, path.next())
#| Branch(children.insert(idx, child))
#| }
#| }
#| }
#| }
#|}
#|fn[A : Eq] BuildEntry::add_to_node(
#| self : BuildEntry[A],
#| node : Node[A]?,
#| depth : Int32,
#|) -> Node[A] {
#| let path = self.path.advance(depth)
#| match node {
#| None => Flat(self.value, path)
#| Some(node) => node.add_with_path(self.value, path)
#| }
#|}
#|fn[A : Eq] build_hashset_node_by_add(
#| entries : Array[BuildEntry[A]],
#| start : Int32,
#| end : Int32,
#| depth : Int32,
#|) -> Node[A] {
#| for i = start, node = (None : Node[A]?) {
#| if i == end {
#| break node.unwrap()
#| }
#| let node = entries[i].add_to_node(node, depth)
#| continue i + 1, Some(node)
#| }
#|}
#|fn[A : Eq] build_hashset_node_range(
#| entries : Array[BuildEntry[A]],
#| start : Int32,
#| end : Int32,
#| depth : Int32,
#|) -> Node[A] {
#| if end - start == 1 {
#| let entry = entries[start]
#| return Flat(entry.value, entry.path.advance(depth))
#| }
#| if depth == 5 {
#| return build_hashset_node_by_add(entries, start, end, depth)
#| }
#| let starts = FixedArray::make(32, 0)
#| for i in start.. Flat(value, path.push(first_idx))
#| _ => Branch(@sparse_array.singleton(first_idx, first_child))
#| }
#| }
#| let indices = FixedArray::make(child_count, first_idx)
#| let children = FixedArray::make(child_count, first_child)
#| for idx = first_idx + 1, out = 1; idx < 32; {
#| if nexts[idx] == starts[idx] {
#| continue idx + 1, out
#| }
#| indices[out] = idx
#| children[out] = build_hashset_node_range(
#| partitioned,
#| starts[idx],
#| nexts[idx],
#| depth + 1,
#| )
#| continue idx + 1, out + 1
#| }
#| Branch(@sparse_array.from_sorted_fixed_array(indices, children))
#|}
#|fn[A : Eq + Hash] hash_set_from_iter_by_add(iter : Iter[A]) -> HashSet[A] {
#| iter.fold(init=empty(), (s, e) => s.insert(e))
#|}
#|fn[A : Eq + Hash] hash_set_from_array_by_add(arr : ArrayView[A]) -> HashSet[A] {
#| for value in arr; set = empty() {
#| continue set.insert(value)
#| } nobreak {
#| set
#| }
#|}
#|fn[A : Eq + Hash] hash_set_from_array(arr : ArrayView[A]) -> HashSet[A] {
#| guard arr.length() > 0 else { return empty() }
#| if arr.length() <= bulk_build_threshold {
#| return hash_set_from_array_by_add(arr)
#| }
#| let entries = Array::makei(arr.length(), i => {
#| let value = arr[i]
#| { value, path: @path.of(value), }
#| })
#| { data: Some(build_hashset_node_range(entries, 0, entries.length(), 0)), }
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn[A : Eq + Hash] HashSet::insert(self : HashSet[A], key : A) -> HashSet[A] {
#| match self.data {
#| None => { data: Some(Flat(key, @path.of(key))), }
#| Some(node) => {
#| let new_node = node.add_with_path(key, @path.of(key))
#| if physical_equal(new_node, node) {
#| self
#| } else {
#| { data: Some(new_node), }
#| }
#| }
#| }
#|}
#|pub fn[A : Eq + Hash] HashSet::remove(self : HashSet[A], key : A) -> HashSet[A] {
#| match self.data {
#| None => self
#| Some(node) =>
#| match node.remove_with_path(key, @path.of(key)) {
#| Some(new_node) if physical_equal(new_node, node) => self
#| new_data => { data: new_data, }
#| }
#| }
#|}
#|fn[A] collapse_branch(children : @sparse_array.SparseArray[Node[A]]) -> Node[A] {
#| match children.data {
#| [Flat(key, path)] => Flat(key, path.push(children.elem_info.first_idx()))
#| _ => Branch(children)
#| }
#|}
#|fn[A : Eq] Node::remove_with_path(
#| self : Node[A],
#| key : A,
#| path : @path.Path,
#|) -> Node[A]? {
#| match self {
#| Leaf(key1, bucket) =>
#| if key1 == key {
#| match bucket {
#| Empty => None
#| More(key2, tail=Empty) => Some(Flat(key2, path))
#| More(key2, tail=xs) => Some(Leaf(key2, xs))
#| }
#| } else if bucket.find_index(x => key.equal(x)) is Some(index) {
#| match bucket.remove_at(index) {
#| Empty => Some(Flat(key1, path))
#| new_bucket => Some(Leaf(key1, new_bucket))
#| }
#| } else {
#| Some(self)
#| }
#| Flat(key1, path1) =>
#| if path == path1 && key == key1 {
#| None
#| } else {
#| Some(self)
#| }
#| Branch(children) => {
#| let idx = path.idx()
#| match children.get(idx) {
#| None => Some(self)
#| Some(child) => {
#| let new_child = child.remove_with_path(key, path.next())
#| if new_child is Some(nc) && physical_equal(nc, child) {
#| return Some(self)
#| }
#| let new_children = match (children.length(), new_child) {
#| (1, None) => return None
#| (_, None) => children.remove(idx)
#| (_, Some(new_child)) => children.replace(idx, new_child)
#| }
#| Some(collapse_branch(new_children))
#| }
#| }
#| }
#| }
#|}
#|#alias(size, deprecated)
#|pub fn[A] HashSet::length(self : HashSet[A]) -> Int32 {
#| fn node_size(node : Node[A]) {
#| match node {
#| Leaf(_, bucket) => 1 + bucket.length()
#| Flat(_) => 1
#| Branch(children) =>
#| for child in children.data; total_size = 0 {
#| continue total_size + node_size(child)
#| } nobreak {
#| total_size
#| }
#| }
#| }
#| match self.data {
#| None => 0
#| Some(node) => node_size(node)
#| }
#|}
#|pub fn[K : Eq] HashSet::union(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| fn go(node1 : Node[K], node2 : Node[K]) {
#| if physical_equal(node1, node2) {
#| return node1
#| }
#| match (node1, node2) {
#| (node, Flat(key, path)) | (Flat(key, path), node) =>
#| node.add_with_path(key, path)
#| (Branch(children1), Branch(children2)) =>
#| Branch(children1.union(children2, go))
#| (Leaf(key1, bucket1), Leaf(key2, bucket2)) => {
#| let keys1 = bucket1.prepend(key1)
#| let keys2 = bucket2.prepend(key2)
#| match keys1.filter(k => !keys2.contains(k)) {
#| Empty => node2
#| More(head, tail~) => Leaf(key2, bucket2 + tail.prepend(head))
#| }
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| {
#| data: match (self.data, other.data) {
#| (None, x) | (x, None) => x
#| (Some(a), Some(b)) => Some(go(a, b))
#| },
#| }
#|}
#|pub fn[K : Eq] HashSet::intersection(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| fn go(node1 : Node[K], node2 : Node[K]) {
#| if physical_equal(node1, node2) {
#| return Some(node1)
#| }
#| match (node1, node2) {
#| (node, Flat(key, path) as flat) | (Flat(key, path) as flat, node) =>
#| if node.contains(key, path) {
#| Some(flat)
#| } else {
#| None
#| }
#| (Branch(children1), Branch(children2)) =>
#| match children1.intersection(children2, go) {
#| None => None
#| Some(children) => Some(collapse_branch(children))
#| }
#| (Leaf(key1, bucket1), Leaf(key2, bucket2)) => {
#| let keys1 = bucket1.prepend(key1)
#| let keys2 = bucket2.prepend(key2)
#| match keys1.filter(k => keys2.contains(k)) {
#| Empty => None
#| More(head, tail=Empty) => Some(Flat(head, @path.exhausted))
#| More(head, tail~) => Some(Leaf(head, tail))
#| }
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| {
#| data: match (self.data, other.data) {
#| (None, _) | (_, None) => None
#| (Some(a), Some(b)) => go(a, b)
#| },
#| }
#|}
#|pub fn[K : Eq] HashSet::difference(
#| self : HashSet[K],
#| other : HashSet[K],
#|) -> HashSet[K] {
#| fn go(node1 : Node[_], node2) {
#| if physical_equal(node1, node2) {
#| return None
#| }
#| match (node1, node2) {
#| (node, Flat(k, path)) => node.remove_with_path(k, path)
#| (Flat(key, path) as flat, node) =>
#| if node.contains(key, path) {
#| None
#| } else {
#| Some(flat)
#| }
#| (Branch(children1), Branch(children2)) =>
#| match children1.difference(children2, go) {
#| None => None
#| Some(children) => Some(collapse_branch(children))
#| }
#| (Leaf(key1, bucket1), Leaf(key2, bucket2)) => {
#| let keys1 = bucket1.prepend(key1)
#| let keys2 = bucket2.prepend(key2)
#| match keys1.filter(k => !keys2.contains(k)) {
#| Empty => None
#| More(head, tail=Empty) => Some(Flat(head, @path.exhausted))
#| More(head, tail~) => Some(Leaf(head, tail))
#| }
#| }
#| _ => abort("Unreachable")
#| }
#| }
#| match (self.data, other.data) {
#| (None, _) => { data: None, }
#| (_, None) => self
#| (Some(a), Some(b)) => { data: go(a, b), }
#| }
#|}
#|pub fn[A] HashSet::is_empty(self : HashSet[A]) -> Bool {
#| self.data is None
#|}
#|pub fn[A] HashSet::each(
#| self : HashSet[A],
#| f : (A) -> Unit raise?,
#|) -> Unit raise? {
#| fn go(node : Node[A]) raise? {
#| match node {
#| Leaf(k, bucket) => {
#| f(k)
#| bucket.each(f)
#| }
#| Flat(k, _) => f(k)
#| Branch(children) => children.each(go)
#| }
#| }
#| if self.data is Some(node) {
#| go(node)
#| }
#|}
#|priv enum CurrNode[A] {
#| Tree(Node[A])
#| Bucket(@list.List[A])
#|}
#|#alias(iterator, deprecated)
#|pub fn[A] HashSet::iter(self : HashSet[A]) -> Iter[A] {
#| let empty = Bucket(List([]))
#| let mut curr_node = match self.data {
#| Some(tree) => Tree(tree)
#| None => empty
#| }
#| let mut curr_index = 0
#| let parents = []
#| Iter(fn() {
#| for cn = curr_node {
#| match cn {
#| Tree(Flat(x, _)) => {
#| curr_node = empty
#| break Some(x)
#| }
#| Tree(Leaf(x, bucket)) => {
#| curr_node = Bucket(bucket)
#| break Some(x)
#| }
#| Bucket(More(x, tail~)) => {
#| curr_node = Bucket(tail)
#| break Some(x)
#| }
#| Tree(Branch(children)) as n if curr_index < children.length() => {
#| let child = children.data[curr_index]
#| parents.push((n, curr_index + 1))
#| curr_index = 0
#| continue Tree(child)
#| }
#| Bucket(Empty) | Tree(Branch(_)) if parents.pop()
#| is Some((parent, parent_index)) => {
#| curr_node = parent
#| curr_index = parent_index
#| continue parent
#| }
#| Bucket(Empty) | Tree(Branch(_)) => break None
#| }
#| }
#| })
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A : Eq + Hash] HashSet::from_iter(iter : Iter[A]) -> HashSet[A] {
#| if iter.size_hint() is Some(len) && len <= bulk_build_threshold {
#| return hash_set_from_iter_by_add(iter)
#| }
#| let entries = match iter.size_hint() {
#| Some(len) => Array(capacity=len)
#| None => []
#| }
#| iter.each(value => entries.push({ value, path: @path.of(value), }))
#| if entries.is_empty() {
#| empty()
#| } else {
#| { data: Some(build_hashset_node_range(entries, 0, entries.length(), 0)), }
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show] Show for HashSet[A]
#|pub impl[A : Show] Show for HashSet[A] with fn output(self, logger) {
#| logger.write_iter(
#| self.iter(),
#| prefix="@immut/hashset.from_array([",
#| suffix="])",
#| )
#|}
#|#as_free_fn(deprecated="Use @immut/hashset.HashSet([...]) instead")
#|#alias(of, deprecated="Use @immut/hashset.HashSet([...]) instead")
#|#as_free_fn(of, deprecated="Use @immut/hashset.HashSet([...]) instead")
#|#deprecated("Use @immut/hashset.HashSet([...]) instead")
#|pub fn[A : Eq + Hash] HashSet::from_array(arr : ArrayView[A]) -> HashSet[A] {
#| hash_set_from_array(arr)
#|}
#|pub fn[A : Eq + Hash] HashSet::HashSet(arr : ArrayView[A]) -> HashSet[A] {
#| hash_set_from_array(arr)
#|}
#|pub impl[A : Hash] Hash for HashSet[A] with fn hash_combine(self, hasher) {
#| hasher.combine(self.iter().fold(init=0, (x, y) => x ^ y.hash()))
#|}
#|impl[A : Eq] Eq for Node[A] with fn equal(self, other) {
#| if physical_equal(self, other) {
#| return true
#| }
#| match (self, other) {
#| (Leaf(x, xs), Leaf(y, ys)) =>
#| xs.length() == ys.length() &&
#| ({
#| let keys1 = xs.prepend(x)
#| let keys2 = ys.prepend(y)
#| keys1.iter().all(k => keys2.contains(k))
#| })
#| (Flat(x, pathx), Flat(y, pathy)) => pathx == pathy && x == y
#| (Branch(xs), Branch(ys)) => xs == ys
#| _ => false
#| }
#|}
#|test "hash" {
#| @test.assert_eq(
#| Hash::hash(HashSet([1, 2, 3, 4])),
#| Hash::hash(HashSet([3, 2]).insert(1).insert(4)),
#| )
#| @test.assert_not_eq(
#| Hash::hash(HashSet([1, 2, 3])),
#| Hash::hash(HashSet([1, 2, 4])),
#| )
#|}
#|test "eq" {
#| @test.assert_eq(HashSet([1, 2, 3, 4]), HashSet([3, 2]).insert(1).insert(4))
#| @test.assert_not_eq(HashSet([1, 2, 3]), HashSet([1, 2, 4]))
#|}
),
"debug.mbt": (
#|pub impl[K : @debug.Debug] @debug.Debug for HashSet[K] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "HashSet",
#| @debug.Repr::array([ for x in self => Repr(x) ]),
#| )
#|}
#|test "Debug for HashSet" {
#| let hs : HashSet[Int32] = HashSet([3])
#| @debug.debug_inspect(hs, content="")
#|}
),
"deprecated.mbt": "",
"extends.mbt": (
#|pub extend HashSet with Eq::{equal}
#|pub extend HashSet with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend HashSet with Show::{to_string}
),
"types.mbt": (
#|#unsafe_cycle_free
#|priv enum Node[A] {
#| Flat(A, @path.Path)
#| Leaf(A, @list.List[A])
#| Branch(@sparse_array.SparseArray[Node[A]])
#|}
#|#derive(Eq)
#|#valtype
#|struct HashSet[A] {
#| data : Node[A]?
#|}
),
},
)
///|
let moonbitlang_core_immut_internal_path_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/internal/path",
deps={},
files={
"extends.mbt": (
#|pub extend Path with Eq::{equal}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Path with Eq::{not_equal}
),
"path.mbt": (
#|#derive(Eq)
#|pub struct Path(UInt32)
#|const SEGMENT_LENGTH : Int32 = 5
#|const INDEX_MASK : UInt32 = (1 << SEGMENT_LENGTH) - 1
#|const SEGMENT_NUM : Int32 = 32 / SEGMENT_LENGTH
#|const HEAD_TAG : UInt32 = 0xffffffffU << (SEGMENT_LENGTH * SEGMENT_NUM)
#|pub fn[A : Hash] of(key : A) -> Path {
#| key.hash().reinterpret_as_uint() | HEAD_TAG
#|}
#|const MAX_TAIL : UInt32 = 0xffffffffU >> (SEGMENT_LENGTH * (SEGMENT_NUM - 1))
#|pub fn Path::is_last(self : Path) -> Bool {
#| let Path(self) = self
#| self <= MAX_TAIL
#|}
#|pub fn Path::push(self : Path, idx : Int32) -> Path {
#| let Path(self) = self
#| (self << SEGMENT_LENGTH) | idx.reinterpret_as_uint()
#|}
#|pub fn Path::idx(self : Path) -> Int32 {
#| let Path(self) = self
#| (self & INDEX_MASK).reinterpret_as_int()
#|}
#|pub fn Path::idx_at(self : Path, depth : Int32) -> Int32 {
#| let Path(self) = self
#| ((self >> (SEGMENT_LENGTH * depth)) & INDEX_MASK).reinterpret_as_int()
#|}
#|pub fn Path::next(self : Path) -> Path {
#| let Path(self) = self
#| self >> SEGMENT_LENGTH
#|}
#|pub fn Path::advance(self : Path, depth : Int32) -> Path {
#| let Path(self) = self
#| self >> (SEGMENT_LENGTH * depth)
#|}
#|pub let exhausted : Path = Path(HEAD_TAG >> (SEGMENT_LENGTH * SEGMENT_NUM))
),
},
)
///|
let moonbitlang_core_immut_internal_sparse_array_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/internal/sparse_array",
deps={},
files={
"bitset.mbt": (
#|#derive(Eq)
#|pub struct Bitset(UInt32)
#|let empty_bitset : Bitset = Bitset(0)
#|fn Bitset::has(self : Bitset, idx : Int32) -> Bool {
#| (self.0 & (1U << idx)) != 0
#|}
#|fn Bitset::index_of(self : Bitset, idx : Int32) -> Int32 {
#| (self.0 & ((1U << idx) - 1)).popcnt()
#|}
#|pub fn Bitset::first_idx(self : Bitset) -> Int32 {
#| self.0.ctz()
#|}
#|fn Bitset::union(self : Bitset, other : Bitset) -> Bitset {
#| Bitset(self.0 | other.0)
#|}
#|fn Bitset::intersection(self : Bitset, other : Bitset) -> Bitset {
#| Bitset(self.0 & other.0)
#|}
#|fn Bitset::insert(self : Bitset, idx : Int32) -> Bitset {
#| Bitset(self.0 | (1U << idx))
#|}
#|fn Bitset::remove(self : Bitset, idx : Int32) -> Bitset {
#| Bitset(self.0 ^ (1U << idx))
#|}
#|#alias(size, deprecated)
#|fn Bitset::length(self : Bitset) -> Int32 {
#| let Bitset(self) = self
#| self.popcnt()
#|}
#|test "Bitset::has" {
#| let b = empty_bitset.insert(2)
#| inspect(b.has(0), content="false")
#| inspect(b.has(2), content="true")
#| let b = b.insert(0)
#| inspect(b.has(0), content="true")
#| inspect(b.has(2), content="true")
#|}
#|test "Bitset::index_of" {
#| let b = empty_bitset.insert(2)
#| inspect(b.index_of(2), content="0")
#| let b = b.insert(0)
#| inspect(b.index_of(2), content="1")
#| let b = b.insert(5)
#| inspect(b.index_of(2), content="1")
#| inspect(b.index_of(3), content="2")
#| inspect(b.index_of(4), content="2")
#| inspect(b.index_of(5), content="2")
#| inspect(b.index_of(6), content="3")
#|}
#|test "Bitset::union" {
#| let b1 = empty_bitset.insert(2).insert(3)
#| let b2 = empty_bitset.insert(0).insert(1)
#| let b3 = b1.union(b2)
#| inspect(b3.has(0), content="true")
#| inspect(b3.has(1), content="true")
#| inspect(b3.has(2), content="true")
#| inspect(b3.has(3), content="true")
#|}
#|test "Bitset::intersection" {
#| let b1 = empty_bitset.insert(2).insert(3)
#| let b2 = empty_bitset.insert(0).insert(1).insert(2)
#| let b3 = b1.intersection(b2)
#| inspect(b3.has(0), content="false")
#| inspect(b3.has(1), content="false")
#| inspect(b3.has(2), content="true")
#| inspect(b3.has(3), content="false")
#|}
#|test "Bitset::remove" {
#| let b = empty_bitset.insert(2).insert(3)
#| inspect(b.has(2), content="true")
#| inspect(b.has(3), content="true")
#| inspect(b.index_of(2), content="0")
#| inspect(b.index_of(3), content="1")
#| let b = b.remove(2)
#| inspect(b.has(2), content="false")
#| inspect(b.has(3), content="true")
#| inspect(b.index_of(3), content="0")
#|}
#|test "Bitset::size" {
#| let b = empty_bitset
#| inspect(b.length(), content="0")
#| let b = b.insert(0)
#| inspect(b.length(), content="1")
#| let b = b.insert(1)
#| inspect(b.length(), content="2")
#| let b = b.insert(1)
#| inspect(b.length(), content="2")
#|}
#|test "Bitset::ctpop" {
#| @debug.debug_inspect(
#| ([0, 0xf0f0f0f0, 0x3c3c0ff0] : Array[_]).map(x => x.popcnt()),
#| content="[0, 16, 16]",
#| )
#|}
),
"extends.mbt": (
#|pub extend Bitset with Eq::{equal}
#|pub extend SparseArray with Eq::{equal}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Bitset with Eq::{not_equal}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SparseArray with Eq::{not_equal}
),
"sparse_array.mbt": (
#|#derive(Eq)
#|pub struct SparseArray[X] {
#| elem_info : Bitset
#| data : FixedArray[X]
#|}
#|pub fn[X] empty() -> SparseArray[X] {
#| { elem_info: empty_bitset, data: [], }
#|}
#|#owned(value)
#|pub fn[X] singleton(idx : Int32, value : X) -> SparseArray[X] {
#| { elem_info: empty_bitset.insert(idx), data: [value], }
#|}
#|pub fn[X] SparseArray::get(self : SparseArray[X], idx : Int32) -> X? {
#| if self.elem_info.has(idx) {
#| Some(self.data[self.elem_info.index_of(idx)])
#| } else {
#| None
#| }
#|}
#|fn[X] SparseArray::unsafe_get(self : SparseArray[X], idx : Int32) -> X {
#| self.data[self.elem_info.index_of(idx)]
#|}
#|#owned(value1, value2)
#|pub fn[X] doubleton(
#| idx1 : Int32,
#| value1 : X,
#| idx2 : Int32,
#| value2 : X,
#|) -> SparseArray[X] {
#| {
#| elem_info: empty_bitset.insert(idx1).insert(idx2),
#| data: if idx1 < idx2 {
#| [value1, value2]
#| } else {
#| [value2, value1]
#| },
#| }
#|}
#|#owned(data)
#|pub fn[X] from_sorted_fixed_array(
#| indices : FixedArray[Int32],
#| data : FixedArray[X],
#|) -> SparseArray[X] {
#| let mut elem_info = empty_bitset
#| for idx in indices {
#| elem_info = elem_info.insert(idx)
#| }
#| { elem_info, data, }
#|}
#|#owned(value)
#|pub fn[X] SparseArray::insert(
#| self : SparseArray[X],
#| idx : Int32,
#| value : X,
#|) -> SparseArray[X] {
#| let old_data = self.data
#| let old_len = old_data.length()
#| let new_len = old_len + 1
#| let pos_of_new_item = self.elem_info.index_of(idx)
#| let new_data = FixedArray::make_and_blit(
#| old_data,
#| allocate_len=new_len,
#| init=value,
#| len=pos_of_new_item,
#| )
#| new_data[pos_of_new_item] = value
#| FixedArray::unsafe_blit(
#| new_data,
#| pos_of_new_item + 1,
#| old_data,
#| pos_of_new_item,
#| old_len - pos_of_new_item,
#| )
#| { elem_info: self.elem_info.insert(idx), data: new_data, }
#|}
#|pub fn[X] SparseArray::remove(
#| self : SparseArray[X],
#| idx : Int32,
#|) -> SparseArray[X] {
#| let old_data = self.data
#| let old_len = old_data.length()
#| let pos_of_removed_item = self.elem_info.index_of(idx)
#| let new_data = FixedArray::make_and_blit(
#| old_data,
#| allocate_len=old_len - 1,
#| init=old_data.unsafe_get(0),
#| len=pos_of_removed_item,
#| )
#| FixedArray::unsafe_blit(
#| new_data,
#| pos_of_removed_item,
#| old_data,
#| pos_of_removed_item + 1,
#| old_len - pos_of_removed_item - 1,
#| )
#| { elem_info: self.elem_info.remove(idx), data: new_data, }
#|}
#|pub fn[X] SparseArray::union(
#| self : SparseArray[X],
#| other : SparseArray[X],
#| f : (X, X) -> X raise?,
#|) -> SparseArray[X] raise? {
#| let union_elem_info = self.elem_info.union(other.elem_info)
#| let data = FixedArray::make(union_elem_info.length(), self.data[0])
#| for rest = union_elem_info, index = 0; rest != empty_bitset; {
#| let idx = rest.first_idx()
#| data[index] = match self.get(idx) {
#| None => other.unsafe_get(idx)
#| Some(value1) =>
#| match other.get(idx) {
#| None => value1
#| Some(value2) => f(value1, value2)
#| }
#| }
#| continue rest.remove(idx), index + 1
#| }
#| { elem_info: union_elem_info, data, }
#|}
#|fn[A] FixedArray::copy_prefix(
#| self : FixedArray[A],
#| len~ : Int32,
#|) -> FixedArray[A] {
#| FixedArray::make_and_blit(self, allocate_len=len, init=self[0], len~)
#|}
#|pub fn[X] SparseArray::intersection(
#| self : SparseArray[X],
#| other : SparseArray[X],
#| f : (X, X) -> X? raise?,
#|) -> SparseArray[X]? raise? {
#| let inter_elem_info = self.elem_info.intersection(other.elem_info)
#| guard inter_elem_info != 0 else { return None }
#| let data = FixedArray::make(inter_elem_info.length(), self.data[0])
#| for rest = inter_elem_info, index = 0, elem_info = inter_elem_info; rest != 0; {
#| let idx = rest.first_idx()
#| match f(self.unsafe_get(idx), other.unsafe_get(idx)) {
#| Some(value) => {
#| data[index] = value
#| continue rest.remove(idx), index + 1, elem_info
#| }
#| None => continue rest.remove(idx), index, elem_info.remove(idx)
#| }
#| } nobreak {
#| if elem_info == empty_bitset {
#| None
#| } else if elem_info == inter_elem_info {
#| Some({ elem_info, data, })
#| } else {
#| Some({ elem_info, data: data.copy_prefix(len=index), })
#| }
#| }
#|}
#|pub fn[X] SparseArray::difference(
#| self : SparseArray[X],
#| other : SparseArray[X],
#| f : (X, X) -> X?,
#|) -> SparseArray[X]? {
#| let self_elem_info = self.elem_info
#| let data = FixedArray::make(self_elem_info.length(), self.data[0])
#| for rest = self_elem_info, index = 0, elem_info = self_elem_info; rest !=
#| empty_bitset; {
#| let idx = rest.first_idx()
#| match other.get(idx) {
#| None => {
#| data[index] = self.unsafe_get(idx)
#| continue rest.remove(idx), index + 1, elem_info
#| }
#| Some(value2) =>
#| match f(self.unsafe_get(idx), value2) {
#| None => continue rest.remove(idx), index, elem_info.remove(idx)
#| Some(v) => {
#| data[index] = v
#| continue rest.remove(idx), index + 1, elem_info
#| }
#| }
#| }
#| } nobreak {
#| if elem_info == empty_bitset {
#| None
#| } else if elem_info == self_elem_info {
#| Some({ elem_info, data, })
#| } else {
#| Some({ elem_info, data: data.copy_prefix(len=index), })
#| }
#| }
#|}
#|pub fn[X, Y] SparseArray::map(
#| self : SparseArray[X],
#| f : (X) -> Y raise?,
#|) -> SparseArray[Y] raise? {
#| { elem_info: self.elem_info, data: self.data.map(f), }
#|}
#|pub fn[X] SparseArray::filter_map(
#| self : SparseArray[X],
#| pred : (X) -> X? raise?,
#|) -> SparseArray[X]? raise? {
#| let old_data = self.data
#| let len = old_data.length()
#| for rest = self.elem_info, i = 0; rest != empty_bitset; {
#| let idx = rest.first_idx()
#| let old = old_data.unsafe_get(i)
#| let first = pred(old)
#| if first is Some(v) && physical_equal(v, old) {
#| continue rest.remove(idx), i + 1
#| }
#| let data = FixedArray::make_and_blit(
#| old_data,
#| allocate_len=len,
#| init=old,
#| len=i,
#| )
#| let (elem_info, index) = match first {
#| None => (self.elem_info.remove(idx), i)
#| Some(v) => {
#| data[i] = v
#| (self.elem_info, i + 1)
#| }
#| }
#| for rest = rest.remove(idx), j = i + 1, elem_info = elem_info, index = index; rest !=
#| empty_bitset; {
#| let idx = rest.first_idx()
#| match pred(old_data.unsafe_get(j)) {
#| None => continue rest.remove(idx), j + 1, elem_info.remove(idx), index
#| Some(v) => {
#| data[index] = v
#| continue rest.remove(idx), j + 1, elem_info, index + 1
#| }
#| }
#| } nobreak {
#| return if elem_info == empty_bitset {
#| None
#| } else if index == len {
#| Some({ elem_info, data, })
#| } else {
#| Some({ elem_info, data: data.copy_prefix(len=index), })
#| }
#| }
#| } nobreak {
#| Some(self)
#| }
#|}
#|#owned(value)
#|pub fn[X] SparseArray::replace(
#| self : SparseArray[X],
#| idx : Int32,
#| value : X,
#|) -> SparseArray[X] {
#| let new_data = self.data.copy()
#| new_data[self.elem_info.index_of(idx)] = value
#| { elem_info: self.elem_info, data: new_data, }
#|}
#|#alias(size, deprecated)
#|pub fn[X] SparseArray::length(self : SparseArray[X]) -> Int32 {
#| self.data.length()
#|}
#|pub fn[X] SparseArray::each(
#| self : SparseArray[X],
#| f : (X) -> Unit raise?,
#|) -> Unit raise? {
#| for i in 0.. Repr(x))
#| @debug.Repr::opaque_("PriorityQueue", @debug.Repr::array(xs))
#|}
#|test "Debug for PriorityQueue" {
#| let pq : PriorityQueue[Int32] = from_array([3, 1, 2])
#| @debug.debug_inspect(pq, content="")
#|}
),
"extends.mbt": (
#|pub extend PriorityQueue with Compare::{compare}
#|pub extend PriorityQueue with Eq::{equal}
#|pub extend PriorityQueue with Hash::{hash}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with Default::{default}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with @json.ToJson::{to_json}
),
"priority_queue.mbt": (
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[A] PriorityQueue::empty() -> PriorityQueue[A] {
#| { node: Empty, size: 0, }
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[A : Compare] PriorityQueue::PriorityQueue(
#| array : ArrayView[A],
#|) -> PriorityQueue[A] {
#| guard array.length() > 0 else { return PriorityQueue::empty() }
#| from_array_in_place(array.to_owned().mut_view())
#|}
#|fn[A : Compare] from_array_in_place(heap : MutArrayView[A]) -> PriorityQueue[A] {
#| let len = heap.length()
#| guard len > 0 else { return PriorityQueue::empty() }
#| heapify(heap)
#| { node: priority_queue_node_from_heap(heap, 0, len), size: len, }
#|}
#|fn[A : Compare] heapify(heap : MutArrayView[A]) -> Unit {
#| let len = heap.length()
#| for i in (len / 2)>..0 {
#| sift_down(heap, i, len)
#| }
#|}
#|fn[A : Compare] sift_down(
#| heap : MutArrayView[A],
#| start : Int32,
#| len : Int32,
#|) -> Unit {
#| for root = start {
#| let left = root * 2 + 1
#| guard left < len else { break }
#| let right = left + 1
#| let child = if right < len && heap.unsafe_get(right) > heap.unsafe_get(left) {
#| right
#| } else {
#| left
#| }
#| guard heap.unsafe_get(root) < heap.unsafe_get(child) else { break }
#| let root_value = heap.unsafe_get(root)
#| heap.unsafe_set(root, heap.unsafe_get(child))
#| heap.unsafe_set(child, root_value)
#| continue child
#| }
#|}
#|fn[A] priority_queue_node_from_heap(
#| heap : MutArrayView[A],
#| index : Int32,
#| len : Int32,
#|) -> Node[A] {
#| if index >= len {
#| Empty
#| } else {
#| let value = heap.unsafe_get(index)
#| let left_index = index * 2 + 1
#| if left_index >= len {
#| Leaf(value)
#| } else {
#| Branch(
#| value,
#| left=priority_queue_node_from_heap(heap, left_index, len),
#| right=priority_queue_node_from_heap(heap, left_index + 1, len),
#| )
#| }
#| }
#|}
#|pub fn[A : Compare] PriorityQueue::to_array(
#| self : PriorityQueue[A],
#|) -> Array[A] {
#| let arr : Array[A] = []
#| let stack : Array[Node[A]] = [self.node]
#| while stack.pop() is Some(node) {
#| match node {
#| Empty => ()
#| Leaf(a) => arr.push(a)
#| Branch(a, left=l, right=r) => {
#| arr.push(a)
#| stack.push(l)
#| stack.push(r)
#| }
#| }
#| }
#| arr.sort()
#| arr.rev_in_place()
#| arr
#|}
#|#alias(iterator, deprecated)
#|pub fn[A : Compare] PriorityQueue::iter(self : PriorityQueue[A]) -> Iter[A] {
#| self.to_array().iter()
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A : Compare] PriorityQueue::from_iter(
#| iter : Iter[A],
#|) -> PriorityQueue[A] {
#| from_array_in_place(iter.to_array().mut_view())
#|}
#|priv struct Path(Int32)
#|fn path(size : Int32) -> Path {
#| for x = size, y = 0 {
#| match (x, y) {
#| (1, y) => break Path(y)
#| (x, y) => continue x >> 1, (y << 1) | (x & 1)
#| }
#| }
#|}
#|fn Path::is_left(self : Path) -> Bool {
#| (self.0 & 1) == 0
#|}
#|fn Path::next(self : Path) -> Path {
#| Path(self.0 >> 1)
#|}
#|pub fn[A : Compare] PriorityQueue::pop(
#| self : PriorityQueue[A],
#|) -> PriorityQueue[A]? {
#| match self.node {
#| Empty => None
#| Leaf(_) => Some(PriorityQueue::empty())
#| Branch(_) => {
#| let (value, temp) = self.node.remove_last_leaf(path(self.size))
#| Some({ node: temp.change_and_down(value), size: self.size - 1, })
#| }
#| }
#|}
#|fn[A] Node::remove_last_leaf(self : Node[A], path : Path) -> (A, Node[A]) {
#| match self {
#| Empty => abort("Priority queue is empty!")
#| Leaf(a) => (a, Empty)
#| Branch(a, left=Leaf(l_top), right=Empty) => (l_top, Leaf(a))
#| Branch(a, left=l, right=r) =>
#| if path.is_left() {
#| let (e, ld) = l.remove_last_leaf(path.next())
#| (e, Branch(a, left=ld, right=r))
#| } else {
#| let (e, rd) = r.remove_last_leaf(path.next())
#| (e, Branch(a, left=l, right=rd))
#| }
#| }
#|}
#|#owned(value)
#|fn[A : Compare] Node::change_and_down(self : Node[A], value : A) -> Node[A] {
#| match self {
#| Empty => abort("unreachable")
#| Leaf(_) => Leaf(value)
#| Branch(_, left=l, right=r) =>
#| match (l, r) {
#| (Leaf(l_top), Empty) =>
#| if value >= l_top {
#| Branch(value, left=l, right=Empty)
#| } else {
#| Branch(l_top, left=Leaf(value), right=Empty)
#| }
#| (Branch(l_top, ..) | Leaf(l_top), Branch(r_top, ..) | Leaf(r_top)) =>
#| if value >= l_top && value >= r_top {
#| Branch(value, left=l, right=r)
#| } else if l_top >= r_top {
#| Branch(l_top, left=l.change_and_down(value), right=r)
#| } else {
#| Branch(r_top, left=l, right=r.change_and_down(value))
#| }
#| _ => abort("unreachable")
#| }
#| }
#|}
#|#internal(unsafe, "Panics if the queue is empty.")
#|#doc(hidden)
#|#alias(pop_exn, deprecated)
#|pub fn[A : Compare] PriorityQueue::unsafe_pop(
#| self : PriorityQueue[A],
#|) -> PriorityQueue[A] {
#| match self.node {
#| Empty => abort("Priority queue is empty!")
#| Leaf(_) => PriorityQueue::empty()
#| Branch(_) => {
#| let (value, temp) = self.node.remove_last_leaf(path(self.size))
#| { node: temp.change_and_down(value), size: self.size - 1, }
#| }
#| }
#|}
#|#owned(value)
#|pub fn[A : Compare] PriorityQueue::push(
#| self : PriorityQueue[A],
#| value : A,
#|) -> PriorityQueue[A] {
#| match self.node {
#| Empty => { node: Leaf(value), size: 1, }
#| Leaf(_) | Branch(_) => {
#| let size = self.size + 1
#| { node: self.node.push(value, path(size)), size, }
#| }
#| }
#|}
#|#owned(value)
#|fn[A : Compare] Node::push(self : Node[A], value : A, path : Path) -> Node[A] {
#| match self {
#| Empty => Leaf(value)
#| Leaf(a) => {
#| let (high, low) = if a > value { (a, value) } else { (value, a) }
#| Branch(high, left=Leaf(low), right=Empty)
#| }
#| Branch(a, left=l, right=r) => {
#| let (high, low) = if a > value { (a, value) } else { (value, a) }
#| if path.is_left() {
#| Branch(high, left=l.push(low, path.next()), right=r)
#| } else {
#| Branch(high, left=l, right=r.push(low, path.next()))
#| }
#| }
#| }
#|}
#|pub fn[A] PriorityQueue::peek(self : PriorityQueue[A]) -> A? {
#| match self.node {
#| Empty => None
#| Leaf(a) | Branch(a, ..) => Some(a)
#| }
#|}
#|pub fn[A] PriorityQueue::is_empty(self : PriorityQueue[A]) -> Bool {
#| self.node is Empty
#|}
#|pub impl[A] Default for PriorityQueue[A] with fn default() {
#| PriorityQueue::empty()
#|}
#|pub fn[A] PriorityQueue::length(self : PriorityQueue[A]) -> Int32 {
#| self.size
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show + Compare] Show for PriorityQueue[A]
#|pub impl[A : Show + Compare] Show for PriorityQueue[A] with fn output(
#| self,
#| logger,
#|) {
#| logger.write_iter(
#| self.iter(),
#| prefix="@immut/priority_queue.from_array([",
#| suffix="])",
#| )
#|}
#|pub impl[A : Compare] Eq for PriorityQueue[A] with fn equal(self, other) {
#| physical_equal(self, other) ||
#| (self.length() == other.length() && self.to_array() == other.to_array())
#|}
#|pub impl[A : Hash + Compare] Hash for PriorityQueue[A] with fn hash_combine(
#| self,
#| hasher,
#|) {
#| for e in self {
#| hasher.combine(e)
#| }
#|}
#|pub impl[A : Compare] Compare for PriorityQueue[A] with fn compare(self, other) {
#| let len_cmp = self.length().compare(other.length())
#| if len_cmp != 0 {
#| return len_cmp
#| }
#| let self_arr = self.to_array()
#| let other_arr = other.to_array()
#| for i, x in self_arr {
#| let cmp = x.compare(other_arr[i])
#| if cmp != 0 {
#| return cmp
#| }
#| } nobreak {
#| return 0
#| }
#|}
),
"types.mbt": (
#|struct PriorityQueue[A] {
#| node : Node[A]
#| size : Int32
#|}
#|priv enum Node[A] {
#| Empty
#| Leaf(A)
#| Branch(A, left~ : Node[A], right~ : Node[A])
#|}
#|pub impl[A : @json.ToJson + Compare] @json.ToJson for PriorityQueue[A] with fn to_json(
#| self : PriorityQueue[A],
#|) {
#| [
#| for item in self => item
#| ]
#|}
),
},
)
///|
let moonbitlang_core_immut_sorted_map_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/sorted_map",
deps={},
files={
"debug.mbt": (
#|pub impl[K : @debug.Debug, V : @debug.Debug] @debug.Debug for SortedMap[K, V] with fn to_repr(
#| self,
#|) {
#| @debug.Repr::opaque_(
#| "SortedMap",
#| @debug.Repr::map(self.to_array().map(kv => (Repr(kv.0), Repr(kv.1)))),
#| )
#|}
#|test "Debug for SortedMap" {
#| let sm : SortedMap[Int32, String] = SortedMap([(1, "a"), (2, "b")])
#| @debug.debug_inspect(
#| sm,
#| content=(
#| #|
#| ),
#| )
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `values` instead")
#|#coverage.skip
#|pub fn[K, V] SortedMap::elems(self : SortedMap[K, V]) -> Array[V] {
#| self.values().collect()
#|}
#|#as_free_fn(deprecated="Use `@json.from_json` instead")
#|#deprecated("Use `@json.from_json` instead")
#|pub fn[V : @json.FromJson] SortedMap::from_json(
#| json : @json.Json,
#|) -> SortedMap[String, V] raise @json.JsonDecodeError {
#| @json.from_json(json)
#|}
),
"extends.mbt": (
#|pub extend SortedMap with Compare::{compare}
#|pub extend SortedMap with Eq::{equal}
#|pub extend SortedMap with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with @json.ToJson::{to_json}
),
"map.mbt": (
#|#alias(add, deprecated="Use `insert` instead")
#|#owned(value)
#|pub fn[K : Compare, V] SortedMap::insert(
#| self : SortedMap[K, V],
#| key : K,
#| value : V,
#|) -> SortedMap[K, V] {
#| match self {
#| Empty => singleton(key, value)
#| Tree(k, value=v, l, r, ..) => {
#| let c = key.compare(k)
#| if c == 0 {
#| make_tree(k, value, l, r)
#| } else if c < 0 {
#| balance(k, v, l.insert(key, value), r)
#| } else {
#| balance(k, v, l, r.insert(key, value))
#| }
#| }
#| }
#|}
#|fn[K, V] SortedMap::split_max(
#| self : SortedMap[K, V],
#|) -> (K, V, SortedMap[K, V]) {
#| match self {
#| Tree(k, value=v, l, Empty, ..) => (k, v, l)
#| Tree(k, value=v, l, r, ..) => {
#| let (k1, v1, r) = r.split_max()
#| (k1, v1, balance(k, v, l, r))
#| }
#| Empty => abort("Map::split_max error")
#| }
#|}
#|fn[K, V] SortedMap::split_min(
#| self : SortedMap[K, V],
#|) -> (K, V, SortedMap[K, V]) {
#| match self {
#| Tree(k, value=v, Empty, r, ..) => (k, v, r)
#| Tree(k, value=v, l, r, ..) => {
#| let (k1, v1, l) = l.split_min()
#| (k1, v1, balance(k, v, l, r))
#| }
#| Empty => abort("Map::split_min error")
#| }
#|}
#|fn[K, V] glue(l : SortedMap[K, V], r : SortedMap[K, V]) -> SortedMap[K, V] {
#| match (l, r) {
#| (Empty, r) => r
#| (l, Empty) => l
#| (l, r) =>
#| if l.length() > r.length() {
#| let (k, v, l) = l.split_max()
#| balance(k, v, l, r)
#| } else {
#| let (k, v, r) = r.split_min()
#| balance(k, v, l, r)
#| }
#| }
#|}
#|fn[K, V] concat(l : SortedMap[K, V], r : SortedMap[K, V]) -> SortedMap[K, V] {
#| match (l, r) {
#| (Empty, r) => r
#| (l, Empty) => l
#| (l, r) => {
#| let (k, v, r) = r.split_min()
#| join(l, k, v, r)
#| }
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::remove(
#| self : SortedMap[K, V],
#| key : K,
#|) -> SortedMap[K, V] {
#| match self {
#| Empty => self
#| Tree(k, value=v, l, r, ..) => {
#| let c = key.compare(k)
#| if c == 0 {
#| glue(l, r)
#| } else if c < 0 {
#| let nl = l.remove(key)
#| if physical_equal(nl, l) {
#| self
#| } else {
#| balance(k, v, nl, r)
#| }
#| } else {
#| let nr = r.remove(key)
#| if physical_equal(nr, r) {
#| self
#| } else {
#| balance(k, v, l, nr)
#| }
#| }
#| }
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::update(
#| self : SortedMap[K, V],
#| key : K,
#| f : (V?) -> V? raise?,
#|) -> SortedMap[K, V] raise? {
#| match f(self.get(key)) {
#| Some(value) => self.insert(key, value)
#| None => self.remove(key)
#| }
#|}
#|#alias(filter_with_key, deprecated)
#|pub fn[K, V] SortedMap::filter(
#| self : SortedMap[K, V],
#| pred : (K, V) -> Bool raise?,
#|) -> SortedMap[K, V] raise? {
#| match self {
#| Empty => Empty
#| Tree(k, value=v, l, r, ..) => {
#| let keep = pred(k, v)
#| let kept_l = l.filter(pred)
#| let kept_r = r.filter(pred)
#| if keep {
#| if kept_l.length() + kept_r.length() + 1 == self.length() {
#| self
#| } else {
#| join(kept_l, k, v, kept_r)
#| }
#| } else {
#| match (kept_l, kept_r) {
#| (Empty, right) => right
#| (left, Empty) => left
#| (left, right) => concat(left, right)
#| }
#| }
#| }
#| }
#|}
#|pub fn[K, V] SortedMap::to_array(self : SortedMap[K, V]) -> Array[(K, V)] {
#| [
#| for k, v in self => (k, v)
#| ]
#|}
#|pub fn[K : Compare, V] SortedMap::merge(
#| self : SortedMap[K, V],
#| other : SortedMap[K, V],
#|) -> SortedMap[K, V] {
#| match (self, other) {
#| (Empty, _) => other
#| (_, Empty) => self
#| (_, Tree(k2, value=v2, l2, r2, ..)) => {
#| let (l1, _, r1) = self.split(k2)
#| let merged_l = l1.merge(l2)
#| let merged_r = r1.merge(r2)
#| join(merged_l, k2, v2, merged_r)
#| }
#| }
#|}
#|fn[K : Compare, V] SortedMap::split(
#| self : SortedMap[K, V],
#| key : K,
#|) -> (SortedMap[K, V], V?, SortedMap[K, V]) {
#| match self {
#| Empty => (Empty, None, Empty)
#| Tree(k, value=v, l, r, ..) => {
#| let c = key.compare(k)
#| if c == 0 {
#| (l, Some(v), r)
#| } else if c < 0 {
#| let (ll, found, lr) = l.split(key)
#| (ll, found, join(lr, k, v, r))
#| } else {
#| let (rl, found, rr) = r.split(key)
#| (join(l, k, v, rl), found, rr)
#| }
#| }
#| }
#|}
#|fn[K, V] join(
#| l : SortedMap[K, V],
#| k : K,
#| v : V,
#| r : SortedMap[K, V],
#|) -> SortedMap[K, V] {
#| match (l, r) {
#| (Empty, _) =>
#| if r.length() < 2 {
#| make_tree(k, v, l, r)
#| } else {
#| r.add_min_entry(k, v)
#| }
#| (_, Empty) =>
#| if l.length() < 2 {
#| make_tree(k, v, l, r)
#| } else {
#| l.add_max_entry(k, v)
#| }
#| (Tree(lk, value=lv, ll, lr, size=ls), Tree(rk, value=rv, rl, rr, size=rs)) =>
#| if ls * DELTA < rs {
#| balance(rk, rv, join(l, k, v, rl), rr)
#| } else if rs * DELTA < ls {
#| balance(lk, lv, ll, join(lr, k, v, r))
#| } else {
#| make_tree(k, v, l, r)
#| }
#| }
#|}
#|fn[K, V] SortedMap::add_min_entry(
#| self : SortedMap[K, V],
#| k : K,
#| v : V,
#|) -> SortedMap[K, V] {
#| match self {
#| Empty => singleton(k, v)
#| Tree(k1, value=v1, l, r, ..) => balance(k1, v1, l.add_min_entry(k, v), r)
#| }
#|}
#|fn[K, V] SortedMap::add_max_entry(
#| self : SortedMap[K, V],
#| k : K,
#| v : V,
#|) -> SortedMap[K, V] {
#| match self {
#| Empty => singleton(k, v)
#| Tree(k1, value=v1, l, r, ..) => balance(k1, v1, l, r.add_max_entry(k, v))
#| }
#|}
#|const DELTA = 3
#|const GAMMA = 2
#|fn[K, V] balance(
#| key : K,
#| value : V,
#| l : SortedMap[K, V],
#| r : SortedMap[K, V],
#|) -> SortedMap[K, V] {
#| fn single_l(k1, v1, x, r : SortedMap[K, V]) {
#| guard! r is Tree(k2, value=v2, y, z, ..)
#| make_tree(k2, v2, make_tree(k1, v1, x, y), z)
#| }
#| fn single_r(k2, v2, l : SortedMap[K, V], z) {
#| guard! l is Tree(k1, value=v1, x, y, ..)
#| make_tree(k1, v1, x, make_tree(k2, v2, y, z))
#| }
#| fn double_l(k1, v1, x, r : SortedMap[K, V]) {
#| guard! r is Tree(k3, value=v3, Tree(k2, value=v2, y1, y2, ..), z, ..)
#| make_tree(k2, v2, make_tree(k1, v1, x, y1), make_tree(k3, v3, y2, z))
#| }
#| fn double_r(k3, v3, l : SortedMap[K, V], z) {
#| guard! l is Tree(k1, value=v1, x, Tree(k2, value=v2, y1, y2, ..), ..)
#| make_tree(k2, v2, make_tree(k1, v1, x, y1), make_tree(k3, v3, y2, z))
#| }
#| let ln = l.length()
#| let rn = r.length()
#| if ln + rn < 2 {
#| make_tree(key, value, l, r)
#| } else if rn > DELTA * ln {
#| guard! r is Tree(_, rl, rr, ..)
#| if rl.length() < GAMMA * rr.length() {
#| single_l(key, value, l, r)
#| } else {
#| double_l(key, value, l, r)
#| }
#| } else if ln > DELTA * rn {
#| guard! l is Tree(_, ll, lr, ..)
#| if lr.length() < GAMMA * ll.length() {
#| single_r(key, value, l, r)
#| } else {
#| double_r(key, value, l, r)
#| }
#| } else {
#| make_tree(key, value, l, r)
#| }
#|}
#|test "insert" {
#| let m = SortedMap([(3, "three"), (8, "eight"), (1, "one")])
#| inspect(m.debug_tree(), content="(3,three,(1,one,_,_),(8,eight,_,_))")
#| let m = m
#| .insert(5, "five")
#| .insert(2, "two")
#| .insert(0, "zero")
#| .insert(1, "one_updated")
#| inspect(
#| m.debug_tree(),
#| content="(3,three,(1,one_updated,(0,zero,_,_),(2,two,_,_)),(8,eight,(5,five,_,_),_))",
#| )
#|}
#|test "remove" {
#| let m1 = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| inspect(
#| m1.debug_tree(),
#| content="(3,three,(1,one,(0,zero,_,_),(2,two,_,_)),(8,eight,_,_))",
#| )
#| let m2 = m1.remove(1).remove(3)
#| inspect(m2.debug_tree(), content="(2,two,(0,zero,_,_),(8,eight,_,_))")
#| let m3 = m1.remove(8)
#| inspect(
#| m3.debug_tree(),
#| content=(
#| #|(1,one,(0,zero,_,_),(3,three,(2,two,_,_),_))
#| ),
#| )
#| let e : SortedMap[Int32, Int32] = Empty
#| inspect(e.remove(1).debug_tree(), content="_")
#|}
#|test "contains" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| inspect(
#| m.debug_tree(),
#| content="(3,three,(1,one,(0,zero,_,_),(2,two,_,_)),(8,eight,_,_))",
#| )
#| inspect(m.contains(8), content="true")
#| inspect(m.contains(2), content="true")
#| inspect(m.contains(4), content="false")
#|}
#|test "map" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| let n = m.map((_, v) => v + "X")
#| @test.assert_eq(
#| n.debug_tree(),
#| "(3,threeX,(1,oneX,(0,zeroX,_,_),(2,twoX,_,_)),(8,eightX,_,_))",
#| )
#|}
#|test "map_with_key" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| let n = m.map((k, v) => "\{k}-\{v}")
#| @test.assert_eq(
#| n.debug_tree(),
#| "(3,3-three,(1,1-one,(0,0-zero,_,_),(2,2-two,_,_)),(8,8-eight,_,_))",
#| )
#|}
#|test "filter" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| let fm = m.filter((_, v) => v.length() > 3)
#| inspect(fm.debug_tree(), content="(3,three,(0,zero,_,_),(8,eight,_,_))")
#|}
#|test "filter_with_key" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| let fm = m.filter((k, v) => k > 3 && v.length() > 3)
#| inspect(fm.debug_tree(), content="(8,eight,_,_)")
#|}
#|test "singleton" {
#| let m = singleton(3, "three")
#| inspect(m.debug_tree(), content="(3,three,_,_)")
#|}
#|test "empty" {
#| let m : SortedMap[Int32, Int32] = empty()
#| inspect(m.debug_tree(), content="_")
#|}
#|test "split_max" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| @test.assert_eq(
#| m.debug_tree(),
#| "(3,three,(1,one,(0,zero,_,_),(2,two,_,_)),(8,eight,_,_))",
#| )
#| let (k, v, r) = m.split_max()
#| inspect(k, content="8")
#| inspect(v, content="eight")
#| inspect(
#| r.debug_tree(),
#| content=(
#| #|(1,one,(0,zero,_,_),(3,three,(2,two,_,_),_))
#| ),
#| )
#|}
#|test "split_min" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (2, "two"),
#| (1, "one"),
#| (0, "zero"),
#| ])
#| @test.assert_eq(
#| m.debug_tree(),
#| "(3,three,(1,one,(0,zero,_,_),(2,two,_,_)),(8,eight,_,_))",
#| )
#| let (k, v, r) = m.split_min()
#| inspect(k, content="0")
#| inspect(v, content="zero")
#| inspect(
#| r.debug_tree(),
#| content="(3,three,(1,one,_,(2,two,_,_)),(8,eight,_,_))",
#| )
#|}
#|test "glue" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| @test.assert_eq(
#| m.debug_tree(),
#| "(3,three,(1,one,(0,zero,_,_),(2,two,_,_)),(8,eight,_,_))",
#| )
#| let (l, r) = match m {
#| Tree(_, l, r, ..) => (l, r)
#| _ => abort("unreachable")
#| }
#| let m = glue(l, r)
#| inspect(
#| m.debug_tree(),
#| content="(2,two,(1,one,(0,zero,_,_),_),(8,eight,_,_))",
#| )
#|}
#|test "split_max with non-empty tree" {
#| let m = SortedMap([
#| (3, "three"),
#| (8, "eight"),
#| (1, "one"),
#| (2, "two"),
#| (0, "zero"),
#| ])
#| let (k, v, r) = m.split_max()
#| inspect(k, content="8")
#| inspect(v, content="eight")
#| inspect(
#| r.debug_tree(),
#| content=(
#| #|(1,one,(0,zero,_,_),(3,three,(2,two,_,_),_))
#| ),
#| )
#|}
),
"traits_impl.mbt": (
#|pub impl[K, V] Default for SortedMap[K, V] with fn default() {
#| empty()
#|}
#|pub impl[K : Eq, V : Eq] Eq for SortedMap[K, V] with fn equal(self, other) -> Bool {
#| if physical_equal(self, other) {
#| return true
#| }
#| guard self.length() == other.length() else { return false }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| guard a == b else { break false }
#| } nobreak {
#| true
#| }
#|}
#|pub impl[K : Compare, V : Compare] Compare for SortedMap[K, V] with fn compare(
#| self,
#| other,
#|) -> Int32 {
#| guard self.length() == other.length() else {
#| return self.length() - other.length()
#| }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| let cmp = compare(a, b)
#| guard cmp is 0 else { break cmp }
#| } nobreak {
#| 0
#| }
#|}
#|pub impl[K : Hash, V : Hash] Hash for SortedMap[K, V] with fn hash_combine(
#| self,
#| hasher,
#|) {
#| for e in self {
#| hasher.combine(e.0)
#| hasher.combine(e.1)
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[K : Show, V : Show] Show for SortedMap[K, V]
#|#warnings("-deprecated")
#|pub impl[K : Show, V : Show] Show for SortedMap[K, V] with fn output(
#| self,
#| logger,
#|) {
#| logger.write_iter(
#| self.iter(),
#| prefix="@immut/sorted_map.from_array([",
#| suffix="])",
#| )
#|}
#|pub impl[K : Show, V : @json.ToJson] @json.ToJson for SortedMap[K, V] with fn to_json(
#| self,
#|) {
#| @json.Json::object(
#| Map(
#| capacity=self.length(),
#| [
#| for k, v in self => (k.to_string(), v.to_json())
#| ],
#| ),
#| )
#|}
#|pub impl[V : @json.FromJson] @json.FromJson for SortedMap[String, V] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Object(obj) else {
#| raise JsonDecodeError(
#| (path, "@immut/sorted_map.from_json: expected object"),
#| )
#| }
#| for k, v in obj; map = (empty() : SortedMap[String, V]) {
#| continue map.insert(k, V::from_json(v, path.add_key(k)))
#| } nobreak {
#| map
#| }
#|}
),
"types.mbt": (
#|enum SortedMap[K, V] {
#| Empty
#| Tree(K, value~ : V, size~ : Int32, SortedMap[K, V], SortedMap[K, V])
#|}
),
"utils.mbt": (
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[K, V] SortedMap::empty() -> SortedMap[K, V] {
#| Empty
#|}
#|#as_free_fn
#|#owned(key, value)
#|pub fn[K, V] SortedMap::singleton(key : K, value : V) -> SortedMap[K, V] {
#| Tree(key, value~, size=1, Empty, Empty)
#|}
#|pub fn[K : Compare, V] SortedMap::contains(
#| self : SortedMap[K, V],
#| key : K,
#|) -> Bool {
#| for x = self {
#| match x {
#| Empty => break false
#| Tree(k, l, r, ..) => {
#| let c = key.compare(k)
#| if c == 0 {
#| break true
#| } else if c < 0 {
#| continue l
#| } else {
#| continue r
#| }
#| }
#| }
#| }
#|}
#|#alias(size, deprecated)
#|#inline
#|pub fn[K, V] SortedMap::length(self : SortedMap[K, V]) -> Int32 {
#| match self {
#| Empty => 0
#| Tree(_) as t => t.size
#| }
#|}
#|pub fn[K, V] SortedMap::is_empty(self : SortedMap[K, V]) -> Bool {
#| self.length() == 0
#|}
#|#inline
#|#owned(key, value, l, r)
#|fn[K, V] make_tree(
#| key : K,
#| value : V,
#| l : SortedMap[K, V],
#| r : SortedMap[K, V],
#|) -> SortedMap[K, V] {
#| let size = l.length() + r.length() + 1
#| Tree(key, value~, size~, l, r)
#|}
#|#alias(lookup, deprecated)
#|pub fn[K : Compare, V] SortedMap::get(self : SortedMap[K, V], key : K) -> V? {
#| for x = self {
#| match x {
#| Empty => break None
#| Tree(k, value~, l, r, ..) => {
#| let c = key.compare(k)
#| if c == 0 {
#| break Some(value)
#| } else if c < 0 {
#| continue l
#| } else {
#| continue r
#| }
#| }
#| }
#| }
#|}
#|#alias("_[_]")
#|pub fn[K : Compare, V] SortedMap::at(self : SortedMap[K, V], key : K) -> V {
#| for x = self {
#| match x {
#| Empty => break panic()
#| Tree(k, value~, l, r, ..) => {
#| let c = key.compare(k)
#| if c == 0 {
#| break value
#| } else if c < 0 {
#| continue l
#| } else {
#| continue r
#| }
#| }
#| }
#| }
#|}
#|pub fn[K, V] SortedMap::each(
#| self : SortedMap[K, V],
#| f : (K, V) -> Unit raise?,
#|) -> Unit raise? {
#| match self {
#| Empty => ()
#| Tree(k, value~, l, r, ..) => {
#| l.each(f)
#| f(k, value)
#| r.each(f)
#| }
#| }
#|}
#|pub fn[K, V] SortedMap::eachi(
#| self : SortedMap[K, V],
#| f : (Int32, K, V) -> Unit raise?,
#|) -> Unit raise? {
#| fn do_eachi(m : SortedMap[K, V], i : Int32) -> Unit raise? {
#| match m {
#| Empty => ()
#| Tree(k, value~, l, r, ..) => {
#| do_eachi(l, i)
#| f(l.length() + i, k, value)
#| do_eachi(r, l.length() + i + 1)
#| }
#| }
#| }
#| do_eachi(self, 0)
#|}
#|#alias(map_with_key, deprecated)
#|pub fn[K, X, Y] SortedMap::map(
#| self : SortedMap[K, X],
#| f : (K, X) -> Y raise?,
#|) -> SortedMap[K, Y] raise? {
#| match self {
#| Empty => Empty
#| Tree(k, value~, l, r, size~) =>
#| Tree(k, value=f(k, value), size~, l.map(f), r.map(f))
#| }
#|}
#|#alias(foldr_with_key, deprecated)
#|pub fn[K, V, A] SortedMap::rev_fold(
#| self : SortedMap[K, V],
#| init~ : A,
#| f : (A, K, V) -> A raise?,
#|) -> A raise? {
#| fn go(m : SortedMap[K, V], acc : A) -> A raise? {
#| match m {
#| Empty => acc
#| Tree(k, value~, l, r, ..) => go(l, f(go(r, acc), k, value))
#| }
#| }
#| go(self, init)
#|}
#|#alias(foldl_with_key, deprecated)
#|pub fn[K, V, A] SortedMap::fold(
#| self : SortedMap[K, V],
#| init~ : A,
#| f : (A, K, V) -> A raise?,
#|) -> A raise? {
#| fn go(m : SortedMap[K, V], acc : A) -> A raise? {
#| match m {
#| Empty => acc
#| Tree(k, value~, l, r, ..) => go(r, f(go(l, acc), k, value))
#| }
#| }
#| go(self, init)
#|}
#|fn[K : Show, V : Show] SortedMap::debug_tree(self : SortedMap[K, V]) -> String {
#| match self {
#| Empty => "_"
#| Tree(k, value~, l, r, ..) => {
#| let l = l.debug_tree()
#| let r = r.debug_tree()
#| "(\{k},\{value},\{l},\{r})"
#| }
#| }
#|}
#|#as_free_fn(deprecated="Use @immut/sorted_map.SortedMap([...]) instead")
#|#alias(of, deprecated="Use @immut/sorted_map.SortedMap([...]) instead")
#|#as_free_fn(of, deprecated="Use @immut/sorted_map.SortedMap([...]) instead")
#|#deprecated("Use @immut/sorted_map.SortedMap([...]) instead")
#|pub fn[K : Compare, V] SortedMap::from_array(
#| array : ArrayView[(K, V)],
#|) -> SortedMap[K, V] {
#| sorted_map_from_array(array)
#|}
#|fn[K : Compare, V] sorted_map_from_array(
#| array : ArrayView[(K, V)],
#|) -> SortedMap[K, V] {
#| guard array.length() > 0 else { return Empty }
#| let (order, has_duplicates) = sorted_map_array_order(array)
#| if order == 1 {
#| if has_duplicates {
#| let compacted = sorted_map_compact_ordered_array(array, true)
#| return sorted_map_from_ascending_array(compacted, 0, compacted.length())
#| }
#| return sorted_map_from_ascending_array(array, 0, array.length())
#| } else if order == -1 {
#| if has_duplicates {
#| let compacted = sorted_map_compact_ordered_array(array, false)
#| return sorted_map_from_ascending_array(compacted, 0, compacted.length())
#| }
#| return sorted_map_from_descending_array(array, 0, array.length())
#| }
#| if array.length() >= sorted_map_sort_build_threshold {
#| sorted_map_from_unsorted_array(array)
#| } else {
#| sorted_map_from_array_by_add(array)
#| }
#|}
#|let sorted_map_sort_build_threshold = 64
#|priv struct SortEntry[K, V] {
#| key : K
#| value : V
#| index : Int32
#|}
#|impl[K : Compare, V] Eq for SortEntry[K, V] with fn equal(self, other) {
#| self.key == other.key && self.index == other.index
#|}
#|impl[K : Compare, V] Compare for SortEntry[K, V] with fn compare(self, other) {
#| let c = self.key.compare(other.key)
#| if c == 0 {
#| self.index.compare(other.index)
#| } else {
#| c
#| }
#|}
#|fn[K : Compare, V] sorted_map_from_unsorted_array(
#| array : ArrayView[(K, V)],
#|) -> SortedMap[K, V] {
#| let entries : FixedArray[SortEntry[K, V]] = FixedArray::makei(
#| array.length(),
#| i => { key: array[i].0, value: array[i].1, index: i, },
#| )
#| sorted_map_from_unsorted_entries(entries.mut_view())
#|}
#|fn[K : Compare, V] sorted_map_from_unsorted_entries(
#| entries : MutArrayView[SortEntry[K, V]],
#|) -> SortedMap[K, V] {
#| entries.sort()
#| let len = sorted_map_compact_entries_in_place(entries)
#| sorted_map_from_sorted_entries(entries.exact_view(), 0, len)
#|}
#|fn[K : Compare, V] sorted_map_from_array_by_add(
#| array : ArrayView[(K, V)],
#|) -> SortedMap[K, V] {
#| for item in array; mp = Empty {
#| let (k, v) = item
#| continue mp.insert(k, v)
#| } nobreak {
#| mp
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::SortedMap(
#| array : ArrayView[(K, V)],
#|) -> SortedMap[K, V] {
#| sorted_map_from_array(array)
#|}
#|fn[K : Compare, V] sorted_map_compact_entries_in_place(
#| entries : MutArrayView[SortEntry[K, V]],
#|) -> Int32 {
#| let len = entries.length()
#| let mut write = 0
#| for start = 0; start < len; {
#| let end = for end = start; end + 1 < len &&
#| entries.unsafe_get(end).key.compare(
#| entries.unsafe_get(end + 1).key,
#| ) ==
#| 0; {
#| continue end + 1
#| } nobreak {
#| end
#| }
#| entries.unsafe_set(write, {
#| key: entries.unsafe_get(start).key,
#| value: entries.unsafe_get(end).value,
#| index: entries.unsafe_get(start).index,
#| })
#| write += 1
#| continue end + 1
#| }
#| write
#|}
#|fn[K : Compare, V] sorted_map_array_order(
#| array : ArrayView[(K, V)],
#|) -> (Int32, Bool) {
#| let len = array.length()
#| guard len > 1 else { return (1, false) }
#| let mut order = 0
#| let mut has_duplicates = false
#| for i in 1.. Array[(K, V)] {
#| let result = []
#| let len = array.length()
#| if ascending {
#| for start = 0; start < len; {
#| let end = for end = start; end + 1 < len &&
#| array[end].0.compare(array[end + 1].0) == 0; {
#| continue end + 1
#| } nobreak {
#| end
#| }
#| result.push((array[start].0, array[end].1))
#| continue end + 1
#| }
#| } else {
#| for end = len - 1; end >= 0; {
#| let start = for start = end; start > 0 &&
#| array[start - 1].0.compare(array[start].0) == 0; {
#| continue start - 1
#| } nobreak {
#| start
#| }
#| result.push((array[start].0, array[end].1))
#| continue start - 1
#| }
#| }
#| result
#|}
#|fn[K, V] sorted_map_from_ascending_array(
#| array : ArrayView[(K, V)],
#| start : Int32,
#| end : Int32,
#|) -> SortedMap[K, V] {
#| if start >= end {
#| Empty
#| } else {
#| let mid = (start + end) / 2
#| let (key, value) = array[mid]
#| let left = sorted_map_from_ascending_array(array, start, mid)
#| let right = sorted_map_from_ascending_array(array, mid + 1, end)
#| make_tree(key, value, left, right)
#| }
#|}
#|fn[K, V] sorted_map_from_descending_array(
#| array : ArrayView[(K, V)],
#| start : Int32,
#| end : Int32,
#|) -> SortedMap[K, V] {
#| if start >= end {
#| Empty
#| } else {
#| let mid = (start + end) / 2
#| let (key, value) = array[mid]
#| let left = sorted_map_from_descending_array(array, mid + 1, end)
#| let right = sorted_map_from_descending_array(array, start, mid)
#| make_tree(key, value, left, right)
#| }
#|}
#|fn[K, V] sorted_map_from_sorted_entries(
#| entries : ArrayView[SortEntry[K, V]],
#| start : Int32,
#| end : Int32,
#|) -> SortedMap[K, V] {
#| if start >= end {
#| Empty
#| } else {
#| let mid = (start + end) / 2
#| let entry = entries[mid]
#| let left = sorted_map_from_sorted_entries(entries, start, mid)
#| let right = sorted_map_from_sorted_entries(entries, mid + 1, end)
#| make_tree(entry.key, entry.value, left, right)
#| }
#|}
#|#alias(iterator, deprecated)
#|pub fn[K, V] SortedMap::iter(self : SortedMap[K, V]) -> Iter[(K, V)] {
#| let mut curr_node = self
#| let parents = []
#| Iter(
#| fn() {
#| for x = curr_node {
#| match x {
#| Tree(k, value~, Empty, r, ..) => {
#| curr_node = r
#| break Some((k, value))
#| }
#| Tree(k, value~, l, r, ..) => {
#| parents.push((k, value, r))
#| continue l
#| }
#| Empty if parents.pop() is Some((k, v, r)) => {
#| curr_node = r
#| break Some((k, v))
#| }
#| Empty => break None
#| }
#| }
#| },
#| size_hint=self.length(),
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[K, V] SortedMap::iter2(self : SortedMap[K, V]) -> Iter2[K, V] {
#| self.iter()
#|}
#|pub fn[K : Compare, V] SortedMap::range(
#| self : SortedMap[K, V],
#| low~ : K,
#| high~ : K,
#|) -> Iter2[K, V] {
#| let todo_list = []
#| let mut next_node = self
#| Iter(fn() {
#| for x = next_node {
#| match x {
#| Tree(k, value~, l, r, ..) => {
#| let cmp_key_low = k.compare(low)
#| let cmp_key_high = k.compare(high)
#| if cmp_key_low < 0 {
#| continue r
#| } else if cmp_key_high > 0 {
#| continue l
#| } else if l is Empty {
#| next_node = r
#| break Some((k, value))
#| } else {
#| todo_list.push((k, value, r))
#| continue l
#| }
#| }
#| Empty if todo_list.pop() is Some((k, value, r)) => {
#| next_node = r
#| break Some((k, value))
#| }
#| Empty => break None
#| }
#| }
#| })
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Compare, V] SortedMap::from_iter(
#| iter : Iter[(K, V)],
#|) -> SortedMap[K, V] {
#| iter.fold(init=empty(), (m, e) => m.insert(e.0, e.1))
#|}
#|#alias(keys_as_iter, deprecated)
#|pub fn[K, V] SortedMap::keys(self : SortedMap[K, V]) -> Iter[K] {
#| self.iter().map(p => p.0)
#|}
#|pub fn[K, V] SortedMap::values(self : SortedMap[K, V]) -> Iter[V] {
#| self.iter().map(p => p.1)
#|}
#|pub fn[K, V] SortedMap::rev_keys(self : SortedMap[K, V]) -> Iter[K] {
#| let mut curr_node = self
#| let parents = []
#| Iter(
#| fn() {
#| for x = curr_node {
#| match x {
#| Tree(k, l, Empty, ..) => {
#| curr_node = l
#| break Some(k)
#| }
#| Tree(k, l, r, ..) => {
#| parents.push((k, l))
#| continue r
#| }
#| Empty if parents.pop() is Some((k, l)) => {
#| curr_node = l
#| break Some(k)
#| }
#| Empty => break None
#| }
#| }
#| },
#| size_hint=self.length(),
#| )
#|}
#|pub fn[K, V] SortedMap::rev_values(self : SortedMap[K, V]) -> Iter[V] {
#| let mut curr_node = self
#| let parents = []
#| Iter(
#| fn() {
#| for x = curr_node {
#| match x {
#| Tree(_k, value~, l, Empty, ..) => {
#| curr_node = l
#| break Some(value)
#| }
#| Tree(_k, value~, l, r, ..) => {
#| parents.push((value, l))
#| continue r
#| }
#| Empty if parents.pop() is Some((v, l)) => {
#| curr_node = l
#| break Some(v)
#| }
#| Empty => break None
#| }
#| }
#| },
#| size_hint=self.length(),
#| )
#|}
),
},
)
///|
let moonbitlang_core_immut_sorted_set_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/sorted_set",
deps={},
files={
"debug.mbt": (
#|pub impl[K : @debug.Debug] @debug.Debug for SortedSet[K] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "SortedSet",
#| @debug.Repr::array([ for x in self => Repr(x) ]),
#| )
#|}
#|test "Debug for SortedSet" {
#| let ss : SortedSet[Int32] = SortedSet([3, 1, 2])
#| @debug.debug_inspect(ss, content="")
#|}
),
"deprecated.mbt": (
#|#as_free_fn(deprecated="Use `@json.from_json` instead")
#|#deprecated("Use `@json.from_json` instead")
#|pub fn[A : @json.FromJson + Compare] SortedSet::from_json(
#| json : @json.Json,
#|) -> SortedSet[A] raise @json.JsonDecodeError {
#| @json.from_json(json)
#|}
),
"extends.mbt": (
#|pub extend SortedSet with Compare::{compare}
#|pub extend SortedSet with Eq::{equal}
#|pub extend SortedSet with Hash::{hash}
#|pub extend SortedSet with Sub::{sub}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with @json.ToJson::{to_json}
),
"generic.mbt": (
#|#alias(rev_iterator, deprecated)
#|pub fn[A] SortedSet::rev_iter(self : SortedSet[A]) -> Iter[A] {
#| let mut curr_node = self
#| let parents = []
#| Iter(
#| fn() {
#| for x = curr_node {
#| match x {
#| Node(left~, right=Empty, value~, ..) => {
#| curr_node = left
#| break Some(value)
#| }
#| Node(left~, right~, value~, ..) => {
#| parents.push((value, left))
#| continue right
#| }
#| Empty if parents.pop() is Some((value, left)) => {
#| curr_node = left
#| break Some(value)
#| }
#| Empty => break None
#| }
#| }
#| },
#| size_hint=self.length(),
#| )
#|}
#|#alias(iterator, deprecated)
#|pub fn[A] SortedSet::iter(self : SortedSet[A]) -> Iter[A] {
#| let mut curr_node = self
#| let parents = []
#| Iter(
#| fn() {
#| for x = curr_node {
#| match x {
#| Node(left=Empty, right~, value~, size=_) => {
#| curr_node = right
#| break Some(value)
#| }
#| Node(left~, right~, value~, size=_) => {
#| parents.push((value, right))
#| continue left
#| }
#| Empty if parents.pop() is Some((value, right)) => {
#| curr_node = right
#| break Some(value)
#| }
#| Empty => break None
#| }
#| }
#| },
#| size_hint=self.length(),
#| )
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A : Compare] SortedSet::from_iter(iter : Iter[A]) -> SortedSet[A] {
#| iter.fold(init=empty(), (s, e) => s.insert(e))
#|}
#|test {
#| @json.json_inspect(SortedSet([2, 7, 1, 2, 3, 4, 5]), content=[
#| 1, 2, 3, 4, 5, 7,
#| ])
#|}
#|pub impl[A : Eq] Eq for SortedSet[A] with fn equal(self, other) -> Bool {
#| if physical_equal(self, other) {
#| return true
#| }
#| guard self.length() == other.length() else { return false }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| guard a == b else { break false }
#| } nobreak {
#| true
#| }
#|}
#|pub impl[A : Compare] Compare for SortedSet[A] with fn compare(self, other) -> Int32 {
#| let my_size = self.length()
#| let other_size = other.length()
#| guard my_size == other_size else { return my_size - other_size }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| let cmp = a.compare(b)
#| guard cmp is 0 else { break cmp }
#| } nobreak {
#| 0
#| }
#|}
#|test "Eq - equal sets" {
#| let s1 = SortedSet([1, 2, 3, 4, 5])
#| let s2 = SortedSet([5, 4, 3, 2, 1])
#| inspect(s1 == s2, content="true")
#|}
#|test "Eq - different elements same size" {
#| let s1 = SortedSet([1, 2, 3, 4, 5])
#| let s2 = SortedSet([1, 2, 3, 4, 6])
#| inspect(s1 == s2, content="false")
#|}
#|test "Eq - different sizes" {
#| let s1 = SortedSet([1, 2, 3])
#| let s2 = SortedSet([1, 2, 3, 4, 5])
#| inspect(s1 == s2, content="false")
#|}
#|test "Eq - empty sets" {
#| let s1 : SortedSet[Int32] = empty()
#| let s2 : SortedSet[Int32] = empty()
#| inspect(s1 == s2, content="true")
#|}
#|test "Eq - one empty one not" {
#| let s1 : SortedSet[Int32] = empty()
#| let s2 = SortedSet([1])
#| inspect(s1 == s2, content="false")
#|}
#|test "Compare - equal sets" {
#| let s1 = SortedSet([1, 2, 3])
#| let s2 = SortedSet([3, 2, 1])
#| inspect(s1.compare(s2), content="0")
#|}
#|test "Compare - first smaller by size" {
#| let s1 = SortedSet([1, 2])
#| let s2 = SortedSet([1, 2, 3])
#| inspect(s1.compare(s2) < 0, content="true")
#|}
#|test "Compare - first larger by size" {
#| let s1 = SortedSet([1, 2, 3, 4])
#| let s2 = SortedSet([1, 2])
#| inspect(s1.compare(s2) > 0, content="true")
#|}
#|test "Compare - same size first smaller by elements" {
#| let s1 = SortedSet([1, 2, 3])
#| let s2 = SortedSet([1, 2, 4])
#| inspect(s1.compare(s2) < 0, content="true")
#|}
#|test "Compare - same size first larger by elements" {
#| let s1 = SortedSet([1, 2, 5])
#| let s2 = SortedSet([1, 2, 4])
#| inspect(s1.compare(s2) > 0, content="true")
#|}
#|test "Compare - empty sets" {
#| let s1 : SortedSet[Int32] = empty()
#| let s2 : SortedSet[Int32] = empty()
#| inspect(s1.compare(s2), content="0")
#|}
),
"immutable_set.mbt": (
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[A] SortedSet::empty() -> SortedSet[A] {
#| Empty
#|}
#|pub impl[A] Default for SortedSet[A] with fn default() {
#| Empty
#|}
#|#as_free_fn
#|#owned(value)
#|pub fn[A] SortedSet::singleton(value : A) -> SortedSet[A] {
#| Node(left=Empty, value~, right=Empty, size=1)
#|}
#|#as_free_fn(deprecated="Use @immut/sorted_set.SortedSet([...]) instead")
#|#alias(of, deprecated="Use @immut/sorted_set.SortedSet([...]) instead")
#|#as_free_fn(of, deprecated="Use @immut/sorted_set.SortedSet([...]) instead")
#|#deprecated("Use @immut/sorted_set.SortedSet([...]) instead")
#|pub fn[A : Compare] SortedSet::from_array(array : ArrayView[A]) -> SortedSet[A] {
#| sorted_set_from_array(array)
#|}
#|fn[A : Compare] sorted_set_from_array(array : ArrayView[A]) -> SortedSet[A] {
#| guard array.length() > 0 else { return Empty }
#| let (order, has_duplicates) = sorted_set_array_order(array)
#| if order == 1 {
#| if has_duplicates {
#| let compacted = sorted_set_compact_ordered_array(array, true)
#| return sorted_set_from_ascending_array(compacted, 0, compacted.length())
#| }
#| return sorted_set_from_ascending_array(array, 0, array.length())
#| } else if order == -1 {
#| if has_duplicates {
#| let compacted = sorted_set_compact_ordered_array(array, false)
#| return sorted_set_from_ascending_array(compacted, 0, compacted.length())
#| }
#| return sorted_set_from_descending_array(array, 0, array.length())
#| }
#| if array.length() >= sorted_set_sort_build_threshold {
#| sorted_set_from_unsorted_buffer(array.to_owned().mut_view())
#| } else {
#| sorted_set_from_array_by_add(array)
#| }
#|}
#|let sorted_set_sort_build_threshold = 64
#|fn[A : Compare] sorted_set_from_unsorted_buffer(
#| buf : MutArrayView[A],
#|) -> SortedSet[A] {
#| buf.stable_sort()
#| let len = sorted_set_dedupe_in_place(buf)
#| sorted_set_from_ascending_array(buf.exact_view(start=0, end=len), 0, len)
#|}
#|fn[A : Compare] sorted_set_dedupe_in_place(buf : MutArrayView[A]) -> Int32 {
#| let len = buf.length()
#| let mut write = 0
#| for start = 0; start < len; {
#| let end = for end = start; end + 1 < len &&
#| buf.unsafe_get(end).compare(buf.unsafe_get(end + 1)) == 0; {
#| continue end + 1
#| } nobreak {
#| end
#| }
#| buf.unsafe_set(write, buf.unsafe_get(start))
#| write += 1
#| continue end + 1
#| }
#| write
#|}
#|fn[A : Compare] sorted_set_from_array_by_add(
#| array : ArrayView[A],
#|) -> SortedSet[A] {
#| for value in array; set = Empty {
#| continue set.insert(value)
#| } nobreak {
#| set
#| }
#|}
#|pub fn[A : Compare] SortedSet::SortedSet(array : ArrayView[A]) -> SortedSet[A] {
#| sorted_set_from_array(array)
#|}
#|fn[A : Compare] sorted_set_array_order(array : ArrayView[A]) -> (Int32, Bool) {
#| let len = array.length()
#| guard len > 1 else { return (1, false) }
#| let mut order = 0
#| let mut has_duplicates = false
#| for i in 1.. Array[A] {
#| let result = []
#| let len = array.length()
#| if ascending {
#| for start = 0; start < len; {
#| let end = for end = start; end + 1 < len &&
#| array[end].compare(array[end + 1]) == 0; {
#| continue end + 1
#| } nobreak {
#| end
#| }
#| result.push(array[start])
#| continue end + 1
#| }
#| } else {
#| for end = len - 1; end >= 0; {
#| let start = for start = end; start > 0 &&
#| array[start - 1].compare(array[start]) == 0; {
#| continue start - 1
#| } nobreak {
#| start
#| }
#| result.push(array[start])
#| continue start - 1
#| }
#| }
#| result
#|}
#|fn[A] sorted_set_from_ascending_array(
#| array : ArrayView[A],
#| start : Int32,
#| end : Int32,
#|) -> SortedSet[A] {
#| if start >= end {
#| Empty
#| } else {
#| let mid = (start + end) / 2
#| let value = array[mid]
#| let left = sorted_set_from_ascending_array(array, start, mid)
#| let right = sorted_set_from_ascending_array(array, mid + 1, end)
#| create(left, value, right)
#| }
#|}
#|fn[A] sorted_set_from_descending_array(
#| array : ArrayView[A],
#| start : Int32,
#| end : Int32,
#|) -> SortedSet[A] {
#| if start >= end {
#| Empty
#| } else {
#| let mid = (start + end) / 2
#| let value = array[mid]
#| let left = sorted_set_from_descending_array(array, mid + 1, end)
#| let right = sorted_set_from_descending_array(array, start, mid)
#| create(left, value, right)
#| }
#|}
#|pub fn[A] SortedSet::to_array(self : SortedSet[A]) -> Array[A] {
#| let arr = []
#| fn aux(set : SortedSet[A]) {
#| match set {
#| Empty => ()
#| Node(left~, value~, right~, ..) => {
#| aux(left)
#| arr.push(value)
#| aux(right)
#| }
#| }
#| }
#| aux(self)
#| arr
#|}
#|pub fn[A] SortedSet::remove_min(self : SortedSet[A]) -> SortedSet[A] {
#| match self {
#| Empty => abort("remove_min: empty ImmutableSet")
#| Node(left~, right~, value~, ..) =>
#| if left is Empty {
#| right
#| } else {
#| balance(left.remove_min(), value, right)
#| }
#| }
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn[A : Compare] SortedSet::insert(
#| self : SortedSet[A],
#| value : A,
#|) -> SortedSet[A] {
#| match self {
#| Empty => Node(left=Empty, value~, right=Empty, size=1)
#| Node(left~, right~, value=node_value, ..) => {
#| let compare_result = value.compare(node_value)
#| if compare_result == 0 {
#| self
#| } else if compare_result < 0 {
#| let ll = left.insert(value)
#| if physical_equal(left, ll) {
#| self
#| } else {
#| balance(ll, node_value, right)
#| }
#| } else {
#| let rr = right.insert(value)
#| if physical_equal(right, rr) {
#| self
#| } else {
#| balance(left, node_value, rr)
#| }
#| }
#| }
#| }
#|}
#|pub fn[A : Compare] SortedSet::remove(
#| self : SortedSet[A],
#| value : A,
#|) -> SortedSet[A] {
#| match self {
#| Empty => Empty
#| Node(left~, right~, value=node_value, ..) => {
#| let compare_result = value.compare(node_value)
#| if compare_result == 0 {
#| left.merge(right)
#| } else if compare_result < 0 {
#| let new_left = left.remove(value)
#| if physical_equal(left, new_left) {
#| self
#| } else {
#| balance(new_left, node_value, right)
#| }
#| } else {
#| let new_right = right.remove(value)
#| if physical_equal(right, new_right) {
#| self
#| } else {
#| balance(left, node_value, new_right)
#| }
#| }
#| }
#| }
#|}
#|pub fn[A] SortedSet::min(self : SortedSet[A]) -> A {
#| for cur = self {
#| match cur {
#| Empty => abort("min: there are no values in sorted_set.")
#| Node(left~, value~, ..) =>
#| if left is Empty {
#| break value
#| } else {
#| continue left
#| }
#| }
#| }
#|}
#|pub fn[A] SortedSet::min_option(self : SortedSet[A]) -> A? {
#| for cur = self {
#| match cur {
#| Empty => break None
#| Node(left~, value~, ..) =>
#| if left is Empty {
#| break Some(value)
#| } else {
#| continue left
#| }
#| }
#| }
#|}
#|pub fn[A] SortedSet::max(self : SortedSet[A]) -> A {
#| for cur = self {
#| match cur {
#| Empty => abort("max: there are no values in ImmutableSet.")
#| Node(right~, value~, ..) =>
#| if right is Empty {
#| break value
#| } else {
#| continue right
#| }
#| }
#| }
#|}
#|pub fn[A] SortedSet::max_option(self : SortedSet[A]) -> A? {
#| for cur = self {
#| match cur {
#| Empty => break None
#| Node(right~, value~, ..) =>
#| if right is Empty {
#| break Some(value)
#| } else {
#| continue right
#| }
#| }
#| }
#|}
#|pub fn[A : Compare] SortedSet::split(
#| self : SortedSet[A],
#| divide : A,
#|) -> (SortedSet[A], Bool, SortedSet[A]) {
#| match self {
#| Empty => (Empty, false, Empty)
#| Node(left~, right~, value~, ..) => {
#| let compare_result = divide.compare(value)
#| if compare_result == 0 {
#| (left, true, right)
#| } else if compare_result < 0 {
#| let (left_left, present, right_left) = left.split(divide)
#| (left_left, present, join(right_left, value, right))
#| } else {
#| let (left_right, present, right_right) = right.split(divide)
#| (join(left, value, left_right), present, right_right)
#| }
#| }
#| }
#|}
#|pub fn[A] SortedSet::is_empty(self : SortedSet[A]) -> Bool {
#| self is Empty
#|}
#|pub fn[A : Compare] SortedSet::contains(self : SortedSet[A], value : A) -> Bool {
#| match self {
#| Empty => false
#| Node(left~, right~, value=node_value, ..) => {
#| let compare_result = value.compare(node_value)
#| compare_result == 0 ||
#| (if compare_result < 0 { left } else { right }).contains(value)
#| }
#| }
#|}
#|pub fn[A : Compare] SortedSet::range(
#| self : SortedSet[A],
#| low~ : A,
#| high~ : A,
#|) -> Iter[A] {
#| let todo_list = []
#| let mut next_node = self
#| Iter(fn() {
#| for x = next_node {
#| match x {
#| Node(left~, right~, value~, ..) => {
#| let cmp_value_low = value.compare(low)
#| let cmp_value_high = value.compare(high)
#| if cmp_value_low < 0 {
#| continue right
#| } else if cmp_value_high > 0 {
#| continue left
#| } else if left is Empty {
#| next_node = right
#| break Some(value)
#| } else {
#| todo_list.push((value, right))
#| continue left
#| }
#| }
#| Empty if todo_list.pop() is Some((value, right)) => {
#| next_node = right
#| break Some(value)
#| }
#| Empty => break None
#| }
#| }
#| })
#|}
#|pub fn[A : Compare] SortedSet::union(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> SortedSet[A] {
#| match (self, other) {
#| (Empty, _) => other
#| (_, Empty) => self
#| (
#| Node(left=l1, value=v1, right=r1, size=s1),
#| Node(left=l2, value=v2, right=r2, size=s2),
#| ) =>
#| if s1 >= s2 {
#| if s2 == 1 {
#| self.insert(v2)
#| } else {
#| let (l2, _, r2) = other.split(v1)
#| join(l1.union(l2), v1, r1.union(r2))
#| }
#| } else if s1 == 1 {
#| other.insert(v1)
#| } else {
#| let (l1, _, r1) = self.split(v2)
#| join(l1.union(l2), v2, r1.union(r2))
#| }
#| }
#|}
#|pub impl[A : Compare] Add for SortedSet[A] with fn add(self, other) {
#| return self.union(other)
#|}
#|#alias(inter, deprecated)
#|pub fn[A : Compare] SortedSet::intersection(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> SortedSet[A] {
#| match (self, other) {
#| (Empty, _) | (_, Empty) => Empty
#| (Node(left=l1, value=v1, right=r1, ..), _) =>
#| match other.split(v1) {
#| (l2, false, r2) => l1.intersection(l2).concat(r1.intersection(r2))
#| (l2, true, r2) => join(l1.intersection(l2), v1, r1.intersection(r2))
#| }
#| }
#|}
#|#alias(diff, deprecated)
#|pub fn[A : Compare] SortedSet::difference(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> SortedSet[A] {
#| match (self, other) {
#| (Empty, _) => Empty
#| (_, Empty) => self
#| (Node(left=l1, value=v1, right=r1, ..), _) =>
#| match other.split(v1) {
#| (l2, false, r2) => join(l1.difference(l2), v1, r1.difference(r2))
#| (l2, true, r2) => l1.difference(l2).concat(r1.difference(r2))
#| }
#| }
#|}
#|pub impl[A : Compare] Sub for SortedSet[A] with fn sub(self, other) {
#| self.difference(other)
#|}
#|pub fn[A : Compare] SortedSet::symmetric_difference(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> SortedSet[A] {
#| match (self, other) {
#| (Empty, _) => other
#| (_, Empty) => self
#| (Node(left=l1, value=v1, right=r1, ..), _) =>
#| match other.split(v1) {
#| (l2, false, r2) =>
#| join(l1.symmetric_difference(l2), v1, r1.symmetric_difference(r2))
#| (l2, true, r2) =>
#| l1.symmetric_difference(l2).concat(r1.symmetric_difference(r2))
#| }
#| }
#|}
#|#alias(subset, deprecated="Use `is_subset` instead")
#|pub fn[A : Compare] SortedSet::is_subset(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> Bool {
#| match (self, other) {
#| (Empty, _) => true
#| (_, Empty) => false
#| (
#| Node(left=l1, value=v1, right=r1, ..),
#| Node(left=l2, value=v2, right=r2, ..),
#| ) => {
#| let compare_result = v1.compare(v2)
#| if compare_result == 0 {
#| l1.is_subset(l2) && r1.is_subset(r2)
#| } else if compare_result < 0 {
#| Node(left=l1, value=v1, right=Empty, size=1).is_subset(l2) &&
#| r1.is_subset(other)
#| } else {
#| Node(left=Empty, value=v1, right=r1, size=1).is_subset(r2) &&
#| l1.is_subset(other)
#| }
#| }
#| }
#|}
#|pub fn[A : Compare] SortedSet::is_superset(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> Bool {
#| other.is_subset(self)
#|}
#|#alias(disjoint, deprecated="Use `is_disjoint` instead")
#|pub fn[A : Compare] SortedSet::is_disjoint(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> Bool {
#| match (self, other) {
#| (Empty, _) | (_, Empty) => true
#| (Node(left=l1, value=v1, right=r1, ..), _) =>
#| if physical_equal(self, other) {
#| false
#| } else {
#| match other.split_bis(v1) {
#| NotFound(l2, r2) => l1.is_disjoint(l2) && r1.is_disjoint(r2())
#| Found => false
#| }
#| }
#| }
#|}
#|pub fn[A] SortedSet::each(
#| self : SortedSet[A],
#| f : (A) -> Unit raise?,
#|) -> Unit raise? {
#| match self {
#| Empty => ()
#| Node(left~, value~, right~, ..) => {
#| left.each(f)
#| f(value)
#| right.each(f)
#| }
#| }
#|}
#|pub fn[A, B] SortedSet::fold(
#| self : SortedSet[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| match self {
#| Empty => init
#| Node(left~, value~, right~, ..) =>
#| right.fold(init=f(left.fold(init~, f), value), f)
#| }
#|}
#|pub fn[A, B : Compare] SortedSet::map(
#| self : SortedSet[A],
#| f : (A) -> B raise?,
#|) -> SortedSet[B] raise? {
#| match self {
#| Empty => Empty
#| Node(left~, value~, right~, ..) =>
#| try_join(left.map(f), f(value), right.map(f))
#| }
#|}
#|pub fn[A] SortedSet::all(
#| self : SortedSet[A],
#| f : (A) -> Bool raise?,
#|) -> Bool raise? {
#| match self {
#| Empty => true
#| Node(left~, value~, right~, ..) => f(value) && left.all(f) && right.all(f)
#| }
#|}
#|pub fn[A] SortedSet::any(
#| self : SortedSet[A],
#| f : (A) -> Bool raise?,
#|) -> Bool raise? {
#| match self {
#| Empty => false
#| Node(left~, value~, right~, ..) => f(value) || left.any(f) || right.any(f)
#| }
#|}
#|pub fn[A] SortedSet::filter(
#| self : SortedSet[A],
#| f : (A) -> Bool raise?,
#|) -> SortedSet[A] raise? {
#| match self {
#| Empty => Empty
#| Node(left~, value~, right~, ..) => {
#| let l = left.filter(f)
#| let v = f(value)
#| let r = right.filter(f)
#| if v {
#| if physical_equal(l, left) && physical_equal(r, right) {
#| self
#| } else {
#| join(l, value, r)
#| }
#| } else {
#| l.concat(r)
#| }
#| }
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show] Show for SortedSet[A]
#|pub impl[A : Show] Show for SortedSet[A] with fn output(self, logger) {
#| logger.write_iter(
#| self.iter(),
#| prefix="@immut/sorted_set.from_array([",
#| suffix="])",
#| )
#|}
#|pub impl[A : @json.ToJson] @json.ToJson for SortedSet[A] with fn to_json(self) {
#| [
#| for a in self => a
#| ]
#|}
#|pub impl[A : @json.FromJson + Compare] @json.FromJson for SortedSet[A] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(arr) else {
#| raise path.error("@immut/sorted_set.from_json: expected array")
#| }
#| for i, x in arr; set = (empty() : SortedSet[A]) {
#| continue set.insert(@json.FromJson::from_json(x, path.add_index(i)))
#| } nobreak {
#| set
#| }
#|}
#|const DELTA = 3
#|const GAMMA = 2
#|priv enum SplitBis[A] {
#| Found
#| NotFound(SortedSet[A], () -> SortedSet[A])
#|}
#|impl[T] Show for SplitBis[T] with fn output(self, logger) {
#| match self {
#| Found => logger <+ "Found"
#| NotFound(_) => logger <+ "NotFound"
#| }
#|}
#|fn[A : Compare] SortedSet::split_bis(
#| self : SortedSet[A],
#| value : A,
#|) -> SplitBis[A] {
#| match self {
#| Empty => NotFound(Empty, () => Empty)
#| Node(left~, value=node_value, right~, ..) => {
#| let compare_result = value.compare(node_value)
#| if compare_result == 0 {
#| Found
#| } else if compare_result < 0 {
#| match left.split_bis(value) {
#| Found => Found
#| NotFound(ll, rl) => NotFound(ll, () => join(rl(), node_value, right))
#| }
#| } else {
#| match right.split_bis(value) {
#| Found => Found
#| NotFound(lr, rr) => NotFound(join(left, node_value, lr), rr)
#| }
#| }
#| }
#| }
#|}
#|#alias(size, deprecated)
#|#inline
#|pub fn[A] SortedSet::length(self : SortedSet[A]) -> Int32 {
#| match self {
#| Node(size~, ..) | (Empty with size = 0) => size
#| }
#|}
#|#inline
#|#owned(left, value, right)
#|fn[A] create(
#| left : SortedSet[A],
#| value : A,
#| right : SortedSet[A],
#|) -> SortedSet[A] {
#| Node(left~, right~, value~, size=left.length() + right.length() + 1)
#|}
#|fn[A] balance(
#| left : SortedSet[A],
#| value : A,
#| right : SortedSet[A],
#|) -> SortedSet[A] {
#| let left_size = left.length()
#| let right_size = right.length()
#| if left_size + right_size < 2 {
#| create(left, value, right)
#| } else if left_size > right_size * DELTA {
#| match left {
#| Empty => abort("balance: left is empty.")
#| Node(left=ll, value=lv, right=lr, ..) =>
#| if lr.length() < GAMMA * ll.length() {
#| create(ll, lv, create(lr, value, right))
#| } else {
#| match lr {
#| Empty => abort("balance: right left.right is empty.")
#| Node(left=lrl, value=lrv, right=lrr, ..) =>
#| create(create(ll, lv, lrl), lrv, create(lrr, value, right))
#| }
#| }
#| }
#| } else if right_size > left_size * DELTA {
#| match right {
#| Empty => abort("balance: right is empty")
#| Node(left=rl, value=rv, right=rr, ..) =>
#| if rl.length() < GAMMA * rr.length() {
#| create(create(left, value, rl), rv, rr)
#| } else {
#| match rl {
#| Empty => abort("balance: right.left is empty")
#| Node(left=rll, value=rlv, right=rlr, ..) =>
#| create(create(left, value, rll), rlv, create(rlr, rv, rr))
#| }
#| }
#| }
#| } else {
#| create(left, value, right)
#| }
#|}
#|#owned(value)
#|fn[A] SortedSet::add_min_value(self : SortedSet[A], value : A) -> SortedSet[A] {
#| match self {
#| Empty => singleton(value)
#| Node(left~, value=node_value, right~, ..) =>
#| balance(left.add_min_value(value), node_value, right)
#| }
#|}
#|#owned(value)
#|fn[A] SortedSet::add_max_value(self : SortedSet[A], value : A) -> SortedSet[A] {
#| match self {
#| Empty => singleton(value)
#| Node(left~, value=node_value, right~, ..) =>
#| balance(left, node_value, right.add_max_value(value))
#| }
#|}
#|fn[A] join(
#| left : SortedSet[A],
#| value : A,
#| right : SortedSet[A],
#|) -> SortedSet[A] {
#| match (left, right) {
#| (Empty, _) => right.add_min_value(value)
#| (_, Empty) => left.add_max_value(value)
#| (
#| Node(left=ll, value=lv, right=lr, size=ls),
#| Node(left=rl, value=rv, right=rr, size=rs),
#| ) =>
#| if ls > rs * DELTA {
#| balance(ll, lv, join(lr, value, right))
#| } else if rs > ls * DELTA {
#| balance(join(left, value, rl), rv, rr)
#| } else {
#| create(left, value, right)
#| }
#| }
#|}
#|fn[A : Compare] try_join(
#| left : SortedSet[A],
#| value : A,
#| right : SortedSet[A],
#|) -> SortedSet[A] {
#| if (left == Empty || left.max().compare(value) < 0) &&
#| (right == Empty || value.compare(right.min()) < 0) {
#| join(left, value, right)
#| } else {
#| left.union(right.insert(value))
#| }
#|}
#|fn[A] SortedSet::merge(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> SortedSet[A] {
#| match (self, other) {
#| (Empty, _) => other
#| (_, Empty) => self
#| _ => balance(self, other.min(), other.remove_min())
#| }
#|}
#|fn[A] SortedSet::concat(
#| self : SortedSet[A],
#| other : SortedSet[A],
#|) -> SortedSet[A] {
#| match (self, other) {
#| (Empty, _) => other
#| (_, Empty) => self
#| _ => join(self, other.min(), other.remove_min())
#| }
#|}
#|pub impl[A : Hash] Hash for SortedSet[A] with fn hash_combine(self, hasher) {
#| for t in self {
#| t.hash_combine(hasher)
#| }
#|}
#|test "split_bis" {
#| inspect(SortedSet([1, 2, 3]).split_bis(1), content="Found")
#| inspect(SortedSet([1, 2, 3]).split_bis(3), content="Found")
#| inspect(SortedSet([1, 2, 3]).split_bis(0), content="NotFound")
#| inspect(SortedSet([1, 2, 3]).split_bis(4), content="NotFound")
#|}
#|test "balance with left height greater" {
#| let left = SortedSet([1, 2, 3])
#| let value = 4
#| let right = SortedSet([5])
#| let balanced_set = balance(left, value, right)
#| @debug.debug_inspect(
#| balanced_set,
#| content=(
#| #|
#| ),
#| )
#|}
#|test "balance with right height greater" {
#| let left = SortedSet([1])
#| let value = 2
#| let right = SortedSet([3, 4, 5])
#| let balanced_set = balance(left, value, right)
#| @debug.debug_inspect(
#| balanced_set,
#| content=(
#| #|
#| ),
#| )
#|}
#|test "join with different heights" {
#| let left = SortedSet([1, 2, 3])
#| let value = 4
#| let right = SortedSet([5, 6, 7])
#| let joined_set = join(left, value, right)
#| @debug.debug_inspect(
#| joined_set,
#| content=(
#| #|
#| ),
#| )
#|}
#|test "hash" {
#| @test.assert_eq(
#| Hash::hash(SortedSet([1, 2, 3, 4])),
#| Hash::hash(SortedSet([3, 2]).insert(1).insert(4)),
#| )
#| @test.assert_not_eq(
#| Hash::hash(SortedSet([1, 2, 3])),
#| Hash::hash(SortedSet([1, 2, 4])),
#| )
#|}
),
"types.mbt": (
#|enum SortedSet[A] {
#| Empty
#| Node(left~ : SortedSet[A], right~ : SortedSet[A], size~ : Int32, value~ : A)
#|}
),
},
)
///|
let moonbitlang_core_immut_vector_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/vector",
deps={},
files={
"debug.mbt": (
#|pub impl[A : @debug.Debug] @debug.Debug for Vector[A] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "Vector",
#| @debug.Repr::array(self.to_array().map(x => Repr(x))),
#| )
#|}
#|test "Debug for Vector" {
#| let xs : Vector[Int32] = from_array([1, 2, 3])
#| @debug.debug_inspect(xs, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("We don't copy immutable vector")
#|#coverage.skip
#|#alias(clone, deprecated)
#|pub fn[A] Vector::copy(self : Vector[A]) -> Vector[A] {
#| self
#|}
),
"extends.mbt": (
#|pub extend Vector with Add::{add}
#|pub extend Vector with Compare::{compare}
#|pub extend Vector with Eq::{equal}
#|pub extend Vector with Hash::{hash}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with Default::{default}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with @debug.Debug::{to_repr}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with @json.FromJson::{from_json}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Vector with @json.ToJson::{to_json}
),
"tree.mbt": (
#|const NUM_BITS = 5
#|const BRANCHING_FACTOR : Int32 = 1 << NUM_BITS
#|const BITMASK : Int32 = BRANCHING_FACTOR - 1
#|const LINEAR_THRESHOLD : Int32 = 4
#|const E_MAX : Int32 = 2
#|let e_max_2 : Int32 = E_MAX / 2
#|fn[T] Tree::empty() -> Tree[T] {
#| Empty
#|}
#|#owned(leaf)
#|fn[T] new_branch_left(leaf : FixedArray[T], shift : Int32) -> Tree[T] {
#| let sizes : FixedArray[Int32]? = if leaf.length() == BRANCHING_FACTOR {
#| None
#| } else {
#| Some([leaf.length()])
#| }
#| match shift {
#| 0 => Leaf(leaf)
#| s => Node([new_branch_left(leaf, s - NUM_BITS)], sizes)
#| }
#|}
#|fn[T] Tree::get(self : Tree[T], index : Int32, shift : Int32) -> T {
#| fn get_radix(node : Tree[T], shift : Int32) -> T {
#| for cur = node, s = shift {
#| match cur {
#| Leaf(leaf) => break leaf[index & BITMASK]
#| Node(node, None) =>
#| continue node[radix_indexing(index, s)], s - NUM_BITS
#| Node(_, Some(_)) =>
#| abort("Unreachable: Node should not have sizes in get_radix")
#| Empty => abort("Index out of bounds")
#| }
#| }
#| }
#| match self {
#| Leaf(leaf) => leaf[index]
#| Node(children, Some(sizes)) => {
#| let branch_index = get_branch_index(sizes, index)
#| let sub_index = if branch_index == 0 {
#| index
#| } else {
#| index - sizes[branch_index - 1]
#| }
#| children[branch_index].get(sub_index, shift - NUM_BITS)
#| }
#| Node(_, None) => get_radix(self, shift)
#| Empty => abort("Index out of bounds")
#| }
#|}
#|#owned(value)
#|fn[T] Tree::set(
#| self : Tree[T],
#| index : Int32,
#| shift : Int32,
#| value : T,
#|) -> Tree[T] {
#| fn set_radix(node : Tree[T], shift : Int32) -> Tree[T] {
#| match node {
#| Leaf(leaf) => Leaf(immutable_set(leaf, index & BITMASK, value))
#| Node(node, None) => {
#| let sub_idx = radix_indexing(index, shift)
#| Node(
#| immutable_set(
#| node,
#| sub_idx,
#| set_radix(node[radix_indexing(index, shift)], shift - NUM_BITS),
#| ),
#| None,
#| )
#| }
#| Node(_, Some(_)) =>
#| abort("Unreachable: Node should not have sizes in set_radix")
#| Empty => abort("Index out of bounds")
#| }
#| }
#| match self {
#| Leaf(leaf) => Leaf(immutable_set(leaf, index & BITMASK, value))
#| Node(children, Some(sizes)) => {
#| let branch_index = get_branch_index(sizes, index)
#| let sub_index = if branch_index == 0 {
#| index
#| } else {
#| index - sizes[branch_index - 1]
#| }
#| Node(
#| immutable_set(
#| children,
#| branch_index,
#| children[branch_index].set(sub_index, shift - NUM_BITS, value),
#| ),
#| Some(sizes),
#| )
#| }
#| Node(_children, None) => set_radix(self, shift)
#| Empty => abort("Index out of bounds")
#| }
#|}
#|#owned(value)
#|fn[T] Tree::push_end(
#| self : Tree[T],
#| shift : Int32,
#| value : T,
#|) -> (Tree[T], Int32) {
#| fn worker(node : Tree[T], shift : Int32) -> Tree[T]? {
#| match node {
#| Leaf(leaf) => {
#| if shift != 0 {
#| abort(
#| "Unreachable: Leaf should not have a non-zero shift, which means we have not reached the bottom of the tree",
#| )
#| }
#| if leaf.length() < BRANCHING_FACTOR {
#| Some(Leaf(immutable_push(leaf, value)))
#| } else {
#| None
#| }
#| }
#| Node(nodes, sizes) => {
#| let len = nodes.length()
#| match worker(nodes[len - 1], shift - NUM_BITS) {
#| Some(new_node) => {
#| let new_nodes = nodes.copy()
#| new_nodes[len - 1] = new_node
#| let new_sizes = match sizes {
#| Some(_) => append_sizes_last(sizes, 1)
#| None => compute_sizes(new_nodes, shift - NUM_BITS)
#| }
#| Some(Node(new_nodes, new_sizes))
#| }
#| None =>
#| if len < BRANCHING_FACTOR {
#| let new_nodes = immutable_push(
#| nodes,
#| new_branch_left([value], shift - NUM_BITS),
#| )
#| let new_sizes = match sizes {
#| Some(_) => push_sizes_last(sizes, 1)
#| None => compute_sizes(new_nodes, shift - NUM_BITS)
#| }
#| Some(Node(new_nodes, new_sizes))
#| } else {
#| None
#| }
#| }
#| }
#| Empty => Some(Leaf([value]))
#| }
#| }
#| match worker(self, shift) {
#| Some(new_tree) => (new_tree, shift)
#| None => {
#| let new_branch = new_branch_left([value], shift)
#| let len = self.size(shift)
#| (
#| Node([self, new_branch], Some(FixedArray::from_array([len, 1 + len]))),
#| shift + NUM_BITS,
#| )
#| }
#| }
#|}
#|fn[A] Tree::each(self : Tree[A], f : (A) -> Unit raise?) -> Unit raise? {
#| match self {
#| Empty => ()
#| Leaf(l) => l.each(f)
#| Node(ns, _) => ns.each(t => t.each(f))
#| }
#|}
#|fn[A : Eq] Tree::contains(self : Tree[A], value : A) -> Bool {
#| match self {
#| Empty => false
#| Leaf(elems) =>
#| for elem in elems {
#| guard elem != value else { break true }
#| } nobreak {
#| false
#| }
#| Node(children, _) =>
#| for child in children {
#| guard !child.contains(value) else { break true }
#| } nobreak {
#| false
#| }
#| }
#|}
#|fn[A] Tree::eachi(
#| self : Tree[A],
#| f : (Int32, A) -> Unit raise?,
#| shift : Int32,
#| start : Int32,
#|) -> Unit raise? {
#| match self {
#| Empty => ()
#| Leaf(l) =>
#| for i, x in l {
#| f(start + i, x)
#| }
#| Node(ns, None) => {
#| let child_shift = shift - NUM_BITS
#| for child in ns; start = start {
#| child.eachi(f, child_shift, start)
#| continue start + (1 << shift)
#| }
#| }
#| Node(ns, Some(sizes)) => {
#| let child_shift = shift - NUM_BITS
#| for i, child in ns; offset = 0 {
#| child.eachi(f, child_shift, start + offset)
#| continue sizes[i]
#| }
#| }
#| }
#|}
#|fn[A, B] Tree::fold(
#| self : Tree[A],
#| acc : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| match self {
#| Empty => acc
#| Leaf(l) => l.fold(f, init=acc)
#| Node(n, _) => n.fold((acc, t) => t.fold(acc, f), init=acc)
#| }
#|}
#|fn[A, B] Tree::rev_fold(
#| self : Tree[A],
#| acc : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| match self {
#| Empty => acc
#| Leaf(l) => l.rev_fold(f, init=acc)
#| Node(n, _) => n.rev_fold((acc, t) => t.rev_fold(acc, f), init=acc)
#| }
#|}
#|fn[A, B] Tree::map(self : Tree[A], f : (A) -> B raise?) -> Tree[B] raise? {
#| match self {
#| Empty => Empty
#| Leaf(l) => Leaf(l.map(f))
#| Node(n, szs) =>
#| Node(FixedArray::makei(n.length(), i => n[i].map(f)), copy_sizes(szs))
#| }
#|}
#|fn[A] Tree::concat(
#| left : Tree[A],
#| left_shift : Int32,
#| right : Tree[A],
#| right_shift : Int32,
#| top : Bool,
#|) -> (Tree[A], Int32) {
#| if left_shift > right_shift {
#| let (c, c_shift) = Tree::concat(
#| left.right_child(),
#| left_shift - NUM_BITS,
#| right,
#| right_shift,
#| false,
#| )
#| guard! c_shift == left_shift
#| return rebalance(left, c, Empty, left_shift, top)
#| } else if right_shift > left_shift {
#| let (c, c_shift) = Tree::concat(
#| left,
#| left_shift,
#| right.left_child(),
#| right_shift - NUM_BITS,
#| false,
#| )
#| guard! c_shift == right_shift
#| return rebalance(Empty, c, right, right_shift, top)
#| } else if left_shift == 0 {
#| let left_elems = left.leaf_elements()
#| let right_elems = right.leaf_elements()
#| let left_len = left_elems.length()
#| let right_len = right_elems.length()
#| let len = left_len + right_len
#| if top && len <= BRANCHING_FACTOR {
#| return (
#| Leaf(
#| FixedArray::makei(len, (i : Int32) => {
#| if i < left_len {
#| left_elems[i]
#| } else {
#| right_elems[i - left_len]
#| }
#| }),
#| ),
#| 0,
#| )
#| } else {
#| return (
#| Node(
#| FixedArray::from_array([left, right]),
#| Some(FixedArray::from_array([left_len, len])),
#| ),
#| NUM_BITS,
#| )
#| }
#| } else {
#| let (c, c_shift) = Tree::concat(
#| left.right_child(),
#| left_shift - NUM_BITS,
#| right.left_child(),
#| right_shift - NUM_BITS,
#| false,
#| )
#| guard! c_shift == left_shift
#| guard! c_shift == right_shift
#| return rebalance(left, c, right, left_shift, top)
#| }
#|}
#|fn[A] rebalance(
#| left : Tree[A],
#| center : Tree[A],
#| right : Tree[A],
#| shift : Int32,
#| top : Bool,
#|) -> (Tree[A], Int32) {
#| let t = tri_merge(left, center, right) // t is a list of trees of (H-1) height
#| let (nc, nc_len) = redis_plan(t)
#| let new_t = redis(t, nc, nc_len, shift - NUM_BITS) // new_t is a list of trees of (H-1) height
#| guard! new_t.length() == nc_len
#| if nc_len <= BRANCHING_FACTOR {
#| let node = Node(new_t, compute_sizes(new_t, shift - NUM_BITS)) // node of H height
#| if !top {
#| return (Node(FixedArray::from_array([node]), None), shift + NUM_BITS)
#| } else {
#| return (node, shift)
#| }
#| } else {
#| let count = (new_t.length() + BRANCHING_FACTOR - 1) / BRANCHING_FACTOR
#| let new_children = FixedArray::makei(count, i => {
#| let lo = i * BRANCHING_FACTOR
#| let hi = min(lo + BRANCHING_FACTOR, new_t.length())
#| let chunk = FixedArray::makei(hi - lo, j => new_t[lo + j])
#| Node(chunk, compute_sizes(chunk, shift - NUM_BITS)) // height H
#| })
#| return (
#| Node(new_children, compute_sizes(new_children, shift)),
#| shift + NUM_BITS,
#| ) // return (H+1) height node
#| }
#|}
#|fn[A] tri_merge(
#| left : Tree[A],
#| center : Tree[A],
#| right : Tree[A],
#|) -> FixedArray[Tree[A]] {
#| if left.is_leaf() || !center.is_node() || right.is_leaf() {
#| abort("Unreachable: input to merge is invalid")
#| }
#| fn get_children(self : Tree[A]) -> FixedArray[Tree[A]] {
#| match self {
#| Node(children, _) => children
#| Empty => []
#| Leaf(_) => abort("Unreachable")
#| }
#| }
#| let left_children = get_children(left)
#| let center_children = get_children(center)
#| let right_children = get_children(right)
#| let left_len = left_children.length()
#| let left_len = if left_len == 0 { 0 } else { left_len - 1 }
#| let center_len = center_children.length()
#| let right_len = right_children.length()
#| let right_len = if right_len == 0 { 0 } else { right_len - 1 }
#| FixedArray::makei(left_len + center_len + right_len, i => {
#| if i < left_len {
#| left_children[i]
#| } else if i < left_len + center_len {
#| center_children[i - left_len]
#| } else if right_len > 0 {
#| right_children[1 + i - left_len - center_len]
#| } else {
#| abort("Unreachable")
#| }
#| })
#|}
#|fn[A] redis_plan(t : FixedArray[Tree[A]]) -> (FixedArray[Int32], Int32) {
#| let node_counts = FixedArray::makei(t.length(), i => t[i].local_size())
#| let total_nodes = node_counts.fold(init=0, (acc, x) => acc + x)
#| let opt_len = (total_nodes + BRANCHING_FACTOR - 1) / BRANCHING_FACTOR
#| let mut new_len = t.length()
#| let mut i = 0
#| while opt_len + e_max_2 < new_len {
#| while node_counts[i] > BRANCHING_FACTOR - e_max_2 {
#| i += 1
#| }
#| let mut remaining_nodes = node_counts[i]
#| while remaining_nodes > 0 {
#| let min_size = min(remaining_nodes + node_counts[i + 1], BRANCHING_FACTOR)
#| node_counts[i] = min_size
#| remaining_nodes = remaining_nodes + node_counts[i + 1] - min_size
#| i += 1
#| }
#| for j in i..<(new_len - 1) {
#| node_counts[j] = node_counts[j + 1]
#| }
#| new_len -= 1
#| i -= 1
#| }
#| return (node_counts, new_len)
#|}
#|fn[A] redis(
#| old_t : FixedArray[Tree[A]],
#| node_counts : FixedArray[Int32],
#| node_nums : Int32,
#| shift : Int32,
#|) -> FixedArray[Tree[A]] {
#| let old_len = old_t.length()
#| let new_t = FixedArray::make(node_nums, Empty)
#| let mut old_offset = 0
#| let mut j = 0 // the index of in the old tree
#| if shift == 0 {
#| let mut old_leaf_elems = ([] : FixedArray[_])
#| let mut old_leaf_len = 0
#| for i in 0.. Bool {
#| match self {
#| Leaf(_) => true
#| Node(_, None) => true
#| Node(_, Some(_)) => false
#| Empty => false
#| }
#|}
#|fn[A] compute_sizes(
#| children : FixedArray[Tree[A]],
#| shift : Int32,
#|) -> FixedArray[Int32]? {
#| let len = children.length()
#| let sizes = FixedArray::make(len, 0)
#| let mut sum = 0
#| let mut flag = true
#| let full_subtree_size = BRANCHING_FACTOR << shift
#| for i, child in children {
#| let sz = child.size(shift)
#| flag = flag && sz == full_subtree_size && child.is_radix()
#| sum += sz
#| sizes[i] = sum
#| }
#| if flag {
#| None
#| } else {
#| Some(sizes)
#| }
#|}
#|test "Debug for Tree" {
#| @debug.debug_inspect((Empty : Tree[Int32]), content="Empty")
#| @debug.debug_inspect(
#| Node([Leaf([4, 2])], Some([2])),
#| content=(
#| #|Node(
#| #| )]>,
#| #| Some(),
#| #|)
#| ),
#| )
#| @debug.debug_inspect(
#| Node([Empty, Leaf([42])], Some([0, 1])),
#| content=(
#| #|Node(
#| #| )]>,
#| #| Some(),
#| #|)
#| ),
#| )
#|}
),
"tree_append.mbt": (
#|fn append_sizes_last(
#| sizes : FixedArray[Int32]?,
#| delta : Int32,
#|) -> FixedArray[Int32]? {
#| match sizes {
#| Some(sizes) => {
#| let new_sizes = sizes.copy()
#| new_sizes[new_sizes.length() - 1] += delta
#| Some(new_sizes)
#| }
#| None => None
#| }
#|}
#|fn push_sizes_last(
#| sizes : FixedArray[Int32]?,
#| delta : Int32,
#|) -> FixedArray[Int32]? {
#| match sizes {
#| Some(sizes) =>
#| Some(immutable_push(sizes, delta + sizes[sizes.length() - 1]))
#| None => None
#| }
#|}
#|fn[T] radix_append_sizes(
#| children : FixedArray[Tree[T]],
#| child_shift : Int32,
#| delta : Int32,
#|) -> FixedArray[Int32]? {
#| if delta == BRANCHING_FACTOR {
#| None
#| } else {
#| compute_sizes(children, child_shift)
#| }
#|}
#|fn[T] Tree::append_right_leaf(
#| self : Tree[T],
#| shift : Int32,
#| leaf : FixedArray[T],
#|) -> (Tree[T], Int32) {
#| let delta = leaf.length()
#| guard delta > 0 else { return (self, shift) }
#| fn worker(
#| node : Tree[T],
#| shift : Int32,
#| leaf : FixedArray[T],
#| delta : Int32,
#| ) -> Tree[T]? {
#| match node {
#| Leaf(elems) => {
#| guard shift == 0 else {
#| abort("Unreachable: Leaf should only appear at shift 0")
#| }
#| if elems.length() + delta <= BRANCHING_FACTOR {
#| Some(Leaf(immutable_concat(elems, leaf)))
#| } else {
#| None
#| }
#| }
#| Node(nodes, sizes) => {
#| let len = nodes.length()
#| match worker(nodes[len - 1], shift - NUM_BITS, leaf, delta) {
#| Some(new_node) => {
#| let new_nodes = immutable_set(nodes, len - 1, new_node)
#| let new_sizes = match sizes {
#| Some(_) => append_sizes_last(sizes, delta)
#| None => radix_append_sizes(new_nodes, shift - NUM_BITS, delta)
#| }
#| Some(Node(new_nodes, new_sizes))
#| }
#| None =>
#| if len < BRANCHING_FACTOR {
#| let new_nodes = immutable_push(
#| nodes,
#| new_branch_left(leaf, shift - NUM_BITS),
#| )
#| let new_sizes = match sizes {
#| Some(_) => push_sizes_last(sizes, delta)
#| None => radix_append_sizes(new_nodes, shift - NUM_BITS, delta)
#| }
#| Some(Node(new_nodes, new_sizes))
#| } else {
#| None
#| }
#| }
#| }
#| Empty => Some(Leaf(leaf))
#| }
#| }
#| match worker(self, shift, leaf, delta) {
#| Some(new_tree) => (new_tree, shift)
#| None => {
#| let new_branch = new_branch_left(leaf, shift)
#| let self_radix = match self {
#| Node(_, None) => true
#| Leaf(elems) => elems.length() == BRANCHING_FACTOR
#| Node(_, Some(_)) => false
#| Empty =>
#| abort("Unreachable: Empty tree should have been handled in worker")
#| }
#| let new_sizes : FixedArray[Int32]? = if self_radix &&
#| delta == BRANCHING_FACTOR {
#| None
#| } else {
#| let len = self.size(shift)
#| Some([len, len + delta])
#| }
#| (Node([self, new_branch], new_sizes), shift + NUM_BITS)
#| }
#| }
#|}
#|fn[T] Tree::concat_with_suffix(
#| left : Tree[T],
#| left_shift : Int32,
#| suffix : FixedArray[T],
#| right : Tree[T],
#| right_shift : Int32,
#| top : Bool,
#|) -> (Tree[T], Int32) {
#| if suffix.is_empty() {
#| return Tree::concat(left, left_shift, right, right_shift, top)
#| }
#| if left_shift > right_shift {
#| let (c, c_shift) = Tree::concat_with_suffix(
#| left.right_child(),
#| left_shift - NUM_BITS,
#| suffix,
#| right,
#| right_shift,
#| false,
#| )
#| guard! c_shift == left_shift
#| rebalance(left, c, Empty, left_shift, top)
#| } else if right_shift > left_shift {
#| let (c, c_shift) = Tree::concat_with_suffix(
#| left,
#| left_shift,
#| suffix,
#| right.left_child(),
#| right_shift - NUM_BITS,
#| false,
#| )
#| guard! c_shift == right_shift
#| rebalance(Empty, c, right, right_shift, top)
#| } else if left_shift == 0 {
#| let leaves = FixedArray::from_array([left, Leaf(suffix), right])
#| let (node_counts, new_len) = redis_plan(leaves)
#| let new_children = redis(leaves, node_counts, new_len, 0)
#| if top && new_len == 1 {
#| (new_children[0], 0)
#| } else {
#| let node = Node(new_children, compute_sizes(new_children, 0))
#| (node, NUM_BITS)
#| }
#| } else {
#| let (c, c_shift) = Tree::concat_with_suffix(
#| left.right_child(),
#| left_shift - NUM_BITS,
#| suffix,
#| right.left_child(),
#| right_shift - NUM_BITS,
#| false,
#| )
#| guard! c_shift == left_shift
#| guard! c_shift == right_shift
#| rebalance(left, c, right, left_shift, top)
#| }
#|}
),
"tree_pop.mbt": (
#|fn[A] Tree::last_leaf_elements(self : Tree[A]) -> FixedArray[A] {
#| match self {
#| Leaf(leaf) => leaf
#| Node(children, _) => children[children.length() - 1].last_leaf_elements()
#| Empty => abort("Empty tree has no last leaf")
#| }
#|}
#|fn[A] Tree::pop_rightmost_leaf(self : Tree[A], shift : Int32) -> Tree[A] {
#| match self {
#| Empty | Leaf(_) => Empty
#| Node(children, _) => {
#| let last_index = children.length() - 1
#| let new_last = children[last_index].pop_rightmost_leaf(shift - NUM_BITS)
#| if new_last is Empty {
#| if last_index == 0 {
#| Empty
#| } else {
#| let new_children = immutable_slice(children, 0, last_index)
#| Node(new_children, compute_sizes(new_children, shift - NUM_BITS))
#| }
#| } else {
#| let new_children = immutable_set(children, last_index, new_last)
#| Node(new_children, compute_sizes(new_children, shift - NUM_BITS))
#| }
#| }
#| }
#|}
#|fn[A] Tree::shrink_top(self : Tree[A], shift : Int32) -> (Tree[A], Int32) {
#| let mut current_shift = shift
#| for tree = self {
#| match tree {
#| Node(children, _) if current_shift > 0 && children.length() == 1 => {
#| current_shift -= NUM_BITS
#| continue children[0]
#| }
#| Empty => break (Empty, 0)
#| current => break (current, current_shift)
#| }
#| }
#|}
),
"tree_slice.mbt": (
#|fn[A] Tree::slice_right(self : Tree[A], shift : Int32, end : Int32) -> Tree[A] {
#| guard end > 0 else { return Empty }
#| guard end < self.size(shift) else { return self }
#| match self {
#| Empty => Empty
#| Leaf(leaf) => Leaf(immutable_slice(leaf, 0, end))
#| Node(children, sizes) => {
#| let child_shift = shift - NUM_BITS
#| let child_index = match sizes {
#| Some(sizes) => get_branch_index(sizes, end - 1)
#| None => radix_indexing(end - 1, shift)
#| }
#| let child_start = match sizes {
#| Some(sizes) => if child_index == 0 { 0 } else { sizes[child_index - 1] }
#| None => child_index * (1 << shift)
#| }
#| let new_len = child_index + 1
#| let new_children = FixedArray::make(new_len, children[0])
#| for i in 0.. Tree[A] {
#| guard start > 0 else { return self }
#| match self {
#| Empty => Empty
#| Leaf(leaf) => Leaf(immutable_slice(leaf, start, leaf.length()))
#| Node(children, sizes) => {
#| let child_shift = shift - NUM_BITS
#| let child_index = match sizes {
#| Some(sizes) => get_branch_index(sizes, start)
#| None => radix_indexing(start, shift)
#| }
#| let child_start = match sizes {
#| Some(sizes) =>
#| if child_index == 0 {
#| start
#| } else {
#| start - sizes[child_index - 1]
#| }
#| None => start - child_index * (1 << shift)
#| }
#| let new_len = children.length() - child_index
#| let new_children = FixedArray::make(
#| new_len,
#| children[child_index].slice_left(child_shift, child_start),
#| )
#| for i in 1.. Bool {
#| self is Node(_, _)
#|}
#|fn[A] Tree::is_leaf(self : Tree[A]) -> Bool {
#| self is Leaf(_)
#|}
#|fn[A] Tree::right_child(self : Tree[A]) -> Tree[A] {
#| match self {
#| Node(children, _) => children[children.length() - 1]
#| Leaf(_) | Empty => abort("Should not get children on non-`Node`s")
#| }
#|}
#|fn[A] Tree::left_child(self : Tree[A]) -> Tree[A] {
#| match self {
#| Node(children, _) => children[0]
#| Leaf(_) | Empty => abort("Should not get children on non-`Node`s")
#| }
#|}
#|fn[A] Tree::leaf_elements(self : Tree[A]) -> FixedArray[A] {
#| guard self is Leaf(children) else {
#| abort("Should not call `get_leaf_elements` on non-leaf nodes")
#| }
#| children
#|}
#|fn[A] Tree::node_children(self : Tree[A]) -> FixedArray[Tree[A]] {
#| guard self is Node(children, _) else {
#| abort("Should not call `node_children` on non-`Node`s")
#| }
#| children
#|}
#|fn[A] Tree::local_size(self : Tree[A]) -> Int32 {
#| match self {
#| Empty => 0
#| Leaf(l) => l.length()
#| Node(children, _) => children.length()
#| }
#|}
#|fn[A] Tree::size(self : Tree[A], shift : Int32) -> Int32 {
#| match self {
#| Empty => 0
#| Leaf(l) => l.length()
#| Node(_, Some(sizes)) => sizes[sizes.length() - 1]
#| Node(children, None) => {
#| let len_1 = children.length() - 1
#| (len_1 << shift) + children[len_1].size(shift - NUM_BITS)
#| }
#| }
#|}
),
"types.mbt": (
#|struct Vector[A] {
#| tree : Tree[A]
#| tail : FixedArray[A]
#| size : Int32
#| shift : Int32
#|}
#|#derive(Eq, @debug.Debug)
#|#unsafe_cycle_free
#|priv enum Tree[A] {
#| Empty
#| Node(FixedArray[Tree[A]], FixedArray[Int32]?) // (Subtrees, Sizes of subtrees)
#| Leaf(FixedArray[A])
#|}
),
"utils.mbt": (
#|#owned(v)
#|fn[T] immutable_set(arr : FixedArray[T], i : Int32, v : T) -> FixedArray[T] {
#| let arr = arr.copy()
#| arr[i] = v
#| arr
#|}
#|#owned(val)
#|fn[T] immutable_push(arr : FixedArray[T], val : T) -> FixedArray[T] {
#| let len = arr.length()
#| let new_arr = FixedArray::make_and_blit(
#| arr,
#| allocate_len=len + 1,
#| init=val,
#| len~,
#| )
#| new_arr[len] = val
#| new_arr
#|}
#|fn[T] immutable_concat(
#| left : FixedArray[T],
#| right : FixedArray[T],
#|) -> FixedArray[T] {
#| let left_len = left.length()
#| let right_len = right.length()
#| if left_len == 0 {
#| right.copy()
#| } else if right_len == 0 {
#| left.copy()
#| } else {
#| FixedArray::makei(left_len + right_len, i => {
#| if i < left_len {
#| left[i]
#| } else {
#| right[i - left_len]
#| }
#| })
#| }
#|}
#|fn[T] immutable_slice(
#| arr : FixedArray[T],
#| start : Int32,
#| end : Int32,
#|) -> FixedArray[T] {
#| let len = end - start
#| if len <= 0 {
#| []
#| } else {
#| FixedArray::makei(len, i => arr[start + i])
#| }
#|}
#|fn shr_as_uint(x : Int32, y : Int32) -> Int32 {
#| (x.reinterpret_as_uint() >> y).reinterpret_as_int()
#|}
#|fn radix_indexing(index : Int32, shift : Int32) -> Int32 {
#| shr_as_uint(index, shift) & BITMASK
#|}
#|fn get_branch_index(sizes : FixedArray[Int32], index : Int32) -> Int32 {
#| let lo = for lo = 0, hi = sizes.length(); LINEAR_THRESHOLD < hi - lo; {
#| let mid = (lo + hi) / 2
#| if sizes[mid] <= index {
#| continue mid, hi
#| } else {
#| continue lo, mid
#| }
#| } nobreak {
#| lo
#| }
#| for lo = lo; sizes[lo] <= index; {
#| continue lo + 1
#| } nobreak {
#| lo
#| }
#|}
#|fn copy_sizes(sizes : FixedArray[Int32]?) -> FixedArray[Int32]? {
#| sizes.map(sizes => sizes.copy())
#|}
#|fn min(a : Int32, b : Int32) -> Int32 {
#| if a < b {
#| a
#| } else {
#| b
#| }
#|}
),
"utils_wbtest2.mbt": (
#|test "immutable_set" {
#| let arr = FixedArray::from_array([1, 2, 3, 4, 5])
#| let new_arr = immutable_set(arr, 2, 10)
#| inspect(new_arr[2], content="10")
#| inspect(arr[2], content="3") // Original array unchanged
#| inspect(new_arr.length(), content="5")
#|}
#|test "immutable_push" {
#| let arr = FixedArray::from_array([1, 2, 3])
#| let new_arr = immutable_push(arr, 4)
#| inspect(new_arr.length(), content="4")
#| inspect(new_arr[3], content="4")
#| inspect(arr.length(), content="3") // Original array unchanged
#|}
#|test "shr_as_uint" {
#| inspect(shr_as_uint(16, 2), content="4")
#| inspect(shr_as_uint(8, 1), content="4")
#| inspect(shr_as_uint(-1, 1), content="2147483647") // Handle negative numbers as unsigned
#|}
#|test "radix_indexing" {
#| inspect(radix_indexing(35, 5), content="1") // 35 >> 5 = 1, 1 & 31 = 1
#| inspect(radix_indexing(100, 5), content="3") // 100 >> 5 = 3, 3 & 31 = 3
#| inspect(radix_indexing(7, 5), content="0") // 7 >> 5 = 0, 0 & 31 = 0
#|}
#|test "get_branch_index" {
#| let sizes = FixedArray::from_array([3, 6, 10, 15])
#| inspect(get_branch_index(sizes, 2), content="0") // Index 2 is in first branch (contains indexes 0-2)
#| inspect(get_branch_index(sizes, 5), content="1") // Index 5 is in second branch (contains indexes 3-5)
#| inspect(get_branch_index(sizes, 8), content="2") // Index 8 is in third branch (contains indexes 6-9)
#|}
#|test "copy_sizes with Some" {
#| let original = FixedArray::from_array([1, 2, 3])
#| let sizes = Some(original)
#| let copied = copy_sizes(sizes)
#| match copied {
#| Some(copied_arr) => {
#| inspect(copied_arr.length(), content="3")
#| inspect(copied_arr[0], content="1")
#| inspect(copied_arr[1], content="2")
#| inspect(copied_arr[2], content="3")
#| original[0] = 100
#| inspect(copied_arr[0], content="1") // Should still be 1
#| }
#| None => inspect("Should not be None", content="error")
#| }
#|}
#|test "copy_sizes with None" {
#| let sizes : FixedArray[Int32]? = None
#| let copied = copy_sizes(sizes)
#| match copied {
#| None => inspect("None copied correctly", content="None copied correctly")
#| Some(_) => inspect("Should be None", content="error")
#| }
#|}
#|test "min function" {
#| inspect(min(5, 3), content="3")
#| inspect(min(1, 10), content="1")
#| inspect(min(-5, 0), content="-5")
#| inspect(min(42, 42), content="42")
#|}
),
"vector.mbt": (
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[A] Vector::empty() -> Vector[A] {
#| make_t(Tree::empty(), [], 0, 0)
#|}
#|pub impl[A] Default for Vector[A] with fn default() {
#| Vector::empty()
#|}
#|#as_free_fn
#|#owned(value)
#|pub fn[A] Vector::singleton(value : A) -> Vector[A] {
#| make_t(Tree::empty(), [value], 1, 0)
#|}
#|#as_free_fn
#|#owned(value)
#|pub fn[A] Vector::make(len : Int32, value : A) -> Vector[A] {
#| guard len > 0 else { empty() }
#| let tail_len = tail_len_of_size(len)
#| let tree_len = len - tail_len
#| let tail = FixedArray::make(tail_len, value)
#| let (tree, shift) = if tree_len == 0 {
#| (Tree::empty(), 0)
#| } else {
#| let leaves = Array::make(
#| tree_len / BRANCHING_FACTOR,
#| FixedArray::make(BRANCHING_FACTOR, value),
#| )
#| let shift = shift_of_leaf_count(leaves.length())
#| (from_leaves(leaves, shift), shift)
#| }
#| make_t(tree, tail, len, shift)
#|}
#|#as_free_fn
#|pub fn[A] Vector::makei(
#| len : Int32,
#| f : (Int32) -> A raise?,
#|) -> Vector[A] raise? {
#| guard len > 0 else { empty() }
#| let tail_len = tail_len_of_size(len)
#| let tree_len = len - tail_len
#| let (tree, shift) = if tree_len == 0 {
#| (Tree::empty(), 0)
#| } else {
#| let quot = tree_len / BRANCHING_FACTOR
#| let leaves : Array[FixedArray[A]] = Array::make(quot, [])
#| for k in 0.. {
#| f(k * BRANCHING_FACTOR + i)
#| })
#| }
#| let shift = shift_of_leaf_count(leaves.length())
#| (from_leaves(leaves, shift), shift)
#| }
#| let tail = FixedArray::makei(tail_len, i => f(tree_len + i))
#| make_t(tree, tail, len, shift)
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[A] Vector::Vector(arr : ArrayView[A]) -> Vector[A] {
#| guard arr.length() > 0 else { return empty() }
#| Vector::makei(arr.length(), i => arr[i])
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A] Vector::from_iter(iter : Iter[A]) -> Vector[A] {
#| let mut buf : FixedArray[A] = []
#| let mut index = 0
#| let leaves = []
#| while iter.next() is Some(x) {
#| if index == 0 {
#| buf = FixedArray::make(BRANCHING_FACTOR, x)
#| index += 1
#| } else if index < BRANCHING_FACTOR {
#| buf[index] = x
#| index += 1
#| } else {
#| leaves.push(buf)
#| index = 1
#| buf = FixedArray::make(BRANCHING_FACTOR, x)
#| }
#| }
#| if index == 0 {
#| return empty()
#| }
#| let tail = if index == BRANCHING_FACTOR {
#| if leaves.is_empty() {
#| buf
#| } else {
#| leaves.push(buf)
#| []
#| }
#| } else {
#| FixedArray::make_and_blit(buf, allocate_len=index, init=buf[0], len=index)
#| }
#| let tree_len = tree_len_of_leaf_count(leaves.length())
#| let (tree, shift) = if tree_len == 0 {
#| (Tree::empty(), 0)
#| } else {
#| let shift = shift_of_leaf_count(leaves.length())
#| (from_leaves(leaves, shift), shift)
#| }
#| make_t(tree, tail, checked_size_add(tree_len, tail.length()), shift)
#|}
#|pub fn[A] Vector::to_array(self : Vector[A]) -> Array[A] {
#| if self.is_empty() {
#| []
#| } else {
#| let arr = Array::make(self.length(), self[0])
#| self.eachi((i, v) => arr[i] = v)
#| arr
#| }
#|}
#|pub fn[A] Vector::is_empty(self : Vector[A]) -> Bool {
#| self.size == 0
#|}
#|pub fn[A] Vector::length(self : Vector[A]) -> Int32 {
#| self.size
#|}
#|fn[A] Vector::tail_offset(self : Vector[A]) -> Int32 {
#| self.size - self.tail.length()
#|}
#|#alias("_[_]")
#|pub fn[A] Vector::at(self : Vector[A], index : Int32) -> A {
#| guard! index >= 0 && index < self.size
#| let tail_offset = self.tail_offset()
#| if index >= tail_offset {
#| self.tail[index - tail_offset]
#| } else {
#| self.tree.get(index, self.shift)
#| }
#|}
#|pub fn[A] Vector::get(self : Vector[A], index : Int32) -> A? {
#| guard 0 <= index && index < self.size else { None }
#| Some(self[index])
#|}
#|pub fn[A] Vector::peek(self : Vector[A]) -> A? {
#| if self.size == 0 {
#| None
#| } else {
#| Some(self[self.size - 1])
#| }
#|}
#|pub fn[A : Eq] Vector::contains(self : Vector[A], value : A) -> Bool {
#| if self.tree.contains(value) {
#| return true
#| }
#| self.tail.contains(value)
#|}
#|#owned(value)
#|pub fn[A] Vector::set(self : Vector[A], index : Int32, value : A) -> Vector[A] {
#| guard! index >= 0 && index < self.size
#| let tail_len = self.tail.length()
#| if tail_len == 0 {
#| make_t(
#| self.tree.set(index, self.shift, value),
#| self.tail,
#| self.size,
#| self.shift,
#| )
#| } else {
#| let tail_offset = self.tail_offset()
#| if index >= tail_offset {
#| make_t(
#| self.tree,
#| immutable_set(self.tail, index - tail_offset, value),
#| self.size,
#| self.shift,
#| )
#| } else {
#| make_t(
#| self.tree.set(index, self.shift, value),
#| self.tail,
#| self.size,
#| self.shift,
#| )
#| }
#| }
#|}
#|#owned(value)
#|pub fn[A] Vector::push(self : Vector[A], value : A) -> Vector[A] {
#| let size = checked_size_add(self.size, 1)
#| if self.tail.length() < BRANCHING_FACTOR {
#| make_t(self.tree, immutable_push(self.tail, value), size, self.shift)
#| } else {
#| let (tree, shift) = if self.tree is Empty {
#| (Leaf(self.tail), 0)
#| } else {
#| self.tree.append_right_leaf(self.shift, self.tail)
#| }
#| make_t(tree, [value], size, shift)
#| }
#|}
#|pub fn[A] Vector::pop(self : Vector[A]) -> Vector[A]? {
#| if self.size == 0 {
#| None
#| } else if self.size == 1 {
#| Some(empty())
#| } else if self.tail.length() > 1 {
#| Some(
#| make_t(
#| self.tree,
#| immutable_slice(self.tail, 0, self.tail.length() - 1),
#| self.size - 1,
#| self.shift,
#| ),
#| )
#| } else if self.tail.length() == 1 {
#| let new_tail = self.tree.last_leaf_elements()
#| let (tree, shift) = self.tree
#| .pop_rightmost_leaf(self.shift)
#| .shrink_top(self.shift)
#| Some(make_t(tree, new_tail, self.size - 1, shift))
#| } else {
#| let last_leaf = self.tree.last_leaf_elements()
#| if last_leaf.length() > 1 {
#| let (tree, shift) = self.tree
#| .pop_rightmost_leaf(self.shift)
#| .shrink_top(self.shift)
#| Some(
#| make_t(
#| tree,
#| immutable_slice(last_leaf, 0, last_leaf.length() - 1),
#| self.size - 1,
#| shift,
#| ),
#| )
#| } else {
#| let stripped = self.tree.pop_rightmost_leaf(self.shift)
#| let (promoted_tree, promoted_shift) = stripped.shrink_top(self.shift)
#| if promoted_tree is Empty {
#| Some(empty())
#| } else {
#| let new_tail = promoted_tree.last_leaf_elements()
#| let (tree, shift) = promoted_tree
#| .pop_rightmost_leaf(promoted_shift)
#| .shrink_top(promoted_shift)
#| Some(make_t(tree, new_tail, self.size - 1, shift))
#| }
#| }
#| }
#|}
#|pub fn[A] Vector::concat(self : Vector[A], other : Vector[A]) -> Vector[A] {
#| if self.is_empty() {
#| return other
#| }
#| if other.is_empty() {
#| return self
#| }
#| let size = checked_size_add(self.size, other.size)
#| if other.tree is Empty {
#| let combined_tail_len = self.tail.length() + other.tail.length()
#| if combined_tail_len <= BRANCHING_FACTOR {
#| return make_t(
#| self.tree,
#| immutable_concat(self.tail, other.tail),
#| size,
#| self.shift,
#| )
#| } else if self.tail.is_empty() {
#| return make_t(self.tree, other.tail, size, self.shift)
#| } else {
#| let (tree, shift) = self.normalize_tree()
#| return make_t(tree, other.tail, size, shift)
#| }
#| }
#| let (tree, shift) = if self.tree is Empty {
#| Tree::concat(Leaf(self.tail), 0, other.tree, other.shift, true)
#| } else if self.tail.is_empty() {
#| Tree::concat(self.tree, self.shift, other.tree, other.shift, true)
#| } else {
#| Tree::concat_with_suffix(
#| self.tree,
#| self.shift,
#| self.tail,
#| other.tree,
#| other.shift,
#| true,
#| )
#| }
#| make_t(tree, other.tail, size, shift)
#|}
#|pub fn[A] Vector::split(
#| self : Vector[A],
#| index : Int32,
#|) -> (Vector[A], Vector[A]) {
#| guard 0 <= index && index <= self.size else { abort("Index out of bounds") }
#| (self.slice_unchecked(0, index), self.slice_unchecked(index, self.size))
#|}
#|pub fn[A] Vector::take(self : Vector[A], count : Int32) -> Vector[A] {
#| let end = if count <= 0 { 0 } else { min(count, self.size) }
#| self.slice_unchecked(0, end)
#|}
#|pub fn[A] Vector::drop(self : Vector[A], count : Int32) -> Vector[A] {
#| let start = if count <= 0 { 0 } else { min(count, self.size) }
#| self.slice_unchecked(start, self.size)
#|}
#|pub fn[A] Vector::slice(
#| self : Vector[A],
#| start : Int32,
#| end : Int32,
#|) -> Vector[A] {
#| guard start <= end else { abort("start index greater than end index") }
#| guard 0 <= start && end <= self.size else { abort("Index out of bounds") }
#| self.slice_unchecked(start, end)
#|}
#|pub impl[A] Add for Vector[A] with fn add(self, other) {
#| self.concat(other)
#|}
#|#alias(iterator, deprecated)
#|pub fn[A] Vector::iter(self : Vector[A]) -> Iter[A] {
#| let mut in_tree = true
#| let mut curr_tree = self.tree
#| let mut curr_index = 0
#| let mut curr_leaf : FixedArray[A] = []
#| let mut leaf_index = 0
#| let mut leaf_len = 0
#| let parents = []
#| let mut tail_index = 0
#| let tail_len = self.tail.length()
#| Iter(
#| fn() {
#| if leaf_index < leaf_len {
#| let elem = curr_leaf.unsafe_get(leaf_index)
#| leaf_index += 1
#| return Some(elem)
#| }
#| let tree_result = if in_tree {
#| for tree = curr_tree {
#| match tree {
#| Node(children, _) as t if curr_index < children.length() => {
#| let child = children.unsafe_get(curr_index)
#| parents.push((t, curr_index + 1))
#| curr_tree = child
#| curr_index = 0
#| continue child
#| }
#| Leaf(elems) if curr_index < elems.length() => {
#| let len = elems.length()
#| let elem = elems.unsafe_get(curr_index)
#| curr_leaf = elems
#| leaf_len = len
#| leaf_index = curr_index + 1
#| curr_index = len
#| break Some(elem)
#| }
#| _ if parents.pop() is Some((parent_tree, parent_index)) => {
#| curr_tree = parent_tree
#| curr_index = parent_index
#| continue parent_tree
#| }
#| _ => {
#| in_tree = false
#| break None
#| }
#| }
#| }
#| } else {
#| None
#| }
#| match tree_result {
#| Some(_) => tree_result
#| None =>
#| if tail_index < tail_len {
#| let elem = self.tail.unsafe_get(tail_index)
#| tail_index += 1
#| Some(elem)
#| } else {
#| None
#| }
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|pub fn[A] Vector::each(self : Vector[A], f : (A) -> Unit raise?) -> Unit raise? {
#| self.tree.each(f)
#| self.tail.each(f)
#|}
#|pub fn[A] Vector::eachi(
#| self : Vector[A],
#| f : (Int32, A) -> Unit raise?,
#|) -> Unit raise? {
#| self.tree.eachi(f, self.shift, 0)
#| let tail_offset = self.tail_offset()
#| for i, x in self.tail {
#| f(tail_offset + i, x)
#| }
#|}
#|#alias(fold_left, deprecated)
#|pub fn[A, B] Vector::fold(
#| self : Vector[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| let acc = self.tree.fold(init, f)
#| self.tail.fold(f, init=acc)
#|}
#|#alias(fold_right, deprecated)
#|pub fn[A, B] Vector::rev_fold(
#| self : Vector[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| let acc = self.tail.rev_fold(f, init~)
#| self.tree.rev_fold(acc, f)
#|}
#|pub fn[A, B] Vector::map(
#| self : Vector[A],
#| f : (A) -> B raise?,
#|) -> Vector[B] raise? {
#| make_t(self.tree.map(f), self.tail.map(f), self.size, self.shift)
#|}
#|pub fn[A] Vector::filter(
#| self : Vector[A],
#| f : (A) -> Bool raise?,
#|) -> Vector[A] raise? {
#| let arr : Array[A] = Array(capacity=self.size)
#| self.each(value => if f(value) { arr.push(value) })
#| from_array(arr)
#|}
#|pub fn[A] Vector::rev(self : Vector[A]) -> Vector[A] {
#| if self.size == 0 {
#| empty()
#| } else {
#| let arr = Array::make(self.size, self[0])
#| self.eachi((i, value) => arr[self.size - 1 - i] = value)
#| from_array(arr)
#| }
#|}
#|pub impl[A : Hash] Hash for Vector[A] with fn hash_combine(self, hasher) {
#| for e in self {
#| hasher.combine(e)
#| }
#|}
#|pub impl[A : Eq] Eq for Vector[A] with fn equal(self, other) {
#| if physical_equal(self, other) {
#| true
#| } else if self.size != other.size {
#| false
#| } else {
#| let left = self.iter()
#| let right = other.iter()
#| for _ in 0.. value
#| ]
#|}
#|pub impl[A : @json.FromJson] @json.FromJson for Vector[A] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(arr) else {
#| raise path.error("@immut/vector.from_json: expected array")
#| }
#| let len = arr.length()
#| guard len != 0 else { return empty() }
#| let values : Array[A] = Array(capacity=len)
#| for i, value in arr {
#| values.push(A::from_json(value, path.add_index(i)))
#| }
#| Vector::from_array(values)
#|}
#|pub impl[A : Compare] Compare for Vector[A] with fn compare(self, other) {
#| let len_self = self.length()
#| let len_other = other.length()
#| let cmp = len_self.compare(len_other)
#| guard cmp is 0 else { return cmp }
#| for i in 0.. Int32 {
#| let required = size + added
#| guard required >= size else { abort("Vector capacity overflow") }
#| required
#|}
#|#owned(tree, tail)
#|fn[A] make_t(
#| tree : Tree[A],
#| tail : FixedArray[A],
#| size : Int32,
#| shift : Int32,
#|) -> Vector[A] {
#| { tree, tail, size, shift, }
#|}
#|fn[A] from_leaves(leaves : ArrayView[FixedArray[A]], shift : Int32) -> Tree[A] {
#| guard shift > 0 else { return Leaf(leaves[0]) }
#| let child_shift = shift - NUM_BITS
#| let leaves_per_child = 1 << child_shift
#| let full = leaves.length() / leaves_per_child
#| let len = if leaves.length() % leaves_per_child == 0 {
#| full
#| } else {
#| full + 1
#| }
#| let arr = FixedArray::makei(len, i => {
#| let lo = i * leaves_per_child
#| let hi = min(lo + leaves_per_child, leaves.length())
#| from_leaves(leaves[lo:hi], child_shift)
#| })
#| Node(arr, None)
#|}
#|fn shift_of_leaf_count(leaf_count : Int32) -> Int32 {
#| guard leaf_count >= 0 else { abort("Vector capacity overflow") }
#| for capacity = 1, shift = 0; capacity < leaf_count; {
#| let next = capacity * BRANCHING_FACTOR
#| guard next > capacity else { abort("Vector capacity overflow") }
#| continue next, shift + NUM_BITS
#| } nobreak {
#| shift
#| }
#|}
#|fn tree_len_of_leaf_count(leaf_count : Int32) -> Int32 {
#| guard leaf_count >= 0 else { abort("Vector capacity overflow") }
#| let len = leaf_count * BRANCHING_FACTOR
#| guard len / BRANCHING_FACTOR == leaf_count else {
#| abort("Vector capacity overflow")
#| }
#| len
#|}
#|fn tail_len_of_size(size : Int32) -> Int32 {
#| if size <= BRANCHING_FACTOR {
#| size
#| } else {
#| size % BRANCHING_FACTOR
#| }
#|}
#|fn[A] Vector::normalize_tree(self : Vector[A]) -> (Tree[A], Int32) {
#| if self.size == 0 {
#| (Tree::empty(), 0)
#| } else if self.tail.is_empty() {
#| (self.tree, self.shift)
#| } else if self.tail_offset() == 0 {
#| (Leaf(self.tail), 0)
#| } else if self.tail.length() == 1 {
#| self.tree.push_end(self.shift, self.tail[0])
#| } else {
#| self.tree.append_right_leaf(self.shift, self.tail)
#| }
#|}
#|fn[A] Vector::slice_unchecked(
#| self : Vector[A],
#| start : Int32,
#| end : Int32,
#|) -> Vector[A] {
#| let len = end - start
#| if len == 0 {
#| empty()
#| } else if start == 0 && end == self.size {
#| self
#| } else {
#| let tail_offset = self.tail_offset()
#| if start >= tail_offset {
#| make_t(
#| Empty,
#| immutable_slice(self.tail, start - tail_offset, end - tail_offset),
#| len,
#| 0,
#| )
#| } else {
#| let tree_end = min(end, tail_offset)
#| let sliced_tree = if tree_end == tail_offset {
#| self.tree
#| } else {
#| self.tree.slice_right(self.shift, tree_end)
#| }
#| let sliced_tree = if start == 0 {
#| sliced_tree
#| } else {
#| sliced_tree.slice_left(self.shift, start)
#| }
#| let (tree, shift) = sliced_tree.shrink_top(self.shift)
#| let tail = if end > tail_offset {
#| immutable_slice(self.tail, 0, end - tail_offset)
#| } else {
#| []
#| }
#| make_t(tree, tail, len, shift)
#| }
#| }
#|}
),
},
)
///|
let moonbitlang_core_immut_vector_map_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/immut/vector_map",
deps={},
files={
"extends.mbt": (
#|pub extend VectorMap with Eq::{equal}
#|pub extend VectorMap with Hash::{hash}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend VectorMap with Default::{default}
#|#deprecated("Use `Json(x)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend VectorMap with @json.ToJson::{to_json}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend VectorMap with @debug.Debug::{to_repr}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend VectorMap with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend VectorMap with Hash::{hash_combine}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend VectorMap with @json.FromJson::{from_json}
),
"iter.mbt": (
#|pub fn[K, V] VectorMap::each(
#| self : VectorMap[K, V],
#| f : (K, V) -> Unit raise?,
#|) -> Unit raise? {
#| self.spine.each(entry => if entry is Some((k, v)) { f(k, v) })
#|}
#|pub fn[K, V] VectorMap::eachi(
#| self : VectorMap[K, V],
#| f : (Int32, K, V) -> Unit raise?,
#|) -> Unit raise? {
#| let mut i = 0
#| self.spine.each(entry => {
#| if entry is Some((k, v)) {
#| f(i, k, v)
#| i += 1
#| }
#| })
#|}
#|#alias(iterator, deprecated)
#|pub fn[K, V] VectorMap::iter(self : VectorMap[K, V]) -> Iter[(K, V)] {
#| self.spine.iter().filter_map(entry => entry)
#|}
#|#alias(iterator2, deprecated)
#|pub fn[K, V] VectorMap::iter2(self : VectorMap[K, V]) -> Iter2[K, V] {
#| self.iter()
#|}
#|pub fn[K, V] VectorMap::keys(self : VectorMap[K, V]) -> Iter[K] {
#| self.iter().map(kv => kv.0)
#|}
#|#alias(elems, deprecated)
#|pub fn[K, V] VectorMap::values(self : VectorMap[K, V]) -> Iter[V] {
#| self.iter().map(kv => kv.1)
#|}
#|pub fn[K, V, A] VectorMap::fold(
#| self : VectorMap[K, V],
#| init~ : A,
#| f : (A, K, V) -> A raise?,
#|) -> A raise? {
#| self.spine.fold(init~, (acc, entry) => {
#| match entry {
#| Some((k, v)) => f(acc, k, v)
#| None => acc
#| }
#| })
#|}
#|pub fn[K, V] VectorMap::to_array(self : VectorMap[K, V]) -> Array[(K, V)] {
#| let result : Array[(K, V)] = Array(capacity=self.size)
#| self.each((k, v) => result.push((k, v)))
#| result
#|}
#|#alias(map_with_key, deprecated)
#|pub fn[K, V, A] VectorMap::map(
#| self : VectorMap[K, V],
#| f : (K, V) -> A raise?,
#|) -> VectorMap[K, A] raise? {
#| {
#| index: self.index,
#| spine: self.spine.map(entry => {
#| match entry {
#| Some((k, v)) => Some((k, f(k, v)))
#| None => None
#| }
#| }),
#| size: self.size,
#| }
#|}
#|#alias(filter_with_key, deprecated)
#|pub fn[K, V] VectorMap::filter(
#| self : VectorMap[K, V],
#| pred : (K, V) -> Bool raise?,
#|) -> VectorMap[K, V] raise? {
#| let remap = FixedArray::make(self.spine.length(), -1)
#| let kept : Array[(K, V)?] = []
#| self.spine.eachi((slot, entry) => {
#| if entry is Some((k, v)) {
#| if pred(k, v) {
#| remap[slot] = kept.length()
#| kept.push(entry)
#| }
#| }
#| })
#| if kept.length() == self.size {
#| return self
#| }
#| {
#| index: self.index
#| .filter((_, slot) => remap[slot] >= 0)
#| .map((_, slot) => remap[slot]),
#| spine: @vector.from_iter(kept.iter()),
#| size: kept.length(),
#| }
#|}
),
"traits_impl.mbt": (
#|pub impl[K : Eq, V : Eq] Eq for VectorMap[K, V] with fn equal(self, other) {
#| if physical_equal(self, other) {
#| return true
#| }
#| guard self.size == other.size else { return false }
#| let left = self.iter()
#| let right = other.iter()
#| for _ in 0.. (Repr(k), Repr(v)) ]),
#| )
#|}
#|pub impl[K : @json.ToJson, V : @json.ToJson] @json.ToJson for VectorMap[K, V] with fn to_json(
#| self,
#|) {
#| [
#| for k, v in self => ([k, v] : @json.Json)
#| ]
#|}
#|pub impl[K : @json.FromJson + Eq + Hash, V : @json.FromJson] @json.FromJson for VectorMap[
#| K,
#| V,
#|] with fn from_json(json, path) {
#| guard json is Array(pairs) else {
#| raise path.error("@immut/vector_map.from_json: expected array")
#| }
#| let entries : Array[(K, V)] = Array(capacity=pairs.length())
#| for i, pair in pairs {
#| let entry_path = path.add_index(i)
#| guard pair is Array([key, value]) else {
#| raise JsonDecodeError(
#| (entry_path, "@immut/vector_map.from_json: expected [key, value] pair"),
#| )
#| }
#| entries.push(
#| (
#| @json.FromJson::from_json(key, entry_path.add_index(0)),
#| @json.FromJson::from_json(value, entry_path.add_index(1)),
#| ),
#| )
#| }
#| from_pairs(entries.iter())
#|}
),
"types.mbt": (
#|struct VectorMap[K, V] {
#| index : @hashmap.HashMap[K, Int32]
#| spine : @vector.Vector[(K, V)?]
#| size : Int32
#|}
#|const MIN_COMPACT_LENGTH : Int32 = 32
),
"vector_map.mbt": (
#|#alias(new, deprecated)
#|#as_free_fn(new, deprecated)
#|#as_free_fn
#|pub fn[K, V] VectorMap::empty() -> VectorMap[K, V] {
#| { index: @hashmap.empty(), spine: @vector.empty(), size: 0, }
#|}
#|pub impl[K, V] Default for VectorMap[K, V] with fn default() {
#| VectorMap::empty()
#|}
#|#as_free_fn
#|pub fn[K : Hash, V] VectorMap::singleton(key : K, value : V) -> VectorMap[K, V] {
#| {
#| index: @hashmap.singleton(key, 0),
#| spine: @vector.singleton(Some((key, value))),
#| size: 1,
#| }
#|}
#|pub fn[K : Eq + Hash, V] VectorMap::VectorMap(
#| arr : ArrayView[(K, V)],
#|) -> VectorMap[K, V] {
#| guard arr.length() > 0 else { return empty() }
#| from_pairs(arr.iter())
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Eq + Hash, V] VectorMap::from_iter(
#| iter : Iter[(K, V)],
#|) -> VectorMap[K, V] {
#| from_pairs(iter)
#|}
#|fn[K : Eq + Hash, V] from_pairs(iter : Iter[(K, V)]) -> VectorMap[K, V] {
#| let entries : Array[(K, V)] = []
#| let mut index : @hashmap.HashMap[K, Int32] = @hashmap.empty()
#| for kv in iter {
#| let (key, value) = kv
#| match index.get(key) {
#| Some(slot) => entries[slot] = (entries[slot].0, value)
#| None => {
#| index = index.insert(key, entries.length())
#| entries.push((key, value))
#| }
#| }
#| }
#| {
#| index,
#| spine: @vector.from_iter(entries.iter().map(kv => Some(kv))),
#| size: entries.length(),
#| }
#|}
#|#alias(size, deprecated)
#|pub fn[K, V] VectorMap::length(self : VectorMap[K, V]) -> Int32 {
#| self.size
#|}
#|pub fn[K, V] VectorMap::is_empty(self : VectorMap[K, V]) -> Bool {
#| self.size == 0
#|}
#|#alias(find, deprecated)
#|pub fn[K : Eq + Hash, V] VectorMap::get(self : VectorMap[K, V], key : K) -> V? {
#| guard self.index.get(key) is Some(slot) else { return None }
#| guard self.spine.get(slot) is Some(Some((_, value))) else { return None }
#| Some(value)
#|}
#|#alias("_[_]")
#|pub fn[K : Eq + Hash, V] VectorMap::at(self : VectorMap[K, V], key : K) -> V {
#| guard! self.get(key) is Some(value)
#| value
#|}
#|pub fn[K : Eq + Hash, V] VectorMap::contains(
#| self : VectorMap[K, V],
#| key : K,
#|) -> Bool {
#| self.index.contains(key)
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn[K : Eq + Hash, V] VectorMap::insert(
#| self : VectorMap[K, V],
#| key : K,
#| value : V,
#|) -> VectorMap[K, V] {
#| match self.index.get(key) {
#| Some(slot) => {
#| guard! self.spine.get(slot) is Some(Some((old_key, _)))
#| {
#| index: self.index,
#| spine: self.spine.set(slot, Some((old_key, value))),
#| size: self.size,
#| }
#| }
#| None => {
#| let map = {
#| index: self.index.insert(key, self.spine.length()),
#| spine: self.spine.push(Some((key, value))),
#| size: self.size + 1,
#| }
#| if map.should_compact() {
#| map.compact()
#| } else {
#| map
#| }
#| }
#| }
#|}
#|pub fn[K : Eq + Hash, V] VectorMap::remove(
#| self : VectorMap[K, V],
#| key : K,
#|) -> VectorMap[K, V] {
#| guard self.index.get(key) is Some(slot) else { return self }
#| let map = {
#| index: self.index.remove(key),
#| spine: trim_trailing(self.spine.set(slot, None)),
#| size: self.size - 1,
#| }
#| if map.should_compact() {
#| map.compact()
#| } else {
#| map
#| }
#|}
#|pub fn[K : Eq + Hash, V] VectorMap::update(
#| self : VectorMap[K, V],
#| key : K,
#| f : (V?) -> V? raise?,
#|) -> VectorMap[K, V] raise? {
#| match f(self.get(key)) {
#| Some(value) => self.insert(key, value)
#| None => self.remove(key)
#| }
#|}
#|fn[K, V] trim_trailing(
#| spine : @vector.Vector[(K, V)?],
#|) -> @vector.Vector[(K, V)?] {
#| for s = spine {
#| guard s.peek() is Some(None) else { break s }
#| guard s.pop() is Some(rest) else { break s }
#| continue rest
#| }
#|}
#|fn[K, V] VectorMap::should_compact(self : VectorMap[K, V]) -> Bool {
#| let length = self.spine.length()
#| length >= MIN_COMPACT_LENGTH && length - self.size > self.size
#|}
#|fn[K, V] VectorMap::compact(self : VectorMap[K, V]) -> VectorMap[K, V] {
#| let remap = FixedArray::make(self.spine.length(), 0)
#| let kept : Array[(K, V)?] = Array(capacity=self.size)
#| self.spine.eachi((slot, entry) => {
#| if entry is Some(_) {
#| remap[slot] = kept.length()
#| kept.push(entry)
#| }
#| })
#| {
#| index: self.index.map((_, slot) => remap[slot]),
#| spine: @vector.from_iter(kept.iter()),
#| size: kept.length(),
#| }
#|}
),
},
)
///|
let moonbitlang_core_int_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/int",
deps={},
files={
"int.mbt": (
#|pub const MAX_VALUE = 2147483647
#|#deprecated("Use `MAX_VALUE` instead")
#|pub let max_value = 2147483647
#|pub const MIN_VALUE = -2147483648
#|#deprecated("Use `MIN_VALUE` instead")
#|pub let min_value = -2147483648
#|#deprecated("Use Int::abs instead")
#|pub fn abs(x : Int32) -> Int32 {
#| Int32::abs(x)
#|}
),
},
)
///|
let moonbitlang_core_int16_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/int16",
deps={},
files={
"builtin_types.mbt": (
#|#builtin
#|pub type Int16
),
"extends.mbt": (
#|pub extend Int16 with Add::{add}
#|pub extend Int16 with BitAnd::{land}
#|pub extend Int16 with BitOr::{lor}
#|pub extend Int16 with BitXOr::{lxor}
#|pub extend Int16 with Compare::{compare}
#|pub extend Int16 with Div::{div}
#|pub extend Int16 with Eq::{equal}
#|pub extend Int16 with Hash::{hash}
#|pub extend Int16 with Mod::{mod}
#|pub extend Int16 with Mul::{mul}
#|pub extend Int16 with Neg::{neg}
#|pub extend Int16 with Shl::{shl}
#|pub extend Int16 with Shr::{shr}
#|pub extend Int16 with Sub::{sub}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int16 with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int16 with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Int16 with Hash::{hash_combine}
),
"int16.mbt": (
#|pub const MAX_VALUE : Int16 = 32767
#|#deprecated("Use `MAX_VALUE` instead")
#|pub let max_value : Int16 = 32767
#|pub const MIN_VALUE : Int16 = -32768
#|#deprecated("Use `MIN_VALUE` instead")
#|pub let min_value : Int16 = -32768
#|pub fn Int16::Int16(self : Int16) -> Int16 = "%identity"
#|pub impl Add for Int16 with fn add(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() + that.to_int())
#|}
#|pub impl Sub for Int16 with fn sub(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() - that.to_int())
#|}
#|pub impl Mul for Int16 with fn mul(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() * that.to_int())
#|}
#|pub impl Div for Int16 with fn div(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() / that.to_int())
#|}
#|pub impl Mod for Int16 with fn mod(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() % that.to_int())
#|}
#|pub impl Eq for Int16 with fn equal(self, that) {
#| self.to_int() == that.to_int()
#|}
#|pub impl Eq for Int16 with fn not_equal(self, that) {
#| self.to_int() != that.to_int()
#|}
#|pub impl Compare for Int16 with fn compare(self, that) {
#| self.to_int().compare(that.to_int())
#|}
#|pub impl Compare for Int16 with fn lt(x, y) {
#| x.to_int() < y.to_int()
#|}
#|pub impl Compare for Int16 with fn le(x, y) {
#| x.to_int() <= y.to_int()
#|}
#|pub impl Compare for Int16 with fn gt(x, y) {
#| x.to_int() > y.to_int()
#|}
#|pub impl Compare for Int16 with fn ge(x, y) {
#| x.to_int() >= y.to_int()
#|}
#|pub impl Hash for Int16 with fn hash_combine(self, hasher) {
#| hasher.combine_int(self.to_int())
#|}
#|pub impl Shl for Int16 with fn shl(self : Int16, that : Int32) -> Int16 {
#| Int16::from_int(self.to_int() << that)
#|}
#|pub impl Shr for Int16 with fn shr(self : Int16, that : Int32) -> Int16 {
#| Int16::from_int(self.to_int() >> that)
#|}
#|pub impl BitOr for Int16 with fn lor(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() | that.to_int())
#|}
#|pub impl BitAnd for Int16 with fn land(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() & that.to_int())
#|}
#|pub impl BitXOr for Int16 with fn lxor(self : Int16, that : Int16) -> Int16 {
#| Int16::from_int(self.to_int() ^ that.to_int())
#|}
#|pub fn Int16::lnot(self : Int16) -> Int16 {
#| Int16::from_int(self.to_int().lnot())
#|}
#|pub impl Neg for Int16 with fn neg(self : Int16) -> Int16 {
#| Int16::from_int(-self.to_int())
#|}
#|pub fn Int16::abs(self : Int16) -> Int16 {
#| if self < 0 {
#| -self
#| } else {
#| self
#| }
#|}
#|pub impl Default for Int16 with fn default() {
#| 0
#|}
#|pub impl Show for Int16 with fn to_string(self) {
#| Int16::to_string(self)
#|}
#|pub fn Int16::reinterpret_as_uint16(self : Int16) -> UInt16 {
#| self.to_int().to_uint16()
#|}
#|pub fn Int16::reinterpret_from_uint16(self : UInt16) -> Int16 {
#| Int16::from_int(self.to_int())
#|}
#|pub fn Int16::from_int(self : Int32) -> Int16 = "%i32_to_i16"
#|pub fn Int16::from_byte(self : Byte) -> Int16 = "%byte_to_i16"
#|#cfg(not(target="js"))
#|pub fn Int16::from_int64(self : Int64) -> Int16 = "%i64_to_i16"
#|#cfg(target="js")
#|pub fn Int16::from_int64(self : Int64) -> Int16 {
#| Int16::from_int(self.to_int())
#|}
#|#cfg(target="js")
#|pub fn Int16::to_int64(self : Int16) -> Int64 {
#| self.to_int().to_int64()
#|}
#|#cfg(not(target="js"))
#|pub fn Int16::to_int64(self : Int16) -> Int64 = "%i16_to_i64"
#|pub fn Int16::to_int(self : Int16) -> Int32 = "%i16_to_i32"
#|pub fn Int16::to_byte(self : Int16) -> Byte = "%i16_to_byte"
#|pub fn Int16::to_string(self : Int16, radix~ : Int32 = 10) -> String {
#| self.to_int().to_string(radix~)
#|}
#|pub fn Int16::to_double(self : Int16) -> Double {
#| self.to_int().to_double()
#|}
#|pub fn Int16::from_uint(self : UInt32) -> Int16 {
#| Int16::from_int(self.reinterpret_as_int())
#|}
#|pub fn Int16::from_uint64(self : UInt64) -> Int16 {
#| Int16::from_int(self.to_int())
#|}
#|pub fn Int16::min(self : Int16, other : Int16) -> Int16 {
#| if self < other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Int16::max(self : Int16, other : Int16) -> Int16 {
#| if self > other {
#| self
#| } else {
#| other
#| }
#|}
#|pub fn Int16::clamp(self : Int16, min~ : Int16, max~ : Int16) -> Int16 {
#| guard! min <= max
#| if self < min {
#| min
#| } else if self > max {
#| max
#| } else {
#| self
#| }
#|}
#|pub fn Int16::clz(self : Int16) -> Int32 {
#| (self.to_int() & 0xFFFF).clz() - 16
#|}
#|pub fn Int16::ctz(self : Int16) -> Int32 {
#| (self.to_int() | 0x10000).ctz()
#|}
#|pub fn Int16::popcnt(self : Int16) -> Int32 {
#| (self.to_int() & 0xFFFF).popcnt()
#|}
#|pub fn Int16::to_uint(self : Int16) -> UInt32 {
#| self.to_int().reinterpret_as_uint()
#|}
#|pub fn Int16::to_uint64(self : Int16) -> UInt64 {
#| self.to_int().to_uint64()
#|}
),
},
)
///|
let moonbitlang_core_int64_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/int64",
deps={},
files={
"int64.mbt": (
#|pub const MAX_VALUE = 9223372036854775807L
#|#deprecated("Use `MAX_VALUE` instead")
#|pub let max_value = 9223372036854775807L
#|pub const MIN_VALUE = -9223372036854775808L
#|#deprecated("Use `MIN_VALUE` instead")
#|pub let min_value = -9223372036854775808L
#|#deprecated("Use `x.to_int64()` (`Int::to_int64`) instead")
#|#doc(hidden)
#|pub fn from_int(i : Int32) -> Int64 {
#| i.to_int64()
#|}
#|#deprecated("Use Int64::abs instead")
#|#doc(hidden)
#|pub fn abs(x : Int64) -> Int64 {
#| Int64::abs(x)
#|}
),
},
)
///|
let moonbitlang_core_internal_checksum_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/internal/checksum",
deps={},
files={
"adler32.mbt": (
#|const ADLER_MODULUS : Int32 = 65521
#|const ADLER_BLOCK_SIZE : Int32 = 3800
#|struct Adler32 {
#| mut a : Int32
#| mut b : Int32
#|}
#|pub fn Adler32::Adler32() -> Adler32 {
#| { a: 1, b: 0, }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#inline
#|fn adler_group8(data : BytesView, i : Int32) -> Int32 {
#| let d0 = data.unsafe_get(i).to_int()
#| let d1 = data.unsafe_get(i + 1).to_int()
#| let d2 = data.unsafe_get(i + 2).to_int()
#| let d3 = data.unsafe_get(i + 3).to_int()
#| let d4 = data.unsafe_get(i + 4).to_int()
#| let d5 = data.unsafe_get(i + 5).to_int()
#| let d6 = data.unsafe_get(i + 6).to_int()
#| let d7 = data.unsafe_get(i + 7).to_int()
#| adler_group_sums(d0, d1, d2, d3, d4, d5, d6, d7)
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#inline
#|fn adler_group_sums(
#| d0 : Int32,
#| d1 : Int32,
#| d2 : Int32,
#| d3 : Int32,
#| d4 : Int32,
#| d5 : Int32,
#| d6 : Int32,
#| d7 : Int32,
#|) -> Int32 {
#| let s01 = d0 + d1
#| let s0123 = s01 + (d2 + d3)
#| let s45 = d4 + d5
#| let s4567 = s45 + (d6 + d7)
#| let weighted = (s0123 << 2) +
#| (s0123 + (s01 << 1) + d0 + d2) +
#| (s4567 + (s45 << 1) + d4 + d6)
#| (s0123 + s4567) | (weighted << 16)
#|}
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn adler_group8(data : BytesView, i : Int32) -> Int32 {
#| let x = data.unsafe_read_uint64_le(i)
#| let even = x & 0x00FF00FF00FF00FFUL // d0, d2, d4, d6
#| let odd = (x >> 8) & 0x00FF00FF00FF00FFUL // d1, d3, d5, d7
#| let sum = ((even + odd) * 0x0001000100010001UL) >> 48
#| let indexed = ((even * 0x0000000200040006UL) >> 48) +
#| ((odd * 0x0001000300050007UL) >> 48)
#| (sum | (((sum << 3) - indexed) << 16)).to_int()
#|}
#|fn adler_groups_end(start : Int32, end : Int32) -> Int32 {
#| end - (end - start) % 8
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn adler_bytes_as_fixedarray(data : Bytes) -> FixedArray[Byte] = "%identity"
#|#cfg(any(target="native", target="wasm"))
#|#inline
#|fn adler_lane_sum(lanes : V128) -> Int32 {
#| (@simd.i32x4_extract_lane(lanes, 0) +
#| @simd.i32x4_extract_lane(lanes, 1) +
#| @simd.i32x4_extract_lane(lanes, 2) +
#| @simd.i32x4_extract_lane(lanes, 3)).reinterpret_as_int()
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn Adler32::fold_chunks16(
#| self : Adler32,
#| data : BytesView,
#| start : Int32,
#| end : Int32,
#|) -> Int32 {
#| let chunks = (end - start) / 16
#| guard chunks > 0 else { return start }
#| let bytes = adler_bytes_as_fixedarray(data.data())
#| let offset = data.start_offset() + start
#| let weights_low = @simd.i16x8_const(16, 15, 14, 13, 12, 11, 10, 9)
#| let weights_high = @simd.i16x8_const(8, 7, 6, 5, 4, 3, 2, 1)
#| let mut sums = @simd.i32x4_splat(0)
#| let mut before = @simd.i32x4_splat(0)
#| let mut weighted = @simd.i32x4_splat(0)
#| for chunk in 0.. Int32 {
#| start
#|}
#|pub fn Adler32::update(self : Adler32, data : BytesView) -> Unit {
#| let mut a = self.a
#| let mut b = self.b
#| let mut start = 0
#| while start < data.length() {
#| let end = start + (data.length() - start).min(ADLER_BLOCK_SIZE)
#| let mut a_sum = 0
#| let mut weighted_sum = 0
#| self.a = a
#| self.b = b
#| let mut i = self.fold_chunks16(data, start, end)
#| a = self.a
#| b = self.b
#| let groups_end = adler_groups_end(i, end)
#| while i < groups_end {
#| let packed = adler_group8(data, i)
#| let weighted = packed >> 16
#| a_sum = a_sum + a
#| weighted_sum = weighted_sum + weighted
#| a = a + (packed & 0xFFFF)
#| i = i + 8
#| }
#| b = b + (a_sum << 3) + weighted_sum
#| while i < end {
#| a = a + data.unsafe_get(i).to_int()
#| b = b + a
#| i = i + 1
#| }
#| a = a % ADLER_MODULUS
#| b = b % ADLER_MODULUS
#| start = end
#| }
#| self.a = a
#| self.b = b
#|}
#|pub fn Adler32::update_byte(self : Adler32, byte : Byte) -> Unit {
#| self.a = (self.a + byte.to_int()) % ADLER_MODULUS
#| self.b = (self.b + self.a) % ADLER_MODULUS
#|}
#|pub fn Adler32::finish(self : Adler32) -> UInt32 {
#| (self.b.reinterpret_as_uint() << 16) | self.a.reinterpret_as_uint()
#|}
#|pub fn adler32(data : BytesView) -> UInt32 {
#| let d = Adler32()
#| d.update(data[:])
#| d.finish()
#|}
),
"array.mbt": (
#|#cfg(any(target="native", target="wasm"))
#|pub fn Crc32::update_array(checksum : Crc32, data : ArrayView[Byte]) -> Unit {
#| let mut position = 0
#| while position < data.length() {
#| let end = position + (data.length() - position).min(4096)
#| checksum.update(Bytes::from_array(data[position:end]))
#| position = end
#| }
#|}
#|#cfg(any(target="native", target="wasm"))
#|pub fn Adler32::update_array(
#| checksum : Adler32,
#| data : ArrayView[Byte],
#|) -> Unit {
#| let mut position = 0
#| while position < data.length() {
#| let end = position + (data.length() - position).min(4096)
#| checksum.update(Bytes::from_array(data[position:end]))
#| position = end
#| }
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn Crc32::update_array(self : Crc32, data : ArrayView[Byte]) -> Unit {
#| let mut crc = self.state
#| let mut position = 0
#| while data.length() - position >= 8 {
#| crc = crc32_table.unsafe_get(
#| 1792 +
#| ((crc & 0xFF).reinterpret_as_int() ^ data.unsafe_get(position).to_int()),
#| ) ^
#| crc32_table.unsafe_get(
#| 1536 +
#| (
#| ((crc >> 8) & 0xFF).reinterpret_as_int() ^
#| data.unsafe_get(position + 1).to_int()
#| ),
#| ) ^
#| crc32_table.unsafe_get(
#| 1280 +
#| (
#| ((crc >> 16) & 0xFF).reinterpret_as_int() ^
#| data.unsafe_get(position + 2).to_int()
#| ),
#| ) ^
#| crc32_table.unsafe_get(
#| 1024 +
#| (
#| (crc >> 24).reinterpret_as_int() ^
#| data.unsafe_get(position + 3).to_int()
#| ),
#| ) ^
#| crc32_table.unsafe_get(768 + data.unsafe_get(position + 4).to_int()) ^
#| crc32_table.unsafe_get(512 + data.unsafe_get(position + 5).to_int()) ^
#| crc32_table.unsafe_get(256 + data.unsafe_get(position + 6).to_int()) ^
#| crc32_table.unsafe_get(data.unsafe_get(position + 7).to_int())
#| position += 8
#| }
#| for i in position..> 8)
#| }
#| self.state = crc
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|pub fn Adler32::update_array(self : Adler32, data : ArrayView[Byte]) -> Unit {
#| let mut a = self.a
#| let mut b = self.b
#| let mut start = 0
#| while start < data.length() {
#| let end = start + (data.length() - start).min(ADLER_BLOCK_SIZE)
#| let mut a_sum = 0
#| let mut weighted_sum = 0
#| let mut i = start
#| let groups_end = adler_groups_end(start, end)
#| while i < groups_end {
#| let packed = adler_group_sums(
#| data.unsafe_get(i).to_int(),
#| data.unsafe_get(i + 1).to_int(),
#| data.unsafe_get(i + 2).to_int(),
#| data.unsafe_get(i + 3).to_int(),
#| data.unsafe_get(i + 4).to_int(),
#| data.unsafe_get(i + 5).to_int(),
#| data.unsafe_get(i + 6).to_int(),
#| data.unsafe_get(i + 7).to_int(),
#| )
#| a_sum += a
#| weighted_sum += packed >> 16
#| a += packed & 0xFFFF
#| i += 8
#| }
#| b += (a_sum << 3) + weighted_sum
#| while i < end {
#| a += data.unsafe_get(i).to_int()
#| b += a
#| i += 1
#| }
#| a %= ADLER_MODULUS
#| b %= ADLER_MODULUS
#| start = end
#| }
#| self.a = a
#| self.b = b
#|}
),
"crc32.mbt": (
#|fn build_crc32_table() -> FixedArray[UInt32] {
#| let t = FixedArray::make(16 * 256, 0U)
#| for n in 0..<256 {
#| let mut c = n.reinterpret_as_uint()
#| for _k in 0..<8 {
#| c = if (c & 1) != 0 { 0xEDB88320 ^ (c >> 1) } else { c >> 1 }
#| }
#| t[n] = c
#| }
#| for slice in 1..<16 {
#| for n in 0..<256 {
#| let previous = t[(slice - 1) * 256 + n]
#| t[slice * 256 + n] = t[(previous & 0xFF).reinterpret_as_int()] ^
#| (previous >> 8)
#| }
#| }
#| t
#|}
#|let crc32_table : FixedArray[UInt32] = build_crc32_table()
#|struct Crc32 {
#| mut state : UInt32 // running CRC, before the final one's-complement
#|}
#|pub fn Crc32::Crc32() -> Crc32 {
#| { state: 0xFFFFFFFF, }
#|}
#|fn crc32_blocks16(initial : UInt32, data : BytesView) -> UInt32 {
#| let mut crc = initial
#| let mut position = 0
#| let bytes = data.data()
#| let start = data.start_offset()
#| while data.length() - position >= 16 {
#| let offset = start + position
#| let first = crc ^ bytes.unsafe_read_uint32_le(offset)
#| crc = crc32_table.unsafe_get(3840 + (first & 0xFF).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(3584 + ((first >> 8) & 0xFF).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(3328 + ((first >> 16) & 0xFF).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(3072 + (first >> 24).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(2816 + bytes.unsafe_get(offset + 4).to_int()) ^
#| crc32_table.unsafe_get(2560 + bytes.unsafe_get(offset + 5).to_int()) ^
#| crc32_table.unsafe_get(2304 + bytes.unsafe_get(offset + 6).to_int()) ^
#| crc32_table.unsafe_get(2048 + bytes.unsafe_get(offset + 7).to_int()) ^
#| crc32_table.unsafe_get(1792 + bytes.unsafe_get(offset + 8).to_int()) ^
#| crc32_table.unsafe_get(1536 + bytes.unsafe_get(offset + 9).to_int()) ^
#| crc32_table.unsafe_get(1280 + bytes.unsafe_get(offset + 10).to_int()) ^
#| crc32_table.unsafe_get(1024 + bytes.unsafe_get(offset + 11).to_int()) ^
#| crc32_table.unsafe_get(768 + bytes.unsafe_get(offset + 12).to_int()) ^
#| crc32_table.unsafe_get(512 + bytes.unsafe_get(offset + 13).to_int()) ^
#| crc32_table.unsafe_get(256 + bytes.unsafe_get(offset + 14).to_int()) ^
#| crc32_table.unsafe_get(bytes.unsafe_get(offset + 15).to_int())
#| position = position + 16
#| }
#| crc
#|}
#|pub fn Crc32::update(self : Crc32, data : BytesView) -> Unit {
#| let mut crc = self.state
#| guard data.length() >= 8 else {
#| for byte in data {
#| crc = crc32_table[((crc ^ byte.to_uint()) & 0xFF).reinterpret_as_int()] ^
#| (crc >> 8)
#| }
#| self.state = crc
#| return
#| }
#| let mut position = 0
#| let bytes = data.data()
#| let start = data.start_offset()
#| if data.length() >= 16 {
#| crc = crc32_blocks16(crc, data)
#| position = data.length() & 0xfffffff0
#| }
#| if data.length() - position >= 8 {
#| let offset = start + position
#| let first = crc ^ bytes.unsafe_read_uint32_le(offset)
#| crc = crc32_table.unsafe_get(1792 + (first & 0xFF).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(1536 + ((first >> 8) & 0xFF).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(1280 + ((first >> 16) & 0xFF).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(1024 + (first >> 24).reinterpret_as_int()) ^
#| crc32_table.unsafe_get(768 + bytes.unsafe_get(offset + 4).to_int()) ^
#| crc32_table.unsafe_get(512 + bytes.unsafe_get(offset + 5).to_int()) ^
#| crc32_table.unsafe_get(256 + bytes.unsafe_get(offset + 6).to_int()) ^
#| crc32_table.unsafe_get(bytes.unsafe_get(offset + 7).to_int())
#| position = position + 8
#| }
#| for i in position..> 8)
#| }
#| self.state = crc
#|}
#|pub fn Crc32::update_byte(self : Crc32, b : Byte) -> Unit {
#| self.state = crc32_table[((self.state ^ b.to_uint()) & 0xFF).reinterpret_as_int()] ^
#| (self.state >> 8)
#|}
#|pub fn Crc32::finish(self : Crc32) -> UInt32 {
#| self.state ^ 0xFFFFFFFF
#|}
#|pub fn crc32(data : BytesView) -> UInt32 {
#| let d = Crc32()
#| d.update(data)
#| d.finish()
#|}
),
},
)
///|
let moonbitlang_core_internal_edit_distance_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/internal/edit_distance",
deps={},
files={
"edit_distance.mbt": (
#|priv trait LevInput {
#| fn a_length(Self) -> Int32
#| fn b_length(Self) -> Int32
#| fn eq(Self, Int32, Int32) -> Bool
#|}
#|priv struct ViewPair[T] {
#| a : ArrayView[T]
#| b : ArrayView[T]
#|}
#|impl[T : Eq] LevInput for ViewPair[T] with fn a_length(self) {
#| self.a.length()
#|}
#|impl[T : Eq] LevInput for ViewPair[T] with fn b_length(self) {
#| self.b.length()
#|}
#|impl[T : Eq] LevInput for ViewPair[T] with fn eq(self, i, j) {
#| self.a[i] == self.b[j]
#|}
#|priv struct StrPair {
#| a : StringView
#| b : StringView
#|}
#|impl LevInput for StrPair with fn a_length(self) {
#| self.a.length()
#|}
#|impl LevInput for StrPair with fn b_length(self) {
#| self.b.length()
#|}
#|impl LevInput for StrPair with fn eq(self, i, j) {
#| self.a[i] == self.b[j]
#|}
#|fn[S : LevInput] lev_distance(s : S) -> Int32 {
#| let alen = s.a_length()
#| let blen = s.b_length()
#| let mut p = 0
#| while p < blen && s.eq(p, p) {
#| p += 1
#| }
#| let mut suffix = 0
#| while suffix < blen - p && s.eq(alen - 1 - suffix, blen - 1 - suffix) {
#| suffix += 1
#| }
#| let m = alen - p - suffix
#| let n = blen - p - suffix
#| if n == 0 {
#| return m
#| }
#| guard m < @int.MAX_VALUE else { abort("edit_distance: input too large") }
#| let mut prev = FixedArray::makei(n + 1, j => j)
#| let mut cur = FixedArray::make(n + 1, 0)
#| for i0 in 0.. Int32? {
#| let alen = s.a_length()
#| let blen = s.b_length()
#| let mut p = 0
#| while p < blen && s.eq(p, p) {
#| p += 1
#| }
#| let mut suffix = 0
#| while suffix < blen - p && s.eq(alen - 1 - suffix, blen - 1 - suffix) {
#| suffix += 1
#| }
#| let m = alen - p - suffix
#| let n = blen - p - suffix
#| if m - n > max_distance {
#| return None
#| }
#| if max_distance >= m {
#| return Some(lev_distance(s))
#| }
#| guard m < @int.MAX_VALUE else {
#| abort("edit_distance_within: input too large")
#| }
#| let k = max_distance
#| let inf = k + 1
#| let mut prev = FixedArray::makei(n + 1, j => if j <= k { j } else { inf })
#| let mut cur = FixedArray::make(n + 1, inf)
#| for i0 in 0..= n - i { n } else { i + k }
#| cur[lo - 1] = if lo == 1 && i <= k { i } else { inf }
#| let mut row_min = inf
#| for j in lo..<=hi {
#| let sub = prev[j - 1] + (if s.eq(p + i - 1, p + j - 1) { 0 } else { 1 })
#| let del = prev[j] + 1
#| let ins = cur[j - 1] + 1
#| let best = sub.min(del).min(ins).min(inf)
#| cur[j] = best
#| row_min = row_min.min(best)
#| }
#| if row_min > k {
#| return None
#| }
#| if hi < n {
#| cur[hi + 1] = inf
#| }
#| let t = prev
#| prev = cur
#| cur = t
#| }
#| if prev[n] <= k {
#| Some(prev[n])
#| } else {
#| None
#| }
#|}
#|pub fn[T : Eq] edit_distance(a : ArrayView[T], b : ArrayView[T]) -> Int32 {
#| let (a, b) = if a.length() >= b.length() { (a, b) } else { (b, a) }
#| let s : ViewPair[T] = { a, b, }
#| lev_distance(s)
#|}
#|pub fn[T : Eq] edit_distance_within(
#| a : ArrayView[T],
#| b : ArrayView[T],
#| max_distance~ : Int32,
#|) -> Int32? {
#| if max_distance < 0 {
#| return None
#| }
#| let (a, b) = if a.length() >= b.length() { (a, b) } else { (b, a) }
#| let s : ViewPair[T] = { a, b, }
#| lev_within(s, max_distance)
#|}
#|pub fn edit_distance_str(a : StringView, b : StringView) -> Int32 {
#| let (a, b) = if a.length() >= b.length() { (a, b) } else { (b, a) }
#| let s : StrPair = { a, b, }
#| lev_distance(s)
#|}
#|pub fn edit_distance_str_within(
#| a : StringView,
#| b : StringView,
#| max_distance~ : Int32,
#|) -> Int32? {
#| if max_distance < 0 {
#| return None
#| }
#| let (a, b) = if a.length() >= b.length() { (a, b) } else { (b, a) }
#| let s : StrPair = { a, b, }
#| lev_within(s, max_distance)
#|}
),
},
)
///|
let moonbitlang_core_internal_os_string_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/internal/os_string",
deps={},
files={
"os_string.mbt": (
#|#cfg(any(not(target="native"), platform="windows"))
#|struct OsString(String)
#|#cfg(all(target="native", not(platform="windows")))
#|struct OsString(Bytes)
#|#cfg(any(not(target="native"), platform="windows"))
#|pub impl Show for OsString with fn to_string(self) {
#| self.0
#|}
#|#cfg(all(target="native", not(platform="windows")))
#|pub impl Show for OsString with fn to_string(self) {
#| @utf8.decode_lossy(self.0)
#|}
#|pub extend OsString with @builtin.Show::{to_string}
#|#cfg(all(target="native", not(platform="windows")))
#|pub fn OsString::bytes_length(self : OsString) -> Int32 {
#| self.0.length()
#|}
#|#cfg(any(not(target="native"), platform="windows"))
#|pub fn OsString::bytes_length(self : OsString) -> Int32 {
#| self.0.length() * 2
#|}
#|#cfg(any(not(target="native"), platform="windows"))
#|#as_free_fn
#|pub fn OsString::from_string(str : StringView) -> OsString {
#| OsString(str.to_owned())
#|}
#|#cfg(all(target="native", not(platform="windows")))
#|#as_free_fn
#|pub fn OsString::from_string(str : StringView) -> OsString {
#| OsString(@utf8.encode(str))
#|}
#|pub let empty : OsString = from_string("")
#|#cfg(all(target="native", platform="windows"))
#|let _unused_import : Unit = ignore(@utf8.encode(""))
),
},
)
///|
let moonbitlang_core_internal_strconv_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/internal/strconv",
deps={},
files={
"strconv_bool.mbt": (
#|pub fn parse_bool(str : StringView) -> Bool raise {
#| lexmatch str with longest {
#| re"^(true|TRUE|True|t|T|1)$" => true
#| re"^(false|FALSE|False|f|F|0)$" => false
#| _ => syntax_err()
#| }
#|}
),
"strconv_decimal.mbt": (
#|let max_shift = 59
#|priv struct Decimal {
#| digits : FixedArray[Byte]
#| mut digits_num : Int32
#| mut decimal_point : Int32
#| mut negative : Bool
#| mut truncated : Bool
#| mut overflowed : Int32
#|}
#|let powtab : ReadOnlyArray[Int32] = [
#| 1, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59,
#|]
#|fn Decimal::new_priv() -> Decimal {
#| {
#| digits: FixedArray::make(800, b'\x00'),
#| digits_num: 0,
#| decimal_point: 0,
#| negative: false,
#| truncated: false,
#| overflowed: 0,
#| }
#|}
#|fn Decimal::from_int64_priv(v : Int64) -> Decimal {
#| let d = Decimal::new_priv()
#| d.assign(v)
#| d
#|}
#|fn parse_decimal_priv(str : StringView) -> Decimal raise {
#| parse_decimal_from_view(str)
#|}
#|fn parse_decimal_from_view(str : StringView) -> Decimal raise {
#| let d = Decimal::new_priv()
#| let mut has_dp = false
#| let mut has_digits = false
#| let rest = match str {
#| ['-', .. rest] => {
#| d.negative = true
#| rest
#| }
#| ['+', .. rest] => rest
#| _ => str
#| }
#| let rest = for rest = rest {
#| match rest {
#| ['_', .. rest] => continue rest
#| ['.', .. rest] => {
#| guard !has_dp else { syntax_err() }
#| has_dp = true
#| d.decimal_point = d.digits_num
#| continue rest
#| }
#| ['0'..<='9' as digit, .. rest] => {
#| has_digits = true
#| if digit == '0' && d.digits_num == 0 {
#| d.decimal_point -= 1
#| continue rest
#| }
#| if d.digits_num < d.digits.length() {
#| d.digits[d.digits_num] = (digit.to_int() - '0').to_byte()
#| d.digits_num += 1
#| } else {
#| if !has_dp {
#| d.overflowed += 1
#| }
#| if digit != '0' {
#| d.truncated = true
#| }
#| }
#| continue rest
#| }
#| rest => break rest
#| }
#| }
#| guard has_digits else { syntax_err() }
#| if !has_dp {
#| d.decimal_point = d.digits_num
#| }
#| let rest = match rest {
#| ['e' | 'E', .. rest] => {
#| let mut exp_sign = 1
#| let rest = match rest {
#| ['+', .. rest] => rest
#| ['-', .. rest] => {
#| exp_sign = -1
#| rest
#| }
#| rest => rest
#| }
#| guard rest is ['0'..<='9', ..] else { syntax_err() }
#| let effective_dp = d.decimal_point + d.overflowed
#| let exp_limit = if exp_sign > 0 {
#| if effective_dp < 311 {
#| 311 - effective_dp
#| } else {
#| 0
#| }
#| } else if effective_dp > -331 {
#| effective_dp + 331
#| } else {
#| 0
#| }
#| let mut exp = 0
#| let rest = for rest = rest {
#| match rest {
#| ['_', .. rest] => continue rest
#| ['0'..<='9' as digit, .. rest] => {
#| if exp < exp_limit {
#| exp = exp * 10 + (digit.to_int() - '0')
#| if exp > exp_limit {
#| exp = exp_limit
#| }
#| }
#| continue rest
#| }
#| rest => break rest
#| }
#| }
#| d.decimal_point += exp_sign * exp
#| rest
#| }
#| rest => rest
#| }
#| guard rest is [] else { syntax_err() }
#| d.trim()
#| d
#|}
#|fn Decimal::to_double_priv(self : Decimal) -> Double raise {
#| let mut exponent = 0
#| let mut mantissa = 0L
#| let effective_dp = self.decimal_point + self.overflowed
#| if self.digits_num == 0 || effective_dp < -330 {
#| mantissa = 0
#| exponent = double_info.bias
#| let bits = assemble_bits(mantissa, exponent, self.negative)
#| return bits.reinterpret_as_double()
#| }
#| if self.decimal_point > 310 {
#| range_err()
#| }
#| self.decimal_point += self.overflowed
#| while self.decimal_point > 0 {
#| let n = if self.decimal_point >= powtab.length() {
#| 60
#| } else {
#| powtab[self.decimal_point]
#| }
#| self.shift_priv(-n)
#| exponent += n
#| }
#| while self.decimal_point < 0 ||
#| (self.decimal_point == 0 && self.digits[0].to_int() < 5) {
#| let n = if -self.decimal_point >= powtab.length() {
#| 60
#| } else {
#| powtab[-self.decimal_point]
#| }
#| self.shift_priv(n)
#| exponent -= n
#| }
#| exponent -= 1
#| if exponent < double_info.bias + 1 {
#| let n = double_info.bias + 1 - exponent
#| self.shift_priv(-n)
#| exponent += n
#| }
#| if exponent - double_info.bias >= (1 << double_info.exponent_bits) - 1 {
#| range_err()
#| }
#| self.shift_priv(double_info.mantissa_bits + 1)
#| mantissa = self.rounded_integer()
#| if mantissa == 2L << double_info.mantissa_bits {
#| mantissa = mantissa >> 1
#| exponent += 1
#| if exponent - double_info.bias >= (1 << double_info.exponent_bits) - 1 {
#| range_err()
#| }
#| }
#| if (mantissa & (1L << double_info.mantissa_bits)) == 0L {
#| exponent = double_info.bias
#| }
#| let bits = assemble_bits(mantissa, exponent, self.negative)
#| bits.reinterpret_as_double()
#|}
#|fn Decimal::shift_priv(self : Decimal, s : Int32) -> Unit {
#| if self.digits_num == 0 {
#| return
#| }
#| let mut s = s
#| if s > 0 {
#| while s > max_shift {
#| self.left_shift(max_shift)
#| s -= max_shift
#| }
#| self.left_shift(s)
#| }
#| if s < 0 {
#| while s < -max_shift {
#| self.right_shift(max_shift)
#| s += max_shift
#| }
#| self.right_shift(-s)
#| }
#|}
#|fn assemble_bits(mantissa : Int64, exponent : Int32, negative : Bool) -> Int64 {
#| let biased_exp = exponent - double_info.bias
#| let mut bits = mantissa & ((1L << double_info.mantissa_bits) - 1L)
#| let exp_bits = (biased_exp & ((1 << double_info.exponent_bits) - 1)).to_int64()
#| bits = bits | (exp_bits << double_info.mantissa_bits)
#| if negative {
#| bits = bits | (1L << double_info.mantissa_bits << double_info.exponent_bits)
#| }
#| bits
#|}
#|fn Decimal::rounded_integer(self : Decimal) -> Int64 {
#| if self.decimal_point > 20 {
#| return 0xFFFFFFFFFFFFFFFFL
#| }
#| let (n, i) = for n = 0L, i = 0; i < self.decimal_point && i < self.digits_num; {
#| continue n * 10L + self.digits[i].to_int64(), i + 1
#| } nobreak {
#| (n, i)
#| }
#| let n = for n = n, i = i; i < self.decimal_point; {
#| continue n * 10L, i + 1
#| } nobreak {
#| n
#| }
#| if self.should_round_up(self.decimal_point) {
#| n + 1L
#| } else {
#| n
#| }
#|}
#|fn Decimal::should_round_up(self : Decimal, d : Int32) -> Bool {
#| if d < 0 || d >= self.digits_num {
#| return false
#| }
#| if self.digits[d].to_int() == 5 && d + 1 == self.digits_num {
#| if self.truncated {
#| return true
#| }
#| return d > 0 && self.digits[d - 1].to_int() % 2 != 0
#| }
#| self.digits[d].to_int() >= 5
#|}
#|fn Decimal::assign(self : Decimal, v : Int64) -> Unit {
#| let buf = FixedArray::make(24, b'\x00')
#| let n = for n = 0, v = v; v > 0; {
#| let v1 = v / 10
#| buf[n] = (v - v1 * 10).to_byte()
#| continue n + 1, v1
#| } nobreak {
#| n
#| }
#| self.digits_num = 0
#| for i in n>..0 {
#| self.digits[self.digits_num] = buf[i]
#| self.digits_num += 1
#| }
#| self.decimal_point = self.digits_num
#| self.trim()
#|}
#|fn Decimal::right_shift(self : Decimal, s : Int32) -> Unit {
#| let mut read_index = 0
#| let mut write_index = 0
#| let mut acc = 0UL
#| while acc >> s == 0 {
#| if read_index >= self.digits_num {
#| while acc >> s == 0 {
#| acc *= 10
#| read_index += 1
#| }
#| break
#| }
#| let d = self.digits[read_index]
#| acc = acc * 10 + d.to_int64().reinterpret_as_uint64()
#| read_index += 1
#| }
#| self.decimal_point -= read_index - 1
#| let mask = (1UL << s) - 1
#| while read_index < self.digits_num {
#| let out = acc >> s
#| self.digits[write_index] = out.to_byte()
#| write_index += 1
#| acc = acc & mask
#| let d = self.digits[read_index]
#| acc = acc * 10 + d.to_int64().reinterpret_as_uint64()
#| read_index += 1
#| }
#| while acc > 0 {
#| let out = acc >> s
#| if write_index < self.digits.length() {
#| self.digits[write_index] = out.to_byte()
#| write_index += 1
#| } else if out > 0 {
#| self.truncated = true
#| }
#| acc = acc & mask
#| acc *= 10
#| }
#| self.digits_num = write_index
#| self.trim()
#|}
#|let left_shift_cheats : ReadOnlyArray[(Int32, String)] = [
#| (0, ""),
#| (1, "5"), // * 2
#| (1, "25"), // * 4
#| (1, "125"), // * 8
#| (2, "625"), // * 16
#| (2, "3125"), // * 32
#| (2, "15625"), // * 64
#| (3, "78125"), // * 128
#| (3, "390625"), // * 256
#| (3, "1953125"), // * 512
#| (4, "9765625"), // * 1024
#| (4, "48828125"), // * 2048
#| (4, "244140625"), // * 4096
#| (4, "1220703125"), // * 8192
#| (5, "6103515625"), // * 16384
#| (5, "30517578125"), // * 32768
#| (5, "152587890625"), // * 65536
#| (6, "762939453125"), // * 131072
#| (6, "3814697265625"), // * 262144
#| (6, "19073486328125"), // * 524288
#| (7, "95367431640625"), // * 1048576
#| (7, "476837158203125"), // * 2097152
#| (7, "2384185791015625"), // * 4194304
#| (7, "11920928955078125"), // * 8388608
#| (8, "59604644775390625"), // * 16777216
#| (8, "298023223876953125"), // * 33554432
#| (8, "1490116119384765625"), // * 67108864
#| (9, "7450580596923828125"), // * 134217728
#| (9, "37252902984619140625"), // * 268435456
#| (9, "186264514923095703125"), // * 536870912
#| (10, "931322574615478515625"), // * 1073741824
#| (10, "4656612873077392578125"), // * 2147483648
#| (10, "23283064365386962890625"), // * 4294967296
#| (10, "116415321826934814453125"), // * 8589934592
#| (11, "582076609134674072265625"), // * 17179869184
#| (11, "2910383045673370361328125"), // * 34359738368
#| (11, "14551915228366851806640625"), // * 68719476736
#| (12, "72759576141834259033203125"), // * 137438953472
#| (12, "363797880709171295166015625"), // * 274877906944
#| (12, "1818989403545856475830078125"), // * 549755813888
#| (13, "9094947017729282379150390625"), // * 1099511627776
#| (13, "45474735088646411895751953125"), // * 2199023255552
#| (13, "227373675443232059478759765625"), // * 4398046511104
#| (13, "1136868377216160297393798828125"), // * 8796093022208
#| (14, "5684341886080801486968994140625"), // * 17592186044416
#| (14, "28421709430404007434844970703125"), // * 35184372088832
#| (14, "142108547152020037174224853515625"), // * 70368744177664
#| (15, "710542735760100185871124267578125"), // * 140737488355328
#| (15, "3552713678800500929355621337890625"), // * 281474976710656
#| (15, "17763568394002504646778106689453125"), // * 562949953421312
#| (16, "88817841970012523233890533447265625"), // * 1125899906842624
#| (16, "444089209850062616169452667236328125"), // * 2251799813685248
#| (16, "2220446049250313080847263336181640625"), // * 4503599627370496
#| (16, "11102230246251565404236316680908203125"), // * 9007199254740992
#| (17, "55511151231257827021181583404541015625"), // * 18014398509481984
#| (17, "277555756156289135105907917022705078125"), // * 36028797018963968
#| (17, "1387778780781445675529539585113525390625"), // * 72057594037927936
#| (18, "6938893903907228377647697925567626953125"), // * 144115188075855872
#| (18, "34694469519536141888238489627838134765625"), // * 288230376151711744
#| (18, "173472347597680709441192448139190673828125"), // * 576460752303423488
#| (19, "867361737988403547205962240695953369140625"), // * 1152921504606846976
#|]
#|fn Decimal::new_digits(self : Decimal, s : Int32) -> Int32 {
#| let new_digits = left_shift_cheats[s].0
#| let cheat_num = left_shift_cheats[s].1
#| let less = for i, code_unit in cheat_num.code_units() {
#| if i >= self.digits_num {
#| break true
#| }
#| let d = code_unit.to_int() - '0'
#| if self.digits[i].to_int() != d {
#| break self.digits[i].to_int() < d
#| }
#| } nobreak {
#| false
#| }
#| if less {
#| new_digits - 1
#| } else {
#| new_digits
#| }
#|}
#|fn Decimal::left_shift(self : Decimal, s : Int32) -> Unit {
#| let new_digits = self.new_digits(s)
#| let mut read_index = self.digits_num - 1
#| let mut write_index = self.digits_num + new_digits
#| let mut acc = 0L
#| while read_index >= 0 {
#| let d = self.digits[read_index].to_int64()
#| acc += d << s
#| let quo = acc / 10L
#| let rem = (acc - quo * 10L).to_int()
#| write_index -= 1
#| if write_index < self.digits.length() {
#| self.digits[write_index] = rem.to_byte()
#| } else if rem != 0 {
#| self.truncated = true
#| }
#| acc = quo
#| read_index -= 1
#| }
#| while acc > 0L {
#| let quo = acc / 10L
#| let rem = (acc - 10L * quo).to_int()
#| write_index -= 1
#| if write_index < self.digits.length() {
#| self.digits[write_index] = rem.to_byte()
#| } else if rem != 0 {
#| self.truncated = true
#| }
#| acc = quo
#| }
#| self.digits_num += new_digits
#| if self.digits_num > self.digits.length() {
#| self.digits_num = self.digits.length()
#| }
#| self.decimal_point += new_digits
#| self.trim()
#|}
#|fn Decimal::trim(self : Decimal) -> Unit {
#| while self.digits_num > 0 && self.digits[self.digits_num - 1] == 0 {
#| self.digits_num -= 1
#| }
#| if self.digits_num == 0 {
#| self.decimal_point = 0
#| }
#|}
#|impl Show for Decimal with fn output(self, logger) {
#| if self.digits_num == 0 {
#| logger.write_char('0')
#| return
#| }
#| if self.decimal_point <= 0 {
#| logger <+ "0."
#| for _ in 0..<-self.decimal_point {
#| logger.write_char('0')
#| }
#| for i in 0.. String {
#| Show::to_string(self)
#|}
#|test "new" {
#| let hpd = Decimal::from_int64_priv(1L)
#| inspect(hpd.digits.length(), content="800")
#| inspect(hpd.digits_num, content="1")
#| inspect(hpd.decimal_point, content="1")
#| inspect(hpd.negative, content="false")
#| inspect(hpd.truncated, content="false")
#|}
#|test "from_int64" {
#| let hpd = Decimal::from_int64_priv(123456789L)
#| inspect(hpd.to_string(), content="123456789")
#|}
#|test "to_string" {
#| let hpd = Decimal::from_int64_priv(123456789L)
#| hpd.decimal_point = 1
#| inspect(hpd.to_string(), content="1.23456789")
#| hpd.decimal_point = 0
#| inspect(hpd.to_string(), content="0.123456789")
#| hpd.decimal_point = -1
#| inspect(hpd.to_string(), content="0.0123456789")
#| hpd.decimal_point = 10
#| inspect(hpd.to_string(), content="1234567890")
#|}
#|test "parse decimal with underscore" {
#| inspect(parse_decimal_priv("1e1_2"), content="1000000000000")
#| inspect(parse_decimal_priv("1e12"), content="1000000000000")
#| inspect(parse_decimal_priv("1_2_3"), content="123")
#|}
#|test "parse decimal with large numbers" {
#| let s = String::make(100, '9')
#| inspect(parse_decimal_priv(s), content=s)
#|}
#|test "should_round_up when truncated and half-way" {
#| let decimal = parse_decimal_priv("12.50")
#| inspect(decimal.to_double_priv(), content="12.5")
#|}
#|test "parse_decimal with large numbers and truncation" {
#| let s = String::make(1000, '9')
#| inspect(parse_decimal_priv(s), content=String::make(800, '9'))
#|}
#|test "rounded_integer overflow when decimal_point > 20" {
#| let decimal = Decimal::new_priv()
#| decimal.negative = false
#| decimal.decimal_point = 25 // This is > 20 but < 310
#| decimal.digits_num = 1
#| decimal.digits[0] = Int(1).to_byte()
#| decimal.truncated = false
#| let result = decimal.rounded_integer()
#| inspect(result, content="-1") // Should be Int64::max_value
#|}
#|test "parse_double mantissa normalization boundary" {
#| inspect(parse_double("1.9999999999999999"), content="2")
#| inspect(parse_double("9007199254740991.5"), content="9007199254740992")
#|}
#|test "parse_double large magnitude cancellation" {
#| let input = "1" + String::make(800, '0') + "e-800"
#| inspect(parse_double(input), content="1")
#| let input = "-1" + String::make(800, '0') + "e-800"
#| inspect(parse_double(input), content="-1")
#| let input = "1." + String::make(800, '0')
#| inspect(parse_double(input), content="1")
#| let input = "1" + String::make(1999, '0') + "e-1999"
#| inspect(parse_double(input), content="1")
#| let input = "1" + String::make(399, '0') + ".5"
#| try parse_double(input) catch {
#| e =>
#| inspect(
#| e,
#| content=(
#| #|Failure: value out of range
#| ),
#| )
#| } noraise {
#| _ => fail("expected parse_double to raise")
#| }
#| let input = "1" + String::make(800, '0') + ".5"
#| try parse_double(input) catch {
#| e =>
#| inspect(
#| e,
#| content=(
#| #|Failure: value out of range
#| ),
#| )
#| } noraise {
#| _ => fail("expected parse_double to raise")
#| }
#| let input = "1" + String::make(1499, '0') + "e-1499"
#| inspect(parse_double(input), content="1")
#| let input = "0." + String::make(1999, '0') + "1"
#| inspect(parse_double(input), content="0")
#|}
),
"strconv_double.mbt": (
#|priv struct FloatInfo {
#| mantissa_bits : Int32
#| exponent_bits : Int32
#| bias : Int32
#|}
#|let double_info : FloatInfo = {
#| mantissa_bits: 52,
#| exponent_bits: 11,
#| bias: -1023,
#|}
#|let mantissa_explicit_bits = 52
#|let min_exponent_fast_path : Int64 = -22L
#|let max_exponent_fast_path : Int64 = 22L
#|let max_exponent_disguised_fast_path : Int64 = 37L
#|let max_mantissa_fast_path : UInt64 = 2UL << mantissa_explicit_bits
#|pub fn parse_double(str : StringView) -> Double raise {
#| guard !str.is_empty() else { syntax_err() }
#| guard check_underscore(str) else { syntax_err() }
#| match parse_number(str) {
#| None => parse_inf_nan(str)
#| Some(num) =>
#| match num.try_fast_path() {
#| Some(value) => value
#| None => {
#| let fast = if num.many_digits {
#| @double.not_a_number
#| } else {
#| try_eisel_lemire64(num.mantissa, num.exponent, num.negative)
#| }
#| if fast.is_nan() {
#| parse_decimal_priv(str).to_double_priv() // fallback to slow path
#| } else {
#| fast
#| }
#| }
#| }
#| }
#|}
#|fn Number::is_fast_path(self : Number) -> Bool {
#| min_exponent_fast_path <= self.exponent &&
#| self.exponent <= max_exponent_disguised_fast_path &&
#| self.mantissa <= max_mantissa_fast_path &&
#| !self.many_digits
#|}
#|let table : ReadOnlyArray[Double] = [
#| 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0,
#| 1000000000.0, 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
#| 100000000000000.0, 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
#| 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0, 1000000000000000000000.0,
#| 10000000000000000000000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
#|]
#|fn pow10_fast_path(exponent : Int32) -> Double {
#| table[exponent & 31]
#|}
#|let int_pow10 : ReadOnlyArray[UInt64] = [
#| 1UL, 10UL, 100UL, 1000UL, 10000UL, 100000UL, 1000000UL, 10000000UL, 100000000UL,
#| 1000000000UL, 10000000000UL, 100000000000UL, 1000000000000UL, 10000000000000UL,
#| 100000000000000UL, 1000000000000000UL,
#|]
#|fn Number::try_fast_path(self : Number) -> Double? {
#| guard self.is_fast_path() else { return None }
#| let value = if self.exponent <= max_exponent_fast_path {
#| let value = self.mantissa.to_double()
#| if self.exponent < 0L {
#| value / pow10_fast_path(-self.exponent.to_int())
#| } else {
#| value * pow10_fast_path(self.exponent.to_int())
#| }
#| } else {
#| let shift = self.exponent - max_exponent_fast_path
#| guard checked_mul(self.mantissa, int_pow10[shift.to_int()])
#| is Some(mantissa) else {
#| return None
#| }
#| if mantissa > max_mantissa_fast_path {
#| return None
#| }
#| mantissa.to_double() * pow10_fast_path(max_exponent_fast_path.to_int())
#| }
#| Some(if self.negative { -value } else { value })
#|}
#|test "parse_double_nan" {
#| assert_true(parse_double("nan").is_nan())
#| assert_true(parse_double("NaN").is_nan())
#| assert_true(parse_double("NAN").is_nan())
#|}
),
"strconv_eisel_lemire.mbt": (
#|#valtype
#|priv struct EiselProduct {
#| lo : UInt64
#| hi : UInt64
#|}
#|fn eisel_umul128(a : UInt64, b : UInt64) -> EiselProduct {
#| let a_lo = a & 0xffffffffUL
#| let a_hi = a >> 32
#| let b_lo = b & 0xffffffffUL
#| let b_hi = b >> 32
#| let x = a_lo * b_lo
#| let y = a_hi * b_lo + (x >> 32)
#| let z = a_lo * b_hi + (y & 0xffffffffUL)
#| let hi = a_hi * b_hi + (y >> 32) + (z >> 32)
#| { lo: a * b, hi, }
#|}
#|fn eisel_mul_log2_10(exponent : Int32) -> Int32 {
#| (exponent * 108853) >> 15
#|}
#|#doc(hidden)
#|pub fn try_eisel_lemire64(
#| mantissa : UInt64,
#| exponent : Int64,
#| negative : Bool,
#|) -> Double {
#| if mantissa == 0UL {
#| return if negative {
#| 0x8000000000000000UL.reinterpret_as_double()
#| } else {
#| 0.0
#| }
#| }
#| if exponent < EISEL_LEMIRE_POW10_MIN.to_int64() ||
#| exponent > EISEL_LEMIRE_POW10_MAX.to_int64() {
#| return @double.not_a_number
#| }
#| let exponent = exponent.to_int()
#| let table_index = (exponent - EISEL_LEMIRE_POW10_MIN) * 2
#| let pow_hi = eisel_lemire_pow10_table[table_index]
#| let pow_lo = eisel_lemire_pow10_table[table_index + 1]
#| let pow_exp2 = 1 + eisel_mul_log2_10(exponent)
#| let leading_zeros = mantissa.clz()
#| let normalized = mantissa << leading_zeros
#| let mut result_exp2 = pow_exp2 + 63 + 1023 - leading_zeros
#| let product = eisel_umul128(normalized, pow_hi)
#| let mut product_hi = product.hi
#| let mut product_lo = product.lo
#| if (product_hi & 0x1ffUL) == 0x1ffUL && product_lo + normalized < normalized {
#| let wider = eisel_umul128(normalized, pow_lo)
#| let mut merged_hi = product_hi
#| let merged_lo = product_lo + wider.hi
#| if merged_lo < product_lo {
#| merged_hi += 1UL
#| }
#| if (merged_hi & 0x1ffUL) == 0x1ffUL &&
#| merged_lo + 1UL == 0UL &&
#| wider.lo + normalized < normalized {
#| return @double.not_a_number
#| }
#| product_hi = merged_hi
#| product_lo = merged_lo
#| }
#| let top_bit = (product_hi >> 63).to_int()
#| let mut result_mantissa = product_hi >> (top_bit + 9)
#| result_exp2 -= 1 - top_bit
#| if product_lo == 0UL &&
#| (product_hi & 0x1ffUL) == 0UL &&
#| (result_mantissa & 3UL) == 1UL {
#| return @double.not_a_number
#| }
#| result_mantissa += result_mantissa & 1UL
#| result_mantissa = result_mantissa >> 1
#| if result_mantissa >> 53 > 0UL {
#| result_mantissa = result_mantissa >> 1
#| result_exp2 += 1
#| }
#| if result_exp2 <= 0 || result_exp2 >= 0x7ff {
#| return @double.not_a_number
#| }
#| let exponent_bits = result_exp2.reinterpret_as_uint().to_uint64() << 52
#| let mut result_bits = exponent_bits | (result_mantissa & 0x000fffffffffffffUL)
#| if negative {
#| result_bits = result_bits | 0x8000000000000000UL
#| }
#| result_bits.reinterpret_as_double()
#|}
),
"strconv_eisel_lemire_table.mbt": (
#|const EISEL_LEMIRE_POW10_MIN : Int32 = -348
#|const EISEL_LEMIRE_POW10_MAX : Int32 = 347
#|let eisel_lemire_pow10_table : ReadOnlyArray[UInt64] = [
#| 0xfa8fd5a0081c0288UL, 0x1732c869cd60e453UL, // 1e-348 * 2**1284
#| 0x9c99e58405118195UL, 0x0e7fbd42205c8eb4UL, // 1e-347 * 2**1280
#| 0xc3c05ee50655e1faUL, 0x521fac92a873b261UL, // 1e-346 * 2**1277
#| 0xf4b0769e47eb5a78UL, 0xe6a797b752909ef9UL, // 1e-345 * 2**1274
#| 0x98ee4a22ecf3188bUL, 0x9028bed2939a635cUL, // 1e-344 * 2**1270
#| 0xbf29dcaba82fdeaeUL, 0x7432ee873880fc33UL, // 1e-343 * 2**1267
#| 0xeef453d6923bd65aUL, 0x113faa2906a13b3fUL, // 1e-342 * 2**1264
#| 0x9558b4661b6565f8UL, 0x4ac7ca59a424c507UL, // 1e-341 * 2**1260
#| 0xbaaee17fa23ebf76UL, 0x5d79bcf00d2df649UL, // 1e-340 * 2**1257
#| 0xe95a99df8ace6f53UL, 0xf4d82c2c107973dcUL, // 1e-339 * 2**1254
#| 0x91d8a02bb6c10594UL, 0x79071b9b8a4be869UL, // 1e-338 * 2**1250
#| 0xb64ec836a47146f9UL, 0x9748e2826cdee284UL, // 1e-337 * 2**1247
#| 0xe3e27a444d8d98b7UL, 0xfd1b1b2308169b25UL, // 1e-336 * 2**1244
#| 0x8e6d8c6ab0787f72UL, 0xfe30f0f5e50e20f7UL, // 1e-335 * 2**1240
#| 0xb208ef855c969f4fUL, 0xbdbd2d335e51a935UL, // 1e-334 * 2**1237
#| 0xde8b2b66b3bc4723UL, 0xad2c788035e61382UL, // 1e-333 * 2**1234
#| 0x8b16fb203055ac76UL, 0x4c3bcb5021afcc31UL, // 1e-332 * 2**1230
#| 0xaddcb9e83c6b1793UL, 0xdf4abe242a1bbf3dUL, // 1e-331 * 2**1227
#| 0xd953e8624b85dd78UL, 0xd71d6dad34a2af0dUL, // 1e-330 * 2**1224
#| 0x87d4713d6f33aa6bUL, 0x8672648c40e5ad68UL, // 1e-329 * 2**1220
#| 0xa9c98d8ccb009506UL, 0x680efdaf511f18c2UL, // 1e-328 * 2**1217
#| 0xd43bf0effdc0ba48UL, 0x0212bd1b2566def2UL, // 1e-327 * 2**1214
#| 0x84a57695fe98746dUL, 0x014bb630f7604b57UL, // 1e-326 * 2**1210
#| 0xa5ced43b7e3e9188UL, 0x419ea3bd35385e2dUL, // 1e-325 * 2**1207
#| 0xcf42894a5dce35eaUL, 0x52064cac828675b9UL, // 1e-324 * 2**1204
#| 0x818995ce7aa0e1b2UL, 0x7343efebd1940993UL, // 1e-323 * 2**1200
#| 0xa1ebfb4219491a1fUL, 0x1014ebe6c5f90bf8UL, // 1e-322 * 2**1197
#| 0xca66fa129f9b60a6UL, 0xd41a26e077774ef6UL, // 1e-321 * 2**1194
#| 0xfd00b897478238d0UL, 0x8920b098955522b4UL, // 1e-320 * 2**1191
#| 0x9e20735e8cb16382UL, 0x55b46e5f5d5535b0UL, // 1e-319 * 2**1187
#| 0xc5a890362fddbc62UL, 0xeb2189f734aa831dUL, // 1e-318 * 2**1184
#| 0xf712b443bbd52b7bUL, 0xa5e9ec7501d523e4UL, // 1e-317 * 2**1181
#| 0x9a6bb0aa55653b2dUL, 0x47b233c92125366eUL, // 1e-316 * 2**1177
#| 0xc1069cd4eabe89f8UL, 0x999ec0bb696e840aUL, // 1e-315 * 2**1174
#| 0xf148440a256e2c76UL, 0xc00670ea43ca250dUL, // 1e-314 * 2**1171
#| 0x96cd2a865764dbcaUL, 0x380406926a5e5728UL, // 1e-313 * 2**1167
#| 0xbc807527ed3e12bcUL, 0xc605083704f5ecf2UL, // 1e-312 * 2**1164
#| 0xeba09271e88d976bUL, 0xf7864a44c633682eUL, // 1e-311 * 2**1161
#| 0x93445b8731587ea3UL, 0x7ab3ee6afbe0211dUL, // 1e-310 * 2**1157
#| 0xb8157268fdae9e4cUL, 0x5960ea05bad82964UL, // 1e-309 * 2**1154
#| 0xe61acf033d1a45dfUL, 0x6fb92487298e33bdUL, // 1e-308 * 2**1151
#| 0x8fd0c16206306babUL, 0xa5d3b6d479f8e056UL, // 1e-307 * 2**1147
#| 0xb3c4f1ba87bc8696UL, 0x8f48a4899877186cUL, // 1e-306 * 2**1144
#| 0xe0b62e2929aba83cUL, 0x331acdabfe94de87UL, // 1e-305 * 2**1141
#| 0x8c71dcd9ba0b4925UL, 0x9ff0c08b7f1d0b14UL, // 1e-304 * 2**1137
#| 0xaf8e5410288e1b6fUL, 0x07ecf0ae5ee44dd9UL, // 1e-303 * 2**1134
#| 0xdb71e91432b1a24aUL, 0xc9e82cd9f69d6150UL, // 1e-302 * 2**1131
#| 0x892731ac9faf056eUL, 0xbe311c083a225cd2UL, // 1e-301 * 2**1127
#| 0xab70fe17c79ac6caUL, 0x6dbd630a48aaf406UL, // 1e-300 * 2**1124
#| 0xd64d3d9db981787dUL, 0x092cbbccdad5b108UL, // 1e-299 * 2**1121
#| 0x85f0468293f0eb4eUL, 0x25bbf56008c58ea5UL, // 1e-298 * 2**1117
#| 0xa76c582338ed2621UL, 0xaf2af2b80af6f24eUL, // 1e-297 * 2**1114
#| 0xd1476e2c07286faaUL, 0x1af5af660db4aee1UL, // 1e-296 * 2**1111
#| 0x82cca4db847945caUL, 0x50d98d9fc890ed4dUL, // 1e-295 * 2**1107
#| 0xa37fce126597973cUL, 0xe50ff107bab528a0UL, // 1e-294 * 2**1104
#| 0xcc5fc196fefd7d0cUL, 0x1e53ed49a96272c8UL, // 1e-293 * 2**1101
#| 0xff77b1fcbebcdc4fUL, 0x25e8e89c13bb0f7aUL, // 1e-292 * 2**1098
#| 0x9faacf3df73609b1UL, 0x77b191618c54e9acUL, // 1e-291 * 2**1094
#| 0xc795830d75038c1dUL, 0xd59df5b9ef6a2417UL, // 1e-290 * 2**1091
#| 0xf97ae3d0d2446f25UL, 0x4b0573286b44ad1dUL, // 1e-289 * 2**1088
#| 0x9becce62836ac577UL, 0x4ee367f9430aec32UL, // 1e-288 * 2**1084
#| 0xc2e801fb244576d5UL, 0x229c41f793cda73fUL, // 1e-287 * 2**1081
#| 0xf3a20279ed56d48aUL, 0x6b43527578c1110fUL, // 1e-286 * 2**1078
#| 0x9845418c345644d6UL, 0x830a13896b78aaa9UL, // 1e-285 * 2**1074
#| 0xbe5691ef416bd60cUL, 0x23cc986bc656d553UL, // 1e-284 * 2**1071
#| 0xedec366b11c6cb8fUL, 0x2cbfbe86b7ec8aa8UL, // 1e-283 * 2**1068
#| 0x94b3a202eb1c3f39UL, 0x7bf7d71432f3d6a9UL, // 1e-282 * 2**1064
#| 0xb9e08a83a5e34f07UL, 0xdaf5ccd93fb0cc53UL, // 1e-281 * 2**1061
#| 0xe858ad248f5c22c9UL, 0xd1b3400f8f9cff68UL, // 1e-280 * 2**1058
#| 0x91376c36d99995beUL, 0x23100809b9c21fa1UL, // 1e-279 * 2**1054
#| 0xb58547448ffffb2dUL, 0xabd40a0c2832a78aUL, // 1e-278 * 2**1051
#| 0xe2e69915b3fff9f9UL, 0x16c90c8f323f516cUL, // 1e-277 * 2**1048
#| 0x8dd01fad907ffc3bUL, 0xae3da7d97f6792e3UL, // 1e-276 * 2**1044
#| 0xb1442798f49ffb4aUL, 0x99cd11cfdf41779cUL, // 1e-275 * 2**1041
#| 0xdd95317f31c7fa1dUL, 0x40405643d711d583UL, // 1e-274 * 2**1038
#| 0x8a7d3eef7f1cfc52UL, 0x482835ea666b2572UL, // 1e-273 * 2**1034
#| 0xad1c8eab5ee43b66UL, 0xda3243650005eecfUL, // 1e-272 * 2**1031
#| 0xd863b256369d4a40UL, 0x90bed43e40076a82UL, // 1e-271 * 2**1028
#| 0x873e4f75e2224e68UL, 0x5a7744a6e804a291UL, // 1e-270 * 2**1024
#| 0xa90de3535aaae202UL, 0x711515d0a205cb36UL, // 1e-269 * 2**1021
#| 0xd3515c2831559a83UL, 0x0d5a5b44ca873e03UL, // 1e-268 * 2**1018
#| 0x8412d9991ed58091UL, 0xe858790afe9486c2UL, // 1e-267 * 2**1014
#| 0xa5178fff668ae0b6UL, 0x626e974dbe39a872UL, // 1e-266 * 2**1011
#| 0xce5d73ff402d98e3UL, 0xfb0a3d212dc8128fUL, // 1e-265 * 2**1008
#| 0x80fa687f881c7f8eUL, 0x7ce66634bc9d0b99UL, // 1e-264 * 2**1004
#| 0xa139029f6a239f72UL, 0x1c1fffc1ebc44e80UL, // 1e-263 * 2**1001
#| 0xc987434744ac874eUL, 0xa327ffb266b56220UL, // 1e-262 * 2**998
#| 0xfbe9141915d7a922UL, 0x4bf1ff9f0062baa8UL, // 1e-261 * 2**995
#| 0x9d71ac8fada6c9b5UL, 0x6f773fc3603db4a9UL, // 1e-260 * 2**991
#| 0xc4ce17b399107c22UL, 0xcb550fb4384d21d3UL, // 1e-259 * 2**988
#| 0xf6019da07f549b2bUL, 0x7e2a53a146606a48UL, // 1e-258 * 2**985
#| 0x99c102844f94e0fbUL, 0x2eda7444cbfc426dUL, // 1e-257 * 2**981
#| 0xc0314325637a1939UL, 0xfa911155fefb5308UL, // 1e-256 * 2**978
#| 0xf03d93eebc589f88UL, 0x793555ab7eba27caUL, // 1e-255 * 2**975
#| 0x96267c7535b763b5UL, 0x4bc1558b2f3458deUL, // 1e-254 * 2**971
#| 0xbbb01b9283253ca2UL, 0x9eb1aaedfb016f16UL, // 1e-253 * 2**968
#| 0xea9c227723ee8bcbUL, 0x465e15a979c1cadcUL, // 1e-252 * 2**965
#| 0x92a1958a7675175fUL, 0x0bfacd89ec191ec9UL, // 1e-251 * 2**961
#| 0xb749faed14125d36UL, 0xcef980ec671f667bUL, // 1e-250 * 2**958
#| 0xe51c79a85916f484UL, 0x82b7e12780e7401aUL, // 1e-249 * 2**955
#| 0x8f31cc0937ae58d2UL, 0xd1b2ecb8b0908810UL, // 1e-248 * 2**951
#| 0xb2fe3f0b8599ef07UL, 0x861fa7e6dcb4aa15UL, // 1e-247 * 2**948
#| 0xdfbdcece67006ac9UL, 0x67a791e093e1d49aUL, // 1e-246 * 2**945
#| 0x8bd6a141006042bdUL, 0xe0c8bb2c5c6d24e0UL, // 1e-245 * 2**941
#| 0xaecc49914078536dUL, 0x58fae9f773886e18UL, // 1e-244 * 2**938
#| 0xda7f5bf590966848UL, 0xaf39a475506a899eUL, // 1e-243 * 2**935
#| 0x888f99797a5e012dUL, 0x6d8406c952429603UL, // 1e-242 * 2**931
#| 0xaab37fd7d8f58178UL, 0xc8e5087ba6d33b83UL, // 1e-241 * 2**928
#| 0xd5605fcdcf32e1d6UL, 0xfb1e4a9a90880a64UL, // 1e-240 * 2**925
#| 0x855c3be0a17fcd26UL, 0x5cf2eea09a55067fUL, // 1e-239 * 2**921
#| 0xa6b34ad8c9dfc06fUL, 0xf42faa48c0ea481eUL, // 1e-238 * 2**918
#| 0xd0601d8efc57b08bUL, 0xf13b94daf124da26UL, // 1e-237 * 2**915
#| 0x823c12795db6ce57UL, 0x76c53d08d6b70858UL, // 1e-236 * 2**911
#| 0xa2cb1717b52481edUL, 0x54768c4b0c64ca6eUL, // 1e-235 * 2**908
#| 0xcb7ddcdda26da268UL, 0xa9942f5dcf7dfd09UL, // 1e-234 * 2**905
#| 0xfe5d54150b090b02UL, 0xd3f93b35435d7c4cUL, // 1e-233 * 2**902
#| 0x9efa548d26e5a6e1UL, 0xc47bc5014a1a6dafUL, // 1e-232 * 2**898
#| 0xc6b8e9b0709f109aUL, 0x359ab6419ca1091bUL, // 1e-231 * 2**895
#| 0xf867241c8cc6d4c0UL, 0xc30163d203c94b62UL, // 1e-230 * 2**892
#| 0x9b407691d7fc44f8UL, 0x79e0de63425dcf1dUL, // 1e-229 * 2**888
#| 0xc21094364dfb5636UL, 0x985915fc12f542e4UL, // 1e-228 * 2**885
#| 0xf294b943e17a2bc4UL, 0x3e6f5b7b17b2939dUL, // 1e-227 * 2**882
#| 0x979cf3ca6cec5b5aUL, 0xa705992ceecf9c42UL, // 1e-226 * 2**878
#| 0xbd8430bd08277231UL, 0x50c6ff782a838353UL, // 1e-225 * 2**875
#| 0xece53cec4a314ebdUL, 0xa4f8bf5635246428UL, // 1e-224 * 2**872
#| 0x940f4613ae5ed136UL, 0x871b7795e136be99UL, // 1e-223 * 2**868
#| 0xb913179899f68584UL, 0x28e2557b59846e3fUL, // 1e-222 * 2**865
#| 0xe757dd7ec07426e5UL, 0x331aeada2fe589cfUL, // 1e-221 * 2**862
#| 0x9096ea6f3848984fUL, 0x3ff0d2c85def7621UL, // 1e-220 * 2**858
#| 0xb4bca50b065abe63UL, 0x0fed077a756b53a9UL, // 1e-219 * 2**855
#| 0xe1ebce4dc7f16dfbUL, 0xd3e8495912c62894UL, // 1e-218 * 2**852
#| 0x8d3360f09cf6e4bdUL, 0x64712dd7abbbd95cUL, // 1e-217 * 2**848
#| 0xb080392cc4349decUL, 0xbd8d794d96aacfb3UL, // 1e-216 * 2**845
#| 0xdca04777f541c567UL, 0xecf0d7a0fc5583a0UL, // 1e-215 * 2**842
#| 0x89e42caaf9491b60UL, 0xf41686c49db57244UL, // 1e-214 * 2**838
#| 0xac5d37d5b79b6239UL, 0x311c2875c522ced5UL, // 1e-213 * 2**835
#| 0xd77485cb25823ac7UL, 0x7d633293366b828bUL, // 1e-212 * 2**832
#| 0x86a8d39ef77164bcUL, 0xae5dff9c02033197UL, // 1e-211 * 2**828
#| 0xa8530886b54dbdebUL, 0xd9f57f830283fdfcUL, // 1e-210 * 2**825
#| 0xd267caa862a12d66UL, 0xd072df63c324fd7bUL, // 1e-209 * 2**822
#| 0x8380dea93da4bc60UL, 0x4247cb9e59f71e6dUL, // 1e-208 * 2**818
#| 0xa46116538d0deb78UL, 0x52d9be85f074e608UL, // 1e-207 * 2**815
#| 0xcd795be870516656UL, 0x67902e276c921f8bUL, // 1e-206 * 2**812
#| 0x806bd9714632dff6UL, 0x00ba1cd8a3db53b6UL, // 1e-205 * 2**808
#| 0xa086cfcd97bf97f3UL, 0x80e8a40eccd228a4UL, // 1e-204 * 2**805
#| 0xc8a883c0fdaf7df0UL, 0x6122cd128006b2cdUL, // 1e-203 * 2**802
#| 0xfad2a4b13d1b5d6cUL, 0x796b805720085f81UL, // 1e-202 * 2**799
#| 0x9cc3a6eec6311a63UL, 0xcbe3303674053bb0UL, // 1e-201 * 2**795
#| 0xc3f490aa77bd60fcUL, 0xbedbfc4411068a9cUL, // 1e-200 * 2**792
#| 0xf4f1b4d515acb93bUL, 0xee92fb5515482d44UL, // 1e-199 * 2**789
#| 0x991711052d8bf3c5UL, 0x751bdd152d4d1c4aUL, // 1e-198 * 2**785
#| 0xbf5cd54678eef0b6UL, 0xd262d45a78a0635dUL, // 1e-197 * 2**782
#| 0xef340a98172aace4UL, 0x86fb897116c87c34UL, // 1e-196 * 2**779
#| 0x9580869f0e7aac0eUL, 0xd45d35e6ae3d4da0UL, // 1e-195 * 2**775
#| 0xbae0a846d2195712UL, 0x8974836059cca109UL, // 1e-194 * 2**772
#| 0xe998d258869facd7UL, 0x2bd1a438703fc94bUL, // 1e-193 * 2**769
#| 0x91ff83775423cc06UL, 0x7b6306a34627ddcfUL, // 1e-192 * 2**765
#| 0xb67f6455292cbf08UL, 0x1a3bc84c17b1d542UL, // 1e-191 * 2**762
#| 0xe41f3d6a7377eecaUL, 0x20caba5f1d9e4a93UL, // 1e-190 * 2**759
#| 0x8e938662882af53eUL, 0x547eb47b7282ee9cUL, // 1e-189 * 2**755
#| 0xb23867fb2a35b28dUL, 0xe99e619a4f23aa43UL, // 1e-188 * 2**752
#| 0xdec681f9f4c31f31UL, 0x6405fa00e2ec94d4UL, // 1e-187 * 2**749
#| 0x8b3c113c38f9f37eUL, 0xde83bc408dd3dd04UL, // 1e-186 * 2**745
#| 0xae0b158b4738705eUL, 0x9624ab50b148d445UL, // 1e-185 * 2**742
#| 0xd98ddaee19068c76UL, 0x3badd624dd9b0957UL, // 1e-184 * 2**739
#| 0x87f8a8d4cfa417c9UL, 0xe54ca5d70a80e5d6UL, // 1e-183 * 2**735
#| 0xa9f6d30a038d1dbcUL, 0x5e9fcf4ccd211f4cUL, // 1e-182 * 2**732
#| 0xd47487cc8470652bUL, 0x7647c3200069671fUL, // 1e-181 * 2**729
#| 0x84c8d4dfd2c63f3bUL, 0x29ecd9f40041e073UL, // 1e-180 * 2**725
#| 0xa5fb0a17c777cf09UL, 0xf468107100525890UL, // 1e-179 * 2**722
#| 0xcf79cc9db955c2ccUL, 0x7182148d4066eeb4UL, // 1e-178 * 2**719
#| 0x81ac1fe293d599bfUL, 0xc6f14cd848405530UL, // 1e-177 * 2**715
#| 0xa21727db38cb002fUL, 0xb8ada00e5a506a7cUL, // 1e-176 * 2**712
#| 0xca9cf1d206fdc03bUL, 0xa6d90811f0e4851cUL, // 1e-175 * 2**709
#| 0xfd442e4688bd304aUL, 0x908f4a166d1da663UL, // 1e-174 * 2**706
#| 0x9e4a9cec15763e2eUL, 0x9a598e4e043287feUL, // 1e-173 * 2**702
#| 0xc5dd44271ad3cdbaUL, 0x40eff1e1853f29fdUL, // 1e-172 * 2**699
#| 0xf7549530e188c128UL, 0xd12bee59e68ef47cUL, // 1e-171 * 2**696
#| 0x9a94dd3e8cf578b9UL, 0x82bb74f8301958ceUL, // 1e-170 * 2**692
#| 0xc13a148e3032d6e7UL, 0xe36a52363c1faf01UL, // 1e-169 * 2**689
#| 0xf18899b1bc3f8ca1UL, 0xdc44e6c3cb279ac1UL, // 1e-168 * 2**686
#| 0x96f5600f15a7b7e5UL, 0x29ab103a5ef8c0b9UL, // 1e-167 * 2**682
#| 0xbcb2b812db11a5deUL, 0x7415d448f6b6f0e7UL, // 1e-166 * 2**679
#| 0xebdf661791d60f56UL, 0x111b495b3464ad21UL, // 1e-165 * 2**676
#| 0x936b9fcebb25c995UL, 0xcab10dd900beec34UL, // 1e-164 * 2**672
#| 0xb84687c269ef3bfbUL, 0x3d5d514f40eea742UL, // 1e-163 * 2**669
#| 0xe65829b3046b0afaUL, 0x0cb4a5a3112a5112UL, // 1e-162 * 2**666
#| 0x8ff71a0fe2c2e6dcUL, 0x47f0e785eaba72abUL, // 1e-161 * 2**662
#| 0xb3f4e093db73a093UL, 0x59ed216765690f56UL, // 1e-160 * 2**659
#| 0xe0f218b8d25088b8UL, 0x306869c13ec3532cUL, // 1e-159 * 2**656
#| 0x8c974f7383725573UL, 0x1e414218c73a13fbUL, // 1e-158 * 2**652
#| 0xafbd2350644eeacfUL, 0xe5d1929ef90898faUL, // 1e-157 * 2**649
#| 0xdbac6c247d62a583UL, 0xdf45f746b74abf39UL, // 1e-156 * 2**646
#| 0x894bc396ce5da772UL, 0x6b8bba8c328eb783UL, // 1e-155 * 2**642
#| 0xab9eb47c81f5114fUL, 0x066ea92f3f326564UL, // 1e-154 * 2**639
#| 0xd686619ba27255a2UL, 0xc80a537b0efefebdUL, // 1e-153 * 2**636
#| 0x8613fd0145877585UL, 0xbd06742ce95f5f36UL, // 1e-152 * 2**632
#| 0xa798fc4196e952e7UL, 0x2c48113823b73704UL, // 1e-151 * 2**629
#| 0xd17f3b51fca3a7a0UL, 0xf75a15862ca504c5UL, // 1e-150 * 2**626
#| 0x82ef85133de648c4UL, 0x9a984d73dbe722fbUL, // 1e-149 * 2**622
#| 0xa3ab66580d5fdaf5UL, 0xc13e60d0d2e0ebbaUL, // 1e-148 * 2**619
#| 0xcc963fee10b7d1b3UL, 0x318df905079926a8UL, // 1e-147 * 2**616
#| 0xffbbcfe994e5c61fUL, 0xfdf17746497f7052UL, // 1e-146 * 2**613
#| 0x9fd561f1fd0f9bd3UL, 0xfeb6ea8bedefa633UL, // 1e-145 * 2**609
#| 0xc7caba6e7c5382c8UL, 0xfe64a52ee96b8fc0UL, // 1e-144 * 2**606
#| 0xf9bd690a1b68637bUL, 0x3dfdce7aa3c673b0UL, // 1e-143 * 2**603
#| 0x9c1661a651213e2dUL, 0x06bea10ca65c084eUL, // 1e-142 * 2**599
#| 0xc31bfa0fe5698db8UL, 0x486e494fcff30a62UL, // 1e-141 * 2**596
#| 0xf3e2f893dec3f126UL, 0x5a89dba3c3efccfaUL, // 1e-140 * 2**593
#| 0x986ddb5c6b3a76b7UL, 0xf89629465a75e01cUL, // 1e-139 * 2**589
#| 0xbe89523386091465UL, 0xf6bbb397f1135823UL, // 1e-138 * 2**586
#| 0xee2ba6c0678b597fUL, 0x746aa07ded582e2cUL, // 1e-137 * 2**583
#| 0x94db483840b717efUL, 0xa8c2a44eb4571cdcUL, // 1e-136 * 2**579
#| 0xba121a4650e4ddebUL, 0x92f34d62616ce413UL, // 1e-135 * 2**576
#| 0xe896a0d7e51e1566UL, 0x77b020baf9c81d17UL, // 1e-134 * 2**573
#| 0x915e2486ef32cd60UL, 0x0ace1474dc1d122eUL, // 1e-133 * 2**569
#| 0xb5b5ada8aaff80b8UL, 0x0d819992132456baUL, // 1e-132 * 2**566
#| 0xe3231912d5bf60e6UL, 0x10e1fff697ed6c69UL, // 1e-131 * 2**563
#| 0x8df5efabc5979c8fUL, 0xca8d3ffa1ef463c1UL, // 1e-130 * 2**559
#| 0xb1736b96b6fd83b3UL, 0xbd308ff8a6b17cb2UL, // 1e-129 * 2**556
#| 0xddd0467c64bce4a0UL, 0xac7cb3f6d05ddbdeUL, // 1e-128 * 2**553
#| 0x8aa22c0dbef60ee4UL, 0x6bcdf07a423aa96bUL, // 1e-127 * 2**549
#| 0xad4ab7112eb3929dUL, 0x86c16c98d2c953c6UL, // 1e-126 * 2**546
#| 0xd89d64d57a607744UL, 0xe871c7bf077ba8b7UL, // 1e-125 * 2**543
#| 0x87625f056c7c4a8bUL, 0x11471cd764ad4972UL, // 1e-124 * 2**539
#| 0xa93af6c6c79b5d2dUL, 0xd598e40d3dd89bcfUL, // 1e-123 * 2**536
#| 0xd389b47879823479UL, 0x4aff1d108d4ec2c3UL, // 1e-122 * 2**533
#| 0x843610cb4bf160cbUL, 0xcedf722a585139baUL, // 1e-121 * 2**529
#| 0xa54394fe1eedb8feUL, 0xc2974eb4ee658828UL, // 1e-120 * 2**526
#| 0xce947a3da6a9273eUL, 0x733d226229feea32UL, // 1e-119 * 2**523
#| 0x811ccc668829b887UL, 0x0806357d5a3f525fUL, // 1e-118 * 2**519
#| 0xa163ff802a3426a8UL, 0xca07c2dcb0cf26f7UL, // 1e-117 * 2**516
#| 0xc9bcff6034c13052UL, 0xfc89b393dd02f0b5UL, // 1e-116 * 2**513
#| 0xfc2c3f3841f17c67UL, 0xbbac2078d443ace2UL, // 1e-115 * 2**510
#| 0x9d9ba7832936edc0UL, 0xd54b944b84aa4c0dUL, // 1e-114 * 2**506
#| 0xc5029163f384a931UL, 0x0a9e795e65d4df11UL, // 1e-113 * 2**503
#| 0xf64335bcf065d37dUL, 0x4d4617b5ff4a16d5UL, // 1e-112 * 2**500
#| 0x99ea0196163fa42eUL, 0x504bced1bf8e4e45UL, // 1e-111 * 2**496
#| 0xc06481fb9bcf8d39UL, 0xe45ec2862f71e1d6UL, // 1e-110 * 2**493
#| 0xf07da27a82c37088UL, 0x5d767327bb4e5a4cUL, // 1e-109 * 2**490
#| 0x964e858c91ba2655UL, 0x3a6a07f8d510f86fUL, // 1e-108 * 2**486
#| 0xbbe226efb628afeaUL, 0x890489f70a55368bUL, // 1e-107 * 2**483
#| 0xeadab0aba3b2dbe5UL, 0x2b45ac74ccea842eUL, // 1e-106 * 2**480
#| 0x92c8ae6b464fc96fUL, 0x3b0b8bc90012929dUL, // 1e-105 * 2**476
#| 0xb77ada0617e3bbcbUL, 0x09ce6ebb40173744UL, // 1e-104 * 2**473
#| 0xe55990879ddcaabdUL, 0xcc420a6a101d0515UL, // 1e-103 * 2**470
#| 0x8f57fa54c2a9eab6UL, 0x9fa946824a12232dUL, // 1e-102 * 2**466
#| 0xb32df8e9f3546564UL, 0x47939822dc96abf9UL, // 1e-101 * 2**463
#| 0xdff9772470297ebdUL, 0x59787e2b93bc56f7UL, // 1e-100 * 2**460
#| 0x8bfbea76c619ef36UL, 0x57eb4edb3c55b65aUL, // 1e-99 * 2**456
#| 0xaefae51477a06b03UL, 0xede622920b6b23f1UL, // 1e-98 * 2**453
#| 0xdab99e59958885c4UL, 0xe95fab368e45ecedUL, // 1e-97 * 2**450
#| 0x88b402f7fd75539bUL, 0x11dbcb0218ebb414UL, // 1e-96 * 2**446
#| 0xaae103b5fcd2a881UL, 0xd652bdc29f26a119UL, // 1e-95 * 2**443
#| 0xd59944a37c0752a2UL, 0x4be76d3346f0495fUL, // 1e-94 * 2**440
#| 0x857fcae62d8493a5UL, 0x6f70a4400c562ddbUL, // 1e-93 * 2**436
#| 0xa6dfbd9fb8e5b88eUL, 0xcb4ccd500f6bb952UL, // 1e-92 * 2**433
#| 0xd097ad07a71f26b2UL, 0x7e2000a41346a7a7UL, // 1e-91 * 2**430
#| 0x825ecc24c873782fUL, 0x8ed400668c0c28c8UL, // 1e-90 * 2**426
#| 0xa2f67f2dfa90563bUL, 0x728900802f0f32faUL, // 1e-89 * 2**423
#| 0xcbb41ef979346bcaUL, 0x4f2b40a03ad2ffb9UL, // 1e-88 * 2**420
#| 0xfea126b7d78186bcUL, 0xe2f610c84987bfa8UL, // 1e-87 * 2**417
#| 0x9f24b832e6b0f436UL, 0x0dd9ca7d2df4d7c9UL, // 1e-86 * 2**413
#| 0xc6ede63fa05d3143UL, 0x91503d1c79720dbbUL, // 1e-85 * 2**410
#| 0xf8a95fcf88747d94UL, 0x75a44c6397ce912aUL, // 1e-84 * 2**407
#| 0x9b69dbe1b548ce7cUL, 0xc986afbe3ee11abaUL, // 1e-83 * 2**403
#| 0xc24452da229b021bUL, 0xfbe85badce996168UL, // 1e-82 * 2**400
#| 0xf2d56790ab41c2a2UL, 0xfae27299423fb9c3UL, // 1e-81 * 2**397
#| 0x97c560ba6b0919a5UL, 0xdccd879fc967d41aUL, // 1e-80 * 2**393
#| 0xbdb6b8e905cb600fUL, 0x5400e987bbc1c920UL, // 1e-79 * 2**390
#| 0xed246723473e3813UL, 0x290123e9aab23b68UL, // 1e-78 * 2**387
#| 0x9436c0760c86e30bUL, 0xf9a0b6720aaf6521UL, // 1e-77 * 2**383
#| 0xb94470938fa89bceUL, 0xf808e40e8d5b3e69UL, // 1e-76 * 2**380
#| 0xe7958cb87392c2c2UL, 0xb60b1d1230b20e04UL, // 1e-75 * 2**377
#| 0x90bd77f3483bb9b9UL, 0xb1c6f22b5e6f48c2UL, // 1e-74 * 2**373
#| 0xb4ecd5f01a4aa828UL, 0x1e38aeb6360b1af3UL, // 1e-73 * 2**370
#| 0xe2280b6c20dd5232UL, 0x25c6da63c38de1b0UL, // 1e-72 * 2**367
#| 0x8d590723948a535fUL, 0x579c487e5a38ad0eUL, // 1e-71 * 2**363
#| 0xb0af48ec79ace837UL, 0x2d835a9df0c6d851UL, // 1e-70 * 2**360
#| 0xdcdb1b2798182244UL, 0xf8e431456cf88e65UL, // 1e-69 * 2**357
#| 0x8a08f0f8bf0f156bUL, 0x1b8e9ecb641b58ffUL, // 1e-68 * 2**353
#| 0xac8b2d36eed2dac5UL, 0xe272467e3d222f3fUL, // 1e-67 * 2**350
#| 0xd7adf884aa879177UL, 0x5b0ed81dcc6abb0fUL, // 1e-66 * 2**347
#| 0x86ccbb52ea94baeaUL, 0x98e947129fc2b4e9UL, // 1e-65 * 2**343
#| 0xa87fea27a539e9a5UL, 0x3f2398d747b36224UL, // 1e-64 * 2**340
#| 0xd29fe4b18e88640eUL, 0x8eec7f0d19a03aadUL, // 1e-63 * 2**337
#| 0x83a3eeeef9153e89UL, 0x1953cf68300424acUL, // 1e-62 * 2**333
#| 0xa48ceaaab75a8e2bUL, 0x5fa8c3423c052dd7UL, // 1e-61 * 2**330
#| 0xcdb02555653131b6UL, 0x3792f412cb06794dUL, // 1e-60 * 2**327
#| 0x808e17555f3ebf11UL, 0xe2bbd88bbee40bd0UL, // 1e-59 * 2**323
#| 0xa0b19d2ab70e6ed6UL, 0x5b6aceaeae9d0ec4UL, // 1e-58 * 2**320
#| 0xc8de047564d20a8bUL, 0xf245825a5a445275UL, // 1e-57 * 2**317
#| 0xfb158592be068d2eUL, 0xeed6e2f0f0d56712UL, // 1e-56 * 2**314
#| 0x9ced737bb6c4183dUL, 0x55464dd69685606bUL, // 1e-55 * 2**310
#| 0xc428d05aa4751e4cUL, 0xaa97e14c3c26b886UL, // 1e-54 * 2**307
#| 0xf53304714d9265dfUL, 0xd53dd99f4b3066a8UL, // 1e-53 * 2**304
#| 0x993fe2c6d07b7fabUL, 0xe546a8038efe4029UL, // 1e-52 * 2**300
#| 0xbf8fdb78849a5f96UL, 0xde98520472bdd033UL, // 1e-51 * 2**297
#| 0xef73d256a5c0f77cUL, 0x963e66858f6d4440UL, // 1e-50 * 2**294
#| 0x95a8637627989aadUL, 0xdde7001379a44aa8UL, // 1e-49 * 2**290
#| 0xbb127c53b17ec159UL, 0x5560c018580d5d52UL, // 1e-48 * 2**287
#| 0xe9d71b689dde71afUL, 0xaab8f01e6e10b4a6UL, // 1e-47 * 2**284
#| 0x9226712162ab070dUL, 0xcab3961304ca70e8UL, // 1e-46 * 2**280
#| 0xb6b00d69bb55c8d1UL, 0x3d607b97c5fd0d22UL, // 1e-45 * 2**277
#| 0xe45c10c42a2b3b05UL, 0x8cb89a7db77c506aUL, // 1e-44 * 2**274
#| 0x8eb98a7a9a5b04e3UL, 0x77f3608e92adb242UL, // 1e-43 * 2**270
#| 0xb267ed1940f1c61cUL, 0x55f038b237591ed3UL, // 1e-42 * 2**267
#| 0xdf01e85f912e37a3UL, 0x6b6c46dec52f6688UL, // 1e-41 * 2**264
#| 0x8b61313bbabce2c6UL, 0x2323ac4b3b3da015UL, // 1e-40 * 2**260
#| 0xae397d8aa96c1b77UL, 0xabec975e0a0d081aUL, // 1e-39 * 2**257
#| 0xd9c7dced53c72255UL, 0x96e7bd358c904a21UL, // 1e-38 * 2**254
#| 0x881cea14545c7575UL, 0x7e50d64177da2e54UL, // 1e-37 * 2**250
#| 0xaa242499697392d2UL, 0xdde50bd1d5d0b9e9UL, // 1e-36 * 2**247
#| 0xd4ad2dbfc3d07787UL, 0x955e4ec64b44e864UL, // 1e-35 * 2**244
#| 0x84ec3c97da624ab4UL, 0xbd5af13bef0b113eUL, // 1e-34 * 2**240
#| 0xa6274bbdd0fadd61UL, 0xecb1ad8aeacdd58eUL, // 1e-33 * 2**237
#| 0xcfb11ead453994baUL, 0x67de18eda5814af2UL, // 1e-32 * 2**234
#| 0x81ceb32c4b43fcf4UL, 0x80eacf948770ced7UL, // 1e-31 * 2**230
#| 0xa2425ff75e14fc31UL, 0xa1258379a94d028dUL, // 1e-30 * 2**227
#| 0xcad2f7f5359a3b3eUL, 0x096ee45813a04330UL, // 1e-29 * 2**224
#| 0xfd87b5f28300ca0dUL, 0x8bca9d6e188853fcUL, // 1e-28 * 2**221
#| 0x9e74d1b791e07e48UL, 0x775ea264cf55347dUL, // 1e-27 * 2**217
#| 0xc612062576589ddaUL, 0x95364afe032a819dUL, // 1e-26 * 2**214
#| 0xf79687aed3eec551UL, 0x3a83ddbd83f52204UL, // 1e-25 * 2**211
#| 0x9abe14cd44753b52UL, 0xc4926a9672793542UL, // 1e-24 * 2**207
#| 0xc16d9a0095928a27UL, 0x75b7053c0f178293UL, // 1e-23 * 2**204
#| 0xf1c90080baf72cb1UL, 0x5324c68b12dd6338UL, // 1e-22 * 2**201
#| 0x971da05074da7beeUL, 0xd3f6fc16ebca5e03UL, // 1e-21 * 2**197
#| 0xbce5086492111aeaUL, 0x88f4bb1ca6bcf584UL, // 1e-20 * 2**194
#| 0xec1e4a7db69561a5UL, 0x2b31e9e3d06c32e5UL, // 1e-19 * 2**191
#| 0x9392ee8e921d5d07UL, 0x3aff322e62439fcfUL, // 1e-18 * 2**187
#| 0xb877aa3236a4b449UL, 0x09befeb9fad487c2UL, // 1e-17 * 2**184
#| 0xe69594bec44de15bUL, 0x4c2ebe687989a9b3UL, // 1e-16 * 2**181
#| 0x901d7cf73ab0acd9UL, 0x0f9d37014bf60a10UL, // 1e-15 * 2**177
#| 0xb424dc35095cd80fUL, 0x538484c19ef38c94UL, // 1e-14 * 2**174
#| 0xe12e13424bb40e13UL, 0x2865a5f206b06fb9UL, // 1e-13 * 2**171
#| 0x8cbccc096f5088cbUL, 0xf93f87b7442e45d3UL, // 1e-12 * 2**167
#| 0xafebff0bcb24aafeUL, 0xf78f69a51539d748UL, // 1e-11 * 2**164
#| 0xdbe6fecebdedd5beUL, 0xb573440e5a884d1bUL, // 1e-10 * 2**161
#| 0x89705f4136b4a597UL, 0x31680a88f8953030UL, // 1e-9 * 2**157
#| 0xabcc77118461cefcUL, 0xfdc20d2b36ba7c3dUL, // 1e-8 * 2**154
#| 0xd6bf94d5e57a42bcUL, 0x3d32907604691b4cUL, // 1e-7 * 2**151
#| 0x8637bd05af6c69b5UL, 0xa63f9a49c2c1b10fUL, // 1e-6 * 2**147
#| 0xa7c5ac471b478423UL, 0x0fcf80dc33721d53UL, // 1e-5 * 2**144
#| 0xd1b71758e219652bUL, 0xd3c36113404ea4a8UL, // 1e-4 * 2**141
#| 0x83126e978d4fdf3bUL, 0x645a1cac083126e9UL, // 1e-3 * 2**137
#| 0xa3d70a3d70a3d70aUL, 0x3d70a3d70a3d70a3UL, // 1e-2 * 2**134
#| 0xccccccccccccccccUL, 0xccccccccccccccccUL, // 1e-1 * 2**131
#| 0x8000000000000000UL, 0x0000000000000000UL, // 1e0 * 2**127
#| 0xa000000000000000UL, 0x0000000000000000UL, // 1e1 * 2**124
#| 0xc800000000000000UL, 0x0000000000000000UL, // 1e2 * 2**121
#| 0xfa00000000000000UL, 0x0000000000000000UL, // 1e3 * 2**118
#| 0x9c40000000000000UL, 0x0000000000000000UL, // 1e4 * 2**114
#| 0xc350000000000000UL, 0x0000000000000000UL, // 1e5 * 2**111
#| 0xf424000000000000UL, 0x0000000000000000UL, // 1e6 * 2**108
#| 0x9896800000000000UL, 0x0000000000000000UL, // 1e7 * 2**104
#| 0xbebc200000000000UL, 0x0000000000000000UL, // 1e8 * 2**101
#| 0xee6b280000000000UL, 0x0000000000000000UL, // 1e9 * 2**98
#| 0x9502f90000000000UL, 0x0000000000000000UL, // 1e10 * 2**94
#| 0xba43b74000000000UL, 0x0000000000000000UL, // 1e11 * 2**91
#| 0xe8d4a51000000000UL, 0x0000000000000000UL, // 1e12 * 2**88
#| 0x9184e72a00000000UL, 0x0000000000000000UL, // 1e13 * 2**84
#| 0xb5e620f480000000UL, 0x0000000000000000UL, // 1e14 * 2**81
#| 0xe35fa931a0000000UL, 0x0000000000000000UL, // 1e15 * 2**78
#| 0x8e1bc9bf04000000UL, 0x0000000000000000UL, // 1e16 * 2**74
#| 0xb1a2bc2ec5000000UL, 0x0000000000000000UL, // 1e17 * 2**71
#| 0xde0b6b3a76400000UL, 0x0000000000000000UL, // 1e18 * 2**68
#| 0x8ac7230489e80000UL, 0x0000000000000000UL, // 1e19 * 2**64
#| 0xad78ebc5ac620000UL, 0x0000000000000000UL, // 1e20 * 2**61
#| 0xd8d726b7177a8000UL, 0x0000000000000000UL, // 1e21 * 2**58
#| 0x878678326eac9000UL, 0x0000000000000000UL, // 1e22 * 2**54
#| 0xa968163f0a57b400UL, 0x0000000000000000UL, // 1e23 * 2**51
#| 0xd3c21bcecceda100UL, 0x0000000000000000UL, // 1e24 * 2**48
#| 0x84595161401484a0UL, 0x0000000000000000UL, // 1e25 * 2**44
#| 0xa56fa5b99019a5c8UL, 0x0000000000000000UL, // 1e26 * 2**41
#| 0xcecb8f27f4200f3aUL, 0x0000000000000000UL, // 1e27 * 2**38
#| 0x813f3978f8940984UL, 0x4000000000000000UL, // 1e28 * 2**34
#| 0xa18f07d736b90be5UL, 0x5000000000000000UL, // 1e29 * 2**31
#| 0xc9f2c9cd04674edeUL, 0xa400000000000000UL, // 1e30 * 2**28
#| 0xfc6f7c4045812296UL, 0x4d00000000000000UL, // 1e31 * 2**25
#| 0x9dc5ada82b70b59dUL, 0xf020000000000000UL, // 1e32 * 2**21
#| 0xc5371912364ce305UL, 0x6c28000000000000UL, // 1e33 * 2**18
#| 0xf684df56c3e01bc6UL, 0xc732000000000000UL, // 1e34 * 2**15
#| 0x9a130b963a6c115cUL, 0x3c7f400000000000UL, // 1e35 * 2**11
#| 0xc097ce7bc90715b3UL, 0x4b9f100000000000UL, // 1e36 * 2**8
#| 0xf0bdc21abb48db20UL, 0x1e86d40000000000UL, // 1e37 * 2**5
#| 0x96769950b50d88f4UL, 0x1314448000000000UL, // 1e38 * 2**1
#| 0xbc143fa4e250eb31UL, 0x17d955a000000000UL, // 1e39 * 2**-2
#| 0xeb194f8e1ae525fdUL, 0x5dcfab0800000000UL, // 1e40 * 2**-5
#| 0x92efd1b8d0cf37beUL, 0x5aa1cae500000000UL, // 1e41 * 2**-9
#| 0xb7abc627050305adUL, 0xf14a3d9e40000000UL, // 1e42 * 2**-12
#| 0xe596b7b0c643c719UL, 0x6d9ccd05d0000000UL, // 1e43 * 2**-15
#| 0x8f7e32ce7bea5c6fUL, 0xe4820023a2000000UL, // 1e44 * 2**-19
#| 0xb35dbf821ae4f38bUL, 0xdda2802c8a800000UL, // 1e45 * 2**-22
#| 0xe0352f62a19e306eUL, 0xd50b2037ad200000UL, // 1e46 * 2**-25
#| 0x8c213d9da502de45UL, 0x4526f422cc340000UL, // 1e47 * 2**-29
#| 0xaf298d050e4395d6UL, 0x9670b12b7f410000UL, // 1e48 * 2**-32
#| 0xdaf3f04651d47b4cUL, 0x3c0cdd765f114000UL, // 1e49 * 2**-35
#| 0x88d8762bf324cd0fUL, 0xa5880a69fb6ac800UL, // 1e50 * 2**-39
#| 0xab0e93b6efee0053UL, 0x8eea0d047a457a00UL, // 1e51 * 2**-42
#| 0xd5d238a4abe98068UL, 0x72a4904598d6d880UL, // 1e52 * 2**-45
#| 0x85a36366eb71f041UL, 0x47a6da2b7f864750UL, // 1e53 * 2**-49
#| 0xa70c3c40a64e6c51UL, 0x999090b65f67d924UL, // 1e54 * 2**-52
#| 0xd0cf4b50cfe20765UL, 0xfff4b4e3f741cf6dUL, // 1e55 * 2**-55
#| 0x82818f1281ed449fUL, 0xbff8f10e7a8921a4UL, // 1e56 * 2**-59
#| 0xa321f2d7226895c7UL, 0xaff72d52192b6a0dUL, // 1e57 * 2**-62
#| 0xcbea6f8ceb02bb39UL, 0x9bf4f8a69f764490UL, // 1e58 * 2**-65
#| 0xfee50b7025c36a08UL, 0x02f236d04753d5b4UL, // 1e59 * 2**-68
#| 0x9f4f2726179a2245UL, 0x01d762422c946590UL, // 1e60 * 2**-72
#| 0xc722f0ef9d80aad6UL, 0x424d3ad2b7b97ef5UL, // 1e61 * 2**-75
#| 0xf8ebad2b84e0d58bUL, 0xd2e0898765a7deb2UL, // 1e62 * 2**-78
#| 0x9b934c3b330c8577UL, 0x63cc55f49f88eb2fUL, // 1e63 * 2**-82
#| 0xc2781f49ffcfa6d5UL, 0x3cbf6b71c76b25fbUL, // 1e64 * 2**-85
#| 0xf316271c7fc3908aUL, 0x8bef464e3945ef7aUL, // 1e65 * 2**-88
#| 0x97edd871cfda3a56UL, 0x97758bf0e3cbb5acUL, // 1e66 * 2**-92
#| 0xbde94e8e43d0c8ecUL, 0x3d52eeed1cbea317UL, // 1e67 * 2**-95
#| 0xed63a231d4c4fb27UL, 0x4ca7aaa863ee4bddUL, // 1e68 * 2**-98
#| 0x945e455f24fb1cf8UL, 0x8fe8caa93e74ef6aUL, // 1e69 * 2**-102
#| 0xb975d6b6ee39e436UL, 0xb3e2fd538e122b44UL, // 1e70 * 2**-105
#| 0xe7d34c64a9c85d44UL, 0x60dbbca87196b616UL, // 1e71 * 2**-108
#| 0x90e40fbeea1d3a4aUL, 0xbc8955e946fe31cdUL, // 1e72 * 2**-112
#| 0xb51d13aea4a488ddUL, 0x6babab6398bdbe41UL, // 1e73 * 2**-115
#| 0xe264589a4dcdab14UL, 0xc696963c7eed2dd1UL, // 1e74 * 2**-118
#| 0x8d7eb76070a08aecUL, 0xfc1e1de5cf543ca2UL, // 1e75 * 2**-122
#| 0xb0de65388cc8ada8UL, 0x3b25a55f43294bcbUL, // 1e76 * 2**-125
#| 0xdd15fe86affad912UL, 0x49ef0eb713f39ebeUL, // 1e77 * 2**-128
#| 0x8a2dbf142dfcc7abUL, 0x6e3569326c784337UL, // 1e78 * 2**-132
#| 0xacb92ed9397bf996UL, 0x49c2c37f07965404UL, // 1e79 * 2**-135
#| 0xd7e77a8f87daf7fbUL, 0xdc33745ec97be906UL, // 1e80 * 2**-138
#| 0x86f0ac99b4e8dafdUL, 0x69a028bb3ded71a3UL, // 1e81 * 2**-142
#| 0xa8acd7c0222311bcUL, 0xc40832ea0d68ce0cUL, // 1e82 * 2**-145
#| 0xd2d80db02aabd62bUL, 0xf50a3fa490c30190UL, // 1e83 * 2**-148
#| 0x83c7088e1aab65dbUL, 0x792667c6da79e0faUL, // 1e84 * 2**-152
#| 0xa4b8cab1a1563f52UL, 0x577001b891185938UL, // 1e85 * 2**-155
#| 0xcde6fd5e09abcf26UL, 0xed4c0226b55e6f86UL, // 1e86 * 2**-158
#| 0x80b05e5ac60b6178UL, 0x544f8158315b05b4UL, // 1e87 * 2**-162
#| 0xa0dc75f1778e39d6UL, 0x696361ae3db1c721UL, // 1e88 * 2**-165
#| 0xc913936dd571c84cUL, 0x03bc3a19cd1e38e9UL, // 1e89 * 2**-168
#| 0xfb5878494ace3a5fUL, 0x04ab48a04065c723UL, // 1e90 * 2**-171
#| 0x9d174b2dcec0e47bUL, 0x62eb0d64283f9c76UL, // 1e91 * 2**-175
#| 0xc45d1df942711d9aUL, 0x3ba5d0bd324f8394UL, // 1e92 * 2**-178
#| 0xf5746577930d6500UL, 0xca8f44ec7ee36479UL, // 1e93 * 2**-181
#| 0x9968bf6abbe85f20UL, 0x7e998b13cf4e1ecbUL, // 1e94 * 2**-185
#| 0xbfc2ef456ae276e8UL, 0x9e3fedd8c321a67eUL, // 1e95 * 2**-188
#| 0xefb3ab16c59b14a2UL, 0xc5cfe94ef3ea101eUL, // 1e96 * 2**-191
#| 0x95d04aee3b80ece5UL, 0xbba1f1d158724a12UL, // 1e97 * 2**-195
#| 0xbb445da9ca61281fUL, 0x2a8a6e45ae8edc97UL, // 1e98 * 2**-198
#| 0xea1575143cf97226UL, 0xf52d09d71a3293bdUL, // 1e99 * 2**-201
#| 0x924d692ca61be758UL, 0x593c2626705f9c56UL, // 1e100 * 2**-205
#| 0xb6e0c377cfa2e12eUL, 0x6f8b2fb00c77836cUL, // 1e101 * 2**-208
#| 0xe498f455c38b997aUL, 0x0b6dfb9c0f956447UL, // 1e102 * 2**-211
#| 0x8edf98b59a373fecUL, 0x4724bd4189bd5eacUL, // 1e103 * 2**-215
#| 0xb2977ee300c50fe7UL, 0x58edec91ec2cb657UL, // 1e104 * 2**-218
#| 0xdf3d5e9bc0f653e1UL, 0x2f2967b66737e3edUL, // 1e105 * 2**-221
#| 0x8b865b215899f46cUL, 0xbd79e0d20082ee74UL, // 1e106 * 2**-225
#| 0xae67f1e9aec07187UL, 0xecd8590680a3aa11UL, // 1e107 * 2**-228
#| 0xda01ee641a708de9UL, 0xe80e6f4820cc9495UL, // 1e108 * 2**-231
#| 0x884134fe908658b2UL, 0x3109058d147fdcddUL, // 1e109 * 2**-235
#| 0xaa51823e34a7eedeUL, 0xbd4b46f0599fd415UL, // 1e110 * 2**-238
#| 0xd4e5e2cdc1d1ea96UL, 0x6c9e18ac7007c91aUL, // 1e111 * 2**-241
#| 0x850fadc09923329eUL, 0x03e2cf6bc604ddb0UL, // 1e112 * 2**-245
#| 0xa6539930bf6bff45UL, 0x84db8346b786151cUL, // 1e113 * 2**-248
#| 0xcfe87f7cef46ff16UL, 0xe612641865679a63UL, // 1e114 * 2**-251
#| 0x81f14fae158c5f6eUL, 0x4fcb7e8f3f60c07eUL, // 1e115 * 2**-255
#| 0xa26da3999aef7749UL, 0xe3be5e330f38f09dUL, // 1e116 * 2**-258
#| 0xcb090c8001ab551cUL, 0x5cadf5bfd3072cc5UL, // 1e117 * 2**-261
#| 0xfdcb4fa002162a63UL, 0x73d9732fc7c8f7f6UL, // 1e118 * 2**-264
#| 0x9e9f11c4014dda7eUL, 0x2867e7fddcdd9afaUL, // 1e119 * 2**-268
#| 0xc646d63501a1511dUL, 0xb281e1fd541501b8UL, // 1e120 * 2**-271
#| 0xf7d88bc24209a565UL, 0x1f225a7ca91a4226UL, // 1e121 * 2**-274
#| 0x9ae757596946075fUL, 0x3375788de9b06958UL, // 1e122 * 2**-278
#| 0xc1a12d2fc3978937UL, 0x0052d6b1641c83aeUL, // 1e123 * 2**-281
#| 0xf209787bb47d6b84UL, 0xc0678c5dbd23a49aUL, // 1e124 * 2**-284
#| 0x9745eb4d50ce6332UL, 0xf840b7ba963646e0UL, // 1e125 * 2**-288
#| 0xbd176620a501fbffUL, 0xb650e5a93bc3d898UL, // 1e126 * 2**-291
#| 0xec5d3fa8ce427affUL, 0xa3e51f138ab4cebeUL, // 1e127 * 2**-294
#| 0x93ba47c980e98cdfUL, 0xc66f336c36b10137UL, // 1e128 * 2**-298
#| 0xb8a8d9bbe123f017UL, 0xb80b0047445d4184UL, // 1e129 * 2**-301
#| 0xe6d3102ad96cec1dUL, 0xa60dc059157491e5UL, // 1e130 * 2**-304
#| 0x9043ea1ac7e41392UL, 0x87c89837ad68db2fUL, // 1e131 * 2**-308
#| 0xb454e4a179dd1877UL, 0x29babe4598c311fbUL, // 1e132 * 2**-311
#| 0xe16a1dc9d8545e94UL, 0xf4296dd6fef3d67aUL, // 1e133 * 2**-314
#| 0x8ce2529e2734bb1dUL, 0x1899e4a65f58660cUL, // 1e134 * 2**-318
#| 0xb01ae745b101e9e4UL, 0x5ec05dcff72e7f8fUL, // 1e135 * 2**-321
#| 0xdc21a1171d42645dUL, 0x76707543f4fa1f73UL, // 1e136 * 2**-324
#| 0x899504ae72497ebaUL, 0x6a06494a791c53a8UL, // 1e137 * 2**-328
#| 0xabfa45da0edbde69UL, 0x0487db9d17636892UL, // 1e138 * 2**-331
#| 0xd6f8d7509292d603UL, 0x45a9d2845d3c42b6UL, // 1e139 * 2**-334
#| 0x865b86925b9bc5c2UL, 0x0b8a2392ba45a9b2UL, // 1e140 * 2**-338
#| 0xa7f26836f282b732UL, 0x8e6cac7768d7141eUL, // 1e141 * 2**-341
#| 0xd1ef0244af2364ffUL, 0x3207d795430cd926UL, // 1e142 * 2**-344
#| 0x8335616aed761f1fUL, 0x7f44e6bd49e807b8UL, // 1e143 * 2**-348
#| 0xa402b9c5a8d3a6e7UL, 0x5f16206c9c6209a6UL, // 1e144 * 2**-351
#| 0xcd036837130890a1UL, 0x36dba887c37a8c0fUL, // 1e145 * 2**-354
#| 0x802221226be55a64UL, 0xc2494954da2c9789UL, // 1e146 * 2**-358
#| 0xa02aa96b06deb0fdUL, 0xf2db9baa10b7bd6cUL, // 1e147 * 2**-361
#| 0xc83553c5c8965d3dUL, 0x6f92829494e5acc7UL, // 1e148 * 2**-364
#| 0xfa42a8b73abbf48cUL, 0xcb772339ba1f17f9UL, // 1e149 * 2**-367
#| 0x9c69a97284b578d7UL, 0xff2a760414536efbUL, // 1e150 * 2**-371
#| 0xc38413cf25e2d70dUL, 0xfef5138519684abaUL, // 1e151 * 2**-374
#| 0xf46518c2ef5b8cd1UL, 0x7eb258665fc25d69UL, // 1e152 * 2**-377
#| 0x98bf2f79d5993802UL, 0xef2f773ffbd97a61UL, // 1e153 * 2**-381
#| 0xbeeefb584aff8603UL, 0xaafb550ffacfd8faUL, // 1e154 * 2**-384
#| 0xeeaaba2e5dbf6784UL, 0x95ba2a53f983cf38UL, // 1e155 * 2**-387
#| 0x952ab45cfa97a0b2UL, 0xdd945a747bf26183UL, // 1e156 * 2**-391
#| 0xba756174393d88dfUL, 0x94f971119aeef9e4UL, // 1e157 * 2**-394
#| 0xe912b9d1478ceb17UL, 0x7a37cd5601aab85dUL, // 1e158 * 2**-397
#| 0x91abb422ccb812eeUL, 0xac62e055c10ab33aUL, // 1e159 * 2**-401
#| 0xb616a12b7fe617aaUL, 0x577b986b314d6009UL, // 1e160 * 2**-404
#| 0xe39c49765fdf9d94UL, 0xed5a7e85fda0b80bUL, // 1e161 * 2**-407
#| 0x8e41ade9fbebc27dUL, 0x14588f13be847307UL, // 1e162 * 2**-411
#| 0xb1d219647ae6b31cUL, 0x596eb2d8ae258fc8UL, // 1e163 * 2**-414
#| 0xde469fbd99a05fe3UL, 0x6fca5f8ed9aef3bbUL, // 1e164 * 2**-417
#| 0x8aec23d680043beeUL, 0x25de7bb9480d5854UL, // 1e165 * 2**-421
#| 0xada72ccc20054ae9UL, 0xaf561aa79a10ae6aUL, // 1e166 * 2**-424
#| 0xd910f7ff28069da4UL, 0x1b2ba1518094da04UL, // 1e167 * 2**-427
#| 0x87aa9aff79042286UL, 0x90fb44d2f05d0842UL, // 1e168 * 2**-431
#| 0xa99541bf57452b28UL, 0x353a1607ac744a53UL, // 1e169 * 2**-434
#| 0xd3fa922f2d1675f2UL, 0x42889b8997915ce8UL, // 1e170 * 2**-437
#| 0x847c9b5d7c2e09b7UL, 0x69956135febada11UL, // 1e171 * 2**-441
#| 0xa59bc234db398c25UL, 0x43fab9837e699095UL, // 1e172 * 2**-444
#| 0xcf02b2c21207ef2eUL, 0x94f967e45e03f4bbUL, // 1e173 * 2**-447
#| 0x8161afb94b44f57dUL, 0x1d1be0eebac278f5UL, // 1e174 * 2**-451
#| 0xa1ba1ba79e1632dcUL, 0x6462d92a69731732UL, // 1e175 * 2**-454
#| 0xca28a291859bbf93UL, 0x7d7b8f7503cfdcfeUL, // 1e176 * 2**-457
#| 0xfcb2cb35e702af78UL, 0x5cda735244c3d43eUL, // 1e177 * 2**-460
#| 0x9defbf01b061adabUL, 0x3a0888136afa64a7UL, // 1e178 * 2**-464
#| 0xc56baec21c7a1916UL, 0x088aaa1845b8fdd0UL, // 1e179 * 2**-467
#| 0xf6c69a72a3989f5bUL, 0x8aad549e57273d45UL, // 1e180 * 2**-470
#| 0x9a3c2087a63f6399UL, 0x36ac54e2f678864bUL, // 1e181 * 2**-474
#| 0xc0cb28a98fcf3c7fUL, 0x84576a1bb416a7ddUL, // 1e182 * 2**-477
#| 0xf0fdf2d3f3c30b9fUL, 0x656d44a2a11c51d5UL, // 1e183 * 2**-480
#| 0x969eb7c47859e743UL, 0x9f644ae5a4b1b325UL, // 1e184 * 2**-484
#| 0xbc4665b596706114UL, 0x873d5d9f0dde1feeUL, // 1e185 * 2**-487
#| 0xeb57ff22fc0c7959UL, 0xa90cb506d155a7eaUL, // 1e186 * 2**-490
#| 0x9316ff75dd87cbd8UL, 0x09a7f12442d588f2UL, // 1e187 * 2**-494
#| 0xb7dcbf5354e9beceUL, 0x0c11ed6d538aeb2fUL, // 1e188 * 2**-497
#| 0xe5d3ef282a242e81UL, 0x8f1668c8a86da5faUL, // 1e189 * 2**-500
#| 0x8fa475791a569d10UL, 0xf96e017d694487bcUL, // 1e190 * 2**-504
#| 0xb38d92d760ec4455UL, 0x37c981dcc395a9acUL, // 1e191 * 2**-507
#| 0xe070f78d3927556aUL, 0x85bbe253f47b1417UL, // 1e192 * 2**-510
#| 0x8c469ab843b89562UL, 0x93956d7478ccec8eUL, // 1e193 * 2**-514
#| 0xaf58416654a6babbUL, 0x387ac8d1970027b2UL, // 1e194 * 2**-517
#| 0xdb2e51bfe9d0696aUL, 0x06997b05fcc0319eUL, // 1e195 * 2**-520
#| 0x88fcf317f22241e2UL, 0x441fece3bdf81f03UL, // 1e196 * 2**-524
#| 0xab3c2fddeeaad25aUL, 0xd527e81cad7626c3UL, // 1e197 * 2**-527
#| 0xd60b3bd56a5586f1UL, 0x8a71e223d8d3b074UL, // 1e198 * 2**-530
#| 0x85c7056562757456UL, 0xf6872d5667844e49UL, // 1e199 * 2**-534
#| 0xa738c6bebb12d16cUL, 0xb428f8ac016561dbUL, // 1e200 * 2**-537
#| 0xd106f86e69d785c7UL, 0xe13336d701beba52UL, // 1e201 * 2**-540
#| 0x82a45b450226b39cUL, 0xecc0024661173473UL, // 1e202 * 2**-544
#| 0xa34d721642b06084UL, 0x27f002d7f95d0190UL, // 1e203 * 2**-547
#| 0xcc20ce9bd35c78a5UL, 0x31ec038df7b441f4UL, // 1e204 * 2**-550
#| 0xff290242c83396ceUL, 0x7e67047175a15271UL, // 1e205 * 2**-553
#| 0x9f79a169bd203e41UL, 0x0f0062c6e984d386UL, // 1e206 * 2**-557
#| 0xc75809c42c684dd1UL, 0x52c07b78a3e60868UL, // 1e207 * 2**-560
#| 0xf92e0c3537826145UL, 0xa7709a56ccdf8a82UL, // 1e208 * 2**-563
#| 0x9bbcc7a142b17ccbUL, 0x88a66076400bb691UL, // 1e209 * 2**-567
#| 0xc2abf989935ddbfeUL, 0x6acff893d00ea435UL, // 1e210 * 2**-570
#| 0xf356f7ebf83552feUL, 0x0583f6b8c4124d43UL, // 1e211 * 2**-573
#| 0x98165af37b2153deUL, 0xc3727a337a8b704aUL, // 1e212 * 2**-577
#| 0xbe1bf1b059e9a8d6UL, 0x744f18c0592e4c5cUL, // 1e213 * 2**-580
#| 0xeda2ee1c7064130cUL, 0x1162def06f79df73UL, // 1e214 * 2**-583
#| 0x9485d4d1c63e8be7UL, 0x8addcb5645ac2ba8UL, // 1e215 * 2**-587
#| 0xb9a74a0637ce2ee1UL, 0x6d953e2bd7173692UL, // 1e216 * 2**-590
#| 0xe8111c87c5c1ba99UL, 0xc8fa8db6ccdd0437UL, // 1e217 * 2**-593
#| 0x910ab1d4db9914a0UL, 0x1d9c9892400a22a2UL, // 1e218 * 2**-597
#| 0xb54d5e4a127f59c8UL, 0x2503beb6d00cab4bUL, // 1e219 * 2**-600
#| 0xe2a0b5dc971f303aUL, 0x2e44ae64840fd61dUL, // 1e220 * 2**-603
#| 0x8da471a9de737e24UL, 0x5ceaecfed289e5d2UL, // 1e221 * 2**-607
#| 0xb10d8e1456105dadUL, 0x7425a83e872c5f47UL, // 1e222 * 2**-610
#| 0xdd50f1996b947518UL, 0xd12f124e28f77719UL, // 1e223 * 2**-613
#| 0x8a5296ffe33cc92fUL, 0x82bd6b70d99aaa6fUL, // 1e224 * 2**-617
#| 0xace73cbfdc0bfb7bUL, 0x636cc64d1001550bUL, // 1e225 * 2**-620
#| 0xd8210befd30efa5aUL, 0x3c47f7e05401aa4eUL, // 1e226 * 2**-623
#| 0x8714a775e3e95c78UL, 0x65acfaec34810a71UL, // 1e227 * 2**-627
#| 0xa8d9d1535ce3b396UL, 0x7f1839a741a14d0dUL, // 1e228 * 2**-630
#| 0xd31045a8341ca07cUL, 0x1ede48111209a050UL, // 1e229 * 2**-633
#| 0x83ea2b892091e44dUL, 0x934aed0aab460432UL, // 1e230 * 2**-637
#| 0xa4e4b66b68b65d60UL, 0xf81da84d5617853fUL, // 1e231 * 2**-640
#| 0xce1de40642e3f4b9UL, 0x36251260ab9d668eUL, // 1e232 * 2**-643
#| 0x80d2ae83e9ce78f3UL, 0xc1d72b7c6b426019UL, // 1e233 * 2**-647
#| 0xa1075a24e4421730UL, 0xb24cf65b8612f81fUL, // 1e234 * 2**-650
#| 0xc94930ae1d529cfcUL, 0xdee033f26797b627UL, // 1e235 * 2**-653
#| 0xfb9b7cd9a4a7443cUL, 0x169840ef017da3b1UL, // 1e236 * 2**-656
#| 0x9d412e0806e88aa5UL, 0x8e1f289560ee864eUL, // 1e237 * 2**-660
#| 0xc491798a08a2ad4eUL, 0xf1a6f2bab92a27e2UL, // 1e238 * 2**-663
#| 0xf5b5d7ec8acb58a2UL, 0xae10af696774b1dbUL, // 1e239 * 2**-666
#| 0x9991a6f3d6bf1765UL, 0xacca6da1e0a8ef29UL, // 1e240 * 2**-670
#| 0xbff610b0cc6edd3fUL, 0x17fd090a58d32af3UL, // 1e241 * 2**-673
#| 0xeff394dcff8a948eUL, 0xddfc4b4cef07f5b0UL, // 1e242 * 2**-676
#| 0x95f83d0a1fb69cd9UL, 0x4abdaf101564f98eUL, // 1e243 * 2**-680
#| 0xbb764c4ca7a4440fUL, 0x9d6d1ad41abe37f1UL, // 1e244 * 2**-683
#| 0xea53df5fd18d5513UL, 0x84c86189216dc5edUL, // 1e245 * 2**-686
#| 0x92746b9be2f8552cUL, 0x32fd3cf5b4e49bb4UL, // 1e246 * 2**-690
#| 0xb7118682dbb66a77UL, 0x3fbc8c33221dc2a1UL, // 1e247 * 2**-693
#| 0xe4d5e82392a40515UL, 0x0fabaf3feaa5334aUL, // 1e248 * 2**-696
#| 0x8f05b1163ba6832dUL, 0x29cb4d87f2a7400eUL, // 1e249 * 2**-700
#| 0xb2c71d5bca9023f8UL, 0x743e20e9ef511012UL, // 1e250 * 2**-703
#| 0xdf78e4b2bd342cf6UL, 0x914da9246b255416UL, // 1e251 * 2**-706
#| 0x8bab8eefb6409c1aUL, 0x1ad089b6c2f7548eUL, // 1e252 * 2**-710
#| 0xae9672aba3d0c320UL, 0xa184ac2473b529b1UL, // 1e253 * 2**-713
#| 0xda3c0f568cc4f3e8UL, 0xc9e5d72d90a2741eUL, // 1e254 * 2**-716
#| 0x8865899617fb1871UL, 0x7e2fa67c7a658892UL, // 1e255 * 2**-720
#| 0xaa7eebfb9df9de8dUL, 0xddbb901b98feeab7UL, // 1e256 * 2**-723
#| 0xd51ea6fa85785631UL, 0x552a74227f3ea565UL, // 1e257 * 2**-726
#| 0x8533285c936b35deUL, 0xd53a88958f87275fUL, // 1e258 * 2**-730
#| 0xa67ff273b8460356UL, 0x8a892abaf368f137UL, // 1e259 * 2**-733
#| 0xd01fef10a657842cUL, 0x2d2b7569b0432d85UL, // 1e260 * 2**-736
#| 0x8213f56a67f6b29bUL, 0x9c3b29620e29fc73UL, // 1e261 * 2**-740
#| 0xa298f2c501f45f42UL, 0x8349f3ba91b47b8fUL, // 1e262 * 2**-743
#| 0xcb3f2f7642717713UL, 0x241c70a936219a73UL, // 1e263 * 2**-746
#| 0xfe0efb53d30dd4d7UL, 0xed238cd383aa0110UL, // 1e264 * 2**-749
#| 0x9ec95d1463e8a506UL, 0xf4363804324a40aaUL, // 1e265 * 2**-753
#| 0xc67bb4597ce2ce48UL, 0xb143c6053edcd0d5UL, // 1e266 * 2**-756
#| 0xf81aa16fdc1b81daUL, 0xdd94b7868e94050aUL, // 1e267 * 2**-759
#| 0x9b10a4e5e9913128UL, 0xca7cf2b4191c8326UL, // 1e268 * 2**-763
#| 0xc1d4ce1f63f57d72UL, 0xfd1c2f611f63a3f0UL, // 1e269 * 2**-766
#| 0xf24a01a73cf2dccfUL, 0xbc633b39673c8cecUL, // 1e270 * 2**-769
#| 0x976e41088617ca01UL, 0xd5be0503e085d813UL, // 1e271 * 2**-773
#| 0xbd49d14aa79dbc82UL, 0x4b2d8644d8a74e18UL, // 1e272 * 2**-776
#| 0xec9c459d51852ba2UL, 0xddf8e7d60ed1219eUL, // 1e273 * 2**-779
#| 0x93e1ab8252f33b45UL, 0xcabb90e5c942b503UL, // 1e274 * 2**-783
#| 0xb8da1662e7b00a17UL, 0x3d6a751f3b936243UL, // 1e275 * 2**-786
#| 0xe7109bfba19c0c9dUL, 0x0cc512670a783ad4UL, // 1e276 * 2**-789
#| 0x906a617d450187e2UL, 0x27fb2b80668b24c5UL, // 1e277 * 2**-793
#| 0xb484f9dc9641e9daUL, 0xb1f9f660802dedf6UL, // 1e278 * 2**-796
#| 0xe1a63853bbd26451UL, 0x5e7873f8a0396973UL, // 1e279 * 2**-799
#| 0x8d07e33455637eb2UL, 0xdb0b487b6423e1e8UL, // 1e280 * 2**-803
#| 0xb049dc016abc5e5fUL, 0x91ce1a9a3d2cda62UL, // 1e281 * 2**-806
#| 0xdc5c5301c56b75f7UL, 0x7641a140cc7810fbUL, // 1e282 * 2**-809
#| 0x89b9b3e11b6329baUL, 0xa9e904c87fcb0a9dUL, // 1e283 * 2**-813
#| 0xac2820d9623bf429UL, 0x546345fa9fbdcd44UL, // 1e284 * 2**-816
#| 0xd732290fbacaf133UL, 0xa97c177947ad4095UL, // 1e285 * 2**-819
#| 0x867f59a9d4bed6c0UL, 0x49ed8eabcccc485dUL, // 1e286 * 2**-823
#| 0xa81f301449ee8c70UL, 0x5c68f256bfff5a74UL, // 1e287 * 2**-826
#| 0xd226fc195c6a2f8cUL, 0x73832eec6fff3111UL, // 1e288 * 2**-829
#| 0x83585d8fd9c25db7UL, 0xc831fd53c5ff7eabUL, // 1e289 * 2**-833
#| 0xa42e74f3d032f525UL, 0xba3e7ca8b77f5e55UL, // 1e290 * 2**-836
#| 0xcd3a1230c43fb26fUL, 0x28ce1bd2e55f35ebUL, // 1e291 * 2**-839
#| 0x80444b5e7aa7cf85UL, 0x7980d163cf5b81b3UL, // 1e292 * 2**-843
#| 0xa0555e361951c366UL, 0xd7e105bcc332621fUL, // 1e293 * 2**-846
#| 0xc86ab5c39fa63440UL, 0x8dd9472bf3fefaa7UL, // 1e294 * 2**-849
#| 0xfa856334878fc150UL, 0xb14f98f6f0feb951UL, // 1e295 * 2**-852
#| 0x9c935e00d4b9d8d2UL, 0x6ed1bf9a569f33d3UL, // 1e296 * 2**-856
#| 0xc3b8358109e84f07UL, 0x0a862f80ec4700c8UL, // 1e297 * 2**-859
#| 0xf4a642e14c6262c8UL, 0xcd27bb612758c0faUL, // 1e298 * 2**-862
#| 0x98e7e9cccfbd7dbdUL, 0x8038d51cb897789cUL, // 1e299 * 2**-866
#| 0xbf21e44003acdd2cUL, 0xe0470a63e6bd56c3UL, // 1e300 * 2**-869
#| 0xeeea5d5004981478UL, 0x1858ccfce06cac74UL, // 1e301 * 2**-872
#| 0x95527a5202df0ccbUL, 0x0f37801e0c43ebc8UL, // 1e302 * 2**-876
#| 0xbaa718e68396cffdUL, 0xd30560258f54e6baUL, // 1e303 * 2**-879
#| 0xe950df20247c83fdUL, 0x47c6b82ef32a2069UL, // 1e304 * 2**-882
#| 0x91d28b7416cdd27eUL, 0x4cdc331d57fa5441UL, // 1e305 * 2**-886
#| 0xb6472e511c81471dUL, 0xe0133fe4adf8e952UL, // 1e306 * 2**-889
#| 0xe3d8f9e563a198e5UL, 0x58180fddd97723a6UL, // 1e307 * 2**-892
#| 0x8e679c2f5e44ff8fUL, 0x570f09eaa7ea7648UL, // 1e308 * 2**-896
#| 0xb201833b35d63f73UL, 0x2cd2cc6551e513daUL, // 1e309 * 2**-899
#| 0xde81e40a034bcf4fUL, 0xf8077f7ea65e58d1UL, // 1e310 * 2**-902
#| 0x8b112e86420f6191UL, 0xfb04afaf27faf782UL, // 1e311 * 2**-906
#| 0xadd57a27d29339f6UL, 0x79c5db9af1f9b563UL, // 1e312 * 2**-909
#| 0xd94ad8b1c7380874UL, 0x18375281ae7822bcUL, // 1e313 * 2**-912
#| 0x87cec76f1c830548UL, 0x8f2293910d0b15b5UL, // 1e314 * 2**-916
#| 0xa9c2794ae3a3c69aUL, 0xb2eb3875504ddb22UL, // 1e315 * 2**-919
#| 0xd433179d9c8cb841UL, 0x5fa60692a46151ebUL, // 1e316 * 2**-922
#| 0x849feec281d7f328UL, 0xdbc7c41ba6bcd333UL, // 1e317 * 2**-926
#| 0xa5c7ea73224deff3UL, 0x12b9b522906c0800UL, // 1e318 * 2**-929
#| 0xcf39e50feae16befUL, 0xd768226b34870a00UL, // 1e319 * 2**-932
#| 0x81842f29f2cce375UL, 0xe6a1158300d46640UL, // 1e320 * 2**-936
#| 0xa1e53af46f801c53UL, 0x60495ae3c1097fd0UL, // 1e321 * 2**-939
#| 0xca5e89b18b602368UL, 0x385bb19cb14bdfc4UL, // 1e322 * 2**-942
#| 0xfcf62c1dee382c42UL, 0x46729e03dd9ed7b5UL, // 1e323 * 2**-945
#| 0x9e19db92b4e31ba9UL, 0x6c07a2c26a8346d1UL, // 1e324 * 2**-949
#| 0xc5a05277621be293UL, 0xc7098b7305241885UL, // 1e325 * 2**-952
#| 0xf70867153aa2db38UL, 0xb8cbee4fc66d1ea7UL, // 1e326 * 2**-955
#| 0x9a65406d44a5c903UL, 0x737f74f1dc043328UL, // 1e327 * 2**-959
#| 0xc0fe908895cf3b44UL, 0x505f522e53053ff2UL, // 1e328 * 2**-962
#| 0xf13e34aabb430a15UL, 0x647726b9e7c68fefUL, // 1e329 * 2**-965
#| 0x96c6e0eab509e64dUL, 0x5eca783430dc19f5UL, // 1e330 * 2**-969
#| 0xbc789925624c5fe0UL, 0xb67d16413d132072UL, // 1e331 * 2**-972
#| 0xeb96bf6ebadf77d8UL, 0xe41c5bd18c57e88fUL, // 1e332 * 2**-975
#| 0x933e37a534cbaae7UL, 0x8e91b962f7b6f159UL, // 1e333 * 2**-979
#| 0xb80dc58e81fe95a1UL, 0x723627bbb5a4adb0UL, // 1e334 * 2**-982
#| 0xe61136f2227e3b09UL, 0xcec3b1aaa30dd91cUL, // 1e335 * 2**-985
#| 0x8fcac257558ee4e6UL, 0x213a4f0aa5e8a7b1UL, // 1e336 * 2**-989
#| 0xb3bd72ed2af29e1fUL, 0xa988e2cd4f62d19dUL, // 1e337 * 2**-992
#| 0xe0accfa875af45a7UL, 0x93eb1b80a33b8605UL, // 1e338 * 2**-995
#| 0x8c6c01c9498d8b88UL, 0xbc72f130660533c3UL, // 1e339 * 2**-999
#| 0xaf87023b9bf0ee6aUL, 0xeb8fad7c7f8680b4UL, // 1e340 * 2**-1002
#| 0xdb68c2ca82ed2a05UL, 0xa67398db9f6820e1UL, // 1e341 * 2**-1005
#| 0x892179be91d43a43UL, 0x88083f8943a1148cUL, // 1e342 * 2**-1009
#| 0xab69d82e364948d4UL, 0x6a0a4f6b948959b0UL, // 1e343 * 2**-1012
#| 0xd6444e39c3db9b09UL, 0x848ce34679abb01cUL, // 1e344 * 2**-1015
#| 0x85eab0e41a6940e5UL, 0xf2d80e0c0c0b4e11UL, // 1e345 * 2**-1019
#| 0xa7655d1d2103911fUL, 0x6f8e118f0f0e2195UL, // 1e346 * 2**-1022
#| 0xd13eb46469447567UL, 0x4b7195f2d2d1a9fbUL, // 1e347 * 2**-1025
#|]
),
"strconv_errors.mbt": (
#|let range_err_str = "value out of range"
#|let syntax_err_str = "invalid syntax"
#|let base_err_str = "invalid base"
#|fn[T] range_err() -> T raise {
#| raise Failure::Failure(range_err_str)
#|}
#|fn[T] syntax_err() -> T raise {
#| raise Failure::Failure(syntax_err_str)
#|}
#|fn[T] base_err() -> T raise {
#| raise Failure::Failure(base_err_str)
#|}
),
"strconv_int.mbt": (
#|const INT_MIN = 0x80000000
#|const INT_MAX = 0x7fffffff
#|const INT64_MIN = -0x8000000000000000L
#|const INT64_MAX = 0x7fffffffffffffffL
#|fn check_and_consume_base(
#| view : StringView,
#| base : Int32,
#|) -> (Int32, StringView, Bool) raise {
#| if base == 0 {
#| match view {
#| ['0', 'x' | 'X', .. rest] => (16, rest, true)
#| ['0', 'o' | 'O', .. rest] => (8, rest, true)
#| ['0', 'b' | 'B', .. rest] => (2, rest, true)
#| _ => (10, view, false)
#| }
#| } else {
#| match view {
#| ['0', 'x' | 'X', .. rest] if base == 16 => (16, rest, true)
#| ['0', 'o' | 'O', .. rest] if base == 8 => (8, rest, true)
#| ['0', 'b' | 'B', .. rest] if base == 2 => (2, rest, true)
#| _ => if base is (2..<=36) { (base, view, false) } else { base_err() }
#| }
#| }
#|}
#|#label_migration(radix, alias=base, msg="Use radix= instead")
#|pub fn parse_int64(str : StringView, radix~ : Int32 = 0) -> Int64 raise {
#| guard str != "" else { syntax_err() }
#| let (neg, rest) = match str {
#| ['+', .. rest] => (false, rest)
#| ['-', .. rest] => (true, rest)
#| rest => (false, rest)
#| }
#| let (num_base, rest, allow_underscore) = check_and_consume_base(rest, radix)
#| let overflow_threshold = overflow_threshold(num_base, neg)
#| let has_digit = rest
#| is (['0'..<='9' | 'a'..<='z' | 'A'..<='Z', ..]
#| | ['_', '0'..<='9' | 'a'..<='z' | 'A'..<='Z', ..])
#| guard has_digit else { syntax_err() }
#| for rest = rest, acc = 0L, allow_underscore = allow_underscore {
#| match (rest, acc, allow_underscore) {
#| (['_'], _, _) =>
#| syntax_err()
#| (['_', ..], _, false) => syntax_err()
#| (['_', .. rest], acc, true) => continue rest, acc, false
#| ([c, .. rest], acc, _) => {
#| let c = c.to_int()
#| let d = match c {
#| '0'..<='9' => c - '0'
#| 'a'..<='z' => c + (10 - 'a')
#| 'A'..<='Z' => c + (10 - 'A')
#| _ => syntax_err()
#| }
#| guard d < num_base else { syntax_err() }
#| if neg {
#| guard acc >= overflow_threshold else { range_err() }
#| let next_acc = acc * num_base.to_int64() - d.to_int64()
#| guard next_acc <= acc else { range_err() }
#| continue rest, next_acc, true
#| } else {
#| guard acc < overflow_threshold else { range_err() }
#| let next_acc = acc * num_base.to_int64() + d.to_int64()
#| guard next_acc >= acc else { range_err() }
#| continue rest, next_acc, true
#| }
#| }
#| ([], acc, _) => break acc
#| }
#| }
#|}
#|#label_migration(radix, alias=base, msg="Use radix= instead")
#|pub fn parse_int(str : StringView, radix~ : Int32 = 0) -> Int32 raise {
#| let n = parse_int64(str, radix~)
#| if n < INT_MIN.to_int64() || n > INT_MAX.to_int64() {
#| range_err()
#| }
#| n.to_int()
#|}
#|fn check_underscore(str : StringView) -> Bool {
#| guard str.contains_code_unit(UInt16('_')) else { return true }
#| let rest = match str {
#| ['+' | '-', .. rest] => rest
#| rest => rest
#| }
#| let (rest, allow_underscore, hex) = lexmatch rest with longest {
#| (re"^0[xX]", after=rest) => (rest, true, true)
#| (re"^0[oO]", after=rest) => (rest, true, false)
#| (re"^0[bB]", after=rest) => (rest, true, false)
#| _ => (rest, false, false)
#| }
#| let follow_underscore = false
#| for rest = rest, allow_underscore = allow_underscore, follow_underscore = follow_underscore {
#| match (rest, allow_underscore, follow_underscore) {
#| ([], _, _) => break true
#| (['_'], _, _) => break false
#| (['_', ..], false, _) => break false
#| (['_', .. rest], true, _) => continue rest, false, true
#| (['0'..<='9', .. rest], _, _) => continue rest, true, false
#| (['a'..<='f' | 'A'..<='F', .. rest], _, _) if hex =>
#| continue rest, true, false
#| (['.' | 'e' | 'E' | '+' | '-', ..], _, true) => break false
#| (['.' | 'e' | 'E' | '+' | '-', .. rest], _, _) =>
#| continue rest, false, false
#| ([_, ..], _, true) => break false
#| ([_, .. rest], _, _) => continue rest, false, false
#| }
#| }
#|}
#|fn overflow_threshold(base : Int32, neg : Bool) -> Int64 {
#| if !neg {
#| if base == 10 {
#| INT64_MAX / 10L + 1L
#| } else if base == 16 {
#| INT64_MAX / 16L + 1L
#| } else {
#| INT64_MAX / base.to_int64() + 1L
#| }
#| } else if base == 10 {
#| INT64_MIN / 10L
#| } else if base == 16 {
#| INT64_MIN / 16L
#| } else {
#| INT64_MIN / base.to_int64()
#| }
#|}
),
"strconv_number.mbt": (
#|let min_19digit_int : UInt64 = 100_0000_0000_0000_0000UL
#|priv struct Number {
#| exponent : Int64
#| mantissa : UInt64
#| negative : Bool
#| many_digits : Bool
#|}
#|fn parse_digits(s : StringView, x : UInt64) -> (StringView, UInt64, Int32) {
#| s.fold_digits(x, (digit, acc : UInt64) => {
#| acc * 10UL + digit.reinterpret_as_uint().to_uint64()
#| })
#|}
#|fn try_parse_19digits(
#| s : StringView,
#| x : UInt64,
#|) -> (StringView, UInt64, Int32) {
#| let mut x = x
#| let mut len = 0
#| for s = s {
#| match s {
#| ['0'..<='9' as ch, .. rest] if x < min_19digit_int => {
#| len += 1
#| x = x * 10UL + (ch.to_int() - '0').reinterpret_as_uint().to_uint64() // no overflows here
#| continue rest
#| }
#| ['_', .. rest] => continue rest
#| s => break (s, x, len)
#| }
#| }
#|}
#|fn parse_scientific(s : StringView) -> (StringView, Int64)? {
#| let mut s = s
#| let exp_num = 0L
#| let mut neg_exp = false
#| if s is ['+' | '-' as ch, .. rest] {
#| neg_exp = ch == '-'
#| s = rest
#| }
#| if s is ['0'..<='9', ..] {
#| let (s, exp_num, _) = s.fold_digits(exp_num, (digit, exp_num : Int64) => {
#| if exp_num < 0x10000L {
#| 10L * exp_num + digit.to_int64() // no overflows here
#| } else {
#| exp_num
#| }
#| })
#| if neg_exp {
#| Some((s, -exp_num))
#| } else {
#| Some((s, exp_num))
#| }
#| } else {
#| None
#| }
#|}
#|fn parse_number(s : StringView) -> Number? raise {
#| let start = s
#| let (s, negative) = match s {
#| ['-', .. rest] => (rest, true)
#| ['+', .. rest] | rest => (rest, false)
#| }
#| if s.is_empty() {
#| return None
#| }
#| let (s, mantissa, consumed) = parse_digits(s, 0UL)
#| let mut mantissa = mantissa
#| let mut s = s
#| let mut n_digits = consumed
#| let mut n_after_dot = 0
#| let mut exponent = 0L
#| if s is ['.', .. rest] {
#| s = rest
#| let (new_s, new_mantissa, consumed_digit) = parse_digits(s, mantissa)
#| s = new_s
#| mantissa = new_mantissa
#| n_after_dot = consumed_digit
#| exponent = -n_after_dot.to_int64()
#| }
#| n_digits += n_after_dot
#| if n_digits == 0 {
#| return None
#| }
#| let mut exp_number = 0L
#| if s is ['e' | 'E', .. rest] {
#| let (new_s, exp_number_val) = match parse_scientific(rest) {
#| Some(res) => res
#| None => return None
#| }
#| s = new_s
#| exp_number = exp_number_val
#| exponent += exp_number
#| }
#| guard s is "" else { syntax_err() }
#| if n_digits <= 19 {
#| return Some({ exponent, mantissa, negative, many_digits: false, })
#| }
#| n_digits -= 19
#| let mut many_digits = false
#| for s = start {
#| match s {
#| ['0' | '.' as ch, .. rest] => {
#| n_digits -= (ch.to_int() - 46) / 2 // '0' = b'.' + 2
#| continue rest
#| }
#| _ => break
#| }
#| }
#| if n_digits > 0 {
#| many_digits = true
#| mantissa = 0UL
#| let s = start
#| let (s, new_mantissa, consumed_digit) = try_parse_19digits(s, mantissa)
#| mantissa = new_mantissa
#| exponent = (if mantissa >= min_19digit_int {
#| consumed_digit // big int
#| } else {
#| guard s is [_, .. s] else { return None }
#| let (_, new_mantissa, consumed_digit) = try_parse_19digits(s, mantissa)
#| mantissa = new_mantissa
#| consumed_digit
#| }).to_int64()
#| exponent += exp_number
#| } // add back the explicit part
#| Some({ exponent, mantissa, negative, many_digits, })
#|}
#|fn parse_inf_nan(rest : StringView) -> Double raise {
#| let (pos, rest) = match rest {
#| ['-', .. rest] => (false, rest)
#| ['+', .. rest] | rest => (true, rest)
#| }
#| lexmatch rest with longest {
#| re"^(?i:nan)$" => @double.not_a_number
#| re"^(?i:inf(inity)?)$" =>
#| if pos {
#| @double.infinity
#| } else {
#| @double.neg_infinity
#| }
#| _ => syntax_err()
#| }
#|}
#|fn checked_mul(a : UInt64, b : UInt64) -> UInt64? {
#| if a == 0UL || b == 0UL {
#| return Some(0UL)
#| }
#| if a == 1UL {
#| return Some(b)
#| }
#| if b == 1UL {
#| return Some(a)
#| }
#| if b.clz() == 0 || a.clz() == 0 {
#| return None
#| }
#| let quotient : UInt64 = @uint64.MAX_VALUE / b
#| if a > quotient {
#| return None
#| }
#| Some(a * b)
#|}
),
"strconv_string_view.mbt": (
#|fn[T] StringView::fold_digits(
#| self : Self,
#| init : T,
#| f : (Int32, T) -> T,
#|) -> (Self, T, Int32) {
#| for str = self, ret = init, len = 0 {
#| match str {
#| ['0'..<='9' as ch, .. rest] =>
#| continue rest, f(ch.to_int() - '0', ret), len + 1
#| ['_', .. rest] => continue rest, ret, len
#| _ => break (str, ret, len)
#| }
#| }
#|}
),
"strconv_uint.mbt": (
#|const UINT_MAX : UInt32 = 0xffffffff
#|const UINT64_MAX : UInt64 = 0xffffffffffffffffUL
#|#label_migration(radix, alias=base, msg="Use radix= instead")
#|pub fn parse_uint64(str : StringView, radix~ : Int32 = 0) -> UInt64 raise {
#| guard str != "" else { syntax_err() }
#| if str is ['+' | '-', ..] {
#| syntax_err()
#| }
#| let (num_base, rest, allow_underscore) = check_and_consume_base(str, radix)
#| let overflow_threshold = match num_base {
#| 10 => UINT64_MAX / 10 + 1
#| 16 => UINT64_MAX / 16 + 1
#| _ => UINT64_MAX / num_base.to_uint64() + 1
#| }
#| let has_digit = rest
#| is (['0'..<='9' | 'a'..<='z' | 'A'..<='Z', ..]
#| | ['_', '0'..<='9' | 'a'..<='z' | 'A'..<='Z', ..])
#| guard has_digit else { syntax_err() }
#| for rest = rest, acc = 0UL, allow_underscore = allow_underscore {
#| match (rest, acc, allow_underscore) {
#| (['_'], _, _) =>
#| syntax_err()
#| (['_', ..], _, false) => syntax_err()
#| (['_', .. rest], acc, true) => continue rest, acc, false
#| ([c, .. rest], acc, _) => {
#| let c = c.to_int()
#| let d = match c {
#| '0'..<='9' => c - '0'
#| 'a'..<='z' => c + (10 - 'a')
#| 'A'..<='Z' => c + (10 - 'A')
#| _ => syntax_err()
#| }
#| guard d < num_base else { syntax_err() }
#| guard acc < overflow_threshold else { range_err() }
#| let next_acc = acc * num_base.to_uint64() + d.to_uint64()
#| guard next_acc >= acc && next_acc <= UINT64_MAX else { range_err() }
#| continue rest, next_acc, true
#| }
#| ([], acc, _) => break acc
#| }
#| }
#|}
#|#label_migration(radix, alias=base, msg="Use radix= instead")
#|pub fn parse_uint(str : StringView, radix~ : Int32 = 0) -> UInt32 raise {
#| let n = parse_uint64(str, radix~)
#| if n > UINT_MAX.to_uint64() {
#| range_err()
#| }
#| n.to_uint()
#|}
),
},
)
///|
let moonbitlang_core_json_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/json",
deps={},
files={
"deprecated.mbt": "",
"extends.mbt": (
#|pub extend Json with Eq::{equal}
#|pub extend JsonDecodeError with Eq::{equal}
#|pub extend JsonDecodeError with Show::{to_string}
#|pub extend DecodeContext with Eq::{equal}
#|pub extend DecodeContext with Show::{to_string}
#|pub extend ParseError with Eq::{equal}
#|pub extend ParseError with Show::{to_string}
#|pub extend Position with Eq::{equal}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Json with FromJson::{from_json}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend Json with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Json with ToJson::{to_json}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Json with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Json with Eq::{not_equal}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend JsonDecodeError with Eq::{not_equal}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend JsonDecodeError with ToJson::{to_json}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend DecodeContext with Eq::{not_equal}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend DecodeContext with ToJson::{to_json}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ParseError with Eq::{not_equal}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ParseError with ToJson::{to_json}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Position with Eq::{not_equal}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Position with ToJson::{to_json}
),
"from_json.mbt": (
#|#derive(Eq, Show, ToJson)
#|#warnings("-deprecated_syntax-deprecated")
#|pub(all) suberror JsonDecodeError {
#| JsonDecodeError((DecodeContext, String))
#|}
#|pub(open) trait FromJson {
#| fn from_json(Json, DecodeContext) -> Self raise JsonDecodeError
#|}
#|pub fn[T : FromJson] from_json(
#| json : Json,
#| path~ : DecodeContext = Root,
#|) -> T raise JsonDecodeError {
#| FromJson::from_json(json, path)
#|}
#|fn[T] decode_error(
#| path : DecodeContext,
#| msg : String,
#|) -> T raise JsonDecodeError {
#| raise path.error(msg)
#|}
#|pub impl FromJson for Bool with fn from_json(json, path) {
#| match json {
#| true => true
#| false => false
#| _ => decode_error(path, "Bool::from_json: expected boolean")
#| }
#|}
#|pub impl FromJson for Int32 with fn from_json(json, path) {
#| guard json is Number(n, ..) &&
#| n != @double.infinity &&
#| n != @double.neg_infinity else {
#| decode_error(path, "Int::from_json: expected number")
#| }
#| guard n == n.floor() else {
#| decode_error(path, "Int::from_json: expected integer")
#| }
#| let max_ok = 2147483647.0
#| let min_ok = -2147483648.0
#| if n > max_ok || n < min_ok {
#| decode_error(path, "Int::from_json: overflow")
#| }
#| n.to_int()
#|}
#|pub impl FromJson for Int64 with fn from_json(json, path) {
#| guard json is String(str) else {
#| decode_error(
#| path, "Int64::from_json: expected a string such as \"5\" (Int64 is encoded as a string to avoid precision loss)",
#| )
#| }
#| @internal/strconv.parse_int64(str) catch {
#| Failure::Failure(error) =>
#| decode_error(path, "Int64::from_json: parsing failure \{error}")
#| error => decode_error(path, "Int64::from_json: parsing failure \{error}")
#| }
#|}
#|pub impl FromJson for UInt32 with fn from_json(json, path) {
#| guard json is Number(n, ..) &&
#| n != @double.infinity &&
#| n != @double.neg_infinity else {
#| decode_error(path, "UInt::from_json: expected number")
#| }
#| guard n == n.floor() else {
#| decode_error(path, "UInt::from_json: expected integer")
#| }
#| let max_ok = 4294967295.0
#| if n < 0.0 || n > max_ok {
#| decode_error(path, "UInt::from_json: overflow")
#| }
#| n.to_uint()
#|}
#|pub impl FromJson for UInt64 with fn from_json(json, path) {
#| guard json is String(str) else {
#| decode_error(
#| path, "UInt64::from_json: expected a string such as \"5\" (UInt64 is encoded as a string to avoid precision loss)",
#| )
#| }
#| @internal/strconv.parse_uint64(str) catch {
#| Failure::Failure(error) =>
#| decode_error(path, "UInt64::from_json: parsing failure \{error}")
#| error => decode_error(path, "UInt64::from_json: parsing failure \{error}")
#| }
#|}
#|pub impl FromJson for Double with fn from_json(json, path) {
#| match json {
#| String("NaN") => @double.not_a_number
#| String("Infinity") => @double.infinity
#| String("-Infinity") => @double.neg_infinity
#| Number(n, ..) if n != @double.infinity && n != @double.neg_infinity => n
#| _ => decode_error(path, "Double::from_json: expected number")
#| }
#|}
#|pub impl FromJson for @float.Float with fn from_json(json, path) {
#| match json {
#| String("NaN") => @float.not_a_number
#| String("Infinity") => @float.infinity
#| String("-Infinity") => @float.neg_infinity
#| Number(n, ..) if n != @double.infinity && n != @double.neg_infinity =>
#| @float.Float::from_double(n)
#| _ => decode_error(path, "Float::from_json: expected number")
#| }
#|}
#|pub impl FromJson for String with fn from_json(json, path) {
#| guard json is String(a) else {
#| decode_error(path, "String::from_json: expected string")
#| }
#| a
#|}
#|pub impl FromJson for StringView with fn from_json(json, path) {
#| guard json is String(a) else {
#| decode_error(path, "View::from_json: expected string")
#| }
#| a
#|}
#|pub impl FromJson for Char with fn from_json(json, path) {
#| guard json is String(a) else {
#| decode_error(path, "Char::from_json: expected string")
#| }
#| match a {
#| [c] => c
#| _ => decode_error(path, "Char::from_json: expected single character")
#| }
#|}
#|pub impl[X : FromJson] FromJson for Array[X] with fn from_json(json, path) {
#| guard json is Array(a) else {
#| decode_error(path, "Array::from_json: expected array")
#| }
#| decode_elements(a, path)
#|}
#|fn[X : FromJson] decode_elements(
#| a : Array[Json],
#| path : DecodeContext,
#|) -> Array[X] raise JsonDecodeError {
#| guard! DecodeContext::Index(path, index=0) is (Index(_) as new_path)
#| let res : Array[X] = Array(capacity=a.length())
#| for i, x in a {
#| new_path.index = i
#| res.push(FromJson::from_json(x, new_path))
#| }
#| res
#|}
#|pub impl[X : FromJson] FromJson for ArrayView[X] with fn from_json(json, path) {
#| guard json is Array(a) else {
#| decode_error(path, "ArrayView::from_json: expected array")
#| }
#| decode_elements(a, path)
#|}
#|pub impl[X : FromJson] FromJson for FixedArray[X] with fn from_json(json, path) {
#| guard json is Array(a) else {
#| decode_error(path, "FixedArray::from_json: expected array")
#| }
#| let len = a.length()
#| if len == 0 {
#| return []
#| }
#| guard! DecodeContext::Index(path, index=0) is (Index(_) as new_path)
#| let res = FixedArray::make(
#| len,
#| FromJson::from_json(a.unsafe_get(0), new_path),
#| )
#| for i in 1.. None
#| Array([value]) => Some(FromJson::from_json(value, path.add_index(0)))
#| _ => decode_error(path, "Option::from_json: expected array or null")
#| }
#|}
#|pub impl[Ok : FromJson, Err : FromJson] FromJson for Result[Ok, Err] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Object(obj) else {
#| decode_error(path, "Result::from_json: expected object")
#| }
#| if obj.length() != 1 {
#| decode_error(path, "Result::from_json: expected object with one field")
#| }
#| match obj {
#| { "Ok": ok, .. } => Ok(FromJson::from_json(ok, path.add_key("Ok")))
#| { "Err": err, .. } => Err(FromJson::from_json(err, path.add_key("Err")))
#| _ =>
#| decode_error(
#| path, "Result::from_json: expected object with Ok or Err field",
#| )
#| }
#|}
#|pub impl FromJson for Unit with fn from_json(json, path) {
#| guard json is Null else {
#| decode_error(path, "Unit::from_json: expected null")
#| }
#|}
#|pub impl FromJson for Json with fn from_json(json, _path) {
#| json
#|}
#|pub impl FromJson for Bytes with fn from_json(json, path) {
#| guard json is String(a) else {
#| decode_error(path, "Bytes::from_json: expected string")
#| }
#| let buffer = @buffer.Buffer(capacity=a.length())
#| for x = a[:] {
#| match x {
#| [] => break
#| [
#| .. "\\x",
#| '0'..<='9'
#| | 'a'..<='f' as x,
#| '0'..<='9'
#| | 'a'..<='f' as y,
#| .. rest,
#| ] => {
#| let upper = (x.to_int() & 0xF) + (x.to_int() >> 6) * 9
#| let lower = (y.to_int() & 0xF) + (y.to_int() >> 6) * 9
#| buffer.write_byte(((upper << 4) | lower).to_byte())
#| continue rest
#| }
#| [' '..<='~' as ch, .. rest] => {
#| guard ch != '\\' && ch != '"' else {
#| decode_error(path, "Bytes::from_json: invalid escape sequence")
#| }
#| buffer.write_byte(ch.to_uint().to_byte())
#| continue rest
#| }
#| _ => decode_error(path, "Bytes::from_json: invalid byte sequence")
#| }
#| }
#| buffer.to_bytes()
#|}
),
"internal_types.mbt": (
#|priv struct ParseContext {
#| mut offset : Int32
#| input : StringView
#| end_offset : Int32
#| reject_duplicate_keys : Bool
#| preserve_number_repr : Bool
#|}
#|fn ParseContext::make(
#| input : StringView,
#| reject_duplicate_keys~ : Bool = false,
#| preserve_number_repr~ : Bool = false,
#|) -> ParseContext {
#| {
#| offset: 0,
#| input,
#| end_offset: input.length(),
#| reject_duplicate_keys,
#| preserve_number_repr,
#| }
#|}
#|impl @lexbuf.Scannable for ParseContext with fn scan_input(self) {
#| self.input
#|}
#|impl @lexbuf.Scannable for ParseContext with fn scan_cursor(self) {
#| self.offset
#|}
#|impl @lexbuf.Scannable for ParseContext with fn set_scan_cursor(self, cursor) {
#| self.offset = cursor
#|}
),
"json.mbt": (
#|#deprecated("Suggestion: `if json is Null { Some(()) } else { None }`")
#|pub fn Json::as_null(self : Json) -> Unit? {
#| guard self is Null else { return None }
#| Some(())
#|}
#|#deprecated("Suggestion: `if json is True { Some(true) } else if json is False { Some(false) } else { None }`")
#|pub fn Json::as_bool(self : Json) -> Bool? {
#| match self {
#| True => Some(true)
#| False => Some(false)
#| _ => None
#| }
#|}
#|#deprecated("Suggestion: `if json is Number(n) { Some(n) } else { None }`")
#|pub fn Json::as_number(self : Json) -> Double? {
#| guard self is Number(n, ..) else { return None }
#| Some(n)
#|}
#|#deprecated("Suggestion: `if json is String(s) { Some(s) } else { None }`")
#|pub fn Json::as_string(self : Json) -> String? {
#| guard self is String(s) else { return None }
#| Some(s)
#|}
#|#deprecated("Suggestion: `if json is Array(array) { Some(array) } else { None }`")
#|pub fn Json::as_array(self : Json) -> Array[Json]? {
#| guard self is Array(arr) else { return None }
#| Some(arr)
#|}
#|#deprecated("Suggestion: `if json is Array(array) { array.get(index) } else { None }`")
#|pub fn Json::item(self : Json, index : Int32) -> Json? {
#| if self is Array(arr) {
#| arr.get(index)
#| } else {
#| None
#| }
#|}
#|#deprecated
#|pub fn Json::as_object(self : Json) -> Map[String, Json]? {
#| guard self is Object(obj) else { return None }
#| Some(obj)
#|}
#|#deprecated("Suggestion: `if json is Object(obj) { obj.get(key) } else { None }`")
#|pub fn Json::value(self : Json, key : String) -> Json? {
#| if self is Object(obj) {
#| obj.get(key)
#| } else {
#| None
#| }
#|}
#|#inline
#|fn write_indent(
#| buf : StringBuilder,
#| cache : Array[String],
#| level : Int32,
#| indent : Int32,
#|) -> Unit {
#| while cache.length() <= level {
#| match cache {
#| [.., last] => cache.push(last + " ".repeat(indent))
#| [] => cache.push("\n")
#| }
#| }
#| buf.write_string(cache[level])
#|}
#|priv enum WriteFrame {
#| Array(Array[Json], mut i~ : Int32) // (arr, index)
#| Object(Iter[(String, Json)], mut first~ : Bool) // (kvs, first)
#|}
#|struct Replacer {
#| f : (String, Json) -> Json?
#|}
#|#alias(new, deprecated="Use `Replacer()` instead")
#|pub fn Replacer::Replacer(f : (String, Json) -> Json?) -> Replacer {
#| { f, }
#|}
#|pub fn Replacer::keep(array : ArrayView[StringView]) -> Replacer {
#| { f: (idx, value) => if array.contains(idx) { Some(value) } else { None }, }
#|}
#|pub fn Replacer::exclude(array : ArrayView[StringView]) -> Replacer {
#| { f: (idx, value) => if array.contains(idx) { None } else { Some(value) }, }
#|}
#|pub fn Json::stringify(
#| self : Json,
#| escape_slash~ : Bool = false,
#| indent~ : Int32 = 0,
#| replacer? : Replacer,
#|) -> String {
#| let buf = StringBuilder(capacity=0)
#| let indent_cache : Array[String] = []
#| let stack : Array[WriteFrame] = []
#| let mut depth = 0
#| for x = Some(self) {
#| match x {
#| Some(value) => {
#| match value {
#| Object(members) =>
#| if members.is_empty() {
#| buf <+ "{}"
#| } else {
#| depth += 1
#| buf.write_char('{')
#| stack.push(Object(members.iter(), first=true))
#| }
#| Array(arr) =>
#| if arr.is_empty() {
#| buf <+ "[]"
#| } else {
#| depth += 1
#| buf.write_char('[')
#| if indent > 0 {
#| write_indent(buf, indent_cache, depth, indent)
#| }
#| stack.push(Array(arr, i=0))
#| }
#| String(s) => {
#| buf.write_char('\"')
#| write_escaped(buf, s, escape_slash~)
#| buf.write_char('\"')
#| }
#| Number(n, repr~) =>
#| match repr {
#| None =>
#| if !n.is_finite() {
#| continue Some(Json(n))
#| } else {
#| buf.write_object(n)
#| }
#| Some(r) => buf.write_string(r)
#| }
#| True => buf <+ "true"
#| False => buf <+ "false"
#| Null => buf <+ "null"
#| }
#| continue None
#| }
#| None =>
#| match stack {
#| [] => break
#| [.., Array(arr, i~) as frame] =>
#| if i < arr.length() {
#| let element = arr[i]
#| frame.i = i + 1
#| if i > 0 {
#| buf.write_char(',')
#| if indent > 0 {
#| write_indent(buf, indent_cache, depth, indent)
#| }
#| }
#| continue Some(element)
#| } else {
#| depth -= 1
#| ignore(stack.pop())
#| if indent > 0 {
#| write_indent(buf, indent_cache, depth, indent)
#| }
#| buf.write_char(']')
#| continue None
#| }
#| [.., Object(iterator, first~) as frame] =>
#| match iterator.next() {
#| Some((k, v)) => {
#| let mut v2 = v
#| if replacer is Some(replacer) {
#| if (replacer.f)(k, v) is Some(v) {
#| v2 = v
#| } else {
#| continue None
#| }
#| }
#| if !first {
#| buf.write_char(',')
#| }
#| if indent > 0 {
#| write_indent(buf, indent_cache, depth, indent)
#| }
#| buf.write_char('\"')
#| write_escaped(buf, k, escape_slash~)
#| buf <+ "\":"
#| if indent > 0 {
#| buf.write_char(' ')
#| }
#| frame.first = false
#| continue Some(v2)
#| }
#| None => {
#| depth -= 1
#| ignore(stack.pop())
#| if indent > 0 && !first {
#| write_indent(buf, indent_cache, depth, indent)
#| }
#| buf.write_char('}')
#| continue None
#| }
#| }
#| }
#| }
#| }
#| buf.to_string()
#|}
#|#inline
#|fn need_escape_scalar(
#| str : String,
#| escape_slash : Bool,
#| start : Int32,
#| end : Int32,
#|) -> Bool {
#| for i in start.. Bool {
#| code == '"' || code == '\\' || code < ' ' || (escape_slash && code == '/')
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|#warnings("-unused_value")
#|fn suppress_unused_v128_import_on_scalar_targets() -> Unit {
#| ignore(@simd.i16x8_splat(0))
#|}
#|#cfg(not(any(target="native", target="wasm")))
#|fn need_escape(str : String, escape_slash : Bool) -> Bool {
#| need_escape_scalar(str, escape_slash, 0, str.length())
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn need_escape(str : String, escape_slash : Bool) -> Bool {
#| let len = str.length()
#| guard len >= 8 else { return need_escape_scalar(str, escape_slash, 0, len) }
#| let control_limit = @simd.i16x8_splat(' ')
#| let quote = @simd.i16x8_splat('"')
#| let backslash = @simd.i16x8_splat('\\')
#| let slash = @simd.i16x8_splat('/')
#| let tail_start = for pos = 0; pos + 8 <= len; {
#| let block = @simd.v128_load_i16x8(str, pos)
#| let escaped = @simd.v128_or_(
#| @simd.i16x8_lt_u(block, control_limit),
#| @simd.v128_or_(
#| @simd.i16x8_eq(block, quote),
#| @simd.i16x8_eq(block, backslash),
#| ),
#| )
#| let escaped = if escape_slash {
#| @simd.v128_or_(escaped, @simd.i16x8_eq(block, slash))
#| } else {
#| escaped
#| }
#| if @simd.v128_any_true(escaped) {
#| return true
#| }
#| continue pos + 8
#| } nobreak {
#| pos
#| }
#| need_escape_scalar(str, escape_slash, tail_start, len)
#|}
#|fn write_escaped(
#| buf : StringBuilder,
#| str : String,
#| escape_slash~ : Bool,
#|) -> Unit {
#| if !need_escape(str, escape_slash) {
#| buf.write_string(str)
#| return
#| }
#| for i, code in str.code_units(); run_start = 0 {
#| guard needs_escape_unit(code, escape_slash) else { continue run_start }
#| buf.write_view(str.exact_view(start=run_start, end=i))
#| write_escape(buf, code)
#| continue i + 1
#| } nobreak {
#| buf.write_view(str.exact_view(start=run_start))
#| }
#|}
#|fn write_escape(buf : StringBuilder, code : UInt16) -> Unit {
#| match code {
#| '"' => buf <+ "\\\""
#| '\\' => buf <+ "\\\\"
#| '/' => buf <+ "\\/"
#| '\n' => buf <+ "\\n"
#| '\r' => buf <+ "\\r"
#| '\b' => buf <+ "\\b"
#| '\t' => buf <+ "\\t"
#| 0x0C => buf <+ "\\f"
#| _ => buf <+ "\\u00\{code.to_byte().to_hex()}"
#| }
#|}
#|pub fn Json::transform(self : Self, replacer : Replacer) -> Json {
#| match self {
#| Object(members) =>
#| members
#| .iter()
#| .filter_map(pair => {
#| let (k, v) = pair
#| if (replacer.f)(k, v) is Some(v2) {
#| Some((k, v2.transform(replacer)))
#| } else {
#| None
#| }
#| })
#| |> Map::from_iter()
#| |> Object
#| Array(members) => Array(members.map(m => m.transform(replacer)))
#| value => value
#| }
#|}
#|#callsite(autofill(args_loc, loc))
#|pub fn json_inspect(
#| obj : &ToJson,
#| content? : Json,
#| loc~ : SourceLoc,
#| args_loc~ : ArgsLoc,
#|) -> Unit raise InspectError {
#| let actual = obj.to_json().stringify(escape_slash=false)
#| let want = match content {
#| Some(x) | (None with x = Json("")) => x.stringify(escape_slash=false)
#| }
#| if actual != want {
#| let loc = loc.to_json_string()
#| let args_loc = args_loc.to_json()
#| raise InspectError(
#| (
#| $|@EXPECT_FAILED {"loc": \{loc}, "args_loc": \{args_loc}, "expect": \{want.escape()}, "actual": \{actual.escape()}, "mode": "json"}
#| ),
#| )
#| }
#|}
),
"json_path.mbt": (
#|#alias(JsonPath)
#|#derive(Eq)
#|enum DecodeContext {
#| Root
#| Key(DecodeContext, mut key~ : String)
#| Index(DecodeContext, mut index~ : Int32)
#|}
#|pub fn DecodeContext::copy(self : DecodeContext) -> DecodeContext {
#| match self {
#| Root => Root
#| Key(parent, key~) => Key(parent.copy(), key~)
#| Index(parent, index~) => Index(parent.copy(), index~)
#| }
#|}
#|pub fn DecodeContext::error(
#| self : DecodeContext,
#| message : String,
#|) -> JsonDecodeError {
#| JsonDecodeError((self.copy(), message))
#|}
#|pub fn DecodeContext::add_index(
#| self : DecodeContext,
#| index : Int32,
#|) -> DecodeContext {
#| Index(self, index~)
#|}
#|pub fn DecodeContext::add_key(
#| self : DecodeContext,
#| key : String,
#|) -> DecodeContext {
#| Key(self, key~)
#|}
#|pub impl Show for DecodeContext with fn output(self, logger) {
#| fn write_token(logger : &Logger, token : String) -> Unit {
#| if !token.contains_any(chars="~/") {
#| logger.write_string(token)
#| return
#| }
#| for ch in token {
#| match ch {
#| '~' => logger <+ "~0"
#| '/' => logger <+ "~1"
#| _ => logger.write_char(ch)
#| }
#| }
#| }
#| fn build_path(path : DecodeContext, logger : &Logger) -> Unit {
#| match path {
#| Root => ()
#| Key(parent, key~) =>
#| logger <+
#| "\{cb => build_path(parent, cb)}/\{cb => write_token(cb, key)}"
#| Index(parent, index~) =>
#| logger <+
#| "\{cb => build_path(parent, cb)}/\{cb => cb.write_object(index)}"
#| }
#| }
#| build_path(self, logger)
#|}
#|pub impl ToJson for DecodeContext with fn to_json(self) {
#| String(self.to_string())
#|}
#|test "show JsonPath - JSON Pointer format" {
#| @builtin.inspect(Root, content="")
#| @builtin.inspect(Root.add_key("foo"), content="/foo")
#| @builtin.inspect(Root.add_index(0), content="/0")
#| let path : DecodeContext = Key(Index(Root, index=0), key="foo")
#| @builtin.inspect(path, content="/0/foo")
#| @builtin.inspect(path.add_index(1), content="/0/foo/1")
#| @builtin.inspect(path.add_key("bar"), content="/0/foo/bar")
#| @builtin.inspect(
#| Root.add_key("foo").add_key("foo1").add_index(2),
#| content="/foo/foo1/2",
#| )
#| @builtin.inspect(Root.add_key("foo").add_key("foo1"), content="/foo/foo1")
#| @builtin.inspect(
#| Root.add_key("foo").add_key("foo1").add_index(2).add_key("bar"),
#| content="/foo/foo1/2/bar",
#| )
#| @builtin.inspect(
#| Root.add_key("foo").add_key("foo1").add_index(2).add_key("bar").add_index(3),
#| content="/foo/foo1/2/bar/3",
#| )
#| @builtin.inspect(
#| Root
#| .add_key("foo")
#| .add_key("foo1")
#| .add_index(2)
#| .add_key("bar")
#| .add_index(3)
#| .add_key("baz"),
#| content="/foo/foo1/2/bar/3/baz",
#| )
#| @builtin.inspect(
#| Root.add_key("foo").add_key("foo1").add_index(2).add_index(3).add_index(4),
#| content="/foo/foo1/2/3/4",
#| )
#|}
#|test "show JsonPath - special characters escaping" {
#| @builtin.inspect(Root.add_key("foo~bar"), content="/foo~0bar")
#| @builtin.inspect(Root.add_key("foo~~bar~~"), content="/foo~0~0bar~0~0")
#| @builtin.inspect(Root.add_key("foo/bar"), content="/foo~1bar")
#| @builtin.inspect(Root.add_key("foo//bar//"), content="/foo~1~1bar~1~1")
#| @builtin.inspect(Root.add_key("foo~/bar"), content="/foo~0~1bar")
#| @builtin.inspect(Root.add_key("~foo/bar~"), content="/~0foo~1bar~0")
#| @builtin.inspect(
#| Root.add_key("a/b").add_key("c~d").add_index(0).add_key("e~/f"),
#| content="/a~1b/c~0d/0/e~0~1f",
#| )
#| @builtin.inspect(Root.add_key(""), content="/")
#| @builtin.inspect(Root.add_key("~/"), content="/~0~1")
#|}
#|test "show JsonPath - RFC 6901 compliance examples" {
#| @builtin.inspect(Root, content="")
#| @builtin.inspect(Root.add_key("foo"), content="/foo")
#| @builtin.inspect(Root.add_key("foo").add_index(0), content="/foo/0")
#| @builtin.inspect(Root.add_key(""), content="/")
#| @builtin.inspect(Root.add_key("a/b"), content="/a~1b")
#| @builtin.inspect(Root.add_key("c%d"), content="/c%d")
#| @builtin.inspect(Root.add_key("e^f"), content="/e^f")
#| @builtin.inspect(Root.add_key("g|h"), content="/g|h")
#| @builtin.inspect(Root.add_key("i\\j"), content="/i\\j")
#| @builtin.inspect(Root.add_key("k\"l"), content="/k\"l")
#| @builtin.inspect(Root.add_key(" "), content="/ ")
#| @builtin.inspect(Root.add_key("m~n"), content="/m~0n")
#|}
),
"lex_main.mbt": (
#|fn ParseContext::lex_value(ctx : ParseContext) -> Json? raise ParseError {
#| lexscan ctx with longest {
#| re"^[ \t\r\n]*" + re"[{\[]" => None
#| re"^[ \t\r\n]*" + re"\"" => Some(Json::string(ctx.lex_string()))
#| re"^[ \t\r\n]*" +
#| ((re"-?(0|[1-9][0-9]*)" + re"(\.[0-9]+)?" + re"([eE][+\-]?[0-9]+)?") as num) => {
#| let num_end = ctx.offset
#| let { value: n, repr, } = ctx.lex_number_end(
#| num_end - num.length(),
#| num_end,
#| )
#| let repr = repr.map(repr => repr.to_owned())
#| Some(Json::number(n, repr?))
#| }
#| re"^[ \t\r\n]*" + re"null" => Some(null)
#| re"^[ \t\r\n]*" + re"true" => Some(Json::boolean(true))
#| re"^[ \t\r\n]*" + re"false" => Some(Json::boolean(false))
#| re"^[ \t\r\n]*" + re"-" => ctx.unexpected_char()
#| re"^[ \t\r\n]*" + re"-?(0|[1-9][0-9]*)" + re"\." => ctx.unexpected_char()
#| re"^[ \t\r\n]*" +
#| re"-?(0|[1-9][0-9]*)" +
#| re"(\.[0-9]+)?" +
#| re"[eE][+\-]?" => ctx.unexpected_char()
#| re"^[ \t\r\n]*" + re"(n|nu|nul|t|tr|tru|f|fa|fal|fals)" =>
#| ctx.unexpected_char()
#| _ => ctx.unexpected_token()
#| }
#|}
),
"lex_misc.mbt": (
#|const SURROGATE_LOW_CHAR = 0xD800
#|const SURROGATE_HIGH_CHAR = 0xDFFF
#|fn ParseContext::expect_char(
#| ctx : ParseContext,
#| c : Char,
#|) -> Unit raise ParseError {
#| guard ctx.offset < ctx.end_offset else { raise InvalidEof }
#| let c1 = ctx.input.unsafe_get(ctx.offset).to_int()
#| ctx.offset += 1
#| let c0 = c.to_int()
#| if c0 <= 0xFFFF {
#| if c0 != c1 {
#| ctx.invalid_char(shift=-1)
#| }
#| } else {
#| guard c1 is (SURROGATE_LOW_CHAR..<=SURROGATE_HIGH_CHAR) &&
#| ctx.offset < ctx.end_offset else {
#| ctx.invalid_char(shift=-1)
#| }
#| let c2 = ctx.input.unsafe_get(ctx.offset).to_int()
#| let c3 = (c1 << 10) + c2 - 0x35fdc00
#| if c3 != c0 {
#| ctx.invalid_char(shift=-1)
#| } else {
#| ctx.offset += 1 // consume and move forward
#| }
#| }
#|}
#|test "expect_char" {
#| let ctx = ParseContext::make("abc")
#| ctx.expect_char('a')
#| ctx.expect_char('b')
#| ctx.expect_char('c')
#| try ctx.expect_char('d') catch {
#| err => json_inspect(err, content="InvalidEof")
#| } noraise {
#| _ => fail("expected InvalidEof")
#| }
#|}
#|test "expect_char with surrogate pair" {
#| let ctx = ParseContext::make("a\u{1F600}bc\u{1F600}c")
#| ctx.expect_char('a')
#| ctx.expect_char((0x1F600).unsafe_to_char())
#| ctx.expect_char('b')
#| ctx.expect_char('c')
#| ctx.expect_char((0x1F600).unsafe_to_char())
#| ctx.expect_char('c')
#| try ctx.expect_char('d') catch {
#| err => json_inspect(err, content="InvalidEof")
#| } noraise {
#| _ => fail("expected InvalidEof")
#| }
#|}
#|fn ParseContext::lex_skip_whitespace(ctx : ParseContext) -> Unit {
#| let end = ctx.end_offset
#| ctx.offset = for offset = ctx.offset {
#| if offset >= end {
#| break offset
#| }
#| match ctx.input.unsafe_get(offset) {
#| ' ' | '\t' | '\r' | '\n' => continue offset + 1
#| _ => break offset
#| }
#| }
#|}
#|test "lex_skip_whitespace" {
#| let ctx = ParseContext::make("")
#| ctx.lex_skip_whitespace()
#| json_inspect(ctx.offset, content=0)
#| let ctx = ParseContext::make(" \t\r\n")
#| ctx.lex_skip_whitespace()
#| json_inspect(ctx.offset, content=6)
#| let ctx = ParseContext::make(" \t\r\n{")
#| ctx.lex_skip_whitespace()
#| json_inspect(ctx.offset, content=4)
#| let ctx = ParseContext::make("{}")
#| ctx.lex_skip_whitespace()
#| json_inspect(ctx.offset, content=0)
#| let ctx = ParseContext::make("\u{00A0}x")
#| ctx.lex_skip_whitespace()
#| json_inspect(ctx.offset, content=0)
#|}
#|fn[T] ParseContext::unexpected_char(ctx : ParseContext) -> T raise ParseError {
#| guard ctx.offset < ctx.end_offset else { raise InvalidEof }
#| ctx.invalid_char()
#|}
#|fn[T] ParseContext::unexpected_token(ctx : ParseContext) -> T raise ParseError {
#| ctx.lex_skip_whitespace()
#| ctx.unexpected_char()
#|}
#|test "expect_char at the upper BMP boundary" {
#| let ctx = ParseContext::make("\u{FFFF}😀")
#| ctx.expect_char('\u{FFFF}')
#| ctx.expect_char('😀')
#| json_inspect(ctx.offset, content=3)
#|}
),
"lex_number.mbt": (
#|const SAFE_INTEGER_LIMIT : Int64 = 9007199254740991L
#|const MAX_MANTISSA_FAST_PATH : UInt64 = 9007199254740992UL
#|const MIN_EXPONENT_FAST_PATH : Int64 = -22L
#|const MAX_EXPONENT_FAST_PATH : Int64 = 22L
#|const MAX_EXPONENT_DISGUISED_FAST_PATH : Int64 = 37L
#|const EXPONENT_CAP : Int64 = 100000L
#|const MAX_UINT64 : UInt64 = 0xffffffffffffffffUL
#|let pow10_table : ReadOnlyArray[Double] = [
#| 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0,
#| 1000000000.0, 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
#| 100000000000000.0, 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
#| 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0, 1000000000000000000000.0,
#| 10000000000000000000000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
#|]
#|let int_pow10_table : ReadOnlyArray[UInt64] = [
#| 1UL, 10UL, 100UL, 1000UL, 10000UL, 100000UL, 1000000UL, 10000000UL, 100000000UL,
#| 1000000000UL, 10000000000UL, 100000000000UL, 1000000000000UL, 10000000000000UL,
#| 100000000000000UL, 1000000000000000UL,
#|]
#|#valtype
#|priv struct JsonNumberScan {
#| negative : Bool
#| is_integer : Bool
#| mantissa : UInt64
#| exponent : Int64
#| many_digits : Bool
#|}
#|fn json_pow10_fast_path(exponent : Int32) -> Double {
#| pow10_table[exponent & 31]
#|}
#|fn checked_mul(a : UInt64, b : UInt64) -> UInt64? {
#| if a == 0UL || b == 0UL {
#| return Some(0UL)
#| }
#| if a == 1UL {
#| return Some(b)
#| }
#| if b == 1UL {
#| return Some(a)
#| }
#| if b.clz() == 0 || a.clz() == 0 {
#| return None
#| }
#| let quotient = MAX_UINT64 / b
#| if a > quotient {
#| None
#| } else {
#| Some(a * b)
#| }
#|}
#|fn JsonNumberScan::try_fast_double(self : JsonNumberScan) -> Double {
#| if self.mantissa == 0UL {
#| let value = 0.0
#| return if self.negative { -value } else { value }
#| }
#| if self.many_digits ||
#| self.exponent < MIN_EXPONENT_FAST_PATH ||
#| self.exponent > MAX_EXPONENT_DISGUISED_FAST_PATH ||
#| self.mantissa > MAX_MANTISSA_FAST_PATH {
#| return @double.not_a_number
#| }
#| let value = if self.exponent <= MAX_EXPONENT_FAST_PATH {
#| let value = self.mantissa.to_double()
#| if self.exponent < 0L {
#| value / json_pow10_fast_path(-self.exponent.to_int())
#| } else {
#| value * json_pow10_fast_path(self.exponent.to_int())
#| }
#| } else {
#| let shift = self.exponent - MAX_EXPONENT_FAST_PATH
#| guard checked_mul(self.mantissa, int_pow10_table[shift.to_int()])
#| is Some(mantissa) else {
#| return @double.not_a_number
#| }
#| if mantissa > MAX_MANTISSA_FAST_PATH {
#| return @double.not_a_number
#| }
#| mantissa.to_double() * json_pow10_fast_path(MAX_EXPONENT_FAST_PATH.to_int())
#| }
#| if self.negative {
#| -value
#| } else {
#| value
#| }
#|}
#|fn ParseContext::scan_json_number(
#| ctx : ParseContext,
#| start : Int32,
#| end : Int32,
#|) -> JsonNumberScan {
#| let negative = ctx.input.unsafe_get(start) == '-'
#| let mut has_decimal = false
#| let mut has_exponent = false
#| let mut exponent_negative = false
#| let mut exponent_part = 0L
#| let mut fractional_digits = 0
#| let mut mantissa = 0UL
#| let mut significant_digits = 0
#| let mut seen_nonzero = false
#| for i in (if negative { start + 1 } else { start }).. {
#| let digit = c.to_int() - '0'
#| if has_exponent {
#| if exponent_part < EXPONENT_CAP {
#| let next_exponent = exponent_part * 10L + digit.to_int64()
#| exponent_part = if next_exponent > EXPONENT_CAP {
#| EXPONENT_CAP
#| } else {
#| next_exponent
#| }
#| }
#| } else {
#| if has_decimal {
#| fractional_digits += 1
#| }
#| if digit != 0 || seen_nonzero {
#| seen_nonzero = true
#| significant_digits += 1
#| if significant_digits <= 19 {
#| mantissa = mantissa * 10UL +
#| digit.reinterpret_as_uint().to_uint64()
#| }
#| }
#| }
#| }
#| '.' => has_decimal = true
#| 'e' | 'E' => {
#| has_exponent = true
#| if i + 1 < end {
#| let next = ctx.input.unsafe_get(i + 1)
#| if next == '-' {
#| exponent_negative = true
#| }
#| }
#| }
#| _ => ()
#| }
#| }
#| let exponent_part = if exponent_negative {
#| -exponent_part
#| } else {
#| exponent_part
#| }
#| {
#| negative,
#| is_integer: !has_decimal && !has_exponent,
#| mantissa,
#| exponent: exponent_part - fractional_digits.to_int64(),
#| many_digits: significant_digits > 19,
#| }
#|}
#|#valtype
#|priv struct LexedNumber {
#| repr : StringView?
#| value : Double
#|}
#|fn ParseContext::lex_integer_end(
#| ctx : ParseContext,
#| start : Int32,
#| end : Int32,
#|) -> LexedNumber {
#| let negative = ctx.input.unsafe_get(start) == '-'
#| let number_start = if negative { start + 1 } else { start }
#| for i = number_start, acc = 0L {
#| if i >= end {
#| let value = if negative { -acc } else { acc }
#| break { value: value.to_double(), repr: None, }
#| }
#| let digit = (ctx.input.unsafe_get(i).to_int() - '0').to_int64()
#| if acc > (SAFE_INTEGER_LIMIT - digit) / 10L {
#| let s = ctx.input.unsafe_view(start~, end~)
#| try {
#| let value = @internal/strconv.parse_double(s)
#| return { value, repr: Some(s), }
#| } catch {
#| _ =>
#| return if negative {
#| { value: @double.neg_infinity, repr: Some(s), }
#| } else {
#| { value: @double.infinity, repr: Some(s), }
#| }
#| }
#| }
#| continue i + 1, acc * 10L + digit
#| }
#|}
#|fn ParseContext::lex_number_end(
#| ctx : ParseContext,
#| start : Int32,
#| end : Int32,
#|) -> LexedNumber {
#| let number = ctx.lex_number_value(start, end)
#| if ctx.preserve_number_repr {
#| { value: number.value, repr: Some(ctx.input.unsafe_view(start~, end~)), }
#| } else {
#| number
#| }
#|}
#|fn ParseContext::lex_number_value(
#| ctx : ParseContext,
#| start : Int32,
#| end : Int32,
#|) -> LexedNumber {
#| let scan = ctx.scan_json_number(start, end)
#| if scan.is_integer {
#| if !scan.many_digits &&
#| scan.exponent == 0L &&
#| scan.mantissa <= SAFE_INTEGER_LIMIT.reinterpret_as_uint64() {
#| let v = scan.mantissa.reinterpret_as_int64().to_double()
#| let value = if scan.negative { -v } else { v }
#| return { value, repr: None, }
#| }
#| return ctx.lex_integer_end(start, end)
#| }
#| let fast = scan.try_fast_double()
#| if !fast.is_nan() {
#| return { value: fast, repr: None, }
#| }
#| if !scan.many_digits {
#| let fast = @internal/strconv.try_eisel_lemire64(
#| scan.mantissa,
#| scan.exponent,
#| scan.negative,
#| )
#| if !fast.is_nan() {
#| return { value: fast, repr: None, }
#| }
#| }
#| let s = ctx.input.unsafe_view(start~, end~)
#| try {
#| let d = @internal/strconv.parse_double(s)
#| { value: d, repr: None, }
#| } catch {
#| _ =>
#| if scan.negative {
#| { value: @double.neg_infinity, repr: Some(s), }
#| } else {
#| { value: @double.infinity, repr: Some(s), }
#| }
#| }
#|}
),
"lex_string.mbt": (
#|fn ParseContext::lex_string(ctx : ParseContext) -> String raise ParseError {
#| let string_start = ctx.offset
#| let end = ctx.end_offset
#| let scalar_end = if LEX_STRING_USE_SIMD &&
#| end - string_start > LEX_STRING_SCALAR_UNITS {
#| string_start + LEX_STRING_SCALAR_UNITS
#| } else {
#| end
#| }
#| for i in string_start.. String raise ParseError {
#| let base = ctx.input.start_offset()
#| let i = find_string_special_long(
#| ctx.input.data(),
#| base + from,
#| base + ctx.end_offset,
#| ) -
#| base
#| guard i < ctx.end_offset else { raise InvalidEof }
#| let c = ctx.input.unsafe_get(i)
#| if c == '"' {
#| ctx.offset = i + 1
#| ctx.input.data().unsafe_substring(start=base + string_start, end=base + i)
#| } else if c == '\\' {
#| ctx.offset = i
#| ctx.lex_string_slow(string_start)
#| } else {
#| ctx.offset = i + 1
#| ctx.invalid_char(shift=-1)
#| }
#|}
#|fn ParseContext::lex_string_slow(
#| ctx : ParseContext,
#| run_start : Int32,
#|) -> String raise ParseError {
#| let end = ctx.end_offset
#| let buf = StringBuilder(capacity=(end - run_start).min(32))
#| let base = ctx.input.start_offset()
#| let input = ctx.input.data()
#| for i = ctx.offset, start = run_start, scalar_limit = 0 {
#| guard i < end else { raise InvalidEof }
#| let unit = input.unsafe_get(base + i)
#| if unit <= '\\' {
#| if unit < ' ' {
#| ctx.offset = i + 1
#| ctx.invalid_char(shift=-1)
#| }
#| if unit == '"' || unit == '\\' {
#| if start < i {
#| buf.write_view(input.unsafe_view(start=base + start, end=base + i))
#| }
#| if unit == '"' {
#| ctx.offset = i + 1
#| break
#| }
#| ctx.offset = i + 1
#| buf.write_char(ctx.lex_escape())
#| continue ctx.offset, ctx.offset, ctx.offset + LEX_STRING_SCALAR_UNITS
#| }
#| }
#| if LEX_STRING_ESCAPE_SIMD && i >= scalar_limit {
#| let next = find_string_special_long(input, base + i + 1, base + end)
#| continue next - base, start, scalar_limit
#| }
#| continue i + 1, start, scalar_limit
#| }
#| buf.to_string()
#|}
#|fn ParseContext::lex_escape(ctx : ParseContext) -> Char raise ParseError {
#| let i = ctx.offset
#| guard i < ctx.end_offset else { raise InvalidEof }
#| let escape = ctx.input.unsafe_get(i)
#| ctx.offset = i + 1
#| match escape {
#| 'b' => '\b'
#| 'f' => '\u{0C}'
#| 'n' => '\n'
#| 'r' => '\r'
#| 't' => '\t'
#| '"' | '\\' | '/' => escape.unsafe_to_char()
#| 'u' => ctx.lex_unicode_escape()
#| _ => {
#| ctx.offset = i
#| ctx.reject_char()
#| }
#| }
#|}
#|fn ParseContext::lex_unicode_escape(
#| ctx : ParseContext,
#|) -> Char raise ParseError {
#| let escape_start = ctx.offset - 2
#| let first = ctx.lex_hex_digits(4)
#| match first {
#| 0xD800..<=0xDBFF => {
#| guard ctx.input.get(ctx.offset) is Some('\\') else {
#| ctx.reject_char(blame=escape_start)
#| }
#| lexscan ctx {
#| re"^\\u" => ()
#| _ => {
#| ctx.offset += 1
#| ctx.reject_char(blame=escape_start)
#| }
#| }
#| let second = ctx.lex_hex_digits(4)
#| guard second is (0xDC00..<=0xDFFF) else {
#| ctx.unpaired_surrogate(escape_start)
#| }
#| ((first << 10) + second - 0x35fdc00).unsafe_to_char()
#| }
#| 0xDC00..<=0xDFFF => ctx.unpaired_surrogate(escape_start)
#| _ => first.unsafe_to_char()
#| }
#|}
#|fn find_string_special_scalar(
#| str : String,
#| start : Int32,
#| end : Int32,
#|) -> Int32 {
#| for i in start.. Int32 {
#| find_string_special_scalar(str, start, end)
#|}
#|#cfg(any(target="native", target="wasm"))
#|fn find_string_special_long(str : String, start : Int32, end : Int32) -> Int32 {
#| let control_limit = @simd.i16x8_splat(' ')
#| let quote = @simd.i16x8_splat('"')
#| let backslash = @simd.i16x8_splat('\\')
#| let tail_start = for pos = start; end - pos >= 8; {
#| let block = @simd.v128_load_i16x8(str, pos)
#| let special = @simd.v128_or_(
#| @simd.i16x8_lt_u(block, control_limit),
#| @simd.v128_or_(
#| @simd.i16x8_eq(block, quote),
#| @simd.i16x8_eq(block, backslash),
#| ),
#| )
#| if @simd.v128_any_true(special) {
#| return pos + @simd.i16x8_bitmask(special).ctz()
#| }
#| continue pos + 8
#| } nobreak {
#| pos
#| }
#| find_string_special_scalar(str, tail_start, end)
#|}
#|fn[T] ParseContext::unpaired_surrogate(
#| ctx : ParseContext,
#| escape_start : Int32,
#|) -> T raise ParseError {
#| ctx.invalid_char(shift=escape_start - ctx.offset)
#|}
#|fn ParseContext::lex_hex_digits(
#| ctx : ParseContext,
#| n : Int32,
#|) -> Int32 raise ParseError {
#| for _ in 0.. unit.to_int() - '0'
#| 'A'..<='F' => unit.to_int() - 'A' + 10
#| 'a'..<='f' => unit.to_int() - 'a' + 10
#| _ => ctx.reject_char()
#| }
#| ctx.offset += 1
#| continue (r << 4) | d
#| } nobreak {
#| r
#| }
#|}
#|fn[T] ParseContext::reject_char(
#| ctx : ParseContext,
#| blame~ : Int32 = ctx.offset,
#|) -> T raise ParseError {
#| guard ctx.offset < ctx.end_offset else { raise InvalidEof }
#| ctx.offset += match ctx.input.get_char(ctx.offset) {
#| Some(c) => c.utf16_len()
#| None => 1
#| }
#| ctx.invalid_char(shift=blame - ctx.offset)
#|}
),
"parse.mbt": (
#|pub fn valid(input : StringView) -> Bool {
#| try {
#| parse(input) |> ignore
#| true
#| } catch {
#| _ => return false
#| }
#|}
#|#label_migration(max_nesting_depth, fill=false)
#|pub fn parse(
#| input : StringView,
#| max_nesting_depth~ : Int32 = 128,
#| reject_duplicate_keys~ : Bool = false,
#| preserve_number_repr~ : Bool = false,
#|) -> Json raise ParseError {
#| let ctx = ParseContext::make(
#| input,
#| reject_duplicate_keys~,
#| preserve_number_repr~,
#| )
#| let val = match ctx.lex_value() {
#| Some(value) => value
#| None => ctx.parse_container(remaining_available_depth=max_nesting_depth)
#| }
#| ctx.lex_skip_whitespace()
#| if ctx.offset >= ctx.end_offset {
#| val
#| } else {
#| ctx.invalid_char()
#| }
#|}
#|fn ParseContext::parse_container(
#| ctx : ParseContext,
#| remaining_available_depth~ : Int32,
#|) -> Json raise ParseError {
#| if ctx.input.unsafe_get(ctx.offset - 1) == '{' {
#| ctx.parse_object(remaining_available_depth~)
#| } else {
#| ctx.parse_array(remaining_available_depth~)
#| }
#|}
#|fn ParseContext::parse_object(
#| ctx : ParseContext,
#| remaining_available_depth~ : Int32,
#|) -> Json raise ParseError {
#| let map = Map([])
#| if ctx.lex_empty_container_end('}') {
#| return Json::object(map)
#| }
#| if remaining_available_depth <= 0 {
#| let name = ctx.lex_property_name(map)
#| return ctx.stack_safe_parse(
#| InsideObject(map, last_key=name, next=Outermost),
#| )
#| }
#| let child_remaining_available_depth = remaining_available_depth - 1
#| for ;; {
#| let name = ctx.lex_property_name(map)
#| map[name] = match ctx.lex_value() {
#| Some(value) => value
#| None =>
#| ctx.parse_container(
#| remaining_available_depth=child_remaining_available_depth,
#| )
#| }
#| lexscan ctx {
#| re"^[ \t\r\n]*" + re"," => continue
#| re"^[ \t\r\n]*" + re"[}]" => break Json::object(map)
#| _ => ctx.unexpected_token()
#| }
#| }
#|}
#|fn ParseContext::lex_property_name(
#| ctx : ParseContext,
#| map : Map[String, Json],
#|) -> String raise ParseError {
#| lexscan ctx {
#| re"^[ \t\r\n]*" + re"\"" => ()
#| _ => ctx.unexpected_token()
#| }
#| let offset = ctx.offset - 1
#| let name = ctx.lex_string()
#| if ctx.reject_duplicate_keys && map.contains(name) {
#| raise DuplicateKey(offset_to_position(ctx.input, offset), name)
#| }
#| lexscan ctx {
#| re"^[ \t\r\n]*" + re":" => name
#| _ => ctx.unexpected_token()
#| }
#|}
#|fn ParseContext::parse_array(
#| ctx : ParseContext,
#| remaining_available_depth~ : Int32,
#|) -> Json raise ParseError {
#| let vec = []
#| if ctx.lex_empty_container_end(']') {
#| return Json::array(vec)
#| }
#| if remaining_available_depth <= 0 {
#| return ctx.stack_safe_parse(InsideArray(vec, next=Outermost))
#| }
#| let child_remaining_available_depth = remaining_available_depth - 1
#| for ;; {
#| let value = match ctx.lex_value() {
#| Some(value) => value
#| None =>
#| ctx.parse_container(
#| remaining_available_depth=child_remaining_available_depth,
#| )
#| }
#| vec.push(value)
#| lexscan ctx {
#| re"^[ \t\r\n]*" + re"," => continue
#| re"^[ \t\r\n]*" + re"\]" => break Json::array(vec)
#| _ => ctx.unexpected_token()
#| }
#| }
#|}
#|fn ParseContext::lex_empty_container_end(
#| ctx : ParseContext,
#| close : UInt16,
#|) -> Bool {
#| ctx.lex_skip_whitespace()
#| if ctx.offset < ctx.end_offset && ctx.input.unsafe_get(ctx.offset) == close {
#| ctx.offset += 1
#| true
#| } else {
#| false
#| }
#|}
#|priv enum ParserStack {
#| Outermost
#| InsideArray(Array[Json], next~ : ParserStack)
#| InsideObject(Map[String, Json], mut last_key~ : String, next~ : ParserStack)
#|}
#|fn ParseContext::stack_safe_parse(
#| ctx : ParseContext,
#| stack : ParserStack,
#|) -> Json raise ParseError {
#| main_parse_loop~: for stack = stack {
#| let value = if ctx.lex_value() is Some(value) {
#| value
#| } else if ctx.input.unsafe_get(ctx.offset - 1) == '{' {
#| let map = Map([])
#| if ctx.lex_empty_container_end('}') {
#| Json::object(map)
#| } else {
#| let last_key = ctx.lex_property_name(map)
#| continue main_parse_loop~ InsideObject(map, last_key~, next=stack)
#| }
#| } else {
#| let vec = []
#| if ctx.lex_empty_container_end(']') {
#| Json::array(vec)
#| } else {
#| continue main_parse_loop~ InsideArray(vec, next=stack)
#| }
#| }
#| for value = value, stack = stack {
#| match stack {
#| Outermost => break main_parse_loop~ value
#| InsideArray(vec, next~) => {
#| vec.push(value)
#| lexscan ctx {
#| re"^[ \t\r\n]*" + re"," => continue main_parse_loop~ stack
#| re"^[ \t\r\n]*" + re"\]" => continue Json::array(vec), next
#| _ => ctx.unexpected_token()
#| }
#| }
#| InsideObject(map, last_key~, next~) as obj => {
#| map[last_key] = value
#| lexscan ctx {
#| re"^[ \t\r\n]*" + re"," => {
#| obj.last_key = ctx.lex_property_name(map)
#| continue main_parse_loop~ stack
#| }
#| re"^[ \t\r\n]*" + re"[}]" => continue Json::object(map), next
#| _ => ctx.unexpected_token()
#| }
#| }
#| }
#| }
#| }
#|}
),
"to_json.mbt": (
#|pub(open) trait ToJson {
#| fn to_json(Self) -> Json
#|}
#|pub fn[T : ToJson] to_json(value : T) -> Json {
#| ToJson::to_json(value)
#|}
#|pub impl ToJson for Json with fn to_json(self) {
#| self
#|}
#|pub impl ToJson for Unit with fn to_json(_self) {
#| Null
#|}
#|pub impl ToJson for Bool with fn to_json(self : Bool) -> Json {
#| if self {
#| true
#| } else {
#| false
#| }
#|}
#|pub impl ToJson for Byte with fn to_json(self : Byte) -> Json {
#| Json::number(self.to_double())
#|}
#|pub impl ToJson for Char with fn to_json(self : Char) -> Json {
#| Json::string(self.to_string())
#|}
#|pub impl ToJson for @int16.Int16 with fn to_json(self : @int16.Int16) -> Json {
#| Json::number(self.to_int().to_double())
#|}
#|pub impl ToJson for Int32 with fn to_json(self : Int32) -> Json {
#| Json::number(self.to_double())
#|}
#|pub impl ToJson for Int64 with fn to_json(self : Int64) -> Json {
#| ToJson::to_json(self.to_string())
#|}
#|pub impl ToJson for UInt16 with fn to_json(self : UInt16) -> Json {
#| Json::number(self.to_int().to_double())
#|}
#|pub impl ToJson for UInt32 with fn to_json(self : UInt32) -> Json {
#| Json::number(self.to_uint64().to_double())
#|}
#|pub impl ToJson for UInt64 with fn to_json(self : UInt64) -> Json {
#| ToJson::to_json(self.to_string())
#|}
#|pub impl ToJson for @float.Float with fn to_json(self : @float.Float) -> Json {
#| ToJson::to_json(self.to_double())
#|}
#|pub impl ToJson for Double with fn to_json(self : Double) -> Json {
#| if self != self {
#| Json::string("NaN")
#| } else if self > 0x7FEFFFFFFFFFFFFFL.reinterpret_as_double() {
#| Json::string("Infinity")
#| } else if self < 0xFFEFFFFFFFFFFFFFL.reinterpret_as_double() {
#| Json::string("-Infinity")
#| } else {
#| Json::number(self)
#| }
#|}
#|pub impl ToJson for String with fn to_json(self : String) -> Json {
#| String(self)
#|}
#|pub impl ToJson for StringView with fn to_json(self) {
#| ToJson::to_json(self.to_owned())
#|}
#|pub impl ToJson for Bytes with fn to_json(self : Bytes) -> Json {
#| ToJson::to_json(self[:])
#|}
#|pub impl ToJson for BytesView with fn to_json(self) -> Json {
#| let sb = StringBuilder()
#| for byte in self {
#| if byte is (b' '..<=b'~') && byte != b'"' && byte != b'\\' {
#| sb.write_char(byte.to_char())
#| } else {
#| let value = byte.to_int()
#| sb.write_string("\\x")
#| sb.write_char(lower_hex_digit(value >> 4))
#| sb.write_char(lower_hex_digit(value & 0x0f))
#| }
#| }
#| Json::string(sb.to_string())
#|}
#|fn lower_hex_digit(value : Int32) -> Char {
#| if value < 10 {
#| (value + '0'.to_int()).unsafe_to_char()
#| } else {
#| (value - 10 + 'a'.to_int()).unsafe_to_char()
#| }
#|}
#|pub impl[X : ToJson] ToJson for Array[X] with fn to_json(self) {
#| Array(self.map(x => x.to_json()))
#|}
#|pub impl[X : ToJson] ToJson for FixedArray[X] with fn to_json(self) {
#| let len = self.length()
#| if len == 0 {
#| return []
#| }
#| let res = Array::unsafe_make_uninit(len)
#| for i, x in self {
#| res.unsafe_set(i, ToJson::to_json(x))
#| }
#| Array(res)
#|}
#|pub impl[T : ToJson] ToJson for ReadOnlyArray[T] with fn to_json(self) {
#| ToJson::to_json(self[:])
#|}
#|pub impl[X : ToJson] ToJson for ArrayView[X] with fn to_json(self) {
#| let len = self.length()
#| if len == 0 {
#| return []
#| }
#| let res = Array::unsafe_make_uninit(len)
#| for i, x in self {
#| res.unsafe_set(i, ToJson::to_json(x))
#| }
#| Array(res)
#|}
#|pub impl[X : ToJson] ToJson for Iter[X] with fn to_json(self) {
#| [
#| for x in self => x
#| ]
#|}
#|pub impl[K : Show, V : ToJson] ToJson for Map[K, V] with fn to_json(self) {
#| let object = Map([], capacity=self.length())
#| for k, v in self {
#| object[k.to_string()] = v.to_json()
#| }
#| Object(object)
#|}
#|pub impl[T : ToJson] ToJson for T? with fn to_json(self) {
#| match self {
#| None => Null
#| Some(value) => [value]
#| }
#|}
#|pub impl[Ok : ToJson, Err : ToJson] ToJson for Result[Ok, Err] with fn to_json(
#| self : Result[Ok, Err],
#|) -> Json {
#| match self {
#| Ok(ok) => { "Ok": ok }
#| Err(err) => { "Err": err }
#| }
#|}
#|pub impl ToJson for Error with fn to_json(self) = "%error.to_json"
#|pub impl ToJson for Failure with fn to_json(self) {
#| let Failure(message) = self
#| ["Failure", message]
#|}
),
"tuple_fromjson.mbt": (
#|pub impl[A : FromJson, B : FromJson] FromJson for (A, B) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [a, b] => {
#| let a : A = FromJson::from_json(a, path.add_index(0))
#| let b : B = FromJson::from_json(b, path.add_index(1))
#| (a, b)
#| }
#| _ => decode_error(path, "expected tuple of size 2")
#| }
#|}
#|pub impl[A : FromJson, B : FromJson, C : FromJson] FromJson for (A, B, C) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [a, b, c] => {
#| let a : A = FromJson::from_json(a, path.add_index(0))
#| let b : B = FromJson::from_json(b, path.add_index(1))
#| let c : C = FromJson::from_json(c, path.add_index(2))
#| (a, b, c)
#| }
#| _ => decode_error(path, "expected tuple of size 3")
#| }
#|}
#|pub impl[A : FromJson, B : FromJson, C : FromJson, D : FromJson] FromJson for (
#| A,
#| B,
#| C,
#| D,
#|) with fn from_json(json, path) {
#| match json {
#| [a, b, c, d] => {
#| let a : A = FromJson::from_json(a, path.add_index(0))
#| let b : B = FromJson::from_json(b, path.add_index(1))
#| let c : C = FromJson::from_json(c, path.add_index(2))
#| let d : D = FromJson::from_json(d, path.add_index(3))
#| (a, b, c, d)
#| }
#| _ => decode_error(path, "expected tuple of size 4")
#| }
#|}
#|pub impl[A : FromJson, B : FromJson, C : FromJson, D : FromJson, E : FromJson] FromJson for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|) with fn from_json(json, path) {
#| match json {
#| [a, b, c, d, e] => {
#| let a : A = FromJson::from_json(a, path.add_index(0))
#| let b : B = FromJson::from_json(b, path.add_index(1))
#| let c : C = FromJson::from_json(c, path.add_index(2))
#| let d : D = FromJson::from_json(d, path.add_index(3))
#| let e : E = FromJson::from_json(e, path.add_index(4))
#| (a, b, c, d, e)
#| }
#| _ => decode_error(path, "expected tuple of size 5")
#| }
#|}
#|pub impl[
#| A : FromJson,
#| B : FromJson,
#| C : FromJson,
#| D : FromJson,
#| E : FromJson,
#| F : FromJson,
#|] FromJson for (A, B, C, D, E, F) with fn from_json(json, path) {
#| match json {
#| [a, b, c, d, e, f] => {
#| let a : A = FromJson::from_json(a, path.add_index(0))
#| let b : B = FromJson::from_json(b, path.add_index(1))
#| let c : C = FromJson::from_json(c, path.add_index(2))
#| let d : D = FromJson::from_json(d, path.add_index(3))
#| let e : E = FromJson::from_json(e, path.add_index(4))
#| let f : F = FromJson::from_json(f, path.add_index(5))
#| (a, b, c, d, e, f)
#| }
#| _ => decode_error(path, "expected tuple of size 6")
#| }
#|}
#|pub impl[
#| A : FromJson,
#| B : FromJson,
#| C : FromJson,
#| D : FromJson,
#| E : FromJson,
#| F : FromJson,
#| G : FromJson,
#|] FromJson for (A, B, C, D, E, F, G) with fn from_json(json, path) {
#| match json {
#| [a, b, c, d, e, f, g] => {
#| let a : A = FromJson::from_json(a, path.add_index(0))
#| let b : B = FromJson::from_json(b, path.add_index(1))
#| let c : C = FromJson::from_json(c, path.add_index(2))
#| let d : D = FromJson::from_json(d, path.add_index(3))
#| let e : E = FromJson::from_json(e, path.add_index(4))
#| let f : F = FromJson::from_json(f, path.add_index(5))
#| let g : G = FromJson::from_json(g, path.add_index(6))
#| (a, b, c, d, e, f, g)
#| }
#| _ => decode_error(path, "expected tuple of size 7")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7) with fn from_json(json, path) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| (x0, x1, x2, x3, x4, x5, x6, x7)
#| }
#| _ => decode_error(path, "expected tuple of size 8")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8)
#| }
#| _ => decode_error(path, "expected tuple of size 9")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9)
#| }
#| _ => decode_error(path, "expected tuple of size 10")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#| T10 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| let x10 : T10 = FromJson::from_json(x10, path.add_index(10))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
#| }
#| _ => decode_error(path, "expected tuple of size 11")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#| T10 : FromJson,
#| T11 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| let x10 : T10 = FromJson::from_json(x10, path.add_index(10))
#| let x11 : T11 = FromJson::from_json(x11, path.add_index(11))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11)
#| }
#| _ => decode_error(path, "expected tuple of size 12")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#| T10 : FromJson,
#| T11 : FromJson,
#| T12 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| let x10 : T10 = FromJson::from_json(x10, path.add_index(10))
#| let x11 : T11 = FromJson::from_json(x11, path.add_index(11))
#| let x12 : T12 = FromJson::from_json(x12, path.add_index(12))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)
#| }
#| _ => decode_error(path, "expected tuple of size 13")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#| T10 : FromJson,
#| T11 : FromJson,
#| T12 : FromJson,
#| T13 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| let x10 : T10 = FromJson::from_json(x10, path.add_index(10))
#| let x11 : T11 = FromJson::from_json(x11, path.add_index(11))
#| let x12 : T12 = FromJson::from_json(x12, path.add_index(12))
#| let x13 : T13 = FromJson::from_json(x13, path.add_index(13))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13)
#| }
#| _ => decode_error(path, "expected tuple of size 14")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#| T10 : FromJson,
#| T11 : FromJson,
#| T12 : FromJson,
#| T13 : FromJson,
#| T14 : FromJson,
#|] FromJson for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) with fn from_json(
#| json,
#| path,
#|) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| let x10 : T10 = FromJson::from_json(x10, path.add_index(10))
#| let x11 : T11 = FromJson::from_json(x11, path.add_index(11))
#| let x12 : T12 = FromJson::from_json(x12, path.add_index(12))
#| let x13 : T13 = FromJson::from_json(x13, path.add_index(13))
#| let x14 : T14 = FromJson::from_json(x14, path.add_index(14))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14)
#| }
#| _ => decode_error(path, "expected tuple of size 15")
#| }
#|}
#|pub impl[
#| T0 : FromJson,
#| T1 : FromJson,
#| T2 : FromJson,
#| T3 : FromJson,
#| T4 : FromJson,
#| T5 : FromJson,
#| T6 : FromJson,
#| T7 : FromJson,
#| T8 : FromJson,
#| T9 : FromJson,
#| T10 : FromJson,
#| T11 : FromJson,
#| T12 : FromJson,
#| T13 : FromJson,
#| T14 : FromJson,
#| T15 : FromJson,
#|] FromJson for (
#| T0,
#| T1,
#| T2,
#| T3,
#| T4,
#| T5,
#| T6,
#| T7,
#| T8,
#| T9,
#| T10,
#| T11,
#| T12,
#| T13,
#| T14,
#| T15,
#|) with fn from_json(json, path) {
#| match json {
#| [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15] => {
#| let x0 : T0 = FromJson::from_json(x0, path.add_index(0))
#| let x1 : T1 = FromJson::from_json(x1, path.add_index(1))
#| let x2 : T2 = FromJson::from_json(x2, path.add_index(2))
#| let x3 : T3 = FromJson::from_json(x3, path.add_index(3))
#| let x4 : T4 = FromJson::from_json(x4, path.add_index(4))
#| let x5 : T5 = FromJson::from_json(x5, path.add_index(5))
#| let x6 : T6 = FromJson::from_json(x6, path.add_index(6))
#| let x7 : T7 = FromJson::from_json(x7, path.add_index(7))
#| let x8 : T8 = FromJson::from_json(x8, path.add_index(8))
#| let x9 : T9 = FromJson::from_json(x9, path.add_index(9))
#| let x10 : T10 = FromJson::from_json(x10, path.add_index(10))
#| let x11 : T11 = FromJson::from_json(x11, path.add_index(11))
#| let x12 : T12 = FromJson::from_json(x12, path.add_index(12))
#| let x13 : T13 = FromJson::from_json(x13, path.add_index(13))
#| let x14 : T14 = FromJson::from_json(x14, path.add_index(14))
#| let x15 : T15 = FromJson::from_json(x15, path.add_index(15))
#| (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15)
#| }
#| _ => decode_error(path, "expected tuple of size 16")
#| }
#|}
),
"tuple_to_json.mbt": (
#|pub impl[A : ToJson, B : ToJson] ToJson for (A, B) with fn to_json(self) {
#| let (a0, a1) = self
#| [a0, a1]
#|}
#|pub impl[A : ToJson, B : ToJson, C : ToJson] ToJson for (A, B, C) with fn to_json(
#| self,
#|) {
#| let (a0, a1, a2) = self
#| [a0, a1, a2]
#|}
#|pub impl[A : ToJson, B : ToJson, C : ToJson, D : ToJson] ToJson for (A, B, C, D) with fn to_json(
#| self,
#|) {
#| let (a0, a1, a2, a3) = self
#| [a0, a1, a2, a3]
#|}
#|pub impl[A : ToJson, B : ToJson, C : ToJson, D : ToJson, E : ToJson] ToJson for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4) = self
#| [a0, a1, a2, a3, a4]
#|}
#|pub impl[A : ToJson, B : ToJson, C : ToJson, D : ToJson, E : ToJson, F : ToJson] ToJson for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#|) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5) = self
#| [a0, a1, a2, a3, a4, a5]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#|] ToJson for (A, B, C, D, E, F, G) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6) = self
#| [a0, a1, a2, a3, a4, a5, a6]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#| K : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J, K) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#| K : ToJson,
#| L : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J, K, L) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#| K : ToJson,
#| L : ToJson,
#| M : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J, K, L, M) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#| K : ToJson,
#| L : ToJson,
#| M : ToJson,
#| N : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J, K, L, M, N) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#| K : ToJson,
#| L : ToJson,
#| M : ToJson,
#| N : ToJson,
#| O : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) with fn to_json(self) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14]
#|}
#|pub impl[
#| A : ToJson,
#| B : ToJson,
#| C : ToJson,
#| D : ToJson,
#| E : ToJson,
#| F : ToJson,
#| G : ToJson,
#| H : ToJson,
#| I : ToJson,
#| J : ToJson,
#| K : ToJson,
#| L : ToJson,
#| M : ToJson,
#| N : ToJson,
#| O : ToJson,
#| P : ToJson,
#|] ToJson for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) with fn to_json(
#| self,
#|) {
#| let (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) = self
#| [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15]
#|}
),
"types.mbt": (
#|#derive(Eq, ToJson)
#|pub(all) struct Position {
#| line : Int32 // 1-based
#| column : Int32 // 0-based
#|}
#|#derive(Eq, ToJson)
#|#visibility(change_to="abstract")
#|pub(all) suberror ParseError {
#| InvalidChar(Position, Char)
#| InvalidEof
#| InvalidNumber(Position, String)
#| InvalidIdentEscape(Position)
#| DuplicateKey(Position, String)
#| DepthLimitExceeded
#|}
#|pub impl Show for ParseError with fn output(self, logger) {
#| match self {
#| InvalidChar({ line, column, }, c) =>
#| logger <+
#| $|Invalid character \{c.escape()} at line \{line}, column \{column}
#| InvalidEof => logger <+ "Unexpected end of file"
#| DuplicateKey({ line, column, }, key) =>
#| logger <+
#| $|Duplicate key \{key.escape()} at line \{line}, column \{column}
#| InvalidNumber({ line, column, }, s) =>
#| logger <+
#| $|Invalid number \{s} at line \{line}, column \{column}
#| InvalidIdentEscape({ line, column, }) =>
#| logger <+
#| $|Invalid escape sequence in identifier at line \{line}, column \{column}
#| DepthLimitExceeded =>
#| logger <+
#| $|Depth limit exceeded, please increase the max_nesting_depth parameter
#| }
#|}
#|#deprecated("Use Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl Show for Json
#|#warnings("-deprecated")
#|pub impl Show for Json with fn output(self, logger) {
#| match self {
#| Null => logger <+ "Null"
#| True => logger <+ "True"
#| False => logger <+ "False"
#| Number(n, repr~) => {
#| fn write_contents(cb : &Logger) -> Unit {
#| Show::output(n, cb)
#| if repr is Some(repr) {
#| cb <+ ", repr=Some(\{repr})"
#| }
#| }
#| logger <+ "Number(\{cb => write_contents(cb)})"
#| }
#| String(s) => logger <+ "String(\{cb => Show::output(s, cb)})"
#| Array(a) => logger <+ "Array(\{cb => Show::output(a, cb)})"
#| Object(o) => logger <+ "Object(\{cb => Show::output(o, cb)})"
#| }
#|}
),
"utils.mbt": (
#|fn offset_to_position(input : StringView, offset : Int32) -> Position {
#| for code_unit in input.code_units()[:offset]; line = 1, column = 0 {
#| if code_unit == '\n' {
#| continue line + 1, 0
#| } else {
#| continue line, column + 1
#| }
#| } nobreak {
#| { line, column, }
#| }
#|}
#|fn[T] ParseContext::invalid_char(
#| ctx : ParseContext,
#| shift~ : Int32 = 0,
#|) -> T raise ParseError {
#| let offset = ctx.offset + shift
#| let replacement_char : Char = '\u{fffd}'
#| raise InvalidChar(
#| offset_to_position(ctx.input, offset),
#| ctx.input.get_char(offset).unwrap_or(replacement_char),
#| )
#|}
),
"value.mbt": (
#|pub enum Json {
#| Null
#| True
#| False
#| Number(Double, repr~ : String?) // 1.0000000000000000000e100
#| String(String)
#| Array(Array[Json])
#| Object(Map[String, Json])
#|}
#|pub impl Eq for Json with fn equal(a, b) {
#| match (a, b) {
#| (Null, Null) | (True, True) | (False, False) => true
#| (Number(a_num, ..), Number(b_num, ..)) => a_num == b_num
#| (String(a_str), String(b_str)) => a_str == b_str
#| (Array(a_arr), Array(b_arr)) => a_arr == b_arr
#| (Object(a_obj), Object(b_obj)) => a_obj == b_obj
#| _ => false
#| }
#|}
#|pub fn Json::null() -> Json {
#| return Null
#|}
#|pub let null : Json = Null
#|pub fn Json::empty_object() -> Json {
#| Object(Map([]))
#|}
#|#owned(repr)
#|pub fn Json::number(number : Double, repr? : String) -> Json {
#| return Number(number, repr~)
#|}
#|#owned(string)
#|pub fn Json::string(string : String) -> Json {
#| return String(string)
#|}
#|pub fn Json::boolean(boolean : Bool) -> Json {
#| if boolean {
#| True
#| } else {
#| False
#| }
#|}
#|#owned(array)
#|pub fn Json::array(array : Array[Json]) -> Json {
#| return Array(array)
#|}
#|#owned(object)
#|pub fn Json::object(object : Map[String, Json]) -> Json {
#| return Object(object)
#|}
#|pub fn[T : ToJson] Json::Json(value : T) -> Json {
#| ToJson::to_json(value)
#|}
#|pub impl Default for Json with fn default() {
#| false
#|}
),
},
)
///|
let moonbitlang_core_lazy_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/lazy",
deps={},
files={
"debug.mbt": (
#|pub impl[A : @debug.Debug] @debug.Debug for Lazy[A] with fn to_repr(self) {
#| match self.state {
#| Forced(v) => @debug.Repr::opaque_("Lazy", Repr(v))
#| Forcing => @debug.Repr::opaque_("Lazy", @debug.Repr::literal("forcing"))
#| Unforced(_) => @debug.Repr::opaque_("Lazy", @debug.Repr::omitted())
#| }
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Lazy with @debug.Debug::{to_repr}
),
"lazy.mbt": (
#|priv enum LazyState[A] {
#| Unforced(() -> A)
#| Forcing
#| Forced(A)
#|}
#|struct Lazy[A] {
#| mut state : LazyState[A]
#|}
#|#owned(thunk)
#|pub fn[A] Lazy::Lazy(thunk : () -> A) -> Lazy[A] {
#| { state: Unforced(thunk), }
#|}
#|#owned(value)
#|pub fn[A] Lazy::ready(value : A) -> Lazy[A] {
#| { state: Forced(value), }
#|}
#|pub fn[A] Lazy::peek(self : Lazy[A]) -> A? {
#| match self.state {
#| Forced(v) => Some(v)
#| _ => None
#| }
#|}
#|pub fn[A] Lazy::force(self : Lazy[A]) -> A {
#| match self.state {
#| Forced(v) => v
#| Forcing => abort("Lazy::force: reentrant force on the same cell")
#| Unforced(thunk) => {
#| self.state = Forcing
#| let v = thunk()
#| self.state = Forced(v)
#| v
#| }
#| }
#|}
),
},
)
///|
let moonbitlang_core_lazy_list_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/lazy_list",
deps={},
files={
"debug.mbt": (
#|pub impl[A : @debug.Debug] @debug.Debug for LazyList[A] with fn to_repr(self) {
#| let parts = []
#| for cur = self {
#| match cur.cell {
#| Empty => break
#| Cons(x, tail~) => {
#| parts.push(@debug.Repr(x))
#| match tail.peek() {
#| Some(next) => continue next
#| None => {
#| parts.push(@debug.Repr::omitted())
#| break
#| }
#| }
#| }
#| }
#| }
#| @debug.Repr::opaque_("LazyList", @debug.Repr::array(parts))
#|}
),
"extends.mbt": (
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend LazyList with Default::{default}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend LazyList with @debug.Debug::{to_repr}
),
"lazy_list.mbt": (
#|#owned(head, tail)
#|fn[A] cons(head : A, tail : LazyList[A]) -> LazyList[A] {
#| { cell: Cons(head, tail=Tail::ready(tail)), }
#|}
#|#as_free_fn
#|pub fn[A] LazyList::empty() -> LazyList[A] {
#| { cell: Empty, }
#|}
#|pub impl[A] Default for LazyList[A] with fn default() {
#| LazyList::empty()
#|}
#|pub fn[A] LazyList::LazyList(arr : ArrayView[A]) -> LazyList[A] {
#| for i = arr.length() - 1, acc = LazyList::empty(); i >= 0; {
#| continue i - 1, { cell: Cons(arr[i], tail=Tail::ready(acc)), }
#| } nobreak {
#| acc
#| }
#|}
#|#as_free_fn
#|#owned(head, tail)
#|pub fn[A] LazyList::lazy_cons(
#| head : A,
#| tail : () -> LazyList[A],
#|) -> LazyList[A] {
#| { cell: Cons(head, tail=Tail::delay(tail)), }
#|}
#|#as_free_fn
#|pub fn[A] LazyList::from_iter(iter : Iter[A]) -> LazyList[A] {
#| match iter.next() {
#| None => LazyList::empty()
#| Some(x) => LazyList::lazy_cons(x, () => LazyList::from_iter(iter))
#| }
#|}
#|pub fn[A] LazyList::is_empty(self : LazyList[A]) -> Bool {
#| self.cell is Empty
#|}
#|pub fn[A] LazyList::head(self : LazyList[A]) -> A? {
#| match self.cell {
#| Empty => None
#| Cons(x, ..) => Some(x)
#| }
#|}
#|pub fn[A] LazyList::tail(self : LazyList[A]) -> LazyList[A]? {
#| match self.cell {
#| Empty => None
#| Cons(_, tail~) => Some(tail.force())
#| }
#|}
#|pub fn[A, B] LazyList::map(self : LazyList[A], f : (A) -> B) -> LazyList[B] {
#| match self.cell {
#| Empty => LazyList::empty()
#| Cons(x, tail~) => LazyList::lazy_cons(f(x), () => tail.force().map(f))
#| }
#|}
#|pub fn[A] LazyList::filter(
#| self : LazyList[A],
#| pred : (A) -> Bool,
#|) -> LazyList[A] {
#| let mut cur = self
#| while cur.cell is Cons(x, tail~) {
#| if pred(x) {
#| let t = tail
#| return LazyList::lazy_cons(x, () => t.force().filter(pred))
#| }
#| cur = tail.force()
#| }
#| LazyList::empty()
#|}
#|pub fn[A] LazyList::take(self : LazyList[A], n : Int32) -> LazyList[A] {
#| if n <= 0 {
#| return LazyList::empty()
#| }
#| match self.cell {
#| Empty => LazyList::empty()
#| Cons(x, tail~) =>
#| if n == 1 {
#| cons(x, LazyList::empty())
#| } else {
#| LazyList::lazy_cons(x, () => tail.force().take(n - 1))
#| }
#| }
#|}
#|pub fn[A] LazyList::drop(self : LazyList[A], n : Int32) -> LazyList[A] {
#| let mut cur = self
#| let mut k = n
#| while k > 0 && cur.cell is Cons(_, tail~) {
#| cur = tail.force()
#| k -= 1
#| }
#| cur
#|}
#|pub fn[A] LazyList::take_while(
#| self : LazyList[A],
#| pred : (A) -> Bool,
#|) -> LazyList[A] {
#| match self.cell {
#| Empty => LazyList::empty()
#| Cons(x, tail~) =>
#| if pred(x) {
#| LazyList::lazy_cons(x, () => tail.force().take_while(pred))
#| } else {
#| LazyList::empty()
#| }
#| }
#|}
#|pub fn[A] LazyList::drop_while(
#| self : LazyList[A],
#| pred : (A) -> Bool raise?,
#|) -> LazyList[A] raise? {
#| let mut cur = self
#| while cur.cell is Cons(x, tail~) && pred(x) {
#| cur = tail.force()
#| }
#| cur
#|}
#|#owned(other)
#|pub fn[A] LazyList::concat(
#| self : LazyList[A],
#| other : LazyList[A],
#|) -> LazyList[A] {
#| match self.cell {
#| Empty => other
#| Cons(x, tail~) =>
#| if other.cell is Empty {
#| self
#| } else {
#| { cell: Cons(x, tail=tail.append(One(Tail::ready(other)))), }
#| }
#| }
#|}
#|fn[A] LazyList::concat_lazy(
#| self : LazyList[A],
#| other_thunk : () -> LazyList[A],
#|) -> LazyList[A] {
#| match self.cell {
#| Empty => other_thunk()
#| Cons(x, tail~) =>
#| { cell: Cons(x, tail=tail.append(One(Tail::delay(other_thunk)))), }
#| }
#|}
#|pub fn[A, B] LazyList::flat_map(
#| self : LazyList[A],
#| f : (A) -> LazyList[B],
#|) -> LazyList[B] {
#| let mut cur = self
#| while cur.cell is Cons(x, tail~) {
#| match f(x).cell {
#| Cons(y, tail=ys) => {
#| let outer_tail = tail
#| return LazyList::lazy_cons(y, () => {
#| ys.force().concat_lazy(() => outer_tail.force().flat_map(f))
#| })
#| }
#| Empty => cur = tail.force()
#| }
#| }
#| LazyList::empty()
#|}
#|pub fn[A, B] LazyList::zip(
#| self : LazyList[A],
#| other : LazyList[B],
#|) -> LazyList[(A, B)] {
#| match (self.cell, other.cell) {
#| (Cons(x, tail=tx), Cons(y, tail=ty)) =>
#| LazyList::lazy_cons((x, y), () => {
#| match tx.force().cell {
#| Empty => LazyList::empty()
#| Cons(_) => tx.force().zip(ty.force())
#| }
#| })
#| _ => LazyList::empty()
#| }
#|}
#|pub fn[A] LazyList::each(
#| self : LazyList[A],
#| f : (A) -> Unit raise?,
#|) -> Unit raise? {
#| let mut cur = self
#| while cur.cell is Cons(x, tail~) {
#| f(x)
#| cur = tail.force()
#| }
#|}
#|pub fn[A, B] LazyList::fold(
#| self : LazyList[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| let mut cur = self
#| let mut acc = init
#| while cur.cell is Cons(x, tail~) {
#| acc = f(acc, x)
#| cur = tail.force()
#| }
#| acc
#|}
#|pub fn[A] LazyList::to_array(self : LazyList[A]) -> Array[A] {
#| let arr = []
#| self.each(x => arr.push(x))
#| arr
#|}
#|pub fn[A] LazyList::iter(self : LazyList[A]) -> Iter[A] {
#| let mut advance : () -> LazyList[A] = () => self
#| Iter(() => {
#| match advance().cell {
#| Empty => None
#| Cons(head, tail~) => {
#| advance = () => tail.force()
#| Some(head)
#| }
#| }
#| })
#|}
),
"tail.mbt": (
#|#owned(value)
#|fn[A] Tail::ready(value : LazyList[A]) -> Tail[A] {
#| { state: Forced(value), }
#|}
#|#owned(thunk)
#|fn[A] Tail::delay(thunk : () -> LazyList[A]) -> Tail[A] {
#| { state: Unforced(thunk), }
#|}
#|#owned(pending)
#|fn[A] Tail::append(self : Tail[A], pending : Pending[A]) -> Tail[A] {
#| { state: Append(self, pending), }
#|}
#|fn[A] Tail::peek(self : Tail[A]) -> LazyList[A]? {
#| match self.state {
#| Forced(v) => Some(v)
#| _ => None
#| }
#|}
#|fn[A] Tail::force(self : Tail[A]) -> LazyList[A] {
#| match self.state {
#| Forced(v) => v
#| Forcing => abort("LazyList: reentrant force on the same tail")
#| Unforced(thunk) => {
#| self.state = Forcing
#| let v = thunk()
#| self.state = Forced(v)
#| v
#| }
#| Append(inner, pending) => {
#| self.state = Forcing
#| let (base, rest) = for cur = inner, rest = pending {
#| match cur.state {
#| Append(next, ps) => continue next, ps.concat(rest)
#| _ => break (cur.force(), rest)
#| }
#| }
#| let value = base.append_pending(rest)
#| self.state = Forced(value)
#| value
#| }
#| }
#|}
#|fn[A] LazyList::append_pending(
#| self : LazyList[A],
#| pending : Pending[A],
#|) -> LazyList[A] {
#| for cur = self, rest = pending {
#| match cur.cell {
#| Cons(x, tail~) =>
#| break if rest is Nil {
#| cur
#| } else {
#| { cell: Cons(x, tail=tail.append(rest)), }
#| }
#| Empty =>
#| match rest.uncons() {
#| None => break cur
#| Some((t, more)) => continue t.force(), more
#| }
#| }
#| }
#|}
#|#owned(other)
#|fn[A] Pending::concat(self : Pending[A], other : Pending[A]) -> Pending[A] {
#| match (self, other) {
#| (Nil, _) => other
#| (_, Nil) => self
#| _ => Cat(self, other)
#| }
#|}
#|fn[A] Pending::uncons(self : Pending[A]) -> (Tail[A], Pending[A])? {
#| for cur = self {
#| match cur {
#| Nil => break None
#| One(t) => break Some((t, Nil))
#| Cat(One(t), b) => break Some((t, b))
#| Cat(Cat(x, y), b) => continue Cat(x, Cat(y, b))
#| Cat(Nil, b) => continue b
#| }
#| }
#|}
),
"types.mbt": (
#|priv enum LazyListCell[A] {
#| Empty
#| Cons(A, tail~ : Tail[A])
#|}
#|priv struct Tail[A] {
#| mut state : TailState[A]
#|}
#|priv enum TailState[A] {
#| Unforced(() -> LazyList[A])
#| Forcing
#| Append(Tail[A], Pending[A])
#| Forced(LazyList[A])
#|}
#|priv enum Pending[A] {
#| Nil
#| One(Tail[A])
#| Cat(Pending[A], Pending[A])
#|}
#|struct LazyList[A] {
#| cell : LazyListCell[A]
#|}
),
},
)
///|
let moonbitlang_core_lexbuf_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/lexbuf",
deps={},
files={
"async_bytes_lexbuf.mbt": (
#|struct AsyncBytesLexbuf {
#| source : async () -> Bytes?
#| storage : BytesLexbufStorage
#| mut cursor : Int32
#| mut retained_from : Int32
#| mut eof : Bool
#|}
#|#alias(from_fn)
#|pub fn AsyncBytesLexbuf::AsyncBytesLexbuf(
#| source : async () -> Bytes?,
#|) -> AsyncBytesLexbuf {
#| {
#| source,
#| storage: BytesLexbufStorage(),
#| cursor: 0,
#| retained_from: -1,
#| eof: false,
#| }
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__get_cursor(self : AsyncBytesLexbuf) -> Int32 {
#| self.cursor
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__buffer_end(self : AsyncBytesLexbuf) -> Int32 {
#| self.storage.buffer_end
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__eof_reached(self : AsyncBytesLexbuf) -> Bool {
#| self.eof
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__unsafe_code_unit_at(
#| self : AsyncBytesLexbuf,
#| position : Int32,
#|) -> Int32 {
#| debug_assert(() => {
#| position >= self.storage.buffer_start && position < self.__buffer_end()
#| })
#| self.storage.unsafe_code_unit_at(position)
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__advance(
#| self : AsyncBytesLexbuf,
#| count : Int32,
#|) -> Unit {
#| debug_assert(() => count >= 0 && self.cursor + count <= self.__buffer_end())
#| self.cursor += count
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__retain_from_cursor(self : AsyncBytesLexbuf) -> Unit {
#| if self.retained_from < 0 || self.cursor < self.retained_from {
#| self.retained_from = self.cursor
#| }
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__get_bytesview(
#| self : AsyncBytesLexbuf,
#| start : Int32,
#| end : Int32,
#|) -> BytesView {
#| guard self.retained_from >= 0 &&
#| self.retained_from <= start &&
#| start <= end &&
#| start >= self.storage.buffer_start &&
#| end <= self.__buffer_end() else {
#| abort("AsyncBytesLexbuf::__get_bytesview: range was not retained")
#| }
#| self.storage.get_bytesview(start, end)
#|}
#|#doc(hidden)
#|pub fn AsyncBytesLexbuf::__commit_to(
#| self : AsyncBytesLexbuf,
#| position : Int32,
#|) -> Unit {
#| guard self.retained_from >= 0 &&
#| self.storage.buffer_start <= position &&
#| position <= self.cursor else {
#| abort("AsyncBytesLexbuf::__commit_to: position is not retained")
#| }
#| self.cursor = position
#| self.retained_from = -1
#|}
#|#doc(hidden)
#|pub async fn AsyncBytesLexbuf::__refill(self : AsyncBytesLexbuf) -> Unit {
#| guard self.cursor == self.__buffer_end() else {
#| abort("AsyncBytesLexbuf::__refill: unread buffered input remains")
#| }
#| self.compact_buffer()
#| while !self.eof {
#| match (self.source)() {
#| None => {
#| self.eof = true
#| return
#| }
#| Some(chunk) =>
#| if !chunk.is_empty() {
#| self.storage.append(chunk)
#| return
#| }
#| }
#| }
#|}
#|fn AsyncBytesLexbuf::compact_buffer(self : AsyncBytesLexbuf) -> Unit {
#| let keep_from = if self.retained_from >= 0 {
#| self.retained_from
#| } else {
#| self.cursor
#| }
#| self.storage.discard_before(keep_from)
#|}
),
"async_lexbuf.mbt": (
#|struct AsyncLexbuf {
#| source : async () -> String?
#| storage : LexbufStorage
#| mut cursor : Int32
#| mut retained_from : Int32
#| mut eof : Bool
#|}
#|#alias(from_fn)
#|pub fn AsyncLexbuf::AsyncLexbuf(source : async () -> String?) -> AsyncLexbuf {
#| {
#| source,
#| storage: LexbufStorage(),
#| cursor: 0,
#| retained_from: -1,
#| eof: false,
#| }
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__get_cursor(self : AsyncLexbuf) -> Int32 {
#| self.cursor
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__buffer_end(self : AsyncLexbuf) -> Int32 {
#| self.storage.buffer_end
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__eof_reached(self : AsyncLexbuf) -> Bool {
#| self.eof
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__unsafe_code_unit_at(
#| self : AsyncLexbuf,
#| position : Int32,
#|) -> Int32 {
#| debug_assert(() => {
#| position >= self.storage.buffer_start && position < self.__buffer_end()
#| })
#| self.storage.unsafe_code_unit_at(position)
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__advance(self : AsyncLexbuf, count : Int32) -> Unit {
#| debug_assert(() => count >= 0 && self.cursor + count <= self.__buffer_end())
#| self.cursor += count
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__retain_from_cursor(self : AsyncLexbuf) -> Unit {
#| if self.retained_from < 0 || self.cursor < self.retained_from {
#| self.retained_from = self.cursor
#| }
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__get_stringview(
#| self : AsyncLexbuf,
#| start : Int32,
#| end : Int32,
#|) -> StringView {
#| guard self.retained_from >= 0 &&
#| self.retained_from <= start &&
#| start <= end &&
#| start >= self.storage.buffer_start &&
#| end <= self.__buffer_end() else {
#| abort("AsyncLexbuf::__get_stringview: range was not retained")
#| }
#| self.storage.get_stringview(start, end)
#|}
#|#doc(hidden)
#|pub fn AsyncLexbuf::__commit_to(self : AsyncLexbuf, position : Int32) -> Unit {
#| guard self.retained_from >= 0 &&
#| self.storage.buffer_start <= position &&
#| position <= self.cursor else {
#| abort("AsyncLexbuf::__commit_to: position is not retained")
#| }
#| self.cursor = position
#| self.retained_from = -1
#|}
#|#doc(hidden)
#|pub async fn AsyncLexbuf::__refill(self : AsyncLexbuf) -> Unit {
#| guard self.cursor == self.__buffer_end() else {
#| abort("AsyncLexbuf::__refill: unread buffered input remains")
#| }
#| self.compact_buffer()
#| while !self.eof {
#| match (self.source)() {
#| None => {
#| self.eof = true
#| return
#| }
#| Some(chunk) =>
#| if !chunk.is_empty() {
#| self.storage.append(chunk)
#| return
#| }
#| }
#| }
#|}
#|fn AsyncLexbuf::compact_buffer(self : AsyncLexbuf) -> Unit {
#| let keep_from = if self.retained_from >= 0 {
#| self.retained_from
#| } else {
#| self.cursor
#| }
#| self.storage.discard_before(keep_from)
#|}
),
"bytes_lexbuf.mbt": (
#|struct BytesLexbuf {
#| source : () -> Bytes?
#| storage : BytesLexbufStorage
#| mut cursor : Int32
#| mut retained_from : Int32
#| mut eof : Bool
#|}
#|#alias(from_fn)
#|pub fn BytesLexbuf::BytesLexbuf(source : () -> Bytes?) -> BytesLexbuf {
#| {
#| source,
#| storage: BytesLexbufStorage(),
#| cursor: 0,
#| retained_from: -1,
#| eof: false,
#| }
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__get_cursor(self : BytesLexbuf) -> Int32 {
#| self.cursor
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__buffer_end(self : BytesLexbuf) -> Int32 {
#| self.storage.buffer_end
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__eof_reached(self : BytesLexbuf) -> Bool {
#| self.eof
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__unsafe_code_unit_at(
#| self : BytesLexbuf,
#| position : Int32,
#|) -> Int32 {
#| debug_assert(() => {
#| position >= self.storage.buffer_start && position < self.__buffer_end()
#| })
#| self.storage.unsafe_code_unit_at(position)
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__advance(self : BytesLexbuf, count : Int32) -> Unit {
#| debug_assert(() => count >= 0 && self.cursor + count <= self.__buffer_end())
#| self.cursor += count
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__retain_from_cursor(self : BytesLexbuf) -> Unit {
#| if self.retained_from < 0 || self.cursor < self.retained_from {
#| self.retained_from = self.cursor
#| }
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__get_bytesview(
#| self : BytesLexbuf,
#| start : Int32,
#| end : Int32,
#|) -> BytesView {
#| guard self.retained_from >= 0 &&
#| self.retained_from <= start &&
#| start <= end &&
#| start >= self.storage.buffer_start &&
#| end <= self.__buffer_end() else {
#| abort("BytesLexbuf::__get_bytesview: range was not retained")
#| }
#| self.storage.get_bytesview(start, end)
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__commit_to(self : BytesLexbuf, position : Int32) -> Unit {
#| guard self.retained_from >= 0 &&
#| self.storage.buffer_start <= position &&
#| position <= self.cursor else {
#| abort("BytesLexbuf::__commit_to: position is not retained")
#| }
#| self.cursor = position
#| self.retained_from = -1
#|}
#|#doc(hidden)
#|pub fn BytesLexbuf::__refill(self : BytesLexbuf) -> Unit {
#| guard self.cursor == self.__buffer_end() else {
#| abort("BytesLexbuf::__refill: unread buffered input remains")
#| }
#| self.compact_buffer()
#| while !self.eof {
#| match (self.source)() {
#| None => {
#| self.eof = true
#| break
#| }
#| Some(chunk) =>
#| if !chunk.is_empty() {
#| self.storage.append(chunk)
#| break
#| }
#| }
#| }
#|}
#|fn BytesLexbuf::compact_buffer(self : BytesLexbuf) -> Unit {
#| let keep_from = if self.retained_from >= 0 {
#| self.retained_from
#| } else {
#| self.cursor
#| }
#| self.storage.discard_before(keep_from)
#|}
),
"bytes_scanner.mbt": (
#|pub struct BytesScanner {
#| data : BytesView
#| mut cursor : Int32
#|}
#|pub fn BytesScanner::BytesScanner(
#| data : BytesView,
#| cursor~ : Int32 = 0,
#|) -> BytesScanner {
#| guard cursor >= 0 && cursor <= data.length() else {
#| abort("BytesScanner: cursor is out of range")
#| }
#| { data, cursor, }
#|}
#|#doc(hidden)
#|pub fn BytesScanner::__unsafe_set_cursor(
#| self : BytesScanner,
#| cursor : Int32,
#|) -> Unit {
#| self.cursor = cursor
#|}
),
"bytes_storage.mbt": (
#|priv struct BytesLexbufStorage {
#| mut chunks : Array[Bytes]
#| mut chunk_starts : Array[Int32]
#| mut lookup_chunk : Int32
#| mut buffer_start : Int32
#| mut buffer_end : Int32
#|}
#|fn BytesLexbufStorage::BytesLexbufStorage() -> BytesLexbufStorage {
#| {
#| chunks: [],
#| chunk_starts: [],
#| lookup_chunk: 0,
#| buffer_start: 0,
#| buffer_end: 0,
#| }
#|}
#|fn BytesLexbufStorage::append(self : BytesLexbufStorage, chunk : Bytes) -> Unit {
#| guard !chunk.is_empty() else { abort("BytesLexbufStorage: empty chunk") }
#| if self.chunks is [.., last] &&
#| last.length() + chunk.length() <= LEXBUF_MERGED_CHUNK_LIMIT {
#| self.chunks[self.chunks.length() - 1] = last + chunk
#| self.buffer_end += chunk.length()
#| return
#| }
#| self.chunk_starts.push(self.buffer_end)
#| self.chunks.push(chunk)
#| self.buffer_end += chunk.length()
#|}
#|fn BytesLexbufStorage::chunk_contains(
#| self : BytesLexbufStorage,
#| index : Int32,
#| position : Int32,
#|) -> Bool {
#| if index < 0 || index >= self.chunks.length() {
#| return false
#| }
#| let start = self.chunk_starts.unsafe_get(index)
#| start <= position && position < start + self.chunks.unsafe_get(index).length()
#|}
#|fn BytesLexbufStorage::find_chunk(
#| self : BytesLexbufStorage,
#| position : Int32,
#|) -> Int32 {
#| let cached = self.lookup_chunk
#| if self.chunk_contains(cached, position) {
#| return cached
#| }
#| if self.chunk_contains(cached + 1, position) {
#| self.lookup_chunk = cached + 1
#| return cached + 1
#| }
#| if self.chunk_contains(cached - 1, position) {
#| self.lookup_chunk = cached - 1
#| return cached - 1
#| }
#| let low = for low = 0, high = self.chunks.length(); low < high; {
#| let middle = low + (high - low) / 2
#| if self.chunk_starts.unsafe_get(middle) <= position {
#| continue middle + 1, high
#| } else {
#| continue low, middle
#| }
#| } nobreak {
#| low
#| }
#| let index = low - 1
#| guard self.chunk_contains(index, position) else {
#| abort("BytesLexbufStorage: position is not buffered")
#| }
#| self.lookup_chunk = index
#| index
#|}
#|fn BytesLexbufStorage::unsafe_code_unit_at(
#| self : BytesLexbufStorage,
#| position : Int32,
#|) -> Int32 {
#| let index = self.find_chunk(position)
#| let chunk = self.chunks.unsafe_get(index)
#| chunk.unsafe_get(position - self.chunk_starts.unsafe_get(index)).to_int()
#|}
#|fn BytesLexbufStorage::get_bytesview(
#| self : BytesLexbufStorage,
#| start : Int32,
#| end : Int32,
#|) -> BytesView {
#| if start == end {
#| return b""
#| }
#| let first = self.find_chunk(start)
#| let first_chunk = self.chunks.unsafe_get(first)
#| let first_start = self.chunk_starts.unsafe_get(first)
#| let first_end = first_start + first_chunk.length()
#| if end <= first_end {
#| return first_chunk.unsafe_view(
#| start=start - first_start,
#| end=end - first_start,
#| )
#| }
#| let joined = FixedArray::make(end - start, b'\x00')
#| joined.blit_from_bytesview(
#| 0,
#| first_chunk.unsafe_view(start=start - first_start, end=first_chunk.length()),
#| )
#| for index in (first + 1).. Unit {
#| guard self.buffer_start <= position && position <= self.buffer_end else {
#| abort("BytesLexbufStorage: invalid retention state")
#| }
#| let length = self.chunks.length()
#| let first = for index in 0.. position {
#| break index
#| }
#| } nobreak {
#| length
#| }
#| self.buffer_start = position
#| if first == length {
#| self.chunks.clear()
#| self.chunk_starts.clear()
#| } else if first > 0 {
#| let chunks : Array[Bytes] = []
#| let chunk_starts : Array[Int32] = []
#| for index in first.. Lexbuf {
#| {
#| source: () => None,
#| storage: LexbufStorage::from_string(content),
#| cursor: 0,
#| retained_from: -1,
#| eof: true,
#| }
#|}
),
"lexbuf.mbt": (
#|struct Lexbuf {
#| source : () -> String?
#| storage : LexbufStorage
#| mut cursor : Int32
#| mut retained_from : Int32
#| mut eof : Bool
#|}
#|#alias(from_fn)
#|pub fn Lexbuf::Lexbuf(source : () -> String?) -> Lexbuf {
#| {
#| source,
#| storage: LexbufStorage(),
#| cursor: 0,
#| retained_from: -1,
#| eof: false,
#| }
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__get_cursor(self : Lexbuf) -> Int32 {
#| self.cursor
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__buffer_end(self : Lexbuf) -> Int32 {
#| self.storage.buffer_end
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__eof_reached(self : Lexbuf) -> Bool {
#| self.eof
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__unsafe_code_unit_at(self : Lexbuf, position : Int32) -> Int32 {
#| debug_assert(() => {
#| position >= self.storage.buffer_start && position < self.__buffer_end()
#| })
#| self.storage.unsafe_code_unit_at(position)
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__advance(self : Lexbuf, count : Int32) -> Unit {
#| debug_assert(() => count >= 0 && self.cursor + count <= self.__buffer_end())
#| self.cursor += count
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__retain_from_cursor(self : Lexbuf) -> Unit {
#| if self.retained_from < 0 || self.cursor < self.retained_from {
#| self.retained_from = self.cursor
#| }
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__get_stringview(
#| self : Lexbuf,
#| start : Int32,
#| end : Int32,
#|) -> StringView {
#| guard self.retained_from >= 0 &&
#| self.retained_from <= start &&
#| start <= end &&
#| start >= self.storage.buffer_start &&
#| end <= self.__buffer_end() else {
#| abort("Lexbuf::__get_stringview: range was not retained")
#| }
#| self.storage.get_stringview(start, end)
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__commit_to(self : Lexbuf, position : Int32) -> Unit {
#| guard self.retained_from >= 0 &&
#| self.storage.buffer_start <= position &&
#| position <= self.cursor else {
#| abort("Lexbuf::__commit_to: position is not retained")
#| }
#| self.cursor = position
#| self.retained_from = -1
#|}
#|#doc(hidden)
#|pub fn Lexbuf::__refill(self : Lexbuf) -> Unit {
#| guard self.cursor == self.__buffer_end() else {
#| abort("Lexbuf::__refill: unread buffered input remains")
#| }
#| self.compact_buffer()
#| while !self.eof {
#| match (self.source)() {
#| None => {
#| self.eof = true
#| break
#| }
#| Some(chunk) =>
#| if !chunk.is_empty() {
#| self.storage.append(chunk)
#| break
#| }
#| }
#| }
#|}
#|fn Lexbuf::compact_buffer(self : Lexbuf) -> Unit {
#| let keep_from = if self.retained_from >= 0 {
#| self.retained_from
#| } else {
#| self.cursor
#| }
#| self.storage.discard_before(keep_from)
#|}
),
"read_line.mbt": (
#|pub fn Lexbuf::read_line(self : Lexbuf) -> StringView? {
#| lexscan self {
#| (re"^[^\n]*" as line) + re"\r\n" => Some(line)
#| (re"^[^\n]*" as line) + re"\n" => Some(line)
#| re"^[^\n]+" as line => Some(line)
#| _ => None
#| }
#|}
#|pub async fn AsyncLexbuf::read_line(self : AsyncLexbuf) -> StringView? {
#| lexscan self {
#| (re"^[^\n]*" as line) + re"\r\n" => Some(line)
#| (re"^[^\n]*" as line) + re"\n" => Some(line)
#| re"^[^\n]+" as line => Some(line)
#| _ => None
#| }
#|}
),
"scannable.mbt": (
#|pub(open) trait Scannable {
#| fn scan_input(Self) -> StringView
#| fn scan_cursor(Self) -> Int32
#| fn set_scan_cursor(Self, Int32) -> Unit
#|}
#|pub(open) trait BytesScannable {
#| fn scan_input(Self) -> BytesView
#| fn scan_cursor(Self) -> Int32
#| fn set_scan_cursor(Self, Int32) -> Unit
#|}
#|pub impl Scannable for StringScanner with fn scan_input(self) {
#| self.data
#|}
#|pub impl Scannable for StringScanner with fn scan_cursor(self) {
#| self.cursor
#|}
#|pub impl Scannable for StringScanner with fn set_scan_cursor(self, cursor) {
#| guard cursor >= 0 && cursor <= self.data.length() else {
#| abort("StringScanner: cursor is out of range")
#| }
#| guard cursor == self.data.length() ||
#| !self.data.unsafe_get(cursor).is_trailing_surrogate() else {
#| abort("StringScanner: cursor is inside a surrogate pair")
#| }
#| self.cursor = cursor
#|}
#|pub impl BytesScannable for BytesScanner with fn scan_input(self) {
#| self.data
#|}
#|pub impl BytesScannable for BytesScanner with fn scan_cursor(self) {
#| self.cursor
#|}
#|pub impl BytesScannable for BytesScanner with fn set_scan_cursor(self, cursor) {
#| guard cursor >= 0 && cursor <= self.data.length() else {
#| abort("BytesScanner: cursor is out of range")
#| }
#| self.cursor = cursor
#|}
#|#deprecated("Call through `@lexbuf.Scannable`", skip_current_package=true)
#|#doc(hidden)
#|pub extend StringScanner with Scannable::{
#| scan_input,
#| scan_cursor,
#| set_scan_cursor,
#|}
#|#deprecated("Call through `@lexbuf.BytesScannable`", skip_current_package=true)
#|#doc(hidden)
#|pub extend BytesScanner with BytesScannable::{
#| scan_input,
#| scan_cursor,
#| set_scan_cursor,
#|}
),
"storage.mbt": (
#|priv struct LexbufStorage {
#| mut chunks : Array[String]
#| mut chunk_starts : Array[Int32]
#| mut lookup_chunk : Int32
#| mut buffer_start : Int32
#| mut buffer_end : Int32
#|}
#|const LEXBUF_MERGED_CHUNK_LIMIT : Int32 = 64
#|fn LexbufStorage::LexbufStorage() -> LexbufStorage {
#| {
#| chunks: [],
#| chunk_starts: [],
#| lookup_chunk: 0,
#| buffer_start: 0,
#| buffer_end: 0,
#| }
#|}
#|fn LexbufStorage::from_string(content : String) -> LexbufStorage {
#| let storage = LexbufStorage()
#| if !content.is_empty() {
#| storage.append(content)
#| }
#| storage
#|}
#|fn LexbufStorage::append(self : LexbufStorage, chunk : String) -> Unit {
#| guard !chunk.is_empty() else { abort("LexbufStorage: empty chunk") }
#| if self.chunks is [.., last] &&
#| last.length() + chunk.length() <= LEXBUF_MERGED_CHUNK_LIMIT {
#| self.chunks[self.chunks.length() - 1] = last + chunk
#| self.buffer_end += chunk.length()
#| return
#| }
#| self.chunk_starts.push(self.buffer_end)
#| self.chunks.push(chunk)
#| self.buffer_end += chunk.length()
#|}
#|fn LexbufStorage::chunk_contains(
#| self : LexbufStorage,
#| index : Int32,
#| position : Int32,
#|) -> Bool {
#| if index < 0 || index >= self.chunks.length() {
#| return false
#| }
#| let start = self.chunk_starts.unsafe_get(index)
#| start <= position && position < start + self.chunks.unsafe_get(index).length()
#|}
#|fn LexbufStorage::find_chunk(self : LexbufStorage, position : Int32) -> Int32 {
#| let cached = self.lookup_chunk
#| if self.chunk_contains(cached, position) {
#| return cached
#| }
#| if self.chunk_contains(cached + 1, position) {
#| self.lookup_chunk = cached + 1
#| return cached + 1
#| }
#| if self.chunk_contains(cached - 1, position) {
#| self.lookup_chunk = cached - 1
#| return cached - 1
#| }
#| let low = for low = 0, high = self.chunks.length(); low < high; {
#| let middle = low + (high - low) / 2
#| if self.chunk_starts.unsafe_get(middle) <= position {
#| continue middle + 1, high
#| } else {
#| continue low, middle
#| }
#| } nobreak {
#| low
#| }
#| let index = low - 1
#| guard self.chunk_contains(index, position) else {
#| abort("LexbufStorage: position is not buffered")
#| }
#| self.lookup_chunk = index
#| index
#|}
#|fn LexbufStorage::unsafe_code_unit_at(
#| self : LexbufStorage,
#| position : Int32,
#|) -> Int32 {
#| let index = self.find_chunk(position)
#| let chunk = self.chunks.unsafe_get(index)
#| chunk.unsafe_get(position - self.chunk_starts.unsafe_get(index)).to_int()
#|}
#|fn LexbufStorage::get_stringview(
#| self : LexbufStorage,
#| start : Int32,
#| end : Int32,
#|) -> StringView {
#| if start == end {
#| return ""
#| }
#| let first = self.find_chunk(start)
#| let first_chunk = self.chunks.unsafe_get(first)
#| let first_start = self.chunk_starts.unsafe_get(first)
#| let first_end = first_start + first_chunk.length()
#| if end <= first_end {
#| return first_chunk.unsafe_view(
#| start=start - first_start,
#| end=end - first_start,
#| )
#| }
#| let builder = StringBuilder(capacity=(end - start) * 2)
#| builder.write_view(
#| first_chunk.unsafe_view(start=start - first_start, end=first_chunk.length()),
#| )
#| for index in (first + 1).. Unit {
#| guard self.buffer_start <= position && position <= self.buffer_end else {
#| abort("LexbufStorage: invalid retention state")
#| }
#| let length = self.chunks.length()
#| let first = for index in 0.. position {
#| break index
#| }
#| } nobreak {
#| length
#| }
#| self.buffer_start = position
#| if first == length {
#| self.chunks.clear()
#| self.chunk_starts.clear()
#| } else if first > 0 {
#| let chunks : Array[String] = []
#| let chunk_starts : Array[Int32] = []
#| for index in first.. StringScanner {
#| guard cursor >= 0 && cursor <= data.length() else {
#| abort("StringScanner: cursor is out of range")
#| }
#| guard cursor == data.length() ||
#| !data.unsafe_get(cursor).is_trailing_surrogate() else {
#| abort("StringScanner: cursor is inside a surrogate pair")
#| }
#| { data, cursor, }
#|}
#|#doc(hidden)
#|pub fn StringScanner::__unsafe_set_cursor(
#| self : StringScanner,
#| cursor : Int32,
#|) -> Unit {
#| self.cursor = cursor
#|}
),
},
)
///|
let moonbitlang_core_list_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/list",
deps={},
files={
"debug.mbt": (
#|pub impl[A : @debug.Debug] @debug.Debug for List[A] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "List",
#| @debug.Repr::array(self.to_array().map(x => Repr(x))),
#| )
#|}
#|test "Debug for List" {
#| let xs : List[Int32] = List([1, 2, 3])
#| @debug.debug_inspect(xs, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("use `_.to_array().rev_fold(...)` instead")
#|pub fn[A, B] List::rev_fold(self : List[A], init~ : B, f : (B, A) -> B) -> B {
#| let xs = self.to_array()
#| xs.rev_iter().fold(init~, f)
#|}
#|#deprecated("use `_.rev().foldi(...)` instead")
#|pub fn[A, B] List::rev_foldi(
#| self : List[A],
#| init~ : B,
#| f : (Int32, B, A) -> B,
#|) -> B {
#| self.rev().foldi(init~, (i, b, a) => f(i, b, a))
#|}
#|#deprecated("use `unsafe_tail` instead")
#|pub fn[A] List::tail(self : List[A]) -> List[A] {
#| match self {
#| Empty => Empty
#| More(_, tail~) => tail
#| }
#|}
#|#as_free_fn(deprecated="Use `@json.from_json` instead")
#|#deprecated("Use `@json.from_json` instead")
#|pub fn[A : @json.FromJson] List::from_json(
#| json : @json.Json,
#|) -> List[A] raise @json.JsonDecodeError {
#| @json.from_json(json)
#|}
#|#alias(empty, deprecated="Use `List([])` instead")
#|#as_free_fn(deprecated="Use `List([])` instead")
#|#as_free_fn(empty, deprecated="Use `List([])` instead")
#|#deprecated("Use `List([])` instead")
#|#inline
#|pub fn[A] List::new() -> List[A] {
#| Empty
#|}
#|#deprecated("Use `find_with_index` and swap the callback parameters to (index, element)")
#|#locals(f)
#|pub fn[A] List::findi(
#| self : List[A],
#| f : (A, Int32) -> Bool raise?,
#|) -> A? raise? {
#| for list = self, index = 0 {
#| match list {
#| Empty => break None
#| More(element, tail=list) =>
#| if f(element, index) {
#| break Some(element)
#| } else {
#| continue list, index + 1
#| }
#| }
#| }
#|}
),
"extends.mbt": (
#|pub extend List with Compare::{compare}
#|pub extend List with Eq::{equal}
#|pub extend List with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend List with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend List with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend List with Eq::{not_equal}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend List with Hash::{hash_combine}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend List with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend List with @json.ToJson::{to_json}
),
"list.mbt": (
#|#as_free_fn
#|#as_free_fn(construct, deprecated="Use cons instead")
#|#owned(head, tail)
#|pub fn[A] List::cons(head : A, tail : List[A]) -> List[A] {
#| More(head, tail~)
#|}
#|#alias(add, deprecated="Use `prepend` instead")
#|#owned(head)
#|pub fn[A] List::prepend(self : List[A], head : A) -> List[A] {
#| More(head, tail=self)
#|}
#|pub impl[A : Eq] Eq for List[A] with fn equal(self, other) -> Bool {
#| if physical_equal(self, other) {
#| return true
#| }
#| match (self, other) {
#| (Empty, Empty) => true
#| (More(x, tail=xs), More(y, tail=ys)) => x == y && xs == ys
#| _ => false
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show] Show for List[A]
#|pub impl[A : Show] Show for List[A] with fn output(xs, logger) {
#| logger.write_iter(xs.iter(), prefix="@list.from_array([", suffix="])")
#|}
#|pub impl[A : @json.ToJson] @json.ToJson for List[A] with fn to_json(self) {
#| let capacity = self.length()
#| guard capacity != 0 else { return [] }
#| let jsons = Array(capacity~)
#| for a in self {
#| jsons.push(a.to_json())
#| }
#| @json.Json::array(jsons)
#|}
#|pub impl[A : @json.FromJson] @json.FromJson for List[A] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(arr) else {
#| raise path.error("@list.from_json: expected array")
#| }
#| for i in arr.length()>..0; list = Empty {
#| continue list.prepend(A::from_json(arr[i], path.add_index(i)))
#| } nobreak {
#| list
#| }
#|}
#|#as_free_fn(deprecated="Use @list.List([...]) instead")
#|#alias(of, deprecated="Use @list.List([...]) instead")
#|#as_free_fn(of, deprecated="Use @list.List([...]) instead")
#|#deprecated("Use @list.List([...]) instead")
#|pub fn[A] List::from_array(arr : ArrayView[A]) -> List[A] {
#| List(arr)
#|}
#|pub fn[A] List::List(arr : ArrayView[A]) -> List[A] {
#| for i in arr.length()>..0; list = Empty {
#| continue More(arr[i], tail=list)
#| } nobreak {
#| list
#| }
#|}
#|pub fn[A] List::length(self : List[A]) -> Int32 {
#| for x = self, acc = 0 {
#| match (x, acc) {
#| (Empty, len) => break len
#| (More(_, tail=rest), acc) => continue rest, acc + 1
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::each(self : List[A], f : (A) -> Unit raise?) -> Unit raise? {
#| for x = self {
#| match x {
#| Empty => break
#| More(head, tail~) => {
#| f(head)
#| continue tail
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::eachi(
#| self : List[A],
#| f : (Int32, A) -> Unit raise?,
#|) -> Unit raise? {
#| for x = self, i = 0 {
#| match (x, i) {
#| (Empty, _) => break
#| (More(x, tail=xs), i) => {
#| f(i, x)
#| continue xs, i + 1
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, B] List::map(self : List[A], f : (A) -> B raise?) -> List[B] raise? {
#| match self {
#| Empty => Empty
#| More(hd, tail~) => {
#| let dest = More(f(hd), tail=Empty)
#| for d = dest, t = tail {
#| match (d, t) {
#| (_, Empty) => break
#| (More(_) as dest, More(hd, tail~)) => {
#| dest.tail = More(f(hd), tail=Empty)
#| continue dest.tail, tail
#| }
#| (Empty, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, B] List::mapi(
#| self : List[A],
#| f : (Int32, A) -> B raise?,
#|) -> List[B] raise? {
#| match self {
#| Empty => Empty
#| More(hd, tail~) => {
#| let dest = More(f(0, hd), tail=Empty)
#| for i = 1, d = dest, t = tail {
#| match (i, d, t) {
#| (_, _, Empty) => break
#| (i, More(_) as dest, More(hd, tail~)) => {
#| dest.tail = More(f(i, hd), tail=Empty)
#| continue i + 1, dest.tail, tail
#| }
#| (_, Empty, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, B] List::rev_map(
#| self : List[A],
#| f : (A) -> B raise?,
#|) -> List[B] raise? {
#| for a = Empty, b = self {
#| match (a, b) {
#| (acc, Empty) => break acc
#| (acc, More(x, tail=xs)) => continue More(f(x), tail=acc), xs
#| }
#| }
#|}
#|pub fn[A] List::to_array(self : List[A]) -> Array[A] {
#| [
#| for x in self => x
#| ]
#|}
#|#locals(f)
#|pub fn[A] List::filter(
#| self : List[A],
#| f : (A) -> Bool raise?,
#|) -> List[A] raise? {
#| for x = self {
#| match x {
#| Empty => break Empty
#| More(head, tail~) =>
#| if !f(head) {
#| continue tail
#| } else {
#| let dest = More(head, tail=Empty)
#| for d = dest, t = tail {
#| match (d, t) {
#| (_, Empty) => break
#| (More(_) as dest, More(hd, tail~)) =>
#| if f(hd) {
#| dest.tail = More(hd, tail=Empty)
#| continue dest.tail, tail
#| } else {
#| continue dest, tail
#| }
#| (Empty, _) =>
#| panic()
#| }
#| }
#| break dest
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::all(self : List[A], f : (A) -> Bool raise?) -> Bool raise? {
#| for x = self {
#| match x {
#| Empty => break true
#| More(head, tail~) => if f(head) { continue tail } else { break false }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::any(self : List[A], f : (A) -> Bool raise?) -> Bool raise? {
#| for x = self {
#| match x {
#| Empty => break false
#| More(head, tail~) => if f(head) { break true } else { continue tail }
#| }
#| }
#|}
#|#internal(unsafe, "Panic if the list is empty")
#|#doc(hidden)
#|pub fn[A] List::unsafe_head(self : List[A]) -> A {
#| match self {
#| Empty => abort("head of empty list")
#| More(head, tail=_) => head
#| }
#|}
#|pub fn[A] List::unsafe_tail(self : List[A]) -> List[A] {
#| match self {
#| Empty => abort("tail of empty list")
#| More(_, tail~) => tail
#| }
#|}
#|pub fn[A] List::head(self : List[A]) -> A? {
#| match self {
#| Empty => None
#| More(head, tail=_) => Some(head)
#| }
#|}
#|#internal(unsafe, "Panic if the list is empty")
#|#doc(hidden)
#|pub fn[A] List::unsafe_last(self : List[A]) -> A {
#| for x = self {
#| match x {
#| Empty => break abort("last of empty list")
#| More(head, tail=Empty) => break head
#| More(_, tail~) => continue tail
#| }
#| }
#|}
#|pub fn[A] List::last(self : List[A]) -> A? {
#| for x = self {
#| match x {
#| Empty => break None
#| More(head, tail=Empty) => break Some(head)
#| More(_, tail~) => continue tail
#| }
#| }
#|}
#|#owned(other)
#|pub fn[A] List::concat(self : List[A], other : List[A]) -> List[A] {
#| match self {
#| Empty => other
#| More(hd, tail=Empty) => More(hd, tail=other)
#| More(hd, tail~) => {
#| let dest = More(hd, tail=Empty)
#| for d = dest, t = tail {
#| match (d, t) {
#| (More(_) as dest, Empty) => {
#| dest.tail = other
#| break
#| }
#| (More(_) as dest, More(head, tail~)) => {
#| dest.tail = More(head, tail=Empty)
#| continue dest.tail, tail
#| }
#| (Empty, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|#owned(other)
#|pub fn[A] List::rev_concat(self : List[A], other : List[A]) -> List[A] {
#| for a = self, b = other {
#| match (a, b) {
#| (Empty, other) => break other
#| (More(head, tail~), other) => continue tail, More(head, tail=other)
#| }
#| }
#|}
#|pub fn[A] List::rev(self : List[A]) -> List[A] {
#| self.rev_concat(Empty)
#|}
#|#locals(f)
#|pub fn[A, B] List::fold(
#| self : List[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| for x = self, acc = init {
#| match (x, acc) {
#| (Empty, acc) => break acc
#| (More(head, tail~), acc) => continue tail, f(acc, head)
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, B] List::foldi(
#| self : List[A],
#| init~ : B,
#| f : (Int32, B, A) -> B raise?,
#|) -> B raise? {
#| for xs = self, i = 0, acc = init {
#| match xs {
#| Empty => break acc
#| More(x, tail=rest) => continue rest, i + 1, f(i, acc, x)
#| }
#| }
#|}
#|#as_free_fn
#|pub fn[A, B] List::zip(self : List[A], other : List[B]) -> List[(A, B)] {
#| let res = for a = self, b = other, acc = Empty {
#| match (a, b, acc) {
#| (Empty, _, acc) | (_, Empty, acc) => break acc
#| (More(x, tail=xs), More(y, tail=ys), acc) =>
#| continue xs, ys, More((x, y), tail=acc)
#| }
#| }
#| res.reverse_in_place()
#|}
#|#locals(f)
#|pub fn[A, B] List::flat_map(
#| self : List[A],
#| f : (A) -> List[B] raise?,
#|) -> List[B] raise? {
#| for x = self {
#| match x {
#| Empty => break Empty
#| More(head, tail~) =>
#| match f(head) {
#| Empty => continue tail
#| More(hd, tail=tl) => {
#| let dest = More(hd, tail=Empty)
#| let dest1 = for d = dest, t = tl {
#| match (d, t) {
#| (dest, Empty) => break dest
#| (More(_) as dest, More(hd, tail~)) => {
#| dest.tail = More(hd, tail=Empty)
#| continue dest.tail, tail
#| }
#| (Empty, _) => panic()
#| }
#| }
#| loop_over_tail~: for d = dest1, t = tail {
#| match (d, t) {
#| (_, Empty) => break loop_over_tail~
#| (More(_) as dest, More(t_hd, tail=Empty)) => {
#| dest.tail = f(t_hd)
#| break loop_over_tail~
#| }
#| (dest, More(t_hd, tail=t_tl)) =>
#| for d2 = dest, t2 = f(t_hd) {
#| match (d2, t2) {
#| (dest, Empty) => continue loop_over_tail~ dest, t_tl
#| (More(_) as dest, More(hd, tail~)) => {
#| dest.tail = More(hd, tail=Empty)
#| continue dest.tail, tail
#| }
#| (Empty, _) => panic()
#| }
#| }
#| }
#| }
#| break dest
#| }
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, B] List::filter_map(
#| self : List[A],
#| f : (A) -> B? raise?,
#|) -> List[B] raise? {
#| for x = self {
#| match x {
#| Empty => break Empty
#| More(hd, tail~) =>
#| match f(hd) {
#| None => continue tail
#| Some(head) => {
#| let dest = More(head, tail=Empty)
#| for d = dest, t = tail {
#| match (d, t) {
#| (_, Empty) => break
#| (More(_) as dest, More(hd, tail~)) =>
#| match f(hd) {
#| None => continue dest, tail
#| Some(head) => {
#| dest.tail = More(head, tail=Empty)
#| continue dest.tail, tail
#| }
#| }
#| (Empty, _) => panic()
#| }
#| }
#| break dest
#| }
#| }
#| }
#| }
#|}
#|#internal(unsafe, "Panic if the index is out of bounds")
#|#doc(hidden)
#|pub fn[A] List::unsafe_nth(self : List[A], n : Int32) -> A {
#| for x = self, i = n {
#| match (x, i) {
#| (Empty, _) => break abort("nth: index out of bounds")
#| (More(head, tail=_), 0) => break head
#| (More(_, tail~), n) => continue tail, n - 1
#| }
#| }
#|}
#|pub fn[A] List::nth(self : List[A], n : Int32) -> A? {
#| for x = self, i = n {
#| match (x, i) {
#| (Empty, _) => break None
#| (More(head, tail=_), 0) => break Some(head)
#| (More(_, tail~), n) => continue tail, n - 1
#| }
#| }
#|}
#|#as_free_fn
#|pub fn[A] List::repeat(n : Int32, x : A) -> List[A] {
#| if n < 0 {
#| abort("negative repeat count")
#| }
#| for acc = Empty, i = n {
#| if i <= 0 {
#| break acc
#| } else {
#| continue More(x, tail=acc), i - 1
#| }
#| }
#|}
#|pub fn[A] List::intersperse(self : List[A], separator : A) -> List[A] {
#| match self {
#| Empty => Empty
#| More(head, tail=Empty) => More(head, tail=Empty)
#| More(head, tail~) => {
#| let dest = More(head, tail=Empty)
#| for d = dest, t = tail {
#| match (d, t) {
#| (_, Empty) => break
#| (More(_) as dest, More(hd, tail=tl)) => {
#| let new_tail = More(hd, tail=Empty)
#| dest.tail = More(separator, tail=new_tail)
#| continue new_tail, tl
#| }
#| (Empty, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|pub fn[A] List::is_empty(self : List[A]) -> Bool {
#| self is Empty
#|}
#|pub fn[A, B] List::unzip(self : List[(A, B)]) -> (List[A], List[B]) {
#| match self {
#| Empty => (Empty, Empty)
#| More((x, y), tail~) => {
#| let xs = More(x, tail=Empty)
#| let ys = More(y, tail=Empty)
#| for a = tail, b = xs, c = ys {
#| match (a, b, c) {
#| (Empty, _, _) => break
#| (More((x, y), tail~), More(_) as xptr, More(_) as yptr) => {
#| xptr.tail = More(x, tail=Empty)
#| yptr.tail = More(y, tail=Empty)
#| continue tail, xptr.tail, yptr.tail
#| }
#| (_, _, _) => break abort("unreachable")
#| }
#| }
#| (xs, ys)
#| }
#| }
#|}
#|pub fn[A] List::flatten(self : List[List[A]]) -> List[A] {
#| for x = self {
#| match x {
#| Empty => break Empty
#| More(head, tail~) =>
#| match head {
#| Empty => continue tail
#| More(hd, tail=tl) => {
#| let dest = More(hd, tail=Empty)
#| let dest1 = for d = dest, t = tl {
#| match (d, t) {
#| (dest, Empty) => break dest
#| (More(_) as dest, More(hd, tail~)) => {
#| dest.tail = More(hd, tail=Empty)
#| continue dest.tail, tail
#| }
#| (Empty, _) => panic()
#| }
#| }
#| loop_over_tail~: for d = dest1, t = tail {
#| match (d, t) {
#| (_, Empty) => break loop_over_tail~
#| (More(_) as dest, More(t_hd, tail=Empty)) => {
#| dest.tail = t_hd
#| break loop_over_tail~
#| }
#| (dest, More(t_hd, tail=t_tl)) =>
#| for d2 = dest, t2 = t_hd {
#| match (d2, t2) {
#| (dest, Empty) => continue loop_over_tail~ dest, t_tl
#| (More(_) as dest, More(hd, tail~)) => {
#| dest.tail = More(hd, tail=Empty)
#| continue dest.tail, tail
#| }
#| (Empty, _) => panic()
#| }
#| }
#| }
#| }
#| break dest
#| }
#| }
#| }
#| }
#|}
#|#internal(unsafe, "Panic if the list is empty")
#|#doc(hidden)
#|pub fn[A : Compare] List::unsafe_maximum(self : List[A]) -> A {
#| guard self.maximum() is Some(max) else { abort("maximum: empty list") }
#| max
#|}
#|pub fn[A : Compare] List::maximum(self : List[A]) -> A? {
#| guard self is More(head, tail~) else { return None }
#| for rest = tail, curr_max = head {
#| match rest {
#| Empty => break Some(curr_max)
#| More(item, tail~) =>
#| continue tail, if item > curr_max { item } else { curr_max }
#| }
#| }
#|}
#|#internal(unsafe, "Panic if the list is empty")
#|#doc(hidden)
#|pub fn[A : Compare] List::unsafe_minimum(self : List[A]) -> A {
#| guard self.minimum() is Some(min) else { abort("minimum: empty list") }
#| min
#|}
#|pub fn[A : Compare] List::minimum(self : List[A]) -> A? {
#| guard self is More(head, tail~) else { return None }
#| for rest = tail, curr_min = head {
#| match rest {
#| Empty => break Some(curr_min)
#| More(item, tail~) =>
#| continue tail, if item < curr_min { item } else { curr_min }
#| }
#| }
#|}
#|pub fn[A : Compare] List::sort(self : List[A]) -> List[A] {
#| let arr = self.to_array()
#| arr.sort()
#| List(arr)
#|}
#|pub impl[A] Add for List[A] with fn add(self, other) {
#| self.concat(other)
#|}
#|pub fn[A : Eq] List::contains(self : List[A], value : A) -> Bool {
#| for cur = self {
#| match cur {
#| Empty => break false
#| More(x, tail=xs) => if x == value { break true } else { continue xs }
#| }
#| }
#|}
#|#as_free_fn
#|#locals(f)
#|pub fn[A, S] List::unfold(
#| f : (S) -> (A, S)? raise?,
#| init~ : S,
#|) -> List[A] raise? {
#| match f(init) {
#| None => Empty
#| Some((element, new_state)) => {
#| let dest = More(element, tail=Empty)
#| for d = dest, t = f(new_state) {
#| match (d, t) {
#| (_, None) => break
#| (More(_) as dest, Some((element, new_state))) => {
#| dest.tail = More(element, tail=Empty)
#| continue dest.tail, f(new_state)
#| }
#| (Empty, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|#as_free_fn
#|#locals(f)
#|pub fn[A, S] List::rev_unfold(
#| f : (S) -> (A, S)? raise?,
#| init~ : S,
#|) -> List[A] raise? {
#| for a = f(init), b = Empty {
#| match (a, b) {
#| (None, acc) => break acc
#| (Some((x, s)), acc) => continue f(s), More(x, tail=acc)
#| }
#| }
#|}
#|pub fn[A] List::take(self : List[A], n : Int32) -> List[A] {
#| if n <= 0 {
#| Empty
#| } else {
#| match self {
#| Empty => Empty
#| More(head, tail~) => {
#| let dest = More(head, tail=Empty)
#| for d = dest, t = tail, i = n - 1 {
#| match (d, t, i) {
#| (_, Empty, _) | (_, _, 0) => break
#| (More(_) as dest, More(x, tail=xs), n) => {
#| dest.tail = More(x, tail=Empty)
#| continue dest.tail, xs, n - 1
#| }
#| (Empty, _, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#| }
#|}
#|pub fn[A] List::drop(self : List[A], n : Int32) -> List[A] {
#| if n <= 0 {
#| self
#| } else {
#| for a = n, b = self {
#| match (a, b) {
#| (1, More(_, tail=xs)) | ((_, Empty) with xs = Empty) => break xs
#| (n, More(_, tail=xs)) => continue n - 1, xs
#| }
#| }
#| }
#|}
#|#locals(p)
#|pub fn[A] List::take_while(
#| self : List[A],
#| p : (A) -> Bool raise?,
#|) -> List[A] raise? {
#| match self {
#| Empty => Empty
#| More(head, tail~) =>
#| if p(head) {
#| let dest = More(head, tail=Empty)
#| for d = dest, t = tail {
#| match (d, t) {
#| (_, Empty) => break
#| (More(_) as dest, More(x, tail=xs)) if p(x) => {
#| dest.tail = More(x, tail=Empty)
#| continue dest.tail, xs
#| }
#| (More(_), _) => break
#| (Empty, _) => panic()
#| }
#| }
#| dest
#| } else {
#| Empty
#| }
#| }
#|}
#|#locals(p)
#|pub fn[A] List::drop_while(
#| self : List[A],
#| p : (A) -> Bool raise?,
#|) -> List[A] raise? {
#| for cur = self {
#| match cur {
#| Empty => break Empty
#| More(x, tail=xs) =>
#| if p(x) {
#| continue xs
#| } else {
#| break More(x, tail=xs)
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A, E] List::scan_left(
#| self : List[A],
#| f : (E, A) -> E raise?,
#| init~ : E,
#|) -> List[E] raise? {
#| let dest = More(init, tail=Empty)
#| for d = dest, s = self, a = init {
#| match (d, s, a) {
#| (_, Empty, _) => break
#| (Empty, _, _) => panic()
#| (More(_) as dest, More(x, tail=xs), acc) => {
#| let next = f(acc, x)
#| dest.tail = More(next, tail=Empty)
#| continue dest.tail, xs, next
#| }
#| }
#| }
#| dest
#|}
#|pub fn[A, B] List::scan_right(
#| self : List[A],
#| f : (B, A) -> B raise?,
#| init~ : B,
#|) -> List[B] raise? {
#| self.rev().scan_left(f, init~).reverse_in_place()
#|}
#|pub fn[A : Eq, B] List::lookup(self : List[(A, B)], v : A) -> B? {
#| for cur = self {
#| match cur {
#| Empty => break None
#| More((x, y), tail=xs) => if x == v { break Some(y) } else { continue xs }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::find(self : List[A], f : (A) -> Bool raise?) -> A? raise? {
#| for cur = self {
#| match cur {
#| Empty => break None
#| More(element, tail=list) =>
#| if f(element) {
#| break Some(element)
#| } else {
#| continue list
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::find_index(
#| self : Self[A],
#| f : (A) -> Bool raise?,
#|) -> Int32? raise? {
#| for cur = self, idx = 0 {
#| match (cur, idx) {
#| (Empty, _) => break None
#| (More(element, tail=list), idx) =>
#| if f(element) {
#| break Some(idx)
#| } else {
#| continue list, idx + 1
#| }
#| }
#| }
#|}
#|#locals(f)
#|pub fn[A] List::find_with_index(
#| self : List[A],
#| f : (Int32, A) -> Bool raise?,
#|) -> A? raise? {
#| for index, element in self {
#| if f(index, element) {
#| return Some(element)
#| }
#| }
#| None
#|}
#|pub fn[A] List::remove_at(self : List[A], index : Int32) -> List[A] {
#| match (index, self) {
#| (0, More(_, tail~))
#| | ((_, Empty) with tail = Empty)
#| | ((_..<0, _) with tail = self) => tail
#| (n, More(head, tail~)) => {
#| let dest = More(head, tail=Empty)
#| for d = dest, t = tail, i = n - 1 {
#| match (d, t, i) {
#| (_, Empty, _) => break
#| (More(_) as dest, More(_, tail~), 0) => {
#| dest.tail = tail
#| break
#| }
#| (More(_) as dest, More(x, tail=xs), n) => {
#| dest.tail = More(x, tail=Empty)
#| continue dest.tail, xs, n - 1
#| }
#| (Empty, _, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|pub fn[A : Eq] List::remove(self : List[A], elem : A) -> List[A] {
#| match self {
#| Empty => Empty
#| More(head, tail~) if head == elem => tail
#| More(head, tail~) => {
#| let dest = More(head, tail~)
#| for d = dest, t = tail {
#| match (d, t) {
#| (_, Empty) => break
#| (More(_) as dest, More(x, tail=xs)) =>
#| if x == elem {
#| dest.tail = xs
#| break
#| } else {
#| dest.tail = More(x, tail=Empty)
#| continue dest.tail, xs
#| }
#| (Empty, _) => panic()
#| }
#| }
#| dest
#| }
#| }
#|}
#|#alias(is_prefix, deprecated)
#|pub fn[A : Eq] List::has_prefix(self : List[A], prefix : List[A]) -> Bool {
#| for a = self, b = prefix {
#| match (a, b) {
#| (_, Empty) => break true
#| (Empty, More(_)) => break false
#| (More(h1, tail=t1), More(h2, tail=t2)) =>
#| if h1 == h2 {
#| continue t1, t2
#| } else {
#| break false
#| }
#| }
#| }
#|}
#|#alias(is_suffix, deprecated)
#|pub fn[A : Eq] List::has_suffix(self : List[A], suffix : List[A]) -> Bool {
#| guard suffix is More(_) else { return true }
#| for lead = self, probe = suffix {
#| match (lead, probe) {
#| (_, Empty) =>
#| break for lag = self, ahead = lead {
#| match (lag, ahead) {
#| (_, Empty) => break lag.has_prefix(suffix)
#| (More(_, tail=lag_tail), More(_, tail=ahead_tail)) =>
#| continue lag_tail, ahead_tail
#| (Empty, More(_)) =>
#| break false
#| }
#| }
#| (Empty, More(_)) => break false
#| (More(_, tail=lead_tail), More(_, tail=probe_tail)) =>
#| continue lead_tail, probe_tail
#| }
#| }
#|}
#|pub fn[A] List::intercalate(self : List[List[A]], sep : List[A]) -> List[A] {
#| self.intersperse(sep).flatten()
#|}
#|pub impl[X] Default for List[X] with fn default() {
#| Empty
#|}
#|#deprecated("Use `List([])` or `Default::default()` instead")
#|pub fn[X] default() -> List[X] {
#| Empty
#|}
#|#alias(iterator, deprecated)
#|pub fn[A] List::iter(self : List[A]) -> Iter[A] {
#| let mut next = self
#| Iter(fn() {
#| match next {
#| Empty => None
#| More(head, tail~) => {
#| next = tail
#| Some(head)
#| }
#| }
#| })
#|}
#|#alias(iterator2, deprecated)
#|pub fn[A] List::iter2(self : List[A]) -> Iter2[Int32, A] {
#| let mut i = 0
#| let mut next = self
#| Iter(fn() {
#| match next {
#| Empty => None
#| More(head, tail~) => {
#| let result = (i, head)
#| next = tail
#| i += 1
#| Some(result)
#| }
#| }
#| })
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A] List::from_iter(iter : Iter[A]) -> List[A] {
#| let mut head = Empty
#| let mut tail = Empty
#| for x in iter {
#| match tail {
#| Empty => {
#| tail = More(x, tail~)
#| head = tail
#| }
#| More(_) as prev_tail => {
#| tail = More(x, tail=Empty)
#| prev_tail.tail = tail
#| }
#| }
#| }
#| head
#|}
#|#as_free_fn
#|#alias(from_iterator_rev, deprecated)
#|#as_free_fn(from_iterator_rev, deprecated)
#|pub fn[A] List::from_iter_rev(iter : Iter[A]) -> List[A] {
#| iter.fold(init=Empty, (acc, e) => More(e, tail=acc))
#|}
#|#as_free_fn
#|#owned(x)
#|pub fn[A] List::singleton(x : A) -> List[A] {
#| More(x, tail=Empty)
#|}
#|pub impl[A : Hash] Hash for List[A] with fn hash_combine(self, hasher) {
#| for e in self {
#| hasher.combine(e)
#| }
#|}
#|fn[A] List::reverse_in_place(self : List[A]) -> List[A] {
#| match self {
#| Empty | More(_, tail=Empty) => self
#| More(head, tail~) =>
#| for a = More(head, tail=Empty), b = tail {
#| match (a, b) {
#| (result, Empty) => break result
#| (result, More(_, tail=xs) as new_result) => {
#| new_result.tail = result
#| continue new_result, xs
#| }
#| }
#| }
#| }
#|}
#|pub impl[A : Compare] Compare for List[A] with fn compare(self, other) {
#| for a = self, b = other {
#| match (a, b) {
#| (Empty, Empty) => break 0
#| (Empty, More(_, tail=_)) => break -1
#| (More(_, tail=_), Empty) => break 1
#| (More(x, tail=xs), More(y, tail=ys)) => {
#| let cmp = x.compare(y)
#| if cmp != 0 {
#| break cmp
#| } else {
#| continue xs, ys
#| }
#| }
#| }
#| }
#|}
),
"types.mbt": (
#|#unsafe_cycle_free
#|pub enum List[A] {
#| Empty
#| More(A, mut tail~ : List[A])
#|}
),
},
)
///|
let moonbitlang_core_math_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/math",
deps={},
files={
"algebraic.mbt": (
#|pub fn cbrtf(x : @float.Float) -> @float.Float {
#| let b1 : UInt32 = 709958130 // B1 = (127-127.0/3-0.03306235651)*2**23 */
#| let b2 : UInt32 = 642849266 // B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
#| let mut ui : UInt32 = x.reinterpret_as_uint()
#| let mut hx : UInt32 = ui & 0x7fffffff
#| if hx >= 0x7f800000 {
#| return x + x
#| }
#| if hx < 0x00800000 {
#| if hx == 0 {
#| return x
#| } // cbrt(+-0) is itx
#| ui = (x * @float.Float(0x1.0p24)).reinterpret_as_uint()
#| hx = ui & 0x7fffffff
#| hx = hx / 3 + b2
#| } else {
#| hx = hx / 3 + b1
#| }
#| ui = ui & 0x80000000
#| ui = ui | hx
#| let dx = x.to_double()
#| let t = @float.Float::reinterpret_from_uint(ui).to_double()
#| let r = t * t * t
#| let t = t * (dx + dx + r) / (dx + r + r)
#| let r = t * t * t
#| let t = t * (dx + dx + r) / (dx + r + r)
#| @float.Float::from_double(t)
#|}
),
"algebraic_double_js.mbt": (
#|pub fn cbrt(x : Double) -> Double = "Math" "cbrt"
),
"deprecated.mbt": (
#|#deprecated("Use `PI` instead")
#|pub let pi = 0x3.243F6A8885A308CA8A54
#|#deprecated("Use @cmp.maximum instead")
#|pub fn[T : Compare] maximum(x : T, y : T) -> T {
#| if x > y {
#| x
#| } else {
#| y
#| }
#|}
#|#deprecated("Use @cmp.minimum instead")
#|pub fn[T : Compare] minimum(x : T, y : T) -> T {
#| if x > y {
#| y
#| } else {
#| x
#| }
#|}
),
"exp.mbt": (
#|fn top12(x : @float.Float) -> UInt32 {
#| x.reinterpret_as_uint() >> 20
#|}
#|fn __math_xflowf(sign : UInt32, y : @float.Float) -> @float.Float {
#| return (if sign != 0 { -y } else { y }) * y
#|}
#|fn __math_oflowf(sign : UInt32) -> @float.Float {
#| return __math_xflowf(sign, 0x1.0p97)
#|}
#|fn __math_uflowf(sign : UInt32) -> @float.Float {
#| return __math_xflowf(sign, 0x1.0p-95)
#|}
#|let exp2f_table_bits = 5
#|priv struct Exp2fData {
#| tab : FixedArray[UInt64]
#| shift : Double
#| invln2_scaled : Double
#| poly_scaled : FixedArray[Double]
#|}
#|let expf_n : UInt64 = (1 << exp2f_table_bits).to_uint64()
#|let exp2f_data_n : Double = (1 << exp2f_table_bits).to_double()
#|let exp2f_data : Exp2fData = {
#| tab: [
#| 0x3ff0000000000000, 0x3fefd9b0d3158574, 0x3fefb5586cf9890f, 0x3fef9301d0125b51,
#| 0x3fef72b83c7d517b, 0x3fef54873168b9aa, 0x3fef387a6e756238, 0x3fef1e9df51fdee1,
#| 0x3fef06fe0a31b715, 0x3feef1a7373aa9cb, 0x3feedea64c123422, 0x3feece086061892d,
#| 0x3feebfdad5362a27, 0x3feeb42b569d4f82, 0x3feeab07dd485429, 0x3feea47eb03a5585,
#| 0x3feea09e667f3bcd, 0x3fee9f75e8ec5f74, 0x3feea11473eb0187, 0x3feea589994cce13,
#| 0x3feeace5422aa0db, 0x3feeb737b0cdc5e5, 0x3feec49182a3f090, 0x3feed503b23e255d,
#| 0x3feee89f995ad3ad, 0x3feeff76f2fb5e47, 0x3fef199bdd85529c, 0x3fef3720dcef9069,
#| 0x3fef5818dcfba487, 0x3fef7c97337b9b5f, 0x3fefa4afa2a490da, 0x3fefd0765b6e4540,
#| ],
#| shift: 0x1.8p+52,
#| invln2_scaled: 0x1.71547652b82fep+0 * exp2f_data_n,
#| poly_scaled: [
#| 0x1.c6af84b912394p-5 / exp2f_data_n / exp2f_data_n / exp2f_data_n,
#| 0x1.ebfce50fac4f3p-3 / exp2f_data_n / exp2f_data_n,
#| 0x1.62e42ff0c52d6p-1 / exp2f_data_n,
#| ],
#|}
#|pub fn expf(x : @float.Float) -> @float.Float {
#| let xd = x.to_double()
#| let abstop = top12(x) & 0x7ff
#| if abstop >= top12(88.0) {
#| if x.reinterpret_as_uint() == @float.neg_infinity.reinterpret_as_uint() {
#| return 0.0
#| }
#| if abstop >= top12(@float.infinity) {
#| return x + x
#| }
#| if x > 0x1.62e42ep6 {
#| return __math_oflowf(0)
#| }
#| if x < -0x1.9fe368p6 {
#| return __math_uflowf(0)
#| }
#| }
#| let z = exp2f_data.invln2_scaled * xd
#| let kd = z + exp2f_data.shift
#| let ki = kd.reinterpret_as_uint64()
#| let kd = kd - exp2f_data.shift
#| let r = z - kd
#| let t = exp2f_data.tab[(ki % expf_n).to_int()]
#| let t = t + (ki << (52 - exp2f_table_bits))
#| let s = t.reinterpret_as_double()
#| let z = exp2f_data.poly_scaled[0] * r + exp2f_data.poly_scaled[1]
#| let r2 = r * r
#| let y = exp2f_data.poly_scaled[2] * r + 1
#| let y = z * r2 + y
#| let y = y * s
#| @float.Float::from_double(y)
#|}
#|pub fn expm1f(x : @float.Float) -> @float.Float {
#| let float_ln2_hi : @float.Float = 6.9314575195e-01 // 0x3f317200
#| let float_ln2_lo : @float.Float = 1.4286067653e-06 // 0x35bfbe8e
#| let inv_ln2 : @float.Float = 1.4426950216e+00 // 0x3fb8aa3b
#| let mut x = x
#| let q1 : @float.Float = -3.3333212137e-2 // -0x888868.0p-28
#| let q2 : @float.Float = 1.5807170421e-3 // 0xcf3010.0p-33
#| let mut hx = x.reinterpret_as_uint()
#| let sign = hx >> 31 != 0
#| hx = hx & 0x7fffffff
#| if hx >= 0x4195b844 {
#| if hx > 0x7f800000 {
#| return x
#| }
#| if sign {
#| return -1.0
#| }
#| if hx > 0x42b17217 {
#| x *= @float.Float(0x1.0p127)
#| return x
#| }
#| }
#| let mut k : Int32 = 0
#| let mut hi : @float.Float = 0
#| let mut lo : @float.Float = 0
#| let mut c : @float.Float = 0
#| if hx > 0x3eb17218 {
#| if hx < 0x3F851592 {
#| if !sign {
#| hi = x - float_ln2_hi
#| lo = float_ln2_lo
#| k = 1
#| } else {
#| hi = x + float_ln2_hi
#| lo = -float_ln2_lo
#| k = -1
#| }
#| } else {
#| k = (inv_ln2 * x + (if sign { -0.5 } else { 0.5 })).to_int()
#| hi = x - @float.Float::from_int(k) * float_ln2_hi // t*ln2_hi is exact here
#| lo = @float.Float::from_int(k) * float_ln2_lo
#| }
#| x = hi - lo
#| c = hi - x - lo
#| } else if hx < 0x33000000 {
#| return x
#| } else {
#| k = 0
#| }
#| let hfx = @float.Float(0.5) * x
#| let hxs = x * hfx
#| let r1 = @float.Float(1.0) + hxs * (q1 + hxs * q2)
#| let t = @float.Float(3.0) - r1 * hfx
#| let mut e = hxs * ((r1 - t) / (@float.Float(6.0) - x * t))
#| if k == 0 {
#| return x - (x * e - hxs)
#| }
#| e = x * (e - c) - c
#| e -= hxs
#| if k == -1 {
#| return @float.Float(0.5) * (x - e) - 0.5
#| }
#| if k == 1 {
#| if x < -0.25 {
#| return -@float.Float(2.0) * (e - (x + 0.5))
#| }
#| return @float.Float(1.0) + @float.Float(2.0) * (x - e)
#| }
#| let twopk = @float.Float::reinterpret_from_int((0x7f + k) << 23) // 2^k
#| if !(k is (0..<=56)) {
#| let mut y = x - e + 1.0
#| if k == 128 {
#| y = y * 2.0 * @float.Float(0x1.0p127)
#| } else {
#| y *= twopk
#| }
#| return y - 1.0
#| }
#| let uf = @float.Float::reinterpret_from_int((0x7f - k) << 23) // 2^-k
#| if k < 23 {
#| (x - e + (@float.Float(1.0) - uf)) * twopk
#| } else {
#| (x - (e + uf) + 1.0) * twopk
#| }
#|}
),
"exp_double_js.mbt": (
#|pub fn exp(x : Double) -> Double = "Math" "exp"
#|pub fn expm1(x : Double) -> Double = "Math" "expm1"
),
"hyperbolic.mbt": (
#|fn k_expo2f(x : @float.Float) -> @float.Float {
#| let k = 235
#| let k_ln2 = @float.Float::reinterpret_from_int(0x4322e3bc)
#| let scale = @float.Float::reinterpret_from_int((0x7f + k / 2) << 23)
#| expf(x - k_ln2) * scale * scale
#|}
#|pub fn sinhf(x : @float.Float) -> @float.Float {
#| let mut h : @float.Float = 0.5
#| let mut ix = x.reinterpret_as_uint()
#| if ix >> 31 != 0 {
#| h = -h
#| }
#| ix = ix & 0x7fffffff
#| let absx = @float.Float::reinterpret_from_uint(ix)
#| let w = ix
#| if w < 0x42b17217 {
#| let t = expm1f(absx)
#| if w < 0x3f800000 {
#| if w < 0x3f800000U - (12U << 23) {
#| return x
#| }
#| return h * (@float.Float(2.0) * t - t * t / (t + 1.0))
#| }
#| return h * (t + t / (t + 1.0))
#| }
#| h * k_expo2f(absx) * 2.0
#|}
#|pub fn coshf(x : @float.Float) -> @float.Float {
#| let mut x = x
#| let mut ix = x.reinterpret_as_uint()
#| ix = ix & 0x7fffffff
#| x = @float.Float::reinterpret_from_uint(ix)
#| let w = ix
#| if w < 0x3f317217 {
#| if w < 0x3f800000U - (12U << 23) {
#| return 1.0
#| }
#| let t = expm1f(x)
#| return @float.Float(1.0) + t * t / (@float.Float(2.0) * (t + 1.0))
#| }
#| if w < 0x42b17217 {
#| let t = expf(x)
#| return (t + @float.Float(1.0) / t) * 0.5
#| }
#| k_expo2f(x)
#|}
#|pub fn tanhf(x : @float.Float) -> @float.Float {
#| let mut ix = x.reinterpret_as_uint()
#| let sign = ix >> 31 != 0
#| ix = ix & 0x7fffffff
#| let x = @float.Float::reinterpret_from_uint(ix)
#| let w = ix
#| let tt = if w > 0x3f0c9f54 {
#| if w > 0x41200000 {
#| @float.Float(1.0) + @float.Float(0.0) / x
#| } else {
#| let t = expm1f(x * 2.0)
#| @float.Float(1.0) - @float.Float(2.0) / (t + 2.0)
#| }
#| } else if w > 0x3e82c578 {
#| let t = expm1f(x * 2.0)
#| t / (t + 2.0)
#| } else if w >= 0x00800000 {
#| let t = expm1f(x * -2.0)
#| -t / (t + 2.0)
#| } else {
#| x
#| }
#| if sign {
#| -tt
#| } else {
#| tt
#| }
#|}
#|pub fn asinhf(x : @float.Float) -> @float.Float {
#| let u = x.reinterpret_as_uint()
#| let i = u & 0x7fffffff
#| let sign = u >> 31 != 0
#| let ln2 : @float.Float = 0.693147180559945309417232121458176568
#| let x = @float.Float::reinterpret_from_uint(i)
#| let x = if i >= 0x3f800000U + (12U << 23) {
#| lnf(x) + ln2
#| } else if i >= 0x3f800000U + (1U << 23) {
#| lnf(x * 2.0 + @float.Float(1.0) / ((x * x + 1.0).sqrt() + x))
#| } else if i >= 0x3f800000U - (12U << 23) {
#| ln_1pf(x + x * x / ((x * x + 1.0).sqrt() + 1.0))
#| } else {
#| x
#| }
#| if sign {
#| -x
#| } else {
#| x
#| }
#|}
#|pub fn acoshf(x : @float.Float) -> @float.Float {
#| let ln2 : @float.Float = 0.693147180559945309417232121458176568
#| let u = x.reinterpret_as_uint()
#| let a = u & 0x7fffffffU
#| if a < 0x3f800000U + (1U << 23) {
#| return ln_1pf(
#| x - 1.0 + ((x - 1.0) * (x - 1.0) + @float.Float(2.0) * (x - 1.0)).sqrt(),
#| )
#| }
#| if u < 0x3f800000U + (12U << 23) {
#| return lnf(x * 2.0 - @float.Float(1.0) / (x + (x * x - 1.0).sqrt()))
#| }
#| return lnf(x) + ln2
#|}
#|pub fn atanhf(x : @float.Float) -> @float.Float {
#| let u = x.reinterpret_as_uint()
#| let sign = u >> 31 != 0
#| let u = u & 0x7fffffff
#| let x = @float.Float::reinterpret_from_uint(u)
#| let x = if u < 0x3f800000U - (1U << 23) {
#| if u < 0x3f800000U - (32U << 23) {
#| x
#| } else {
#| ln_1pf(x * 2.0 + x * 2.0 * x / (@float.Float(1.0) - x)) * 0.5
#| }
#| } else {
#| ln_1pf(x / (@float.Float(1.0) - x) * 2.0) * 0.5
#| }
#| if sign {
#| -x
#| } else {
#| x
#| }
#|}
),
"hyperbolic_double_js.mbt": (
#|pub fn sinh(x : Double) -> Double = "Math" "sinh"
#|pub fn cosh(x : Double) -> Double = "Math" "cosh"
#|pub fn tanh(x : Double) -> Double = "Math" "tanh"
#|pub fn asinh(x : Double) -> Double = "Math" "asinh"
#|pub fn acosh(x : Double) -> Double = "Math" "acosh"
#|pub fn atanh(x : Double) -> Double = "Math" "atanh"
),
"hypot.mbt": (
#|pub fn hypot(x : Double, y : Double) -> Double {
#| if x.is_inf() || y.is_inf() {
#| return @double.infinity
#| }
#| if x.is_nan() || y.is_nan() {
#| return @double.not_a_number
#| }
#| let x = x.abs()
#| let y = y.abs()
#| let (x, y) = if y > x { (y, x) } else { (x, y) }
#| let ex = (x.reinterpret_as_uint64() >> 52).to_int()
#| let ey = (y.reinterpret_as_uint64() >> 52).to_int()
#| if y == 0.0 || ex - ey > 64 {
#| return x
#| }
#| let (x, y, scale) = if ex > 0x3ff + 510 {
#| (x * 0x1.0p-700, y * 0x1.0p-700, 0x1.0p700)
#| } else if ey < 0x3ff - 450 {
#| (x * 0x1.0p700, y * 0x1.0p700, 0x1.0p-700)
#| } else {
#| (x, y, 1.0)
#| }
#| let difference = x - y
#| let square_sum = if difference > y {
#| let high = (x.reinterpret_as_uint64() & 0xffffffff00000000UL).reinterpret_as_double()
#| let low = x - high
#| high * high - (y * -y - low * (x + high))
#| } else {
#| let twice_x = x + x
#| let x_high = (twice_x.reinterpret_as_uint64() & 0xffffffff00000000UL).reinterpret_as_double()
#| let x_low = twice_x - x_high
#| let y_high = (y.reinterpret_as_uint64() & 0xffffffff00000000UL).reinterpret_as_double()
#| let y_low = y - y_high
#| x_high * y_high - (difference * -difference - (x_high * y_low + x_low * y))
#| }
#| scale * square_sum.sqrt()
#|}
#|pub fn hypotf(x : @float.Float, y : @float.Float) -> @float.Float {
#| if x.is_inf() || y.is_inf() {
#| return @float.infinity
#| }
#| let x = x.to_double()
#| let y = y.to_double()
#| @float.Float::from_double((x * x + y * y).sqrt())
#|}
),
"log.mbt": (
#|let logf_off = 0x3f330000U
#|let logf_table_bits = 4
#|let logf_n : UInt32 = 1U << logf_table_bits
#|priv struct LogfData {
#| invc : FixedArray[Double]
#| logc : FixedArray[Double]
#| ln2 : Double
#| poly : FixedArray[Double]
#|}
#|let logf_data : LogfData = {
#| invc: [
#| 0x1.661ec79f8f3bep+0, 0x1.571ed4aaf883dp+0, 0x1.49539f0f010bp+0, 0x1.3c995b0b80385p+0,
#| 0x1.30d190c8864a5p+0, 0x1.25e227b0b8eap+0, 0x1.1bb4a4a1a343fp+0, 0x1.12358f08ae5bap+0,
#| 0x1.0953f419900a7p+0, 0x1.0p+0, 0x1.e608cfd9a47acp-1, 0x1.ca4b31f026aap-1, 0x1.b2036576afce6p-1,
#| 0x1.9c2d163a1aa2dp-1, 0x1.886e6037841edp-1, 0x1.767dcf5534862p-1,
#| ],
#| logc: [
#| -0x1.57bf7808caadep-2, -0x1.2bef0a7c06ddbp-2, -0x1.01eae7f513a67p-2, -0x1.b31d8a68224e9p-3,
#| -0x1.6574f0ac07758p-3, -0x1.1aa2bc79c81p-3, -0x1.a4e76ce8c0e5ep-4, -0x1.1973c5a611cccp-4,
#| -0x1.252f438e10c1ep-5, 0x0.0p+0, 0x1.aa5aa5df25984p-5, 0x1.c5e53aa362eb4p-4,
#| 0x1.526e57720db08p-3, 0x1.bc2860d22477p-3, 0x1.1058bc8a07ee1p-2, 0x1.4043057b6ee09p-2,
#| ],
#| ln2: 0x1.62e42fefa39efp-1,
#| poly: [-0x1.00ea348b88334p-2, 0x1.5575b0be00b6ap-2, -0x1.ffffef20a4123p-2],
#|}
#|pub fn lnf(x : @float.Float) -> @float.Float {
#| let mut ix : UInt32 = x.reinterpret_as_uint()
#| if ix == 0x3f800000U {
#| return 0.0
#| }
#| if ix - 0x00800000U >= 0x7f800000U - 0x00800000U {
#| if ix * 2 == 0 {
#| return @float.neg_infinity
#| }
#| if ix == 0x7f800000U {
#| return x
#| }
#| if (ix & 0x80000000U) != 0 || ix * 2 >= 0xff000000U {
#| return @float.not_a_number
#| }
#| ix = (x * 0x1.0p23).reinterpret_as_uint()
#| ix -= (23 << 23).reinterpret_as_uint()
#| }
#| let tmp = ix - logf_off
#| let i = ((tmp >> (23 - logf_table_bits)) % logf_n).reinterpret_as_int()
#| let k = tmp.reinterpret_as_int() >> 23
#| let iz = ix - (tmp & 0xff800000U)
#| let invc = logf_data.invc[i]
#| let logc = logf_data.logc[i]
#| let z = @float.Float::reinterpret_from_uint(iz).to_double()
#| let r = z * invc - 1
#| let y0 = logc + k.to_double() * logf_data.ln2
#| let r2 = r * r
#| let y = logf_data.poly[1] * r + logf_data.poly[2]
#| let y = logf_data.poly[0] * r2 + y
#| let y = y * r2 + (y0 + r)
#| @float.Float::from_double(y)
#|}
#|pub fn ln_1pf(x : @float.Float) -> @float.Float {
#| let lg1_f : @float.Float = 0.66666662693
#| let lg2_f : @float.Float = 0.40000972152
#| let lg3_f : @float.Float = 0.28498786688
#| let lg4_f : @float.Float = 0.24279078841
#| let float_ln2_hi : @float.Float = 6.9314575195e-01 // 0x3f317200
#| let float_ln2_lo : @float.Float = 1.4286067653e-06 // 0x35bfbe8e
#| let mut ui : UInt32 = x.reinterpret_as_uint()
#| let mut f : @float.Float = 0
#| let mut c : @float.Float = 0
#| let mut iu : UInt32 = 0
#| let one : @float.Float = 1.0
#| let mut k = 1
#| if ui < 0x3ed413d0 || ui >> 31 > 0 {
#| if ui >= 0xbf800000 {
#| if x == -1.0 {
#| return x / 0.0
#| }
#| return (x - x) / 0.0
#| }
#| if ui << 1 < 0x33800000U << 1 {
#| return x
#| }
#| if ui <= 0xbe95f619 {
#| k = 0
#| c = 0.0
#| f = x
#| }
#| } else if ui >= 0x7f800000 {
#| return x
#| }
#| if k > 0 {
#| ui = (one + x).reinterpret_as_uint()
#| iu = ui
#| iu += 0x3f800000U - 0x3f3504f3U
#| k = (iu >> 23).reinterpret_as_int() - 0x7f
#| if k < 25 {
#| let fui = @float.Float::reinterpret_from_uint(ui)
#| c = if k >= 2 { one - (fui - x) } else { x - (fui - 1.0) }
#| c /= @float.Float::reinterpret_from_uint(ui)
#| } else {
#| c = 0.0
#| }
#| iu = (iu & 0x007fffff) + 0x3f3504f3
#| ui = iu
#| f = @float.Float::reinterpret_from_uint(ui) - 1.0
#| }
#| let s = f / (f + 2.0)
#| let z = s * s
#| let w = z * z
#| let t1 = w * (lg2_f + w * lg4_f)
#| let t2 = z * (lg1_f + w * lg3_f)
#| let r = t2 + t1
#| let hfsq = f * f * 0.5
#| let dk = @float.Float::from_int(k)
#| s * (hfsq + r) + (dk * float_ln2_lo + c) - hfsq + f + dk * float_ln2_hi
#|}
),
"log_double_js.mbt": (
#|pub fn ln(x : Double) -> Double = "Math" "log"
#|pub fn log2(x : Double) -> Double = "Math" "log2"
#|pub fn log10(x : Double) -> Double = "Math" "log10"
#|pub fn ln_1p(x : Double) -> Double = "Math" "log1p"
),
"pow.mbt": (
#|pub fn powf(base : @float.Float, exponent : @float.Float) -> @float.Float {
#| let huge : @float.Float = 1.0e30
#| let tiny : @float.Float = 1.0e-30
#| let cp : @float.Float = 9.6179670095e-01 // 0x3f76384f =2/(3ln2) */
#| let cp_h : @float.Float = 9.6191406250e-01 // 0x3f764000 =12b cp */
#| let cp_l : @float.Float = -1.1736857402e-04 // 0xb8f623c6 =tail of cp_h */
#| let lg2 : @float.Float = 6.9314718246e-01 // 0x3f317218 */
#| let lg2_h : @float.Float = 6.93145752e-01 // 0x3f317200 */
#| let lg2_l : @float.Float = 1.42860654e-06 // 0x35bfbe8c */
#| let ovt : @float.Float = 4.2995665694e-08 // -(128-log2(ovfl+.5ulp)) */
#| let ivln2 : @float.Float = 1.4426950216e+00 // 0x3f317218 */
#| let ivln2_h : @float.Float = 1.4426879883e+00 // 0x3f317218 */
#| let ivln2_l : @float.Float = 7.0526075433e-06 // 0x35bfbe8c */
#| let l1 : @float.Float = 6.0000002384e-01 // 0x3f19999a */
#| let l2 : @float.Float = 4.2857143283e-01 // 0x3edb6db7 */
#| let l3 : @float.Float = 3.3333334327e-01 // 0x3eaaaaab */
#| let l4 : @float.Float = 2.7272811532e-01 // 0x3e8ba305 */
#| let l5 : @float.Float = 2.3066075146e-01 // 0x3e6c3255 */
#| let l6 : @float.Float = 2.0697501302e-01 // 0x3e53f142 */
#| let p1 : @float.Float = 1.6666667163e-01 // 0x3e2aaaab */
#| let p2 : @float.Float = -2.7777778450e-03 // 0xbb360b61 */
#| let p3 : @float.Float = 6.6137559770e-05 // 0x388ab355 */
#| let p4 : @float.Float = -1.6533901999e-06 // 0xb5ddea0e */
#| let p5 : @float.Float = 4.1381369442e-08 // 0x3331bb4c */
#| let mut z : @float.Float = 0
#| let mut ax : @float.Float = 0
#| let mut z_h : @float.Float = 0
#| let mut z_l : @float.Float = 0
#| let mut p_h : @float.Float = 0
#| let mut p_l : @float.Float = 0
#| let mut y1 : @float.Float = 0
#| let mut t1 : @float.Float = 0
#| let mut t2 : @float.Float = 0
#| let mut r : @float.Float = 0
#| let mut s : @float.Float = 0
#| let mut sn : @float.Float = 0
#| let mut t : @float.Float = 0
#| let mut u : @float.Float = 0
#| let mut v : @float.Float = 0
#| let mut w : @float.Float = 0
#| let mut i : Int32 = 0
#| let mut j : Int32 = 0
#| let mut k : Int32 = 0
#| let mut yisint : Int32 = 0
#| let mut n : Int32 = 0
#| let mut hx : Int32 = 0
#| let mut hy : Int32 = 0
#| let mut ix : Int32 = 0
#| let mut iy : Int32 = 0
#| let mut i_s : Int32 = 0
#| let (x, y) = (base, exponent)
#| let bp : ReadOnlyArray[@float.Float] = [1.0, 1.5]
#| let dp_h : ReadOnlyArray[@float.Float] = [0.0, 5.84960938e-01] // 0x3f15c000 */
#| let dp_l : ReadOnlyArray[@float.Float] = [0.0, 1.56322085e-06] // 0x35d1cfdc */
#| let two24 : @float.Float = 16777216.0
#| hx = x.reinterpret_as_int()
#| hy = y.reinterpret_as_int()
#| ix = hx & 0x7fffffff
#| iy = hy & 0x7fffffff
#| if iy == 0 {
#| return 1.0
#| }
#| if hx == 0x3f800000 {
#| return 1.0
#| }
#| if ix > 0x7f800000 || iy > 0x7f800000 {
#| return x + y
#| }
#| yisint = 0
#| if hx < 0 {
#| if iy >= 0x4b800000 {
#| yisint = 2 // even integer y
#| } else if iy >= 0x3f800000 {
#| k = (iy >> 23) - 0x7f // exponent
#| j = iy >> (23 - k)
#| if j << (23 - k) == iy {
#| yisint = 2 - (j & 1)
#| }
#| }
#| }
#| if iy == 0x7f800000 {
#| if ix == 0x3f800000 {
#| return 1.0
#| } else if ix > 0x3f800000 {
#| return if hy >= 0 { y } else { 0.0 }
#| } else {
#| return if hy >= 0 { 0.0 } else { -y }
#| }
#| }
#| if iy == 0x3f800000 {
#| return if hy >= 0 { x } else { @float.Float(1.0) / x }
#| }
#| if hy == 0x40000000 {
#| return x * x
#| }
#| if hy == 0x3f000000 && hx >= 0 {
#| return x.sqrt()
#| }
#| ax = x.abs()
#| if ix == 0x7f800000 || ix == 0 || ix == 0x3f800000 {
#| z = ax
#| if hy < 0 {
#| z = @float.Float(1.0) / z
#| }
#| if hx < 0 {
#| if ((ix - 0x3f800000) | yisint) == 0 {
#| z = (z - z) / (z - z) // (-1)**non-int is NaN
#| } else if yisint == 1 {
#| z = -z
#| }
#| } // (x<0)**odd = -(|x|**odd)
#| return z
#| }
#| sn = 1.0 // sign of result
#| if hx < 0 {
#| if yisint == 0 {
#| return @float.not_a_number
#| }
#| if yisint == 1 {
#| sn = -1.0
#| }
#| }
#| if iy > 0x4d000000 {
#| if ix < 0x3f7ffff6 {
#| return if hy < 0 { sn * huge * huge } else { sn * tiny * tiny }
#| }
#| if ix > 0x3f800007 {
#| return if hy > 0 { sn * huge * huge } else { sn * tiny * tiny }
#| }
#| t = ax - 1.0 // t has 20 trailing zeros
#| w = t *
#| t *
#| (@float.Float(0.5) - t * (@float.Float(0.333333333333) - t * 0.25))
#| u = ivln2_h * t // IVLN2_H has 16 sig. bits
#| v = t * ivln2_l - w * ivln2
#| t1 = u + v
#| i_s = t1.reinterpret_as_int()
#| t1 = @float.Float::reinterpret_from_int(i_s & 0xfffff000)
#| t2 = v - (t1 - u)
#| } else {
#| let mut s2 : @float.Float = 0
#| let mut s_h : @float.Float = 0
#| let mut s_l : @float.Float = 0
#| let mut t_h : @float.Float = 0
#| let mut t_l : @float.Float = 0
#| n = 0
#| if ix < 0x00800000 {
#| ax *= two24
#| n -= 24
#| ix = ax.reinterpret_as_int()
#| }
#| n += (ix >> 23) - 0x7f
#| j = ix & 0x007fffff
#| ix = j | 0x3f800000 // normalize ix
#| if j <= 0x1cc471 {
#| k = 0
#| } else if j < 0x5db3d7 {
#| k = 1
#| } else {
#| k = 0
#| n += 1
#| ix -= 0x00800000
#| }
#| ax = @float.Float::reinterpret_from_int(ix)
#| u = ax - bp[k]
#| v = @float.Float(1.0) / (ax + bp[k])
#| s = u * v
#| s_h = s
#| i_s = s_h.reinterpret_as_int()
#| s_h = @float.Float::reinterpret_from_int(i_s & 0xfffff000)
#| i_s = (((ix.reinterpret_as_uint() >> 1) & 0xfffff000) | 0x20000000).reinterpret_as_int()
#| t_h = @float.Float::reinterpret_from_uint(
#| i_s.reinterpret_as_uint() + 0x00400000 + (k.reinterpret_as_uint() << 21),
#| )
#| t_l = ax - (t_h - bp[k])
#| s_l = v * (u - s_h * t_h - s_h * t_l)
#| s2 = s * s
#| r = s2 * s2 * (l1 + s2 * (l2 + s2 * (l3 + s2 * (l4 + s2 * (l5 + s2 * l6)))))
#| r += s_l * (s_h + s)
#| s2 = s_h * s_h
#| t_h = @float.Float(3.0) + s2 + r
#| i_s = t_h.reinterpret_as_int()
#| t_h = @float.Float::reinterpret_from_int(i_s & 0xfffff000)
#| t_l = r - (t_h - 3.0 - s2)
#| u = s_h * t_h
#| v = s_l * t_h + t_l * s
#| p_h = u + v
#| i_s = p_h.reinterpret_as_int()
#| p_h = @float.Float::reinterpret_from_int(i_s & 0xfffff000)
#| p_l = v - (p_h - u)
#| z_h = cp_h * p_h // cp_h+cp_l = 2/(3*log2)
#| z_l = cp_l * p_h + p_l * cp + dp_l[k]
#| t = @float.Float::from_int(n)
#| t1 = z_h + z_l + dp_h[k] + t
#| i_s = t1.reinterpret_as_int()
#| t1 = @float.Float::reinterpret_from_int(i_s & 0xfffff000)
#| t2 = z_l - (t1 - t - dp_h[k] - z_h)
#| }
#| i_s = y.reinterpret_as_int()
#| y1 = @float.Float::reinterpret_from_int(i_s & 0xfffff000)
#| p_l = (y - y1) * t1 + y * t2
#| p_h = y1 * t1
#| z = p_l + p_h
#| j = z.reinterpret_as_int()
#| if j > 0x43000000 {
#| return sn * huge * huge // overflow
#| } else if j == 0x43000000 {
#| if p_l + ovt > z - p_h {
#| return sn * huge * huge
#| }
#| } else if ( // overflow
#| j & 0x7fffffff
#| ) >
#| 0x43160000 {
#| return sn * tiny * tiny // underflow
#| } else if j.reinterpret_as_uint() == 0xc3160000 && p_l <= z - p_h {
#| return sn * tiny * tiny
#| } // underflow
#| i = j & 0x7fffffff
#| k = (i >> 23) - 0x7f
#| n = 0
#| if i > 0x3f000000 {
#| n = j + (0x00800000 >> (k + 1))
#| k = ((n & 0x7fffffff) >> 23) - 0x7f // new k for n
#| t = @float.Float::reinterpret_from_int(n & (0x007fffff >> k).lnot())
#| n = ((n & 0x007fffff) | 0x00800000) >> (23 - k)
#| if j < 0 {
#| n = -n
#| }
#| p_h -= t
#| }
#| t = p_l + p_h
#| i_s = t.reinterpret_as_int()
#| t = @float.Float::reinterpret_from_int(i_s & 0xffff8000)
#| u = t * lg2_h
#| v = (p_l - (t - p_h)) * lg2 + t * lg2_l
#| z = u + v
#| w = v - (z - u)
#| t = z * z
#| t1 = z - t * (p1 + t * (p2 + t * (p3 + t * (p4 + t * p5))))
#| r = z * t1 / (t1 - 2.0) - (w + z * w)
#| z = @float.Float(1.0) - (r - z)
#| j = z.reinterpret_as_int()
#| j += n << 23
#| if j >> 23 <= 0 {
#| z = scalbnf(z, n)
#| } else {
#| z = @float.Float::reinterpret_from_int(j)
#| }
#| sn * z
#|}
),
"pow_double_js.mbt": (
#|pub fn pow(base : Double, exponent : Double) -> Double {
#| if base == 1.0 {
#| return 1.0
#| }
#| if base == -1.0 &&
#| (exponent == @double.infinity || exponent == @double.neg_infinity) {
#| return 1.0
#| }
#| math_pow_js(base, exponent)
#|}
#|fn math_pow_js(base : Double, exponent : Double) -> Double = "Math" "pow"
),
"prime.mbt": (
#|const DEFAULT_PRIME_TEST_ITERS = 64
#|let small_primes : ReadOnlyArray[@bigint.BigInt] = [
#| 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
#| 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
#| 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
#| 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
#| 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
#| 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557,
#| 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647,
#| 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757,
#| 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
#| 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
#| 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069,
#| 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181,
#| 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283,
#| 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
#| 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487,
#| 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583,
#| 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
#| 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
#| 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907,
#| 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017,
#| 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129,
#| 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
#| 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347,
#| 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441,
#| 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579,
#| 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687,
#| 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777,
#| 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887,
#| 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011,
#| 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137,
#| 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257,
#| 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361,
#| 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491,
#| 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583,
#| 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697,
#| 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821,
#| 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923,
#| 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049,
#| 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157,
#| 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271,
#| 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409,
#| 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519,
#| 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649,
#| 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783,
#| 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909,
#| 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003,
#| 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113,
#| 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261,
#| 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399,
#| 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501,
#| 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639,
#| 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737,
#| 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849,
#| 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981,
#| 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101,
#| 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221,
#| 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329,
#| 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451,
#| 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581,
#| 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709,
#| 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833,
#| 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961,
#| 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069,
#| 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213,
#| 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349,
#| 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499,
#| 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589,
#| 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703,
#| 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853,
#| 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963,
#| 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111,
#| 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237,
#| 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369,
#| 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521,
#| 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641,
#| 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741,
#| 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861,
#| 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001,
#| 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133,
#| 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241,
#| 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377,
#| 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473,
#| 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623,
#| 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743,
#| 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857,
#| 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007,
#| 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111,
#| 10133, 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223,
#| 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321,
#| 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, 10453,
#| 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, 10559, 10567,
#| 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, 10657, 10663, 10667,
#| 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10771, 10781, 10789,
#| 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10891, 10903,
#| 10909, 10937, 10939, 10949, 10957, 10973, 10979, 10987, 10993, 11003, 11027, 11047,
#| 11057, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149,
#| 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261,
#| 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383,
#| 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491,
#| 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621,
#| 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, 11777,
#| 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, 11839, 11863,
#| 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, 11941, 11953, 11959,
#| 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073,
#| 12097, 12101, 12107, 12109, 12113, 12119, 12143, 12149, 12157, 12161, 12163, 12197,
#| 12203, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289,
#| 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413,
#| 12421, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511,
#| 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611,
#| 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713,
#| 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829,
#| 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953,
#| 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043,
#| 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13163,
#| 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, 13249, 13259, 13267, 13291,
#| 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411,
#| 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523,
#| 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669,
#| 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751,
#| 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873,
#| 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967,
#| 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087,
#| 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249,
#| 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, 14387, 14389,
#| 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489,
#| 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593,
#| 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14699, 14713, 14717,
#| 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797,
#| 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897,
#| 14923, 14929, 14939, 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053,
#| 15061, 15073, 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149,
#| 15161, 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269,
#| 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359,
#| 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443, 15451, 15461,
#| 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, 15581, 15583,
#| 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, 15667, 15671, 15679,
#| 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, 15767, 15773, 15787, 15791,
#| 15797, 15803, 15809, 15817, 15823, 15859, 15877, 15881, 15887, 15889, 15901, 15907,
#| 15913, 15919, 15923, 15937, 15959, 15971, 15973, 15991, 16001, 16007, 16033, 16057,
#| 16061, 16063, 16067, 16069, 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139,
#| 16141, 16183, 16187, 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267,
#| 16273, 16301, 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417,
#| 16421, 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529,
#| 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651,
#| 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, 16763,
#| 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, 16901, 16903,
#| 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, 16993, 17011, 17021,
#| 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, 17099, 17107, 17117, 17123,
#| 17137, 17159, 17167, 17183, 17189, 17191, 17203, 17207, 17209, 17231, 17239, 17257,
#| 17291, 17293, 17299, 17317, 17321, 17327, 17333, 17341, 17351, 17359, 17377, 17383,
#| 17387, 17389, 17393, 17401, 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477,
#| 17483, 17489, 17491, 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581,
#| 17597, 17599, 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713,
#| 17729, 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839,
#| 17851, 17863,
#|]
#|pub fn is_probable_prime(
#| number : @bigint.BigInt,
#| rand : @random.Rand,
#| iters~ : Int32 = DEFAULT_PRIME_TEST_ITERS,
#|) -> Bool {
#| if iters <= 0 {
#| abort("non-positive iters for probably prime")
#| }
#| if number <= 1N {
#| return false
#| }
#| if number <= 3N {
#| return true
#| }
#| if number % 2N == 0 {
#| return false
#| }
#| trial_divisions(number) && miller_rabin_test(number, iters, rand)
#|}
#|pub fn probable_prime(bits : Int32, rand : @random.Rand) -> @bigint.BigInt {
#| guard bits >= 2 else { abort("probable_prime: bits must be at least two") }
#| let high_bit = 1N << (bits - 1)
#| for ;; {
#| let b = rand.bigint(bits) | high_bit
#| if is_probable_prime(b, rand) {
#| break b
#| }
#| }
#|}
#|fn trial_divisions(n : @bigint.BigInt) -> Bool {
#| let td = match n.bit_length() {
#| _..<=512 => 64
#| 512..<=1024 => 128
#| 1024..<=2048 => 384
#| 2048..<=4096 => 1024
#| _ => 2048
#| }
#| for i in 1..| Bool {
#| if w <= 3N {
#| abort("candidate of miller rabin test must larger than 3")
#| }
#| let w1 = w - 1N
#| let w3 = w - 3N
#| let a = w1.ctz()
#| let m = w1 >> a
#| let w3_len = w3.bit_length()
#| next~: for _ in 0.. Double {
#| let mut n = exp
#| let mut y : Double = x
#| if n > 1023 {
#| y *= 0x1.0p1023
#| n -= 1023
#| if n > 1023 {
#| y *= 0x1.0p1023
#| n -= 1023
#| if n > 1023 {
#| n = 1023
#| }
#| }
#| } else if n < -1022 {
#| y *= 0x1.0p-1022 * 0x1.0p53
#| n += 1022 - 53
#| if n < -1022 {
#| y *= 0x1.0p-1022 * 0x1.0p53
#| n += 1022 - 53
#| if n < -1022 {
#| n = -1022
#| }
#| }
#| }
#| let ui = (0x3ff + n).to_uint64() << 52
#| return y * ui.reinterpret_as_double()
#|}
#|pub fn scalbnf(y : @float.Float, exp : Int32) -> @float.Float {
#| let mut y = y
#| let mut n = exp
#| if n > 127 {
#| y *= @float.Float(0x1.0p127)
#| n -= 127
#| if n > 127 {
#| y *= @float.Float(0x1.0p127)
#| n -= 127
#| if n > 127 {
#| n = 127
#| }
#| }
#| } else if n < -126 {
#| y *= @float.Float(0x1.0p-126) * 0x1.0p24
#| n += 126 - 24
#| if n < -126 {
#| y *= @float.Float(0x1.0p-126) * 0x1.0p24
#| n += 126 - 24
#| if n < -126 {
#| n = -126
#| }
#| }
#| }
#| let u = (0x7f + n) << 23
#| return y * @float.Float::reinterpret_from_int(u)
#|}
#|test "scalbn" {
#| inspect(scalbn(1.5, 2), content="6")
#| inspect(scalbn(2.0, -1), content="1")
#| inspect(scalbn(3.0, 0), content="3")
#| inspect(scalbn(1.0, 1024), content="Infinity")
#| inspect(scalbn(1.0, -1023), content="1.1125369292536007e-308")
#| inspect(scalbn(1.0, 2047), content="Infinity")
#| inspect(scalbn(1.0, -1992), content="0")
#| inspect(scalbn(1.0, 3070), content="Infinity")
#| inspect(scalbn(1.0, -2961), content="0")
#| inspect(scalbn(@double.infinity, 10), content="Infinity")
#| inspect(scalbn(@double.not_a_number, 10), content="NaN")
#| inspect(scalbn(0.0, 10), content="0")
#|}
),
"trig.mbt": (
#|pub const PI = 0x3.243F6A8885A308CA8A54
#|const SIN_SWITCHOVER : @float.Float = 201.15625
#|const COS_SWITCHOVER : @float.Float = 142.90625
#|fn mulh(a : UInt32, b : UInt32) -> UInt32 {
#| let a = a.to_uint64()
#| let b = b.to_uint64()
#| let res = a * b
#| (res >> 32).to_uint()
#|}
#|fn mul(a : UInt32, b : UInt32) -> (UInt32, UInt32) {
#| let a = a.to_uint64()
#| let b = b.to_uint64()
#| let res = a * b
#| ((res >> 32).to_uint(), res.to_uint())
#|}
#|fn trig_reduce(
#| x : @float.Float,
#| switch_over : @float.Float,
#|) -> (@float.Float, Int32) {
#| if x.abs() <= switch_over {
#| let mut j : @float.Float = 0.0
#| let mut r : @float.Float = 0.0
#| j = x * @float.Float::reinterpret_from_int(0x3f22f983) +
#| @float.Float::reinterpret_from_int(0x4b40_0000)
#| j = @float.Float::from_int(j.reinterpret_as_int() - 0x4b40_0000)
#| r = x - j * @float.Float::reinterpret_from_int(0x3fc90f80)
#| r -= j * @float.Float::reinterpret_from_int(0x37354440)
#| r -= j * @float.Float::reinterpret_from_int(0x2c34611a)
#| return (r, j.to_int())
#| }
#| let xispos = x > 0.0
#| let mut exp : Int32 = ((x.reinterpret_as_int() >> 23) & 0xff) - 126
#| let ix = ((x.reinterpret_as_uint() & 0x007fffff) << 8) | 0x80000000
#| let ind = exp >> 5
#| exp = exp & 0x1f
#| let two_over_pi : ReadOnlyArray[UInt32] = [
#| 0x00000000, 0x28be60db, 0x9391054a, 0x7f09d5f4, 0x7d4d3770, 0x36d8a566, 0x4f10e410,
#| 0000000000,
#| ]
#| let mut hi = two_over_pi[ind]
#| let mut mi = two_over_pi[ind + 1]
#| let mut lo = two_over_pi[ind + 2]
#| let tp = two_over_pi[ind + 3]
#| if exp > 0 {
#| hi = (hi << exp) | (mi >> (32 - exp))
#| mi = (mi << exp) | (lo >> (32 - exp))
#| lo = (lo << exp) | (tp >> (32 - exp))
#| }
#| let phi = 0U
#| let (h, l) = mul(ix, lo)
#| let plo = phi + l
#| let phi = h + (if plo < l { 1 } else { 0 })
#| let (h, l) = mul(ix, mi)
#| let mut plo = phi + l
#| let phi = h + (if plo < l { 1 } else { 0 })
#| let l = ix * hi
#| let mut phi = phi + l
#| let mut q : Int32 = (phi >> 30).reinterpret_as_int()
#| phi = phi & 0x3fffffff
#| if (phi & 0x2000_0000) != 0 {
#| phi -= 0x4000_0000
#| q += 1
#| }
#| let s : UInt32 = phi & 0x8000_0000
#| if phi >= 0x8000_0000 {
#| phi = phi.lnot()
#| plo = 0U - plo
#| phi += if plo == 0 { 1 } else { 0 }
#| }
#| exp = 0
#| while phi < 0x8000_0000 {
#| phi = (phi << 1) | (plo >> 31)
#| plo = plo << 1
#| exp -= 1
#| }
#| phi = mulh(phi, 0xc90f_daa2)
#| if phi < 0x8000_0000 {
#| phi = phi << 1
#| exp -= 1
#| }
#| let mut r = s +
#| ((exp + 128) << 23).reinterpret_as_uint() +
#| (phi >> 8) +
#| (if (phi & 0xff) > 0x7e { 1 } else { 0 })
#| if !xispos {
#| r = r ^ 0x8000_0000
#| q = -q
#| }
#| let r = @float.Float::reinterpret_from_uint(r)
#| return (r, q)
#|}
#|fn sinf_poly(x : @float.Float) -> @float.Float {
#| let s = x * x
#| let mut r = @float.Float::reinterpret_from_int(0x3640_5000)
#| r = r * s - @float.Float::reinterpret_from_int(0x3950_3486)
#| r = r * s + @float.Float::reinterpret_from_int(0x3c08_88c1)
#| r = r * s - @float.Float::reinterpret_from_int(0x3e2a_aaab)
#| let t = x * s
#| r = r * t + x
#| r
#|}
#|fn cosf_poly(x : @float.Float) -> @float.Float {
#| let s = x * x
#| let mut r = @float.Float::reinterpret_from_int(0x37cd_4000)
#| r = r * s - @float.Float::reinterpret_from_int(0x3ab6_077d)
#| r = r * s + @float.Float::reinterpret_from_int(0x3d2a_aaa8)
#| r = r * s - @float.Float::reinterpret_from_int(0x3f00_0000)
#| r = r * s + @float.Float::reinterpret_from_int(0x3f80_0000)
#| r
#|}
#|fn sin_cos_core(x : @float.Float, q : Int32) -> @float.Float {
#| let mut r = if (q & 1) != 0 { cosf_poly(x) } else { sinf_poly(x) }
#| if (q & 2) != 0 {
#| r = -r
#| }
#| r
#|}
#|fn tanf_poly(x : @float.Float, odd : Bool) -> @float.Float {
#| let x = x.to_double()
#| let coef : ReadOnlyArray[Double] = [
#| 0.333331395030791399758, // 0x15554d3418c99f.0p-54 */
#| 0.133392002712976742718, // 0x1112fd38999f72.0p-55 */
#| 0.0533812378445670393523, // 0x1b54c91d865afe.0p-57 */
#| 0.0245283181166547278873, // 0x191df3908c33ce.0p-58 */
#| 0.00297435743359967304927, // 0x185dadfcecf44e.0p-61 */
#| 0.00946564784943673166728, // 0x1362b9bf971bcd.0p-59 */
#| ]
#| let z = x * x
#| let mut r = coef[4] + z * coef[5]
#| let t = coef[2] + z * coef[3]
#| let w = z * z
#| let s = z * x
#| let u = coef[0] + z * coef[1]
#| r = x + s * u + s * w * (t + w * r)
#| @float.Float::from_double(if odd { -1.0 / r } else { r })
#|}
#|pub fn sinf(x : @float.Float) -> @float.Float {
#| if !x.is_finite() {
#| return @float.not_a_number
#| }
#| if x == 0.0 {
#| return x
#| }
#| let (x, q) = trig_reduce(x, SIN_SWITCHOVER)
#| sin_cos_core(x, q)
#|}
#|pub fn cosf(x : @float.Float) -> @float.Float {
#| if !x.is_finite() {
#| return @float.not_a_number
#| }
#| if x == 0.0 {
#| return 1.0
#| }
#| let (x, q) = trig_reduce(x, COS_SWITCHOVER)
#| sin_cos_core(x, q + 1)
#|}
#|pub fn tanf(x : @float.Float) -> @float.Float {
#| if !x.is_finite() {
#| return @float.not_a_number
#| }
#| if x == 0.0 {
#| return x
#| }
#| let (x, q) = trig_reduce(x, COS_SWITCHOVER)
#| tanf_poly(x, (q & 1) != 0)
#|}
#|pub fn asinf(x : @float.Float) -> @float.Float {
#| let x1p120 = 0x3870000000000000UL.reinterpret_as_double()
#| let pio2 : Double = 1.570796326794896558e+00
#| let ps0 : @float.Float = 1.6666586697e-01
#| let ps1 : @float.Float = -4.2743422091e-02
#| let ps2 : @float.Float = -8.6563630030e-03
#| let qs2 : @float.Float = -7.0662963390e-01
#| fn r(z : @float.Float) -> @float.Float {
#| let p = z * (ps0 + z * (ps1 + z * ps2))
#| let q = z * qs2 + 1.0
#| p / q
#| }
#| let hx = x.reinterpret_as_uint()
#| let ix = hx & 0x7fffffff
#| if ix >= 0x3f800000 {
#| if ix == 0x3f800000 {
#| return @float.Float::from_double(x.to_double() * pio2 + x1p120)
#| }
#| return @float.not_a_number // asin(|x|>1) is NaN
#| }
#| if ix < 0x3f000000 {
#| if ix is (0x00800000..<=0x39800000) {
#| return x
#| }
#| return x + x * r(x * x)
#| }
#| let z = (@float.Float(1.0) - x.abs()) * 0.5
#| let s = z.to_double().sqrt()
#| let x = @float.Float::from_double(pio2 - 2.0 * (s + s * r(z).to_double()))
#| if hx >> 31 != 0 {
#| -x
#| } else {
#| x
#| }
#|}
#|pub fn acosf(x : @float.Float) -> @float.Float {
#| let pio2_hi : @float.Float = 1.5707962513
#| let pio2_lo : @float.Float = 7.5497894159e-08
#| let ps0 : @float.Float = 1.6666586697e-01
#| let ps1 : @float.Float = -4.2743422091e-02
#| let ps2 : @float.Float = -8.6563630030e-03
#| let qs1 : @float.Float = -7.0662963390e-01
#| let one : @float.Float = 1.0
#| let two : @float.Float = 2.0
#| fn r(z : @float.Float) -> @float.Float {
#| let p = z * (ps0 + z * (ps1 + z * ps2))
#| let q = z * qs1 + 1.0
#| p / q
#| }
#| let hx = x.reinterpret_as_int()
#| let ix = hx & 0x7fffffff
#| if ix >= 0x3f800000 {
#| if ix == 0x3f800000 {
#| if hx >> 31 != 0 {
#| return two * pio2_hi + 0x1.0p-120
#| }
#| return 0.0
#| }
#| return @float.not_a_number
#| }
#| if ix < 0x3f000000 {
#| if ix <= 0x32800000 {
#| return pio2_hi + 0x1.0p-120
#| }
#| return pio2_hi - (x - (pio2_lo - x * r(x * x)))
#| }
#| if hx >> 31 != 0 {
#| let z = (x + 1.0) * 0.5
#| let s = z.sqrt()
#| let w = r(z) * s - pio2_lo
#| return two * (pio2_hi - (s + w))
#| }
#| let z = (one - x) * 0.5
#| let s = z.sqrt()
#| let df = @float.Float::reinterpret_from_uint(
#| s.reinterpret_as_uint() & 0xfffff000,
#| )
#| let c = (z - df * df) / (s + df)
#| let w = r(z) * s + c
#| two * (df + w)
#|}
#|pub fn atanf(x : @float.Float) -> @float.Float {
#| let atanhi : ReadOnlyArray[@float.Float] = [
#| 4.6364760399e-01, 7.8539812565e-01, 9.8279368877e-01, 1.5707962513e+00,
#| ]
#| let atanlo : ReadOnlyArray[@float.Float] = [
#| 5.0121582440e-09, 3.7748947079e-08, 3.4473217170e-08, 7.5497894159e-08,
#| ]
#| let a_t : ReadOnlyArray[@float.Float] = [
#| 3.3333328366e-01, -1.9999158382e-01, 1.4253635705e-01, -1.0648017377e-01, 6.1687607318e-02,
#| ]
#| let ix = x.reinterpret_as_int()
#| let sign = ix >> 31
#| let ix = ix & 0x7fffffff
#| let mut id = 0
#| let mut x = x
#| let one : @float.Float = 1.0
#| let two : @float.Float = 2.0
#| if ix >= 0x4c800000 {
#| if x.is_nan() {
#| return x
#| }
#| let z = atanhi[3] + 0x1.0p-120
#| let z = if sign != 0 { -z } else { z }
#| return z
#| }
#| if ix < 0x3ee00000 {
#| if ix < 0x39800000 {
#| return x
#| }
#| id = -1
#| } else {
#| x = x.abs()
#| if ix < 0x3f980000 {
#| if ix < 0x3f300000 {
#| id = 0
#| x = (two * x - one) / (two + x)
#| } else {
#| id = 1
#| x = (x - one) / (x + one)
#| }
#| } else if ix < 0x401c0000 {
#| id = 2
#| x = (x - 1.5) / (one + x * 1.5)
#| } else {
#| id = 3
#| x = -one / x
#| }
#| }
#| let z = x * x
#| let w = z * z
#| let s1 = z * (a_t[0] + w * (a_t[2] + w * a_t[4]))
#| let s2 = w * (a_t[1] + w * a_t[3])
#| if id < 0 {
#| return x - x * (s1 + s2)
#| }
#| let z = atanhi[id] - (x * (s1 + s2) - atanlo[id] - x)
#| if sign != 0 {
#| -z
#| } else {
#| z
#| }
#|}
#|pub fn atan2f(y : @float.Float, x : @float.Float) -> @float.Float {
#| if x.is_nan() || y.is_nan() {
#| return @float.not_a_number
#| }
#| let pi : @float.Float = 3.1415927410e+00
#| let pi_lo : @float.Float = -8.7422776573e-08
#| let zero : @float.Float = 0.0
#| let ix = x.reinterpret_as_uint()
#| let iy = y.reinterpret_as_uint()
#| if ix == 0x3f800000 {
#| return atanf(y)
#| }
#| let m = ((iy >> 31) & 1) | ((ix >> 30) & 2)
#| let ix = ix & 0x7fffffff
#| let iy = iy & 0x7fffffff
#| if iy == 0 {
#| match m {
#| 0 | 1 => return y
#| 2 => return pi
#| _ => return -pi
#| }
#| }
#| if ix == 0 {
#| let res = if (m & 1) != 0 { -pi / 2 } else { pi / 2 }
#| return res
#| }
#| if ix == 0x7f800000 {
#| if iy == 0x7f800000 {
#| match m {
#| 0 => return pi / 4
#| 1 => return -pi / 4
#| 2 => return pi * 3.0 / 4
#| _ => return -pi * 3.0 / 4
#| }
#| } else {
#| match m {
#| 0 => return 0.0
#| 1 => return -0.0
#| 2 => return pi
#| _ => return -pi
#| }
#| }
#| }
#| if ix + (26U << 23) < iy || iy == 0x7f800000 {
#| let res = if (m & 1) != 0 { -pi / 2 } else { pi / 2 }
#| return res
#| }
#| let z = if (m & 2) != 0 && iy + (26U << 23) < ix {
#| zero
#| } else {
#| atanf(y.abs() / x.abs())
#| }
#| match m {
#| 0 => z
#| 1 => -z
#| 2 => pi - (z - pi_lo)
#| _ => z - pi_lo - pi
#| }
#|}
),
"trig_double_js.mbt": (
#|pub fn sin(x : Double) -> Double = "Math" "sin"
#|pub fn cos(x : Double) -> Double = "Math" "cos"
#|pub fn tan(x : Double) -> Double = "Math" "tan"
#|pub fn asin(x : Double) -> Double = "Math" "asin"
#|pub fn acos(x : Double) -> Double = "Math" "acos"
#|pub fn atan(x : Double) -> Double = "Math" "atan"
#|pub fn atan2(y : Double, x : Double) -> Double = "Math" "atan2"
),
},
)
///|
let moonbitlang_core_option_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/option",
deps={},
files={
"deprecated.mbt": (
#|#deprecated("Use `if condition { Some(value()) } else { None }` instead")
#|pub fn[T] when(condition : Bool, value : () -> T) -> T? {
#| if condition {
#| Some(value())
#| } else {
#| None
#| }
#|}
#|#deprecated("Use `if !condition { Some(value()) } else { None }` instead")
#|pub fn[T] unless(condition : Bool, value : () -> T) -> T? {
#| if !condition {
#| Some(value())
#| } else {
#| None
#| }
#|}
#|#deprecated("Use `None` instead")
#|pub fn[T] empty() -> T? {
#| None
#|}
#|#deprecated("Use `Some(value)` instead")
#|pub fn[T] some(value : T) -> T? {
#| Some(value)
#|}
),
"methods.mbt": "",
"option.mbt": "",
},
)
///|
let moonbitlang_core_path_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/path",
deps={},
files={
"extends.mbt": (
#|#deprecated("Use `/` or `join` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Path with Div::{div}
#|pub extend Path with Eq::{equal}
#|pub extend Path with Show::{to_string}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Path with Eq::{not_equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Path with @debug.Debug::{to_repr}
),
"path.mbt": (
#|let is_windows : Bool = @ffi.is_windows()
#|pub using @error {type ResolveError}
#|#derive(Eq, @debug.Debug)
#|pub(all) struct Path(String)
#|pub impl Show for Path with fn output(path, logger) {
#| logger.write_object(path.0)
#|}
#|pub impl Show for Path with fn to_string(path) {
#| path.0
#|}
#|pub fn Path::basename(path : Path) -> StringView {
#| if is_windows {
#| @win32.basename(path.0)
#| } else {
#| @posix.basename(path.0)
#| }
#|}
#|pub fn Path::dirname(path : Path) -> Path {
#| if is_windows {
#| Path(@win32.dirname(path.0))
#| } else {
#| Path(@posix.dirname(path.0))
#| }
#|}
#|pub fn Path::extname(path : Path) -> StringView {
#| if is_windows {
#| @win32.extname(path.0)
#| } else {
#| @posix.extname(path.0)
#| }
#|}
#|pub fn Path::is_absolute(path : Path) -> Bool {
#| if is_windows {
#| @win32.is_absolute(path.0)
#| } else {
#| @posix.is_absolute(path.0)
#| }
#|}
#|pub fn Path::join(lhs : Path, rhs : Path) -> Path {
#| if is_windows {
#| Path(@win32.join(lhs.0, rhs.0))
#| } else {
#| Path(@posix.join(lhs.0, rhs.0))
#| }
#|}
#|pub impl Div for Path with fn div(lhs, rhs) {
#| lhs.join(rhs)
#|}
#|pub fn Path::normalize(path : Path) -> Path {
#| if is_windows {
#| Path(@win32.normalize(path.0))
#| } else {
#| Path(@posix.normalize(path.0))
#| }
#|}
#|pub fn Path::relative(path : Path, base~ : Path) -> Path raise ResolveError {
#| if is_windows {
#| Path(@win32.relative(path.0, base=base.0))
#| } else {
#| Path(@posix.relative(path.0, base=base.0))
#| }
#|}
#|pub fn Path::resolve(path : Path) -> Path raise ResolveError {
#| if is_windows {
#| Path(@win32.resolve(path.0))
#| } else {
#| Path(@posix.resolve(path.0))
#| }
#|}
#|pub let delimiter : Char = if is_windows { ';' } else { ':' }
#|pub let sep : Char = if is_windows { '\\' } else { '/' }
),
},
)
///|
let moonbitlang_core_path_error_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/path/error",
deps={},
files={
"error.mbt": (
#|#derive(@debug.Debug)
#|pub(all) suberror ResolveError {
#| CurrentDirectoryUnavailable
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend ResolveError with @debug.Debug::{to_repr}
),
},
)
///|
let moonbitlang_core_path_internal_ffi_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/path/internal/ffi",
deps={},
files={
"is_windows.mbt": (
#|#cfg(target="js")
#|pub extern "js" fn is_windows() -> Bool =
#| #|function () {
#| #| return typeof process !== "undefined" && process.platform === "win32";
#| #|};
#|#cfg(any(target="wasm", target="wasm-gc"))
#|#unsafe_skip_stub_check
#|fn get_platform() -> Int32 = "moonbitlang/async" "runtime/get_platform"
#|#cfg(any(target="wasm", target="wasm-gc"))
#|pub fn is_windows() -> Bool {
#| get_platform() == 2
#|}
#|#cfg(all(target="native", platform="windows"))
#|pub fn is_windows() -> Bool {
#| true
#|}
#|#cfg(all(target="native", not(platform="windows")))
#|pub fn is_windows() -> Bool {
#| false
#|}
),
},
)
///|
let moonbitlang_core_path_internal_posix_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/path/internal/posix",
deps={},
files={
"path.mbt": (
#|pub using @error {type ResolveError}
#|pub fn basename(path : String) -> StringView {
#| let path = path[:].trim_end(chars="/")
#| match path.rev_split_once("/") {
#| None => path
#| Some((_, name)) => name
#| }
#|}
#|pub fn dirname(path : String) -> String {
#| let mut path = path[:]
#| guard !path.is_empty() else { "." }
#| while path is [.. rest, last_char] && last_char == '/' {
#| path = rest
#| }
#| match path.rev_find("/") {
#| None => if path.is_empty() { "/" } else { "." }
#| Some(0) => "/"
#| Some(1) => if path is ['/', '/', ..] { "//" } else { path[:1].to_owned() }
#| Some(idx) => path[:idx].to_owned()
#| }
#|}
#|pub fn extname(path : String) -> StringView {
#| let basename = basename(path)
#| if basename == "." || basename == ".." {
#| return ""
#| }
#| match basename.rev_find(".") {
#| None | Some(0) => ""
#| Some(idx) => basename[idx:]
#| }
#|}
#|pub fn is_absolute(path : String) -> Bool {
#| path is ['/', ..]
#|}
#|pub fn join(lhs : String, rhs : String) -> String {
#| guard !lhs.is_empty() || !rhs.is_empty() else { "." }
#| guard !lhs.is_empty() else { normalize(rhs) }
#| guard !rhs.is_empty() else { normalize(lhs) }
#| normalize(lhs + "/" + rhs)
#|}
#|pub fn normalize(path : String) -> String {
#| Show::to_string(UnixPath::parse(path))
#|}
#|fn trim_resolved_trailing_separators(path : String) -> String {
#| let mut raw = path[:]
#| while raw is [.. rest, last_char] && last_char == '/' && !rest.is_empty() {
#| raw = rest
#| }
#| raw.to_owned()
#|}
#|pub fn relative(path : String, base~ : String) -> String raise ResolveError {
#| relative_with_current_dir(path, base~, @env.current_dir)
#|}
#|fn relative_with_current_dir(
#| path : String,
#| base~ : String,
#| current_dir : () -> String?,
#|) -> String raise ResolveError {
#| let from = UnixPath::parse(resolve_with_current_dir(base, current_dir))
#| let to = UnixPath::parse(resolve_with_current_dir(path, current_dir))
#| guard from.prefix == to.prefix else { "" }
#| let mut common = 0
#| let limit = @cmp.minimum(from.components.length(), to.components.length())
#| while common < limit && from.components[common] == to.components[common] {
#| common += 1
#| }
#| let components : Array[StringView] = [
#| ..(Array::make(from.components.length() - common, "..") : Array[StringView]),
#| ..to.components[common:],
#| ]
#| guard !components.is_empty() else { "" }
#| Show::to_string(UnixPath::{
#| prefix: None,
#| trailing_separator: false,
#| components,
#| })
#|}
#|pub fn resolve(path : String) -> String raise ResolveError {
#| resolve_with_current_dir(path, @env.current_dir)
#|}
#|fn resolve_with_current_dir(
#| path : String,
#| current_dir : () -> String?,
#|) -> String raise ResolveError {
#| let resolved = {
#| guard !is_absolute(path) else { normalize(path) }
#| guard current_dir() is Some(cwd) else { raise CurrentDirectoryUnavailable }
#| join(cwd, path) |> normalize
#| }
#| trim_resolved_trailing_separators(resolved)
#|}
#|pub let delimiter : Char = ':'
#|pub let sep : Char = '/'
),
"unix_path.mbt": (
#|#derive(@json.ToJson, Hash, Eq)
#|priv enum UnixPrefix {
#| Root
#| None
#|}
#|#derive(@json.ToJson, Hash, Eq)
#|priv struct UnixPath {
#| prefix : UnixPrefix
#| trailing_separator : Bool
#| components : Array[StringView]
#|}
#|fn is_unix_sep_at(path : StringView, index : Int32) -> Bool {
#| path.get_char(index) == Some('/')
#|}
#|impl Show for UnixPath with fn output(self : UnixPath, logger : &Logger) -> Unit {
#| let { prefix, trailing_separator, components, } = self
#| match prefix {
#| Root => logger.write_char('/')
#| None => ()
#| }
#| match components {
#| [] =>
#| if prefix is None {
#| if trailing_separator {
#| logger <+ "./"
#| } else {
#| logger.write_char('.')
#| }
#| }
#| [x, .. xs] => {
#| logger.write_view(x)
#| for x in xs {
#| logger <+ "/\{x}"
#| }
#| if trailing_separator {
#| logger.write_char('/')
#| }
#| }
#| }
#|}
#|fn push_unix_component(
#| components : Array[StringView],
#| component : StringView,
#| allow_above_root~ : Bool,
#|) -> Unit {
#| match component {
#| "." => ()
#| ".." =>
#| match components.pop() {
#| Some("..") => {
#| components.push("..")
#| if allow_above_root {
#| components.push("..")
#| }
#| }
#| None => if allow_above_root { components.push("..") }
#| Some(_) => ()
#| }
#| _ => components.push(component)
#| }
#|}
#|fn skip_unix_separators(path : StringView, start : Int32) -> Int32 {
#| let mut index = start
#| while index < path.length() && is_unix_sep_at(path, index) {
#| index += 1
#| }
#| index
#|}
#|fn parse_unix_components(
#| path : StringView,
#| start : Int32,
#| allow_above_root~ : Bool,
#|) -> Array[StringView] {
#| let components : Array[StringView] = []
#| path[start:]
#| .split("/")
#| .filter(fn(component) { !component.is_empty() })
#| .each(fn(component) {
#| push_unix_component(components, component, allow_above_root~)
#| })
#| components
#|}
#|fn UnixPath::parse(path : StringView) -> UnixPath {
#| let prefix = if !path.is_empty() && is_unix_sep_at(path, 0) {
#| Root
#| } else {
#| None
#| }
#| let start = if prefix is Root { skip_unix_separators(path, 0) } else { 0 }
#| let trailing_separator = path.length() > 0 &&
#| is_unix_sep_at(path, path.length() - 1)
#| let components = parse_unix_components(
#| path,
#| start,
#| allow_above_root=prefix is None,
#| )
#| { prefix, trailing_separator, components, }
#|}
),
},
)
///|
let moonbitlang_core_path_internal_unicode_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/path/internal/unicode",
deps={},
files={
"lowercase.mbt": (
#|let shift_one : ReadOnlyArray[Char] = [
#| 'Ā', 'Ă', 'Ą', 'Ć', 'Ĉ', 'Ċ', 'Č', 'Ď', 'Đ', 'Ē', 'Ĕ', 'Ė', 'Ę', 'Ě',
#| 'Ĝ', 'Ğ', 'Ġ', 'Ģ', 'Ĥ', 'Ħ', 'Ĩ', 'Ī', 'Ĭ', 'Į', 'IJ', 'Ĵ', 'Ķ', 'Ĺ',
#| 'Ļ', 'Ľ', 'Ŀ', 'Ł', 'Ń', 'Ņ', 'Ň', 'Ŋ', 'Ō', 'Ŏ', 'Ő', 'Œ', 'Ŕ', 'Ŗ',
#| 'Ř', 'Ś', 'Ŝ', 'Ş', 'Š', 'Ţ', 'Ť', 'Ŧ', 'Ũ', 'Ū', 'Ŭ', 'Ů', 'Ű', 'Ų',
#| 'Ŵ', 'Ŷ', 'Ź', 'Ż', 'Ž', 'Ƃ', 'Ƅ', 'Ƈ', 'Ƌ', 'Ƒ', 'Ƙ', 'Ơ', 'Ƣ', 'Ƥ',
#| 'Ƨ', 'Ƭ', 'Ư', 'Ƴ', 'Ƶ', 'Ƹ', 'Ƽ', 'Dž', 'Lj', 'Nj', 'Ǎ', 'Ǐ', 'Ǒ', 'Ǔ',
#| 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ', 'Ǟ', 'Ǡ', 'Ǣ', 'Ǥ', 'Ǧ', 'Ǩ', 'Ǫ', 'Ǭ', 'Ǯ', 'Dz',
#| 'Ǵ', 'Ǹ', 'Ǻ', 'Ǽ', 'Ǿ', 'Ȁ', 'Ȃ', 'Ȅ', 'Ȇ', 'Ȉ', 'Ȋ', 'Ȍ', 'Ȏ', 'Ȑ',
#| 'Ȓ', 'Ȕ', 'Ȗ', 'Ș', 'Ț', 'Ȝ', 'Ȟ', 'Ȣ', 'Ȥ', 'Ȧ', 'Ȩ', 'Ȫ', 'Ȭ', 'Ȯ',
#| 'Ȱ', 'Ȳ', 'Ȼ', 'Ɂ', 'Ɇ', 'Ɉ', 'Ɋ', 'Ɍ', 'Ɏ', 'Ͱ', 'Ͳ', 'Ͷ', 'Ϙ', 'Ϛ',
#| 'Ϝ', 'Ϟ', 'Ϡ', 'Ϣ', 'Ϥ', 'Ϧ', 'Ϩ', 'Ϫ', 'Ϭ', 'Ϯ', 'Ϸ', 'Ϻ', 'Ѡ', 'Ѣ',
#| 'Ѥ', 'Ѧ', 'Ѩ', 'Ѫ', 'Ѭ', 'Ѯ', 'Ѱ', 'Ѳ', 'Ѵ', 'Ѷ', 'Ѹ', 'Ѻ', 'Ѽ', 'Ѿ',
#| 'Ҁ', 'Ҋ', 'Ҍ', 'Ҏ', 'Ґ', 'Ғ', 'Ҕ', 'Җ', 'Ҙ', 'Қ', 'Ҝ', 'Ҟ', 'Ҡ', 'Ң',
#| 'Ҥ', 'Ҧ', 'Ҩ', 'Ҫ', 'Ҭ', 'Ү', 'Ұ', 'Ҳ', 'Ҵ', 'Ҷ', 'Ҹ', 'Һ', 'Ҽ', 'Ҿ',
#| 'Ӂ', 'Ӄ', 'Ӆ', 'Ӈ', 'Ӊ', 'Ӌ', 'Ӎ', 'Ӑ', 'Ӓ', 'Ӕ', 'Ӗ', 'Ә', 'Ӛ', 'Ӝ',
#| 'Ӟ', 'Ӡ', 'Ӣ', 'Ӥ', 'Ӧ', 'Ө', 'Ӫ', 'Ӭ', 'Ӯ', 'Ӱ', 'Ӳ', 'Ӵ', 'Ӷ', 'Ӹ',
#| 'Ӻ', 'Ӽ', 'Ӿ', 'Ԁ', 'Ԃ', 'Ԅ', 'Ԇ', 'Ԉ', 'Ԋ', 'Ԍ', 'Ԏ', 'Ԑ', 'Ԓ', 'Ԕ',
#| 'Ԗ', 'Ԙ', 'Ԛ', 'Ԝ', 'Ԟ', 'Ԡ', 'Ԣ', 'Ԥ', 'Ԧ', 'Ԩ', 'Ԫ', 'Ԭ', 'Ԯ', '',
#| 'Ḁ', 'Ḃ', 'Ḅ', 'Ḇ', 'Ḉ', 'Ḋ', 'Ḍ', 'Ḏ', 'Ḑ', 'Ḓ', 'Ḕ', 'Ḗ',
#| 'Ḙ', 'Ḛ', 'Ḝ', 'Ḟ', 'Ḡ', 'Ḣ', 'Ḥ', 'Ḧ', 'Ḩ', 'Ḫ', 'Ḭ', 'Ḯ',
#| 'Ḱ', 'Ḳ', 'Ḵ', 'Ḷ', 'Ḹ', 'Ḻ', 'Ḽ', 'Ḿ', 'Ṁ', 'Ṃ', 'Ṅ', 'Ṇ',
#| 'Ṉ', 'Ṋ', 'Ṍ', 'Ṏ', 'Ṑ', 'Ṓ', 'Ṕ', 'Ṗ', 'Ṙ', 'Ṛ', 'Ṝ', 'Ṟ',
#| 'Ṡ', 'Ṣ', 'Ṥ', 'Ṧ', 'Ṩ', 'Ṫ', 'Ṭ', 'Ṯ', 'Ṱ', 'Ṳ', 'Ṵ', 'Ṷ',
#| 'Ṹ', 'Ṻ', 'Ṽ', 'Ṿ', 'Ẁ', 'Ẃ', 'Ẅ', 'Ẇ', 'Ẉ', 'Ẋ', 'Ẍ', 'Ẏ',
#| 'Ẑ', 'Ẓ', 'Ẕ', 'Ạ', 'Ả', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Ắ', 'Ằ',
#| 'Ẳ', 'Ẵ', 'Ặ', 'Ẹ', 'Ẻ', 'Ẽ', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'Ỉ',
#| 'Ị', 'Ọ', 'Ỏ', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ớ', 'Ờ', 'Ở', 'Ỡ',
#| 'Ợ', 'Ụ', 'Ủ', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Ỳ', 'Ỵ', 'Ỷ', 'Ỹ',
#| 'Ỻ', 'Ỽ', 'Ỿ', 'Ↄ', 'Ⱡ', 'Ⱨ', 'Ⱪ', 'Ⱬ', 'Ⱳ', 'Ⱶ', 'Ⲁ', 'Ⲃ',
#| 'Ⲅ', 'Ⲇ', 'Ⲉ', 'Ⲋ', 'Ⲍ', 'Ⲏ', 'Ⲑ', 'Ⲓ', 'Ⲕ', 'Ⲗ', 'Ⲙ', 'Ⲛ',
#| 'Ⲝ', 'Ⲟ', 'Ⲡ', 'Ⲣ', 'Ⲥ', 'Ⲧ', 'Ⲩ', 'Ⲫ', 'Ⲭ', 'Ⲯ', 'Ⲱ', 'Ⲳ',
#| 'Ⲵ', 'Ⲷ', 'Ⲹ', 'Ⲻ', 'Ⲽ', 'Ⲿ', 'Ⳁ', 'Ⳃ', 'Ⳅ', 'Ⳇ', 'Ⳉ', 'Ⳋ',
#| 'Ⳍ', 'Ⳏ', 'Ⳑ', 'Ⳓ', 'Ⳕ', 'Ⳗ', 'Ⳙ', 'Ⳛ', 'Ⳝ', 'Ⳟ', 'Ⳡ', 'Ⳣ',
#| 'Ⳬ', 'Ⳮ', 'Ⳳ', 'Ꙁ', 'Ꙃ', 'Ꙅ', 'Ꙇ', 'Ꙉ', 'Ꙋ', 'Ꙍ', 'Ꙏ', 'Ꙑ',
#| 'Ꙓ', 'Ꙕ', 'Ꙗ', 'Ꙙ', 'Ꙛ', 'Ꙝ', 'Ꙟ', 'Ꙡ', 'Ꙣ', 'Ꙥ', 'Ꙧ', 'Ꙩ',
#| 'Ꙫ', 'Ꙭ', 'Ꚁ', 'Ꚃ', 'Ꚅ', 'Ꚇ', 'Ꚉ', 'Ꚋ', 'Ꚍ', 'Ꚏ', 'Ꚑ', 'Ꚓ',
#| 'Ꚕ', 'Ꚗ', 'Ꚙ', 'Ꚛ', 'Ꜣ', 'Ꜥ', 'Ꜧ', 'Ꜩ', 'Ꜫ', 'Ꜭ', 'Ꜯ', 'Ꜳ',
#| 'Ꜵ', 'Ꜷ', 'Ꜹ', 'Ꜻ', 'Ꜽ', 'Ꜿ', 'Ꝁ', 'Ꝃ', 'Ꝅ', 'Ꝇ', 'Ꝉ', 'Ꝋ',
#| 'Ꝍ', 'Ꝏ', 'Ꝑ', 'Ꝓ', 'Ꝕ', 'Ꝗ', 'Ꝙ', 'Ꝛ', 'Ꝝ', 'Ꝟ', 'Ꝡ', 'Ꝣ',
#| 'Ꝥ', 'Ꝧ', 'Ꝩ', 'Ꝫ', 'Ꝭ', 'Ꝯ', 'Ꝺ', 'Ꝼ', 'Ꝿ', 'Ꞁ', 'Ꞃ', 'Ꞅ',
#| 'Ꞇ', 'Ꞌ', 'Ꞑ', 'Ꞓ', 'Ꞗ', 'Ꞙ', 'Ꞛ', 'Ꞝ', 'Ꞟ', 'Ꞡ', 'Ꞣ', 'Ꞥ',
#| 'Ꞧ', 'Ꞩ', 'Ꞵ', 'Ꞷ', 'Ꞹ', 'Ꞻ', 'Ꞽ', 'Ꞿ', 'Ꟁ', 'Ꟃ', 'Ꟈ', 'Ꟊ',
#| '', '', 'Ꟑ', '', '', 'Ꟗ', 'Ꟙ', '', 'Ꟶ',
#|]
#|let shift_ranges : ReadOnlyArray[(Char, Char, Int32)] = [
#| ('A', 'Z', 0x20),
#| ('À', 'Ö', 0x20),
#| ('Ø', 'Þ', 0x20),
#| ('Α', 'Ρ', 0x20),
#| ('Σ', 'Ϋ', 0x20),
#| ('А', 'Я', 0x20),
#| ('A', 'Z', 0x20),
#| ('\u{10d50}', '\u{10d65}', 0x20),
#| ('\u{118a0}', '\u{118bf}', 0x20),
#| ('\u{16e40}', '\u{16e5f}', 0x20),
#| ('Ա', 'Ֆ', 0x30),
#| ('Ⰰ', 'Ⱟ', 0x30),
#| ('Ꭰ', 'Ꮿ', 0x97d0),
#| ('Ἀ', 'Ἇ', -8),
#| ('Ἐ', 'Ἕ', -8),
#| ('Ἠ', 'Ἧ', -8),
#| ('Ἰ', 'Ἷ', -8),
#| ('Ὀ', 'Ὅ', -8),
#| ('Ὑ', 'Ὑ', -8),
#| ('Ὓ', 'Ὓ', -8),
#| ('Ὕ', 'Ὕ', -8),
#| ('Ὗ', 'Ὗ', -8),
#| ('Ὠ', 'Ὧ', -8),
#| ('ᾈ', 'ᾏ', -8),
#| ('ᾘ', 'ᾟ', -8),
#| ('ᾨ', 'ᾯ', -8),
#| ('Ᾰ', 'Ᾱ', -8),
#| ('Ῐ', 'Ῑ', -8),
#| ('Ῠ', 'Ῡ', -8),
#| ('\u{10400}', '\u{10427}', 0x28),
#| ('\u{104b0}', '\u{104d3}', 0x28),
#| ('Ό', 'Ό', 0x40),
#| ('\u{10c80}', '\u{10cb2}', 0x40),
#| ('Ა', 'Ჺ', -3008),
#| ('Ჽ', 'Ჿ', -3008),
#| ('Ⴀ', 'Ⴥ', 0x1c60),
#| ('Ⴧ', 'Ⴧ', 0x1c60),
#| ('Ⴭ', 'Ⴭ', 0x1c60),
#| ('\u{10570}', '\u{1057a}', 0x27),
#| ('\u{1057c}', '\u{1058a}', 0x27),
#| ('\u{1058c}', '\u{10592}', 0x27),
#| ('\u{10594}', '\u{10595}', 0x27),
#| ('\u{1e900}', '\u{1e921}', 0x22),
#| ('Ⓐ', 'Ⓩ', 0x1a),
#| ('\u{16ea0}', '\u{16eb8}', 0x1b),
#| ('Ѐ', 'Џ', 0x50),
#| ('Ⅰ', 'Ⅿ', 0x10),
#| ('Ϗ', 'Ϗ', 0x8),
#| ('Ᏸ', 'Ᏽ', 0x8),
#| ('DŽ', 'DŽ', 0x2),
#| ('LJ', 'LJ', 0x2),
#| ('NJ', 'NJ', 0x2),
#| ('DZ', 'DZ', 0x2),
#| ('Ƞ', 'Ƞ', -130),
#| ('Ͻ', 'Ͽ', -130),
#| ('Ὲ', 'Ή', -86),
#| ('Ɖ', 'Ɗ', 0xcd),
#| ('Ɠ', 'Ɠ', 0xcd),
#| ('Ʀ', 'Ʀ', 0xda),
#| ('Ʃ', 'Ʃ', 0xda),
#| ('Ʈ', 'Ʈ', 0xda),
#| ('Έ', 'Ί', 0x25),
#| ('ᾼ', 'ᾼ', -9),
#| ('ῌ', 'ῌ', -9),
#| ('ῼ', 'ῼ', -9),
#| ('Ɩ', 'Ɩ', 0xd3),
#| ('Ɯ', 'Ɯ', 0xd3),
#| ('Ʊ', 'Ʋ', 0xd9),
#| ('Ύ', 'Ώ', 0x3f),
#| ('Ϲ', 'Ϲ', -7),
#| ('Ῥ', 'Ῥ', -7),
#| ('Ὰ', 'Ά', -74),
#| ('Ὶ', 'Ί', -100),
#| ('Ὺ', 'Ύ', -112),
#| ('Ὸ', 'Ό', -128),
#| ('Ὼ', 'Ώ', -126),
#| ('Ȿ', 'Ɀ', -10815),
#| ('Ɦ', 'Ɦ', -42308),
#| ('Ɪ', 'Ɪ', -42308),
#|]
#|let explicit_maps : ReadOnlyArray[(Char, Char)] = [
#| ('Ÿ', 'ÿ'),
#| ('Ɓ', 'ɓ'),
#| ('Ɔ', 'ɔ'),
#| ('Ǝ', 'ǝ'),
#| ('Ə', 'ə'),
#| ('Ɛ', 'ɛ'),
#| ('Ɣ', 'ɣ'),
#| ('Ɨ', 'ɨ'),
#| ('Ɲ', 'ɲ'),
#| ('Ɵ', 'ɵ'),
#| ('Ʒ', 'ʒ'),
#| ('Ƕ', 'ƕ'),
#| ('Ƿ', 'ƿ'),
#| ('Ⱥ', 'ⱥ'),
#| ('Ƚ', 'ƚ'),
#| ('Ⱦ', 'ⱦ'),
#| ('Ƀ', 'ƀ'),
#| ('Ʉ', 'ʉ'),
#| ('Ʌ', 'ʌ'),
#| ('Ϳ', 'ϳ'),
#| ('Ά', 'ά'),
#| ('ϴ', 'θ'),
#| ('Ӏ', 'ӏ'),
#| ('ẞ', 'ß'),
#| ('Ω', 'ω'),
#| ('K', 'k'),
#| ('Å', 'å'),
#| ('Ⅎ', 'ⅎ'),
#| ('Ɫ', 'ɫ'),
#| ('Ᵽ', 'ᵽ'),
#| ('Ɽ', 'ɽ'),
#| ('Ɑ', 'ɑ'),
#| ('Ɱ', 'ɱ'),
#| ('Ɐ', 'ɐ'),
#| ('Ɒ', 'ɒ'),
#| ('Ᵹ', 'ᵹ'),
#| ('Ɥ', 'ɥ'),
#| ('Ɜ', 'ɜ'),
#| ('Ɡ', 'ɡ'),
#| ('Ɬ', 'ɬ'),
#| ('Ʞ', 'ʞ'),
#| ('Ʇ', 'ʇ'),
#| ('Ʝ', 'ʝ'),
#| ('Ꭓ', 'ꭓ'),
#| ('Ꞔ', 'ꞔ'),
#| ('Ʂ', 'ʂ'),
#| ('Ᶎ', 'ᶎ'),
#| ('', 'ɤ'),
#| ('', 'ƛ'),
#|]
#|const LOWERCASE_ENTRY_COUNT = 1487
#|pub fn to_lowercase(ch : Char) -> Char {
#| lowercase_map.force().get(ch).unwrap_or(ch)
#|}
#|fn Char::shift_char(c : Char, delta : Int32) -> Char {
#| Int32::to_char(c.to_int() + delta).unwrap()
#|}
#|let lowercase_map : @lazy.Lazy[@hashmap.HashMap[Char, Char]] = Lazy(() => {
#| let map : @hashmap.HashMap[Char, Char] = HashMap(
#| [],
#| capacity=LOWERCASE_ENTRY_COUNT * 2,
#| )
#| for e in shift_one {
#| map[e] = e.shift_char(0x1)
#| }
#| for e in explicit_maps {
#| map[e.0] = e.1
#| }
#| for (start, end, delta) in shift_ranges {
#| for code in start.to_int().until(end.to_int(), inclusive=true) {
#| let ch = code.to_char().unwrap()
#| map[ch] = ch.shift_char(delta)
#| }
#| }
#| map
#|})
),
},
)
///|
let moonbitlang_core_path_internal_win32_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/path/internal/win32",
deps={},
files={
"path.mbt": (
#|pub using @error {type ResolveError}
#|fn is_win_sep(ch : Char) -> Bool {
#| ch == '\\' || ch == '/'
#|}
#|fn last_win_sep(path : StringView) -> Int32? {
#| match (path.rev_find("/"), path.rev_find("\\")) {
#| (None, None) => None
#| (Some(idx), None) | (None, Some(idx)) => Some(idx)
#| (Some(lidx), Some(ridx)) => Some(@cmp.maximum(lidx, ridx))
#| }
#|}
#|fn is_drive_relative(path : String) -> Bool {
#| start_with_win_drive(path) && (path.length() == 2 || !is_win_sep_at(path, 2))
#|}
#|fn current_dir_for_drive_with_cwd(
#| device : StringView,
#| fallback_cwd : String,
#|) -> String {
#| let device = device.to_owned()
#| let cwd = @env.get_env_var("=\{device}").unwrap_or(fallback_cwd)
#| if start_with_win_drive(cwd) &&
#| !cwd[0:2].equal_ignore_ascii_case(device) &&
#| cwd.length() > 2 &&
#| is_win_sep_at(cwd, 2) {
#| "\{device}\\"
#| } else if !start_with_win_drive(cwd) &&
#| cwd.length() > 0 &&
#| is_win_sep_at(cwd, 0) {
#| "\{device}\{cwd}"
#| } else if !start_with_win_drive(cwd) {
#| "\{device}\\\{cwd}"
#| } else {
#| cwd
#| }
#|}
#|pub fn basename(path : String) -> StringView {
#| let mut path = path[:]
#| if start_with_win_drive(path) {
#| path = path[2:]
#| }
#| let path = path.trim_end(chars="/\\")
#| match last_win_sep(path) {
#| None => path
#| Some(idx) => path[idx + 1:]
#| }
#|}
#|pub fn dirname(path : String) -> String {
#| let raw = path
#| guard !raw.is_empty() else { "." }
#| let root_end = WinPath::parse(raw).root_end
#| guard root_end < raw.length() else { raw }
#| let mut path = raw[:]
#| while path.length() > root_end &&
#| path is [.. rest, last_char] &&
#| is_win_sep(last_char) {
#| path = rest
#| }
#| if path.length() <= root_end {
#| raw[0:root_end].to_owned()
#| } else {
#| match last_win_sep(path) {
#| None => if root_end > 0 { raw[0:root_end].to_owned() } else { "." }
#| Some(idx) =>
#| if idx < root_end {
#| raw[0:root_end].to_owned()
#| } else {
#| raw[0:idx].to_owned()
#| }
#| }
#| }
#|}
#|pub fn extname(path : String) -> StringView {
#| let basename = basename(path)
#| if basename == "." || basename == ".." {
#| return ""
#| }
#| match basename.rev_find(".") {
#| None | Some(0) => ""
#| Some(idx) => basename[idx:]
#| }
#|}
#|pub fn is_absolute(path : String) -> Bool {
#| match WinPath::parse(path).prefix {
#| Root | VerbatimRoot | DeviceRoot | UNC(_) | VolumeLetterRoot(_) => true
#| _ => false
#| }
#|}
#|pub fn root(path : String) -> StringView {
#| path[:WinPath::parse(path).root_end]
#|}
#|pub fn join(lhs : String, rhs : String) -> String {
#| guard !lhs.is_empty() || !rhs.is_empty() else { "." }
#| guard !lhs.is_empty() else { normalize(rhs) }
#| guard !rhs.is_empty() else { normalize(lhs) }
#| let joined = lhs + "\\" + rhs
#| let intentional_unc = lhs.length() > 2 &&
#| is_win_sep_at(lhs, 0) &&
#| is_win_sep_at(lhs, 1) &&
#| !is_win_sep_at(lhs, 2)
#| if intentional_unc {
#| normalize(joined)
#| } else {
#| let leading = skip_win_separators(joined, 0)
#| if leading > 1 {
#| normalize(joined[leading - 1:].to_owned())
#| } else {
#| normalize(joined)
#| }
#| }
#|}
#|pub fn normalize(path : String) -> String {
#| let raw = path
#| let parsed = WinPath::parse(raw)
#| let normalized = Show::to_string(parsed)
#| if needs_normalize_drive_guard(raw, normalized, parsed) {
#| ".\\\{normalized}"
#| } else {
#| normalized
#| }
#|}
#|fn needs_normalize_drive_guard(
#| raw : String,
#| normalized : String,
#| parsed : WinPath,
#|) -> Bool {
#| guard parsed.prefix is None && raw.contains_char(':') else { return false }
#| if start_with_win_drive(normalized) {
#| return true
#| }
#| for i in 0.. Bool {
#| guard path.components.is_empty() else { false }
#| match path.prefix {
#| Root | VolumeLetterRoot(_) | UNC(_) => true
#| _ => false
#| }
#|}
#|fn trim_resolved_trailing_separators(path : String) -> String {
#| let parsed = WinPath::parse(path)
#| guard !is_regular_win_root(parsed) else { path }
#| let mut raw = path[:]
#| while raw is [.. rest, last_char] && is_win_sep(last_char) && !rest.is_empty() {
#| raw = rest
#| }
#| raw.to_owned()
#|}
#|pub fn relative(path : String, base~ : String) -> String raise ResolveError {
#| relative_with_current_dir(path, base~, @env.current_dir)
#|}
#|fn relative_with_current_dir(
#| path : String,
#| base~ : String,
#| current_dir : () -> String?,
#|) -> String raise ResolveError {
#| let resolved_from = resolve_with_current_dir(base, current_dir)
#| let resolved_to = resolve_with_current_dir(path, current_dir)
#| relative_resolved_paths(resolved_from, resolved_to)
#|}
#|fn win_string_equal_ignore_case(left : StringView, right : StringView) -> Bool {
#| let mut li = 0
#| let mut ri = 0
#| while left.get_char(li) is Some(l) && right.get_char(ri) is Some(r) {
#| guard @unicode.to_lowercase(l) == @unicode.to_lowercase(r) else {
#| return false
#| }
#| li += l.utf16_len()
#| ri += r.utf16_len()
#| }
#| left.get_char(li) is None && right.get_char(ri) is None
#|}
#|fn split_win_parts(path : StringView) -> Array[StringView] {
#| let parts : Array[StringView] = []
#| let mut start = 0
#| let mut i = 0
#| while path.get_char(i) is Some(ch) {
#| if is_win_sep(ch) {
#| if start < i {
#| parts.push(path[start:i])
#| }
#| i = skip_win_separators(path, i)
#| start = i
#| } else {
#| i += ch.utf16_len()
#| }
#| }
#| if start < i {
#| parts.push(path[start:i])
#| }
#| parts
#|}
#|fn relative_resolved_paths(from_orig : String, to_orig : String) -> String {
#| if from_orig == to_orig || win_string_equal_ignore_case(from_orig, to_orig) {
#| return ""
#| }
#| let from_parts = split_win_parts(from_orig)
#| let to_parts = split_win_parts(to_orig)
#| if from_parts.is_empty() ||
#| to_parts.is_empty() ||
#| !win_string_equal_ignore_case(from_parts[0], to_parts[0]) {
#| return to_orig
#| }
#| let mut common = 0
#| let limit = @cmp.minimum(from_parts.length(), to_parts.length())
#| while common < limit &&
#| win_string_equal_ignore_case(from_parts[common], to_parts[common]) {
#| common += 1
#| }
#| let components : Array[StringView] = [
#| ..(Array::make(from_parts.length() - common, "..") : Array[StringView]),
#| ..to_parts[common:],
#| ]
#| guard !components.is_empty() else { "" }
#| let builder = StringBuilder()
#| for i in 0.. 0 {
#| builder.write_char('\\')
#| }
#| builder.write_stringview(components[i])
#| }
#| builder.to_string()
#|}
#|pub fn resolve(path : String) -> String raise ResolveError {
#| resolve_with_current_dir(path, @env.current_dir)
#|}
#|fn resolve_with_current_dir(
#| path : String,
#| current_dir : () -> String?,
#|) -> String raise ResolveError {
#| match WinPath::parse(path).prefix {
#| VolumeLetterRoot(_) | UNC(_) | VerbatimRoot | DeviceRoot =>
#| trim_resolved_trailing_separators(normalize(path))
#| _ => {
#| guard current_dir() is Some(cwd) else {
#| raise CurrentDirectoryUnavailable
#| }
#| resolve_with_cwd(path, cwd)
#| }
#| }
#|}
#|fn cwd_device_for_drive_less_rooted_path(cwd : String) -> String? {
#| match WinPath::parse(cwd).prefix {
#| VolumeLetterRoot(_) => Some(cwd[0:2].to_owned())
#| UNC(hostname~, shared_folder~) => Some("\\\\\{hostname}\\\{shared_folder}")
#| _ => None
#| }
#|}
#|fn resolve_with_cwd(path : String, cwd : String) -> String {
#| let raw_path = path
#| let parsed = WinPath::parse(raw_path)
#| let resolved = match parsed.prefix {
#| Root =>
#| match cwd_device_for_drive_less_rooted_path(cwd) {
#| Some(device) => normalize("\{device}\{raw_path}")
#| None => normalize(path)
#| }
#| VolumeLetterRoot(_) | UNC(_) | VerbatimRoot | DeviceRoot => normalize(path)
#| _ =>
#| if is_drive_relative(raw_path) {
#| let base = normalize(current_dir_for_drive_with_cwd(raw_path[0:2], cwd))
#| let tail = raw_path[2:]
#| if tail.is_empty() {
#| base
#| } else {
#| join(base, tail.to_owned())
#| }
#| } else {
#| join(cwd, path)
#| }
#| }
#| trim_resolved_trailing_separators(resolved)
#|}
#|pub let delimiter : Char = ';'
#|pub let sep : Char = '\\'
),
"win_path.mbt": (
#|#derive(@json.ToJson, Hash, Eq)
#|priv struct WinPath {
#| prefix : WinPrefix
#| trailing_separator : Bool
#| components : Array[StringView]
#| root_end : Int32
#|}
#|#derive(@json.ToJson, Hash, Eq)
#|priv enum WinPrefix {
#| Root
#| VerbatimRoot
#| DeviceRoot
#| UNC(hostname~ : String, shared_folder~ : String)
#| VolumeLetterRoot(Char)
#| VolumeLetter(Char)
#| None
#|}
#|const VerbatimPrefix : String =
#| #|\\?\
#|const ColonBackslash : String =
#| #|:\
#|const DevicePrefix : String =
#| #|\\.\
#|const UncPrefix : String =
#| #|\\
#|const BackSlash : Char = '\\'
#|impl Show for WinPrefix with fn output(self, logger) {
#| match self {
#| Root => logger.write_char(BackSlash)
#| VerbatimRoot => logger.write_string(VerbatimPrefix)
#| DeviceRoot => logger.write_string(DevicePrefix)
#| UNC(hostname~, shared_folder~) =>
#| logger <+
#| "\{UncPrefix}\{hostname}\{BackSlash}\{shared_folder}\{BackSlash}"
#| VolumeLetterRoot(letter) => logger <+ "\{letter}\{ColonBackslash}"
#| VolumeLetter(letter) => logger <+ "\{letter}:"
#| None => ()
#| }
#|}
#|fn WinPath::output(self : WinPath, logger : &Logger) -> Unit {
#| let { prefix, trailing_separator, components, .. } = self
#| let mut prefix_ends_with_sep = false
#| match prefix {
#| Root => {
#| logger.write_char(BackSlash)
#| prefix_ends_with_sep = true
#| }
#| VerbatimRoot => {
#| logger.write_string(VerbatimPrefix)
#| prefix_ends_with_sep = true
#| }
#| DeviceRoot => {
#| logger.write_string(DevicePrefix)
#| prefix_ends_with_sep = true
#| }
#| UNC(hostname~, shared_folder~) => {
#| logger <+
#| "\{UncPrefix}\{hostname}\{BackSlash}\{shared_folder}\{BackSlash}"
#| prefix_ends_with_sep = true
#| }
#| VolumeLetterRoot(letter) => {
#| logger <+ "\{letter}\{ColonBackslash}"
#| prefix_ends_with_sep = true
#| }
#| VolumeLetter(letter) => logger <+ "\{letter}:"
#| None => ()
#| }
#| match components {
#| [] =>
#| match prefix {
#| None | VolumeLetter(_) =>
#| if trailing_separator {
#| logger <+ ".\\"
#| } else {
#| logger.write_char('.')
#| }
#| _ => ()
#| }
#| [x, .. xs] => {
#| if !prefix_ends_with_sep &&
#| !(prefix is None) &&
#| !(prefix is VolumeLetter(_)) {
#| logger.write_char(BackSlash)
#| }
#| logger.write_view(x)
#| for component in xs {
#| logger <+ "\{BackSlash}\{component}"
#| }
#| if trailing_separator {
#| logger.write_char(BackSlash)
#| }
#| }
#| }
#|}
#|impl Show for WinPath with fn output(self, logger) {
#| WinPath::output(self, logger)
#|}
#|fn is_win_sep_at(path : StringView, index : Int32) -> Bool {
#| path.get_char(index) is Some(ch) && is_win_sep(ch)
#|}
#|fn start_with_win_drive(path : StringView) -> Bool {
#| path is [ch, ':', ..] && ch.is_ascii_alphabetic()
#|}
#|fn skip_win_separators(path : StringView, start : Int32) -> Int32 {
#| let mut index = start
#| while index < path.length() && is_win_sep_at(path, index) {
#| index += 1
#| }
#| index
#|}
#|fn find_next_win_sep(path : StringView, start : Int32) -> Int32? {
#| for i in start.. Unit {
#| match component {
#| "." => ()
#| ".." =>
#| match components.pop() {
#| Some("..") => {
#| components.push("..")
#| if allow_above_root {
#| components.push("..")
#| }
#| }
#| None => if allow_above_root { components.push("..") }
#| Some(_) => ()
#| }
#| _ => components.push(component)
#| }
#|}
#|fn parse_windows_components(
#| path : StringView,
#| start : Int32,
#| allow_above_root~ : Bool,
#|) -> Array[StringView] {
#| let components : Array[StringView] = []
#| path[start:]
#| .replace_all(old="/", new="\\")
#| .split("\\")
#| .filter(fn(component) { !component.is_empty() })
#| .each(fn(component) {
#| push_windows_component(components, component, allow_above_root~)
#| })
#| components
#|}
#|fn parse_verbatim_prefixed_path(
#| path : StringView,
#| trailing_separator : Bool,
#|) -> WinPath? {
#| guard path.length() >= 4 && path.get_char(2) == Some('?') else { return None }
#| guard is_win_sep_at(path, 3) else { return None }
#| let marker_end = 4
#| let components = parse_windows_components(
#| path,
#| marker_end,
#| allow_above_root=false,
#| )
#| let root_end = component_root_end(path, marker_end)
#| Some({ prefix: VerbatimRoot, trailing_separator, components, root_end, })
#|}
#|fn parse_device_prefixed_path(
#| path : StringView,
#| trailing_separator : Bool,
#|) -> WinPath? {
#| guard path.length() >= 4 && path.get_char(2) == Some('.') else { return None }
#| guard is_win_sep_at(path, 3) else { return None }
#| let part_start = 4
#| let components = parse_windows_components(
#| path,
#| part_start,
#| allow_above_root=false,
#| )
#| let root_end = component_root_end(path, part_start)
#| Some({ prefix: DeviceRoot, trailing_separator, components, root_end, })
#|}
#|fn component_root_end(path : StringView, start : Int32) -> Int32 {
#| match find_next_win_sep(path, start) {
#| None => path.length()
#| Some(index) => index + 1
#| }
#|}
#|fn parse_unc_path(path : StringView, trailing_separator : Bool) -> WinPath? {
#| guard path.length() <= 2 || !is_win_sep_at(path, 2) else { return None }
#| let hostname_start = 2
#| guard hostname_start < path.length() else { return None }
#| match find_next_win_sep(path, hostname_start) {
#| Some(hostname_end) => {
#| let share_start = skip_win_separators(path, hostname_end)
#| guard share_start < path.length() else { return None }
#| let share_end = match find_next_win_sep(path, share_start) {
#| Some(index) => index
#| _ => path.length()
#| }
#| let root_end = share_end + (if share_end < path.length() { 1 } else { 0 })
#| let component_start = {
#| guard share_end < path.length() else { path.length() }
#| skip_win_separators(path, share_end)
#| }
#| let components = parse_windows_components(
#| path,
#| component_start,
#| allow_above_root=false,
#| )
#| Some({
#| prefix: UNC(
#| hostname=path[hostname_start:hostname_end].to_owned(),
#| shared_folder=path[share_start:share_end].to_owned(),
#| ),
#| trailing_separator,
#| components,
#| root_end,
#| })
#| }
#| _ => None
#| }
#|}
#|fn WinPath::parse(path : StringView) -> WinPath {
#| let trailing_separator = path.length() > 0 &&
#| is_win_sep_at(path, path.length() - 1)
#| guard path.length() >= 2 && is_win_sep_at(path, 0) && is_win_sep_at(path, 1) else {
#| return parse_drive_root_relative(path, trailing_separator)
#| }
#| match () {
#| _ if parse_verbatim_prefixed_path(path, trailing_separator) is Some(parsed) =>
#| parsed
#| _ if parse_device_prefixed_path(path, trailing_separator) is Some(parsed) =>
#| parsed
#| _ if parse_unc_path(path, trailing_separator) is Some(parsed) => parsed
#| _ => parse_drive_root_relative(path, trailing_separator)
#| }
#|}
#|fn parse_drive_root_relative(
#| path : StringView,
#| trailing_separator : Bool,
#|) -> WinPath {
#| if start_with_win_drive(path) {
#| let letter = path[0:1].get_char(0).unwrap()
#| guard path.length() > 2 && is_win_sep_at(path, 2) else {
#| let components = parse_windows_components(path, 2, allow_above_root=true)
#| return {
#| prefix: VolumeLetter(letter),
#| trailing_separator,
#| components,
#| root_end: 2,
#| }
#| }
#| let components = parse_windows_components(
#| path,
#| skip_win_separators(path, 2),
#| allow_above_root=false,
#| )
#| return {
#| prefix: VolumeLetterRoot(letter),
#| trailing_separator,
#| components,
#| root_end: 3,
#| }
#| }
#| guard path.length() > 0 && is_win_sep_at(path, 0) else {
#| let components = parse_windows_components(path, 0, allow_above_root=true)
#| return { prefix: None, trailing_separator, components, root_end: 0, }
#| }
#| let components = parse_windows_components(
#| path,
#| skip_win_separators(path, 0),
#| allow_above_root=false,
#| )
#| { prefix: Root, trailing_separator, components, root_end: 1, }
#|}
),
},
)
///|
let moonbitlang_core_prelude_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/prelude",
deps={},
files={
"prelude.mbt": (
#|pub using @buffer {type Buffer}
#|pub using @set {type Set}
#|pub using @bigint {type BigInt}
#|pub using @builtin {
#| abort,
#| assert_true,
#| assert_false,
#| compare,
#| debug_assert,
#| fail,
#| ignore,
#| hash,
#| inspect,
#| panic,
#| physical_equal,
#| println,
#| eprintln,
#| trait Eq,
#| trait Compare,
#| trait Hash,
#| trait Logger,
#| trait Show,
#| trait Default,
#| trait Add,
#| trait Sub,
#| trait Mul,
#| trait Div,
#| trait Mod,
#| trait Neg,
#| trait Shl,
#| trait Shr,
#| trait BitAnd,
#| trait BitOr,
#| trait BitXOr,
#| type ArgsLoc,
#| type Array,
#| type ArrayView,
#| type MutArrayView,
#| type UninitializedArray,
#| type Failure,
#| type Hasher,
#| type Iter,
#| type Iter2,
#| type Map,
#| type InspectError,
#| type SnapshotError,
#| type SourceLoc,
#| type StringBuilder,
#|}
#|pub using @builtin {
#| type Unit,
#| type Bool,
#| type Byte,
#| type Char,
#| type Int,
#| type Int32,
#| type Int64,
#| type UInt,
#| type UInt32,
#| type UInt16,
#| type UInt64,
#| type Double,
#| type String,
#| type V128,
#| type Bytes,
#| type StringView,
#| type BytesView,
#| type FixedArray,
#| type ReadOnlyArray,
#| type Error,
#| type FuncRef,
#| type Option,
#| type Result,
#|}
#|pub using @int16 {type Int16}
#|pub using @float {type Float}
#|#deprecated("Use !expr instead")
#|#warnings("-deprecated")
#|pub using @builtin {not}
#|pub using @debug {debug, type Repr, trait Debug, debug_inspect, repr}
#|#deprecated("Use `Repr(x)` instead")
#|#warnings("-deprecated")
#|pub using @debug {to_repr}
#|#deprecated("for debugging only, not for production")
#|#warnings("-deprecated")
#|pub using @debug {dump}
#|pub using @string {type Regex}
#|pub using @bytes {type BytesRegex}
#|pub using @lexbuf {
#| trait Scannable,
#| trait BytesScannable,
#| type StringScanner,
#| type BytesScanner,
#| type Lexbuf,
#| type AsyncLexbuf,
#| type BytesLexbuf,
#| type AsyncBytesLexbuf,
#|}
#|#deprecated("use `value |> x => { ... ; x} instead")
#|pub fn[T] tap(value : T, f : (T) -> Unit raise) -> T raise {
#| f(value)
#| value
#|}
#|#deprecated("use `value |> (x) => {...}` operator instead")
#|pub fn[T, R] then(value : T, f : (T) -> R raise?) -> R raise? {
#| f(value)
#|}
#|pub let null : Json = @json.null
#|pub using @json {type Json, trait ToJson, trait FromJson, json_inspect}
#|#deprecated("The name `Iterator` is deprecated, use `Iter` instead. Note that if you have defined `iterator()` method to support `for .. in` loop, you should also rename `iterator()` to `iter()`. See https://github.com/moonbitlang/core/pull/3127 for more details.")
#|pub type Iterator[X] = Iter[X]
#|#deprecated("The name `Iterator2` is deprecated, use `Iter2` instead. Note that if you have defined `iterator2()` method to support `for .. in` loop, you should also rename `iterator2()` to `iter2()`. See https://github.com/moonbitlang/core/pull/3127 for more details.")
#|pub type Iterator2[X, Y] = Iter2[X, Y]
#|pub using @ref {type Ref}
#|pub using @lazy {type Lazy}
#|pub using @test {assert_eq, assert_not_eq}
#|pub using @stdio {stdin, stdout, stderr}
#|#cfg(not(target="wasm-gc"))
#|#warnings("-unused_value")
#|let _import_async : Unit = ignore(@async.run_async_main)
),
},
)
///|
let moonbitlang_core_priority_queue_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/priority_queue",
deps={},
files={
"debug.mbt": (
#|pub impl[A : @debug.Debug + Compare] @debug.Debug for PriorityQueue[A] with fn to_repr(
#| self,
#|) {
#| @debug.Repr::opaque_(
#| "PriorityQueue",
#| @debug.Repr::array([ for x in self => Repr(x) ]),
#| )
#|}
#|test "Debug for PriorityQueue" {
#| let pq : PriorityQueue[Int32] = from_array([3, 1, 2])
#| @debug.debug_inspect(pq, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `PriorityQueue([])` instead")
#|#as_free_fn(deprecated="Use `PriorityQueue([])` instead")
#|pub fn[A] PriorityQueue::new() -> PriorityQueue[A] {
#| new_priority_queue()
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with Default::{default}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend PriorityQueue with @json.ToJson::{to_json}
),
"priority_queue.mbt": (
#|fn[A] new_priority_queue() -> PriorityQueue[A] {
#| { len: 0, top: None, }
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[A : Compare] PriorityQueue::PriorityQueue(
#| arr : ArrayView[A],
#|) -> PriorityQueue[A] {
#| guard arr is [a0, ..] else { return new_priority_queue() }
#| let len = arr.length()
#| for i = 1, acc = { content: a0, sibling: None, child: None, } {
#| if i < len {
#| continue i + 1,
#| meld(acc, { content: arr[i], sibling: None, child: None, })
#| } else {
#| break { len, top: Some(acc), }
#| }
#| }
#|}
#|fn[A] copy_node(x : Node[A]?) -> Node[A]? {
#| match x {
#| None => None
#| Some(root) => {
#| let cloned_root : Node[A] = {
#| content: root.content,
#| sibling: None,
#| child: None,
#| }
#| let stack : Array[(Node[A], Node[A])] = [(root, cloned_root)]
#| while stack.pop() is Some((orig, clone)) {
#| if orig.child is Some(child) {
#| let cloned_child : Node[A] = {
#| content: child.content,
#| sibling: None,
#| child: None,
#| }
#| clone.child = Some(cloned_child)
#| stack.push((child, cloned_child))
#| }
#| if orig.sibling is Some(sib) {
#| let cloned_sib : Node[A] = {
#| content: sib.content,
#| sibling: None,
#| child: None,
#| }
#| clone.sibling = Some(cloned_sib)
#| stack.push((sib, cloned_sib))
#| }
#| }
#| Some(cloned_root)
#| }
#| }
#|}
#|#alias(clone, deprecated)
#|pub fn[A] PriorityQueue::copy(self : PriorityQueue[A]) -> PriorityQueue[A] {
#| { len: self.len, top: copy_node(self.top), }
#|}
#|pub fn[A : Compare] PriorityQueue::to_array(
#| self : PriorityQueue[A],
#|) -> Array[A] {
#| let arr = Array(capacity=self.len)
#| let stack : Array[Node[A]?] = [self.top]
#| while stack.pop() is Some(node) {
#| match node {
#| None => ()
#| Some({ content, sibling, child, }) => {
#| arr.push(content)
#| stack.push(sibling)
#| stack.push(child)
#| }
#| }
#| }
#| arr.sort()
#| arr.rev_in_place()
#| arr
#|}
#|#alias(iterator, deprecated)
#|pub fn[A : Compare] PriorityQueue::iter(self : PriorityQueue[A]) -> Iter[A] {
#| self.to_array().iter()
#|}
#|pub impl[A : @json.ToJson + Compare] @json.ToJson for PriorityQueue[A] with fn to_json(
#| self,
#|) {
#| [
#| for x in self => x
#| ]
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Compare] PriorityQueue::from_iter(
#| iter : Iter[K],
#|) -> PriorityQueue[K] {
#| let s = new_priority_queue()
#| while iter.next() is Some(e) {
#| s.push(e)
#| }
#| s
#|}
#|#owned(x, y)
#|fn[A : Compare] meld(x : Node[A], y : Node[A]) -> Node[A] {
#| if x.content > y.content {
#| y.sibling = x.child
#| x.child = Some(y)
#| x
#| } else {
#| x.sibling = y.child
#| y.child = Some(x)
#| y
#| }
#|}
#|#owned(x)
#|fn[A : Compare] merges(x : Node[A]?) -> Node[A]? {
#| let (x, acc) = match x {
#| None => return None
#| Some({ sibling: None, .. }) as x => return x
#| Some({ sibling: Some({ sibling: s2, .. } as s1), .. } as x) => {
#| x.sibling = None
#| s1.sibling = None
#| (s2, meld(x, s1))
#| }
#| }
#| for siblings = x, merged = acc {
#| match (siblings, merged) {
#| (None, acc) => break Some(acc)
#| (Some({ sibling: None, .. } as x), acc) => break Some(meld(acc, x))
#| (Some({ sibling: Some({ sibling: s2, .. } as s1), .. } as x), acc) => {
#| x.sibling = None
#| s1.sibling = None
#| continue s2, meld(acc, meld(x, s1))
#| }
#| }
#| }
#|}
#|pub fn[A] PriorityQueue::length(self : PriorityQueue[A]) -> Int32 {
#| self.len
#|}
#|#internal(unsafe, "Panic if the queue is empty.")
#|#doc(hidden)
#|pub fn[A : Compare] PriorityQueue::unsafe_pop(self : PriorityQueue[A]) -> Unit {
#| self.top = match self.top {
#| None => abort("The PriorityQueue is empty!")
#| Some({ child, .. }) => merges(child)
#| }
#| self.len -= 1
#|}
#|pub fn[A : Compare] PriorityQueue::pop(self : PriorityQueue[A]) -> A? {
#| match self.top {
#| None => None
#| Some({ content, child, .. }) => {
#| self.len -= 1
#| self.top = merges(child)
#| Some(content)
#| }
#| }
#|}
#|#owned(value)
#|pub fn[A : Compare] PriorityQueue::push(
#| self : PriorityQueue[A],
#| value : A,
#|) -> Unit {
#| let x = { content: value, sibling: None, child: None, }
#| self.top = match self.top {
#| None => Some(x)
#| Some(top) => Some(meld(top, x))
#| }
#| self.len += 1
#|}
#|pub fn[A] PriorityQueue::peek(self : PriorityQueue[A]) -> A? {
#| match self.top {
#| None => None
#| Some({ content, .. }) => Some(content)
#| }
#|}
#|pub fn[A] PriorityQueue::clear(self : PriorityQueue[A]) -> Unit {
#| self.top = None
#| self.len = 0
#|}
#|pub fn[A] PriorityQueue::is_empty(self : PriorityQueue[A]) -> Bool {
#| self.len == 0
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show + Compare] Show for PriorityQueue[A]
#|pub impl[A : Show + Compare] Show for PriorityQueue[A] with fn output(
#| self,
#| logger,
#|) {
#| logger.write_iter(
#| self.iter(),
#| prefix="@priority_queue.from_array([",
#| suffix="])",
#| )
#|}
#|pub impl[K] Default for PriorityQueue[K] with fn default() {
#| new_priority_queue()
#|}
),
"types.mbt": (
#|#unsafe_cycle_free
#|priv struct Node[A] {
#| content : A
#| mut sibling : Node[A]?
#| mut child : Node[A]?
#|}
#|struct PriorityQueue[A] {
#| mut len : Int32
#| mut top : Node[A]?
#|}
),
},
)
///|
let moonbitlang_core_queue_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/queue",
deps={},
files={
"debug.mbt": (
#|pub impl[A : @debug.Debug] @debug.Debug for Queue[A] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "Queue",
#| @debug.Repr::array([ for x in self => Repr(x) ]),
#| )
#|}
#|test "Debug for Queue" {
#| let q : Queue[Int32] = from_array([1, 2, 3])
#| @debug.debug_inspect(q, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `Queue([])` instead")
#|#as_free_fn(deprecated="Use `Queue([])` instead")
#|pub fn[A] Queue::new() -> Queue[A] {
#| new_queue()
#|}
),
"extends.mbt": (
#|pub extend Queue with Compare::{compare}
#|pub extend Queue with Eq::{equal}
#|pub extend Queue with Hash::{hash}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with @debug.Debug::{to_repr}
#|#deprecated("Use `@debug.Debug` instead of `Show` for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with Show::{to_string}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with Default::{default}
#|#deprecated("Use `Hash::hash_combine` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with Hash::{hash_combine}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with Eq::{not_equal}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with @json.FromJson::{from_json}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Queue with @json.ToJson::{to_json}
),
"queue.mbt": (
#|fn[A] new_queue() -> Queue[A] {
#| { inner: Deque([]), }
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[A] Queue::Queue(arr : ArrayView[A], capacity? : Int32) -> Queue[A] {
#| { inner: Deque(arr, capacity?), }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[A : Show] Show for Queue[A]
#|pub impl[A : Show] Show for Queue[A] with fn output(self, logger) {
#| logger.write_iter(self.iter(), prefix="@queue.from_array([", suffix="])")
#|}
#|pub impl[A : Eq] Eq for Queue[A] with fn equal(self, other) {
#| self.inner == other.inner
#|}
#|pub impl[A : Compare] Compare for Queue[A] with fn compare(self, other) {
#| self.inner.compare(other.inner)
#|}
#|pub impl[A : Hash] Hash for Queue[A] with fn hash_combine(self, hasher) {
#| Hash::hash_combine(self.inner, hasher)
#|}
#|pub fn[A] Queue::clear(self : Queue[A]) -> Unit {
#| self.inner.clear()
#|}
#|pub fn[A] Queue::shrink_to_fit(self : Queue[A]) -> Unit {
#| self.inner.shrink_to_fit()
#|}
#|pub fn[A] Queue::length(self : Queue[A]) -> Int32 {
#| self.inner.length()
#|}
#|pub fn[A] Queue::is_empty(self : Queue[A]) -> Bool {
#| self.inner.is_empty()
#|}
#|#owned(x)
#|pub fn[A] Queue::push(self : Queue[A], x : A) -> Unit {
#| self.inner.push_back(x)
#|}
#|#internal(unsafe, "Panics if the queue is empty.")
#|#doc(hidden)
#|#alias(peek_exn, deprecated)
#|pub fn[A] Queue::unsafe_peek(self : Queue[A]) -> A {
#| match self.inner.front() {
#| None => abort("Queue is empty")
#| Some(x) => x
#| }
#|}
#|pub fn[A] Queue::peek(self : Queue[A]) -> A? {
#| self.inner.front()
#|}
#|#internal(unsafe, "Panics if the queue is empty.")
#|#doc(hidden)
#|#alias(pop_exn, deprecated)
#|pub fn[A] Queue::unsafe_pop(self : Queue[A]) -> A {
#| match self.inner.pop_front() {
#| None => abort("Queue is empty")
#| Some(x) => x
#| }
#|}
#|pub fn[A] Queue::pop(self : Queue[A]) -> A? {
#| self.inner.pop_front()
#|}
#|#locals(f)
#|pub fn[A] Queue::each(self : Queue[A], f : (A) -> Unit raise?) -> Unit raise? {
#| for x in self.inner {
#| f(x)
#| }
#|}
#|#locals(f)
#|pub fn[A] Queue::eachi(
#| self : Queue[A],
#| f : (Int32, A) -> Unit raise?,
#|) -> Unit raise? {
#| for i, x in self.inner {
#| f(i, x)
#| }
#|}
#|#locals(f)
#|pub fn[A, B] Queue::fold(
#| self : Queue[A],
#| init~ : B,
#| f : (B, A) -> B raise?,
#|) -> B raise? {
#| let mut acc = init
#| for x in self.inner {
#| acc = f(acc, x)
#| }
#| acc
#|}
#|#alias(clone, deprecated)
#|pub fn[A] Queue::copy(self : Queue[A]) -> Queue[A] {
#| { inner: self.inner.copy(), }
#|}
#|pub fn[A] Queue::transfer(self : Queue[A], dst : Queue[A]) -> Unit {
#| if physical_equal(self.inner, dst.inner) {
#| return // no-op for self-transfer
#| }
#| dst.inner.append(self.inner)
#| self.inner.clear()
#|}
#|#alias(iterator, deprecated)
#|pub fn[A] Queue::iter(self : Queue[A]) -> Iter[A] {
#| self.inner.iter()
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[A] Queue::from_iter(iter : Iter[A]) -> Queue[A] {
#| { inner: @deque.from_iter(iter), }
#|}
#|test "from_array_2" {
#| let q = from_array([1, 2, 3, 4])
#| q.push(11)
#| @test.assert_eq(q, from_array([1, 2, 3, 4, 11]))
#| q.unsafe_pop() |> ignore
#| @test.assert_eq(q, from_array([2, 3, 4, 11]))
#|}
#|test "equal" {
#| let queue = from_array([1, 2, 3, 4])
#| let queue2 = from_array([1, 2, 3, 4])
#| let queue3 = from_array([1, 2, 3, 5])
#| assert_true(queue == queue2)
#| assert_false(queue == queue3)
#| queue.unsafe_pop() |> ignore
#| assert_false(queue == queue2)
#| @test.assert_eq(queue, from_array([2, 3, 4]))
#| assert_false(from_array([]) == from_array([1]))
#|}
#|test "push" {
#| let queue : Queue[Int32] = new_queue()
#| queue.push(1)
#| queue.push(2)
#| queue.push(3)
#| queue.push(1)
#| @test.assert_eq(queue.length(), 4)
#| @test.assert_eq(queue, from_array([1, 2, 3, 1]))
#|}
#|test "copy" {
#| let queue : Queue[Int32] = from_array([1, 2, 3, 4])
#| let queue2 : Queue[Int32] = queue.copy()
#| @test.assert_eq(queue2.length(), 4)
#| @test.assert_eq(queue2, from_array([1, 2, 3, 4]))
#| @test.assert_eq(queue.length(), 4)
#| @test.assert_eq(queue, from_array([1, 2, 3, 4]))
#|}
#|test "transfer" {
#| let queue : Queue[Int32] = from_array([1, 2, 3, 4])
#| let queue2 : Queue[Int32] = from_array([5, 6, 7, 8])
#| queue.transfer(queue2)
#| @test.assert_eq(queue.length(), 0)
#| @test.assert_eq(queue2.length(), 8)
#| @test.assert_eq(queue2, from_array([5, 6, 7, 8, 1, 2, 3, 4]))
#|}
#|pub impl[A] Default for Queue[A] with fn default() {
#| new_queue()
#|}
#|pub fn[A] Queue::to_array(self : Queue[A]) -> Array[A] {
#| self.inner.to_array()
#|}
#|pub impl[A : @json.ToJson] @json.ToJson for Queue[A] with fn to_json(self) {
#| @json.ToJson::to_json(self.inner)
#|}
#|pub impl[A : @json.FromJson] @json.FromJson for Queue[A] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(_) else {
#| raise path.error("@queue.from_json: expected array")
#| }
#| { inner: @json.FromJson::from_json(json, path), }
#|}
),
"types.mbt": (
#|#valtype
#|struct Queue[A] {
#| inner : @deque.Deque[A]
#|}
),
},
)
///|
let moonbitlang_core_quickcheck_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/quickcheck",
deps={},
files={
"arbitrary.mbt": (
#|pub(open) trait Arbitrary {
#| fn arbitrary(Int32, @splitmix.RandomState) -> Self
#|}
#|pub impl Arbitrary for Unit with fn arbitrary(_, _) {
#| ()
#|}
#|pub impl Arbitrary for Bool with fn arbitrary(_, rs) {
#| rs.next_double() < 0.5
#|}
#|pub impl Arbitrary for Byte with fn arbitrary(_, rs) {
#| rs.next_uint().to_byte()
#|}
#|pub impl Arbitrary for Bytes with fn arbitrary(size, rs) {
#| if size == 0 {
#| b""
#| } else {
#| let sz = rs.next_positive_int() % size
#| Bytes::makei(sz, _ => Arbitrary::arbitrary(size, rs))
#| }
#|}
#|pub impl Arbitrary for @float.Float with fn arbitrary(_, rs) {
#| rs.next_float()
#|}
#|pub impl Arbitrary for Double with fn arbitrary(_, rs) {
#| rs.next_double()
#|}
#|pub impl Arbitrary for Char with fn arbitrary(_, rs) {
#| if rs.next_uint() % 4 < 3 {
#| (rs.next_uint() % 128).reinterpret_as_int().to_char().unwrap()
#| } else {
#| let index = (rs.next_uint() % (0x110000 - 0x800)).reinterpret_as_int()
#| let code = if index < 0xD800 { index } else { index + 0x800 }
#| code.to_char().unwrap()
#| }
#|}
#|pub impl Arbitrary for String with fn arbitrary(size, rs) {
#| let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
#| let buf = StringBuilder(capacity=len * 2)
#| for i in 0.. {
#| guard i < len else { None }
#| let elem = X::arbitrary(i, rs)
#| i += 1
#| Some(elem)
#| })
#|}
#|pub fn[T : Arbitrary] gen(size? : Int32, state? : @splitmix.RandomState) -> T {
#| let size = size.unwrap_or(0)
#| let state = state.unwrap_or_else(() => {
#| (Default::default() : @splitmix.RandomState)
#| })
#| Arbitrary::arbitrary(size, state)
#|}
#|pub impl[X : Arbitrary] Arbitrary for X? with fn arbitrary(i, rs) {
#| if rs.next_double() < 0.3 {
#| None
#| } else {
#| Some(Arbitrary::arbitrary(i, rs))
#| }
#|}
#|pub impl[T : Arbitrary, E : Arbitrary] Arbitrary for Result[T, E] with fn arbitrary(
#| size,
#| rs,
#|) {
#| if Arbitrary::arbitrary(size, rs) {
#| Ok(T::arbitrary(size, rs))
#| } else {
#| Err(E::arbitrary(size, rs))
#| }
#|}
#|pub impl[X : Arbitrary] Arbitrary for FixedArray[X] with fn arbitrary(size, rs) {
#| let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
#| FixedArray::makei(len, i => X::arbitrary(i, rs))
#|}
#|pub impl[X : Arbitrary] Arbitrary for ReadOnlyArray[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Array[X] = Arbitrary::arbitrary(size, rs)
#| ReadOnlyArray::from_array(values)
#|}
#|pub impl[A : Arbitrary] Arbitrary for ArrayView[A] with fn arbitrary(size, rs) {
#| let arr : Array[A] = Arbitrary::arbitrary(size, rs)
#| arr
#|}
#|pub impl[X : Arbitrary] Arbitrary for Array[X] with fn arbitrary(size, rs) {
#| let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
#| Array::makei(len, x => X::arbitrary(x, rs))
#|}
#|pub impl[X : Arbitrary] Arbitrary for @ref.Ref[X] with fn arbitrary(size, rs) {
#| Ref(X::arbitrary(size, rs))
#|}
#|pub impl[A : Arbitrary, B : Arbitrary] Arbitrary for (A, B) with fn arbitrary(
#| size,
#| r0,
#|) {
#| let r1 = r0.split()
#| (Arbitrary::arbitrary(size, r0), Arbitrary::arbitrary(size, r1))
#|}
#|pub impl[A : Arbitrary, B : Arbitrary, C : Arbitrary] Arbitrary for (A, B, C) with fn arbitrary(
#| size,
#| r0,
#|) {
#| let r1 = r0.split()
#| let (v1, v2) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2)
#|}
#|pub impl[A : Arbitrary, B : Arbitrary, C : Arbitrary, D : Arbitrary] Arbitrary for (
#| A,
#| B,
#| C,
#| D,
#|) with fn arbitrary(size, r0) {
#| let r1 = r0.split()
#| let (v1, v2, v3) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2, v3)
#|}
#|pub impl[
#| A : Arbitrary,
#| B : Arbitrary,
#| C : Arbitrary,
#| D : Arbitrary,
#| E : Arbitrary,
#|] Arbitrary for (A, B, C, D, E) with fn arbitrary(size, r0) {
#| let r1 = r0.split()
#| let (v1, v2, v3, v4) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2, v3, v4)
#|}
#|pub impl[
#| A : Arbitrary,
#| B : Arbitrary,
#| C : Arbitrary,
#| D : Arbitrary,
#| E : Arbitrary,
#| F : Arbitrary,
#|] Arbitrary for (A, B, C, D, E, F) with fn arbitrary(size, r0) {
#| let r1 = r0.split()
#| let (v1, v2, v3, v4, v5) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2, v3, v4, v5)
#|}
#|pub impl[
#| A : Arbitrary,
#| B : Arbitrary,
#| C : Arbitrary,
#| D : Arbitrary,
#| E : Arbitrary,
#| F : Arbitrary,
#| G : Arbitrary,
#|] Arbitrary for (A, B, C, D, E, F, G) with fn arbitrary(size, r0) {
#| let r1 = r0.split()
#| let (v1, v2, v3, v4, v5, v6) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2, v3, v4, v5, v6)
#|}
#|pub impl[
#| A : Arbitrary,
#| B : Arbitrary,
#| C : Arbitrary,
#| D : Arbitrary,
#| E : Arbitrary,
#| F : Arbitrary,
#| G : Arbitrary,
#| H : Arbitrary,
#|] Arbitrary for (A, B, C, D, E, F, G, H) with fn arbitrary(size, r0) {
#| let r1 = r0.split()
#| let (v1, v2, v3, v4, v5, v6, v7) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2, v3, v4, v5, v6, v7)
#|}
#|pub impl[
#| A : Arbitrary,
#| B : Arbitrary,
#| C : Arbitrary,
#| D : Arbitrary,
#| E : Arbitrary,
#| F : Arbitrary,
#| G : Arbitrary,
#| H : Arbitrary,
#| I : Arbitrary,
#|] Arbitrary for (A, B, C, D, E, F, G, H, I) with fn arbitrary(size, r0) {
#| let r1 = r0.split()
#| let (v1, v2, v3, v4, v5, v6, v7, v8) = Arbitrary::arbitrary(size, r1)
#| (Arbitrary::arbitrary(size, r0), v1, v2, v3, v4, v5, v6, v7, v8)
#|}
),
"arbitrary_bigint.mbt": (
#|fn[T] @splitmix.RandomState::pick(
#| self : @splitmix.RandomState,
#| values : ReadOnlyArray[T],
#|) -> T {
#| let index = self.next_uint(limit=values.length().reinterpret_as_uint())
#| values[index.reinterpret_as_int()]
#|}
#|fn with_random_sign(
#| magnitude : @bigint.BigInt,
#| rs : @splitmix.RandomState,
#|) -> @bigint.BigInt {
#| if magnitude.is_zero() || rs.next_uint(limit=2U) == 0U {
#| magnitude
#| } else {
#| -magnitude
#| }
#|}
#|fn around(base : @bigint.BigInt, rs : @splitmix.RandomState) -> @bigint.BigInt {
#| with_random_sign(base + rs.pick([-1N, 0N, 1N]), rs)
#|}
#|fn random_bigint(size : Int32, rs : @splitmix.RandomState) -> @bigint.BigInt {
#| let chunk_limit = size.clamp(min=0, max=31) + 1
#| let chunk_count = for count in 1.. {
#| rs.next_uint(limit=256U).to_byte()
#| })
#| with_random_sign(@bigint.BigInt::from_octets(bytes), rs)
#|}
#|pub impl Arbitrary for @bigint.BigInt with fn arbitrary(size, rs) {
#| match rs.next_uint(limit=100U) {
#| 0..<35 => random_bigint(size, rs)
#| 35..<60 => rs.pick([0N, 1N, -1N, 2N, -2N, 10N, -10N])
#| 60..<85 =>
#| around(
#| 1N <<
#| rs.pick([0, 1, 7, 8, 15, 16, 31, 32, 52, 53, 63, 64, 127, 128, 255, 256]),
#| rs,
#| )
#| _ => {
#| let exponent = rs.pick([1, 2, 3, 6, 9, 18, 19, 38, 76, 100])
#| around(
#| Int(0).until(exponent).fold(init=1N, (power, _) => power * 10N),
#| rs,
#| )
#| }
#| }
#|}
),
"arbitrary_collections.mbt": (
#|pub impl[X : Arbitrary] Arbitrary for @list.List[X] with fn arbitrary(size, rs) {
#| let values : Array[X] = Arbitrary::arbitrary(size, rs)
#| List(values)
#|}
#|pub impl[X : Arbitrary] Arbitrary for @queue.Queue[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Iter[X] = Arbitrary::arbitrary(size, rs)
#| @queue.Queue::from_iter(values)
#|}
#|pub impl[X : Arbitrary] Arbitrary for @deque.Deque[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Array[X] = Arbitrary::arbitrary(size, rs)
#| Deque(values)
#|}
#|pub impl[K : Arbitrary + Hash + Eq, V : Arbitrary] Arbitrary for Map[K, V] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Iter[(K, V)] = Arbitrary::arbitrary(size, rs)
#| Map::from_iter(values)
#|}
#|pub impl[X : Arbitrary + Eq + Hash] Arbitrary for @set.Set[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Iter[X] = Arbitrary::arbitrary(size, rs)
#| @set.Set::from_iter(values)
#|}
#|pub impl[K : Arbitrary + Hash + Eq, V : Arbitrary] Arbitrary for @hashmap.HashMap[
#| K,
#| V,
#|] with fn arbitrary(size, rs) {
#| let values : Iter[(K, V)] = Arbitrary::arbitrary(size, rs)
#| @hashmap.HashMap::from_iter(values)
#|}
#|pub impl[X : Arbitrary + Eq + Hash] Arbitrary for @hashset.HashSet[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Iter[X] = Arbitrary::arbitrary(size, rs)
#| @hashset.HashSet::from_iter(values)
#|}
#|pub impl[K : Arbitrary + Compare, V : Arbitrary] Arbitrary for @sorted_map.SortedMap[
#| K,
#| V,
#|] with fn arbitrary(size, rs) {
#| let values : Iter[(K, V)] = Arbitrary::arbitrary(size, rs)
#| @sorted_map.SortedMap::from_iter(values)
#|}
#|pub impl[X : Arbitrary + Compare] Arbitrary for @sorted_set.SortedSet[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let values : Iter[X] = Arbitrary::arbitrary(size, rs)
#| @sorted_set.SortedSet::from_iter(values)
#|}
#|pub impl[X : Arbitrary + Compare] Arbitrary for @priority_queue.PriorityQueue[X] with fn arbitrary(
#| size,
#| rs,
#|) {
#| let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
#| let queue : @priority_queue.PriorityQueue[X] = PriorityQueue([])
#| for i in 0.. UInt32 {
#| match (bits, signed) {
#| (16, _) => 5U
#| (32, _) => 10U
#| (64, true) => 19U
#| (64, false) => 20U
#| _ => abort("arbitrary integer: unsupported bit width")
#| }
#|}
#|fn canonical_integer_bits(
#| mask : UInt64,
#| signed : Bool,
#| rs : @splitmix.RandomState,
#|) -> UInt64 {
#| guard signed else { rs.next_uint(limit=4U).to_uint64() }
#| match rs.next_uint(limit=5U) {
#| 0U => 0UL
#| 1U => 1UL
#| 2U => mask
#| 3U => 2UL
#| _ => mask - 1UL
#| }
#|}
#|fn apply_random_sign(
#| magnitude : UInt64,
#| signed : Bool,
#| rs : @splitmix.RandomState,
#|) -> UInt64 {
#| if signed && rs.next_uint(limit=2U) == 1U {
#| 0UL - magnitude
#| } else {
#| magnitude
#| }
#|}
#|fn boundary_delta(base : UInt64, rs : @splitmix.RandomState) -> UInt64 {
#| match rs.next_uint(limit=3U) {
#| 0U => base - 1UL
#| 1U => base
#| _ => base + 1UL
#| }
#|}
#|fn binary_boundary_bits(
#| bits : Int32,
#| signed : Bool,
#| rs : @splitmix.RandomState,
#|) -> UInt64 {
#| let exponent_count = if signed { bits - 1 } else { bits }
#| let exponent = rs
#| .next_uint(limit=exponent_count.reinterpret_as_uint())
#| .reinterpret_as_int()
#| let magnitude = boundary_delta(1UL << exponent, rs)
#| apply_random_sign(magnitude, signed, rs)
#|}
#|fn decimal_boundary_bits(
#| bits : Int32,
#| signed : Bool,
#| rs : @splitmix.RandomState,
#|) -> UInt64 {
#| let exponent = rs
#| .next_uint(limit=decimal_power_count(bits, signed))
#| .reinterpret_as_int()
#| let magnitude = boundary_delta(
#| Int(0).until(exponent).fold(init=1UL, (power, _) => power * 10UL),
#| rs,
#| )
#| apply_random_sign(magnitude, signed, rs)
#|}
#|fn extreme_integer_bits(
#| bits : Int32,
#| mask : UInt64,
#| signed : Bool,
#| rs : @splitmix.RandomState,
#|) -> UInt64 {
#| let choice = rs.next_uint(limit=4U)
#| if signed {
#| let sign_bit = 1UL << (bits - 1)
#| match choice {
#| 0U => sign_bit
#| 1U => sign_bit + 1UL
#| 2U => sign_bit - 2UL
#| _ => sign_bit - 1UL
#| }
#| } else {
#| match choice {
#| 0U => mask
#| 1U => mask - 1UL
#| 2U => mask - 2UL
#| _ => 1UL << (bits - 1)
#| }
#| }
#|}
#|fn gen_integer_bits(
#| bits : Int32,
#| signed : Bool,
#| rs : @splitmix.RandomState,
#|) -> UInt64 {
#| let mask = if bits == 64 {
#| 0xffff_ffff_ffff_ffffUL
#| } else {
#| (1UL << bits) - 1UL
#| }
#| let value = match rs.next_uint(limit=100U) {
#| 0..<40 => rs.next_uint64()
#| 40..<60 => canonical_integer_bits(mask, signed, rs)
#| 60..<80 => binary_boundary_bits(bits, signed, rs)
#| 80..<90 => decimal_boundary_bits(bits, signed, rs)
#| _ => extreme_integer_bits(bits, mask, signed, rs)
#| }
#| value & mask
#|}
#|pub impl Arbitrary for @int16.Int16 with fn arbitrary(_, rs) {
#| @int16.Int16::reinterpret_from_uint16(
#| gen_integer_bits(16, true, rs).to_uint16(),
#| )
#|}
#|pub impl Arbitrary for UInt16 with fn arbitrary(_, rs) {
#| gen_integer_bits(16, false, rs).to_uint16()
#|}
#|pub impl Arbitrary for Int32 with fn arbitrary(_, rs) {
#| gen_integer_bits(32, true, rs).to_uint().reinterpret_as_int()
#|}
#|pub impl Arbitrary for UInt32 with fn arbitrary(_, rs) {
#| gen_integer_bits(32, false, rs).to_uint()
#|}
#|pub impl Arbitrary for Int64 with fn arbitrary(_, rs) {
#| gen_integer_bits(64, true, rs).reinterpret_as_int64()
#|}
#|pub impl Arbitrary for UInt64 with fn arbitrary(_, rs) {
#| gen_integer_bits(64, false, rs)
#|}
),
"driver.mbt": (
#|fn size_at(
#| index : UInt32,
#| count : UInt32,
#| max_size : UInt32,
#| recent_discards : UInt32,
#|) -> Int32 {
#| let base_size = if count <= 1 {
#| 0UL
#| } else {
#| index.to_uint64() * max_size.to_uint64() / (count - 1).to_uint64()
#| }
#| let size = base_size + recent_discards.to_uint64()
#| let limit = if max_size.to_uint64() > @int.MAX_VALUE.to_uint64() {
#| @int.MAX_VALUE.to_uint64()
#| } else {
#| max_size.to_uint64()
#| }
#| if size > limit {
#| limit.to_int()
#| } else {
#| size.to_int()
#| }
#|}
#|priv enum CaseOutcome {
#| Passed
#| Discarded
#| Falsified
#| Raised(Error)
#|}
#|enum QuickCheckReport[A] {
#| Passed(tests~ : UInt32, observations~ : ObservationSummary)
#| GaveUp(
#| tests~ : UInt32,
#| discarded~ : UInt32,
#| observations~ : ObservationSummary,
#| )
#| Falsified(
#| counterexample~ : A,
#| context~ : String?,
#| tests~ : UInt32,
#| size~ : Int32,
#| shrinks~ : UInt32,
#| shrink_attempts~ : UInt32,
#| observations~ : ObservationSummary,
#| )
#| Raised(
#| counterexample~ : A,
#| error~ : Error,
#| context~ : String?,
#| tests~ : UInt32,
#| size~ : Int32,
#| shrinks~ : UInt32,
#| shrink_attempts~ : UInt32,
#| observations~ : ObservationSummary,
#| )
#|}
#|pub impl[A : @debug.Debug] @debug.Debug for QuickCheckReport[A] with fn to_repr(
#| self,
#|) {
#| match self {
#| Passed(tests~, observations~) =>
#| @debug.Repr::ctor(
#| "Passed",
#| [
#| (Some("tests"), Repr(tests)),
#| ..if !observations.is_empty() {
#| [(Some("observations"), @debug.Repr(observations))]
#| },
#| ],
#| )
#| GaveUp(tests~, discarded~, observations~) =>
#| @debug.Repr::ctor(
#| "GaveUp",
#| [
#| (Some("tests"), Repr(tests)),
#| (Some("discarded"), Repr(discarded)),
#| ..if !observations.is_empty() {
#| [(Some("observations"), @debug.Repr(observations))]
#| },
#| ],
#| )
#| Falsified(
#| counterexample~,
#| context~,
#| tests~,
#| size~,
#| shrinks~,
#| shrink_attempts~,
#| observations~,
#| ) =>
#| @debug.Repr::ctor(
#| "Falsified",
#| [
#| (Some("counterexample"), Repr(counterexample)),
#| ..if context is Some(text) {
#| [(Some("context"), @debug.Repr(text))]
#| },
#| (Some("tests"), Repr(tests)),
#| (Some("size"), Repr(size)),
#| (Some("shrinks"), Repr(shrinks)),
#| (Some("shrink_attempts"), Repr(shrink_attempts)),
#| ..if !observations.is_empty() {
#| [(Some("observations"), @debug.Repr(observations))]
#| },
#| ],
#| )
#| Raised(
#| counterexample~,
#| error~,
#| context~,
#| tests~,
#| size~,
#| shrinks~,
#| shrink_attempts~,
#| observations~,
#| ) =>
#| @debug.Repr::ctor(
#| "Raised",
#| [
#| (Some("counterexample"), Repr(counterexample)),
#| ..if context is Some(text) {
#| [(Some("context"), @debug.Repr(text))]
#| },
#| (Some("error"), Repr(error)),
#| (Some("tests"), Repr(tests)),
#| (Some("size"), Repr(size)),
#| (Some("shrinks"), Repr(shrinks)),
#| (Some("shrink_attempts"), Repr(shrink_attempts)),
#| ..if !observations.is_empty() {
#| [(Some("observations"), @debug.Repr(observations))]
#| },
#| ],
#| )
#| }
#|}
#|#deprecated("Use `@debug.Debug::to_repr` instead")
#|#doc(hidden)
#|pub extend QuickCheckReport with @debug.Debug::{to_repr}
#|fn[A] evaluate_case(
#| property : (A) -> Bool raise?,
#| filter : (A) -> Bool,
#| input : A,
#|) -> CaseOutcome {
#| guard filter(input) else { Discarded }
#| let invoke : (A) -> Bool raise = a => property(a)
#| try invoke(input) catch {
#| err => Raised(err)
#| } noraise {
#| true => Passed
#| false => Falsified
#| }
#|}
#|impl Eq for CaseOutcome with fn equal(expected, actual) {
#| match (expected, actual) {
#| (Falsified, Falsified) | (Raised(_), Raised(_)) => true
#| _ => false
#| }
#|}
#|fn[A : @shrink.Shrink] shrink_failure(
#| property : (A) -> Bool raise?,
#| filter : (A) -> Bool,
#| input : A,
#| initial : CaseOutcome,
#| max_shrinks : UInt32,
#|) -> (A, CaseOutcome, UInt32, UInt32) {
#| let mut current = input
#| let mut current_outcome = initial
#| let mut successful = 0U
#| let mut attempted = 0U
#| let mut found = true
#| while attempted < max_shrinks && found {
#| found = false
#| let candidates = @shrink.Shrink::shrink(current)
#| while attempted < max_shrinks && candidates.next() is Some(candidate) {
#| attempted = attempted + 1
#| let outcome = evaluate_case(property, filter, candidate)
#| if initial == outcome {
#| current = candidate
#| current_outcome = outcome
#| successful = successful + 1
#| found = true
#| break
#| }
#| }
#| }
#| (current, current_outcome, successful, attempted)
#|}
#|fn context_line(context : String?) -> String {
#| match context {
#| Some(text) => "\ncontext: \{text}"
#| None => ""
#| }
#|}
#|fn[A : @debug.Debug] failure_report(report : QuickCheckReport[A]) -> String {
#| match report {
#| Passed(..) => abort("quickcheck internal error: a passed property reported")
#| GaveUp(tests~, discarded~, observations~) =>
#| (
#| $|QuickCheck gave up after \{tests} test(s)
#| $|discarded: \{discarded}\{observations.report(tests, prefix="\n")}
#| )
#| Falsified(
#| counterexample~,
#| context~,
#| tests~,
#| size~,
#| shrinks~,
#| shrink_attempts~,
#| observations~,
#| ) =>
#| (
#| $|QuickCheck falsified after \{tests} test(s)
#| $|counterexample: \{@debug.to_string(counterexample)}\{context_line(context)}
#| $|size: \{size}
#| $|shrinks: \{shrinks} successful, \{shrink_attempts} attempted\{observations.report(tests - 1U, prefix="\n")}
#| )
#| Raised(
#| counterexample~,
#| error~,
#| context~,
#| tests~,
#| size~,
#| shrinks~,
#| shrink_attempts~,
#| observations~,
#| ) =>
#| (
#| $|QuickCheck property raised after \{tests} test(s)
#| $|counterexample: \{@debug.to_string(counterexample)}\{context_line(context)}
#| $|error: \{@debug.to_string(error)}
#| $|size: \{size}
#| $|shrinks: \{shrinks} successful, \{shrink_attempts} attempted\{observations.report(tests - 1U, prefix="\n")}
#| )
#| }
#|}
#|pub fn[A : Arbitrary + @shrink.Shrink] report(
#| property : (A) -> Bool raise?,
#| filter~ : (A) -> Bool = _ => true,
#| observe~ : (A) -> Observations = _ => [],
#| counterexample_context? : (A) -> String,
#| count~ : UInt32 = 100,
#| max_size~ : UInt32 = 100,
#| max_shrinks~ : UInt32 = 100,
#| discard_ratio~ : UInt32 = 10,
#| seed~ : UInt64 = 37,
#|) -> QuickCheckReport[A] {
#| let state = @splitmix.RandomState(seed~)
#| let observations = ObservationSummary::empty()
#| for tests = 0U, discarded = 0U, recent_discards = 0U; tests < count; {
#| let size = size_at(tests, count, max_size, recent_discards)
#| let input : A = Arbitrary::arbitrary(size, state.split())
#| let outcome = evaluate_case(property, filter, input)
#| match outcome {
#| Passed => {
#| observations.record(observe(input))
#| continue tests + 1, discarded, 0U
#| }
#| Discarded => {
#| let next_discarded = discarded + 1
#| if discard_ratio == 0 || next_discarded / discard_ratio >= count {
#| return GaveUp(tests~, discarded=next_discarded, observations~)
#| }
#| continue tests, next_discarded, recent_discards + 1
#| }
#| _ => {
#| let completed_tests = tests + 1
#| let (counterexample, outcome, shrinks, shrink_attempts) = shrink_failure(
#| property, filter, input, outcome, max_shrinks,
#| )
#| let context = counterexample_context.map(render => {
#| render(counterexample)
#| })
#| return match outcome {
#| Falsified =>
#| Falsified(
#| counterexample~,
#| context~,
#| tests=completed_tests,
#| size~,
#| shrinks~,
#| shrink_attempts~,
#| observations~,
#| )
#| Raised(error) =>
#| Raised(
#| counterexample~,
#| error~,
#| context~,
#| tests=completed_tests,
#| size~,
#| shrinks~,
#| shrink_attempts~,
#| observations~,
#| )
#| Passed | Discarded =>
#| abort(
#| "quickcheck internal error: unexpected non-failure after shrinking",
#| )
#| }
#| }
#| }
#| } nobreak {
#| Passed(tests=count, observations~)
#| }
#|}
#|#callsite(autofill(loc))
#|pub fn[A : Arbitrary + @shrink.Shrink + @debug.Debug] check(
#| property : (A) -> Bool raise?,
#| filter~ : (A) -> Bool = _ => true,
#| observe~ : (A) -> Observations = _ => [],
#| counterexample_context? : (A) -> String,
#| count~ : UInt32 = 100,
#| max_size~ : UInt32 = 100,
#| max_shrinks~ : UInt32 = 100,
#| discard_ratio~ : UInt32 = 10,
#| seed~ : UInt64 = 37,
#| loc~ : SourceLoc,
#|) -> Unit raise {
#| let result = report(
#| property,
#| filter~,
#| observe~,
#| counterexample_context?,
#| count~,
#| max_size~,
#| max_shrinks~,
#| discard_ratio~,
#| seed~,
#| )
#| match result {
#| Passed(observations~, ..) if observations.is_empty() => ()
#| Passed(tests~, observations~) => println(observations.report(tests))
#| GaveUp(..) | Falsified(..) | Raised(..) =>
#| fail(failure_report(result), loc~)
#| }
#|}
),
"facade.mbt": (
#|pub using @shrink {trait Shrink}
),
"generator.mbt": (
#|pub using @splitmix {type RandomState}
#|struct Generator[T] {
#| generate : (Int32, RandomState) -> T
#|}
#|pub fn[T] Generator::Generator(
#| generate : (Int32, RandomState) -> T,
#|) -> Generator[T] {
#| { generate, }
#|}
#|pub fn[T : Arbitrary] spawn() -> Generator[T] {
#| Generator(Arbitrary::arbitrary)
#|}
#|pub fn[T] Generator::run(
#| self : Generator[T],
#| size : Int32,
#| state : RandomState,
#|) -> T {
#| (self.generate)(size, state)
#|}
#|pub fn[T] Generator::sample(
#| self : Generator[T],
#| size~ : Int32 = 100,
#| seed~ : UInt64 = 37,
#|) -> T {
#| self.run(size, RandomState(seed~))
#|}
#|pub fn[T] Generator::samples(
#| self : Generator[T],
#| count~ : Int32 = 10,
#| size~ : Int32 = 100,
#| seed~ : UInt64 = 37,
#|) -> Array[T] {
#| let state = @splitmix.RandomState(seed~)
#| Array::makei(count, _ => self.run(size, state))
#|}
#|pub fn[T] pure(value : T) -> Generator[T] {
#| Generator((_, _) => value)
#|}
#|pub fn[T, U] Generator::map(
#| self : Generator[T],
#| transform : (T) -> U,
#|) -> Generator[U] {
#| Generator((size, state) => transform(self.run(size, state)))
#|}
#|pub fn[T, U] Generator::flat_map(
#| self : Generator[T],
#| transform : (T) -> Generator[U],
#|) -> Generator[U] {
#| Generator((size, state) => {
#| let next_state = state.split()
#| transform(self.run(size, state)).run(size, next_state)
#| })
#|}
#|pub fn[T, U] Generator::zip(
#| self : Generator[T],
#| other : Generator[U],
#|) -> Generator[(T, U)] {
#| self.zip_with(other, (first, second) => (first, second))
#|}
#|pub fn[T, U, V] Generator::zip_with(
#| self : Generator[T],
#| other : Generator[U],
#| combine : (T, U) -> V,
#|) -> Generator[V] {
#| Generator((size, state) => {
#| let other_state = state.split()
#| combine(self.run(size, state), other.run(size, other_state))
#| })
#|}
#|pub fn[T, U, V, W] Generator::zip_with3(
#| self : Generator[T],
#| second : Generator[U],
#| third : Generator[V],
#| combine : (T, U, V) -> W,
#|) -> Generator[W] {
#| Generator((size, state) => {
#| let second_state = state.split()
#| let third_state = second_state.split()
#| combine(
#| self.run(size, state),
#| second.run(size, second_state),
#| third.run(size, third_state),
#| )
#| })
#|}
#|pub fn[T] sized(create : (Int32) -> Generator[T]) -> Generator[T] {
#| Generator((size, state) => create(size).run(size, state))
#|}
#|pub fn[T] Generator::scale(
#| self : Generator[T],
#| transform : (Int32) -> Int32,
#|) -> Generator[T] {
#| Generator((size, state) => self.run(transform(size), state))
#|}
#|pub fn[T] Generator::resize(self : Generator[T], size : Int32) -> Generator[T] {
#| self.scale(_ => size)
#|}
#|#label_migration(inclusive, fill=true, msg="Pass inclusive=false to keep the previous int_range behavior")
#|pub fn int_range(
#| lower : Int32,
#| upper : Int32,
#| inclusive~ : Bool = false,
#|) -> Generator[Int32] {
#| guard lower < upper else {
#| if lower == upper {
#| return pure(lower)
#| }
#| abort("int_range: lower bound exceeds upper bound")
#| }
#| let span = upper.to_int64() - lower.to_int64()
#| let width = if inclusive { span + 1L } else { span }
#| Generator((_, state) => {
#| let offset = state.next_uint64(limit=width.reinterpret_as_uint64())
#| (lower.to_int64() + offset.reinterpret_as_int64()).to_int()
#| })
#|}
#|#label_migration(inclusive, fill=true, msg="Pass inclusive=true to keep the previous char_range behavior")
#|pub fn char_range(
#| lower : Char,
#| upper : Char,
#| inclusive~ : Bool = true,
#|) -> Generator[Char] {
#| let lo = lower.to_int()
#| let hi = upper.to_int()
#| fn is_surrogate(code : Int32) -> Bool {
#| code >= 0xD800 && code <= 0xDFFF
#| }
#| guard lo <= hi else { abort("char_range: lower bound exceeds upper bound") }
#| guard lo >= 0 && hi <= 0x10FFFF && !is_surrogate(lo) && !is_surrogate(hi) else {
#| abort("char_range: bounds must be Unicode scalar values")
#| }
#| let gap = if lo < 0xD800 && hi > 0xDFFF { 0x800 } else { 0 }
#| let end = hi - gap + (if inclusive { 1 } else { 0 })
#| int_range(lo, end, inclusive=false).map(code => {
#| let code = if code >= 0xD800 { code + gap } else { code }
#| code.to_char().unwrap()
#| })
#|}
#|pub fn[T] one_of(generators : Array[Generator[T]]) -> Generator[T] {
#| guard !generators.is_empty() else { abort("one_of: empty array") }
#| int_range(0, generators.length(), inclusive=false).flat_map(index => {
#| generators[index]
#| })
#|}
#|pub fn[T] elements(values : Array[T]) -> Generator[T] {
#| guard !values.is_empty() else { abort("elements: empty array") }
#| int_range(0, values.length(), inclusive=false).map(index => values[index])
#|}
#|pub fn[T] frequency(generators : Array[(UInt32, Generator[T])]) -> Generator[T] {
#| guard !generators.is_empty() else { abort("frequency: empty array") }
#| let total = for pair in generators; total = UInt64(0) {
#| let (weight, _) = pair
#| continue total + weight.to_uint64()
#| } nobreak {
#| total
#| }
#| guard total > 0 else { abort("frequency: total weight is zero") }
#| Generator((size, state) => {
#| let choice = state.next_uint64(limit=total)
#| for pair in generators; remaining = choice {
#| let (weight, generator) = pair
#| if remaining < weight.to_uint64() {
#| break generator.run(size, state)
#| }
#| continue remaining - weight.to_uint64()
#| } nobreak {
#| abort("frequency: invalid weights")
#| }
#| })
#|}
#|pub fn[T] Generator::array_with_size(
#| self : Generator[T],
#| size : Int32,
#|) -> Generator[Array[T]] {
#| guard size >= 0 else { abort("array_with_size: negative size") }
#| Generator((sample_size, state) => {
#| Array::makei(size, _ => self.run(sample_size, state))
#| })
#|}
),
"observation.mbt": (
#|#derive(@debug.Debug)
#|enum Observation {
#| Label(String)
#| Class(label~ : String, hit~ : Bool)
#|}
#|type Observations = Array[Observation]
#|priv struct ObservationSummary {
#| labels : Map[@list.List[String], UInt32]
#| classes : Map[String, UInt32]
#|}
#|impl @debug.Debug for ObservationSummary with fn to_repr(self) {
#| @debug.Repr::record(
#| Map(
#| [
#| ..if !self.labels.is_empty() {
#| [("labels", @debug.Repr(self.labels))]
#| },
#| ..if !self.classes.is_empty() {
#| [("classes", @debug.Repr(self.classes))]
#| },
#| ],
#| ),
#| )
#|}
#|pub impl Show for Observation with fn output(self, logger) {
#| match self {
#| Label(value) => logger.write_string(value)
#| Class(label~, ..) => logger.write_string(label)
#| }
#|}
#|#deprecated("Use `@debug.Debug::to_repr` instead")
#|#doc(hidden)
#|pub extend Observation with @debug.Debug::{to_repr}
#|pub fn label(value : String) -> Observation {
#| Label(value)
#|}
#|pub fn classify(condition : Bool, label : String) -> Observation {
#| Class(label~, hit=condition)
#|}
#|pub fn[T : @debug.Debug] collect(value : T) -> Observation {
#| label(@debug.to_string(value))
#|}
#|fn ObservationSummary::empty() -> ObservationSummary {
#| { labels: {}, classes: {}, }
#|}
#|fn ObservationSummary::is_empty(self : ObservationSummary) -> Bool {
#| self.labels.is_empty() && self.classes.is_empty()
#|}
#|fn ObservationSummary::record(
#| self : ObservationSummary,
#| observations : Observations,
#|) -> Unit {
#| let labels = []
#| let classes : Map[String, Bool] = Map([])
#| for observation in observations {
#| match observation {
#| Label(value) => labels.push(value)
#| Class(label~, hit~) =>
#| classes[label] = classes.get_or_default(label, false) || hit
#| }
#| }
#| labels.sort()
#| labels.dedup()
#| if !labels.is_empty() {
#| let key = @list.List(labels)
#| self.labels[key] = self.labels.get_or_default(key, 0U) + 1U
#| }
#| for label, hit in classes {
#| let increment = if hit { 1U } else { 0U }
#| self.classes[label] = self.classes.get_or_default(label, 0U) + increment
#| }
#|}
#|#derive(Eq)
#|priv struct ObservationRow {
#| count : UInt32
#| name : String
#|}
#|impl Compare for ObservationRow with fn compare(
#| left : ObservationRow,
#| right : ObservationRow,
#|) {
#| match Compare::compare(right.count, left.count) {
#| 0 => Compare::compare(left.name, right.name)
#| order => order
#| }
#|}
#|fn percentage(count : UInt32, total : UInt32) -> UInt32 {
#| guard total > 0U else { return 0U }
#| let total = total.to_uint64()
#| ((count.to_uint64() * 100UL + total / 2UL) / total).to_uint()
#|}
#|fn write_observation_rows(
#| builder : StringBuilder,
#| heading : String,
#| rows : Array[ObservationRow],
#| total : UInt32,
#| count_width : Int32,
#|) -> Unit {
#| guard !rows.is_empty() else { return }
#| rows.sort_by(Compare::compare)
#| builder <+ "\n \{heading}:"
#| for row in rows {
#| let count = "\{row.count}".pad_start(count_width, ' ')
#| let percent = "\{percentage(row.count, total)}".pad_start(3, ' ')
#| builder <+ "\n \{count} \{percent}% \{row.name}"
#| }
#|}
#|fn ObservationSummary::report(
#| self : ObservationSummary,
#| total : UInt32,
#| prefix~ : String = "",
#|) -> String {
#| guard !self.is_empty() else { return "" }
#| let label_rows = [
#| for labels, count in self.labels => {
#| { count, name: labels.iter().join(", "), }
#| }
#| ]
#| let class_rows = [ for name, count in self.classes => { count, name, } ]
#| let builder = StringBuilder()
#| builder <+ "\{prefix}observations:"
#| let count_width = "\{total}".length()
#| write_observation_rows(builder, "labels", label_rows, total, count_width)
#| write_observation_rows(builder, "classes", class_rows, total, count_width)
#| builder.to_string()
#|}
#|pub extend Observation with Show::{to_string}
),
"utils.mbt": (
#|pub fn[X : Arbitrary] samples(x : Int32) -> Array[X] {
#| let rs = @splitmix.RandomState()
#| let array = Array::make(x, X::arbitrary(0, rs))
#| for i in 1.. Iter[@list.List[T]] {
#| let mut prefix : @list.List[T] = List([])
#| let mut remaining = xs
#| Iter(fn() {
#| match remaining {
#| Empty => None
#| More(x, tail~) => {
#| let entry = (prefix, x, tail)
#| prefix = prefix.prepend(x)
#| remaining = tail
#| Some(entry)
#| }
#| }
#| }).flat_map(entry => {
#| let (prefix, x, tail) = entry
#| Shrink::shrink(x).map(x_ => prefix.rev_concat(tail.prepend(x_)))
#| })
#|}
#|pub impl[T : Shrink] Shrink for @list.List[T] with fn shrink(xs) {
#| let n = xs.length()
#| [
#| for k = n / 2; k > 0; k = k / 2 => k
#| ]
#| .rev_iter()
#| .flat_map(k => removes_list(k, n, xs))
#| .concat(deferred(() => shrink_list_elements(xs)))
#|}
#|pub impl[T : Shrink] Shrink for @queue.Queue[T] with fn shrink(xs) {
#| Shrink::shrink(xs.iter().collect()).map(candidate => Queue(candidate))
#|}
#|pub impl[T : Shrink] Shrink for @deque.Deque[T] with fn shrink(xs) {
#| Shrink::shrink(xs.to_array()).map(candidate => Deque(candidate))
#|}
#|pub impl[T : Shrink] Shrink for @immut_vector.Vector[T] with fn shrink(xs) {
#| Shrink::shrink(xs.to_array()).map(candidate => Vector(candidate))
#|}
#|pub impl[T : Shrink + Eq + Hash] Shrink for @hashset.HashSet[T] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.to_array()).map(candidate => HashSet(candidate))
#|}
#|pub impl[T : Shrink + Compare] Shrink for @sorted_set.SortedSet[T] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.to_array()).map(candidate => SortedSet(candidate))
#|}
#|pub impl[T : Shrink + Eq + Hash] Shrink for @immut_hashset.HashSet[T] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.iter().collect()).map(candidate => HashSet(candidate))
#|}
#|pub impl[T : Shrink + Compare] Shrink for @immut_sorted_set.SortedSet[T] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.to_array()).map(candidate => SortedSet(candidate))
#|}
#|pub impl[T : Shrink + Eq + Hash] Shrink for @set.Set[T] with fn shrink(xs) {
#| Shrink::shrink(xs.to_array()).map(candidate => Set(candidate))
#|}
#|pub impl[K : Shrink + Hash + Eq, V : Shrink] Shrink for Map[K, V] with fn shrink(
#| xs,
#|) {
#| let entries = xs.to_array()
#| Shrink::shrink(entries).map(candidate => Map(candidate))
#|}
#|pub impl[T : Shrink + Compare] Shrink for @priority_queue.PriorityQueue[T] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.to_array()).map(candidate => PriorityQueue(candidate))
#|}
#|pub impl[T : Shrink + Compare] Shrink for @immut_priority_queue.PriorityQueue[T] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.to_array()).map(candidate => PriorityQueue(candidate))
#|}
#|pub impl[K : Shrink + Hash + Eq, V : Shrink] Shrink for @hashmap.HashMap[K, V] with fn shrink(
#| xs,
#|) {
#| Shrink::shrink(xs.to_array()).map(candidate => HashMap(candidate))
#|}
#|pub impl[K : Shrink + Compare, V : Shrink] Shrink for @sorted_map.SortedMap[
#| K,
#| V,
#|] with fn shrink(xs) {
#| Shrink::shrink(xs.to_array()).map(candidate => SortedMap(candidate))
#|}
#|pub impl[K : Shrink + Eq + Hash, V : Shrink] Shrink for @immut_hashmap.HashMap[
#| K,
#| V,
#|] with fn shrink(xs) {
#| Shrink::shrink(xs.to_array()).map(candidate => HashMap(candidate))
#|}
#|pub impl[K : Shrink + Compare, V : Shrink] Shrink for @immut_sorted_map.SortedMap[
#| K,
#| V,
#|] with fn shrink(xs) {
#| Shrink::shrink(xs.to_array()).map(candidate => SortedMap(candidate))
#|}
),
"composite.mbt": (
#|pub impl[T : Shrink] Shrink for T? with fn shrink(x) {
#| match x {
#| None => [||]
#| Some(v) => Shrink::shrink(v).map(v1 => Some(v1)).concat([|None|])
#| }
#|}
#|pub impl[T : Shrink, E : Shrink] Shrink for Result[T, E] with fn shrink(x) {
#| match x {
#| Ok(v) => Shrink::shrink(v).map(v1 => Ok(v1))
#| Err(e) => Shrink::shrink(e).map(e1 => Err(e1))
#| }
#|}
#|fn[X : Shrink] shrink_array_elements(xs : ArrayView[X]) -> Iter[Array[X]] {
#| xs
#| .iter2()
#| .iter()
#| .flat_map(entry => {
#| let (i, x) = entry
#| X::shrink(x).map(x_ => {
#| let candidate = xs.to_owned()
#| candidate[i] = x_
#| candidate
#| })
#| })
#|}
#|pub impl[X : Shrink] Shrink for Array[X] with fn shrink(xs) {
#| let view = xs[:]
#| let n = view.length()
#| [
#| for k = n; k > 0; k = k / 2 => k
#| ]
#| .iter()
#| .flat_map(k => removes_array(k, n, xs))
#| .concat(deferred(() => shrink_array_elements(view)))
#|}
#|pub impl[X : Shrink] Shrink for FixedArray[X] with fn shrink(xs) {
#| Shrink::shrink(xs[:].to_owned()).map(candidate => {
#| FixedArray::from_array(candidate)
#| })
#|}
#|pub impl[X : Shrink] Shrink for ReadOnlyArray[X] with fn shrink(xs) {
#| Shrink::shrink(xs[:].to_owned()).map(candidate => {
#| ReadOnlyArray::from_array(candidate)
#| })
#|}
#|pub impl[X : Shrink] Shrink for ArrayView[X] with fn shrink(xs) {
#| let candidates : Iter[Array[X]] = Shrink::shrink(xs.to_owned())
#| candidates.map(candidate => candidate)
#|}
#|pub impl[X : Shrink] Shrink for @ref.Ref[X] with fn shrink(x) {
#| Shrink::shrink(x.val).map(value => Ref(value))
#|}
#|pub impl[A : Shrink, B : Shrink] Shrink for (A, B) with fn shrink(x) {
#| let (a, b) = x
#| Shrink::shrink(a)
#| .map(a1 => (a1, b))
#| .concat(Shrink::shrink(b).map(b1 => (a, b1)))
#|}
#|pub impl[A : Shrink, B : Shrink, C : Shrink] Shrink for (A, B, C) with fn shrink(
#| x,
#|) {
#| let (a, b, c) = x
#| Shrink::shrink((a, (b, c))).map(y => {
#| let (a1, (b1, c1)) = y
#| (a1, b1, c1)
#| })
#|}
#|pub impl[A : Shrink, B : Shrink, C : Shrink, D : Shrink] Shrink for (A, B, C, D) with fn shrink(
#| x,
#|) {
#| let (a, b, c, d) = x
#| Shrink::shrink((a, (b, c, d))).map(y => {
#| let (a1, (b1, c1, d1)) = y
#| (a1, b1, c1, d1)
#| })
#|}
#|pub impl[A : Shrink, B : Shrink, C : Shrink, D : Shrink, E : Shrink] Shrink for (
#| A,
#| B,
#| C,
#| D,
#| E,
#|) with fn shrink(x) {
#| let (a, b, c, d, e) = x
#| Shrink::shrink((a, (b, c, d, e))).map(y => {
#| let (a1, (b1, c1, d1, e1)) = y
#| (a1, b1, c1, d1, e1)
#| })
#|}
#|pub impl[A : Shrink, B : Shrink, C : Shrink, D : Shrink, E : Shrink, F : Shrink] Shrink for (
#| A,
#| B,
#| C,
#| D,
#| E,
#| F,
#|) with fn shrink(x) {
#| let (a, b, c, d, e, f) = x
#| Shrink::shrink((a, (b, c, d, e, f))).map(y => {
#| let (a1, (b1, c1, d1, e1, f1)) = y
#| (a1, b1, c1, d1, e1, f1)
#| })
#|}
#|pub impl[
#| A : Shrink,
#| B : Shrink,
#| C : Shrink,
#| D : Shrink,
#| E : Shrink,
#| F : Shrink,
#| G : Shrink,
#|] Shrink for (A, B, C, D, E, F, G) with fn shrink(x) {
#| let (a, b, c, d, e, f, g) = x
#| Shrink::shrink((a, (b, c, d, e, f, g))).map(y => {
#| let (a1, (b1, c1, d1, e1, f1, g1)) = y
#| (a1, b1, c1, d1, e1, f1, g1)
#| })
#|}
#|pub impl[
#| A : Shrink,
#| B : Shrink,
#| C : Shrink,
#| D : Shrink,
#| E : Shrink,
#| F : Shrink,
#| G : Shrink,
#| H : Shrink,
#|] Shrink for (A, B, C, D, E, F, G, H) with fn shrink(x) {
#| let (a, b, c, d, e, f, g, h) = x
#| Shrink::shrink((a, (b, c, d, e, f, g, h))).map(y => {
#| let (a1, (b1, c1, d1, e1, f1, g1, h1)) = y
#| (a1, b1, c1, d1, e1, f1, g1, h1)
#| })
#|}
#|pub impl[
#| A : Shrink,
#| B : Shrink,
#| C : Shrink,
#| D : Shrink,
#| E : Shrink,
#| F : Shrink,
#| G : Shrink,
#| H : Shrink,
#| I : Shrink,
#|] Shrink for (A, B, C, D, E, F, G, H, I) with fn shrink(x) {
#| let (a, b, c, d, e, f, g, h, i) = x
#| Shrink::shrink((a, (b, c, d, e, f, g, h, i))).map(y => {
#| let (a1, (b1, c1, d1, e1, f1, g1, h1, i1)) = y
#| (a1, b1, c1, d1, e1, f1, g1, h1, i1)
#| })
#|}
),
"shrink.mbt": (
#|pub(open) trait Shrink {
#| fn shrink(Self) -> Iter[Self] = _
#|}
#|impl Shrink with fn shrink(_a) {
#| [||]
#|}
#|pub impl Shrink for Int32 with fn shrink(x) {
#| [|..[ for z = x / 2; z != 0; z = z / 2 => x - z ], ..if x != 0 { [0] }|]
#|}
#|pub impl Shrink for Int64 with fn shrink(x) {
#| [|..[ for z = x / 2; z != 0; z = z / 2 => x - z ], ..if x != 0 { [0L] }|]
#|}
#|pub impl Shrink for @int16.Int16 with fn shrink(x) {
#| Shrink::shrink(x.to_int()).map(@int16.Int16::from_int)
#|}
#|pub impl Shrink for UInt16 with fn shrink(x) {
#| Shrink::shrink(x.to_uint()).map(UInt32::to_uint16)
#|}
#|pub impl Shrink for @bigint.BigInt with fn shrink(x) {
#| let zero = @bigint.BigInt::from_int(0)
#| guard x != zero else { return Iter::empty() }
#| let magnitude = if x < zero { -x } else { x }
#| let bit_length = magnitude.bit_length()
#| let negative = x < zero
#| let mut shift = bit_length - 1
#| let mut emit_zero = true
#| Iter(
#| () => {
#| if shift > 0 {
#| let delta = magnitude >> shift
#| shift -= 1
#| Some(if negative { x + delta } else { x - delta })
#| } else if emit_zero {
#| emit_zero = false
#| Some(zero)
#| } else {
#| None
#| }
#| },
#| size_hint=bit_length,
#| )
#|}
#|pub impl Shrink for UInt32 with fn shrink(x) {
#| [|..[ for z = x / 2; z > 0; z = z / 2 => x - z ], ..if x != 0 { [0U] }|]
#|}
#|pub impl Shrink for UInt64 with fn shrink(x) {
#| [|..[ for z = x / 2; z > 0; z = z / 2 => x - z ], ..if x != 0 { [0UL] }|]
#|}
#|pub impl Shrink for Bool with fn shrink(b) {
#| if !b {
#| [||]
#| } else {
#| [|false|]
#| }
#|}
#|pub impl Shrink for Byte with fn shrink(x) {
#| let xi = x.to_int()
#| [|
#| ..[
#| for z = xi / 2; z > 0; z = z / 2 => (xi - z).to_byte()
#| ],
#| ..if xi != 0 {
#| [Byte(0)]
#| },
#| |]
#|}
#|pub impl Shrink for Char with fn shrink(c) {
#| let candidates = [
#| 'a',
#| 'b',
#| 'c',
#| ..if c.is_ascii_uppercase() {
#| [c.to_ascii_lowercase()]
#| },
#| 'A',
#| 'B',
#| 'C',
#| '1',
#| '2',
#| '3',
#| ' ',
#| '\n',
#| ]
#| candidates.sort()
#| candidates.dedup()
#| let char_stamp = (c : Char) => {
#| (
#| (!c.is_ascii_lowercase(), !c.is_ascii_uppercase(), !c.is_ascii_digit()),
#| (c != ' ', !c.is_whitespace(), c),
#| )
#| }
#| let stamp = char_stamp(c)
#| candidates.iter().filter(candidate => char_stamp(candidate) < stamp)
#|}
#|pub impl Shrink for Double with fn shrink(x) {
#| shrink_decimal(x)
#|}
#|pub impl Shrink for @float.Float with fn shrink(x) {
#| shrink_decimal(x.to_double())
#| .map(@float.Float::from_double)
#| .filter(y => y != x)
#|}
#|pub impl Shrink for String with fn shrink(s) {
#| Shrink::shrink(s.to_array()).map(chars => String::from_array(chars))
#|}
#|pub impl Shrink for Bytes with fn shrink(bytes) {
#| Shrink::shrink(bytes.to_array()).map(xs => Bytes::from_array(xs))
#|}
#|pub impl Shrink for Unit
),
"utils.mbt": (
#|fn[T] deferred(f : () -> Iter[T]) -> Iter[T] {
#| let cell = @lazy.Lazy(f)
#| Iter(fn() { cell.force().next() })
#|}
#|fn[T] removes_array(k : Int32, n : Int32, xs : Array[T]) -> Iter[Array[T]] {
#| guard k <= n else { [||] }
#| let mut start = 0
#| Iter(
#| fn() {
#| guard start + k <= n else { None }
#| let candidate = [..xs[:start], ..xs[start + k:]]
#| start += k
#| Some(candidate)
#| },
#| size_hint=(n - k) / k + 1,
#| )
#|}
#|fn[T] removes_list(
#| k : Int32,
#| n : Int32,
#| xs : @list.List[T],
#|) -> Iter[@list.List[T]] {
#| guard k <= n else { [||] }
#| let mut start = (n - k) / k * k
#| Iter(
#| fn() {
#| guard start >= 0 else { None }
#| let candidate = xs.take(start).concat(xs.drop(start + k))
#| start -= k
#| Some(candidate)
#| },
#| size_hint=(n - k) / k + 1,
#| )
#|}
#|fn shrink_decimal(x : Double) -> Iter[Double] {
#| guard !x.is_nan() else { [|0.0, 1.0, -1.0, 2.0|] }
#| guard !x.is_inf() else { [|0.0, 1.0, -1.0, 1000.0, -1000.0|] }
#| guard x >= 0.0 else { [|-x|].concat(shrink_decimal(-x).map(Double::neg)) }
#| guard x != 0.0 else { [||] }
#| [|1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0|].flat_map(p => {
#| let m = (x * p + 0.5).floor().to_int64()
#| if p != 1.0 && m % 10L == 0L {
#| return [||]
#| }
#| [|m|]
#| .concat(Shrink::shrink(m))
#| .map(n => n.to_double() / p)
#| .filter(y => y >= 0.0 && y < x)
#| })
#|}
),
},
)
///|
let moonbitlang_core_quickcheck_splitmix_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/quickcheck/splitmix",
deps={},
files={
"deprecated.mbt": (
#|#deprecated("Use `RandomState()` instead")
#|pub fn RandomState::new(seed~ : UInt64 = 37) -> RandomState {
#| RandomState(seed~)
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend RandomState with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend RandomState with Default::{default}
),
"random.mbt": (
#|#derive(@debug.Debug)
#|struct RandomState {
#| mut seed : UInt64
#| gamma : UInt64
#|}
#|let golden_gamma : UInt64 = 0x9e3779b97f4a7c15
#|let double_ulp : Double = 1.0 / (1L << 53).to_double()
#|let float_ulp : @float.Float = 1.0F / @float.Float::from_int64(1L << 24)
#|#as_free_fn(new, deprecated="Use `RandomState()` instead")
#|pub fn RandomState::RandomState(seed~ : UInt64 = 37) -> RandomState {
#| { seed: mix64(seed), gamma: mix_gamma(seed + golden_gamma), }
#|}
#|#alias(clone, deprecated="Use `RandomState::copy` instead")
#|pub fn RandomState::copy(self : RandomState) -> RandomState {
#| { ..self, }
#|}
#|pub fn RandomState::next(self : RandomState) -> Unit {
#| self.next_uint64() |> ignore
#|}
#|fn RandomState::next_uint64_raw(self : RandomState) -> UInt64 {
#| let { seed, gamma, } = self
#| self.seed = seed + gamma
#| mix64(self.seed)
#|}
#|pub fn RandomState::next_uint64(self : RandomState, limit? : UInt64) -> UInt64 {
#| match limit {
#| None => self.next_uint64_raw()
#| Some(0UL) => abort("RandomState::next_uint64: limit must be positive")
#| Some(limit) => {
#| let mask = if limit == 1UL {
#| 0UL
#| } else {
#| 0xffff_ffff_ffff_ffffUL >> (limit - 1UL).clz()
#| }
#| for candidate = self.next_uint64_raw() & mask; candidate >= limit; {
#| continue self.next_uint64_raw() & mask
#| } nobreak {
#| candidate
#| }
#| }
#| }
#|}
#|pub fn RandomState::next_uint(self : RandomState, limit? : UInt32) -> UInt32 {
#| match limit {
#| None => self.next_uint64_raw().to_uint()
#| Some(0U) => abort("RandomState::next_uint: limit must be positive")
#| Some(limit) => self.next_uint64(limit=limit.to_uint64()).to_uint()
#| }
#|}
#|pub fn RandomState::next_int64(self : RandomState) -> Int64 {
#| self.next_uint64().reinterpret_as_int64()
#|}
#|pub fn RandomState::next_two_uint(self : RandomState) -> (UInt32, UInt32) {
#| let g = self.next_uint64()
#| ((g >> 32).to_uint(), g.to_uint())
#|}
#|pub fn RandomState::next_int(self : RandomState) -> Int32 {
#| self.next_uint().reinterpret_as_int()
#|}
#|pub fn RandomState::next_positive_int(self : RandomState) -> Int32 {
#| let r = self.next_int()
#| match r {
#| -2147483648 => 2147483647
#| 0 => 1
#| r if r < 0 => -r
#| r => r
#| }
#|}
#|pub fn RandomState::next_float(self : RandomState) -> @float.Float {
#| let u = self.next_uint64()
#| @float.Float::from_uint64(u >> 40) * float_ulp
#|}
#|pub fn RandomState::next_double(self : RandomState) -> Double {
#| let u = self.next_uint64()
#| (u >> 11).to_double() * double_ulp
#|}
#|pub fn RandomState::split(self : RandomState) -> RandomState {
#| let seed1 = self.seed + self.gamma
#| self.seed = seed1 + self.gamma
#| { seed: mix64(seed1), gamma: mix_gamma(self.seed), }
#|}
#|fn shift_xor(n : Int32, w : UInt64) -> UInt64 {
#| w ^ (w >> n)
#|}
#|fn shift_xor_mul(n : Int32, k : UInt64, w : UInt64) -> UInt64 {
#| shift_xor(n, w) * k
#|}
#|fn mix64(z0 : UInt64) -> UInt64 {
#| let z1 = shift_xor_mul(33, 0xff51afd7ed558ccd, z0)
#| let z2 = shift_xor_mul(33, 0xc4ceb9fe1a85ec53, z1)
#| shift_xor(33, z2)
#|}
#|fn mix64variant13(z0 : UInt64) -> UInt64 {
#| let z1 = shift_xor_mul(30, 0xbf58476d1ce4e5b9, z0)
#| let z2 = shift_xor_mul(27, 0x94d049bb133111eb, z1)
#| shift_xor(31, z2)
#|}
#|fn mix_gamma(z0 : UInt64) -> UInt64 {
#| let z1 = mix64variant13(z0) | 1
#| let n = (z1 ^ (z1 >> 1)).popcnt()
#| if n >= 24 {
#| z1
#| } else {
#| z1 ^ 0xaaaaaaaaaaaaaaaa
#| }
#|}
#|pub impl Default for RandomState with fn default() {
#| RandomState()
#|}
#|test "non-zero default" {
#| let rs = (Default::default() : RandomState)
#| @test.assert_not_eq(rs.seed, 0)
#| @test.assert_not_eq(rs.gamma, 0)
#|}
),
},
)
///|
let moonbitlang_core_random_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/random",
deps={},
files={
"debug.mbt": (
#|pub impl @debug.Debug for Rand with fn to_repr(_) {
#| @debug.Repr::opaque_("Rand", @debug.Repr::omitted())
#|}
),
"default.mbt": (
#|let default_rand : Rand = Rand()
#|pub fn int32(limit? : Int32) -> Int32 {
#| match limit {
#| None => default_rand.int()
#| Some(limit) => {
#| guard limit > 0 else { abort("int32: limit must be positive") }
#| default_rand.int(limit~)
#| }
#| }
#|}
#|pub fn int64(limit? : Int64) -> Int64 {
#| match limit {
#| None => default_rand.int64()
#| Some(limit) => {
#| guard limit > 0L else { abort("int64: limit must be positive") }
#| default_rand.int64(limit~)
#| }
#| }
#|}
#|pub fn uint32(limit? : UInt32) -> UInt32 {
#| match limit {
#| None => default_rand.uint()
#| Some(limit) => {
#| guard limit > 0U else { abort("uint32: limit must be positive") }
#| default_rand.uint(limit~)
#| }
#| }
#|}
#|pub fn uint64(limit? : UInt64) -> UInt64 {
#| match limit {
#| None => default_rand.uint64()
#| Some(limit) => {
#| guard limit > 0UL else { abort("uint64: limit must be positive") }
#| default_rand.uint64(limit~)
#| }
#| }
#|}
#|pub fn double(min~ : Double = 0.0, max~ : Double = 1.0) -> Double {
#| default_rand.double(min~, max~)
#|}
#|pub fn float(
#| min~ : @float.Float = 0.0F,
#| max~ : @float.Float = 1.0F,
#|) -> @float.Float {
#| default_rand.float(min~, max~)
#|}
#|pub fn boolean() -> Bool {
#| default_rand.boolean()
#|}
#|pub fn bigint(bits~ : Int32) -> @bigint.BigInt {
#| guard bits >= 0 else { abort("bigint: bits must be non-negative") }
#| default_rand.bigint(bits)
#|}
#|pub fn shuffle(length~ : Int32, swap : (Int32, Int32) -> Unit) -> Unit {
#| default_rand.shuffle(length, swap)
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `Rand::chacha8(seed~)` instead")
#|pub fn new(seed~ : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456") -> Rand {
#| if seed.length() != 32 {
#| abort("seed must be 32 bytes long")
#| }
#| { source: @random_source.ChaCha8(seed) as &Source, }
#|}
#|#deprecated("You may use `Rand::chacha8(seed~)` instead of `Rand(generator=chacha8(seed~))`")
#|pub fn chacha8(seed~ : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456") -> &Source {
#| @random_source.ChaCha8(seed)
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Rand with @debug.Debug::{to_repr}
),
"random.mbt": (
#|struct Rand {
#| source : &Source
#|}
#|pub(open) trait Source {
#| fn next(Self) -> UInt64
#|}
#|impl Source for @random_source.ChaCha8 with fn next(
#| self : @random_source.ChaCha8,
#|) -> UInt64 {
#| self.next_uint64()
#|}
#|pub fn Rand::chacha8(
#| seed~ : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
#|) -> Rand {
#| if seed.length() != 32 {
#| abort("seed must be 32 bytes long")
#| }
#| { source: @random_source.ChaCha8(seed) as &Source, }
#|}
#|let fixed_test_seed : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"
#|#alias(new, deprecated="Use `Rand()` instead")
#|pub fn Rand::Rand(generator? : &Source) -> Rand {
#| match generator {
#| None =>
#| match @env.rand(32) {
#| Some(seed) => Rand::chacha8(seed~)
#| None => Rand::chacha8()
#| }
#| Some(gen) => { source: gen, }
#| }
#|}
#|fn Rand::next(self : Rand) -> UInt64 {
#| self.source.next()
#|}
#|test "next" {
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| let n = r.next()
#| let exp = 13219109469176600229UL
#| @test.assert_eq(n, exp)
#|}
#|pub fn Rand::int(self : Rand, limit~ : Int32 = 0) -> Int32 {
#| if limit < 0 {
#| abort("Rand::int: invalid argument limit")
#| }
#| if limit == 0 {
#| (self.next() >> 33).to_int()
#| } else {
#| self.uint(limit=limit.reinterpret_as_uint()).reinterpret_as_int()
#| }
#|}
#|pub fn Rand::int64(self : Rand, limit~ : Int64 = 0) -> Int64 {
#| if limit < 0 {
#| abort("Rand::int64: invalid argument limit")
#| }
#| if limit == 0 {
#| let mask : UInt64 = (1UL << 63) - 1UL
#| return (self.next() & mask).reinterpret_as_int64()
#| } else {
#| self.uint64(limit=limit.reinterpret_as_uint64()).reinterpret_as_int64()
#| }
#|}
#|pub fn Rand::uint(self : Rand, limit~ : UInt32 = 0) -> UInt32 {
#| if limit == 0 {
#| return self.next().to_uint()
#| }
#| self.uint64(limit=limit.to_uint64()).to_uint()
#|}
#|test "uint" {
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| let n = r.uint(limit=10U)
#| inspect(n, content="7")
#| let n = r.uint(limit=10U)
#| inspect(n, content="0")
#| let n = r.uint(limit=10U)
#| inspect(n, content="5")
#|}
#|pub fn Rand::uint64(self : Rand, limit~ : UInt64 = 0) -> UInt64 {
#| if limit == 0 {
#| return self.next()
#| } else if (limit & (limit - 1)) == 0 {
#| return self.next() & (limit - 1)
#| }
#| let r = umul128(self.next(), limit)
#| if r.lo >= limit {
#| return r.hi
#| }
#| let thresh = (limit.lnot() + 1) % limit
#| for r = r; r.lo < thresh; {
#| continue umul128(self.next(), limit)
#| } nobreak {
#| r.hi
#| }
#|}
#|test "UInt64" {
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| let n = r.uint64()
#| let exp = 13219109469176600229UL
#| @test.assert_eq(n, exp)
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| let n = r.uint64(limit=10UL)
#| inspect(n, content="7")
#| let n = r.uint64(limit=10UL)
#| inspect(n, content="0")
#| let n = r.uint64(limit=10UL)
#| inspect(n, content="5")
#|}
#|pub fn Rand::double(
#| self : Rand,
#| min~ : Double = 0.0,
#| max~ : Double = 1.0,
#|) -> Double {
#| if min == 0.0 && max == 1.0 {
#| (self.next() << 11 >> 11).to_double() / (1UL << 53).to_double()
#| } else {
#| self.double_in_range(min~, max~)
#| }
#|}
#|test "double" {
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| let n = r.double()
#| inspect(n, content="0.615969772029264")
#|}
#|pub fn Rand::float(
#| self : Rand,
#| min~ : @float.Float = 0.0F,
#| max~ : @float.Float = 1.0F,
#|) -> @float.Float {
#| if min == 0.0F && max == 1.0F {
#| @float.Float::from_uint(self.uint() << 8 >> 8) /
#| @float.Float::from_uint(1U << 24)
#| } else {
#| self.float_in_range(min~, max~)
#| }
#|}
#|fn double_gamma(anchor : Double) -> Double {
#| if anchor == 0.0 {
#| 1UL.reinterpret_as_double()
#| } else {
#| let toward_zero = (anchor.reinterpret_as_uint64() - 1).reinterpret_as_double()
#| (anchor - toward_zero).abs()
#| }
#|}
#|fn ceil_sections(
#| scaled_min : Double,
#| scaled_max : Double,
#| anchor_is_max~ : Bool,
#|) -> UInt64 {
#| let distance = scaled_max - scaled_min
#| let error = if anchor_is_max {
#| -scaled_min - (distance - scaled_max)
#| } else {
#| scaled_max - (distance + scaled_min)
#| }
#| let ceiling = distance.ceil()
#| let sections = ceiling.to_uint64()
#| if distance != ceiling {
#| sections
#| } else if error > 0.0 {
#| sections + error.ceil().to_uint64()
#| } else {
#| sections - (-error).floor().to_uint64()
#| }
#|}
#|fn split_uint64_for_double(value : UInt64) -> (Double, Double) {
#| ((value >> 2).to_double(), (value & 0x3).to_double())
#|}
#|fn float_gamma(anchor : @float.Float) -> @float.Float {
#| if anchor == 0.0F {
#| @float.Float::reinterpret_from_uint(1U)
#| } else {
#| let toward_zero = @float.Float::reinterpret_from_uint(
#| anchor.reinterpret_as_uint() - 1,
#| )
#| (anchor - toward_zero).abs()
#| }
#|}
#|fn split_uint64_for_float(value : UInt64) -> (@float.Float, @float.Float) {
#| (
#| @float.Float::from_uint64(value >> 2),
#| @float.Float::from_uint64(value & 0x3),
#| )
#|}
#|fn Rand::double_in_range(self : Rand, min~ : Double, max~ : Double) -> Double {
#| guard min.is_finite() && max.is_finite() else {
#| abort("Rand::double: bounds must be finite")
#| }
#| let underflow_threshold = 4.0 * @double.min_positive
#| guard (min == 0.0 || min.abs() >= underflow_threshold) &&
#| (max == 0.0 || max.abs() >= underflow_threshold) else {
#| abort("Rand::double: bounds must be outside the underflow region")
#| }
#| guard min < max else { abort("Rand::double: min must be less than max") }
#| let anchor_is_max = min.abs() <= max.abs()
#| let gamma = double_gamma(if anchor_is_max { max } else { min })
#| let scaled_min = min / gamma
#| let scaled_max = max / gamma
#| guard (min == 0.0 || scaled_min.abs() >= @double.min_positive) &&
#| (max == 0.0 || scaled_max.abs() >= @double.min_positive) else {
#| abort("Rand::double: scaling a bound would underflow")
#| }
#| let sections = ceil_sections(scaled_min, scaled_max, anchor_is_max~)
#| let k = self.uint64(limit=sections) + 1
#| if anchor_is_max {
#| if k == sections {
#| min
#| } else {
#| let (k_high, k_low) = split_uint64_for_double(k)
#| 4.0 * (max * 0.25 - k_high * gamma) - k_low * gamma
#| }
#| } else {
#| let (k_high, k_low) = split_uint64_for_double(k - 1)
#| 4.0 * (min * 0.25 + k_high * gamma) + k_low * gamma
#| }
#|}
#|fn Rand::float_in_range(
#| self : Rand,
#| min~ : @float.Float,
#| max~ : @float.Float,
#|) -> @float.Float {
#| guard min.is_finite() && max.is_finite() else {
#| abort("Rand::float: bounds must be finite")
#| }
#| let underflow_threshold = 4.0F * @float.min_positive
#| guard (min == 0.0F || min.abs() >= underflow_threshold) &&
#| (max == 0.0F || max.abs() >= underflow_threshold) else {
#| abort("Rand::float: bounds must be outside the underflow region")
#| }
#| guard min < max else { abort("Rand::float: min must be less than max") }
#| let anchor_is_max = min.abs() <= max.abs()
#| let gamma = float_gamma(if anchor_is_max { max } else { min })
#| let sections = ceil_sections(
#| min.to_double() / gamma.to_double(),
#| max.to_double() / gamma.to_double(),
#| anchor_is_max~,
#| )
#| let k = self.uint64(limit=sections) + 1
#| if anchor_is_max {
#| if k == sections {
#| min
#| } else {
#| let (k_high, k_low) = split_uint64_for_float(k)
#| 4.0F * (max * 0.25F - k_high * gamma) - k_low * gamma
#| }
#| } else {
#| let (k_high, k_low) = split_uint64_for_float(k - 1)
#| 4.0F * (min * 0.25F + k_high * gamma) + k_low * gamma
#| }
#|}
#|test "ceil_sections corrects a negative Dekker residual" {
#| let min = 0xffefffffffffffffUL.reinterpret_as_double()
#| let max = 0x7fe0000000000000UL.reinterpret_as_double()
#| let gamma = double_gamma(min)
#| let sections = ceil_sections(min / gamma, max / gamma, anchor_is_max=false)
#| @test.assert_eq(sections, 0x2fffffffffffffUL)
#|}
#|pub fn Rand::boolean(self : Rand) -> Bool {
#| (self.next() & 1) == 1
#|}
#|pub fn Rand::bigint(self : Rand, bits : Int32) -> @bigint.BigInt {
#| let mod = bits % 8
#| let len = if mod == 0 { bits / 8 } else { bits / 8 + 1 }
#| let bytes = Bytes::makei(len, i => {
#| if i == 0 && mod != 0 {
#| let mask = (1U << mod) - 1U
#| (self.uint(limit=256) & mask).to_byte()
#| } else {
#| self.uint(limit=256).to_byte()
#| }
#| })
#| @bigint.BigInt::from_octets(bytes)
#|}
#|test "bigint" {
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| inspect(r.bigint(1), content="1")
#| inspect(r.bigint(3), content="4")
#| inspect(r.bigint(7), content="124")
#| inspect(r.bigint(8), content="214")
#| inspect(r.bigint(32), content="2910404175")
#| inspect(r.bigint(40), content="714745001576")
#| inspect(r.bigint(64), content="13430064486797060338")
#| inspect(r.bigint(128), content="251068071753473224445949321151725639522")
#|}
#|#valtype
#|priv struct UInt128 {
#| hi : UInt64
#| lo : UInt64
#|}
#|fn umul128(a : UInt64, b : UInt64) -> UInt128 {
#| let aLo = a & 0xffffffff
#| let aHi = a >> 32
#| let bLo = b & 0xffffffff
#| let bHi = b >> 32
#| let x = aLo * bLo
#| let y = aHi * bLo + (x >> 32)
#| let z = aLo * bHi + (y & 0xffffffff)
#| let w = aHi * bHi + (y >> 32) + (z >> 32)
#| { hi: w, lo: a * b, }
#|}
#|test "umul128" {
#| let r = umul128(0x123456789ABCDEF0, 0xFEDCBA9876543210)
#| @test.assert_eq(r.hi, 1305938385386173474UL)
#| @test.assert_eq(r.lo, 2552847189736476416UL)
#|}
#|test "umul128: handles small numbers correctly" {
#| let r = umul128(1UL, 1UL)
#| @test.assert_eq(r.hi, 0UL)
#| @test.assert_eq(r.lo, 1UL)
#|}
#|test "umul128: handles large numbers correctly" {
#| let r = umul128(1UL, 0xFFFFFFFFFFFFFFFFUL)
#| @test.assert_eq(r.hi, 0UL)
#| @test.assert_eq(r.lo, 0xFFFFFFFFFFFFFFFFUL)
#|}
#|test "umul128: handles zero correctly" {
#| let r = umul128(0UL, 0UL)
#| @test.assert_eq(r.hi, 0UL)
#| @test.assert_eq(r.lo, 0UL)
#|}
#|pub fn Rand::shuffle(
#| self : Rand,
#| limit : Int32,
#| swap : (Int32, Int32) -> Unit,
#|) -> Unit {
#| if limit < 0 {
#| abort("Rand::shuffle: invalid argument limit")
#| }
#| for i in limit>..1 {
#| let j = self.int(limit=i + 1)
#| swap(i, j)
#| }
#|}
#|test "shuffle" {
#| let r = Rand::chacha8(seed=fixed_test_seed)
#| let a = [1, 2, 3, 4, 5]
#| r.shuffle(a.length(), (i : Int32, j : Int32) => {
#| let t = a[i]
#| a[i] = a[j]
#| a[j] = t
#| })
#| @debug.debug_inspect(a, content="[3, 5, 2, 1, 4]")
#|}
),
},
)
///|
let moonbitlang_core_random_internal_random_source_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/random/internal/random_source",
deps={},
files={
"random_source_chacha.mbt": (
#|struct ChaCha8 {
#| buffer : FixedArray[UInt32]
#| seed : FixedArray[UInt32]
#| mut i : UInt32
#| mut n : UInt32
#| mut counter : UInt32
#|}
#|pub fn ChaCha8::ChaCha8(seed : BytesView) -> ChaCha8 {
#| if seed.length() != 32 {
#| abort("seed must be 32 bytes long")
#| }
#| let seed = FixedArray::makei(SEED_CHUNK_NUM * 2, i => {
#| match seed[i * 4:] {
#| [u32le(x), ..] => x
#| _ => abort("seed must be 32 bytes long")
#| }
#| })
#| let buffer = FixedArray::make(BUFFER_CHUNK_NUM * 2, 0U)
#| chacha_block(seed, buffer, 0)
#| { seed, buffer, counter: 0, i: 0, n: BUFFER_CHUNK_NUM.reinterpret_as_uint(), }
#|}
#|const COUNTER_INC = 4U
#|const COUNTER_MAX = 16U
#|const BUFFER_CHUNK_NUM = 32
#|const SEED_CHUNK_NUM = 4
#|pub fn ChaCha8::next_uint64(self : ChaCha8) -> UInt64 {
#| let mut i = self.i
#| if i >= self.n {
#| self.refill()
#| i = 0
#| }
#| self.i = i + 1
#| let index = i.reinterpret_as_int() & (BUFFER_CHUNK_NUM - 1)
#| let lo = self.buffer[index * 2].to_uint64()
#| let hi = self.buffer[index * 2 + 1].to_uint64()
#| (hi << 32) | lo
#|}
#|pub fn ChaCha8::refill(self : ChaCha8) -> Unit {
#| self.counter += COUNTER_INC
#| if self.counter == COUNTER_MAX {
#| self.buffer.blit_to(
#| self.seed,
#| len=SEED_CHUNK_NUM * 2,
#| src_offset=(BUFFER_CHUNK_NUM - SEED_CHUNK_NUM) * 2,
#| )
#| self.counter = 0
#| }
#| chacha_block(self.seed, self.buffer, self.counter)
#| self.i = 0
#| self.n = if self.counter == COUNTER_MAX - COUNTER_INC {
#| (BUFFER_CHUNK_NUM - SEED_CHUNK_NUM).reinterpret_as_uint()
#| } else {
#| BUFFER_CHUNK_NUM.reinterpret_as_uint()
#| }
#|}
#|#valtype
#|priv struct Quadruple(UInt32, UInt32, UInt32, UInt32)
#|fn chacha_block(
#| seed : FixedArray[UInt32],
#| buf : FixedArray[UInt32],
#| counter : UInt32,
#|) -> Unit {
#| fn qr(t : Quadruple) -> Quadruple {
#| let Quadruple(a, b, c, d) = t
#| let a = a + b
#| let d = d ^ a
#| let d = (d << 16) | (d >> 16)
#| let c = c + d
#| let b = b ^ c
#| let b = (b << 12) | (b >> 20)
#| let a = a + b
#| let d = d ^ a
#| let d = (d << 8) | (d >> 24)
#| let c = c + d
#| let b = b ^ c
#| let b = (b << 7) | (b >> 25)
#| Quadruple(a, b, c, d)
#| }
#| setup(seed, buf, counter)
#| for i in 0..<4 {
#| let mut b0 = buf[0 * 4 + i]
#| let mut b1 = buf[1 * 4 + i]
#| let mut b2 = buf[2 * 4 + i]
#| let mut b3 = buf[3 * 4 + i]
#| let mut b4 = buf[4 * 4 + i]
#| let mut b5 = buf[5 * 4 + i]
#| let mut b6 = buf[6 * 4 + i]
#| let mut b7 = buf[7 * 4 + i]
#| let mut b8 = buf[8 * 4 + i]
#| let mut b9 = buf[9 * 4 + i]
#| let mut b10 = buf[10 * 4 + i]
#| let mut b11 = buf[11 * 4 + i]
#| let mut b12 = buf[12 * 4 + i]
#| let mut b13 = buf[13 * 4 + i]
#| let mut b14 = buf[14 * 4 + i]
#| let mut b15 = buf[15 * 4 + i]
#| for _ in 0..<4 {
#| let Quadruple(tb1_0, tb1_1, tb1_2, tb1_3) = qr(Quadruple(b0, b4, b8, b12))
#| b0 = tb1_0
#| b4 = tb1_1
#| b8 = tb1_2
#| b12 = tb1_3
#| let Quadruple(tb2_0, tb2_1, tb2_2, tb2_3) = qr(Quadruple(b1, b5, b9, b13))
#| b1 = tb2_0
#| b5 = tb2_1
#| b9 = tb2_2
#| b13 = tb2_3
#| let Quadruple(tb3_0, tb3_1, tb3_2, tb3_3) = qr(
#| Quadruple(b2, b6, b10, b14),
#| )
#| b2 = tb3_0
#| b6 = tb3_1
#| b10 = tb3_2
#| b14 = tb3_3
#| let Quadruple(tb4_0, tb4_1, tb4_2, tb4_3) = qr(
#| Quadruple(b3, b7, b11, b15),
#| )
#| b3 = tb4_0
#| b7 = tb4_1
#| b11 = tb4_2
#| b15 = tb4_3
#| let Quadruple(tb5_0, tb5_1, tb5_2, tb5_3) = qr(
#| Quadruple(b0, b5, b10, b15),
#| )
#| b0 = tb5_0
#| b5 = tb5_1
#| b10 = tb5_2
#| b15 = tb5_3
#| let Quadruple(tb6_0, tb6_1, tb6_2, tb6_3) = qr(
#| Quadruple(b1, b6, b11, b12),
#| )
#| b1 = tb6_0
#| b6 = tb6_1
#| b11 = tb6_2
#| b12 = tb6_3
#| let Quadruple(tb7_0, tb7_1, tb7_2, tb7_3) = qr(Quadruple(b2, b7, b8, b13))
#| b2 = tb7_0
#| b7 = tb7_1
#| b8 = tb7_2
#| b13 = tb7_3
#| let Quadruple(tb8_0, tb8_1, tb8_2, tb8_3) = qr(Quadruple(b3, b4, b9, b14))
#| b3 = tb8_0
#| b4 = tb8_1
#| b9 = tb8_2
#| b14 = tb8_3
#| }
#| buf[0 * 4 + i] = b0
#| buf[1 * 4 + i] = b1
#| buf[2 * 4 + i] = b2
#| buf[3 * 4 + i] = b3
#| buf[4 * 4 + i] += b4
#| buf[5 * 4 + i] += b5
#| buf[6 * 4 + i] += b6
#| buf[7 * 4 + i] += b7
#| buf[8 * 4 + i] += b8
#| buf[9 * 4 + i] += b9
#| buf[10 * 4 + i] += b10
#| buf[11 * 4 + i] += b11
#| buf[12 * 4 + i] = b12
#| buf[13 * 4 + i] = b13
#| buf[14 * 4 + i] = b14
#| buf[15 * 4 + i] = b15
#| }
#|}
#|fn setup(
#| seed : FixedArray[UInt32],
#| b32 : FixedArray[UInt32],
#| counter : UInt32,
#|) -> Unit {
#| b32[0 * 4 + 0] = 0x61707865
#| b32[0 * 4 + 1] = 0x61707865
#| b32[0 * 4 + 2] = 0x61707865
#| b32[0 * 4 + 3] = 0x61707865
#| b32[1 * 4 + 0] = 0x3320646e
#| b32[1 * 4 + 1] = 0x3320646e
#| b32[1 * 4 + 2] = 0x3320646e
#| b32[1 * 4 + 3] = 0x3320646e
#| b32[2 * 4 + 0] = 0x79622d32
#| b32[2 * 4 + 1] = 0x79622d32
#| b32[2 * 4 + 2] = 0x79622d32
#| b32[2 * 4 + 3] = 0x79622d32
#| b32[3 * 4 + 0] = 0x6b206574
#| b32[3 * 4 + 1] = 0x6b206574
#| b32[3 * 4 + 2] = 0x6b206574
#| b32[3 * 4 + 3] = 0x6b206574
#| b32[4 * 4 + 0] = seed[0]
#| b32[4 * 4 + 1] = seed[0]
#| b32[4 * 4 + 2] = seed[0]
#| b32[4 * 4 + 3] = seed[0]
#| b32[5 * 4 + 0] = seed[1]
#| b32[5 * 4 + 1] = seed[1]
#| b32[5 * 4 + 2] = seed[1]
#| b32[5 * 4 + 3] = seed[1]
#| b32[6 * 4 + 0] = seed[2]
#| b32[6 * 4 + 1] = seed[2]
#| b32[6 * 4 + 2] = seed[2]
#| b32[6 * 4 + 3] = seed[2]
#| b32[7 * 4 + 0] = seed[3]
#| b32[7 * 4 + 1] = seed[3]
#| b32[7 * 4 + 2] = seed[3]
#| b32[7 * 4 + 3] = seed[3]
#| b32[8 * 4 + 0] = seed[4]
#| b32[8 * 4 + 1] = seed[4]
#| b32[8 * 4 + 2] = seed[4]
#| b32[8 * 4 + 3] = seed[4]
#| b32[9 * 4 + 0] = seed[5]
#| b32[9 * 4 + 1] = seed[5]
#| b32[9 * 4 + 2] = seed[5]
#| b32[9 * 4 + 3] = seed[5]
#| b32[10 * 4 + 0] = seed[6]
#| b32[10 * 4 + 1] = seed[6]
#| b32[10 * 4 + 2] = seed[6]
#| b32[10 * 4 + 3] = seed[6]
#| b32[11 * 4 + 0] = seed[7]
#| b32[11 * 4 + 1] = seed[7]
#| b32[11 * 4 + 2] = seed[7]
#| b32[11 * 4 + 3] = seed[7]
#| b32[12 * 4 + 0] = counter + 0
#| b32[12 * 4 + 1] = counter + 1
#| b32[12 * 4 + 2] = counter + 2
#| b32[12 * 4 + 3] = counter + 3
#| b32[13 * 4 + 0] = 0
#| b32[13 * 4 + 1] = 0
#| b32[13 * 4 + 2] = 0
#| b32[13 * 4 + 3] = 0
#| b32[14 * 4 + 0] = 0
#| b32[14 * 4 + 1] = 0
#| b32[14 * 4 + 2] = 0
#| b32[14 * 4 + 3] = 0
#| b32[15 * 4 + 0] = 0
#| b32[15 * 4 + 1] = 0
#| b32[15 * 4 + 2] = 0
#| b32[15 * 4 + 3] = 0
#|}
#|test "BUFFER_CHUNK_NUM is power of 2" {
#| @test.assert_eq(BUFFER_CHUNK_NUM.clz() + BUFFER_CHUNK_NUM.ctz(), 31)
#|}
#|fn chacha8_values_at(
#| seed : BytesView,
#| positions : Array[Int32],
#|) -> Array[UInt64] {
#| let s = ChaCha8(seed)
#| let values = [
#| for _ in 0..<=positions[positions.length() - 1] => s.next_uint64()
#| ]
#| [
#| for position in positions => values[position]
#| ]
#|}
#|test "boundary conformance samples" {
#| let positions = [
#| 0, 1, 31, 32, 33, 95, 96, 123, 124, 125, 247, 248, 249, 371, 372,
#| ]
#| @test.assert_eq(chacha8_values_at(Bytes::make(32, b'\x00'), positions), [
#| 12432809566553409497UL, 7766219816435805978UL, 125952780581614778UL, 11456203247299035011UL,
#| 11766211380141310478UL, 6247410371955873534UL, 3688096476295560216UL, 8184198519316962464UL,
#| 10179668788428909817UL, 5258471208527403019UL, 7420841827674394489UL, 10937539205284161074UL,
#| 7354588099337910803UL, 1988377183516050978UL, 14819101479397601098UL,
#| ])
#| @test.assert_eq(
#| chacha8_values_at(Bytes::makei(32, i => i.to_byte()), positions),
#| [
#| 12537355132343524571UL, 11147319424196738781UL, 10643391562797985389UL, 3312211745067898083UL,
#| 9018521450285820678UL, 4858190046463179352UL, 990490856749627748UL, 7053860951501379351UL,
#| 7333627332515303615UL, 13837653776835850180UL, 2865794351678725456UL, 5128231522832781158UL,
#| 5308840319225627323UL, 16534914931032675201UL, 9337503077864249277UL,
#| ],
#| )
#| @test.assert_eq(
#| chacha8_values_at(Bytes::makei(32, i => (31 - i).to_byte()), positions),
#| [
#| 13207452830590730783UL, 5009552866163029026UL, 10502404787336730197UL, 9712881148437595521UL,
#| 16401154181191732095UL, 16593913030854572746UL, 14562847379601139255UL, 7882510831935376133UL,
#| 7240113522724531080UL, 1369140773094178244UL, 12544778259996841346UL, 17662407199021422433UL,
#| 8595294092888404219UL, 3588698780051976674UL, 13209002768099367735UL,
#| ],
#| )
#|}
#|test "output" {
#| let s = ChaCha8(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456")
#| let res = [ for _ in 0..<372 => s.next_uint64() ]
#| let expected = [
#| 13219109469176600229UL, 1252193259764759612UL, 10098646897834928252UL, 9142445797040479446UL,
#| 14963947397558572717UL, 1011356013395962489UL, 4360278772332321598UL, 11266561239126549327UL,
#| 16103107970030431910UL, 3951155890335085418UL, 17217491508779737119UL, 620366957612729398UL,
#| 15701003340574544488UL, 16946962128229337786UL, 5734710318278665825UL, 14107018073513584172UL,
#| 10514695901183618686UL, 8668570317698669510UL, 6644195231047269920UL, 5695522258251995816UL,
#| 18173674537264966642UL, 6561080880156464316UL, 7896566770606135777UL, 17198961315715285235UL,
#| 3258233649777561765UL, 4039019003980269744UL, 17199080872890330697UL, 17597083911441838460UL,
#| 9541597232904836516UL, 10269632480771352091UL, 1791309313159652499UL, 1121290688170442051UL,
#| 14767415290099658880UL, 18264015867885335344UL, 10795746865526236211UL, 7814966709161082135UL,
#| 5056082071174014306UL, 6606687772221963370UL, 902460967827941768UL, 15247644503822243389UL,
#| 13837920613503741631UL, 6215820458297045417UL, 2701601382099612372UL, 8049443921449462014UL,
#| 12580932726856406240UL, 5068579462968063859UL, 2054759765710618254UL, 8060325759976441792UL,
#| 3952703220727229276UL, 11868554197401451959UL, 5611917470950228677UL, 14826085289999852455UL,
#| 18131733951999902497UL, 6983040833037131399UL, 15098965934858340182UL, 7162487557317849930UL,
#| 389612597396113390UL, 5480468948381420210UL, 14924541494497045954UL, 438038016380948435UL,
#| 15107526033745383085UL, 3577988464916041960UL, 12379376873809997548UL, 4629939204773816090UL,
#| 4382836348202044776UL, 15911307370497934714UL, 16912525941835256446UL, 1749399752872286892UL,
#| 17865934552210109010UL, 6342441227090084049UL, 6679300346758746971UL, 16633103117551110662UL,
#| 369926567391976152UL, 3270154504052173416UL, 12351442307584366520UL, 14053856752323452444UL,
#| 5700095014850418212UL, 18134114469714363192UL, 11060594460025237009UL, 1231359220627153402UL,
#| 562452272787830041UL, 2978775404523127572UL, 4722226048622717816UL, 6253931703231703629UL,
#| 9215422719337958629UL, 3873253580645818250UL, 1288530853271360801UL, 11001669669030575221UL,
#| 15088099745414120440UL, 10121311161118028787UL, 2646319338087045047UL, 8736474933647836016UL,
#| 10270419548610515257UL, 4775977266197392933UL, 5858955664787759631UL, 15884112255303985129UL,
#| 1570402559843208890UL, 14683984304255533684UL, 4590793746421620769UL, 15088822470495597798UL,
#| 430063278371467896UL, 17951554591419905623UL, 871816740556950425UL, 17903421440867820178UL,
#| 13750571273678813240UL, 17408919480923799904UL, 14550467517685344003UL, 17801022151644150006UL,
#| 15135601085914355452UL, 3483086720686978374UL, 9411102566564534151UL, 1565275101947305404UL,
#| 18195171881473325257UL, 3771067494878101692UL, 7443114766343082856UL, 1311911009840065015UL,
#| 1031414546975514137UL, 16727931877057143004UL, 420176525623228030UL, 16779254430513255833UL,
#| 2085321528439172864UL, 14769502908867070793UL, 3476754806476653326UL, 9663999206884821105UL,
#| 6864012991572870279UL, 2719468417766812445UL, 13735767778024137757UL, 7331676388138190150UL,
#| 15110277136215905228UL, 118894811326561078UL, 6308935423749160781UL, 1857610629058673910UL,
#| 15843686394045370811UL, 10951543581626859443UL, 3587431610669043023UL, 670469650389812154UL,
#| 476209600999700306UL, 6589566024609793738UL, 8281611217869782149UL, 15129277549214352190UL,
#| 13446421929377692845UL, 17071158676955966275UL, 7349330903428905191UL, 3277698994734830674UL,
#| 14731635658592643043UL, 5477400073429632652UL, 67518392538487036UL, 11035168000757098093UL,
#| 9797693702590129641UL, 14649582299731326424UL, 15236666389149272153UL, 16511980240231261314UL,
#| 2249833951459307500UL, 13214955418772830438UL, 329963039908253864UL, 8574511220958401562UL,
#| 4538345089129965474UL, 16353802882881726632UL, 8935276449126753317UL, 13532443277699696063UL,
#| 8669346374187395779UL, 17810417737921607796UL, 3904677225590828551UL, 10264576476552345822UL,
#| 3042606770072189440UL, 12878478126980245939UL, 17862138228081367377UL, 16992359172961651842UL,
#| 17034107108697981338UL, 12909026467335450156UL, 2470441546010339704UL, 3634699771143106326UL,
#| 366960336551226348UL, 5510841956427041079UL, 15461212383052875073UL, 16433279593065139848UL,
#| 4486890193637877972UL, 14360905330534596440UL, 4449385520993789019UL, 3743094329222006520UL,
#| 2212668117926313829UL, 4681723502265844963UL, 13296513799272088946UL, 2343616111600662093UL,
#| 14717171566066041529UL, 11738831003851491807UL, 15851419960104864784UL, 8132762647638440149UL,
#| 3464418631265929631UL, 340027485718840165UL, 8395791638092059350UL, 3109485649064359417UL,
#| 10944349985020042736UL, 6522608726689095595UL, 17803052569896648610UL, 2064238649661354599UL,
#| 4092763376312896325UL, 15988266335744638521UL, 16848187183754420180UL, 199841868197609864UL,
#| 17491157950173501157UL, 12533485177359032661UL, 6254686503080376974UL, 5499127381395659705UL,
#| 10487847042307388775UL, 12368654789669906974UL, 2361938659724380109UL, 8148818964487342298UL,
#| 12717021730603072605UL, 15728257831000719154UL, 15823591063810279774UL, 6928887951986678348UL,
#| 17959062744186568888UL, 16016655507227678734UL, 14898526437837544110UL, 16974466848830908932UL,
#| 15320157178342639871UL, 8261903640164714632UL, 14435417271854760UL, 9182672404209601003UL,
#| 5994285655773158181UL, 15654703508405219038UL, 16079243345868226083UL, 10784497444694088416UL,
#| 4442318623009455690UL, 7578904045792755239UL, 4660917127407640248UL, 1338871040102977572UL,
#| 7923968833761895669UL, 16874906449763725362UL, 6867431507591727612UL, 6311558572587278442UL,
#| 11133241125491102835UL, 11738312930013082355UL, 11475845091361842759UL, 1643869946623606078UL,
#| 10415771985971144045UL, 16177299554619494974UL, 539570561320537202UL, 14686903393511273001UL,
#| 4445884513647149105UL, 5098090666552700373UL, 15054419504752089185UL, 3474469100259604016UL,
#| 6290470378995655110UL, 12581245818984890955UL, 12787308417917333104UL, 15615073174539148765UL,
#| 12984984669203863910UL, 5985056152809872019UL, 11524779172526084917UL, 12665285781050723551UL,
#| 17258307941988319274UL, 1228317169172941352UL, 11160397433235040502UL, 9071341095814202162UL,
#| 16466220084500004578UL, 17881879368693790694UL, 11823018491644357805UL, 16578188432210840351UL,
#| 9406010430949605407UL, 11905115929048085077UL, 9944213925190004442UL, 7029678422186467496UL,
#| 11401152387492259905UL, 10919895159361451251UL, 8495109194203518959UL, 16018667729961568082UL,
#| 167446916071967039UL, 5832416432401633017UL, 2555624166217836526UL, 4693948454793001376UL,
#| 15027938303073958689UL, 3816085682362256244UL, 3185235675206645589UL, 14811052307061536715UL,
#| 16390566561388972206UL, 16262619695511054469UL, 18052593823409859051UL, 3945540604022845918UL,
#| 15826009464703323776UL, 7719326347171925540UL, 9769778031520080067UL, 421859423473436660UL,
#| 13367093898566883586UL, 6779889834239319169UL, 609582038820777224UL, 8614685872506396309UL,
#| 4322299411370527537UL, 12846241946314463225UL, 7603764924892388925UL, 14478986206635481787UL,
#| 12755177540574038518UL, 6194314060039108547UL, 7432498317049243916UL, 13744344095303706494UL,
#| 4564573594452194471UL, 15206391374377392170UL, 14985029622013689232UL, 14328768324291300885UL,
#| 10654090591156151429UL, 7338671807663089189UL, 16334907285225846266UL, 7423398075744368270UL,
#| 13908293662617472937UL, 17175512688958868014UL, 7864028190390018941UL, 13126986956610526127UL,
#| 7926473773501694475UL, 1941817884837167576UL, 15856689629067382306UL, 11177334980354607906UL,
#| 7146885710676518584UL, 15983324774902192370UL, 17386093453964105449UL, 4150817240659351930UL,
#| 7715942048190956297UL, 7276773301104081815UL, 13271959532491042945UL, 1194118668544183835UL,
#| 17605071903565571062UL, 8948583740938417863UL, 3864418170274807074UL, 3699237212540510096UL,
#| 14714003198217388528UL, 14269453830925281739UL, 8957047989018367339UL, 12402048382093370001UL,
#| 11758047802384768757UL, 7093460309329455699UL, 384408371497488372UL, 1458858056370266940UL,
#| 15774153359596962328UL, 5577985376535770293UL, 3965786813088619889UL, 17835800480481320920UL,
#| 32994991092960231UL, 17146972595756459521UL, 12264557860820767038UL, 13718959782553074566UL,
#| 1372337231943282583UL, 15958043944784669526UL, 17520298198669188203UL, 3031291504644241637UL,
#| 6514984797562383624UL, 4345071177348538448UL, 3359260195018829989UL, 2607793539626362761UL,
#| 16792663172613334423UL, 9898393582456908268UL, 2786478731415243607UL, 12029113901756054096UL,
#| 3691479334576095829UL, 11929280456476427793UL, 18327972477027512052UL, 254886074006853656UL,
#| 2927328918014182935UL, 4439939690082200099UL, 5969478689443458641UL, 14369317615642310767UL,
#| 17764732325494572140UL, 10115938472474762880UL, 369466901724782737UL, 2269989694701338999UL,
#| 1231466417883291608UL, 4918841702239877720UL, 13883670878602349637UL, 11470126477371023265UL,
#| 17128636052794644986UL, 205335811880395712UL, 12443170494233065292UL, 15986026963143729439UL,
#| ]
#| @test.assert_eq(res, expected)
#|}
),
},
)
///|
let moonbitlang_core_range_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/range",
deps={},
files={
"range.mbt": (
#|priv enum Direction {
#| Backward
#| Still
#| Forward
#|}
#|trait Step: Add + Compare {
#| fn direction(Self) -> Int32
#| fn step_one() -> Self
#| fn enumerate(from : Self, to : Self, step : Self, inclusive : Bool) -> Iter[
#| Self,
#| ] = _
#|}
#|impl Step with fn enumerate(from, to, step, inclusive) {
#| let dir = direction_of(step)
#| let mut i = from
#| let mut done = false
#| Iter(() => {
#| guard !done else { None }
#| guard in_range(dir, i, to) || (inclusive && i == to) else { None }
#| let value = i
#| let next = i + step
#| if progresses(dir, i, next) {
#| i = next
#| } else {
#| done = true
#| }
#| Some(value)
#| })
#|}
#|fn[T : Step] direction_of(step : T) -> Direction {
#| let n = T::direction(step)
#| if n > 0 {
#| Forward
#| } else if n < 0 {
#| Backward
#| } else {
#| Still
#| }
#|}
#|fn[T : Compare] in_range(dir : Direction, i : T, to : T) -> Bool {
#| match dir {
#| Forward => Compare::compare(i, to) < 0
#| Backward => Compare::compare(i, to) > 0
#| Still => false
#| }
#|}
#|fn[T : Compare] progresses(dir : Direction, i : T, next : T) -> Bool {
#| match dir {
#| Forward => Compare::compare(next, i) > 0
#| Backward => Compare::compare(next, i) < 0
#| Still => false
#| }
#|}
#|const COUNTER_LIMIT : Double = 9007199254740992.0
#|pub impl Step for Int32 with fn direction(self) {
#| if self > 0 {
#| 1
#| } else if self < 0 {
#| -1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for Int32 with fn step_one() {
#| 1
#|}
#|pub impl Step for Int64 with fn direction(self) {
#| if self > 0L {
#| 1
#| } else if self < 0L {
#| -1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for Int64 with fn step_one() {
#| 1L
#|}
#|pub impl Step for UInt32 with fn direction(self) {
#| if self > 0U {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for UInt32 with fn step_one() {
#| 1U
#|}
#|pub impl Step for UInt64 with fn direction(self) {
#| if self > 0UL {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for UInt64 with fn step_one() {
#| 1UL
#|}
#|pub impl Step for @float.Float with fn direction(self) {
#| if self > @float.Float(0.0) {
#| 1
#| } else if self < @float.Float(0.0) {
#| -1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for @float.Float with fn step_one() {
#| @float.Float(1.0)
#|}
#|pub impl Step for @float.Float with fn enumerate(from, to, step, inclusive) {
#| let from_d = from.to_double()
#| let step_d = step.to_double()
#| let dir = direction_of(step)
#| let mut i = from
#| let mut k = 1.0
#| let mut done = false
#| Iter(() => {
#| guard !done else { None }
#| guard in_range(dir, i, to) || (inclusive && i == to) else { None }
#| let value = i
#| let next = @float.Float::from_double(from_d + k * step_d)
#| k = k + 1.0
#| if progresses(dir, i, next) {
#| i = next
#| } else {
#| done = true
#| }
#| Some(value)
#| })
#|}
#|pub impl Step for Double with fn direction(self) {
#| if self > 0.0 {
#| 1
#| } else if self < 0.0 {
#| -1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for Double with fn step_one() {
#| 1.0
#|}
#|pub impl Step for Double with fn enumerate(from, to, step, inclusive) {
#| let dir = direction_of(step)
#| let mut i = from
#| let mut k = 1.0
#| let mut done = false
#| Iter(() => {
#| guard !done else { None }
#| guard in_range(dir, i, to) || (inclusive && i == to) else { None }
#| let value = i
#| let next = if k >= COUNTER_LIMIT {
#| i + step
#| } else {
#| let offset = k * step
#| if offset.is_inf() {
#| (from * 0.5 + k * (step * 0.5)) * 2.0
#| } else {
#| from + offset
#| }
#| }
#| k = k + 1.0
#| if progresses(dir, i, next) {
#| i = next
#| } else {
#| done = true
#| }
#| Some(value)
#| })
#|}
#|pub impl Step for @int16.Int16 with fn direction(self) {
#| if self > @int16.Int16(0) {
#| 1
#| } else if self < @int16.Int16(0) {
#| -1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for @int16.Int16 with fn step_one() {
#| @int16.Int16(1)
#|}
#|pub impl Step for UInt16 with fn direction(self) {
#| if self > UInt16(0) {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for UInt16 with fn step_one() {
#| UInt16(1)
#|}
#|pub impl Step for Byte with fn direction(self) {
#| if self > Byte(0) {
#| 1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for Byte with fn step_one() {
#| Byte(1)
#|}
#|pub impl Step for @bigint.BigInt with fn direction(self) {
#| if self > 0N {
#| 1
#| } else if self < 0N {
#| -1
#| } else {
#| 0
#| }
#|}
#|pub impl Step for @bigint.BigInt with fn step_one() {
#| 1N
#|}
#|#label_migration(start, alias=from, msg="Use start= instead")
#|#label_migration(end, alias=to, msg="Use end= instead")
#|pub fn[T : Step] iter(
#| start~ : T,
#| end~ : T,
#| step~ : T = T::step_one(),
#| inclusive~ : Bool = false,
#|) -> Iter[T] {
#| T::enumerate(start, end, step, inclusive)
#|}
),
},
)
///|
let moonbitlang_core_ref_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/ref",
deps={},
files={
"debug.mbt": (
#|pub impl[T : @debug.Debug] @debug.Debug for Ref[T] with fn to_repr(self) {
#| @debug.Repr::opaque_("Ref", Repr(self.val))
#|}
#|test "Debug for Ref" {
#| let r : Ref[Int32] = Ref(1)
#| @debug.debug_inspect(r, content="")
#|}
),
"extends.mbt": (
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Ref with @debug.Debug::{to_repr}
#|#deprecated("Use `@debug.Debug` instead of `Show`", skip_current_package=true)
#|#doc(hidden)
#|pub extend Ref with Show::{to_string}
),
"ref.mbt": (
#|pub(all) struct Ref[T] {
#| mut val : T
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[X : Show] Show for Ref[X]
#|pub impl[X : Show] Show for Ref[X] with fn output(self, logger) {
#| logger <+
#| $|{val: \{self.val}}
#|}
#|#alias(new, deprecated="Use `Ref(x)` instead")
#|#owned(x)
#|pub fn[T] Ref::Ref(x : T) -> Ref[T] {
#| { val: x, }
#|}
#|test "to_string" {
#| @debug.debug_inspect(
#| Ref(3),
#| content=(
#| #|
#| ),
#| )
#|}
#|#deprecated("Use `Ref(x)` instead")
#|#owned(x)
#|pub fn[T] new(x : T) -> Ref[T] {
#| { val: x, }
#|}
#|pub fn[T, R] Ref::map(self : Ref[T], f : (T) -> R raise?) -> Ref[R] raise? {
#| { val: f(self.val), }
#|}
#|pub fn[T, R] Ref::protect(self : Ref[T], a : T, f : () -> R raise?) -> R raise? {
#| let old = self.val
#| self.val = a
#| defer {
#| self.val = old
#| }
#| f()
#|}
#|#as_free_fn
#|pub fn[T] Ref::swap(self : Ref[T], that : Ref[T]) -> Unit {
#| let tmp = self.val
#| self.val = that.val
#| that.val = tmp
#|}
#|test "swap" {
#| let x = Ref(1)
#| let y = Ref(2)
#| swap(x, y)
#| inspect(x.val, content="2")
#| inspect(y.val, content="1")
#|}
#|pub fn[T] Ref::update(self : Ref[T], f : (T) -> T raise?) -> Unit raise? {
#| self.val = f(self.val)
#|}
#|test "decr" {
#| let a = Ref(1)
#| a.val -= 1
#| inspect(a.val, content="0")
#| a.val -= 5
#| inspect(a.val, content="-5")
#|}
#|test "incr" {
#| let a = Ref(1)
#| a.val += 1
#| inspect(a.val, content="2")
#| a.val += 5
#| inspect(a.val, content="7")
#|}
),
},
)
///|
let moonbitlang_core_result_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/result",
deps={},
files={
"deprecated.mbt": (
#|#deprecated("Use `Ok(value)` instead")
#|pub fn[T, E] ok(value : T) -> Result[T, E] {
#| Ok(value)
#|}
#|#deprecated("Use `Err(value)` instead")
#|pub fn[T, E] err(value : E) -> Result[T, E] {
#| Err(value)
#|}
),
},
)
///|
let moonbitlang_core_set_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/set",
deps={},
files={
"debug.mbt": (
#|pub impl[K : @debug.Debug] @debug.Debug for Set[K] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "Set",
#| @debug.Repr::array(self.to_array().map(x => Repr(x))),
#| )
#|}
#|test "Debug for Set" {
#| let s : Set[Int32] = from_array([1, 2, 3])
#| @debug.debug_inspect(s, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `Set([], capacity=...)` instead")
#|#as_free_fn(deprecated="Use `Set([], capacity=...)` instead")
#|pub fn[K] Set::new(capacity~ : Int32 = default_init_capacity) -> Set[K] {
#| new_set(capacity)
#|}
#|#deprecated("The bucket count is an implementation detail; the constructor's `capacity` hint is the only sizing control")
#|pub fn[K] Set::capacity(self : Set[K]) -> Int32 {
#| self.capacity
#|}
),
"extends.mbt": (
#|pub extend Set with BitAnd::{land}
#|pub extend Set with BitOr::{lor}
#|pub extend Set with BitXOr::{lxor}
#|pub extend Set with Eq::{equal}
#|pub extend Set with Sub::{sub}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Set with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Set with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Set with Eq::{not_equal}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Set with @json.FromJson::{from_json}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend Set with @json.ToJson::{to_json}
),
"grow_heuristic.mbt": (
#|fn calc_grow_threshold(capacity : Int32) -> Int32 {
#| capacity / 16 * 13 + capacity % 16 * 13 / 16
#|}
),
"linked_hash_set.mbt": (
#|#unsafe_cycle_free
#|priv struct Entry[K] {
#| mut prev : Int32
#| mut next : Entry[K]?
#| mut psl : Int32
#| hash : Int32
#| key : K
#|}
#|struct Set[K] {
#| mut entries : FixedArray[Entry[K]?]
#| mut size : Int32 // active keys count
#| mut capacity : Int32 // current capacity
#| mut capacity_mask : Int32 // capacity_mask = capacity - 1, used to find idx
#| mut grow_at : Int32 // threshold that triggers grow
#| mut head : Entry[K]? // head of linked list
#| mut tail : Int32 // tail of linked list
#|}
#|let default_init_capacity = 8
#|fn[K] new_set(capacity : Int32) -> Set[K] {
#| let capacity = capacity.next_power_of_two()
#| {
#| size: 0,
#| capacity,
#| capacity_mask: capacity - 1,
#| grow_at: calc_grow_threshold(capacity),
#| entries: FixedArray::make(capacity, None),
#| head: None,
#| tail: -1,
#| }
#|}
#|fn capacity_for_length(length : Int32) -> Int32 {
#| guard length <= calc_grow_threshold(0x40000000) else {
#| abort("Set capacity overflow")
#| }
#| let mut capacity = length.next_power_of_two()
#| if length > calc_grow_threshold(capacity) {
#| capacity *= 2
#| }
#| capacity
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[K : Hash + Eq] Set::Set(arr : ArrayView[K], capacity? : Int32) -> Set[K] {
#| let length = arr.length()
#| let capacity = match capacity {
#| Some(capacity) => {
#| guard capacity >= 0 else { abort("capacity must be non-negative") }
#| capacity_for_length(capacity.max(length))
#| }
#| None =>
#| if length == 0 {
#| default_init_capacity
#| } else {
#| capacity_for_length(length)
#| }
#| }
#| let m = new_set(capacity)
#| for e in arr {
#| m.insert(e)
#| }
#| m
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|pub fn[K : Hash + Eq] Set::insert(self : Set[K], key : K) -> Unit {
#| self.add_with_hash(key, Hash::hash(key))
#|}
#|fn[K : Eq] Set::add_with_hash(self : Set[K], key : K, hash : Int32) -> Unit {
#| let (idx, psl) = for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| if self.size >= self.grow_at {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| break (idx, psl)
#| }
#| Some(curr_entry) => {
#| if curr_entry.hash == hash && curr_entry.key == key {
#| return
#| }
#| if psl > curr_entry.psl {
#| if self.size >= self.grow_at {
#| self.grow()
#| continue 0, hash & self.capacity_mask
#| }
#| self.push_away(idx, curr_entry)
#| break (idx, psl)
#| }
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#| let entry = { prev: self.tail, next: None, psl, key, hash, }
#| self.add_entry_to_tail(idx, entry)
#|}
#|#owned(entry)
#|fn[K] Set::push_away(self : Set[K], idx : Int32, entry : Entry[K]) -> Unit {
#| for psl = entry.psl + 1, idx = (idx + 1) & self.capacity_mask, entry = entry {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| break
#| }
#| Some(curr_entry) =>
#| if psl > curr_entry.psl {
#| entry.psl = psl
#| self.set_entry(entry, idx)
#| continue curr_entry.psl + 1,
#| (idx + 1) & self.capacity_mask,
#| curr_entry
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask, entry
#| }
#| }
#| }
#|}
#|#owned(entry)
#|fn[K] Set::set_entry(self : Set[K], entry : Entry[K], new_idx : Int32) -> Unit {
#| match entry.next {
#| None => self.tail = new_idx
#| Some(next) => next.prev = new_idx
#| }
#| self.entries.unsafe_set(new_idx, Some(entry))
#|}
#|#alias(add_and_check, deprecated="Use `insert_and_check` instead")
#|pub fn[K : Hash + Eq] Set::insert_and_check(self : Set[K], key : K) -> Bool {
#| let size = self.size
#| self.add_with_hash(key, Hash::hash(key))
#| self.size > size
#|}
#|pub fn[K : Hash + Eq] Set::contains(self : Set[K], key : K) -> Bool {
#| self.contains_with_hash(key, Hash::hash(key))
#|}
#|#inline
#|fn[K : Eq] Set::contains_with_hash(
#| self : Set[K],
#| key : K,
#| hash : Int32,
#|) -> Bool {
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break false }
#| if entry.hash == hash && entry.key == key {
#| break true
#| }
#| if i > entry.psl {
#| break false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq] Set::remove(self : Set[K], key : K) -> Unit {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break }
#| if entry.hash == hash && entry.key == key {
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| break
#| }
#| if i > entry.psl {
#| break
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|pub fn[K : Hash + Eq] Set::remove_and_check(self : Set[K], key : K) -> Bool {
#| let hash = Hash::hash(key)
#| for i = 0, idx = hash & self.capacity_mask {
#| guard self.entries.unsafe_get(idx) is Some(entry) else { break false }
#| if entry.hash == hash && entry.key == key {
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| break true
#| }
#| if i > entry.psl {
#| break false
#| }
#| continue i + 1, (idx + 1) & self.capacity_mask
#| }
#|}
#|#owned(entry)
#|fn[K] Set::add_entry_to_tail(
#| self : Set[K],
#| idx : Int32,
#| entry : Entry[K],
#|) -> Unit {
#| match self.tail {
#| -1 => self.head = Some(entry)
#| tail => self.entries[tail].unwrap().next = Some(entry)
#| }
#| self.tail = idx
#| self.entries.unsafe_set(idx, Some(entry))
#| self.size += 1
#|}
#|fn[K] Set::remove_entry(self : Set[K], entry : Entry[K]) -> Unit {
#| match entry.prev {
#| -1 => self.head = entry.next
#| idx => self.entries[idx].unwrap().next = entry.next
#| }
#| match entry.next {
#| None => self.tail = entry.prev
#| Some(next) => next.prev = entry.prev
#| }
#|}
#|fn[K] Set::shift_back(self : Set[K], idx : Int32) -> Unit {
#| for cur = idx {
#| let next = (cur + 1) & self.capacity_mask
#| match self.entries.unsafe_get(next) {
#| None | Some({ psl: 0, .. }) => {
#| self.entries.unsafe_set(cur, None)
#| break
#| }
#| Some(entry) => {
#| entry.psl -= 1
#| self.set_entry(entry, cur)
#| continue next
#| }
#| }
#| }
#|}
#|fn[K] Set::grow(self : Set[K]) -> Unit {
#| let old_head = self.head
#| let new_capacity = self.capacity << 1
#| self.entries = FixedArray::make(new_capacity, None)
#| self.capacity = new_capacity
#| self.capacity_mask = new_capacity - 1
#| self.grow_at = calc_grow_threshold(self.capacity)
#| self.size = 0
#| self.head = None
#| self.tail = -1
#| for x = old_head {
#| match x {
#| None => break
#| Some(e) => {
#| let next_in_chain = e.next
#| e.next = None
#| self.rehash_place_entry(e)
#| continue next_in_chain
#| }
#| }
#| }
#|}
#|#owned(outer)
#|fn[K] Set::rehash_place_entry(self : Set[K], outer : Entry[K]) -> Unit {
#| let hash = outer.hash
#| for psl = 0, idx = hash & self.capacity_mask {
#| match self.entries.unsafe_get(idx) {
#| None => {
#| outer.psl = psl
#| outer.prev = self.tail
#| self.add_entry_to_tail(idx, outer)
#| return
#| }
#| Some(curr) =>
#| if psl > curr.psl {
#| self.push_away(idx, curr)
#| outer.psl = psl
#| outer.prev = self.tail
#| self.add_entry_to_tail(idx, outer)
#| return
#| } else {
#| continue psl + 1, (idx + 1) & self.capacity_mask
#| }
#| }
#| }
#|}
#|#alias(size, deprecated)
#|pub fn[K] Set::length(self : Set[K]) -> Int32 {
#| self.size
#|}
#|pub fn[K] Set::is_empty(self : Set[K]) -> Bool {
#| self.size == 0
#|}
#|#locals(f)
#|pub fn[K] Set::each(self : Set[K], f : (K) -> Unit raise?) -> Unit raise? {
#| for x = self.head {
#| match x {
#| Some({ key, next, .. }) => {
#| f(key)
#| continue next
#| }
#| None => break
#| }
#| }
#|}
#|#locals(f)
#|pub fn[K] Set::eachi(
#| self : Set[K],
#| f : (Int32, K) -> Unit raise?,
#|) -> Unit raise? {
#| for i = 0, curr = self.head {
#| match (i, curr) {
#| (i, Some({ key, next, .. })) => {
#| f(i, key)
#| continue i + 1, next
#| }
#| (_, None) => break
#| }
#| }
#|}
#|pub fn[K] Set::clear(self : Set[K]) -> Unit {
#| self.entries.fill(None)
#| self.size = 0
#| self.head = None
#| self.tail = -1
#|}
#|#alias(iterator, deprecated)
#|pub fn[K] Set::iter(self : Set[K]) -> Iter[K] {
#| let mut curr_entry = self.head
#| let len = self.size
#| let mut remaining = len
#| Iter(
#| fn() {
#| guard remaining > 0 && curr_entry is Some({ key, next, .. }) else { None }
#| curr_entry = next
#| remaining -= 1
#| Some(key)
#| },
#| size_hint=len,
#| )
#|}
#|pub fn[K] Set::to_array(self : Set[K]) -> Array[K] {
#| let arr = Array(capacity=self.size)
#| for x = self.head {
#| match x {
#| Some({ key, next, .. }) => {
#| arr.push(key)
#| continue next
#| }
#| None => break
#| }
#| }
#| arr
#|}
#|pub impl[K : Eq] Eq for Set[K] with fn equal(self, other) {
#| self.size == other.size && self.all_in(other)
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Hash + Eq] Set::from_iter(iter : Iter[K]) -> Set[K] {
#| let m = new_set(default_init_capacity)
#| while iter.next() is Some(e) {
#| m.insert(e)
#| }
#| m
#|}
#|pub impl[K] Default for Set[K] with fn default() {
#| new_set(default_init_capacity)
#|}
#|#alias(clone, deprecated)
#|pub fn[K] Set::copy(self : Set[K]) -> Set[K] {
#| let other = {
#| capacity: self.capacity,
#| entries: FixedArray::make(self.capacity, None),
#| size: self.size,
#| capacity_mask: self.capacity_mask,
#| grow_at: self.grow_at,
#| head: None,
#| tail: self.tail,
#| }
#| if self.size == 0 {
#| return other
#| }
#| guard! self.entries[self.tail] is Some(last)
#| for entry = last, idx = self.tail, next = (None : Entry[K]?) {
#| match (entry, idx, next) {
#| ({ prev, psl, hash, key, .. }, idx, next) => {
#| let new_entry = { prev, next, psl, hash, key, }
#| other.entries[idx] = Some(new_entry)
#| if prev != -1 {
#| continue self.entries[prev].unwrap(), prev, Some(new_entry)
#| } else {
#| other.head = Some(new_entry)
#| break
#| }
#| }
#| }
#| }
#| other
#|}
#|#locals(f)
#|pub fn[K] Set::retain(self : Set[K], f : (K) -> Bool) -> Unit {
#| for x = self.head, remove_prev = false {
#| match x {
#| Some({ key, next, prev: idx, .. }) => {
#| if remove_prev {
#| guard! self.entries[idx] is Some(entry)
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| }
#| continue next, !f(key)
#| }
#| None => {
#| if remove_prev {
#| let idx = self.tail
#| guard! self.entries[idx] is Some(entry)
#| self.remove_entry(entry)
#| self.shift_back(idx)
#| self.size -= 1
#| }
#| break
#| }
#| }
#| }
#|}
#|pub fn[K] Set::filter(self : Set[K], f : (K) -> Bool raise?) -> Set[K] raise? {
#| let kept : Array[Entry[K]] = []
#| for x = self.head {
#| match x {
#| Some({ key, hash, next, .. }) => {
#| if f(key) {
#| kept.push({ prev: -1, next: None, psl: 0, key, hash, })
#| }
#| continue next
#| }
#| None => break
#| }
#| }
#| let other = new_set(
#| if kept.is_empty() {
#| default_init_capacity
#| } else {
#| capacity_for_length(kept.length())
#| },
#| )
#| for entry in kept {
#| other.rehash_place_entry(entry)
#| }
#| other
#|}
#|pub fn[K : Eq] Set::difference(self : Set[K], other : Set[K]) -> Set[K] {
#| let m = new_set(default_init_capacity)
#| m.add_missing_from(self, other)
#| m
#|}
#|fn[K : Eq] Set::add_missing_from(
#| self : Set[K],
#| from : Set[K],
#| other : Set[K],
#|) -> Unit {
#| for x = from.head {
#| match x {
#| Some({ key, hash, next, .. }) => {
#| if !other.contains_with_hash(key, hash) {
#| self.add_with_hash(key, hash)
#| }
#| continue next
#| }
#| None => break
#| }
#| }
#|}
#|pub fn[K : Eq] Set::symmetric_difference(
#| self : Set[K],
#| other : Set[K],
#|) -> Set[K] {
#| let m = new_set(default_init_capacity)
#| m.add_missing_from(self, other)
#| m.add_missing_from(other, self)
#| m
#|}
#|pub fn[K : Eq] Set::union(self : Set[K], other : Set[K]) -> Set[K] {
#| let m = self.copy()
#| m.union_in_place(other)
#| m
#|}
#|pub fn[K : Eq] Set::union_in_place(self : Set[K], other : Set[K]) -> Unit {
#| if physical_equal(self, other) {
#| return
#| }
#| for x = other.head {
#| match x {
#| Some({ key, hash, next, .. }) => {
#| self.add_with_hash(key, hash)
#| continue next
#| }
#| None => break
#| }
#| }
#|}
#|pub fn[K : Eq] Set::intersection(self : Set[K], other : Set[K]) -> Set[K] {
#| let m = new_set(default_init_capacity)
#| for x = self.head {
#| match x {
#| Some({ key, hash, next, .. }) => {
#| if other.contains_with_hash(key, hash) {
#| m.add_with_hash(key, hash)
#| }
#| continue next
#| }
#| None => break
#| }
#| }
#| m
#|}
#|fn[K : Eq] Set::all_in(self : Set[K], other : Set[K]) -> Bool {
#| for x = self.head {
#| match x {
#| Some({ key, hash, next, .. }) => {
#| guard other.contains_with_hash(key, hash) else { break false }
#| continue next
#| }
#| None => break true
#| }
#| }
#|}
#|fn[K : Eq] Set::none_in(self : Set[K], other : Set[K]) -> Bool {
#| for x = self.head {
#| match x {
#| Some({ key, hash, next, .. }) => {
#| guard !other.contains_with_hash(key, hash) else { break false }
#| continue next
#| }
#| None => break true
#| }
#| }
#|}
#|pub impl[X : @json.ToJson] @json.ToJson for Set[X] with fn to_json(self) {
#| let res = Array(capacity=self.size)
#| for v in self {
#| res.push(v.to_json())
#| }
#| @json.Json::array(res)
#|}
#|pub impl[X : @json.FromJson + Hash + Eq] @json.FromJson for Set[X] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Array(arr) else {
#| raise path.error("@set.from_json: expected array")
#| }
#| let res : Set[X] = new_set(
#| if arr.is_empty() {
#| default_init_capacity
#| } else {
#| capacity_for_length(arr.length())
#| },
#| )
#| for i, x in arr {
#| res.insert(@json.FromJson::from_json(x, path.add_index(i)))
#| }
#| res
#|}
#|pub fn[K : Eq] Set::is_disjoint(self : Set[K], other : Set[K]) -> Bool {
#| if self.length() <= other.length() {
#| self.none_in(other)
#| } else {
#| other.none_in(self)
#| }
#|}
#|pub fn[K : Eq] Set::is_subset(self : Set[K], other : Set[K]) -> Bool {
#| self.length() <= other.length() && self.all_in(other)
#|}
#|pub fn[K : Eq] Set::is_superset(self : Set[K], other : Set[K]) -> Bool {
#| other.is_subset(self)
#|}
#|pub impl[K : Eq] BitAnd for Set[K] with fn land(self, other) {
#| self.intersection(other)
#|}
#|pub impl[K : Eq] BitOr for Set[K] with fn lor(self, other) {
#| self.union(other)
#|}
#|pub impl[K : Eq] BitXOr for Set[K] with fn lxor(self, other) {
#| self.symmetric_difference(other)
#|}
#|pub impl[K : Eq] Sub for Set[K] with fn sub(self, other) {
#| self.difference(other)
#|}
),
},
)
///|
let moonbitlang_core_simd_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/simd",
deps={},
files={
"lanes.mbt": (
#|fn u8_lane(value : V128, lane : Int32) -> Byte {
#| let word = if lane < 8 { lo(value) } else { hi(value) }
#| ((word >> (lane % 8 * 8)) & 0xffUL).to_byte()
#|}
#|fn u16_lane(value : V128, lane : Int32) -> UInt16 {
#| let word = if lane < 4 { lo(value) } else { hi(value) }
#| ((word >> (lane % 4 * 16)) & 0xffffUL).to_uint16()
#|}
#|fn u32_lane(value : V128, lane : Int32) -> UInt32 {
#| let word = if lane < 2 { lo(value) } else { hi(value) }
#| ((word >> (lane % 2 * 32)) & 0xffffffffUL).to_uint()
#|}
#|fn u64_lane(value : V128, lane : Int32) -> UInt64 {
#| if lane == 0 {
#| lo(value)
#| } else {
#| hi(value)
#| }
#|}
#|fn pack_u8x16_by(f : (Int32) -> Byte) -> V128 {
#| let mut lo_word = 0UL
#| for i in 0..<8 {
#| lo_word = lo_word | (f(i).to_uint64() << (i * 8))
#| }
#| let mut hi_word = 0UL
#| for i in 0..<8 {
#| hi_word = hi_word | (f(i + 8).to_uint64() << (i * 8))
#| }
#| make(lo_word, hi_word)
#|}
#|fn pack_u16x8_by(f : (Int32) -> UInt16) -> V128 {
#| let mut lo_word = 0UL
#| for i in 0..<4 {
#| lo_word = lo_word | (f(i).to_uint64() << (i * 16))
#| }
#| let mut hi_word = 0UL
#| for i in 0..<4 {
#| hi_word = hi_word | (f(i + 4).to_uint64() << (i * 16))
#| }
#| make(lo_word, hi_word)
#|}
#|fn pack_u32x4_by(f : (Int32) -> UInt32) -> V128 {
#| make(
#| f(0).to_uint64() | (f(1).to_uint64() << 32),
#| f(2).to_uint64() | (f(3).to_uint64() << 32),
#| )
#|}
#|fn pack_u64x2_by(f : (Int32) -> UInt64) -> V128 {
#| make(f(0), f(1))
#|}
#|fn widen_u8x4_to_u16x4(value : UInt64) -> UInt64 {
#| (value & 0xffUL) |
#| ((value & 0xff00UL) << 8) |
#| ((value & 0xff0000UL) << 16) |
#| ((value & 0xff000000UL) << 24)
#|}
#|fn sign_extend_u8x4_to_u16x4(value : UInt64) -> UInt64 {
#| let value = widen_u8x4_to_u16x4(value)
#| value | (((value & 0x0080008000800080UL) << 1) * 0xffUL)
#|}
#|fn widen_u16x2_to_u32x2(value : UInt64) -> UInt64 {
#| (value & 0xffffUL) | ((value & 0xffff0000UL) << 16)
#|}
#|fn sign_extend_u16x2_to_u32x2(value : UInt64) -> UInt64 {
#| let value = widen_u16x2_to_u32x2(value)
#| value | (((value & 0x0000800000008000UL) << 1) * 0xffffUL)
#|}
#|fn sign_extend_u32_to_u64(value : UInt64) -> UInt64 {
#| let value = value & 0xffffffffUL
#| if (value & 0x80000000UL) != 0UL {
#| value | 0xffffffff00000000UL
#| } else {
#| value
#| }
#|}
#|fn i8_signed(value : Byte) -> Int32 {
#| let value = value.to_int()
#| if value >= 128 {
#| value - 256
#| } else {
#| value
#| }
#|}
#|fn i16_signed(value : UInt16) -> Int32 {
#| @int16.Int16::reinterpret_from_uint16(value).to_int()
#|}
#|fn bool_to_u8_mask(value : Bool) -> Byte {
#| if value {
#| 0xff
#| } else {
#| 0
#| }
#|}
#|fn bool_to_u16_mask(value : Bool) -> UInt16 {
#| if value {
#| 0xffff
#| } else {
#| 0
#| }
#|}
#|fn bool_to_u32_mask(value : Bool) -> UInt32 {
#| if value {
#| 0xffffffff
#| } else {
#| 0
#| }
#|}
#|fn bool_to_u64_mask(value : Bool) -> UInt64 {
#| if value {
#| 0xffffffffffffffffUL
#| } else {
#| 0UL
#| }
#|}
#|fn i8_abs(value : Byte) -> Byte {
#| let value = i8_signed(value)
#| if value < 0 {
#| (-value).to_byte()
#| } else {
#| value.to_byte()
#| }
#|}
#|fn i16_abs(value : UInt16) -> UInt16 {
#| @int16.Int16::abs(@int16.Int16::reinterpret_from_uint16(value)).reinterpret_as_uint16()
#|}
#|fn i8_add_sat_s(a : Byte, b : Byte) -> Byte {
#| let sum = i8_signed(a) + i8_signed(b)
#| if sum > 127 {
#| 0x7f
#| } else if sum < -128 {
#| 0x80
#| } else {
#| sum.to_byte()
#| }
#|}
#|fn i8_add_sat_u(a : Byte, b : Byte) -> Byte {
#| let sum = a.to_uint() + b.to_uint()
#| if sum > 255 {
#| 0xff
#| } else {
#| sum.to_byte()
#| }
#|}
#|fn i8_sub_sat_s(a : Byte, b : Byte) -> Byte {
#| let diff = i8_signed(a) - i8_signed(b)
#| if diff > 127 {
#| 0x7f
#| } else if diff < -128 {
#| 0x80
#| } else {
#| diff.to_byte()
#| }
#|}
#|fn i8_sub_sat_u(a : Byte, b : Byte) -> Byte {
#| if a < b {
#| 0
#| } else {
#| a - b
#| }
#|}
#|fn i8_min_s(a : Byte, b : Byte) -> Byte {
#| if i8_signed(a) < i8_signed(b) {
#| a
#| } else {
#| b
#| }
#|}
#|fn i8_max_s(a : Byte, b : Byte) -> Byte {
#| if i8_signed(a) > i8_signed(b) {
#| a
#| } else {
#| b
#| }
#|}
#|fn i8_avgr_u(a : Byte, b : Byte) -> Byte {
#| ((a.to_uint() + b.to_uint() + 1) / 2).to_byte()
#|}
#|fn i16_add_sat_s(a : UInt16, b : UInt16) -> UInt16 {
#| let sum = i16_signed(a) + i16_signed(b)
#| if sum > 32767 {
#| 0x7fff
#| } else if sum < -32768 {
#| 0x8000
#| } else {
#| sum.to_uint16()
#| }
#|}
#|fn i16_add_sat_u(a : UInt16, b : UInt16) -> UInt16 {
#| let sum = a.to_uint() + b.to_uint()
#| if sum > 65535 {
#| 0xffff
#| } else {
#| sum.to_uint16()
#| }
#|}
#|fn i16_sub_sat_s(a : UInt16, b : UInt16) -> UInt16 {
#| let diff = i16_signed(a) - i16_signed(b)
#| if diff > 32767 {
#| 0x7fff
#| } else if diff < -32768 {
#| 0x8000
#| } else {
#| diff.to_uint16()
#| }
#|}
#|fn i16_sub_sat_u(a : UInt16, b : UInt16) -> UInt16 {
#| if a < b {
#| 0
#| } else {
#| a - b
#| }
#|}
#|fn i16_min_s(a : UInt16, b : UInt16) -> UInt16 {
#| if i16_signed(a) < i16_signed(b) {
#| a
#| } else {
#| b
#| }
#|}
#|fn i16_max_s(a : UInt16, b : UInt16) -> UInt16 {
#| if i16_signed(a) > i16_signed(b) {
#| a
#| } else {
#| b
#| }
#|}
#|fn i16_avgr_u(a : UInt16, b : UInt16) -> UInt16 {
#| ((a.to_uint() + b.to_uint() + 1) / 2).to_uint16()
#|}
#|#inline
#|fn zip_u8x4_by(
#| left : UInt32,
#| right : UInt32,
#| f : (Byte, Byte) -> Byte,
#|) -> UInt32 {
#| f(left.to_byte(), right.to_byte()).to_uint() |
#| (f((left >> 8).to_byte(), (right >> 8).to_byte()).to_uint() << 8) |
#| (f((left >> 16).to_byte(), (right >> 16).to_byte()).to_uint() << 16) |
#| (f((left >> 24).to_byte(), (right >> 24).to_byte()).to_uint() << 24)
#|}
#|#inline
#|fn zip_u8x16_by(value : V128, other : V128, f : (Byte, Byte) -> Byte) -> V128 {
#| let left_low = lo(value)
#| let left_high = hi(value)
#| let right_low = lo(other)
#| let right_high = hi(other)
#| let w0 = zip_u8x4_by(left_low.to_uint(), right_low.to_uint(), f)
#| let w1 = zip_u8x4_by(
#| (left_low >> 32).to_uint(),
#| (right_low >> 32).to_uint(),
#| f,
#| )
#| let w2 = zip_u8x4_by(left_high.to_uint(), right_high.to_uint(), f)
#| let w3 = zip_u8x4_by(
#| (left_high >> 32).to_uint(),
#| (right_high >> 32).to_uint(),
#| f,
#| )
#| make(
#| w0.to_uint64() | (w1.to_uint64() << 32),
#| w2.to_uint64() | (w3.to_uint64() << 32),
#| )
#|}
),
"simd_basic.mbt": (
#|#intrinsic("%v128.v128_const_i8x16")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_const(
#| a0 : Byte,
#| a1 : Byte,
#| a2 : Byte,
#| a3 : Byte,
#| a4 : Byte,
#| a5 : Byte,
#| a6 : Byte,
#| a7 : Byte,
#| a8 : Byte,
#| a9 : Byte,
#| a10 : Byte,
#| a11 : Byte,
#| a12 : Byte,
#| a13 : Byte,
#| a14 : Byte,
#| a15 : Byte,
#|) -> V128 {
#| make(
#| a0.to_uint64() |
#| (a1.to_uint64() << 8) |
#| (a2.to_uint64() << 16) |
#| (a3.to_uint64() << 24) |
#| (a4.to_uint64() << 32) |
#| (a5.to_uint64() << 40) |
#| (a6.to_uint64() << 48) |
#| (a7.to_uint64() << 56),
#| a8.to_uint64() |
#| (a9.to_uint64() << 8) |
#| (a10.to_uint64() << 16) |
#| (a11.to_uint64() << 24) |
#| (a12.to_uint64() << 32) |
#| (a13.to_uint64() << 40) |
#| (a14.to_uint64() << 48) |
#| (a15.to_uint64() << 56),
#| )
#|}
#|#intrinsic("%v128.v128_const_i16x8")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_const(
#| a0 : UInt16,
#| a1 : UInt16,
#| a2 : UInt16,
#| a3 : UInt16,
#| a4 : UInt16,
#| a5 : UInt16,
#| a6 : UInt16,
#| a7 : UInt16,
#|) -> V128 {
#| make(
#| a0.to_uint64() |
#| (a1.to_uint64() << 16) |
#| (a2.to_uint64() << 32) |
#| (a3.to_uint64() << 48),
#| a4.to_uint64() |
#| (a5.to_uint64() << 16) |
#| (a6.to_uint64() << 32) |
#| (a7.to_uint64() << 48),
#| )
#|}
#|#intrinsic("%v128.v128_const_i32x4")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_const(a0 : UInt32, a1 : UInt32, a2 : UInt32, a3 : UInt32) -> V128 {
#| make(
#| a0.to_uint64() | (a1.to_uint64() << 32),
#| a2.to_uint64() | (a3.to_uint64() << 32),
#| )
#|}
#|#intrinsic("%v128.v128_const_i64x2")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_const(a0 : UInt64, a1 : UInt64) -> V128 {
#| make(a0, a1)
#|}
#|#intrinsic("%v128.v128_const_f32x4")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_const(
#| a0 : @float.Float,
#| a1 : @float.Float,
#| a2 : @float.Float,
#| a3 : @float.Float,
#|) -> V128 {
#| i32x4_const(
#| a0.reinterpret_as_uint(),
#| a1.reinterpret_as_uint(),
#| a2.reinterpret_as_uint(),
#| a3.reinterpret_as_uint(),
#| )
#|}
#|#intrinsic("%v128.v128_const_f64x2")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_const(a0 : Double, a1 : Double) -> V128 {
#| i64x2_const(a0.reinterpret_as_uint64(), a1.reinterpret_as_uint64())
#|}
#|#intrinsic("%v128.i8x16_splat")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_splat(value : Byte) -> V128 {
#| let word = value.to_uint64() * 0x0101010101010101UL
#| make(word, word)
#|}
#|#intrinsic("%v128.i16x8_splat")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_splat(value : UInt16) -> V128 {
#| let word = value.to_uint64() * 0x0001000100010001UL
#| make(word, word)
#|}
#|#intrinsic("%v128.i32x4_splat")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_splat(value : UInt32) -> V128 {
#| pack_u32x4_by(_ => value)
#|}
#|#intrinsic("%v128.i64x2_splat")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_splat(value : UInt64) -> V128 {
#| make(value, value)
#|}
#|#intrinsic("%v128.f32x4_splat")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_splat(value : @float.Float) -> V128 {
#| f32x4_const(value, value, value, value)
#|}
#|#intrinsic("%v128.f64x2_splat")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_splat(value : Double) -> V128 {
#| f64x2_const(value, value)
#|}
#|#intrinsic("%v128.i8x16_extract_lane_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_extract_lane_u(value : V128, lane : Int32) -> UInt32 {
#| u8_lane(value, lane).to_uint()
#|}
#|#intrinsic("%v128.i8x16_extract_lane_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_extract_lane_s(value : V128, lane : Int32) -> UInt32 {
#| i8_signed(u8_lane(value, lane)).reinterpret_as_uint()
#|}
#|#intrinsic("%v128.i16x8_extract_lane_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extract_lane_u(value : V128, lane : Int32) -> UInt32 {
#| u16_lane(value, lane).to_uint()
#|}
#|#intrinsic("%v128.i16x8_extract_lane_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extract_lane_s(value : V128, lane : Int32) -> UInt32 {
#| i16_signed(u16_lane(value, lane)).reinterpret_as_uint()
#|}
#|#intrinsic("%v128.i32x4_extract_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extract_lane(value : V128, lane : Int32) -> UInt32 {
#| u32_lane(value, lane)
#|}
#|#intrinsic("%v128.i64x2_extract_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extract_lane(value : V128, lane : Int32) -> UInt64 {
#| u64_lane(value, lane)
#|}
#|#intrinsic("%v128.f32x4_extract_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_extract_lane(value : V128, lane : Int32) -> @float.Float {
#| @float.Float::reinterpret_from_uint(u32_lane(value, lane))
#|}
#|#intrinsic("%v128.f64x2_extract_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_extract_lane(value : V128, lane : Int32) -> Double {
#| u64_lane(value, lane).reinterpret_as_double()
#|}
#|#intrinsic("%v128.i8x16_replace_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_replace_lane(
#| value : V128,
#| replacement : UInt32,
#| lane : Int32,
#|) -> V128 {
#| guard lane >= 0 && lane < 16 else { return value }
#| let shift = (lane & 7) * 8
#| let mask = 0xffUL << shift
#| let bits = (replacement.to_uint64() & 0xffUL) << shift
#| if lane < 8 {
#| make((lo(value) & mask.lnot()) | bits, hi(value))
#| } else {
#| make(lo(value), (hi(value) & mask.lnot()) | bits)
#| }
#|}
#|#intrinsic("%v128.i16x8_replace_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_replace_lane(
#| value : V128,
#| replacement : UInt32,
#| lane : Int32,
#|) -> V128 {
#| guard lane >= 0 && lane < 8 else { return value }
#| let shift = (lane & 3) * 16
#| let mask = 0xffffUL << shift
#| let bits = (replacement.to_uint64() & 0xffffUL) << shift
#| if lane < 4 {
#| make((lo(value) & mask.lnot()) | bits, hi(value))
#| } else {
#| make(lo(value), (hi(value) & mask.lnot()) | bits)
#| }
#|}
#|#intrinsic("%v128.i32x4_replace_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_replace_lane(
#| value : V128,
#| replacement : UInt32,
#| lane : Int32,
#|) -> V128 {
#| guard lane >= 0 && lane < 4 else { return value }
#| let shift = (lane & 1) * 32
#| let mask = 0xffffffffUL << shift
#| let bits = replacement.to_uint64() << shift
#| if lane < 2 {
#| make((lo(value) & mask.lnot()) | bits, hi(value))
#| } else {
#| make(lo(value), (hi(value) & mask.lnot()) | bits)
#| }
#|}
#|#intrinsic("%v128.i64x2_replace_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_replace_lane(
#| value : V128,
#| replacement : UInt64,
#| lane : Int32,
#|) -> V128 {
#| if lane == 0 {
#| make(replacement, hi(value))
#| } else if lane == 1 {
#| make(lo(value), replacement)
#| } else {
#| value
#| }
#|}
#|#intrinsic("%v128.f32x4_replace_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_replace_lane(
#| value : V128,
#| replacement : @float.Float,
#| lane : Int32,
#|) -> V128 {
#| i32x4_replace_lane(value, replacement.reinterpret_as_uint(), lane)
#|}
#|#intrinsic("%v128.f64x2_replace_lane")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_replace_lane(
#| value : V128,
#| replacement : Double,
#| lane : Int32,
#|) -> V128 {
#| i64x2_replace_lane(value, replacement.reinterpret_as_uint64(), lane)
#|}
#|#intrinsic("%v128.v128_not")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_not(value : V128) -> V128 {
#| make(lo(value).lnot(), hi(value).lnot())
#|}
#|#intrinsic("%v128.v128_and")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_and_(value : V128, other : V128) -> V128 {
#| make(lo(value) & lo(other), hi(value) & hi(other))
#|}
#|#intrinsic("%v128.v128_or")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_or_(value : V128, other : V128) -> V128 {
#| make(lo(value) | lo(other), hi(value) | hi(other))
#|}
#|#intrinsic("%v128.v128_xor")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_xor(value : V128, other : V128) -> V128 {
#| make(lo(value) ^ lo(other), hi(value) ^ hi(other))
#|}
#|#intrinsic("%v128.v128_andnot")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_andnot(value : V128, other : V128) -> V128 {
#| v128_and_(value, v128_not(other))
#|}
#|#intrinsic("%v128.v128_bitselect")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_bitselect(value : V128, other : V128, mask : V128) -> V128 {
#| v128_or_(v128_and_(value, mask), v128_andnot(other, mask))
#|}
#|#intrinsic("%v128.v128_any_true")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_any_true(value : V128) -> Bool {
#| lo(value) != 0UL || hi(value) != 0UL
#|}
),
"simd_float.mbt": (
#|fn pack_f32x4_by(f : (Int32) -> @float.Float) -> V128 {
#| pack_u32x4_by(i => f(i).reinterpret_as_uint())
#|}
#|fn pack_f64x2_by(f : (Int32) -> Double) -> V128 {
#| pack_u64x2_by(i => f(i).reinterpret_as_uint64())
#|}
#|fn f32x4_map(value : V128, f : (@float.Float) -> @float.Float) -> V128 {
#| pack_f32x4_by(i => f(f32x4_extract_lane(value, i)))
#|}
#|fn f64x2_map(value : V128, f : (Double) -> Double) -> V128 {
#| pack_f64x2_by(i => f(f64x2_extract_lane(value, i)))
#|}
#|fn f32x4_zip(
#| value : V128,
#| other : V128,
#| f : (@float.Float, @float.Float) -> @float.Float,
#|) -> V128 {
#| pack_f32x4_by(i => {
#| f(f32x4_extract_lane(value, i), f32x4_extract_lane(other, i))
#| })
#|}
#|fn f64x2_zip(
#| value : V128,
#| other : V128,
#| f : (Double, Double) -> Double,
#|) -> V128 {
#| pack_f64x2_by(i => {
#| f(f64x2_extract_lane(value, i), f64x2_extract_lane(other, i))
#| })
#|}
#|fn f32x4_compare(
#| value : V128,
#| other : V128,
#| f : (@float.Float, @float.Float) -> Bool,
#|) -> V128 {
#| pack_u32x4_by(i => {
#| bool_to_u32_mask(
#| f(f32x4_extract_lane(value, i), f32x4_extract_lane(other, i)),
#| )
#| })
#|}
#|fn f64x2_compare(
#| value : V128,
#| other : V128,
#| f : (Double, Double) -> Bool,
#|) -> V128 {
#| pack_u64x2_by(i => {
#| bool_to_u64_mask(
#| f(f64x2_extract_lane(value, i), f64x2_extract_lane(other, i)),
#| )
#| })
#|}
#|fn f32_nearest_even(value : @float.Float) -> @float.Float {
#| if !value.is_finite() || value == 0.0F {
#| value
#| } else {
#| let down = value.floor()
#| let up = value.ceil()
#| let down_delta = value - down
#| let up_delta = up - value
#| if down_delta < up_delta {
#| down
#| } else if down_delta > up_delta {
#| up
#| } else if (down / 2.0F).trunc() * 2.0F == down {
#| down
#| } else {
#| up
#| }
#| }
#|}
#|fn f64_nearest_even(value : Double) -> Double {
#| if !value.is_finite() || value == 0.0 {
#| value
#| } else {
#| let down = value.floor()
#| let up = value.ceil()
#| let down_delta = value - down
#| let up_delta = up - value
#| if down_delta < up_delta {
#| down
#| } else if down_delta > up_delta {
#| up
#| } else if (down / 2.0).trunc() * 2.0 == down {
#| down
#| } else {
#| up
#| }
#| }
#|}
#|fn f32_min(value : @float.Float, other : @float.Float) -> @float.Float {
#| if value.is_nan() {
#| value
#| } else if other.is_nan() {
#| other
#| } else if value == other {
#| @float.Float::reinterpret_from_uint(
#| value.reinterpret_as_uint() | other.reinterpret_as_uint(),
#| )
#| } else if value < other {
#| value
#| } else {
#| other
#| }
#|}
#|fn f32_max(value : @float.Float, other : @float.Float) -> @float.Float {
#| if value.is_nan() {
#| value
#| } else if other.is_nan() {
#| other
#| } else if value == other {
#| @float.Float::reinterpret_from_uint(
#| value.reinterpret_as_uint() & other.reinterpret_as_uint(),
#| )
#| } else if value > other {
#| value
#| } else {
#| other
#| }
#|}
#|fn f64_min(value : Double, other : Double) -> Double {
#| if value.is_nan() {
#| value
#| } else if other.is_nan() {
#| other
#| } else if value == other {
#| (value.reinterpret_as_uint64() | other.reinterpret_as_uint64()).reinterpret_as_double()
#| } else if value < other {
#| value
#| } else {
#| other
#| }
#|}
#|fn f64_max(value : Double, other : Double) -> Double {
#| if value.is_nan() {
#| value
#| } else if other.is_nan() {
#| other
#| } else if value == other {
#| (value.reinterpret_as_uint64() & other.reinterpret_as_uint64()).reinterpret_as_double()
#| } else if value > other {
#| value
#| } else {
#| other
#| }
#|}
#|fn f32_pmin(value : @float.Float, other : @float.Float) -> @float.Float {
#| if other < value {
#| other
#| } else {
#| value
#| }
#|}
#|fn f32_pmax(value : @float.Float, other : @float.Float) -> @float.Float {
#| if value < other {
#| other
#| } else {
#| value
#| }
#|}
#|fn f64_pmin(value : Double, other : Double) -> Double {
#| if other < value {
#| other
#| } else {
#| value
#| }
#|}
#|fn f64_pmax(value : Double, other : Double) -> Double {
#| if value < other {
#| other
#| } else {
#| value
#| }
#|}
#|fn f32_trunc_sat_s(value : @float.Float) -> UInt32 {
#| value.to_int().reinterpret_as_uint()
#|}
#|fn f32_trunc_sat_u(value : @float.Float) -> UInt32 {
#| value.to_double().to_uint()
#|}
#|fn f64_trunc_sat_s(value : Double) -> UInt32 {
#| value.to_int().reinterpret_as_uint()
#|}
#|fn f64_trunc_sat_u(value : Double) -> UInt32 {
#| value.to_uint()
#|}
#|#intrinsic("%v128.f32x4_abs")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_abs(value : V128) -> V128 {
#| f32x4_map(value, fn(x) { x.abs() })
#|}
#|#intrinsic("%v128.f32x4_neg")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_neg(value : V128) -> V128 {
#| f32x4_map(value, fn(x) { -x })
#|}
#|#intrinsic("%v128.f32x4_sqrt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_sqrt(value : V128) -> V128 {
#| f32x4_map(value, fn(x) { x.sqrt() })
#|}
#|#intrinsic("%v128.f32x4_ceil")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_ceil(value : V128) -> V128 {
#| f32x4_map(value, fn(x) { x.ceil() })
#|}
#|#intrinsic("%v128.f32x4_floor")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_floor(value : V128) -> V128 {
#| f32x4_map(value, fn(x) { x.floor() })
#|}
#|#intrinsic("%v128.f32x4_trunc")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_trunc(value : V128) -> V128 {
#| f32x4_map(value, fn(x) { x.trunc() })
#|}
#|#intrinsic("%v128.f32x4_nearest")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_nearest(value : V128) -> V128 {
#| f32x4_map(value, f32_nearest_even)
#|}
#|#intrinsic("%v128.f32x4_add")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_add(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, fn(x, y) { x + y })
#|}
#|#intrinsic("%v128.f32x4_sub")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_sub(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, fn(x, y) { x - y })
#|}
#|#intrinsic("%v128.f32x4_mul")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_mul(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, fn(x, y) { x * y })
#|}
#|#intrinsic("%v128.f32x4_div")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_div(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, fn(x, y) { x / y })
#|}
#|#intrinsic("%v128.f32x4_min")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_min(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, f32_min)
#|}
#|#intrinsic("%v128.f32x4_max")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_max(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, f32_max)
#|}
#|#intrinsic("%v128.f32x4_pmin")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_pmin(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, f32_pmin)
#|}
#|#intrinsic("%v128.f32x4_pmax")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_pmax(value : V128, other : V128) -> V128 {
#| f32x4_zip(value, other, f32_pmax)
#|}
#|#intrinsic("%v128.f32x4_relaxed_min")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_relaxed_min(value : V128, other : V128) -> V128 {
#| f32x4_min(value, other)
#|}
#|#intrinsic("%v128.f32x4_relaxed_max")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_relaxed_max(value : V128, other : V128) -> V128 {
#| f32x4_max(value, other)
#|}
#|#intrinsic("%v128.f32x4_relaxed_madd")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_relaxed_madd(value : V128, other : V128, addend : V128) -> V128 {
#| pack_f32x4_by(i => {
#| f32x4_extract_lane(value, i) * f32x4_extract_lane(other, i) +
#| f32x4_extract_lane(addend, i)
#| })
#|}
#|#intrinsic("%v128.f32x4_relaxed_nmadd")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_relaxed_nmadd(value : V128, other : V128, addend : V128) -> V128 {
#| pack_f32x4_by(i => {
#| f32x4_extract_lane(addend, i) -
#| f32x4_extract_lane(value, i) * f32x4_extract_lane(other, i)
#| })
#|}
#|#intrinsic("%v128.f32x4_eq")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_eq(value : V128, other : V128) -> V128 {
#| f32x4_compare(value, other, fn(x, y) { x == y })
#|}
#|#intrinsic("%v128.f32x4_ne")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_ne(value : V128, other : V128) -> V128 {
#| f32x4_compare(value, other, fn(x, y) { x != y })
#|}
#|#intrinsic("%v128.f32x4_lt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_lt(value : V128, other : V128) -> V128 {
#| f32x4_compare(value, other, fn(x, y) { x < y })
#|}
#|#intrinsic("%v128.f32x4_le")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_le(value : V128, other : V128) -> V128 {
#| f32x4_compare(value, other, fn(x, y) { x <= y })
#|}
#|#intrinsic("%v128.f32x4_gt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_gt(value : V128, other : V128) -> V128 {
#| f32x4_compare(value, other, fn(x, y) { x > y })
#|}
#|#intrinsic("%v128.f32x4_ge")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_ge(value : V128, other : V128) -> V128 {
#| f32x4_compare(value, other, fn(x, y) { x >= y })
#|}
#|#intrinsic("%v128.f32x4_convert_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_convert_i32x4_s(value : V128) -> V128 {
#| pack_f32x4_by(i => {
#| @float.Float::from_int(u32_lane(value, i).reinterpret_as_int())
#| })
#|}
#|#intrinsic("%v128.f32x4_convert_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_convert_i32x4_u(value : V128) -> V128 {
#| pack_f32x4_by(i => @float.Float::from_uint(u32_lane(value, i)))
#|}
#|#intrinsic("%v128.f32x4_demote_f64x2_zero")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f32x4_demote_f64x2_zero(value : V128) -> V128 {
#| f32x4_const(
#| @float.Float::from_double(f64x2_extract_lane(value, 0)),
#| @float.Float::from_double(f64x2_extract_lane(value, 1)),
#| 0.0F,
#| 0.0F,
#| )
#|}
#|#intrinsic("%v128.i32x4_trunc_sat_f32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_trunc_sat_f32x4_s(value : V128) -> V128 {
#| pack_u32x4_by(i => f32_trunc_sat_s(f32x4_extract_lane(value, i)))
#|}
#|#intrinsic("%v128.i32x4_trunc_sat_f32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_trunc_sat_f32x4_u(value : V128) -> V128 {
#| pack_u32x4_by(i => f32_trunc_sat_u(f32x4_extract_lane(value, i)))
#|}
#|#intrinsic("%v128.i32x4_relaxed_trunc_f32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_relaxed_trunc_f32x4_s(value : V128) -> V128 {
#| i32x4_trunc_sat_f32x4_s(value)
#|}
#|#intrinsic("%v128.i32x4_relaxed_trunc_f32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_relaxed_trunc_f32x4_u(value : V128) -> V128 {
#| i32x4_trunc_sat_f32x4_u(value)
#|}
#|#intrinsic("%v128.f64x2_abs")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_abs(value : V128) -> V128 {
#| f64x2_map(value, fn(x) { x.abs() })
#|}
#|#intrinsic("%v128.f64x2_neg")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_neg(value : V128) -> V128 {
#| f64x2_map(value, fn(x) { -x })
#|}
#|#intrinsic("%v128.f64x2_sqrt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_sqrt(value : V128) -> V128 {
#| f64x2_map(value, fn(x) { x.sqrt() })
#|}
#|#intrinsic("%v128.f64x2_ceil")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_ceil(value : V128) -> V128 {
#| f64x2_map(value, fn(x) { x.ceil() })
#|}
#|#intrinsic("%v128.f64x2_floor")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_floor(value : V128) -> V128 {
#| f64x2_map(value, fn(x) { x.floor() })
#|}
#|#intrinsic("%v128.f64x2_trunc")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_trunc(value : V128) -> V128 {
#| f64x2_map(value, fn(x) { x.trunc() })
#|}
#|#intrinsic("%v128.f64x2_nearest")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_nearest(value : V128) -> V128 {
#| f64x2_map(value, f64_nearest_even)
#|}
#|#intrinsic("%v128.f64x2_add")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_add(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, fn(x, y) { x + y })
#|}
#|#intrinsic("%v128.f64x2_sub")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_sub(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, fn(x, y) { x - y })
#|}
#|#intrinsic("%v128.f64x2_mul")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_mul(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, fn(x, y) { x * y })
#|}
#|#intrinsic("%v128.f64x2_div")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_div(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, fn(x, y) { x / y })
#|}
#|#intrinsic("%v128.f64x2_min")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_min(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, f64_min)
#|}
#|#intrinsic("%v128.f64x2_max")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_max(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, f64_max)
#|}
#|#intrinsic("%v128.f64x2_pmin")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_pmin(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, f64_pmin)
#|}
#|#intrinsic("%v128.f64x2_pmax")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_pmax(value : V128, other : V128) -> V128 {
#| f64x2_zip(value, other, f64_pmax)
#|}
#|#intrinsic("%v128.f64x2_relaxed_min")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_relaxed_min(value : V128, other : V128) -> V128 {
#| f64x2_min(value, other)
#|}
#|#intrinsic("%v128.f64x2_relaxed_max")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_relaxed_max(value : V128, other : V128) -> V128 {
#| f64x2_max(value, other)
#|}
#|#intrinsic("%v128.f64x2_relaxed_madd")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_relaxed_madd(value : V128, other : V128, addend : V128) -> V128 {
#| pack_f64x2_by(i => {
#| f64x2_extract_lane(value, i) * f64x2_extract_lane(other, i) +
#| f64x2_extract_lane(addend, i)
#| })
#|}
#|#intrinsic("%v128.f64x2_relaxed_nmadd")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_relaxed_nmadd(value : V128, other : V128, addend : V128) -> V128 {
#| pack_f64x2_by(i => {
#| f64x2_extract_lane(addend, i) -
#| f64x2_extract_lane(value, i) * f64x2_extract_lane(other, i)
#| })
#|}
#|#intrinsic("%v128.f64x2_eq")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_eq(value : V128, other : V128) -> V128 {
#| f64x2_compare(value, other, fn(x, y) { x == y })
#|}
#|#intrinsic("%v128.f64x2_ne")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_ne(value : V128, other : V128) -> V128 {
#| f64x2_compare(value, other, fn(x, y) { x != y })
#|}
#|#intrinsic("%v128.f64x2_lt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_lt(value : V128, other : V128) -> V128 {
#| f64x2_compare(value, other, fn(x, y) { x < y })
#|}
#|#intrinsic("%v128.f64x2_le")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_le(value : V128, other : V128) -> V128 {
#| f64x2_compare(value, other, fn(x, y) { x <= y })
#|}
#|#intrinsic("%v128.f64x2_gt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_gt(value : V128, other : V128) -> V128 {
#| f64x2_compare(value, other, fn(x, y) { x > y })
#|}
#|#intrinsic("%v128.f64x2_ge")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_ge(value : V128, other : V128) -> V128 {
#| f64x2_compare(value, other, fn(x, y) { x >= y })
#|}
#|#intrinsic("%v128.f64x2_convert_low_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_convert_low_i32x4_s(value : V128) -> V128 {
#| pack_f64x2_by(i => u32_lane(value, i).reinterpret_as_int().to_double())
#|}
#|#intrinsic("%v128.f64x2_convert_low_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_convert_low_i32x4_u(value : V128) -> V128 {
#| pack_f64x2_by(i => u32_lane(value, i).to_double())
#|}
#|#intrinsic("%v128.f64x2_promote_low_f32x4")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn f64x2_promote_low_f32x4(value : V128) -> V128 {
#| pack_f64x2_by(i => f32x4_extract_lane(value, i).to_double())
#|}
#|#intrinsic("%v128.i32x4_trunc_sat_f64x2_s_zero")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_trunc_sat_f64x2_s_zero(value : V128) -> V128 {
#| i32x4_const(
#| f64_trunc_sat_s(f64x2_extract_lane(value, 0)),
#| f64_trunc_sat_s(f64x2_extract_lane(value, 1)),
#| 0,
#| 0,
#| )
#|}
#|#intrinsic("%v128.i32x4_trunc_sat_f64x2_u_zero")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_trunc_sat_f64x2_u_zero(value : V128) -> V128 {
#| i32x4_const(
#| f64_trunc_sat_u(f64x2_extract_lane(value, 0)),
#| f64_trunc_sat_u(f64x2_extract_lane(value, 1)),
#| 0,
#| 0,
#| )
#|}
#|#intrinsic("%v128.i32x4_relaxed_trunc_f64x2_s_zero")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_relaxed_trunc_f64x2_s_zero(value : V128) -> V128 {
#| i32x4_trunc_sat_f64x2_s_zero(value)
#|}
#|#intrinsic("%v128.i32x4_relaxed_trunc_f64x2_u_zero")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_relaxed_trunc_f64x2_u_zero(value : V128) -> V128 {
#| i32x4_trunc_sat_f64x2_u_zero(value)
#|}
),
"simd_int_arith.mbt": (
#|#intrinsic("%v128.i8x16_add")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_add(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => a + b)
#|}
#|#intrinsic("%v128.i8x16_sub")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_sub(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => a - b)
#|}
#|#intrinsic("%v128.i8x16_add_sat_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_add_sat_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_add_sat_s(a, b))
#|}
#|#intrinsic("%v128.i8x16_add_sat_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_add_sat_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_add_sat_u(a, b))
#|}
#|#intrinsic("%v128.i8x16_sub_sat_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_sub_sat_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_sub_sat_s(a, b))
#|}
#|#intrinsic("%v128.i8x16_sub_sat_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_sub_sat_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_sub_sat_u(a, b))
#|}
#|#intrinsic("%v128.i8x16_neg")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_neg(value : V128) -> V128 {
#| pack_u8x16_by(i => Byte(0) - u8_lane(value, i))
#|}
#|#intrinsic("%v128.i8x16_abs")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_abs(value : V128) -> V128 {
#| pack_u8x16_by(i => i8_abs(u8_lane(value, i)))
#|}
#|#intrinsic("%v128.i8x16_min_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_min_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_min_s(a, b))
#|}
#|#intrinsic("%v128.i8x16_min_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_min_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => if a < b { a } else { b })
#|}
#|#intrinsic("%v128.i8x16_max_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_max_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_max_s(a, b))
#|}
#|#intrinsic("%v128.i8x16_max_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_max_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => if a > b { a } else { b })
#|}
#|#intrinsic("%v128.i8x16_avgr_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_avgr_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => i8_avgr_u(a, b))
#|}
#|#intrinsic("%v128.i8x16_popcnt")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_popcnt(value : V128) -> V128 {
#| pack_u8x16_by(i => u8_lane(value, i).to_uint().popcnt().to_byte())
#|}
#|#intrinsic("%v128.i16x8_add")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_add(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => u16_lane(value, i) + u16_lane(other, i))
#|}
#|#intrinsic("%v128.i16x8_sub")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_sub(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => u16_lane(value, i) - u16_lane(other, i))
#|}
#|#intrinsic("%v128.i16x8_add_sat_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_add_sat_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_add_sat_s(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_add_sat_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_add_sat_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_add_sat_u(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_sub_sat_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_sub_sat_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_sub_sat_s(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_sub_sat_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_sub_sat_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_sub_sat_u(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_mul")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_mul(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => u16_lane(value, i) * u16_lane(other, i))
#|}
#|#intrinsic("%v128.i16x8_neg")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_neg(value : V128) -> V128 {
#| pack_u16x8_by(i => UInt16(0) - u16_lane(value, i))
#|}
#|#intrinsic("%v128.i16x8_abs")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_abs(value : V128) -> V128 {
#| pack_u16x8_by(i => i16_abs(u16_lane(value, i)))
#|}
#|#intrinsic("%v128.i16x8_min_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_min_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_min_s(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_min_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_min_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| if u16_lane(value, i) < u16_lane(other, i) {
#| u16_lane(value, i)
#| } else {
#| u16_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i16x8_max_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_max_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_max_s(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_max_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_max_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| if u16_lane(value, i) > u16_lane(other, i) {
#| u16_lane(value, i)
#| } else {
#| u16_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i16x8_avgr_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_avgr_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_avgr_u(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_add")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_add(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => u32_lane(value, i) + u32_lane(other, i))
#|}
#|#intrinsic("%v128.i32x4_sub")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_sub(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => u32_lane(value, i) - u32_lane(other, i))
#|}
#|#intrinsic("%v128.i32x4_mul")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_mul(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => u32_lane(value, i) * u32_lane(other, i))
#|}
#|#intrinsic("%v128.i32x4_neg")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_neg(value : V128) -> V128 {
#| pack_u32x4_by(i => 0U - u32_lane(value, i))
#|}
#|#intrinsic("%v128.i32x4_abs")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_abs(value : V128) -> V128 {
#| pack_u32x4_by(i => {
#| u32_lane(value, i).reinterpret_as_int().abs().reinterpret_as_uint()
#| })
#|}
#|#intrinsic("%v128.i32x4_min_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_min_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| if u32_lane(value, i).reinterpret_as_int() <
#| u32_lane(other, i).reinterpret_as_int() {
#| u32_lane(value, i)
#| } else {
#| u32_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i32x4_min_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_min_u(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| if u32_lane(value, i) < u32_lane(other, i) {
#| u32_lane(value, i)
#| } else {
#| u32_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i32x4_max_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_max_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| if u32_lane(value, i).reinterpret_as_int() >
#| u32_lane(other, i).reinterpret_as_int() {
#| u32_lane(value, i)
#| } else {
#| u32_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i32x4_max_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_max_u(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| if u32_lane(value, i) > u32_lane(other, i) {
#| u32_lane(value, i)
#| } else {
#| u32_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i64x2_add")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_add(value : V128, other : V128) -> V128 {
#| make(lo(value) + lo(other), hi(value) + hi(other))
#|}
#|#intrinsic("%v128.i64x2_sub")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_sub(value : V128, other : V128) -> V128 {
#| make(lo(value) - lo(other), hi(value) - hi(other))
#|}
#|#intrinsic("%v128.i64x2_mul")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_mul(value : V128, other : V128) -> V128 {
#| make(lo(value) * lo(other), hi(value) * hi(other))
#|}
#|#intrinsic("%v128.i64x2_neg")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_neg(value : V128) -> V128 {
#| make(0UL - lo(value), 0UL - hi(value))
#|}
#|#intrinsic("%v128.i64x2_abs")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_abs(value : V128) -> V128 {
#| pack_u64x2_by(i => {
#| u64_lane(value, i).reinterpret_as_int64().abs().reinterpret_as_uint64()
#| })
#|}
),
"simd_int_compare.mbt": (
#|#intrinsic("%v128.i8x16_eq")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_eq(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => bool_to_u8_mask(a == b))
#|}
#|#intrinsic("%v128.i8x16_ne")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_ne(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => bool_to_u8_mask(a != b))
#|}
#|#intrinsic("%v128.i8x16_lt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_lt_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => {
#| bool_to_u8_mask(i8_signed(a) < i8_signed(b))
#| })
#|}
#|#intrinsic("%v128.i8x16_lt_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_lt_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => bool_to_u8_mask(a < b))
#|}
#|#intrinsic("%v128.i8x16_le_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_le_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => {
#| bool_to_u8_mask(i8_signed(a) <= i8_signed(b))
#| })
#|}
#|#intrinsic("%v128.i8x16_le_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_le_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => bool_to_u8_mask(a <= b))
#|}
#|#intrinsic("%v128.i8x16_gt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_gt_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => {
#| bool_to_u8_mask(i8_signed(a) > i8_signed(b))
#| })
#|}
#|#intrinsic("%v128.i8x16_gt_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_gt_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => bool_to_u8_mask(a > b))
#|}
#|#intrinsic("%v128.i8x16_ge_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_ge_s(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => {
#| bool_to_u8_mask(i8_signed(a) >= i8_signed(b))
#| })
#|}
#|#intrinsic("%v128.i8x16_ge_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_ge_u(value : V128, other : V128) -> V128 {
#| zip_u8x16_by(value, other, (a, b) => bool_to_u8_mask(a >= b))
#|}
#|#intrinsic("%v128.i16x8_eq")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_eq(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => bool_to_u16_mask(u16_lane(value, i) == u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_ne")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_ne(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => bool_to_u16_mask(u16_lane(value, i) != u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_lt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_lt_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| bool_to_u16_mask(
#| i16_signed(u16_lane(value, i)) < i16_signed(u16_lane(other, i)),
#| )
#| })
#|}
#|#intrinsic("%v128.i16x8_lt_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_lt_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => bool_to_u16_mask(u16_lane(value, i) < u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_le_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_le_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| bool_to_u16_mask(
#| i16_signed(u16_lane(value, i)) <= i16_signed(u16_lane(other, i)),
#| )
#| })
#|}
#|#intrinsic("%v128.i16x8_le_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_le_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => bool_to_u16_mask(u16_lane(value, i) <= u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_gt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_gt_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| bool_to_u16_mask(
#| i16_signed(u16_lane(value, i)) > i16_signed(u16_lane(other, i)),
#| )
#| })
#|}
#|#intrinsic("%v128.i16x8_gt_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_gt_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => bool_to_u16_mask(u16_lane(value, i) > u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_ge_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_ge_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| bool_to_u16_mask(
#| i16_signed(u16_lane(value, i)) >= i16_signed(u16_lane(other, i)),
#| )
#| })
#|}
#|#intrinsic("%v128.i16x8_ge_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_ge_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => bool_to_u16_mask(u16_lane(value, i) >= u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_eq")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_eq(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => bool_to_u32_mask(u32_lane(value, i) == u32_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_ne")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_ne(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => bool_to_u32_mask(u32_lane(value, i) != u32_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_lt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_lt_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| bool_to_u32_mask(
#| u32_lane(value, i).reinterpret_as_int() <
#| u32_lane(other, i).reinterpret_as_int(),
#| )
#| })
#|}
#|#intrinsic("%v128.i32x4_lt_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_lt_u(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => bool_to_u32_mask(u32_lane(value, i) < u32_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_le_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_le_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| bool_to_u32_mask(
#| u32_lane(value, i).reinterpret_as_int() <=
#| u32_lane(other, i).reinterpret_as_int(),
#| )
#| })
#|}
#|#intrinsic("%v128.i32x4_le_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_le_u(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => bool_to_u32_mask(u32_lane(value, i) <= u32_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_gt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_gt_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| bool_to_u32_mask(
#| u32_lane(value, i).reinterpret_as_int() >
#| u32_lane(other, i).reinterpret_as_int(),
#| )
#| })
#|}
#|#intrinsic("%v128.i32x4_gt_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_gt_u(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => bool_to_u32_mask(u32_lane(value, i) > u32_lane(other, i)))
#|}
#|#intrinsic("%v128.i32x4_ge_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_ge_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => {
#| bool_to_u32_mask(
#| u32_lane(value, i).reinterpret_as_int() >=
#| u32_lane(other, i).reinterpret_as_int(),
#| )
#| })
#|}
#|#intrinsic("%v128.i32x4_ge_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_ge_u(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => bool_to_u32_mask(u32_lane(value, i) >= u32_lane(other, i)))
#|}
#|#intrinsic("%v128.i64x2_eq")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_eq(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => bool_to_u64_mask(u64_lane(value, i) == u64_lane(other, i)))
#|}
#|#intrinsic("%v128.i64x2_ne")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_ne(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => bool_to_u64_mask(u64_lane(value, i) != u64_lane(other, i)))
#|}
#|#intrinsic("%v128.i64x2_lt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_lt_s(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| bool_to_u64_mask(
#| u64_lane(value, i).reinterpret_as_int64() <
#| u64_lane(other, i).reinterpret_as_int64(),
#| )
#| })
#|}
#|#intrinsic("%v128.i64x2_le_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_le_s(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| bool_to_u64_mask(
#| u64_lane(value, i).reinterpret_as_int64() <=
#| u64_lane(other, i).reinterpret_as_int64(),
#| )
#| })
#|}
#|#intrinsic("%v128.i64x2_gt_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_gt_s(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| bool_to_u64_mask(
#| u64_lane(value, i).reinterpret_as_int64() >
#| u64_lane(other, i).reinterpret_as_int64(),
#| )
#| })
#|}
#|#intrinsic("%v128.i64x2_ge_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_ge_s(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| bool_to_u64_mask(
#| u64_lane(value, i).reinterpret_as_int64() >=
#| u64_lane(other, i).reinterpret_as_int64(),
#| )
#| })
#|}
),
"simd_int_extend.mbt": (
#|fn narrow_i16_to_i8_s(value : UInt16) -> Byte {
#| let value = i16_signed(value)
#| if value > 127 {
#| 0x7f
#| } else if value < -128 {
#| 0x80
#| } else {
#| value.to_byte()
#| }
#|}
#|fn narrow_i16_to_i8_u(value : UInt16) -> Byte {
#| let signed = i16_signed(value)
#| if signed < 0 {
#| 0
#| } else if signed > 255 {
#| 0xff
#| } else {
#| signed.to_byte()
#| }
#|}
#|fn narrow_i32_to_i16_s(value : UInt32) -> UInt16 {
#| let signed = value.reinterpret_as_int()
#| if signed > 32767 {
#| 0x7fff
#| } else if signed < -32768 {
#| 0x8000
#| } else {
#| signed.to_uint16()
#| }
#|}
#|fn narrow_i32_to_i16_u(value : UInt32) -> UInt16 {
#| let signed = value.reinterpret_as_int()
#| if signed < 0 {
#| 0
#| } else if signed > 65535 {
#| 0xffff
#| } else {
#| signed.to_uint16()
#| }
#|}
#|fn i16_q15mulr_sat_s(value : UInt16, other : UInt16) -> UInt16 {
#| let rounded = (i16_signed(value) * i16_signed(other) + 0x4000) >> 15
#| if rounded > 32767 {
#| 0x7fff
#| } else if rounded < -32768 {
#| 0x8000
#| } else {
#| rounded.to_uint16()
#| }
#|}
#|fn i16_pair_dot(value : V128, other : V128, pair : Int32) -> UInt32 {
#| (i16_signed(u16_lane(value, pair * 2)) * i16_signed(u16_lane(other, pair * 2)) +
#| i16_signed(u16_lane(value, pair * 2 + 1)) *
#| i16_signed(u16_lane(other, pair * 2 + 1))).reinterpret_as_uint()
#|}
#|#intrinsic("%v128.i16x8_extend_low_i8x16_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extend_low_i8x16_s(value : V128) -> V128 {
#| pack_u16x8_by(i => i8_signed(u8_lane(value, i)).to_uint16())
#|}
#|#intrinsic("%v128.i16x8_extend_high_i8x16_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extend_high_i8x16_s(value : V128) -> V128 {
#| pack_u16x8_by(i => i8_signed(u8_lane(value, i + 8)).to_uint16())
#|}
#|#intrinsic("%v128.i16x8_extend_low_i8x16_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extend_low_i8x16_u(value : V128) -> V128 {
#| pack_u16x8_by(i => u8_lane(value, i).to_uint16())
#|}
#|#intrinsic("%v128.i16x8_extend_high_i8x16_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extend_high_i8x16_u(value : V128) -> V128 {
#| pack_u16x8_by(i => u8_lane(value, i + 8).to_uint16())
#|}
#|#intrinsic("%v128.i32x4_extend_low_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extend_low_i16x8_s(value : V128) -> V128 {
#| pack_u32x4_by(i => i16_signed(u16_lane(value, i)).reinterpret_as_uint())
#|}
#|#intrinsic("%v128.i32x4_extend_high_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extend_high_i16x8_s(value : V128) -> V128 {
#| pack_u32x4_by(i => i16_signed(u16_lane(value, i + 4)).reinterpret_as_uint())
#|}
#|#intrinsic("%v128.i32x4_extend_low_i16x8_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extend_low_i16x8_u(value : V128) -> V128 {
#| pack_u32x4_by(i => u16_lane(value, i).to_uint())
#|}
#|#intrinsic("%v128.i32x4_extend_high_i16x8_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extend_high_i16x8_u(value : V128) -> V128 {
#| pack_u32x4_by(i => u16_lane(value, i + 4).to_uint())
#|}
#|#intrinsic("%v128.i64x2_extend_low_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extend_low_i32x4_s(value : V128) -> V128 {
#| pack_u64x2_by(i => {
#| u32_lane(value, i).reinterpret_as_int().to_int64().reinterpret_as_uint64()
#| })
#|}
#|#intrinsic("%v128.i64x2_extend_high_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extend_high_i32x4_s(value : V128) -> V128 {
#| pack_u64x2_by(i => {
#| u32_lane(value, i + 2)
#| .reinterpret_as_int()
#| .to_int64()
#| .reinterpret_as_uint64()
#| })
#|}
#|#intrinsic("%v128.i64x2_extend_low_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extend_low_i32x4_u(value : V128) -> V128 {
#| pack_u64x2_by(i => u32_lane(value, i).to_uint64())
#|}
#|#intrinsic("%v128.i64x2_extend_high_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extend_high_i32x4_u(value : V128) -> V128 {
#| pack_u64x2_by(i => u32_lane(value, i + 2).to_uint64())
#|}
#|#intrinsic("%v128.i8x16_relaxed_laneselect")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_relaxed_laneselect(
#| value : V128,
#| other : V128,
#| mask : V128,
#|) -> V128 {
#| pack_u8x16_by(i => {
#| if (u8_lane(mask, i) & 0x80) != 0 {
#| u8_lane(value, i)
#| } else {
#| u8_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i16x8_relaxed_laneselect")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_relaxed_laneselect(
#| value : V128,
#| other : V128,
#| mask : V128,
#|) -> V128 {
#| pack_u16x8_by(i => {
#| if (u16_lane(mask, i) & 0x8000) != 0 {
#| u16_lane(value, i)
#| } else {
#| u16_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i32x4_relaxed_laneselect")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_relaxed_laneselect(
#| value : V128,
#| other : V128,
#| mask : V128,
#|) -> V128 {
#| pack_u32x4_by(i => {
#| if (u32_lane(mask, i) & 0x80000000) != 0 {
#| u32_lane(value, i)
#| } else {
#| u32_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i64x2_relaxed_laneselect")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_relaxed_laneselect(
#| value : V128,
#| other : V128,
#| mask : V128,
#|) -> V128 {
#| pack_u64x2_by(i => {
#| if (u64_lane(mask, i) & 0x8000000000000000UL) != 0UL {
#| u64_lane(value, i)
#| } else {
#| u64_lane(other, i)
#| }
#| })
#|}
#|#intrinsic("%v128.i8x16_narrow_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_narrow_i16x8_s(value : V128, other : V128) -> V128 {
#| pack_u8x16_by(i => {
#| if i < 8 {
#| narrow_i16_to_i8_s(u16_lane(value, i))
#| } else {
#| narrow_i16_to_i8_s(u16_lane(other, i - 8))
#| }
#| })
#|}
#|#intrinsic("%v128.i8x16_narrow_i16x8_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_narrow_i16x8_u(value : V128, other : V128) -> V128 {
#| pack_u8x16_by(i => {
#| if i < 8 {
#| narrow_i16_to_i8_u(u16_lane(value, i))
#| } else {
#| narrow_i16_to_i8_u(u16_lane(other, i - 8))
#| }
#| })
#|}
#|#intrinsic("%v128.i16x8_narrow_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_narrow_i32x4_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| if i < 4 {
#| narrow_i32_to_i16_s(u32_lane(value, i))
#| } else {
#| narrow_i32_to_i16_s(u32_lane(other, i - 4))
#| }
#| })
#|}
#|#intrinsic("%v128.i16x8_narrow_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_narrow_i32x4_u(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| if i < 4 {
#| narrow_i32_to_i16_u(u32_lane(value, i))
#| } else {
#| narrow_i32_to_i16_u(u32_lane(other, i - 4))
#| }
#| })
#|}
#|#intrinsic("%v128.i16x8_extmul_low_i8x16_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extmul_low_i8x16_s(value : V128, other : V128) -> V128 {
#| i16x8_mul(i16x8_extend_low_i8x16_s(value), i16x8_extend_low_i8x16_s(other))
#|}
#|#intrinsic("%v128.i16x8_extmul_high_i8x16_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extmul_high_i8x16_s(value : V128, other : V128) -> V128 {
#| i16x8_mul(i16x8_extend_high_i8x16_s(value), i16x8_extend_high_i8x16_s(other))
#|}
#|#intrinsic("%v128.i16x8_extmul_low_i8x16_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extmul_low_i8x16_u(value : V128, other : V128) -> V128 {
#| i16x8_mul(i16x8_extend_low_i8x16_u(value), i16x8_extend_low_i8x16_u(other))
#|}
#|#intrinsic("%v128.i16x8_extmul_high_i8x16_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extmul_high_i8x16_u(value : V128, other : V128) -> V128 {
#| i16x8_mul(i16x8_extend_high_i8x16_u(value), i16x8_extend_high_i8x16_u(other))
#|}
#|#intrinsic("%v128.i32x4_extmul_low_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extmul_low_i16x8_s(value : V128, other : V128) -> V128 {
#| i32x4_mul(i32x4_extend_low_i16x8_s(value), i32x4_extend_low_i16x8_s(other))
#|}
#|#intrinsic("%v128.i32x4_extmul_high_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extmul_high_i16x8_s(value : V128, other : V128) -> V128 {
#| i32x4_mul(i32x4_extend_high_i16x8_s(value), i32x4_extend_high_i16x8_s(other))
#|}
#|#intrinsic("%v128.i32x4_extmul_low_i16x8_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extmul_low_i16x8_u(value : V128, other : V128) -> V128 {
#| i32x4_mul(i32x4_extend_low_i16x8_u(value), i32x4_extend_low_i16x8_u(other))
#|}
#|#intrinsic("%v128.i32x4_extmul_high_i16x8_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extmul_high_i16x8_u(value : V128, other : V128) -> V128 {
#| i32x4_mul(i32x4_extend_high_i16x8_u(value), i32x4_extend_high_i16x8_u(other))
#|}
#|#intrinsic("%v128.i64x2_extmul_low_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extmul_low_i32x4_s(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| (u32_lane(value, i).reinterpret_as_int().to_int64() *
#| u32_lane(other, i).reinterpret_as_int().to_int64()).reinterpret_as_uint64()
#| })
#|}
#|#intrinsic("%v128.i64x2_extmul_high_i32x4_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extmul_high_i32x4_s(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| (u32_lane(value, i + 2).reinterpret_as_int().to_int64() *
#| u32_lane(other, i + 2).reinterpret_as_int().to_int64()).reinterpret_as_uint64()
#| })
#|}
#|#intrinsic("%v128.i64x2_extmul_low_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extmul_low_i32x4_u(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| u32_lane(value, i).to_uint64() * u32_lane(other, i).to_uint64()
#| })
#|}
#|#intrinsic("%v128.i64x2_extmul_high_i32x4_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_extmul_high_i32x4_u(value : V128, other : V128) -> V128 {
#| pack_u64x2_by(i => {
#| u32_lane(value, i + 2).to_uint64() * u32_lane(other, i + 2).to_uint64()
#| })
#|}
#|#intrinsic("%v128.i16x8_q15mulr_sat_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_q15mulr_sat_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => i16_q15mulr_sat_s(u16_lane(value, i), u16_lane(other, i)))
#|}
#|#intrinsic("%v128.i16x8_relaxed_q15mulr_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_relaxed_q15mulr_s(value : V128, other : V128) -> V128 {
#| i16x8_q15mulr_sat_s(value, other)
#|}
#|#intrinsic("%v128.i16x8_extadd_pairwise_i8x16_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extadd_pairwise_i8x16_s(value : V128) -> V128 {
#| pack_u16x8_by(i => {
#| (i8_signed(u8_lane(value, i * 2)) + i8_signed(u8_lane(value, i * 2 + 1))).to_uint16()
#| })
#|}
#|#intrinsic("%v128.i16x8_extadd_pairwise_i8x16_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_extadd_pairwise_i8x16_u(value : V128) -> V128 {
#| pack_u16x8_by(i => {
#| u8_lane(value, i * 2).to_uint16() + u8_lane(value, i * 2 + 1).to_uint16()
#| })
#|}
#|#intrinsic("%v128.i32x4_extadd_pairwise_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extadd_pairwise_i16x8_s(value : V128) -> V128 {
#| pack_u32x4_by(i => {
#| (i16_signed(u16_lane(value, i * 2)) + i16_signed(u16_lane(value, i * 2 + 1))).reinterpret_as_uint()
#| })
#|}
#|#intrinsic("%v128.i32x4_extadd_pairwise_i16x8_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_extadd_pairwise_i16x8_u(value : V128) -> V128 {
#| pack_u32x4_by(i => {
#| u16_lane(value, i * 2).to_uint() + u16_lane(value, i * 2 + 1).to_uint()
#| })
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_relaxed_dot_i8x16_i7x16_s(value : V128, other : V128) -> V128 {
#| pack_u16x8_by(i => {
#| (i8_signed(u8_lane(value, i * 2)) * i8_signed(u8_lane(other, i * 2)) +
#| i8_signed(u8_lane(value, i * 2 + 1)) * i8_signed(u8_lane(other, i * 2 + 1))).to_uint16()
#| })
#|}
#|#intrinsic("%v128.i32x4_dot_i16x8_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_dot_i16x8_s(value : V128, other : V128) -> V128 {
#| pack_u32x4_by(i => i16_pair_dot(value, other, i))
#|}
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_relaxed_dot_i8x16_i7x16_add_s(
#| value : V128,
#| other : V128,
#| accumulator : V128,
#|) -> V128 {
#| i32x4_add(
#| i32x4_extadd_pairwise_i16x8_s(i16x8_relaxed_dot_i8x16_i7x16_s(value, other)),
#| accumulator,
#| )
#|}
),
"simd_int_shift.mbt": (
#|fn has_zero_byte(value : UInt64) -> Bool {
#| ((value - 0x0101010101010101UL) & value.lnot() & 0x8080808080808080UL) != 0UL
#|}
#|fn has_zero_u16_lane(value : UInt64) -> Bool {
#| let bytes = value | (value >> 8) | 0xff00ff00ff00ff00UL
#| has_zero_byte(bytes)
#|}
#|#intrinsic("%v128.i8x16_shl")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_shl(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 7
#| pack_u8x16_by(i => (u8_lane(value, i).to_uint() << shift).to_byte())
#|}
#|#intrinsic("%v128.i8x16_shr_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_shr_u(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 7
#| pack_u8x16_by(i => (u8_lane(value, i).to_uint() >> shift).to_byte())
#|}
#|#intrinsic("%v128.i8x16_shr_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_shr_s(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 7
#| pack_u8x16_by(i => (i8_signed(u8_lane(value, i)) >> shift).to_byte())
#|}
#|#intrinsic("%v128.i16x8_shl")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_shl(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 15
#| pack_u16x8_by(i => (u16_lane(value, i).to_uint() << shift).to_uint16())
#|}
#|#intrinsic("%v128.i16x8_shr_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_shr_u(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 15
#| pack_u16x8_by(i => (u16_lane(value, i).to_uint() >> shift).to_uint16())
#|}
#|#intrinsic("%v128.i16x8_shr_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_shr_s(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 15
#| pack_u16x8_by(i => (i16_signed(u16_lane(value, i)) >> shift).to_uint16())
#|}
#|#intrinsic("%v128.i32x4_shl")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_shl(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 31
#| pack_u32x4_by(i => u32_lane(value, i) << shift)
#|}
#|#intrinsic("%v128.i32x4_shr_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_shr_u(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 31
#| pack_u32x4_by(i => u32_lane(value, i) >> shift)
#|}
#|#intrinsic("%v128.i32x4_shr_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_shr_s(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 31
#| pack_u32x4_by(i => {
#| (u32_lane(value, i).reinterpret_as_int() >> shift).reinterpret_as_uint()
#| })
#|}
#|#intrinsic("%v128.i64x2_shl")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_shl(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 63
#| make(lo(value) << shift, hi(value) << shift)
#|}
#|#intrinsic("%v128.i64x2_shr_u")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_shr_u(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 63
#| make(lo(value) >> shift, hi(value) >> shift)
#|}
#|#intrinsic("%v128.i64x2_shr_s")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_shr_s(value : V128, shift : Int32) -> V128 {
#| let shift = shift & 63
#| pack_u64x2_by(i => {
#| (u64_lane(value, i).reinterpret_as_int64() >> shift).reinterpret_as_uint64()
#| })
#|}
#|#intrinsic("%v128.i8x16_all_true")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_all_true(value : V128) -> Bool {
#| !has_zero_byte(lo(value)) && !has_zero_byte(hi(value))
#|}
#|#intrinsic("%v128.i16x8_all_true")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_all_true(value : V128) -> Bool {
#| !has_zero_u16_lane(lo(value)) && !has_zero_u16_lane(hi(value))
#|}
#|#intrinsic("%v128.i32x4_all_true")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_all_true(value : V128) -> Bool {
#| let lo_word = lo(value)
#| let hi_word = hi(value)
#| (lo_word & 0xffffffffUL) != 0UL &&
#| lo_word >> 32 != 0UL &&
#| (hi_word & 0xffffffffUL) != 0UL &&
#| hi_word >> 32 != 0UL
#|}
#|#intrinsic("%v128.i64x2_all_true")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_all_true(value : V128) -> Bool {
#| lo(value) != 0UL && hi(value) != 0UL
#|}
#|#intrinsic("%v128.i8x16_bitmask")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_bitmask(value : V128) -> Int32 {
#| let low = ((lo(value) & 0x8080808080808080UL) * 0x0002040810204081UL) >> 56
#| let high = ((hi(value) & 0x8080808080808080UL) * 0x0002040810204081UL) >> 56
#| low.to_int() | (high.to_int() << 8)
#|}
#|#intrinsic("%v128.i16x8_bitmask")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i16x8_bitmask(value : V128) -> Int32 {
#| let mut result = 0
#| for i in 0..<8 {
#| result = result | (((u16_lane(value, i).to_int() >> 15) & 1) << i)
#| }
#| result
#|}
#|#intrinsic("%v128.i32x4_bitmask")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i32x4_bitmask(value : V128) -> Int32 {
#| let mut result = 0
#| for i in 0..<4 {
#| result = result |
#| ((u32_lane(value, i).reinterpret_as_int() >> 31) & (1 << i))
#| }
#| result
#|}
#|#intrinsic("%v128.i64x2_bitmask")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i64x2_bitmask(value : V128) -> Int32 {
#| let mut result = 0
#| for i in 0..<2 {
#| result = result | ((u64_lane(value, i) >> 63).to_int() << i)
#| }
#| result
#|}
),
"simd_memory.mbt": (
#|fn load_u16_le(bytes : FixedArray[Byte], offset : Int32) -> UInt16 {
#| bytes.unsafe_get(offset).to_uint16() |
#| (bytes.unsafe_get(offset + 1).to_uint16() << 8)
#|}
#|fn load_u16x4_le(str : String, offset : Int32) -> UInt64 {
#| for i in 0..<4; word = UInt64(0) {
#| continue word | (str.unsafe_get(offset + i).to_uint64() << (16 * i))
#| } nobreak {
#| word
#| }
#|}
#|fn load_u32_le(bytes : FixedArray[Byte], offset : Int32) -> UInt32 {
#| bytes.unsafe_get(offset).to_uint() |
#| (bytes.unsafe_get(offset + 1).to_uint() << 8) |
#| (bytes.unsafe_get(offset + 2).to_uint() << 16) |
#| (bytes.unsafe_get(offset + 3).to_uint() << 24)
#|}
#|fn load_u64_le(bytes : FixedArray[Byte], offset : Int32) -> UInt64 {
#| bytes.unsafe_get(offset).to_uint64() |
#| (bytes.unsafe_get(offset + 1).to_uint64() << 8) |
#| (bytes.unsafe_get(offset + 2).to_uint64() << 16) |
#| (bytes.unsafe_get(offset + 3).to_uint64() << 24) |
#| (bytes.unsafe_get(offset + 4).to_uint64() << 32) |
#| (bytes.unsafe_get(offset + 5).to_uint64() << 40) |
#| (bytes.unsafe_get(offset + 6).to_uint64() << 48) |
#| (bytes.unsafe_get(offset + 7).to_uint64() << 56)
#|}
#|fn store_u16_le(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : UInt16,
#|) -> Unit {
#| bytes.unsafe_set(offset, value.to_byte())
#| bytes.unsafe_set(offset + 1, (value >> 8).to_byte())
#|}
#|fn store_u32_le(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : UInt32,
#|) -> Unit {
#| bytes.unsafe_set(offset, value.to_byte())
#| bytes.unsafe_set(offset + 1, (value >> 8).to_byte())
#| bytes.unsafe_set(offset + 2, (value >> 16).to_byte())
#| bytes.unsafe_set(offset + 3, (value >> 24).to_byte())
#|}
#|fn store_u64_le(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : UInt64,
#|) -> Unit {
#| bytes.unsafe_set(offset, value.to_byte())
#| bytes.unsafe_set(offset + 1, (value >> 8).to_byte())
#| bytes.unsafe_set(offset + 2, (value >> 16).to_byte())
#| bytes.unsafe_set(offset + 3, (value >> 24).to_byte())
#| bytes.unsafe_set(offset + 4, (value >> 32).to_byte())
#| bytes.unsafe_set(offset + 5, (value >> 40).to_byte())
#| bytes.unsafe_set(offset + 6, (value >> 48).to_byte())
#| bytes.unsafe_set(offset + 7, (value >> 56).to_byte())
#|}
#|#intrinsic("%v128.i8x16_swizzle")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_swizzle(value : V128, indices : V128) -> V128 {
#| pack_u8x16_by(i => {
#| let index = u8_lane(indices, i).to_int()
#| if index < 16 {
#| u8_lane(value, index)
#| } else {
#| 0
#| }
#| })
#|}
#|#intrinsic("%v128.i8x16_relaxed_swizzle")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_relaxed_swizzle(value : V128, indices : V128) -> V128 {
#| i8x16_swizzle(value, indices)
#|}
#|fn shuffle_lane(value : V128, other : V128, index : Int32) -> Byte {
#| if index < 16 {
#| u8_lane(value, index)
#| } else {
#| u8_lane(other, index - 16)
#| }
#|}
#|#intrinsic("%v128.i8x16_shuffle")
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn i8x16_shuffle(
#| value : V128,
#| other : V128,
#| i0 : Int32,
#| i1 : Int32,
#| i2 : Int32,
#| i3 : Int32,
#| i4 : Int32,
#| i5 : Int32,
#| i6 : Int32,
#| i7 : Int32,
#| i8 : Int32,
#| i9 : Int32,
#| i10 : Int32,
#| i11 : Int32,
#| i12 : Int32,
#| i13 : Int32,
#| i14 : Int32,
#| i15 : Int32,
#|) -> V128 {
#| pack_u8x16_by(fn(i) {
#| match i {
#| 0 => shuffle_lane(value, other, i0)
#| 1 => shuffle_lane(value, other, i1)
#| 2 => shuffle_lane(value, other, i2)
#| 3 => shuffle_lane(value, other, i3)
#| 4 => shuffle_lane(value, other, i4)
#| 5 => shuffle_lane(value, other, i5)
#| 6 => shuffle_lane(value, other, i6)
#| 7 => shuffle_lane(value, other, i7)
#| 8 => shuffle_lane(value, other, i8)
#| 9 => shuffle_lane(value, other, i9)
#| 10 => shuffle_lane(value, other, i10)
#| 11 => shuffle_lane(value, other, i11)
#| 12 => shuffle_lane(value, other, i12)
#| 13 => shuffle_lane(value, other, i13)
#| 14 => shuffle_lane(value, other, i14)
#| _ => shuffle_lane(value, other, i15)
#| }
#| })
#|}
#|#intrinsic("%v128.v128_load")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| make(load_u64_le(bytes, offset), load_u64_le(bytes, offset + 8))
#|}
#|#intrinsic("%v128.v128_load_i16x8")
#|#borrow(str)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load_i16x8(str : String, offset : Int32) -> V128 {
#| make(load_u16x4_le(str, offset), load_u16x4_le(str, offset + 4))
#|}
#|#intrinsic("%v128.v128_load8x8_s")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load8x8_s(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| let value = load_u64_le(bytes, offset)
#| make(sign_extend_u8x4_to_u16x4(value), sign_extend_u8x4_to_u16x4(value >> 32))
#|}
#|#intrinsic("%v128.v128_load8x8_u")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load8x8_u(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| let value = load_u64_le(bytes, offset)
#| make(widen_u8x4_to_u16x4(value), widen_u8x4_to_u16x4(value >> 32))
#|}
#|#intrinsic("%v128.v128_load16x4_s")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load16x4_s(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| let value = load_u64_le(bytes, offset)
#| make(
#| sign_extend_u16x2_to_u32x2(value),
#| sign_extend_u16x2_to_u32x2(value >> 32),
#| )
#|}
#|#intrinsic("%v128.v128_load16x4_u")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load16x4_u(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| let value = load_u64_le(bytes, offset)
#| make(widen_u16x2_to_u32x2(value), widen_u16x2_to_u32x2(value >> 32))
#|}
#|#intrinsic("%v128.v128_load32x2_s")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load32x2_s(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| let value = load_u64_le(bytes, offset)
#| make(sign_extend_u32_to_u64(value), sign_extend_u32_to_u64(value >> 32))
#|}
#|#intrinsic("%v128.v128_load32x2_u")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load32x2_u(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| let value = load_u64_le(bytes, offset)
#| make(value & 0xffffffffUL, value >> 32)
#|}
#|#intrinsic("%v128.v128_load8_splat")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load8_splat(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| i8x16_splat(bytes.unsafe_get(offset))
#|}
#|#intrinsic("%v128.v128_load16_splat")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load16_splat(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| i16x8_splat(load_u16_le(bytes, offset))
#|}
#|#intrinsic("%v128.v128_load32_splat")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load32_splat(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| i32x4_splat(load_u32_le(bytes, offset))
#|}
#|#intrinsic("%v128.v128_load64_splat")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load64_splat(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| i64x2_splat(load_u64_le(bytes, offset))
#|}
#|#intrinsic("%v128.v128_load32_zero")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load32_zero(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| i32x4_const(load_u32_le(bytes, offset), 0, 0, 0)
#|}
#|#intrinsic("%v128.v128_load64_zero")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load64_zero(bytes : FixedArray[Byte], offset : Int32) -> V128 {
#| make(load_u64_le(bytes, offset), 0UL)
#|}
#|#intrinsic("%v128.v128_load8_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load8_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> V128 {
#| i8x16_replace_lane(value, bytes.unsafe_get(offset).to_uint(), lane)
#|}
#|#intrinsic("%v128.v128_load16_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load16_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> V128 {
#| i16x8_replace_lane(value, load_u16_le(bytes, offset).to_uint(), lane)
#|}
#|#intrinsic("%v128.v128_load32_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load32_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> V128 {
#| i32x4_replace_lane(value, load_u32_le(bytes, offset), lane)
#|}
#|#intrinsic("%v128.v128_load64_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_load64_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> V128 {
#| i64x2_replace_lane(value, load_u64_le(bytes, offset), lane)
#|}
#|#intrinsic("%v128.v128_store")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_store(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#|) -> Unit {
#| store_u64_le(bytes, offset, lo(value))
#| store_u64_le(bytes, offset + 8, hi(value))
#|}
#|#intrinsic("%v128.v128_store8_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_store8_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> Unit {
#| bytes.unsafe_set(offset, u8_lane(value, lane))
#|}
#|#intrinsic("%v128.v128_store16_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_store16_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> Unit {
#| store_u16_le(bytes, offset, u16_lane(value, lane))
#|}
#|#intrinsic("%v128.v128_store32_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_store32_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> Unit {
#| store_u32_le(bytes, offset, u32_lane(value, lane))
#|}
#|#intrinsic("%v128.v128_store64_lane")
#|#borrow(bytes)
#|#internal(experimental, "subject to breaking change without notice")
#|#doc(hidden)
#|pub fn v128_store64_lane(
#| bytes : FixedArray[Byte],
#| offset : Int32,
#| value : V128,
#| lane : Int32,
#|) -> Unit {
#| store_u64_le(bytes, offset, u64_lane(value, lane))
#|}
),
"v128.mbt": (
#|fn make(lo : UInt64, hi : UInt64) -> V128 = "%v128.make"
#|fn lo(value : V128) -> UInt64 = "%v128.lo"
#|fn hi(value : V128) -> UInt64 = "%v128.hi"
),
},
)
///|
let moonbitlang_core_sorted_map_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/sorted_map",
deps={},
files={
"debug.mbt": (
#|pub impl[K : @debug.Debug, V : @debug.Debug] @debug.Debug for SortedMap[K, V] with fn to_repr(
#| self,
#|) {
#| @debug.Repr::opaque_(
#| "SortedMap",
#| @debug.Repr::map([ for k, v in self => (Repr(k), Repr(v)) ]),
#| )
#|}
#|test "Debug for SortedMap" {
#| let sm : SortedMap[Int32, String] = from_array([(1, "a"), (2, "b")])
#| @debug.debug_inspect(
#| sm,
#| content=(
#| #|
#| ),
#| )
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `SortedMap([])` instead")
#|#as_free_fn(deprecated="Use `SortedMap([])` instead")
#|pub fn[K, V] SortedMap::new() -> SortedMap[K, V] {
#| new_sorted_map()
#|}
),
"extends.mbt": (
#|pub extend SortedMap with Compare::{compare}
#|pub extend SortedMap with Eq::{equal}
#|#deprecated("Use `Debug::to_repr` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with @debug.Debug::{to_repr}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with @json.FromJson::{from_json}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Eq::{not_equal}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with Show::{to_string}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedMap with @json.ToJson::{to_json}
),
"map.mbt": (
#|pub impl[K : Eq, V : Eq] Eq for SortedMap[K, V] with fn equal(self, other) {
#| guard self.size == other.size else { return false }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| guard a == b else { break false }
#| } nobreak {
#| true
#| }
#|}
#|pub impl[K : Compare, V : Compare] Compare for SortedMap[K, V] with fn compare(
#| self,
#| other,
#|) -> Int32 {
#| guard self.size == other.size else { return self.size - other.size }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| let cmp = a.compare(b)
#| guard cmp is 0 else { break cmp }
#| } nobreak {
#| 0
#| }
#|}
#|fn[K, V] new_sorted_map() -> SortedMap[K, V] {
#| { root: None, size: 0, }
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[K : Compare, V] SortedMap::SortedMap(
#| entries : ArrayView[(K, V)],
#|) -> SortedMap[K, V] {
#| let { length: prefix_len, ascending, } = sorted_map_ordered_prefix(entries)
#| let prefix = entries[:prefix_len]
#| let map = {
#| root: sorted_map_build_ordered(prefix, 0, prefix_len, ascending),
#| size: prefix_len,
#| }
#| for (key, value) in entries[prefix_len:] {
#| map.set(key, value)
#| }
#| map
#|}
#|#valtype
#|priv struct OrderedPrefix {
#| length : Int32
#| ascending : Bool
#|}
#|fn[K : Compare, V] sorted_map_ordered_prefix(
#| entries : ArrayView[(K, V)],
#|) -> OrderedPrefix {
#| let len = entries.length()
#| if len < 2 {
#| return { length: len, ascending: true, }
#| }
#| let ascending = entries[0].0 < entries[1].0
#| if !ascending && !(entries[1].0 < entries[0].0) {
#| return { length: 1, ascending: true, }
#| }
#| for i in 2.. Node[K, V]? {
#| guard start < end else { return None }
#| let middle = start + (end - start) / 2
#| let index = if ascending { middle } else { entries.length() - 1 - middle }
#| let (key, value) = entries[index]
#| let left = sorted_map_build_ordered(entries, start, middle, ascending)
#| let right = sorted_map_build_ordered(entries, middle + 1, end, ascending)
#| let height = 1 + Int32::max(height(left), height(right))
#| Some({ key, value, left, right, height, })
#|}
#|#alias("_[_]=_")
#|#alias(add, deprecated="Use set instead")
#|#owned(value)
#|pub fn[K : Compare, V] SortedMap::set(
#| self : SortedMap[K, V],
#| key : K,
#| value : V,
#|) -> Unit {
#| let (new_root, inserted) = add_node(self.root, key, value)
#| if self.root != new_root {
#| self.root = new_root
#| }
#| if inserted {
#| self.size += 1
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::remove(
#| self : SortedMap[K, V],
#| key : K,
#|) -> Unit {
#| if self.root is Some(old_root) {
#| let (new_root, deleted) = delete_node(old_root, key)
#| if self.root != new_root {
#| self.root = new_root
#| }
#| if deleted {
#| self.size -= 1
#| }
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::get(self : SortedMap[K, V], key : K) -> V? {
#| for x = self.root {
#| match x {
#| Some(node) => {
#| let cmp = key.compare(node.key)
#| if cmp == 0 {
#| break Some(node.value)
#| } else if cmp > 0 {
#| continue node.right
#| } else {
#| continue node.left
#| }
#| }
#| None => break None
#| }
#| }
#|}
#|#alias("_[_]")
#|pub fn[K : Compare, V] SortedMap::at(self : SortedMap[K, V], key : K) -> V {
#| for x = self.root {
#| match x {
#| Some(node) => {
#| let cmp = key.compare(node.key)
#| if cmp == 0 {
#| break node.value
#| } else if cmp > 0 {
#| continue node.right
#| } else {
#| continue node.left
#| }
#| }
#| None => panic()
#| }
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::get_or_init(
#| self : SortedMap[K, V],
#| key : K,
#| init : () -> V raise?,
#|) -> V raise? {
#| match self.get(key) {
#| Some(v) => v
#| None => {
#| let v = init()
#| self.set(key, v)
#| v
#| }
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::update_or_default(
#| self : SortedMap[K, V],
#| key : K,
#| default : V,
#| f : (V) -> V raise?,
#|) -> Unit raise? {
#| let (new_root, inserted) = update_or_default_node(self.root, key, default, f)
#| if self.root != new_root {
#| self.root = new_root
#| }
#| if inserted {
#| self.size += 1
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::update(
#| self : SortedMap[K, V],
#| key : K,
#| f : (V?) -> V? raise?,
#|) -> Unit raise? {
#| match f(self.get(key)) {
#| Some(value) => self.set(key, value)
#| None => self.remove(key)
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::get_or_default(
#| self : SortedMap[K, V],
#| key : K,
#| default : V,
#|) -> V {
#| match self.get(key) {
#| Some(v) | (None with v = default) => v
#| }
#|}
#|pub fn[K : Compare, V] SortedMap::contains(
#| self : SortedMap[K, V],
#| key : K,
#|) -> Bool {
#| self.get(key) is Some(_)
#|}
#|pub fn[K, V] SortedMap::is_empty(self : SortedMap[K, V]) -> Bool {
#| self.size == 0
#|}
#|#alias(size, deprecated)
#|pub fn[K, V] SortedMap::length(self : SortedMap[K, V]) -> Int32 {
#| self.size
#|}
#|pub fn[K, V] SortedMap::clear(self : SortedMap[K, V]) -> Unit {
#| self.root = None
#| self.size = 0
#|}
#|pub fn[K, V] SortedMap::each(
#| self : SortedMap[K, V],
#| f : (K, V) -> Unit raise?,
#|) -> Unit raise? {
#| fn dfs(root : Node[K, V]?) -> Unit raise? {
#| if root is Some(root) {
#| dfs(root.left)
#| f(root.key, root.value)
#| dfs(root.right)
#| }
#| }
#| dfs(self.root)
#|}
#|pub fn[K, V] SortedMap::eachi(
#| self : SortedMap[K, V],
#| f : (Int32, K, V) -> Unit raise?,
#|) -> Unit raise? {
#| let mut i = 0
#| self.each((k, v) => {
#| f(i, k, v)
#| i += 1
#| })
#|}
#|#alias(keys_as_iter, deprecated)
#|pub fn[K, V] SortedMap::keys(self : SortedMap[K, V]) -> Iter[K] {
#| let todo_list = []
#| let mut next_node = self.root
#| Iter(
#| fn() {
#| for x = next_node {
#| match x {
#| Some({ left, key, value: _, right, height: _, }) =>
#| if left is None {
#| next_node = right
#| break Some(key)
#| } else {
#| todo_list.push((key, right))
#| continue left
#| }
#| None if todo_list.pop() is Some((key, right)) => {
#| next_node = right
#| break Some(key)
#| }
#| None => break None
#| }
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|#alias(values_as_iter, deprecated)
#|pub fn[K, V] SortedMap::values(self : SortedMap[K, V]) -> Iter[V] {
#| let todo_list = []
#| let mut next_node = self.root
#| Iter(
#| fn() {
#| for x = next_node {
#| match x {
#| Some({ left, key: _, value, right, height: _, }) =>
#| if left is None {
#| next_node = right
#| break Some(value)
#| } else {
#| todo_list.push((value, right))
#| continue left
#| }
#| None if todo_list.pop() is Some((value, right)) => {
#| next_node = right
#| break Some(value)
#| }
#| None => break None
#| }
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|pub fn[K, V] SortedMap::to_array(self : SortedMap[K, V]) -> Array[(K, V)] {
#| [
#| for k, v in self => (k, v)
#| ]
#|}
#|#alias(iterator, deprecated)
#|pub fn[K, V] SortedMap::iter(self : SortedMap[K, V]) -> Iter[(K, V)] {
#| let todo_list = []
#| let mut next_node = self.root
#| Iter(
#| fn() {
#| for x = next_node {
#| match x {
#| Some({ left, key, value, right, height: _, }) =>
#| if left is None {
#| next_node = right
#| break Some((key, value))
#| } else {
#| todo_list.push((key, value, right))
#| continue left
#| }
#| None if todo_list.pop() is Some((key, value, right)) => {
#| next_node = right
#| break Some((key, value))
#| }
#| None => break None
#| }
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|#alias(iterator2, deprecated)
#|pub fn[K, V] SortedMap::iter2(self : SortedMap[K, V]) -> Iter2[K, V] {
#| self.iter()
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[K : Compare, V] SortedMap::from_iter(
#| iter : Iter[(K, V)],
#|) -> SortedMap[K, V] {
#| let m = new_sorted_map()
#| while iter.next() is Some((k, v)) {
#| m[k] = v
#| }
#| m
#|}
#|#label_migration(low, allow_positional=true)
#|#label_migration(high, allow_positional=true)
#|pub fn[K : Compare, V] SortedMap::range(
#| self : SortedMap[K, V],
#| low~ : K,
#| high~ : K,
#|) -> Iter2[K, V] {
#| let todo_list = []
#| let mut next_node = self.root
#| Iter(fn() {
#| for x = next_node {
#| match x {
#| Some({ left, key, value, right, height: _, }) => {
#| let cmp_key_low = key.compare(low)
#| let cmp_key_high = key.compare(high)
#| if cmp_key_low < 0 {
#| continue right
#| } else if cmp_key_high > 0 {
#| continue left
#| } else if left is None {
#| next_node = right
#| break Some((key, value))
#| } else {
#| todo_list.push((key, value, right))
#| continue left
#| }
#| }
#| None if todo_list.pop() is Some((key, value, right)) => {
#| next_node = right
#| break Some((key, value))
#| }
#| None => break None
#| }
#| }
#| })
#|}
#|#alias(clone, deprecated)
#|pub fn[K, V] SortedMap::copy(self : SortedMap[K, V]) -> SortedMap[K, V] {
#| fn copy_node(node : Node[K, V]?) -> Node[K, V]? {
#| match node {
#| None => None
#| Some({ key, value, left, right, height, }) =>
#| Some({
#| key,
#| value,
#| left: copy_node(left),
#| right: copy_node(right),
#| height,
#| })
#| }
#| }
#| { root: copy_node(self.root), size: self.size, }
#|}
#|pub fn[K : Compare, V] SortedMap::merge(
#| self : SortedMap[K, V],
#| other : SortedMap[K, V],
#|) -> SortedMap[K, V] {
#| let result = self.copy()
#| other.each((k, v) => result.set(k, v))
#| result
#|}
#|pub fn[K : Compare, V] SortedMap::merge_in_place(
#| self : SortedMap[K, V],
#| other : SortedMap[K, V],
#|) -> Unit {
#| other.each((k, v) => self.set(k, v))
#|}
#|fn[K, V] replace_root_with_min(
#| root : Node[K, V],
#| node : Node[K, V],
#|) -> Node[K, V]? {
#| let (l, r) = (node.left, node.right)
#| match l {
#| None => {
#| root.key = node.key
#| root.value = node.value
#| r
#| }
#| Some(ln) => {
#| node.left = replace_root_with_min(root, ln)
#| Some(balance(node))
#| }
#| }
#|}
#|fn[K, V] Node::update_height(self : Node[K, V]) -> Unit {
#| self.height = 1 + height(self.left).max(height(self.right))
#|}
#|fn[K, V] height_ge(x1 : Node[K, V]?, x2 : Node[K, V]?) -> Bool {
#| match (x1, x2) {
#| (
#| Some({ height: h1, .. })
#| | (None with h1 = 0),
#| Some({ height: h2, .. })
#| | (None with h2 = 0),
#| ) => h1 >= h2
#| }
#|}
#|#owned(root)
#|fn[K, V] balance(root : Node[K, V]) -> Node[K, V] {
#| let (l, r) = (root.left, root.right)
#| let (hl, hr) = (height(l), height(r))
#| let new_root = if hl > hr + 1 {
#| let { left: ll, right: lr, .. } = l.unwrap()
#| if height_ge(ll, lr) {
#| rotate_r(root)
#| } else {
#| rotate_lr(root)
#| }
#| } else if hr > hl + 1 {
#| let { left: rl, right: rr, .. } = r.unwrap()
#| if height_ge(rr, rl) {
#| rotate_l(root)
#| } else {
#| rotate_rl(root)
#| }
#| } else {
#| root
#| }
#| new_root.update_height()
#| new_root
#|}
#|#owned(n)
#|fn[K, V] rotate_l(n : Node[K, V]) -> Node[K, V] {
#| let r = n.right.unwrap()
#| n.right = r.left
#| n.update_height()
#| r.left = Some(n)
#| r.update_height()
#| r
#|}
#|#owned(n)
#|fn[K, V] rotate_r(n : Node[K, V]) -> Node[K, V] {
#| let l = n.left.unwrap()
#| n.left = l.right
#| n.update_height()
#| l.right = Some(n)
#| l.update_height()
#| l
#|}
#|#owned(n)
#|fn[K, V] rotate_lr(n : Node[K, V]) -> Node[K, V] {
#| let l = n.left.unwrap()
#| let v = rotate_l(l)
#| n.left = Some(v)
#| rotate_r(n)
#|}
#|#owned(n)
#|fn[K, V] rotate_rl(n : Node[K, V]) -> Node[K, V] {
#| let r = n.right.unwrap()
#| let v = rotate_r(r)
#| n.right = Some(v)
#| rotate_l(n)
#|}
#|#owned(value)
#|fn[K : Compare, V] add_node(
#| root : Node[K, V]?,
#| key : K,
#| value : V,
#|) -> (Node[K, V]?, Bool) {
#| match root {
#| None => (Some(new_node(key, value)), true)
#| Some(n) =>
#| if key == n.key {
#| n.value = value
#| (Some(n), false)
#| } else {
#| let (l, r) = (n.left, n.right)
#| if key < n.key {
#| let (nl, inserted) = add_node(l, key, value)
#| n.left = nl
#| (Some(balance(n)), inserted)
#| } else {
#| let (nr, inserted) = add_node(r, key, value)
#| n.right = nr
#| (Some(balance(n)), inserted)
#| }
#| }
#| }
#|}
#|fn[K : Compare, V] update_or_default_node(
#| root : Node[K, V]?,
#| key : K,
#| default : V,
#| f : (V) -> V raise?,
#|) -> (Node[K, V]?, Bool) raise? {
#| match root {
#| None => (Some(new_node(key, default)), true)
#| Some(n) =>
#| if key == n.key {
#| n.value = f(n.value)
#| (Some(n), false)
#| } else {
#| let (l, r) = (n.left, n.right)
#| if key < n.key {
#| let (nl, inserted) = update_or_default_node(l, key, default, f)
#| n.left = nl
#| (Some(balance(n)), inserted)
#| } else {
#| let (nr, inserted) = update_or_default_node(r, key, default, f)
#| n.right = nr
#| (Some(balance(n)), inserted)
#| }
#| }
#| }
#|}
#|fn[K : Compare, V] delete_node(
#| root : Node[K, V],
#| key : K,
#|) -> (Node[K, V]?, Bool) {
#| if key == root.key {
#| let (l, r) = (root.left, root.right)
#| let n = match (l, r) {
#| (Some(_), Some(nr)) => {
#| root.right = replace_root_with_min(root, nr)
#| Some(balance(root))
#| }
#| (None, Some(_)) => r
#| (Some(_), None) | (None, None) => l
#| }
#| (n, true)
#| } else if key < root.key {
#| match root.left {
#| None => (Some(root), false)
#| Some(l) => {
#| let (nl, deleted) = delete_node(l, key)
#| root.left = nl
#| (Some(balance(root)), deleted)
#| }
#| }
#| } else {
#| match root.right {
#| None => (Some(root), false)
#| Some(r) => {
#| let (nr, deleted) = delete_node(r, key)
#| root.right = nr
#| (Some(balance(root)), deleted)
#| }
#| }
#| }
#|}
#|test "new" {
#| let map : SortedMap[Int32, String] = new_sorted_map()
#| inspect(map.debug_tree(), content="_")
#| inspect(map.length(), content="0")
#|}
#|test "of" {
#| let map = from_array([(3, "c"), (2, "b"), (1, "a")])
#| inspect(map.debug_tree(), content="([2]2,b,([1]1,a,_,_),([1]3,c,_,_))")
#| inspect(map.length(), content="3")
#|}
#|test "add1" {
#| let map = new_sorted_map()
#| map.set(6, "a")
#| map.set(5, "b")
#| map.set(4, "c")
#| map.set(3, "d")
#| map.set(2, "e")
#| map.set(1, "f")
#| inspect(
#| map.debug_tree(),
#| content="([3]3,d,([2]2,e,([1]1,f,_,_),_),([2]5,b,([1]4,c,_,_),([1]6,a,_,_)))",
#| )
#| inspect(map.length(), content="6")
#|}
#|test "add2" {
#| let map = new_sorted_map()
#| map.set(1, "a")
#| map.set(2, "b")
#| map.set(3, "c")
#| map.set(4, "d")
#| map.set(5, "e")
#| map.set(6, "f")
#| inspect(
#| map.debug_tree(),
#| content="([3]4,d,([2]2,b,([1]1,a,_,_),([1]3,c,_,_)),([2]5,e,_,([1]6,f,_,_)))",
#| )
#| inspect(map.length(), content="6")
#|}
#|test "add3" {
#| let map = new_sorted_map()
#| map.set(4, "a")
#| map.set(1, "b")
#| map.set(3, "c")
#| map.set(2, "d")
#| inspect(
#| map.debug_tree(),
#| content="([3]3,c,([2]1,b,_,([1]2,d,_,_)),([1]4,a,_,_))",
#| )
#|}
#|test "add4" {
#| let map = new_sorted_map()
#| map.set(1, "a")
#| map.set(4, "b")
#| map.set(2, "c")
#| map.set(3, "d")
#| inspect(
#| map.debug_tree(),
#| content="([3]2,c,([1]1,a,_,_),([2]4,b,([1]3,d,_,_),_))",
#| )
#|}
#|test "add duplicate key" {
#| let map = from_array([(3, "c"), (2, "b"), (1, "a")])
#| inspect(map.debug_tree(), content="([2]2,b,([1]1,a,_,_),([1]3,c,_,_))")
#| map.set(1, "x")
#| inspect(map.debug_tree(), content="([2]2,b,([1]1,x,_,_),([1]3,c,_,_))")
#|}
#|test "remove" {
#| let map = new_sorted_map()
#| for
#| (key, value) in [
#| (1, "a"),
#| (2, "b"),
#| (3, "c"),
#| (4, "d"),
#| (5, "e"),
#| (6, "f"),
#| (7, "g"),
#| (8, "h"),
#| ] {
#| map.set(key, value)
#| }
#| inspect(
#| map.debug_tree(),
#| content="([4]4,d,([2]2,b,([1]1,a,_,_),([1]3,c,_,_)),([3]6,f,([1]5,e,_,_),([2]7,g,_,([1]8,h,_,_))))",
#| )
#| inspect(map.length(), content="8")
#| map.remove(6)
#| inspect(
#| map.debug_tree(),
#| content="([3]4,d,([2]2,b,([1]1,a,_,_),([1]3,c,_,_)),([2]7,g,([1]5,e,_,_),([1]8,h,_,_)))",
#| )
#| inspect(map.length(), content="7")
#| map.remove(4)
#| inspect(
#| map.debug_tree(),
#| content="([3]5,e,([2]2,b,([1]1,a,_,_),([1]3,c,_,_)),([2]7,g,_,([1]8,h,_,_)))",
#| )
#| inspect(map.length(), content="6")
#| map.remove(2)
#| inspect(
#| map.debug_tree(),
#| content="([3]5,e,([2]3,c,([1]1,a,_,_),_),([2]7,g,_,([1]8,h,_,_)))",
#| )
#| inspect(map.length(), content="5")
#| map.remove(3)
#| inspect(
#| map.debug_tree(),
#| content="([3]5,e,([1]1,a,_,_),([2]7,g,_,([1]8,h,_,_)))",
#| )
#| inspect(map.length(), content="4")
#| map.remove(5)
#| inspect(map.debug_tree(), content="([2]7,g,([1]1,a,_,_),([1]8,h,_,_))")
#| inspect(map.length(), content="3")
#| map.remove(7)
#| inspect(map.debug_tree(), content="([2]8,h,([1]1,a,_,_),_)")
#| inspect(map.length(), content="2")
#| map.remove(8)
#| inspect(map.debug_tree(), content="([1]1,a,_,_)")
#| inspect(map.length(), content="1")
#| map.remove(1)
#| inspect(map.debug_tree(), content="_")
#| inspect(map.size, content="0")
#|}
#|test "_[_]=_" {
#| let map = new_sorted_map()
#| map[1] = "a"
#| map[2] = "b"
#| map[3] = "c"
#| inspect(map.debug_tree(), content="([2]2,b,([1]1,a,_,_),([1]3,c,_,_))")
#|}
#|test "clear" {
#| let map = from_array([(3, "c"), (2, "b"), (1, "a")])
#| map.clear()
#| inspect(map.debug_tree(), content="_")
#| inspect(map.length(), content="0")
#|}
#|test "copy" {
#| let map1 = from_array([
#| (1, "a"),
#| (2, "b"),
#| (3, "c"),
#| (4, "d"),
#| (5, "e"),
#| (6, "f"),
#| ])
#| let map2 = map1.copy()
#| inspect(map1.debug_tree(), content=map2.debug_tree())
#| inspect(map1.length(), content="6")
#| inspect(map2.length(), content="6")
#| map2.set(7, "g")
#| inspect(map1.length(), content="6")
#| inspect(map2.length(), content="7")
#| @debug.debug_inspect(map1.get(7), content="None")
#| @debug.debug_inspect(map2.get(7), content="Some(\"g\")")
#| map1.remove(1)
#| inspect(map1.length(), content="5")
#| inspect(map2.length(), content="7")
#| @debug.debug_inspect(map1.get(1), content="None")
#| @debug.debug_inspect(map2.get(1), content="Some(\"a\")")
#|}
#|pub impl[K, V] Default for SortedMap[K, V] with fn default() {
#| new_sorted_map()
#|}
),
"navigate.mbt": (
#|pub fn[K, V] SortedMap::first(self : SortedMap[K, V]) -> (K, V)? {
#| guard self.root is Some(root) else { return None }
#| for node = root {
#| match node.left {
#| Some(left) => continue left
#| None => break Some((node.key, node.value))
#| }
#| }
#|}
#|pub fn[K, V] SortedMap::last(self : SortedMap[K, V]) -> (K, V)? {
#| guard self.root is Some(root) else { return None }
#| for node = root {
#| match node.right {
#| Some(right) => continue right
#| None => break Some((node.key, node.value))
#| }
#| }
#|}
#|pub fn[K, V] SortedMap::pop_first(self : SortedMap[K, V]) -> (K, V)? {
#| guard self.root is Some(root) else { return None }
#| let (new_root, key, value) = remove_min_node(root)
#| self.root = new_root
#| self.size -= 1
#| Some((key, value))
#|}
#|pub fn[K, V] SortedMap::pop_last(self : SortedMap[K, V]) -> (K, V)? {
#| guard self.root is Some(root) else { return None }
#| let (new_root, key, value) = remove_max_node(root)
#| self.root = new_root
#| self.size -= 1
#| Some((key, value))
#|}
#|pub fn[K : Compare, V] SortedMap::first_ge(
#| self : SortedMap[K, V],
#| key : K,
#|) -> (K, V)? {
#| successor(self.root, key, inclusive=true)
#|}
#|pub fn[K : Compare, V] SortedMap::first_gt(
#| self : SortedMap[K, V],
#| key : K,
#|) -> (K, V)? {
#| successor(self.root, key, inclusive=false)
#|}
#|pub fn[K : Compare, V] SortedMap::last_le(
#| self : SortedMap[K, V],
#| key : K,
#|) -> (K, V)? {
#| predecessor(self.root, key, inclusive=true)
#|}
#|pub fn[K : Compare, V] SortedMap::last_lt(
#| self : SortedMap[K, V],
#| key : K,
#|) -> (K, V)? {
#| predecessor(self.root, key, inclusive=false)
#|}
#|pub fn[K, V] SortedMap::rev_iter(self : SortedMap[K, V]) -> Iter[(K, V)] {
#| rev_nodes(self).map(node => (node.key, node.value))
#|}
#|pub fn[K, V] SortedMap::rev_iter2(self : SortedMap[K, V]) -> Iter2[K, V] {
#| self.rev_iter()
#|}
#|pub fn[K, V] SortedMap::rev_keys(self : SortedMap[K, V]) -> Iter[K] {
#| rev_nodes(self).map(node => node.key)
#|}
#|pub fn[K, V] SortedMap::rev_values(self : SortedMap[K, V]) -> Iter[V] {
#| rev_nodes(self).map(node => node.value)
#|}
#|fn[K, V] rev_nodes(map : SortedMap[K, V]) -> Iter[Node[K, V]] {
#| let stack : Array[Node[K, V]] = []
#| let mut next_node = map.root
#| Iter(
#| fn() {
#| for x = next_node {
#| match x {
#| Some(node) =>
#| if node.right is None {
#| next_node = node.left
#| break Some(node)
#| } else {
#| stack.push(node)
#| continue node.right
#| }
#| None =>
#| match stack.pop() {
#| Some(node) => {
#| next_node = node.left
#| break Some(node)
#| }
#| None => {
#| next_node = None
#| break None
#| }
#| }
#| }
#| }
#| },
#| size_hint=map.size,
#| )
#|}
#|fn[K : Compare, V] successor(
#| root : Node[K, V]?,
#| key : K,
#| inclusive~ : Bool,
#|) -> (K, V)? {
#| for x = root, best = None {
#| match x {
#| Some(node) => {
#| let cmp = node.key.compare(key)
#| if cmp == 0 && inclusive {
#| break Some((node.key, node.value))
#| } else if cmp > 0 {
#| continue node.left, Some(node)
#| } else {
#| continue node.right, best
#| }
#| }
#| None => break best.map(node => (node.key, node.value))
#| }
#| }
#|}
#|fn[K : Compare, V] predecessor(
#| root : Node[K, V]?,
#| key : K,
#| inclusive~ : Bool,
#|) -> (K, V)? {
#| for x = root, best = None {
#| match x {
#| Some(node) => {
#| let cmp = node.key.compare(key)
#| if cmp == 0 && inclusive {
#| break Some((node.key, node.value))
#| } else if cmp < 0 {
#| continue node.right, Some(node)
#| } else {
#| continue node.left, best
#| }
#| }
#| None => break best.map(node => (node.key, node.value))
#| }
#| }
#|}
#|fn[K, V] remove_min_node(node : Node[K, V]) -> (Node[K, V]?, K, V) {
#| match node.left {
#| None => (node.right, node.key, node.value)
#| Some(left) => {
#| let (new_left, key, value) = remove_min_node(left)
#| node.left = new_left
#| (Some(balance(node)), key, value)
#| }
#| }
#|}
#|fn[K, V] remove_max_node(node : Node[K, V]) -> (Node[K, V]?, K, V) {
#| match node.right {
#| None => (node.left, node.key, node.value)
#| Some(right) => {
#| let (new_right, key, value) = remove_max_node(right)
#| node.right = new_right
#| (Some(balance(node)), key, value)
#| }
#| }
#|}
),
"range_bounds.mbt": (
#|pub fn[K : Compare, V] SortedMap::range_bounds(
#| self : SortedMap[K, V],
#| lower : @cmp.Bound[K],
#| upper : @cmp.Bound[K],
#|) -> Iter2[K, V] {
#| let stack : Array[Node[K, V]] = []
#| let mut next_node = self.root
#| Iter(fn() {
#| for x = next_node {
#| match x {
#| Some(node) =>
#| if !satisfies_lower(node.key, lower) {
#| continue node.right
#| } else if !satisfies_upper(node.key, upper) {
#| continue node.left
#| } else if node.left is None {
#| next_node = node.right
#| break Some((node.key, node.value))
#| } else {
#| stack.push(node)
#| continue node.left
#| }
#| None =>
#| match stack.pop() {
#| Some(node) => {
#| next_node = node.right
#| break Some((node.key, node.value))
#| }
#| None => {
#| next_node = None
#| break None
#| }
#| }
#| }
#| }
#| })
#|}
#|pub fn[K : Compare, V] SortedMap::rev_range_bounds(
#| self : SortedMap[K, V],
#| lower : @cmp.Bound[K],
#| upper : @cmp.Bound[K],
#|) -> Iter2[K, V] {
#| let stack : Array[Node[K, V]] = []
#| let mut next_node = self.root
#| Iter(fn() {
#| for x = next_node {
#| match x {
#| Some(node) =>
#| if !satisfies_upper(node.key, upper) {
#| continue node.left
#| } else if !satisfies_lower(node.key, lower) {
#| continue node.right
#| } else if node.right is None {
#| next_node = node.left
#| break Some((node.key, node.value))
#| } else {
#| stack.push(node)
#| continue node.right
#| }
#| None =>
#| match stack.pop() {
#| Some(node) => {
#| next_node = node.left
#| break Some((node.key, node.value))
#| }
#| None => {
#| next_node = None
#| break None
#| }
#| }
#| }
#| }
#| })
#|}
#|#label_migration(low, allow_positional=true)
#|#label_migration(high, allow_positional=true)
#|pub fn[K : Compare, V] SortedMap::rev_range(
#| self : SortedMap[K, V],
#| low~ : K,
#| high~ : K,
#|) -> Iter2[K, V] {
#| self.rev_range_bounds(Included(low), Included(high))
#|}
#|fn[K : Compare] satisfies_lower(key : K, lower : @cmp.Bound[K]) -> Bool {
#| match lower {
#| Included(bound) => key.compare(bound) >= 0
#| Excluded(bound) => key.compare(bound) > 0
#| Unbounded => true
#| }
#|}
#|fn[K : Compare] satisfies_upper(key : K, upper : @cmp.Bound[K]) -> Bool {
#| match upper {
#| Included(bound) => key.compare(bound) <= 0
#| Excluded(bound) => key.compare(bound) < 0
#| Unbounded => true
#| }
#|}
),
"types.mbt": (
#|#unsafe_cycle_free
#|priv struct Node[K, V] {
#| mut key : K
#| mut value : V
#| mut left : Node[K, V]?
#| mut right : Node[K, V]?
#| mut height : Int32
#|}
#|struct SortedMap[K, V] {
#| mut root : Node[K, V]?
#| mut size : Int32
#|}
),
"utils.mbt": (
#|#owned(key, value)
#|fn[K, V] new_node(key : K, value : V) -> Node[K, V] {
#| { key, value, left: None, right: None, height: 1, }
#|}
#|impl[K : Eq, V] Eq for Node[K, V] with fn equal(self, other) {
#| self.key == other.key
#|}
#|fn[K, V] height(node : Node[K, V]?) -> Int32 {
#| match node {
#| Some({ height, .. }) | (None with height = 0) => height
#| }
#|}
#|fn[K : Show, V : Show] Node::debug_node(self : Node[K, V]) -> String {
#| let l = match self.left {
#| Some(left) => left.debug_node()
#| None => "_"
#| }
#| let r = match self.right {
#| Some(right) => right.debug_node()
#| None => "_"
#| }
#| "([\{self.height}]\{self.key},\{self.value},\{l},\{r})"
#|}
#|fn[K : Show, V : Show] SortedMap::debug_tree(self : SortedMap[K, V]) -> String {
#| match self.root {
#| Some(root) => root.debug_node()
#| None => "_"
#| }
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[K : Show, V : Show] Show for SortedMap[K, V]
#|#warnings("-deprecated")
#|pub impl[K : Show, V : Show] Show for SortedMap[K, V] with fn output(
#| self,
#| logger,
#|) {
#| logger.write_iter(self.iter(), prefix="@sorted_map.from_array([", suffix="])")
#|}
#|pub impl[K : Show, V : @json.ToJson] @json.ToJson for SortedMap[K, V] with fn to_json(
#| self,
#|) {
#| @json.Json::object(
#| Map(
#| capacity=self.length(),
#| [
#| for k, v in self => (k.to_string(), v.to_json())
#| ],
#| ),
#| )
#|}
#|pub impl[V : @json.FromJson] @json.FromJson for SortedMap[String, V] with fn from_json(
#| json,
#| path,
#|) {
#| guard json is Object(obj) else {
#| raise path.error("@sorted_map.from_json: expected object")
#| }
#| let map = new_sorted_map()
#| for k, v in obj {
#| map.set(k, V::from_json(v, path.add_key(k)))
#| }
#| map
#|}
),
},
)
///|
let moonbitlang_core_sorted_set_module : RuntimePackage = RuntimePackage::new(
"moonbitlang/core/sorted_set",
deps={},
files={
"debug.mbt": (
#|pub impl[V : @debug.Debug] @debug.Debug for SortedSet[V] with fn to_repr(self) {
#| @debug.Repr::opaque_(
#| "SortedSet",
#| @debug.Repr::array([ for x in self => Repr(x) ]),
#| )
#|}
#|test "Debug for SortedSet" {
#| let ss : SortedSet[Int32] = from_array([3, 1, 2])
#| @debug.debug_inspect(ss, content="")
#|}
),
"deprecated.mbt": (
#|#deprecated("Use `SortedSet([])` instead")
#|#as_free_fn(deprecated="Use `SortedSet([])` instead")
#|pub fn[V] SortedSet::new() -> SortedSet[V] {
#| new_sorted_set()
#|}
),
"extends.mbt": (
#|pub extend SortedSet with Compare::{compare}
#|pub extend SortedSet with Eq::{equal}
#|#deprecated("Use the `@debug.Debug` trait instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with @debug.Debug::{to_repr}
#|#deprecated("Use `Default::default` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Default::{default}
#|#deprecated("Use `!=` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Eq::{not_equal}
#|#deprecated("Use `@debug.Debug` instead of `Show` for collections", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with Show::{to_string}
#|#deprecated("Use `@json.from_json` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with @json.FromJson::{from_json}
#|#deprecated("Use `Json(value)` instead", skip_current_package=true)
#|#doc(hidden)
#|pub extend SortedSet with @json.ToJson::{to_json}
),
"set.mbt": (
#|fn[V] new_sorted_set() -> SortedSet[V] {
#| { root: None, size: 0, }
#|}
#|#as_free_fn
#|#owned(value)
#|pub fn[V] SortedSet::singleton(value : V) -> SortedSet[V] {
#| { root: Some({ value, left: None, right: None, height: 1, }), size: 1, }
#|}
#|#alias(from_array)
#|#as_free_fn(from_array)
#|#alias(of, deprecated="Use from_array instead")
#|#as_free_fn(of, deprecated="Use from_array instead")
#|pub fn[V : Compare] SortedSet::SortedSet(array : ArrayView[V]) -> SortedSet[V] {
#| let set = new_sorted_set()
#| for x in array {
#| set.insert(x)
#| }
#| set
#|}
#|#alias(deep_clone, deprecated)
#|#alias(clone, deprecated)
#|pub fn[V] SortedSet::copy(self : SortedSet[V]) -> SortedSet[V] {
#| match self.root {
#| None => new_sorted_set()
#| Some(_) => { root: copy_tree(self.root), size: self.size, }
#| }
#|}
#|fn[V] copy_tree(node : Node[V]?) -> Node[V]? {
#| match node {
#| None => None
#| Some(node) => {
#| let left = copy_tree(node.left)
#| let right = copy_tree(node.right)
#| let new_node = new_node(node.value, left~, right~, height=node.height)
#| Some(new_node)
#| }
#| }
#|}
#|#owned(value, left, right)
#|fn[V] new_node(
#| value : V,
#| left~ : Node[V]? = None,
#| right~ : Node[V]? = None,
#| height~ : Int32 = 1,
#|) -> Node[V] {
#| { value, left, right, height, }
#|}
#|#owned(value, left, right)
#|fn[V] new_node_update_height(
#| value : V,
#| left~ : Node[V]?,
#| right~ : Node[V]?,
#|) -> Node[V] {
#| { value, left, right, height: height(left).max(height(right)) + 1, }
#|}
#|#alias(add, deprecated="Use `insert` instead")
#|#owned(value)
#|pub fn[V : Compare] SortedSet::insert(self : SortedSet[V], value : V) -> Unit {
#| let (new_root, inserted) = add_node(self.root, value)
#| if self.root != new_root {
#| self.root = new_root
#| }
#| if inserted {
#| self.size += 1
#| }
#|}
#|pub fn[V : Compare] SortedSet::remove(self : SortedSet[V], value : V) -> Unit {
#| if self.root is Some(old_root) {
#| let (new_root, deleted) = delete_node(old_root, value)
#| if self.root != new_root {
#| self.root = new_root
#| }
#| if deleted {
#| self.size -= 1
#| }
#| }
#|}
#|pub fn[V : Compare] SortedSet::contains(self : SortedSet[V], value : V) -> Bool {
#| for root = self.root, value = value {
#| match (root, value) {
#| (None, _) => break false
#| (Some(node), value) => {
#| let compare_result = value.compare(node.value)
#| if compare_result == 0 {
#| break true
#| } else if compare_result < 0 {
#| continue node.left, value
#| } else {
#| continue node.right, value
#| }
#| }
#| }
#| }
#|}
#|fn[V : Compare] lookup(root : Node[V]?, value : V) -> V? {
#| for node = root {
#| match node {
#| None => break None
#| Some(n) => {
#| let compare_result = value.compare(n.value)
#| if compare_result == 0 {
#| break Some(n.value)
#| } else if compare_result < 0 {
#| continue n.left
#| } else {
#| continue n.right
#| }
#| }
#| }
#| }
#|}
#|pub fn[V : Compare] SortedSet::union(
#| self : SortedSet[V],
#| src : SortedSet[V],
#|) -> SortedSet[V] {
#| let mut dups = 0
#| fn aux(a : Node[V]?, b : Node[V]?) -> Node[V]? {
#| match (a, b) {
#| (Some(_), None) => a
#| (None, Some(_)) => b
#| (Some({ value: va, left: la, right: ra, .. }), Some(_)) => {
#| let { left: l, found, right: r, } = split_member(b, va)
#| if found {
#| dups += 1
#| }
#| Some(join(aux(la, l), va, aux(ra, r)))
#| }
#| (None, None) => None
#| }
#| }
#| match (self.root, src.root) {
#| (Some(_), Some(_)) => {
#| let t = aux(copy_tree(self.root), copy_tree(src.root))
#| { root: t, size: self.size + src.size - dups, }
#| }
#| (Some(_), None) => { root: copy_tree(self.root), size: self.size, }
#| (None, Some(_)) => { root: copy_tree(src.root), size: src.size, }
#| (None, None) => new_sorted_set()
#| }
#|}
#|#valtype
#|priv struct SplitResult[V] {
#| left : Node[V]?
#| found : Bool
#| right : Node[V]?
#|}
#|fn[V : Compare] split_member(root : Node[V]?, value : V) -> SplitResult[V] {
#| match root {
#| None => { left: None, found: false, right: None, }
#| Some(node) => {
#| let comp = value.compare(node.value)
#| if comp == 0 {
#| { left: node.left, found: true, right: node.right, }
#| } else if comp < 0 {
#| let { left, found, right, } = split_member(node.left, value)
#| { left, found, right: Some(join(right, node.value, node.right)), }
#| } else {
#| let { left, found, right, } = split_member(node.right, value)
#| { left: Some(join(node.left, node.value, left)), found, right, }
#| }
#| }
#| }
#|}
#|fn[V] concat(left : Node[V]?, right : Node[V]?) -> Node[V]? {
#| match (left, right) {
#| (None, _) => right
#| (_, None) => left
#| (Some(_), Some(r)) => {
#| let (min_val, rest) = remove_min(r)
#| Some(join(left, min_val, rest))
#| }
#| }
#|}
#|fn[V] remove_min(node : Node[V]) -> (V, Node[V]?) {
#| match node.left {
#| None => (node.value, node.right)
#| Some(left) => {
#| let (min_val, new_left) = remove_min(left)
#| (min_val, Some(join(new_left, node.value, node.right)))
#| }
#| }
#|}
#|#owned(left, value, right)
#|fn[V] join(left : Node[V]?, value : V, right : Node[V]?) -> Node[V] {
#| let (hl, hr) = (height(left), height(right))
#| if hl > hr + 1 {
#| join_right(left, value, right)
#| } else if hr > hl + 1 {
#| join_left(left, value, right)
#| } else {
#| new_node_update_height(value, left~, right~)
#| }
#|}
#|#owned(l, v)
#|fn[V] join_left(l : Node[V]?, v : V, r : Node[V]?) -> Node[V] {
#| let { value: rv, left: rl, right: rr, .. } = r.unwrap()
#| let node = if height(rl) <= height(l) + 1 {
#| let new_l = new_node_update_height(left=l, v, right=rl)
#| if height(Some(new_l)) <= height(rr) + 1 {
#| new_node_update_height(left=Some(new_l), rv, right=rr)
#| } else {
#| let new_l = rotate_l(new_l)
#| let new = new_node_update_height(left=Some(new_l), rv, right=rr)
#| rotate_r(new)
#| }
#| } else {
#| let new_l = join_left(l, v, rl)
#| let new = new_node_update_height(left=Some(new_l), rv, right=rr)
#| if height(Some(new_l)) <= height(rr) + 1 {
#| new
#| } else {
#| rotate_r(new)
#| }
#| }
#| node.update_height()
#| node
#|}
#|#owned(v, r)
#|fn[V] join_right(l : Node[V]?, v : V, r : Node[V]?) -> Node[V] {
#| let { value: lv, left: ll, right: lr, .. } = l.unwrap()
#| let node = if height(lr) <= height(r) + 1 {
#| let new_r = new_node_update_height(left=lr, v, right=r)
#| if height(Some(new_r)) <= height(ll) + 1 {
#| new_node_update_height(left=ll, lv, right=Some(new_r))
#| } else {
#| let new_r = rotate_r(new_r)
#| let new = new_node_update_height(left=ll, lv, right=Some(new_r))
#| rotate_l(new)
#| }
#| } else {
#| let new_r = join_right(lr, v, r)
#| let new = new_node_update_height(left=ll, lv, right=Some(new_r))
#| if height(Some(new_r)) <= height(ll) + 1 {
#| new
#| } else {
#| rotate_l(new)
#| }
#| }
#| node.update_height()
#| node
#|}
#|#alias(diff, deprecated)
#|pub fn[V : Compare] SortedSet::difference(
#| self : SortedSet[V],
#| src : SortedSet[V],
#|) -> SortedSet[V] {
#| match (self.root, src.root) {
#| (None, _) => new_sorted_set()
#| (_, None) => { root: copy_tree(self.root), size: self.size, }
#| (Some(_), Some(_)) => {
#| if self.size < src.size / 16 {
#| let ret = new_sorted_set()
#| self.each(x => if !src.contains(x) { ret.insert(x) })
#| return ret
#| }
#| let mut found_count = 0
#| fn aux(a : Node[V]?, b : Node[V]?) -> Node[V]? {
#| match (a, b) {
#| (None, _) => None
#| (_, None) => a
#| (Some({ value: va, left: la, right: ra, .. }), _) => {
#| let { left: lb, found, right: rb, } = split_member(b, va)
#| if found {
#| found_count += 1
#| concat(aux(la, lb), aux(ra, rb))
#| } else {
#| Some(join(aux(la, lb), va, aux(ra, rb)))
#| }
#| }
#| }
#| }
#| let t = aux(copy_tree(self.root), copy_tree(src.root))
#| { root: t, size: self.size - found_count, }
#| }
#| }
#|}
#|pub fn[V : Compare] SortedSet::symmetric_difference(
#| self : SortedSet[V],
#| other : SortedSet[V],
#|) -> SortedSet[V] {
#| let mut found_count = 0
#| fn aux(a : Node[V]?, b : Node[V]?) -> Node[V]? {
#| match (a, b) {
#| (None, _) => b
#| (_, None) => a
#| (Some({ value: va, left: la, right: ra, .. }), _) => {
#| let { left: lb, found, right: rb, } = split_member(b, va)
#| if found {
#| found_count += 1
#| concat(aux(la, lb), aux(ra, rb))
#| } else {
#| Some(join(aux(la, lb), va, aux(ra, rb)))
#| }
#| }
#| }
#| }
#| let t = aux(copy_tree(self.root), copy_tree(other.root))
#| { root: t, size: self.size + other.size - 2 * found_count, }
#|}
#|#alias(intersect, deprecated)
#|pub fn[V : Compare] SortedSet::intersection(
#| self : SortedSet[V],
#| src : SortedSet[V],
#|) -> SortedSet[V] {
#| match (self.root, src.root) {
#| (None, _) | (_, None) => new_sorted_set()
#| (Some(_), Some(_)) => {
#| if self.size < src.size / 16 {
#| let ret = new_sorted_set()
#| self.each(x => if src.contains(x) { ret.insert(x) })
#| return ret
#| }
#| if src.size < self.size / 16 {
#| let ret = new_sorted_set()
#| src.each(x => if lookup(self.root, x) is Some(v) { ret.insert(v) })
#| return ret
#| }
#| let mut found_count = 0
#| fn aux(a : Node[V]?, b : Node[V]?) -> Node[V]? {
#| match (a, b) {
#| (None, _) | (_, None) => None
#| (Some({ value: va, left: la, right: ra, .. }), _) => {
#| let { left: lb, found, right: rb, } = split_member(b, va)
#| if found {
#| found_count += 1
#| Some(join(aux(la, lb), va, aux(ra, rb)))
#| } else {
#| concat(aux(la, lb), aux(ra, rb))
#| }
#| }
#| }
#| }
#| let t = aux(copy_tree(self.root), copy_tree(src.root))
#| { root: t, size: found_count, }
#| }
#| }
#|}
#|#alias(subset, deprecated="Use `is_subset` instead")
#|pub fn[V : Compare] SortedSet::is_subset(
#| self : SortedSet[V],
#| src : SortedSet[V],
#|) -> Bool {
#| self.iter().all(x => src.contains(x))
#|}
#|#alias(disjoint, deprecated="Use `is_disjoint` instead")
#|pub fn[V : Compare] SortedSet::is_disjoint(
#| self : SortedSet[V],
#| src : SortedSet[V],
#|) -> Bool {
#| self.iter().all(x => !src.contains(x))
#|}
#|pub fn[V : Compare] SortedSet::is_superset(
#| self : SortedSet[V],
#| src : SortedSet[V],
#|) -> Bool {
#| src.is_subset(self)
#|}
#|pub impl[V : Eq] Eq for SortedSet[V] with fn equal(self, other) {
#| guard self.size == other.size else { return false }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| guard a == b else { break false }
#| } nobreak {
#| true
#| }
#|}
#|pub impl[V : Compare] Compare for SortedSet[V] with fn compare(self, other) -> Int32 {
#| guard self.size == other.size else { return self.size - other.size }
#| let iter = self.iter()
#| let iter1 = other.iter()
#| while iter.next() is Some(a) && iter1.next() is Some(b) {
#| let cmp = a.compare(b)
#| guard cmp is 0 else { break cmp }
#| } nobreak {
#| 0
#| }
#|}
#|pub fn[V] SortedSet::is_empty(self : SortedSet[V]) -> Bool {
#| self.root is None
#|}
#|#alias(size, deprecated)
#|pub fn[V] SortedSet::length(self : SortedSet[V]) -> Int32 {
#| self.size
#|}
#|pub fn[V] SortedSet::each(
#| self : SortedSet[V],
#| f : (V) -> Unit raise?,
#|) -> Unit raise? {
#| fn dfs(root : Node[V]?) -> Unit raise? {
#| if root is Some(root) {
#| dfs(root.left)
#| f(root.value)
#| dfs(root.right)
#| }
#| }
#| dfs(self.root)
#|}
#|pub fn[V] SortedSet::eachi(
#| self : SortedSet[V],
#| f : (Int32, V) -> Unit raise?,
#|) -> Unit raise? {
#| let mut i = 0
#| self.each(v => {
#| f(i, v)
#| i += 1
#| })
#|}
#|pub fn[V] SortedSet::to_array(self : SortedSet[V]) -> Array[V] {
#| let arr = Array(capacity=self.size)
#| fn dfs(root : Node[V]?) -> Unit {
#| if root is Some(root) {
#| dfs(root.left)
#| let v = root.value
#| arr.push(v)
#| dfs(root.right)
#| }
#| }
#| dfs(self.root)
#| arr
#|}
#|#alias(iterator, deprecated)
#|pub fn[V] SortedSet::iter(self : SortedSet[V]) -> Iter[V] {
#| let mut curr_node = self.root
#| let parents = []
#| Iter(
#| fn() {
#| for x = curr_node {
#| match x {
#| Some({ left: None, value, right, height: _, }) => {
#| curr_node = right
#| break Some(value)
#| }
#| Some({ left, value, right, height: _, }) => {
#| parents.push((value, right))
#| continue left
#| }
#| None if parents.pop() is Some((value, right)) => {
#| curr_node = right
#| break Some(value)
#| }
#| None => break None
#| }
#| }
#| },
#| size_hint=self.size,
#| )
#|}
#|#as_free_fn
#|#alias(from_iterator, deprecated)
#|#as_free_fn(from_iterator, deprecated)
#|pub fn[V : Compare] SortedSet::from_iter(iter : Iter[V]) -> SortedSet[V] {
#| let s = new_sorted_set()
#| while iter.next() is Some(e) {
#| s.insert(e)
#| }
#| s
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|pub impl[V : Show] Show for SortedSet[V]
#|pub impl[V : Show] Show for SortedSet[V] with fn output(self, logger) {
#| logger.write_iter(self.iter(), prefix="@sorted_set.from_array([", suffix="])")
#|}
#|#deprecated("Use @debug.Debug instead of Show for debugging purposes. See https://github.com/moonbitlang/core/blob/main/debug/README.mbt.md")
#|impl[T : Show] Show for Node[T]
#|impl[T : Show] Show for Node[T] with fn output(self, logger) {
#| fn count(root : Node[T]?) -> Int32 {
#| match root {
#| None => 0
#| Some(root) => count(root.left) + count(root.right) + 1
#| }
#| }
#| let x = { root: Some(self), size: count(Some(self)), }
#| logger.write_iter(x.iter())
#|}
#|#label_migration(low, allow_positional=true)
#|#label_migration(high, allow_positional=true)
#|pub fn[V : Compare] SortedSet::range(
#| self : SortedSet[V],
#| low~ : V,
#| high~ : V,
#|) -> Iter[V] {
#| let mut curr_node = self.root
#| let parents = []
#| let iter = Iter(() => {
#| for x = curr_node {
#| match x {
#| Some({ value, left, right, height: _, }) => {
#| let cmp_key_low = value.compare(low)
#| let cmp_key_high = value.compare(high)
#| if cmp_key_low < 0 {
#| continue right
#| } else if cmp_key_high > 0 {
#| continue left
#| } else if left is None {
#| curr_node = right
#| break Some(value)
#| } else {
#| parents.push((value, right))
#| continue left
#| }
#| }
#| None if parents.pop() is Some((value, right)) => {
#| curr_node = right
#| break Some(value)
#| }
#| None => break None
#| }
#| }
#| })
#| iter.iter()
#|}
#|fn[V] replace_root_with_min(root : Node[V], node : Node[V]) -> Node[V]? {
#| let (l, r) = (node.left, node.right)
#| match l {
#| None => {
#| root.value = node.value
#| r
#| }
#| Some(ln) => {
#| node.left = replace_root_with_min(root, ln)
#| Some(balance(node))
#| }
#| }
#|}
#|fn[V] Node::update_height(self : Node[V]) -> Unit {
#| self.height = 1 + height(self.left).max(height(self.right))
#|}
#|fn[V] height_ge(x1 : Node[V]?, x2 : Node[V]?) -> Bool {
#| match (x1, x2) {
#| (
#| Some({ height: h1, .. })
#| | (None with h1 = 0),
#| Some({ height: h2, .. })
#| | (None with h2 = 0),
#| ) => h1 >= h2
#| }
#|}
#|#owned(root)
#|fn[V] balance(root : Node[V]) -> Node[V] {
#| let (l, r) = (root.left, root.right)
#| let (hl, hr) = (height(l), height(r))
#| let new_root = if hl > hr + 1 {
#| let { left: ll, right: lr, .. } = l.unwrap()
#| if height_ge(ll, lr) {
#| rotate_r(root)
#| } else {
#| rotate_lr(root)
#| }
#| } else if hr > hl + 1 {
#| let { left: rl, right: rr, .. } = r.unwrap()
#| if height_ge(rr, rl) {
#| rotate_l(root)
#| } else {
#| rotate_rl(root)
#| }
#| } else {
#| root
#| }
#| new_root.update_height()
#| new_root
#|}
#|#owned(n)
#|fn[V] rotate_l(n : Node[V]) -> Node[V] {
#| let r = n.right.unwrap()
#| n.right = r.left
#| n.update_height()
#| r.left = Some(n)
#| r.update_height()
#| r
#|}
#|#owned(n)
#|fn[V] rotate_r(n : Node[V]) -> Node[V] {
#| let l = n.left.unwrap()
#| n.left = l.right
#| n.update_height()
#| l.right = Some(n)
#| l.update_height()
#| l
#|}
#|#owned(n)
#|fn[V] rotate_lr(n : Node[V]) -> Node[V] {
#| let l = n.left.unwrap()
#| let v = rotate_l(l)
#| n.left = Some(v)
#| rotate_r(n)
#|}
#|#owned(n)
#|fn[V] rotate_rl(n : Node[V]) -> Node[V] {
#| let r = n.right.unwrap()
#| let v = rotate_r(r)
#| n.right = Some(v)
#| rotate_l(n)
#|}
#|#owned(value)
#|fn[V : Compare] add_node(root : Node[V]?, value : V) -> (Node[V]?, Bool) {
#| match root {
#| None => (Some(new_node(value)), true)
#| Some(n) => {
#| let comp = value.compare(n.value)
#| if comp == 0 {
#| (Some(n), false)
#| } else {
#| let (l, r) = (n.left, n.right)
#| if comp < 0 {
#| let (nl, inserted) = add_node(l, value)
#| n.left = nl
#| (Some(balance(n)), inserted)
#| } else {
#| let (nr, inserted) = add_node(r, value)
#| n.right = nr
#| (Some(balance(n)), inserted)
#| }
#| }
#| }
#| }
#|}
#|fn[V : Compare] delete_node(root : Node[V], value : V) -> (Node[V]?, Bool) {
#| let comp = value.compare(root.value)
#| if comp == 0 {
#| let (l, r) = (root.left, root.right)
#| let n = match (l, r) {
#| (Some(_), Some(nr)) => {
#| root.right = replace_root_with_min(root, nr)
#| Some(balance(root))
#| }
#| (None, Some(_)) => r
#| (Some(_), None) | (None, None) => l
#| }
#| (n, true)
#| } else if comp < 0 {
#| match root.left {
#| None => (Some(root), false)
#| Some(l) => {
#| let (nl, deleted) = delete_node(l, value)
#| root.left = nl
#| (Some(balance(root)), deleted)
#| }
#| }
#| } else {
#| match root.right {
#| None => (Some(root), false)
#| Some(r) => {
#| let (nr, deleted) = delete_node(r, value)
#| root.right = nr
#| (Some(balance(root)), deleted)
#| }
#| }
#| }
#|}
#|test "copy" {
#| let set = from_array([1, 2, 3, 4, 5])
#| let copied_set = set.copy()
#| @debug.debug_inspect(
#| copied_set,
#| content=(
#| #|
#| ),
#| )
#| inspect(set.debug_tree() == copied_set.debug_tree(), content="true")
#| let set : SortedSet[Int32] = from_array([])
#| let copied_set = set.copy()
#| @debug.debug_inspect(
#| copied_set,
#| content=(
#| #|
#| ),
#| )
#| inspect(set.debug_tree() == copied_set.debug_tree(), content="true")
#|}
#|test "union" {
#| let set1 = from_array([1, 2, 3])
#| let set2 = from_array([4, 5, 6])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set3.debug_tree(),
#| content="([3]3,([2]2,([1]1,_,_),_),([2]5,([1]4,_,_),([1]6,_,_)))",
#| )
#| let set1 = from_array([1, 2, 3])
#| let set2 = from_array([2, 3, 4])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(set3.debug_tree(), content="([3]2,([1]1,_,_),([2]3,_,([1]4,_,_)))")
#| let set1 = from_array([1, 2, 3])
#| let set2 = from_array([2, 3])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(set3.debug_tree(), content="([2]2,([1]1,_,_),([1]3,_,_))")
#| let set1 : SortedSet[Int32] = new_sorted_set()
#| let set2 = new_sorted_set()
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(set3.debug_tree(), content="_")
#| let set1 = from_array([1, 2, 3])
#| let set2 = from_array([])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(set3.debug_tree(), content="([2]2,([1]1,_,_),([1]3,_,_))")
#| let set1 = from_array([])
#| let set2 = from_array([1, 2, 3])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(set3.debug_tree(), content="([2]2,([1]1,_,_),([1]3,_,_))")
#| let set1 = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#| let set2 = from_array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set3.debug_tree(),
#| content="([5]14,([4]8,([3]4,([2]2,([1]1,_,_),([1]3,_,_)),([2]6,([1]5,_,_),([1]7,_,_))),([3]12,([2]10,([1]9,_,_),([1]11,_,_)),([1]13,_,_))),([3]18,([2]16,([1]15,_,_),([1]17,_,_)),([2]19,_,([1]20,_,_))))",
#| )
#| let set1 = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#| let set2 = from_array([6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set3.debug_tree(),
#| content="([5]11,([4]4,([2]2,([1]1,_,_),([1]3,_,_)),([3]8,([2]6,([1]5,_,_),([1]7,_,_)),([2]9,_,([1]10,_,_)))),([3]13,([1]12,_,_),([2]14,_,([1]15,_,_))))",
#| )
#| let set1 = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#| let set2 = from_array([6, 7, 8, 9, 10])
#| let set3 = set1.union(set2)
#| @debug.debug_inspect(
#| set3,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set3.debug_tree(),
#| content="([4]4,([2]2,([1]1,_,_),([1]3,_,_)),([3]8,([2]6,([1]5,_,_),([1]7,_,_)),([2]9,_,([1]10,_,_))))",
#| )
#|}
#|#warnings("-deprecated")
#|test "split_member" {
#| let { left: l, found, right: r, } = split_member(
#| from_array([7, 2, 9, 4, 5, 6, 3, 8, 1]).root,
#| 5,
#| )
#| inspect(found, content="true")
#| inspect(l, content="Some([1, 2, 3, 4])")
#| inspect(r, content="Some([6, 7, 8, 9])")
#| let { left: l, found, right: r, } = split_member(
#| from_array([7, 2, 9, 4, 5, 6, 3, 8, 1]).root,
#| 0,
#| )
#| inspect(found, content="false")
#| inspect(l, content="None")
#| inspect(r, content="Some([1, 2, 3, 4, 5, 6, 7, 8, 9])")
#| let { left: l, found, right: r, } = split_member(
#| from_array([7, 2, 9, 4, 5, 6, 3, 8, 1]).root,
#| 10,
#| )
#| inspect(found, content="false")
#| inspect(l, content="Some([1, 2, 3, 4, 5, 6, 7, 8, 9])")
#| inspect(r, content="None")
#| let { left: l, found, right: r, } = split_member(
#| from_array([7, 2, 9, 4, 5, 6, 3, 8, 1]).root,
#| 4,
#| )
#| inspect(found, content="true")
#| inspect(l, content="Some([1, 2, 3])")
#| inspect(r, content="Some([5, 6, 7, 8, 9])")
#| let { left: l, found, right: r, } = split_member(from_array([]).root, 7)
#| inspect(found, content="false")
#| inspect(l, content="None")
#| inspect(r, content="None")
#|}
#|test "join" {
#| fn join_to_array(
#| l : SortedSet[Int32],
#| value : Int32,
#| r : SortedSet[Int32],
#| ) -> Array[Int32] {
#| let root = join(l.root, value, r.root)
#| ({ root: Some(root), size: l.size + r.size + 1, } : SortedSet[Int32]).to_array()
#| }
#| let l = from_array([13, 8, 17, 1, 11, 15, 25, 6])
#| let r = from_array([27, 28, 40, 35, 33])
#| @debug.debug_inspect(
#| join_to_array(l, 26, r),
#| content=(
#| #|[1, 6, 8, 11, 13, 15, 17, 25, 26, 27, 28, 33, 35, 40]
#| ),
#| )
#| let l = from_array([3, 2, 5, 1, 4])
#| let r = from_array([7])
#| @debug.debug_inspect(
#| join_to_array(l, 6, r),
#| content=(
#| #|[1, 2, 3, 4, 5, 6, 7]
#| ),
#| )
#| let l = from_array([3, 2, 5, 1, 4])
#| let r = from_array([])
#| @debug.debug_inspect(
#| join_to_array(l, 6, r),
#| content=(
#| #|[1, 2, 3, 4, 5, 6]
#| ),
#| )
#| let l = from_array([])
#| let r = from_array([])
#| @debug.debug_inspect(
#| join_to_array(l, 6, r),
#| content=(
#| #|[6]
#| ),
#| )
#| let l = from_array([])
#| let r = from_array([7, 8, 9, 10, 11, 12])
#| @debug.debug_inspect(
#| join_to_array(l, 6, r),
#| content=(
#| #|[6, 7, 8, 9, 10, 11, 12]
#| ),
#| )
#|}
#|test "insert to empty set" {
#| let set = new_sorted_set()
#| set.insert(1)
#| @test.assert_eq(set.contains(1), true)
#| inspect(set.debug_tree(), content="([1]1,_,_)")
#|}
#|test "insert to non-empty set" {
#| let set = new_sorted_set()
#| set.insert(1)
#| set.insert(2)
#| @test.assert_eq(set.contains(1), true)
#| @test.assert_eq(set.contains(2), true)
#| inspect(set.debug_tree(), content="([2]1,_,([1]2,_,_))")
#|}
#|test "insert duplicate value" {
#| let set = new_sorted_set()
#| set.insert(1)
#| set.insert(1)
#| @test.assert_eq(set.contains(1), true)
#| @test.assert_eq(set.length(), 1)
#| inspect(set.debug_tree(), content="([1]1,_,_)")
#|}
#|test "insert multiple values" {
#| let set = new_sorted_set()
#| set.insert(1)
#| set.insert(2)
#| set.insert(3)
#| @test.assert_eq(set.contains(1), true)
#| @test.assert_eq(set.contains(2), true)
#| @test.assert_eq(set.contains(3), true)
#| @test.assert_eq(set.length(), 3)
#| inspect(set.debug_tree(), content="([2]2,([1]1,_,_),([1]3,_,_))")
#|}
#|test "add_and_remove" {
#| let set = from_array([7, 2, 9, 4, 5, 6, 3, 8, 1])
#| set.remove(8)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]5,([3]3,([2]2,([1]1,_,_),_),([1]4,_,_)),([2]7,([1]6,_,_),([1]9,_,_)))",
#| )
#| let set = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#| set.remove(1)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]4,([2]2,_,([1]3,_,_)),([3]8,([2]6,([1]5,_,_),([1]7,_,_)),([2]9,_,([1]10,_,_))))",
#| )
#| set.remove(5)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]4,([2]2,_,([1]3,_,_)),([3]8,([2]6,_,([1]7,_,_)),([2]9,_,([1]10,_,_))))",
#| )
#| set.remove(10)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]4,([2]2,_,([1]3,_,_)),([3]8,([2]6,_,([1]7,_,_)),([1]9,_,_)))",
#| )
#| set.remove(4)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([3]6,([2]2,_,([1]3,_,_)),([2]8,([1]7,_,_),([1]9,_,_)))",
#| )
#| set.insert(1)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([3]6,([2]2,([1]1,_,_),([1]3,_,_)),([2]8,([1]7,_,_),([1]9,_,_)))",
#| )
#| set.insert(5)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([3]2,([1]1,_,_),([2]3,_,([1]5,_,_))),([2]8,([1]7,_,_),([1]9,_,_)))",
#| )
#| set.insert(10)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([3]2,([1]1,_,_),([2]3,_,([1]5,_,_))),([3]8,([1]7,_,_),([2]9,_,([1]10,_,_))))",
#| )
#| set.insert(4)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([3]2,([1]1,_,_),([2]4,([1]3,_,_),([1]5,_,_))),([3]8,([1]7,_,_),([2]9,_,([1]10,_,_))))",
#| )
#| set.insert(11)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([3]2,([1]1,_,_),([2]4,([1]3,_,_),([1]5,_,_))),([3]8,([1]7,_,_),([2]10,([1]9,_,_),([1]11,_,_))))",
#| )
#| set.remove(11)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([3]2,([1]1,_,_),([2]4,([1]3,_,_),([1]5,_,_))),([3]8,([1]7,_,_),([2]10,([1]9,_,_),_)))",
#| )
#| set.remove(12)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| set.insert(10)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| set.remove(1)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([3]4,([2]2,_,([1]3,_,_)),([1]5,_,_)),([3]8,([1]7,_,_),([2]10,([1]9,_,_),_)))",
#| )
#| set.remove(2)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([2]4,([1]3,_,_),([1]5,_,_)),([3]8,([1]7,_,_),([2]10,([1]9,_,_),_)))",
#| )
#| set.remove(3)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([4]6,([2]4,_,([1]5,_,_)),([3]8,([1]7,_,_),([2]10,([1]9,_,_),_)))",
#| )
#| set.remove(4)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #|
#| ),
#| )
#| inspect(
#| set.debug_tree(),
#| content="([3]8,([2]6,([1]5,_,_),([1]7,_,_)),([2]10,([1]9,_,_),_))",
#| )
#| set.remove(5)
#| @debug.debug_inspect(
#| set,
#| content=(
#| #| |