### Example with `count_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows an example of the optimized `count_opt` function for counting byte occurrences, potentially offering performance benefits. ```rust let s = b"count the occurrences"; let c = b'c'; let num = count_opt(s, c); assert_eq!(num, 3); ``` -------------------------------- ### Example with `memchrnot_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows an example of `memchrnot_opt` for finding the first byte not equal to a specific character, potentially with performance improvements. ```rust let s = b"xxxxabc"; let c = b'x'; let index = memchrnot_opt(s, c); assert_eq!(index, Some(4)); ``` -------------------------------- ### Example with `memrcmany_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows an example of `memrcmany_opt` for finding the last byte within a set of characters, potentially faster than `memrcmany`. ```rust let s = b"find any of these"; let chars = b"aeiou"; let index = memrcmany_opt(s, chars); assert_eq!(index, Some(15)); ``` -------------------------------- ### Benchmark Setup Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Sets up a byte slice for benchmarking purposes. ```rust const BENCHMARK_SIZE: usize = 1024; #[inline(never)] fn benchmark_setup() -> Vec { let mut v = Vec::with_capacity(BENCHMARK_SIZE); for i in 0..BENCHMARK_SIZE { v.push((i % 256) as u8); } v } ``` -------------------------------- ### Basic Usage Example Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates a simple use case of `memchr` to find the first occurrence of a byte within a string slice. This example shows how to handle the `Option` return value. ```rust let haystack = b"hello world"; let needle = b'o'; let index = memchr(haystack, needle); assert_eq!(index, Some(4)); ``` -------------------------------- ### Begin SummarySink operation Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Starts an operation that will output to a SummarySink. ```rust begin> ``` ```rust begin<&&grep_pcre2::matcher::RegexMatcher, &mut grep_printer::summary::SummarySink<&&grep_pcre2::matcher::RegexMatcher, grep_cli::wtr::StandardStream>> ``` -------------------------------- ### Example with `memchr_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates the usage of the optimized `memchr_opt` function. It performs the same task as `memchr` but may offer better performance. ```rust let haystack = b"optimized search"; let needle = b's'; let index = memchr_opt(haystack, needle); assert_eq!(index, Some(9)); ``` -------------------------------- ### Example with `memcmanynot_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates the optimized `memcmanynot_opt` function for finding the first byte not in a given set, offering potential speed improvements. ```rust let s = b"consonants only"; let chars = b"aeiou"; let index = memcmanynot_opt(s, chars); assert_eq!(index, Some(0)); ``` -------------------------------- ### Example with `memcmany_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates the usage of `memcmany_opt` for finding the first byte within a set of characters, with potential performance gains. ```rust let s = b"find any of these"; let chars = b"aeiou"; let index = memcmany_opt(s, chars); assert_eq!(index, Some(1)); ``` -------------------------------- ### Example with `memrcmanynot_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates the use of `memrcmanynot_opt` for finding the last byte not in a given set, potentially faster than `memrcmanynot`. ```rust let s = b"consonants only"; let chars = b"aeiou"; let index = memrcmanynot_opt(s, chars); assert_eq!(index, Some(14)); ``` -------------------------------- ### Vector Initialization Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Creates a new, empty `Vec` with no allocated capacity. This is the most basic way to start a vector. ```rust pub fn new() -> Vec { Vec { buf: RawVec::new(), len: 0 } } ``` -------------------------------- ### Rust Macro Definitions Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Examples of Rust macros, including internal and uint-specific macros. ```rust macro_rules! vec { ( $( $x:expr ),* ) => { { let mut temp_vec = ::alloc::vec::Vec::new(); $( temp_vec.push($x); )* temp_vec } }; } ``` ```rust macro_rules! panic { () => ({ panic!("{}"); }); ($fmt:expr) => ({ panic!($fmt); }); ($fmt:expr, $( $arg:tt )*) => ({ ::core::panicking::panic_fmt(format_args!($fmt, $( $arg )*)); }); } ``` -------------------------------- ### Using `memcmanynot` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates `memcmanynot` to find the first byte in "hello world" that is not a vowel. This can be used to find the start of consonants. ```rust let s = b"aeiouxyz"; let chars = b"aeiou"; let index = memcmanynot(s, chars); assert_eq!(index, Some(5)); ``` -------------------------------- ### Integer SIMD Operations Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Provides examples of integer SIMD operations, including splatting values and loading data. ```rust _ZN9bytecount12integer_simd5splat17h990601c90747d9bfE ``` ```rust _ZN9bytecount12integer_simd20usize_load_unchecked17hd46cdc6c35858e6aE ``` ```rust _ZN9bytecount12integer_simd14bytewise_equal17hcf06e64a4ebf2386E ``` ```rust _ZN89_$LT$core..ops..range..Range$LT$T$GT$$u20$as$u20$core..iter..range..RangeIteratorImpl$GT$9spec_next17he126d99f487442c3E ``` ```rust _ZN4core4iter5range101_$LT$impl$u20$core..iter..traits..iterator..Iterator$u20$for$u20$core..ops..range..Range$LT$A$GT$$GT$4next17h94511884e3c92d0dE ``` ```rust _ZN9bytecount12integer_simd9sum_usize17h697ce95304b87a5dE ``` ```rust _ZN9bytecount12integer_simd20is_leading_utf8_byte17h89860113ac974089E ``` ```rust _ZN9bytecount12integer_simd11chunk_count17h80f090702f6f6f07E ``` ```rust _ZN9bytecount12integer_simd15chunk_num_chars17ha8363c48ae05c054E ``` -------------------------------- ### Raw Vector Initialization Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Initializes a new `RawVec` with a null pointer and zero capacity. This is the starting point for managing a dynamically sized buffer. ```rust pub fn new() -> Self { assert!(mem::size_of::() != 0, "size of T cannot be zero"); RawVec { ptr: std::ptr::null_mut(), cap: 0, } } ``` -------------------------------- ### Example with `memrchr_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates the use of the optimized `memrchr_opt` function to find the last occurrence of a byte. This is equivalent to `memrchr` but potentially faster. ```rust let haystack = b"find last optimized"; let needle = b'd'; let index = memrchr_opt(haystack, needle); assert_eq!(index, Some(18)); ``` -------------------------------- ### Find first occurrence of a byte in a slice with a custom searcher and offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt This example demonstrates using `memchr_with_offset` with a custom searcher, allowing you to specify a starting offset for the search. ```rust use memchr::memchr_with_offset; let haystack = b"search for a pattern"; let needle = b't'; let offset = 10; // Search starts from index 10 assert_eq!(memchr_with_offset(needle, haystack, offset), Some(13)); ``` -------------------------------- ### Find last occurrence of a byte in a slice with a custom searcher and offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt This example demonstrates using `memrchr_with_offset` with a custom searcher, allowing you to specify a starting offset for the search from the end. ```rust use memchr::memrchr_with_offset; let haystack = b"search for a pattern"; let needle = b'a'; let offset = 10; // Search starts from index 10 backwards assert_eq!(memrchr_with_offset(needle, haystack, offset), Some(13)); ``` -------------------------------- ### Protected Mode Transition Initialization Example Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/sliceslice/i386-notutf8.txt This assembly code snippet is part of the reset initialization for protected mode on the 386 processor. It outlines assumptions about the initial state after reset, including the starting execution address and segment base. ```assembly NAME RESET ;***************************************************************** ; Upon reset the 386 starts executing at address 0FFFFFFF0H. The ; upper 12 address bits remain high until a FAR call or jump is ; executed. ; ; Assume the following: ; ; ; - a short jump at address 0FFFFFFF0H (placed there by the ; system builder) causes execution to begin at START in segment ; RESET_CODE. ; ; ; - segment RESET_CODE is based at physical address 0FFFF0000H, ; i.e. at the start of the last 64K in the 4G address space. ; Note that this is the base of the CS register at reset. If ; you locate ROMcode above this address, you will need to ; figure out an adjustment factor to address things within this ; segment. ; ;***************************************************************** ``` -------------------------------- ### FFI Example: Find first 'o' or ' ' in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memchr2` function to find the first 'o' or ' ' in "hello world". ```c #include #include // Assuming memchr2 is available from the memchr crate // void *memchr2(int c1, int c2, const void *s, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); int needle1 = 'o'; int needle2 = ' '; char *result = memchr2(needle1, needle2, haystack, n); if (result != NULL) { printf("Found first '%c' or '%c' at index %ld\n", needle1, needle2, result - haystack); } else { printf("Neither '%c' nor '%c' found\n", needle1, needle2); } return 0; } ``` -------------------------------- ### FFI Example: Find first 'o' in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memchr` function to find the first 'o' in "hello world". ```c #include #include // Assuming memchr is available from the memchr crate // void *memchr(const void *s, int c, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); int needle = 'o'; char *result = memchr(haystack, needle, n); if (result != NULL) { printf("Found '%c' at index %ld\n", needle, result - haystack); } else { printf("'%c' not found\n", needle); } return 0; } ``` -------------------------------- ### Getting String Length in Bytes Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows how to get the length of a string in bytes using the `len()` method. ```rust let s = String::from("hello"); println!("Length in bytes: {}", s.len()); ``` -------------------------------- ### Getting String Length in Characters Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates getting the number of Unicode characters in a string using `chars().count()`. ```rust let s = String::from("hello"); println!("Length in characters: {}", s.chars().count()); ``` -------------------------------- ### Assertion Failure: Start <= End Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt This assertion failure indicates that the start index is greater than the end index, which is an invalid state. ```rust assertion failed: start <= end ``` -------------------------------- ### FFI Example: Find first 'h', ' ', or 'd' in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memchr3` function to find the first 'h', ' ', or 'd' in "hello world". ```c #include #include // Assuming memchr3 is available from the memchr crate // void *memchr3(int c1, int c2, int c3, const void *s, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); int needle1 = 'h'; int needle2 = ' '; int needle3 = 'd'; char *result = memchr3(needle1, needle2, needle3, haystack, n); if (result != NULL) { printf("Found first '%c', '%c', or '%c' at index %ld\n", needle1, needle2, needle3, result - haystack); } else { printf("None of '%c', '%c', or '%c' found\n", needle1, needle2, needle3); } return 0; } ``` -------------------------------- ### FFI Example: Find first vowel in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memchr_any` function to find the first vowel in "hello world". ```c #include #include // Assuming memchr_any is available from the memchr crate // void *memchr_any(const unsigned char *cs, size_t m, const void *s, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); const unsigned char *needles = "aeiou"; size_t m = strlen((const char *)needles); char *result = memchr_any(needles, m, haystack, n); if (result != NULL) { printf("Found first vowel '%c' at index %ld\n", *result, result - haystack); } else { printf("No vowels found\n"); } return 0; } ``` -------------------------------- ### Assembly Implementation Example (x86_64) Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Example of an assembly implementation for memchr on x86_64 architecture. This demonstrates low-level optimization techniques. ```asm pub unsafe extern "C" fn memchr(haystack: *const u8, needle: u8, len: usize) -> *const u8 { let mut ptr = haystack; let end = haystack.add(len); while ptr != end { if *ptr == needle { return ptr; } ptr = ptr.add(1); } std::ptr::null() } ``` -------------------------------- ### Checking if a String Starts or Ends with a Pattern Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows how to check if a string starts or ends with a given pattern using `starts_with()` and `ends_with()`. ```rust let s = String::from("hello world"); println!("Starts with 'hello': {}", s.starts_with("hello")); println!("Ends with 'world': {}", s.ends_with("world")); ``` -------------------------------- ### FFI Example: Count 'l' in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memcount` function to count occurrences of 'l' in "hello world". ```c #include #include // Assuming memcount is available from the memchr crate // size_t memcount(int c, const void *s, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); int needle = 'l'; size_t count = memcount(needle, haystack, n); printf("Found %zu occurrences of '%c'\n", count, needle); return 0; } ``` -------------------------------- ### Using `Vec` with `get()` for Safe Access Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates safe access to vector elements using the `get()` method, which returns an `Option<&T>`. This prevents panics from out-of-bounds indexing. ```rust let v = vec![10, 20, 30]; let third = v.get(2); match third { Some(val) => println!("The third element is {}", val), None => println!("There is no third element."), } let fifth = v.get(5); match fifth { Some(val) => println!("The fifth element is {}", val), None => println!("There is no fifth element."), } ``` -------------------------------- ### Find First Occurrence of a Byte in a Subslice Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Finds the first occurrence of a byte `c` in a subslice of `s` starting from `start` and ending at `end`. Returns `None` if the byte is not found. ```rust pub fn memchr2(s: &[u8], c1: u8, c2: u8) -> Option { memchr2_internal(s, c1, c2) } ``` -------------------------------- ### Byte Search with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of a byte within a byte slice, starting from a specified offset. Returns the index relative to the start of the slice or None if the byte is not found. ```rust pub fn memchr_with_offset(needle: u8, haystack: &[u8], offset: usize) -> Option { memchr_internal(needle, &haystack[offset..]) } ``` -------------------------------- ### Find Last Occurrence of a Byte with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/pathological/random-huge.txt Use `memrchr` with a slice starting from a specific offset to search within a portion of the byte slice. The returned index is relative to the start of the slice. ```rust use memchr::memrchr; let haystack = b"hello world"; let needle = b'o'; let offset = 5; assert_eq!(memrchr(needle, &haystack[offset..]), Some(2)); // 'o' at index 7 in original, index 2 in slice ``` -------------------------------- ### FFI Example: Find last 'o' in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memrchr` function to find the last 'o' in "hello world". ```c #include #include // Assuming memrchr is available from the memchr crate // void *memrchr(const void *s, int c, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); int needle = 'o'; char *result = memrchr(haystack, needle, n); if (result != NULL) { printf("Found last '%c' at index %ld\n", needle, result - haystack); } else { printf("'%c' not found\n", needle); } return 0; } ``` -------------------------------- ### Find First Occurrence of a Byte with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/pathological/random-huge.txt Use `memchr` with a slice starting from a specific offset to search within a portion of the byte slice. The returned index is relative to the start of the slice. ```rust use memchr::memchr; let haystack = b"hello world"; let needle = b'o'; let offset = 5; assert_eq!(memchr(needle, &haystack[offset..]), Some(2)); // 'o' at index 7 in original, index 2 in slice ``` -------------------------------- ### Using `count` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates the `count` function to determine how many times the byte 'l' appears in "hello world". This is useful for frequency analysis. ```rust let s = b"hello world"; let c = b'l'; let num = count(s, c); assert_eq!(num, 3); ``` -------------------------------- ### Creating a `String` with Capacity Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows how to create a `String` with a pre-allocated capacity using `String::with_capacity()`. This can improve performance by reducing reallocations. ```rust let mut s = String::with_capacity(10); println!("Capacity: {}, Length: {}", s.capacity(), s.len()); s.push_str("hello"); println!("Capacity: {}, Length: {}", s.capacity(), s.len()); ``` -------------------------------- ### Assertion failed: end >= start && end <= len Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt A runtime assertion failure, likely related to slicing or range operations, ensuring that the end of a range is valid with respect to the start and the total length. ```rust assertion failed: end >= start && end <= len ``` -------------------------------- ### Walking a Directory Tree Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates how to recursively traverse a directory using `walkdir::WalkDir`. This is useful for processing all files within a directory structure. ```rust use walkdir::WalkDir; let target = "/tmp"; for entry in WalkDir::new(target) { match entry { Ok(entry) => { println!("{}", entry.path().display()); } Err(err) => { eprintln!("Error accessing {}: {}", target, err); } } } ``` -------------------------------- ### Find all occurrences of a pattern in a specific range Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Finds all occurrences of a byte pattern within a specified range of a byte slice. Returns a vector of indices relative to the start of the slice where the pattern starts. ```rust use memchr::memmem::FindAll; let haystack = b"abababab"; let needle = b"aba"; let mut finder = FindAll::new(needle); let mut results = vec![]; let mut start = 0; let range = &haystack[1..]; while let Some(idx) = finder.find(&range[start..]) { results.push(start + idx); start += idx + 1; } assert_eq!(results, vec![1, 3, 5]); ``` -------------------------------- ### Memtrim_start_opt Function Implementation Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt This snippet demonstrates the memtrim_start_opt function, an optimized version of memtrim_start that might use faster searching algorithms. ```Rust pub fn memtrim_start_opt<'a>(s: &'a [u8], cut: &[u8]) -> &'a [u8] { #[cfg(target_arch = "x86_64")] { if let Some(slice) = memtrim_start_x86_64(s, cut) { return slice; } } memtrim_start(s, cut) } ``` -------------------------------- ### Find First Occurrence of Any Byte from a Set with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/pathological/random-huge.txt Use `memchr_any` with a slice starting from a specific offset to search within a portion of the byte slice. The returned index is relative to the start of the slice. ```rust use memchr::memchr_any; let haystack = b"hello world"; let needles = b"aeiou"; let offset = 5; assert_eq!(memchr_any(needles, &haystack[offset..]), Some(1)); // 'o' at index 7 in original, index 1 in slice ``` -------------------------------- ### Find Last Occurrence of a Byte in a Slice with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the last occurrence of a byte within a slice, starting from a specified offset. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memrchr_offset; let haystack = b"hello world"; let needle = b'o'; let offset = 5; assert_eq!(memrchr_offset(needle, haystack, offset), Some(7)); ``` -------------------------------- ### Working with Byte Buffers Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates creating and manipulating byte buffers. Useful for low-level data handling and serialization. ```rust use std::io::Cursor; let mut buffer = Cursor::new(vec![0; 10]); buffer.write_all(&[1, 2, 3]).unwrap(); assert_eq!(buffer.position(), 3); assert_eq!(buffer.into_inner(), vec![1, 2, 3, 0, 0, 0, 0, 0, 0, 0]); ``` -------------------------------- ### Find First Occurrence of a Byte in a Slice with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of a byte within a slice, starting from a specified offset. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr_offset; let haystack = b"hello world"; let needle = b'o'; let offset = 5; assert_eq!(memchr_offset(needle, haystack, offset), Some(7)); ``` -------------------------------- ### Find Substring (reversed with offset) Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the last occurrence of a sub-slice within a larger byte slice, starting from a specified offset. Returns the starting index relative to the beginning of the haystack or None if not found. ```rust pub fn memrmem_with_offset<'a>(needle: &'a [u8], haystack: &'a [u8], offset: usize) -> Option { memrmem_internal(needle, &haystack[offset..]) } ``` -------------------------------- ### Example usage of memchr_any with a byte slice Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates finding the first occurrence of any vowel in the byte slice `b"hello world"`. ```rust use memchr::memchr_any; let haystack = b"hello world"; let needles = b"aeiou"; assert_eq!(memchr_any(needles, haystack), Some(1)); ``` -------------------------------- ### Find Substring with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Finds the first occurrence of a subslice (needle) within a larger slice (haystack), starting the search from a specified offset. Returns the starting index of the first match, or None if not found. ```rust use memchr::memmem::find; let haystack = b"hello world"; let needle = b"world"; assert_eq!(find(&haystack[5..], needle), Some(1)); ``` -------------------------------- ### Creating an Empty `String` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates how to create an empty, growable `String`. Use `String::new()` for this purpose. ```rust let mut s = String::new(); s.push_str("hello"); s.push('!'); println!("{}", s); ``` -------------------------------- ### Find Last Occurrence of a Byte in a Slice with Offset and Limit Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the last occurrence of a byte within a slice, starting from a specified offset and up to a specified limit. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memrchr_offset_limit; let haystack = b"hello world"; let needle = b'o'; let offset = 5; let limit = 10; assert_eq!(memrchr_offset_limit(needle, haystack, offset, limit), Some(7)); ``` -------------------------------- ### Begin JSONSink operation Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Initiates an operation with a JSONSink, preparing it for receiving data. ```rust begin<&&grep_pcre2::matcher::RegexMatcher, &mut grep_printer::json::JSONSink<&&grep_pcre2::matcher::RegexMatcher, grep_cli::wtr::StandardStream>> ``` -------------------------------- ### Find First Occurrence of a Byte in a Slice with Offset and Limit Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of a byte within a slice, starting from a specified offset and up to a specified limit. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr_offset_limit; let haystack = b"hello world"; let needle = b'o'; let offset = 5; let limit = 10; assert_eq!(memchr_offset_limit(needle, haystack, offset, limit), Some(7)); ``` -------------------------------- ### FFI Example: Find last vowel in "hello world" Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates calling the C-compatible `memrchr_any` function to find the last vowel in "hello world". ```c #include #include // Assuming memrchr_any is available from the memchr crate // void *memrchr_any(const unsigned char *cs, size_t m, const void *s, size_t n); int main() { const char *haystack = "hello world"; size_t n = strlen(haystack); const unsigned char *needles = "aeiou"; size_t m = strlen((const char *)needles); char *result = memrchr_any(needles, m, haystack, n); if (result != NULL) { printf("Found last vowel '%c' at index %ld\n", *result, result - haystack); } else { printf("No vowels found\n"); } return 0; } ``` -------------------------------- ### Basic `String` Formatting with `format!` Macro in Rust Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates how to use the `format!` macro to create formatted strings. This macro is similar to `println!` but returns a `String` instead of printing to the console. ```rust fn main() { let name = "Alice"; let age = 30; let s = format!("My name is {} and I am {} years old.", name, age); println!("{}", s); } ``` -------------------------------- ### Find First Occurrence of Any Byte from a Set in a Slice with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of any byte from a given set within a slice, starting from a specified offset. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr2_offset; let haystack = b"hello world"; let needle1 = b'w'; let needle2 = b'd'; let offset = 5; assert_eq!(memchr2_offset(needle1, needle2, haystack, offset), Some(7)); ``` -------------------------------- ### Find Last Occurrence of Any Byte from a Mask in a Slice with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the last occurrence of any byte that matches a given bitmask within a slice, starting from a specified offset. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memrchr_mask_offset; let haystack = b"hello world"; let mask = 0b0000_0100; // Matches 'd' let offset = 5; assert_eq!(memrchr_mask_offset(mask, haystack, offset), Some(10)); ``` -------------------------------- ### Using `memchrnot` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows an example of `memchrnot` to find the first byte that is not 'l' in "hello world". This can be used to find the end of a sequence of a specific character. ```rust let s = b"lllhello world"; let c = b'l'; let index = memchrnot(s, c); assert_eq!(index, Some(3)); ``` -------------------------------- ### Example usage of memchr2 with a byte slice Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates finding the first occurrence of either 'o' or ' ' in the byte slice `b"hello world"`. ```rust use memchr::memchr2; let haystack = b"hello world"; let needle1 = b'o'; let needle2 = b' '; assert_eq!(memchr2(needle1, needle2, haystack), Some(4)); ``` -------------------------------- ### Find First Occurrence of Any Byte from a Mask in a Slice with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of any byte that matches a given bitmask within a slice, starting from a specified offset. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr_mask_offset; let haystack = b"hello world"; let mask = 0b0000_0100; // Matches 'd' let offset = 5; assert_eq!(memchr_mask_offset(mask, haystack, offset), Some(10)); ``` -------------------------------- ### Find First Occurrence of Any Byte from a Set of Three in a Slice with Offset Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of any byte from a given set of three within a slice, starting from a specified offset. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr3_offset; let haystack = b"hello world"; let needle1 = b'x'; let needle2 = b'w'; let needle3 = b'd'; let offset = 5; assert_eq!(memchr3_offset(needle1, needle2, needle3, haystack, offset), Some(7)); ``` -------------------------------- ### Creating and Using a BTreeMap in Rust Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates the creation and basic usage of `BTreeMap`, an ordered map implementation. Elements are sorted by key. ```rust use std::collections::BTreeMap; fn main() { let mut map = BTreeMap::new(); map.insert(1, "one"); map.insert(2, "two"); println!("{:?}", map); } ``` -------------------------------- ### Find First Occurrence of Any Byte from a Set in a Slice with Offset and Limit Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of any byte from a given set within a slice, starting from a specified offset and up to a specified limit. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr2_offset_limit; let haystack = b"hello world"; let needle1 = b'w'; let needle2 = b'd'; let offset = 5; let limit = 10; assert_eq!(memchr2_offset_limit(needle1, needle2, haystack, offset, limit), Some(7)); ``` -------------------------------- ### Using `memchr` for Byte Searching Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates using the `memchr` crate for efficient searching of a byte within a byte slice. This is often faster than manual iteration. ```rust extern crate memchr; let haystack = b"hello world"; let needle = b'o'; match memchr::memchr(needle, haystack) { Some(index) => println!("Found '{}' at index {}", needle as char, index), None => println!("'{}' not found.", needle as char), } ``` -------------------------------- ### Example usage of memchr3 with a byte slice Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates finding the first occurrence of 'h', ' ', or 'd' in the byte slice `b"hello world"`. ```rust use memchr::memchr3; let haystack = b"hello world"; let needle1 = b'h'; let needle2 = b' '; let needle3 = b'd'; assert_eq!(memchr3(needle1, needle2, needle3, haystack), Some(0)); ``` -------------------------------- ### Find Last Occurrence of Any Byte from a Mask in a Slice with Offset and Limit Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the last occurrence of any byte that matches a given bitmask within a slice, starting from a specified offset and up to a specified limit. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memrchr_mask_offset_limit; let haystack = b"hello world"; let mask = 0b0000_0100; // Matches 'd' let offset = 5; let limit = 10; assert_eq!(memrchr_mask_offset_limit(mask, haystack, offset, limit), Some(10)); ``` -------------------------------- ### Example with `memrchrnot_opt` Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates the optimized `memrchrnot_opt` function for finding the last byte not matching a given character. This offers potential speed benefits. ```rust let s = b"abcxxxx"; let c = b'x'; let index = memrchrnot_opt(s, c); assert_eq!(index, Some(2)); ``` -------------------------------- ### Basic `HashMap` Usage in Rust Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates the creation and basic operations of `HashMap`, a hash table implementation. Keys and values can be of any type that implements `Eq` and `Hash`. ```rust use std::collections::HashMap; fn main() { let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50); println!("{:?}", scores); } ``` -------------------------------- ### Find First Occurrence of Any Byte from a Mask in a Slice with Offset and Limit Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of any byte that matches a given bitmask within a slice, starting from a specified offset and up to a specified limit. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr_mask_offset_limit; let haystack = b"hello world"; let mask = 0b0000_0100; // Matches 'd' let offset = 5; let limit = 10; assert_eq!(memchr_mask_offset_limit(mask, haystack, offset, limit), Some(10)); ``` -------------------------------- ### Rust Settings Module Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Utilities for managing settings. ```rust pub mod settings { // ... settings related items } ``` -------------------------------- ### Find the first occurrence of a pattern in a specific range (offset) Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Finds the first occurrence of a byte pattern within a specified range of a byte slice, starting the search from a specified offset within that range. Returns the index relative to the start of the slice, or None if not found. ```rust use memchr::memmem::FindOptimized; let haystack = b"this is a test string"; let needle = b"test"; let finder = FindOptimized::new(needle); let range = &haystack[11..]; assert_eq!(finder.find(range), None); let range = &haystack[10..]; assert_eq!(finder.find(range), Some(0)); ``` -------------------------------- ### Create Regex Execution Builder Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Initializes a `RegexOptions` struct with default values, preparing it for building regex execution configurations. This is used for creating multiple regexes efficiently. ```rust _ZN74_$LT$regex..re_builder..RegexOptions$u20$as$u20$core..default..Default$GT$7default17hcbd26fb2758ed7b3E ``` -------------------------------- ### Find First Occurrence of Any Byte from a Set of Three in a Slice with Offset and Limit Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Searches for the first occurrence of any byte from a given set of three within a slice, starting from a specified offset and up to a specified limit. Returns the index relative to the start of the slice if found, otherwise None. ```rust use memchr::memchr3_offset_limit; let haystack = b"hello world"; let needle1 = b'x'; let needle2 = b'w'; let needle3 = b'd'; let offset = 5; let limit = 10; assert_eq!(memchr3_offset_limit(needle1, needle2, needle3, haystack, offset, limit), Some(7)); ``` -------------------------------- ### Get Vector Length Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Returns the number of elements currently in the vector. ```rust _ZN5alloc3vec16Vec$LT$T$C$A$GT$3len17he4f83075c177bcb5E ``` -------------------------------- ### Basic `HashSet` Usage in Rust Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates the creation and basic operations of `HashSet`, a hash set implementation. It stores unique elements. ```rust use std::collections::HashSet; fn main() { let mut set = HashSet::new(); set.insert(1); set.insert(2); set.insert(2); println!("{:?}", set); } ``` -------------------------------- ### Initialize LDT and Start Task (Assembly) Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/sliceslice/i386.txt Assembly code for initializing the Local Descriptor Table (LDT) and setting up descriptors for the Global Descriptor Table (GDT), Interrupt Descriptor Table (IDT), and Task State Segment (TSS). It concludes with jumping to the entry pointer to start the first task. ```assembly LAR DX,BX ; save access rights MOV [BX].access,DS_ACCESS ; set access as data segment MOV FS,BX ; FS points to eprom LDT XOR si,si ; FS:SI points to eprom LDT XOR di,di ; ES:DI points to RAM LDT MOV CX,[BX].lim_0_15 ; get count to move INC CX ; ; move initial LDT to RAM REP MOVS BYTE PTR ES:[di],BYTE PTR FS:[si] MOV [BX].access,DH ; restore access rights in ; INIT_LDT descriptor ; ; change base of alias (of INIT_LDT) to point to location in RAM. MOV ES:[INIT_LDT_ALIAS].bas_0_15,AX SHR EAX,16 MOV ES:[INIT_LDT_ALIAS].bas_16_23,AL MOV ES:[INIT_LDT_ALIAS].bas_24_31,AH ; ; now set the base value in INIT_LDT descriptor MOV AX,INIT_LDT_A_OFFSET.bas_0_15 MOV INIT_LDT_OFFSET.bas_0_15,AX MOV AL,INIT_LDT_A_OFFSET.bas_16_23 MOV INIT_LDT_OFFSET.bas_16_23,AL MOV AL,INIT_LDT_A_OFFSET.bas_24_31 MOV INIT_LDT_OFFSET.bas_24_31,AL ; ; Now GDT, IDT, initial TSS and initial LDT are all set up. ; ; Start the first task! ' JMP ENTRY_POINTER RESET_CODE ends END START, SS:DUMMY,DS:DUMMY ``` -------------------------------- ### String Slicing in Rust Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Shows how to create string slices from a `String`. Slices provide a view into a string without taking ownership. ```rust let s = String::from("hello world"); let hello = &s[0..5]; let world = &s[6..11]; println!("{}", hello); println!("{}", world); ``` -------------------------------- ### Generic Reader Strategies Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Illustrates different approaches for reading file content with the generic reader, including reading the entire file into memory. ```rust generic reader: reading everything to heap for multiline ``` ```rust generic reader: searching via roll buffer strategy: reading entire file on to heap for mulitline ``` -------------------------------- ### Get slice iterator Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Obtains an iterator over the elements of a slice. Used for traversing collections. ```rust iter ``` -------------------------------- ### Using `memmem` for Substring Searching Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Demonstrates using the `memmem` module from the `memchr` crate for efficient substring searching (finding a byte slice within another byte slice). ```rust extern crate memchr; let haystack = b"this is a test string"; let needle = b"test"; match memchr::memmem::find(haystack, needle) { Some(index) => println!("Found '{}' at index {}", std::str::from_utf8(needle).unwrap(), index), None => println!("'{}' not found.", std::str::from_utf8(needle).unwrap()), } ``` -------------------------------- ### Get byte count Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Calculates the byte count of data, often used for serialization or network transmission. ```rust byte_count<&&grep_regex::matcher::RegexMatcher, &mut grep_printer::json::JSONSink<&&grep_regex::matcher::RegexMatcher, termcolor::Buffer>> ``` -------------------------------- ### Get next item from iterator Source: https://github.com/burntsushi/memchr/blob/master/benchmarks/haystacks/rg-13.0.0.txt Retrieves the next item from an iterator. Standard operation for processing sequences. ```rust next ```