-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.rs
More file actions
62 lines (57 loc) · 1.61 KB
/
string.rs
File metadata and controls
62 lines (57 loc) · 1.61 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
/// Format docstring. The assumption is that docstrings are written in
/// the following format:
///
/// ```ignore
/// f = (x: Int) =>
/// "This is a function that does stuff.
///
/// # Args
///
/// - x: A number for controlling how the stuff is done
///
/// "
/// ```
///
/// Leading and trailing blank lines will be removed and the content
/// will be dedented such that the output will be formatted like this:
///
/// ```ignore
/// This is a function that does stuff.
///
/// # Args
///
/// - x: A number for controlling how the stuff is done
/// ```
pub fn format_doc(string: &str) -> String {
let string = string.trim();
let mut new_string = String::with_capacity(string.len());
let mut shortest_prefix = usize::MAX;
// Find the shortest prefix
for line in string.lines().skip(1) {
let trimmed_line = line.trim_start();
if !trimmed_line.is_empty() {
let prefix_len = line.len() - trimmed_line.len();
if prefix_len < shortest_prefix {
shortest_prefix = prefix_len;
}
}
}
if shortest_prefix == usize::MAX {
shortest_prefix = 0;
}
let prefix = " ".repeat(shortest_prefix);
// Trim the shortest prefix from all non-empty lines.
for line in string.lines() {
if line.trim().is_empty() {
new_string.push('\n');
} else {
if let Some(line) = line.strip_prefix(&prefix) {
new_string.push_str(line);
} else {
new_string.push_str(line);
}
new_string.push('\n');
}
}
new_string
}