### StringSanitizer::from Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Shows how to create a StringSanitizer from an existing String. ```rust let mut instance = StringSanitizer::from(" HELLO "); ``` -------------------------------- ### String::capacity Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns the capacity of the String in bytes. ```rust let s = String::with_capacity(10); assert!(s.capacity() >= 10); ``` -------------------------------- ### StringSanitizer::to_kebab_case Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates converting the string to kebab case. ```rust let mut instance = StringSanitizer::from("hello world"); instance.to_kebab_case(); assert_eq!(instance.get(), "hello-world"); ``` -------------------------------- ### String::is_empty Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Checks if the String is empty. ```rust let mut v = String::new(); assert!(v.is_empty()); v.push('a'); assert!(!v.is_empty()); ``` -------------------------------- ### StringSanitizer::clamp_max Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates limiting the maximum length of the string. ```rust let mut instance = StringSanitizer::from("hello world"); instance.clamp_max(5); assert_eq!(instance.get(), "hello"); ``` -------------------------------- ### StringSanitizer::get Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Illustrates consuming the StringSanitizer to retrieve the underlying sanitized String. ```rust let mut instance = StringSanitizer::from(" HELLO "); instance.trim(); assert_eq!(instance.get(), "HELLO"); ``` -------------------------------- ### StringSanitizer::to_uppercase Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates converting the string content to uppercase. ```rust let mut instance = StringSanitizer::from(" hello "); instance.to_uppercase(); assert_eq!(instance.get(), " HELLO "); ``` -------------------------------- ### String Parsing Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Demonstrates parsing a string slice into different types using the `parse` method. Includes basic usage, turbofish syntax, and handling parse errors. ```rust let four: u32 = "4".parse().unwrap(); assert_eq!(4, four); ``` ```rust let four = "4".parse::(); assert_eq!(Ok(4), four); ``` ```rust let nope = "j".parse::(); assert!(nope.is_err()); ``` -------------------------------- ### StringSanitizer::cut Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Shows truncating the string to a specified length from the beginning. ```rust let mut instance = StringSanitizer::from("hello world"); instance.cut(5); assert_eq!(instance.get(), "hello"); ``` -------------------------------- ### Check String Prefix Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Verify if a string starts with a specific pattern. Supports string literals and character slices. ```rust let bananas = "bananas"; assert!(bananas.starts_with("bana")); assert!(!bananas.starts_with("nana")); ``` ```rust let bananas = "bananas"; // Note that both of these assert successfully. assert!(bananas.starts_with(&['b', 'a', 'n', 'a'])); assert!(bananas.starts_with(&['a', 'b', 'c', 'd'])); ``` -------------------------------- ### IntSanitizer Usage Example Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/int_sanitizer.rs.html Demonstrates how to create an IntSanitizer instance and use the clamp method to constrain its value within a range. The clamp method ensures the integer stays between the provided minimum and maximum values. ```rust use sanitizer::prelude::*; let mut instance = IntSanitizer::from(5); instance .clamp(9, 15); assert_eq!(instance.get(), 9); ``` -------------------------------- ### String Trimming Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Demonstrates various ways to trim characters from the end of a string using simple patterns, numeric checks, character slices, and closures. ```rust assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar"); ``` ```rust assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar"); ``` ```rust let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_end_matches(x), "12foo1bar"); ``` ```rust assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo"); ``` -------------------------------- ### Basic String Sanitization Examples Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Demonstrates various string transformations like alphanumeric filtering, case conversion, and format changes. These are utility functions for common string cleaning tasks. ```rust string_test!(alphanumeric, "Hello,藏World&&" => "Hello藏World"); ``` ```rust string_test!(to_lowercase, "HELLO" => "hello"); ``` ```rust string_test!(to_uppercase, "hello" => "HELLO"); ``` ```rust string_test!(to_camel_case, "some_string" => "someString"); ``` ```rust string_test!(to_snake_case, "someString" => "some_string"); ``` ```rust string_test!(to_kebab_case, "someString" => "some-string"); ``` ```rust string_test!(to_screaming_kebab_case, "someString" => "SOME-STRING"); ``` ```rust string_test!(to_screaming_snakecase, "someString" => "SOME_STRING"); ``` -------------------------------- ### IntSanitizer Methods Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Provides methods for interacting with the IntSanitizer, such as getting the sanitized value, clamping it, or applying custom sanitization functions. ```APIDOC ## IntSanitizer sanitizer::prelude # Struct IntSanitizer Copy item path Source Search Settings Help ## §Example ``` use sanitizer::prelude::*; let mut instance = IntSanitizer::from(5); instance .clamp(9, 15); assert_eq!(instance.get(), 9); ``` ## Implementations Source ### impl IntSanitizer Source #### pub fn get(self) -> T Consume the struct and return T Source #### pub fn clamp(&mut self, min: T, max: T) -> &mut Self Sets the int equal to the max value if it exceds the provided max value provided in the function argument Source #### pub fn call(&mut self, func: F) -> &mut Self where F: FnOnce(T) -> T, Call a custom function for sanitizing the value of type T ``` -------------------------------- ### String::as_bytes Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns a byte slice of the String's contents. ```rust let s = String::from("hello"); assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes()); ``` -------------------------------- ### StringSanitizer::to_lowercase Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Shows converting the string content to lowercase. ```rust let mut instance = StringSanitizer::from(" HELLO "); instance.to_lowercase(); assert_eq!(instance.get(), " hello "); ``` -------------------------------- ### StringSanitizer::to_camel_case Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Illustrates converting the string to camel case. ```rust let mut instance = StringSanitizer::from("hello world"); instance.to_camel_case(); assert_eq!(instance.get(), "helloWorld"); ``` -------------------------------- ### StringSanitizer::to_screaming_kebab_case Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Illustrates converting the string to screaming kebab case (all caps with hyphens). ```rust let mut instance = StringSanitizer::from("hello world"); instance.to_screaming_kebab_case(); assert_eq!(instance.get(), "HELLO-WORLD"); ``` -------------------------------- ### StringSanitizer::to_snake_case Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Shows converting the string to snake case. ```rust let mut instance = StringSanitizer::from("hello world"); instance.to_snake_case(); assert_eq!(instance.get(), "hello_world"); ``` -------------------------------- ### String Left Trimming Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Illustrates removing prefixes from a string that match a given pattern, including character literals, numeric checks, character slices, and closures. ```rust assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11"); ``` ```rust assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123"); ``` ```rust let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12"); ``` -------------------------------- ### str::is_empty Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Checks if the string slice is empty. ```rust let s = ""; assert!(s.is_empty()); let s = "not empty"; assert!(!s.is_empty()); ``` -------------------------------- ### StringSanitizer::call Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates applying a custom sanitization function to the string. ```rust let mut instance = StringSanitizer::from("hello world"); instance.call(|s| s.replace("world", "Rust")); assert_eq!(instance.get(), "hello Rust"); ``` -------------------------------- ### Example of Sanitizer Derive Macro Usage Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/derive.Sanitizer.html Demonstrates how to use the `#[derive(Sanitizer)]` macro with custom sanitization functions. It shows applying `trim` to a String and a custom function `eight` to a u8, along with a `main` function to test the sanitization. ```rust use sanitizer::prelude::*; #[derive(Sanitizer)] struct User { #[sanitizer(trim)] name: String, #[sanitizer(custom(eight))] acc_no: u8 } fn eight(mut acc_no: u8) -> u8 { if acc_no != 8 { acc_no = 8; } acc_no } fn main() { let mut instance = User { name: String::from("John, Doe "), acc_no: 10 }; instance.sanitize(); assert_eq!(instance.name, "John, Doe"); assert_eq!(instance.acc_no, 8); } ``` -------------------------------- ### Trim start matches Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Removes all matching prefixes from a string slice. The pattern can be a string slice, character, slice of characters, or a closure. ```rust Returns a string slice with all prefixes that match a pattern repeatedly removed. The pattern can be a `&str`, `char`, a slice of `char`s, or a function or closure that determines if a character matches. ``` -------------------------------- ### String::len Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns the length of the String in bytes. Note that this may not correspond to the number of characters. ```rust let a = String::from("foo"); assert_eq!(a.len(), 3); let fancy_f = String::from("ƒoo"); assert_eq!(fancy_f.len(), 4); assert_eq!(fancy_f.chars().count(), 3); ``` -------------------------------- ### StringSanitizer::to_screaming_snakecase Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Shows converting the string to screaming snake case (all caps with underscores). ```rust let mut instance = StringSanitizer::from("hello world"); instance.to_screaming_snakecase(); assert_eq!(instance.get(), "HELLO_WORLD"); ``` -------------------------------- ### StringSanitizer::e164 Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Illustrates converting a phone number string to the E.164 international standard format. ```rust let mut instance = StringSanitizer::from("+1 (555) 123-4567"); instance.e164(); assert_eq!(instance.get(), "+15551234567"); ``` -------------------------------- ### String Right Trimming Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Shows how to remove suffixes from a string based on specified patterns, including character literals, numeric checks, character slices, and closures. ```rust assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar"); ``` ```rust assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar"); ``` ```rust let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar"); ``` ```rust assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo"); ``` -------------------------------- ### String::as_str Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Extracts a string slice containing the entire String. ```rust let s = String::from("foo"); assert_eq!("foo", s.as_str()); ``` -------------------------------- ### Trim End Matches Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates removing characters from the end of a string based on simple patterns, numeric characters, character slices, or closures. ```rust assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar"); assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar"); let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_end_matches(x), "12foo1bar"); ``` ```rust assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo"); ``` -------------------------------- ### Trim Prefix Example (Nightly) Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Removes a prefix from a string slice, always returning a string slice. Useful for method chaining when the prefix might not be present. ```rust #![feature(trim_prefix_suffix)] // Prefix present - removes it assert_eq!("foo:bar".trim_prefix("foo:"), "bar"); assert_eq!("foofoo".trim_prefix("foo"), "foo"); // Prefix absent - returns original string assert_eq!("foo:bar".trim_prefix("bar"), "foo:bar"); // Method chaining example assert_eq!("".trim_prefix('<').trim_suffix('>'), "https://example.com/"); ``` -------------------------------- ### Macro for string sanitization tests Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html A macro to simplify writing tests for StringSanitizer methods, automating the setup, execution, and assertion. ```rust macro_rules! string_test { ( $sanitizer : ident, $from : expr => $to : expr ) => { paste::paste! { #[test] fn [<$sanitizer>]() { let mut sanitizer = StringSanitizer::from($from); sanitizer.$sanitizer(); assert_eq!($to, sanitizer.get()); } } }; } ``` -------------------------------- ### Trim Start Matches Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Removes leading characters that match a given pattern. The pattern can be a string slice, character, slice of characters, or a closure. ```rust assert_eq!("11foo1bar11".trim_start_matches('1'), "foo1bar11"); ``` -------------------------------- ### str::len Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns the length of the string slice in bytes. Note that this may not correspond to the number of characters. ```rust let len = "foo".len(); assert_eq!(3, len); assert_eq!("ƒoo".len(), 4); // fancy f! assert_eq!("ƒoo".chars().count(), 3); ``` -------------------------------- ### str::is_char_boundary Example Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Checks if a given byte index is a valid UTF-8 character boundary. ```rust let s = "Löwe 老虎 Léopard"; assert!(s.is_char_boundary(0)); // start of `老` assert!(s.is_char_boundary(6)); assert!(s.is_char_boundary(s.len())); // second byte of `ö` assert!(!s.is_char_boundary(2)); // third byte of `老` assert!(!s.is_char_boundary(8)); ``` -------------------------------- ### Trim Left Matches Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates removing characters from the beginning of a string based on simple patterns, numeric characters, character slices, or closures. Note that 'left' refers to the byte string's first position. ```rust assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11"); assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123"); let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12"); ``` -------------------------------- ### Trim Suffix Example (Nightly) Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Removes a suffix from a string slice, always returning a string slice. Useful for method chaining when the suffix might not be present. ```rust #![feature(trim_prefix_suffix)] // Suffix present - removes it assert_eq!("bar:foo".trim_suffix(":foo"), "bar"); assert_eq!("foofoo".trim_suffix("foo"), "foo"); // Suffix absent - returns original string assert_eq!("bar:foo".trim_suffix("bar"), "bar:foo"); // Method chaining example assert_eq!("".trim_prefix('<').trim_suffix('>'), "https://example.com/"); ``` -------------------------------- ### Check if string starts with a pattern Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns `true` if the given pattern matches a prefix of this string slice, `false` otherwise. The pattern can be a `&str`, `char`, slice of `char`s, or a function. ```rust let bananas = "bananas"; assert!(bananas.starts_with("bana")); assert!(!bananas.starts_with("apple")); ``` -------------------------------- ### Safely get a subslice of a string Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Use `get` to retrieve a subslice of a string slice, returning `None` if the indices are out of bounds or do not fall on UTF-8 sequence boundaries. This is a non-panicking alternative to indexing. ```rust let v = String::from("🗻∈🌏"); assert_eq!(Some("🗻"), v.get(0..4)); // indices not on UTF-8 sequence boundaries assert!(v.get(1..).is_none()); assert!(v.get(..8).is_none()); // out of bounds assert!(v.get(..42).is_none()); ``` -------------------------------- ### From Implementations for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Demonstrates how to create an IntSanitizer from various integer types using the `From` trait. ```APIDOC ## Trait Implementations Source ### impl From for IntSanitizer Source #### fn from(content: i16) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: i32) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: i64) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: i8) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: isize) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: u16) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: u32) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: u64) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: u8) -> Self Converts to this type from the input type. Source ### impl From for IntSanitizer Source #### fn from(content: usize) -> Self Converts to this type from the input type. ``` -------------------------------- ### From Implementations Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Demonstrates how to create a StringSanitizer instance from different string types. ```APIDOC ## From Implementations ### `impl From for StringSanitizer` Creates a `StringSanitizer` from an owned `String`. ### `impl From<&str> for StringSanitizer` Creates a `StringSanitizer` from a string slice (`&str`). ``` -------------------------------- ### Get the underlying String Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Consumes the StringSanitizer and returns the contained String. ```rust pub fn get(self) -> String { self.0 } ``` -------------------------------- ### IntSanitizer From Implementations Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.IntSanitizer.html Demonstrates how to create an IntSanitizer instance from various integer types using the `From` trait. ```APIDOC ## IntSanitizer From Implementations ### `From` - **Description**: Converts an `i16` into an `IntSanitizer`. - **Signature**: `fn from(content: i16) -> Self` ### `From` - **Description**: Converts an `i32` into an `IntSanitizer`. - **Signature**: `fn from(content: i32) -> Self` ### `From` - **Description**: Converts an `i64` into an `IntSanitizer`. - **Signature**: `fn from(content: i64) -> Self` ### `From` - **Description**: Converts an `i8` into an `IntSanitizer`. - **Signature**: `fn from(content: i8) -> Self` ### `From` - **Description**: Converts an `isize` into an `IntSanitizer`. - **Signature**: `fn from(content: isize) -> Self` ### `From` - **Description**: Converts a `u16` into an `IntSanitizer`. - **Signature**: `fn from(content: u16) -> Self` ### `From` - **Description**: Converts a `u32` into an `IntSanitizer`. - **Signature**: `fn from(content: u32) -> Self` ### `From` - **Description**: Converts a `u64` into an `IntSanitizer`. - **Signature**: `fn from(content: u64) -> Self` ### `From` - **Description**: Converts a `u8` into an `IntSanitizer`. - **Signature**: `fn from(content: u8) -> Self` ### `From` - **Description**: Converts a `usize` into an `IntSanitizer`. - **Signature**: `fn from(content: usize) -> Self` ``` -------------------------------- ### Trim Right Matches Examples Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Demonstrates removing characters from the end of a string based on simple patterns, numeric characters, character slices, or closures. Note that 'right' refers to the byte string's last position. ```rust assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar"); assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar"); let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar"); ``` ```rust assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo"); ``` -------------------------------- ### CloneToUninit for T Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Experimental nightly-only API for cloning into uninitialized memory. ```APIDOC ## unsafe fn clone_to_uninit(&self, dest: *mut u8) ### Description Performs copy-assignment from `self` to `dest`. ### Method `clone_to_uninit` ``` -------------------------------- ### Initialize and Sanitize String Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Demonstrates creating a StringSanitizer instance and applying multiple sanitization methods like trim and to_lowercase. ```rust use sanitizer::prelude::*; let mut instance = StringSanitizer::from(" HELLO "); instance .trim() .to_lowercase(); assert_eq!(instance.get(), "hello"); ``` -------------------------------- ### Substring Access Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Methods for safely getting string slices (sub-slices) by index. ```APIDOC ## get ### Description Returns a subslice of `str` if the indices are valid and lie on UTF-8 character boundaries. This is a non-panicking alternative to indexing. ### Parameters - **i** (I: SliceIndex) - The range or index to get the subslice. ### Returns - Option<&>::Output> - An `Option` containing the subslice if successful, or `None` if the indices are invalid or out of bounds. ### Examples ```rust let v = String::from("🗻∈🌏"); assert_eq!(Some("🗻"), v.get(0..4)); // indices not on UTF-8 sequence boundaries assert!(v.get(1..).is_none()); assert!(v.get(..8).is_none()); // out of bounds assert!(v.get(..42).is_none()); ``` ``` ```APIDOC ## get_mut ### Description Returns a mutable subslice of `str` if the indices are valid and lie on UTF-8 character boundaries. This is a non-panicking alternative to mutable indexing. ### Parameters - **i** (I: SliceIndex) - The range or index to get the mutable subslice. ### Returns - Option<&mut >::Output> - An `Option` containing the mutable subslice if successful, or `None` if the indices are invalid or out of bounds. ### Examples ```rust let mut v = String::from("hello"); // correct length assert!(v.get_mut(0..5).is_some()); // out of bounds assert!(v.get_mut(..42).is_none()); assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v)); assert_eq!("hello", v); { let s = v.get_mut(0..2); let s = s.map(|s| { s.make_ascii_uppercase(); &*s }); assert_eq!(Some("HE"), s); } assert_eq!("HEllo", v); ``` ``` -------------------------------- ### Create a new StringSanitizer instance Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Initializes a new StringSanitizer with the provided String content. ```rust pub fn new(content: String) -> Self { Self(content) } ``` -------------------------------- ### From for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Converts a u64 value into an IntSanitizer. ```rust fn from(content: u64) -> Self ``` -------------------------------- ### get Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Safely retrieves a subslice of the string, returning `None` if the indices are invalid or out of bounds. ```APIDOC ## get ### Description Returns a subslice of `str`. This is the non-panicking alternative to indexing the `str`. Returns `None` whenever equivalent indexing operation would panic. ### Method `get(&self, i: I) -> Option<&>::Output>` where I: SliceIndex ### Parameters - **i** (I: SliceIndex) - The range or index to slice. ### Returns - **Option<&>::Output>** - An `Option` containing the subslice if valid, otherwise `None`. ### Example ```rust let v = String::from("🗻∈🌏"); assert_eq!(Some("🗻"), v.get(0..4)); // indices not on UTF-8 sequence boundaries assert!(v.get(1..).is_none()); assert!(v.get(..8).is_none()); // out of bounds assert!(v.get(..42).is_none()); ``` ``` -------------------------------- ### as_ptr Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Obtains a raw pointer to the beginning of the string slice's byte representation. ```APIDOC ## as_ptr ### Description Converts a string slice to a raw pointer. As string slices are a slice of bytes, the raw pointer points to a `u8`. This pointer will be pointing to the first byte of the string slice. The caller must ensure that the returned pointer is never written to. If you need to mutate the contents of the string slice, use `as_mut_ptr`. ### Method `as_ptr(&self) -> *const u8` ### Returns - ***const u8** - A raw pointer to the first byte of the string slice. ### Example ```rust let s = "Hello"; let ptr = s.as_ptr(); ``` ``` -------------------------------- ### starts_with Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns `true` if the given pattern matches a prefix of this string slice, and `false` otherwise. The pattern can be a `&str`, `char`, a slice of `char`s, or a function or closure that determines if a character matches. ```APIDOC ## pub fn starts_with

