spdx 0.13.4

Helper crate for SPDX expressions
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::{borrow::Cow, collections::HashMap, sync::LazyLock};

use regex::{Regex, Replacer};
use unicode_normalization::UnicodeNormalization;

type PreprocFn = dyn Fn(Cow<'_, str>) -> Cow<'_, str>;

trait CowRegex {
    fn replace_all_cow<'a, R: Replacer>(&self, text: Cow<'a, str>, replace: R) -> Cow<'a, str>;
}

impl CowRegex for Regex {
    fn replace_all_cow<'a, R: Replacer>(&self, text: Cow<'a, str>, replace: R) -> Cow<'a, str> {
        match text {
            Cow::Borrowed(find) => self.replace_all(find, replace),
            Cow::Owned(find) => Cow::Owned(self.replace_all(&find, replace).into_owned()),
        }
    }
}

/// A list of preprocessors that normalize text without removing anything
/// substantial. These operate on one line at a time.
pub const PREPROC_NORMALIZE: [&PreprocFn; 6] = [
    &normalize_unicode,
    &remove_junk,
    &blackbox_urls,
    &normalize_horizontal_whitespace,
    &normalize_punctuation,
    &trim,
];

/// A list of preprocessors that more aggressively normalize/mangle text
/// to make for friendlier matching. May remove statements and lines, and
/// more heavily normalize punctuation.
pub const PREPROC_AGGRESSIVE: [&PreprocFn; 8] = [
    &remove_common_tokens,
    &normalize_vertical_whitespace,
    &remove_punctuation,
    &lowercaseify,
    &remove_title_line,
    &remove_copyright_statements,
    &collapse_whitespace,
    &trim,
];

pub fn apply_normalizers(text: &str) -> Vec<String> {
    let mut lines = Vec::new();
    for line in text.split('\n') {
        let mut out = Cow::from(line);
        for preproc in &PREPROC_NORMALIZE {
            out = preproc(out);
        }
        lines.push(out.into());
    }
    lines
}

pub fn apply_aggressive(text: &str) -> String {
    let mut out = text.into();
    for preproc in &PREPROC_AGGRESSIVE {
        out = preproc(out);
    }
    out.into()
}

// Line-by-line normalizers

fn normalize_unicode(input: Cow<'_, str>) -> Cow<'_, str> {
    input.nfc().collect::<String>().into()
}

fn remove_junk(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[^\w\s\pP]+").unwrap());

    RX.replace_all_cow(input, "")
}

fn blackbox_urls(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"https?://\S+").unwrap());

    RX.replace_all_cow(input, "http://blackboxed/url")
}

fn normalize_horizontal_whitespace(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"(?x)[ \t\p{Zs} \\ / \| \x2044 ]+").unwrap());

    RX.replace_all_cow(input, " ")
}

