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
use std::io::stdin;

/// Gets the first char of a string, and returns it as string.
///
/// ```
/// let hello = String::from("Hello");
/// let first = yarns::head(hello);
/// //first = (String)"H"
/// ```

pub fn head(word: &String) -> String {
    word.chars().take(1).collect()
}

/// Returns a new string starting at the index provided in the second argument, going the length provided as the third argument.
///
/// ```
/// let hello = String::from("Hello");
/// let sub= yarns::substr(&hello, 1, 3);
/// //sub = (String) "ell"
/// ```

pub fn substr(word: &String, start:usize, length:usize) -> String {
    let res: String = word.chars().skip(start).take(length).collect();
    res
}

/// Concatenates two strings.
///
/// ```
/// let hello = String::from("Hello");
/// let world = String::from("World");
/// let hw = yarns.concat(&hello, &world);
/// //hw = (String)"HelloWorld"

pub fn concat(str1: &String, str2: &String) -> String {
    format!("{}{}", str1, str2)
}

/// Returns wether a given String contains the given Substring.
///
/// ```
/// let hello = String::from("Hello");
/// let ell = String::from("ell");
/// let has_it = yarns.contains(&hello, &ell);
/// //has_it = (bool) true

pub fn contains(main: &String, sub: &String) -> bool {
    main[..].contains(&sub[..])
}

/// Shortcut for simple binding of user input to an immutable String.
pub fn read() -> String {
    let mut temp = String::new();
    stdin().read_line(& mut temp).expect("failed to read user input");
    temp
}

#[cfg(test)]
mod tests {
    #[test]
    fn head_hello_h() {
        assert_eq!("h", ::head(&"hello".to_string()));
    }

    #[test]
    fn substr_hello_1_3() {
        let val = String::from("Hello");
        assert_eq!("ell", ::substr(&val, 1, 3));
    }

    #[test]
    fn concat_hello_goodbye() {
        let val1 = String::from("Hello");
        let val2 = String::from("Goodbye");
        assert_eq!("HelloGoodbye", ::concat(&val1, &val2));
    }

    #[test]
    fn contains_hello_hell() {
        let val1 = String::from("hello");
        let val2 = String::from("hell");
        assert_eq!(true, ::contains(&val1, &val2));
    }
}