(&self, pat: P) -> bool where P: Pattern, ### Description Returns `true` if the given pattern matches a prefix of this string slice. Returns `false` if it does not. The pattern can be a `&str`, in which case this function will return true if the `&str` is a prefix of this string slice. The pattern can also be a `char`, a slice of `char`s, or a function or closure that determines if a character matches. These will only be checked against the first character of this string slice. Look at the second example below regarding behavior for slices of `char`s. ``` -------------------------------- ### PartialEq Implementations Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Enables comparison of StringSanitizer instances with strings. ```APIDOC ## PartialEq Implementations ### `impl PartialEq for StringSanitizer` Compares a `StringSanitizer` with a string slice (`&str`). ### `impl PartialEq<&str> for StringSanitizer` Compares a `StringSanitizer` with a reference to a string slice (`&&str`). ### `impl PartialEq for StringSanitizer` Compares a `StringSanitizer` with an owned `String`. ``` -------------------------------- ### From for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Converts an i64 value into an IntSanitizer. ```rust fn from(content: i64) -> Self ``` -------------------------------- ### Implement From for StringSanitizer Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Enables creating a StringSanitizer directly from a String using the `from` method. ```rust impl From for StringSanitizer { fn from(content: String) -> Self { Self::new(content) } } ``` -------------------------------- ### Convert string to uppercase Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Use `to_uppercase` to get the uppercase equivalent of a string. This handles Unicode case mapping and can result in character expansion. ```rust let s = "hello"; assert_eq!("HELLO", s.to_uppercase()); ``` ```rust let new_year = "农历新年"; assert_eq!(new_year, new_year.to_uppercase()); ``` ```rust let s = "tschüß"; assert_eq!("TSCHÜSS", s.to_uppercase()); ``` -------------------------------- ### From for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Converts a usize value into an IntSanitizer. ```rust fn from(content: usize) -> Self ``` -------------------------------- ### match_indices Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns an iterator over the disjoint matches of a pattern within this string slice, along with the starting index of each match. Iteration is from the beginning of the string. ```APIDOC ## pub fn match_indices

