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
use std::ops::Sub;
use line_numbers::LineNumber;
pub(crate) fn format_line_num(line_num: LineNumber) -> String {
format!("{} ", line_num.display())
}
#[derive(Debug, PartialEq, Clone, Copy)]
struct LinePosition {
pub(crate) line: LineNumber,
column: usize,
}
pub(crate) fn byte_len(s: &str) -> usize {
s.len()
}
pub(crate) trait MaxLine {
fn max_line(&self) -> LineNumber;
}
impl<S: AsRef<str>> MaxLine for S {
fn max_line(&self) -> LineNumber {
(self
.as_ref()
.trim_end()
.split('\n')
.count() as u32)
.sub(1)
.into()
}
}
pub(crate) fn is_all_whitespace(s: &str) -> bool {
s.chars().all(|c| c.is_whitespace())
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn str_max_line() {
let line: String = "foo\nbar".into();
assert_eq!(line.max_line().0, 1);
}
#[test]
fn empty_str_max_line() {
let line: String = "".into();
assert_eq!(line.max_line().0, 0);
}
#[test]
fn str_max_line_trailing_newline() {
let line: String = "foo\nbar\n".into();
assert_eq!(line.max_line().0, 1);
}
#[test]
fn str_max_line_extra_trailing_newline() {
let line: String = "foo\nbar\n\n".into();
assert_eq!(line.max_line().0, 1);
}
#[test]
fn test_is_all_whiteapce() {
assert!(is_all_whitespace(" \n\t"));
}
}