Crates.io | byteutils |
lib.rs | byteutils |
version | 0.1.0 |
source | src |
created_at | 2024-11-24 06:05:16.078215 |
updated_at | 2024-11-24 06:05:16.078215 |
description | Rust library that provides a collection of frequently used utility functions for working with bytes, strings, and vectors. It includes common tasks such as converting between strings and byte arrays, manipulating strings, and performing common operations on vectors. |
homepage | |
repository | https://github.com/0xSSOMPW/byteutils |
max_upload_size | |
id | 1458976 |
size | 46,695 |
byteutils
is a small Rust library that provides a collection of frequently used utility functions for working with bytes, strings, and vectors. It includes common tasks such as converting between strings and byte arrays, manipulating strings, and performing common operations on vectors.
Add byteutils
to your Cargo.toml
:
[dependencies]
byteutils = "0.1.0"
// Convert bytes to hex string
let bytes = &[15, 255, 0, 128];
let hex_string = byteutils::bytes_to_hex(bytes);
assert_eq!(byteutils::bytes_to_hex(bytes), "0fff0080");
// Convert hex string to bytes
let bytes = byteutils::hex_to_bytes("0fff0080").unwrap();
assert_eq!(byteutils::hex_to_bytes("0fff0080").unwrap(), vec![15, 255, 0, 128]);
// Convert bytes to string
let bytes = "Hello".as_bytes();
let string = byteutils::bytes_to_string(bytes).unwrap();
assert_eq!(string, "Hello");
// Convert string to bytes
let string = "Hello";
let bytes = byteutils::string_to_bytes(string);
assert_eq!(bytes, vec![72, 101, 108, 108, 111]);
// Convert string to hex
let string = "Hello";
let hex = byteutils::string_to_hex(string);
assert_eq!(hex, "48656c6c6f");
// Convert hex to string
let hex = "48656c6c6f";
let string = byteutils::hex_to_string(hex).unwrap();
assert_eq!(string, "Hello");
// Convert a comma-separated string to a vector of words
let input = "hello, world, , rust ";
let result = byteutils::string::to_array(input);
assert_eq!(result, vec!["hello", "world", "rust"]);
// Escape SQL characters in a string
let input = "O'Connor\\Path";
let escaped = byteutils::string::escape_sql(input);
assert_eq!(escaped, "O''Connor\\\\Path");
// Enclose a string in single quotes
let name = "table_name";
let quoted = byteutils::string::enclose_quotes(name);
assert_eq!(quoted, "'table_name'");
// Check if a word exists in a string
let source = "Hello world! Welcome to Rust.";
assert!(byteutils::string::is_contain_word(source, "world"));
assert!(!byteutils::string::is_contain_word(source, "Rust!"));
// Check if multiple words exist in a string
let source = "The quick brown fox jumps over the lazy dog.";
let words = vec!["quick".to_string(), "slow".to_string(), "fox".to_string()];
assert!(byteutils::string::has_contain_words(source, &words));
let no_match_words = vec!["cat".to_string(), "elephant".to_string()];
assert!(!byteutils::string::has_contain_words(source, &no_match_words));
// Replace placeholders in a string
let template = "Hello {{name}}! Welcome to {{place}}.";
let result = byteutils::string::replace_placeholder(template, "name", "John");
assert_eq!(result, "Hello John! Welcome to {{place}}.");
// Replace multiple placeholders using a HashMap
use std::collections::HashMap;
let mut replacements = HashMap::new();
replacements.insert("name".to_string(), "John".to_string());
replacements.insert("age".to_string(), "30".to_string());
let template = "Hello {{name}}! You are {{age}} years old.";
let result = byteutils::string::replace_multiple_placeholders(template, &replacements);
assert_eq!(result, "Hello John! You are 30 years old.");
// Deduplicate a vector
let mut numbers = vec![1, 2, 3, 2, 4, 1, 5];
byteutils::vec::dedup(&mut numbers);
assert_eq!(numbers, vec![1, 2, 3, 4, 5]);
// Retain elements based on a condition
let mut numbers = vec![1, 2, 3, 4, 5, 6];
byteutils::vec::retain_if(&mut numbers, |&x| x % 2 == 0);
assert_eq!(numbers, vec![2, 4, 6]);
// Reverse a vector in place
let mut vec = vec![1, 2, 3, 4, 5];
byteutils::vec::reverse_in_place(&mut vec);
assert_eq!(vec, vec![5, 4, 3, 2, 1]);
// Split a vector at a given index
let mut vec = vec![1, 2, 3, 4, 5];
let (left, right) = byteutils::vec::split_at_vec(&mut vec, 3);
assert_eq!(left, vec![1, 2, 3]);
assert_eq!(right, vec![4, 5]);
// Get unique elements from a vector
let numbers = vec![1, 2, 2, 3, 1, 4];
let unique = byteutils::vec::get_unique(&numbers);
assert_eq!(unique, vec![1, 2, 3, 4]);
// Get unique elements from a vector of strings
let words = vec!["apple", "banana", "apple", "cherry"];
let unique = byteutils::vec::get_unique(&words);
assert_eq!(unique, vec!["apple", "banana", "cherry"]);
// Handle empty vectors
let empty: Vec<i32> = vec![];
let unique = byteutils::vec::get_unique(&empty);
assert_eq!(unique, Vec::<i32>::new());
This project is licensed under the MIT License - see the LICENSE file for details.