(&self, pat: P) -> MatchIndices<'_, P> ### Description Returns an iterator over the disjoint matches of a pattern within this string slice as well as the index that the match starts at. ### Parameters - `pat`: The pattern to search for. Can be a `&str`, `char`, a slice of `char`s, or a function/closure that determines if a character matches. ### Returns A `MatchIndices` iterator yielding tuples of `(index, matched_slice)` for each disjoint match. ### Iterator Behavior The returned iterator will be a `DoubleEndedIterator` if the pattern allows a reverse search and forward/reverse search yields the same elements. ### Examples ```rust let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect(); assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); let v: Vec<_> = "1abcabc2".match_indices("abc").collect(); assert_eq!(v, [(1, "abc"), (4, "abc")]); let v: Vec<_> = "ababa".match_indices("aba").collect(); assert_eq!(v, [(0, "aba")]); // only the first `aba` ``` ``` -------------------------------- ### Get raw pointer to string slice Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Use `as_ptr` to obtain a raw pointer to the first byte of a string slice. This pointer must not be written to. ```rust let s = "Hello"; let ptr = s.as_ptr(); ``` -------------------------------- ### Trim start matches from a string Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Removes characters from the beginning of a string that match a given pattern. The pattern can be a character, a numeric check, or a slice of characters. ```rust assert_eq!("11foo1bar11".trim_start_matches('1'), "foo1bar11"); ``` ```rust assert_eq!("123foo1bar123".trim_start_matches(char::is_numeric), "foo1bar123"); ``` ```rust let x: &[_] = &['1', '2']; assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12"); ``` -------------------------------- ### rsplitn Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns an iterator over substrings separated by a pattern, starting from the end of the string, limited to at most `n` items. The last item contains the remainder of the string. ```APIDOC ## pub fn rsplitn

