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
pub fn has_new_line_occurrences_in_leading_whitespace(text: &str, occurrences: i8) -> bool {
    if occurrences == 0 {
        return has_no_new_lines_in_leading_whitespace(text);
    }

    let mut found_occurrences = 0;
    for c in text.chars() {
        if !c.is_whitespace() {
            return false;
        }
        if c == '\n' {
            found_occurrences += 1;
            if found_occurrences >= occurrences {
                return true;
            }
        }
    }

    return false;
}

pub fn has_no_new_lines_in_leading_whitespace(text: &str) -> bool {
    for c in text.chars() {
        if !c.is_whitespace() {
            return true;
        }
        if c == '\n' {
            return false;
        }
    }

    return true;
}

pub fn has_new_line_occurrences_in_trailing_whitespace(text: &str, occurrences: i8) -> bool {
    if occurrences == 0 {
        return has_no_new_lines_in_trailing_whitespace(text);
    }

    let mut found_occurrences = 0;
    for c in text.chars().rev() {
        if !c.is_whitespace() {
            return false;
        }
        if c == '\n' {
            found_occurrences += 1;
            if found_occurrences >= occurrences {
                return true;
            }
        }
    }

    return false;
}

pub fn has_no_new_lines_in_trailing_whitespace(text: &str) -> bool {
    for c in text.chars().rev() {
        if !c.is_whitespace() {
            return true;
        }
        if c == '\n' {
            return false;
        }
    }

    return true;
}