swc_css_minifier/
util.rs

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
use std::mem::take;

use swc_common::EqIgnoreSpan;

pub(crate) fn dedup<T>(v: &mut Vec<T>)
where
    T: EqIgnoreSpan,
{
    let mut remove_list = Vec::new();

    for (i, i1) in v.iter().enumerate() {
        for (j, j1) in v.iter().enumerate() {
            if i < j && i1.eq_ignore_span(j1) {
                remove_list.push(j);
            }
        }
    }

    // Fast path. We don't face real duplicates in most cases.
    if remove_list.is_empty() {
        return;
    }

    let new = take(v)
        .into_iter()
        .enumerate()
        .filter_map(|(idx, value)| {
            if remove_list.contains(&idx) {
                None
            } else {
                Some(value)
            }
        })
        .collect::<Vec<_>>();

    *v = new;
}