(&self, n: usize, pat: P) -> RSplitN<'_, P> ### Description Returns an iterator over substrings of this string slice, separated by a pattern, starting from the end of the string, restricted to returning at most `n` items. If `n` substrings are returned, the last substring (the `n`th substring) will contain the remainder of the string. The pattern can be a `&str`, `char`, a slice of `char`s, or a function or closure that determines if a character matches. ### Iterator behavior The returned iterator will not be double ended, because it is not efficient to support. For splitting from the front, the `splitn` method can be used. ### Examples Simple patterns: ``` let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect(); assert_eq!(v, ["lamb", "little", "Mary had a"]); let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect(); assert_eq!(v, ["leopard", "tiger", "lionX"]); let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect(); assert_eq!(v, ["leopard", "lion::tiger"]); ``` A more complex pattern, using a closure: ``` let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect(); assert_eq!(v, ["ghi", "abc1def"]); ``` ``` -------------------------------- ### Any for T Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Provides runtime type information for any type T. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ``` -------------------------------- ### Get Unchecked String Subslice Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Returns an unchecked subslice of `str`. Callers must ensure indices are within bounds and lie on UTF-8 sequence boundaries. ```rust let s = "hello"; let sub = unsafe { s.get_unchecked(0..2) }; assert_eq!("he", sub); ``` -------------------------------- ### from Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Converts an owned String into a StringSanitizer. ```APIDOC ## from(content: String) -> Self ### Description Converts an owned String into a StringSanitizer. ### Parameters - `content` (String): The String to convert. ### Returns - `Self`: A new StringSanitizer instance. ``` -------------------------------- ### From for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Converts a u32 value into an IntSanitizer. ```rust fn from(content: u32) -> Self ``` -------------------------------- ### Splitting a String Slice at an Index Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Shows how to divide a string slice into two at a specified byte index. The index must be on a UTF-8 code point boundary. Panics if the index is invalid. ```rust let s = "Per Martin-Löf"; let (first, last) = s.split_at(3); assert_eq!("Per", first); assert_eq!(" Martin-Löf", last); ``` -------------------------------- ### From for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Converts an i32 value into an IntSanitizer. ```rust fn from(content: i32) -> Self ``` -------------------------------- ### Sanitize Vec Implementation Source: https://docs.rs/sanitizer/1.0.0/sanitizer/trait.Sanitizer.html Shows how to sanitize all elements within a Vec. The `sanitize` method is applied to each item in the vector. This example uses a custom `MyValue` type. ```rust use sanitizer::Sanitizer; #[derive(Debug, PartialEq)] struct MyValue(i32); impl Sanitizer for MyValue { /// If the inner value is `0`, change it to `1`. fn sanitize(&mut self) { if self.0 == 0 { self.0 = 1; } } } let mut values = vec![MyValue(0), MyValue(2)]; values.sanitize(); assert_eq!(values, vec![MyValue(1), MyValue(2)]); ``` -------------------------------- ### Convert string to lowercase Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Use `to_lowercase` to get the lowercase equivalent of a string. This handles Unicode case mapping, including special cases like the Greek sigma. ```rust let s = "HELLO"; assert_eq!("hello", s.to_lowercase()); ``` ```rust let sigma = "Σ"; assert_eq!("σ", sigma.to_lowercase()); // but at the end of a word, it's ς, not σ: let odysseus = "ὈΔΥΣΣΕΎΣ"; assert_eq!("ὀδυσσεύς", odysseus.to_lowercase()); ``` ```rust let new_year = "农历新年"; assert_eq!(new_year, new_year.to_lowercase()); ``` -------------------------------- ### IntSanitizer::new Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/int_sanitizer.rs.html Creates a new instance of the IntSanitizer struct from the given integer value. ```APIDOC /// Make a new instance of the struct from the given T pub(crate) fn new(int: T) -> Self { Self(int) } ``` -------------------------------- ### From for IntSanitizer Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.IntSanitizer.html Converts an isize value into an IntSanitizer. ```rust fn from(content: isize) -> Self ``` -------------------------------- ### Find Match Indices Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Returns an iterator over the disjoint matches of a pattern within a string, along with their starting indices. Overlapping matches are handled by returning only the first occurrence. ```rust let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect(); assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); ``` ```rust let v: Vec<_> = "1abcabc2".match_indices("abc").collect(); assert_eq!(v, [(1, "abc"), (4, "abc")]); ``` ```rust let v: Vec<_> = "ababa".match_indices("aba").collect(); // only the first `aba` assert_eq!(v, [(0, "aba")]); ``` -------------------------------- ### Convert String to ASCII Lowercase Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Converts a string to its ASCII lowercase equivalent in-place. Non-ASCII letters remain unchanged. Use `to_ascii_lowercase()` to get a new lowercased string. ```rust let mut s = String::from("GRÜßE, JÜRGEN ❤"); s.make_ascii_lowercase(); assert_eq!("grÜße, jÜrgen ❤", s); ``` -------------------------------- ### IntSanitizer Basic Clamp Min Test Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/int_sanitizer.rs.html Tests the clamp method of IntSanitizer to ensure it correctly caps the minimum value when the input is below the specified minimum. The value should be adjusted to the provided minimum. ```rust let int: u8 = 50; let mut instance = IntSanitizer::from(int); instance.clamp(99, 101); assert_eq!(99, instance.get()); ``` -------------------------------- ### Implement PartialEq<&str> for StringSanitizer Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Enables comparison of StringSanitizer with references to string slices. ```rust impl PartialEq<&str> for StringSanitizer { fn eq(&self, rhs: &&str) -> bool { self.0 == *rhs } } ``` -------------------------------- ### Unsafe String Slicing with get_unchecked Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Demonstrates how to get an unchecked subslice of a string using byte indices. Ensure indices are valid and on UTF-8 boundaries to avoid memory unsafety. ```rust let v = "🗻∈🌏"; unsafe { assert_eq!("🗻", v.get_unchecked(0..4)); assert_eq!("∈", v.get_unchecked(4..7)); assert_eq!("🌏", v.get_unchecked(7..11)); } ``` -------------------------------- ### Sanitize Option Implementation Source: https://docs.rs/sanitizer/1.0.0/sanitizer/trait.Sanitizer.html Demonstrates how to sanitize a value wrapped in an Option. The `sanitize` method is called on the inner value if it exists. This example shows sanitizing a custom `MyValue` type. ```rust use sanitizer::Sanitizer; #[derive(Debug, PartialEq)] struct MyValue(i32); impl Sanitizer for MyValue { /// If the inner value is `0`, change it to `1`. fn sanitize(&mut self) { if self.0 == 0 { self.0 = 1; } } } let mut wrapped_value = Some(MyValue(0)); wrapped_value.sanitize(); assert_eq!(wrapped_value, Some(MyValue(1))); ``` -------------------------------- ### strip_prefix Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Removes a prefix from a string slice if it exists. Returns `Some(&str)` with the prefix removed, or `None` if the string does not start with the prefix. The pattern can be a `&str`, `char`, a slice of `char`s, or a function. ```APIDOC ## strip_prefix

