forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.rs
More file actions
62 lines (50 loc) · 1.82 KB
/
Copy pathnormalize.rs
File metadata and controls
62 lines (50 loc) · 1.82 KB
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
use std::borrow::Cow;
use std::path::Path;
use common::paths::normalize_slashes;
pub fn normalize_newlines(input: &str) -> Cow<'_, str> {
if !input.contains('\r') {
return Cow::Borrowed(input);
}
let mut normalized = input.replace("\r\n", "\n");
normalized = normalized.replace('\r', "\n");
Cow::Owned(normalized)
}
pub fn normalize_path_separators(input: &str) -> String {
let mut normalized = String::with_capacity(input.len());
let mut token_start = None;
for (idx, ch) in input.char_indices() {
if ch.is_whitespace() {
if let Some(start) = token_start.take() {
push_normalized_token(&mut normalized, &input[start..idx]);
}
normalized.push(ch);
} else if token_start.is_none() {
token_start = Some(idx);
}
}
if let Some(start) = token_start {
push_normalized_token(&mut normalized, &input[start..]);
}
normalized
}
pub fn replace_path_token(text: &str, path: &Path, token: &str) -> String {
fn file_url_prefix(path: &str) -> String {
format!("file:///{}", path.trim_start_matches('/'))
}
let native_path = path.to_string_lossy();
let normalized_path = normalize_slashes(native_path.as_ref());
let file_url = file_url_prefix(&normalized_path);
let file_url_token = format!("file://{token}");
let mut output = text.replace(&format!("{file_url}/"), &format!("{file_url_token}/"));
output = output.replace(&file_url, &file_url_token);
output = output.replace(native_path.as_ref(), token);
output = output.replace(&normalized_path, token);
output
}
fn push_normalized_token(output: &mut String, token: &str) {
if token.contains('\\') {
output.push_str(&normalize_slashes(token));
} else {
output.push_str(token);
}
}