fn normalize_punctuation(input: Cow<'_, str>) -> Cow<'_, str> {
    struct Rx {
        quotes: Regex,
        dash: Regex,
        open: Regex,
        close: Regex,
        under: Regex,
        copy: Regex,
    }
    static RX: LazyLock<Rx> = LazyLock::new(|| Rx {
        quotes: Regex::new(r#"["'\p{Pi}\p{Pf}]+"#).unwrap(),
        dash: Regex::new(r"\p{Pd}+").unwrap(),
        open: Regex::new(r"\p{Ps}+").unwrap(),
        close: Regex::new(r"\p{Pe}+").unwrap(),
        under: Regex::new(r"\p{Pc}+").unwrap(),
        copy: Regex::new(r"[©Ⓒⓒ]").unwrap(),
    });

    let mut out = input;
    let rx = &RX;
    out = rx.quotes.replace_all_cow(out, "'");
    out = rx.dash.replace_all_cow(out, "-");
    out = rx.open.replace_all_cow(out, "(");
    out = rx.close.replace_all_cow(out, ")");
    out = rx.under.replace_all_cow(out, "_");
    rx.copy.replace_all_cow(out, "(c)")
}

fn trim(input: Cow<'_, str>) -> Cow<'_, str> {
    match input {
        Cow::Borrowed(text) => text.trim().into(),
        Cow::Owned(text) => Cow::Owned(text.trim().to_owned()),
    }
}

// Aggressive preprocessors

// Cut prefix of string near given byte index.
// If given index doesn't lie at char boundary,
// returns the biggest prefix with length not exceeding idx.
// If index is bigger than length or string, returns the whole string.
fn trim_byte_adjusted(s: &str, idx: usize) -> &str {
    if idx >= s.len() {
        return s;
    }

    if let Some(sub) = s.get(..idx) {
        sub
    } else {
        // Inspect bytes before index
        let trailing_continuation = s.as_bytes()[..idx]
            .iter()
            .rev()
            // Multibyte characters are encoded in UTF-8 in the following manner:
            //    first byte | rest of bytes
            //    1..10xxxxx   10xxxxxx
            //    ^^^^ number of ones is equal to number of bytes in codepoint
            // Number of 10xxxxxx bytes in codepoint is at most 3 in valid UTF-8-encoded string,
            // so this loop actually runs a little iterations
            .take_while(|&byte| byte & 0b1100_0000 == 0b1000_0000)
            .count();
        // Subtract 1 to take the first byte in codepoint into account
        &s[..idx - trailing_continuation - 1]
    }
}

fn lcs_substr<'a>(f_line: &'a str, s_line: &'a str) -> &'a str {
    // find the length of common prefix in byte representations of strings
    let prefix_len = f_line
        .as_bytes()
        .iter()
        .zip(s_line.as_bytes())
        .take_while(|&(&f, &s)| f == s)
        .count();

    trim_byte_adjusted(f_line, prefix_len).trim()
}

fn remove_common_tokens(input: Cow<'_, str>) -> Cow<'_, str> {
    let mut l_iter = input.split('\n');

    let mut prefix_counts = HashMap::<_, u32>::new();

    // pass 1: iterate through the text to record common prefixes
    if let Some(first) = l_iter.next() {
        let mut pair = ("", first);
        let line_pairs = std::iter::from_fn(|| {
            pair = (pair.1, l_iter.next()?);
            Some(pair)
        });
        for (a, b) in line_pairs {
            let common = lcs_substr(a, b);

            // why start at 1, then immediately add 1?
            // lcs_substr compares two lines!
            // this doesn't need to be exact, just consistent.
            if common.len() > 3 {
                *prefix_counts.entry(common).or_insert(1) += 1;
            }
        }
    }

    // look at the most common observed prefix
    let most_common = match prefix_counts.iter().max_by_key(|&(_k, v)| v) {
        Some((prefix, _count)) => prefix,
        None => return input,
    };

    // reconcile the count with other longer prefixes that may be stored
    let common_count = prefix_counts
        .iter()
        .filter_map(|(s, count)| Some(count).filter(|_| s.starts_with(most_common)))
        .sum::<u32>();

    let line_count = input.split('\n').count();

    // the common string must be at least 80% of the text
    let prefix_threshold = (0.8f32 * line_count as f32) as _;
    if common_count < prefix_threshold {
        return input;
    }

    // pass 2: remove that substring
    let mut rem = String::with_capacity(input.len());
    for line in input.split('\n') {
        rem.push_str(line.strip_prefix(most_common).unwrap_or(line).trim());
        rem.push('\n');
    }

    // pop trailing newline
    rem.pop();
    rem.into()
}

fn normalize_vertical_whitespace(input: Cow<'_, str>) -> Cow<'_, str> {
    struct Rx {
        misc: Regex,
        num: Regex,
    }
    static RX: LazyLock<Rx> = LazyLock::new(|| Rx {
        misc: Regex::new(r"[\r\n\v\f]").unwrap(),
        num: Regex::new(r"\n{3,}").unwrap(),
    });

    let mut out = input;
    let rx = &RX;
    out = rx.misc.replace_all_cow(out, "\n");
    rx.num.replace_all_cow(out, "\n\n")
}

fn remove_punctuation(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[^\w\s]+").unwrap());

    RX.replace_all_cow(input, "")
}

fn lowercaseify(input: Cow<'_, str>) -> Cow<'_, str> {
    input.to_lowercase().into()
}

fn remove_title_line(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"^.*license( version \S+)?( copyright.*)?\n\n").unwrap());

    RX.replace_all_cow(input, "")
}

fn remove_copyright_statements(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> = LazyLock::new(|| {
        Regex::new(
            r"(?mx)
            (
                # either a new paragraph, or the beginning of the text + empty lines
                (\n\n|\A\n*)
                # any number of lines starting with 'copyright' followed by a new paragraph
                (^\x20*copyright.*?$)+
                \n\n
            )
            |
            (
                # or the very first line if it has 'copyright' in it
                \A.*copyright.*$
            )
            |
            (
                # or any lines that really look like a copyright statement
                ^copyright (\s+(c|\d+))+ .*?$
            )
        ",
        )
        .unwrap()
    });

    RX.replace_all_cow(input, "\n\n")
}

fn collapse_whitespace(input: Cow<'_, str>) -> Cow<'_, str> {
    static RX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
    RX.replace_all_cow(input, " ")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn trim_byte_adjusted_respects_multibyte_characters() {
        let input = "RustКраб橙蟹🦀";
        let expected = [
            "",
            "R",
            "Ru",
            "Rus",
            "Rust",
            "Rust",
            "RustК",
            "RustК",
            "RustКр",
            "RustКр",
            "RustКра",
            "RustКра",
            "RustКраб",
            "RustКраб",
            "RustКраб",
            "RustКраб橙",
            "RustКраб橙",
            "RustКраб橙",
            "RustКраб橙蟹",
            "RustКраб橙蟹",
            "RustКраб橙蟹",
            "RustКраб橙蟹",
            "RustКраб橙蟹🦀",
        ];

        for (i, &outcome) in expected.iter().enumerate() {
            assert_eq!(outcome, trim_byte_adjusted(input, i));
        }
    }

    #[test]
    fn greatest_substring_removal() {
        // the funky string syntax \n\ is to add a newline but skip the
        // leading whitespace in the source code
        let text = "%%Copyright: Copyright\n\
                    %%Copyright: All rights reserved.\n\
                    %%Copyright: Redistribution and use in source and binary forms, with or\n\
                    %%Copyright: without modification, are permitted provided that the\n\
                    %%Copyright: following conditions are met:\n\
                    \n\
                    abcd";

        let new_text = remove_common_tokens(text.into());
        println!("{}", new_text);

        assert!(
            !new_text.contains("%%Copyright"),
            "new text shouldn't contain the common substring"
        );
    }

    #[test]
    fn greatest_substring_removal_keep_inner() {
        let text = "this string should still have\n\
                    this word -> this <- in it even though\n\
                    this is still the most common word";
        let new_text = remove_common_tokens(text.into());
        println!("-- {}", new_text);
        // the "this" at the start of the line can be discarded...
        assert!(!new_text.contains("\nthis"));
        // ...but the "this" in the middle of sentences shouldn't be
        assert!(new_text.contains("this"));

        let text = "aaaa bbbb cccc dddd\n\
                    eeee ffff aaaa gggg\n\
                    hhhh iiii jjjj";
        let new_text = remove_common_tokens(text.into());
        println!("-- {}", new_text);
        assert!(new_text.contains("aaaa")); // similar to above test
    }

    #[test]
    fn greatest_substring_removal_42() {
        // https://github.com/jpeddicord/askalono/issues/42
        let text = "AAAAAA line 1\n\
                    AAAAAA another line here\n\
                    AAAAAA yet another line here\n\
                    AAAAAA how long will this go on\n\
                    AAAAAA another line here\n\
                    AAAAAA more\n\
                    AAAAAA one more\n\
                    AAAAAA two more\n\
                    AAAAAA three more\n\
                    AAAAAA four more\n\
                    AAAAAA five more\n\
                    AAAAAA six more\n\
                    \n\
                    preserve\n\
                    keep";
        let new_text = remove_common_tokens(text.into());
        println!("{}", new_text);

        assert!(new_text.contains("preserve"));
        assert!(new_text.contains("keep"));
        assert!(!new_text.contains("AAAAAA"));
    }

    #[test]
    fn normalize_no_line_mangle() {
        let text = "some license

        copyright 2012 person

        \tlicense\r
        text

        \t



        goes
        here";

        let text_lines = text.lines().count();

        let normalized = apply_normalizers(text);
        let normalized_lines = normalized.len();

        assert_eq!(
            text_lines, normalized_lines,
            "normalizers shouldnt change line counts"
        );
    }
}