(&self, prefix: P) -> Option<&str> ### Description Returns a string slice with the prefix removed. If the string starts with the pattern `prefix`, returns the substring after the prefix, wrapped in `Some`. Unlike `trim_start_matches`, this method removes the prefix exactly once. If the string does not start with `prefix`, returns `None`. ### Parameters #### Path Parameters - **prefix** (P: Pattern) - Required - The pattern to remove from the beginning of the string. ### Response #### Success Response (Some) - **&str** - The substring after the prefix. #### Failure Response (None) - **None** - If the string does not start with the prefix. ### Request Example ```rust assert_eq!("foo:bar".strip_prefix("foo:"), Some("bar")); assert_eq!("foo:bar".strip_prefix("bar"), None); assert_eq!("foofoo".strip_prefix("foo"), Some("foo")); ``` ``` -------------------------------- ### IntSanitizer Methods Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.IntSanitizer.html This section details the methods available for the IntSanitizer struct, allowing users to manipulate and retrieve the sanitized integer value. ```APIDOC ## IntSanitizer Methods ### `get()` - **Description**: Consumes the struct and returns the inner value of type T. - **Signature**: `pub fn get(self) -> T` ### `clamp()` - **Description**: Sets the integer to the maximum value if it exceeds the provided maximum value. Returns a mutable reference to the IntSanitizer instance. - **Signature**: `pub fn clamp(&mut self, min: T, max: T) -> &mut Self` ### `call()` - **Description**: Applies a custom sanitization function to the integer value. Returns a mutable reference to the IntSanitizer instance. - **Signature**: `pub fn call(&mut self, func: F) -> &mut Self where F: FnOnce(T) -> T` ``` -------------------------------- ### Implement PartialEq for StringSanitizer Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Enables comparison of StringSanitizer with String objects. ```rust impl PartialEq for StringSanitizer { fn eq(&self, rhs: &String) -> bool { self.0 == rhs.as_str() } } ``` -------------------------------- ### Get Mutable Raw Pointer to String Bytes Source: https://docs.rs/sanitizer/1.0.0/sanitizer/prelude/struct.StringSanitizer.html Converts a mutable string slice to a raw pointer to its first byte. The caller must ensure the string remains valid UTF-8 after modifications. ```rust let mut s = String::from("hello"); let bytes = unsafe { s.as_bytes_mut() }; assert_eq!(b"hello", bytes); ``` -------------------------------- ### split_at Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Divides one string slice into two at a specified byte offset. The offset must be on a UTF-8 code point boundary. Returns two slices: from the start to the offset, and from the offset to the end. ```APIDOC ## split_at(&self, mid: usize) -> (&str, &str) ### Description Divides one string slice into two at an index. The argument, `mid`, should be a byte offset from the start of the string. It must also be on the boundary of a UTF-8 code point. The two slices returned go from the start of the string slice to `mid`, and from `mid` to the end of the string slice. To get mutable string slices instead, see the `split_at_mut` method. ### Panics Panics if `mid` is not on a UTF-8 code point boundary, or if it is past the end of the last code point of the string slice. For a non-panicking alternative see `split_at_checked`. ### Examples ```rust let s = "Per Martin-Löf"; let (first, last) = s.split_at(3); assert_eq!("Per", first); assert_eq!(" Martin-Löf", last); ``` ``` -------------------------------- ### Implement From<&str> for StringSanitizer Source: https://docs.rs/sanitizer/1.0.0/src/sanitizer/string_sanitizer.rs.html Enables creating a StringSanitizer directly from a string slice using the `from` method. ```rust impl From<&str> for StringSanitizer { fn from(content: &str) -> Self { Self::new(content.to_owned()) } } ``` -------------------------------- ### Get mutable raw pointer to string slice Source: https://docs.rs/sanitizer/1.0.0/sanitizer/struct.StringSanitizer.html Use `as_mut_ptr` to obtain a mutable raw pointer to the first byte of a string slice. The caller must ensure modifications maintain UTF-8 validity. ```rust let mut s = String::from("hello"); // The pointer can be used here, but it's not shown in this example. ```