rustils/lib.rs
1/*!
2
3# Usage
4
5This crate is [on crates.io](https://crates.io/crates/rustils/) and can be
6used by adding `rustils` to your dependencies in your project's `Cargo.toml`.
7
8```toml
9[dependencies]
10rustils = "0.1.23"
11```
12
13and this to your crate root:
14
15```rust
16extern crate rustils;
17```
18
19# Examples
20
21```
22use rustils::parse::byte::ToI8;
23use rustils::error::ParseError::InvalidNumber;
24
25let a = -128_i32;
26let b = 127_i32;
27let c = -129_i32;
28let d = 128_i32;
29
30//Rust
31assert_eq!(a as i8, -128_i8);
32assert_eq!(b as i8, 127_i8);
33assert_eq!(c as i8, 127_i8);
34assert_eq!(d as i8, -128_i8);
35
36//rustils
37assert_eq!(a.to_i8(), -128_i8);
38assert_eq!(b.to_i8(), 127_i8);
39assert_eq!(c.to_i8_res(), Err(InvalidNumber("-129".to_string())));
40assert_eq!(d.to_i8_res(), Err(InvalidNumber("128".to_string())));
41```
42
43```
44use rustils::string::StringUtils;
45
46let text = "你好。How are you?";
47
48//Rust function
49assert_eq!(text.find('好'), Some(3));
50
51//rustils functions
52assert_eq!(text.find_char_opt('好'), Some(1));
53assert_eq!(text.find_char('好'), 1);
54```
55
56```
57use rustils::string::StringUtils;
58
59let text1 = &mut String::from("你好。How are you?");
60
61//Rust function
62assert_eq!(text1.remove(3), '好');
63assert_eq!(text1, "你。How are you?");
64
65let text3 = String::from("你好。How are you?");
66let text4 = &mut String::from("你好。How are you?");
67let regex = r"[aeiou]+|[好]+";
68
69//rustils functions
70assert_eq!(text3.remove_regex(regex), String::from("你。How are you?"));
71assert_eq!(text3.remove_all_regex(regex), String::from("你。Hw r y?"));
72
73assert_eq!(true, text4.remove_regex_mut(regex));
74assert_eq!(text4, "你。How are you?");
75
76assert_eq!(true, text4.remove_all_regex_mut(regex));
77assert_eq!(text4, "你。Hw r y?");
78```
79*/
80
81extern crate rand;
82extern crate regex;
83extern crate core;
84
85#[doc(hidden)] pub mod impls;
86#[doc(hidden)] pub mod boolean;
87
88/// Array manipulation
89pub mod array;
90pub mod error;
91
92/// Parsing primitives to others
93pub mod parse;
94pub mod random;
95pub mod sorting;
96
97/// String manipulation
98pub mod string;
99
100pub enum RoundingMode { Trunc, Round, Ceil, Floor }
101
102#[derive(Debug)]
103enum CharProp { Alpha, AlphaNumeric, AlphaNumericSpace, AlphaSpace, Lower, Numeric, NumericSpace, Upper, Whitespace }
104
105fn char_property(s: &str, prop: CharProp, logic: bool) -> (bool, Vec<bool>) {
106 let mut b = logic;
107
108 let mut c = (*s).chars();
109 let mut vec = Vec::<bool>::new();
110
111 loop {
112 let n = c.next();
113 if n == None { break; }
114 else {
115 let nu = n.unwrap();
116 let temp = match prop {
117 CharProp::Alpha => nu.is_alphabetic(),
118 CharProp::AlphaNumeric => nu.is_alphanumeric(),
119 CharProp::AlphaNumericSpace => nu.is_alphanumeric() || nu.is_whitespace(),
120 CharProp::AlphaSpace => nu.is_alphabetic() || nu.is_whitespace(),
121 CharProp::Lower => nu.is_lowercase(),
122 CharProp::Numeric => nu.is_numeric(),
123 CharProp::NumericSpace => nu.is_numeric() || nu.is_whitespace(),
124 CharProp::Upper => nu.is_uppercase(),
125 CharProp::Whitespace => nu.is_whitespace()
126 };
127
128 if logic { b &= temp; }
129 else { b |= temp; }
130
131 vec.push(temp);
132 }
133 }
134
135 (b, vec)
136}
137
138fn has_char_property(s: &str, prop: CharProp) -> (bool, Vec<bool>){
139 char_property(s, prop, false)
140}
141
142fn is_char_property(s: &str, prop: CharProp) -> (bool, Vec<bool>) {
143 char_property(s, prop, true)
144}