1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*!

# Usage

This crate is [on crates.io](https://crates.io/crates/rustils/) and can be
used by adding `rustils` to your dependencies in your project's `Cargo.toml`.

```toml
[dependencies]
rustils = "0.1.23"
```

and this to your crate root:

```rust
extern crate rustils;
```

# Examples

```
use rustils::parse::byte::ToI8;
use rustils::error::ParseError::InvalidNumber;

let a = -128_i32;
let b = 127_i32;
let c = -129_i32;
let d = 128_i32;

//Rust
assert_eq!(a as i8, -128_i8);
assert_eq!(b as i8, 127_i8);
assert_eq!(c as i8, 127_i8);
assert_eq!(d as i8, -128_i8);

//rustils
assert_eq!(a.to_i8(), -128_i8);
assert_eq!(b.to_i8(), 127_i8);
assert_eq!(c.to_i8_res(), Err(InvalidNumber("-129".to_string())));
assert_eq!(d.to_i8_res(), Err(InvalidNumber("128".to_string())));
```

```
use rustils::string::StringUtils;

let text = "你好。How are you?";

//Rust function
assert_eq!(text.find('好'), Some(3));

//rustils functions
assert_eq!(text.find_char_opt('好'), Some(1));
assert_eq!(text.find_char('好'), 1);
```

```
use rustils::string::StringUtils;

let text1 = &mut String::from("你好。How are you?");

//Rust function
assert_eq!(text1.remove(3), '好');
assert_eq!(text1, "你。How are you?");

let text3 = String::from("你好。How are you?");
let text4 = &mut String::from("你好。How are you?");
let regex = r"[aeiou]+|[好]+";

//rustils functions
assert_eq!(text3.remove_regex(regex), String::from("你。How are you?"));
assert_eq!(text3.remove_all_regex(regex), String::from("你。Hw r y?"));

assert_eq!(true, text4.remove_regex_mut(regex));
assert_eq!(text4, "你。How are you?");

assert_eq!(true, text4.remove_all_regex_mut(regex));
assert_eq!(text4, "你。Hw r y?");
```
*/

extern crate rand;
extern crate regex;
extern crate core;

#[doc(hidden)] pub mod impls;
#[doc(hidden)] pub mod boolean;

/// Array manipulation
pub mod array;
pub mod error;

/// Parsing primitives to others
pub mod parse;
pub mod random;
pub mod sorting;

/// String manipulation
pub mod string;

pub enum RoundingMode { Trunc, Round, Ceil, Floor }

#[derive(Debug)]
enum CharProp { Alpha, AlphaNumeric, AlphaNumericSpace, AlphaSpace, Lower, Numeric, NumericSpace, Upper, Whitespace }

fn char_property(s: &str, prop: CharProp, logic: bool) -> (bool, Vec<bool>) {
    let mut b = logic;

    let mut c = (*s).chars();
    let mut vec = Vec::<bool>::new();

    loop {
        let n = c.next();
        if n == None { break; }
        else {
            let nu = n.unwrap();
            let temp = match prop {
                CharProp::Alpha => nu.is_alphabetic(),
                CharProp::AlphaNumeric => nu.is_alphanumeric(),
                CharProp::AlphaNumericSpace => nu.is_alphanumeric() || nu.is_whitespace(),
                CharProp::AlphaSpace => nu.is_alphabetic() || nu.is_whitespace(),
                CharProp::Lower => nu.is_lowercase(),
                CharProp::Numeric => nu.is_numeric(),
                CharProp::NumericSpace => nu.is_numeric() || nu.is_whitespace(),
                CharProp::Upper => nu.is_uppercase(),
                CharProp::Whitespace => nu.is_whitespace()
            };

            if logic { b &= temp; }
            else { b |= temp; }

            vec.push(temp);
        }
    }

    (b, vec)
}

fn has_char_property(s: &str, prop: CharProp) -> (bool, Vec<bool>){
    char_property(s, prop, false)
}

fn is_char_property(s: &str, prop: CharProp) -> (bool, Vec<bool>) {
    char_property(s, prop, true)
}