1#![cfg_attr(not(feature = "std"), no_std)]
2
3extern crate alloc;
4
5#[cfg(not(feature = "std"))]
6use alloc::string::String;
7
8#[cfg(feature = "std")]
9use convert_case::{Boundary, Case, Casing};
10
11#[cfg(feature = "std")]
24pub fn camel_to_snake(text: &str) -> String {
25 text.from_case(Case::UpperCamel)
26 .without_boundaries(&[Boundary::UpperDigit, Boundary::LowerDigit])
27 .to_case(Case::Snake)
28}
29
30pub fn event_absolute_position(len: usize, index: i32) -> Option<usize> {
42 if index.is_negative() {
43 let abs_idx = index.wrapping_abs() as usize;
44 if abs_idx > len {
45 return None;
46 }
47 Some(len - abs_idx)
48 } else {
49 if index as usize >= len {
50 return None;
51 }
52 Some(index as usize)
53 }
54}
55
56pub static KEY_DELIMITER: &str = "#";
57
58static TABLE: &[u8] = b"0123456789abcdef";
59
60#[inline]
61fn hex(byte: u8) -> u8 {
62 TABLE[byte as usize]
63}
64
65pub fn hex_to_slice(src: &[u8], dst: &mut [u8]) {
85 for (byte, slots) in src.iter().zip(dst.chunks_exact_mut(2)) {
86 slots[0] = hex((*byte >> 4) & 0xf);
87 slots[1] = hex(*byte & 0xf);
88 }
89}
90
91#[inline]
93pub fn create_key(left: &str, right: &str) -> String {
94 alloc::format!("{}{}{}", left, KEY_DELIMITER, right)
95}
96
97#[cfg(test)]
98mod tests {
99 use crate::{camel_to_snake, event_absolute_position};
100
101 #[test]
102 fn camel_to_snake_works() {
103 assert_eq!("owned_token", camel_to_snake("OwnedToken"));
104 assert_eq!("ownable", camel_to_snake("Ownable"));
105 assert_eq!("erc20", camel_to_snake("Erc20"));
106 assert_eq!(
107 "erc20_implementation",
108 camel_to_snake("Erc20Implementation")
109 );
110 }
111
112 #[test]
113 fn event_absolute_position_works() {
114 assert_eq!(event_absolute_position(0, 1), None);
115 assert_eq!(event_absolute_position(10, 10), None);
116 assert_eq!(event_absolute_position(10, -11), None);
117 assert_eq!(event_absolute_position(10, 0), Some(0));
118 assert_eq!(event_absolute_position(10, 1), Some(1));
119 assert_eq!(event_absolute_position(10, -1), Some(9));
120 assert_eq!(event_absolute_position(10, -2), Some(8));
121 assert_eq!(event_absolute_position(10, -10), Some(0));
122 }
123}