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
pub fn has_new_line_occurrences_in_leading_whitespace(text: &str, occurrences: i8) -> bool {
if occurrences == 0 {
return has_no_new_lines_in_leading_whitespace(text);
}
let mut found_occurrences = 0;
for c in text.chars() {
if !c.is_whitespace() {
return false;
}
if c == '\n' {
found_occurrences += 1;
if found_occurrences >= occurrences {
return true;
}
}
}
return false;
}
pub fn has_no_new_lines_in_leading_whitespace(text: &str) -> bool {
for c in text.chars() {
if !c.is_whitespace() {
return true;
}
if c == '\n' {
return false;
}
}
return true;
}
pub fn has_new_line_occurrences_in_trailing_whitespace(text: &str, occurrences: i8) -> bool {
if occurrences == 0 {
return has_no_new_lines_in_trailing_whitespace(text);
}
let mut found_occurrences = 0;
for c in text.chars().rev() {
if !c.is_whitespace() {
return false;
}
if c == '\n' {
found_occurrences += 1;
if found_occurrences >= occurrences {
return true;
}
}
}
return false;
}
pub fn has_no_new_lines_in_trailing_whitespace(text: &str) -> bool {
for c in text.chars().rev() {
if !c.is_whitespace() {
return true;
}
if c == '\n' {
return false;
}
}
return true;
}
pub fn get_line_number_of_pos(text: &str, pos: usize) -> usize {
let text_bytes = text.as_bytes();
let mut line_count = 1;
for i in 0..pos {
if text_bytes.get(i) == Some(&('\n' as u8)) {
line_count += 1;
}
}
line_count
}
pub fn get_column_number_of_pos(text: &str, pos: usize) -> usize {
let text_bytes = text.as_bytes();
let line_start_byte_pos = get_line_start_byte_pos(pos, &text_bytes);
return text[line_start_byte_pos..pos].chars().count() + 1;
fn get_line_start_byte_pos(pos: usize, text_bytes: &[u8]) -> usize {
for i in (0..pos).rev() {
if text_bytes.get(i) == Some(&('\n' as u8)) {
return i + 1;
}
}
0
}
}