1 // Copyright (C) 2025 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::{
16 ffi::{OsStr, OsString},
17 path::Path,
18 };
19
strip_punctuation(text: &str) -> String20 pub(crate) fn strip_punctuation(text: &str) -> String {
21 let lowercase = text.to_lowercase();
22 let mut processed = String::with_capacity(lowercase.len());
23 for c in lowercase.chars() {
24 if c.is_alphanumeric() || c == '.' {
25 processed.push(c)
26 } else if !processed.ends_with(' ') {
27 processed.push(' ')
28 }
29 }
30 processed.trim().to_string()
31 }
32
normalize_filename(file: impl AsRef<Path>) -> OsString33 pub(crate) fn normalize_filename(file: impl AsRef<Path>) -> OsString {
34 // File should be relative
35 let file = file.as_ref();
36 let mut basename = if file.extension() == Some(OsStr::new("txt"))
37 || file.extension() == Some(OsStr::new("md"))
38 {
39 file.with_extension("")
40 } else {
41 file.to_owned()
42 };
43 let uppercase_name = basename.as_mut_os_string();
44 uppercase_name.make_ascii_uppercase();
45 uppercase_name.to_owned()
46 }
47
48 #[cfg(test)]
49 mod tests {
50 use super::*;
51
52 #[test]
test_strip_punctuation()53 fn test_strip_punctuation() {
54 assert_eq!(strip_punctuation("FOO BAR"), "foo bar", "Converted to lowercase");
55 assert_eq!(strip_punctuation("foo, bar"), "foo bar", "Punctuation removed");
56 assert_eq!(strip_punctuation("foo. bar"), "foo. bar", "Periods preserved");
57 assert_eq!(
58 strip_punctuation(" foo bar "),
59 "foo bar",
60 "Leading and trailing whitespace stripped"
61 );
62 assert_eq!(
63 strip_punctuation(" foo\n\n\n\nbar "),
64 "foo bar",
65 "Multiple whitespace replaced with single space"
66 );
67 }
68
69 #[test]
test_normalize_filename()70 fn test_normalize_filename() {
71 assert_eq!(normalize_filename(Path::new("LICENSE")), OsString::from("LICENSE"));
72 assert_eq!(
73 normalize_filename(Path::new("LICENSE.txt")),
74 OsString::from("LICENSE"),
75 ".txt extension removed"
76 );
77 assert_eq!(
78 normalize_filename(Path::new("LICENSE.md")),
79 OsString::from("LICENSE"),
80 ".md extension removed"
81 );
82 assert_eq!(
83 normalize_filename(Path::new("LICENSE.jpg")),
84 OsString::from("LICENSE.JPG"),
85 "Other extensions preserved"
86 );
87 assert_eq!(
88 normalize_filename(Path::new("license")),
89 OsString::from("LICENSE"),
90 "Converted to uppercase"
91 );
92 }
93 }
94