pactum_annotations/
norms.rs1pub fn trim(value: &mut String) {
4 let trimmed = value.trim().to_string();
5 *value = trimmed;
6}
7
8pub fn lowercase(value: &mut String) {
9 *value = value.to_lowercase();
10}
11
12pub fn uppercase(value: &mut String) {
13 *value = value.to_uppercase();
14}
15
16pub fn strip_whitespace(value: &mut String) {
17 value.retain(|ch| !ch.is_whitespace());
18}
19
20pub fn clamp_i64(value: &mut i64, min: i64, max: i64) {
21 if *value < min {
22 *value = min;
23 } else if *value > max {
24 *value = max;
25 }
26}
27
28pub fn clamp_f64(value: &mut f64, min: f64, max: f64) {
29 if *value < min {
30 *value = min;
31 } else if *value > max {
32 *value = max;
33 }
34}
35
36pub trait ClampNum {
38 fn clamp_in_place(&mut self, min: f64, max: f64);
39}
40
41macro_rules! impl_clamp_int {
42 ($($ty:ty),*) => {
43 $(
44 impl ClampNum for $ty {
45 fn clamp_in_place(&mut self, min: f64, max: f64) {
46 let lo = min.ceil().clamp(<$ty>::MIN as f64, <$ty>::MAX as f64) as $ty;
47 let hi = max.floor().clamp(<$ty>::MIN as f64, <$ty>::MAX as f64) as $ty;
48 if *self < lo {
49 *self = lo;
50 } else if *self > hi {
51 *self = hi;
52 }
53 }
54 }
55 )*
56 };
57}
58
59impl_clamp_int!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
60
61impl ClampNum for f32 {
62 fn clamp_in_place(&mut self, min: f64, max: f64) {
63 if !self.is_finite() {
64 return;
65 }
66 let lo = min as f32;
67 let hi = max as f32;
68 if *self < lo {
69 *self = lo;
70 } else if *self > hi {
71 *self = hi;
72 }
73 }
74}
75
76impl ClampNum for f64 {
77 fn clamp_in_place(&mut self, min: f64, max: f64) {
78 if !self.is_finite() {
79 return;
80 }
81 clamp_f64(self, min, max);
82 }
83}