summaryrefslogtreecommitdiffstats
path: root/vendor/minifier/src/json/json_minifier.rs
blob: ad9fae2ce66b4a722a892f871c70f96915823e87 (plain)
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
// Take a look at the license at the top of the repository in the LICENSE file.

#[derive(Debug, Clone)]
pub struct JsonMinifier {
    pub is_string: bool,
    pub escaped_quotation: u8,
}

impl Default for JsonMinifier {
    fn default() -> Self {
        Self::new()
    }
}

impl JsonMinifier {
    pub fn new() -> Self {
        JsonMinifier {
            is_string: false,
            escaped_quotation: 0,
        }
    }
}

#[inline]
pub fn keep_element(minifier: &mut JsonMinifier, item1: &char, item2: Option<&char>) -> bool {
    let remove_element =
        item1.is_ascii_control() || is_whitespace_outside_string(minifier, item1, item2);
    !remove_element
}

#[inline]
fn is_whitespace_outside_string(
    minifier: &mut JsonMinifier,
    item1: &char,
    item2: Option<&char>,
) -> bool {
    if !minifier.is_string && item1.eq(&'"') {
        minifier.is_string = true;
    } else if minifier.is_string {
        if item1.eq(&'\\') && item2.eq(&Some(&'"')) {
            minifier.escaped_quotation = 4;
        }
        if minifier.escaped_quotation > 0 {
            minifier.escaped_quotation -= 1;
        } else if item1.eq(&'"') {
            minifier.is_string = false;
        }
    }
    !minifier.is_string && item1.is_whitespace()
}