### MD5 Hashing in MoonBit (Direct and Buffered) Source: https://github.com/moonbitlang/x/blob/main/crypto/README.mbt.md Demonstrates both direct and buffered MD5 hashing. For buffered hashing, initialize a context, update it with byte arrays, and finalize to get the hash. ```moonbit ///| test { let input = "The quick brown fox jumps over the lazy dog" inspect( bytes_to_hex_string(md5(@encoding.encode(UTF16, input))), content="b0986ae6ee1eefee8a4a399090126837", ) // buffered let ctx = MD5::new() ctx.update(b"a") ctx.update(b"b") ctx.update(b"c") inspect( bytes_to_hex_string(ctx.finalize()), content="900150983cd24fb0d6963f7d28e17f72", ) } ``` -------------------------------- ### SM3 Hashing in MoonBit (Direct and Buffered) Source: https://github.com/moonbitlang/x/blob/main/crypto/README.mbt.md Provides examples for direct and buffered SM3 hashing. Buffered hashing requires converting byte literals to fixed arrays before updating the context. ```moonbit ///| test { let input = "The quick brown fox jumps over the lazy dog" inspect( bytes_to_hex_string(sm3(@encoding.encode(UTF16, input))), content="fc2b31896629e88652ca1e3be449ec7ec93f7e5e29769f273fb973bc1858c66d", ) //buffered let ctx = SM3::new() ctx.update(b"a".to_fixedarray()) ctx.update(b"b".to_fixedarray()) ctx.update(b"c".to_fixedarray()) inspect( bytes_to_hex_string(ctx.finalize()), content="66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", ) } ``` -------------------------------- ### Import moonbitlang/x Package in moon.pkg.json Source: https://github.com/moonbitlang/x/blob/main/README.md Specify the moonbitlang/x packages you wish to import in your moon.pkg.json file. For example, to import the json5 package. ```json { "import": [ "moonbitlang/x/json5" ] } ``` -------------------------------- ### Construct a Version 4 UUID Source: https://github.com/moonbitlang/x/blob/main/uuid/README.mbt.md Constructs a version 4 UUID from a hexadecimal string and asserts its string representation. This example demonstrates the basic usage of UUID creation and conversion. ```moonbit ///| test { let u = @uuid.from_hex("ddf99703-742f-7505-4c54-df36a9c243fe").as_version(V4) inspect(u.to_string(), content="ddf99703-742f-4505-8c54-df36a9c243fe") } ``` -------------------------------- ### Stack Length and Emptiness Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Get the number of elements in the stack using `length` or check if it's empty with `is_empty`. ```moonbit let st = Stack::of([1, 2, 3]) inspect(st.length(), content="3") // 3 inspect(st.is_empty(), content="false") // false ``` -------------------------------- ### Get Basename and Dirname from Path Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Extract the last component (filename) or the directory part of a path. Handles trailing slashes by returning an empty string for basename. ```moonbit ///| test "basename and dirname examples" { // Get the last component (filename) let path : Path = "usr/local/bin" inspect(path.basename(), content="bin") let path : Path = "project/src/main.mbt" inspect(path.basename(), content="main.mbt") // Get the directory part let path : Path = "usr/local/bin" inspect(path.dirname(), content="usr/local") let path : Path = "project/src/main.mbt" inspect(path.dirname(), content="project/src") // Handle trailing slashes let path : Path = "usr/local/" inspect(path.basename(), content="") inspect(path.dirname(), content="usr/local") } ``` -------------------------------- ### Detect Absolute Paths Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Determines if a given path is an absolute path. Absolute paths start with a forward slash '/'. Relative paths and empty strings return false. ```moonbit ///| test "absolute path detection" { let path : Path = "/home/user" json_inspect(path.is_absolute(), content=true) let path : Path = "/usr/local/bin" json_inspect(path.is_absolute(), content=true) // Relative paths let path : Path = "home/user" json_inspect(path.is_absolute(), content=false) let path : Path = "../project" json_inspect(path.is_absolute(), content=false) let path : Path = "" json_inspect(path.is_absolute(), content=false) } ``` -------------------------------- ### Absolute Path Detection Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Detects various types of absolute paths on Windows, including standard drive letters, UNC paths, verbatim paths, volume GUIDs, device namespaces, and symlink paths. Relative paths are correctly identified as not absolute. ```moonbit ///| test "absolute path detection" { // Standard drive letter paths let path : Path = "C:\\" json_inspect(path.is_absolute(), content=true) let path : Path = "D:\\folder\\file" json_inspect(path.is_absolute(), content=true) // UNC paths (network shares) let path : Path = "\\\\server\\share\\file" json_inspect(path.is_absolute(), content=true) // Verbatim UNC paths let path : Path = "\\\\?\\UNC\\server\\share\\file" json_inspect(path.is_absolute(), content=true) // Verbatim drive letter paths let path : Path = "\\\\?\\C:\\file" json_inspect(path.is_absolute(), content=true) // Volume GUID paths let path : Path = "\\\\?\\Volume{12345678-1234-1234-1234-1234567890ab}\\file" json_inspect(path.is_absolute(), content=true) // Device namespace paths let path : Path = "\\\\.\\COM56" json_inspect(path.is_absolute(), content=true) // Verbatim symlink paths let path : Path = "\\\\?\\GLOBALROOT\\file" json_inspect(path.is_absolute(), content=true) // Relative paths let path : Path = "C:folder\\file" // Drive-relative json_inspect(path.is_absolute(), content=false) let path : Path = "Users\\user" json_inspect(path.is_absolute(), content=false) let path : Path = "" json_inspect(path.is_absolute(), content=false) } ``` -------------------------------- ### Create and Inspect ZonedDateTime Source: https://github.com/moonbitlang/x/blob/main/time/README.mbt.md Demonstrates creating a fixed time zone and a ZonedDateTime from Unix seconds and the time zone. Assumes UTC+8 fixed time zone. ```moonbit ///| test { // creates a UTC+8 fixed time zone. let zone = @time.fixed_zone("Asia/Shanghai", 8 * 60 * 60) // creates a ZonedDateTime from unix second and time zone. let date_time = @time.unix(1714227729L, nanosecond=1000, zone~) inspect(date_time, content="2024-04-27T22:22:09.000001+08:00[Asia/Shanghai]") } ``` -------------------------------- ### Create Stack Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Create a new empty stack or initialize one from an array or a list of elements. ```moonbit let st : @stack.Stack[Unit] = Stack::new() inspect(st, content="Stack::[]") let st2 = Stack::from_array([1, 2, 3]) inspect(st2, content="Stack::[1, 2, 3]") let st3 = Stack::of([1, 2, 3]) inspect(st3, content="Stack::[1, 2, 3]") ``` -------------------------------- ### Check Posix Platform Constants Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Verify the path component separator and the path list delimiter using inspect. This snippet demonstrates the usage of @posix.sep and @posix.delimiter. ```moonbit ///| test "platform constants" { // Path component separator inspect(@posix.sep, content="/") // Path list delimiter (for PATH environment variable) inspect(@posix.delimiter, content=":") } ``` -------------------------------- ### Rational Arithmetic Operations Source: https://github.com/moonbitlang/x/blob/main/rational/README.mbt.md Demonstrates addition, subtraction, multiplication, division, negation, reciprocal, and absolute value for Rational numbers. ```moonbit ///| test { let a = @rational.new(1L, 2L).unwrap() let b = @rational.new(1L, 3L).unwrap() assert_eq(a + b, @rational.new(5L, 6L).unwrap()) assert_eq(a - b, @rational.new(1L, 6L).unwrap()) assert_eq(a * b, @rational.new(1L, 6L).unwrap()) assert_eq(a / b, @rational.new(3L, 2L).unwrap()) assert_eq(-a, @rational.new(-1L, 2L).unwrap()) assert_eq(a.reciprocal(), @rational.new(2L, 1L).unwrap()) assert_eq(a.abs(), @rational.new(1L, 2L).unwrap()) } ``` -------------------------------- ### Rational Comparison Operations Source: https://github.com/moonbitlang/x/blob/main/rational/README.mbt.md Illustrates equality, inequality, less than, less than or equal to, greater than, greater than or equal to, and comparison for Rational numbers. ```moonbit ///| test { let a = @rational.new(1L, 2L).unwrap() let b = @rational.new(1L, 3L).unwrap() assert_eq(a == b, false) assert_eq(a != b, true) assert_eq(a < b, false) assert_eq(a <= b, false) assert_eq(a > b, true) assert_eq(a >= b, true) assert_eq(a.compare(b), 1) } ``` -------------------------------- ### Calculate Relative Paths in Windows Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.mbt.md Use the `relative` method to find the path difference between two Windows locations. Handles same directory, going up a level, and sibling directories. ```moonbit test "relative path calculation" { // Same directory level let base = "C:\\Users\\user_name" let path : Path = "C:\\Users\\user_name\\proj_a" // inspect(path) inspect(path.relative(base~), content="proj_a") // Go up one level let base = "C:\\Users\\user_name\\proj_a" let path : Path = "C:\\Users\\user_name" inspect(path.relative(base~), content="..") // Same path let base = "C:\\Users\\user_name" let path : Path = "C:\\Users\\user_name" inspect(path.relative(base~), content="") // Sibling directories let base = "C:\\Users\\user_name\\proj_a" let path : Path = "C:\\Users\\user_name\\proj_b" inspect(path.relative(base~), content="..\\proj_b") } ``` -------------------------------- ### Path Joining Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Combines path components using the Windows path separator (`\`). Handles trailing separators on the base path and correctly resolves absolute paths provided as components. ```moonbit ///| test "path joining" { // Basic joining let path : Path = "Users" inspect(path.join("user"), content="Users\\user") let path : Path = "project" inspect(path.join("src"), content="project\\src") // Handle trailing backslashes let path : Path = "Users\\" inspect(path.join("user"), content="Users\\user") // Absolute paths override let path : Path = "relative" inspect(path.join("\\absolute"), content="\\absolute") let path : Path = "C:\\" inspect( path.join("folder").join("file.txt").to_string(), content="C:\\folder\\file.txt", ) } ``` -------------------------------- ### Rational Integer Operations Source: https://github.com/moonbitlang/x/blob/main/rational/README.mbt.md Shows floor, ceiling, fractional part, truncation, and integer check for Rational numbers. ```moonbit ///| test { let a = @rational.new(1L, 2L).unwrap() assert_eq(a.floor(), 0) assert_eq(a.ceil(), 1) assert_eq(a.fract().to_string(), "1/2") assert_eq(a.trunc(), 0) assert_eq(a.is_integer(), false) } ``` -------------------------------- ### Run Current Encoding Benchmarks Source: https://github.com/moonbitlang/x/blob/main/encoding/internal/benchmark/README.md Execute the benchmark script to measure the performance of the current codebase. Ensure all pre-requisites are met before running. ```sh ./encoding/internal/benchmark/bench.sh ``` -------------------------------- ### Rational String Operations Source: https://github.com/moonbitlang/x/blob/main/rational/README.mbt.md Illustrates converting a Rational number to its string representation. ```moonbit ///| test { let a = @rational.new(1L, 2L).unwrap() assert_eq(a.to_string(), "1/2") } ``` -------------------------------- ### Add moonbitlang/x Dependency Source: https://github.com/moonbitlang/x/blob/main/README.md Use this command to add the moonbitlang/x module to your project dependencies. ```bash moon add moonbitlang/x ``` -------------------------------- ### Join Path Components Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Combines multiple path components into a single path string, handling separators correctly. Absolute paths provided as arguments will override the base path. ```moonbit ///| test "path joining" { let path : Path = "usr" inspect(path.join("local"), content="usr/local") let path : Path = "project" inspect(path.join("src"), content="project/src") let path : Path = "usr/" inspect(path.join("local"), content="usr/local") // Absolute paths override let path : Path = "relative" inspect(path.join("/absolute"), content="/absolute") let path : Path = "/" let path = path.join("folder").join("file.txt") inspect(path.to_string(), content="/folder/file.txt") } ``` -------------------------------- ### Resolve and Normalize Windows Paths Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.mbt.md Use the `resolve` method to convert relative paths to absolute paths and normalize them. Note that resolving relative paths depends on the current working directory. ```moonbit test "path resolution" { // Resolve and normalize absolute paths let path : Path = "\\Users\\..\\Windows\\System32" inspect(path.resolve(), content="\\Windows\\System32") let path : Path = "\\a\\b\\c\\..\\..\\.." inspect(path.resolve(), content="\\") // Note: resolve() with relative paths depends on current working directory // and will join with the current directory before normalizing } ``` ```moonbit test { let path : Path = "a\\b\\..\\c" inspect(path.resolve(), content="C:\\current\\working\\directory\\a\\c") } ``` -------------------------------- ### Push and Pop Elements Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Add elements using `push` and remove the top element using `pop`. `pop` returns `Some(element)` or `None` if empty. ```moonbit let st = Stack::new() st.push(1) st.push(2) debug_inspect(st.pop(), content="Some(2)") ``` -------------------------------- ### Rational Double Operations Source: https://github.com/moonbitlang/x/blob/main/rational/README.mbt.md Demonstrates conversion to and from double-precision floating-point numbers for Rational values. ```moonbit ///| test { let a = @rational.new(1L, 2L).unwrap() assert_eq(a.to_double(), 0.5) assert_eq(@rational.from_double(0.5).to_string(), "1/2") } ``` -------------------------------- ### Windows Platform Constants Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.mbt.md Access Windows-specific path constants provided by the package. Includes the path component separator and the delimiter for the PATH environment variable. ```moonbit test "platform constants" { // Path component separator inspect(@win32.sep, content="\\") // Path list delimiter (for PATH environment variable) inspect(@win32.delimiter, content=";") } ``` -------------------------------- ### Unsafe Pop Panic Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Demonstrates that `unsafe_pop` will panic when called on an empty stack. ```moonbit let st = Stack::new() st.unsafe_pop() ``` -------------------------------- ### Convert Stack to Array Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Convert the stack to an array using `to_array` or by creating an array from its iterator. ```moonbit let st = Stack::of([1, 2, 3]) debug_inspect(st.to_array(), content="[1, 2, 3]") debug_inspect(Array::from_iter(st.iter()), content="[1, 2, 3]") ``` -------------------------------- ### Calculate Relative Path (Windows) Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Calculates the relative path between two Windows locations. Handles cases for same directory level, going up one level, same path, and sibling directories. ```moonbit let base = "C:\\Users\\user_name" let path : Path = "C:\\Users\\user_name\\proj_a" inspect(path.relative(base~), content="proj_a") ``` ```moonbit let base = "C:\\Users\\user_name\\proj_a" let path : Path = "C:\\Users\\user_name" inspect(path.relative(base~), content="..") ``` ```moonbit let base = "C:\\Users\\user_name" let path : Path = "C:\\Users\\user_name" inspect(path.relative(base~), content="") ``` ```moonbit let base = "C:\\Users\\user_name\\proj_a" let path : Path = "C:\\Users\\user_name\\proj_b" inspect(path.relative(base~), content="..\\proj_b") ``` -------------------------------- ### Unsafe Pop and Peek Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Use `unsafe_pop` to remove and return the top element, panicking if the stack is empty. `unsafe_peek` returns the top element without removing it. ```moonbit let st = Stack::new() st.push(1) inspect(st.unsafe_pop(), content="1") // 1 ``` ```moonbit let st = Stack::of([1, 2, 3]) debug_inspect(st.peek(), content="Some(1)") inspect(st.unsafe_peek(), content="1") // 1 ``` -------------------------------- ### Platform Constants (Windows) Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Accesses Windows-specific path constants, including the path component separator and the path list delimiter. ```moonbit inspect(@win32.sep, content="\\") ``` ```moonbit inspect(@win32.delimiter, content=";") ``` -------------------------------- ### Resolve and Normalize Absolute Paths (Windows) Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Converts relative paths to absolute paths and normalizes them. Note that resolving relative paths depends on the current working directory. ```moonbit let path : Path = "\\Users\\..\\Windows\\System32" inspect(path.resolve(), content="\\Windows\\System32") ``` ```moonbit let path : Path = "\\a\\b\\c\\..\\..\\.." inspect(path.resolve(), content="\\") ``` ```moonbit let path : Path = "a\\b\\..\\c" inspect(path.resolve(), content="C:\\current\\working\\directory\\a\\c") ``` -------------------------------- ### Basename and Dirname Operations Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Extract the last component (filename) or the directory part of a Windows path. Handles trailing backslashes correctly. ```moonbit ///| test "basename and dirname examples" { // Get the last component (filename) let path : Path = "C:\\Users\\user" inspect(path.basename(), content="user") let path : Path = "project\\src\\main.mbt" inspect(path.basename(), content="main.mbt") // Get the directory part let path : Path = "C:\\Users\\user" inspect(path.dirname(), content="C:\\Users") let path : Path = "project\\src\\main.mbt" inspect(path.dirname(), content="project\\src") // Handle trailing backslashes let path : Path = "C:\\Users\\" inspect(path.basename(), content="") inspect(path.dirname(), content="C:\\Users") } ``` -------------------------------- ### Compare Encoding Benchmarks Between Commits Source: https://github.com/moonbitlang/x/blob/main/encoding/internal/benchmark/README.md Run benchmarks to compare the performance of the current code against a previous commit. This is useful for identifying performance regressions or improvements. ```sh ./encoding/internal/benchmark/bench_diff.sh HEAD~1 ``` -------------------------------- ### Path Normalization Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Cleans up redundant path components, resolving `.` (current directory) and `..` (parent directory) in Windows paths. Handles complex sequences and leading separators. ```moonbit ///| test "path normalization" { // Remove redundant components let path : Path = "a\\.\\b\\..\\c\\" inspect(path.normalize(), content="a\\c") let path : Path = "C:\\Users\\..\\Windows" inspect(path.normalize(), content="C:\\Windows") // Handle complex cases let path : Path = "\\a\\b\\..\\..\\c\\." inspect(path.normalize(), content="\\c") let path : Path = "a\\b\\c\\.." inspect(path.normalize(), content="a\\b") } ``` -------------------------------- ### Traverse Stack Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Iterate over the stack elements using the `iter` method and process each element. ```moonbit let st = Stack::of([1, 2, 3]) let mut sum = 0 st.iter().each(fn(x) { sum += x }) inspect(sum, content="6") ``` -------------------------------- ### Resolve Path Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.mbt.md Converts a path to an absolute path and normalizes it. For relative paths, it resolves them against the current working directory. ```moonbit ///| test "path resolution" { // Resolve and normalize absolute paths let path : Path = "/a/b/../../c/." inspect(path.resolve(), content="/c") let path : Path = "/a/b/c/../../.." inspect(path.resolve(), content="/") // Note: resolve() with relative paths depends on current working directory // and will join with the current directory before normalizing } ``` ```moonbit ///| test { let path : Path = "a/b/../c" inspect(path.resolve(), content="/current/working/directory/a/c") } ``` -------------------------------- ### Resolve Path Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Converts a path to an absolute path and normalizes it. For relative paths, it resolves them against the current working directory. ```moonbit ///| test "path resolution" { // Resolve and normalize absolute paths let path : Path = "/a/b/../../c/." inspect(path.resolve(), content="/c") let path : Path = "/a/b/c/../../.." inspect(path.resolve(), content="/") // Note: resolve() with relative paths depends on current working directory // and will join with the current directory before normalizing } ``` ```moonbit ///| test { let path : Path = "a/b/../c" inspect(path.resolve(), content="/current/working/directory/a/c") } ``` -------------------------------- ### Normalize Path Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Cleans up a path by removing redundant components like '.' and '..', and resolving them. Handles complex sequences of directory changes. ```moonbit ///| test "path normalization" { // Remove redundant components let path : Path = "a/./b/../c/" inspect(path.normalize(), content="a/c") let path : Path = "/usr/local/../bin" inspect(path.normalize(), content="/usr/bin") // Handle complex cases let path : Path = "/a/b/../../c/." inspect(path.normalize(), content="/c") let path : Path = "a/b/c/.." inspect(path.normalize(), content="a/b") } ``` -------------------------------- ### Calculate Relative Path Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Computes the relative path from a base path to a target path. Handles cases including same directory, parent directories, and sibling directories. ```moonbit ///| test "relative path calculation" { // Same directory level let base = "/home/user_name" let path : Path = "/home/user_name/proj_a" inspect(path.relative(base~), content="proj_a") // Go up one level let base = "/home/user_name/proj_a" let path : Path = "/home/user_name" inspect(@posix.Path::relative(base~, path), content="..") // Same path let base = "/home/user_name" let path : Path = "/home/user_name" inspect(path.relative(base~), content="") // Sibling directories let base = "/home/user_name/proj_a" let path : Path = "/home/user_name/proj_b" inspect(path.relative(base~), content="../proj_b") } ``` -------------------------------- ### Extract File Extension from Path Source: https://github.com/moonbitlang/x/blob/main/path/posix/README.md Extracts the file extension from a path, including the leading dot. Returns an empty string for paths without extensions or directory paths. ```moonbit ///| test "extension extraction" { // Get file extension including the dot let path : Path = "document.txt" inspect(path.extname(), content=".txt") let path : Path = "archive.tar.gz" inspect(path.extname(), content=".gz") let path : Path = "project/main.mbt.md" inspect(path.extname(), content=".md") // Files without extensions let path : Path = "README" inspect(path.extname(), content="") let path : Path = "project/" inspect(path.extname(), content="") } ``` -------------------------------- ### Extension Name Extraction Source: https://github.com/moonbitlang/x/blob/main/path/win32/README.md Extracts the file extension from a Windows path, including the leading dot. Returns an empty string for files without extensions. ```moonbit ///| test "extension extraction" { // Get file extension including the dot let path : Path = "document.txt" inspect(path.extname(), content=".txt") let path : Path = "archive.tar.gz" inspect(path.extname(), content=".gz") let path : Path = "project\\main.mbt.md" inspect(path.extname(), content=".md") // Files without extensions let path : Path = "README" inspect(path.extname(), content="") let path : Path = "project\\" inspect(path.extname(), content="") } ``` -------------------------------- ### Decode UTF-8 Byte Stream with @encoding Source: https://github.com/moonbitlang/x/blob/main/encoding/README.mbt.md Use the streaming decoder to consume byte chunks and decode them into a UTF-8 string. Ensure to call finish() to complete the decoding process. ```moonbit ///| test { // Initialize a streaming UTF-8 decoder let decoder = @encoding.decoder(UTF8) // Consume byte chunks let inputs = [b"abc", b"\xf0", b"\x9f\x90\xb0"] // UTF8(🐰) == inspect(decoder.consume(inputs[0]), content="abc") inspect(decoder.consume(inputs[1]), content="") inspect(decoder.consume(inputs[2]), content="🐰") // Finish decoding assert_true(decoder.finish().is_empty()) } ``` -------------------------------- ### Clear Stack Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Remove all elements from the stack using the `clear` method. ```moonbit let st = @stack.Stack::from_array([1, 2, 3]) st.clear() inspect(st, content="Stack::[]") ``` -------------------------------- ### Drop Top Element Source: https://github.com/moonbitlang/x/blob/main/stack/README.mbt.md Use the `drop` method to remove the top element without returning it. ```moonbit let st = Stack::of([1, 2, 3]) st.drop() inspect(st, content="Stack::[2, 3]") ``` -------------------------------- ### SHA-1 Hashing in MoonBit Source: https://github.com/moonbitlang/x/blob/main/crypto/README.mbt.md Calculates the SHA-1 hash of a UTF-16 encoded string. Ensure the input string is correctly encoded before hashing. ```moonbit ///| test { let input = "The quick brown fox jumps over the lazy dog" inspect( bytes_to_hex_string(sha1(@encoding.encode(UTF16, input))), content="bd136cb58899c93173c33a90dde95ead0d0cf6df", ) } ``` -------------------------------- ### Encode String to UTF-8 Bytes with @encoding Source: https://github.com/moonbitlang/x/blob/main/encoding/README.mbt.md Encode a given string into a sequence of UTF-8 encoded bytes using the @encoding.encode function. The output is a byte array representing the string. ```moonbit ///| test { // Encode a string to UTF-8 let src = "你好👀" let bytes = @encoding.encode(UTF8, src) inspect( bytes, content=( #|b"\xe4\xbd\xa0\xe5\xa5\xbd\xf0\x9f\x91\x80" ), ) } ``` -------------------------------- ### Encode Single Character to UTF-8 Bytes Source: https://github.com/moonbitlang/x/blob/main/encoding/README.mbt.md Convert a single character into its UTF-8 byte representation using the @encoding.to_utf8_bytes function. ```moonbit ///| test { inspect( @encoding.to_utf8_bytes('A'), content=( #|b"A" ), ) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.