logo
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::fmt;

/// Utility mixins for Strings. Designed to be imported with 'using'.
pub trait StringExtensions {
    /// Gets the extension of a file name, or None if there is no extension
    fn file_extension(&self) -> Option<String>;

    /// Returns a file name without its extension
    fn remove_file_extension(&self) -> String;

    /// Gets the extension of a full path or URL, with special handling for '/' and '?' characters.
    /// Returns None if there is no extension
    fn url_extension(&self) -> Option<String>;

    /// Joins two strings with a path separator
    fn join_path(&self, relative: String) -> String;

    fn hash_code<S: Into<String>>(&self, str: S) -> u32;

    /// Substitute all "{n}" tokens with the corresponding values.
    ///
    // ```
    // "{1} sat on a {0}".substitute(["wall", "Humpty Dumpty"]);
    // // returns "Humpty Dumpty sat on a wall"
    // ```
    //
    fn substitute(&self, values: Vec<String>) -> String;
}

impl StringExtensions for String {
    fn file_extension(&self) -> Option<String> {
        if let Some(dot) = self.rfind(".") {
            Some(self[dot + 1..].to_string())
        } else {
            None
        }
    }

    fn remove_file_extension(&self) -> String {
        if let Some(dot) = self.rfind(".") {
            self[..dot].to_string()
        } else {
            self.clone()
        }
    }

    fn url_extension(&self) -> Option<String> {
        let mut out = self.clone();
        let question = self.rfind("?");
        if let Some(question) = self.rfind("?") {
            out = out[..question].to_string();
        }

        if let Some(slash) = self.rfind("/") {
            out = out[slash + 1..].to_string();
        }

        out.file_extension()
    }

    fn join_path(&self, relative: String) -> String {
        const SEP: char = '/'; // std::path::MAIN_SEPARATOR
        let mut out = self.clone();
        if let Some(last) = self.chars().last() {
            if last != SEP {
                out.push(SEP)
            }
        }

        format!("{}{}", out, relative)
    }

    fn hash_code<S: Into<String>>(&self, str: S) -> u32 {
        let mut code = 0;
        let str: String = str.into();
        if !str.is_empty() {
            let chars = str.chars(); //.nth(i).unwrap()
            for ch in str.chars() {
                code = 31 * code + ch.to_digit(10).unwrap();
            }
        }

        code
    }

    fn substitute(&self, values: Vec<String>) -> String {
        // FIXME -> If your {0} replacement has a {1} in it, then that'll get replaced next
        // iteration
        let mut out = String::from("");
        for ii in 0..values.len() {
            out = self.replace(format!("{{{}}}", ii).as_str(), values[ii].as_str());
        }

        out
    }
}

// TODO: should remade it
impl StringExtensions for str {
    fn file_extension(&self) -> Option<String> {
        if let Some(dot) = self.rfind(".") {
            Some(self[dot + 1..].to_string())
        } else {
            None
        }
    }

    fn remove_file_extension(&self) -> String {
        if let Some(dot) = self.rfind(".") {
            self[..dot].to_string()
        } else {
            self.to_owned()
        }
    }

    // FIXME: what did it mean. i need tests for this function
    fn url_extension(&self) -> Option<String> {
        let mut out = self.to_string();

        if let Some(question) = out.rfind("?") {
            out = out[..question].to_string();
        }

        if let Some(slash) = out.rfind("/") {
            out = out[slash + 1..].to_string()
        }

        out.file_extension()
    }

    fn join_path(&self, relative: String) -> String {
        const SEP: char = '/'; // std::path::MAIN_SEPARATOR
        let mut out = self.to_string();
        if let Some(last) = self.chars().last() {
            if last != SEP {
                out.push(SEP)
            }
        }

        format!("{}{}", out, relative)
    }

    fn hash_code<S: Into<String>>(&self, str: S) -> u32 {
        let mut code = 0;
        let str: String = str.into();
        if !str.is_empty() {
            let chars = str.chars(); //.nth(i).unwrap()
            for ch in str.chars() {
                code = 31 * code + ch.to_digit(10).unwrap();
            }
        }

        code
    }

    fn substitute(&self, values: Vec<String>) -> String {
        // FIXME -> If your {0} replacement has a {1} in it, then that'll get replaced next
        // iteration
        let mut out = String::from("");
        for ii in 0..values.len() {
            out = out.replace(format!("{{{}}}", ii).as_str(), values[ii].as_str());
        }

        out
    }
}