### Perform custom natural ordering comparisons with compare_iter Source: https://context7.com/lifthrasiir/rust-natord/llms.txt Demonstrates using compare_iter with various configurations, including custom character skipping, case-insensitive comparison, hexadecimal digit support, and byte slice comparison. ```rust use natord::compare_iter; use std::cmp::Ordering; fn main() { // Custom comparison: only consider alphanumeric characters, ignore punctuation let result = compare_iter( "file-1.txt".chars(), "file-2.txt".chars(), |&c| c.is_whitespace() || c == '-' || c == '.', // skip whitespace, hyphens, dots |&l, &r| l.cmp(&r), // standard char comparison |&c| c.to_digit(10).map(|v| v as isize) // decimal digit conversion ); assert_eq!(result, Ordering::Less); // Case-insensitive custom comparison let left = "Item-10".chars().flat_map(|c| c.to_lowercase()); let right = "ITEM-2".chars().flat_map(|c| c.to_lowercase()); let result = compare_iter( left, right, |&c| c.is_whitespace() || c == '-', |&l, &r| l.cmp(&r), |&c| c.to_digit(10).map(|v| v as isize) ); assert_eq!(result, Ordering::Greater); // 10 > 2 // Hexadecimal digit support let result = compare_iter( "addr0x0f".chars(), "addr0x10".chars(), |&c| c.is_whitespace(), |&l, &r| l.cmp(&r), |&c| c.to_digit(16).map(|v| v as isize) // hexadecimal conversion ); assert_eq!(result, Ordering::Less); // 0x0f (15) < 0x10 (16) // Working with byte slices instead of strings let bytes_left: Vec = b"data100".to_vec(); let bytes_right: Vec = b"data20".to_vec(); let result = compare_iter( bytes_left.iter(), bytes_right.iter(), |&&b| b == b' ', // skip spaces |&&l, &&r| l.cmp(&r), // byte comparison |&&b| if b >= b'0' && b <= b'9' { Some((b - b'0') as isize) } else { None } ); assert_eq!(result, Ordering::Greater); // 100 > 20 println!("Custom iterator comparisons passed!"); } ``` -------------------------------- ### Sort Files Naturally in Rust Source: https://github.com/lifthrasiir/rust-natord/blob/master/README.md Use the `natord::compare` function within `sort_by` to achieve natural ordering for a vector of strings. This ensures files are sorted in a human-friendly manner. ```rust let mut files = vec!("rfc2086.txt", "rfc822.txt", "rfc1.txt"); files.sort_by(|&a, &b| natord::compare(a, b)); assert_eq!(files, ["rfc1.txt", "rfc822.txt", "rfc2086.txt"]); ``` -------------------------------- ### Compare Strings with Natural Ordering (Case-Sensitive) Source: https://context7.com/lifthrasiir/rust-natord/llms.txt Compares two strings using natural ordering with case sensitivity. Handles Unicode whitespace and sorts decimal digit sequences numerically. Suitable for Rust's sorting functions. ```rust use natord::compare; use std::cmp::Ordering; fn main() { // Basic string comparison assert_eq!(compare("file1.txt", "file2.txt"), Ordering::Less); assert_eq!(compare("file10.txt", "file2.txt"), Ordering::Greater); assert_eq!(compare("hello", "hello"), Ordering::Equal); // Sorting a vector of filenames naturally let mut files = vec!["rfc2086.txt", "rfc822.txt", "rfc1.txt"]; files.sort_by(|a, b| compare(a, b)); assert_eq!(files, ["rfc1.txt", "rfc822.txt", "rfc2086.txt"]); // Sorting version strings let mut versions = vec!["v1.10", "v1.2", "v1.1", "v2.0"]; versions.sort_by(|a, b| compare(a, b)); assert_eq!(versions, ["v1.1", "v1.2", "v1.10", "v2.0"]); // Handling mixed alphanumeric content let mut items = vec!["pic100", "pic5", "pic20", "pic5a", "pic5b"]; items.sort_by(|a, b| compare(a, b)); assert_eq!(items, ["pic5", "pic5a", "pic5b", "pic20", "pic100"]); // Leading zeros are handled with left-aligned comparison let mut decimals = vec!["1.02", "1.1", "1.001", "1.010"]; decimals.sort_by(|a, b| compare(a, b)); assert_eq!(decimals, ["1.001", "1.010", "1.02", "1.1"]); println!("All comparisons passed!"); } ``` -------------------------------- ### compare - Case-Sensitive Natural Ordering Source: https://context7.com/lifthrasiir/rust-natord/llms.txt Compares two strings using natural ordering with case sensitivity. This function handles Unicode whitespace and sorts decimal digit sequences numerically. It returns a standard `Ordering` value. ```APIDOC ## compare ### Description Compares two strings using natural ordering with case sensitivity. This function handles Unicode whitespace (skipping it during comparison) and properly sorts decimal digit sequences numerically. It returns a standard `Ordering` value (`Less`, `Equal`, or `Greater`) suitable for use with Rust's sorting functions. ### Method `compare(a: &str, b: &str) -> Ordering` ### Parameters None ### Request Example ```rust use natord::compare; use std::cmp::Ordering; fn main() { assert_eq!(compare("file1.txt", "file2.txt"), Ordering::Less); assert_eq!(compare("file10.txt", "file2.txt"), Ordering::Greater); let mut files = vec!["rfc2086.txt", "rfc822.txt", "rfc1.txt"]; files.sort_by(|a, b| compare(a, b)); assert_eq!(files, ["rfc1.txt", "rfc822.txt", "rfc2086.txt"]); } ``` ### Response #### Success Response `Ordering` - Returns `Ordering::Less`, `Ordering::Equal`, or `Ordering::Greater`. #### Response Example ```rust use std::cmp::Ordering; // Example return value Ordering::Less ``` ``` -------------------------------- ### Compare Strings with Natural Ordering (Case-Insensitive) Source: https://context7.com/lifthrasiir/rust-natord/llms.txt Compares two strings using natural ordering while ignoring case differences. Uses Unicode case folding for comparison and skips Unicode whitespace. Ideal for case-insensitive sorting of filenames or user-provided data. ```rust use natord::compare_ignore_case; use std::cmp::Ordering; fn main() { // Case-insensitive comparison assert_eq!(compare_ignore_case("File1.txt", "file1.txt"), Ordering::Equal); assert_eq!(compare_ignore_case("ABC", "abc"), Ordering::Equal); // Natural ordering still works with mixed case assert_eq!(compare_ignore_case("File10.txt", "FILE2.txt"), Ordering::Greater); assert_eq!(compare_ignore_case("Item1", "item2"), Ordering::Less); // Sorting files case-insensitively let mut files = vec!["README.md", "file10.txt", "File2.txt", "file1.txt"]; files.sort_by(|a, b| compare_ignore_case(a, b)); assert_eq!(files, ["file1.txt", "File2.txt", "file10.txt", "README.md"]); // Sorting user-generated content let mut items = vec!["Chapter10", "chapter2", "CHAPTER1", "chapter11"]; items.sort_by(|a, b| compare_ignore_case(a, b)); assert_eq!(items, ["CHAPTER1", "chapter2", "Chapter10", "chapter11"]); println!("Case-insensitive comparisons passed!"); } ``` -------------------------------- ### compare_ignore_case - Case-Insensitive Natural Ordering Source: https://context7.com/lifthrasiir/rust-natord/llms.txt Compares two strings using natural ordering while ignoring case differences. It converts characters to lowercase using Unicode case folding before comparison, skipping Unicode whitespace and sorting decimal digits numerically. ```APIDOC ## compare_ignore_case ### Description Compares two strings using natural ordering while ignoring case differences. This function converts characters to lowercase before comparison using Unicode case folding, making it ideal for case-insensitive sorting of filenames or user-provided data. It also skips Unicode whitespace and handles decimal digits numerically. ### Method `compare_ignore_case(a: &str, b: &str) -> Ordering` ### Parameters None ### Request Example ```rust use natord::compare_ignore_case; use std::cmp::Ordering; fn main() { assert_eq!(compare_ignore_case("File1.txt", "file1.txt"), Ordering::Equal); let mut files = vec!["README.md", "file10.txt", "File2.txt", "file1.txt"]; files.sort_by(|a, b| compare_ignore_case(a, b)); assert_eq!(files, ["file1.txt", "File2.txt", "file10.txt", "README.md"]); } ``` ### Response #### Success Response `Ordering` - Returns `Ordering::Less`, `Ordering::Equal`, or `Ordering::Greater`. #### Response Example ```rust use std::cmp::Ordering; // Example return value Ordering::Equal ``` ``` -------------------------------- ### compare_iter Function Source: https://context7.com/lifthrasiir/rust-natord/llms.txt A generic comparison function that accepts iterators and customizable behavior functions. This allows natural ordering to be applied to any sequence type with custom rules for skipping characters, comparing non-digit characters, and converting characters to digits. ```APIDOC ## compare_iter ### Description A generic comparison function that accepts iterators and customizable behavior functions. This allows natural ordering to be applied to any sequence type with custom rules for skipping characters, comparing non-digit characters, and converting characters to digits. Use this when you need specialized comparison logic beyond standard string comparison. ### Method `compare_iter` ### Parameters This function takes iterators and three closure arguments: 1. **Skip Function**: `FnMut(Item) -> bool` - A closure that determines if an item should be skipped during comparison. 2. **Comparison Function**: `FnMut(&Item, &Item) -> Ordering` - A closure for comparing non-digit items. 3. **Digit Conversion Function**: `FnMut(Item) -> Option` - A closure that attempts to convert an item into a numerical value (as `isize`). ### Request Example ```rust use natord::compare_iter; use std::cmp::Ordering; // Custom comparison: only consider alphanumeric characters, ignore punctuation let result = compare_iter( "file-1.txt".chars(), "file-2.txt".chars(), |&c| c.is_whitespace() || c == '-' || c == '.', // skip whitespace, hyphens, dots |&l, &r| l.cmp(&r), // standard char comparison |&c| c.to_digit(10).map(|v| v as isize) // decimal digit conversion ); assert_eq!(result, Ordering::Less); // Case-insensitive custom comparison let left = "Item-10".chars().flat_map(|c| c.to_lowercase()); let right = "ITEM-2".chars().flat_map(|c| c.to_lowercase()); let result = compare_iter( left, right, |&c| c.is_whitespace() || c == '-', |&l, &r| l.cmp(&r), |&c| c.to_digit(10).map(|v| v as isize) ); assert_eq!(result, Ordering::Greater); // 10 > 2 // Hexadecimal digit support let result = compare_iter( "addr0x0f".chars(), "addr0x10".chars(), |&c| c.is_whitespace(), |&l, &r| l.cmp(&r), |&c| c.to_digit(16).map(|v| v as isize) // hexadecimal conversion ); assert_eq!(result, Ordering::Less); // 0x0f (15) < 0x10 (16) // Working with byte slices instead of strings let bytes_left: Vec = b"data100".to_vec(); let bytes_right: Vec = b"data20".to_vec(); let result = compare_iter( bytes_left.iter(), bytes_right.iter(), |&&b| b == b' ', // skip spaces |&&l, &&r| l.cmp(&r), // byte comparison |&&b| if b >= b'0' && b <= b'9' { Some((b - b'0') as isize) } else { None } ); assert_eq!(result, Ordering::Greater); // 100 > 20 println!("Custom iterator comparisons passed!"); ``` ### Response Returns an `Ordering` enum value (`Less`, `Equal`, or `Greater`) indicating the comparison result. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.