### Install Bigstringaf via OPAM Source: https://github.com/inhabitedtype/bigstringaf/blob/master/README.md Standard installation command for the library using the OPAM package manager. ```bash opam install bigstringaf ``` -------------------------------- ### Pin and Install Development Dependencies Source: https://github.com/inhabitedtype/bigstringaf/blob/master/README.md Commands to pin the local repository and install only the necessary dependencies for development. ```bash opam pin add -n bigstringaf . opam install --deps-only bigstringaf ``` -------------------------------- ### Install Test Dependencies and Run Tests Source: https://github.com/inhabitedtype/bigstringaf/blob/master/README.md Commands to install the alcotest package and execute the project test suite. ```bash opam install alcotest make test ``` -------------------------------- ### Create a Bigstring Buffer Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Allocates a new bigstring of the specified length with uninitialized contents. Use `length` to get the size. ```ocaml open Bigstringaf (* Create a 1024-byte buffer *) let buffer = create 1024 let () = Printf.printf "Buffer length: %d\n" (length buffer) (* Output: Buffer length: 1024 *) ``` -------------------------------- ### Get an Empty Bigstring Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Returns an empty bigstring with length 0. This is useful as a placeholder that only needs to be allocated once. ```ocaml open Bigstringaf let placeholder = empty let () = Printf.printf "Empty length: %d\n" (length placeholder) (* Output: Empty length: 0 *) ``` -------------------------------- ### Get and Set Characters in Bigstring Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Reads or writes a single character at the specified offset with bounds checking. `get` retrieves a character, and `set` modifies it. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:4 "test" (* Read character *) let ch = get buffer 0 let () = Printf.printf "First char: %c\n" ch (* Output: First char: t *) (* Write character *) let () = set buffer 0 'b' let () = Printf.printf "Modified: %s\n" (to_string buffer) (* Output: Modified: best *) ``` -------------------------------- ### Get Bigstring Length Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Returns the length of the bigstring in bytes. This is a simple utility function. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:10 "0123456789" let () = Printf.printf "Length: %d\n" (length buffer) (* Output: Length: 10 *) ``` -------------------------------- ### Memory Comparison Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Compare bigstrings byte-by-byte using C-style memcmp for performance. ```ocaml open Bigstringaf let buf1 = of_string ~off:0 ~len:5 "AAAAA" let buf2 = of_string ~off:0 ~len:5 "AAAAB" let buf3 = of_string ~off:0 ~len:5 "AAAAA" let cmp1 = memcmp buf1 0 buf2 0 5 let cmp2 = memcmp buf1 0 buf3 0 5 let cmp3 = memcmp buf2 0 buf1 0 5 let () = Printf.printf "buf1 vs buf2: %d (negative = less than)\n" cmp1 let () = Printf.printf "buf1 vs buf3: %d (zero = equal)\n" cmp2 let () = Printf.printf "buf2 vs buf1: %d (positive = greater than)\n" cmp3 (* Output: buf1 vs buf2: -1 (negative = less than) buf1 vs buf3: 0 (zero = equal) buf2 vs buf1: 1 (positive = greater than) *) ``` -------------------------------- ### Create a View of a Bigstring Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Returns a new view into the same underlying buffer without allocating new memory. Changes to the sub-view affect the original bigstring. Use `set` to modify. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:26 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" let view = sub buffer ~off:5 ~len:10 let () = Printf.printf "View: %s (length: %d)\n" (to_string view) (length view) (* Output: View: FGHIJKLMNO (length: 10) *) (* Modifying view affects original buffer *) let () = set view 0 'X' let () = Printf.printf "Original after modification: %s\n" (to_string buffer) (* Output: Original after modification: ABCDEXGHIJKLMNOPQRSTUVWXYZ *) ``` -------------------------------- ### Blit from String or Bytes Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Copy bytes from OCaml string or bytes values into a bigstring. ```ocaml open Bigstringaf let dst = create 20 let () = for i = 0 to 19 do set dst i '_' done (* Copy from string *) let src_string = "Hello" let () = blit_from_string src_string ~src_off:0 dst ~dst_off:0 ~len:5 let () = Printf.printf "After string blit: %s\n" (to_string dst) (* Output: After string blit: Hello_______________ *) (* Copy from bytes *) let src_bytes = Bytes.of_string "World" let () = blit_from_bytes src_bytes ~src_off:0 dst ~dst_off:6 ~len:5 let () = Printf.printf "After bytes blit: %s\n" (to_string dst) (* Output: After bytes blit: Hello_World_________ *) ``` -------------------------------- ### String Comparison Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Compare a bigstring with an OCaml string byte-by-byte. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:5 "Hello" let str1 = "Hello" let str2 = "World" let cmp1 = memcmp_string buffer 0 str1 0 5 let cmp2 = memcmp_string buffer 0 str2 0 5 let () = Printf.printf "buffer vs 'Hello': %d (equal)\n" cmp1 let () = Printf.printf "buffer vs 'World': %d (not equal)\n" cmp2 (* Output: buffer vs 'Hello': 0 (equal) buffer vs 'World': -15 (not equal) *) ``` -------------------------------- ### Bigstringaf Core Operations Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Core functions for creating, manipulating, and converting Bigstring buffers. ```APIDOC ## create ### Description Allocates a new bigstring of the specified length with uninitialized contents. ### Parameters - **len** (int) - Required - The length of the buffer to allocate. ## empty ### Description Returns an empty bigstring with length 0. ## of_string ### Description Creates a bigstring from a substring of an OCaml string, copying the specified range. ### Parameters - **off** (int) - Required - Offset in the source string. - **len** (int) - Required - Length of the substring to copy. - **data** (string) - Required - The source string. ## copy ### Description Allocates a new bigstring and copies a range of bytes from the source bigstring. ### Parameters - **src** (t) - Required - The source bigstring. - **off** (int) - Required - Offset in the source bigstring. - **len** (int) - Required - Number of bytes to copy. ## sub ### Description Returns a new view into the same underlying buffer without allocating new memory. ### Parameters - **src** (t) - Required - The source bigstring. - **off** (int) - Required - Offset in the source bigstring. - **len** (int) - Required - Length of the view. ## length ### Description Returns the length of the bigstring in bytes. ## get / set ### Description Reads or writes a single character at the specified offset with bounds checking. ## substring ### Description Extracts a range of bytes from a bigstring and returns them as an OCaml string. ## to_string ### Description Converts the entire bigstring to an OCaml string. ``` -------------------------------- ### Blit to Bytes Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Copy bytes from a bigstring to an OCaml bytes value. ```ocaml open Bigstringaf let src = of_string ~off:0 ~len:13 "Hello, World!" let dst = Bytes.make 5 '_' let () = blit_to_bytes src ~src_off:7 dst ~dst_off:0 ~len:5 let () = Printf.printf "Bytes: %s\n" (Bytes.to_string dst) (* Output: Bytes: World *) ``` -------------------------------- ### Little-Endian Integer Operations Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Read and write 16, 32, and 64-bit integers in little-endian byte order. Byte swapping is handled automatically on big-endian architectures. ```ocaml open Bigstringaf let buffer = create 16 (* Write integers in little-endian format *) let () = set_int16_le buffer 0 0xDEAD let () = set_int32_le buffer 2 0xDEADBEEFl let () = set_int64_le buffer 6 0xDEADBEEF8BADF00DL (* Read integers back *) let i16 = get_int16_le buffer 0 let i32 = get_int32_le buffer 2 let i64 = get_int64_le buffer 6 let () = Printf.printf "int16: 0x%04X\n" i16 let () = Printf.printf "int32: 0x%08lX\n" i32 let () = Printf.printf "int64: 0x%016LX\n" i64 (* Output: int16: 0xDEAD int32: 0xDEADBEEF int64: 0xDEADBEEF8BADF00D *) (* Sign-extended 16-bit read *) let buffer2 = of_string ~off:0 ~len:2 "\xFF\x7F" let signed = get_int16_sign_extended_le buffer2 0 let () = Printf.printf "Sign-extended: %d\n" signed (* Output: Sign-extended: 32767 *) ``` -------------------------------- ### Big-Endian Integer Operations Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Read and write 16, 32, and 64-bit integers in big-endian (network) byte order. ```ocaml open Bigstringaf let buffer = create 16 (* Write integers in big-endian (network) format *) let () = set_int16_be buffer 0 0xDEAD let () = set_int32_be buffer 2 0xDEADBEEFl let () = set_int64_be buffer 6 0xDEADBEEF8BADF00DL (* Read integers back *) let i16 = get_int16_be buffer 0 let i32 = get_int32_be buffer 2 let i64 = get_int64_be buffer 6 let () = Printf.printf "int16: 0x%04X\n" i16 let () = Printf.printf "int32: 0x%08lX\n" i32 let () = Printf.printf "int64: 0x%016LX\n" i64 (* Output: int16: 0xDEAD int32: 0xDEADBEEF int64: 0xDEADBEEF8BADF00D *) (* Verify bytes are in network order *) let () = Printf.printf "First byte: 0x%02X (should be 0xDE)\n" (Char.code (get buffer 0)) (* Output: First byte: 0xDE (should be 0xDE) *) ``` -------------------------------- ### Error handling for safe operations Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Demonstrates how safe Bigstringaf operations raise `Invalid_argument` exceptions with descriptive messages when bounds checks fail. This ensures memory safety by preventing out-of-bounds access. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:4 "test" (* Attempting to read beyond bounds *) let () = try let _ = get buffer 10 in () with Invalid_argument msg -> Printf.printf "Error: %s\n" msg (* Output: Error: index out of bounds *) (* Attempting invalid blit *) let () = try let _ = copy buffer ~off:0 ~len:(-8) in () with Invalid_argument msg -> Printf.printf "Error: %s\n" msg (* Output: Error: Bigstringaf.copy invalid range: { buffer_len: 4, off: 0, len: -8 } *) (* Attempting overflow attack *) let () = try let _ = of_string ~off:max_int ~len:2 "abc" in () with Invalid_argument msg -> Printf.printf "Error: %s\n" msg (* Output: Error: Bigstringaf.of_string invalid range: { buffer_len: 3, off: , len: 2 } *) ``` -------------------------------- ### Create Bigstring from String Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Creates a bigstring from a substring of an OCaml string, copying the specified range. Supports creating from the full string or a partial range using `off` and `len`. ```ocaml open Bigstringaf (* Create bigstring from full string *) let data = "Hello, World!" let buffer = of_string ~off:0 ~len:(String.length data) data let () = Printf.printf "Created buffer of length %d\n" (length buffer) (* Output: Created buffer of length 13 *) (* Create bigstring from substring (skip "Hello, ") *) let partial = of_string ~off:7 ~len:6 data let result = to_string partial let () = Printf.printf "Partial: %s\n" result (* Output: Partial: World! *) ``` -------------------------------- ### Unsafe memory access and operations Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Provides memory-unsafe variants for direct memory access and operations like blitting. Use these only when external checks guarantee memory bounds are valid to maximize performance. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:8 "\xDE\xAD\xBE\xEF\x8B\xAD\xF0\x0D" (* Unsafe character access *) let ch = unsafe_get buffer 0 let () = Printf.printf "Unsafe get: 0x%02X\n" (Char.code ch) (* Output: Unsafe get: 0xDE *) (* Unsafe integer reads *) let i16 = unsafe_get_int16_be buffer 0 let i32 = unsafe_get_int32_be buffer 0 let i64 = unsafe_get_int64_be buffer 0 let () = Printf.printf "Unsafe int16: 0x%04X\n" i16 let () = Printf.printf "Unsafe int32: 0x%08lX\n" i32 let () = Printf.printf "Unsafe int64: 0x%016LX\n" i64 (* Output: Unsafe int16: 0xDEAD Unsafe int32: 0xDEADBEEF Unsafe int64: 0xDEADBEEF8BADF00D *) (* Unsafe blit (uses memmove for overlapping regions) *) let src = of_string ~off:0 ~len:10 "ABCDEFGHIJ" let dst = create 10 let () = unsafe_blit src ~src_off:0 dst ~dst_off:0 ~len:10 let () = Printf.printf "Unsafe blit result: %s\n" (to_string dst) (* Output: Unsafe blit result: ABCDEFGHIJ *) ``` -------------------------------- ### Copy a Range of a Bigstring Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Allocates a new bigstring and copies a specified range of bytes from a source bigstring. Uses `off` and `len` to define the range to copy. ```ocaml open Bigstringaf let original = of_string ~off:0 ~len:26 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" let copied = copy original ~off:10 ~len:5 let () = Printf.printf "Copied: %s\n" (to_string copied) (* Output: Copied: KLMNO *) ``` -------------------------------- ### Convert Bigstring to String Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Converts the entire bigstring to an OCaml string. This operation copies the entire content of the bigstring. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:11 "Hello World" let str = to_string buffer let () = Printf.printf "String: %s\n" str (* Output: String: Hello World *) ``` -------------------------------- ### Blit Operations Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Copy bytes between bigstrings with bounds checking, supporting overlapping regions. ```ocaml open Bigstringaf let src = of_string ~off:0 ~len:26 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" let dst = of_string ~off:0 ~len:26 "abcdefghijklmnopqrstuvwxyz" (* Copy 8 bytes from src offset 0 to dst offset 4 *) let () = blit src ~src_off:0 dst ~dst_off:4 ~len:8 let () = Printf.printf "Result: %s\n" (to_string dst) (* Output: Result: abcdABCDEFGHmnopqrstuvwxyz *) (* Overlapping blit within same buffer *) let buf = of_string ~off:0 ~len:26 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" let () = blit buf ~src_off:0 buf ~dst_off:4 ~len:8 let () = Printf.printf "Overlapping: %s\n" (to_string buf) (* Output: Overlapping: ABCDABCDEFGHMNOPQRSTUVWXYZ *) ``` -------------------------------- ### Search for a byte in a bigstring Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Searches for a byte within a specified range of a bigstring. Returns the index of the first occurrence or -1 if not found. Ensure the offset and length parameters correctly define the search range. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:23 "hello world foo bar baz" let len = length buffer (* Find first space *) let pos1 = memchr buffer 0 ' ' len let () = Printf.printf "First space at: %d\n" pos1 (* Output: First space at: 5 *) (* Find space starting from offset 7 *) let pos2 = memchr buffer 7 ' ' (len - 7) let () = Printf.printf "Next space at: %d\n" pos2 (* Output: Next space at: 11 *) (* Search for non-existent character *) let pos3 = memchr buffer 0 'Z' len let () = Printf.printf "Character 'Z': %d\n" pos3 (* Output: Character 'Z': -1 *) ``` -------------------------------- ### Extract Substring from Bigstring Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt Extracts a range of bytes from a bigstring and returns them as an OCaml string. Uses `off` and `len` to specify the range. ```ocaml open Bigstringaf let buffer = of_string ~off:0 ~len:13 "Hello, World!" let sub = substring buffer ~off:0 ~len:5 let () = Printf.printf "Substring: %s\n" sub (* Output: Substring: Hello *) ``` -------------------------------- ### Bigstring Type Definition Source: https://context7.com/inhabitedtype/bigstringaf/llms.txt The core bigstring type is defined as a C-layout character Bigarray. ```ocaml type t = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.