-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix.rs
More file actions
94 lines (78 loc) · 2.04 KB
/
Copy pathunix.rs
File metadata and controls
94 lines (78 loc) · 2.04 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
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
#![cfg(unix)]
fn maybe_special(last: u8) -> Option<u8> {
match last as char {
'/' => match fastrand::u8(0..6) {
0..2 => Some(b'.'),
2..5 => None,
_ => Some(b'/'),
},
'.' => match fastrand::u8(0..3) {
0 => Some(b'.'),
1 => None,
_ => Some(b'/'),
},
_ => match fastrand::u8(0..6) {
0 => Some(b'/'),
_ => None,
},
}
}
fn maybe_norm_special(last: u8) -> Option<u8> {
match last as char {
'/' => match fastrand::u8(0..3) {
0 => Some(b'.'),
_ => None,
},
'.' => match fastrand::bool() {
true => Some(b'.'),
false => None,
},
_ => match fastrand::u8(0..6) {
0 => Some(b'/'),
_ => None,
},
}
}
pub fn relative(len: usize) -> String {
if len == 0 {
return String::new();
}
let mut raw = super::alphanumeric(len).into_bytes();
let init = match fastrand::bool() {
true => b'.',
false => b'x',
};
raw[0] = init;
if let Some(raw) = raw.get_mut(1..) {
super::rewrite(raw, init, maybe_special);
}
String::from_utf8(raw).unwrap()
}
pub fn absolute(len: usize) -> String {
assert!(len > 0);
let mut raw = super::alphanumeric(len).into_bytes();
raw[0] = b'/';
if let Some(raw) = raw.get_mut(1..) {
super::rewrite(raw, b'/', maybe_special);
}
String::from_utf8(raw).unwrap()
}
pub fn common(len: usize) -> String {
match fastrand::bool() {
true => absolute(len),
false => relative(len),
}
}
pub fn normal(len: usize) -> String {
assert!(len > 0);
let mut raw = super::alphanumeric(len).into_bytes();
raw[0] = b'/';
let last = match raw.get_mut(1..) {
Some(raw) => super::rewrite(raw, b'/', maybe_norm_special),
None => b'/',
};
if len != 1 && matches!(last, b'/' | b'.') {
raw[len - 1] = b'x';
}
String::from_utf8(raw).unwrap()
}